dma-sh.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(CONFIG_CPU_SUBTYPE_SH7763) || \
  22. defined(CONFIG_CPU_SUBTYPE_SH7764) || \
  23. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  24. defined(CONFIG_CPU_SUBTYPE_SH7785)
  25. #define DMAC_IRQ_MULTI 1
  26. #endif
  27. #if defined(DMAE1_IRQ)
  28. #define NR_DMAE 2
  29. #else
  30. #define NR_DMAE 1
  31. #endif
  32. static const char *dmae_name[] = {
  33. "DMAC Address Error0", "DMAC Address Error1"
  34. };
  35. static inline unsigned int get_dmte_irq(unsigned int chan)
  36. {
  37. unsigned int irq = 0;
  38. if (chan < ARRAY_SIZE(dmte_irq_map))
  39. irq = dmte_irq_map[chan];
  40. #if defined(DMAC_IRQ_MULTI)
  41. if (irq > DMTE6_IRQ)
  42. return DMTE6_IRQ;
  43. return DMTE0_IRQ;
  44. #else
  45. return irq;
  46. #endif
  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(dma_base_addr[chan->chan] + CHCR);
  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(dma_base_addr[chan->chan] + CHCR);
  72. if (!(chcr & CHCR_TE))
  73. return IRQ_NONE;
  74. chcr &= ~(CHCR_IE | CHCR_DE);
  75. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  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. #if defined(DMAC_IRQ_MULTI)
  85. IRQF_SHARED,
  86. #else
  87. IRQF_DISABLED,
  88. #endif
  89. chan->dev_id, chan);
  90. }
  91. static void sh_dmac_free_dma(struct dma_channel *chan)
  92. {
  93. free_irq(get_dmte_irq(chan->chan), chan);
  94. }
  95. static int
  96. sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
  97. {
  98. if (!chcr)
  99. chcr = RS_DUAL | CHCR_IE;
  100. if (chcr & CHCR_IE) {
  101. chcr &= ~CHCR_IE;
  102. chan->flags |= DMA_TEI_CAPABLE;
  103. } else {
  104. chan->flags &= ~DMA_TEI_CAPABLE;
  105. }
  106. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  107. chan->flags |= DMA_CONFIGURED;
  108. return 0;
  109. }
  110. static void sh_dmac_enable_dma(struct dma_channel *chan)
  111. {
  112. int irq;
  113. u32 chcr;
  114. chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
  115. chcr |= CHCR_DE;
  116. if (chan->flags & DMA_TEI_CAPABLE)
  117. chcr |= CHCR_IE;
  118. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  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(dma_base_addr[chan->chan] + CHCR);
  133. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  134. ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
  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, (dma_base_addr[chan->chan]+SAR));
  163. if (chan->dar || (mach_is_dreamcast() &&
  164. chan->chan == PVR2_CASCADE_CHAN))
  165. ctrl_outl(chan->dar, (dma_base_addr[chan->chan] + DAR));
  166. ctrl_outl(chan->count >> calc_xmit_shift(chan),
  167. (dma_base_addr[chan->chan] + TCR));
  168. sh_dmac_enable_dma(chan);
  169. return 0;
  170. }
  171. static int sh_dmac_get_dma_residue(struct dma_channel *chan)
  172. {
  173. if (!(ctrl_inl(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
  174. return 0;
  175. return ctrl_inl(dma_base_addr[chan->chan] + TCR)
  176. << calc_xmit_shift(chan);
  177. }
  178. static inline int dmaor_reset(int no)
  179. {
  180. unsigned long dmaor = dmaor_read_reg(no);
  181. /* Try to clear the error flags first, incase they are set */
  182. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  183. dmaor_write_reg(no, dmaor);
  184. dmaor |= DMAOR_INIT;
  185. dmaor_write_reg(no, dmaor);
  186. /* See if we got an error again */
  187. if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
  188. printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  189. return -EINVAL;
  190. }
  191. return 0;
  192. }
  193. #if defined(CONFIG_CPU_SH4)
  194. static irqreturn_t dma_err(int irq, void *dummy)
  195. {
  196. #if defined(DMAC_IRQ_MULTI)
  197. int cnt = 0;
  198. switch (irq) {
  199. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  200. case DMTE6_IRQ:
  201. cnt++;
  202. #endif
  203. case DMTE0_IRQ:
  204. if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
  205. disable_irq(irq);
  206. /* DMA multi and error IRQ */
  207. return IRQ_HANDLED;
  208. }
  209. default:
  210. return IRQ_NONE;
  211. }
  212. #else
  213. dmaor_reset(0);
  214. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  215. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  216. defined(CONFIG_CPU_SUBTYPE_SH7785)
  217. dmaor_reset(1);
  218. #endif
  219. disable_irq(irq);
  220. return IRQ_HANDLED;
  221. #endif
  222. }
  223. #endif
  224. static struct dma_ops sh_dmac_ops = {
  225. .request = sh_dmac_request_dma,
  226. .free = sh_dmac_free_dma,
  227. .get_residue = sh_dmac_get_dma_residue,
  228. .xfer = sh_dmac_xfer_dma,
  229. .configure = sh_dmac_configure_channel,
  230. };
  231. static struct dma_info sh_dmac_info = {
  232. .name = "sh_dmac",
  233. .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
  234. .ops = &sh_dmac_ops,
  235. .flags = DMAC_CHANNELS_TEI_CAPABLE,
  236. };
  237. static unsigned int get_dma_error_irq(int n)
  238. {
  239. #if defined(DMAC_IRQ_MULTI)
  240. return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
  241. #else
  242. return (n == 0) ? DMAE0_IRQ :
  243. #if defined(DMAE1_IRQ)
  244. DMAE1_IRQ;
  245. #else
  246. -1;
  247. #endif
  248. #endif
  249. }
  250. static int __init sh_dmac_init(void)
  251. {
  252. struct dma_info *info = &sh_dmac_info;
  253. int i;
  254. #ifdef CONFIG_CPU_SH4
  255. int n;
  256. for (n = 0; n < NR_DMAE; n++) {
  257. i = request_irq(get_dma_error_irq(n), dma_err,
  258. #if defined(DMAC_IRQ_MULTI)
  259. IRQF_SHARED,
  260. #else
  261. IRQF_DISABLED,
  262. #endif
  263. dmae_name[n], (void *)dmae_name[n]);
  264. if (unlikely(i < 0)) {
  265. printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
  266. return i;
  267. }
  268. }
  269. #endif /* CONFIG_CPU_SH4 */
  270. /*
  271. * Initialize DMAOR, and clean up any error flags that may have
  272. * been set.
  273. */
  274. i = dmaor_reset(0);
  275. if (unlikely(i != 0))
  276. return i;
  277. #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
  278. defined(CONFIG_CPU_SUBTYPE_SH7780) || \
  279. defined(CONFIG_CPU_SUBTYPE_SH7785)
  280. i = dmaor_reset(1);
  281. if (unlikely(i != 0))
  282. return i;
  283. #endif
  284. return register_dmac(info);
  285. }
  286. static void __exit sh_dmac_exit(void)
  287. {
  288. #ifdef CONFIG_CPU_SH4
  289. int n;
  290. for (n = 0; n < NR_DMAE; n++) {
  291. free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
  292. }
  293. #endif /* CONFIG_CPU_SH4 */
  294. unregister_dmac(&sh_dmac_info);
  295. }
  296. subsys_initcall(sh_dmac_init);
  297. module_exit(sh_dmac_exit);
  298. MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
  299. MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
  300. MODULE_LICENSE("GPL");