blob: 06ed0609a95d6c58ecf476d483f0ef6ea617e83e [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,
27#if defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
28 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
29 defined(CONFIG_CPU_SUBTYPE_SH7780)
30 DMTE4_IRQ,
31 DMTE5_IRQ,
32 DMTE6_IRQ,
33 DMTE7_IRQ,
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090034#endif
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090035};
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090037static inline unsigned int get_dmte_irq(unsigned int chan)
38{
39 unsigned int irq = 0;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090040 if (chan < ARRAY_SIZE(dmte_irq_map))
41 irq = dmte_irq_map[chan];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return irq;
43}
44
45/*
46 * We determine the correct shift size based off of the CHCR transmit size
47 * for the given channel. Since we know that it will take:
48 *
49 * info->count >> ts_shift[transmit_size]
50 *
51 * iterations to complete the transfer.
52 */
53static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
54{
55 u32 chcr = ctrl_inl(CHCR[chan->chan]);
56
Paul Mundt0d831772006-01-16 22:14:09 -080057 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60/*
61 * The transfer end interrupt must read the chcr register to end the
62 * hardware interrupt active condition.
63 * Besides that it needs to waken any waiting process, which should handle
64 * setting up the next transfer.
65 */
Paul Mundt35f3c512006-10-06 15:31:16 +090066static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Paul Mundt35f3c512006-10-06 15:31:16 +090068 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 u32 chcr;
70
71 chcr = ctrl_inl(CHCR[chan->chan]);
72
73 if (!(chcr & CHCR_TE))
74 return IRQ_NONE;
75
76 chcr &= ~(CHCR_IE | CHCR_DE);
77 ctrl_outl(chcr, CHCR[chan->chan]);
78
79 wake_up(&chan->wait_queue);
80
81 return IRQ_HANDLED;
82}
83
84static int sh_dmac_request_dma(struct dma_channel *chan)
85{
Paul Mundt9e3043c2006-09-27 16:55:24 +090086 if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
87 return 0;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Paul Mundte803aaf2006-11-24 14:50:05 +090090 IRQF_DISABLED, chan->dev_id, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93static void sh_dmac_free_dma(struct dma_channel *chan)
94{
95 free_irq(get_dmte_irq(chan->chan), chan);
96}
97
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090098static int
Paul Mundt0d831772006-01-16 22:14:09 -080099sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800102 chcr = RS_DUAL | CHCR_IE;
103
104 if (chcr & CHCR_IE) {
105 chcr &= ~CHCR_IE;
106 chan->flags |= DMA_TEI_CAPABLE;
107 } else {
108 chan->flags &= ~DMA_TEI_CAPABLE;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 ctrl_outl(chcr, CHCR[chan->chan]);
112
113 chan->flags |= DMA_CONFIGURED;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void sh_dmac_enable_dma(struct dma_channel *chan)
118{
Paul Mundt0d831772006-01-16 22:14:09 -0800119 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 u32 chcr;
121
122 chcr = ctrl_inl(CHCR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800123 chcr |= CHCR_DE;
124
125 if (chan->flags & DMA_TEI_CAPABLE)
126 chcr |= CHCR_IE;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 ctrl_outl(chcr, CHCR[chan->chan]);
129
Paul Mundt0d831772006-01-16 22:14:09 -0800130 if (chan->flags & DMA_TEI_CAPABLE) {
131 irq = get_dmte_irq(chan->chan);
132 enable_irq(irq);
133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static void sh_dmac_disable_dma(struct dma_channel *chan)
137{
Paul Mundt0d831772006-01-16 22:14:09 -0800138 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 u32 chcr;
140
Paul Mundt0d831772006-01-16 22:14:09 -0800141 if (chan->flags & DMA_TEI_CAPABLE) {
142 irq = get_dmte_irq(chan->chan);
143 disable_irq(irq);
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 chcr = ctrl_inl(CHCR[chan->chan]);
147 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
148 ctrl_outl(chcr, CHCR[chan->chan]);
149}
150
151static int sh_dmac_xfer_dma(struct dma_channel *chan)
152{
153 /*
154 * If we haven't pre-configured the channel with special flags, use
155 * the defaults.
156 */
Paul Mundt0d831772006-01-16 22:14:09 -0800157 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 sh_dmac_configure_channel(chan, 0);
159
160 sh_dmac_disable_dma(chan);
161
162 /*
163 * Single-address mode usage note!
164 *
165 * It's important that we don't accidentally write any value to SAR/DAR
166 * (this includes 0) that hasn't been directly specified by the user if
167 * we're in single-address mode.
168 *
169 * In this case, only one address can be defined, anything else will
170 * result in a DMA address error interrupt (at least on the SH-4),
171 * which will subsequently halt the transfer.
172 *
173 * Channel 2 on the Dreamcast is a special case, as this is used for
174 * cascading to the PVR2 DMAC. In this case, we still need to write
175 * SAR and DAR, regardless of value, in order for cascading to work.
176 */
Paul Mundt0d831772006-01-16 22:14:09 -0800177 if (chan->sar || (mach_is_dreamcast() &&
178 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 ctrl_outl(chan->sar, SAR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800180 if (chan->dar || (mach_is_dreamcast() &&
181 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ctrl_outl(chan->dar, DAR[chan->chan]);
183
184 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
185
186 sh_dmac_enable_dma(chan);
187
188 return 0;
189}
190
191static int sh_dmac_get_dma_residue(struct dma_channel *chan)
192{
193 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
194 return 0;
195
196 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
197}
198
Paul Mundt0d831772006-01-16 22:14:09 -0800199#ifdef CONFIG_CPU_SUBTYPE_SH7780
200#define dmaor_read_reg() ctrl_inw(DMAOR)
201#define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
202#else
203#define dmaor_read_reg() ctrl_inl(DMAOR)
204#define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
205#endif
206
207static inline int dmaor_reset(void)
208{
209 unsigned long dmaor = dmaor_read_reg();
210
211 /* Try to clear the error flags first, incase they are set */
212 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
213 dmaor_write_reg(dmaor);
214
215 dmaor |= DMAOR_INIT;
216 dmaor_write_reg(dmaor);
217
218 /* See if we got an error again */
219 if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
220 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
221 return -EINVAL;
222 }
223
224 return 0;
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900228static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Paul Mundt0d831772006-01-16 22:14:09 -0800230 dmaor_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 disable_irq(irq);
232
233 return IRQ_HANDLED;
234}
235#endif
236
237static struct dma_ops sh_dmac_ops = {
238 .request = sh_dmac_request_dma,
239 .free = sh_dmac_free_dma,
240 .get_residue = sh_dmac_get_dma_residue,
241 .xfer = sh_dmac_xfer_dma,
242 .configure = sh_dmac_configure_channel,
243};
244
245static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800246 .name = "sh_dmac",
247 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .ops = &sh_dmac_ops,
249 .flags = DMAC_CHANNELS_TEI_CAPABLE,
250};
251
252static int __init sh_dmac_init(void)
253{
254 struct dma_info *info = &sh_dmac_info;
255 int i;
256
257#ifdef CONFIG_CPU_SH4
Thomas Gleixner6d208192006-07-01 19:29:25 -0700258 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900259 if (unlikely(i < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return i;
261#endif
262
Paul Mundt0d831772006-01-16 22:14:09 -0800263 /*
264 * Initialize DMAOR, and clean up any error flags that may have
265 * been set.
266 */
267 i = dmaor_reset();
Paul Mundt9e3043c2006-09-27 16:55:24 +0900268 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800269 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 return register_dmac(info);
272}
273
274static void __exit sh_dmac_exit(void)
275{
276#ifdef CONFIG_CPU_SH4
277 free_irq(DMAE_IRQ, 0);
278#endif
Paul Mundt0d831772006-01-16 22:14:09 -0800279 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282subsys_initcall(sh_dmac_init);
283module_exit(sh_dmac_exit);
284
Paul Mundt0d831772006-01-16 22:14:09 -0800285MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
286MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287MODULE_LICENSE("GPL");