1 // SPDX-License-Identifier: GPL-2.0
3 * arch/sh/drivers/dma/dma-sh.c
5 * SuperH On-chip DMAC Support
7 * Copyright (C) 2000 Takashi YOSHII
8 * Copyright (C) 2003, 2004 Paul Mundt
9 * Copyright (C) 2005 Andriy Skulysh
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
15 #include <mach-dreamcast/mach/dma.h>
17 #include <asm/dma-register.h>
18 #include <cpu/dma-register.h>
22 * Some of the SoCs feature two DMAC modules. In such a case, the channels are
23 * distributed equally among them.
26 #define SH_DMAC_NR_MD_CH (CONFIG_NR_ONCHIP_DMA_CHANNELS / 2)
28 #define SH_DMAC_NR_MD_CH CONFIG_NR_ONCHIP_DMA_CHANNELS
31 #define SH_DMAC_CH_SZ 0x10
34 * Define the default configuration for dual address memory-memory transfer.
35 * The 0x400 value represents auto-request, external->external.
37 #define RS_DUAL (DM_INC | SM_INC | RS_AUTO | TS_INDEX2VAL(XMIT_SZ_32BIT))
39 static unsigned long dma_find_base(unsigned int chan
)
41 unsigned long base
= SH_DMAC_BASE0
;
44 if (chan
>= SH_DMAC_NR_MD_CH
)
51 static unsigned long dma_base_addr(unsigned int chan
)
53 unsigned long base
= dma_find_base(chan
);
55 chan
= (chan
% SH_DMAC_NR_MD_CH
) * SH_DMAC_CH_SZ
;
57 /* DMAOR is placed inside the channel register space. Step over it. */
59 base
+= SH_DMAC_CH_SZ
;
64 #ifdef CONFIG_SH_DMA_IRQ_MULTI
65 static inline unsigned int get_dmte_irq(unsigned int chan
)
67 return chan
>= 6 ? DMTE6_IRQ
: DMTE0_IRQ
;
71 static unsigned int dmte_irq_map
[] = {
72 DMTE0_IRQ
, DMTE0_IRQ
+ 1, DMTE0_IRQ
+ 2, DMTE0_IRQ
+ 3,
75 DMTE4_IRQ
, DMTE4_IRQ
+ 1,
79 DMTE6_IRQ
, DMTE6_IRQ
+ 1,
83 DMTE8_IRQ
, DMTE9_IRQ
, DMTE10_IRQ
, DMTE11_IRQ
,
87 static inline unsigned int get_dmte_irq(unsigned int chan
)
89 return dmte_irq_map
[chan
];
94 * We determine the correct shift size based off of the CHCR transmit size
95 * for the given channel. Since we know that it will take:
97 * info->count >> ts_shift[transmit_size]
99 * iterations to complete the transfer.
101 static unsigned int ts_shift
[] = TS_SHIFT
;
103 static inline unsigned int calc_xmit_shift(struct dma_channel
*chan
)
105 u32 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
106 int cnt
= ((chcr
& CHCR_TS_LOW_MASK
) >> CHCR_TS_LOW_SHIFT
) |
107 ((chcr
& CHCR_TS_HIGH_MASK
) >> CHCR_TS_HIGH_SHIFT
);
109 return ts_shift
[cnt
];
113 * The transfer end interrupt must read the chcr register to end the
114 * hardware interrupt active condition.
115 * Besides that it needs to waken any waiting process, which should handle
116 * setting up the next transfer.
118 static irqreturn_t
dma_tei(int irq
, void *dev_id
)
120 struct dma_channel
*chan
= dev_id
;
123 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
125 if (!(chcr
& CHCR_TE
))
128 chcr
&= ~(CHCR_IE
| CHCR_DE
);
129 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
131 wake_up(&chan
->wait_queue
);
136 static int sh_dmac_request_dma(struct dma_channel
*chan
)
138 if (unlikely(!(chan
->flags
& DMA_TEI_CAPABLE
)))
141 return request_irq(get_dmte_irq(chan
->chan
), dma_tei
, IRQF_SHARED
,
145 static void sh_dmac_free_dma(struct dma_channel
*chan
)
147 free_irq(get_dmte_irq(chan
->chan
), chan
);
151 sh_dmac_configure_channel(struct dma_channel
*chan
, unsigned long chcr
)
154 chcr
= RS_DUAL
| CHCR_IE
;
156 if (chcr
& CHCR_IE
) {
158 chan
->flags
|= DMA_TEI_CAPABLE
;
160 chan
->flags
&= ~DMA_TEI_CAPABLE
;
163 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
165 chan
->flags
|= DMA_CONFIGURED
;
169 static void sh_dmac_enable_dma(struct dma_channel
*chan
)
174 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
177 if (chan
->flags
& DMA_TEI_CAPABLE
)
180 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
182 if (chan
->flags
& DMA_TEI_CAPABLE
) {
183 irq
= get_dmte_irq(chan
->chan
);
188 static void sh_dmac_disable_dma(struct dma_channel
*chan
)
193 if (chan
->flags
& DMA_TEI_CAPABLE
) {
194 irq
= get_dmte_irq(chan
->chan
);
198 chcr
= __raw_readl(dma_base_addr(chan
->chan
) + CHCR
);
199 chcr
&= ~(CHCR_DE
| CHCR_TE
| CHCR_IE
);
200 __raw_writel(chcr
, (dma_base_addr(chan
->chan
) + CHCR
));
203 static int sh_dmac_xfer_dma(struct dma_channel
*chan
)
206 * If we haven't pre-configured the channel with special flags, use
209 if (unlikely(!(chan
->flags
& DMA_CONFIGURED
)))
210 sh_dmac_configure_channel(chan
, 0);
212 sh_dmac_disable_dma(chan
);
215 * Single-address mode usage note!
217 * It's important that we don't accidentally write any value to SAR/DAR
218 * (this includes 0) that hasn't been directly specified by the user if
219 * we're in single-address mode.
221 * In this case, only one address can be defined, anything else will
222 * result in a DMA address error interrupt (at least on the SH-4),
223 * which will subsequently halt the transfer.
225 * Channel 2 on the Dreamcast is a special case, as this is used for
226 * cascading to the PVR2 DMAC. In this case, we still need to write
227 * SAR and DAR, regardless of value, in order for cascading to work.
229 if (chan
->sar
|| (mach_is_dreamcast() &&
230 chan
->chan
== PVR2_CASCADE_CHAN
))
231 __raw_writel(chan
->sar
, (dma_base_addr(chan
->chan
) + SAR
));
232 if (chan
->dar
|| (mach_is_dreamcast() &&
233 chan
->chan
== PVR2_CASCADE_CHAN
))
234 __raw_writel(chan
->dar
, (dma_base_addr(chan
->chan
) + DAR
));
236 __raw_writel(chan
->count
>> calc_xmit_shift(chan
),
237 (dma_base_addr(chan
->chan
) + TCR
));
239 sh_dmac_enable_dma(chan
);
244 static int sh_dmac_get_dma_residue(struct dma_channel
*chan
)
246 if (!(__raw_readl(dma_base_addr(chan
->chan
) + CHCR
) & CHCR_DE
))
249 return __raw_readl(dma_base_addr(chan
->chan
) + TCR
)
250 << calc_xmit_shift(chan
);
256 #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
257 defined(CONFIG_CPU_SUBTYPE_SH7724) || \
258 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
259 defined(CONFIG_CPU_SUBTYPE_SH7785)
265 #define dmaor_read_reg(n) __raw_readw(dma_find_base((n) * \
266 SH_DMAC_NR_MD_CH) + DMAOR)
267 #define dmaor_write_reg(n, data) __raw_writew(data, \
268 dma_find_base((n) * \
269 SH_DMAC_NR_MD_CH) + DMAOR)
271 static inline int dmaor_reset(int no
)
273 unsigned long dmaor
= dmaor_read_reg(no
);
275 /* Try to clear the error flags first, incase they are set */
276 dmaor
&= ~(DMAOR_NMIF
| DMAOR_AE
);
277 dmaor_write_reg(no
, dmaor
);
280 dmaor_write_reg(no
, dmaor
);
282 /* See if we got an error again */
283 if ((dmaor_read_reg(no
) & (DMAOR_AE
| DMAOR_NMIF
))) {
284 printk(KERN_ERR
"dma-sh: Can't initialize DMAOR.\n");
294 #ifdef CONFIG_CPU_SH4
296 #if defined(DMAE1_IRQ)
302 static const char *dmae_name
[] = {
303 "DMAC Address Error0",
304 "DMAC Address Error1"
307 #ifdef CONFIG_SH_DMA_IRQ_MULTI
308 static inline unsigned int get_dma_error_irq(int n
)
310 return get_dmte_irq(n
* 6);
314 static unsigned int dmae_irq_map
[] = {
322 static inline unsigned int get_dma_error_irq(int n
)
324 return dmae_irq_map
[n
];
328 static irqreturn_t
dma_err(int irq
, void *dummy
)
332 for (i
= 0; i
< NR_DMAOR
; i
++)
340 static int dmae_irq_init(void)
344 for (n
= 0; n
< NR_DMAE
; n
++) {
345 int i
= request_irq(get_dma_error_irq(n
), dma_err
,
346 IRQF_SHARED
, dmae_name
[n
], (void *)dmae_name
[n
]);
347 if (unlikely(i
< 0)) {
348 printk(KERN_ERR
"%s request_irq fail\n", dmae_name
[n
]);
356 static void dmae_irq_free(void)
360 for (n
= 0; n
< NR_DMAE
; n
++)
361 free_irq(get_dma_error_irq(n
), NULL
);
364 static inline int dmae_irq_init(void)
369 static void dmae_irq_free(void)
374 static struct dma_ops sh_dmac_ops
= {
375 .request
= sh_dmac_request_dma
,
376 .free
= sh_dmac_free_dma
,
377 .get_residue
= sh_dmac_get_dma_residue
,
378 .xfer
= sh_dmac_xfer_dma
,
379 .configure
= sh_dmac_configure_channel
,
382 static struct dma_info sh_dmac_info
= {
384 .nr_channels
= CONFIG_NR_ONCHIP_DMA_CHANNELS
,
386 .flags
= DMAC_CHANNELS_TEI_CAPABLE
,
389 static int __init
sh_dmac_init(void)
391 struct dma_info
*info
= &sh_dmac_info
;
395 * Initialize DMAE, for parts that support it.
397 rc
= dmae_irq_init();
398 if (unlikely(rc
!= 0))
402 * Initialize DMAOR, and clean up any error flags that may have
405 for (i
= 0; i
< NR_DMAOR
; i
++) {
407 if (unlikely(rc
!= 0))
411 return register_dmac(info
);
414 static void __exit
sh_dmac_exit(void)
417 unregister_dmac(&sh_dmac_info
);
420 subsys_initcall(sh_dmac_init
);
421 module_exit(sh_dmac_exit
);
423 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
424 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
425 MODULE_LICENSE("GPL v2");