mantis_dma.c 6.4 KB

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