flexcop-pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * This file is part of linux driver the digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. *
  4. * flexcop-pci.c - covers the PCI part including DMA transfers.
  5. *
  6. * see flexcop.c for copyright information.
  7. */
  8. #define FC_LOG_PREFIX "flexcop-pci"
  9. #include "flexcop-common.h"
  10. static int enable_pid_filtering = 1;
  11. module_param(enable_pid_filtering, int, 0444);
  12. MODULE_PARM_DESC(enable_pid_filtering, "enable hardware pid filtering: supported values: 0 (fullts), 1");
  13. static int irq_chk_intv;
  14. module_param(irq_chk_intv, int, 0644);
  15. MODULE_PARM_DESC(irq_chk_intv, "set the interval for IRQ watchdog (currently just debugging).");
  16. #ifdef CONFIG_DVB_B2C2_FLEXCOP_DEBUG
  17. #define dprintk(level,args...) \
  18. do { if ((debug & level)) printk(args); } while (0)
  19. #define DEBSTATUS ""
  20. #else
  21. #define dprintk(level,args...)
  22. #define DEBSTATUS " (debugging is not enabled)"
  23. #endif
  24. #define deb_info(args...) dprintk(0x01,args)
  25. #define deb_reg(args...) dprintk(0x02,args)
  26. #define deb_ts(args...) dprintk(0x04,args)
  27. #define deb_irq(args...) dprintk(0x08,args)
  28. #define deb_chk(args...) dprintk(0x10,args)
  29. static int debug = 0;
  30. module_param(debug, int, 0644);
  31. MODULE_PARM_DESC(debug, "set debug level (1=info,2=regs,4=TS,8=irqdma (|-able))." DEBSTATUS);
  32. #define DRIVER_VERSION "0.1"
  33. #define DRIVER_NAME "Technisat/B2C2 FlexCop II/IIb/III Digital TV PCI Driver"
  34. #define DRIVER_AUTHOR "Patrick Boettcher <patrick.boettcher@desy.de>"
  35. struct flexcop_pci {
  36. struct pci_dev *pdev;
  37. #define FC_PCI_INIT 0x01
  38. #define FC_PCI_DMA_INIT 0x02
  39. int init_state;
  40. void __iomem *io_mem;
  41. u32 irq;
  42. /* buffersize (at least for DMA1, need to be % 188 == 0,
  43. * this logic is required */
  44. #define FC_DEFAULT_DMA1_BUFSIZE (1280 * 188)
  45. #define FC_DEFAULT_DMA2_BUFSIZE (10 * 188)
  46. struct flexcop_dma dma[2];
  47. int active_dma1_addr; /* 0 = addr0 of dma1; 1 = addr1 of dma1 */
  48. u32 last_dma1_cur_pos; /* position of the pointer last time the timer/packet irq occured */
  49. int count;
  50. spinlock_t irq_lock;
  51. unsigned long last_irq;
  52. struct delayed_work irq_check_work;
  53. struct flexcop_device *fc_dev;
  54. };
  55. static int lastwreg,lastwval,lastrreg,lastrval;
  56. static flexcop_ibi_value flexcop_pci_read_ibi_reg (struct flexcop_device *fc, flexcop_ibi_register r)
  57. {
  58. struct flexcop_pci *fc_pci = fc->bus_specific;
  59. flexcop_ibi_value v;
  60. v.raw = readl(fc_pci->io_mem + r);
  61. if (lastrreg != r || lastrval != v.raw) {
  62. lastrreg = r; lastrval = v.raw;
  63. deb_reg("new rd: %3x: %08x\n",r,v.raw);
  64. }
  65. return v;
  66. }
  67. static int flexcop_pci_write_ibi_reg(struct flexcop_device *fc, flexcop_ibi_register r, flexcop_ibi_value v)
  68. {
  69. struct flexcop_pci *fc_pci = fc->bus_specific;
  70. if (lastwreg != r || lastwval != v.raw) {
  71. lastwreg = r; lastwval = v.raw;
  72. deb_reg("new wr: %3x: %08x\n",r,v.raw);
  73. }
  74. writel(v.raw, fc_pci->io_mem + r);
  75. return 0;
  76. }
  77. static void flexcop_pci_irq_check_work(struct work_struct *work)
  78. {
  79. struct flexcop_pci *fc_pci =
  80. container_of(work, struct flexcop_pci, irq_check_work.work);
  81. struct flexcop_device *fc = fc_pci->fc_dev;
  82. flexcop_ibi_value v = fc->read_ibi_reg(fc,sram_dest_reg_714);
  83. flexcop_dump_reg(fc_pci->fc_dev,dma1_000,4);
  84. if (v.sram_dest_reg_714.net_ovflow_error)
  85. deb_chk("sram net_ovflow_error\n");
  86. if (v.sram_dest_reg_714.media_ovflow_error)
  87. deb_chk("sram media_ovflow_error\n");
  88. if (v.sram_dest_reg_714.cai_ovflow_error)
  89. deb_chk("sram cai_ovflow_error\n");
  90. if (v.sram_dest_reg_714.cai_ovflow_error)
  91. deb_chk("sram cai_ovflow_error\n");
  92. schedule_delayed_work(&fc_pci->irq_check_work,
  93. msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
  94. }
  95. /* When PID filtering is turned on, we use the timer IRQ, because small amounts
  96. * of data need to be passed to the user space instantly as well. When PID
  97. * filtering is turned off, we use the page-change-IRQ */
  98. static irqreturn_t flexcop_pci_isr(int irq, void *dev_id)
  99. {
  100. struct flexcop_pci *fc_pci = dev_id;
  101. struct flexcop_device *fc = fc_pci->fc_dev;
  102. flexcop_ibi_value v;
  103. irqreturn_t ret = IRQ_HANDLED;
  104. spin_lock_irq(&fc_pci->irq_lock);
  105. v = fc->read_ibi_reg(fc,irq_20c);
  106. /* errors */
  107. if (v.irq_20c.Data_receiver_error)
  108. deb_chk("data receiver error\n");
  109. if (v.irq_20c.Continuity_error_flag)
  110. deb_chk("Contunuity error flag is set\n");
  111. if (v.irq_20c.LLC_SNAP_FLAG_set)
  112. deb_chk("LLC_SNAP_FLAG_set is set\n");
  113. if (v.irq_20c.Transport_Error)
  114. deb_chk("Transport error\n");
  115. if ((fc_pci->count % 1000) == 0)
  116. deb_chk("%d valid irq took place so far\n",fc_pci->count);
  117. if (v.irq_20c.DMA1_IRQ_Status == 1) {
  118. if (fc_pci->active_dma1_addr == 0)
  119. flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr0,fc_pci->dma[0].size / 188);
  120. else
  121. flexcop_pass_dmx_packets(fc_pci->fc_dev,fc_pci->dma[0].cpu_addr1,fc_pci->dma[0].size / 188);
  122. deb_irq("page change to page: %d\n",!fc_pci->active_dma1_addr);
  123. fc_pci->active_dma1_addr = !fc_pci->active_dma1_addr;
  124. } else if (v.irq_20c.DMA1_Timer_Status == 1) {
  125. /* for the timer IRQ we only can use buffer dmx feeding, because we don't have
  126. * complete TS packets when reading from the DMA memory */
  127. dma_addr_t cur_addr =
  128. fc->read_ibi_reg(fc,dma1_008).dma_0x8.dma_cur_addr << 2;
  129. u32 cur_pos = cur_addr - fc_pci->dma[0].dma_addr0;
  130. deb_irq("%u irq: %08x cur_addr: %llx: cur_pos: %08x, last_cur_pos: %08x ",
  131. jiffies_to_usecs(jiffies - fc_pci->last_irq),
  132. v.raw, (unsigned long long)cur_addr, cur_pos,
  133. fc_pci->last_dma1_cur_pos);
  134. fc_pci->last_irq = jiffies;
  135. /* buffer end was reached, restarted from the beginning
  136. * pass the data from last_cur_pos to the buffer end to the demux
  137. */
  138. if (cur_pos < fc_pci->last_dma1_cur_pos) {
  139. deb_irq(" end was reached: passing %d bytes ",(fc_pci->dma[0].size*2 - 1) - fc_pci->last_dma1_cur_pos);
  140. flexcop_pass_dmx_data(fc_pci->fc_dev,
  141. fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
  142. (fc_pci->dma[0].size*2) - fc_pci->last_dma1_cur_pos);
  143. fc_pci->last_dma1_cur_pos = 0;
  144. }
  145. if (cur_pos > fc_pci->last_dma1_cur_pos) {
  146. deb_irq(" passing %d bytes ",cur_pos - fc_pci->last_dma1_cur_pos);
  147. flexcop_pass_dmx_data(fc_pci->fc_dev,
  148. fc_pci->dma[0].cpu_addr0 + fc_pci->last_dma1_cur_pos,
  149. cur_pos - fc_pci->last_dma1_cur_pos);
  150. }
  151. deb_irq("\n");
  152. fc_pci->last_dma1_cur_pos = cur_pos;
  153. fc_pci->count++;
  154. } else {
  155. deb_irq("isr for flexcop called, apparently without reason (%08x)\n",v.raw);
  156. ret = IRQ_NONE;
  157. }
  158. spin_unlock_irq(&fc_pci->irq_lock);
  159. return ret;
  160. }
  161. static int flexcop_pci_stream_control(struct flexcop_device *fc, int onoff)
  162. {
  163. struct flexcop_pci *fc_pci = fc->bus_specific;
  164. if (onoff) {
  165. flexcop_dma_config(fc,&fc_pci->dma[0],FC_DMA_1);
  166. flexcop_dma_config(fc,&fc_pci->dma[1],FC_DMA_2);
  167. flexcop_dma_config_timer(fc,FC_DMA_1,0);
  168. flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,1);
  169. deb_irq("DMA xfer enabled\n");
  170. fc_pci->last_dma1_cur_pos = 0;
  171. flexcop_dma_control_timer_irq(fc,FC_DMA_1,1);
  172. deb_irq("IRQ enabled\n");
  173. // fc_pci->active_dma1_addr = 0;
  174. // flexcop_dma_control_size_irq(fc,FC_DMA_1,1);
  175. if (irq_chk_intv > 0)
  176. schedule_delayed_work(&fc_pci->irq_check_work,
  177. msecs_to_jiffies(irq_chk_intv < 100 ? 100 : irq_chk_intv));
  178. } else {
  179. if (irq_chk_intv > 0)
  180. cancel_delayed_work(&fc_pci->irq_check_work);
  181. flexcop_dma_control_timer_irq(fc,FC_DMA_1,0);
  182. deb_irq("IRQ disabled\n");
  183. // flexcop_dma_control_size_irq(fc,FC_DMA_1,0);
  184. flexcop_dma_xfer_control(fc,FC_DMA_1,FC_DMA_SUBADDR_0 | FC_DMA_SUBADDR_1,0);
  185. deb_irq("DMA xfer disabled\n");
  186. }
  187. return 0;
  188. }
  189. static int flexcop_pci_dma_init(struct flexcop_pci *fc_pci)
  190. {
  191. int ret;
  192. if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[0],FC_DEFAULT_DMA1_BUFSIZE)) != 0)
  193. return ret;
  194. if ((ret = flexcop_dma_allocate(fc_pci->pdev,&fc_pci->dma[1],FC_DEFAULT_DMA2_BUFSIZE)) != 0) {
  195. flexcop_dma_free(&fc_pci->dma[0]);
  196. return ret;
  197. }
  198. flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_MEDIA | FC_SRAM_DEST_NET, FC_SRAM_DEST_TARGET_DMA1);
  199. flexcop_sram_set_dest(fc_pci->fc_dev,FC_SRAM_DEST_CAO | FC_SRAM_DEST_CAI, FC_SRAM_DEST_TARGET_DMA2);
  200. fc_pci->init_state |= FC_PCI_DMA_INIT;
  201. return ret;
  202. }
  203. static void flexcop_pci_dma_exit(struct flexcop_pci *fc_pci)
  204. {
  205. if (fc_pci->init_state & FC_PCI_DMA_INIT) {
  206. flexcop_dma_free(&fc_pci->dma[0]);
  207. flexcop_dma_free(&fc_pci->dma[1]);
  208. }
  209. fc_pci->init_state &= ~FC_PCI_DMA_INIT;
  210. }
  211. static int flexcop_pci_init(struct flexcop_pci *fc_pci)
  212. {
  213. int ret;
  214. u8 card_rev;
  215. pci_read_config_byte(fc_pci->pdev, PCI_CLASS_REVISION, &card_rev);
  216. info("card revision %x", card_rev);
  217. if ((ret = pci_enable_device(fc_pci->pdev)) != 0)
  218. return ret;
  219. pci_set_master(fc_pci->pdev);
  220. /* enable interrupts */
  221. // pci_write_config_dword(pdev, 0x6c, 0x8000);
  222. if ((ret = pci_request_regions(fc_pci->pdev, DRIVER_NAME)) != 0)
  223. goto err_pci_disable_device;
  224. fc_pci->io_mem = pci_iomap(fc_pci->pdev, 0, 0x800);
  225. if (!fc_pci->io_mem) {
  226. err("cannot map io memory\n");
  227. ret = -EIO;
  228. goto err_pci_release_regions;
  229. }
  230. pci_set_drvdata(fc_pci->pdev, fc_pci);
  231. if ((ret = request_irq(fc_pci->pdev->irq, flexcop_pci_isr,
  232. IRQF_SHARED, DRIVER_NAME, fc_pci)) != 0)
  233. goto err_pci_iounmap;
  234. spin_lock_init(&fc_pci->irq_lock);
  235. fc_pci->init_state |= FC_PCI_INIT;
  236. return ret;
  237. err_pci_iounmap:
  238. pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
  239. pci_set_drvdata(fc_pci->pdev, NULL);
  240. err_pci_release_regions:
  241. pci_release_regions(fc_pci->pdev);
  242. err_pci_disable_device:
  243. pci_disable_device(fc_pci->pdev);
  244. return ret;
  245. }
  246. static void flexcop_pci_exit(struct flexcop_pci *fc_pci)
  247. {
  248. if (fc_pci->init_state & FC_PCI_INIT) {
  249. free_irq(fc_pci->pdev->irq, fc_pci);
  250. pci_iounmap(fc_pci->pdev, fc_pci->io_mem);
  251. pci_set_drvdata(fc_pci->pdev, NULL);
  252. pci_release_regions(fc_pci->pdev);
  253. pci_disable_device(fc_pci->pdev);
  254. }
  255. fc_pci->init_state &= ~FC_PCI_INIT;
  256. }
  257. static int flexcop_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  258. {
  259. struct flexcop_device *fc;
  260. struct flexcop_pci *fc_pci;
  261. int ret = -ENOMEM;
  262. if ((fc = flexcop_device_kmalloc(sizeof(struct flexcop_pci))) == NULL) {
  263. err("out of memory\n");
  264. return -ENOMEM;
  265. }
  266. /* general flexcop init */
  267. fc_pci = fc->bus_specific;
  268. fc_pci->fc_dev = fc;
  269. fc->read_ibi_reg = flexcop_pci_read_ibi_reg;
  270. fc->write_ibi_reg = flexcop_pci_write_ibi_reg;
  271. fc->i2c_request = flexcop_i2c_request;
  272. fc->get_mac_addr = flexcop_eeprom_check_mac_addr;
  273. fc->stream_control = flexcop_pci_stream_control;
  274. if (enable_pid_filtering)
  275. info("will use the HW PID filter.");
  276. else
  277. info("will pass the complete TS to the demuxer.");
  278. fc->pid_filtering = enable_pid_filtering;
  279. fc->bus_type = FC_PCI;
  280. fc->dev = &pdev->dev;
  281. fc->owner = THIS_MODULE;
  282. /* bus specific part */
  283. fc_pci->pdev = pdev;
  284. if ((ret = flexcop_pci_init(fc_pci)) != 0)
  285. goto err_kfree;
  286. /* init flexcop */
  287. if ((ret = flexcop_device_initialize(fc)) != 0)
  288. goto err_pci_exit;
  289. /* init dma */
  290. if ((ret = flexcop_pci_dma_init(fc_pci)) != 0)
  291. goto err_fc_exit;
  292. INIT_DELAYED_WORK(&fc_pci->irq_check_work, flexcop_pci_irq_check_work);
  293. return ret;
  294. err_fc_exit:
  295. flexcop_device_exit(fc);
  296. err_pci_exit:
  297. flexcop_pci_exit(fc_pci);
  298. err_kfree:
  299. flexcop_device_kfree(fc);
  300. return ret;
  301. }
  302. /* in theory every _exit function should be called exactly two times,
  303. * here and in the bail-out-part of the _init-function
  304. */
  305. static void flexcop_pci_remove(struct pci_dev *pdev)
  306. {
  307. struct flexcop_pci *fc_pci = pci_get_drvdata(pdev);
  308. flexcop_pci_dma_exit(fc_pci);
  309. flexcop_device_exit(fc_pci->fc_dev);
  310. flexcop_pci_exit(fc_pci);
  311. flexcop_device_kfree(fc_pci->fc_dev);
  312. }
  313. static struct pci_device_id flexcop_pci_tbl[] = {
  314. { PCI_DEVICE(0x13d0, 0x2103) },
  315. /* { PCI_DEVICE(0x13d0, 0x2200) }, ? */
  316. { },
  317. };
  318. MODULE_DEVICE_TABLE(pci, flexcop_pci_tbl);
  319. static struct pci_driver flexcop_pci_driver = {
  320. .name = "b2c2_flexcop_pci",
  321. .id_table = flexcop_pci_tbl,
  322. .probe = flexcop_pci_probe,
  323. .remove = flexcop_pci_remove,
  324. };
  325. static int __init flexcop_pci_module_init(void)
  326. {
  327. return pci_register_driver(&flexcop_pci_driver);
  328. }
  329. static void __exit flexcop_pci_module_exit(void)
  330. {
  331. pci_unregister_driver(&flexcop_pci_driver);
  332. }
  333. module_init(flexcop_pci_module_init);
  334. module_exit(flexcop_pci_module_exit);
  335. MODULE_AUTHOR(DRIVER_AUTHOR);
  336. MODULE_DESCRIPTION(DRIVER_NAME);
  337. MODULE_LICENSE("GPL");