bttv-risc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. while (uoffset && uoffset >= sg_dma_len(usg)) {
  170. uoffset -= sg_dma_len(usg);
  171. usg = sg_next(usg);
  172. }
  173. while (voffset && voffset >= sg_dma_len(vsg)) {
  174. voffset -= sg_dma_len(vsg);
  175. vsg = sg_next(vsg);
  176. }
  177. /* calculate max number of bytes we can write */
  178. ylen = todo;
  179. if (yoffset + ylen > sg_dma_len(ysg))
  180. ylen = sg_dma_len(ysg) - yoffset;
  181. if (chroma) {
  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. if (NULL == (skips = kmalloc(sizeof(*skips) * ov->nclips,GFP_KERNEL)))
  231. return -ENOMEM;
  232. /* estimate risc mem: worst case is (1.5*clip+1) * lines instructions
  233. + sync + jump (all 2 dwords) */
  234. dwords = (3 * ov->nclips + 2) *
  235. ((skip_even || skip_odd) ? (ov->w.height+1)>>1 : ov->w.height);
  236. dwords += 4;
  237. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,dwords*4)) < 0) {
  238. kfree(skips);
  239. return rc;
  240. }
  241. /* sync instruction */
  242. rp = risc->cpu;
  243. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
  244. *(rp++) = cpu_to_le32(0);
  245. addr = (unsigned long)btv->fbuf.base;
  246. addr += btv->fbuf.fmt.bytesperline * ov->w.top;
  247. addr += (fmt->depth >> 3) * ov->w.left;
  248. /* scan lines */
  249. for (maxy = -1, line = 0; line < ov->w.height;
  250. line++, addr += btv->fbuf.fmt.bytesperline) {
  251. if ((btv->opt_vcr_hack) &&
  252. (line >= (ov->w.height - VCR_HACK_LINES)))
  253. continue;
  254. if ((line%2) == 0 && skip_even)
  255. continue;
  256. if ((line%2) == 1 && skip_odd)
  257. continue;
  258. /* calculate clipping */
  259. if (line > maxy)
  260. btcx_calc_skips(line, ov->w.width, &maxy,
  261. skips, &nskips, ov->clips, ov->nclips);
  262. /* write out risc code */
  263. for (start = 0, skip = 0; start < ov->w.width; start = end) {
  264. if (skip >= nskips) {
  265. ri = BT848_RISC_WRITE;
  266. end = ov->w.width;
  267. } else if (start < skips[skip].start) {
  268. ri = BT848_RISC_WRITE;
  269. end = skips[skip].start;
  270. } else {
  271. ri = BT848_RISC_SKIP;
  272. end = skips[skip].end;
  273. skip++;
  274. }
  275. if (BT848_RISC_WRITE == ri)
  276. ra = addr + (fmt->depth>>3)*start;
  277. else
  278. ra = 0;
  279. if (0 == start)
  280. ri |= BT848_RISC_SOL;
  281. if (ov->w.width == end)
  282. ri |= BT848_RISC_EOL;
  283. ri |= (fmt->depth>>3) * (end-start);
  284. *(rp++)=cpu_to_le32(ri);
  285. if (0 != ra)
  286. *(rp++)=cpu_to_le32(ra);
  287. }
  288. }
  289. /* save pointer to jmp instruction address */
  290. risc->jmp = rp;
  291. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  292. kfree(skips);
  293. return 0;
  294. }
  295. /* ---------------------------------------------------------- */
  296. static void
  297. bttv_calc_geo_old(struct bttv *btv, struct bttv_geometry *geo,
  298. int width, int height, int interleaved,
  299. const struct bttv_tvnorm *tvnorm)
  300. {
  301. u32 xsf, sr;
  302. int vdelay;
  303. int swidth = tvnorm->swidth;
  304. int totalwidth = tvnorm->totalwidth;
  305. int scaledtwidth = tvnorm->scaledtwidth;
  306. if (btv->input == btv->dig) {
  307. swidth = 720;
  308. totalwidth = 858;
  309. scaledtwidth = 858;
  310. }
  311. vdelay = tvnorm->vdelay;
  312. xsf = (width*scaledtwidth)/swidth;
  313. geo->hscale = ((totalwidth*4096UL)/xsf-4096);
  314. geo->hdelay = tvnorm->hdelayx1;
  315. geo->hdelay = (geo->hdelay*width)/swidth;
  316. geo->hdelay &= 0x3fe;
  317. sr = ((tvnorm->sheight >> (interleaved?0:1))*512)/height - 512;
  318. geo->vscale = (0x10000UL-sr) & 0x1fff;
  319. geo->crop = ((width>>8)&0x03) | ((geo->hdelay>>6)&0x0c) |
  320. ((tvnorm->sheight>>4)&0x30) | ((vdelay>>2)&0xc0);
  321. geo->vscale |= interleaved ? (BT848_VSCALE_INT<<8) : 0;
  322. geo->vdelay = vdelay;
  323. geo->width = width;
  324. geo->sheight = tvnorm->sheight;
  325. geo->vtotal = tvnorm->vtotal;
  326. if (btv->opt_combfilter) {
  327. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  328. geo->comb = (width < 769) ? 1 : 0;
  329. } else {
  330. geo->vtc = 0;
  331. geo->comb = 0;
  332. }
  333. }
  334. static void
  335. bttv_calc_geo (struct bttv * btv,
  336. struct bttv_geometry * geo,
  337. unsigned int width,
  338. unsigned int height,
  339. int both_fields,
  340. const struct bttv_tvnorm * tvnorm,
  341. const struct v4l2_rect * crop)
  342. {
  343. unsigned int c_width;
  344. unsigned int c_height;
  345. u32 sr;
  346. if ((crop->left == tvnorm->cropcap.defrect.left
  347. && crop->top == tvnorm->cropcap.defrect.top
  348. && crop->width == tvnorm->cropcap.defrect.width
  349. && crop->height == tvnorm->cropcap.defrect.height
  350. && width <= tvnorm->swidth /* see PAL-Nc et al */)
  351. || btv->input == btv->dig) {
  352. bttv_calc_geo_old(btv, geo, width, height,
  353. both_fields, tvnorm);
  354. return;
  355. }
  356. /* For bug compatibility the image size checks permit scale
  357. factors > 16. See bttv_crop_calc_limits(). */
  358. c_width = min((unsigned int) crop->width, width * 16);
  359. c_height = min((unsigned int) crop->height, height * 16);
  360. geo->width = width;
  361. geo->hscale = (c_width * 4096U + (width >> 1)) / width - 4096;
  362. /* Even to store Cb first, odd for Cr. */
  363. geo->hdelay = ((crop->left * width + c_width) / c_width) & ~1;
  364. geo->sheight = c_height;
  365. geo->vdelay = crop->top - tvnorm->cropcap.bounds.top + MIN_VDELAY;
  366. sr = c_height >> !both_fields;
  367. sr = (sr * 512U + (height >> 1)) / height - 512;
  368. geo->vscale = (0x10000UL - sr) & 0x1fff;
  369. geo->vscale |= both_fields ? (BT848_VSCALE_INT << 8) : 0;
  370. geo->vtotal = tvnorm->vtotal;
  371. geo->crop = (((geo->width >> 8) & 0x03) |
  372. ((geo->hdelay >> 6) & 0x0c) |
  373. ((geo->sheight >> 4) & 0x30) |
  374. ((geo->vdelay >> 2) & 0xc0));
  375. if (btv->opt_combfilter) {
  376. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  377. geo->comb = (width < 769) ? 1 : 0;
  378. } else {
  379. geo->vtc = 0;
  380. geo->comb = 0;
  381. }
  382. }
  383. static void
  384. bttv_apply_geo(struct bttv *btv, struct bttv_geometry *geo, int odd)
  385. {
  386. int off = odd ? 0x80 : 0x00;
  387. if (geo->comb)
  388. btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  389. else
  390. btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  391. btwrite(geo->vtc, BT848_E_VTC+off);
  392. btwrite(geo->hscale >> 8, BT848_E_HSCALE_HI+off);
  393. btwrite(geo->hscale & 0xff, BT848_E_HSCALE_LO+off);
  394. btaor((geo->vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
  395. btwrite(geo->vscale & 0xff, BT848_E_VSCALE_LO+off);
  396. btwrite(geo->width & 0xff, BT848_E_HACTIVE_LO+off);
  397. btwrite(geo->hdelay & 0xff, BT848_E_HDELAY_LO+off);
  398. btwrite(geo->sheight & 0xff, BT848_E_VACTIVE_LO+off);
  399. btwrite(geo->vdelay & 0xff, BT848_E_VDELAY_LO+off);
  400. btwrite(geo->crop, BT848_E_CROP+off);
  401. btwrite(geo->vtotal>>8, BT848_VTOTAL_HI);
  402. btwrite(geo->vtotal & 0xff, BT848_VTOTAL_LO);
  403. }
  404. /* ---------------------------------------------------------- */
  405. /* risc group / risc main loop / dma management */
  406. void
  407. bttv_set_dma(struct bttv *btv, int override)
  408. {
  409. unsigned long cmd;
  410. int capctl;
  411. btv->cap_ctl = 0;
  412. if (NULL != btv->curr.top) btv->cap_ctl |= 0x02;
  413. if (NULL != btv->curr.bottom) btv->cap_ctl |= 0x01;
  414. if (NULL != btv->cvbi) btv->cap_ctl |= 0x0c;
  415. capctl = 0;
  416. capctl |= (btv->cap_ctl & 0x03) ? 0x03 : 0x00; /* capture */
  417. capctl |= (btv->cap_ctl & 0x0c) ? 0x0c : 0x00; /* vbi data */
  418. capctl |= override;
  419. d2printk("%d: capctl=%x lirq=%d top=%08llx/%08llx even=%08llx/%08llx\n",
  420. btv->c.nr,capctl,btv->loop_irq,
  421. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  422. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  423. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0,
  424. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  425. cmd = BT848_RISC_JUMP;
  426. if (btv->loop_irq) {
  427. cmd |= BT848_RISC_IRQ;
  428. cmd |= (btv->loop_irq & 0x0f) << 16;
  429. cmd |= (~btv->loop_irq & 0x0f) << 20;
  430. }
  431. if (btv->curr.frame_irq || btv->loop_irq || btv->cvbi) {
  432. mod_timer(&btv->timeout, jiffies+BTTV_TIMEOUT);
  433. } else {
  434. del_timer(&btv->timeout);
  435. }
  436. btv->main.cpu[RISC_SLOT_LOOP] = cpu_to_le32(cmd);
  437. btaor(capctl, ~0x0f, BT848_CAP_CTL);
  438. if (capctl) {
  439. if (btv->dma_on)
  440. return;
  441. btwrite(btv->main.dma, BT848_RISC_STRT_ADD);
  442. btor(3, BT848_GPIO_DMA_CTL);
  443. btv->dma_on = 1;
  444. } else {
  445. if (!btv->dma_on)
  446. return;
  447. btand(~3, BT848_GPIO_DMA_CTL);
  448. btv->dma_on = 0;
  449. }
  450. return;
  451. }
  452. int
  453. bttv_risc_init_main(struct bttv *btv)
  454. {
  455. int rc;
  456. if ((rc = btcx_riscmem_alloc(btv->c.pci,&btv->main,PAGE_SIZE)) < 0)
  457. return rc;
  458. dprintk("%d: risc main @ %08llx\n",
  459. btv->c.nr, (unsigned long long)btv->main.dma);
  460. btv->main.cpu[0] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  461. BT848_FIFO_STATUS_VRE);
  462. btv->main.cpu[1] = cpu_to_le32(0);
  463. btv->main.cpu[2] = cpu_to_le32(BT848_RISC_JUMP);
  464. btv->main.cpu[3] = cpu_to_le32(btv->main.dma + (4<<2));
  465. /* top field */
  466. btv->main.cpu[4] = cpu_to_le32(BT848_RISC_JUMP);
  467. btv->main.cpu[5] = cpu_to_le32(btv->main.dma + (6<<2));
  468. btv->main.cpu[6] = cpu_to_le32(BT848_RISC_JUMP);
  469. btv->main.cpu[7] = cpu_to_le32(btv->main.dma + (8<<2));
  470. btv->main.cpu[8] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  471. BT848_FIFO_STATUS_VRO);
  472. btv->main.cpu[9] = cpu_to_le32(0);
  473. /* bottom field */
  474. btv->main.cpu[10] = cpu_to_le32(BT848_RISC_JUMP);
  475. btv->main.cpu[11] = cpu_to_le32(btv->main.dma + (12<<2));
  476. btv->main.cpu[12] = cpu_to_le32(BT848_RISC_JUMP);
  477. btv->main.cpu[13] = cpu_to_le32(btv->main.dma + (14<<2));
  478. /* jump back to top field */
  479. btv->main.cpu[14] = cpu_to_le32(BT848_RISC_JUMP);
  480. btv->main.cpu[15] = cpu_to_le32(btv->main.dma + (0<<2));
  481. return 0;
  482. }
  483. int
  484. bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
  485. int irqflags)
  486. {
  487. unsigned long cmd;
  488. unsigned long next = btv->main.dma + ((slot+2) << 2);
  489. if (NULL == risc) {
  490. d2printk("%d: risc=%p slot[%d]=NULL\n", btv->c.nr, risc, slot);
  491. btv->main.cpu[slot+1] = cpu_to_le32(next);
  492. } else {
  493. d2printk("%d: risc=%p slot[%d]=%08llx irq=%d\n",
  494. btv->c.nr, risc, slot,
  495. (unsigned long long)risc->dma, irqflags);
  496. cmd = BT848_RISC_JUMP;
  497. if (irqflags) {
  498. cmd |= BT848_RISC_IRQ;
  499. cmd |= (irqflags & 0x0f) << 16;
  500. cmd |= (~irqflags & 0x0f) << 20;
  501. }
  502. risc->jmp[0] = cpu_to_le32(cmd);
  503. risc->jmp[1] = cpu_to_le32(next);
  504. btv->main.cpu[slot+1] = cpu_to_le32(risc->dma);
  505. }
  506. return 0;
  507. }
  508. void
  509. bttv_dma_free(struct videobuf_queue *q,struct bttv *btv, struct bttv_buffer *buf)
  510. {
  511. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  512. BUG_ON(in_interrupt());
  513. videobuf_waiton(q, &buf->vb, 0, 0);
  514. videobuf_dma_unmap(q->dev, dma);
  515. videobuf_dma_free(dma);
  516. btcx_riscmem_free(btv->c.pci,&buf->bottom);
  517. btcx_riscmem_free(btv->c.pci,&buf->top);
  518. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  519. }
  520. int
  521. bttv_buffer_activate_vbi(struct bttv *btv,
  522. struct bttv_buffer *vbi)
  523. {
  524. struct btcx_riscmem *top;
  525. struct btcx_riscmem *bottom;
  526. int top_irq_flags;
  527. int bottom_irq_flags;
  528. top = NULL;
  529. bottom = NULL;
  530. top_irq_flags = 0;
  531. bottom_irq_flags = 0;
  532. if (vbi) {
  533. unsigned int crop, vdelay;
  534. vbi->vb.state = VIDEOBUF_ACTIVE;
  535. list_del(&vbi->vb.queue);
  536. /* VDELAY is start of video, end of VBI capturing. */
  537. crop = btread(BT848_E_CROP);
  538. vdelay = btread(BT848_E_VDELAY_LO) + ((crop & 0xc0) << 2);
  539. if (vbi->geo.vdelay > vdelay) {
  540. vdelay = vbi->geo.vdelay & 0xfe;
  541. crop = (crop & 0x3f) | ((vbi->geo.vdelay >> 2) & 0xc0);
  542. btwrite(vdelay, BT848_E_VDELAY_LO);
  543. btwrite(crop, BT848_E_CROP);
  544. btwrite(vdelay, BT848_O_VDELAY_LO);
  545. btwrite(crop, BT848_O_CROP);
  546. }
  547. if (vbi->vbi_count[0] > 0) {
  548. top = &vbi->top;
  549. top_irq_flags = 4;
  550. }
  551. if (vbi->vbi_count[1] > 0) {
  552. top_irq_flags = 0;
  553. bottom = &vbi->bottom;
  554. bottom_irq_flags = 4;
  555. }
  556. }
  557. bttv_risc_hook(btv, RISC_SLOT_O_VBI, top, top_irq_flags);
  558. bttv_risc_hook(btv, RISC_SLOT_E_VBI, bottom, bottom_irq_flags);
  559. return 0;
  560. }
  561. int
  562. bttv_buffer_activate_video(struct bttv *btv,
  563. struct bttv_buffer_set *set)
  564. {
  565. /* video capture */
  566. if (NULL != set->top && NULL != set->bottom) {
  567. if (set->top == set->bottom) {
  568. set->top->vb.state = VIDEOBUF_ACTIVE;
  569. if (set->top->vb.queue.next)
  570. list_del(&set->top->vb.queue);
  571. } else {
  572. set->top->vb.state = VIDEOBUF_ACTIVE;
  573. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  574. if (set->top->vb.queue.next)
  575. list_del(&set->top->vb.queue);
  576. if (set->bottom->vb.queue.next)
  577. list_del(&set->bottom->vb.queue);
  578. }
  579. bttv_apply_geo(btv, &set->top->geo, 1);
  580. bttv_apply_geo(btv, &set->bottom->geo,0);
  581. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  582. set->top_irq);
  583. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  584. set->frame_irq);
  585. btaor((set->top->btformat & 0xf0) | (set->bottom->btformat & 0x0f),
  586. ~0xff, BT848_COLOR_FMT);
  587. btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
  588. ~0x0f, BT848_COLOR_CTL);
  589. } else if (NULL != set->top) {
  590. set->top->vb.state = VIDEOBUF_ACTIVE;
  591. if (set->top->vb.queue.next)
  592. list_del(&set->top->vb.queue);
  593. bttv_apply_geo(btv, &set->top->geo,1);
  594. bttv_apply_geo(btv, &set->top->geo,0);
  595. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  596. set->frame_irq);
  597. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  598. btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  599. btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  600. } else if (NULL != set->bottom) {
  601. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  602. if (set->bottom->vb.queue.next)
  603. list_del(&set->bottom->vb.queue);
  604. bttv_apply_geo(btv, &set->bottom->geo,1);
  605. bttv_apply_geo(btv, &set->bottom->geo,0);
  606. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  607. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  608. set->frame_irq);
  609. btaor(set->bottom->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  610. btaor(set->bottom->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  611. } else {
  612. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  613. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  614. }
  615. return 0;
  616. }
  617. /* ---------------------------------------------------------- */
  618. /* calculate geometry, build risc code */
  619. int
  620. bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf)
  621. {
  622. const struct bttv_tvnorm *tvnorm = bttv_tvnorms + buf->tvnorm;
  623. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  624. dprintk("%d: buffer field: %s format: %s size: %dx%d\n",
  625. btv->c.nr, v4l2_field_names[buf->vb.field],
  626. buf->fmt->name, buf->vb.width, buf->vb.height);
  627. /* packed pixel modes */
  628. if (buf->fmt->flags & FORMAT_FLAGS_PACKED) {
  629. int bpl = (buf->fmt->depth >> 3) * buf->vb.width;
  630. int bpf = bpl * (buf->vb.height >> 1);
  631. bttv_calc_geo(btv,&buf->geo,buf->vb.width,buf->vb.height,
  632. V4L2_FIELD_HAS_BOTH(buf->vb.field),
  633. tvnorm,&buf->crop);
  634. switch (buf->vb.field) {
  635. case V4L2_FIELD_TOP:
  636. bttv_risc_packed(btv,&buf->top,dma->sglist,
  637. /* offset */ 0,bpl,
  638. /* padding */ 0,/* skip_lines */ 0,
  639. buf->vb.height);
  640. break;
  641. case V4L2_FIELD_BOTTOM:
  642. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  643. 0,bpl,0,0,buf->vb.height);
  644. break;
  645. case V4L2_FIELD_INTERLACED:
  646. bttv_risc_packed(btv,&buf->top,dma->sglist,
  647. 0,bpl,bpl,0,buf->vb.height >> 1);
  648. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  649. bpl,bpl,bpl,0,buf->vb.height >> 1);
  650. break;
  651. case V4L2_FIELD_SEQ_TB:
  652. bttv_risc_packed(btv,&buf->top,dma->sglist,
  653. 0,bpl,0,0,buf->vb.height >> 1);
  654. bttv_risc_packed(btv,&buf->bottom,dma->sglist,
  655. bpf,bpl,0,0,buf->vb.height >> 1);
  656. break;
  657. default:
  658. BUG();
  659. }
  660. }
  661. /* planar modes */
  662. if (buf->fmt->flags & FORMAT_FLAGS_PLANAR) {
  663. int uoffset, voffset;
  664. int ypadding, cpadding, lines;
  665. /* calculate chroma offsets */
  666. uoffset = buf->vb.width * buf->vb.height;
  667. voffset = buf->vb.width * buf->vb.height;
  668. if (buf->fmt->flags & FORMAT_FLAGS_CrCb) {
  669. /* Y-Cr-Cb plane order */
  670. uoffset >>= buf->fmt->hshift;
  671. uoffset >>= buf->fmt->vshift;
  672. uoffset += voffset;
  673. } else {
  674. /* Y-Cb-Cr plane order */
  675. voffset >>= buf->fmt->hshift;
  676. voffset >>= buf->fmt->vshift;
  677. voffset += uoffset;
  678. }
  679. switch (buf->vb.field) {
  680. case V4L2_FIELD_TOP:
  681. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  682. buf->vb.height,/* both_fields */ 0,
  683. tvnorm,&buf->crop);
  684. bttv_risc_planar(btv, &buf->top, dma->sglist,
  685. 0,buf->vb.width,0,buf->vb.height,
  686. uoffset,voffset,buf->fmt->hshift,
  687. buf->fmt->vshift,0);
  688. break;
  689. case V4L2_FIELD_BOTTOM:
  690. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  691. buf->vb.height,0,
  692. tvnorm,&buf->crop);
  693. bttv_risc_planar(btv, &buf->bottom, dma->sglist,
  694. 0,buf->vb.width,0,buf->vb.height,
  695. uoffset,voffset,buf->fmt->hshift,
  696. buf->fmt->vshift,0);
  697. break;
  698. case V4L2_FIELD_INTERLACED:
  699. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  700. buf->vb.height,1,
  701. tvnorm,&buf->crop);
  702. lines = buf->vb.height >> 1;
  703. ypadding = buf->vb.width;
  704. cpadding = buf->vb.width >> buf->fmt->hshift;
  705. bttv_risc_planar(btv,&buf->top,
  706. dma->sglist,
  707. 0,buf->vb.width,ypadding,lines,
  708. uoffset,voffset,
  709. buf->fmt->hshift,
  710. buf->fmt->vshift,
  711. cpadding);
  712. bttv_risc_planar(btv,&buf->bottom,
  713. dma->sglist,
  714. ypadding,buf->vb.width,ypadding,lines,
  715. uoffset+cpadding,
  716. voffset+cpadding,
  717. buf->fmt->hshift,
  718. buf->fmt->vshift,
  719. cpadding);
  720. break;
  721. case V4L2_FIELD_SEQ_TB:
  722. bttv_calc_geo(btv,&buf->geo,buf->vb.width,
  723. buf->vb.height,1,
  724. tvnorm,&buf->crop);
  725. lines = buf->vb.height >> 1;
  726. ypadding = buf->vb.width;
  727. cpadding = buf->vb.width >> buf->fmt->hshift;
  728. bttv_risc_planar(btv,&buf->top,
  729. dma->sglist,
  730. 0,buf->vb.width,0,lines,
  731. uoffset >> 1,
  732. voffset >> 1,
  733. buf->fmt->hshift,
  734. buf->fmt->vshift,
  735. 0);
  736. bttv_risc_planar(btv,&buf->bottom,
  737. dma->sglist,
  738. lines * ypadding,buf->vb.width,0,lines,
  739. lines * ypadding + (uoffset >> 1),
  740. lines * ypadding + (voffset >> 1),
  741. buf->fmt->hshift,
  742. buf->fmt->vshift,
  743. 0);
  744. break;
  745. default:
  746. BUG();
  747. }
  748. }
  749. /* raw data */
  750. if (buf->fmt->flags & FORMAT_FLAGS_RAW) {
  751. /* build risc code */
  752. buf->vb.field = V4L2_FIELD_SEQ_TB;
  753. bttv_calc_geo(btv,&buf->geo,tvnorm->swidth,tvnorm->sheight,
  754. 1,tvnorm,&buf->crop);
  755. bttv_risc_packed(btv, &buf->top, dma->sglist,
  756. /* offset */ 0, RAW_BPL, /* padding */ 0,
  757. /* skip_lines */ 0, RAW_LINES);
  758. bttv_risc_packed(btv, &buf->bottom, dma->sglist,
  759. buf->vb.size/2 , RAW_BPL, 0, 0, RAW_LINES);
  760. }
  761. /* copy format info */
  762. buf->btformat = buf->fmt->btformat;
  763. buf->btswap = buf->fmt->btswap;
  764. return 0;
  765. }
  766. /* ---------------------------------------------------------- */
  767. /* calculate geometry, build risc code */
  768. int
  769. bttv_overlay_risc(struct bttv *btv,
  770. struct bttv_overlay *ov,
  771. const struct bttv_format *fmt,
  772. struct bttv_buffer *buf)
  773. {
  774. /* check interleave, bottom+top fields */
  775. dprintk("%d: overlay fields: %s format: %s size: %dx%d\n",
  776. btv->c.nr, v4l2_field_names[buf->vb.field],
  777. fmt->name, ov->w.width, ov->w.height);
  778. /* calculate geometry */
  779. bttv_calc_geo(btv,&buf->geo,ov->w.width,ov->w.height,
  780. V4L2_FIELD_HAS_BOTH(ov->field),
  781. &bttv_tvnorms[ov->tvnorm],&buf->crop);
  782. /* build risc code */
  783. switch (ov->field) {
  784. case V4L2_FIELD_TOP:
  785. bttv_risc_overlay(btv, &buf->top, fmt, ov, 0, 0);
  786. break;
  787. case V4L2_FIELD_BOTTOM:
  788. bttv_risc_overlay(btv, &buf->bottom, fmt, ov, 0, 0);
  789. break;
  790. case V4L2_FIELD_INTERLACED:
  791. bttv_risc_overlay(btv, &buf->top, fmt, ov, 0, 1);
  792. bttv_risc_overlay(btv, &buf->bottom, fmt, ov, 1, 0);
  793. break;
  794. default:
  795. BUG();
  796. }
  797. /* copy format info */
  798. buf->btformat = fmt->btformat;
  799. buf->btswap = fmt->btswap;
  800. buf->vb.field = ov->field;
  801. return 0;
  802. }