dma-sh.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * arch/sh/drivers/dma/dma-sh.c
  3. *
  4. * SuperH On-chip DMAC Support
  5. *
  6. * Copyright (C) 2000 Takashi YOSHII
  7. * Copyright (C) 2003, 2004 Paul Mundt
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/config.h>
  14. #include <linux/init.h>
  15. #include <linux/irq.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <asm/signal.h>
  19. #include <asm/irq.h>
  20. #include <asm/dma.h>
  21. #include <asm/io.h>
  22. #include "dma-sh.h"
  23. /*
  24. * The SuperH DMAC supports a number of transmit sizes, we list them here,
  25. * with their respective values as they appear in the CHCR registers.
  26. *
  27. * Defaults to a 64-bit transfer size.
  28. */
  29. enum {
  30. XMIT_SZ_64BIT,
  31. XMIT_SZ_8BIT,
  32. XMIT_SZ_16BIT,
  33. XMIT_SZ_32BIT,
  34. XMIT_SZ_256BIT,
  35. };
  36. /*
  37. * The DMA count is defined as the number of bytes to transfer.
  38. */
  39. static unsigned int ts_shift[] = {
  40. [XMIT_SZ_64BIT] = 3,
  41. [XMIT_SZ_8BIT] = 0,
  42. [XMIT_SZ_16BIT] = 1,
  43. [XMIT_SZ_32BIT] = 2,
  44. [XMIT_SZ_256BIT] = 5,
  45. };
  46. static inline unsigned int get_dmte_irq(unsigned int chan)
  47. {
  48. unsigned int irq;
  49. /*
  50. * Normally we could just do DMTE0_IRQ + chan outright, though in the
  51. * case of the 7751R, the DMTE IRQs for channels > 4 start right above
  52. * the SCIF
  53. */
  54. if (chan < 4) {
  55. irq = DMTE0_IRQ + chan;
  56. } else {
  57. irq = DMTE4_IRQ + chan - 4;
  58. }
  59. return irq;
  60. }
  61. /*
  62. * We determine the correct shift size based off of the CHCR transmit size
  63. * for the given channel. Since we know that it will take:
  64. *
  65. * info->count >> ts_shift[transmit_size]
  66. *
  67. * iterations to complete the transfer.
  68. */
  69. static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
  70. {
  71. u32 chcr = ctrl_inl(CHCR[chan->chan]);
  72. chcr >>= 4;
  73. return ts_shift[chcr & 0x0007];
  74. }
  75. /*
  76. * The transfer end interrupt must read the chcr register to end the
  77. * hardware interrupt active condition.
  78. * Besides that it needs to waken any waiting process, which should handle
  79. * setting up the next transfer.
  80. */
  81. static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
  82. {
  83. struct dma_channel *chan = (struct dma_channel *)dev_id;
  84. u32 chcr;
  85. chcr = ctrl_inl(CHCR[chan->chan]);
  86. if (!(chcr & CHCR_TE))
  87. return IRQ_NONE;
  88. chcr &= ~(CHCR_IE | CHCR_DE);
  89. ctrl_outl(chcr, CHCR[chan->chan]);
  90. wake_up(&chan->wait_queue);
  91. return IRQ_HANDLED;
  92. }
  93. static int sh_dmac_request_dma(struct dma_channel *chan)
  94. {
  95. return request_irq(get_dmte_irq(chan->chan), dma_tei,
  96. SA_INTERRUPT, "DMAC Transfer End", chan);
  97. }
  98. static void sh_dmac_free_dma(struct dma_channel *chan)
  99. {
  100. free_irq(get_dmte_irq(chan->chan), chan);
  101. }
  102. static void sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  103. {
  104. if (!chcr)
  105. chcr = RS_DUAL;
  106. ctrl_outl(chcr, CHCR[chan->chan]);
  107. chan->flags |= DMA_CONFIGURED;
  108. }
  109. static void sh_dmac_enable_dma(struct dma_channel *chan)
  110. {
  111. int irq = get_dmte_irq(chan->chan);
  112. u32 chcr;
  113. chcr = ctrl_inl(CHCR[chan->chan]);
  114. chcr |= CHCR_DE | CHCR_IE;
  115. ctrl_outl(chcr, CHCR[chan->chan]);
  116. enable_irq(irq);
  117. }
  118. static void sh_dmac_disable_dma(struct dma_channel *chan)
  119. {
  120. int irq = get_dmte_irq(chan->chan);
  121. u32 chcr;
  122. disable_irq(irq);
  123. chcr = ctrl_inl(CHCR[chan->chan]);
  124. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  125. ctrl_outl(chcr, CHCR[chan->chan]);
  126. }
  127. static int sh_dmac_xfer_dma(struct dma_channel *chan)
  128. {
  129. /*
  130. * If we haven't pre-configured the channel with special flags, use
  131. * the defaults.
  132. */
  133. if (!(chan->flags & DMA_CONFIGURED))
  134. sh_dmac_configure_channel(chan, 0);
  135. sh_dmac_disable_dma(chan);
  136. /*
  137. * Single-address mode usage note!
  138. *
  139. * It's important that we don't accidentally write any value to SAR/DAR
  140. * (this includes 0) that hasn't been directly specified by the user if
  141. * we're in single-address mode.
  142. *
  143. * In this case, only one address can be defined, anything else will
  144. * result in a DMA address error interrupt (at least on the SH-4),
  145. * which will subsequently halt the transfer.
  146. *
  147. * Channel 2 on the Dreamcast is a special case, as this is used for
  148. * cascading to the PVR2 DMAC. In this case, we still need to write
  149. * SAR and DAR, regardless of value, in order for cascading to work.
  150. */
  151. if (chan->sar || (mach_is_dreamcast() && chan->chan == 2))
  152. ctrl_outl(chan->sar, SAR[chan->chan]);
  153. if (chan->dar || (mach_is_dreamcast() && chan->chan == 2))
  154. ctrl_outl(chan->dar, DAR[chan->chan]);
  155. ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
  156. sh_dmac_enable_dma(chan);
  157. return 0;
  158. }
  159. static int sh_dmac_get_dma_residue(struct dma_channel *chan)
  160. {
  161. if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
  162. return 0;
  163. return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
  164. }
  165. #if defined(CONFIG_CPU_SH4)
  166. static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
  167. {
  168. unsigned long dmaor = ctrl_inl(DMAOR);
  169. printk("DMAE: DMAOR=%lx\n", dmaor);
  170. ctrl_outl(ctrl_inl(DMAOR)&~DMAOR_NMIF, DMAOR);
  171. ctrl_outl(ctrl_inl(DMAOR)&~DMAOR_AE, DMAOR);
  172. ctrl_outl(ctrl_inl(DMAOR)|DMAOR_DME, DMAOR);
  173. disable_irq(irq);
  174. return IRQ_HANDLED;
  175. }
  176. #endif
  177. static struct dma_ops sh_dmac_ops = {
  178. .request = sh_dmac_request_dma,
  179. .free = sh_dmac_free_dma,
  180. .get_residue = sh_dmac_get_dma_residue,
  181. .xfer = sh_dmac_xfer_dma,
  182. .configure = sh_dmac_configure_channel,
  183. };
  184. static struct dma_info sh_dmac_info = {
  185. .name = "SuperH DMAC",
  186. .nr_channels = 4,
  187. .ops = &sh_dmac_ops,
  188. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  189. };
  190. static int __init sh_dmac_init(void)
  191. {
  192. struct dma_info *info = &sh_dmac_info;
  193. int i;
  194. #ifdef CONFIG_CPU_SH4
  195. make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  196. i = request_irq(DMAE_IRQ, dma_err, SA_INTERRUPT, "DMAC Address Error", 0);
  197. if (i < 0)
  198. return i;
  199. #endif
  200. for (i = 0; i < info->nr_channels; i++) {
  201. int irq = get_dmte_irq(i);
  202. make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  203. }
  204. ctrl_outl(0x8000 | DMAOR_DME, DMAOR);
  205. return register_dmac(info);
  206. }
  207. static void __exit sh_dmac_exit(void)
  208. {
  209. #ifdef CONFIG_CPU_SH4
  210. free_irq(DMAE_IRQ, 0);
  211. #endif
  212. }
  213. subsys_initcall(sh_dmac_init);
  214. module_exit(sh_dmac_exit);
  215. MODULE_LICENSE("GPL");