blob: 37fb5b8bbc3f122d0e0a1b3e50ee43b6e630fcc6 [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 Mundt71b80642008-07-29 20:20:36 +090017#include <mach-dreamcast/mach/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/dma.h>
19#include <asm/io.h>
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090020#include <asm/dma-sh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090022#if defined(DMAE1_IRQ)
23#define NR_DMAE 2
24#else
25#define NR_DMAE 1
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090026#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090027
28static const char *dmae_name[] = {
29 "DMAC Address Error0", "DMAC Address Error1"
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090030};
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090032static inline unsigned int get_dmte_irq(unsigned int chan)
33{
34 unsigned int irq = 0;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090035 if (chan < ARRAY_SIZE(dmte_irq_map))
36 irq = dmte_irq_map[chan];
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090037
Nobuhiro Iwamatsu988f8312009-03-16 03:22:07 +000038#if defined(CONFIG_SH_DMA_IRQ_MULTI)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090039 if (irq > DMTE6_IRQ)
40 return DMTE6_IRQ;
41 return DMTE0_IRQ;
42#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return irq;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47/*
48 * We determine the correct shift size based off of the CHCR transmit size
49 * for the given channel. Since we know that it will take:
50 *
51 * info->count >> ts_shift[transmit_size]
52 *
53 * iterations to complete the transfer.
54 */
55static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
56{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090057 u32 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Paul Mundt0d831772006-01-16 22:14:09 -080059 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62/*
63 * The transfer end interrupt must read the chcr register to end the
64 * hardware interrupt active condition.
65 * Besides that it needs to waken any waiting process, which should handle
66 * setting up the next transfer.
67 */
Paul Mundt35f3c512006-10-06 15:31:16 +090068static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Paul Mundt35f3c512006-10-06 15:31:16 +090070 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u32 chcr;
72
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090073 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (!(chcr & CHCR_TE))
76 return IRQ_NONE;
77
78 chcr &= ~(CHCR_IE | CHCR_DE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090079 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 wake_up(&chan->wait_queue);
82
83 return IRQ_HANDLED;
84}
85
86static int sh_dmac_request_dma(struct dma_channel *chan)
87{
Julia Lawallb2d7c7f2008-02-26 21:42:11 +010088 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
Paul Mundt9e3043c2006-09-27 16:55:24 +090089 return 0;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Nobuhiro Iwamatsu988f8312009-03-16 03:22:07 +000092#if defined(CONFIG_SH_DMA_IRQ_MULTI)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090093 IRQF_SHARED,
94#else
95 IRQF_DISABLED,
96#endif
97 chan->dev_id, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void sh_dmac_free_dma(struct dma_channel *chan)
101{
102 free_irq(get_dmte_irq(chan->chan), chan);
103}
104
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900105static int
Paul Mundt0d831772006-01-16 22:14:09 -0800106sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800109 chcr = RS_DUAL | CHCR_IE;
110
111 if (chcr & CHCR_IE) {
112 chcr &= ~CHCR_IE;
113 chan->flags |= DMA_TEI_CAPABLE;
114 } else {
115 chan->flags &= ~DMA_TEI_CAPABLE;
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900118 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 chan->flags |= DMA_CONFIGURED;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static void sh_dmac_enable_dma(struct dma_channel *chan)
125{
Paul Mundt0d831772006-01-16 22:14:09 -0800126 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 u32 chcr;
128
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900129 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Paul Mundt0d831772006-01-16 22:14:09 -0800130 chcr |= CHCR_DE;
131
132 if (chan->flags & DMA_TEI_CAPABLE)
133 chcr |= CHCR_IE;
134
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900135 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Paul Mundt0d831772006-01-16 22:14:09 -0800137 if (chan->flags & DMA_TEI_CAPABLE) {
138 irq = get_dmte_irq(chan->chan);
139 enable_irq(irq);
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static void sh_dmac_disable_dma(struct dma_channel *chan)
144{
Paul Mundt0d831772006-01-16 22:14:09 -0800145 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 u32 chcr;
147
Paul Mundt0d831772006-01-16 22:14:09 -0800148 if (chan->flags & DMA_TEI_CAPABLE) {
149 irq = get_dmte_irq(chan->chan);
150 disable_irq(irq);
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900153 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900155 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int sh_dmac_xfer_dma(struct dma_channel *chan)
159{
160 /*
161 * If we haven't pre-configured the channel with special flags, use
162 * the defaults.
163 */
Paul Mundt0d831772006-01-16 22:14:09 -0800164 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 sh_dmac_configure_channel(chan, 0);
166
167 sh_dmac_disable_dma(chan);
168
169 /*
170 * Single-address mode usage note!
171 *
172 * It's important that we don't accidentally write any value to SAR/DAR
173 * (this includes 0) that hasn't been directly specified by the user if
174 * we're in single-address mode.
175 *
176 * In this case, only one address can be defined, anything else will
177 * result in a DMA address error interrupt (at least on the SH-4),
178 * which will subsequently halt the transfer.
179 *
180 * Channel 2 on the Dreamcast is a special case, as this is used for
181 * cascading to the PVR2 DMAC. In this case, we still need to write
182 * SAR and DAR, regardless of value, in order for cascading to work.
183 */
Paul Mundt0d831772006-01-16 22:14:09 -0800184 if (chan->sar || (mach_is_dreamcast() &&
185 chan->chan == PVR2_CASCADE_CHAN))
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900186 ctrl_outl(chan->sar, (dma_base_addr[chan->chan]+SAR));
Paul Mundt0d831772006-01-16 22:14:09 -0800187 if (chan->dar || (mach_is_dreamcast() &&
188 chan->chan == PVR2_CASCADE_CHAN))
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900189 ctrl_outl(chan->dar, (dma_base_addr[chan->chan] + DAR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900191 ctrl_outl(chan->count >> calc_xmit_shift(chan),
192 (dma_base_addr[chan->chan] + TCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 sh_dmac_enable_dma(chan);
195
196 return 0;
197}
198
199static int sh_dmac_get_dma_residue(struct dma_channel *chan)
200{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900201 if (!(ctrl_inl(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900204 return ctrl_inl(dma_base_addr[chan->chan] + TCR)
205 << calc_xmit_shift(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900208static inline int dmaor_reset(int no)
Paul Mundt0d831772006-01-16 22:14:09 -0800209{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900210 unsigned long dmaor = dmaor_read_reg(no);
Paul Mundt0d831772006-01-16 22:14:09 -0800211
212 /* Try to clear the error flags first, incase they are set */
213 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900214 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800215
216 dmaor |= DMAOR_INIT;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900217 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800218
219 /* See if we got an error again */
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900220 if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
Paul Mundt0d831772006-01-16 22:14:09 -0800221 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
222 return -EINVAL;
223 }
224
225 return 0;
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900229static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Nobuhiro Iwamatsu988f8312009-03-16 03:22:07 +0000231#if defined(CONFIG_SH_DMA_IRQ_MULTI)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900232 int cnt = 0;
233 switch (irq) {
234#if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
235 case DMTE6_IRQ:
236 cnt++;
237#endif
238 case DMTE0_IRQ:
239 if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
240 disable_irq(irq);
241 /* DMA multi and error IRQ */
242 return IRQ_HANDLED;
243 }
244 default:
245 return IRQ_NONE;
246 }
247#else
248 dmaor_reset(0);
249#if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
250 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
251 defined(CONFIG_CPU_SUBTYPE_SH7785)
252 dmaor_reset(1);
253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 disable_irq(irq);
255
256 return IRQ_HANDLED;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259#endif
260
261static struct dma_ops sh_dmac_ops = {
262 .request = sh_dmac_request_dma,
263 .free = sh_dmac_free_dma,
264 .get_residue = sh_dmac_get_dma_residue,
265 .xfer = sh_dmac_xfer_dma,
266 .configure = sh_dmac_configure_channel,
267};
268
269static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800270 .name = "sh_dmac",
271 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .ops = &sh_dmac_ops,
273 .flags = DMAC_CHANNELS_TEI_CAPABLE,
274};
275
Nobuhiro Iwamatsu02ebd322009-03-13 04:31:34 +0000276#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900277static unsigned int get_dma_error_irq(int n)
278{
Nobuhiro Iwamatsu988f8312009-03-16 03:22:07 +0000279#if defined(CONFIG_SH_DMA_IRQ_MULTI)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900280 return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
281#else
282 return (n == 0) ? DMAE0_IRQ :
283#if defined(DMAE1_IRQ)
284 DMAE1_IRQ;
285#else
286 -1;
287#endif
288#endif
289}
Nobuhiro Iwamatsu02ebd322009-03-13 04:31:34 +0000290#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static int __init sh_dmac_init(void)
293{
294 struct dma_info *info = &sh_dmac_info;
295 int i;
296
297#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900298 int n;
299
300 for (n = 0; n < NR_DMAE; n++) {
301 i = request_irq(get_dma_error_irq(n), dma_err,
Nobuhiro Iwamatsu988f8312009-03-16 03:22:07 +0000302#if defined(CONFIG_SH_DMA_IRQ_MULTI)
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900303 IRQF_SHARED,
304#else
305 IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900307 dmae_name[n], (void *)dmae_name[n]);
308 if (unlikely(i < 0)) {
309 printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
310 return i;
311 }
312 }
313#endif /* CONFIG_CPU_SH4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Paul Mundt0d831772006-01-16 22:14:09 -0800315 /*
316 * Initialize DMAOR, and clean up any error flags that may have
317 * been set.
318 */
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900319 i = dmaor_reset(0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900320 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800321 return i;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900322#if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
323 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
324 defined(CONFIG_CPU_SUBTYPE_SH7785)
325 i = dmaor_reset(1);
326 if (unlikely(i != 0))
327 return i;
328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 return register_dmac(info);
331}
332
333static void __exit sh_dmac_exit(void)
334{
335#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900336 int n;
337
338 for (n = 0; n < NR_DMAE; n++) {
339 free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
340 }
341#endif /* CONFIG_CPU_SH4 */
Paul Mundt0d831772006-01-16 22:14:09 -0800342 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345subsys_initcall(sh_dmac_init);
346module_exit(sh_dmac_exit);
347
Paul Mundt0d831772006-01-16 22:14:09 -0800348MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
349MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350MODULE_LICENSE("GPL");