bttv-risc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/pci.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <media/v4l2-ioctl.h>
  27. #include "bttvp.h"
  28. #define VCR_HACK_LINES 4
  29. /* ---------------------------------------------------------- */
  30. /* risc code generators */
  31. int
  32. bttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
  33. struct scatterlist *sglist,
  34. unsigned int offset, unsigned int bpl,
  35. unsigned int padding, unsigned int skip_lines,
  36. unsigned int store_lines)
  37. {
  38. u32 instructions,line,todo;
  39. struct scatterlist *sg;
  40. __le32 *rp;
  41. int rc;
  42. /* estimate risc mem: worst case is one write per page border +
  43. one write per scan line + sync + jump (all 2 dwords). padding
  44. can cause next bpl to start close to a page border. First DMA
  45. region may be smaller than PAGE_SIZE */
  46. instructions = skip_lines * 4;
  47. instructions += (1 + ((bpl + padding) * store_lines)
  48. / PAGE_SIZE + store_lines) * 8;
  49. instructions += 2 * 8;
  50. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions)) < 0)
  51. return rc;
  52. /* sync instruction */
  53. rp = risc->cpu;
  54. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
  55. *(rp++) = cpu_to_le32(0);
  56. while (skip_lines-- > 0) {
  57. *(rp++) = cpu_to_le32(BT848_RISC_SKIP | BT848_RISC_SOL |
  58. BT848_RISC_EOL | bpl);
  59. }
  60. /* scan lines */
  61. sg = sglist;
  62. for (line = 0; line < store_lines; line++) {
  63. if ((btv->opt_vcr_hack) &&
  64. (line >= (store_lines - VCR_HACK_LINES)))
  65. continue;
  66. while (offset && offset >= sg_dma_len(sg)) {
  67. offset -= sg_dma_len(sg);
  68. sg++;
  69. }
  70. if (bpl <= sg_dma_len(sg)-offset) {
  71. /* fits into current chunk */
  72. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
  73. BT848_RISC_EOL|bpl);
  74. *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
  75. offset+=bpl;
  76. } else {
  77. /* scanline needs to be splitted */
  78. todo = bpl;
  79. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_SOL|
  80. (sg_dma_len(sg)-offset));
  81. *(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);
  82. todo -= (sg_dma_len(sg)-offset);
  83. offset = 0;
  84. sg++;
  85. while (todo > sg_dma_len(sg)) {
  86. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|
  87. sg_dma_len(sg));
  88. *(rp++)=cpu_to_le32(sg_dma_address(sg));
  89. todo -= sg_dma_len(sg);
  90. sg++;
  91. }
  92. *(rp++)=cpu_to_le32(BT848_RISC_WRITE|BT848_RISC_EOL|
  93. todo);
  94. *(rp++)=cpu_to_le32(sg_dma_address(sg));
  95. offset += todo;
  96. }
  97. offset += padding;
  98. }
  99. /* save pointer to jmp instruction address */
  100. risc->jmp = rp;
  101. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  102. return 0;
  103. }
  104. static int
  105. bttv_risc_planar(struct bttv *btv, struct btcx_riscmem *risc,
  106. struct scatterlist *sglist,
  107. unsigned int yoffset, unsigned int ybpl,
  108. unsigned int ypadding, unsigned int ylines,
  109. unsigned int uoffset, unsigned int voffset,
  110. unsigned int hshift, unsigned int vshift,
  111. unsigned int cpadding)
  112. {
  113. unsigned int instructions,line,todo,ylen,chroma;
  114. __le32 *rp;
  115. u32 ri;
  116. struct scatterlist *ysg;
  117. struct scatterlist *usg;
  118. struct scatterlist *vsg;
  119. int topfield = (0 == yoffset);
  120. int rc;
  121. /* estimate risc mem: worst case is one write per page border +
  122. one write per scan line (5 dwords)
  123. plus sync + jump (2 dwords) */
  124. instructions = ((3 + (ybpl + ypadding) * ylines * 2)
  125. / PAGE_SIZE) + ylines;
  126. instructions += 2;
  127. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,instructions*4*5)) < 0)
  128. return rc;
  129. /* sync instruction */
  130. rp = risc->cpu;
  131. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM3);
  132. *(rp++) = cpu_to_le32(0);
  133. /* scan lines */
  134. ysg = sglist;
  135. usg = sglist;
  136. vsg = sglist;
  137. for (line = 0; line < ylines; line++) {
  138. if ((btv->opt_vcr_hack) &&
  139. (line >= (ylines - VCR_HACK_LINES)))
  140. continue;
  141. switch (vshift) {
  142. case 0:
  143. chroma = 1;
  144. break;
  145. case 1:
  146. if (topfield)
  147. chroma = ((line & 1) == 0);
  148. else
  149. chroma = ((line & 1) == 1);
  150. break;
  151. case 2:
  152. if (topfield)
  153. chroma = ((line & 3) == 0);
  154. else
  155. chroma = ((line & 3) == 2);
  156. break;
  157. default:
  158. chroma = 0;
  159. break;
  160. }
  161. for (todo = ybpl; todo > 0; todo -= ylen) {
  162. /* go to next sg entry if needed */
  163. while (yoffset && yoffset >= sg_dma_len(ysg)) {
  164. yoffset -= sg_dma_len(ysg);
  165. ysg++;
  166. }
  167. while (uoffset && uoffset >= sg_dma_len(usg)) {
  168. uoffset -= sg_dma_len(usg);
  169. usg++;
  170. }
  171. while (voffset && voffset >= sg_dma_len(vsg)) {
  172. voffset -= sg_dma_len(vsg);
  173. vsg++;
  174. }
  175. /* calculate max number of bytes we can write */
  176. ylen = todo;
  177. if (yoffset + ylen > sg_dma_len(ysg))
  178. ylen = sg_dma_len(ysg) - yoffset;
  179. if (chroma) {
  180. if (uoffset + (ylen>>hshift) > sg_dma_len(usg))
  181. ylen = (sg_dma_len(usg) - uoffset) << hshift;
  182. if (voffset + (ylen>>hshift) > sg_dma_len(vsg))
  183. ylen = (sg_dma_len(vsg) - voffset) << hshift;
  184. ri = BT848_RISC_WRITE123;
  185. } else {
  186. ri = BT848_RISC_WRITE1S23;
  187. }
  188. if (ybpl == todo)
  189. ri |= BT848_RISC_SOL;
  190. if (ylen == todo)
  191. ri |= BT848_RISC_EOL;
  192. /* write risc instruction */
  193. *(rp++)=cpu_to_le32(ri | ylen);
  194. *(rp++)=cpu_to_le32(((ylen >> hshift) << 16) |
  195. (ylen >> hshift));
  196. *(rp++)=cpu_to_le32(sg_dma_address(ysg)+yoffset);
  197. yoffset += ylen;
  198. if (chroma) {
  199. *(rp++)=cpu_to_le32(sg_dma_address(usg)+uoffset);
  200. uoffset += ylen >> hshift;
  201. *(rp++)=cpu_to_le32(sg_dma_address(vsg)+voffset);
  202. voffset += ylen >> hshift;
  203. }
  204. }
  205. yoffset += ypadding;
  206. if (chroma) {
  207. uoffset += cpadding;
  208. voffset += cpadding;
  209. }
  210. }
  211. /* save pointer to jmp instruction address */
  212. risc->jmp = rp;
  213. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  214. return 0;
  215. }
  216. static int
  217. bttv_risc_overlay(struct bttv *btv, struct btcx_riscmem *risc,
  218. const struct bttv_format *fmt, struct bttv_overlay *ov,
  219. int skip_even, int skip_odd)
  220. {
  221. int dwords, rc, line, maxy, start, end;
  222. unsigned skip, nskips;
  223. struct btcx_skiplist *skips;
  224. __le32 *rp;
  225. u32 ri,ra;
  226. u32 addr;
  227. /* skip list for window clipping */
  228. if (NULL == (skips = kmalloc(sizeof(*skips) * ov->nclips,GFP_KERNEL)))
  229. return -ENOMEM;
  230. /* estimate risc mem: worst case is (1.5*clip+1) * lines instructions
  231. + sync + jump (all 2 dwords) */
  232. dwords = (3 * ov->nclips + 2) *
  233. ((skip_even || skip_odd) ? (ov->w.height+1)>>1 : ov->w.height);
  234. dwords += 4;
  235. if ((rc = btcx_riscmem_alloc(btv->c.pci,risc,dwords*4)) < 0) {
  236. kfree(skips);
  237. return rc;
  238. }
  239. /* sync instruction */
  240. rp = risc->cpu;
  241. *(rp++) = cpu_to_le32(BT848_RISC_SYNC|BT848_FIFO_STATUS_FM1);
  242. *(rp++) = cpu_to_le32(0);
  243. addr = (unsigned long)btv->fbuf.base;
  244. addr += btv->fbuf.fmt.bytesperline * ov->w.top;
  245. addr += (fmt->depth >> 3) * ov->w.left;
  246. /* scan lines */
  247. for (maxy = -1, line = 0; line < ov->w.height;
  248. line++, addr += btv->fbuf.fmt.bytesperline) {
  249. if ((btv->opt_vcr_hack) &&
  250. (line >= (ov->w.height - VCR_HACK_LINES)))
  251. continue;
  252. if ((line%2) == 0 && skip_even)
  253. continue;
  254. if ((line%2) == 1 && skip_odd)
  255. continue;
  256. /* calculate clipping */
  257. if (line > maxy)
  258. btcx_calc_skips(line, ov->w.width, &maxy,
  259. skips, &nskips, ov->clips, ov->nclips);
  260. /* write out risc code */
  261. for (start = 0, skip = 0; start < ov->w.width; start = end) {
  262. if (skip >= nskips) {
  263. ri = BT848_RISC_WRITE;
  264. end = ov->w.width;
  265. } else if (start < skips[skip].start) {
  266. ri = BT848_RISC_WRITE;
  267. end = skips[skip].start;
  268. } else {
  269. ri = BT848_RISC_SKIP;
  270. end = skips[skip].end;
  271. skip++;
  272. }
  273. if (BT848_RISC_WRITE == ri)
  274. ra = addr + (fmt->depth>>3)*start;
  275. else
  276. ra = 0;
  277. if (0 == start)
  278. ri |= BT848_RISC_SOL;
  279. if (ov->w.width == end)
  280. ri |= BT848_RISC_EOL;
  281. ri |= (fmt->depth>>3) * (end-start);
  282. *(rp++)=cpu_to_le32(ri);
  283. if (0 != ra)
  284. *(rp++)=cpu_to_le32(ra);
  285. }
  286. }
  287. /* save pointer to jmp instruction address */
  288. risc->jmp = rp;
  289. BUG_ON((risc->jmp - risc->cpu + 2) * sizeof(*risc->cpu) > risc->size);
  290. kfree(skips);
  291. return 0;
  292. }
  293. /* ---------------------------------------------------------- */
  294. static void
  295. bttv_calc_geo_old(struct bttv *btv, struct bttv_geometry *geo,
  296. int width, int height, int interleaved,
  297. const struct bttv_tvnorm *tvnorm)
  298. {
  299. u32 xsf, sr;
  300. int vdelay;
  301. int swidth = tvnorm->swidth;
  302. int totalwidth = tvnorm->totalwidth;
  303. int scaledtwidth = tvnorm->scaledtwidth;
  304. if (btv->input == btv->dig) {
  305. swidth = 720;
  306. totalwidth = 858;
  307. scaledtwidth = 858;
  308. }
  309. vdelay = tvnorm->vdelay;
  310. xsf = (width*scaledtwidth)/swidth;
  311. geo->hscale = ((totalwidth*4096UL)/xsf-4096);
  312. geo->hdelay = tvnorm->hdelayx1;
  313. geo->hdelay = (geo->hdelay*width)/swidth;
  314. geo->hdelay &= 0x3fe;
  315. sr = ((tvnorm->sheight >> (interleaved?0:1))*512)/height - 512;
  316. geo->vscale = (0x10000UL-sr) & 0x1fff;
  317. geo->crop = ((width>>8)&0x03) | ((geo->hdelay>>6)&0x0c) |
  318. ((tvnorm->sheight>>4)&0x30) | ((vdelay>>2)&0xc0);
  319. geo->vscale |= interleaved ? (BT848_VSCALE_INT<<8) : 0;
  320. geo->vdelay = vdelay;
  321. geo->width = width;
  322. geo->sheight = tvnorm->sheight;
  323. geo->vtotal = tvnorm->vtotal;
  324. if (btv->opt_combfilter) {
  325. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  326. geo->comb = (width < 769) ? 1 : 0;
  327. } else {
  328. geo->vtc = 0;
  329. geo->comb = 0;
  330. }
  331. }
  332. static void
  333. bttv_calc_geo (struct bttv * btv,
  334. struct bttv_geometry * geo,
  335. unsigned int width,
  336. unsigned int height,
  337. int both_fields,
  338. const struct bttv_tvnorm * tvnorm,
  339. const struct v4l2_rect * crop)
  340. {
  341. unsigned int c_width;
  342. unsigned int c_height;
  343. u32 sr;
  344. if ((crop->left == tvnorm->cropcap.defrect.left
  345. && crop->top == tvnorm->cropcap.defrect.top
  346. && crop->width == tvnorm->cropcap.defrect.width
  347. && crop->height == tvnorm->cropcap.defrect.height
  348. && width <= tvnorm->swidth /* see PAL-Nc et al */)
  349. || btv->input == btv->dig) {
  350. bttv_calc_geo_old(btv, geo, width, height,
  351. both_fields, tvnorm);
  352. return;
  353. }
  354. /* For bug compatibility the image size checks permit scale
  355. factors > 16. See bttv_crop_calc_limits(). */
  356. c_width = min((unsigned int) crop->width, width * 16);
  357. c_height = min((unsigned int) crop->height, height * 16);
  358. geo->width = width;
  359. geo->hscale = (c_width * 4096U + (width >> 1)) / width - 4096;
  360. /* Even to store Cb first, odd for Cr. */
  361. geo->hdelay = ((crop->left * width + c_width) / c_width) & ~1;
  362. geo->sheight = c_height;
  363. geo->vdelay = crop->top - tvnorm->cropcap.bounds.top + MIN_VDELAY;
  364. sr = c_height >> !both_fields;
  365. sr = (sr * 512U + (height >> 1)) / height - 512;
  366. geo->vscale = (0x10000UL - sr) & 0x1fff;
  367. geo->vscale |= both_fields ? (BT848_VSCALE_INT << 8) : 0;
  368. geo->vtotal = tvnorm->vtotal;
  369. geo->crop = (((geo->width >> 8) & 0x03) |
  370. ((geo->hdelay >> 6) & 0x0c) |
  371. ((geo->sheight >> 4) & 0x30) |
  372. ((geo->vdelay >> 2) & 0xc0));
  373. if (btv->opt_combfilter) {
  374. geo->vtc = (width < 193) ? 2 : ((width < 385) ? 1 : 0);
  375. geo->comb = (width < 769) ? 1 : 0;
  376. } else {
  377. geo->vtc = 0;
  378. geo->comb = 0;
  379. }
  380. }
  381. static void
  382. bttv_apply_geo(struct bttv *btv, struct bttv_geometry *geo, int odd)
  383. {
  384. int off = odd ? 0x80 : 0x00;
  385. if (geo->comb)
  386. btor(BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  387. else
  388. btand(~BT848_VSCALE_COMB, BT848_E_VSCALE_HI+off);
  389. btwrite(geo->vtc, BT848_E_VTC+off);
  390. btwrite(geo->hscale >> 8, BT848_E_HSCALE_HI+off);
  391. btwrite(geo->hscale & 0xff, BT848_E_HSCALE_LO+off);
  392. btaor((geo->vscale>>8), 0xe0, BT848_E_VSCALE_HI+off);
  393. btwrite(geo->vscale & 0xff, BT848_E_VSCALE_LO+off);
  394. btwrite(geo->width & 0xff, BT848_E_HACTIVE_LO+off);
  395. btwrite(geo->hdelay & 0xff, BT848_E_HDELAY_LO+off);
  396. btwrite(geo->sheight & 0xff, BT848_E_VACTIVE_LO+off);
  397. btwrite(geo->vdelay & 0xff, BT848_E_VDELAY_LO+off);
  398. btwrite(geo->crop, BT848_E_CROP+off);
  399. btwrite(geo->vtotal>>8, BT848_VTOTAL_HI);
  400. btwrite(geo->vtotal & 0xff, BT848_VTOTAL_LO);
  401. }
  402. /* ---------------------------------------------------------- */
  403. /* risc group / risc main loop / dma management */
  404. void
  405. bttv_set_dma(struct bttv *btv, int override)
  406. {
  407. unsigned long cmd;
  408. int capctl;
  409. btv->cap_ctl = 0;
  410. if (NULL != btv->curr.top) btv->cap_ctl |= 0x02;
  411. if (NULL != btv->curr.bottom) btv->cap_ctl |= 0x01;
  412. if (NULL != btv->cvbi) btv->cap_ctl |= 0x0c;
  413. capctl = 0;
  414. capctl |= (btv->cap_ctl & 0x03) ? 0x03 : 0x00; /* capture */
  415. capctl |= (btv->cap_ctl & 0x0c) ? 0x0c : 0x00; /* vbi data */
  416. capctl |= override;
  417. d2printk(KERN_DEBUG
  418. "bttv%d: capctl=%x lirq=%d top=%08Lx/%08Lx even=%08Lx/%08Lx\n",
  419. btv->c.nr,capctl,btv->loop_irq,
  420. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  421. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  422. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0,
  423. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  424. cmd = BT848_RISC_JUMP;
  425. if (btv->loop_irq) {
  426. cmd |= BT848_RISC_IRQ;
  427. cmd |= (btv->loop_irq & 0x0f) << 16;
  428. cmd |= (~btv->loop_irq & 0x0f) << 20;
  429. }
  430. if (btv->curr.frame_irq || btv->loop_irq || btv->cvbi) {
  431. mod_timer(&btv->timeout, jiffies+BTTV_TIMEOUT);
  432. } else {
  433. del_timer(&btv->timeout);
  434. }
  435. btv->main.cpu[RISC_SLOT_LOOP] = cpu_to_le32(cmd);
  436. btaor(capctl, ~0x0f, BT848_CAP_CTL);
  437. if (capctl) {
  438. if (btv->dma_on)
  439. return;
  440. btwrite(btv->main.dma, BT848_RISC_STRT_ADD);
  441. btor(3, BT848_GPIO_DMA_CTL);
  442. btv->dma_on = 1;
  443. } else {
  444. if (!btv->dma_on)
  445. return;
  446. btand(~3, BT848_GPIO_DMA_CTL);
  447. btv->dma_on = 0;
  448. }
  449. return;
  450. }
  451. int
  452. bttv_risc_init_main(struct bttv *btv)
  453. {
  454. int rc;
  455. if ((rc = btcx_riscmem_alloc(btv->c.pci,&btv->main,PAGE_SIZE)) < 0)
  456. return rc;
  457. dprintk(KERN_DEBUG "bttv%d: risc main @ %08Lx\n",
  458. btv->c.nr,(unsigned long long)btv->main.dma);
  459. btv->main.cpu[0] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  460. BT848_FIFO_STATUS_VRE);
  461. btv->main.cpu[1] = cpu_to_le32(0);
  462. btv->main.cpu[2] = cpu_to_le32(BT848_RISC_JUMP);
  463. btv->main.cpu[3] = cpu_to_le32(btv->main.dma + (4<<2));
  464. /* top field */
  465. btv->main.cpu[4] = cpu_to_le32(BT848_RISC_JUMP);
  466. btv->main.cpu[5] = cpu_to_le32(btv->main.dma + (6<<2));
  467. btv->main.cpu[6] = cpu_to_le32(BT848_RISC_JUMP);
  468. btv->main.cpu[7] = cpu_to_le32(btv->main.dma + (8<<2));
  469. btv->main.cpu[8] = cpu_to_le32(BT848_RISC_SYNC | BT848_RISC_RESYNC |
  470. BT848_FIFO_STATUS_VRO);
  471. btv->main.cpu[9] = cpu_to_le32(0);
  472. /* bottom field */
  473. btv->main.cpu[10] = cpu_to_le32(BT848_RISC_JUMP);
  474. btv->main.cpu[11] = cpu_to_le32(btv->main.dma + (12<<2));
  475. btv->main.cpu[12] = cpu_to_le32(BT848_RISC_JUMP);
  476. btv->main.cpu[13] = cpu_to_le32(btv->main.dma + (14<<2));
  477. /* jump back to top field */
  478. btv->main.cpu[14] = cpu_to_le32(BT848_RISC_JUMP);
  479. btv->main.cpu[15] = cpu_to_le32(btv->main.dma + (0<<2));
  480. return 0;
  481. }
  482. int
  483. bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
  484. int irqflags)
  485. {
  486. unsigned long cmd;
  487. unsigned long next = btv->main.dma + ((slot+2) << 2);
  488. if (NULL == risc) {
  489. d2printk(KERN_DEBUG "bttv%d: risc=%p slot[%d]=NULL\n",
  490. btv->c.nr,risc,slot);
  491. btv->main.cpu[slot+1] = cpu_to_le32(next);
  492. } else {
  493. d2printk(KERN_DEBUG "bttv%d: risc=%p slot[%d]=%08Lx irq=%d\n",
  494. btv->c.nr,risc,slot,(unsigned long long)risc->dma,irqflags);
  495. cmd = BT848_RISC_JUMP;
  496. if (irqflags) {
  497. cmd |= BT848_RISC_IRQ;
  498. cmd |= (irqflags & 0x0f) << 16;
  499. cmd |= (~irqflags & 0x0f) << 20;
  500. }
  501. risc->jmp[0] = cpu_to_le32(cmd);
  502. risc->jmp[1] = cpu_to_le32(next);
  503. btv->main.cpu[slot+1] = cpu_to_le32(risc->dma);
  504. }
  505. return 0;
  506. }
  507. void
  508. bttv_dma_free(struct videobuf_queue *q,struct bttv *btv, struct bttv_buffer *buf)
  509. {
  510. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  511. BUG_ON(in_interrupt());
  512. videobuf_waiton(&buf->vb,0,0);
  513. videobuf_dma_unmap(q, dma);
  514. videobuf_dma_free(dma);
  515. btcx_riscmem_free(btv->c.pci,&buf->bottom);
  516. btcx_riscmem_free(btv->c.pci,&buf->top);
  517. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  518. }
  519. int
  520. bttv_buffer_activate_vbi(struct bttv *btv,
  521. struct bttv_buffer *vbi)
  522. {
  523. struct btcx_riscmem *top;
  524. struct btcx_riscmem *bottom;
  525. int top_irq_flags;
  526. int bottom_irq_flags;
  527. top = NULL;
  528. bottom = NULL;
  529. top_irq_flags = 0;
  530. bottom_irq_flags = 0;
  531. if (vbi) {
  532. unsigned int crop, vdelay;
  533. vbi->vb.state = VIDEOBUF_ACTIVE;
  534. list_del(&vbi->vb.queue);
  535. /* VDELAY is start of video, end of VBI capturing. */
  536. crop = btread(BT848_E_CROP);
  537. vdelay = btread(BT848_E_VDELAY_LO) + ((crop & 0xc0) << 2);
  538. if (vbi->geo.vdelay > vdelay) {
  539. vdelay = vbi->geo.vdelay & 0xfe;
  540. crop = (crop & 0x3f) | ((vbi->geo.vdelay >> 2) & 0xc0);
  541. btwrite(vdelay, BT848_E_VDELAY_LO);
  542. btwrite(crop, BT848_E_CROP);
  543. btwrite(vdelay, BT848_O_VDELAY_LO);
  544. btwrite(crop, BT848_O_CROP);
  545. }
  546. if (vbi->vbi_count[0] > 0) {
  547. top = &vbi->top;
  548. top_irq_flags = 4;
  549. }
  550. if (vbi->vbi_count[1] > 0) {
  551. top_irq_flags = 0;
  552. bottom = &vbi->bottom;
  553. bottom_irq_flags = 4;
  554. }
  555. }
  556. bttv_risc_hook(btv, RISC_SLOT_O_VBI, top, top_irq_flags);
  557. bttv_risc_hook(btv, RISC_SLOT_E_VBI, bottom, bottom_irq_flags);
  558. return 0;
  559. }
  560. int
  561. bttv_buffer_activate_video(struct bttv *btv,
  562. struct bttv_buffer_set *set)
  563. {
  564. /* video capture */
  565. if (NULL != set->top && NULL != set->bottom) {
  566. if (set->top == set->bottom) {
  567. set->top->vb.state = VIDEOBUF_ACTIVE;
  568. if (set->top->vb.queue.next)
  569. list_del(&set->top->vb.queue);
  570. } else {
  571. set->top->vb.state = VIDEOBUF_ACTIVE;
  572. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  573. if (set->top->vb.queue.next)
  574. list_del(&set->top->vb.queue);
  575. if (set->bottom->vb.queue.next)
  576. list_del(&set->bottom->vb.queue);
  577. }
  578. bttv_apply_geo(btv, &set->top->geo, 1);
  579. bttv_apply_geo(btv, &set->bottom->geo,0);
  580. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  581. set->top_irq);
  582. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  583. set->frame_irq);
  584. btaor((set->top->btformat & 0xf0) | (set->bottom->btformat & 0x0f),
  585. ~0xff, BT848_COLOR_FMT);
  586. btaor((set->top->btswap & 0x0a) | (set->bottom->btswap & 0x05),
  587. ~0x0f, BT848_COLOR_CTL);
  588. } else if (NULL != set->top) {
  589. set->top->vb.state = VIDEOBUF_ACTIVE;
  590. if (set->top->vb.queue.next)
  591. list_del(&set->top->vb.queue);
  592. bttv_apply_geo(btv, &set->top->geo,1);
  593. bttv_apply_geo(btv, &set->top->geo,0);
  594. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, &set->top->top,
  595. set->frame_irq);
  596. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  597. btaor(set->top->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  598. btaor(set->top->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  599. } else if (NULL != set->bottom) {
  600. set->bottom->vb.state = VIDEOBUF_ACTIVE;
  601. if (set->bottom->vb.queue.next)
  602. list_del(&set->bottom->vb.queue);
  603. bttv_apply_geo(btv, &set->bottom->geo,1);
  604. bttv_apply_geo(btv, &set->bottom->geo,0);
  605. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  606. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, &set->bottom->bottom,
  607. set->frame_irq);
  608. btaor(set->bottom->btformat & 0xff, ~0xff, BT848_COLOR_FMT);
  609. btaor(set->bottom->btswap & 0x0f, ~0x0f, BT848_COLOR_CTL);
  610. } else {
  611. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  612. bttv_risc_hook(btv, RISC_SLOT_E_FIELD, NULL, 0);
  613. }
  614. return 0;
  615. }
  616. /* ---------------------------------------------------------- */
  617. /* calculate geometry, build risc code */
  618. int
  619. bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf)
  620. {
  621. const struct bttv_tvnorm *tvnorm = bttv_tvnorms + buf->tvnorm;
  622. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  623. dprintk(KERN_DEBUG
  624. "bttv%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(KERN_DEBUG
  776. "bttv%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. }
  804. /*
  805. * Local variables:
  806. * c-basic-offset: 8
  807. * End:
  808. */