dma-sh.c 6.8 KB

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