Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / video / zoran_device.c
blobf97c206920579997c5e003c70ab9afb84998a9e9
1 /*
2 * Zoran zr36057/zr36067 PCI controller driver, for the
3 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
4 * Media Labs LML33/LML33R10.
6 * This part handles device access (PCI/I2C/codec/...)
8 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
10 * Currently maintained by:
11 * Ronald Bultje <rbultje@ronald.bitfreak.net>
12 * Laurent Pinchart <laurent.pinchart@skynet.be>
13 * Mailinglist <mjpeg-users@lists.sf.net>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/vmalloc.h>
34 #include <linux/byteorder/generic.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/i2c.h>
39 #include <linux/i2c-algo-bit.h>
40 #include <linux/videodev.h>
41 #include <linux/spinlock.h>
42 #include <linux/sem.h>
44 #include <linux/pci.h>
45 #include <linux/video_decoder.h>
46 #include <linux/video_encoder.h>
47 #include <linux/delay.h>
48 #include <linux/wait.h>
50 #include <asm/io.h>
52 #include "videocodec.h"
53 #include "zoran.h"
54 #include "zoran_device.h"
55 #include "zoran_card.h"
57 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | \
58 ZR36057_ISR_GIRQ1 | \
59 ZR36057_ISR_JPEGRepIRQ )
61 extern const struct zoran_format zoran_formats[];
63 static int lml33dpath = 0; /* 1 will use digital path in capture
64 * mode instead of analog. It can be
65 * used for picture adjustments using
66 * tool like xawtv while watching image
67 * on TV monitor connected to the output.
68 * However, due to absence of 75 Ohm
69 * load on Bt819 input, there will be
70 * some image imperfections */
72 module_param(lml33dpath, bool, 0644);
73 MODULE_PARM_DESC(lml33dpath,
74 "Use digital path capture mode (on LML33 cards)");
76 static void
77 zr36057_init_vfe (struct zoran *zr);
80 * General Purpose I/O and Guest bus access
84 * This is a bit tricky. When a board lacks a GPIO function, the corresponding
85 * GPIO bit number in the card_info structure is set to 0.
88 void
89 GPIO (struct zoran *zr,
90 int bit,
91 unsigned int value)
93 u32 reg;
94 u32 mask;
96 /* Make sure the bit number is legal
97 * A bit number of -1 (lacking) gives a mask of 0,
98 * making it harmless */
99 mask = (1 << (24 + bit)) & 0xff000000;
100 reg = btread(ZR36057_GPPGCR1) & ~mask;
101 if (value) {
102 reg |= mask;
104 btwrite(reg, ZR36057_GPPGCR1);
105 udelay(1);
109 * Wait til post office is no longer busy
113 post_office_wait (struct zoran *zr)
115 u32 por;
117 // while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
118 while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
119 /* wait for something to happen */
121 if ((por & ZR36057_POR_POTime) && !zr->card.gws_not_connected) {
122 /* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
123 dprintk(1, KERN_INFO "%s: pop timeout %08x\n", ZR_DEVNAME(zr),
124 por);
125 return -1;
128 return 0;
132 post_office_write (struct zoran *zr,
133 unsigned int guest,
134 unsigned int reg,
135 unsigned int value)
137 u32 por;
139 por =
140 ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
141 ((reg & 7) << 16) | (value & 0xFF);
142 btwrite(por, ZR36057_POR);
144 return post_office_wait(zr);
148 post_office_read (struct zoran *zr,
149 unsigned int guest,
150 unsigned int reg)
152 u32 por;
154 por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
155 btwrite(por, ZR36057_POR);
156 if (post_office_wait(zr) < 0) {
157 return -1;
160 return btread(ZR36057_POR) & 0xFF;
164 * detect guests
167 static void
168 dump_guests (struct zoran *zr)
170 if (zr36067_debug > 2) {
171 int i, guest[8];
173 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
174 guest[i] = post_office_read(zr, i, 0);
177 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
179 for (i = 1; i < 8; i++) {
180 printk(" 0x%02x", guest[i]);
182 printk("\n");
186 static inline unsigned long
187 get_time (void)
189 struct timeval tv;
191 do_gettimeofday(&tv);
192 return (1000000 * tv.tv_sec + tv.tv_usec);
195 void
196 detect_guest_activity (struct zoran *zr)
198 int timeout, i, j, res, guest[8], guest0[8], change[8][3];
199 unsigned long t0, t1;
201 dump_guests(zr);
202 printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
203 ZR_DEVNAME(zr));
204 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
205 guest0[i] = guest[i] = post_office_read(zr, i, 0);
208 timeout = 0;
209 j = 0;
210 t0 = get_time();
211 while (timeout < 10000) {
212 udelay(10);
213 timeout++;
214 for (i = 1; (i < 8) && (j < 8); i++) {
215 res = post_office_read(zr, i, 0);
216 if (res != guest[i]) {
217 t1 = get_time();
218 change[j][0] = (t1 - t0);
219 t0 = t1;
220 change[j][1] = i;
221 change[j][2] = res;
222 j++;
223 guest[i] = res;
226 if (j >= 8)
227 break;
229 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
231 for (i = 1; i < 8; i++) {
232 printk(" 0x%02x", guest0[i]);
234 printk("\n");
235 if (j == 0) {
236 printk(KERN_INFO "%s: No activity detected.\n", ZR_DEVNAME(zr));
237 return;
239 for (i = 0; i < j; i++) {
240 printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", ZR_DEVNAME(zr),
241 change[i][0], change[i][1], change[i][2]);
246 * JPEG Codec access
249 void
250 jpeg_codec_sleep (struct zoran *zr,
251 int sleep)
253 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_SLEEP], !sleep);
254 if (!sleep) {
255 dprintk(3,
256 KERN_DEBUG
257 "%s: jpeg_codec_sleep() - wake GPIO=0x%08x\n",
258 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
259 udelay(500);
260 } else {
261 dprintk(3,
262 KERN_DEBUG
263 "%s: jpeg_codec_sleep() - sleep GPIO=0x%08x\n",
264 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
265 udelay(2);
270 jpeg_codec_reset (struct zoran *zr)
272 /* Take the codec out of sleep */
273 jpeg_codec_sleep(zr, 0);
275 if (zr->card.gpcs[GPCS_JPEG_RESET] != 0xff) {
276 post_office_write(zr, zr->card.gpcs[GPCS_JPEG_RESET], 0,
278 udelay(2);
279 } else {
280 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 0);
281 udelay(2);
282 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 1);
283 udelay(2);
286 return 0;
290 * Set the registers for the size we have specified. Don't bother
291 * trying to understand this without the ZR36057 manual in front of
292 * you [AC].
294 * PS: The manual is free for download in .pdf format from
295 * www.zoran.com - nicely done those folks.
298 static void
299 zr36057_adjust_vfe (struct zoran *zr,
300 enum zoran_codec_mode mode)
302 u32 reg;
304 switch (mode) {
305 case BUZ_MODE_MOTION_DECOMPRESS:
306 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
307 reg = btread(ZR36057_VFEHCR);
308 if ((reg & (1 << 10)) && zr->card.type != LML33R10) {
309 reg += ((1 << 10) | 1);
311 btwrite(reg, ZR36057_VFEHCR);
312 break;
313 case BUZ_MODE_MOTION_COMPRESS:
314 case BUZ_MODE_IDLE:
315 default:
316 if (zr->norm == VIDEO_MODE_NTSC ||
317 (zr->card.type == LML33R10 &&
318 zr->norm == VIDEO_MODE_PAL))
319 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
320 else
321 btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
322 reg = btread(ZR36057_VFEHCR);
323 if (!(reg & (1 << 10)) && zr->card.type != LML33R10) {
324 reg -= ((1 << 10) | 1);
326 btwrite(reg, ZR36057_VFEHCR);
327 break;
332 * set geometry
335 static void
336 zr36057_set_vfe (struct zoran *zr,
337 int video_width,
338 int video_height,
339 const struct zoran_format *format)
341 struct tvnorm *tvn;
342 unsigned HStart, HEnd, VStart, VEnd;
343 unsigned DispMode;
344 unsigned VidWinWid, VidWinHt;
345 unsigned hcrop1, hcrop2, vcrop1, vcrop2;
346 unsigned Wa, We, Ha, He;
347 unsigned X, Y, HorDcm, VerDcm;
348 u32 reg;
349 unsigned mask_line_size;
351 tvn = zr->timing;
353 Wa = tvn->Wa;
354 Ha = tvn->Ha;
356 dprintk(2, KERN_INFO "%s: set_vfe() - width = %d, height = %d\n",
357 ZR_DEVNAME(zr), video_width, video_height);
359 if (zr->norm != VIDEO_MODE_PAL &&
360 zr->norm != VIDEO_MODE_NTSC &&
361 zr->norm != VIDEO_MODE_SECAM) {
362 dprintk(1,
363 KERN_ERR "%s: set_vfe() - norm = %d not valid\n",
364 ZR_DEVNAME(zr), zr->norm);
365 return;
367 if (video_width < BUZ_MIN_WIDTH ||
368 video_height < BUZ_MIN_HEIGHT ||
369 video_width > Wa || video_height > Ha) {
370 dprintk(1, KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
371 ZR_DEVNAME(zr), video_width, video_height);
372 return;
375 /**** zr36057 ****/
377 /* horizontal */
378 VidWinWid = video_width;
379 X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
380 We = (VidWinWid * 64) / X;
381 HorDcm = 64 - X;
382 hcrop1 = 2 * ((tvn->Wa - We) / 4);
383 hcrop2 = tvn->Wa - We - hcrop1;
384 HStart = tvn->HStart ? tvn->HStart : 1;
385 /* (Ronald) Original comment:
386 * "| 1 Doesn't have any effect, tested on both a DC10 and a DC10+"
387 * this is false. It inverses chroma values on the LML33R10 (so Cr
388 * suddenly is shown as Cb and reverse, really cool effect if you
389 * want to see blue faces, not useful otherwise). So don't use |1.
390 * However, the DC10 has '0' as HStart, but does need |1, so we
391 * use a dirty check...
393 HEnd = HStart + tvn->Wa - 1;
394 HStart += hcrop1;
395 HEnd -= hcrop2;
396 reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
397 | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
398 if (zr->card.vfe_pol.hsync_pol)
399 reg |= ZR36057_VFEHCR_HSPol;
400 btwrite(reg, ZR36057_VFEHCR);
402 /* Vertical */
403 DispMode = !(video_height > BUZ_MAX_HEIGHT / 2);
404 VidWinHt = DispMode ? video_height : video_height / 2;
405 Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
406 He = (VidWinHt * 64) / Y;
407 VerDcm = 64 - Y;
408 vcrop1 = (tvn->Ha / 2 - He) / 2;
409 vcrop2 = tvn->Ha / 2 - He - vcrop1;
410 VStart = tvn->VStart;
411 VEnd = VStart + tvn->Ha / 2; // - 1; FIXME SnapShot times out with -1 in 768*576 on the DC10 - LP
412 VStart += vcrop1;
413 VEnd -= vcrop2;
414 reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
415 | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
416 if (zr->card.vfe_pol.vsync_pol)
417 reg |= ZR36057_VFEVCR_VSPol;
418 btwrite(reg, ZR36057_VFEVCR);
420 /* scaler and pixel format */
421 reg = 0;
422 reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
423 reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
424 reg |= (DispMode << ZR36057_VFESPFR_DispMode);
425 /* RJ: I don't know, why the following has to be the opposite
426 * of the corresponding ZR36060 setting, but only this way
427 * we get the correct colors when uncompressing to the screen */
428 //reg |= ZR36057_VFESPFR_VCLKPol; /**/
429 /* RJ: Don't know if that is needed for NTSC also */
430 if (zr->norm != VIDEO_MODE_NTSC)
431 reg |= ZR36057_VFESPFR_ExtFl; // NEEDED!!!!!!! Wolfgang
432 reg |= ZR36057_VFESPFR_TopField;
433 if (HorDcm >= 48) {
434 reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
435 } else if (HorDcm >= 32) {
436 reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
437 } else if (HorDcm >= 16) {
438 reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
440 reg |= format->vfespfr;
441 btwrite(reg, ZR36057_VFESPFR);
443 /* display configuration */
444 reg = (16 << ZR36057_VDCR_MinPix)
445 | (VidWinHt << ZR36057_VDCR_VidWinHt)
446 | (VidWinWid << ZR36057_VDCR_VidWinWid);
447 if (pci_pci_problems & PCIPCI_TRITON)
448 // || zr->revision < 1) // Revision 1 has also Triton support
449 reg &= ~ZR36057_VDCR_Triton;
450 else
451 reg |= ZR36057_VDCR_Triton;
452 btwrite(reg, ZR36057_VDCR);
454 /* (Ronald) don't write this if overlay_mask = NULL */
455 if (zr->overlay_mask) {
456 /* Write overlay clipping mask data, but don't enable overlay clipping */
457 /* RJ: since this makes only sense on the screen, we use
458 * zr->overlay_settings.width instead of video_width */
460 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
461 reg = virt_to_bus(zr->overlay_mask);
462 btwrite(reg, ZR36057_MMTR);
463 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
464 btwrite(reg, ZR36057_MMBR);
465 reg =
466 mask_line_size - (zr->overlay_settings.width +
467 31) / 32;
468 if (DispMode == 0)
469 reg += mask_line_size;
470 reg <<= ZR36057_OCR_MaskStride;
471 btwrite(reg, ZR36057_OCR);
474 zr36057_adjust_vfe(zr, zr->codec_mode);
478 * Switch overlay on or off
481 void
482 zr36057_overlay (struct zoran *zr,
483 int on)
485 u32 reg;
487 if (on) {
488 /* do the necessary settings ... */
489 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
491 zr36057_set_vfe(zr,
492 zr->overlay_settings.width,
493 zr->overlay_settings.height,
494 zr->overlay_settings.format);
496 /* Start and length of each line MUST be 4-byte aligned.
497 * This should be allready checked before the call to this routine.
498 * All error messages are internal driver checking only! */
500 /* video display top and bottom registers */
501 reg = (long) zr->buffer.base +
502 zr->overlay_settings.x *
503 ((zr->overlay_settings.format->depth + 7) / 8) +
504 zr->overlay_settings.y *
505 zr->buffer.bytesperline;
506 btwrite(reg, ZR36057_VDTR);
507 if (reg & 3)
508 dprintk(1,
509 KERN_ERR
510 "%s: zr36057_overlay() - video_address not aligned\n",
511 ZR_DEVNAME(zr));
512 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
513 reg += zr->buffer.bytesperline;
514 btwrite(reg, ZR36057_VDBR);
516 /* video stride, status, and frame grab register */
517 reg = zr->buffer.bytesperline -
518 zr->overlay_settings.width *
519 ((zr->overlay_settings.format->depth + 7) / 8);
520 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
521 reg += zr->buffer.bytesperline;
522 if (reg & 3)
523 dprintk(1,
524 KERN_ERR
525 "%s: zr36057_overlay() - video_stride not aligned\n",
526 ZR_DEVNAME(zr));
527 reg = (reg << ZR36057_VSSFGR_DispStride);
528 reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
529 btwrite(reg, ZR36057_VSSFGR);
531 /* Set overlay clipping */
532 if (zr->overlay_settings.clipcount > 0)
533 btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
535 /* ... and switch it on */
536 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
537 } else {
538 /* Switch it off */
539 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
544 * The overlay mask has one bit for each pixel on a scan line,
545 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
548 void
549 write_overlay_mask (struct file *file,
550 struct video_clip *vp,
551 int count)
553 struct zoran_fh *fh = file->private_data;
554 struct zoran *zr = fh->zr;
555 unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
556 u32 *mask;
557 int x, y, width, height;
558 unsigned i, j, k;
559 u32 reg;
561 /* fill mask with one bits */
562 memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
563 reg = 0;
565 for (i = 0; i < count; ++i) {
566 /* pick up local copy of clip */
567 x = vp[i].x;
568 y = vp[i].y;
569 width = vp[i].width;
570 height = vp[i].height;
572 /* trim clips that extend beyond the window */
573 if (x < 0) {
574 width += x;
575 x = 0;
577 if (y < 0) {
578 height += y;
579 y = 0;
581 if (x + width > fh->overlay_settings.width) {
582 width = fh->overlay_settings.width - x;
584 if (y + height > fh->overlay_settings.height) {
585 height = fh->overlay_settings.height - y;
588 /* ignore degenerate clips */
589 if (height <= 0) {
590 continue;
592 if (width <= 0) {
593 continue;
596 /* apply clip for each scan line */
597 for (j = 0; j < height; ++j) {
598 /* reset bit for each pixel */
599 /* this can be optimized later if need be */
600 mask = fh->overlay_mask + (y + j) * mask_line_size;
601 for (k = 0; k < width; ++k) {
602 mask[(x + k) / 32] &=
603 ~((u32) 1 << (x + k) % 32);
609 /* Enable/Disable uncompressed memory grabbing of the 36057 */
611 void
612 zr36057_set_memgrab (struct zoran *zr,
613 int mode)
615 if (mode) {
616 /* We only check SnapShot and not FrameGrab here. SnapShot==1
617 * means a capture is already in progress, but FrameGrab==1
618 * doesn't necessary mean that. It's more correct to say a 1
619 * to 0 transition indicates a capture completed. If a
620 * capture is pending when capturing is tuned off, FrameGrab
621 * will be stuck at 1 until capturing is turned back on.
623 if (btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot)
624 dprintk(1,
625 KERN_WARNING
626 "%s: zr36057_set_memgrab(1) with SnapShot on!?\n",
627 ZR_DEVNAME(zr));
629 /* switch on VSync interrupts */
630 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
631 btor(zr->card.vsync_int, ZR36057_ICR); // SW
633 /* enable SnapShot */
634 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
636 /* Set zr36057 video front end and enable video */
637 zr36057_set_vfe(zr, zr->v4l_settings.width,
638 zr->v4l_settings.height,
639 zr->v4l_settings.format);
641 zr->v4l_memgrab_active = 1;
642 } else {
643 /* switch off VSync interrupts */
644 btand(~zr->card.vsync_int, ZR36057_ICR); // SW
646 zr->v4l_memgrab_active = 0;
647 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
649 /* reenable grabbing to screen if it was running */
650 if (zr->v4l_overlay_active) {
651 zr36057_overlay(zr, 1);
652 } else {
653 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
654 btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
660 wait_grab_pending (struct zoran *zr)
662 unsigned long flags;
664 /* wait until all pending grabs are finished */
666 if (!zr->v4l_memgrab_active)
667 return 0;
669 wait_event_interruptible(zr->v4l_capq,
670 (zr->v4l_pend_tail == zr->v4l_pend_head));
671 if (signal_pending(current))
672 return -ERESTARTSYS;
674 spin_lock_irqsave(&zr->spinlock, flags);
675 zr36057_set_memgrab(zr, 0);
676 spin_unlock_irqrestore(&zr->spinlock, flags);
678 return 0;
681 /*****************************************************************************
683 * Set up the Buz-specific MJPEG part *
685 *****************************************************************************/
687 static inline void
688 set_frame (struct zoran *zr,
689 int val)
691 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_FRAME], val);
694 static void
695 set_videobus_dir (struct zoran *zr,
696 int val)
698 switch (zr->card.type) {
699 case LML33:
700 case LML33R10:
701 if (lml33dpath == 0)
702 GPIO(zr, 5, val);
703 else
704 GPIO(zr, 5, 1);
705 break;
706 default:
707 GPIO(zr, zr->card.gpio[ZR_GPIO_VID_DIR],
708 zr->card.gpio_pol[ZR_GPIO_VID_DIR] ? !val : val);
709 break;
713 static void
714 init_jpeg_queue (struct zoran *zr)
716 int i;
718 /* re-initialize DMA ring stuff */
719 zr->jpg_que_head = 0;
720 zr->jpg_dma_head = 0;
721 zr->jpg_dma_tail = 0;
722 zr->jpg_que_tail = 0;
723 zr->jpg_seq_num = 0;
724 zr->JPEG_error = 0;
725 zr->num_errors = 0;
726 zr->jpg_err_seq = 0;
727 zr->jpg_err_shift = 0;
728 zr->jpg_queued_num = 0;
729 for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
730 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
732 for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
733 zr->stat_com[i] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
737 static void
738 zr36057_set_jpg (struct zoran *zr,
739 enum zoran_codec_mode mode)
741 struct tvnorm *tvn;
742 u32 reg;
744 tvn = zr->timing;
746 /* assert P_Reset, disable code transfer, deassert Active */
747 btwrite(0, ZR36057_JPC);
749 /* MJPEG compression mode */
750 switch (mode) {
752 case BUZ_MODE_MOTION_COMPRESS:
753 default:
754 reg = ZR36057_JMC_MJPGCmpMode;
755 break;
757 case BUZ_MODE_MOTION_DECOMPRESS:
758 reg = ZR36057_JMC_MJPGExpMode;
759 reg |= ZR36057_JMC_SyncMstr;
760 /* RJ: The following is experimental - improves the output to screen */
761 //if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
762 break;
764 case BUZ_MODE_STILL_COMPRESS:
765 reg = ZR36057_JMC_JPGCmpMode;
766 break;
768 case BUZ_MODE_STILL_DECOMPRESS:
769 reg = ZR36057_JMC_JPGExpMode;
770 break;
773 reg |= ZR36057_JMC_JPG;
774 if (zr->jpg_settings.field_per_buff == 1)
775 reg |= ZR36057_JMC_Fld_per_buff;
776 btwrite(reg, ZR36057_JMC);
778 /* vertical */
779 btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
780 reg = (6 << ZR36057_VSP_VsyncSize) |
781 (tvn->Ht << ZR36057_VSP_FrmTot);
782 btwrite(reg, ZR36057_VSP);
783 reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
784 (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
785 btwrite(reg, ZR36057_FVAP);
787 /* horizontal */
788 if (zr->card.vfe_pol.hsync_pol)
789 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
790 else
791 btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
792 reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
793 (tvn->Wt << ZR36057_HSP_LineTot);
794 btwrite(reg, ZR36057_HSP);
795 reg = ((zr->jpg_settings.img_x +
796 tvn->HStart + 4) << ZR36057_FHAP_NAX) |
797 (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
798 btwrite(reg, ZR36057_FHAP);
800 /* field process parameters */
801 if (zr->jpg_settings.odd_even)
802 reg = ZR36057_FPP_Odd_Even;
803 else
804 reg = 0;
806 btwrite(reg, ZR36057_FPP);
808 /* Set proper VCLK Polarity, else colors will be wrong during playback */
809 //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
811 /* code base address */
812 reg = virt_to_bus(zr->stat_com);
813 btwrite(reg, ZR36057_JCBA);
815 /* FIFO threshold (FIFO is 160. double words) */
816 /* NOTE: decimal values here */
817 switch (mode) {
819 case BUZ_MODE_STILL_COMPRESS:
820 case BUZ_MODE_MOTION_COMPRESS:
821 if (zr->card.type != BUZ)
822 reg = 140;
823 else
824 reg = 60;
825 break;
827 case BUZ_MODE_STILL_DECOMPRESS:
828 case BUZ_MODE_MOTION_DECOMPRESS:
829 reg = 20;
830 break;
832 default:
833 reg = 80;
834 break;
837 btwrite(reg, ZR36057_JCFT);
838 zr36057_adjust_vfe(zr, mode);
842 void
843 print_interrupts (struct zoran *zr)
845 int res, noerr = 0;
847 printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
848 if ((res = zr->field_counter) < -1 || res > 1) {
849 printk(" FD:%d", res);
851 if ((res = zr->intr_counter_GIRQ1) != 0) {
852 printk(" GIRQ1:%d", res);
853 noerr++;
855 if ((res = zr->intr_counter_GIRQ0) != 0) {
856 printk(" GIRQ0:%d", res);
857 noerr++;
859 if ((res = zr->intr_counter_CodRepIRQ) != 0) {
860 printk(" CodRepIRQ:%d", res);
861 noerr++;
863 if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
864 printk(" JPEGRepIRQ:%d", res);
865 noerr++;
867 if (zr->JPEG_max_missed) {
868 printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
869 zr->JPEG_min_missed);
871 if (zr->END_event_missed) {
872 printk(" ENDs missed: %d", zr->END_event_missed);
874 //if (zr->jpg_queued_num) {
875 printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
876 zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
878 if (!noerr) {
879 printk(": no interrupts detected.");
881 printk("\n");
884 void
885 clear_interrupt_counters (struct zoran *zr)
887 zr->intr_counter_GIRQ1 = 0;
888 zr->intr_counter_GIRQ0 = 0;
889 zr->intr_counter_CodRepIRQ = 0;
890 zr->intr_counter_JPEGRepIRQ = 0;
891 zr->field_counter = 0;
892 zr->IRQ1_in = 0;
893 zr->IRQ1_out = 0;
894 zr->JPEG_in = 0;
895 zr->JPEG_out = 0;
896 zr->JPEG_0 = 0;
897 zr->JPEG_1 = 0;
898 zr->END_event_missed = 0;
899 zr->JPEG_missed = 0;
900 zr->JPEG_max_missed = 0;
901 zr->JPEG_min_missed = 0x7fffffff;
904 static u32
905 count_reset_interrupt (struct zoran *zr)
907 u32 isr;
909 if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
910 if (isr & ZR36057_ISR_GIRQ1) {
911 btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
912 zr->intr_counter_GIRQ1++;
914 if (isr & ZR36057_ISR_GIRQ0) {
915 btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
916 zr->intr_counter_GIRQ0++;
918 if (isr & ZR36057_ISR_CodRepIRQ) {
919 btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
920 zr->intr_counter_CodRepIRQ++;
922 if (isr & ZR36057_ISR_JPEGRepIRQ) {
923 btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
924 zr->intr_counter_JPEGRepIRQ++;
927 return isr;
930 /* hack */
931 extern void zr36016_write (struct videocodec *codec,
932 u16 reg,
933 u32 val);
935 void
936 jpeg_start (struct zoran *zr)
938 int reg;
940 zr->frame_num = 0;
942 /* deassert P_reset, disable code transfer, deassert Active */
943 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
944 /* stop flushing the internal code buffer */
945 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
946 /* enable code transfer */
947 btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
949 /* clear IRQs */
950 btwrite(IRQ_MASK, ZR36057_ISR);
951 /* enable the JPEG IRQs */
952 btwrite(zr->card.jpeg_int |
953 ZR36057_ICR_JPEGRepIRQ |
954 ZR36057_ICR_IntPinEn,
955 ZR36057_ICR);
957 set_frame(zr, 0); // \FRAME
959 /* set the JPEG codec guest ID */
960 reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
961 (0 << ZR36057_JCGI_JPEGuestReg);
962 btwrite(reg, ZR36057_JCGI);
964 if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
965 zr->card.video_codec == CODEC_TYPE_ZR36050) {
966 /* Enable processing on the ZR36016 */
967 if (zr->vfe)
968 zr36016_write(zr->vfe, 0, 1);
970 /* load the address of the GO register in the ZR36050 latch */
971 post_office_write(zr, 0, 0, 0);
974 /* assert Active */
975 btor(ZR36057_JPC_Active, ZR36057_JPC);
977 /* enable the Go generation */
978 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
979 udelay(30);
981 set_frame(zr, 1); // /FRAME
983 dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
986 void
987 zr36057_enable_jpg (struct zoran *zr,
988 enum zoran_codec_mode mode)
990 static int zero = 0;
991 static int one = 1;
992 struct vfe_settings cap;
993 int field_size =
994 zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
996 zr->codec_mode = mode;
998 cap.x = zr->jpg_settings.img_x;
999 cap.y = zr->jpg_settings.img_y;
1000 cap.width = zr->jpg_settings.img_width;
1001 cap.height = zr->jpg_settings.img_height;
1002 cap.decimation =
1003 zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
1004 cap.quality = zr->jpg_settings.jpg_comp.quality;
1006 switch (mode) {
1008 case BUZ_MODE_MOTION_COMPRESS: {
1009 struct jpeg_app_marker app;
1010 struct jpeg_com_marker com;
1012 /* In motion compress mode, the decoder output must be enabled, and
1013 * the video bus direction set to input.
1015 set_videobus_dir(zr, 0);
1016 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1017 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1019 /* Take the JPEG codec and the VFE out of sleep */
1020 jpeg_codec_sleep(zr, 0);
1022 /* set JPEG app/com marker */
1023 app.appn = zr->jpg_settings.jpg_comp.APPn;
1024 app.len = zr->jpg_settings.jpg_comp.APP_len;
1025 memcpy(app.data, zr->jpg_settings.jpg_comp.APP_data, 60);
1026 zr->codec->control(zr->codec, CODEC_S_JPEG_APP_DATA,
1027 sizeof(struct jpeg_app_marker), &app);
1029 com.len = zr->jpg_settings.jpg_comp.COM_len;
1030 memcpy(com.data, zr->jpg_settings.jpg_comp.COM_data, 60);
1031 zr->codec->control(zr->codec, CODEC_S_JPEG_COM_DATA,
1032 sizeof(struct jpeg_com_marker), &com);
1034 /* Setup the JPEG codec */
1035 zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
1036 sizeof(int), &field_size);
1037 zr->codec->set_video(zr->codec, zr->timing, &cap,
1038 &zr->card.vfe_pol);
1039 zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
1041 /* Setup the VFE */
1042 if (zr->vfe) {
1043 zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
1044 sizeof(int), &field_size);
1045 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1046 &zr->card.vfe_pol);
1047 zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
1050 init_jpeg_queue(zr);
1051 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1053 clear_interrupt_counters(zr);
1054 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
1055 ZR_DEVNAME(zr));
1056 break;
1059 case BUZ_MODE_MOTION_DECOMPRESS:
1060 /* In motion decompression mode, the decoder output must be disabled, and
1061 * the video bus direction set to output.
1063 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1064 set_videobus_dir(zr, 1);
1065 encoder_command(zr, ENCODER_SET_INPUT, &one);
1067 /* Take the JPEG codec and the VFE out of sleep */
1068 jpeg_codec_sleep(zr, 0);
1069 /* Setup the VFE */
1070 if (zr->vfe) {
1071 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1072 &zr->card.vfe_pol);
1073 zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
1075 /* Setup the JPEG codec */
1076 zr->codec->set_video(zr->codec, zr->timing, &cap,
1077 &zr->card.vfe_pol);
1078 zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
1080 init_jpeg_queue(zr);
1081 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1083 clear_interrupt_counters(zr);
1084 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
1085 ZR_DEVNAME(zr));
1086 break;
1088 case BUZ_MODE_IDLE:
1089 default:
1090 /* shut down processing */
1091 btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
1092 ZR36057_ICR);
1093 btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
1094 ZR36057_ISR);
1095 btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // \Go_en
1097 msleep(50);
1099 set_videobus_dir(zr, 0);
1100 set_frame(zr, 1); // /FRAME
1101 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // /CFlush
1102 btwrite(0, ZR36057_JPC); // \P_Reset,\CodTrnsEn,\Active
1103 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1104 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1105 jpeg_codec_reset(zr);
1106 jpeg_codec_sleep(zr, 1);
1107 zr36057_adjust_vfe(zr, mode);
1109 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1110 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1112 dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
1113 break;
1118 /* when this is called the spinlock must be held */
1119 void
1120 zoran_feed_stat_com (struct zoran *zr)
1122 /* move frames from pending queue to DMA */
1124 int frame, i, max_stat_com;
1126 max_stat_com =
1127 (zr->jpg_settings.TmpDcm ==
1128 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1130 while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
1131 zr->jpg_dma_head < zr->jpg_que_head) {
1133 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1134 if (zr->jpg_settings.TmpDcm == 1) {
1135 /* fill 1 stat_com entry */
1136 i = (zr->jpg_dma_head -
1137 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1138 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1139 break;
1140 zr->stat_com[i] =
1141 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1142 } else {
1143 /* fill 2 stat_com entries */
1144 i = ((zr->jpg_dma_head -
1145 zr->jpg_err_shift) & 1) * 2;
1146 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1147 break;
1148 zr->stat_com[i] =
1149 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1150 zr->stat_com[i + 1] =
1151 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1153 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
1154 zr->jpg_dma_head++;
1157 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1158 zr->jpg_queued_num++;
1161 /* when this is called the spinlock must be held */
1162 static void
1163 zoran_reap_stat_com (struct zoran *zr)
1165 /* move frames from DMA queue to done queue */
1167 int i;
1168 u32 stat_com;
1169 unsigned int seq;
1170 unsigned int dif;
1171 struct zoran_jpg_buffer *buffer;
1172 int frame;
1174 /* In motion decompress we don't have a hardware frame counter,
1175 * we just count the interrupts here */
1177 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1178 zr->jpg_seq_num++;
1180 while (zr->jpg_dma_tail < zr->jpg_dma_head) {
1181 if (zr->jpg_settings.TmpDcm == 1)
1182 i = (zr->jpg_dma_tail -
1183 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1184 else
1185 i = ((zr->jpg_dma_tail -
1186 zr->jpg_err_shift) & 1) * 2 + 1;
1188 stat_com = le32_to_cpu(zr->stat_com[i]);
1190 if ((stat_com & 1) == 0) {
1191 return;
1193 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1194 buffer = &zr->jpg_buffers.buffer[frame];
1195 do_gettimeofday(&buffer->bs.timestamp);
1197 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1198 buffer->bs.length = (stat_com & 0x7fffff) >> 1;
1200 /* update sequence number with the help of the counter in stat_com */
1202 seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
1203 dif = (seq - zr->jpg_seq_num) & 0xff;
1204 zr->jpg_seq_num += dif;
1205 } else {
1206 buffer->bs.length = 0;
1208 buffer->bs.seq =
1209 zr->jpg_settings.TmpDcm ==
1210 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1211 buffer->state = BUZ_STATE_DONE;
1213 zr->jpg_dma_tail++;
1217 static void
1218 error_handler (struct zoran *zr,
1219 u32 astat,
1220 u32 stat)
1222 /* This is JPEG error handling part */
1223 if ((zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) &&
1224 (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS)) {
1225 //dprintk(1, KERN_ERR "%s: Internal error: error handling request in mode %d\n", ZR_DEVNAME(zr), zr->codec_mode);
1226 return;
1229 if ((stat & 1) == 0 &&
1230 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
1231 zr->jpg_dma_tail - zr->jpg_que_tail >=
1232 zr->jpg_buffers.num_buffers) {
1233 /* No free buffers... */
1234 zoran_reap_stat_com(zr);
1235 zoran_feed_stat_com(zr);
1236 wake_up_interruptible(&zr->jpg_capq);
1237 zr->JPEG_missed = 0;
1238 return;
1241 if (zr->JPEG_error != 1) {
1243 * First entry: error just happened during normal operation
1245 * In BUZ_MODE_MOTION_COMPRESS:
1247 * Possible glitch in TV signal. In this case we should
1248 * stop the codec and wait for good quality signal before
1249 * restarting it to avoid further problems
1251 * In BUZ_MODE_MOTION_DECOMPRESS:
1253 * Bad JPEG frame: we have to mark it as processed (codec crashed
1254 * and was not able to do it itself), and to remove it from queue.
1256 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1257 udelay(1);
1258 stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
1259 btwrite(0, ZR36057_JPC);
1260 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1261 jpeg_codec_reset(zr);
1262 jpeg_codec_sleep(zr, 1);
1263 zr->JPEG_error = 1;
1264 zr->num_errors++;
1266 /* Report error */
1267 if (zr36067_debug > 1 && zr->num_errors <= 8) {
1268 long frame;
1269 frame =
1270 zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1271 printk(KERN_ERR
1272 "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
1273 ZR_DEVNAME(zr), stat, zr->last_isr,
1274 zr->jpg_que_tail, zr->jpg_dma_tail,
1275 zr->jpg_dma_head, zr->jpg_que_head,
1276 zr->jpg_seq_num, frame);
1277 printk("stat_com frames:");
1279 int i, j;
1280 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1281 for (i = 0;
1282 i < zr->jpg_buffers.num_buffers;
1283 i++) {
1284 if (le32_to_cpu(zr->stat_com[j]) ==
1285 zr->jpg_buffers.
1286 buffer[i].
1287 frag_tab_bus) {
1288 printk("% d->%d",
1289 j, i);
1293 printk("\n");
1296 /* Find an entry in stat_com and rotate contents */
1298 int i;
1300 if (zr->jpg_settings.TmpDcm == 1)
1301 i = (zr->jpg_dma_tail -
1302 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1303 else
1304 i = ((zr->jpg_dma_tail -
1305 zr->jpg_err_shift) & 1) * 2;
1306 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1307 /* Mimic zr36067 operation */
1308 zr->stat_com[i] |= cpu_to_le32(1);
1309 if (zr->jpg_settings.TmpDcm != 1)
1310 zr->stat_com[i + 1] |= cpu_to_le32(1);
1311 /* Refill */
1312 zoran_reap_stat_com(zr);
1313 zoran_feed_stat_com(zr);
1314 wake_up_interruptible(&zr->jpg_capq);
1315 /* Find an entry in stat_com again after refill */
1316 if (zr->jpg_settings.TmpDcm == 1)
1317 i = (zr->jpg_dma_tail -
1318 zr->jpg_err_shift) &
1319 BUZ_MASK_STAT_COM;
1320 else
1321 i = ((zr->jpg_dma_tail -
1322 zr->jpg_err_shift) & 1) * 2;
1324 if (i) {
1325 /* Rotate stat_comm entries to make current entry first */
1326 int j;
1327 u32 bus_addr[BUZ_NUM_STAT_COM];
1329 /* Here we are copying the stat_com array, which
1330 * is already in little endian format, so
1331 * no endian conversions here
1333 memcpy(bus_addr, zr->stat_com,
1334 sizeof(bus_addr));
1335 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1336 zr->stat_com[j] =
1337 bus_addr[(i + j) &
1338 BUZ_MASK_STAT_COM];
1341 zr->jpg_err_shift += i;
1342 zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
1344 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
1345 zr->jpg_err_seq = zr->jpg_seq_num; /* + 1; */
1349 /* Now the stat_comm buffer is ready for restart */
1350 do {
1351 int status, mode;
1353 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1354 decoder_command(zr, DECODER_GET_STATUS, &status);
1355 mode = CODEC_DO_COMPRESSION;
1356 } else {
1357 status = 0;
1358 mode = CODEC_DO_EXPANSION;
1360 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1361 (status & DECODER_STATUS_GOOD)) {
1362 /********** RESTART code *************/
1363 jpeg_codec_reset(zr);
1364 zr->codec->set_mode(zr->codec, mode);
1365 zr36057_set_jpg(zr, zr->codec_mode);
1366 jpeg_start(zr);
1368 if (zr->num_errors <= 8)
1369 dprintk(2, KERN_INFO "%s: Restart\n",
1370 ZR_DEVNAME(zr));
1372 zr->JPEG_missed = 0;
1373 zr->JPEG_error = 2;
1374 /********** End RESTART code ***********/
1376 } while (0);
1379 irqreturn_t
1380 zoran_irq (int irq,
1381 void *dev_id)
1383 u32 stat, astat;
1384 int count;
1385 struct zoran *zr;
1386 unsigned long flags;
1388 zr = dev_id;
1389 count = 0;
1391 if (zr->testing) {
1392 /* Testing interrupts */
1393 spin_lock_irqsave(&zr->spinlock, flags);
1394 while ((stat = count_reset_interrupt(zr))) {
1395 if (count++ > 100) {
1396 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1397 dprintk(1,
1398 KERN_ERR
1399 "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
1400 ZR_DEVNAME(zr), stat);
1401 wake_up_interruptible(&zr->test_q);
1404 zr->last_isr = stat;
1405 spin_unlock_irqrestore(&zr->spinlock, flags);
1406 return IRQ_HANDLED;
1409 spin_lock_irqsave(&zr->spinlock, flags);
1410 while (1) {
1411 /* get/clear interrupt status bits */
1412 stat = count_reset_interrupt(zr);
1413 astat = stat & IRQ_MASK;
1414 if (!astat) {
1415 break;
1417 dprintk(4,
1418 KERN_DEBUG
1419 "zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
1420 astat, btread(ZR36057_ICR));
1421 if (astat & zr->card.vsync_int) { // SW
1423 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1424 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1425 /* count missed interrupts */
1426 zr->JPEG_missed++;
1428 //post_office_read(zr,1,0);
1429 /* Interrupts may still happen when
1430 * zr->v4l_memgrab_active is switched off.
1431 * We simply ignore them */
1433 if (zr->v4l_memgrab_active) {
1435 /* A lot more checks should be here ... */
1436 if ((btread(ZR36057_VSSFGR) &
1437 ZR36057_VSSFGR_SnapShot) == 0)
1438 dprintk(1,
1439 KERN_WARNING
1440 "%s: BuzIRQ with SnapShot off ???\n",
1441 ZR_DEVNAME(zr));
1443 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1444 /* There is a grab on a frame going on, check if it has finished */
1446 if ((btread(ZR36057_VSSFGR) &
1447 ZR36057_VSSFGR_FrameGrab) ==
1448 0) {
1449 /* it is finished, notify the user */
1451 zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1452 zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
1453 do_gettimeofday(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
1454 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1455 zr->v4l_pend_tail++;
1459 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1460 wake_up_interruptible(&zr->v4l_capq);
1462 /* Check if there is another grab queued */
1464 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1465 zr->v4l_pend_tail != zr->v4l_pend_head) {
1467 int frame = zr->v4l_pend[zr->v4l_pend_tail &
1468 V4L_MASK_FRAME];
1469 u32 reg;
1471 zr->v4l_grab_frame = frame;
1473 /* Set zr36057 video front end and enable video */
1475 /* Buffer address */
1477 reg =
1478 zr->v4l_buffers.buffer[frame].
1479 fbuffer_bus;
1480 btwrite(reg, ZR36057_VDTR);
1481 if (zr->v4l_settings.height >
1482 BUZ_MAX_HEIGHT / 2)
1483 reg +=
1484 zr->v4l_settings.
1485 bytesperline;
1486 btwrite(reg, ZR36057_VDBR);
1488 /* video stride, status, and frame grab register */
1489 reg = 0;
1490 if (zr->v4l_settings.height >
1491 BUZ_MAX_HEIGHT / 2)
1492 reg +=
1493 zr->v4l_settings.
1494 bytesperline;
1495 reg =
1496 (reg <<
1497 ZR36057_VSSFGR_DispStride);
1498 reg |= ZR36057_VSSFGR_VidOvf;
1499 reg |= ZR36057_VSSFGR_SnapShot;
1500 reg |= ZR36057_VSSFGR_FrameGrab;
1501 btwrite(reg, ZR36057_VSSFGR);
1503 btor(ZR36057_VDCR_VidEn,
1504 ZR36057_VDCR);
1508 /* even if we don't grab, we do want to increment
1509 * the sequence counter to see lost frames */
1510 zr->v4l_grab_seq++;
1512 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
1513 if (astat & ZR36057_ISR_CodRepIRQ) {
1514 zr->intr_counter_CodRepIRQ++;
1515 IDEBUG(printk
1516 (KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
1517 ZR_DEVNAME(zr)));
1518 btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
1520 #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
1522 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
1523 if (astat & ZR36057_ISR_JPEGRepIRQ) {
1525 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1526 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1527 if (zr36067_debug > 1 &&
1528 (!zr->frame_num || zr->JPEG_error)) {
1529 printk(KERN_INFO
1530 "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
1531 ZR_DEVNAME(zr), stat,
1532 zr->jpg_settings.odd_even,
1533 zr->jpg_settings.
1534 field_per_buff,
1535 zr->JPEG_missed);
1537 char sc[] = "0000";
1538 char sv[5];
1539 int i;
1540 strcpy(sv, sc);
1541 for (i = 0; i < 4; i++) {
1542 if (le32_to_cpu(zr->stat_com[i]) & 1)
1543 sv[i] = '1';
1545 sv[4] = 0;
1546 printk(KERN_INFO
1547 "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
1548 ZR_DEVNAME(zr), sv,
1549 zr->jpg_que_tail,
1550 zr->jpg_dma_tail,
1551 zr->jpg_dma_head,
1552 zr->jpg_que_head);
1554 } else {
1555 if (zr->JPEG_missed > zr->JPEG_max_missed) // Get statistics
1556 zr->JPEG_max_missed =
1557 zr->JPEG_missed;
1558 if (zr->JPEG_missed <
1559 zr->JPEG_min_missed)
1560 zr->JPEG_min_missed =
1561 zr->JPEG_missed;
1564 if (zr36067_debug > 2 && zr->frame_num < 6) {
1565 int i;
1566 printk("%s: seq=%ld stat_com:",
1567 ZR_DEVNAME(zr), zr->jpg_seq_num);
1568 for (i = 0; i < 4; i++) {
1569 printk(" %08x",
1570 le32_to_cpu(zr->stat_com[i]));
1572 printk("\n");
1574 zr->frame_num++;
1575 zr->JPEG_missed = 0;
1576 zr->JPEG_error = 0;
1577 zoran_reap_stat_com(zr);
1578 zoran_feed_stat_com(zr);
1579 wake_up_interruptible(&zr->jpg_capq);
1580 } /*else {
1581 dprintk(1,
1582 KERN_ERR
1583 "%s: JPEG interrupt while not in motion (de)compress mode!\n",
1584 ZR_DEVNAME(zr));
1587 #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
1589 /* DATERR, too many fields missed, error processing */
1590 if ((astat & zr->card.jpeg_int) ||
1591 zr->JPEG_missed > 25 ||
1592 zr->JPEG_error == 1 ||
1593 ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
1594 (zr->frame_num & (zr->JPEG_missed >
1595 zr->jpg_settings.field_per_buff)))) {
1596 error_handler(zr, astat, stat);
1599 count++;
1600 if (count > 10) {
1601 dprintk(2, KERN_WARNING "%s: irq loop %d\n",
1602 ZR_DEVNAME(zr), count);
1603 if (count > 20) {
1604 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1605 dprintk(2,
1606 KERN_ERR
1607 "%s: IRQ lockup, cleared int mask\n",
1608 ZR_DEVNAME(zr));
1609 break;
1612 zr->last_isr = stat;
1614 spin_unlock_irqrestore(&zr->spinlock, flags);
1616 return IRQ_HANDLED;
1619 void
1620 zoran_set_pci_master (struct zoran *zr,
1621 int set_master)
1623 if (set_master) {
1624 pci_set_master(zr->pci_dev);
1625 } else {
1626 u16 command;
1628 pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
1629 command &= ~PCI_COMMAND_MASTER;
1630 pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
1634 void
1635 zoran_init_hardware (struct zoran *zr)
1637 int j, zero = 0;
1639 /* Enable bus-mastering */
1640 zoran_set_pci_master(zr, 1);
1642 /* Initialize the board */
1643 if (zr->card.init) {
1644 zr->card.init(zr);
1647 j = zr->card.input[zr->input].muxsel;
1649 decoder_command(zr, 0, NULL);
1650 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1651 decoder_command(zr, DECODER_SET_INPUT, &j);
1653 encoder_command(zr, 0, NULL);
1654 encoder_command(zr, ENCODER_SET_NORM, &zr->norm);
1655 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1657 /* toggle JPEG codec sleep to sync PLL */
1658 jpeg_codec_sleep(zr, 1);
1659 jpeg_codec_sleep(zr, 0);
1661 /* set individual interrupt enables (without GIRQ1)
1662 * but don't global enable until zoran_open() */
1664 //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR); // SW
1665 // It looks like using only JPEGRepIRQEn is not always reliable,
1666 // may be when JPEG codec crashes it won't generate IRQ? So,
1667 /*CP*/ // btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM WHY ? LP
1668 zr36057_init_vfe(zr);
1670 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1672 btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
1675 void
1676 zr36057_restart (struct zoran *zr)
1678 btwrite(0, ZR36057_SPGPPCR);
1679 mdelay(1);
1680 btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
1681 mdelay(1);
1683 /* assert P_Reset */
1684 btwrite(0, ZR36057_JPC);
1685 /* set up GPIO direction - all output */
1686 btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
1688 /* set up GPIO pins and guest bus timing */
1689 btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
1693 * initialize video front end
1696 static void
1697 zr36057_init_vfe (struct zoran *zr)
1699 u32 reg;
1701 reg = btread(ZR36057_VFESPFR);
1702 reg |= ZR36057_VFESPFR_LittleEndian;
1703 reg &= ~ZR36057_VFESPFR_VCLKPol;
1704 reg |= ZR36057_VFESPFR_ExtFl;
1705 reg |= ZR36057_VFESPFR_TopField;
1706 btwrite(reg, ZR36057_VFESPFR);
1707 reg = btread(ZR36057_VDCR);
1708 if (pci_pci_problems & PCIPCI_TRITON)
1709 // || zr->revision < 1) // Revision 1 has also Triton support
1710 reg &= ~ZR36057_VDCR_Triton;
1711 else
1712 reg |= ZR36057_VDCR_Triton;
1713 btwrite(reg, ZR36057_VDCR);
1717 * Interface to decoder and encoder chips using i2c bus
1721 decoder_command (struct zoran *zr,
1722 int cmd,
1723 void *data)
1725 if (zr->decoder == NULL)
1726 return -EIO;
1728 if (zr->card.type == LML33 &&
1729 (cmd == DECODER_SET_NORM || DECODER_SET_INPUT)) {
1730 int res;
1732 // Bt819 needs to reset its FIFO buffer using #FRST pin and
1733 // LML33 card uses GPIO(7) for that.
1734 GPIO(zr, 7, 0);
1735 res = zr->decoder->driver->command(zr->decoder, cmd, data);
1736 // Pull #FRST high.
1737 GPIO(zr, 7, 1);
1738 return res;
1739 } else
1740 return zr->decoder->driver->command(zr->decoder, cmd,
1741 data);
1745 encoder_command (struct zoran *zr,
1746 int cmd,
1747 void *data)
1749 if (zr->encoder == NULL)
1750 return -1;
1752 return zr->encoder->driver->command(zr->encoder, cmd, data);