dma-sh.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <linux/io.h>
  18. #include <mach-dreamcast/mach/dma.h>
  19. #include <asm/dma.h>
  20. #include <asm/dma-register.h>
  21. #include <cpu/dma-register.h>
  22. #include <cpu/dma.h>
  23. /*
  24. * Define the default configuration for dual address memory-memory transfer.
  25. * The 0x400 value represents auto-request, external->external.
  26. */
  27. #define RS_DUAL (DM_INC | SM_INC | RS_AUTO | TS_INDEX2VAL(XMIT_SZ_32BIT))
  28. static unsigned long dma_find_base(unsigned int chan)
  29. {
  30. unsigned long base = SH_DMAC_BASE0;
  31. #ifdef SH_DMAC_BASE1
  32. if (chan >= 6)
  33. base = SH_DMAC_BASE1;
  34. #endif
  35. return base;
  36. }
  37. static unsigned long dma_base_addr(unsigned int chan)
  38. {
  39. unsigned long base = dma_find_base(chan);
  40. /* Normalize offset calculation */
  41. if (chan >= 9)
  42. chan -= 6;
  43. if (chan >= 4)
  44. base += 0x10;
  45. return base + (chan * 0x10);
  46. }
  47. #ifdef CONFIG_SH_DMA_IRQ_MULTI
  48. static inline unsigned int get_dmte_irq(unsigned int chan)
  49. {
  50. return chan >= 6 ? DMTE6_IRQ : DMTE0_IRQ;
  51. }
  52. #else
  53. static unsigned int dmte_irq_map[] = {
  54. DMTE0_IRQ, DMTE0_IRQ + 1, DMTE0_IRQ + 2, DMTE0_IRQ + 3,
  55. #ifdef DMTE4_IRQ
  56. DMTE4_IRQ, DMTE4_IRQ + 1,
  57. #endif
  58. #ifdef DMTE6_IRQ
  59. DMTE6_IRQ, DMTE6_IRQ + 1,
  60. #endif
  61. #ifdef DMTE8_IRQ
  62. DMTE8_IRQ, DMTE9_IRQ, DMTE10_IRQ, DMTE11_IRQ,
  63. #endif
  64. };
  65. static inline unsigned int get_dmte_irq(unsigned int chan)
  66. {
  67. return dmte_irq_map[chan];
  68. }
  69. #endif
  70. /*
  71. * We determine the correct shift size based off of the CHCR transmit size
  72. * for the given channel. Since we know that it will take:
  73. *
  74. * info->count >> ts_shift[transmit_size]
  75. *
  76. * iterations to complete the transfer.
  77. */
  78. static unsigned int ts_shift[] = TS_SHIFT;
  79. static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
  80. {
  81. u32 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
  82. int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
  83. ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
  84. return ts_shift[cnt];
  85. }
  86. /*
  87. * The transfer end interrupt must read the chcr register to end the
  88. * hardware interrupt active condition.
  89. * Besides that it needs to waken any waiting process, which should handle
  90. * setting up the next transfer.
  91. */
  92. static irqreturn_t dma_tei(int irq, void *dev_id)
  93. {
  94. struct dma_channel *chan = dev_id;
  95. u32 chcr;
  96. chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
  97. if (!(chcr & CHCR_TE))
  98. return IRQ_NONE;
  99. chcr &= ~(CHCR_IE | CHCR_DE);
  100. __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
  101. wake_up(&chan->wait_queue);
  102. return IRQ_HANDLED;
  103. }
  104. static int sh_dmac_request_dma(struct dma_channel *chan)
  105. {
  106. if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
  107. return 0;
  108. return request_irq(get_dmte_irq(chan->chan), dma_tei, IRQF_SHARED,
  109. chan->dev_id, chan);
  110. }
  111. static void sh_dmac_free_dma(struct dma_channel *chan)
  112. {
  113. free_irq(get_dmte_irq(chan->chan), chan);
  114. }
  115. static int
  116. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  117. {
  118. if (!chcr)
  119. chcr = RS_DUAL | CHCR_IE;
  120. if (chcr & CHCR_IE) {
  121. chcr &= ~CHCR_IE;
  122. chan->flags |= DMA_TEI_CAPABLE;
  123. } else {
  124. chan->flags &= ~DMA_TEI_CAPABLE;
  125. }
  126. __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
  127. chan->flags |= DMA_CONFIGURED;
  128. return 0;
  129. }
  130. static void sh_dmac_enable_dma(struct dma_channel *chan)
  131. {
  132. int irq;
  133. u32 chcr;
  134. chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
  135. chcr |= CHCR_DE;
  136. if (chan->flags & DMA_TEI_CAPABLE)
  137. chcr |= CHCR_IE;
  138. __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
  139. if (chan->flags & DMA_TEI_CAPABLE) {
  140. irq = get_dmte_irq(chan->chan);
  141. enable_irq(irq);
  142. }
  143. }
  144. static void sh_dmac_disable_dma(struct dma_channel *chan)
  145. {
  146. int irq;
  147. u32 chcr;
  148. if (chan->flags & DMA_TEI_CAPABLE) {
  149. irq = get_dmte_irq(chan->chan);
  150. disable_irq(irq);
  151. }
  152. chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
  153. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  154. __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
  155. }
  156. static int sh_dmac_xfer_dma(struct dma_channel *chan)
  157. {
  158. /*
  159. * If we haven't pre-configured the channel with special flags, use
  160. * the defaults.
  161. */
  162. if (unlikely(!(chan->flags & DMA_CONFIGURED)))
  163. sh_dmac_configure_channel(chan, 0);
  164. sh_dmac_disable_dma(chan);
  165. /*
  166. * Single-address mode usage note!
  167. *
  168. * It's important that we don't accidentally write any value to SAR/DAR
  169. * (this includes 0) that hasn't been directly specified by the user if
  170. * we're in single-address mode.
  171. *
  172. * In this case, only one address can be defined, anything else will
  173. * result in a DMA address error interrupt (at least on the SH-4),
  174. * which will subsequently halt the transfer.
  175. *
  176. * Channel 2 on the Dreamcast is a special case, as this is used for
  177. * cascading to the PVR2 DMAC. In this case, we still need to write
  178. * SAR and DAR, regardless of value, in order for cascading to work.
  179. */
  180. if (chan->sar || (mach_is_dreamcast() &&
  181. chan->chan == PVR2_CASCADE_CHAN))
  182. __raw_writel(chan->sar, (dma_base_addr(chan->chan) + SAR));
  183. if (chan->dar || (mach_is_dreamcast() &&
  184. chan->chan == PVR2_CASCADE_CHAN))
  185. __raw_writel(chan->dar, (dma_base_addr(chan->chan) + DAR));
  186. __raw_writel(chan->count >> calc_xmit_shift(chan),
  187. (dma_base_addr(chan->chan) + TCR));
  188. sh_dmac_enable_dma(chan);
  189. return 0;
  190. }
  191. static int sh_dmac_get_dma_residue(struct dma_channel *chan)
  192. {
  193. if (!(__raw_readl(dma_base_addr(chan->chan) + CHCR) & CHCR_DE))
  194. return 0;
  195. return __raw_readl(dma_base_addr(chan->chan) + TCR)
  196. << calc_xmit_shift(chan);
  197. }
  198. /*
  199. * DMAOR handling
  200. */
  201. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  202. defined(CONFIG_CPU_SUBTYPE_SH7724) || \
  203. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  204. defined(CONFIG_CPU_SUBTYPE_SH7785)
  205. #define NR_DMAOR 2
  206. #else
  207. #define NR_DMAOR 1
  208. #endif
  209. /*
  210. * DMAOR bases are broken out amongst channel groups. DMAOR0 manages
  211. * channels 0 - 5, DMAOR1 6 - 11 (optional).
  212. */
  213. #define dmaor_read_reg(n) __raw_readw(dma_find_base((n)*6))
  214. #define dmaor_write_reg(n, data) __raw_writew(data, dma_find_base(n)*6)
  215. static inline int dmaor_reset(int no)
  216. {
  217. unsigned long dmaor = dmaor_read_reg(no);
  218. /* Try to clear the error flags first, incase they are set */
  219. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  220. dmaor_write_reg(no, dmaor);
  221. dmaor |= DMAOR_INIT;
  222. dmaor_write_reg(no, dmaor);
  223. /* See if we got an error again */
  224. if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
  225. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  226. return -EINVAL;
  227. }
  228. return 0;
  229. }
  230. /*
  231. * DMAE handling
  232. */
  233. #ifdef CONFIG_CPU_SH4
  234. #if defined(DMAE1_IRQ)
  235. #define NR_DMAE 2
  236. #else
  237. #define NR_DMAE 1
  238. #endif
  239. static const char *dmae_name[] = {
  240. "DMAC Address Error0",
  241. "DMAC Address Error1"
  242. };
  243. #ifdef CONFIG_SH_DMA_IRQ_MULTI
  244. static inline unsigned int get_dma_error_irq(int n)
  245. {
  246. return get_dmte_irq(n * 6);
  247. }
  248. #else
  249. static unsigned int dmae_irq_map[] = {
  250. DMAE0_IRQ,
  251. #ifdef DMAE1_IRQ
  252. DMAE1_IRQ,
  253. #endif
  254. };
  255. static inline unsigned int get_dma_error_irq(int n)
  256. {
  257. return dmae_irq_map[n];
  258. }
  259. #endif
  260. static irqreturn_t dma_err(int irq, void *dummy)
  261. {
  262. int i;
  263. for (i = 0; i < NR_DMAOR; i++)
  264. dmaor_reset(i);
  265. disable_irq(irq);
  266. return IRQ_HANDLED;
  267. }
  268. static int dmae_irq_init(void)
  269. {
  270. int n;
  271. for (n = 0; n < NR_DMAE; n++) {
  272. int i = request_irq(get_dma_error_irq(n), dma_err,
  273. IRQF_SHARED, dmae_name[n], (void *)dmae_name[n]);
  274. if (unlikely(i < 0)) {
  275. printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
  276. return i;
  277. }
  278. }
  279. return 0;
  280. }
  281. static void dmae_irq_free(void)
  282. {
  283. int n;
  284. for (n = 0; n < NR_DMAE; n++)
  285. free_irq(get_dma_error_irq(n), NULL);
  286. }
  287. #else
  288. static inline int dmae_irq_init(void)
  289. {
  290. return 0;
  291. }
  292. static void dmae_irq_free(void)
  293. {
  294. }
  295. #endif
  296. static struct dma_ops sh_dmac_ops = {
  297. .request = sh_dmac_request_dma,
  298. .free = sh_dmac_free_dma,
  299. .get_residue = sh_dmac_get_dma_residue,
  300. .xfer = sh_dmac_xfer_dma,
  301. .configure = sh_dmac_configure_channel,
  302. };
  303. static struct dma_info sh_dmac_info = {
  304. .name = "sh_dmac",
  305. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  306. .ops = &sh_dmac_ops,
  307. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  308. };
  309. static int __init sh_dmac_init(void)
  310. {
  311. struct dma_info *info = &sh_dmac_info;
  312. int i, rc;
  313. /*
  314. * Initialize DMAE, for parts that support it.
  315. */
  316. rc = dmae_irq_init();
  317. if (unlikely(rc != 0))
  318. return rc;
  319. /*
  320. * Initialize DMAOR, and clean up any error flags that may have
  321. * been set.
  322. */
  323. for (i = 0; i < NR_DMAOR; i++) {
  324. rc = dmaor_reset(i);
  325. if (unlikely(rc != 0))
  326. return rc;
  327. }
  328. return register_dmac(info);
  329. }
  330. static void __exit sh_dmac_exit(void)
  331. {
  332. dmae_irq_free();
  333. unregister_dmac(&sh_dmac_info);
  334. }
  335. subsys_initcall(sh_dmac_init);
  336. module_exit(sh_dmac_exit);
  337. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  338. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  339. MODULE_LICENSE("GPL");