dma.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. *
  3. * BRIEF MODULE DESCRIPTION
  4. * A DMA channel allocator for Au1000. API is modeled loosely off of
  5. * linux/kernel/dma.c.
  6. *
  7. * Copyright 2000 MontaVista Software Inc.
  8. * Author: MontaVista Software, Inc.
  9. * stevel@mvista.com or source@mvista.com
  10. * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  20. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  23. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * You should have received a copy of the GNU General Public License along
  29. * with this program; if not, write to the Free Software Foundation, Inc.,
  30. * 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. */
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/errno.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/interrupt.h>
  38. #include <asm/mach-au1x00/au1000.h>
  39. #include <asm/mach-au1x00/au1000_dma.h>
  40. #if defined(CONFIG_SOC_AU1000) || defined(CONFIG_SOC_AU1500) || defined(CONFIG_SOC_AU1100)
  41. /*
  42. * A note on resource allocation:
  43. *
  44. * All drivers needing DMA channels, should allocate and release them
  45. * through the public routines `request_dma()' and `free_dma()'.
  46. *
  47. * In order to avoid problems, all processes should allocate resources in
  48. * the same sequence and release them in the reverse order.
  49. *
  50. * So, when allocating DMAs and IRQs, first allocate the DMA, then the IRQ.
  51. * When releasing them, first release the IRQ, then release the DMA. The
  52. * main reason for this order is that, if you are requesting the DMA buffer
  53. * done interrupt, you won't know the irq number until the DMA channel is
  54. * returned from request_dma.
  55. */
  56. DEFINE_SPINLOCK(au1000_dma_spin_lock);
  57. struct dma_chan au1000_dma_table[NUM_AU1000_DMA_CHANNELS] = {
  58. {.dev_id = -1,},
  59. {.dev_id = -1,},
  60. {.dev_id = -1,},
  61. {.dev_id = -1,},
  62. {.dev_id = -1,},
  63. {.dev_id = -1,},
  64. {.dev_id = -1,},
  65. {.dev_id = -1,}
  66. };
  67. EXPORT_SYMBOL(au1000_dma_table);
  68. // Device FIFO addresses and default DMA modes
  69. static const struct dma_dev {
  70. unsigned int fifo_addr;
  71. unsigned int dma_mode;
  72. } dma_dev_table[DMA_NUM_DEV] = {
  73. {UART0_ADDR + UART_TX, 0},
  74. {UART0_ADDR + UART_RX, 0},
  75. {0, 0},
  76. {0, 0},
  77. {AC97C_DATA, DMA_DW16 }, // coherent
  78. {AC97C_DATA, DMA_DR | DMA_DW16 }, // coherent
  79. {UART3_ADDR + UART_TX, DMA_DW8 | DMA_NC},
  80. {UART3_ADDR + UART_RX, DMA_DR | DMA_DW8 | DMA_NC},
  81. {USBD_EP0RD, DMA_DR | DMA_DW8 | DMA_NC},
  82. {USBD_EP0WR, DMA_DW8 | DMA_NC},
  83. {USBD_EP2WR, DMA_DW8 | DMA_NC},
  84. {USBD_EP3WR, DMA_DW8 | DMA_NC},
  85. {USBD_EP4RD, DMA_DR | DMA_DW8 | DMA_NC},
  86. {USBD_EP5RD, DMA_DR | DMA_DW8 | DMA_NC},
  87. {I2S_DATA, DMA_DW32 | DMA_NC},
  88. {I2S_DATA, DMA_DR | DMA_DW32 | DMA_NC}
  89. };
  90. int au1000_dma_read_proc(char *buf, char **start, off_t fpos,
  91. int length, int *eof, void *data)
  92. {
  93. int i, len = 0;
  94. struct dma_chan *chan;
  95. for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++) {
  96. if ((chan = get_dma_chan(i)) != NULL) {
  97. len += sprintf(buf + len, "%2d: %s\n",
  98. i, chan->dev_str);
  99. }
  100. }
  101. if (fpos >= len) {
  102. *start = buf;
  103. *eof = 1;
  104. return 0;
  105. }
  106. *start = buf + fpos;
  107. if ((len -= fpos) > length)
  108. return length;
  109. *eof = 1;
  110. return len;
  111. }
  112. // Device FIFO addresses and default DMA modes - 2nd bank
  113. static const struct dma_dev dma_dev_table_bank2[DMA_NUM_DEV_BANK2] = {
  114. {SD0_XMIT_FIFO, DMA_DS | DMA_DW8}, // coherent
  115. {SD0_RECV_FIFO, DMA_DS | DMA_DR | DMA_DW8}, // coherent
  116. {SD1_XMIT_FIFO, DMA_DS | DMA_DW8}, // coherent
  117. {SD1_RECV_FIFO, DMA_DS | DMA_DR | DMA_DW8} // coherent
  118. };
  119. void dump_au1000_dma_channel(unsigned int dmanr)
  120. {
  121. struct dma_chan *chan;
  122. if (dmanr >= NUM_AU1000_DMA_CHANNELS)
  123. return;
  124. chan = &au1000_dma_table[dmanr];
  125. printk(KERN_INFO "Au1000 DMA%d Register Dump:\n", dmanr);
  126. printk(KERN_INFO " mode = 0x%08x\n",
  127. au_readl(chan->io + DMA_MODE_SET));
  128. printk(KERN_INFO " addr = 0x%08x\n",
  129. au_readl(chan->io + DMA_PERIPHERAL_ADDR));
  130. printk(KERN_INFO " start0 = 0x%08x\n",
  131. au_readl(chan->io + DMA_BUFFER0_START));
  132. printk(KERN_INFO " start1 = 0x%08x\n",
  133. au_readl(chan->io + DMA_BUFFER1_START));
  134. printk(KERN_INFO " count0 = 0x%08x\n",
  135. au_readl(chan->io + DMA_BUFFER0_COUNT));
  136. printk(KERN_INFO " count1 = 0x%08x\n",
  137. au_readl(chan->io + DMA_BUFFER1_COUNT));
  138. }
  139. /*
  140. * Finds a free channel, and binds the requested device to it.
  141. * Returns the allocated channel number, or negative on error.
  142. * Requests the DMA done IRQ if irqhandler != NULL.
  143. */
  144. int request_au1000_dma(int dev_id, const char *dev_str,
  145. irq_handler_t irqhandler,
  146. unsigned long irqflags,
  147. void *irq_dev_id)
  148. {
  149. struct dma_chan *chan;
  150. const struct dma_dev *dev;
  151. int i, ret;
  152. #if defined(CONFIG_SOC_AU1100)
  153. if (dev_id < 0 || dev_id >= (DMA_NUM_DEV + DMA_NUM_DEV_BANK2))
  154. return -EINVAL;
  155. #else
  156. if (dev_id < 0 || dev_id >= DMA_NUM_DEV)
  157. return -EINVAL;
  158. #endif
  159. for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++) {
  160. if (au1000_dma_table[i].dev_id < 0)
  161. break;
  162. }
  163. if (i == NUM_AU1000_DMA_CHANNELS)
  164. return -ENODEV;
  165. chan = &au1000_dma_table[i];
  166. if (dev_id >= DMA_NUM_DEV) {
  167. dev_id -= DMA_NUM_DEV;
  168. dev = &dma_dev_table_bank2[dev_id];
  169. } else {
  170. dev = &dma_dev_table[dev_id];
  171. }
  172. if (irqhandler) {
  173. chan->irq = AU1000_DMA_INT_BASE + i;
  174. chan->irq_dev = irq_dev_id;
  175. if ((ret = request_irq(chan->irq, irqhandler, irqflags,
  176. dev_str, chan->irq_dev))) {
  177. chan->irq = 0;
  178. chan->irq_dev = NULL;
  179. return ret;
  180. }
  181. } else {
  182. chan->irq = 0;
  183. chan->irq_dev = NULL;
  184. }
  185. // fill it in
  186. chan->io = DMA_CHANNEL_BASE + i * DMA_CHANNEL_LEN;
  187. chan->dev_id = dev_id;
  188. chan->dev_str = dev_str;
  189. chan->fifo_addr = dev->fifo_addr;
  190. chan->mode = dev->dma_mode;
  191. /* initialize the channel before returning */
  192. init_dma(i);
  193. return i;
  194. }
  195. EXPORT_SYMBOL(request_au1000_dma);
  196. void free_au1000_dma(unsigned int dmanr)
  197. {
  198. struct dma_chan *chan = get_dma_chan(dmanr);
  199. if (!chan) {
  200. printk("Trying to free DMA%d\n", dmanr);
  201. return;
  202. }
  203. disable_dma(dmanr);
  204. if (chan->irq)
  205. free_irq(chan->irq, chan->irq_dev);
  206. chan->irq = 0;
  207. chan->irq_dev = NULL;
  208. chan->dev_id = -1;
  209. }
  210. EXPORT_SYMBOL(free_au1000_dma);
  211. #endif // AU1000 AU1500 AU1100