blob: b2ffe649c7c039d9bc3a03c7527ad37922a62552 [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>
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) || \
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +090028 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
Markus Brunner3ea6bc32007-08-20 08:59:33 +090029 defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090030 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
Kristoffer Ericson4f247e82007-09-11 12:49:59 +090031 defined(CONFIG_CPU_SUBTYPE_SH7709) || \
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090032 defined(CONFIG_CPU_SUBTYPE_SH7780)
33 DMTE4_IRQ,
34 DMTE5_IRQ,
Markus Brunner3ea6bc32007-08-20 08:59:33 +090035#endif
36#if defined(CONFIG_CPU_SUBTYPE_SH7751R) || \
37 defined(CONFIG_CPU_SUBTYPE_SH7760) || \
38 defined(CONFIG_CPU_SUBTYPE_SH7780)
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090039 DMTE6_IRQ,
Markus Brunner3ea6bc32007-08-20 08:59:33 +090040 DMTE7_IRQ,
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090041#endif
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090042};
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jamie Lenehanbd71ab82006-10-31 12:35:02 +090044static inline unsigned int get_dmte_irq(unsigned int chan)
45{
46 unsigned int irq = 0;
Manuel Lauss9f8a5e32007-01-25 15:22:11 +090047 if (chan < ARRAY_SIZE(dmte_irq_map))
48 irq = dmte_irq_map[chan];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return irq;
50}
51
52/*
53 * We determine the correct shift size based off of the CHCR transmit size
54 * for the given channel. Since we know that it will take:
55 *
56 * info->count >> ts_shift[transmit_size]
57 *
58 * iterations to complete the transfer.
59 */
60static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
61{
62 u32 chcr = ctrl_inl(CHCR[chan->chan]);
63
Paul Mundt0d831772006-01-16 22:14:09 -080064 return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/*
68 * The transfer end interrupt must read the chcr register to end the
69 * hardware interrupt active condition.
70 * Besides that it needs to waken any waiting process, which should handle
71 * setting up the next transfer.
72 */
Paul Mundt35f3c512006-10-06 15:31:16 +090073static irqreturn_t dma_tei(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Paul Mundt35f3c512006-10-06 15:31:16 +090075 struct dma_channel *chan = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u32 chcr;
77
78 chcr = ctrl_inl(CHCR[chan->chan]);
79
80 if (!(chcr & CHCR_TE))
81 return IRQ_NONE;
82
83 chcr &= ~(CHCR_IE | CHCR_DE);
84 ctrl_outl(chcr, CHCR[chan->chan]);
85
86 wake_up(&chan->wait_queue);
87
88 return IRQ_HANDLED;
89}
90
91static int sh_dmac_request_dma(struct dma_channel *chan)
92{
Julia Lawallb2d7c7f2008-02-26 21:42:11 +010093 if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
Paul Mundt9e3043c2006-09-27 16:55:24 +090094 return 0;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return request_irq(get_dmte_irq(chan->chan), dma_tei,
Paul Mundte803aaf2006-11-24 14:50:05 +090097 IRQF_DISABLED, 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
118 ctrl_outl(chcr, CHCR[chan->chan]);
119
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
129 chcr = ctrl_inl(CHCR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800130 chcr |= CHCR_DE;
131
132 if (chan->flags & DMA_TEI_CAPABLE)
133 chcr |= CHCR_IE;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ctrl_outl(chcr, CHCR[chan->chan]);
136
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
153 chcr = ctrl_inl(CHCR[chan->chan]);
154 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
155 ctrl_outl(chcr, CHCR[chan->chan]);
156}
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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ctrl_outl(chan->sar, SAR[chan->chan]);
Paul Mundt0d831772006-01-16 22:14:09 -0800187 if (chan->dar || (mach_is_dreamcast() &&
188 chan->chan == PVR2_CASCADE_CHAN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ctrl_outl(chan->dar, DAR[chan->chan]);
190
191 ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);
192
193 sh_dmac_enable_dma(chan);
194
195 return 0;
196}
197
198static int sh_dmac_get_dma_residue(struct dma_channel *chan)
199{
200 if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
201 return 0;
202
203 return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
204}
205
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900206#if defined(CONFIG_CPU_SUBTYPE_SH7720) || \
Yoshihiro Shimoda31a49c42007-12-26 11:45:06 +0900207 defined(CONFIG_CPU_SUBTYPE_SH7721) || \
Markus Brunner3ea6bc32007-08-20 08:59:33 +0900208 defined(CONFIG_CPU_SUBTYPE_SH7780)
Paul Mundt0d831772006-01-16 22:14:09 -0800209#define dmaor_read_reg() ctrl_inw(DMAOR)
210#define dmaor_write_reg(data) ctrl_outw(data, DMAOR)
211#else
212#define dmaor_read_reg() ctrl_inl(DMAOR)
213#define dmaor_write_reg(data) ctrl_outl(data, DMAOR)
214#endif
215
216static inline int dmaor_reset(void)
217{
218 unsigned long dmaor = dmaor_read_reg();
219
220 /* Try to clear the error flags first, incase they are set */
221 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
222 dmaor_write_reg(dmaor);
223
224 dmaor |= DMAOR_INIT;
225 dmaor_write_reg(dmaor);
226
227 /* See if we got an error again */
228 if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
229 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
230 return -EINVAL;
231 }
232
233 return 0;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#if defined(CONFIG_CPU_SH4)
Paul Mundt35f3c512006-10-06 15:31:16 +0900237static irqreturn_t dma_err(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Paul Mundt0d831772006-01-16 22:14:09 -0800239 dmaor_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 disable_irq(irq);
241
242 return IRQ_HANDLED;
243}
244#endif
245
246static struct dma_ops sh_dmac_ops = {
247 .request = sh_dmac_request_dma,
248 .free = sh_dmac_free_dma,
249 .get_residue = sh_dmac_get_dma_residue,
250 .xfer = sh_dmac_xfer_dma,
251 .configure = sh_dmac_configure_channel,
252};
253
254static struct dma_info sh_dmac_info = {
Paul Mundt0d831772006-01-16 22:14:09 -0800255 .name = "sh_dmac",
256 .nr_channels = CONFIG_NR_ONCHIP_DMA_CHANNELS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .ops = &sh_dmac_ops,
258 .flags = DMAC_CHANNELS_TEI_CAPABLE,
259};
260
261static int __init sh_dmac_init(void)
262{
263 struct dma_info *info = &sh_dmac_info;
264 int i;
265
266#ifdef CONFIG_CPU_SH4
Thomas Gleixner6d208192006-07-01 19:29:25 -0700267 i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
Paul Mundt9e3043c2006-09-27 16:55:24 +0900268 if (unlikely(i < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return i;
270#endif
271
Paul Mundt0d831772006-01-16 22:14:09 -0800272 /*
273 * Initialize DMAOR, and clean up any error flags that may have
274 * been set.
275 */
276 i = dmaor_reset();
Paul Mundt9e3043c2006-09-27 16:55:24 +0900277 if (unlikely(i != 0))
Paul Mundt0d831772006-01-16 22:14:09 -0800278 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 return register_dmac(info);
281}
282
283static void __exit sh_dmac_exit(void)
284{
285#ifdef CONFIG_CPU_SH4
286 free_irq(DMAE_IRQ, 0);
287#endif
Paul Mundt0d831772006-01-16 22:14:09 -0800288 unregister_dmac(&sh_dmac_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291subsys_initcall(sh_dmac_init);
292module_exit(sh_dmac_exit);
293
Paul Mundt0d831772006-01-16 22:14:09 -0800294MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
295MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296MODULE_LICENSE("GPL");