zoran_device.c 42 KB

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