dma-sh.c 6.7 KB

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