dma.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. *
  3. * BRIEF MODULE DESCRIPTION
  4. * A DMA channel allocator for Au1x00. API is modeled loosely off of
  5. * linux/kernel/dma.c.
  6. *
  7. * Copyright 2000, 2008 MontaVista Software Inc.
  8. * Author: MontaVista Software, Inc. <source@mvista.com>
  9. * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  19. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 675 Mass Ave, Cambridge, MA 02139, USA.
  30. *
  31. */
  32. #include <linux/init.h>
  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. /*
  41. * A note on resource allocation:
  42. *
  43. * All drivers needing DMA channels, should allocate and release them
  44. * through the public routines `request_dma()' and `free_dma()'.
  45. *
  46. * In order to avoid problems, all processes should allocate resources in
  47. * the same sequence and release them in the reverse order.
  48. *
  49. * So, when allocating DMAs and IRQs, first allocate the DMA, then the IRQ.
  50. * When releasing them, first release the IRQ, then release the DMA. The
  51. * main reason for this order is that, if you are requesting the DMA buffer
  52. * done interrupt, you won't know the irq number until the DMA channel is
  53. * returned from request_dma.
  54. */
  55. /* DMA Channel register block spacing */
  56. #define DMA_CHANNEL_LEN 0x00000100
  57. DEFINE_SPINLOCK(au1000_dma_spin_lock);
  58. struct dma_chan au1000_dma_table[NUM_AU1000_DMA_CHANNELS] = {
  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. {.dev_id = -1,}
  67. };
  68. EXPORT_SYMBOL(au1000_dma_table);
  69. /* Device FIFO addresses and default DMA modes */
  70. static const struct dma_dev {
  71. unsigned int fifo_addr;
  72. unsigned int dma_mode;
  73. } dma_dev_table[DMA_NUM_DEV] = {
  74. { AU1000_UART0_PHYS_ADDR + 0x04, DMA_DW8 }, /* UART0_TX */
  75. { AU1000_UART0_PHYS_ADDR + 0x00, DMA_DW8 | DMA_DR }, /* UART0_RX */
  76. { 0, 0 }, /* DMA_REQ0 */
  77. { 0, 0 }, /* DMA_REQ1 */
  78. { AU1000_AC97_PHYS_ADDR + 0x08, DMA_DW16 }, /* AC97 TX c */
  79. { AU1000_AC97_PHYS_ADDR + 0x08, DMA_DW16 | DMA_DR }, /* AC97 RX c */
  80. { AU1000_UART3_PHYS_ADDR + 0x04, DMA_DW8 | DMA_NC }, /* UART3_TX */
  81. { AU1000_UART3_PHYS_ADDR + 0x00, DMA_DW8 | DMA_NC | DMA_DR }, /* UART3_RX */
  82. { AU1000_USB_UDC_PHYS_ADDR + 0x00, DMA_DW8 | DMA_NC | DMA_DR }, /* EP0RD */
  83. { AU1000_USB_UDC_PHYS_ADDR + 0x04, DMA_DW8 | DMA_NC }, /* EP0WR */
  84. { AU1000_USB_UDC_PHYS_ADDR + 0x08, DMA_DW8 | DMA_NC }, /* EP2WR */
  85. { AU1000_USB_UDC_PHYS_ADDR + 0x0c, DMA_DW8 | DMA_NC }, /* EP3WR */
  86. { AU1000_USB_UDC_PHYS_ADDR + 0x10, DMA_DW8 | DMA_NC | DMA_DR }, /* EP4RD */
  87. { AU1000_USB_UDC_PHYS_ADDR + 0x14, DMA_DW8 | DMA_NC | DMA_DR }, /* EP5RD */
  88. /* on Au1500, these 2 are DMA_REQ2/3 (GPIO208/209) instead! */
  89. { AU1000_I2S_PHYS_ADDR + 0x00, DMA_DW32 | DMA_NC}, /* I2S TX */
  90. { AU1000_I2S_PHYS_ADDR + 0x00, DMA_DW32 | DMA_NC | DMA_DR}, /* I2S RX */
  91. };
  92. int au1000_dma_read_proc(char *buf, char **start, off_t fpos,
  93. int length, int *eof, void *data)
  94. {
  95. int i, len = 0;
  96. struct dma_chan *chan;
  97. for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++) {
  98. chan = get_dma_chan(i);
  99. if (chan != NULL)
  100. len += sprintf(buf + len, "%2d: %s\n",
  101. i, chan->dev_str);
  102. }
  103. if (fpos >= len) {
  104. *start = buf;
  105. *eof = 1;
  106. return 0;
  107. }
  108. *start = buf + fpos;
  109. len -= fpos;
  110. if (len > length)
  111. return length;
  112. *eof = 1;
  113. return len;
  114. }
  115. /* Device FIFO addresses and default DMA modes - 2nd bank */
  116. static const struct dma_dev dma_dev_table_bank2[DMA_NUM_DEV_BANK2] = {
  117. { AU1100_SD0_PHYS_ADDR + 0x00, DMA_DS | DMA_DW8 }, /* coherent */
  118. { AU1100_SD0_PHYS_ADDR + 0x04, DMA_DS | DMA_DW8 | DMA_DR }, /* coherent */
  119. { AU1100_SD1_PHYS_ADDR + 0x00, DMA_DS | DMA_DW8 }, /* coherent */
  120. { AU1100_SD1_PHYS_ADDR + 0x04, DMA_DS | DMA_DW8 | DMA_DR } /* coherent */
  121. };
  122. void dump_au1000_dma_channel(unsigned int dmanr)
  123. {
  124. struct dma_chan *chan;
  125. if (dmanr >= NUM_AU1000_DMA_CHANNELS)
  126. return;
  127. chan = &au1000_dma_table[dmanr];
  128. printk(KERN_INFO "Au1000 DMA%d Register Dump:\n", dmanr);
  129. printk(KERN_INFO " mode = 0x%08x\n",
  130. __raw_readl(chan->io + DMA_MODE_SET));
  131. printk(KERN_INFO " addr = 0x%08x\n",
  132. __raw_readl(chan->io + DMA_PERIPHERAL_ADDR));
  133. printk(KERN_INFO " start0 = 0x%08x\n",
  134. __raw_readl(chan->io + DMA_BUFFER0_START));
  135. printk(KERN_INFO " start1 = 0x%08x\n",
  136. __raw_readl(chan->io + DMA_BUFFER1_START));
  137. printk(KERN_INFO " count0 = 0x%08x\n",
  138. __raw_readl(chan->io + DMA_BUFFER0_COUNT));
  139. printk(KERN_INFO " count1 = 0x%08x\n",
  140. __raw_readl(chan->io + DMA_BUFFER1_COUNT));
  141. }
  142. /*
  143. * Finds a free channel, and binds the requested device to it.
  144. * Returns the allocated channel number, or negative on error.
  145. * Requests the DMA done IRQ if irqhandler != NULL.
  146. */
  147. int request_au1000_dma(int dev_id, const char *dev_str,
  148. irq_handler_t irqhandler,
  149. unsigned long irqflags,
  150. void *irq_dev_id)
  151. {
  152. struct dma_chan *chan;
  153. const struct dma_dev *dev;
  154. int i, ret;
  155. if (alchemy_get_cputype() == ALCHEMY_CPU_AU1100) {
  156. if (dev_id < 0 || dev_id >= (DMA_NUM_DEV + DMA_NUM_DEV_BANK2))
  157. return -EINVAL;
  158. } else {
  159. if (dev_id < 0 || dev_id >= DMA_NUM_DEV)
  160. return -EINVAL;
  161. }
  162. for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
  163. if (au1000_dma_table[i].dev_id < 0)
  164. break;
  165. if (i == NUM_AU1000_DMA_CHANNELS)
  166. return -ENODEV;
  167. chan = &au1000_dma_table[i];
  168. if (dev_id >= DMA_NUM_DEV) {
  169. dev_id -= DMA_NUM_DEV;
  170. dev = &dma_dev_table_bank2[dev_id];
  171. } else
  172. dev = &dma_dev_table[dev_id];
  173. if (irqhandler) {
  174. chan->irq_dev = irq_dev_id;
  175. ret = request_irq(chan->irq, irqhandler, irqflags, dev_str,
  176. chan->irq_dev);
  177. if (ret) {
  178. chan->irq_dev = NULL;
  179. return ret;
  180. }
  181. } else {
  182. chan->irq_dev = NULL;
  183. }
  184. /* fill it in */
  185. chan->io = (void __iomem *)(KSEG1ADDR(AU1000_DMA_PHYS_ADDR) +
  186. 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(KERN_ERR "Error trying to free DMA%d\n", dmanr);
  201. return;
  202. }
  203. disable_dma(dmanr);
  204. if (chan->irq_dev)
  205. free_irq(chan->irq, chan->irq_dev);
  206. chan->irq_dev = NULL;
  207. chan->dev_id = -1;
  208. }
  209. EXPORT_SYMBOL(free_au1000_dma);
  210. static int __init au1000_dma_init(void)
  211. {
  212. int base, i;
  213. switch (alchemy_get_cputype()) {
  214. case ALCHEMY_CPU_AU1000:
  215. base = AU1000_DMA_INT_BASE;
  216. break;
  217. case ALCHEMY_CPU_AU1500:
  218. base = AU1500_DMA_INT_BASE;
  219. break;
  220. case ALCHEMY_CPU_AU1100:
  221. base = AU1100_DMA_INT_BASE;
  222. break;
  223. default:
  224. goto out;
  225. }
  226. for (i = 0; i < NUM_AU1000_DMA_CHANNELS; i++)
  227. au1000_dma_table[i].irq = base + i;
  228. printk(KERN_INFO "Alchemy DMA initialized\n");
  229. out:
  230. return 0;
  231. }
  232. arch_initcall(au1000_dma_init);