dma-sh.c 6.7 KB

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