2 * arch/sh/kernel/cpu/dma.c
4 * Copyright (C) 2000 Takashi YOSHII
5 * Copyright (C) 2003 Paul Mundt
7 * PC like DMA API for SuperH's DMAC.
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/module.h>
19 #include <asm/signal.h>
26 * The SuperH DMAC supports a number of transmit sizes, we list them here,
27 * with their respective values as they appear in the CHCR registers.
29 * Defaults to a 64-bit transfer size.
40 * The DMA count is defined as the number of bytes to transfer.
42 static unsigned int ts_shift[] = {
50 struct sh_dmac_channel {
55 } __attribute__ ((aligned(16)));
58 struct sh_dmac_channel channel[4];
62 static volatile struct sh_dmac_info *sh_dmac = (volatile struct sh_dmac_info *)SH_DMAC_BASE;
64 static inline unsigned int get_dmte_irq(unsigned int chan)
69 * Normally we could just do DMTE0_IRQ + chan outright, though in the
70 * case of the 7751R, the DMTE IRQs for channels > 4 start right above
75 irq = DMTE0_IRQ + chan;
77 irq = DMTE4_IRQ + chan - 4;
84 * We determine the correct shift size based off of the CHCR transmit size
85 * for the given channel. Since we know that it will take:
87 * info->count >> ts_shift[transmit_size]
89 * iterations to complete the transfer.
91 static inline unsigned int calc_xmit_shift(struct dma_info *info)
93 return ts_shift[(sh_dmac->channel[info->chan].chcr >> 4) & 0x0007];
97 * The transfer end interrupt must read the chcr register to end the
98 * hardware interrupt active condition.
99 * Besides that it needs to waken any waiting process, which should handle
100 * setting up the next transfer.
102 static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
104 struct dma_info * info = (struct dma_info *)dev_id;
105 u32 chcr = sh_dmac->channel[info->chan].chcr;
107 if (!(chcr & CHCR_TE))
110 sh_dmac->channel[info->chan].chcr = chcr & ~(CHCR_IE | CHCR_DE);
112 wake_up(&info->wait_queue);
117 static int sh_dmac_request_dma(struct dma_info *info)
119 return request_irq(get_dmte_irq(info->chan), dma_tei,
120 SA_INTERRUPT, "DMAC Transfer End", info);
123 static void sh_dmac_free_dma(struct dma_info *info)
125 free_irq(get_dmte_irq(info->chan), info);
128 static void sh_dmac_configure_channel(struct dma_info *info, unsigned long chcr)
133 sh_dmac->channel[info->chan].chcr = chcr;
135 info->configured = 1;
138 static void sh_dmac_enable_dma(struct dma_info *info)
140 int irq = get_dmte_irq(info->chan);
142 sh_dmac->channel[info->chan].chcr |= (CHCR_DE | CHCR_IE);
146 static void sh_dmac_disable_dma(struct dma_info *info)
148 int irq = get_dmte_irq(info->chan);
151 sh_dmac->channel[info->chan].chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
154 static int sh_dmac_xfer_dma(struct dma_info *info)
157 * If we haven't pre-configured the channel with special flags, use
160 if (!info->configured)
161 sh_dmac_configure_channel(info, 0);
163 sh_dmac_disable_dma(info);
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 (info->sar || (mach_is_dreamcast() && info->chan == 2))
181 sh_dmac->channel[info->chan].sar = info->sar;
182 if (info->dar || (mach_is_dreamcast() && info->chan == 2))
183 sh_dmac->channel[info->chan].dar = info->dar;
185 sh_dmac->channel[info->chan].dmatcr = info->count >> calc_xmit_shift(info);
187 sh_dmac_enable_dma(info);
192 static int sh_dmac_get_dma_residue(struct dma_info *info)
194 if (!(sh_dmac->channel[info->chan].chcr & CHCR_DE))
197 return sh_dmac->channel[info->chan].dmatcr << calc_xmit_shift(info);
200 #if defined(CONFIG_CPU_SH4)
201 static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
203 printk("DMAE: DMAOR=%lx\n", sh_dmac->dmaor);
205 sh_dmac->dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
206 sh_dmac->dmaor |= DMAOR_DME;
214 static struct dma_ops sh_dmac_ops = {
215 .name = "SuperH DMAC",
216 .request = sh_dmac_request_dma,
217 .free = sh_dmac_free_dma,
218 .get_residue = sh_dmac_get_dma_residue,
219 .xfer = sh_dmac_xfer_dma,
220 .configure = sh_dmac_configure_channel,
223 static int __init sh_dmac_init(void)
227 #ifdef CONFIG_CPU_SH4
228 make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
229 i = request_irq(DMAE_IRQ, dma_err, SA_INTERRUPT, "DMAC Address Error", 0);
234 for (i = 0; i < MAX_DMAC_CHANNELS; i++) {
235 int irq = get_dmte_irq(i);
237 make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
239 dma_info[i].ops = &sh_dmac_ops;
240 dma_info[i].tei_capable = 1;
243 sh_dmac->dmaor |= 0x8000 | DMAOR_DME;
245 return register_dmac(&sh_dmac_ops);
248 static void __exit sh_dmac_exit(void)
250 #ifdef CONFIG_CPU_SH4
251 free_irq(DMAE_IRQ, 0);
255 subsys_initcall(sh_dmac_init);
256 module_exit(sh_dmac_exit);
258 MODULE_LICENSE("GPL");