blob: 31c2930f2f540ee8bcc4735467aed6587488af1b [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(CONFIG_CPU_SUBTYPE_SH7763) || \
23 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
24 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
25 defined(CONFIG_CPU_SUBTYPE_SH7785)
26#define DMAC_IRQ_MULTI 1
Markus Brunner3ea6bc32007-08-20 08:59:33 +090027#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090028
29#if defined(DMAE1_IRQ)
30#define NR_DMAE 2
31#else
32#define NR_DMAE 1
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090033#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090034
35static const char *dmae_name[] = {
36 "DMAC Address Error0", "DMAC Address Error1"
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090037};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090039static inline unsigned int get_dmte_irq(unsigned int chan)
40{
41 unsigned int irq = 0;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090042 if (chan < ARRAY_SIZE(dmte_irq_map))
43 irq = dmte_irq_map[chan];
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090044
45#if defined(DMAC_IRQ_MULTI)
46 if (irq > DMTE6_IRQ)
47 return DMTE6_IRQ;
48 return DMTE0_IRQ;
49#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return irq;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54/*
55 * We determine the correct shift size based off of the CHCR transmit size
56 * for the given channel. Since we know that it will take:
57 *
58 * info->count >> ts_shift[transmit_size]
59 *
60 * iterations to complete the transfer.
61 */
62static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
63{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090064 u32 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Paul Mundt0d831772006-01-16 22:14:09 -080066 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69/*
70 * The transfer end interrupt must read the chcr register to end the
71 * hardware interrupt active condition.
72 * Besides that it needs to waken any waiting process, which should handle
73 * setting up the next transfer.
74 */
Paul Mundt35f3c512006-10-06 15:31:16 +090075static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Paul Mundt35f3c512006-10-06 15:31:16 +090077 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 u32 chcr;
79
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090080 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (!(chcr & CHCR_TE))
83 return IRQ_NONE;
84
85 chcr &= ~(CHCR_IE | CHCR_DE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090086 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 wake_up(&chan->wait_queue);
89
90 return IRQ_HANDLED;
91}
92
93static int sh_dmac_request_dma(struct dma_channel *chan)
94{
Julia Lawallb2d7c7f2008-02-26 21:42:11 +010095 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
Paul Mundt9e3043c2006-09-27 16:55:24 +090096 return 0;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +090099#if defined(DMAC_IRQ_MULTI)
100 IRQF_SHARED,
101#else
102 IRQF_DISABLED,
103#endif
104 chan->dev_id, chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static void sh_dmac_free_dma(struct dma_channel *chan)
108{
109 free_irq(get_dmte_irq(chan->chan), chan);
110}
111
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900112static int
Paul Mundt0d831772006-01-16 22:14:09 -0800113sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 if (!chcr)
Paul Mundt0d831772006-01-16 22:14:09 -0800116 chcr = RS_DUAL | CHCR_IE;
117
118 if (chcr & CHCR_IE) {
119 chcr &= ~CHCR_IE;
120 chan->flags |= DMA_TEI_CAPABLE;
121 } else {
122 chan->flags &= ~DMA_TEI_CAPABLE;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900125 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 chan->flags |= DMA_CONFIGURED;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +0900128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static void sh_dmac_enable_dma(struct dma_channel *chan)
132{
Paul Mundt0d831772006-01-16 22:14:09 -0800133 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 u32 chcr;
135
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900136 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Paul Mundt0d831772006-01-16 22:14:09 -0800137 chcr |= CHCR_DE;
138
139 if (chan->flags & DMA_TEI_CAPABLE)
140 chcr |= CHCR_IE;
141
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900142 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Paul Mundt0d831772006-01-16 22:14:09 -0800144 if (chan->flags & DMA_TEI_CAPABLE) {
145 irq = get_dmte_irq(chan->chan);
146 enable_irq(irq);
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150static void sh_dmac_disable_dma(struct dma_channel *chan)
151{
Paul Mundt0d831772006-01-16 22:14:09 -0800152 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 u32 chcr;
154
Paul Mundt0d831772006-01-16 22:14:09 -0800155 if (chan->flags & DMA_TEI_CAPABLE) {
156 irq = get_dmte_irq(chan->chan);
157 disable_irq(irq);
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900160 chcr = ctrl_inl(dma_base_addr[chan->chan] + CHCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900162 ctrl_outl(chcr, (dma_base_addr[chan->chan] + CHCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static int sh_dmac_xfer_dma(struct dma_channel *chan)
166{
167 /*
168 * If we haven't pre-configured the channel with special flags, use
169 * the defaults.
170 */
Paul Mundt0d831772006-01-16 22:14:09 -0800171 if (unlikely(!(chan->flags & DMA_CONFIGURED)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sh_dmac_configure_channel(chan, 0);
173
174 sh_dmac_disable_dma(chan);
175
176 /*
177 * Single-address mode usage note!
178 *
179 * It's important that we don't accidentally write any value to SAR/DAR
180 * (this includes 0) that hasn't been directly specified by the user if
181 * we're in single-address mode.
182 *
183 * In this case, only one address can be defined, anything else will
184 * result in a DMA address error interrupt (at least on the SH-4),
185 * which will subsequently halt the transfer.
186 *
187 * Channel 2 on the Dreamcast is a special case, as this is used for
188 * cascading to the PVR2 DMAC. In this case, we still need to write
189 * SAR and DAR, regardless of value, in order for cascading to work.
190 */
Paul Mundt0d831772006-01-16 22:14:09 -0800191 if (chan->sar || (mach_is_dreamcast() &&
192 chan->chan == PVR2_CASCADE_CHAN))
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900193 ctrl_outl(chan->sar, (dma_base_addr[chan->chan]+SAR));
Paul Mundt0d831772006-01-16 22:14:09 -0800194 if (chan->dar || (mach_is_dreamcast() &&
195 chan->chan == PVR2_CASCADE_CHAN))
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900196 ctrl_outl(chan->dar, (dma_base_addr[chan->chan] + DAR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900198 ctrl_outl(chan->count >> calc_xmit_shift(chan),
199 (dma_base_addr[chan->chan] + TCR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 sh_dmac_enable_dma(chan);
202
203 return 0;
204}
205
206static int sh_dmac_get_dma_residue(struct dma_channel *chan)
207{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900208 if (!(ctrl_inl(dma_base_addr[chan->chan] + CHCR) & CHCR_DE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900211 return ctrl_inl(dma_base_addr[chan->chan] + TCR)
212 << calc_xmit_shift(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900215static inline int dmaor_reset(int no)
Paul Mundt0d831772006-01-16 22:14:09 -0800216{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900217 unsigned long dmaor = dmaor_read_reg(no);
Paul Mundt0d831772006-01-16 22:14:09 -0800218
219 /* Try to clear the error flags first, incase they are set */
220 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900221 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800222
223 dmaor |= DMAOR_INIT;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900224 dmaor_write_reg(no, dmaor);
Paul Mundt0d831772006-01-16 22:14:09 -0800225
226 /* See if we got an error again */
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900227 if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
Paul Mundt0d831772006-01-16 22:14:09 -0800228 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
229 return -EINVAL;
230 }
231
232 return 0;
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900236static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900238#if defined(DMAC_IRQ_MULTI)
239 int cnt = 0;
240 switch (irq) {
241#if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
242 case DMTE6_IRQ:
243 cnt++;
244#endif
245 case DMTE0_IRQ:
246 if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
247 disable_irq(irq);
248 /* DMA multi and error IRQ */
249 return IRQ_HANDLED;
250 }
251 default:
252 return IRQ_NONE;
253 }
254#else
255 dmaor_reset(0);
256#if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
257 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
258 defined(CONFIG_CPU_SUBTYPE_SH7785)
259 dmaor_reset(1);
260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 disable_irq(irq);
262
263 return IRQ_HANDLED;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266#endif
267
268static struct dma_ops sh_dmac_ops = {
269 .request = sh_dmac_request_dma,
270 .free = sh_dmac_free_dma,
271 .get_residue = sh_dmac_get_dma_residue,
272 .xfer = sh_dmac_xfer_dma,
273 .configure = sh_dmac_configure_channel,
274};
275
276static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800277 .name = "sh_dmac",
278 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 .ops = &sh_dmac_ops,
280 .flags = DMAC_CHANNELS_TEI_CAPABLE,
281};
282
Nobuhiro Iwamatsu02ebd322009-03-13 04:31:34 +0000283#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900284static unsigned int get_dma_error_irq(int n)
285{
286#if defined(DMAC_IRQ_MULTI)
287 return (n == 0) ? get_dmte_irq(0) : get_dmte_irq(6);
288#else
289 return (n == 0) ? DMAE0_IRQ :
290#if defined(DMAE1_IRQ)
291 DMAE1_IRQ;
292#else
293 -1;
294#endif
295#endif
296}
Nobuhiro Iwamatsu02ebd322009-03-13 04:31:34 +0000297#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static int __init sh_dmac_init(void)
300{
301 struct dma_info *info = &sh_dmac_info;
302 int i;
303
304#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900305 int n;
306
307 for (n = 0; n < NR_DMAE; n++) {
308 i = request_irq(get_dma_error_irq(n), dma_err,
309#if defined(DMAC_IRQ_MULTI)
310 IRQF_SHARED,
311#else
312 IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313#endif
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900314 dmae_name[n], (void *)dmae_name[n]);
315 if (unlikely(i < 0)) {
316 printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
317 return i;
318 }
319 }
320#endif /* CONFIG_CPU_SH4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Paul Mundt0d831772006-01-16 22:14:09 -0800322 /*
323 * Initialize DMAOR, and clean up any error flags that may have
324 * been set.
325 */
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900326 i = dmaor_reset(0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900327 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800328 return i;
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900329#if defined(CONFIG_CPU_SUBTYPE_SH7723) || \
330 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
331 defined(CONFIG_CPU_SUBTYPE_SH7785)
332 i = dmaor_reset(1);
333 if (unlikely(i != 0))
334 return i;
335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 return register_dmac(info);
338}
339
340static void __exit sh_dmac_exit(void)
341{
342#ifdef CONFIG_CPU_SH4
Nobuhiro Iwamatsu71b973a2009-03-10 17:26:49 +0900343 int n;
344
345 for (n = 0; n < NR_DMAE; n++) {
346 free_irq(get_dma_error_irq(n), (void *)dmae_name[n]);
347 }
348#endif /* CONFIG_CPU_SH4 */
Paul Mundt0d831772006-01-16 22:14:09 -0800349 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352subsys_initcall(sh_dmac_init);
353module_exit(sh_dmac_exit);
354
Paul Mundt0d831772006-01-16 22:14:09 -0800355MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
356MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357MODULE_LICENSE("GPL");