dma-sh.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 <asm/dma-sh.h>
  21. #if defined(DMAE1_IRQ)
  22. #define NR_DMAE 2
  23. #else
  24. #define NR_DMAE 1
  25. #endif
  26. static const char *dmae_name[] = {
  27. "DMAC Address Error0", "DMAC Address Error1"
  28. };
  29. static inline unsigned int get_dmte_irq(unsigned int chan)
  30. {
  31. unsigned int irq = 0;
  32. if (chan < ARRAY_SIZE(dmte_irq_map))
  33. irq = dmte_irq_map[chan];
  34. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  35. if (irq > DMTE6_IRQ)
  36. return DMTE6_IRQ;
  37. return DMTE0_IRQ;
  38. #else
  39. return irq;
  40. #endif
  41. }
  42. /*
  43. * We determine the correct shift size based off of the CHCR transmit size
  44. * for the given channel. Since we know that it will take:
  45. *
  46. * info->count >> ts_shift[transmit_size]
  47. *
  48. * iterations to complete the transfer.
  49. */
  50. static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
  51. {
  52. u32 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
  53. return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
  54. }
  55. /*
  56. * The transfer end interrupt must read the chcr register to end the
  57. * hardware interrupt active condition.
  58. * Besides that it needs to waken any waiting process, which should handle
  59. * setting up the next transfer.
  60. */
  61. static irqreturn_t dma_tei(int irq, void *dev_id)
  62. {
  63. struct dma_channel *chan = dev_id;
  64. u32 chcr;
  65. chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
  66. if (!(chcr & CHCR_TE))
  67. return IRQ_NONE;
  68. chcr &= ~(CHCR_IE | CHCR_DE);
  69. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  70. wake_up(&chan->wait_queue);
  71. return IRQ_HANDLED;
  72. }
  73. static int sh_dmac_request_dma(struct dma_channel *chan)
  74. {
  75. if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
  76. return 0;
  77. return request_irq(get_dmte_irq(chan->chan), dma_tei,
  78. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  79. IRQF_SHARED,
  80. #else
  81. IRQF_DISABLED,
  82. #endif
  83. chan->dev_id, chan);
  84. }
  85. static void sh_dmac_free_dma(struct dma_channel *chan)
  86. {
  87. free_irq(get_dmte_irq(chan->chan), chan);
  88. }
  89. static int
  90. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  91. {
  92. if (!chcr)
  93. chcr = RS_DUAL | CHCR_IE;
  94. if (chcr & CHCR_IE) {
  95. chcr &= ~CHCR_IE;
  96. chan->flags |= DMA_TEI_CAPABLE;
  97. } else {
  98. chan->flags &= ~DMA_TEI_CAPABLE;
  99. }
  100. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  101. chan->flags |= DMA_CONFIGURED;
  102. return 0;
  103. }
  104. static void sh_dmac_enable_dma(struct dma_channel *chan)
  105. {
  106. int irq;
  107. u32 chcr;
  108. chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
  109. chcr |= CHCR_DE;
  110. if (chan->flags & DMA_TEI_CAPABLE)
  111. chcr |= CHCR_IE;
  112. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  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(dma_base_addr[chan->chan] + CHCR);
  127. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  128. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  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, (dma_base_addr[chan->chan]+SAR));
  157. if (chan->dar || (mach_is_dreamcast() &&
  158. chan->chan == PVR2_CASCADE_CHAN))
  159. ctrl_outl(chan->dar, (dma_base_addr[chan->chan] + DAR));
  160. ctrl_outl(chan->count >> calc_xmit_shift(chan),
  161. (dma_base_addr[chan->chan] + TCR));
  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(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
  168. return 0;
  169. return ctrl_inl(dma_base_addr[chan->chan] + TCR)
  170. << calc_xmit_shift(chan);
  171. }
  172. static inline int dmaor_reset(int no)
  173. {
  174. unsigned long dmaor = dmaor_read_reg(no);
  175. /* Try to clear the error flags first, incase they are set */
  176. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  177. dmaor_write_reg(no, dmaor);
  178. dmaor |= DMAOR_INIT;
  179. dmaor_write_reg(no, dmaor);
  180. /* See if we got an error again */
  181. if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
  182. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. #if defined(CONFIG_CPU_SH4)
  188. static irqreturn_t dma_err(int irq, void *dummy)
  189. {
  190. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  191. int cnt = 0;
  192. switch (irq) {
  193. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  194. case DMTE6_IRQ:
  195. cnt++;
  196. #endif
  197. case DMTE0_IRQ:
  198. if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
  199. disable_irq(irq);
  200. /* DMA multi and error IRQ */
  201. return IRQ_HANDLED;
  202. }
  203. default:
  204. return IRQ_NONE;
  205. }
  206. #else
  207. dmaor_reset(0);
  208. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  209. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  210. defined(CONFIG_CPU_SUBTYPE_SH7785)
  211. dmaor_reset(1);
  212. #endif
  213. disable_irq(irq);
  214. return IRQ_HANDLED;
  215. #endif
  216. }
  217. #endif
  218. static struct dma_ops sh_dmac_ops = {
  219. .request = sh_dmac_request_dma,
  220. .free = sh_dmac_free_dma,
  221. .get_residue = sh_dmac_get_dma_residue,
  222. .xfer = sh_dmac_xfer_dma,
  223. .configure = sh_dmac_configure_channel,
  224. };
  225. static struct dma_info sh_dmac_info = {
  226. .name = "sh_dmac",
  227. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  228. .ops = &sh_dmac_ops,
  229. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  230. };
  231. #ifdef CONFIG_CPU_SH4
  232. static unsigned int get_dma_error_irq(int n)
  233. {
  234. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  235. return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
  236. #else
  237. return (n == 0) ? DMAE0_IRQ :
  238. #if defined(DMAE1_IRQ)
  239. DMAE1_IRQ;
  240. #else
  241. -1;
  242. #endif
  243. #endif
  244. }
  245. #endif
  246. static int __init sh_dmac_init(void)
  247. {
  248. struct dma_info *info = &sh_dmac_info;
  249. int i;
  250. #ifdef CONFIG_CPU_SH4
  251. int n;
  252. for (n = 0; n < NR_DMAE; n++) {
  253. i = request_irq(get_dma_error_irq(n), dma_err,
  254. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  255. IRQF_SHARED,
  256. #else
  257. IRQF_DISABLED,
  258. #endif
  259. dmae_name[n], (void *)dmae_name[n]);
  260. if (unlikely(i < 0)) {
  261. printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
  262. return i;
  263. }
  264. }
  265. #endif /* CONFIG_CPU_SH4 */
  266. /*
  267. * Initialize DMAOR, and clean up any error flags that may have
  268. * been set.
  269. */
  270. i = dmaor_reset(0);
  271. if (unlikely(i != 0))
  272. return i;
  273. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  274. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  275. defined(CONFIG_CPU_SUBTYPE_SH7785)
  276. i = dmaor_reset(1);
  277. if (unlikely(i != 0))
  278. return i;
  279. #endif
  280. return register_dmac(info);
  281. }
  282. static void __exit sh_dmac_exit(void)
  283. {
  284. #ifdef CONFIG_CPU_SH4
  285. int n;
  286. for (n = 0; n < NR_DMAE; n++) {
  287. free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
  288. }
  289. #endif /* CONFIG_CPU_SH4 */
  290. unregister_dmac(&sh_dmac_info);
  291. }
  292. subsys_initcall(sh_dmac_init);
  293. module_exit(sh_dmac_exit);
  294. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  295. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  296. MODULE_LICENSE("GPL");