PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / media / video / zoran_device.c
blob88d369708e4c82b6b52f9451d17067acd042f577
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>
35 #include <linux/interrupt.h>
36 #include <linux/proc_fs.h>
37 #include <linux/i2c.h>
38 #include <linux/i2c-algo-bit.h>
39 #include <linux/videodev.h>
40 #include <linux/spinlock.h>
41 #include <linux/sem.h>
43 #include <linux/pci.h>
44 #include <linux/video_decoder.h>
45 #include <linux/video_encoder.h>
46 #include <linux/delay.h>
47 #include <linux/wait.h>
49 #include <asm/byteorder.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; /* default = 0
64 * 1 will use digital path in capture
65 * mode instead of analog. It can be
66 * used for picture adjustments using
67 * tool like xawtv while watching image
68 * on TV monitor connected to the output.
69 * However, due to absence of 75 Ohm
70 * load on Bt819 input, there will be
71 * some image imperfections */
73 module_param(lml33dpath, bool, 0644);
74 MODULE_PARM_DESC(lml33dpath,
75 "Use digital path capture mode (on LML33 cards)");
77 static void
78 zr36057_init_vfe (struct zoran *zr);
81 * General Purpose I/O and Guest bus access
85 * This is a bit tricky. When a board lacks a GPIO function, the corresponding
86 * GPIO bit number in the card_info structure is set to 0.
89 void
90 GPIO (struct zoran *zr,
91 int bit,
92 unsigned int value)
94 u32 reg;
95 u32 mask;
97 /* Make sure the bit number is legal
98 * A bit number of -1 (lacking) gives a mask of 0,
99 * making it harmless */
100 mask = (1 << (24 + bit)) & 0xff000000;
101 reg = btread(ZR36057_GPPGCR1) & ~mask;
102 if (value) {
103 reg |= mask;
105 btwrite(reg, ZR36057_GPPGCR1);
106 udelay(1);
110 * Wait til post office is no longer busy
114 post_office_wait (struct zoran *zr)
116 u32 por;
118 // while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
119 while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
120 /* wait for something to happen */
122 if ((por & ZR36057_POR_POTime) && !zr->card.gws_not_connected) {
123 /* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
124 dprintk(1, KERN_INFO "%s: pop timeout %08x\n", ZR_DEVNAME(zr),
125 por);
126 return -1;
129 return 0;
133 post_office_write (struct zoran *zr,
134 unsigned int guest,
135 unsigned int reg,
136 unsigned int value)
138 u32 por;
140 por =
141 ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
142 ((reg & 7) << 16) | (value & 0xFF);
143 btwrite(por, ZR36057_POR);
145 return post_office_wait(zr);
149 post_office_read (struct zoran *zr,
150 unsigned int guest,
151 unsigned int reg)
153 u32 por;
155 por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
156 btwrite(por, ZR36057_POR);
157 if (post_office_wait(zr) < 0) {
158 return -1;
161 return btread(ZR36057_POR) & 0xFF;
165 * detect guests
168 static void
169 dump_guests (struct zoran *zr)
171 if (zr36067_debug > 2) {
172 int i, guest[8];
174 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
175 guest[i] = post_office_read(zr, i, 0);
178 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
180 for (i = 1; i < 8; i++) {
181 printk(" 0x%02x", guest[i]);
183 printk("\n");
187 static inline unsigned long
188 get_time (void)
190 struct timeval tv;
192 do_gettimeofday(&tv);
193 return (1000000 * tv.tv_sec + tv.tv_usec);
196 void
197 detect_guest_activity (struct zoran *zr)
199 int timeout, i, j, res, guest[8], guest0[8], change[8][3];
200 unsigned long t0, t1;
202 dump_guests(zr);
203 printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
204 ZR_DEVNAME(zr));
205 for (i = 1; i < 8; i++) { // Don't read jpeg codec here
206 guest0[i] = guest[i] = post_office_read(zr, i, 0);
209 timeout = 0;
210 j = 0;
211 t0 = get_time();
212 while (timeout < 10000) {
213 udelay(10);
214 timeout++;
215 for (i = 1; (i < 8) && (j < 8); i++) {
216 res = post_office_read(zr, i, 0);
217 if (res != guest[i]) {
218 t1 = get_time();
219 change[j][0] = (t1 - t0);
220 t0 = t1;
221 change[j][1] = i;
222 change[j][2] = res;
223 j++;
224 guest[i] = res;
227 if (j >= 8)
228 break;
230 printk(KERN_INFO "%s: Guests:", ZR_DEVNAME(zr));
232 for (i = 1; i < 8; i++) {
233 printk(" 0x%02x", guest0[i]);
235 printk("\n");
236 if (j == 0) {
237 printk(KERN_INFO "%s: No activity detected.\n", ZR_DEVNAME(zr));
238 return;
240 for (i = 0; i < j; i++) {
241 printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", ZR_DEVNAME(zr),
242 change[i][0], change[i][1], change[i][2]);
247 * JPEG Codec access
250 void
251 jpeg_codec_sleep (struct zoran *zr,
252 int sleep)
254 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_SLEEP], !sleep);
255 if (!sleep) {
256 dprintk(3,
257 KERN_DEBUG
258 "%s: jpeg_codec_sleep() - wake GPIO=0x%08x\n",
259 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
260 udelay(500);
261 } else {
262 dprintk(3,
263 KERN_DEBUG
264 "%s: jpeg_codec_sleep() - sleep GPIO=0x%08x\n",
265 ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
266 udelay(2);
271 jpeg_codec_reset (struct zoran *zr)
273 /* Take the codec out of sleep */
274 jpeg_codec_sleep(zr, 0);
276 if (zr->card.gpcs[GPCS_JPEG_RESET] != 0xff) {
277 post_office_write(zr, zr->card.gpcs[GPCS_JPEG_RESET], 0,
279 udelay(2);
280 } else {
281 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 0);
282 udelay(2);
283 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 1);
284 udelay(2);
287 return 0;
291 * Set the registers for the size we have specified. Don't bother
292 * trying to understand this without the ZR36057 manual in front of
293 * you [AC].
295 * PS: The manual is free for download in .pdf format from
296 * www.zoran.com - nicely done those folks.
299 static void
300 zr36057_adjust_vfe (struct zoran *zr,
301 enum zoran_codec_mode mode)
303 u32 reg;
305 switch (mode) {
306 case BUZ_MODE_MOTION_DECOMPRESS:
307 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
308 reg = btread(ZR36057_VFEHCR);
309 if ((reg & (1 << 10)) && zr->card.type != LML33R10) {
310 reg += ((1 << 10) | 1);
312 btwrite(reg, ZR36057_VFEHCR);
313 break;
314 case BUZ_MODE_MOTION_COMPRESS:
315 case BUZ_MODE_IDLE:
316 default:
317 if (zr->norm == VIDEO_MODE_NTSC ||
318 (zr->card.type == LML33R10 &&
319 zr->norm == VIDEO_MODE_PAL))
320 btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
321 else
322 btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
323 reg = btread(ZR36057_VFEHCR);
324 if (!(reg & (1 << 10)) && zr->card.type != LML33R10) {
325 reg -= ((1 << 10) | 1);
327 btwrite(reg, ZR36057_VFEHCR);
328 break;
333 * set geometry
336 static void
337 zr36057_set_vfe (struct zoran *zr,
338 int video_width,
339 int video_height,
340 const struct zoran_format *format)
342 struct tvnorm *tvn;
343 unsigned HStart, HEnd, VStart, VEnd;
344 unsigned DispMode;
345 unsigned VidWinWid, VidWinHt;
346 unsigned hcrop1, hcrop2, vcrop1, vcrop2;
347 unsigned Wa, We, Ha, He;
348 unsigned X, Y, HorDcm, VerDcm;
349 u32 reg;
350 unsigned mask_line_size;
352 tvn = zr->timing;
354 Wa = tvn->Wa;
355 Ha = tvn->Ha;
357 dprintk(2, KERN_INFO "%s: set_vfe() - width = %d, height = %d\n",
358 ZR_DEVNAME(zr), video_width, video_height);
360 if (zr->norm != VIDEO_MODE_PAL &&
361 zr->norm != VIDEO_MODE_NTSC &&
362 zr->norm != VIDEO_MODE_SECAM) {
363 dprintk(1,
364 KERN_ERR "%s: set_vfe() - norm = %d not valid\n",
365 ZR_DEVNAME(zr), zr->norm);
366 return;
368 if (video_width < BUZ_MIN_WIDTH ||
369 video_height < BUZ_MIN_HEIGHT ||
370 video_width > Wa || video_height > Ha) {
371 dprintk(1, KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
372 ZR_DEVNAME(zr), video_width, video_height);
373 return;
376 /**** zr36057 ****/
378 /* horizontal */
379 VidWinWid = video_width;
380 X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
381 We = (VidWinWid * 64) / X;
382 HorDcm = 64 - X;
383 hcrop1 = 2 * ((tvn->Wa - We) / 4);
384 hcrop2 = tvn->Wa - We - hcrop1;
385 HStart = tvn->HStart ? tvn->HStart : 1;
386 /* (Ronald) Original comment:
387 * "| 1 Doesn't have any effect, tested on both a DC10 and a DC10+"
388 * this is false. It inverses chroma values on the LML33R10 (so Cr
389 * suddenly is shown as Cb and reverse, really cool effect if you
390 * want to see blue faces, not useful otherwise). So don't use |1.
391 * However, the DC10 has '0' as HStart, but does need |1, so we
392 * use a dirty check...
394 HEnd = HStart + tvn->Wa - 1;
395 HStart += hcrop1;
396 HEnd -= hcrop2;
397 reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
398 | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
399 if (zr->card.vfe_pol.hsync_pol)
400 reg |= ZR36057_VFEHCR_HSPol;
401 btwrite(reg, ZR36057_VFEHCR);
403 /* Vertical */
404 DispMode = !(video_height > BUZ_MAX_HEIGHT / 2);
405 VidWinHt = DispMode ? video_height : video_height / 2;
406 Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
407 He = (VidWinHt * 64) / Y;
408 VerDcm = 64 - Y;
409 vcrop1 = (tvn->Ha / 2 - He) / 2;
410 vcrop2 = tvn->Ha / 2 - He - vcrop1;
411 VStart = tvn->VStart;
412 VEnd = VStart + tvn->Ha / 2; // - 1; FIXME SnapShot times out with -1 in 768*576 on the DC10 - LP
413 VStart += vcrop1;
414 VEnd -= vcrop2;
415 reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
416 | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
417 if (zr->card.vfe_pol.vsync_pol)
418 reg |= ZR36057_VFEVCR_VSPol;
419 btwrite(reg, ZR36057_VFEVCR);
421 /* scaler and pixel format */
422 reg = 0;
423 reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
424 reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
425 reg |= (DispMode << ZR36057_VFESPFR_DispMode);
426 /* RJ: I don't know, why the following has to be the opposite
427 * of the corresponding ZR36060 setting, but only this way
428 * we get the correct colors when uncompressing to the screen */
429 //reg |= ZR36057_VFESPFR_VCLKPol; /**/
430 /* RJ: Don't know if that is needed for NTSC also */
431 if (zr->norm != VIDEO_MODE_NTSC)
432 reg |= ZR36057_VFESPFR_ExtFl; // NEEDED!!!!!!! Wolfgang
433 reg |= ZR36057_VFESPFR_TopField;
434 if (HorDcm >= 48) {
435 reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
436 } else if (HorDcm >= 32) {
437 reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
438 } else if (HorDcm >= 16) {
439 reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
441 reg |= format->vfespfr;
442 btwrite(reg, ZR36057_VFESPFR);
444 /* display configuration */
445 reg = (16 << ZR36057_VDCR_MinPix)
446 | (VidWinHt << ZR36057_VDCR_VidWinHt)
447 | (VidWinWid << ZR36057_VDCR_VidWinWid);
448 if (pci_pci_problems & PCIPCI_TRITON)
449 // || zr->revision < 1) // Revision 1 has also Triton support
450 reg &= ~ZR36057_VDCR_Triton;
451 else
452 reg |= ZR36057_VDCR_Triton;
453 btwrite(reg, ZR36057_VDCR);
455 /* (Ronald) don't write this if overlay_mask = NULL */
456 if (zr->overlay_mask) {
457 /* Write overlay clipping mask data, but don't enable overlay clipping */
458 /* RJ: since this makes only sense on the screen, we use
459 * zr->overlay_settings.width instead of video_width */
461 mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
462 reg = virt_to_bus(zr->overlay_mask);
463 btwrite(reg, ZR36057_MMTR);
464 reg = virt_to_bus(zr->overlay_mask + mask_line_size);
465 btwrite(reg, ZR36057_MMBR);
466 reg =
467 mask_line_size - (zr->overlay_settings.width +
468 31) / 32;
469 if (DispMode == 0)
470 reg += mask_line_size;
471 reg <<= ZR36057_OCR_MaskStride;
472 btwrite(reg, ZR36057_OCR);
475 zr36057_adjust_vfe(zr, zr->codec_mode);
479 * Switch overlay on or off
482 void
483 zr36057_overlay (struct zoran *zr,
484 int on)
486 u32 reg;
488 if (on) {
489 /* do the necessary settings ... */
490 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
492 zr36057_set_vfe(zr,
493 zr->overlay_settings.width,
494 zr->overlay_settings.height,
495 zr->overlay_settings.format);
497 /* Start and length of each line MUST be 4-byte aligned.
498 * This should be allready checked before the call to this routine.
499 * All error messages are internal driver checking only! */
501 /* video display top and bottom registers */
502 reg = (long) zr->buffer.base +
503 zr->overlay_settings.x *
504 ((zr->overlay_settings.format->depth + 7) / 8) +
505 zr->overlay_settings.y *
506 zr->buffer.bytesperline;
507 btwrite(reg, ZR36057_VDTR);
508 if (reg & 3)
509 dprintk(1,
510 KERN_ERR
511 "%s: zr36057_overlay() - video_address not aligned\n",
512 ZR_DEVNAME(zr));
513 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
514 reg += zr->buffer.bytesperline;
515 btwrite(reg, ZR36057_VDBR);
517 /* video stride, status, and frame grab register */
518 reg = zr->buffer.bytesperline -
519 zr->overlay_settings.width *
520 ((zr->overlay_settings.format->depth + 7) / 8);
521 if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
522 reg += zr->buffer.bytesperline;
523 if (reg & 3)
524 dprintk(1,
525 KERN_ERR
526 "%s: zr36057_overlay() - video_stride not aligned\n",
527 ZR_DEVNAME(zr));
528 reg = (reg << ZR36057_VSSFGR_DispStride);
529 reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
530 btwrite(reg, ZR36057_VSSFGR);
532 /* Set overlay clipping */
533 if (zr->overlay_settings.clipcount > 0)
534 btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
536 /* ... and switch it on */
537 btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
538 } else {
539 /* Switch it off */
540 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
545 * The overlay mask has one bit for each pixel on a scan line,
546 * and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
549 void
550 write_overlay_mask (struct file *file,
551 struct video_clip *vp,
552 int count)
554 struct zoran_fh *fh = file->private_data;
555 struct zoran *zr = fh->zr;
556 unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
557 u32 *mask;
558 int x, y, width, height;
559 unsigned i, j, k;
560 u32 reg;
562 /* fill mask with one bits */
563 memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
564 reg = 0;
566 for (i = 0; i < count; ++i) {
567 /* pick up local copy of clip */
568 x = vp[i].x;
569 y = vp[i].y;
570 width = vp[i].width;
571 height = vp[i].height;
573 /* trim clips that extend beyond the window */
574 if (x < 0) {
575 width += x;
576 x = 0;
578 if (y < 0) {
579 height += y;
580 y = 0;
582 if (x + width > fh->overlay_settings.width) {
583 width = fh->overlay_settings.width - x;
585 if (y + height > fh->overlay_settings.height) {
586 height = fh->overlay_settings.height - y;
589 /* ignore degenerate clips */
590 if (height <= 0) {
591 continue;
593 if (width <= 0) {
594 continue;
597 /* apply clip for each scan line */
598 for (j = 0; j < height; ++j) {
599 /* reset bit for each pixel */
600 /* this can be optimized later if need be */
601 mask = fh->overlay_mask + (y + j) * mask_line_size;
602 for (k = 0; k < width; ++k) {
603 mask[(x + k) / 32] &=
604 ~((u32) 1 << (x + k) % 32);
610 /* Enable/Disable uncompressed memory grabbing of the 36057 */
612 void
613 zr36057_set_memgrab (struct zoran *zr,
614 int mode)
616 if (mode) {
617 /* We only check SnapShot and not FrameGrab here. SnapShot==1
618 * means a capture is already in progress, but FrameGrab==1
619 * doesn't necessary mean that. It's more correct to say a 1
620 * to 0 transition indicates a capture completed. If a
621 * capture is pending when capturing is tuned off, FrameGrab
622 * will be stuck at 1 until capturing is turned back on.
624 if (btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot)
625 dprintk(1,
626 KERN_WARNING
627 "%s: zr36057_set_memgrab(1) with SnapShot on!?\n",
628 ZR_DEVNAME(zr));
630 /* switch on VSync interrupts */
631 btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
632 btor(zr->card.vsync_int, ZR36057_ICR); // SW
634 /* enable SnapShot */
635 btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
637 /* Set zr36057 video front end and enable video */
638 zr36057_set_vfe(zr, zr->v4l_settings.width,
639 zr->v4l_settings.height,
640 zr->v4l_settings.format);
642 zr->v4l_memgrab_active = 1;
643 } else {
644 /* switch off VSync interrupts */
645 btand(~zr->card.vsync_int, ZR36057_ICR); // SW
647 zr->v4l_memgrab_active = 0;
648 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
650 /* reenable grabbing to screen if it was running */
651 if (zr->v4l_overlay_active) {
652 zr36057_overlay(zr, 1);
653 } else {
654 btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
655 btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
661 wait_grab_pending (struct zoran *zr)
663 unsigned long flags;
665 /* wait until all pending grabs are finished */
667 if (!zr->v4l_memgrab_active)
668 return 0;
670 wait_event_interruptible(zr->v4l_capq,
671 (zr->v4l_pend_tail == zr->v4l_pend_head));
672 if (signal_pending(current))
673 return -ERESTARTSYS;
675 spin_lock_irqsave(&zr->spinlock, flags);
676 zr36057_set_memgrab(zr, 0);
677 spin_unlock_irqrestore(&zr->spinlock, flags);
679 return 0;
682 /*****************************************************************************
684 * Set up the Buz-specific MJPEG part *
686 *****************************************************************************/
688 static inline void
689 set_frame (struct zoran *zr,
690 int val)
692 GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_FRAME], val);
695 static void
696 set_videobus_dir (struct zoran *zr,
697 int val)
699 switch (zr->card.type) {
700 case LML33:
701 case LML33R10:
702 if (lml33dpath == 0)
703 GPIO(zr, 5, val);
704 else
705 GPIO(zr, 5, 1);
706 break;
707 default:
708 GPIO(zr, zr->card.gpio[ZR_GPIO_VID_DIR],
709 zr->card.gpio_pol[ZR_GPIO_VID_DIR] ? !val : val);
710 break;
714 static void
715 init_jpeg_queue (struct zoran *zr)
717 int i;
719 /* re-initialize DMA ring stuff */
720 zr->jpg_que_head = 0;
721 zr->jpg_dma_head = 0;
722 zr->jpg_dma_tail = 0;
723 zr->jpg_que_tail = 0;
724 zr->jpg_seq_num = 0;
725 zr->JPEG_error = 0;
726 zr->num_errors = 0;
727 zr->jpg_err_seq = 0;
728 zr->jpg_err_shift = 0;
729 zr->jpg_queued_num = 0;
730 for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
731 zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
733 for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
734 zr->stat_com[i] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
738 static void
739 zr36057_set_jpg (struct zoran *zr,
740 enum zoran_codec_mode mode)
742 struct tvnorm *tvn;
743 u32 reg;
745 tvn = zr->timing;
747 /* assert P_Reset, disable code transfer, deassert Active */
748 btwrite(0, ZR36057_JPC);
750 /* MJPEG compression mode */
751 switch (mode) {
753 case BUZ_MODE_MOTION_COMPRESS:
754 default:
755 reg = ZR36057_JMC_MJPGCmpMode;
756 break;
758 case BUZ_MODE_MOTION_DECOMPRESS:
759 reg = ZR36057_JMC_MJPGExpMode;
760 reg |= ZR36057_JMC_SyncMstr;
761 /* RJ: The following is experimental - improves the output to screen */
762 //if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
763 break;
765 case BUZ_MODE_STILL_COMPRESS:
766 reg = ZR36057_JMC_JPGCmpMode;
767 break;
769 case BUZ_MODE_STILL_DECOMPRESS:
770 reg = ZR36057_JMC_JPGExpMode;
771 break;
774 reg |= ZR36057_JMC_JPG;
775 if (zr->jpg_settings.field_per_buff == 1)
776 reg |= ZR36057_JMC_Fld_per_buff;
777 btwrite(reg, ZR36057_JMC);
779 /* vertical */
780 btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
781 reg = (6 << ZR36057_VSP_VsyncSize) |
782 (tvn->Ht << ZR36057_VSP_FrmTot);
783 btwrite(reg, ZR36057_VSP);
784 reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
785 (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
786 btwrite(reg, ZR36057_FVAP);
788 /* horizontal */
789 if (zr->card.vfe_pol.hsync_pol)
790 btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
791 else
792 btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
793 reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
794 (tvn->Wt << ZR36057_HSP_LineTot);
795 btwrite(reg, ZR36057_HSP);
796 reg = ((zr->jpg_settings.img_x +
797 tvn->HStart + 4) << ZR36057_FHAP_NAX) |
798 (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
799 btwrite(reg, ZR36057_FHAP);
801 /* field process parameters */
802 if (zr->jpg_settings.odd_even)
803 reg = ZR36057_FPP_Odd_Even;
804 else
805 reg = 0;
807 btwrite(reg, ZR36057_FPP);
809 /* Set proper VCLK Polarity, else colors will be wrong during playback */
810 //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
812 /* code base address */
813 reg = virt_to_bus(zr->stat_com);
814 btwrite(reg, ZR36057_JCBA);
816 /* FIFO threshold (FIFO is 160. double words) */
817 /* NOTE: decimal values here */
818 switch (mode) {
820 case BUZ_MODE_STILL_COMPRESS:
821 case BUZ_MODE_MOTION_COMPRESS:
822 if (zr->card.type != BUZ)
823 reg = 140;
824 else
825 reg = 60;
826 break;
828 case BUZ_MODE_STILL_DECOMPRESS:
829 case BUZ_MODE_MOTION_DECOMPRESS:
830 reg = 20;
831 break;
833 default:
834 reg = 80;
835 break;
838 btwrite(reg, ZR36057_JCFT);
839 zr36057_adjust_vfe(zr, mode);
843 void
844 print_interrupts (struct zoran *zr)
846 int res, noerr = 0;
848 printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
849 if ((res = zr->field_counter) < -1 || res > 1) {
850 printk(" FD:%d", res);
852 if ((res = zr->intr_counter_GIRQ1) != 0) {
853 printk(" GIRQ1:%d", res);
854 noerr++;
856 if ((res = zr->intr_counter_GIRQ0) != 0) {
857 printk(" GIRQ0:%d", res);
858 noerr++;
860 if ((res = zr->intr_counter_CodRepIRQ) != 0) {
861 printk(" CodRepIRQ:%d", res);
862 noerr++;
864 if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
865 printk(" JPEGRepIRQ:%d", res);
866 noerr++;
868 if (zr->JPEG_max_missed) {
869 printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
870 zr->JPEG_min_missed);
872 if (zr->END_event_missed) {
873 printk(" ENDs missed: %d", zr->END_event_missed);
875 //if (zr->jpg_queued_num) {
876 printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
877 zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
879 if (!noerr) {
880 printk(": no interrupts detected.");
882 printk("\n");
885 void
886 clear_interrupt_counters (struct zoran *zr)
888 zr->intr_counter_GIRQ1 = 0;
889 zr->intr_counter_GIRQ0 = 0;
890 zr->intr_counter_CodRepIRQ = 0;
891 zr->intr_counter_JPEGRepIRQ = 0;
892 zr->field_counter = 0;
893 zr->IRQ1_in = 0;
894 zr->IRQ1_out = 0;
895 zr->JPEG_in = 0;
896 zr->JPEG_out = 0;
897 zr->JPEG_0 = 0;
898 zr->JPEG_1 = 0;
899 zr->END_event_missed = 0;
900 zr->JPEG_missed = 0;
901 zr->JPEG_max_missed = 0;
902 zr->JPEG_min_missed = 0x7fffffff;
905 static u32
906 count_reset_interrupt (struct zoran *zr)
908 u32 isr;
910 if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
911 if (isr & ZR36057_ISR_GIRQ1) {
912 btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
913 zr->intr_counter_GIRQ1++;
915 if (isr & ZR36057_ISR_GIRQ0) {
916 btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
917 zr->intr_counter_GIRQ0++;
919 if (isr & ZR36057_ISR_CodRepIRQ) {
920 btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
921 zr->intr_counter_CodRepIRQ++;
923 if (isr & ZR36057_ISR_JPEGRepIRQ) {
924 btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
925 zr->intr_counter_JPEGRepIRQ++;
928 return isr;
931 void
932 jpeg_start (struct zoran *zr)
934 int reg;
936 zr->frame_num = 0;
938 /* deassert P_reset, disable code transfer, deassert Active */
939 btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
940 /* stop flushing the internal code buffer */
941 btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
942 /* enable code transfer */
943 btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
945 /* clear IRQs */
946 btwrite(IRQ_MASK, ZR36057_ISR);
947 /* enable the JPEG IRQs */
948 btwrite(zr->card.jpeg_int |
949 ZR36057_ICR_JPEGRepIRQ |
950 ZR36057_ICR_IntPinEn,
951 ZR36057_ICR);
953 set_frame(zr, 0); // \FRAME
955 /* set the JPEG codec guest ID */
956 reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
957 (0 << ZR36057_JCGI_JPEGuestReg);
958 btwrite(reg, ZR36057_JCGI);
960 if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
961 zr->card.video_codec == CODEC_TYPE_ZR36050) {
962 /* Enable processing on the ZR36016 */
963 if (zr->vfe)
964 zr36016_write(zr->vfe, 0, 1);
966 /* load the address of the GO register in the ZR36050 latch */
967 post_office_write(zr, 0, 0, 0);
970 /* assert Active */
971 btor(ZR36057_JPC_Active, ZR36057_JPC);
973 /* enable the Go generation */
974 btor(ZR36057_JMC_Go_en, ZR36057_JMC);
975 udelay(30);
977 set_frame(zr, 1); // /FRAME
979 dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
982 void
983 zr36057_enable_jpg (struct zoran *zr,
984 enum zoran_codec_mode mode)
986 static int zero;
987 static int one = 1;
988 struct vfe_settings cap;
989 int field_size =
990 zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
992 zr->codec_mode = mode;
994 cap.x = zr->jpg_settings.img_x;
995 cap.y = zr->jpg_settings.img_y;
996 cap.width = zr->jpg_settings.img_width;
997 cap.height = zr->jpg_settings.img_height;
998 cap.decimation =
999 zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
1000 cap.quality = zr->jpg_settings.jpg_comp.quality;
1002 switch (mode) {
1004 case BUZ_MODE_MOTION_COMPRESS: {
1005 struct jpeg_app_marker app;
1006 struct jpeg_com_marker com;
1008 /* In motion compress mode, the decoder output must be enabled, and
1009 * the video bus direction set to input.
1011 set_videobus_dir(zr, 0);
1012 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1013 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1015 /* Take the JPEG codec and the VFE out of sleep */
1016 jpeg_codec_sleep(zr, 0);
1018 /* set JPEG app/com marker */
1019 app.appn = zr->jpg_settings.jpg_comp.APPn;
1020 app.len = zr->jpg_settings.jpg_comp.APP_len;
1021 memcpy(app.data, zr->jpg_settings.jpg_comp.APP_data, 60);
1022 zr->codec->control(zr->codec, CODEC_S_JPEG_APP_DATA,
1023 sizeof(struct jpeg_app_marker), &app);
1025 com.len = zr->jpg_settings.jpg_comp.COM_len;
1026 memcpy(com.data, zr->jpg_settings.jpg_comp.COM_data, 60);
1027 zr->codec->control(zr->codec, CODEC_S_JPEG_COM_DATA,
1028 sizeof(struct jpeg_com_marker), &com);
1030 /* Setup the JPEG codec */
1031 zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
1032 sizeof(int), &field_size);
1033 zr->codec->set_video(zr->codec, zr->timing, &cap,
1034 &zr->card.vfe_pol);
1035 zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
1037 /* Setup the VFE */
1038 if (zr->vfe) {
1039 zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
1040 sizeof(int), &field_size);
1041 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1042 &zr->card.vfe_pol);
1043 zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
1046 init_jpeg_queue(zr);
1047 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1049 clear_interrupt_counters(zr);
1050 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
1051 ZR_DEVNAME(zr));
1052 break;
1055 case BUZ_MODE_MOTION_DECOMPRESS:
1056 /* In motion decompression mode, the decoder output must be disabled, and
1057 * the video bus direction set to output.
1059 decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
1060 set_videobus_dir(zr, 1);
1061 encoder_command(zr, ENCODER_SET_INPUT, &one);
1063 /* Take the JPEG codec and the VFE out of sleep */
1064 jpeg_codec_sleep(zr, 0);
1065 /* Setup the VFE */
1066 if (zr->vfe) {
1067 zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1068 &zr->card.vfe_pol);
1069 zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
1071 /* Setup the JPEG codec */
1072 zr->codec->set_video(zr->codec, zr->timing, &cap,
1073 &zr->card.vfe_pol);
1074 zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
1076 init_jpeg_queue(zr);
1077 zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
1079 clear_interrupt_counters(zr);
1080 dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
1081 ZR_DEVNAME(zr));
1082 break;
1084 case BUZ_MODE_IDLE:
1085 default:
1086 /* shut down processing */
1087 btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
1088 ZR36057_ICR);
1089 btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
1090 ZR36057_ISR);
1091 btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // \Go_en
1093 msleep(50);
1095 set_videobus_dir(zr, 0);
1096 set_frame(zr, 1); // /FRAME
1097 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // /CFlush
1098 btwrite(0, ZR36057_JPC); // \P_Reset,\CodTrnsEn,\Active
1099 btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1100 btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1101 jpeg_codec_reset(zr);
1102 jpeg_codec_sleep(zr, 1);
1103 zr36057_adjust_vfe(zr, mode);
1105 decoder_command(zr, DECODER_ENABLE_OUTPUT, &one);
1106 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1108 dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
1109 break;
1114 /* when this is called the spinlock must be held */
1115 void
1116 zoran_feed_stat_com (struct zoran *zr)
1118 /* move frames from pending queue to DMA */
1120 int frame, i, max_stat_com;
1122 max_stat_com =
1123 (zr->jpg_settings.TmpDcm ==
1124 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1126 while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
1127 zr->jpg_dma_head < zr->jpg_que_head) {
1129 frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1130 if (zr->jpg_settings.TmpDcm == 1) {
1131 /* fill 1 stat_com entry */
1132 i = (zr->jpg_dma_head -
1133 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1134 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1135 break;
1136 zr->stat_com[i] =
1137 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1138 } else {
1139 /* fill 2 stat_com entries */
1140 i = ((zr->jpg_dma_head -
1141 zr->jpg_err_shift) & 1) * 2;
1142 if (!(zr->stat_com[i] & cpu_to_le32(1)))
1143 break;
1144 zr->stat_com[i] =
1145 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1146 zr->stat_com[i + 1] =
1147 cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
1149 zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
1150 zr->jpg_dma_head++;
1153 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1154 zr->jpg_queued_num++;
1157 /* when this is called the spinlock must be held */
1158 static void
1159 zoran_reap_stat_com (struct zoran *zr)
1161 /* move frames from DMA queue to done queue */
1163 int i;
1164 u32 stat_com;
1165 unsigned int seq;
1166 unsigned int dif;
1167 struct zoran_jpg_buffer *buffer;
1168 int frame;
1170 /* In motion decompress we don't have a hardware frame counter,
1171 * we just count the interrupts here */
1173 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1174 zr->jpg_seq_num++;
1176 while (zr->jpg_dma_tail < zr->jpg_dma_head) {
1177 if (zr->jpg_settings.TmpDcm == 1)
1178 i = (zr->jpg_dma_tail -
1179 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1180 else
1181 i = ((zr->jpg_dma_tail -
1182 zr->jpg_err_shift) & 1) * 2 + 1;
1184 stat_com = le32_to_cpu(zr->stat_com[i]);
1186 if ((stat_com & 1) == 0) {
1187 return;
1189 frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1190 buffer = &zr->jpg_buffers.buffer[frame];
1191 do_gettimeofday(&buffer->bs.timestamp);
1193 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1194 buffer->bs.length = (stat_com & 0x7fffff) >> 1;
1196 /* update sequence number with the help of the counter in stat_com */
1198 seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
1199 dif = (seq - zr->jpg_seq_num) & 0xff;
1200 zr->jpg_seq_num += dif;
1201 } else {
1202 buffer->bs.length = 0;
1204 buffer->bs.seq =
1205 zr->jpg_settings.TmpDcm ==
1206 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1207 buffer->state = BUZ_STATE_DONE;
1209 zr->jpg_dma_tail++;
1213 static void
1214 error_handler (struct zoran *zr,
1215 u32 astat,
1216 u32 stat)
1218 /* This is JPEG error handling part */
1219 if ((zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) &&
1220 (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS)) {
1221 //dprintk(1, KERN_ERR "%s: Internal error: error handling request in mode %d\n", ZR_DEVNAME(zr), zr->codec_mode);
1222 return;
1225 if ((stat & 1) == 0 &&
1226 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
1227 zr->jpg_dma_tail - zr->jpg_que_tail >=
1228 zr->jpg_buffers.num_buffers) {
1229 /* No free buffers... */
1230 zoran_reap_stat_com(zr);
1231 zoran_feed_stat_com(zr);
1232 wake_up_interruptible(&zr->jpg_capq);
1233 zr->JPEG_missed = 0;
1234 return;
1237 if (zr->JPEG_error != 1) {
1239 * First entry: error just happened during normal operation
1241 * In BUZ_MODE_MOTION_COMPRESS:
1243 * Possible glitch in TV signal. In this case we should
1244 * stop the codec and wait for good quality signal before
1245 * restarting it to avoid further problems
1247 * In BUZ_MODE_MOTION_DECOMPRESS:
1249 * Bad JPEG frame: we have to mark it as processed (codec crashed
1250 * and was not able to do it itself), and to remove it from queue.
1252 btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1253 udelay(1);
1254 stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
1255 btwrite(0, ZR36057_JPC);
1256 btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1257 jpeg_codec_reset(zr);
1258 jpeg_codec_sleep(zr, 1);
1259 zr->JPEG_error = 1;
1260 zr->num_errors++;
1262 /* Report error */
1263 if (zr36067_debug > 1 && zr->num_errors <= 8) {
1264 long frame;
1265 frame =
1266 zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1267 printk(KERN_ERR
1268 "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
1269 ZR_DEVNAME(zr), stat, zr->last_isr,
1270 zr->jpg_que_tail, zr->jpg_dma_tail,
1271 zr->jpg_dma_head, zr->jpg_que_head,
1272 zr->jpg_seq_num, frame);
1273 printk("stat_com frames:");
1275 int i, j;
1276 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1277 for (i = 0;
1278 i < zr->jpg_buffers.num_buffers;
1279 i++) {
1280 if (le32_to_cpu(zr->stat_com[j]) ==
1281 zr->jpg_buffers.
1282 buffer[i].
1283 frag_tab_bus) {
1284 printk("% d->%d",
1285 j, i);
1289 printk("\n");
1292 /* Find an entry in stat_com and rotate contents */
1294 int i;
1296 if (zr->jpg_settings.TmpDcm == 1)
1297 i = (zr->jpg_dma_tail -
1298 zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1299 else
1300 i = ((zr->jpg_dma_tail -
1301 zr->jpg_err_shift) & 1) * 2;
1302 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1303 /* Mimic zr36067 operation */
1304 zr->stat_com[i] |= cpu_to_le32(1);
1305 if (zr->jpg_settings.TmpDcm != 1)
1306 zr->stat_com[i + 1] |= cpu_to_le32(1);
1307 /* Refill */
1308 zoran_reap_stat_com(zr);
1309 zoran_feed_stat_com(zr);
1310 wake_up_interruptible(&zr->jpg_capq);
1311 /* Find an entry in stat_com again after refill */
1312 if (zr->jpg_settings.TmpDcm == 1)
1313 i = (zr->jpg_dma_tail -
1314 zr->jpg_err_shift) &
1315 BUZ_MASK_STAT_COM;
1316 else
1317 i = ((zr->jpg_dma_tail -
1318 zr->jpg_err_shift) & 1) * 2;
1320 if (i) {
1321 /* Rotate stat_comm entries to make current entry first */
1322 int j;
1323 __le32 bus_addr[BUZ_NUM_STAT_COM];
1325 /* Here we are copying the stat_com array, which
1326 * is already in little endian format, so
1327 * no endian conversions here
1329 memcpy(bus_addr, zr->stat_com,
1330 sizeof(bus_addr));
1331 for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1332 zr->stat_com[j] =
1333 bus_addr[(i + j) &
1334 BUZ_MASK_STAT_COM];
1337 zr->jpg_err_shift += i;
1338 zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
1340 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
1341 zr->jpg_err_seq = zr->jpg_seq_num; /* + 1; */
1345 /* Now the stat_comm buffer is ready for restart */
1346 do {
1347 int status, mode;
1349 if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1350 decoder_command(zr, DECODER_GET_STATUS, &status);
1351 mode = CODEC_DO_COMPRESSION;
1352 } else {
1353 status = 0;
1354 mode = CODEC_DO_EXPANSION;
1356 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1357 (status & DECODER_STATUS_GOOD)) {
1358 /********** RESTART code *************/
1359 jpeg_codec_reset(zr);
1360 zr->codec->set_mode(zr->codec, mode);
1361 zr36057_set_jpg(zr, zr->codec_mode);
1362 jpeg_start(zr);
1364 if (zr->num_errors <= 8)
1365 dprintk(2, KERN_INFO "%s: Restart\n",
1366 ZR_DEVNAME(zr));
1368 zr->JPEG_missed = 0;
1369 zr->JPEG_error = 2;
1370 /********** End RESTART code ***********/
1372 } while (0);
1375 irqreturn_t
1376 zoran_irq (int irq,
1377 void *dev_id)
1379 u32 stat, astat;
1380 int count;
1381 struct zoran *zr;
1382 unsigned long flags;
1384 zr = dev_id;
1385 count = 0;
1387 if (zr->testing) {
1388 /* Testing interrupts */
1389 spin_lock_irqsave(&zr->spinlock, flags);
1390 while ((stat = count_reset_interrupt(zr))) {
1391 if (count++ > 100) {
1392 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1393 dprintk(1,
1394 KERN_ERR
1395 "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
1396 ZR_DEVNAME(zr), stat);
1397 wake_up_interruptible(&zr->test_q);
1400 zr->last_isr = stat;
1401 spin_unlock_irqrestore(&zr->spinlock, flags);
1402 return IRQ_HANDLED;
1405 spin_lock_irqsave(&zr->spinlock, flags);
1406 while (1) {
1407 /* get/clear interrupt status bits */
1408 stat = count_reset_interrupt(zr);
1409 astat = stat & IRQ_MASK;
1410 if (!astat) {
1411 break;
1413 dprintk(4,
1414 KERN_DEBUG
1415 "zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
1416 astat, btread(ZR36057_ICR));
1417 if (astat & zr->card.vsync_int) { // SW
1419 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1420 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1421 /* count missed interrupts */
1422 zr->JPEG_missed++;
1424 //post_office_read(zr,1,0);
1425 /* Interrupts may still happen when
1426 * zr->v4l_memgrab_active is switched off.
1427 * We simply ignore them */
1429 if (zr->v4l_memgrab_active) {
1431 /* A lot more checks should be here ... */
1432 if ((btread(ZR36057_VSSFGR) &
1433 ZR36057_VSSFGR_SnapShot) == 0)
1434 dprintk(1,
1435 KERN_WARNING
1436 "%s: BuzIRQ with SnapShot off ???\n",
1437 ZR_DEVNAME(zr));
1439 if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1440 /* There is a grab on a frame going on, check if it has finished */
1442 if ((btread(ZR36057_VSSFGR) &
1443 ZR36057_VSSFGR_FrameGrab) ==
1444 0) {
1445 /* it is finished, notify the user */
1447 zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1448 zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
1449 do_gettimeofday(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
1450 zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1451 zr->v4l_pend_tail++;
1455 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1456 wake_up_interruptible(&zr->v4l_capq);
1458 /* Check if there is another grab queued */
1460 if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1461 zr->v4l_pend_tail != zr->v4l_pend_head) {
1463 int frame = zr->v4l_pend[zr->v4l_pend_tail &
1464 V4L_MASK_FRAME];
1465 u32 reg;
1467 zr->v4l_grab_frame = frame;
1469 /* Set zr36057 video front end and enable video */
1471 /* Buffer address */
1473 reg =
1474 zr->v4l_buffers.buffer[frame].
1475 fbuffer_bus;
1476 btwrite(reg, ZR36057_VDTR);
1477 if (zr->v4l_settings.height >
1478 BUZ_MAX_HEIGHT / 2)
1479 reg +=
1480 zr->v4l_settings.
1481 bytesperline;
1482 btwrite(reg, ZR36057_VDBR);
1484 /* video stride, status, and frame grab register */
1485 reg = 0;
1486 if (zr->v4l_settings.height >
1487 BUZ_MAX_HEIGHT / 2)
1488 reg +=
1489 zr->v4l_settings.
1490 bytesperline;
1491 reg =
1492 (reg <<
1493 ZR36057_VSSFGR_DispStride);
1494 reg |= ZR36057_VSSFGR_VidOvf;
1495 reg |= ZR36057_VSSFGR_SnapShot;
1496 reg |= ZR36057_VSSFGR_FrameGrab;
1497 btwrite(reg, ZR36057_VSSFGR);
1499 btor(ZR36057_VDCR_VidEn,
1500 ZR36057_VDCR);
1504 /* even if we don't grab, we do want to increment
1505 * the sequence counter to see lost frames */
1506 zr->v4l_grab_seq++;
1508 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
1509 if (astat & ZR36057_ISR_CodRepIRQ) {
1510 zr->intr_counter_CodRepIRQ++;
1511 IDEBUG(printk
1512 (KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
1513 ZR_DEVNAME(zr)));
1514 btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
1516 #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
1518 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
1519 if (astat & ZR36057_ISR_JPEGRepIRQ) {
1521 if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1522 zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1523 if (zr36067_debug > 1 &&
1524 (!zr->frame_num || zr->JPEG_error)) {
1525 printk(KERN_INFO
1526 "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
1527 ZR_DEVNAME(zr), stat,
1528 zr->jpg_settings.odd_even,
1529 zr->jpg_settings.
1530 field_per_buff,
1531 zr->JPEG_missed);
1533 char sc[] = "0000";
1534 char sv[5];
1535 int i;
1536 strcpy(sv, sc);
1537 for (i = 0; i < 4; i++) {
1538 if (le32_to_cpu(zr->stat_com[i]) & 1)
1539 sv[i] = '1';
1541 sv[4] = 0;
1542 printk(KERN_INFO
1543 "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
1544 ZR_DEVNAME(zr), sv,
1545 zr->jpg_que_tail,
1546 zr->jpg_dma_tail,
1547 zr->jpg_dma_head,
1548 zr->jpg_que_head);
1550 } else {
1551 if (zr->JPEG_missed > zr->JPEG_max_missed) // Get statistics
1552 zr->JPEG_max_missed =
1553 zr->JPEG_missed;
1554 if (zr->JPEG_missed <
1555 zr->JPEG_min_missed)
1556 zr->JPEG_min_missed =
1557 zr->JPEG_missed;
1560 if (zr36067_debug > 2 && zr->frame_num < 6) {
1561 int i;
1562 printk("%s: seq=%ld stat_com:",
1563 ZR_DEVNAME(zr), zr->jpg_seq_num);
1564 for (i = 0; i < 4; i++) {
1565 printk(" %08x",
1566 le32_to_cpu(zr->stat_com[i]));
1568 printk("\n");
1570 zr->frame_num++;
1571 zr->JPEG_missed = 0;
1572 zr->JPEG_error = 0;
1573 zoran_reap_stat_com(zr);
1574 zoran_feed_stat_com(zr);
1575 wake_up_interruptible(&zr->jpg_capq);
1576 } /*else {
1577 dprintk(1,
1578 KERN_ERR
1579 "%s: JPEG interrupt while not in motion (de)compress mode!\n",
1580 ZR_DEVNAME(zr));
1583 #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
1585 /* DATERR, too many fields missed, error processing */
1586 if ((astat & zr->card.jpeg_int) ||
1587 zr->JPEG_missed > 25 ||
1588 zr->JPEG_error == 1 ||
1589 ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
1590 (zr->frame_num & (zr->JPEG_missed >
1591 zr->jpg_settings.field_per_buff)))) {
1592 error_handler(zr, astat, stat);
1595 count++;
1596 if (count > 10) {
1597 dprintk(2, KERN_WARNING "%s: irq loop %d\n",
1598 ZR_DEVNAME(zr), count);
1599 if (count > 20) {
1600 btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1601 dprintk(2,
1602 KERN_ERR
1603 "%s: IRQ lockup, cleared int mask\n",
1604 ZR_DEVNAME(zr));
1605 break;
1608 zr->last_isr = stat;
1610 spin_unlock_irqrestore(&zr->spinlock, flags);
1612 return IRQ_HANDLED;
1615 void
1616 zoran_set_pci_master (struct zoran *zr,
1617 int set_master)
1619 if (set_master) {
1620 pci_set_master(zr->pci_dev);
1621 } else {
1622 u16 command;
1624 pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
1625 command &= ~PCI_COMMAND_MASTER;
1626 pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
1630 void
1631 zoran_init_hardware (struct zoran *zr)
1633 int j, zero = 0;
1635 /* Enable bus-mastering */
1636 zoran_set_pci_master(zr, 1);
1638 /* Initialize the board */
1639 if (zr->card.init) {
1640 zr->card.init(zr);
1643 j = zr->card.input[zr->input].muxsel;
1645 decoder_command(zr, 0, NULL);
1646 decoder_command(zr, DECODER_SET_NORM, &zr->norm);
1647 decoder_command(zr, DECODER_SET_INPUT, &j);
1649 encoder_command(zr, 0, NULL);
1650 encoder_command(zr, ENCODER_SET_NORM, &zr->norm);
1651 encoder_command(zr, ENCODER_SET_INPUT, &zero);
1653 /* toggle JPEG codec sleep to sync PLL */
1654 jpeg_codec_sleep(zr, 1);
1655 jpeg_codec_sleep(zr, 0);
1657 /* set individual interrupt enables (without GIRQ1)
1658 * but don't global enable until zoran_open() */
1660 //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR); // SW
1661 // It looks like using only JPEGRepIRQEn is not always reliable,
1662 // may be when JPEG codec crashes it won't generate IRQ? So,
1663 /*CP*/ // btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM WHY ? LP
1664 zr36057_init_vfe(zr);
1666 zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1668 btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
1671 void
1672 zr36057_restart (struct zoran *zr)
1674 btwrite(0, ZR36057_SPGPPCR);
1675 mdelay(1);
1676 btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
1677 mdelay(1);
1679 /* assert P_Reset */
1680 btwrite(0, ZR36057_JPC);
1681 /* set up GPIO direction - all output */
1682 btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
1684 /* set up GPIO pins and guest bus timing */
1685 btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
1689 * initialize video front end
1692 static void
1693 zr36057_init_vfe (struct zoran *zr)
1695 u32 reg;
1697 reg = btread(ZR36057_VFESPFR);
1698 reg |= ZR36057_VFESPFR_LittleEndian;
1699 reg &= ~ZR36057_VFESPFR_VCLKPol;
1700 reg |= ZR36057_VFESPFR_ExtFl;
1701 reg |= ZR36057_VFESPFR_TopField;
1702 btwrite(reg, ZR36057_VFESPFR);
1703 reg = btread(ZR36057_VDCR);
1704 if (pci_pci_problems & PCIPCI_TRITON)
1705 // || zr->revision < 1) // Revision 1 has also Triton support
1706 reg &= ~ZR36057_VDCR_Triton;
1707 else
1708 reg |= ZR36057_VDCR_Triton;
1709 btwrite(reg, ZR36057_VDCR);
1713 * Interface to decoder and encoder chips using i2c bus
1717 decoder_command (struct zoran *zr,
1718 int cmd,
1719 void *data)
1721 if (zr->decoder == NULL)
1722 return -EIO;
1724 if (zr->card.type == LML33 &&
1725 (cmd == DECODER_SET_NORM || cmd == DECODER_SET_INPUT)) {
1726 int res;
1728 // Bt819 needs to reset its FIFO buffer using #FRST pin and
1729 // LML33 card uses GPIO(7) for that.
1730 GPIO(zr, 7, 0);
1731 res = zr->decoder->driver->command(zr->decoder, cmd, data);
1732 // Pull #FRST high.
1733 GPIO(zr, 7, 1);
1734 return res;
1735 } else
1736 return zr->decoder->driver->command(zr->decoder, cmd,
1737 data);
1741 encoder_command (struct zoran *zr,
1742 int cmd,
1743 void *data)
1745 if (zr->encoder == NULL)
1746 return -1;
1748 return zr->encoder->driver->command(zr->encoder, cmd, data);