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/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)
  58. {
  59. struct dma_channel *chan = 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. if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
  72. return 0;
  73. chan->name = kzalloc(32, GFP_KERNEL);
  74. if (unlikely(chan->name == NULL))
  75. return -ENOMEM;
  76. snprintf(chan->name, 32, "DMAC Transfer End (Channel %d)",
  77. chan->chan);
  78. return request_irq(get_dmte_irq(chan->chan), dma_tei,
  79. IRQF_DISABLED, chan->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. kfree(chan->name);
  85. }
  86. static void
  87. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  88. {
  89. if (!chcr)
  90. chcr = RS_DUAL | CHCR_IE;
  91. if (chcr & CHCR_IE) {
  92. chcr &= ~CHCR_IE;
  93. chan->flags |= DMA_TEI_CAPABLE;
  94. } else {
  95. chan->flags &= ~DMA_TEI_CAPABLE;
  96. }
  97. ctrl_outl(chcr, CHCR[chan->chan]);
  98. chan->flags |= DMA_CONFIGURED;
  99. }
  100. static void sh_dmac_enable_dma(struct dma_channel *chan)
  101. {
  102. int irq;
  103. u32 chcr;
  104. chcr = ctrl_inl(CHCR[chan->chan]);
  105. chcr |= CHCR_DE;
  106. if (chan->flags & DMA_TEI_CAPABLE)
  107. chcr |= CHCR_IE;
  108. ctrl_outl(chcr, CHCR[chan->chan]);
  109. if (chan->flags & DMA_TEI_CAPABLE) {
  110. irq = get_dmte_irq(chan->chan);
  111. enable_irq(irq);
  112. }
  113. }
  114. static void sh_dmac_disable_dma(struct dma_channel *chan)
  115. {
  116. int irq;
  117. u32 chcr;
  118. if (chan->flags & DMA_TEI_CAPABLE) {
  119. irq = get_dmte_irq(chan->chan);
  120. disable_irq(irq);
  121. }
  122. chcr = ctrl_inl(CHCR[chan->chan]);
  123. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  124. ctrl_outl(chcr, CHCR[chan->chan]);
  125. }
  126. static int sh_dmac_xfer_dma(struct dma_channel *chan)
  127. {
  128. /*
  129. * If we haven't pre-configured the channel with special flags, use
  130. * the defaults.
  131. */
  132. if (unlikely(!(chan->flags & DMA_CONFIGURED)))
  133. sh_dmac_configure_channel(chan, 0);
  134. sh_dmac_disable_dma(chan);
  135. /*
  136. * Single-address mode usage note!
  137. *
  138. * It's important that we don't accidentally write any value to SAR/DAR
  139. * (this includes 0) that hasn't been directly specified by the user if
  140. * we're in single-address mode.
  141. *
  142. * In this case, only one address can be defined, anything else will
  143. * result in a DMA address error interrupt (at least on the SH-4),
  144. * which will subsequently halt the transfer.
  145. *
  146. * Channel 2 on the Dreamcast is a special case, as this is used for
  147. * cascading to the PVR2 DMAC. In this case, we still need to write
  148. * SAR and DAR, regardless of value, in order for cascading to work.
  149. */
  150. if (chan->sar || (mach_is_dreamcast() &&
  151. chan->chan == PVR2_CASCADE_CHAN))
  152. ctrl_outl(chan->sar, SAR[chan->chan]);
  153. if (chan->dar || (mach_is_dreamcast() &&
  154. chan->chan == PVR2_CASCADE_CHAN))
  155. ctrl_outl(chan->dar, DAR[chan->chan]);
  156. ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
  157. sh_dmac_enable_dma(chan);
  158. return 0;
  159. }
  160. static int sh_dmac_get_dma_residue(struct dma_channel *chan)
  161. {
  162. if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
  163. return 0;
  164. return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
  165. }
  166. #ifdef CONFIG_CPU_SUBTYPE_SH7780
  167. #define dmaor_read_reg() ctrl_inw(DMAOR)
  168. #define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
  169. #else
  170. #define dmaor_read_reg() ctrl_inl(DMAOR)
  171. #define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
  172. #endif
  173. static inline int dmaor_reset(void)
  174. {
  175. unsigned long dmaor = dmaor_read_reg();
  176. /* Try to clear the error flags first, incase they are set */
  177. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  178. dmaor_write_reg(dmaor);
  179. dmaor |= DMAOR_INIT;
  180. dmaor_write_reg(dmaor);
  181. /* See if we got an error again */
  182. if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
  183. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  184. return -EINVAL;
  185. }
  186. return 0;
  187. }
  188. #if defined(CONFIG_CPU_SH4)
  189. static irqreturn_t dma_err(int irq, void *dummy)
  190. {
  191. dmaor_reset();
  192. disable_irq(irq);
  193. return IRQ_HANDLED;
  194. }
  195. #endif
  196. static struct dma_ops sh_dmac_ops = {
  197. .request = sh_dmac_request_dma,
  198. .free = sh_dmac_free_dma,
  199. .get_residue = sh_dmac_get_dma_residue,
  200. .xfer = sh_dmac_xfer_dma,
  201. .configure = sh_dmac_configure_channel,
  202. };
  203. static struct dma_info sh_dmac_info = {
  204. .name = "sh_dmac",
  205. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  206. .ops = &sh_dmac_ops,
  207. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  208. };
  209. static int __init sh_dmac_init(void)
  210. {
  211. struct dma_info *info = &sh_dmac_info;
  212. int i;
  213. #ifdef CONFIG_CPU_SH4
  214. make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  215. i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
  216. if (unlikely(i < 0))
  217. return i;
  218. #endif
  219. for (i = 0; i < info->nr_channels; i++) {
  220. int irq = get_dmte_irq(i);
  221. make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
  222. }
  223. /*
  224. * Initialize DMAOR, and clean up any error flags that may have
  225. * been set.
  226. */
  227. i = dmaor_reset();
  228. if (unlikely(i != 0))
  229. return i;
  230. return register_dmac(info);
  231. }
  232. static void __exit sh_dmac_exit(void)
  233. {
  234. #ifdef CONFIG_CPU_SH4
  235. free_irq(DMAE_IRQ, 0);
  236. #endif
  237. unregister_dmac(&sh_dmac_info);
  238. }
  239. subsys_initcall(sh_dmac_init);
  240. module_exit(sh_dmac_exit);
  241. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  242. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  243. MODULE_LICENSE("GPL");