mantis_dma.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Mantis PCI bridge driver
  4. Copyright (C) Manu Abraham ([email protected])
  5. */
  6. #include <linux/kernel.h>
  7. #include <asm/page.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/pci.h>
  10. #include <asm/irq.h>
  11. #include <linux/signal.h>
  12. #include <linux/sched.h>
  13. #include <linux/interrupt.h>
  14. #include <media/dmxdev.h>
  15. #include <media/dvbdev.h>
  16. #include <media/dvb_demux.h>
  17. #include <media/dvb_frontend.h>
  18. #include <media/dvb_net.h>
  19. #include "mantis_common.h"
  20. #include "mantis_reg.h"
  21. #include "mantis_dma.h"
  22. #define RISC_WRITE (0x01 << 28)
  23. #define RISC_JUMP (0x07 << 28)
  24. #define RISC_IRQ (0x01 << 24)
  25. #define RISC_STATUS(status) ((((~status) & 0x0f) << 20) | ((status & 0x0f) << 16))
  26. #define RISC_FLUSH(risc_pos) (risc_pos = 0)
  27. #define RISC_INSTR(risc_pos, opcode) (mantis->risc_cpu[risc_pos++] = cpu_to_le32(opcode))
  28. #define MANTIS_BUF_SIZE (64 * 1024)
  29. #define MANTIS_BLOCK_BYTES (MANTIS_BUF_SIZE / 4)
  30. #define MANTIS_DMA_TR_BYTES (2 * 1024) /* upper limit: 4095 bytes. */
  31. #define MANTIS_BLOCK_COUNT (MANTIS_BUF_SIZE / MANTIS_BLOCK_BYTES)
  32. #define MANTIS_DMA_TR_UNITS (MANTIS_BLOCK_BYTES / MANTIS_DMA_TR_BYTES)
  33. /* MANTIS_BUF_SIZE / MANTIS_DMA_TR_UNITS must not exceed MANTIS_RISC_SIZE (4k RISC cmd buffer) */
  34. #define MANTIS_RISC_SIZE PAGE_SIZE /* RISC program must fit here. */
  35. int mantis_dma_exit(struct mantis_pci *mantis)
  36. {
  37. if (mantis->buf_cpu) {
  38. dprintk(MANTIS_ERROR, 1,
  39. "DMA=0x%lx cpu=0x%p size=%d",
  40. (unsigned long) mantis->buf_dma,
  41. mantis->buf_cpu,
  42. MANTIS_BUF_SIZE);
  43. dma_free_coherent(&mantis->pdev->dev, MANTIS_BUF_SIZE,
  44. mantis->buf_cpu, mantis->buf_dma);
  45. mantis->buf_cpu = NULL;
  46. }
  47. if (mantis->risc_cpu) {
  48. dprintk(MANTIS_ERROR, 1,
  49. "RISC=0x%lx cpu=0x%p size=%lx",
  50. (unsigned long) mantis->risc_dma,
  51. mantis->risc_cpu,
  52. MANTIS_RISC_SIZE);
  53. dma_free_coherent(&mantis->pdev->dev, MANTIS_RISC_SIZE,
  54. mantis->risc_cpu, mantis->risc_dma);
  55. mantis->risc_cpu = NULL;
  56. }
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(mantis_dma_exit);
  60. static inline int mantis_alloc_buffers(struct mantis_pci *mantis)
  61. {
  62. if (!mantis->buf_cpu) {
  63. mantis->buf_cpu = dma_alloc_coherent(&mantis->pdev->dev,
  64. MANTIS_BUF_SIZE,
  65. &mantis->buf_dma, GFP_KERNEL);
  66. if (!mantis->buf_cpu) {
  67. dprintk(MANTIS_ERROR, 1,
  68. "DMA buffer allocation failed");
  69. goto err;
  70. }
  71. dprintk(MANTIS_ERROR, 1,
  72. "DMA=0x%lx cpu=0x%p size=%d",
  73. (unsigned long) mantis->buf_dma,
  74. mantis->buf_cpu, MANTIS_BUF_SIZE);
  75. }
  76. if (!mantis->risc_cpu) {
  77. mantis->risc_cpu = dma_alloc_coherent(&mantis->pdev->dev,
  78. MANTIS_RISC_SIZE,
  79. &mantis->risc_dma, GFP_KERNEL);
  80. if (!mantis->risc_cpu) {
  81. dprintk(MANTIS_ERROR, 1,
  82. "RISC program allocation failed");
  83. mantis_dma_exit(mantis);
  84. goto err;
  85. }
  86. dprintk(MANTIS_ERROR, 1,
  87. "RISC=0x%lx cpu=0x%p size=%lx",
  88. (unsigned long) mantis->risc_dma,
  89. mantis->risc_cpu, MANTIS_RISC_SIZE);
  90. }
  91. return 0;
  92. err:
  93. dprintk(MANTIS_ERROR, 1, "Out of memory (?) .....");
  94. return -ENOMEM;
  95. }
  96. int mantis_dma_init(struct mantis_pci *mantis)
  97. {
  98. int err;
  99. dprintk(MANTIS_DEBUG, 1, "Mantis DMA init");
  100. err = mantis_alloc_buffers(mantis);
  101. if (err < 0) {
  102. dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer");
  103. /* Stop RISC Engine */
  104. mmwrite(0, MANTIS_DMA_CTL);
  105. return err;
  106. }
  107. return 0;
  108. }
  109. EXPORT_SYMBOL_GPL(mantis_dma_init);
  110. static inline void mantis_risc_program(struct mantis_pci *mantis)
  111. {
  112. u32 buf_pos = 0;
  113. u32 line, step;
  114. u32 risc_pos;
  115. dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program");
  116. RISC_FLUSH(risc_pos);
  117. dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u, bytes per DMA tr %u",
  118. MANTIS_BLOCK_COUNT, MANTIS_BLOCK_BYTES, MANTIS_DMA_TR_BYTES);
  119. for (line = 0; line < MANTIS_BLOCK_COUNT; line++) {
  120. for (step = 0; step < MANTIS_DMA_TR_UNITS; step++) {
  121. dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d], step=[%d]", line, step);
  122. if (step == 0) {
  123. RISC_INSTR(risc_pos, RISC_WRITE |
  124. RISC_IRQ |
  125. RISC_STATUS(line) |
  126. MANTIS_DMA_TR_BYTES);
  127. } else {
  128. RISC_INSTR(risc_pos, RISC_WRITE | MANTIS_DMA_TR_BYTES);
  129. }
  130. RISC_INSTR(risc_pos, mantis->buf_dma + buf_pos);
  131. buf_pos += MANTIS_DMA_TR_BYTES;
  132. }
  133. }
  134. RISC_INSTR(risc_pos, RISC_JUMP);
  135. RISC_INSTR(risc_pos, mantis->risc_dma);
  136. }
  137. void mantis_dma_start(struct mantis_pci *mantis)
  138. {
  139. dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine");
  140. mantis_risc_program(mantis);
  141. mmwrite(mantis->risc_dma, MANTIS_RISC_START);
  142. mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
  143. mmwrite(0, MANTIS_DMA_CTL);
  144. mantis->last_block = mantis->busy_block = 0;
  145. mantis_unmask_ints(mantis, MANTIS_INT_RISCI);
  146. mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN
  147. | MANTIS_RISC_EN, MANTIS_DMA_CTL);
  148. }
  149. void mantis_dma_stop(struct mantis_pci *mantis)
  150. {
  151. dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine");
  152. mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR);
  153. mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN |
  154. MANTIS_DCAP_EN |
  155. MANTIS_RISC_EN)), MANTIS_DMA_CTL);
  156. mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT);
  157. mantis_mask_ints(mantis, MANTIS_INT_RISCI | MANTIS_INT_RISCEN);
  158. }
  159. void mantis_dma_xfer(struct tasklet_struct *t)
  160. {
  161. struct mantis_pci *mantis = from_tasklet(mantis, t, tasklet);
  162. struct mantis_hwconfig *config = mantis->hwconfig;
  163. while (mantis->last_block != mantis->busy_block) {
  164. dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]",
  165. mantis->last_block, mantis->busy_block);
  166. (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
  167. (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES);
  168. mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT;
  169. }
  170. }