dma-sh.c 7.3 KB

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