dma-sh.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 unsigned int ts_shift[] = TS_SHIFT;
  51. static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
  52. {
  53. u32 chcr = __raw_readl(dma_base_addr[chan->chan] + CHCR);
  54. int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
  55. ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
  56. return ts_shift[cnt];
  57. }
  58. /*
  59. * The transfer end interrupt must read the chcr register to end the
  60. * hardware interrupt active condition.
  61. * Besides that it needs to waken any waiting process, which should handle
  62. * setting up the next transfer.
  63. */
  64. static irqreturn_t dma_tei(int irq, void *dev_id)
  65. {
  66. struct dma_channel *chan = dev_id;
  67. u32 chcr;
  68. chcr = __raw_readl(dma_base_addr[chan->chan] + CHCR);
  69. if (!(chcr & CHCR_TE))
  70. return IRQ_NONE;
  71. chcr &= ~(CHCR_IE | CHCR_DE);
  72. __raw_writel(chcr, (dma_base_addr[chan->chan] + CHCR));
  73. wake_up(&chan->wait_queue);
  74. return IRQ_HANDLED;
  75. }
  76. static int sh_dmac_request_dma(struct dma_channel *chan)
  77. {
  78. if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
  79. return 0;
  80. return request_irq(get_dmte_irq(chan->chan), dma_tei,
  81. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  82. IRQF_SHARED,
  83. #else
  84. IRQF_DISABLED,
  85. #endif
  86. chan->dev_id, chan);
  87. }
  88. static void sh_dmac_free_dma(struct dma_channel *chan)
  89. {
  90. free_irq(get_dmte_irq(chan->chan), chan);
  91. }
  92. static int
  93. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  94. {
  95. if (!chcr)
  96. chcr = RS_DUAL | CHCR_IE;
  97. if (chcr & CHCR_IE) {
  98. chcr &= ~CHCR_IE;
  99. chan->flags |= DMA_TEI_CAPABLE;
  100. } else {
  101. chan->flags &= ~DMA_TEI_CAPABLE;
  102. }
  103. __raw_writel(chcr, (dma_base_addr[chan->chan] + CHCR));
  104. chan->flags |= DMA_CONFIGURED;
  105. return 0;
  106. }
  107. static void sh_dmac_enable_dma(struct dma_channel *chan)
  108. {
  109. int irq;
  110. u32 chcr;
  111. chcr = __raw_readl(dma_base_addr[chan->chan] + CHCR);
  112. chcr |= CHCR_DE;
  113. if (chan->flags & DMA_TEI_CAPABLE)
  114. chcr |= CHCR_IE;
  115. __raw_writel(chcr, (dma_base_addr[chan->chan] + CHCR));
  116. if (chan->flags & DMA_TEI_CAPABLE) {
  117. irq = get_dmte_irq(chan->chan);
  118. enable_irq(irq);
  119. }
  120. }
  121. static void sh_dmac_disable_dma(struct dma_channel *chan)
  122. {
  123. int irq;
  124. u32 chcr;
  125. if (chan->flags & DMA_TEI_CAPABLE) {
  126. irq = get_dmte_irq(chan->chan);
  127. disable_irq(irq);
  128. }
  129. chcr = __raw_readl(dma_base_addr[chan->chan] + CHCR);
  130. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  131. __raw_writel(chcr, (dma_base_addr[chan->chan] + CHCR));
  132. }
  133. static int sh_dmac_xfer_dma(struct dma_channel *chan)
  134. {
  135. /*
  136. * If we haven't pre-configured the channel with special flags, use
  137. * the defaults.
  138. */
  139. if (unlikely(!(chan->flags & DMA_CONFIGURED)))
  140. sh_dmac_configure_channel(chan, 0);
  141. sh_dmac_disable_dma(chan);
  142. /*
  143. * Single-address mode usage note!
  144. *
  145. * It's important that we don't accidentally write any value to SAR/DAR
  146. * (this includes 0) that hasn't been directly specified by the user if
  147. * we're in single-address mode.
  148. *
  149. * In this case, only one address can be defined, anything else will
  150. * result in a DMA address error interrupt (at least on the SH-4),
  151. * which will subsequently halt the transfer.
  152. *
  153. * Channel 2 on the Dreamcast is a special case, as this is used for
  154. * cascading to the PVR2 DMAC. In this case, we still need to write
  155. * SAR and DAR, regardless of value, in order for cascading to work.
  156. */
  157. if (chan->sar || (mach_is_dreamcast() &&
  158. chan->chan == PVR2_CASCADE_CHAN))
  159. __raw_writel(chan->sar, (dma_base_addr[chan->chan]+SAR));
  160. if (chan->dar || (mach_is_dreamcast() &&
  161. chan->chan == PVR2_CASCADE_CHAN))
  162. __raw_writel(chan->dar, (dma_base_addr[chan->chan] + DAR));
  163. __raw_writel(chan->count >> calc_xmit_shift(chan),
  164. (dma_base_addr[chan->chan] + TCR));
  165. sh_dmac_enable_dma(chan);
  166. return 0;
  167. }
  168. static int sh_dmac_get_dma_residue(struct dma_channel *chan)
  169. {
  170. if (!(__raw_readl(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
  171. return 0;
  172. return __raw_readl(dma_base_addr[chan->chan] + TCR)
  173. << calc_xmit_shift(chan);
  174. }
  175. static inline int dmaor_reset(int no)
  176. {
  177. unsigned long dmaor = dmaor_read_reg(no);
  178. /* Try to clear the error flags first, incase they are set */
  179. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  180. dmaor_write_reg(no, dmaor);
  181. dmaor |= DMAOR_INIT;
  182. dmaor_write_reg(no, dmaor);
  183. /* See if we got an error again */
  184. if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
  185. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  186. return -EINVAL;
  187. }
  188. return 0;
  189. }
  190. #if defined(CONFIG_CPU_SH4)
  191. static irqreturn_t dma_err(int irq, void *dummy)
  192. {
  193. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  194. int cnt = 0;
  195. switch (irq) {
  196. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  197. case DMTE6_IRQ:
  198. cnt++;
  199. #endif
  200. case DMTE0_IRQ:
  201. if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
  202. disable_irq(irq);
  203. /* DMA multi and error IRQ */
  204. return IRQ_HANDLED;
  205. }
  206. default:
  207. return IRQ_NONE;
  208. }
  209. #else
  210. dmaor_reset(0);
  211. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  212. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  213. defined(CONFIG_CPU_SUBTYPE_SH7785)
  214. dmaor_reset(1);
  215. #endif
  216. disable_irq(irq);
  217. return IRQ_HANDLED;
  218. #endif
  219. }
  220. #endif
  221. static struct dma_ops sh_dmac_ops = {
  222. .request = sh_dmac_request_dma,
  223. .free = sh_dmac_free_dma,
  224. .get_residue = sh_dmac_get_dma_residue,
  225. .xfer = sh_dmac_xfer_dma,
  226. .configure = sh_dmac_configure_channel,
  227. };
  228. static struct dma_info sh_dmac_info = {
  229. .name = "sh_dmac",
  230. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  231. .ops = &sh_dmac_ops,
  232. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  233. };
  234. #ifdef CONFIG_CPU_SH4
  235. static unsigned int get_dma_error_irq(int n)
  236. {
  237. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  238. return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
  239. #else
  240. return (n == 0) ? DMAE0_IRQ :
  241. #if defined(DMAE1_IRQ)
  242. DMAE1_IRQ;
  243. #else
  244. -1;
  245. #endif
  246. #endif
  247. }
  248. #endif
  249. static int __init sh_dmac_init(void)
  250. {
  251. struct dma_info *info = &sh_dmac_info;
  252. int i;
  253. #ifdef CONFIG_CPU_SH4
  254. int n;
  255. for (n = 0; n < NR_DMAE; n++) {
  256. i = request_irq(get_dma_error_irq(n), dma_err,
  257. #if defined(CONFIG_SH_DMA_IRQ_MULTI)
  258. IRQF_SHARED,
  259. #else
  260. IRQF_DISABLED,
  261. #endif
  262. dmae_name[n], (void *)dmae_name[n]);
  263. if (unlikely(i < 0)) {
  264. printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
  265. return i;
  266. }
  267. }
  268. #endif /* CONFIG_CPU_SH4 */
  269. /*
  270. * Initialize DMAOR, and clean up any error flags that may have
  271. * been set.
  272. */
  273. i = dmaor_reset(0);
  274. if (unlikely(i != 0))
  275. return i;
  276. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  277. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  278. defined(CONFIG_CPU_SUBTYPE_SH7785)
  279. i = dmaor_reset(1);
  280. if (unlikely(i != 0))
  281. return i;
  282. #endif
  283. return register_dmac(info);
  284. }
  285. static void __exit sh_dmac_exit(void)
  286. {
  287. #ifdef CONFIG_CPU_SH4
  288. int n;
  289. for (n = 0; n < NR_DMAE; n++) {
  290. free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
  291. }
  292. #endif /* CONFIG_CPU_SH4 */
  293. unregister_dmac(&sh_dmac_info);
  294. }
  295. subsys_initcall(sh_dmac_init);
  296. module_exit(sh_dmac_exit);
  297. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  298. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  299. MODULE_LICENSE("GPL");