dma-sh.c 7.1 KB

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