zoran_device.c 42 KB

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