cx88-mpeg.c 14 KB

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