mantis_dma.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <asm/page.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/pci.h>
  20. #include <asm/irq.h>
  21. #include <linux/signal.h>
  22. #include <linux/sched.h>
  23. #include <linux/interrupt.h>
  24. #include "dmxdev.h"
  25. #include "dvbdev.h"
  26. #include "dvb_demux.h"
  27. #include "dvb_frontend.h"
  28. #include "dvb_net.h"
  29. #include "mantis_common.h"
  30. #include "mantis_reg.h"
  31. #include "mantis_dma.h"
  32. #define RISC_WRITE (0x01 << 28)
  33. #define RISC_JUMP (0x07 << 28)
  34. #define RISC_IRQ (0x01 << 24)
  35. #define RISC_STATUS(status) ((((~status) & 0x0f) << 20) | ((status & 0x0f) << 16))
  36. #define RISC_FLUSH(risc_pos) (risc_pos = 0)
  37. #define RISC_INSTR(risc_pos, opcode) (mantis->risc_cpu[risc_pos++] = cpu_to_le32(opcode))
  38. #define MANTIS_BUF_SIZE (64 * 1024)
  39. #define MANTIS_BLOCK_BYTES (MANTIS_BUF_SIZE / 4)
  40. #define MANTIS_DMA_TR_BYTES (2 * 1024) /* upper limit: 4095 bytes. */
  41. #define MANTIS_BLOCK_COUNT (MANTIS_BUF_SIZE / MANTIS_BLOCK_BYTES)
  42. #define MANTIS_DMA_TR_UNITS (MANTIS_BLOCK_BYTES / MANTIS_DMA_TR_BYTES)
  43. /* MANTIS_BUF_SIZE / MANTIS_DMA_TR_UNITS must not exceed MANTIS_RISC_SIZE (4k RISC cmd buffer) */
  44. #define MANTIS_RISC_SIZE PAGE_SIZE /* RISC program must fit here. */
  45. int mantis_dma_exit(struct mantis_pci *mantis)
  46. {
  47. if (mantis->buf_cpu) {
  48. dprintk(MANTIS_ERROR, 1,
  49. "DMA=0x%lx cpu=0x%p size=%d",
  50. (unsigned long) mantis->buf_dma,
  51. mantis->buf_cpu,
  52. MANTIS_BUF_SIZE);
  53. pci_free_consistent(mantis->pdev, MANTIS_BUF_SIZE,
  54. mantis->buf_cpu, mantis->buf_dma);
  55. mantis->buf_cpu = NULL;
  56. }
  57. if (mantis->risc_cpu) {
  58. dprintk(MANTIS_ERROR, 1,
  59. "RISC=0x%lx cpu=0x%p size=%lx",
  60. (unsigned long) mantis->risc_dma,
  61. mantis->risc_cpu,
  62. MANTIS_RISC_SIZE);
  63. pci_free_consistent(mantis->pdev, MANTIS_RISC_SIZE,
  64. mantis->risc_cpu, mantis->risc_dma);
  65. mantis->risc_cpu = NULL;
  66. }
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(mantis_dma_exit);
  70. static inline int mantis_alloc_buffers(struct mantis_pci *mantis)
  71. {
  72. if (!mantis->buf_cpu) {
  73. mantis->buf_cpu = pci_alloc_consistent(mantis->pdev,
  74. MANTIS_BUF_SIZE,
  75. &mantis->buf_dma);
  76. if (!mantis->buf_cpu) {
  77. dprintk(MANTIS_ERROR, 1,
  78. "DMA buffer allocation failed");
  79. goto err;
  80. }
  81. dprintk(MANTIS_ERROR, 1,
  82. "DMA=0x%lx cpu=0x%p size=%d",
  83. (unsigned long) mantis->buf_dma,
  84. mantis->buf_cpu, MANTIS_BUF_SIZE);
  85. }
  86. if (!mantis->risc_cpu) {
  87. mantis->risc_cpu = pci_alloc_consistent(mantis->pdev,
  88. MANTIS_RISC_SIZE,
  89. &mantis->risc_dma);
  90. if (!mantis->risc_cpu) {
  91. dprintk(MANTIS_ERROR, 1,
  92. "RISC program allocation failed");
  93. mantis_dma_exit(mantis);
  94. goto err;
  95. }
  96. dprintk(MANTIS_ERROR, 1,
  97. "RISC=0x%lx cpu=0x%p size=%lx",
  98. (unsigned long) mantis->risc_dma,
  99. mantis->risc_cpu, MANTIS_RISC_SIZE);
  100. }
  101. return 0;
  102. err:
  103. dprintk(MANTIS_ERROR, 1, "Out of memory (?) .....");
  104. return -ENOMEM;
  105. }
  106. int mantis_dma_init(struct mantis_pci *mantis)
  107. {
  108. int err = 0;
  109. dprintk(MANTIS_DEBUG, 1, "Mantis DMA init");
  110. if (mantis_alloc_buffers(mantis) < 0) {
  111. dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer");
  112. /* Stop RISC Engine */
  113. mmwrite(0, MANTIS_DMA_CTL);
  114. goto err;
  115. }
  116. return 0;
  117. err:
  118. return err;
  119. }
  120. EXPORT_SYMBOL_GPL(mantis_dma_init);
  121. static inline void mantis_risc_program(struct mantis_pci *mantis)
  122. {
  123. u32 buf_pos = 0;
  124. u32 line, step;
  125. u32 risc_pos;
  126. dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program");
  127. RISC_FLUSH(risc_pos);
  128. dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u, bytes per DMA tr %u",
  129. MANTIS_BLOCK_COUNT, MANTIS_BLOCK_BYTES, MANTIS_DMA_TR_BYTES);
  130. for (line = 0; line < MANTIS_BLOCK_COUNT; line++) {
  131. for (step = 0; step < MANTIS_DMA_TR_UNITS; step++) {
  132. dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d], step=[%d]", line, step);
  133. if (step == 0) {
  134. RISC_INSTR(risc_pos, RISC_WRITE |
  135. RISC_IRQ |
  136. RISC_STATUS(line) |
  137. MANTIS_DMA_TR_BYTES);
  138. } else {
  139. RISC_INSTR(risc_pos, RISC_WRITE | MANTIS_DMA_TR_BYTES);
  140. }
  141. RISC_INSTR(risc_pos, mantis->buf_dma + buf_pos);
  142. buf_pos += MANTIS_DMA_TR_BYTES;
  143. }
  144. }
  145. RISC_INSTR(risc_pos, RISC_JUMP);
  146. RISC_INSTR(risc_pos, mantis->risc_dma);
  147. }
  148. void mantis_dma_start(struct mantis_pci *mantis)
  149. {
  150. dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine");
  151. mantis_risc_program(mantis);
  152. mmwrite(mantis->risc_dma, MANTIS_RISC_START);
  153. mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
  154. mmwrite(0, MANTIS_DMA_CTL);
  155. mantis->last_block = mantis->busy_block = 0;
  156. mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_RISCI, MANTIS_INT_MASK);
  157. mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN
  158. | MANTIS_RISC_EN, MANTIS_DMA_CTL);
  159. }
  160. void mantis_dma_stop(struct mantis_pci *mantis)
  161. {
  162. u32 stat = 0, mask = 0;
  163. stat = mmread(MANTIS_INT_STAT);
  164. mask = mmread(MANTIS_INT_MASK);
  165. dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine");
  166. mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR);
  167. mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN |
  168. MANTIS_DCAP_EN |
  169. MANTIS_RISC_EN)), MANTIS_DMA_CTL);
  170. mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT);
  171. mmwrite(mmread(MANTIS_INT_MASK) & ~(MANTIS_INT_RISCI |
  172. MANTIS_INT_RISCEN), MANTIS_INT_MASK);
  173. }
  174. void mantis_dma_xfer(unsigned long data)
  175. {
  176. struct mantis_pci *mantis = (struct mantis_pci *) data;
  177. struct mantis_hwconfig *config = mantis->hwconfig;
  178. while (mantis->last_block != mantis->busy_block) {
  179. dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]",
  180. mantis->last_block, mantis->busy_block);
  181. (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
  182. (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES);
  183. mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT;
  184. }
  185. }