bttv-risc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. bttv-risc.c -- interfaces to other kernel modules
  3. bttv risc code handling
  4. - memory management
  5. - generation
  6. (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/page.h>
  27. #include <asm/pgtable.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include "bttvp.h"
  30. #define VCR_HACK_LINES 4
  31. /* ---------------------------------------------------------- */
  32. /* risc code generators */
  33. int
  34. bttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
  35. struct scatterlist *sglist,
  36. unsigned int offset, unsigned int bpl,
  37. unsigned int padding, unsigned int skip_lines,
  38. unsigned int store_lines)
  39. {
  40. u32 instructions,line,todo;
  41. struct scatterlist *sg;
  42. __le32 *rp;
  43. int rc;
  44. /* estimate risc mem: worst case is one write per page border +
  45. one write per scan line + sync + jump (all 2 dwords). padding
  46. can cause next bpl to start close to a page border. First DMA
  47. region may be smaller than PAGE_SIZE */
  48. instructions = skip_lines * 4;
  49. instructions += (1 + ((bpl + padding) * store_lines)
  50. / PAGE_SIZE + store_lines) * 8;
  51. instructions += 2 * 8;
  52. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions)) < 0)
  53. return rc;
  54. /* sync instruction */
  55. rp = risc->cpu;
  56. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
  57. *(rp++) = cpu_to_le32(0);
  58. while (skip_lines-- > 0) {
  59. *(rp++) = cpu_to_le32(BT848_RISC_SKIP | BT848_RISC_SOL |
  60. BT848_RISC_EOL | bpl);
  61. }
  62. /* scan lines */
  63. sg = sglist;
  64. for (line = 0; line < store_lines; line++) {
  65. if ((btv->opt_vcr_hack) &&
  66. (line >= (store_lines - VCR_HACK_LINES)))
  67. continue;
  68. while (offset && offset >= sg_dma_len(sg)) {
  69. offset -= sg_dma_len(sg);
  70. sg = sg_next(sg);
  71. }
  72. if (bpl <= sg_dma_len(sg)-offset) {
  73. /* fits into current chunk */
  74. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
  75. BT848_RISC_EOL|bpl);
  76. *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
  77. offset+=bpl;
  78. } else {
  79. /* scanline needs to be splitted */
  80. todo = bpl;
  81. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
  82. (sg_dma_len(sg)-offset));
  83. *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
  84. todo -= (sg_dma_len(sg)-offset);
  85. offset = 0;
  86. sg = sg_next(sg);
  87. while (todo > sg_dma_len(sg)) {
  88. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|
  89. sg_dma_len(sg));
  90. *(rp++)=cpu_to_le32(sg_dma_address(sg));
  91. todo -= sg_dma_len(sg);
  92. sg = sg_next(sg);
  93. }
  94. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|
  95. todo);
  96. *(rp++)=cpu_to_le32(sg_dma_address(sg));
  97. offset += todo;
  98. }
  99. offset += padding;
  100. }
  101. /* save pointer to jmp instruction address */
  102. risc->jmp = rp;
  103. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  104. return 0;
  105. }
  106. static int
  107. bttv_risc_planar(struct bttv *btv, struct btcx_riscmem *risc,
  108. struct scatterlist *sglist,
  109. unsigned int yoffset, unsigned int ybpl,
  110. unsigned int ypadding, unsigned int ylines,
  111. unsigned int uoffset, unsigned int voffset,
  112. unsigned int hshift, unsigned int vshift,
  113. unsigned int cpadding)
  114. {
  115. unsigned int instructions,line,todo,ylen,chroma;
  116. __le32 *rp;
  117. u32 ri;
  118. struct scatterlist *ysg;
  119. struct scatterlist *usg;
  120. struct scatterlist *vsg;
  121. int topfield = (0 == yoffset);
  122. int rc;
  123. /* estimate risc mem: worst case is one write per page border +
  124. one write per scan line (5 dwords)
  125. plus sync + jump (2 dwords) */
  126. instructions = ((3 + (ybpl + ypadding) * ylines * 2)
  127. / PAGE_SIZE) + ylines;
  128. instructions += 2;
  129. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions*4*5)) < 0)
  130. return rc;
  131. /* sync instruction */
  132. rp = risc->cpu;
  133. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
  134. *(rp++) = cpu_to_le32(0);
  135. /* scan lines */
  136. ysg = sglist;
  137. usg = sglist;
  138. vsg = sglist;
  139. for (line = 0; line < ylines; line++) {
  140. if ((btv->opt_vcr_hack) &&
  141. (line >= (ylines - VCR_HACK_LINES)))
  142. continue;
  143. switch (vshift) {
  144. case 0:
  145. chroma = 1;
  146. break;
  147. case 1:
  148. if (topfield)
  149. chroma = ((line & 1) == 0);
  150. else
  151. chroma = ((line & 1) == 1);
  152. break;
  153. case 2:
  154. if (topfield)
  155. chroma = ((line & 3) == 0);
  156. else
  157. chroma = ((line & 3) == 2);
  158. break;
  159. default:
  160. chroma = 0;
  161. break;
  162. }
  163. for (todo = ybpl; todo > 0; todo -= ylen) {
  164. /* go to next sg entry if needed */
  165. while (yoffset && yoffset >= sg_dma_len(ysg)) {
  166. yoffset -= sg_dma_len(ysg);
  167. ysg = sg_next(ysg);
  168. }
  169. /* calculate max number of bytes we can write */
  170. ylen = todo;
  171. if (yoffset + ylen > sg_dma_len(ysg))
  172. ylen = sg_dma_len(ysg) - yoffset;
  173. if (chroma) {
  174. while (uoffset && uoffset >= sg_dma_len(usg)) {
  175. uoffset -= sg_dma_len(usg);
  176. usg = sg_next(usg);
  177. }
  178. while (voffset && voffset >= sg_dma_len(vsg)) {
  179. voffset -= sg_dma_len(vsg);
  180. vsg = sg_next(vsg);
  181. }
  182. if (uoffset + (ylen>>hshift) > sg_dma_len(usg))
  183. ylen = (sg_dma_len(usg) - uoffset) << hshift;
  184. if (voffset + (ylen>>hshift) > sg_dma_len(vsg))
  185. ylen = (sg_dma_len(vsg) - voffset) << hshift;
  186. ri = BT848_RISC_WRITE123;
  187. } else {
  188. ri = BT848_RISC_WRITE1S23;
  189. }
  190. if (ybpl == todo)
  191. ri |= BT848_RISC_SOL;
  192. if (ylen == todo)
  193. ri |= BT848_RISC_EOL;
  194. /* write risc instruction */
  195. *(rp++)=cpu_to_le32(ri | ylen);
  196. *(rp++)=cpu_to_le32(((ylen >> hshift) << 16) |
  197. (ylen >> hshift));
  198. *(rp++)=cpu_to_le32(sg_dma_address(ysg)+yoffset);
  199. yoffset += ylen;
  200. if (chroma) {
  201. *(rp++)=cpu_to_le32(sg_dma_address(usg)+uoffset);
  202. uoffset += ylen >> hshift;
  203. *(rp++)=cpu_to_le32(sg_dma_address(vsg)+voffset);
  204. voffset += ylen >> hshift;
  205. }
  206. }
  207. yoffset += ypadding;
  208. if (chroma) {
  209. uoffset += cpadding;
  210. voffset += cpadding;
  211. }
  212. }
  213. /* save pointer to jmp instruction address */
  214. risc->jmp = rp;
  215. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  216. return 0;
  217. }
  218. static int
  219. bttv_risc_overlay(struct bttv *btv, struct btcx_riscmem *risc,
  220. const struct bttv_format *fmt, struct bttv_overlay *ov,
  221. int skip_even, int skip_odd)
  222. {
  223. int dwords, rc, line, maxy, start, end;
  224. unsigned skip, nskips;
  225. struct btcx_skiplist *skips;
  226. __le32 *rp;
  227. u32 ri,ra;
  228. u32 addr;
  229. /* skip list for window clipping */
  230. skips = kmalloc_array(ov->nclips, sizeof(*skips),GFP_KERNEL);
  231. if (NULL == skips)
  232. return -ENOMEM;
  233. /* estimate risc mem: worst case is (1.5*clip+1) * lines instructions
  234. + sync + jump (all 2 dwords) */
  235. dwords = (3 * ov->nclips + 2) *
  236. ((skip_even || skip_odd) ? (ov->w.height+1)>>1 : ov->w.height);
  237. dwords += 4;
  238. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,dwords*4)) < 0) {
  239. kfree(skips);
  240. return rc;
  241. }
  242. /* sync instruction */
  243. rp = risc->cpu;
  244. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
  245. *(rp++) = cpu_to_le32(0);
  246. addr = (unsigned long)btv->fbuf.base;
  247. addr += btv->fbuf.fmt.bytesperline * ov->w.top;
  248. addr += (fmt->depth >> 3) * ov->w.left;
  249. /* scan lines */
  250. for (maxy = -1, line = 0; line < ov->w.height;
  251. line++, addr += btv->fbuf.fmt.bytesperline) {
  252. if ((btv->opt_vcr_hack) &&
  253. (line >= (ov->w.height - VCR_HACK_LINES)))
  254. continue;
  255. if ((line%2) == 0 && skip_even)
  256. continue;
  257. if ((line%2) == 1 && skip_odd)
  258. continue;
  259. /* calculate clipping */
  260. if (line > maxy)
  261. btcx_calc_skips(line, ov->w.width, &maxy,
  262. skips, &nskips, ov->clips, ov->nclips);
  263. /* write out risc code */
  264. for (start = 0, skip = 0; start < ov->w.width; start = end) {
  265. if (skip >= nskips) {
  266. ri = BT848_RISC_WRITE;
  267. end = ov->w.width;
  268. } else if (start < skips[skip].start) {
  269. ri = BT848_RISC_WRITE;
  270. end = skips[skip].start;
  271. } else {
  272. ri = BT848_RISC_SKIP;
  273. end = skips[skip].end;
  274. skip++;
  275. }
  276. if (BT848_RISC_WRITE == ri)
  277. ra = addr + (fmt->depth>>3)*start;
  278. else
  279. ra = 0;
  280. if (0 == start)
  281. ri |= BT848_RISC_SOL;
  282. if (ov->w.width == end)
  283. ri |= BT848_RISC_EOL;
  284. ri |= (fmt->depth>>3) * (end-start);
  285. *(rp++)=cpu_to_le32(ri);
  286. if (0 != ra)
  287. *(rp++)=cpu_to_le32(ra);
  288. }
  289. }
  290. /* save pointer to jmp instruction address */
  291. risc->jmp = rp;
  292. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  293. kfree(skips);
  294. return 0;
  295. }
  296. /* ---------------------------------------------------------- */
  297. static void
  298. bttv_calc_geo_old(struct bttv *btv, struct bttv_geometry *geo,
  299. int width, int height, int interleaved,
  300. const struct bttv_tvnorm *tvnorm)
  301. {
  302. u32 xsf, sr;
  303. int vdelay;
  304. int swidth = tvnorm->swidth;
  305. int totalwidth = tvnorm->totalwidth;
  306. int scaledtwidth = tvnorm->scaledtwidth;
  307. if (btv->input == btv->dig) {
  308. swidth = 720;
  309. totalwidth = 858;
  310. scaledtwidth = 858;
  311. }
  312. vdelay = tvnorm->vdelay;
  313. xsf = (width*scaledtwidth)/swidth;
  314. geo->hscale = ((totalwidth*4096UL)/xsf-4096);
  315. geo->hdelay = tvnorm->hdelayx1;
  316. geo->hdelay = (geo->hdelay*width)/swidth;
  317. geo->hdelay &= 0x3fe;
  318. sr = ((tvnorm->sheight >> (interleaved?0:1))*512)/height - 512;
  319. geo->vscale = (0x10000UL-sr) & 0x1fff;
  320. geo->crop = ((width>>8)&0x03) | ((geo->hdelay>>6)&0x0c) |
  321. ((tvnorm->sheight>>4)&0x30) | ((vdelay>>2)&0xc0);
  322. geo->vscale |= interleaved ? (BT848_VSCALE_INT<<8) : 0;
  323. geo->vdelay = vdelay;
  324. geo->width = width;
  325. geo->sheight = tvnorm->sheight;
  326. geo->vtotal = tvnorm->vtotal;
  327. if (btv->opt_combfilter) {
  328. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  329. geo->comb = (width < 769) ? 1 : 0;
  330. } else {
  331. geo->vtc = 0;
  332. geo->comb = 0;
  333. }
  334. }
  335. static void
  336. bttv_calc_geo (struct bttv * btv,
  337. struct bttv_geometry * geo,
  338. unsigned int width,
  339. unsigned int height,
  340. int both_fields,
  341. const struct bttv_tvnorm * tvnorm,
  342. const struct v4l2_rect * crop)
  343. {
  344. unsigned int c_width;
  345. unsigned int c_height;
  346. u32 sr;
  347. if ((crop->left == tvnorm->cropcap.defrect.left
  348. && crop->top == tvnorm->cropcap.defrect.top
  349. && crop->width == tvnorm->cropcap.defrect.width
  350. && crop->height == tvnorm->cropcap.defrect.height
  351. && width <= tvnorm->swidth /* see PAL-Nc et al */)
  352. || btv->input == btv->dig) {
  353. bttv_calc_geo_old(btv, geo, width, height,
  354. both_fields, tvnorm);
  355. return;
  356. }
  357. /* For bug compatibility the image size checks permit scale
  358. factors > 16. See bttv_crop_calc_limits(). */
  359. c_width = min((unsigned int) crop->width, width * 16);
  360. c_height = min((unsigned int) crop->height, height * 16);
  361. geo->width = width;
  362. geo->hscale = (c_width * 4096U + (width >> 1)) / width - 4096;
  363. /* Even to store Cb first, odd for Cr. */
  364. geo->hdelay = ((crop->left * width + c_width) / c_width) & ~1;
  365. geo->sheight = c_height;
  366. geo->vdelay = crop->top - tvnorm->cropcap.bounds.top + MIN_VDELAY;
  367. sr = c_height >> !both_fields;
  368. sr = (sr * 512U + (height >> 1)) / height - 512;
  369. geo->vscale = (0x10000UL - sr) & 0x1fff;
  370. geo->vscale |= both_fields ? (BT848_VSCALE_INT << 8) : 0;
  371. geo->vtotal = tvnorm->vtotal;
  372. geo->crop = (((geo->width >> 8) & 0x03) |
  373. ((geo->hdelay >> 6) & 0x0c) |
  374. ((geo->sheight >> 4) & 0x30) |
  375. ((geo->vdelay >> 2) & 0xc0));
  376. if (btv->opt_combfilter) {
  377. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  378. geo->comb = (width < 769) ? 1 : 0;
  379. } else {
  380. geo->vtc = 0;
  381. geo->comb = 0;
  382. }
  383. }
  384. static void
  385. bttv_apply_geo(struct bttv *btv, struct bttv_geometry *geo, int odd)
  386. {
  387. int off = odd ? 0x80 : 0x00;
  388. if (geo->comb)
  389. btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  390. else
  391. btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  392. btwrite(geo->vtc, BT848_E_VTC+off);
  393. btwrite(geo->hscale >> 8, BT848_E_HSCALE_HI+off);
  394. btwrite(geo->hscale & 0xff, BT848_E_HSCALE_LO+off);
  395. btaor((geo->vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
  396. btwrite(geo->vscale & 0xff, BT848_E_VSCALE_LO+off);
  397. btwrite(geo->width & 0xff, BT848_E_HACTIVE_LO+off);
  398. btwrite(geo->hdelay & 0xff, BT848_E_HDELAY_LO+off);
  399. btwrite(geo->sheight & 0xff, BT848_E_VACTIVE_LO+off);
  400. btwrite(geo->vdelay & 0xff, BT848_E_VDELAY_LO+off);
  401. btwrite(geo->crop, BT848_E_CROP+off);
  402. btwrite(geo->vtotal>>8, BT848_VTOTAL_HI);
  403. btwrite(geo->vtotal & 0xff, BT848_VTOTAL_LO);
  404. }
  405. /* ---------------------------------------------------------- */
  406. /* risc group / risc main loop / dma management */
  407. void
  408. bttv_set_dma(struct bttv *btv, int override)
  409. {
  410. unsigned long cmd;
  411. int capctl;
  412. btv->cap_ctl = 0;
  413. if (NULL != btv->curr.top) btv->cap_ctl |= 0x02;
  414. if (NULL != btv->curr.bottom) btv->cap_ctl |= 0x01;
  415. if (NULL != btv->cvbi) btv->cap_ctl |= 0x0c;
  416. capctl = 0;
  417. capctl |= (btv->cap_ctl & 0x03) ? 0x03 : 0x00; /* capture */
  418. capctl |= (btv->cap_ctl & 0x0c) ? 0x0c : 0x00; /* vbi data */
  419. capctl |= override;
  420. d2printk("%d: capctl=%x lirq=%d top=%08llx/%08llx even=%08llx/%08llx\n",
  421. btv->c.nr,capctl,btv->loop_irq,
  422. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  423. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  424. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0,
  425. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  426. cmd = BT848_RISC_JUMP;
  427. if (btv->loop_irq) {
  428. cmd |= BT848_RISC_IRQ;
  429. cmd |= (btv->loop_irq & 0x0f) << 16;
  430. cmd |= (~btv->loop_irq & 0x0f) << 20;
  431. }
  432. if (btv->curr.frame_irq || btv->loop_irq || btv->cvbi) {
  433. mod_timer(&btv->timeout, jiffies+BTTV_TIMEOUT);
  434. } else {
  435. del_timer(&btv->timeout);
  436. }
  437. btv->main.cpu[RISC_SLOT_LOOP] = cpu_to_le32(cmd);
  438. btaor(capctl, ~0x0f, BT848_CAP_CTL);
  439. if (capctl) {
  440. if (btv->dma_on)
  441. return;
  442. btwrite(btv->main.dma, BT848_RISC_STRT_ADD);
  443. btor(3, BT848_GPIO_DMA_CTL);
  444. btv->dma_on = 1;
  445. } else {
  446. if (!btv->dma_on)
  447. return;
  448. btand(~3, BT848_GPIO_DMA_CTL);
  449. btv->dma_on = 0;
  450. }
  451. return;
  452. }
  453. int
  454. bttv_risc_init_main(struct bttv *btv)
  455. {
  456. int rc;
  457. if ((rc = btcx_riscmem_alloc(btv->c.pci,&btv->main,PAGE_SIZE)) < 0)
  458. return rc;
  459. dprintk("%d: risc main @ %08llx\n",
  460. btv->c.nr, (unsigned long long)btv->main.dma);
  461. btv->main.cpu[0] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  462. BT848_FIFO_STATUS_VRE);
  463. btv->main.cpu[1] = cpu_to_le32(0);
  464. btv->main.cpu[2] = cpu_to_le32(BT848_RISC_JUMP);
  465. btv->main.cpu[3] = cpu_to_le32(btv->main.dma + (4<<2));
  466. /* top field */
  467. btv->main.cpu[4] = cpu_to_le32(BT848_RISC_JUMP);
  468. btv->main.cpu[5] = cpu_to_le32(btv->main.dma + (6<<2));
  469. btv->main.cpu[6] = cpu_to_le32(BT848_RISC_JUMP);
  470. btv->main.cpu[7] = cpu_to_le32(btv->main.dma + (8<<2));
  471. btv->main.cpu[8] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  472. BT848_FIFO_STATUS_VRO);
  473. btv->main.cpu[9] = cpu_to_le32(0);
  474. /* bottom field */
  475. btv->main.cpu[10] = cpu_to_le32(BT848_RISC_JUMP);
  476. btv->main.cpu[11] = cpu_to_le32(btv->main.dma + (12<<2));
  477. btv->main.cpu[12] = cpu_to_le32(BT848_RISC_JUMP);
  478. btv->main.cpu[13] = cpu_to_le32(btv->main.dma + (14<<2));
  479. /* jump back to top field */
  480. btv->main.cpu[14] = cpu_to_le32(BT848_RISC_JUMP);
  481. btv->main.cpu[15] = cpu_to_le32(btv->main.dma + (0<<2));
  482. return 0;
  483. }
  484. int
  485. bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
  486. int irqflags)
  487. {
  488. unsigned long cmd;
  489. unsigned long next = btv->main.dma + ((slot+2) << 2);
  490. if (NULL == risc) {
  491. d2printk("%d: risc=%p slot[%d]=NULL\n", btv->c.nr, risc, slot);
  492. btv->main.cpu[slot+1] = cpu_to_le32(next);
  493. } else {
  494. d2printk("%d: risc=%p slot[%d]=%08llx irq=%d\n",
  495. btv->c.nr, risc, slot,
  496. (unsigned long long)risc->dma, irqflags);
  497. cmd = BT848_RISC_JUMP;
  498. if (irqflags) {
  499. cmd |= BT848_RISC_IRQ;
  500. cmd |= (irqflags & 0x0f) << 16;
  501. cmd |= (~irqflags & 0x0f) << 20;
  502. }
  503. risc->jmp[0] = cpu_to_le32(cmd);
  504. risc->jmp[1] = cpu_to_le32(next);
  505. btv->main.cpu[slot+1] = cpu_to_le32(risc->dma);
  506. }
  507. return 0;
  508. }
  509. void
  510. bttv_dma_free(struct videobuf_queue *q,struct bttv *btv, struct bttv_buffer *buf)
  511. {
  512. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  513. BUG_ON(in_interrupt());
  514. videobuf_waiton(q, &buf->vb, 0, 0);
  515. videobuf_dma_unmap(q->dev, dma);
  516. videobuf_dma_free(dma);
  517. btcx_riscmem_free(btv->c.pci,&buf->bottom);
  518. btcx_riscmem_free(btv->c.pci,&buf->top);
  519. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  520. }
  521. int
  522. bttv_buffer_activate_vbi(struct bttv *btv,
  523. struct bttv_buffer *vbi)
  524. {
  525. struct btcx_riscmem *top;
  526. struct btcx_riscmem *bottom;
  527. int top_irq_flags;
  528. int bottom_irq_flags;
  529. top = NULL;
  530. bottom = NULL;
  531. top_irq_flags = 0;
  532. bottom_irq_flags = 0;
  533. if (vbi) {
  534. unsigned int crop, vdelay;
  535. vbi->vb.state = VIDEOBUF_ACTIVE;
  536. list_del(&vbi->vb.queue);
  537. /* VDELAY is start of video, end of VBI capturing. */
  538. crop = btread(BT848_E_CROP);
  539. vdelay = btread(BT848_E_VDELAY_LO) + ((crop & 0xc0) << 2);
  540. if (vbi->geo.vdelay > vdelay) {
  541. vdelay = vbi->geo.vdelay & 0xfe;
  542. crop = (crop & 0x3f) | ((vbi->geo.vdelay >> 2) & 0xc0);
  543. btwrite(vdelay, BT848_E_VDELAY_LO);
  544. btwrite(crop, BT848_E_CROP);
  545. btwrite(vdelay, BT848_O_VDELAY_LO);
  546. btwrite(crop, BT848_O_CROP);
  547. }
  548. if (vbi->vbi_count[0] > 0) {
  549. top = &vbi->top;
  550. top_irq_flags = 4;
  551. }
  552. if (vbi->vbi_count[1] > 0) {
  553. top_irq_flags = 0;
  554. bottom = &vbi->bottom;
  555. bottom_irq_flags = 4;
  556. }
  557. }
  558. bttv_risc_hook(btv, RISC_SLOT_O_VBI, top, top_irq_flags);
  559. bttv_risc_hook(btv, RISC_SLOT_E_VBI, bottom, bottom_irq_flags);
  560. return 0;
  561. }
  562. int
  563. bttv_buffer_activate_video(struct bttv *btv,
  564. struct bttv_buffer_set *set)
  565. {
  566. /* video capture */
  567. if (NULL != set->top && NULL != set->bottom) {
  568. if (set->top == set->bottom) {
  569. set->top->vb.state = VIDEOBUF_ACTIVE;
  570. if (set->top->vb.queue.next)
  571. list_del(&set->top->vb.queue);
  572. } else {
  573. set->top->vb.state = VIDEOBUF_ACTIVE;
  574. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  575. if (set->top->vb.queue.next)
  576. list_del(&set->top->vb.queue);
  577. if (set->bottom->vb.queue.next)
  578. list_del(&set->bottom->vb.queue);
  579. }
  580. bttv_apply_geo(btv, &set->top->geo, 1);
  581. bttv_apply_geo(btv, &set->bottom->geo,0);
  582. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  583. set->top_irq);
  584. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  585. set->frame_irq);
  586. btaor((set->top->btformat & 0xf0) | (set->bottom->btformat & 0x0f),
  587. ~0xff, BT848_COLOR_FMT);
  588. btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
  589. ~0x0f, BT848_COLOR_CTL);
  590. } else if (NULL != set->top) {
  591. set->top->vb.state = VIDEOBUF_ACTIVE;
  592. if (set->top->vb.queue.next)
  593. list_del(&set->top->vb.queue);
  594. bttv_apply_geo(btv, &set->top->geo,1);
  595. bttv_apply_geo(btv, &set->top->geo,0);
  596. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  597. set->frame_irq);
  598. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  599. btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  600. btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  601. } else if (NULL != set->bottom) {
  602. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  603. if (set->bottom->vb.queue.next)
  604. list_del(&set->bottom->vb.queue);
  605. bttv_apply_geo(btv, &set->bottom->geo,1);
  606. bttv_apply_geo(btv, &set->bottom->geo,0);
  607. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  608. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  609. set->frame_irq);
  610. btaor(set->bottom->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  611. btaor(set->bottom->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  612. } else {
  613. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  614. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  615. }
  616. return 0;
  617. }
  618. /* ---------------------------------------------------------- */
  619. /* calculate geometry, build risc code */
  620. int
  621. bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf)
  622. {
  623. const struct bttv_tvnorm *tvnorm = bttv_tvnorms + buf->tvnorm;
  624. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  625. dprintk("%d: buffer field: %s format: %s size: %dx%d\n",
  626. btv->c.nr, v4l2_field_names[buf->vb.field],
  627. buf->fmt->name, buf->vb.width, buf->vb.height);
  628. /* packed pixel modes */
  629. if (buf->fmt->flags & FORMAT_FLAGS_PACKED) {
  630. int bpl = (buf->fmt->depth >> 3) * buf->vb.width;
  631. int bpf = bpl * (buf->vb.height >> 1);
  632. bttv_calc_geo(btv,&buf->geo,buf->vb.width,buf->vb.height,
  633. V4L2_FIELD_HAS_BOTH(buf->vb.field),
  634. tvnorm,&buf->crop);
  635. switch (buf->vb.field) {
  636. case V4L2_FIELD_TOP:
  637. bttv_risc_packed(btv,&buf->top,dma->sglist,
  638. /* offset */ 0,bpl,
  639. /* padding */ 0,/* skip_lines */ 0,
  640. buf->vb.height);
  641. break;
  642. case V4L2_FIELD_BOTTOM:
  643. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  644. 0,bpl,0,0,buf->vb.height);
  645. break;
  646. case V4L2_FIELD_INTERLACED:
  647. bttv_risc_packed(btv,&buf->top,dma->sglist,
  648. 0,bpl,bpl,0,buf->vb.height >> 1);
  649. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  650. bpl,bpl,bpl,0,buf->vb.height >> 1);
  651. break;
  652. case V4L2_FIELD_SEQ_TB:
  653. bttv_risc_packed(btv,&buf->top,dma->sglist,
  654. 0,bpl,0,0,buf->vb.height >> 1);
  655. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  656. bpf,bpl,0,0,buf->vb.height >> 1);
  657. break;
  658. default:
  659. BUG();
  660. }
  661. }
  662. /* planar modes */
  663. if (buf->fmt->flags & FORMAT_FLAGS_PLANAR) {
  664. int uoffset, voffset;
  665. int ypadding, cpadding, lines;
  666. /* calculate chroma offsets */
  667. uoffset = buf->vb.width * buf->vb.height;
  668. voffset = buf->vb.width * buf->vb.height;
  669. if (buf->fmt->flags & FORMAT_FLAGS_CrCb) {
  670. /* Y-Cr-Cb plane order */
  671. uoffset >>= buf->fmt->hshift;
  672. uoffset >>= buf->fmt->vshift;
  673. uoffset += voffset;
  674. } else {
  675. /* Y-Cb-Cr plane order */
  676. voffset >>= buf->fmt->hshift;
  677. voffset >>= buf->fmt->vshift;
  678. voffset += uoffset;
  679. }
  680. switch (buf->vb.field) {
  681. case V4L2_FIELD_TOP:
  682. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  683. buf->vb.height,/* both_fields */ 0,
  684. tvnorm,&buf->crop);
  685. bttv_risc_planar(btv, &buf->top, dma->sglist,
  686. 0,buf->vb.width,0,buf->vb.height,
  687. uoffset,voffset,buf->fmt->hshift,
  688. buf->fmt->vshift,0);
  689. break;
  690. case V4L2_FIELD_BOTTOM:
  691. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  692. buf->vb.height,0,
  693. tvnorm,&buf->crop);
  694. bttv_risc_planar(btv, &buf->bottom, dma->sglist,
  695. 0,buf->vb.width,0,buf->vb.height,
  696. uoffset,voffset,buf->fmt->hshift,
  697. buf->fmt->vshift,0);
  698. break;
  699. case V4L2_FIELD_INTERLACED:
  700. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  701. buf->vb.height,1,
  702. tvnorm,&buf->crop);
  703. lines = buf->vb.height >> 1;
  704. ypadding = buf->vb.width;
  705. cpadding = buf->vb.width >> buf->fmt->hshift;
  706. bttv_risc_planar(btv,&buf->top,
  707. dma->sglist,
  708. 0,buf->vb.width,ypadding,lines,
  709. uoffset,voffset,
  710. buf->fmt->hshift,
  711. buf->fmt->vshift,
  712. cpadding);
  713. bttv_risc_planar(btv,&buf->bottom,
  714. dma->sglist,
  715. ypadding,buf->vb.width,ypadding,lines,
  716. uoffset+cpadding,
  717. voffset+cpadding,
  718. buf->fmt->hshift,
  719. buf->fmt->vshift,
  720. cpadding);
  721. break;
  722. case V4L2_FIELD_SEQ_TB:
  723. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  724. buf->vb.height,1,
  725. tvnorm,&buf->crop);
  726. lines = buf->vb.height >> 1;
  727. ypadding = buf->vb.width;
  728. cpadding = buf->vb.width >> buf->fmt->hshift;
  729. bttv_risc_planar(btv,&buf->top,
  730. dma->sglist,
  731. 0,buf->vb.width,0,lines,
  732. uoffset >> 1,
  733. voffset >> 1,
  734. buf->fmt->hshift,
  735. buf->fmt->vshift,
  736. 0);
  737. bttv_risc_planar(btv,&buf->bottom,
  738. dma->sglist,
  739. lines * ypadding,buf->vb.width,0,lines,
  740. lines * ypadding + (uoffset >> 1),
  741. lines * ypadding + (voffset >> 1),
  742. buf->fmt->hshift,
  743. buf->fmt->vshift,
  744. 0);
  745. break;
  746. default:
  747. BUG();
  748. }
  749. }
  750. /* raw data */
  751. if (buf->fmt->flags & FORMAT_FLAGS_RAW) {
  752. /* build risc code */
  753. buf->vb.field = V4L2_FIELD_SEQ_TB;
  754. bttv_calc_geo(btv,&buf->geo,tvnorm->swidth,tvnorm->sheight,
  755. 1,tvnorm,&buf->crop);
  756. bttv_risc_packed(btv, &buf->top, dma->sglist,
  757. /* offset */ 0, RAW_BPL, /* padding */ 0,
  758. /* skip_lines */ 0, RAW_LINES);
  759. bttv_risc_packed(btv, &buf->bottom, dma->sglist,
  760. buf->vb.size/2 , RAW_BPL, 0, 0, RAW_LINES);
  761. }
  762. /* copy format info */
  763. buf->btformat = buf->fmt->btformat;
  764. buf->btswap = buf->fmt->btswap;
  765. return 0;
  766. }
  767. /* ---------------------------------------------------------- */
  768. /* calculate geometry, build risc code */
  769. int
  770. bttv_overlay_risc(struct bttv *btv,
  771. struct bttv_overlay *ov,
  772. const struct bttv_format *fmt,
  773. struct bttv_buffer *buf)
  774. {
  775. /* check interleave, bottom+top fields */
  776. dprintk("%d: overlay fields: %s format: %s size: %dx%d\n",
  777. btv->c.nr, v4l2_field_names[buf->vb.field],
  778. fmt->name, ov->w.width, ov->w.height);
  779. /* calculate geometry */
  780. bttv_calc_geo(btv,&buf->geo,ov->w.width,ov->w.height,
  781. V4L2_FIELD_HAS_BOTH(ov->field),
  782. &bttv_tvnorms[ov->tvnorm],&buf->crop);
  783. /* build risc code */
  784. switch (ov->field) {
  785. case V4L2_FIELD_TOP:
  786. bttv_risc_overlay(btv, &buf->top, fmt, ov, 0, 0);
  787. break;
  788. case V4L2_FIELD_BOTTOM:
  789. bttv_risc_overlay(btv, &buf->bottom, fmt, ov, 0, 0);
  790. break;
  791. case V4L2_FIELD_INTERLACED:
  792. bttv_risc_overlay(btv, &buf->top, fmt, ov, 0, 1);
  793. bttv_risc_overlay(btv, &buf->bottom, fmt, ov, 1, 0);
  794. break;
  795. default:
  796. BUG();
  797. }
  798. /* copy format info */
  799. buf->btformat = fmt->btformat;
  800. buf->btswap = fmt->btswap;
  801. buf->vb.field = ov->field;
  802. return 0;
  803. }