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>
17 #include <mach-dreamcast/mach/dma.h>
20 #include <asm/dma-sh.h>
22 #if defined(DMAE1_IRQ)
28 static const char *dmae_name[] = {
29 "DMAC Address Error0", "DMAC Address Error1"
32 static inline unsigned int get_dmte_irq(unsigned int chan)
35 if (chan < ARRAY_SIZE(dmte_irq_map))
36 irq = dmte_irq_map[chan];
38 #if defined(CONFIG_SH_DMA_IRQ_MULTI)
48 * We determine the correct shift size based off of the CHCR transmit size
49 * for the given channel. Since we know that it will take:
51 * info->count >> ts_shift[transmit_size]
53 * iterations to complete the transfer.
55 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
57 u32 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
59 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
63 * The transfer end interrupt must read the chcr register to end the
64 * hardware interrupt active condition.
65 * Besides that it needs to waken any waiting process, which should handle
66 * setting up the next transfer.
68 static irqreturn_t dma_tei(int irq, void *dev_id)
70 struct dma_channel *chan = dev_id;
73 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
75 if (!(chcr & CHCR_TE))
78 chcr &= ~(CHCR_IE | CHCR_DE);
79 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
81 wake_up(&chan->wait_queue);
86 static int sh_dmac_request_dma(struct dma_channel *chan)
88 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
91 return request_irq(get_dmte_irq(chan->chan), dma_tei,
92 #if defined(CONFIG_SH_DMA_IRQ_MULTI)
100 static void sh_dmac_free_dma(struct dma_channel *chan)
102 free_irq(get_dmte_irq(chan->chan), chan);
106 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
109 chcr = RS_DUAL | CHCR_IE;
111 if (chcr & CHCR_IE) {
113 chan->flags |= DMA_TEI_CAPABLE;
115 chan->flags &= ~DMA_TEI_CAPABLE;
118 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
120 chan->flags |= DMA_CONFIGURED;
124 static void sh_dmac_enable_dma(struct dma_channel *chan)
129 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
132 if (chan->flags & DMA_TEI_CAPABLE)
135 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
137 if (chan->flags & DMA_TEI_CAPABLE) {
138 irq = get_dmte_irq(chan->chan);
143 static void sh_dmac_disable_dma(struct dma_channel *chan)
148 if (chan->flags & DMA_TEI_CAPABLE) {
149 irq = get_dmte_irq(chan->chan);
153 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
154 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
155 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
158 static int sh_dmac_xfer_dma(struct dma_channel *chan)
161 * If we haven't pre-configured the channel with special flags, use
164 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
165 sh_dmac_configure_channel(chan, 0);
167 sh_dmac_disable_dma(chan);
170 * Single-address mode usage note!
172 * It's important that we don't accidentally write any value to SAR/DAR
173 * (this includes 0) that hasn't been directly specified by the user if
174 * we're in single-address mode.
176 * In this case, only one address can be defined, anything else will
177 * result in a DMA address error interrupt (at least on the SH-4),
178 * which will subsequently halt the transfer.
180 * Channel 2 on the Dreamcast is a special case, as this is used for
181 * cascading to the PVR2 DMAC. In this case, we still need to write
182 * SAR and DAR, regardless of value, in order for cascading to work.
184 if (chan->sar || (mach_is_dreamcast() &&
185 chan->chan == PVR2_CASCADE_CHAN))
186 ctrl_outl(chan->sar, (dma_base_addr[chan->chan]+SAR));
187 if (chan->dar || (mach_is_dreamcast() &&
188 chan->chan == PVR2_CASCADE_CHAN))
189 ctrl_outl(chan->dar, (dma_base_addr[chan->chan] + DAR));
191 ctrl_outl(chan->count >> calc_xmit_shift(chan),
192 (dma_base_addr[chan->chan] + TCR));
194 sh_dmac_enable_dma(chan);
199 static int sh_dmac_get_dma_residue(struct dma_channel *chan)
201 if (!(ctrl_inl(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
204 return ctrl_inl(dma_base_addr[chan->chan] + TCR)
205 << calc_xmit_shift(chan);
208 static inline int dmaor_reset(int no)
210 unsigned long dmaor = dmaor_read_reg(no);
212 /* Try to clear the error flags first, incase they are set */
213 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
214 dmaor_write_reg(no, dmaor);
217 dmaor_write_reg(no, dmaor);
219 /* See if we got an error again */
220 if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
221 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
228 #if defined(CONFIG_CPU_SH4)
229 static irqreturn_t dma_err(int irq, void *dummy)
231 #if defined(CONFIG_SH_DMA_IRQ_MULTI)
234 #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
239 if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
241 /* DMA multi and error IRQ */
249 #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
250 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
251 defined(CONFIG_CPU_SUBTYPE_SH7785)
261 static struct dma_ops sh_dmac_ops = {
262 .request = sh_dmac_request_dma,
263 .free = sh_dmac_free_dma,
264 .get_residue = sh_dmac_get_dma_residue,
265 .xfer = sh_dmac_xfer_dma,
266 .configure = sh_dmac_configure_channel,
269 static struct dma_info sh_dmac_info = {
271 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
273 .flags = DMAC_CHANNELS_TEI_CAPABLE,
276 #ifdef CONFIG_CPU_SH4
277 static unsigned int get_dma_error_irq(int n)
279 #if defined(CONFIG_SH_DMA_IRQ_MULTI)
280 return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
282 return (n == 0) ? DMAE0_IRQ :
283 #if defined(DMAE1_IRQ)
292 static int __init sh_dmac_init(void)
294 struct dma_info *info = &sh_dmac_info;
297 #ifdef CONFIG_CPU_SH4
300 for (n = 0; n < NR_DMAE; n++) {
301 i = request_irq(get_dma_error_irq(n), dma_err,
302 #if defined(CONFIG_SH_DMA_IRQ_MULTI)
307 dmae_name[n], (void *)dmae_name[n]);
308 if (unlikely(i < 0)) {
309 printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
313 #endif /* CONFIG_CPU_SH4 */
316 * Initialize DMAOR, and clean up any error flags that may have
320 if (unlikely(i != 0))
322 #if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
323 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
324 defined(CONFIG_CPU_SUBTYPE_SH7785)
326 if (unlikely(i != 0))
330 return register_dmac(info);
333 static void __exit sh_dmac_exit(void)
335 #ifdef CONFIG_CPU_SH4
338 for (n = 0; n < NR_DMAE; n++) {
339 free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
341 #endif /* CONFIG_CPU_SH4 */
342 unregister_dmac(&sh_dmac_info);
345 subsys_initcall(sh_dmac_init);
346 module_exit(sh_dmac_exit);
348 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
349 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
350 MODULE_LICENSE("GPL");