mantis_dma.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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() (mantis->risc_pos = 0)
  37. #define RISC_INSTR(opcode) (mantis->risc_cpu[mantis->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_BLOCK_COUNT (1 << 4)
  41. #define MANTIS_RISC_SIZE PAGE_SIZE
  42. int mantis_dma_exit(struct mantis_pci *mantis)
  43. {
  44. if (mantis->buf_cpu) {
  45. dprintk(MANTIS_ERROR, 1,
  46. "DMA=0x%lx cpu=0x%p size=%d",
  47. (unsigned long) mantis->buf_dma,
  48. mantis->buf_cpu,
  49. MANTIS_BUF_SIZE);
  50. pci_free_consistent(mantis->pdev, MANTIS_BUF_SIZE,
  51. mantis->buf_cpu, mantis->buf_dma);
  52. mantis->buf_cpu = NULL;
  53. }
  54. if (mantis->risc_cpu) {
  55. dprintk(MANTIS_ERROR, 1,
  56. "RISC=0x%lx cpu=0x%p size=%lx",
  57. (unsigned long) mantis->risc_dma,
  58. mantis->risc_cpu,
  59. MANTIS_RISC_SIZE);
  60. pci_free_consistent(mantis->pdev, MANTIS_RISC_SIZE,
  61. mantis->risc_cpu, mantis->risc_dma);
  62. mantis->risc_cpu = NULL;
  63. }
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(mantis_dma_exit);
  67. static inline int mantis_alloc_buffers(struct mantis_pci *mantis)
  68. {
  69. if (!mantis->buf_cpu) {
  70. mantis->buf_cpu = pci_alloc_consistent(mantis->pdev,
  71. MANTIS_BUF_SIZE,
  72. &mantis->buf_dma);
  73. if (!mantis->buf_cpu) {
  74. dprintk(MANTIS_ERROR, 1,
  75. "DMA buffer allocation failed");
  76. goto err;
  77. }
  78. dprintk(MANTIS_ERROR, 1,
  79. "DMA=0x%lx cpu=0x%p size=%d",
  80. (unsigned long) mantis->buf_dma,
  81. mantis->buf_cpu, MANTIS_BUF_SIZE);
  82. }
  83. if (!mantis->risc_cpu) {
  84. mantis->risc_cpu = pci_alloc_consistent(mantis->pdev,
  85. MANTIS_RISC_SIZE,
  86. &mantis->risc_dma);
  87. if (!mantis->risc_cpu) {
  88. dprintk(MANTIS_ERROR, 1,
  89. "RISC program allocation failed");
  90. mantis_dma_exit(mantis);
  91. goto err;
  92. }
  93. dprintk(MANTIS_ERROR, 1,
  94. "RISC=0x%lx cpu=0x%p size=%lx",
  95. (unsigned long) mantis->risc_dma,
  96. mantis->risc_cpu, MANTIS_RISC_SIZE);
  97. }
  98. return 0;
  99. err:
  100. dprintk(MANTIS_ERROR, 1, "Out of memory (?) .....");
  101. return -ENOMEM;
  102. }
  103. static inline int mantis_calc_lines(struct mantis_pci *mantis)
  104. {
  105. mantis->line_bytes = MANTIS_BLOCK_BYTES;
  106. mantis->line_count = MANTIS_BLOCK_COUNT;
  107. while (mantis->line_bytes > 4095) {
  108. mantis->line_bytes >>= 1;
  109. mantis->line_count <<= 1;
  110. }
  111. dprintk(MANTIS_DEBUG, 1, "Mantis RISC block bytes=[%d], line bytes=[%d], line count=[%d]",
  112. MANTIS_BLOCK_BYTES, mantis->line_bytes, mantis->line_count);
  113. if (mantis->line_count > 255) {
  114. dprintk(MANTIS_ERROR, 1, "Buffer size error");
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. int mantis_dma_init(struct mantis_pci *mantis)
  120. {
  121. int err = 0;
  122. dprintk(MANTIS_DEBUG, 1, "Mantis DMA init");
  123. if (mantis_alloc_buffers(mantis) < 0) {
  124. dprintk(MANTIS_ERROR, 1, "Error allocating DMA buffer");
  125. /* Stop RISC Engine */
  126. mmwrite(0, MANTIS_DMA_CTL);
  127. goto err;
  128. }
  129. err = mantis_calc_lines(mantis);
  130. if (err < 0) {
  131. dprintk(MANTIS_ERROR, 1, "Mantis calc lines failed");
  132. goto err;
  133. }
  134. return 0;
  135. err:
  136. return err;
  137. }
  138. EXPORT_SYMBOL_GPL(mantis_dma_init);
  139. static inline void mantis_risc_program(struct mantis_pci *mantis)
  140. {
  141. u32 buf_pos = 0;
  142. u32 line;
  143. dprintk(MANTIS_DEBUG, 1, "Mantis create RISC program");
  144. RISC_FLUSH();
  145. dprintk(MANTIS_DEBUG, 1, "risc len lines %u, bytes per line %u",
  146. mantis->line_count, mantis->line_bytes);
  147. for (line = 0; line < mantis->line_count; line++) {
  148. dprintk(MANTIS_DEBUG, 1, "RISC PROG line=[%d]", line);
  149. if (!(buf_pos % MANTIS_BLOCK_BYTES)) {
  150. RISC_INSTR(RISC_WRITE |
  151. RISC_IRQ |
  152. RISC_STATUS(((buf_pos / MANTIS_BLOCK_BYTES) +
  153. (MANTIS_BLOCK_COUNT - 1)) %
  154. MANTIS_BLOCK_COUNT) |
  155. mantis->line_bytes);
  156. } else {
  157. RISC_INSTR(RISC_WRITE | mantis->line_bytes);
  158. }
  159. RISC_INSTR(mantis->buf_dma + buf_pos);
  160. buf_pos += mantis->line_bytes;
  161. }
  162. RISC_INSTR(RISC_JUMP);
  163. RISC_INSTR(mantis->risc_dma);
  164. }
  165. void mantis_dma_start(struct mantis_pci *mantis)
  166. {
  167. dprintk(MANTIS_DEBUG, 1, "Mantis Start DMA engine");
  168. mantis_risc_program(mantis);
  169. mmwrite(mantis->risc_dma, MANTIS_RISC_START);
  170. mmwrite(mmread(MANTIS_GPIF_ADDR) | MANTIS_GPIF_HIFRDWRN, MANTIS_GPIF_ADDR);
  171. mmwrite(0, MANTIS_DMA_CTL);
  172. mantis->last_block = mantis->finished_block = 0;
  173. mmwrite(mmread(MANTIS_INT_MASK) | MANTIS_INT_RISCI, MANTIS_INT_MASK);
  174. mmwrite(MANTIS_FIFO_EN | MANTIS_DCAP_EN
  175. | MANTIS_RISC_EN, MANTIS_DMA_CTL);
  176. }
  177. void mantis_dma_stop(struct mantis_pci *mantis)
  178. {
  179. u32 stat = 0, mask = 0;
  180. stat = mmread(MANTIS_INT_STAT);
  181. mask = mmread(MANTIS_INT_MASK);
  182. dprintk(MANTIS_DEBUG, 1, "Mantis Stop DMA engine");
  183. mmwrite((mmread(MANTIS_GPIF_ADDR) & (~(MANTIS_GPIF_HIFRDWRN))), MANTIS_GPIF_ADDR);
  184. mmwrite((mmread(MANTIS_DMA_CTL) & ~(MANTIS_FIFO_EN |
  185. MANTIS_DCAP_EN |
  186. MANTIS_RISC_EN)), MANTIS_DMA_CTL);
  187. mmwrite(mmread(MANTIS_INT_STAT), MANTIS_INT_STAT);
  188. mmwrite(mmread(MANTIS_INT_MASK) & ~(MANTIS_INT_RISCI |
  189. MANTIS_INT_RISCEN), MANTIS_INT_MASK);
  190. }
  191. void mantis_dma_xfer(unsigned long data)
  192. {
  193. struct mantis_pci *mantis = (struct mantis_pci *) data;
  194. struct mantis_hwconfig *config = mantis->hwconfig;
  195. while (mantis->last_block != mantis->finished_block) {
  196. dprintk(MANTIS_DEBUG, 1, "last block=[%d] finished block=[%d]",
  197. mantis->last_block, mantis->finished_block);
  198. (config->ts_size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
  199. (&mantis->demux, &mantis->buf_cpu[mantis->last_block * MANTIS_BLOCK_BYTES], MANTIS_BLOCK_BYTES);
  200. mantis->last_block = (mantis->last_block + 1) % MANTIS_BLOCK_COUNT;
  201. }
  202. }