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 int dmte_irq_map[] = {
27 #if defined(CONFIG_CPU_SUBTYPE_SH7720) || \
28 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
29 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
30 defined(CONFIG_CPU_SUBTYPE_SH7709) || \
31 defined(CONFIG_CPU_SUBTYPE_SH7780)
35 #if defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
36 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
37 defined(CONFIG_CPU_SUBTYPE_SH7780)
43 static inline unsigned int get_dmte_irq(unsigned int chan)
46 if (chan < ARRAY_SIZE(dmte_irq_map))
47 irq = dmte_irq_map[chan];
52 * We determine the correct shift size based off of the CHCR transmit size
53 * for the given channel. Since we know that it will take:
55 * info->count >> ts_shift[transmit_size]
57 * iterations to complete the transfer.
59 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
61 u32 chcr = ctrl_inl(CHCR[chan->chan]);
63 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
67 * The transfer end interrupt must read the chcr register to end the
68 * hardware interrupt active condition.
69 * Besides that it needs to waken any waiting process, which should handle
70 * setting up the next transfer.
72 static irqreturn_t dma_tei(int irq, void *dev_id)
74 struct dma_channel *chan = dev_id;
77 chcr = ctrl_inl(CHCR[chan->chan]);
79 if (!(chcr & CHCR_TE))
82 chcr &= ~(CHCR_IE | CHCR_DE);
83 ctrl_outl(chcr, CHCR[chan->chan]);
85 wake_up(&chan->wait_queue);
90 static int sh_dmac_request_dma(struct dma_channel *chan)
92 if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
95 return request_irq(get_dmte_irq(chan->chan), dma_tei,
96 IRQF_DISABLED, chan->dev_id, chan);
99 static void sh_dmac_free_dma(struct dma_channel *chan)
101 free_irq(get_dmte_irq(chan->chan), chan);
105 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
108 chcr = RS_DUAL | CHCR_IE;
110 if (chcr & CHCR_IE) {
112 chan->flags |= DMA_TEI_CAPABLE;
114 chan->flags &= ~DMA_TEI_CAPABLE;
117 ctrl_outl(chcr, CHCR[chan->chan]);
119 chan->flags |= DMA_CONFIGURED;
123 static void sh_dmac_enable_dma(struct dma_channel *chan)
128 chcr = ctrl_inl(CHCR[chan->chan]);
131 if (chan->flags & DMA_TEI_CAPABLE)
134 ctrl_outl(chcr, CHCR[chan->chan]);
136 if (chan->flags & DMA_TEI_CAPABLE) {
137 irq = get_dmte_irq(chan->chan);
142 static void sh_dmac_disable_dma(struct dma_channel *chan)
147 if (chan->flags & DMA_TEI_CAPABLE) {
148 irq = get_dmte_irq(chan->chan);
152 chcr = ctrl_inl(CHCR[chan->chan]);
153 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
154 ctrl_outl(chcr, CHCR[chan->chan]);
157 static int sh_dmac_xfer_dma(struct dma_channel *chan)
160 * If we haven't pre-configured the channel with special flags, use
163 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
164 sh_dmac_configure_channel(chan, 0);
166 sh_dmac_disable_dma(chan);
169 * Single-address mode usage note!
171 * It's important that we don't accidentally write any value to SAR/DAR
172 * (this includes 0) that hasn't been directly specified by the user if
173 * we're in single-address mode.
175 * In this case, only one address can be defined, anything else will
176 * result in a DMA address error interrupt (at least on the SH-4),
177 * which will subsequently halt the transfer.
179 * Channel 2 on the Dreamcast is a special case, as this is used for
180 * cascading to the PVR2 DMAC. In this case, we still need to write
181 * SAR and DAR, regardless of value, in order for cascading to work.
183 if (chan->sar || (mach_is_dreamcast() &&
184 chan->chan == PVR2_CASCADE_CHAN))
185 ctrl_outl(chan->sar, SAR[chan->chan]);
186 if (chan->dar || (mach_is_dreamcast() &&
187 chan->chan == PVR2_CASCADE_CHAN))
188 ctrl_outl(chan->dar, DAR[chan->chan]);
190 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
192 sh_dmac_enable_dma(chan);
197 static int sh_dmac_get_dma_residue(struct dma_channel *chan)
199 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
202 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
205 #if defined(CONFIG_CPU_SUBTYPE_SH7720) || \
206 defined(CONFIG_CPU_SUBTYPE_SH7780)
207 #define dmaor_read_reg() ctrl_inw(DMAOR)
208 #define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
210 #define dmaor_read_reg() ctrl_inl(DMAOR)
211 #define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
214 static inline int dmaor_reset(void)
216 unsigned long dmaor = dmaor_read_reg();
218 /* Try to clear the error flags first, incase they are set */
219 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
220 dmaor_write_reg(dmaor);
223 dmaor_write_reg(dmaor);
225 /* See if we got an error again */
226 if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
227 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
234 #if defined(CONFIG_CPU_SH4)
235 static irqreturn_t dma_err(int irq, void *dummy)
244 static struct dma_ops sh_dmac_ops = {
245 .request = sh_dmac_request_dma,
246 .free = sh_dmac_free_dma,
247 .get_residue = sh_dmac_get_dma_residue,
248 .xfer = sh_dmac_xfer_dma,
249 .configure = sh_dmac_configure_channel,
252 static struct dma_info sh_dmac_info = {
254 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
256 .flags = DMAC_CHANNELS_TEI_CAPABLE,
259 static int __init sh_dmac_init(void)
261 struct dma_info *info = &sh_dmac_info;
264 #ifdef CONFIG_CPU_SH4
265 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
271 * Initialize DMAOR, and clean up any error flags that may have
275 if (unlikely(i != 0))
278 return register_dmac(info);
281 static void __exit sh_dmac_exit(void)
283 #ifdef CONFIG_CPU_SH4
284 free_irq(DMAE_IRQ, 0);
286 unregister_dmac(&sh_dmac_info);
289 subsys_initcall(sh_dmac_init);
290 module_exit(sh_dmac_exit);
292 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
293 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
294 MODULE_LICENSE("GPL");