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 <asm/dreamcast/dma.h>
22 static inline unsigned int get_dmte_irq(unsigned int chan)
27 * Normally we could just do DMTE0_IRQ + chan outright, though in the
28 * case of the 7751R, the DMTE IRQs for channels > 4 start right above
32 irq = DMTE0_IRQ + chan;
35 irq = DMTE4_IRQ + chan - 4;
43 * We determine the correct shift size based off of the CHCR transmit size
44 * for the given channel. Since we know that it will take:
46 * info->count >> ts_shift[transmit_size]
48 * iterations to complete the transfer.
50 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
52 u32 chcr = ctrl_inl(CHCR[chan->chan]);
54 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
58 * The transfer end interrupt must read the chcr register to end the
59 * hardware interrupt active condition.
60 * Besides that it needs to waken any waiting process, which should handle
61 * setting up the next transfer.
63 static irqreturn_t dma_tei(int irq, void *dev_id)
65 struct dma_channel *chan = dev_id;
68 chcr = ctrl_inl(CHCR[chan->chan]);
70 if (!(chcr & CHCR_TE))
73 chcr &= ~(CHCR_IE | CHCR_DE);
74 ctrl_outl(chcr, CHCR[chan->chan]);
76 wake_up(&chan->wait_queue);
81 static int sh_dmac_request_dma(struct dma_channel *chan)
83 if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
86 chan->name = kzalloc(32, GFP_KERNEL);
87 if (unlikely(chan->name == NULL))
89 snprintf(chan->name, 32, "DMAC Transfer End (Channel %d)",
92 return request_irq(get_dmte_irq(chan->chan), dma_tei,
93 IRQF_DISABLED, chan->name, chan);
96 static void sh_dmac_free_dma(struct dma_channel *chan)
98 free_irq(get_dmte_irq(chan->chan), chan);
103 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
106 chcr = RS_DUAL | CHCR_IE;
108 if (chcr & CHCR_IE) {
110 chan->flags |= DMA_TEI_CAPABLE;
112 chan->flags &= ~DMA_TEI_CAPABLE;
115 ctrl_outl(chcr, CHCR[chan->chan]);
117 chan->flags |= DMA_CONFIGURED;
120 static void sh_dmac_enable_dma(struct dma_channel *chan)
125 chcr = ctrl_inl(CHCR[chan->chan]);
128 if (chan->flags & DMA_TEI_CAPABLE)
131 ctrl_outl(chcr, CHCR[chan->chan]);
133 if (chan->flags & DMA_TEI_CAPABLE) {
134 irq = get_dmte_irq(chan->chan);
139 static void sh_dmac_disable_dma(struct dma_channel *chan)
144 if (chan->flags & DMA_TEI_CAPABLE) {
145 irq = get_dmte_irq(chan->chan);
149 chcr = ctrl_inl(CHCR[chan->chan]);
150 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
151 ctrl_outl(chcr, CHCR[chan->chan]);
154 static int sh_dmac_xfer_dma(struct dma_channel *chan)
157 * If we haven't pre-configured the channel with special flags, use
160 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
161 sh_dmac_configure_channel(chan, 0);
163 sh_dmac_disable_dma(chan);
166 * Single-address mode usage note!
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.
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.
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.
180 if (chan->sar || (mach_is_dreamcast() &&
181 chan->chan == PVR2_CASCADE_CHAN))
182 ctrl_outl(chan->sar, SAR[chan->chan]);
183 if (chan->dar || (mach_is_dreamcast() &&
184 chan->chan == PVR2_CASCADE_CHAN))
185 ctrl_outl(chan->dar, DAR[chan->chan]);
187 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
189 sh_dmac_enable_dma(chan);
194 static int sh_dmac_get_dma_residue(struct dma_channel *chan)
196 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
199 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
202 #ifdef CONFIG_CPU_SUBTYPE_SH7780
203 #define dmaor_read_reg() ctrl_inw(DMAOR)
204 #define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
206 #define dmaor_read_reg() ctrl_inl(DMAOR)
207 #define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
210 static inline int dmaor_reset(void)
212 unsigned long dmaor = dmaor_read_reg();
214 /* Try to clear the error flags first, incase they are set */
215 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
216 dmaor_write_reg(dmaor);
219 dmaor_write_reg(dmaor);
221 /* See if we got an error again */
222 if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
223 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
230 #if defined(CONFIG_CPU_SH4)
231 static irqreturn_t dma_err(int irq, void *dummy)
240 static struct dma_ops sh_dmac_ops = {
241 .request = sh_dmac_request_dma,
242 .free = sh_dmac_free_dma,
243 .get_residue = sh_dmac_get_dma_residue,
244 .xfer = sh_dmac_xfer_dma,
245 .configure = sh_dmac_configure_channel,
248 static struct dma_info sh_dmac_info = {
250 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
252 .flags = DMAC_CHANNELS_TEI_CAPABLE,
255 static int __init sh_dmac_init(void)
257 struct dma_info *info = &sh_dmac_info;
260 #ifdef CONFIG_CPU_SH4
261 make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
262 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
267 for (i = 0; i < info->nr_channels; i++) {
268 int irq = get_dmte_irq(i);
270 make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
274 * Initialize DMAOR, and clean up any error flags that may have
278 if (unlikely(i != 0))
281 return register_dmac(info);
284 static void __exit sh_dmac_exit(void)
286 #ifdef CONFIG_CPU_SH4
287 free_irq(DMAE_IRQ, 0);
289 unregister_dmac(&sh_dmac_info);
292 subsys_initcall(sh_dmac_init);
293 module_exit(sh_dmac_exit);
295 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
296 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
297 MODULE_LICENSE("GPL");