blob: b2256562314298e0e63918b7f60323691e08d5b3 [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 Mundt7f47c7182012-05-19 20:38:50 +090017#include <linux/io.h>
Paul Mundt71b80642008-07-29 20:20:36 +090018#include <mach-dreamcast/mach/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/dma.h>
Paul Mundt7f47c7182012-05-19 20:38:50 +090020#include <asm/dma-register.h>
21#include <cpu/dma-register.h>
22#include <cpu/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Paul Mundt7f47c7182012-05-19 20:38:50 +090024/*
25 * Define the default configuration for dual address memory-memory transfer.
26 * The 0x400 value represents auto-request, external->external.
27 */
28#define RS_DUAL (DM_INC | SM_INC | 0x400 | TS_INDEX2VAL(XMIT_SZ_32BIT))
29
30static unsigned long dma_find_base(unsigned int chan)
31{
32 unsigned long base = SH_DMAC_BASE0;
33
34#ifdef SH_DMAC_BASE1
35 if (chan >= 6)
36 base = SH_DMAC_BASE1;
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090037#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090038
Paul Mundt7f47c7182012-05-19 20:38:50 +090039 return base;
40}
41
42static unsigned long dma_base_addr(unsigned int chan)
43{
44 unsigned long base = dma_find_base(chan);
45
46 /* Normalize offset calculation */
47 if (chan >= 9)
48 chan -= 6;
49 if (chan >= 4)
50 base += 0x10;
51
52 return base + (chan * 0x10);
53}
54
55#ifdef CONFIG_SH_DMA_IRQ_MULTI
56static inline unsigned int get_dmte_irq(unsigned int chan)
57{
58 return chan >= 6 ? DMTE6_IRQ : DMTE0_IRQ;
59}
60#else
61
62static unsigned int dmte_irq_map[] = {
63 DMTE0_IRQ, DMTE0_IRQ + 1, DMTE0_IRQ + 2, DMTE0_IRQ + 3,
64
65#ifdef DMTE4_IRQ
66 DMTE4_IRQ, DMTE4_IRQ + 1,
67#endif
68
69#ifdef DMTE6_IRQ
70 DMTE6_IRQ, DMTE6_IRQ + 1,
71#endif
72
73#ifdef DMTE8_IRQ
74 DMTE8_IRQ, DMTE9_IRQ, DMTE10_IRQ, DMTE11_IRQ,
75#endif
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090076};
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090078static inline unsigned int get_dmte_irq(unsigned int chan)
79{
Paul Mundt7f47c7182012-05-19 20:38:50 +090080 return dmte_irq_map[chan];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Paul Mundt7f47c7182012-05-19 20:38:50 +090082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * We determine the correct shift size based off of the CHCR transmit size
86 * for the given channel. Since we know that it will take:
87 *
88 * info->count >> ts_shift[transmit_size]
89 *
90 * iterations to complete the transfer.
91 */
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +000092static unsigned int ts_shift[] = TS_SHIFT;
Paul Mundt7f47c7182012-05-19 20:38:50 +090093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
95{
Paul Mundt7f47c7182012-05-19 20:38:50 +090096 u32 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +000097 int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
98 ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Guennadi Liakhovetski623b4ac2010-02-03 14:44:12 +0000100 return ts_shift[cnt];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
104 * The transfer end interrupt must read the chcr register to end the
105 * hardware interrupt active condition.
106 * Besides that it needs to waken any waiting process, which should handle
107 * setting up the next transfer.
108 */
Paul Mundt35f3c512006-10-06 15:31:16 +0900109static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Paul Mundt35f3c512006-10-06 15:31:16 +0900111 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 u32 chcr;
113
Paul Mundt7f47c7182012-05-19 20:38:50 +0900114 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!(chcr & CHCR_TE))
117 return IRQ_NONE;
118
119 chcr &= ~(CHCR_IE | CHCR_DE);
Paul Mundt7f47c7182012-05-19 20:38:50 +0900120 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 wake_up(&chan->wait_queue);
123
124 return IRQ_HANDLED;
125}
126
127static int sh_dmac_request_dma(struct dma_channel *chan)
128{
Julia Lawallb2d7c7f2008-02-26 21:42:11 +0100129 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
Paul Mundt9e3043c2006-09-27 16:55:24 +0900130 return 0;
131
Paul Mundt7f47c7182012-05-19 20:38:50 +0900132 return request_irq(get_dmte_irq(chan->chan), dma_tei, IRQF_SHARED,
133 chan->dev_id, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static void sh_dmac_free_dma(struct dma_channel *chan)
137{
138 free_irq(get_dmte_irq(chan->chan), chan);
139}
140
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900141static int
Paul Mundt0d831772006-01-16 22:14:09 -0800142sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800145 chcr = RS_DUAL | CHCR_IE;
146
147 if (chcr & CHCR_IE) {
148 chcr &= ~CHCR_IE;
149 chan->flags |= DMA_TEI_CAPABLE;
150 } else {
151 chan->flags &= ~DMA_TEI_CAPABLE;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Paul Mundt7f47c7182012-05-19 20:38:50 +0900154 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 chan->flags |= DMA_CONFIGURED;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static void sh_dmac_enable_dma(struct dma_channel *chan)
161{
Paul Mundt0d831772006-01-16 22:14:09 -0800162 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 u32 chcr;
164
Paul Mundt7f47c7182012-05-19 20:38:50 +0900165 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
Paul Mundt0d831772006-01-16 22:14:09 -0800166 chcr |= CHCR_DE;
167
168 if (chan->flags & DMA_TEI_CAPABLE)
169 chcr |= CHCR_IE;
170
Paul Mundt7f47c7182012-05-19 20:38:50 +0900171 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Paul Mundt0d831772006-01-16 22:14:09 -0800173 if (chan->flags & DMA_TEI_CAPABLE) {
174 irq = get_dmte_irq(chan->chan);
175 enable_irq(irq);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static void sh_dmac_disable_dma(struct dma_channel *chan)
180{
Paul Mundt0d831772006-01-16 22:14:09 -0800181 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 u32 chcr;
183
Paul Mundt0d831772006-01-16 22:14:09 -0800184 if (chan->flags & DMA_TEI_CAPABLE) {
185 irq = get_dmte_irq(chan->chan);
186 disable_irq(irq);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Paul Mundt7f47c7182012-05-19 20:38:50 +0900189 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
Paul Mundt7f47c7182012-05-19 20:38:50 +0900191 __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static int sh_dmac_xfer_dma(struct dma_channel *chan)
195{
196 /*
197 * If we haven't pre-configured the channel with special flags, use
198 * the defaults.
199 */
Paul Mundt0d831772006-01-16 22:14:09 -0800200 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 sh_dmac_configure_channel(chan, 0);
202
203 sh_dmac_disable_dma(chan);
204
205 /*
206 * Single-address mode usage note!
207 *
208 * It's important that we don't accidentally write any value to SAR/DAR
209 * (this includes 0) that hasn't been directly specified by the user if
210 * we're in single-address mode.
211 *
212 * In this case, only one address can be defined, anything else will
213 * result in a DMA address error interrupt (at least on the SH-4),
214 * which will subsequently halt the transfer.
215 *
216 * Channel 2 on the Dreamcast is a special case, as this is used for
217 * cascading to the PVR2 DMAC. In this case, we still need to write
218 * SAR and DAR, regardless of value, in order for cascading to work.
219 */
Paul Mundt0d831772006-01-16 22:14:09 -0800220 if (chan->sar || (mach_is_dreamcast() &&
221 chan->chan == PVR2_CASCADE_CHAN))
Paul Mundt7f47c7182012-05-19 20:38:50 +0900222 __raw_writel(chan->sar, (dma_base_addr(chan->chan) + SAR));
Paul Mundt0d831772006-01-16 22:14:09 -0800223 if (chan->dar || (mach_is_dreamcast() &&
224 chan->chan == PVR2_CASCADE_CHAN))
Paul Mundt7f47c7182012-05-19 20:38:50 +0900225 __raw_writel(chan->dar, (dma_base_addr(chan->chan) + DAR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Paul Mundt9d56dd32010-01-26 12:58:40 +0900227 __raw_writel(chan->count >> calc_xmit_shift(chan),
Paul Mundt7f47c7182012-05-19 20:38:50 +0900228 (dma_base_addr(chan->chan) + TCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 sh_dmac_enable_dma(chan);
231
232 return 0;
233}
234
235static int sh_dmac_get_dma_residue(struct dma_channel *chan)
236{
Paul Mundt7f47c7182012-05-19 20:38:50 +0900237 if (!(__raw_readl(dma_base_addr(chan->chan) + CHCR) & CHCR_DE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239
Paul Mundt7f47c7182012-05-19 20:38:50 +0900240 return __raw_readl(dma_base_addr(chan->chan) + TCR)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900241 << calc_xmit_shift(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Paul Mundt7f47c7182012-05-19 20:38:50 +0900244/*
245 * DMAOR handling
246 */
247#if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
248 defined(CONFIG_CPU_SUBTYPE_SH7724) || \
249 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
250 defined(CONFIG_CPU_SUBTYPE_SH7785)
251#define NR_DMAOR 2
252#else
253#define NR_DMAOR 1
254#endif
255
256/*
257 * DMAOR bases are broken out amongst channel groups. DMAOR0 manages
258 * channels 0 - 5, DMAOR1 6 - 11 (optional).
259 */
260#define dmaor_read_reg(n) __raw_readw(dma_find_base((n)*6))
261#define dmaor_write_reg(n, data) __raw_writew(data, dma_find_base(n)*6)
262
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900263static inline int dmaor_reset(int no)
Paul Mundt0d831772006-01-16 22:14:09 -0800264{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900265 unsigned long dmaor = dmaor_read_reg(no);
Paul Mundt0d831772006-01-16 22:14:09 -0800266
267 /* Try to clear the error flags first, incase they are set */
268 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900269 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800270
271 dmaor |= DMAOR_INIT;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900272 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800273
274 /* See if we got an error again */
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900275 if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
Paul Mundt0d831772006-01-16 22:14:09 -0800276 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
277 return -EINVAL;
278 }
279
280 return 0;
281}
282
Paul Mundt7f47c7182012-05-19 20:38:50 +0900283/*
284 * DMAE handling
285 */
286#ifdef CONFIG_CPU_SH4
287
288#if defined(DMAE1_IRQ)
289#define NR_DMAE 2
290#else
291#define NR_DMAE 1
292#endif
293
294static const char *dmae_name[] = {
295 "DMAC Address Error0",
296 "DMAC Address Error1"
297};
298
299#ifdef CONFIG_SH_DMA_IRQ_MULTI
300static inline unsigned int get_dma_error_irq(int n)
301{
302 return get_dmte_irq(n * 6);
303}
304#else
305
306static unsigned int dmae_irq_map[] = {
307 DMAE0_IRQ,
308
309#ifdef DMAE1_IRQ
310 DMAE1_IRQ,
311#endif
312};
313
314static inline unsigned int get_dma_error_irq(int n)
315{
316 return dmae_irq_map[n];
317}
318#endif
319
Paul Mundt35f3c512006-10-06 15:31:16 +0900320static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Paul Mundt7f47c7182012-05-19 20:38:50 +0900322 int i;
323
324 for (i = 0; i < NR_DMAOR; i++)
325 dmaor_reset(i);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 disable_irq(irq);
328
329 return IRQ_HANDLED;
Paul Mundt7f47c7182012-05-19 20:38:50 +0900330}
331
332static int dmae_irq_init(void)
333{
334 int n;
335
336 for (n = 0; n < NR_DMAE; n++) {
337 int i = request_irq(get_dma_error_irq(n), dma_err,
Mike Frysinger800fb3d2012-08-04 21:54:05 -0400338 IRQF_SHARED, dmae_name[n], (void *)dmae_name[n]);
Paul Mundt7f47c7182012-05-19 20:38:50 +0900339 if (unlikely(i < 0)) {
340 printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
341 return i;
342 }
343 }
344
345 return 0;
346}
347
348static void dmae_irq_free(void)
349{
350 int n;
351
352 for (n = 0; n < NR_DMAE; n++)
353 free_irq(get_dma_error_irq(n), NULL);
354}
355#else
356static inline int dmae_irq_init(void)
357{
358 return 0;
359}
360
361static void dmae_irq_free(void)
362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364#endif
365
366static struct dma_ops sh_dmac_ops = {
367 .request = sh_dmac_request_dma,
368 .free = sh_dmac_free_dma,
369 .get_residue = sh_dmac_get_dma_residue,
370 .xfer = sh_dmac_xfer_dma,
371 .configure = sh_dmac_configure_channel,
372};
373
374static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800375 .name = "sh_dmac",
376 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 .ops = &sh_dmac_ops,
378 .flags = DMAC_CHANNELS_TEI_CAPABLE,
379};
380
381static int __init sh_dmac_init(void)
382{
383 struct dma_info *info = &sh_dmac_info;
Paul Mundt7f47c7182012-05-19 20:38:50 +0900384 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Paul Mundt7f47c7182012-05-19 20:38:50 +0900386 /*
387 * Initialize DMAE, for parts that support it.
388 */
389 rc = dmae_irq_init();
390 if (unlikely(rc != 0))
391 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Paul Mundt0d831772006-01-16 22:14:09 -0800393 /*
394 * Initialize DMAOR, and clean up any error flags that may have
395 * been set.
396 */
Paul Mundt7f47c7182012-05-19 20:38:50 +0900397 for (i = 0; i < NR_DMAOR; i++) {
398 rc = dmaor_reset(i);
399 if (unlikely(rc != 0))
400 return rc;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 return register_dmac(info);
404}
405
406static void __exit sh_dmac_exit(void)
407{
Paul Mundt7f47c7182012-05-19 20:38:50 +0900408 dmae_irq_free();
Paul Mundt0d831772006-01-16 22:14:09 -0800409 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412subsys_initcall(sh_dmac_init);
413module_exit(sh_dmac_exit);
414
Paul Mundt0d831772006-01-16 22:14:09 -0800415MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
416MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417MODULE_LICENSE("GPL");