2 * arch/sh/drivers/dma/dma-sh.c
4 * SuperH On-chip DMAC Support
6 * Copyright (C) 2000 Takashi YOSHII
7 * Copyright (C) 2003, 2004 Paul Mundt
8 * Copyright (C) 2005 Andriy Skulysh
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
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
18 #include <mach-dreamcast/mach/dma.h>
20 #include <asm/dma-register.h>
21 #include <cpu/dma-register.h>
25 * Some of the SoCs feature two DMAC modules. In such a case, the channels are
26 * distributed equally among them.
29 #define SH_DMAC_NR_MD_CH (CONFIG_NR_ONCHIP_DMA_CHANNELS / 2)
31 #define SH_DMAC_NR_MD_CH CONFIG_NR_ONCHIP_DMA_CHANNELS
34 #define SH_DMAC_CH_SZ 0x10
37 * Define the default configuration for dual address memory-memory transfer.
38 * The 0x400 value represents auto-request, external->external.
40 #define RS_DUAL (DM_INC | SM_INC | RS_AUTO | TS_INDEX2VAL(XMIT_SZ_32BIT))
42 static unsigned long dma_find_base(unsigned int chan)
44 unsigned long base = SH_DMAC_BASE0;
47 if (chan >= SH_DMAC_NR_MD_CH)
54 static unsigned long dma_base_addr(unsigned int chan)
56 unsigned long base = dma_find_base(chan);
58 chan = (chan % SH_DMAC_NR_MD_CH) * SH_DMAC_CH_SZ;
60 /* DMAOR is placed inside the channel register space. Step over it. */
62 base += SH_DMAC_CH_SZ;
67 #ifdef CONFIG_SH_DMA_IRQ_MULTI
68 static inline unsigned int get_dmte_irq(unsigned int chan)
70 return chan >= 6 ? DMTE6_IRQ : DMTE0_IRQ;
74 static unsigned int dmte_irq_map[] = {
75 DMTE0_IRQ, DMTE0_IRQ + 1, DMTE0_IRQ + 2, DMTE0_IRQ + 3,
78 DMTE4_IRQ, DMTE4_IRQ + 1,
82 DMTE6_IRQ, DMTE6_IRQ + 1,
86 DMTE8_IRQ, DMTE9_IRQ, DMTE10_IRQ, DMTE11_IRQ,
90 static inline unsigned int get_dmte_irq(unsigned int chan)
92 return dmte_irq_map[chan];
97 * We determine the correct shift size based off of the CHCR transmit size
98 * for the given channel. Since we know that it will take:
100 * info->count >> ts_shift[transmit_size]
102 * iterations to complete the transfer.
104 static unsigned int ts_shift[] = TS_SHIFT;
106 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
108 u32 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
109 int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
110 ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
112 return ts_shift[cnt];
116 * The transfer end interrupt must read the chcr register to end the
117 * hardware interrupt active condition.
118 * Besides that it needs to waken any waiting process, which should handle
119 * setting up the next transfer.
121 static irqreturn_t dma_tei(int irq, void *dev_id)
123 struct dma_channel *chan = dev_id;
126 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
128 if (!(chcr & CHCR_TE))
131 chcr &= ~(CHCR_IE | CHCR_DE);
132 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
134 wake_up(&chan->wait_queue);
139 static int sh_dmac_request_dma(struct dma_channel *chan)
141 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
144 return request_irq(get_dmte_irq(chan->chan), dma_tei, IRQF_SHARED,
148 static void sh_dmac_free_dma(struct dma_channel *chan)
150 free_irq(get_dmte_irq(chan->chan), chan);
154 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
157 chcr = RS_DUAL | CHCR_IE;
159 if (chcr & CHCR_IE) {
161 chan->flags |= DMA_TEI_CAPABLE;
163 chan->flags &= ~DMA_TEI_CAPABLE;
166 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
168 chan->flags |= DMA_CONFIGURED;
172 static void sh_dmac_enable_dma(struct dma_channel *chan)
177 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
180 if (chan->flags & DMA_TEI_CAPABLE)
183 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
185 if (chan->flags & DMA_TEI_CAPABLE) {
186 irq = get_dmte_irq(chan->chan);
191 static void sh_dmac_disable_dma(struct dma_channel *chan)
196 if (chan->flags & DMA_TEI_CAPABLE) {
197 irq = get_dmte_irq(chan->chan);
201 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
202 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
203 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
206 static int sh_dmac_xfer_dma(struct dma_channel *chan)
209 * If we haven't pre-configured the channel with special flags, use
212 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
213 sh_dmac_configure_channel(chan, 0);
215 sh_dmac_disable_dma(chan);
218 * Single-address mode usage note!
220 * It's important that we don't accidentally write any value to SAR/DAR
221 * (this includes 0) that hasn't been directly specified by the user if
222 * we're in single-address mode.
224 * In this case, only one address can be defined, anything else will
225 * result in a DMA address error interrupt (at least on the SH-4),
226 * which will subsequently halt the transfer.
228 * Channel 2 on the Dreamcast is a special case, as this is used for
229 * cascading to the PVR2 DMAC. In this case, we still need to write
230 * SAR and DAR, regardless of value, in order for cascading to work.
232 if (chan->sar || (mach_is_dreamcast() &&
233 chan->chan == PVR2_CASCADE_CHAN))
234 __raw_writel(chan->sar, (dma_base_addr(chan->chan) + SAR));
235 if (chan->dar || (mach_is_dreamcast() &&
236 chan->chan == PVR2_CASCADE_CHAN))
237 __raw_writel(chan->dar, (dma_base_addr(chan->chan) + DAR));
239 __raw_writel(chan->count >> calc_xmit_shift(chan),
240 (dma_base_addr(chan->chan) + TCR));
242 sh_dmac_enable_dma(chan);
247 static int sh_dmac_get_dma_residue(struct dma_channel *chan)
249 if (!(__raw_readl(dma_base_addr(chan->chan) + CHCR) & CHCR_DE))
252 return __raw_readl(dma_base_addr(chan->chan) + TCR)
253 << calc_xmit_shift(chan);
259 #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
260 defined(CONFIG_CPU_SUBTYPE_SH7724) || \
261 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
262 defined(CONFIG_CPU_SUBTYPE_SH7785)
268 #define dmaor_read_reg(n) __raw_readw(dma_find_base((n) * \
269 SH_DMAC_NR_MD_CH) + DMAOR)
270 #define dmaor_write_reg(n, data) __raw_writew(data, \
271 dma_find_base((n) * \
272 SH_DMAC_NR_MD_CH) + DMAOR)
274 static inline int dmaor_reset(int no)
276 unsigned long dmaor = dmaor_read_reg(no);
278 /* Try to clear the error flags first, incase they are set */
279 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
280 dmaor_write_reg(no, dmaor);
283 dmaor_write_reg(no, dmaor);
285 /* See if we got an error again */
286 if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
287 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
297 #ifdef CONFIG_CPU_SH4
299 #if defined(DMAE1_IRQ)
305 static const char *dmae_name[] = {
306 "DMAC Address Error0",
307 "DMAC Address Error1"
310 #ifdef CONFIG_SH_DMA_IRQ_MULTI
311 static inline unsigned int get_dma_error_irq(int n)
313 return get_dmte_irq(n * 6);
317 static unsigned int dmae_irq_map[] = {
325 static inline unsigned int get_dma_error_irq(int n)
327 return dmae_irq_map[n];
331 static irqreturn_t dma_err(int irq, void *dummy)
335 for (i = 0; i < NR_DMAOR; i++)
343 static int dmae_irq_init(void)
347 for (n = 0; n < NR_DMAE; n++) {
348 int i = request_irq(get_dma_error_irq(n), dma_err,
349 IRQF_SHARED, dmae_name[n], (void *)dmae_name[n]);
350 if (unlikely(i < 0)) {
351 printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
359 static void dmae_irq_free(void)
363 for (n = 0; n < NR_DMAE; n++)
364 free_irq(get_dma_error_irq(n), NULL);
367 static inline int dmae_irq_init(void)
372 static void dmae_irq_free(void)
377 static struct dma_ops sh_dmac_ops = {
378 .request = sh_dmac_request_dma,
379 .free = sh_dmac_free_dma,
380 .get_residue = sh_dmac_get_dma_residue,
381 .xfer = sh_dmac_xfer_dma,
382 .configure = sh_dmac_configure_channel,
385 static struct dma_info sh_dmac_info = {
387 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
389 .flags = DMAC_CHANNELS_TEI_CAPABLE,
392 static int __init sh_dmac_init(void)
394 struct dma_info *info = &sh_dmac_info;
398 * Initialize DMAE, for parts that support it.
400 rc = dmae_irq_init();
401 if (unlikely(rc != 0))
405 * Initialize DMAOR, and clean up any error flags that may have
408 for (i = 0; i < NR_DMAOR; i++) {
410 if (unlikely(rc != 0))
414 return register_dmac(info);
417 static void __exit sh_dmac_exit(void)
420 unregister_dmac(&sh_dmac_info);
423 subsys_initcall(sh_dmac_init);
424 module_exit(sh_dmac_exit);
426 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
427 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
428 MODULE_LICENSE("GPL");