dma-sh.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. * Copyright (C) 2005 Andriy Skulysh
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/config.h>
  15. #include <linux/init.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <asm/dreamcast/dma.h>
  20. #include <asm/signal.h>
  21. #include <asm/irq.h>
  22. #include <asm/dma.h>
  23. #include <asm/io.h>
  24. #include "dma-sh.h"
  25. static inline unsigned int get_dmte_irq(unsigned int chan)
  26. {
  27. unsigned int irq = 0;
  28. /*
  29. * Normally we could just do DMTE0_IRQ + chan outright, though in the
  30. * case of the 7751R, the DMTE IRQs for channels > 4 start right above
  31. * the SCIF
  32. */
  33. if (chan < 4) {
  34. irq = DMTE0_IRQ + chan;
  35. } else {
  36. #ifdef DMTE4_IRQ
  37. irq = DMTE4_IRQ + chan - 4;
  38. #endif
  39. }
  40. return irq;
  41. }
  42. /*
  43. * We determine the correct shift size based off of the CHCR transmit size
  44. * for the given channel. Since we know that it will take:
  45. *
  46. * info->count >> ts_shift[transmit_size]
  47. *
  48. * iterations to complete the transfer.
  49. */
  50. static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
  51. {
  52. u32 chcr = ctrl_inl(CHCR[chan->chan]);
  53. return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
  54. }
  55. /*
  56. * The transfer end interrupt must read the chcr register to end the
  57. * hardware interrupt active condition.
  58. * Besides that it needs to waken any waiting process, which should handle
  59. * setting up the next transfer.
  60. */
  61. static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
  62. {
  63. struct dma_channel *chan = (struct dma_channel *)dev_id;
  64. u32 chcr;
  65. chcr = ctrl_inl(CHCR[chan->chan]);
  66. if (!(chcr & CHCR_TE))
  67. return IRQ_NONE;
  68. chcr &= ~(CHCR_IE | CHCR_DE);
  69. ctrl_outl(chcr, CHCR[chan->chan]);
  70. wake_up(&chan->wait_queue);
  71. return IRQ_HANDLED;
  72. }
  73. static int sh_dmac_request_dma(struct dma_channel *chan)
  74. {
  75. char name[32];
  76. snprintf(name, sizeof(name), "DMAC Transfer End (Channel %d)",
  77. chan->chan);
  78. return request_irq(get_dmte_irq(chan->chan), dma_tei,
  79. SA_INTERRUPT, name, chan);
  80. }
  81. static void sh_dmac_free_dma(struct dma_channel *chan)
  82. {
  83. free_irq(get_dmte_irq(chan->chan), chan);
  84. }
  85. static void
  86. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  87. {
  88. if (!chcr)
  89. chcr = RS_DUAL | CHCR_IE;
  90. if (chcr & CHCR_IE) {
  91. chcr &= ~CHCR_IE;
  92. chan->flags |= DMA_TEI_CAPABLE;
  93. } else {
  94. chan->flags &= ~DMA_TEI_CAPABLE;
  95. }
  96. ctrl_outl(chcr, CHCR[chan->chan]);
  97. chan->flags |= DMA_CONFIGURED;
  98. }
  99. static void sh_dmac_enable_dma(struct dma_channel *chan)
  100. {
  101. int irq;
  102. u32 chcr;
  103. chcr = ctrl_inl(CHCR[chan->chan]);
  104. chcr |= CHCR_DE;
  105. if (chan->flags & DMA_TEI_CAPABLE)
  106. chcr |= CHCR_IE;
  107. ctrl_outl(chcr, CHCR[chan->chan]);
  108. if (chan->flags & DMA_TEI_CAPABLE) {
  109. irq = get_dmte_irq(chan->chan);
  110. enable_irq(irq);
  111. }
  112. }
  113. static void sh_dmac_disable_dma(struct dma_channel *chan)
  114. {
  115. int irq;
  116. u32 chcr;
  117. if (chan->flags & DMA_TEI_CAPABLE) {
  118. irq = get_dmte_irq(chan->chan);
  119. disable_irq(irq);
  120. }
  121. chcr = ctrl_inl(CHCR[chan->chan]);
  122. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  123. ctrl_outl(chcr, CHCR[chan->chan]);
  124. }
  125. static int sh_dmac_xfer_dma(struct dma_channel *chan)
  126. {
  127. /*
  128. * If we haven't pre-configured the channel with special flags, use
  129. * the defaults.
  130. */
  131. if (unlikely(!(chan->flags & DMA_CONFIGURED)))
  132. sh_dmac_configure_channel(chan, 0);
  133. sh_dmac_disable_dma(chan);
  134. /*
  135. * Single-address mode usage note!
  136. *
  137. * It's important that we don't accidentally write any value to SAR/DAR
  138. * (this includes 0) that hasn't been directly specified by the user if
  139. * we're in single-address mode.
  140. *
  141. * In this case, only one address can be defined, anything else will
  142. * result in a DMA address error interrupt (at least on the SH-4),
  143. * which will subsequently halt the transfer.
  144. *
  145. * Channel 2 on the Dreamcast is a special case, as this is used for
  146. * cascading to the PVR2 DMAC. In this case, we still need to write
  147. * SAR and DAR, regardless of value, in order for cascading to work.
  148. */
  149. if (chan->sar || (mach_is_dreamcast() &&
  150. chan->chan == PVR2_CASCADE_CHAN))
  151. ctrl_outl(chan->sar, SAR[chan->chan]);
  152. if (chan->dar || (mach_is_dreamcast() &&
  153. chan->chan == PVR2_CASCADE_CHAN))
  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. #ifdef CONFIG_CPU_SUBTYPE_SH7780
  166. #define dmaor_read_reg() ctrl_inw(DMAOR)
  167. #define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
  168. #else
  169. #define dmaor_read_reg() ctrl_inl(DMAOR)
  170. #define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
  171. #endif
  172. static inline int dmaor_reset(void)
  173. {
  174. unsigned long dmaor = dmaor_read_reg();
  175. /* Try to clear the error flags first, incase they are set */
  176. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  177. dmaor_write_reg(dmaor);
  178. dmaor |= DMAOR_INIT;
  179. dmaor_write_reg(dmaor);
  180. /* See if we got an error again */
  181. if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
  182. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. #if defined(CONFIG_CPU_SH4)
  188. static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
  189. {
  190. dmaor_reset();
  191. disable_irq(irq);
  192. return IRQ_HANDLED;
  193. }
  194. #endif
  195. static struct dma_ops sh_dmac_ops = {
  196. .request = sh_dmac_request_dma,
  197. .free = sh_dmac_free_dma,
  198. .get_residue = sh_dmac_get_dma_residue,
  199. .xfer = sh_dmac_xfer_dma,
  200. .configure = sh_dmac_configure_channel,
  201. };
  202. static struct dma_info sh_dmac_info = {
  203. .name = "sh_dmac",
  204. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  205. .ops = &sh_dmac_ops,
  206. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  207. };
  208. static int __init sh_dmac_init(void)
  209. {
  210. struct dma_info *info = &sh_dmac_info;
  211. int i;
  212. #ifdef CONFIG_CPU_SH4
  213. make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  214. i = request_irq(DMAE_IRQ, dma_err, SA_INTERRUPT, "DMAC Address Error", 0);
  215. if (i < 0)
  216. return i;
  217. #endif
  218. for (i = 0; i < info->nr_channels; i++) {
  219. int irq = get_dmte_irq(i);
  220. make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  221. }
  222. /*
  223. * Initialize DMAOR, and clean up any error flags that may have
  224. * been set.
  225. */
  226. i = dmaor_reset();
  227. if (i < 0)
  228. return i;
  229. return register_dmac(info);
  230. }
  231. static void __exit sh_dmac_exit(void)
  232. {
  233. #ifdef CONFIG_CPU_SH4
  234. free_irq(DMAE_IRQ, 0);
  235. #endif
  236. unregister_dmac(&sh_dmac_info);
  237. }
  238. subsys_initcall(sh_dmac_init);
  239. module_exit(sh_dmac_exit);
  240. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  241. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  242. MODULE_LICENSE("GPL");