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.
5  *
6  * This part handles device access (PCI/I2C/codec/...)
7  *
8  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9  *
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>
14  *
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.
19  *
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.
24  *
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.
28  */
29 
30 #include <linux/types.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/vmalloc.h>
34 #include <linux/ktime.h>
35 
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/videodev2.h>
41 #include <media/v4l2-common.h>
42 #include <linux/spinlock.h>
43 #include <linux/sem.h>
44 
45 #include <linux/pci.h>
46 #include <linux/delay.h>
47 #include <linux/wait.h>
48 
49 #include <asm/byteorder.h>
50 #include <asm/io.h>
51 
52 #include "videocodec.h"
53 #include "zoran.h"
54 #include "zoran_device.h"
55 #include "zoran_card.h"
56 
57 #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | \
58 		   ZR36057_ISR_GIRQ1 | \
59 		   ZR36057_ISR_JPEGRepIRQ )
60 
61 static bool lml33dpath;		/* default = 0
62 				 * 1 will use digital path in capture
63 				 * mode instead of analog. It can be
64 				 * used for picture adjustments using
65 				 * tool like xawtv while watching image
66 				 * on TV monitor connected to the output.
67 				 * However, due to absence of 75 Ohm
68 				 * load on Bt819 input, there will be
69 				 * some image imperfections */
70 
71 module_param(lml33dpath, bool, 0644);
72 MODULE_PARM_DESC(lml33dpath,
73 		 "Use digital path capture mode (on LML33 cards)");
74 
75 static void
76 zr36057_init_vfe (struct zoran *zr);
77 
78 /*
79  * General Purpose I/O and Guest bus access
80  */
81 
82 /*
83  * This is a bit tricky. When a board lacks a GPIO function, the corresponding
84  * GPIO bit number in the card_info structure is set to 0.
85  */
86 
87 void
88 GPIO (struct zoran *zr,
89       int           bit,
90       unsigned int  value)
91 {
92 	u32 reg;
93 	u32 mask;
94 
95 	/* Make sure the bit number is legal
96 	 * A bit number of -1 (lacking) gives a mask of 0,
97 	 * making it harmless */
98 	mask = (1 << (24 + bit)) & 0xff000000;
99 	reg = btread(ZR36057_GPPGCR1) & ~mask;
100 	if (value) {
101 		reg |= mask;
102 	}
103 	btwrite(reg, ZR36057_GPPGCR1);
104 	udelay(1);
105 }
106 
107 /*
108  * Wait til post office is no longer busy
109  */
110 
111 int
112 post_office_wait (struct zoran *zr)
113 {
114 	u32 por;
115 
116 //      while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
117 	while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
118 		/* wait for something to happen */
119 	}
120 	if ((por & ZR36057_POR_POTime) && !zr->card.gws_not_connected) {
121 		/* In LML33/BUZ \GWS line is not connected, so it has always timeout set */
122 		dprintk(1, KERN_INFO "%s: pop timeout %08x\n", ZR_DEVNAME(zr),
123 			por);
124 		return -1;
125 	}
126 
127 	return 0;
128 }
129 
130 int
131 post_office_write (struct zoran *zr,
132 		   unsigned int  guest,
133 		   unsigned int  reg,
134 		   unsigned int  value)
135 {
136 	u32 por;
137 
138 	por =
139 	    ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
140 	    ((reg & 7) << 16) | (value & 0xFF);
141 	btwrite(por, ZR36057_POR);
142 
143 	return post_office_wait(zr);
144 }
145 
146 int
147 post_office_read (struct zoran *zr,
148 		  unsigned int  guest,
149 		  unsigned int  reg)
150 {
151 	u32 por;
152 
153 	por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
154 	btwrite(por, ZR36057_POR);
155 	if (post_office_wait(zr) < 0) {
156 		return -1;
157 	}
158 
159 	return btread(ZR36057_POR) & 0xFF;
160 }
161 
162 /*
163  * detect guests
164  */
165 
166 static void
167 dump_guests (struct zoran *zr)
168 {
169 	if (zr36067_debug > 2) {
170 		int i, guest[8];
171 
172 		for (i = 1; i < 8; i++) {	// Don't read jpeg codec here
173 			guest[i] = post_office_read(zr, i, 0);
174 		}
175 
176 		printk(KERN_INFO "%s: Guests: %*ph\n",
177 		       ZR_DEVNAME(zr), 8, guest);
178 	}
179 }
180 
181 void
182 detect_guest_activity (struct zoran *zr)
183 {
184 	int timeout, i, j, res, guest[8], guest0[8], change[8][3];
185 	ktime_t t0, t1;
186 
187 	dump_guests(zr);
188 	printk(KERN_INFO "%s: Detecting guests activity, please wait...\n",
189 	       ZR_DEVNAME(zr));
190 	for (i = 1; i < 8; i++) {	// Don't read jpeg codec here
191 		guest0[i] = guest[i] = post_office_read(zr, i, 0);
192 	}
193 
194 	timeout = 0;
195 	j = 0;
196 	t0 = ktime_get();
197 	while (timeout < 10000) {
198 		udelay(10);
199 		timeout++;
200 		for (i = 1; (i < 8) && (j < 8); i++) {
201 			res = post_office_read(zr, i, 0);
202 			if (res != guest[i]) {
203 				t1 = ktime_get();
204 				change[j][0] = ktime_to_us(ktime_sub(t1, t0));
205 				t0 = t1;
206 				change[j][1] = i;
207 				change[j][2] = res;
208 				j++;
209 				guest[i] = res;
210 			}
211 		}
212 		if (j >= 8)
213 			break;
214 	}
215 
216 	printk(KERN_INFO "%s: Guests: %*ph\n", ZR_DEVNAME(zr), 8, guest0);
217 
218 	if (j == 0) {
219 		printk(KERN_INFO "%s: No activity detected.\n", ZR_DEVNAME(zr));
220 		return;
221 	}
222 	for (i = 0; i < j; i++) {
223 		printk(KERN_INFO "%s: %6d: %d => 0x%02x\n", ZR_DEVNAME(zr),
224 		       change[i][0], change[i][1], change[i][2]);
225 	}
226 }
227 
228 /*
229  * JPEG Codec access
230  */
231 
232 void
233 jpeg_codec_sleep (struct zoran *zr,
234 		  int           sleep)
235 {
236 	GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_SLEEP], !sleep);
237 	if (!sleep) {
238 		dprintk(3,
239 			KERN_DEBUG
240 			"%s: jpeg_codec_sleep() - wake GPIO=0x%08x\n",
241 			ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
242 		udelay(500);
243 	} else {
244 		dprintk(3,
245 			KERN_DEBUG
246 			"%s: jpeg_codec_sleep() - sleep GPIO=0x%08x\n",
247 			ZR_DEVNAME(zr), btread(ZR36057_GPPGCR1));
248 		udelay(2);
249 	}
250 }
251 
252 int
253 jpeg_codec_reset (struct zoran *zr)
254 {
255 	/* Take the codec out of sleep */
256 	jpeg_codec_sleep(zr, 0);
257 
258 	if (zr->card.gpcs[GPCS_JPEG_RESET] != 0xff) {
259 		post_office_write(zr, zr->card.gpcs[GPCS_JPEG_RESET], 0,
260 				  0);
261 		udelay(2);
262 	} else {
263 		GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 0);
264 		udelay(2);
265 		GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_RESET], 1);
266 		udelay(2);
267 	}
268 
269 	return 0;
270 }
271 
272 /*
273  *   Set the registers for the size we have specified. Don't bother
274  *   trying to understand this without the ZR36057 manual in front of
275  *   you [AC].
276  *
277  *   PS: The manual is free for download in .pdf format from
278  *   www.zoran.com - nicely done those folks.
279  */
280 
281 static void
282 zr36057_adjust_vfe (struct zoran          *zr,
283 		    enum zoran_codec_mode  mode)
284 {
285 	u32 reg;
286 
287 	switch (mode) {
288 	case BUZ_MODE_MOTION_DECOMPRESS:
289 		btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
290 		reg = btread(ZR36057_VFEHCR);
291 		if ((reg & (1 << 10)) && zr->card.type != LML33R10) {
292 			reg += ((1 << 10) | 1);
293 		}
294 		btwrite(reg, ZR36057_VFEHCR);
295 		break;
296 	case BUZ_MODE_MOTION_COMPRESS:
297 	case BUZ_MODE_IDLE:
298 	default:
299 		if ((zr->norm & V4L2_STD_NTSC) ||
300 		    (zr->card.type == LML33R10 &&
301 		     (zr->norm & V4L2_STD_PAL)))
302 			btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
303 		else
304 			btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
305 		reg = btread(ZR36057_VFEHCR);
306 		if (!(reg & (1 << 10)) && zr->card.type != LML33R10) {
307 			reg -= ((1 << 10) | 1);
308 		}
309 		btwrite(reg, ZR36057_VFEHCR);
310 		break;
311 	}
312 }
313 
314 /*
315  * set geometry
316  */
317 
318 static void
319 zr36057_set_vfe (struct zoran              *zr,
320 		 int                        video_width,
321 		 int                        video_height,
322 		 const struct zoran_format *format)
323 {
324 	struct tvnorm *tvn;
325 	unsigned HStart, HEnd, VStart, VEnd;
326 	unsigned DispMode;
327 	unsigned VidWinWid, VidWinHt;
328 	unsigned hcrop1, hcrop2, vcrop1, vcrop2;
329 	unsigned Wa, We, Ha, He;
330 	unsigned X, Y, HorDcm, VerDcm;
331 	u32 reg;
332 	unsigned mask_line_size;
333 
334 	tvn = zr->timing;
335 
336 	Wa = tvn->Wa;
337 	Ha = tvn->Ha;
338 
339 	dprintk(2, KERN_INFO "%s: set_vfe() - width = %d, height = %d\n",
340 		ZR_DEVNAME(zr), video_width, video_height);
341 
342 	if (video_width < BUZ_MIN_WIDTH ||
343 	    video_height < BUZ_MIN_HEIGHT ||
344 	    video_width > Wa || video_height > Ha) {
345 		dprintk(1, KERN_ERR "%s: set_vfe: w=%d h=%d not valid\n",
346 			ZR_DEVNAME(zr), video_width, video_height);
347 		return;
348 	}
349 
350 	/**** zr36057 ****/
351 
352 	/* horizontal */
353 	VidWinWid = video_width;
354 	X = DIV_ROUND_UP(VidWinWid * 64, tvn->Wa);
355 	We = (VidWinWid * 64) / X;
356 	HorDcm = 64 - X;
357 	hcrop1 = 2 * ((tvn->Wa - We) / 4);
358 	hcrop2 = tvn->Wa - We - hcrop1;
359 	HStart = tvn->HStart ? tvn->HStart : 1;
360 	/* (Ronald) Original comment:
361 	 * "| 1 Doesn't have any effect, tested on both a DC10 and a DC10+"
362 	 * this is false. It inverses chroma values on the LML33R10 (so Cr
363 	 * suddenly is shown as Cb and reverse, really cool effect if you
364 	 * want to see blue faces, not useful otherwise). So don't use |1.
365 	 * However, the DC10 has '0' as HStart, but does need |1, so we
366 	 * use a dirty check...
367 	 */
368 	HEnd = HStart + tvn->Wa - 1;
369 	HStart += hcrop1;
370 	HEnd -= hcrop2;
371 	reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
372 	    | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
373 	if (zr->card.vfe_pol.hsync_pol)
374 		reg |= ZR36057_VFEHCR_HSPol;
375 	btwrite(reg, ZR36057_VFEHCR);
376 
377 	/* Vertical */
378 	DispMode = !(video_height > BUZ_MAX_HEIGHT / 2);
379 	VidWinHt = DispMode ? video_height : video_height / 2;
380 	Y = DIV_ROUND_UP(VidWinHt * 64 * 2, tvn->Ha);
381 	He = (VidWinHt * 64) / Y;
382 	VerDcm = 64 - Y;
383 	vcrop1 = (tvn->Ha / 2 - He) / 2;
384 	vcrop2 = tvn->Ha / 2 - He - vcrop1;
385 	VStart = tvn->VStart;
386 	VEnd = VStart + tvn->Ha / 2;	// - 1; FIXME SnapShot times out with -1 in 768*576 on the DC10 - LP
387 	VStart += vcrop1;
388 	VEnd -= vcrop2;
389 	reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
390 	    | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
391 	if (zr->card.vfe_pol.vsync_pol)
392 		reg |= ZR36057_VFEVCR_VSPol;
393 	btwrite(reg, ZR36057_VFEVCR);
394 
395 	/* scaler and pixel format */
396 	reg = 0;
397 	reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
398 	reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
399 	reg |= (DispMode << ZR36057_VFESPFR_DispMode);
400 	/* RJ: I don't know, why the following has to be the opposite
401 	 * of the corresponding ZR36060 setting, but only this way
402 	 * we get the correct colors when uncompressing to the screen  */
403 	//reg |= ZR36057_VFESPFR_VCLKPol; /**/
404 	/* RJ: Don't know if that is needed for NTSC also */
405 	if (!(zr->norm & V4L2_STD_NTSC))
406 		reg |= ZR36057_VFESPFR_ExtFl;	// NEEDED!!!!!!! Wolfgang
407 	reg |= ZR36057_VFESPFR_TopField;
408 	if (HorDcm >= 48) {
409 		reg |= 3 << ZR36057_VFESPFR_HFilter;	/* 5 tap filter */
410 	} else if (HorDcm >= 32) {
411 		reg |= 2 << ZR36057_VFESPFR_HFilter;	/* 4 tap filter */
412 	} else if (HorDcm >= 16) {
413 		reg |= 1 << ZR36057_VFESPFR_HFilter;	/* 3 tap filter */
414 	}
415 	reg |= format->vfespfr;
416 	btwrite(reg, ZR36057_VFESPFR);
417 
418 	/* display configuration */
419 	reg = (16 << ZR36057_VDCR_MinPix)
420 	    | (VidWinHt << ZR36057_VDCR_VidWinHt)
421 	    | (VidWinWid << ZR36057_VDCR_VidWinWid);
422 	if (pci_pci_problems & PCIPCI_TRITON)
423 		// || zr->revision < 1) // Revision 1 has also Triton support
424 		reg &= ~ZR36057_VDCR_Triton;
425 	else
426 		reg |= ZR36057_VDCR_Triton;
427 	btwrite(reg, ZR36057_VDCR);
428 
429 	/* (Ronald) don't write this if overlay_mask = NULL */
430 	if (zr->overlay_mask) {
431 		/* Write overlay clipping mask data, but don't enable overlay clipping */
432 		/* RJ: since this makes only sense on the screen, we use
433 		 * zr->overlay_settings.width instead of video_width */
434 
435 		mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
436 		reg = virt_to_bus(zr->overlay_mask);
437 		btwrite(reg, ZR36057_MMTR);
438 		reg = virt_to_bus(zr->overlay_mask + mask_line_size);
439 		btwrite(reg, ZR36057_MMBR);
440 		reg =
441 		    mask_line_size - (zr->overlay_settings.width +
442 				      31) / 32;
443 		if (DispMode == 0)
444 			reg += mask_line_size;
445 		reg <<= ZR36057_OCR_MaskStride;
446 		btwrite(reg, ZR36057_OCR);
447 	}
448 
449 	zr36057_adjust_vfe(zr, zr->codec_mode);
450 }
451 
452 /*
453  * Switch overlay on or off
454  */
455 
456 void
457 zr36057_overlay (struct zoran *zr,
458 		 int           on)
459 {
460 	u32 reg;
461 
462 	if (on) {
463 		/* do the necessary settings ... */
464 		btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);	/* switch it off first */
465 
466 		zr36057_set_vfe(zr,
467 				zr->overlay_settings.width,
468 				zr->overlay_settings.height,
469 				zr->overlay_settings.format);
470 
471 		/* Start and length of each line MUST be 4-byte aligned.
472 		 * This should be already checked before the call to this routine.
473 		 * All error messages are internal driver checking only! */
474 
475 		/* video display top and bottom registers */
476 		reg = (long) zr->vbuf_base +
477 		    zr->overlay_settings.x *
478 		    ((zr->overlay_settings.format->depth + 7) / 8) +
479 		    zr->overlay_settings.y *
480 		    zr->vbuf_bytesperline;
481 		btwrite(reg, ZR36057_VDTR);
482 		if (reg & 3)
483 			dprintk(1,
484 				KERN_ERR
485 				"%s: zr36057_overlay() - video_address not aligned\n",
486 				ZR_DEVNAME(zr));
487 		if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
488 			reg += zr->vbuf_bytesperline;
489 		btwrite(reg, ZR36057_VDBR);
490 
491 		/* video stride, status, and frame grab register */
492 		reg = zr->vbuf_bytesperline -
493 		    zr->overlay_settings.width *
494 		    ((zr->overlay_settings.format->depth + 7) / 8);
495 		if (zr->overlay_settings.height > BUZ_MAX_HEIGHT / 2)
496 			reg += zr->vbuf_bytesperline;
497 		if (reg & 3)
498 			dprintk(1,
499 				KERN_ERR
500 				"%s: zr36057_overlay() - video_stride not aligned\n",
501 				ZR_DEVNAME(zr));
502 		reg = (reg << ZR36057_VSSFGR_DispStride);
503 		reg |= ZR36057_VSSFGR_VidOvf;	/* clear overflow status */
504 		btwrite(reg, ZR36057_VSSFGR);
505 
506 		/* Set overlay clipping */
507 		if (zr->overlay_settings.clipcount > 0)
508 			btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
509 
510 		/* ... and switch it on */
511 		btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
512 	} else {
513 		/* Switch it off */
514 		btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
515 	}
516 }
517 
518 /*
519  * The overlay mask has one bit for each pixel on a scan line,
520  *  and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
521  */
522 
523 void write_overlay_mask(struct zoran_fh *fh, struct v4l2_clip *vp, int count)
524 {
525 	struct zoran *zr = fh->zr;
526 	unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
527 	u32 *mask;
528 	int x, y, width, height;
529 	unsigned i, j, k;
530 
531 	/* fill mask with one bits */
532 	memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
533 
534 	for (i = 0; i < count; ++i) {
535 		/* pick up local copy of clip */
536 		x = vp[i].c.left;
537 		y = vp[i].c.top;
538 		width = vp[i].c.width;
539 		height = vp[i].c.height;
540 
541 		/* trim clips that extend beyond the window */
542 		if (x < 0) {
543 			width += x;
544 			x = 0;
545 		}
546 		if (y < 0) {
547 			height += y;
548 			y = 0;
549 		}
550 		if (x + width > fh->overlay_settings.width) {
551 			width = fh->overlay_settings.width - x;
552 		}
553 		if (y + height > fh->overlay_settings.height) {
554 			height = fh->overlay_settings.height - y;
555 		}
556 
557 		/* ignore degenerate clips */
558 		if (height <= 0) {
559 			continue;
560 		}
561 		if (width <= 0) {
562 			continue;
563 		}
564 
565 		/* apply clip for each scan line */
566 		for (j = 0; j < height; ++j) {
567 			/* reset bit for each pixel */
568 			/* this can be optimized later if need be */
569 			mask = fh->overlay_mask + (y + j) * mask_line_size;
570 			for (k = 0; k < width; ++k) {
571 				mask[(x + k) / 32] &=
572 				    ~((u32) 1 << (x + k) % 32);
573 			}
574 		}
575 	}
576 }
577 
578 /* Enable/Disable uncompressed memory grabbing of the 36057 */
579 
580 void
581 zr36057_set_memgrab (struct zoran *zr,
582 		     int           mode)
583 {
584 	if (mode) {
585 		/* We only check SnapShot and not FrameGrab here.  SnapShot==1
586 		 * means a capture is already in progress, but FrameGrab==1
587 		 * doesn't necessary mean that.  It's more correct to say a 1
588 		 * to 0 transition indicates a capture completed.  If a
589 		 * capture is pending when capturing is tuned off, FrameGrab
590 		 * will be stuck at 1 until capturing is turned back on.
591 		 */
592 		if (btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot)
593 			dprintk(1,
594 				KERN_WARNING
595 				"%s: zr36057_set_memgrab(1) with SnapShot on!?\n",
596 				ZR_DEVNAME(zr));
597 
598 		/* switch on VSync interrupts */
599 		btwrite(IRQ_MASK, ZR36057_ISR);	// Clear Interrupts
600 		btor(zr->card.vsync_int, ZR36057_ICR);	// SW
601 
602 		/* enable SnapShot */
603 		btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
604 
605 		/* Set zr36057 video front end  and enable video */
606 		zr36057_set_vfe(zr, zr->v4l_settings.width,
607 				zr->v4l_settings.height,
608 				zr->v4l_settings.format);
609 
610 		zr->v4l_memgrab_active = 1;
611 	} else {
612 		/* switch off VSync interrupts */
613 		btand(~zr->card.vsync_int, ZR36057_ICR);	// SW
614 
615 		zr->v4l_memgrab_active = 0;
616 		zr->v4l_grab_frame = NO_GRAB_ACTIVE;
617 
618 		/* reenable grabbing to screen if it was running */
619 		if (zr->v4l_overlay_active) {
620 			zr36057_overlay(zr, 1);
621 		} else {
622 			btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
623 			btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
624 		}
625 	}
626 }
627 
628 int
629 wait_grab_pending (struct zoran *zr)
630 {
631 	unsigned long flags;
632 
633 	/* wait until all pending grabs are finished */
634 
635 	if (!zr->v4l_memgrab_active)
636 		return 0;
637 
638 	wait_event_interruptible(zr->v4l_capq,
639 			(zr->v4l_pend_tail == zr->v4l_pend_head));
640 	if (signal_pending(current))
641 		return -ERESTARTSYS;
642 
643 	spin_lock_irqsave(&zr->spinlock, flags);
644 	zr36057_set_memgrab(zr, 0);
645 	spin_unlock_irqrestore(&zr->spinlock, flags);
646 
647 	return 0;
648 }
649 
650 /*****************************************************************************
651  *                                                                           *
652  *  Set up the Buz-specific MJPEG part                                       *
653  *                                                                           *
654  *****************************************************************************/
655 
656 static inline void
657 set_frame (struct zoran *zr,
658 	   int           val)
659 {
660 	GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_FRAME], val);
661 }
662 
663 static void
664 set_videobus_dir (struct zoran *zr,
665 		  int           val)
666 {
667 	switch (zr->card.type) {
668 	case LML33:
669 	case LML33R10:
670 		if (!lml33dpath)
671 			GPIO(zr, 5, val);
672 		else
673 			GPIO(zr, 5, 1);
674 		break;
675 	default:
676 		GPIO(zr, zr->card.gpio[ZR_GPIO_VID_DIR],
677 		     zr->card.gpio_pol[ZR_GPIO_VID_DIR] ? !val : val);
678 		break;
679 	}
680 }
681 
682 static void
683 init_jpeg_queue (struct zoran *zr)
684 {
685 	int i;
686 
687 	/* re-initialize DMA ring stuff */
688 	zr->jpg_que_head = 0;
689 	zr->jpg_dma_head = 0;
690 	zr->jpg_dma_tail = 0;
691 	zr->jpg_que_tail = 0;
692 	zr->jpg_seq_num = 0;
693 	zr->JPEG_error = 0;
694 	zr->num_errors = 0;
695 	zr->jpg_err_seq = 0;
696 	zr->jpg_err_shift = 0;
697 	zr->jpg_queued_num = 0;
698 	for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
699 		zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER;	/* nothing going on */
700 	}
701 	for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
702 		zr->stat_com[i] = cpu_to_le32(1);	/* mark as unavailable to zr36057 */
703 	}
704 }
705 
706 static void
707 zr36057_set_jpg (struct zoran          *zr,
708 		 enum zoran_codec_mode  mode)
709 {
710 	struct tvnorm *tvn;
711 	u32 reg;
712 
713 	tvn = zr->timing;
714 
715 	/* assert P_Reset, disable code transfer, deassert Active */
716 	btwrite(0, ZR36057_JPC);
717 
718 	/* MJPEG compression mode */
719 	switch (mode) {
720 
721 	case BUZ_MODE_MOTION_COMPRESS:
722 	default:
723 		reg = ZR36057_JMC_MJPGCmpMode;
724 		break;
725 
726 	case BUZ_MODE_MOTION_DECOMPRESS:
727 		reg = ZR36057_JMC_MJPGExpMode;
728 		reg |= ZR36057_JMC_SyncMstr;
729 		/* RJ: The following is experimental - improves the output to screen */
730 		//if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
731 		break;
732 
733 	case BUZ_MODE_STILL_COMPRESS:
734 		reg = ZR36057_JMC_JPGCmpMode;
735 		break;
736 
737 	case BUZ_MODE_STILL_DECOMPRESS:
738 		reg = ZR36057_JMC_JPGExpMode;
739 		break;
740 
741 	}
742 	reg |= ZR36057_JMC_JPG;
743 	if (zr->jpg_settings.field_per_buff == 1)
744 		reg |= ZR36057_JMC_Fld_per_buff;
745 	btwrite(reg, ZR36057_JMC);
746 
747 	/* vertical */
748 	btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
749 	reg = (6 << ZR36057_VSP_VsyncSize) |
750 	      (tvn->Ht << ZR36057_VSP_FrmTot);
751 	btwrite(reg, ZR36057_VSP);
752 	reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
753 	      (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
754 	btwrite(reg, ZR36057_FVAP);
755 
756 	/* horizontal */
757 	if (zr->card.vfe_pol.hsync_pol)
758 		btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
759 	else
760 		btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
761 	reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
762 	      (tvn->Wt << ZR36057_HSP_LineTot);
763 	btwrite(reg, ZR36057_HSP);
764 	reg = ((zr->jpg_settings.img_x +
765 		tvn->HStart + 4) << ZR36057_FHAP_NAX) |
766 	      (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
767 	btwrite(reg, ZR36057_FHAP);
768 
769 	/* field process parameters */
770 	if (zr->jpg_settings.odd_even)
771 		reg = ZR36057_FPP_Odd_Even;
772 	else
773 		reg = 0;
774 
775 	btwrite(reg, ZR36057_FPP);
776 
777 	/* Set proper VCLK Polarity, else colors will be wrong during playback */
778 	//btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
779 
780 	/* code base address */
781 	reg = virt_to_bus(zr->stat_com);
782 	btwrite(reg, ZR36057_JCBA);
783 
784 	/* FIFO threshold (FIFO is 160. double words) */
785 	/* NOTE: decimal values here */
786 	switch (mode) {
787 
788 	case BUZ_MODE_STILL_COMPRESS:
789 	case BUZ_MODE_MOTION_COMPRESS:
790 		if (zr->card.type != BUZ)
791 			reg = 140;
792 		else
793 			reg = 60;
794 		break;
795 
796 	case BUZ_MODE_STILL_DECOMPRESS:
797 	case BUZ_MODE_MOTION_DECOMPRESS:
798 		reg = 20;
799 		break;
800 
801 	default:
802 		reg = 80;
803 		break;
804 
805 	}
806 	btwrite(reg, ZR36057_JCFT);
807 	zr36057_adjust_vfe(zr, mode);
808 
809 }
810 
811 void
812 print_interrupts (struct zoran *zr)
813 {
814 	int res, noerr = 0;
815 
816 	printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
817 	if ((res = zr->field_counter) < -1 || res > 1) {
818 		printk(KERN_CONT " FD:%d", res);
819 	}
820 	if ((res = zr->intr_counter_GIRQ1) != 0) {
821 		printk(KERN_CONT " GIRQ1:%d", res);
822 		noerr++;
823 	}
824 	if ((res = zr->intr_counter_GIRQ0) != 0) {
825 		printk(KERN_CONT " GIRQ0:%d", res);
826 		noerr++;
827 	}
828 	if ((res = zr->intr_counter_CodRepIRQ) != 0) {
829 		printk(KERN_CONT " CodRepIRQ:%d", res);
830 		noerr++;
831 	}
832 	if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
833 		printk(KERN_CONT " JPEGRepIRQ:%d", res);
834 		noerr++;
835 	}
836 	if (zr->JPEG_max_missed) {
837 		printk(KERN_CONT " JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
838 		       zr->JPEG_min_missed);
839 	}
840 	if (zr->END_event_missed) {
841 		printk(KERN_CONT " ENDs missed: %d", zr->END_event_missed);
842 	}
843 	//if (zr->jpg_queued_num) {
844 	printk(KERN_CONT " queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
845 	       zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
846 	//}
847 	if (!noerr) {
848 		printk(KERN_CONT ": no interrupts detected.");
849 	}
850 	printk(KERN_CONT "\n");
851 }
852 
853 void
854 clear_interrupt_counters (struct zoran *zr)
855 {
856 	zr->intr_counter_GIRQ1 = 0;
857 	zr->intr_counter_GIRQ0 = 0;
858 	zr->intr_counter_CodRepIRQ = 0;
859 	zr->intr_counter_JPEGRepIRQ = 0;
860 	zr->field_counter = 0;
861 	zr->IRQ1_in = 0;
862 	zr->IRQ1_out = 0;
863 	zr->JPEG_in = 0;
864 	zr->JPEG_out = 0;
865 	zr->JPEG_0 = 0;
866 	zr->JPEG_1 = 0;
867 	zr->END_event_missed = 0;
868 	zr->JPEG_missed = 0;
869 	zr->JPEG_max_missed = 0;
870 	zr->JPEG_min_missed = 0x7fffffff;
871 }
872 
873 static u32
874 count_reset_interrupt (struct zoran *zr)
875 {
876 	u32 isr;
877 
878 	if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
879 		if (isr & ZR36057_ISR_GIRQ1) {
880 			btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
881 			zr->intr_counter_GIRQ1++;
882 		}
883 		if (isr & ZR36057_ISR_GIRQ0) {
884 			btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
885 			zr->intr_counter_GIRQ0++;
886 		}
887 		if (isr & ZR36057_ISR_CodRepIRQ) {
888 			btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
889 			zr->intr_counter_CodRepIRQ++;
890 		}
891 		if (isr & ZR36057_ISR_JPEGRepIRQ) {
892 			btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
893 			zr->intr_counter_JPEGRepIRQ++;
894 		}
895 	}
896 	return isr;
897 }
898 
899 void
900 jpeg_start (struct zoran *zr)
901 {
902 	int reg;
903 
904 	zr->frame_num = 0;
905 
906 	/* deassert P_reset, disable code transfer, deassert Active */
907 	btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
908 	/* stop flushing the internal code buffer */
909 	btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
910 	/* enable code transfer */
911 	btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
912 
913 	/* clear IRQs */
914 	btwrite(IRQ_MASK, ZR36057_ISR);
915 	/* enable the JPEG IRQs */
916 	btwrite(zr->card.jpeg_int |
917 			ZR36057_ICR_JPEGRepIRQ |
918 			ZR36057_ICR_IntPinEn,
919 		ZR36057_ICR);
920 
921 	set_frame(zr, 0);	// \FRAME
922 
923 	/* set the JPEG codec guest ID */
924 	reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
925 	       (0 << ZR36057_JCGI_JPEGuestReg);
926 	btwrite(reg, ZR36057_JCGI);
927 
928 	if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
929 	    zr->card.video_codec == CODEC_TYPE_ZR36050) {
930 		/* Enable processing on the ZR36016 */
931 		if (zr->vfe)
932 			zr36016_write(zr->vfe, 0, 1);
933 
934 		/* load the address of the GO register in the ZR36050 latch */
935 		post_office_write(zr, 0, 0, 0);
936 	}
937 
938 	/* assert Active */
939 	btor(ZR36057_JPC_Active, ZR36057_JPC);
940 
941 	/* enable the Go generation */
942 	btor(ZR36057_JMC_Go_en, ZR36057_JMC);
943 	udelay(30);
944 
945 	set_frame(zr, 1);	// /FRAME
946 
947 	dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
948 }
949 
950 void
951 zr36057_enable_jpg (struct zoran          *zr,
952 		    enum zoran_codec_mode  mode)
953 {
954 	struct vfe_settings cap;
955 	int field_size =
956 	    zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
957 
958 	zr->codec_mode = mode;
959 
960 	cap.x = zr->jpg_settings.img_x;
961 	cap.y = zr->jpg_settings.img_y;
962 	cap.width = zr->jpg_settings.img_width;
963 	cap.height = zr->jpg_settings.img_height;
964 	cap.decimation =
965 	    zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
966 	cap.quality = zr->jpg_settings.jpg_comp.quality;
967 
968 	switch (mode) {
969 
970 	case BUZ_MODE_MOTION_COMPRESS: {
971 		struct jpeg_app_marker app;
972 		struct jpeg_com_marker com;
973 
974 		/* In motion compress mode, the decoder output must be enabled, and
975 		 * the video bus direction set to input.
976 		 */
977 		set_videobus_dir(zr, 0);
978 		decoder_call(zr, video, s_stream, 1);
979 		encoder_call(zr, video, s_routing, 0, 0, 0);
980 
981 		/* Take the JPEG codec and the VFE out of sleep */
982 		jpeg_codec_sleep(zr, 0);
983 
984 		/* set JPEG app/com marker */
985 		app.appn = zr->jpg_settings.jpg_comp.APPn;
986 		app.len = zr->jpg_settings.jpg_comp.APP_len;
987 		memcpy(app.data, zr->jpg_settings.jpg_comp.APP_data, 60);
988 		zr->codec->control(zr->codec, CODEC_S_JPEG_APP_DATA,
989 				   sizeof(struct jpeg_app_marker), &app);
990 
991 		com.len = zr->jpg_settings.jpg_comp.COM_len;
992 		memcpy(com.data, zr->jpg_settings.jpg_comp.COM_data, 60);
993 		zr->codec->control(zr->codec, CODEC_S_JPEG_COM_DATA,
994 				   sizeof(struct jpeg_com_marker), &com);
995 
996 		/* Setup the JPEG codec */
997 		zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
998 				   sizeof(int), &field_size);
999 		zr->codec->set_video(zr->codec, zr->timing, &cap,
1000 				     &zr->card.vfe_pol);
1001 		zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
1002 
1003 		/* Setup the VFE */
1004 		if (zr->vfe) {
1005 			zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
1006 					 sizeof(int), &field_size);
1007 			zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1008 					   &zr->card.vfe_pol);
1009 			zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
1010 		}
1011 
1012 		init_jpeg_queue(zr);
1013 		zr36057_set_jpg(zr, mode);	// \P_Reset, ... Video param, FIFO
1014 
1015 		clear_interrupt_counters(zr);
1016 		dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
1017 			ZR_DEVNAME(zr));
1018 		break;
1019 	}
1020 
1021 	case BUZ_MODE_MOTION_DECOMPRESS:
1022 		/* In motion decompression mode, the decoder output must be disabled, and
1023 		 * the video bus direction set to output.
1024 		 */
1025 		decoder_call(zr, video, s_stream, 0);
1026 		set_videobus_dir(zr, 1);
1027 		encoder_call(zr, video, s_routing, 1, 0, 0);
1028 
1029 		/* Take the JPEG codec and the VFE out of sleep */
1030 		jpeg_codec_sleep(zr, 0);
1031 		/* Setup the VFE */
1032 		if (zr->vfe) {
1033 			zr->vfe->set_video(zr->vfe, zr->timing, &cap,
1034 					   &zr->card.vfe_pol);
1035 			zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
1036 		}
1037 		/* Setup the JPEG codec */
1038 		zr->codec->set_video(zr->codec, zr->timing, &cap,
1039 				     &zr->card.vfe_pol);
1040 		zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
1041 
1042 		init_jpeg_queue(zr);
1043 		zr36057_set_jpg(zr, mode);	// \P_Reset, ... Video param, FIFO
1044 
1045 		clear_interrupt_counters(zr);
1046 		dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
1047 			ZR_DEVNAME(zr));
1048 		break;
1049 
1050 	case BUZ_MODE_IDLE:
1051 	default:
1052 		/* shut down processing */
1053 		btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
1054 		      ZR36057_ICR);
1055 		btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
1056 			ZR36057_ISR);
1057 		btand(~ZR36057_JMC_Go_en, ZR36057_JMC);	// \Go_en
1058 
1059 		msleep(50);
1060 
1061 		set_videobus_dir(zr, 0);
1062 		set_frame(zr, 1);	// /FRAME
1063 		btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);	// /CFlush
1064 		btwrite(0, ZR36057_JPC);	// \P_Reset,\CodTrnsEn,\Active
1065 		btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
1066 		btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
1067 		jpeg_codec_reset(zr);
1068 		jpeg_codec_sleep(zr, 1);
1069 		zr36057_adjust_vfe(zr, mode);
1070 
1071 		decoder_call(zr, video, s_stream, 1);
1072 		encoder_call(zr, video, s_routing, 0, 0, 0);
1073 
1074 		dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
1075 		break;
1076 
1077 	}
1078 }
1079 
1080 /* when this is called the spinlock must be held */
1081 void
1082 zoran_feed_stat_com (struct zoran *zr)
1083 {
1084 	/* move frames from pending queue to DMA */
1085 
1086 	int frame, i, max_stat_com;
1087 
1088 	max_stat_com =
1089 	    (zr->jpg_settings.TmpDcm ==
1090 	     1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
1091 
1092 	while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
1093 	       zr->jpg_dma_head < zr->jpg_que_head) {
1094 
1095 		frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
1096 		if (zr->jpg_settings.TmpDcm == 1) {
1097 			/* fill 1 stat_com entry */
1098 			i = (zr->jpg_dma_head -
1099 			     zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1100 			if (!(zr->stat_com[i] & cpu_to_le32(1)))
1101 				break;
1102 			zr->stat_com[i] =
1103 			    cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1104 		} else {
1105 			/* fill 2 stat_com entries */
1106 			i = ((zr->jpg_dma_head -
1107 			      zr->jpg_err_shift) & 1) * 2;
1108 			if (!(zr->stat_com[i] & cpu_to_le32(1)))
1109 				break;
1110 			zr->stat_com[i] =
1111 			    cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1112 			zr->stat_com[i + 1] =
1113 			    cpu_to_le32(zr->jpg_buffers.buffer[frame].jpg.frag_tab_bus);
1114 		}
1115 		zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
1116 		zr->jpg_dma_head++;
1117 
1118 	}
1119 	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
1120 		zr->jpg_queued_num++;
1121 }
1122 
1123 /* when this is called the spinlock must be held */
1124 static void
1125 zoran_reap_stat_com (struct zoran *zr)
1126 {
1127 	/* move frames from DMA queue to done queue */
1128 
1129 	int i;
1130 	u32 stat_com;
1131 	unsigned int seq;
1132 	unsigned int dif;
1133 	struct zoran_buffer *buffer;
1134 	int frame;
1135 
1136 	/* In motion decompress we don't have a hardware frame counter,
1137 	 * we just count the interrupts here */
1138 
1139 	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1140 		zr->jpg_seq_num++;
1141 	}
1142 	while (zr->jpg_dma_tail < zr->jpg_dma_head) {
1143 		if (zr->jpg_settings.TmpDcm == 1)
1144 			i = (zr->jpg_dma_tail -
1145 			     zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1146 		else
1147 			i = ((zr->jpg_dma_tail -
1148 			      zr->jpg_err_shift) & 1) * 2 + 1;
1149 
1150 		stat_com = le32_to_cpu(zr->stat_com[i]);
1151 
1152 		if ((stat_com & 1) == 0) {
1153 			return;
1154 		}
1155 		frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1156 		buffer = &zr->jpg_buffers.buffer[frame];
1157 		v4l2_get_timestamp(&buffer->bs.timestamp);
1158 
1159 		if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1160 			buffer->bs.length = (stat_com & 0x7fffff) >> 1;
1161 
1162 			/* update sequence number with the help of the counter in stat_com */
1163 
1164 			seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
1165 			dif = (seq - zr->jpg_seq_num) & 0xff;
1166 			zr->jpg_seq_num += dif;
1167 		} else {
1168 			buffer->bs.length = 0;
1169 		}
1170 		buffer->bs.seq =
1171 		    zr->jpg_settings.TmpDcm ==
1172 		    2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
1173 		buffer->state = BUZ_STATE_DONE;
1174 
1175 		zr->jpg_dma_tail++;
1176 	}
1177 }
1178 
1179 static void zoran_restart(struct zoran *zr)
1180 {
1181 	/* Now the stat_comm buffer is ready for restart */
1182 	unsigned int status = 0;
1183 	int mode;
1184 
1185 	if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1186 		decoder_call(zr, video, g_input_status, &status);
1187 		mode = CODEC_DO_COMPRESSION;
1188 	} else {
1189 		status = V4L2_IN_ST_NO_SIGNAL;
1190 		mode = CODEC_DO_EXPANSION;
1191 	}
1192 	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1193 	    !(status & V4L2_IN_ST_NO_SIGNAL)) {
1194 		/********** RESTART code *************/
1195 		jpeg_codec_reset(zr);
1196 		zr->codec->set_mode(zr->codec, mode);
1197 		zr36057_set_jpg(zr, zr->codec_mode);
1198 		jpeg_start(zr);
1199 
1200 		if (zr->num_errors <= 8)
1201 			dprintk(2, KERN_INFO "%s: Restart\n",
1202 				ZR_DEVNAME(zr));
1203 
1204 		zr->JPEG_missed = 0;
1205 		zr->JPEG_error = 2;
1206 		/********** End RESTART code ***********/
1207 	}
1208 }
1209 
1210 static void
1211 error_handler (struct zoran *zr,
1212 	       u32           astat,
1213 	       u32           stat)
1214 {
1215 	int i;
1216 
1217 	/* This is JPEG error handling part */
1218 	if (zr->codec_mode != BUZ_MODE_MOTION_COMPRESS &&
1219 	    zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS) {
1220 		return;
1221 	}
1222 
1223 	if ((stat & 1) == 0 &&
1224 	    zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
1225 	    zr->jpg_dma_tail - zr->jpg_que_tail >= zr->jpg_buffers.num_buffers) {
1226 		/* No free buffers... */
1227 		zoran_reap_stat_com(zr);
1228 		zoran_feed_stat_com(zr);
1229 		wake_up_interruptible(&zr->jpg_capq);
1230 		zr->JPEG_missed = 0;
1231 		return;
1232 	}
1233 
1234 	if (zr->JPEG_error == 1) {
1235 		zoran_restart(zr);
1236 		return;
1237 	}
1238 
1239 	/*
1240 	 * First entry: error just happened during normal operation
1241 	 *
1242 	 * In BUZ_MODE_MOTION_COMPRESS:
1243 	 *
1244 	 * Possible glitch in TV signal. In this case we should
1245 	 * stop the codec and wait for good quality signal before
1246 	 * restarting it to avoid further problems
1247 	 *
1248 	 * In BUZ_MODE_MOTION_DECOMPRESS:
1249 	 *
1250 	 * Bad JPEG frame: we have to mark it as processed (codec crashed
1251 	 * and was not able to do it itself), and to remove it from queue.
1252 	 */
1253 	btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
1254 	udelay(1);
1255 	stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
1256 	btwrite(0, ZR36057_JPC);
1257 	btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
1258 	jpeg_codec_reset(zr);
1259 	jpeg_codec_sleep(zr, 1);
1260 	zr->JPEG_error = 1;
1261 	zr->num_errors++;
1262 
1263 	/* Report error */
1264 	if (zr36067_debug > 1 && zr->num_errors <= 8) {
1265 		long frame;
1266 		int j;
1267 
1268 		frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
1269 		printk(KERN_ERR
1270 		       "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
1271 		       ZR_DEVNAME(zr), stat, zr->last_isr,
1272 		       zr->jpg_que_tail, zr->jpg_dma_tail,
1273 		       zr->jpg_dma_head, zr->jpg_que_head,
1274 		       zr->jpg_seq_num, frame);
1275 		printk(KERN_INFO "stat_com frames:");
1276 		for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
1277 			for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
1278 				if (le32_to_cpu(zr->stat_com[j]) == zr->jpg_buffers.buffer[i].jpg.frag_tab_bus)
1279 					printk(KERN_CONT "% d->%d", j, i);
1280 			}
1281 		}
1282 		printk(KERN_CONT "\n");
1283 	}
1284 	/* Find an entry in stat_com and rotate contents */
1285 	if (zr->jpg_settings.TmpDcm == 1)
1286 		i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1287 	else
1288 		i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
1289 	if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
1290 		/* Mimic zr36067 operation */
1291 		zr->stat_com[i] |= cpu_to_le32(1);
1292 		if (zr->jpg_settings.TmpDcm != 1)
1293 			zr->stat_com[i + 1] |= cpu_to_le32(1);
1294 		/* Refill */
1295 		zoran_reap_stat_com(zr);
1296 		zoran_feed_stat_com(zr);
1297 		wake_up_interruptible(&zr->jpg_capq);
1298 		/* Find an entry in stat_com again after refill */
1299 		if (zr->jpg_settings.TmpDcm == 1)
1300 			i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
1301 		else
1302 			i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
1303 	}
1304 	if (i) {
1305 		/* Rotate stat_comm entries to make current entry first */
1306 		int j;
1307 		__le32 bus_addr[BUZ_NUM_STAT_COM];
1308 
1309 		/* Here we are copying the stat_com array, which
1310 		 * is already in little endian format, so
1311 		 * no endian conversions here
1312 		 */
1313 		memcpy(bus_addr, zr->stat_com, sizeof(bus_addr));
1314 
1315 		for (j = 0; j < BUZ_NUM_STAT_COM; j++)
1316 			zr->stat_com[j] = bus_addr[(i + j) & BUZ_MASK_STAT_COM];
1317 
1318 		zr->jpg_err_shift += i;
1319 		zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
1320 	}
1321 	if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
1322 		zr->jpg_err_seq = zr->jpg_seq_num;	/* + 1; */
1323 	zoran_restart(zr);
1324 }
1325 
1326 irqreturn_t
1327 zoran_irq (int             irq,
1328 	   void           *dev_id)
1329 {
1330 	u32 stat, astat;
1331 	int count;
1332 	struct zoran *zr;
1333 	unsigned long flags;
1334 
1335 	zr = dev_id;
1336 	count = 0;
1337 
1338 	if (zr->testing) {
1339 		/* Testing interrupts */
1340 		spin_lock_irqsave(&zr->spinlock, flags);
1341 		while ((stat = count_reset_interrupt(zr))) {
1342 			if (count++ > 100) {
1343 				btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1344 				dprintk(1,
1345 					KERN_ERR
1346 					"%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
1347 					ZR_DEVNAME(zr), stat);
1348 				wake_up_interruptible(&zr->test_q);
1349 			}
1350 		}
1351 		zr->last_isr = stat;
1352 		spin_unlock_irqrestore(&zr->spinlock, flags);
1353 		return IRQ_HANDLED;
1354 	}
1355 
1356 	spin_lock_irqsave(&zr->spinlock, flags);
1357 	while (1) {
1358 		/* get/clear interrupt status bits */
1359 		stat = count_reset_interrupt(zr);
1360 		astat = stat & IRQ_MASK;
1361 		if (!astat) {
1362 			break;
1363 		}
1364 		dprintk(4,
1365 			KERN_DEBUG
1366 			"zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
1367 			astat, btread(ZR36057_ICR));
1368 		if (astat & zr->card.vsync_int) {	// SW
1369 
1370 			if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1371 			    zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
1372 				/* count missed interrupts */
1373 				zr->JPEG_missed++;
1374 			}
1375 			//post_office_read(zr,1,0);
1376 			/* Interrupts may still happen when
1377 			 * zr->v4l_memgrab_active is switched off.
1378 			 * We simply ignore them */
1379 
1380 			if (zr->v4l_memgrab_active) {
1381 				/* A lot more checks should be here ... */
1382 				if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
1383 					dprintk(1,
1384 						KERN_WARNING
1385 						"%s: BuzIRQ with SnapShot off ???\n",
1386 						ZR_DEVNAME(zr));
1387 
1388 				if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
1389 					/* There is a grab on a frame going on, check if it has finished */
1390 					if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
1391 						/* it is finished, notify the user */
1392 
1393 						zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
1394 						zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
1395 						v4l2_get_timestamp(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
1396 						zr->v4l_grab_frame = NO_GRAB_ACTIVE;
1397 						zr->v4l_pend_tail++;
1398 					}
1399 				}
1400 
1401 				if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
1402 					wake_up_interruptible(&zr->v4l_capq);
1403 
1404 				/* Check if there is another grab queued */
1405 
1406 				if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
1407 				    zr->v4l_pend_tail != zr->v4l_pend_head) {
1408 					int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
1409 					u32 reg;
1410 
1411 					zr->v4l_grab_frame = frame;
1412 
1413 					/* Set zr36057 video front end and enable video */
1414 
1415 					/* Buffer address */
1416 
1417 					reg = zr->v4l_buffers.buffer[frame].v4l.fbuffer_bus;
1418 					btwrite(reg, ZR36057_VDTR);
1419 					if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
1420 						reg += zr->v4l_settings.bytesperline;
1421 					btwrite(reg, ZR36057_VDBR);
1422 
1423 					/* video stride, status, and frame grab register */
1424 					reg = 0;
1425 					if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
1426 						reg += zr->v4l_settings.bytesperline;
1427 					reg = (reg << ZR36057_VSSFGR_DispStride);
1428 					reg |= ZR36057_VSSFGR_VidOvf;
1429 					reg |= ZR36057_VSSFGR_SnapShot;
1430 					reg |= ZR36057_VSSFGR_FrameGrab;
1431 					btwrite(reg, ZR36057_VSSFGR);
1432 
1433 					btor(ZR36057_VDCR_VidEn,
1434 					     ZR36057_VDCR);
1435 				}
1436 			}
1437 
1438 			/* even if we don't grab, we do want to increment
1439 			 * the sequence counter to see lost frames */
1440 			zr->v4l_grab_seq++;
1441 		}
1442 #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
1443 		if (astat & ZR36057_ISR_CodRepIRQ) {
1444 			zr->intr_counter_CodRepIRQ++;
1445 			IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
1446 				ZR_DEVNAME(zr)));
1447 			btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
1448 		}
1449 #endif				/* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
1450 
1451 #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
1452 		if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
1453 		    (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
1454 		     zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
1455 			if (zr36067_debug > 1 && (!zr->frame_num || zr->JPEG_error)) {
1456 				char sv[BUZ_NUM_STAT_COM + 1];
1457 				int i;
1458 
1459 				printk(KERN_INFO
1460 				       "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
1461 				       ZR_DEVNAME(zr), stat,
1462 				       zr->jpg_settings.odd_even,
1463 				       zr->jpg_settings.field_per_buff,
1464 				       zr->JPEG_missed);
1465 
1466 				for (i = 0; i < BUZ_NUM_STAT_COM; i++)
1467 					sv[i] = le32_to_cpu(zr->stat_com[i]) & 1 ? '1' : '0';
1468 				sv[BUZ_NUM_STAT_COM] = 0;
1469 				printk(KERN_INFO
1470 				       "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
1471 				       ZR_DEVNAME(zr), sv,
1472 				       zr->jpg_que_tail,
1473 				       zr->jpg_dma_tail,
1474 				       zr->jpg_dma_head,
1475 				       zr->jpg_que_head);
1476 			} else {
1477 				/* Get statistics */
1478 				if (zr->JPEG_missed > zr->JPEG_max_missed)
1479 					zr->JPEG_max_missed = zr->JPEG_missed;
1480 				if (zr->JPEG_missed < zr->JPEG_min_missed)
1481 					zr->JPEG_min_missed = zr->JPEG_missed;
1482 			}
1483 
1484 			if (zr36067_debug > 2 && zr->frame_num < 6) {
1485 				int i;
1486 
1487 				printk(KERN_INFO "%s: seq=%ld stat_com:",
1488 				       ZR_DEVNAME(zr), zr->jpg_seq_num);
1489 				for (i = 0; i < 4; i++) {
1490 					printk(KERN_CONT " %08x",
1491 					       le32_to_cpu(zr->stat_com[i]));
1492 				}
1493 				printk(KERN_CONT "\n");
1494 			}
1495 			zr->frame_num++;
1496 			zr->JPEG_missed = 0;
1497 			zr->JPEG_error = 0;
1498 			zoran_reap_stat_com(zr);
1499 			zoran_feed_stat_com(zr);
1500 			wake_up_interruptible(&zr->jpg_capq);
1501 		}
1502 #endif				/* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
1503 
1504 		/* DATERR, too many fields missed, error processing */
1505 		if ((astat & zr->card.jpeg_int) ||
1506 		    zr->JPEG_missed > 25 ||
1507 		    zr->JPEG_error == 1	||
1508 		    ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
1509 		     (zr->frame_num && (zr->JPEG_missed > zr->jpg_settings.field_per_buff)))) {
1510 			error_handler(zr, astat, stat);
1511 		}
1512 
1513 		count++;
1514 		if (count > 10) {
1515 			dprintk(2, KERN_WARNING "%s: irq loop %d\n",
1516 				ZR_DEVNAME(zr), count);
1517 			if (count > 20) {
1518 				btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
1519 				dprintk(2,
1520 					KERN_ERR
1521 					"%s: IRQ lockup, cleared int mask\n",
1522 					ZR_DEVNAME(zr));
1523 				break;
1524 			}
1525 		}
1526 		zr->last_isr = stat;
1527 	}
1528 	spin_unlock_irqrestore(&zr->spinlock, flags);
1529 
1530 	return IRQ_HANDLED;
1531 }
1532 
1533 void
1534 zoran_set_pci_master (struct zoran *zr,
1535 		      int           set_master)
1536 {
1537 	if (set_master) {
1538 		pci_set_master(zr->pci_dev);
1539 	} else {
1540 		u16 command;
1541 
1542 		pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
1543 		command &= ~PCI_COMMAND_MASTER;
1544 		pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
1545 	}
1546 }
1547 
1548 void
1549 zoran_init_hardware (struct zoran *zr)
1550 {
1551 	/* Enable bus-mastering */
1552 	zoran_set_pci_master(zr, 1);
1553 
1554 	/* Initialize the board */
1555 	if (zr->card.init) {
1556 		zr->card.init(zr);
1557 	}
1558 
1559 	decoder_call(zr, core, init, 0);
1560 	decoder_call(zr, video, s_std, zr->norm);
1561 	decoder_call(zr, video, s_routing,
1562 		zr->card.input[zr->input].muxsel, 0, 0);
1563 
1564 	encoder_call(zr, core, init, 0);
1565 	encoder_call(zr, video, s_std_output, zr->norm);
1566 	encoder_call(zr, video, s_routing, 0, 0, 0);
1567 
1568 	/* toggle JPEG codec sleep to sync PLL */
1569 	jpeg_codec_sleep(zr, 1);
1570 	jpeg_codec_sleep(zr, 0);
1571 
1572 	/*
1573 	 * set individual interrupt enables (without GIRQ1)
1574 	 * but don't global enable until zoran_open()
1575 	 */
1576 	zr36057_init_vfe(zr);
1577 
1578 	zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
1579 
1580 	btwrite(IRQ_MASK, ZR36057_ISR);	// Clears interrupts
1581 }
1582 
1583 void
1584 zr36057_restart (struct zoran *zr)
1585 {
1586 	btwrite(0, ZR36057_SPGPPCR);
1587 	mdelay(1);
1588 	btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
1589 	mdelay(1);
1590 
1591 	/* assert P_Reset */
1592 	btwrite(0, ZR36057_JPC);
1593 	/* set up GPIO direction - all output */
1594 	btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
1595 
1596 	/* set up GPIO pins and guest bus timing */
1597 	btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
1598 }
1599 
1600 /*
1601  * initialize video front end
1602  */
1603 
1604 static void
1605 zr36057_init_vfe (struct zoran *zr)
1606 {
1607 	u32 reg;
1608 
1609 	reg = btread(ZR36057_VFESPFR);
1610 	reg |= ZR36057_VFESPFR_LittleEndian;
1611 	reg &= ~ZR36057_VFESPFR_VCLKPol;
1612 	reg |= ZR36057_VFESPFR_ExtFl;
1613 	reg |= ZR36057_VFESPFR_TopField;
1614 	btwrite(reg, ZR36057_VFESPFR);
1615 	reg = btread(ZR36057_VDCR);
1616 	if (pci_pci_problems & PCIPCI_TRITON)
1617 		// || zr->revision < 1) // Revision 1 has also Triton support
1618 		reg &= ~ZR36057_VDCR_Triton;
1619 	else
1620 		reg |= ZR36057_VDCR_Triton;
1621 	btwrite(reg, ZR36057_VDCR);
1622 }
1623