zoran_device.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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 int 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 allready 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
  472. write_overlay_mask (struct file *file,
  473. struct v4l2_clip *vp,
  474. int count)
  475. {
  476. struct zoran_fh *fh = file->private_data;
  477. struct zoran *zr = fh->zr;
  478. unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
  479. u32 *mask;
  480. int x, y, width, height;
  481. unsigned i, j, k;
  482. u32 reg;
  483. /* fill mask with one bits */
  484. memset(fh->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
  485. reg = 0;
  486. for (i = 0; i < count; ++i) {
  487. /* pick up local copy of clip */
  488. x = vp[i].c.left;
  489. y = vp[i].c.top;
  490. width = vp[i].c.width;
  491. height = vp[i].c.height;
  492. /* trim clips that extend beyond the window */
  493. if (x < 0) {
  494. width += x;
  495. x = 0;
  496. }
  497. if (y < 0) {
  498. height += y;
  499. y = 0;
  500. }
  501. if (x + width > fh->overlay_settings.width) {
  502. width = fh->overlay_settings.width - x;
  503. }
  504. if (y + height > fh->overlay_settings.height) {
  505. height = fh->overlay_settings.height - y;
  506. }
  507. /* ignore degenerate clips */
  508. if (height <= 0) {
  509. continue;
  510. }
  511. if (width <= 0) {
  512. continue;
  513. }
  514. /* apply clip for each scan line */
  515. for (j = 0; j < height; ++j) {
  516. /* reset bit for each pixel */
  517. /* this can be optimized later if need be */
  518. mask = fh->overlay_mask + (y + j) * mask_line_size;
  519. for (k = 0; k < width; ++k) {
  520. mask[(x + k) / 32] &=
  521. ~((u32) 1 << (x + k) % 32);
  522. }
  523. }
  524. }
  525. }
  526. /* Enable/Disable uncompressed memory grabbing of the 36057 */
  527. void
  528. zr36057_set_memgrab (struct zoran *zr,
  529. int mode)
  530. {
  531. if (mode) {
  532. /* We only check SnapShot and not FrameGrab here. SnapShot==1
  533. * means a capture is already in progress, but FrameGrab==1
  534. * doesn't necessary mean that. It's more correct to say a 1
  535. * to 0 transition indicates a capture completed. If a
  536. * capture is pending when capturing is tuned off, FrameGrab
  537. * will be stuck at 1 until capturing is turned back on.
  538. */
  539. if (btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot)
  540. dprintk(1,
  541. KERN_WARNING
  542. "%s: zr36057_set_memgrab(1) with SnapShot on!?\n",
  543. ZR_DEVNAME(zr));
  544. /* switch on VSync interrupts */
  545. btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
  546. btor(zr->card.vsync_int, ZR36057_ICR); // SW
  547. /* enable SnapShot */
  548. btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
  549. /* Set zr36057 video front end and enable video */
  550. zr36057_set_vfe(zr, zr->v4l_settings.width,
  551. zr->v4l_settings.height,
  552. zr->v4l_settings.format);
  553. zr->v4l_memgrab_active = 1;
  554. } else {
  555. /* switch off VSync interrupts */
  556. btand(~zr->card.vsync_int, ZR36057_ICR); // SW
  557. zr->v4l_memgrab_active = 0;
  558. zr->v4l_grab_frame = NO_GRAB_ACTIVE;
  559. /* reenable grabbing to screen if it was running */
  560. if (zr->v4l_overlay_active) {
  561. zr36057_overlay(zr, 1);
  562. } else {
  563. btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
  564. btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
  565. }
  566. }
  567. }
  568. int
  569. wait_grab_pending (struct zoran *zr)
  570. {
  571. unsigned long flags;
  572. /* wait until all pending grabs are finished */
  573. if (!zr->v4l_memgrab_active)
  574. return 0;
  575. wait_event_interruptible(zr->v4l_capq,
  576. (zr->v4l_pend_tail == zr->v4l_pend_head));
  577. if (signal_pending(current))
  578. return -ERESTARTSYS;
  579. spin_lock_irqsave(&zr->spinlock, flags);
  580. zr36057_set_memgrab(zr, 0);
  581. spin_unlock_irqrestore(&zr->spinlock, flags);
  582. return 0;
  583. }
  584. /*****************************************************************************
  585. * *
  586. * Set up the Buz-specific MJPEG part *
  587. * *
  588. *****************************************************************************/
  589. static inline void
  590. set_frame (struct zoran *zr,
  591. int val)
  592. {
  593. GPIO(zr, zr->card.gpio[ZR_GPIO_JPEG_FRAME], val);
  594. }
  595. static void
  596. set_videobus_dir (struct zoran *zr,
  597. int val)
  598. {
  599. switch (zr->card.type) {
  600. case LML33:
  601. case LML33R10:
  602. if (lml33dpath == 0)
  603. GPIO(zr, 5, val);
  604. else
  605. GPIO(zr, 5, 1);
  606. break;
  607. default:
  608. GPIO(zr, zr->card.gpio[ZR_GPIO_VID_DIR],
  609. zr->card.gpio_pol[ZR_GPIO_VID_DIR] ? !val : val);
  610. break;
  611. }
  612. }
  613. static void
  614. init_jpeg_queue (struct zoran *zr)
  615. {
  616. int i;
  617. /* re-initialize DMA ring stuff */
  618. zr->jpg_que_head = 0;
  619. zr->jpg_dma_head = 0;
  620. zr->jpg_dma_tail = 0;
  621. zr->jpg_que_tail = 0;
  622. zr->jpg_seq_num = 0;
  623. zr->JPEG_error = 0;
  624. zr->num_errors = 0;
  625. zr->jpg_err_seq = 0;
  626. zr->jpg_err_shift = 0;
  627. zr->jpg_queued_num = 0;
  628. for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
  629. zr->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  630. }
  631. for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
  632. zr->stat_com[i] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
  633. }
  634. }
  635. static void
  636. zr36057_set_jpg (struct zoran *zr,
  637. enum zoran_codec_mode mode)
  638. {
  639. struct tvnorm *tvn;
  640. u32 reg;
  641. tvn = zr->timing;
  642. /* assert P_Reset, disable code transfer, deassert Active */
  643. btwrite(0, ZR36057_JPC);
  644. /* MJPEG compression mode */
  645. switch (mode) {
  646. case BUZ_MODE_MOTION_COMPRESS:
  647. default:
  648. reg = ZR36057_JMC_MJPGCmpMode;
  649. break;
  650. case BUZ_MODE_MOTION_DECOMPRESS:
  651. reg = ZR36057_JMC_MJPGExpMode;
  652. reg |= ZR36057_JMC_SyncMstr;
  653. /* RJ: The following is experimental - improves the output to screen */
  654. //if(zr->jpg_settings.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
  655. break;
  656. case BUZ_MODE_STILL_COMPRESS:
  657. reg = ZR36057_JMC_JPGCmpMode;
  658. break;
  659. case BUZ_MODE_STILL_DECOMPRESS:
  660. reg = ZR36057_JMC_JPGExpMode;
  661. break;
  662. }
  663. reg |= ZR36057_JMC_JPG;
  664. if (zr->jpg_settings.field_per_buff == 1)
  665. reg |= ZR36057_JMC_Fld_per_buff;
  666. btwrite(reg, ZR36057_JMC);
  667. /* vertical */
  668. btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
  669. reg = (6 << ZR36057_VSP_VsyncSize) |
  670. (tvn->Ht << ZR36057_VSP_FrmTot);
  671. btwrite(reg, ZR36057_VSP);
  672. reg = ((zr->jpg_settings.img_y + tvn->VStart) << ZR36057_FVAP_NAY) |
  673. (zr->jpg_settings.img_height << ZR36057_FVAP_PAY);
  674. btwrite(reg, ZR36057_FVAP);
  675. /* horizontal */
  676. if (zr->card.vfe_pol.hsync_pol)
  677. btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
  678. else
  679. btand(~ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
  680. reg = ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) |
  681. (tvn->Wt << ZR36057_HSP_LineTot);
  682. btwrite(reg, ZR36057_HSP);
  683. reg = ((zr->jpg_settings.img_x +
  684. tvn->HStart + 4) << ZR36057_FHAP_NAX) |
  685. (zr->jpg_settings.img_width << ZR36057_FHAP_PAX);
  686. btwrite(reg, ZR36057_FHAP);
  687. /* field process parameters */
  688. if (zr->jpg_settings.odd_even)
  689. reg = ZR36057_FPP_Odd_Even;
  690. else
  691. reg = 0;
  692. btwrite(reg, ZR36057_FPP);
  693. /* Set proper VCLK Polarity, else colors will be wrong during playback */
  694. //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
  695. /* code base address */
  696. reg = virt_to_bus(zr->stat_com);
  697. btwrite(reg, ZR36057_JCBA);
  698. /* FIFO threshold (FIFO is 160. double words) */
  699. /* NOTE: decimal values here */
  700. switch (mode) {
  701. case BUZ_MODE_STILL_COMPRESS:
  702. case BUZ_MODE_MOTION_COMPRESS:
  703. if (zr->card.type != BUZ)
  704. reg = 140;
  705. else
  706. reg = 60;
  707. break;
  708. case BUZ_MODE_STILL_DECOMPRESS:
  709. case BUZ_MODE_MOTION_DECOMPRESS:
  710. reg = 20;
  711. break;
  712. default:
  713. reg = 80;
  714. break;
  715. }
  716. btwrite(reg, ZR36057_JCFT);
  717. zr36057_adjust_vfe(zr, mode);
  718. }
  719. void
  720. print_interrupts (struct zoran *zr)
  721. {
  722. int res, noerr = 0;
  723. printk(KERN_INFO "%s: interrupts received:", ZR_DEVNAME(zr));
  724. if ((res = zr->field_counter) < -1 || res > 1) {
  725. printk(" FD:%d", res);
  726. }
  727. if ((res = zr->intr_counter_GIRQ1) != 0) {
  728. printk(" GIRQ1:%d", res);
  729. noerr++;
  730. }
  731. if ((res = zr->intr_counter_GIRQ0) != 0) {
  732. printk(" GIRQ0:%d", res);
  733. noerr++;
  734. }
  735. if ((res = zr->intr_counter_CodRepIRQ) != 0) {
  736. printk(" CodRepIRQ:%d", res);
  737. noerr++;
  738. }
  739. if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
  740. printk(" JPEGRepIRQ:%d", res);
  741. noerr++;
  742. }
  743. if (zr->JPEG_max_missed) {
  744. printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
  745. zr->JPEG_min_missed);
  746. }
  747. if (zr->END_event_missed) {
  748. printk(" ENDs missed: %d", zr->END_event_missed);
  749. }
  750. //if (zr->jpg_queued_num) {
  751. printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
  752. zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
  753. //}
  754. if (!noerr) {
  755. printk(": no interrupts detected.");
  756. }
  757. printk("\n");
  758. }
  759. void
  760. clear_interrupt_counters (struct zoran *zr)
  761. {
  762. zr->intr_counter_GIRQ1 = 0;
  763. zr->intr_counter_GIRQ0 = 0;
  764. zr->intr_counter_CodRepIRQ = 0;
  765. zr->intr_counter_JPEGRepIRQ = 0;
  766. zr->field_counter = 0;
  767. zr->IRQ1_in = 0;
  768. zr->IRQ1_out = 0;
  769. zr->JPEG_in = 0;
  770. zr->JPEG_out = 0;
  771. zr->JPEG_0 = 0;
  772. zr->JPEG_1 = 0;
  773. zr->END_event_missed = 0;
  774. zr->JPEG_missed = 0;
  775. zr->JPEG_max_missed = 0;
  776. zr->JPEG_min_missed = 0x7fffffff;
  777. }
  778. static u32
  779. count_reset_interrupt (struct zoran *zr)
  780. {
  781. u32 isr;
  782. if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
  783. if (isr & ZR36057_ISR_GIRQ1) {
  784. btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
  785. zr->intr_counter_GIRQ1++;
  786. }
  787. if (isr & ZR36057_ISR_GIRQ0) {
  788. btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
  789. zr->intr_counter_GIRQ0++;
  790. }
  791. if (isr & ZR36057_ISR_CodRepIRQ) {
  792. btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
  793. zr->intr_counter_CodRepIRQ++;
  794. }
  795. if (isr & ZR36057_ISR_JPEGRepIRQ) {
  796. btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
  797. zr->intr_counter_JPEGRepIRQ++;
  798. }
  799. }
  800. return isr;
  801. }
  802. void
  803. jpeg_start (struct zoran *zr)
  804. {
  805. int reg;
  806. zr->frame_num = 0;
  807. /* deassert P_reset, disable code transfer, deassert Active */
  808. btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC);
  809. /* stop flushing the internal code buffer */
  810. btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
  811. /* enable code transfer */
  812. btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC);
  813. /* clear IRQs */
  814. btwrite(IRQ_MASK, ZR36057_ISR);
  815. /* enable the JPEG IRQs */
  816. btwrite(zr->card.jpeg_int |
  817. ZR36057_ICR_JPEGRepIRQ |
  818. ZR36057_ICR_IntPinEn,
  819. ZR36057_ICR);
  820. set_frame(zr, 0); // \FRAME
  821. /* set the JPEG codec guest ID */
  822. reg = (zr->card.gpcs[1] << ZR36057_JCGI_JPEGuestID) |
  823. (0 << ZR36057_JCGI_JPEGuestReg);
  824. btwrite(reg, ZR36057_JCGI);
  825. if (zr->card.video_vfe == CODEC_TYPE_ZR36016 &&
  826. zr->card.video_codec == CODEC_TYPE_ZR36050) {
  827. /* Enable processing on the ZR36016 */
  828. if (zr->vfe)
  829. zr36016_write(zr->vfe, 0, 1);
  830. /* load the address of the GO register in the ZR36050 latch */
  831. post_office_write(zr, 0, 0, 0);
  832. }
  833. /* assert Active */
  834. btor(ZR36057_JPC_Active, ZR36057_JPC);
  835. /* enable the Go generation */
  836. btor(ZR36057_JMC_Go_en, ZR36057_JMC);
  837. udelay(30);
  838. set_frame(zr, 1); // /FRAME
  839. dprintk(3, KERN_DEBUG "%s: jpeg_start\n", ZR_DEVNAME(zr));
  840. }
  841. void
  842. zr36057_enable_jpg (struct zoran *zr,
  843. enum zoran_codec_mode mode)
  844. {
  845. struct vfe_settings cap;
  846. int field_size =
  847. zr->jpg_buffers.buffer_size / zr->jpg_settings.field_per_buff;
  848. struct v4l2_routing route = { 0, 0 };
  849. zr->codec_mode = mode;
  850. cap.x = zr->jpg_settings.img_x;
  851. cap.y = zr->jpg_settings.img_y;
  852. cap.width = zr->jpg_settings.img_width;
  853. cap.height = zr->jpg_settings.img_height;
  854. cap.decimation =
  855. zr->jpg_settings.HorDcm | (zr->jpg_settings.VerDcm << 8);
  856. cap.quality = zr->jpg_settings.jpg_comp.quality;
  857. switch (mode) {
  858. case BUZ_MODE_MOTION_COMPRESS: {
  859. struct jpeg_app_marker app;
  860. struct jpeg_com_marker com;
  861. /* In motion compress mode, the decoder output must be enabled, and
  862. * the video bus direction set to input.
  863. */
  864. set_videobus_dir(zr, 0);
  865. decoder_call(zr, video, s_stream, 1);
  866. route.input = 0;
  867. encoder_call(zr, video, s_routing, &route);
  868. /* Take the JPEG codec and the VFE out of sleep */
  869. jpeg_codec_sleep(zr, 0);
  870. /* set JPEG app/com marker */
  871. app.appn = zr->jpg_settings.jpg_comp.APPn;
  872. app.len = zr->jpg_settings.jpg_comp.APP_len;
  873. memcpy(app.data, zr->jpg_settings.jpg_comp.APP_data, 60);
  874. zr->codec->control(zr->codec, CODEC_S_JPEG_APP_DATA,
  875. sizeof(struct jpeg_app_marker), &app);
  876. com.len = zr->jpg_settings.jpg_comp.COM_len;
  877. memcpy(com.data, zr->jpg_settings.jpg_comp.COM_data, 60);
  878. zr->codec->control(zr->codec, CODEC_S_JPEG_COM_DATA,
  879. sizeof(struct jpeg_com_marker), &com);
  880. /* Setup the JPEG codec */
  881. zr->codec->control(zr->codec, CODEC_S_JPEG_TDS_BYTE,
  882. sizeof(int), &field_size);
  883. zr->codec->set_video(zr->codec, zr->timing, &cap,
  884. &zr->card.vfe_pol);
  885. zr->codec->set_mode(zr->codec, CODEC_DO_COMPRESSION);
  886. /* Setup the VFE */
  887. if (zr->vfe) {
  888. zr->vfe->control(zr->vfe, CODEC_S_JPEG_TDS_BYTE,
  889. sizeof(int), &field_size);
  890. zr->vfe->set_video(zr->vfe, zr->timing, &cap,
  891. &zr->card.vfe_pol);
  892. zr->vfe->set_mode(zr->vfe, CODEC_DO_COMPRESSION);
  893. }
  894. init_jpeg_queue(zr);
  895. zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
  896. clear_interrupt_counters(zr);
  897. dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_COMPRESS)\n",
  898. ZR_DEVNAME(zr));
  899. break;
  900. }
  901. case BUZ_MODE_MOTION_DECOMPRESS:
  902. /* In motion decompression mode, the decoder output must be disabled, and
  903. * the video bus direction set to output.
  904. */
  905. decoder_call(zr, video, s_stream, 0);
  906. set_videobus_dir(zr, 1);
  907. route.input = 1;
  908. encoder_call(zr, video, s_routing, &route);
  909. /* Take the JPEG codec and the VFE out of sleep */
  910. jpeg_codec_sleep(zr, 0);
  911. /* Setup the VFE */
  912. if (zr->vfe) {
  913. zr->vfe->set_video(zr->vfe, zr->timing, &cap,
  914. &zr->card.vfe_pol);
  915. zr->vfe->set_mode(zr->vfe, CODEC_DO_EXPANSION);
  916. }
  917. /* Setup the JPEG codec */
  918. zr->codec->set_video(zr->codec, zr->timing, &cap,
  919. &zr->card.vfe_pol);
  920. zr->codec->set_mode(zr->codec, CODEC_DO_EXPANSION);
  921. init_jpeg_queue(zr);
  922. zr36057_set_jpg(zr, mode); // \P_Reset, ... Video param, FIFO
  923. clear_interrupt_counters(zr);
  924. dprintk(2, KERN_INFO "%s: enable_jpg(MOTION_DECOMPRESS)\n",
  925. ZR_DEVNAME(zr));
  926. break;
  927. case BUZ_MODE_IDLE:
  928. default:
  929. /* shut down processing */
  930. btand(~(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ),
  931. ZR36057_ICR);
  932. btwrite(zr->card.jpeg_int | ZR36057_ICR_JPEGRepIRQ,
  933. ZR36057_ISR);
  934. btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // \Go_en
  935. msleep(50);
  936. set_videobus_dir(zr, 0);
  937. set_frame(zr, 1); // /FRAME
  938. btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // /CFlush
  939. btwrite(0, ZR36057_JPC); // \P_Reset,\CodTrnsEn,\Active
  940. btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
  941. btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
  942. jpeg_codec_reset(zr);
  943. jpeg_codec_sleep(zr, 1);
  944. zr36057_adjust_vfe(zr, mode);
  945. decoder_call(zr, video, s_stream, 1);
  946. route.input = 0;
  947. encoder_call(zr, video, s_routing, &route);
  948. dprintk(2, KERN_INFO "%s: enable_jpg(IDLE)\n", ZR_DEVNAME(zr));
  949. break;
  950. }
  951. }
  952. /* when this is called the spinlock must be held */
  953. void
  954. zoran_feed_stat_com (struct zoran *zr)
  955. {
  956. /* move frames from pending queue to DMA */
  957. int frame, i, max_stat_com;
  958. max_stat_com =
  959. (zr->jpg_settings.TmpDcm ==
  960. 1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
  961. while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com &&
  962. zr->jpg_dma_head < zr->jpg_que_head) {
  963. frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
  964. if (zr->jpg_settings.TmpDcm == 1) {
  965. /* fill 1 stat_com entry */
  966. i = (zr->jpg_dma_head -
  967. zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  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].frag_tab_bus);
  972. } else {
  973. /* fill 2 stat_com entries */
  974. i = ((zr->jpg_dma_head -
  975. zr->jpg_err_shift) & 1) * 2;
  976. if (!(zr->stat_com[i] & cpu_to_le32(1)))
  977. break;
  978. zr->stat_com[i] =
  979. cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
  980. zr->stat_com[i + 1] =
  981. cpu_to_le32(zr->jpg_buffers.buffer[frame].frag_tab_bus);
  982. }
  983. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_DMA;
  984. zr->jpg_dma_head++;
  985. }
  986. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
  987. zr->jpg_queued_num++;
  988. }
  989. /* when this is called the spinlock must be held */
  990. static void
  991. zoran_reap_stat_com (struct zoran *zr)
  992. {
  993. /* move frames from DMA queue to done queue */
  994. int i;
  995. u32 stat_com;
  996. unsigned int seq;
  997. unsigned int dif;
  998. struct zoran_jpg_buffer *buffer;
  999. int frame;
  1000. /* In motion decompress we don't have a hardware frame counter,
  1001. * we just count the interrupts here */
  1002. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
  1003. zr->jpg_seq_num++;
  1004. }
  1005. while (zr->jpg_dma_tail < zr->jpg_dma_head) {
  1006. if (zr->jpg_settings.TmpDcm == 1)
  1007. i = (zr->jpg_dma_tail -
  1008. zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  1009. else
  1010. i = ((zr->jpg_dma_tail -
  1011. zr->jpg_err_shift) & 1) * 2 + 1;
  1012. stat_com = le32_to_cpu(zr->stat_com[i]);
  1013. if ((stat_com & 1) == 0) {
  1014. return;
  1015. }
  1016. frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
  1017. buffer = &zr->jpg_buffers.buffer[frame];
  1018. do_gettimeofday(&buffer->bs.timestamp);
  1019. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
  1020. buffer->bs.length = (stat_com & 0x7fffff) >> 1;
  1021. /* update sequence number with the help of the counter in stat_com */
  1022. seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
  1023. dif = (seq - zr->jpg_seq_num) & 0xff;
  1024. zr->jpg_seq_num += dif;
  1025. } else {
  1026. buffer->bs.length = 0;
  1027. }
  1028. buffer->bs.seq =
  1029. zr->jpg_settings.TmpDcm ==
  1030. 2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
  1031. buffer->state = BUZ_STATE_DONE;
  1032. zr->jpg_dma_tail++;
  1033. }
  1034. }
  1035. static void zoran_restart(struct zoran *zr)
  1036. {
  1037. /* Now the stat_comm buffer is ready for restart */
  1038. int status = 0, mode;
  1039. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
  1040. decoder_call(zr, video, g_input_status, &status);
  1041. mode = CODEC_DO_COMPRESSION;
  1042. } else {
  1043. status = V4L2_IN_ST_NO_SIGNAL;
  1044. mode = CODEC_DO_EXPANSION;
  1045. }
  1046. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
  1047. !(status & V4L2_IN_ST_NO_SIGNAL)) {
  1048. /********** RESTART code *************/
  1049. jpeg_codec_reset(zr);
  1050. zr->codec->set_mode(zr->codec, mode);
  1051. zr36057_set_jpg(zr, zr->codec_mode);
  1052. jpeg_start(zr);
  1053. if (zr->num_errors <= 8)
  1054. dprintk(2, KERN_INFO "%s: Restart\n",
  1055. ZR_DEVNAME(zr));
  1056. zr->JPEG_missed = 0;
  1057. zr->JPEG_error = 2;
  1058. /********** End RESTART code ***********/
  1059. }
  1060. }
  1061. static void
  1062. error_handler (struct zoran *zr,
  1063. u32 astat,
  1064. u32 stat)
  1065. {
  1066. int i, j;
  1067. /* This is JPEG error handling part */
  1068. if (zr->codec_mode != BUZ_MODE_MOTION_COMPRESS &&
  1069. zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS) {
  1070. return;
  1071. }
  1072. if ((stat & 1) == 0 &&
  1073. zr->codec_mode == BUZ_MODE_MOTION_COMPRESS &&
  1074. zr->jpg_dma_tail - zr->jpg_que_tail >= zr->jpg_buffers.num_buffers) {
  1075. /* No free buffers... */
  1076. zoran_reap_stat_com(zr);
  1077. zoran_feed_stat_com(zr);
  1078. wake_up_interruptible(&zr->jpg_capq);
  1079. zr->JPEG_missed = 0;
  1080. return;
  1081. }
  1082. if (zr->JPEG_error == 1) {
  1083. zoran_restart(zr);
  1084. return;
  1085. }
  1086. /*
  1087. * First entry: error just happened during normal operation
  1088. *
  1089. * In BUZ_MODE_MOTION_COMPRESS:
  1090. *
  1091. * Possible glitch in TV signal. In this case we should
  1092. * stop the codec and wait for good quality signal before
  1093. * restarting it to avoid further problems
  1094. *
  1095. * In BUZ_MODE_MOTION_DECOMPRESS:
  1096. *
  1097. * Bad JPEG frame: we have to mark it as processed (codec crashed
  1098. * and was not able to do it itself), and to remove it from queue.
  1099. */
  1100. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  1101. udelay(1);
  1102. stat = stat | (post_office_read(zr, 7, 0) & 3) << 8;
  1103. btwrite(0, ZR36057_JPC);
  1104. btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
  1105. jpeg_codec_reset(zr);
  1106. jpeg_codec_sleep(zr, 1);
  1107. zr->JPEG_error = 1;
  1108. zr->num_errors++;
  1109. /* Report error */
  1110. if (zr36067_debug > 1 && zr->num_errors <= 8) {
  1111. long frame;
  1112. frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
  1113. printk(KERN_ERR
  1114. "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
  1115. ZR_DEVNAME(zr), stat, zr->last_isr,
  1116. zr->jpg_que_tail, zr->jpg_dma_tail,
  1117. zr->jpg_dma_head, zr->jpg_que_head,
  1118. zr->jpg_seq_num, frame);
  1119. printk(KERN_INFO "stat_com frames:");
  1120. for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
  1121. for (i = 0; i < zr->jpg_buffers.num_buffers; i++) {
  1122. if (le32_to_cpu(zr->stat_com[j]) == zr->jpg_buffers.buffer[i].frag_tab_bus)
  1123. printk(KERN_CONT "% d->%d", j, i);
  1124. }
  1125. }
  1126. printk(KERN_CONT "\n");
  1127. }
  1128. /* Find an entry in stat_com and rotate contents */
  1129. if (zr->jpg_settings.TmpDcm == 1)
  1130. i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  1131. else
  1132. i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
  1133. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
  1134. /* Mimic zr36067 operation */
  1135. zr->stat_com[i] |= cpu_to_le32(1);
  1136. if (zr->jpg_settings.TmpDcm != 1)
  1137. zr->stat_com[i + 1] |= cpu_to_le32(1);
  1138. /* Refill */
  1139. zoran_reap_stat_com(zr);
  1140. zoran_feed_stat_com(zr);
  1141. wake_up_interruptible(&zr->jpg_capq);
  1142. /* Find an entry in stat_com again after refill */
  1143. if (zr->jpg_settings.TmpDcm == 1)
  1144. i = (zr->jpg_dma_tail - zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  1145. else
  1146. i = ((zr->jpg_dma_tail - zr->jpg_err_shift) & 1) * 2;
  1147. }
  1148. if (i) {
  1149. /* Rotate stat_comm entries to make current entry first */
  1150. int j;
  1151. __le32 bus_addr[BUZ_NUM_STAT_COM];
  1152. /* Here we are copying the stat_com array, which
  1153. * is already in little endian format, so
  1154. * no endian conversions here
  1155. */
  1156. memcpy(bus_addr, zr->stat_com, sizeof(bus_addr));
  1157. for (j = 0; j < BUZ_NUM_STAT_COM; j++)
  1158. zr->stat_com[j] = bus_addr[(i + j) & BUZ_MASK_STAT_COM];
  1159. zr->jpg_err_shift += i;
  1160. zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
  1161. }
  1162. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
  1163. zr->jpg_err_seq = zr->jpg_seq_num; /* + 1; */
  1164. zoran_restart(zr);
  1165. }
  1166. irqreturn_t
  1167. zoran_irq (int irq,
  1168. void *dev_id)
  1169. {
  1170. u32 stat, astat;
  1171. int count;
  1172. struct zoran *zr;
  1173. unsigned long flags;
  1174. zr = dev_id;
  1175. count = 0;
  1176. if (zr->testing) {
  1177. /* Testing interrupts */
  1178. spin_lock_irqsave(&zr->spinlock, flags);
  1179. while ((stat = count_reset_interrupt(zr))) {
  1180. if (count++ > 100) {
  1181. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1182. dprintk(1,
  1183. KERN_ERR
  1184. "%s: IRQ lockup while testing, isr=0x%08x, cleared int mask\n",
  1185. ZR_DEVNAME(zr), stat);
  1186. wake_up_interruptible(&zr->test_q);
  1187. }
  1188. }
  1189. zr->last_isr = stat;
  1190. spin_unlock_irqrestore(&zr->spinlock, flags);
  1191. return IRQ_HANDLED;
  1192. }
  1193. spin_lock_irqsave(&zr->spinlock, flags);
  1194. while (1) {
  1195. /* get/clear interrupt status bits */
  1196. stat = count_reset_interrupt(zr);
  1197. astat = stat & IRQ_MASK;
  1198. if (!astat) {
  1199. break;
  1200. }
  1201. dprintk(4,
  1202. KERN_DEBUG
  1203. "zoran_irq: astat: 0x%08x, mask: 0x%08x\n",
  1204. astat, btread(ZR36057_ICR));
  1205. if (astat & zr->card.vsync_int) { // SW
  1206. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
  1207. zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
  1208. /* count missed interrupts */
  1209. zr->JPEG_missed++;
  1210. }
  1211. //post_office_read(zr,1,0);
  1212. /* Interrupts may still happen when
  1213. * zr->v4l_memgrab_active is switched off.
  1214. * We simply ignore them */
  1215. if (zr->v4l_memgrab_active) {
  1216. /* A lot more checks should be here ... */
  1217. if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_SnapShot) == 0)
  1218. dprintk(1,
  1219. KERN_WARNING
  1220. "%s: BuzIRQ with SnapShot off ???\n",
  1221. ZR_DEVNAME(zr));
  1222. if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
  1223. /* There is a grab on a frame going on, check if it has finished */
  1224. if ((btread(ZR36057_VSSFGR) & ZR36057_VSSFGR_FrameGrab) == 0) {
  1225. /* it is finished, notify the user */
  1226. zr->v4l_buffers.buffer[zr->v4l_grab_frame].state = BUZ_STATE_DONE;
  1227. zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.seq = zr->v4l_grab_seq;
  1228. do_gettimeofday(&zr->v4l_buffers.buffer[zr->v4l_grab_frame].bs.timestamp);
  1229. zr->v4l_grab_frame = NO_GRAB_ACTIVE;
  1230. zr->v4l_pend_tail++;
  1231. }
  1232. }
  1233. if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
  1234. wake_up_interruptible(&zr->v4l_capq);
  1235. /* Check if there is another grab queued */
  1236. if (zr->v4l_grab_frame == NO_GRAB_ACTIVE &&
  1237. zr->v4l_pend_tail != zr->v4l_pend_head) {
  1238. int frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
  1239. u32 reg;
  1240. zr->v4l_grab_frame = frame;
  1241. /* Set zr36057 video front end and enable video */
  1242. /* Buffer address */
  1243. reg = zr->v4l_buffers.buffer[frame].fbuffer_bus;
  1244. btwrite(reg, ZR36057_VDTR);
  1245. if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
  1246. reg += zr->v4l_settings.bytesperline;
  1247. btwrite(reg, ZR36057_VDBR);
  1248. /* video stride, status, and frame grab register */
  1249. reg = 0;
  1250. if (zr->v4l_settings.height > BUZ_MAX_HEIGHT / 2)
  1251. reg += zr->v4l_settings.bytesperline;
  1252. reg = (reg << ZR36057_VSSFGR_DispStride);
  1253. reg |= ZR36057_VSSFGR_VidOvf;
  1254. reg |= ZR36057_VSSFGR_SnapShot;
  1255. reg |= ZR36057_VSSFGR_FrameGrab;
  1256. btwrite(reg, ZR36057_VSSFGR);
  1257. btor(ZR36057_VDCR_VidEn,
  1258. ZR36057_VDCR);
  1259. }
  1260. }
  1261. /* even if we don't grab, we do want to increment
  1262. * the sequence counter to see lost frames */
  1263. zr->v4l_grab_seq++;
  1264. }
  1265. #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
  1266. if (astat & ZR36057_ISR_CodRepIRQ) {
  1267. zr->intr_counter_CodRepIRQ++;
  1268. IDEBUG(printk(KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQ\n",
  1269. ZR_DEVNAME(zr)));
  1270. btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
  1271. }
  1272. #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
  1273. #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
  1274. if ((astat & ZR36057_ISR_JPEGRepIRQ) &&
  1275. (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS ||
  1276. zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)) {
  1277. if (zr36067_debug > 1 && (!zr->frame_num || zr->JPEG_error)) {
  1278. char sc[] = "0000";
  1279. char sv[5];
  1280. int i;
  1281. printk(KERN_INFO
  1282. "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%d\n",
  1283. ZR_DEVNAME(zr), stat,
  1284. zr->jpg_settings.odd_even,
  1285. zr->jpg_settings.field_per_buff,
  1286. zr->JPEG_missed);
  1287. strcpy(sv, sc);
  1288. for (i = 0; i < 4; i++) {
  1289. if (le32_to_cpu(zr->stat_com[i]) & 1)
  1290. sv[i] = '1';
  1291. }
  1292. sv[4] = 0;
  1293. printk(KERN_INFO
  1294. "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ld\n",
  1295. ZR_DEVNAME(zr), sv,
  1296. zr->jpg_que_tail,
  1297. zr->jpg_dma_tail,
  1298. zr->jpg_dma_head,
  1299. zr->jpg_que_head);
  1300. } else {
  1301. /* Get statistics */
  1302. if (zr->JPEG_missed > zr->JPEG_max_missed)
  1303. zr->JPEG_max_missed = zr->JPEG_missed;
  1304. if (zr->JPEG_missed < zr->JPEG_min_missed)
  1305. zr->JPEG_min_missed = zr->JPEG_missed;
  1306. }
  1307. if (zr36067_debug > 2 && zr->frame_num < 6) {
  1308. int i;
  1309. printk(KERN_INFO "%s: seq=%ld stat_com:",
  1310. ZR_DEVNAME(zr), zr->jpg_seq_num);
  1311. for (i = 0; i < 4; i++) {
  1312. printk(KERN_CONT " %08x",
  1313. le32_to_cpu(zr->stat_com[i]));
  1314. }
  1315. printk(KERN_CONT "\n");
  1316. }
  1317. zr->frame_num++;
  1318. zr->JPEG_missed = 0;
  1319. zr->JPEG_error = 0;
  1320. zoran_reap_stat_com(zr);
  1321. zoran_feed_stat_com(zr);
  1322. wake_up_interruptible(&zr->jpg_capq);
  1323. }
  1324. #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
  1325. /* DATERR, too many fields missed, error processing */
  1326. if ((astat & zr->card.jpeg_int) ||
  1327. zr->JPEG_missed > 25 ||
  1328. zr->JPEG_error == 1 ||
  1329. ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) &&
  1330. (zr->frame_num & (zr->JPEG_missed > zr->jpg_settings.field_per_buff)))) {
  1331. error_handler(zr, astat, stat);
  1332. }
  1333. count++;
  1334. if (count > 10) {
  1335. dprintk(2, KERN_WARNING "%s: irq loop %d\n",
  1336. ZR_DEVNAME(zr), count);
  1337. if (count > 20) {
  1338. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1339. dprintk(2,
  1340. KERN_ERR
  1341. "%s: IRQ lockup, cleared int mask\n",
  1342. ZR_DEVNAME(zr));
  1343. break;
  1344. }
  1345. }
  1346. zr->last_isr = stat;
  1347. }
  1348. spin_unlock_irqrestore(&zr->spinlock, flags);
  1349. return IRQ_HANDLED;
  1350. }
  1351. void
  1352. zoran_set_pci_master (struct zoran *zr,
  1353. int set_master)
  1354. {
  1355. if (set_master) {
  1356. pci_set_master(zr->pci_dev);
  1357. } else {
  1358. u16 command;
  1359. pci_read_config_word(zr->pci_dev, PCI_COMMAND, &command);
  1360. command &= ~PCI_COMMAND_MASTER;
  1361. pci_write_config_word(zr->pci_dev, PCI_COMMAND, command);
  1362. }
  1363. }
  1364. void
  1365. zoran_init_hardware (struct zoran *zr)
  1366. {
  1367. struct v4l2_routing route = { 0, 0 };
  1368. /* Enable bus-mastering */
  1369. zoran_set_pci_master(zr, 1);
  1370. /* Initialize the board */
  1371. if (zr->card.init) {
  1372. zr->card.init(zr);
  1373. }
  1374. route.input = zr->card.input[zr->input].muxsel;
  1375. decoder_call(zr, core, init, 0);
  1376. decoder_s_std(zr, zr->norm);
  1377. decoder_s_routing(zr, &route);
  1378. encoder_call(zr, core, init, 0);
  1379. encoder_call(zr, video, s_std_output, zr->norm);
  1380. route.input = 0;
  1381. encoder_call(zr, video, s_routing, &route);
  1382. /* toggle JPEG codec sleep to sync PLL */
  1383. jpeg_codec_sleep(zr, 1);
  1384. jpeg_codec_sleep(zr, 0);
  1385. /* set individual interrupt enables (without GIRQ1)
  1386. * but don't global enable until zoran_open() */
  1387. //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR); // SW
  1388. // It looks like using only JPEGRepIRQEn is not always reliable,
  1389. // may be when JPEG codec crashes it won't generate IRQ? So,
  1390. /*CP*/ // btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM WHY ? LP
  1391. zr36057_init_vfe(zr);
  1392. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1393. btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
  1394. }
  1395. void
  1396. zr36057_restart (struct zoran *zr)
  1397. {
  1398. btwrite(0, ZR36057_SPGPPCR);
  1399. mdelay(1);
  1400. btor(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
  1401. mdelay(1);
  1402. /* assert P_Reset */
  1403. btwrite(0, ZR36057_JPC);
  1404. /* set up GPIO direction - all output */
  1405. btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
  1406. /* set up GPIO pins and guest bus timing */
  1407. btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
  1408. }
  1409. /*
  1410. * initialize video front end
  1411. */
  1412. static void
  1413. zr36057_init_vfe (struct zoran *zr)
  1414. {
  1415. u32 reg;
  1416. reg = btread(ZR36057_VFESPFR);
  1417. reg |= ZR36057_VFESPFR_LittleEndian;
  1418. reg &= ~ZR36057_VFESPFR_VCLKPol;
  1419. reg |= ZR36057_VFESPFR_ExtFl;
  1420. reg |= ZR36057_VFESPFR_TopField;
  1421. btwrite(reg, ZR36057_VFESPFR);
  1422. reg = btread(ZR36057_VDCR);
  1423. if (pci_pci_problems & PCIPCI_TRITON)
  1424. // || zr->revision < 1) // Revision 1 has also Triton support
  1425. reg &= ~ZR36057_VDCR_Triton;
  1426. else
  1427. reg |= ZR36057_VDCR_Triton;
  1428. btwrite(reg, ZR36057_VDCR);
  1429. }
  1430. /*
  1431. * Interface to decoder and encoder chips using i2c bus
  1432. */
  1433. int decoder_s_std(struct zoran *zr, v4l2_std_id std)
  1434. {
  1435. int res;
  1436. /* Bt819 needs to reset its FIFO buffer using #FRST pin and
  1437. LML33 card uses GPIO(7) for that. */
  1438. if (zr->card.type == LML33)
  1439. GPIO(zr, 7, 0);
  1440. res = decoder_call(zr, tuner, s_std, std);
  1441. if (zr->card.type == LML33)
  1442. GPIO(zr, 7, 1); /* Pull #FRST high. */
  1443. return res;
  1444. }
  1445. int decoder_s_routing(struct zoran *zr, struct v4l2_routing *route)
  1446. {
  1447. int res;
  1448. /* Bt819 needs to reset its FIFO buffer using #FRST pin and
  1449. LML33 card uses GPIO(7) for that. */
  1450. if (zr->card.type == LML33)
  1451. GPIO(zr, 7, 0);
  1452. res = decoder_call(zr, video, s_routing, route);
  1453. if (zr->card.type == LML33)
  1454. GPIO(zr, 7, 1); /* Pull #FRST high. */
  1455. return res;
  1456. }