Pull button into test branch
[linux-drm-fsl-dcu.git] / arch / sh / drivers / dma / dma-sh.c
1 /*
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
8  * Copyright (C) 2005 Andriy Skulysh
9  *
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  */
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/module.h>
17 #include <asm/dreamcast/dma.h>
18 #include <asm/dma.h>
19 #include <asm/io.h>
20 #include "dma-sh.h"
21
22
23
24 #ifdef CONFIG_CPU_SH4
25 static struct ipr_data dmae_ipr_map[] = {
26         { DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
27 };
28 #endif
29 static struct ipr_data dmte_ipr_map[] = {
30         /*
31          * Normally we could just do DMTE0_IRQ + chan outright, though in the
32          * case of the 7751R, the DMTE IRQs for channels > 4 start right above
33          * the SCIF
34          */
35         { DMTE0_IRQ + 0, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
36         { DMTE0_IRQ + 1, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
37         { DMTE0_IRQ + 2, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
38         { DMTE0_IRQ + 3, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
39         { DMTE4_IRQ + 0, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
40         { DMTE4_IRQ + 1, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
41         { DMTE4_IRQ + 2, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
42         { DMTE4_IRQ + 3, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY },
43 };
44
45 static inline unsigned int get_dmte_irq(unsigned int chan)
46 {
47         unsigned int irq = 0;
48         if (chan < ARRAY_SIZE(dmte_ipr_map))
49                 irq = dmte_ipr_map[chan].irq;
50         return irq;
51 }
52
53 /*
54  * We determine the correct shift size based off of the CHCR transmit size
55  * for the given channel. Since we know that it will take:
56  *
57  *      info->count >> ts_shift[transmit_size]
58  *
59  * iterations to complete the transfer.
60  */
61 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
62 {
63         u32 chcr = ctrl_inl(CHCR[chan->chan]);
64
65         return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
66 }
67
68 /*
69  * The transfer end interrupt must read the chcr register to end the
70  * hardware interrupt active condition.
71  * Besides that it needs to waken any waiting process, which should handle
72  * setting up the next transfer.
73  */
74 static irqreturn_t dma_tei(int irq, void *dev_id)
75 {
76         struct dma_channel *chan = dev_id;
77         u32 chcr;
78
79         chcr = ctrl_inl(CHCR[chan->chan]);
80
81         if (!(chcr & CHCR_TE))
82                 return IRQ_NONE;
83
84         chcr &= ~(CHCR_IE | CHCR_DE);
85         ctrl_outl(chcr, CHCR[chan->chan]);
86
87         wake_up(&chan->wait_queue);
88
89         return IRQ_HANDLED;
90 }
91
92 static int sh_dmac_request_dma(struct dma_channel *chan)
93 {
94         if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
95                 return 0;
96
97         return request_irq(get_dmte_irq(chan->chan), dma_tei,
98                            IRQF_DISABLED, chan->dev_id, chan);
99 }
100
101 static void sh_dmac_free_dma(struct dma_channel *chan)
102 {
103         free_irq(get_dmte_irq(chan->chan), chan);
104 }
105
106 static void
107 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
108 {
109         if (!chcr)
110                 chcr = RS_DUAL | CHCR_IE;
111
112         if (chcr & CHCR_IE) {
113                 chcr &= ~CHCR_IE;
114                 chan->flags |= DMA_TEI_CAPABLE;
115         } else {
116                 chan->flags &= ~DMA_TEI_CAPABLE;
117         }
118
119         ctrl_outl(chcr, CHCR[chan->chan]);
120
121         chan->flags |= DMA_CONFIGURED;
122 }
123
124 static void sh_dmac_enable_dma(struct dma_channel *chan)
125 {
126         int irq;
127         u32 chcr;
128
129         chcr = ctrl_inl(CHCR[chan->chan]);
130         chcr |= CHCR_DE;
131
132         if (chan->flags & DMA_TEI_CAPABLE)
133                 chcr |= CHCR_IE;
134
135         ctrl_outl(chcr, CHCR[chan->chan]);
136
137         if (chan->flags & DMA_TEI_CAPABLE) {
138                 irq = get_dmte_irq(chan->chan);
139                 enable_irq(irq);
140         }
141 }
142
143 static void sh_dmac_disable_dma(struct dma_channel *chan)
144 {
145         int irq;
146         u32 chcr;
147
148         if (chan->flags & DMA_TEI_CAPABLE) {
149                 irq = get_dmte_irq(chan->chan);
150                 disable_irq(irq);
151         }
152
153         chcr = ctrl_inl(CHCR[chan->chan]);
154         chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
155         ctrl_outl(chcr, CHCR[chan->chan]);
156 }
157
158 static 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          */
164         if (unlikely(!(chan->flags & DMA_CONFIGURED)))
165                 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          */
184         if (chan->sar || (mach_is_dreamcast() &&
185                           chan->chan == PVR2_CASCADE_CHAN))
186                 ctrl_outl(chan->sar, SAR[chan->chan]);
187         if (chan->dar || (mach_is_dreamcast() &&
188                           chan->chan == PVR2_CASCADE_CHAN))
189                 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
198 static 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
206 #ifdef CONFIG_CPU_SUBTYPE_SH7780
207 #define dmaor_read_reg()        ctrl_inw(DMAOR)
208 #define dmaor_write_reg(data)   ctrl_outw(data, DMAOR)
209 #else
210 #define dmaor_read_reg()        ctrl_inl(DMAOR)
211 #define dmaor_write_reg(data)   ctrl_outl(data, DMAOR)
212 #endif
213
214 static inline int dmaor_reset(void)
215 {
216         unsigned long dmaor = dmaor_read_reg();
217
218         /* Try to clear the error flags first, incase they are set */
219         dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
220         dmaor_write_reg(dmaor);
221
222         dmaor |= DMAOR_INIT;
223         dmaor_write_reg(dmaor);
224
225         /* See if we got an error again */
226         if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
227                 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
228                 return -EINVAL;
229         }
230
231         return 0;
232 }
233
234 #if defined(CONFIG_CPU_SH4)
235 static irqreturn_t dma_err(int irq, void *dummy)
236 {
237         dmaor_reset();
238         disable_irq(irq);
239
240         return IRQ_HANDLED;
241 }
242 #endif
243
244 static struct dma_ops sh_dmac_ops = {
245         .request        = sh_dmac_request_dma,
246         .free           = sh_dmac_free_dma,
247         .get_residue    = sh_dmac_get_dma_residue,
248         .xfer           = sh_dmac_xfer_dma,
249         .configure      = sh_dmac_configure_channel,
250 };
251
252 static struct dma_info sh_dmac_info = {
253         .name           = "sh_dmac",
254         .nr_channels    = CONFIG_NR_ONCHIP_DMA_CHANNELS,
255         .ops            = &sh_dmac_ops,
256         .flags          = DMAC_CHANNELS_TEI_CAPABLE,
257 };
258
259 static int __init sh_dmac_init(void)
260 {
261         struct dma_info *info = &sh_dmac_info;
262         int i;
263
264 #ifdef CONFIG_CPU_SH4
265         make_ipr_irq(dmae_ipr_map, ARRAY_SIZE(dmae_ipr_map));
266         i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
267         if (unlikely(i < 0))
268                 return i;
269 #endif
270
271         i = info->nr_channels;
272         if (i > ARRAY_SIZE(dmte_ipr_map))
273                 i = ARRAY_SIZE(dmte_ipr_map);
274         make_ipr_irq(dmte_ipr_map, i);
275
276         /*
277          * Initialize DMAOR, and clean up any error flags that may have
278          * been set.
279          */
280         i = dmaor_reset();
281         if (unlikely(i != 0))
282                 return i;
283
284         return register_dmac(info);
285 }
286
287 static void __exit sh_dmac_exit(void)
288 {
289 #ifdef CONFIG_CPU_SH4
290         free_irq(DMAE_IRQ, 0);
291 #endif
292         unregister_dmac(&sh_dmac_info);
293 }
294
295 subsys_initcall(sh_dmac_init);
296 module_exit(sh_dmac_exit);
297
298 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
299 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
300 MODULE_LICENSE("GPL");