mantis_dma.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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;
  109. dprintk(MANTIS_DEBUG, 1, "Mantis DMA init");
  110. err = mantis_alloc_buffers(mantis);
  111. if (err < 0) {
  112. dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer");
  113. /* Stop RISC Engine */
  114. mmwrite(0, MANTIS_DMA_CTL);
  115. return err;
  116. }
  117. return 0;
  118. }
  119. EXPORT_SYMBOL_GPL(mantis_dma_init);
  120. static inline void mantis_risc_program(struct mantis_pci *mantis)
  121. {
  122. u32 buf_pos = 0;
  123. u32 line, step;
  124. u32 risc_pos;
  125. dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program");
  126. RISC_FLUSH(risc_pos);
  127. dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u, bytes per DMA tr %u",
  128. MANTIS_BLOCK_COUNT, MANTIS_BLOCK_BYTES, MANTIS_DMA_TR_BYTES);
  129. for (line = 0; line < MANTIS_BLOCK_COUNT; line++) {
  130. for (step = 0; step < MANTIS_DMA_TR_UNITS; step++) {
  131. dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d], step=[%d]", line, step);
  132. if (step == 0) {
  133. RISC_INSTR(risc_pos, RISC_WRITE |
  134. RISC_IRQ |
  135. RISC_STATUS(line) |
  136. MANTIS_DMA_TR_BYTES);
  137. } else {
  138. RISC_INSTR(risc_pos, RISC_WRITE | MANTIS_DMA_TR_BYTES);
  139. }
  140. RISC_INSTR(risc_pos, mantis->buf_dma + buf_pos);
  141. buf_pos += MANTIS_DMA_TR_BYTES;
  142. }
  143. }
  144. RISC_INSTR(risc_pos, RISC_JUMP);
  145. RISC_INSTR(risc_pos, mantis->risc_dma);
  146. }
  147. void mantis_dma_start(struct mantis_pci *mantis)
  148. {
  149. dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine");
  150. mantis_risc_program(mantis);
  151. mmwrite(mantis->risc_dma, MANTIS_RISC_START);
  152. mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
  153. mmwrite(0, MANTIS_DMA_CTL);
  154. mantis->last_block = mantis->busy_block = 0;
  155. mantis_unmask_ints(mantis, MANTIS_INT_RISCI);
  156. mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN
  157. | MANTIS_RISC_EN, MANTIS_DMA_CTL);
  158. }
  159. void mantis_dma_stop(struct mantis_pci *mantis)
  160. {
  161. dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine");
  162. mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR);
  163. mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN |
  164. MANTIS_DCAP_EN |
  165. MANTIS_RISC_EN)), MANTIS_DMA_CTL);
  166. mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT);
  167. mantis_mask_ints(mantis, MANTIS_INT_RISCI | MANTIS_INT_RISCEN);
  168. }
  169. void mantis_dma_xfer(unsigned long data)
  170. {
  171. struct mantis_pci *mantis = (struct mantis_pci *) data;
  172. struct mantis_hwconfig *config = mantis->hwconfig;
  173. while (mantis->last_block != mantis->busy_block) {
  174. dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]",
  175. mantis->last_block, mantis->busy_block);
  176. (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
  177. (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES);
  178. mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT;
  179. }
  180. }