pt3_dma.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Earthsoft PT3 driver
  3. *
  4. * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. *
  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. */
  16. #include <linux/dma-mapping.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include "pt3.h"
  20. #define PT3_ACCESS_UNIT (TS_PACKET_SZ * 128)
  21. #define PT3_BUF_CANARY (0x74)
  22. static u32 get_dma_base(int idx)
  23. {
  24. int i;
  25. i = (idx == 1 || idx == 2) ? 3 - idx : idx;
  26. return REG_DMA_BASE + 0x18 * i;
  27. }
  28. int pt3_stop_dma(struct pt3_adapter *adap)
  29. {
  30. struct pt3_board *pt3 = adap->dvb_adap.priv;
  31. u32 base;
  32. u32 stat;
  33. int retry;
  34. base = get_dma_base(adap->adap_idx);
  35. stat = ioread32(pt3->regs[0] + base + OFST_STATUS);
  36. if (!(stat & 0x01))
  37. return 0;
  38. iowrite32(0x02, pt3->regs[0] + base + OFST_DMA_CTL);
  39. for (retry = 0; retry < 5; retry++) {
  40. stat = ioread32(pt3->regs[0] + base + OFST_STATUS);
  41. if (!(stat & 0x01))
  42. return 0;
  43. msleep(50);
  44. }
  45. return -EIO;
  46. }
  47. int pt3_start_dma(struct pt3_adapter *adap)
  48. {
  49. struct pt3_board *pt3 = adap->dvb_adap.priv;
  50. u32 base = get_dma_base(adap->adap_idx);
  51. iowrite32(0x02, pt3->regs[0] + base + OFST_DMA_CTL);
  52. iowrite32(lower_32_bits(adap->desc_buf[0].b_addr),
  53. pt3->regs[0] + base + OFST_DMA_DESC_L);
  54. iowrite32(upper_32_bits(adap->desc_buf[0].b_addr),
  55. pt3->regs[0] + base + OFST_DMA_DESC_H);
  56. iowrite32(0x01, pt3->regs[0] + base + OFST_DMA_CTL);
  57. return 0;
  58. }
  59. static u8 *next_unit(struct pt3_adapter *adap, int *idx, int *ofs)
  60. {
  61. *ofs += PT3_ACCESS_UNIT;
  62. if (*ofs >= DATA_BUF_SZ) {
  63. *ofs -= DATA_BUF_SZ;
  64. (*idx)++;
  65. if (*idx == adap->num_bufs)
  66. *idx = 0;
  67. }
  68. return &adap->buffer[*idx].data[*ofs];
  69. }
  70. int pt3_proc_dma(struct pt3_adapter *adap)
  71. {
  72. int idx, ofs;
  73. idx = adap->buf_idx;
  74. ofs = adap->buf_ofs;
  75. if (adap->buffer[idx].data[ofs] == PT3_BUF_CANARY)
  76. return 0;
  77. while (*next_unit(adap, &idx, &ofs) != PT3_BUF_CANARY) {
  78. u8 *p;
  79. p = &adap->buffer[adap->buf_idx].data[adap->buf_ofs];
  80. if (adap->num_discard > 0)
  81. adap->num_discard--;
  82. else if (adap->buf_ofs + PT3_ACCESS_UNIT > DATA_BUF_SZ) {
  83. dvb_dmx_swfilter_packets(&adap->demux, p,
  84. (DATA_BUF_SZ - adap->buf_ofs) / TS_PACKET_SZ);
  85. dvb_dmx_swfilter_packets(&adap->demux,
  86. adap->buffer[idx].data, ofs / TS_PACKET_SZ);
  87. } else
  88. dvb_dmx_swfilter_packets(&adap->demux, p,
  89. PT3_ACCESS_UNIT / TS_PACKET_SZ);
  90. *p = PT3_BUF_CANARY;
  91. adap->buf_idx = idx;
  92. adap->buf_ofs = ofs;
  93. }
  94. return 0;
  95. }
  96. void pt3_init_dmabuf(struct pt3_adapter *adap)
  97. {
  98. int idx, ofs;
  99. u8 *p;
  100. idx = 0;
  101. ofs = 0;
  102. p = adap->buffer[0].data;
  103. /* mark the whole buffers as "not written yet" */
  104. while (idx < adap->num_bufs) {
  105. p[ofs] = PT3_BUF_CANARY;
  106. ofs += PT3_ACCESS_UNIT;
  107. if (ofs >= DATA_BUF_SZ) {
  108. ofs -= DATA_BUF_SZ;
  109. idx++;
  110. p = adap->buffer[idx].data;
  111. }
  112. }
  113. adap->buf_idx = 0;
  114. adap->buf_ofs = 0;
  115. }
  116. void pt3_free_dmabuf(struct pt3_adapter *adap)
  117. {
  118. struct pt3_board *pt3;
  119. int i;
  120. pt3 = adap->dvb_adap.priv;
  121. for (i = 0; i < adap->num_bufs; i++)
  122. dma_free_coherent(&pt3->pdev->dev, DATA_BUF_SZ,
  123. adap->buffer[i].data, adap->buffer[i].b_addr);
  124. adap->num_bufs = 0;
  125. for (i = 0; i < adap->num_desc_bufs; i++)
  126. dma_free_coherent(&pt3->pdev->dev, PAGE_SIZE,
  127. adap->desc_buf[i].descs, adap->desc_buf[i].b_addr);
  128. adap->num_desc_bufs = 0;
  129. }
  130. int pt3_alloc_dmabuf(struct pt3_adapter *adap)
  131. {
  132. struct pt3_board *pt3;
  133. void *p;
  134. int i, j;
  135. int idx, ofs;
  136. int num_desc_bufs;
  137. dma_addr_t data_addr, desc_addr;
  138. struct xfer_desc *d;
  139. pt3 = adap->dvb_adap.priv;
  140. adap->num_bufs = 0;
  141. adap->num_desc_bufs = 0;
  142. for (i = 0; i < pt3->num_bufs; i++) {
  143. p = dma_alloc_coherent(&pt3->pdev->dev, DATA_BUF_SZ,
  144. &adap->buffer[i].b_addr, GFP_KERNEL);
  145. if (p == NULL)
  146. goto failed;
  147. adap->buffer[i].data = p;
  148. adap->num_bufs++;
  149. }
  150. pt3_init_dmabuf(adap);
  151. /* build circular-linked pointers (xfer_desc) to the data buffers*/
  152. idx = 0;
  153. ofs = 0;
  154. num_desc_bufs =
  155. DIV_ROUND_UP(adap->num_bufs * DATA_BUF_XFERS, DESCS_IN_PAGE);
  156. for (i = 0; i < num_desc_bufs; i++) {
  157. p = dma_alloc_coherent(&pt3->pdev->dev, PAGE_SIZE,
  158. &desc_addr, GFP_KERNEL);
  159. if (p == NULL)
  160. goto failed;
  161. adap->num_desc_bufs++;
  162. adap->desc_buf[i].descs = p;
  163. adap->desc_buf[i].b_addr = desc_addr;
  164. if (i > 0) {
  165. d = &adap->desc_buf[i - 1].descs[DESCS_IN_PAGE - 1];
  166. d->next_l = lower_32_bits(desc_addr);
  167. d->next_h = upper_32_bits(desc_addr);
  168. }
  169. for (j = 0; j < DESCS_IN_PAGE; j++) {
  170. data_addr = adap->buffer[idx].b_addr + ofs;
  171. d = &adap->desc_buf[i].descs[j];
  172. d->addr_l = lower_32_bits(data_addr);
  173. d->addr_h = upper_32_bits(data_addr);
  174. d->size = DATA_XFER_SZ;
  175. desc_addr += sizeof(struct xfer_desc);
  176. d->next_l = lower_32_bits(desc_addr);
  177. d->next_h = upper_32_bits(desc_addr);
  178. ofs += DATA_XFER_SZ;
  179. if (ofs >= DATA_BUF_SZ) {
  180. ofs -= DATA_BUF_SZ;
  181. idx++;
  182. if (idx >= adap->num_bufs) {
  183. desc_addr = adap->desc_buf[0].b_addr;
  184. d->next_l = lower_32_bits(desc_addr);
  185. d->next_h = upper_32_bits(desc_addr);
  186. return 0;
  187. }
  188. }
  189. }
  190. }
  191. return 0;
  192. failed:
  193. pt3_free_dmabuf(adap);
  194. return -ENOMEM;
  195. }