cx88-mpeg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. *
  3. * Support for the mpeg transport stream transfers
  4. * PCI function #2 of the cx2388x.
  5. *
  6. * (c) 2004 Jelle Foks <jelle@foks.8m.com>
  7. * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  8. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/device.h>
  28. #include <linux/interrupt.h>
  29. #include <asm/delay.h>
  30. #include "cx88.h"
  31. /* ------------------------------------------------------------------ */
  32. MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
  33. MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
  34. MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
  35. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  36. MODULE_LICENSE("GPL");
  37. static unsigned int debug = 0;
  38. module_param(debug,int,0644);
  39. MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
  40. #define dprintk(level,fmt, arg...) if (debug >= level) \
  41. printk(KERN_DEBUG "%s/2: " fmt, dev->core->name , ## arg)
  42. /* ------------------------------------------------------------------ */
  43. static int cx8802_start_dma(struct cx8802_dev *dev,
  44. struct cx88_dmaqueue *q,
  45. struct cx88_buffer *buf)
  46. {
  47. struct cx88_core *core = dev->core;
  48. dprintk(0, "cx8802_start_dma w: %d, h: %d, f: %d\n", dev->width, dev->height, buf->vb.field);
  49. /* setup fifo + format */
  50. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
  51. dev->ts_packet_size, buf->risc.dma);
  52. /* write TS length to chip */
  53. cx_write(MO_TS_LNGTH, buf->vb.width);
  54. /* FIXME: this needs a review.
  55. * also: move to cx88-blackbird + cx88-dvb source files? */
  56. if (cx88_boards[core->board].dvb) {
  57. /* negedge driven & software reset */
  58. cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
  59. udelay(100);
  60. cx_write(MO_PINMUX_IO, 0x00);
  61. cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01);
  62. switch (core->board) {
  63. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
  64. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
  65. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  66. cx_write(TS_SOP_STAT, 1<<13);
  67. break;
  68. default:
  69. cx_write(TS_SOP_STAT, 0x00);
  70. break;
  71. }
  72. cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
  73. udelay(100);
  74. }
  75. if (cx88_boards[core->board].blackbird) {
  76. cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
  77. cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
  78. udelay(100);
  79. cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
  80. cx_write(TS_VALERR_CNTRL, 0x2000);
  81. cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
  82. udelay(100);
  83. }
  84. /* reset counter */
  85. cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
  86. q->count = 1;
  87. /* enable irqs */
  88. dprintk( 0, "setting the interrupt mask\n" );
  89. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x04);
  90. cx_set(MO_TS_INTMSK, 0x1f0011);
  91. /* start dma */
  92. cx_set(MO_DEV_CNTRL2, (1<<5));
  93. cx_set(MO_TS_DMACNTRL, 0x11);
  94. return 0;
  95. }
  96. static int cx8802_stop_dma(struct cx8802_dev *dev)
  97. {
  98. struct cx88_core *core = dev->core;
  99. dprintk( 0, "cx8802_stop_dma\n" );
  100. /* stop dma */
  101. cx_clear(MO_TS_DMACNTRL, 0x11);
  102. /* disable irqs */
  103. cx_clear(MO_PCI_INTMSK, 0x000004);
  104. cx_clear(MO_TS_INTMSK, 0x1f0011);
  105. /* Reset the controller */
  106. cx_write(TS_GEN_CNTRL, 0xcd);
  107. return 0;
  108. }
  109. static int cx8802_restart_queue(struct cx8802_dev *dev,
  110. struct cx88_dmaqueue *q)
  111. {
  112. struct cx88_buffer *buf;
  113. struct list_head *item;
  114. dprintk( 0, "cx8802_restart_queue\n" );
  115. if (list_empty(&q->active))
  116. {
  117. dprintk( 0, "cx8802_restart_queue: queue is empty\n" );
  118. return 0;
  119. }
  120. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  121. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  122. buf, buf->vb.i);
  123. cx8802_start_dma(dev, q, buf);
  124. list_for_each(item,&q->active) {
  125. buf = list_entry(item, struct cx88_buffer, vb.queue);
  126. buf->count = q->count++;
  127. }
  128. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  129. return 0;
  130. }
  131. /* ------------------------------------------------------------------ */
  132. int cx8802_buf_prepare(struct cx8802_dev *dev, struct cx88_buffer *buf,
  133. enum v4l2_field field)
  134. {
  135. int size = dev->ts_packet_size * dev->ts_packet_count;
  136. int rc;
  137. dprintk(1, "%s: %p\n", __FUNCTION__, buf);
  138. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  139. return -EINVAL;
  140. if (STATE_NEEDS_INIT == buf->vb.state) {
  141. buf->vb.width = dev->ts_packet_size;
  142. buf->vb.height = dev->ts_packet_count;
  143. buf->vb.size = size;
  144. buf->vb.field = field /*V4L2_FIELD_TOP*/;
  145. if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
  146. goto fail;
  147. cx88_risc_databuffer(dev->pci, &buf->risc,
  148. buf->vb.dma.sglist,
  149. buf->vb.width, buf->vb.height);
  150. }
  151. buf->vb.state = STATE_PREPARED;
  152. return 0;
  153. fail:
  154. cx88_free_buffer(dev->pci,buf);
  155. return rc;
  156. }
  157. void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
  158. {
  159. struct cx88_buffer *prev;
  160. struct cx88_dmaqueue *q = &dev->mpegq;
  161. dprintk( 1, "cx8802_buf_queue\n" );
  162. /* add jump to stopper */
  163. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  164. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  165. if (list_empty(&q->active)) {
  166. dprintk( 0, "queue is empty - first active\n" );
  167. list_add_tail(&buf->vb.queue,&q->active);
  168. cx8802_start_dma(dev, q, buf);
  169. buf->vb.state = STATE_ACTIVE;
  170. buf->count = q->count++;
  171. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  172. dprintk(0,"[%p/%d] %s - first active\n",
  173. buf, buf->vb.i, __FUNCTION__);
  174. } else {
  175. dprintk( 1, "queue is not empty - append to active\n" );
  176. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  177. list_add_tail(&buf->vb.queue,&q->active);
  178. buf->vb.state = STATE_ACTIVE;
  179. buf->count = q->count++;
  180. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  181. dprintk( 1, "[%p/%d] %s - append to active\n",
  182. buf, buf->vb.i, __FUNCTION__);
  183. }
  184. }
  185. /* ----------------------------------------------------------- */
  186. static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
  187. {
  188. struct cx88_dmaqueue *q = &dev->mpegq;
  189. struct cx88_buffer *buf;
  190. unsigned long flags;
  191. spin_lock_irqsave(&dev->slock,flags);
  192. while (!list_empty(&q->active)) {
  193. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  194. list_del(&buf->vb.queue);
  195. buf->vb.state = STATE_ERROR;
  196. wake_up(&buf->vb.done);
  197. dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
  198. buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
  199. }
  200. if (restart)
  201. {
  202. dprintk(0, "restarting queue\n" );
  203. cx8802_restart_queue(dev,q);
  204. }
  205. spin_unlock_irqrestore(&dev->slock,flags);
  206. }
  207. void cx8802_cancel_buffers(struct cx8802_dev *dev)
  208. {
  209. struct cx88_dmaqueue *q = &dev->mpegq;
  210. dprintk( 1, "cx8802_cancel_buffers" );
  211. del_timer_sync(&q->timeout);
  212. cx8802_stop_dma(dev);
  213. do_cancel_buffers(dev,"cancel",0);
  214. }
  215. static void cx8802_timeout(unsigned long data)
  216. {
  217. struct cx8802_dev *dev = (struct cx8802_dev*)data;
  218. dprintk(0, "%s\n",__FUNCTION__);
  219. if (debug)
  220. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  221. cx8802_stop_dma(dev);
  222. do_cancel_buffers(dev,"timeout",1);
  223. }
  224. static char *cx88_mpeg_irqs[32] = {
  225. "ts_risci1", NULL, NULL, NULL,
  226. "ts_risci2", NULL, NULL, NULL,
  227. "ts_oflow", NULL, NULL, NULL,
  228. "ts_sync", NULL, NULL, NULL,
  229. "opc_err", "par_err", "rip_err", "pci_abort",
  230. "ts_err?",
  231. };
  232. static void cx8802_mpeg_irq(struct cx8802_dev *dev)
  233. {
  234. struct cx88_core *core = dev->core;
  235. u32 status, mask, count;
  236. dprintk( 1, "cx8802_mpeg_irq\n" );
  237. status = cx_read(MO_TS_INTSTAT);
  238. mask = cx_read(MO_TS_INTMSK);
  239. if (0 == (status & mask))
  240. return;
  241. cx_write(MO_TS_INTSTAT, status);
  242. if (debug || (status & mask & ~0xff))
  243. cx88_print_irqbits(core->name, "irq mpeg ",
  244. cx88_mpeg_irqs, status, mask);
  245. /* risc op code error */
  246. if (status & (1 << 16)) {
  247. printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
  248. cx_clear(MO_TS_DMACNTRL, 0x11);
  249. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  250. }
  251. /* risc1 y */
  252. if (status & 0x01) {
  253. dprintk( 1, "wake up\n" );
  254. spin_lock(&dev->slock);
  255. count = cx_read(MO_TS_GPCNT);
  256. cx88_wakeup(dev->core, &dev->mpegq, count);
  257. spin_unlock(&dev->slock);
  258. }
  259. /* risc2 y */
  260. if (status & 0x10) {
  261. spin_lock(&dev->slock);
  262. cx8802_restart_queue(dev,&dev->mpegq);
  263. spin_unlock(&dev->slock);
  264. }
  265. /* other general errors */
  266. if (status & 0x1f0100) {
  267. dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
  268. spin_lock(&dev->slock);
  269. cx8802_stop_dma(dev);
  270. cx8802_restart_queue(dev,&dev->mpegq);
  271. spin_unlock(&dev->slock);
  272. }
  273. }
  274. #define MAX_IRQ_LOOP 10
  275. static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
  276. {
  277. struct cx8802_dev *dev = dev_id;
  278. struct cx88_core *core = dev->core;
  279. u32 status;
  280. int loop, handled = 0;
  281. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  282. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x04);
  283. if (0 == status)
  284. goto out;
  285. dprintk( 1, "cx8802_irq\n" );
  286. dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
  287. dprintk( 1, " status: %d\n", status );
  288. handled = 1;
  289. cx_write(MO_PCI_INTSTAT, status);
  290. if (status & core->pci_irqmask)
  291. cx88_core_irq(core,status);
  292. if (status & 0x04)
  293. cx8802_mpeg_irq(dev);
  294. };
  295. if (MAX_IRQ_LOOP == loop) {
  296. dprintk( 0, "clearing mask\n" );
  297. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  298. core->name);
  299. cx_write(MO_PCI_INTMSK,0);
  300. }
  301. out:
  302. return IRQ_RETVAL(handled);
  303. }
  304. /* ----------------------------------------------------------- */
  305. /* exported stuff */
  306. int cx8802_init_common(struct cx8802_dev *dev)
  307. {
  308. struct cx88_core *core = dev->core;
  309. int err;
  310. /* pci init */
  311. if (pci_enable_device(dev->pci))
  312. return -EIO;
  313. pci_set_master(dev->pci);
  314. if (!pci_dma_supported(dev->pci,0xffffffff)) {
  315. printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
  316. return -EIO;
  317. }
  318. pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
  319. pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
  320. printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
  321. "latency: %d, mmio: 0x%lx\n", dev->core->name,
  322. pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
  323. dev->pci_lat,pci_resource_start(dev->pci,0));
  324. /* initialize driver struct */
  325. spin_lock_init(&dev->slock);
  326. /* init dma queue */
  327. INIT_LIST_HEAD(&dev->mpegq.active);
  328. INIT_LIST_HEAD(&dev->mpegq.queued);
  329. dev->mpegq.timeout.function = cx8802_timeout;
  330. dev->mpegq.timeout.data = (unsigned long)dev;
  331. init_timer(&dev->mpegq.timeout);
  332. cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
  333. MO_TS_DMACNTRL,0x11,0x00);
  334. /* get irq */
  335. err = request_irq(dev->pci->irq, cx8802_irq,
  336. SA_SHIRQ | SA_INTERRUPT, dev->core->name, dev);
  337. if (err < 0) {
  338. printk(KERN_ERR "%s: can't get IRQ %d\n",
  339. dev->core->name, dev->pci->irq);
  340. return err;
  341. }
  342. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  343. /* everything worked */
  344. pci_set_drvdata(dev->pci,dev);
  345. return 0;
  346. }
  347. void cx8802_fini_common(struct cx8802_dev *dev)
  348. {
  349. dprintk( 2, "cx8802_fini_common\n" );
  350. cx8802_stop_dma(dev);
  351. pci_disable_device(dev->pci);
  352. /* unregister stuff */
  353. free_irq(dev->pci->irq, dev);
  354. pci_set_drvdata(dev->pci, NULL);
  355. /* free memory */
  356. btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
  357. }
  358. /* ----------------------------------------------------------- */
  359. int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
  360. {
  361. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  362. struct cx88_core *core = dev->core;
  363. /* stop mpeg dma */
  364. spin_lock(&dev->slock);
  365. if (!list_empty(&dev->mpegq.active)) {
  366. dprintk( 2, "suspend\n" );
  367. printk("%s: suspend mpeg\n", core->name);
  368. cx8802_stop_dma(dev);
  369. del_timer(&dev->mpegq.timeout);
  370. }
  371. spin_unlock(&dev->slock);
  372. /* FIXME -- shutdown device */
  373. cx88_shutdown(dev->core);
  374. pci_save_state(pci_dev);
  375. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  376. pci_disable_device(pci_dev);
  377. dev->state.disabled = 1;
  378. }
  379. return 0;
  380. }
  381. int cx8802_resume_common(struct pci_dev *pci_dev)
  382. {
  383. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  384. struct cx88_core *core = dev->core;
  385. int err;
  386. if (dev->state.disabled) {
  387. err=pci_enable_device(pci_dev);
  388. if (err) {
  389. printk(KERN_ERR "%s: can't enable device\n",
  390. dev->core->name);
  391. return err;
  392. }
  393. dev->state.disabled = 0;
  394. }
  395. err=pci_set_power_state(pci_dev, PCI_D0);
  396. if (err) {
  397. printk(KERN_ERR "%s: can't enable device\n",
  398. dev->core->name);
  399. pci_disable_device(pci_dev);
  400. dev->state.disabled = 1;
  401. return err;
  402. }
  403. pci_restore_state(pci_dev);
  404. /* FIXME: re-initialize hardware */
  405. cx88_reset(dev->core);
  406. /* restart video+vbi capture */
  407. spin_lock(&dev->slock);
  408. if (!list_empty(&dev->mpegq.active)) {
  409. printk("%s: resume mpeg\n", core->name);
  410. cx8802_restart_queue(dev,&dev->mpegq);
  411. }
  412. spin_unlock(&dev->slock);
  413. return 0;
  414. }
  415. /* ----------------------------------------------------------- */
  416. EXPORT_SYMBOL(cx8802_buf_prepare);
  417. EXPORT_SYMBOL(cx8802_buf_queue);
  418. EXPORT_SYMBOL(cx8802_cancel_buffers);
  419. EXPORT_SYMBOL(cx8802_init_common);
  420. EXPORT_SYMBOL(cx8802_fini_common);
  421. EXPORT_SYMBOL(cx8802_suspend_common);
  422. EXPORT_SYMBOL(cx8802_resume_common);
  423. /* ----------------------------------------------------------- */
  424. /*
  425. * Local variables:
  426. * c-basic-offset: 8
  427. * End:
  428. * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
  429. */