blob: 4428ee809651521a09c9fbd3d610855a4ab8f3c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/drivers/dma/dma-sh.c
3 *
4 * SuperH On-chip DMAC Support
5 *
6 * Copyright (C) 2000 Takashi YOSHII
7 * Copyright (C) 2003, 2004 Paul Mundt
Paul Mundt0d831772006-01-16 22:14:09 -08008 * Copyright (C) 2005 Andriy Skulysh
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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
12 * for more details.
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
16#include <linux/module.h>
Paul Mundt0d831772006-01-16 22:14:09 -080017#include <asm/dreamcast/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/dma.h>
19#include <asm/io.h>
20#include "dma-sh.h"
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022static inline unsigned int get_dmte_irq(unsigned int chan)
23{
Paul Mundt0d831772006-01-16 22:14:09 -080024 unsigned int irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26 /*
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
29 * the SCIF
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (chan < 4) {
32 irq = DMTE0_IRQ + chan;
33 } else {
Paul Mundt0d831772006-01-16 22:14:09 -080034#ifdef DMTE4_IRQ
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 irq = DMTE4_IRQ + chan - 4;
Paul Mundt0d831772006-01-16 22:14:09 -080036#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 }
38
39 return irq;
40}
41
42/*
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:
45 *
46 * info->count >> ts_shift[transmit_size]
47 *
48 * iterations to complete the transfer.
49 */
50static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
51{
52 u32 chcr = ctrl_inl(CHCR[chan->chan]);
53
Paul Mundt0d831772006-01-16 22:14:09 -080054 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57/*
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.
62 */
63static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
64{
65 struct dma_channel *chan = (struct dma_channel *)dev_id;
66 u32 chcr;
67
68 chcr = ctrl_inl(CHCR[chan->chan]);
69
70 if (!(chcr & CHCR_TE))
71 return IRQ_NONE;
72
73 chcr &= ~(CHCR_IE | CHCR_DE);
74 ctrl_outl(chcr, CHCR[chan->chan]);
75
76 wake_up(&chan->wait_queue);
77
78 return IRQ_HANDLED;
79}
80
81static int sh_dmac_request_dma(struct dma_channel *chan)
82{
Paul Mundt0d831772006-01-16 22:14:09 -080083 char name[32];
84
85 snprintf(name, sizeof(name), "DMAC Transfer End (Channel %d)",
86 chan->chan);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Thomas Gleixner6d208192006-07-01 19:29:25 -070089 IRQF_DISABLED, name, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92static void sh_dmac_free_dma(struct dma_channel *chan)
93{
94 free_irq(get_dmte_irq(chan->chan), chan);
95}
96
Paul Mundt0d831772006-01-16 22:14:09 -080097static void
98sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800101 chcr = RS_DUAL | CHCR_IE;
102
103 if (chcr & CHCR_IE) {
104 chcr &= ~CHCR_IE;
105 chan->flags |= DMA_TEI_CAPABLE;
106 } else {
107 chan->flags &= ~DMA_TEI_CAPABLE;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 ctrl_outl(chcr, CHCR[chan->chan]);
111
112 chan->flags |= DMA_CONFIGURED;
113}
114
115static void sh_dmac_enable_dma(struct dma_channel *chan)
116{
Paul Mundt0d831772006-01-16 22:14:09 -0800117 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 u32 chcr;
119
120 chcr = ctrl_inl(CHCR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800121 chcr |= CHCR_DE;
122
123 if (chan->flags & DMA_TEI_CAPABLE)
124 chcr |= CHCR_IE;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ctrl_outl(chcr, CHCR[chan->chan]);
127
Paul Mundt0d831772006-01-16 22:14:09 -0800128 if (chan->flags & DMA_TEI_CAPABLE) {
129 irq = get_dmte_irq(chan->chan);
130 enable_irq(irq);
131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static void sh_dmac_disable_dma(struct dma_channel *chan)
135{
Paul Mundt0d831772006-01-16 22:14:09 -0800136 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 u32 chcr;
138
Paul Mundt0d831772006-01-16 22:14:09 -0800139 if (chan->flags & DMA_TEI_CAPABLE) {
140 irq = get_dmte_irq(chan->chan);
141 disable_irq(irq);
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 chcr = ctrl_inl(CHCR[chan->chan]);
145 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
146 ctrl_outl(chcr, CHCR[chan->chan]);
147}
148
149static int sh_dmac_xfer_dma(struct dma_channel *chan)
150{
151 /*
152 * If we haven't pre-configured the channel with special flags, use
153 * the defaults.
154 */
Paul Mundt0d831772006-01-16 22:14:09 -0800155 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sh_dmac_configure_channel(chan, 0);
157
158 sh_dmac_disable_dma(chan);
159
160 /*
161 * Single-address mode usage note!
162 *
163 * It's important that we don't accidentally write any value to SAR/DAR
164 * (this includes 0) that hasn't been directly specified by the user if
165 * we're in single-address mode.
166 *
167 * In this case, only one address can be defined, anything else will
168 * result in a DMA address error interrupt (at least on the SH-4),
169 * which will subsequently halt the transfer.
170 *
171 * Channel 2 on the Dreamcast is a special case, as this is used for
172 * cascading to the PVR2 DMAC. In this case, we still need to write
173 * SAR and DAR, regardless of value, in order for cascading to work.
174 */
Paul Mundt0d831772006-01-16 22:14:09 -0800175 if (chan->sar || (mach_is_dreamcast() &&
176 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 ctrl_outl(chan->sar, SAR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800178 if (chan->dar || (mach_is_dreamcast() &&
179 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ctrl_outl(chan->dar, DAR[chan->chan]);
181
182 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
183
184 sh_dmac_enable_dma(chan);
185
186 return 0;
187}
188
189static int sh_dmac_get_dma_residue(struct dma_channel *chan)
190{
191 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
192 return 0;
193
194 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
195}
196
Paul Mundt0d831772006-01-16 22:14:09 -0800197#ifdef CONFIG_CPU_SUBTYPE_SH7780
198#define dmaor_read_reg() ctrl_inw(DMAOR)
199#define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
200#else
201#define dmaor_read_reg() ctrl_inl(DMAOR)
202#define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
203#endif
204
205static inline int dmaor_reset(void)
206{
207 unsigned long dmaor = dmaor_read_reg();
208
209 /* Try to clear the error flags first, incase they are set */
210 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
211 dmaor_write_reg(dmaor);
212
213 dmaor |= DMAOR_INIT;
214 dmaor_write_reg(dmaor);
215
216 /* See if we got an error again */
217 if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
218 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
219 return -EINVAL;
220 }
221
222 return 0;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225#if defined(CONFIG_CPU_SH4)
226static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
227{
Paul Mundt0d831772006-01-16 22:14:09 -0800228 dmaor_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 disable_irq(irq);
230
231 return IRQ_HANDLED;
232}
233#endif
234
235static struct dma_ops sh_dmac_ops = {
236 .request = sh_dmac_request_dma,
237 .free = sh_dmac_free_dma,
238 .get_residue = sh_dmac_get_dma_residue,
239 .xfer = sh_dmac_xfer_dma,
240 .configure = sh_dmac_configure_channel,
241};
242
243static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800244 .name = "sh_dmac",
245 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .ops = &sh_dmac_ops,
247 .flags = DMAC_CHANNELS_TEI_CAPABLE,
248};
249
250static int __init sh_dmac_init(void)
251{
252 struct dma_info *info = &sh_dmac_info;
253 int i;
254
255#ifdef CONFIG_CPU_SH4
256 make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
Thomas Gleixner6d208192006-07-01 19:29:25 -0700257 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (i < 0)
259 return i;
260#endif
261
262 for (i = 0; i < info->nr_channels; i++) {
263 int irq = get_dmte_irq(i);
264
265 make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
266 }
267
Paul Mundt0d831772006-01-16 22:14:09 -0800268 /*
269 * Initialize DMAOR, and clean up any error flags that may have
270 * been set.
271 */
272 i = dmaor_reset();
273 if (i < 0)
274 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return register_dmac(info);
277}
278
279static void __exit sh_dmac_exit(void)
280{
281#ifdef CONFIG_CPU_SH4
282 free_irq(DMAE_IRQ, 0);
283#endif
Paul Mundt0d831772006-01-16 22:14:09 -0800284 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287subsys_initcall(sh_dmac_init);
288module_exit(sh_dmac_exit);
289
Paul Mundt0d831772006-01-16 22:14:09 -0800290MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
291MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292MODULE_LICENSE("GPL");