flexcop-pci.c 10 KB

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