blob: 958bac1c585a9fe91fed90ee7cfdb8605cd3e857 [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
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090022static int dmte_irq_map[] = {
23 DMTE0_IRQ,
24 DMTE1_IRQ,
25 DMTE2_IRQ,
26 DMTE3_IRQ,
Markus Brunner3ea6bc32007-08-20 08:59:33 +090027#if defined(CONFIG_CPU_SUBTYPE_SH7720) || \
28 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090029 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
Kristoffer Ericson4f247e82007-09-11 12:49:59 +090030 defined(CONFIG_CPU_SUBTYPE_SH7709) || \
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090031 defined(CONFIG_CPU_SUBTYPE_SH7780)
32 DMTE4_IRQ,
33 DMTE5_IRQ,
Markus Brunner3ea6bc32007-08-20 08:59:33 +090034#endif
35#if defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
36 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
37 defined(CONFIG_CPU_SUBTYPE_SH7780)
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090038 DMTE6_IRQ,
Markus Brunner3ea6bc32007-08-20 08:59:33 +090039 DMTE7_IRQ,
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090040#endif
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090041};
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090043static inline unsigned int get_dmte_irq(unsigned int chan)
44{
45 unsigned int irq = 0;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090046 if (chan < ARRAY_SIZE(dmte_irq_map))
47 irq = dmte_irq_map[chan];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return irq;
49}
50
51/*
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:
54 *
55 * info->count >> ts_shift[transmit_size]
56 *
57 * iterations to complete the transfer.
58 */
59static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
60{
61 u32 chcr = ctrl_inl(CHCR[chan->chan]);
62
Paul Mundt0d831772006-01-16 22:14:09 -080063 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66/*
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.
71 */
Paul Mundt35f3c512006-10-06 15:31:16 +090072static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Paul Mundt35f3c512006-10-06 15:31:16 +090074 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 chcr;
76
77 chcr = ctrl_inl(CHCR[chan->chan]);
78
79 if (!(chcr & CHCR_TE))
80 return IRQ_NONE;
81
82 chcr &= ~(CHCR_IE | CHCR_DE);
83 ctrl_outl(chcr, CHCR[chan->chan]);
84
85 wake_up(&chan->wait_queue);
86
87 return IRQ_HANDLED;
88}
89
90static int sh_dmac_request_dma(struct dma_channel *chan)
91{
Paul Mundt9e3043c2006-09-27 16:55:24 +090092 if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
93 return 0;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Paul Mundte803aaf2006-11-24 14:50:05 +090096 IRQF_DISABLED, chan->dev_id, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static void sh_dmac_free_dma(struct dma_channel *chan)
100{
101 free_irq(get_dmte_irq(chan->chan), chan);
102}
103
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900104static int
Paul Mundt0d831772006-01-16 22:14:09 -0800105sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800108 chcr = RS_DUAL | CHCR_IE;
109
110 if (chcr & CHCR_IE) {
111 chcr &= ~CHCR_IE;
112 chan->flags |= DMA_TEI_CAPABLE;
113 } else {
114 chan->flags &= ~DMA_TEI_CAPABLE;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 ctrl_outl(chcr, CHCR[chan->chan]);
118
119 chan->flags |= DMA_CONFIGURED;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void sh_dmac_enable_dma(struct dma_channel *chan)
124{
Paul Mundt0d831772006-01-16 22:14:09 -0800125 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 u32 chcr;
127
128 chcr = ctrl_inl(CHCR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800129 chcr |= CHCR_DE;
130
131 if (chan->flags & DMA_TEI_CAPABLE)
132 chcr |= CHCR_IE;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 ctrl_outl(chcr, CHCR[chan->chan]);
135
Paul Mundt0d831772006-01-16 22:14:09 -0800136 if (chan->flags & DMA_TEI_CAPABLE) {
137 irq = get_dmte_irq(chan->chan);
138 enable_irq(irq);
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142static void sh_dmac_disable_dma(struct dma_channel *chan)
143{
Paul Mundt0d831772006-01-16 22:14:09 -0800144 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 u32 chcr;
146
Paul Mundt0d831772006-01-16 22:14:09 -0800147 if (chan->flags & DMA_TEI_CAPABLE) {
148 irq = get_dmte_irq(chan->chan);
149 disable_irq(irq);
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 chcr = ctrl_inl(CHCR[chan->chan]);
153 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
154 ctrl_outl(chcr, CHCR[chan->chan]);
155}
156
157static int sh_dmac_xfer_dma(struct dma_channel *chan)
158{
159 /*
160 * If we haven't pre-configured the channel with special flags, use
161 * the defaults.
162 */
Paul Mundt0d831772006-01-16 22:14:09 -0800163 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 sh_dmac_configure_channel(chan, 0);
165
166 sh_dmac_disable_dma(chan);
167
168 /*
169 * Single-address mode usage note!
170 *
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.
174 *
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.
178 *
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.
182 */
Paul Mundt0d831772006-01-16 22:14:09 -0800183 if (chan->sar || (mach_is_dreamcast() &&
184 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ctrl_outl(chan->sar, SAR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800186 if (chan->dar || (mach_is_dreamcast() &&
187 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ctrl_outl(chan->dar, DAR[chan->chan]);
189
190 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
191
192 sh_dmac_enable_dma(chan);
193
194 return 0;
195}
196
197static int sh_dmac_get_dma_residue(struct dma_channel *chan)
198{
199 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
200 return 0;
201
202 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
203}
204
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900205#if defined(CONFIG_CPU_SUBTYPE_SH7720) || \
206 defined(CONFIG_CPU_SUBTYPE_SH7780)
Paul Mundt0d831772006-01-16 22:14:09 -0800207#define dmaor_read_reg() ctrl_inw(DMAOR)
208#define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
209#else
210#define dmaor_read_reg() ctrl_inl(DMAOR)
211#define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
212#endif
213
214static inline int dmaor_reset(void)
215{
216 unsigned long dmaor = dmaor_read_reg();
217
218 /* Try to clear the error flags first, incase they are set */
219 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
220 dmaor_write_reg(dmaor);
221
222 dmaor |= DMAOR_INIT;
223 dmaor_write_reg(dmaor);
224
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");
228 return -EINVAL;
229 }
230
231 return 0;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900235static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Paul Mundt0d831772006-01-16 22:14:09 -0800237 dmaor_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 disable_irq(irq);
239
240 return IRQ_HANDLED;
241}
242#endif
243
244static 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,
250};
251
252static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800253 .name = "sh_dmac",
254 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .ops = &sh_dmac_ops,
256 .flags = DMAC_CHANNELS_TEI_CAPABLE,
257};
258
259static int __init sh_dmac_init(void)
260{
261 struct dma_info *info = &sh_dmac_info;
262 int i;
263
264#ifdef CONFIG_CPU_SH4
Thomas Gleixner6d208192006-07-01 19:29:25 -0700265 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900266 if (unlikely(i < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return i;
268#endif
269
Paul Mundt0d831772006-01-16 22:14:09 -0800270 /*
271 * Initialize DMAOR, and clean up any error flags that may have
272 * been set.
273 */
274 i = dmaor_reset();
Paul Mundt9e3043c2006-09-27 16:55:24 +0900275 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800276 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 return register_dmac(info);
279}
280
281static void __exit sh_dmac_exit(void)
282{
283#ifdef CONFIG_CPU_SH4
284 free_irq(DMAE_IRQ, 0);
285#endif
Paul Mundt0d831772006-01-16 22:14:09 -0800286 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289subsys_initcall(sh_dmac_init);
290module_exit(sh_dmac_exit);
291
Paul Mundt0d831772006-01-16 22:14:09 -0800292MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
293MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294MODULE_LICENSE("GPL");