blob: d8ece20bb2cf078fd913facab2fdc4def5d1f384 [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 */
Paul Mundt35f3c512006-10-06 15:31:16 +090063static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Paul Mundt35f3c512006-10-06 15:31:16 +090065 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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 Mundt9e3043c2006-09-27 16:55:24 +090083 if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
84 return 0;
85
Paul Mundt0f08f332006-09-27 17:03:56 +090086 chan->name = kzalloc(32, GFP_KERNEL);
87 if (unlikely(chan->name == NULL))
88 return -ENOMEM;
89 snprintf(chan->name, 32, "DMAC Transfer End (Channel %d)",
Paul Mundt0d831772006-01-16 22:14:09 -080090 chan->chan);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Paul Mundt0f08f332006-09-27 17:03:56 +090093 IRQF_DISABLED, chan->name, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96static void sh_dmac_free_dma(struct dma_channel *chan)
97{
98 free_irq(get_dmte_irq(chan->chan), chan);
Paul Mundt0f08f332006-09-27 17:03:56 +090099 kfree(chan->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Paul Mundt0d831772006-01-16 22:14:09 -0800102static void
103sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800106 chcr = RS_DUAL | CHCR_IE;
107
108 if (chcr & CHCR_IE) {
109 chcr &= ~CHCR_IE;
110 chan->flags |= DMA_TEI_CAPABLE;
111 } else {
112 chan->flags &= ~DMA_TEI_CAPABLE;
113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 ctrl_outl(chcr, CHCR[chan->chan]);
116
117 chan->flags |= DMA_CONFIGURED;
118}
119
120static void sh_dmac_enable_dma(struct dma_channel *chan)
121{
Paul Mundt0d831772006-01-16 22:14:09 -0800122 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 u32 chcr;
124
125 chcr = ctrl_inl(CHCR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800126 chcr |= CHCR_DE;
127
128 if (chan->flags & DMA_TEI_CAPABLE)
129 chcr |= CHCR_IE;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 ctrl_outl(chcr, CHCR[chan->chan]);
132
Paul Mundt0d831772006-01-16 22:14:09 -0800133 if (chan->flags & DMA_TEI_CAPABLE) {
134 irq = get_dmte_irq(chan->chan);
135 enable_irq(irq);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static void sh_dmac_disable_dma(struct dma_channel *chan)
140{
Paul Mundt0d831772006-01-16 22:14:09 -0800141 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 u32 chcr;
143
Paul Mundt0d831772006-01-16 22:14:09 -0800144 if (chan->flags & DMA_TEI_CAPABLE) {
145 irq = get_dmte_irq(chan->chan);
146 disable_irq(irq);
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 chcr = ctrl_inl(CHCR[chan->chan]);
150 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
151 ctrl_outl(chcr, CHCR[chan->chan]);
152}
153
154static int sh_dmac_xfer_dma(struct dma_channel *chan)
155{
156 /*
157 * If we haven't pre-configured the channel with special flags, use
158 * the defaults.
159 */
Paul Mundt0d831772006-01-16 22:14:09 -0800160 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 sh_dmac_configure_channel(chan, 0);
162
163 sh_dmac_disable_dma(chan);
164
165 /*
166 * Single-address mode usage note!
167 *
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.
171 *
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.
175 *
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.
179 */
Paul Mundt0d831772006-01-16 22:14:09 -0800180 if (chan->sar || (mach_is_dreamcast() &&
181 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ctrl_outl(chan->sar, SAR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800183 if (chan->dar || (mach_is_dreamcast() &&
184 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ctrl_outl(chan->dar, DAR[chan->chan]);
186
187 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
188
189 sh_dmac_enable_dma(chan);
190
191 return 0;
192}
193
194static int sh_dmac_get_dma_residue(struct dma_channel *chan)
195{
196 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
197 return 0;
198
199 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
200}
201
Paul Mundt0d831772006-01-16 22:14:09 -0800202#ifdef CONFIG_CPU_SUBTYPE_SH7780
203#define dmaor_read_reg() ctrl_inw(DMAOR)
204#define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
205#else
206#define dmaor_read_reg() ctrl_inl(DMAOR)
207#define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
208#endif
209
210static inline int dmaor_reset(void)
211{
212 unsigned long dmaor = dmaor_read_reg();
213
214 /* Try to clear the error flags first, incase they are set */
215 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
216 dmaor_write_reg(dmaor);
217
218 dmaor |= DMAOR_INIT;
219 dmaor_write_reg(dmaor);
220
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");
224 return -EINVAL;
225 }
226
227 return 0;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900231static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Paul Mundt0d831772006-01-16 22:14:09 -0800233 dmaor_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 disable_irq(irq);
235
236 return IRQ_HANDLED;
237}
238#endif
239
240static 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,
246};
247
248static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800249 .name = "sh_dmac",
250 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .ops = &sh_dmac_ops,
252 .flags = DMAC_CHANNELS_TEI_CAPABLE,
253};
254
255static int __init sh_dmac_init(void)
256{
257 struct dma_info *info = &sh_dmac_info;
258 int i;
259
260#ifdef CONFIG_CPU_SH4
261 make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
Thomas Gleixner6d208192006-07-01 19:29:25 -0700262 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900263 if (unlikely(i < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return i;
265#endif
266
267 for (i = 0; i < info->nr_channels; i++) {
268 int irq = get_dmte_irq(i);
269
270 make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
271 }
272
Paul Mundt0d831772006-01-16 22:14:09 -0800273 /*
274 * Initialize DMAOR, and clean up any error flags that may have
275 * been set.
276 */
277 i = dmaor_reset();
Paul Mundt9e3043c2006-09-27 16:55:24 +0900278 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800279 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 return register_dmac(info);
282}
283
284static void __exit sh_dmac_exit(void)
285{
286#ifdef CONFIG_CPU_SH4
287 free_irq(DMAE_IRQ, 0);
288#endif
Paul Mundt0d831772006-01-16 22:14:09 -0800289 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292subsys_initcall(sh_dmac_init);
293module_exit(sh_dmac_exit);
294
Paul Mundt0d831772006-01-16 22:14:09 -0800295MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
296MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297MODULE_LICENSE("GPL");