GNU Linux-libre 4.14.324-gnu1
[releases.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 <linux/io.h>
18 #include <mach-dreamcast/mach/dma.h>
19 #include <asm/dma.h>
20 #include <asm/dma-register.h>
21 #include <cpu/dma-register.h>
22 #include <cpu/dma.h>
23
24 /*
25  * Some of the SoCs feature two DMAC modules. In such a case, the channels are
26  * distributed equally among them.
27  */
28 #ifdef  SH_DMAC_BASE1
29 #define SH_DMAC_NR_MD_CH        (CONFIG_NR_ONCHIP_DMA_CHANNELS / 2)
30 #else
31 #define SH_DMAC_NR_MD_CH        CONFIG_NR_ONCHIP_DMA_CHANNELS
32 #endif
33
34 #define SH_DMAC_CH_SZ           0x10
35
36 /*
37  * Define the default configuration for dual address memory-memory transfer.
38  * The 0x400 value represents auto-request, external->external.
39  */
40 #define RS_DUAL (DM_INC | SM_INC | RS_AUTO | TS_INDEX2VAL(XMIT_SZ_32BIT))
41
42 static unsigned long dma_find_base(unsigned int chan)
43 {
44         unsigned long base = SH_DMAC_BASE0;
45
46 #ifdef SH_DMAC_BASE1
47         if (chan >= SH_DMAC_NR_MD_CH)
48                 base = SH_DMAC_BASE1;
49 #endif
50
51         return base;
52 }
53
54 static unsigned long dma_base_addr(unsigned int chan)
55 {
56         unsigned long base = dma_find_base(chan);
57
58         chan = (chan % SH_DMAC_NR_MD_CH) * SH_DMAC_CH_SZ;
59
60         /* DMAOR is placed inside the channel register space. Step over it. */
61         if (chan >= DMAOR)
62                 base += SH_DMAC_CH_SZ;
63
64         return base + chan;
65 }
66
67 #ifdef CONFIG_SH_DMA_IRQ_MULTI
68 static inline unsigned int get_dmte_irq(unsigned int chan)
69 {
70         return chan >= 6 ? DMTE6_IRQ : DMTE0_IRQ;
71 }
72 #else
73
74 static unsigned int dmte_irq_map[] = {
75         DMTE0_IRQ, DMTE0_IRQ + 1, DMTE0_IRQ + 2, DMTE0_IRQ + 3,
76
77 #ifdef DMTE4_IRQ
78         DMTE4_IRQ, DMTE4_IRQ + 1,
79 #endif
80
81 #ifdef DMTE6_IRQ
82         DMTE6_IRQ, DMTE6_IRQ + 1,
83 #endif
84
85 #ifdef DMTE8_IRQ
86         DMTE8_IRQ, DMTE9_IRQ, DMTE10_IRQ, DMTE11_IRQ,
87 #endif
88 };
89
90 static inline unsigned int get_dmte_irq(unsigned int chan)
91 {
92         return dmte_irq_map[chan];
93 }
94 #endif
95
96 /*
97  * We determine the correct shift size based off of the CHCR transmit size
98  * for the given channel. Since we know that it will take:
99  *
100  *      info->count >> ts_shift[transmit_size]
101  *
102  * iterations to complete the transfer.
103  */
104 static unsigned int ts_shift[] = TS_SHIFT;
105
106 static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
107 {
108         u32 chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
109         int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
110                 ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
111
112         return ts_shift[cnt];
113 }
114
115 /*
116  * The transfer end interrupt must read the chcr register to end the
117  * hardware interrupt active condition.
118  * Besides that it needs to waken any waiting process, which should handle
119  * setting up the next transfer.
120  */
121 static irqreturn_t dma_tei(int irq, void *dev_id)
122 {
123         struct dma_channel *chan = dev_id;
124         u32 chcr;
125
126         chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
127
128         if (!(chcr & CHCR_TE))
129                 return IRQ_NONE;
130
131         chcr &= ~(CHCR_IE | CHCR_DE);
132         __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
133
134         wake_up(&chan->wait_queue);
135
136         return IRQ_HANDLED;
137 }
138
139 static int sh_dmac_request_dma(struct dma_channel *chan)
140 {
141         if (unlikely(!(chan->flags & DMA_TEI_CAPABLE)))
142                 return 0;
143
144         return request_irq(get_dmte_irq(chan->chan), dma_tei, IRQF_SHARED,
145                            chan->dev_id, chan);
146 }
147
148 static void sh_dmac_free_dma(struct dma_channel *chan)
149 {
150         free_irq(get_dmte_irq(chan->chan), chan);
151 }
152
153 static int
154 sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
155 {
156         if (!chcr)
157                 chcr = RS_DUAL | CHCR_IE;
158
159         if (chcr & CHCR_IE) {
160                 chcr &= ~CHCR_IE;
161                 chan->flags |= DMA_TEI_CAPABLE;
162         } else {
163                 chan->flags &= ~DMA_TEI_CAPABLE;
164         }
165
166         __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
167
168         chan->flags |= DMA_CONFIGURED;
169         return 0;
170 }
171
172 static void sh_dmac_enable_dma(struct dma_channel *chan)
173 {
174         int irq;
175         u32 chcr;
176
177         chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
178         chcr |= CHCR_DE;
179
180         if (chan->flags & DMA_TEI_CAPABLE)
181                 chcr |= CHCR_IE;
182
183         __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
184
185         if (chan->flags & DMA_TEI_CAPABLE) {
186                 irq = get_dmte_irq(chan->chan);
187                 enable_irq(irq);
188         }
189 }
190
191 static void sh_dmac_disable_dma(struct dma_channel *chan)
192 {
193         int irq;
194         u32 chcr;
195
196         if (chan->flags & DMA_TEI_CAPABLE) {
197                 irq = get_dmte_irq(chan->chan);
198                 disable_irq(irq);
199         }
200
201         chcr = __raw_readl(dma_base_addr(chan->chan) + CHCR);
202         chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
203         __raw_writel(chcr, (dma_base_addr(chan->chan) + CHCR));
204 }
205
206 static int sh_dmac_xfer_dma(struct dma_channel *chan)
207 {
208         /*
209          * If we haven't pre-configured the channel with special flags, use
210          * the defaults.
211          */
212         if (unlikely(!(chan->flags & DMA_CONFIGURED)))
213                 sh_dmac_configure_channel(chan, 0);
214
215         sh_dmac_disable_dma(chan);
216
217         /*
218          * Single-address mode usage note!
219          *
220          * It's important that we don't accidentally write any value to SAR/DAR
221          * (this includes 0) that hasn't been directly specified by the user if
222          * we're in single-address mode.
223          *
224          * In this case, only one address can be defined, anything else will
225          * result in a DMA address error interrupt (at least on the SH-4),
226          * which will subsequently halt the transfer.
227          *
228          * Channel 2 on the Dreamcast is a special case, as this is used for
229          * cascading to the PVR2 DMAC. In this case, we still need to write
230          * SAR and DAR, regardless of value, in order for cascading to work.
231          */
232         if (chan->sar || (mach_is_dreamcast() &&
233                           chan->chan == PVR2_CASCADE_CHAN))
234                 __raw_writel(chan->sar, (dma_base_addr(chan->chan) + SAR));
235         if (chan->dar || (mach_is_dreamcast() &&
236                           chan->chan == PVR2_CASCADE_CHAN))
237                 __raw_writel(chan->dar, (dma_base_addr(chan->chan) + DAR));
238
239         __raw_writel(chan->count >> calc_xmit_shift(chan),
240                 (dma_base_addr(chan->chan) + TCR));
241
242         sh_dmac_enable_dma(chan);
243
244         return 0;
245 }
246
247 static int sh_dmac_get_dma_residue(struct dma_channel *chan)
248 {
249         if (!(__raw_readl(dma_base_addr(chan->chan) + CHCR) & CHCR_DE))
250                 return 0;
251
252         return __raw_readl(dma_base_addr(chan->chan) + TCR)
253                  << calc_xmit_shift(chan);
254 }
255
256 /*
257  * DMAOR handling
258  */
259 #if defined(CONFIG_CPU_SUBTYPE_SH7723)  || \
260     defined(CONFIG_CPU_SUBTYPE_SH7724)  || \
261     defined(CONFIG_CPU_SUBTYPE_SH7780)  || \
262     defined(CONFIG_CPU_SUBTYPE_SH7785)
263 #define NR_DMAOR        2
264 #else
265 #define NR_DMAOR        1
266 #endif
267
268 #define dmaor_read_reg(n)               __raw_readw(dma_find_base((n) * \
269                                                     SH_DMAC_NR_MD_CH) + DMAOR)
270 #define dmaor_write_reg(n, data)        __raw_writew(data, \
271                                                      dma_find_base((n) * \
272                                                      SH_DMAC_NR_MD_CH) + DMAOR)
273
274 static inline int dmaor_reset(int no)
275 {
276         unsigned long dmaor = dmaor_read_reg(no);
277
278         /* Try to clear the error flags first, incase they are set */
279         dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
280         dmaor_write_reg(no, dmaor);
281
282         dmaor |= DMAOR_INIT;
283         dmaor_write_reg(no, dmaor);
284
285         /* See if we got an error again */
286         if ((dmaor_read_reg(no) & (DMAOR_AE | DMAOR_NMIF))) {
287                 printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
288                 return -EINVAL;
289         }
290
291         return 0;
292 }
293
294 /*
295  * DMAE handling
296  */
297 #ifdef CONFIG_CPU_SH4
298
299 #if defined(DMAE1_IRQ)
300 #define NR_DMAE         2
301 #else
302 #define NR_DMAE         1
303 #endif
304
305 static const char *dmae_name[] = {
306         "DMAC Address Error0",
307         "DMAC Address Error1"
308 };
309
310 #ifdef CONFIG_SH_DMA_IRQ_MULTI
311 static inline unsigned int get_dma_error_irq(int n)
312 {
313         return get_dmte_irq(n * 6);
314 }
315 #else
316
317 static unsigned int dmae_irq_map[] = {
318         DMAE0_IRQ,
319
320 #ifdef DMAE1_IRQ
321         DMAE1_IRQ,
322 #endif
323 };
324
325 static inline unsigned int get_dma_error_irq(int n)
326 {
327         return dmae_irq_map[n];
328 }
329 #endif
330
331 static irqreturn_t dma_err(int irq, void *dummy)
332 {
333         int i;
334
335         for (i = 0; i < NR_DMAOR; i++)
336                 dmaor_reset(i);
337
338         disable_irq(irq);
339
340         return IRQ_HANDLED;
341 }
342
343 static int dmae_irq_init(void)
344 {
345         int n;
346
347         for (n = 0; n < NR_DMAE; n++) {
348                 int i = request_irq(get_dma_error_irq(n), dma_err,
349                                     IRQF_SHARED, dmae_name[n], (void *)dmae_name[n]);
350                 if (unlikely(i < 0)) {
351                         printk(KERN_ERR "%s request_irq fail\n", dmae_name[n]);
352                         return i;
353                 }
354         }
355
356         return 0;
357 }
358
359 static void dmae_irq_free(void)
360 {
361         int n;
362
363         for (n = 0; n < NR_DMAE; n++)
364                 free_irq(get_dma_error_irq(n), NULL);
365 }
366 #else
367 static inline int dmae_irq_init(void)
368 {
369         return 0;
370 }
371
372 static void dmae_irq_free(void)
373 {
374 }
375 #endif
376
377 static struct dma_ops sh_dmac_ops = {
378         .request        = sh_dmac_request_dma,
379         .free           = sh_dmac_free_dma,
380         .get_residue    = sh_dmac_get_dma_residue,
381         .xfer           = sh_dmac_xfer_dma,
382         .configure      = sh_dmac_configure_channel,
383 };
384
385 static struct dma_info sh_dmac_info = {
386         .name           = "sh_dmac",
387         .nr_channels    = CONFIG_NR_ONCHIP_DMA_CHANNELS,
388         .ops            = &sh_dmac_ops,
389         .flags          = DMAC_CHANNELS_TEI_CAPABLE,
390 };
391
392 static int __init sh_dmac_init(void)
393 {
394         struct dma_info *info = &sh_dmac_info;
395         int i, rc;
396
397         /*
398          * Initialize DMAE, for parts that support it.
399          */
400         rc = dmae_irq_init();
401         if (unlikely(rc != 0))
402                 return rc;
403
404         /*
405          * Initialize DMAOR, and clean up any error flags that may have
406          * been set.
407          */
408         for (i = 0; i < NR_DMAOR; i++) {
409                 rc = dmaor_reset(i);
410                 if (unlikely(rc != 0))
411                         return rc;
412         }
413
414         return register_dmac(info);
415 }
416
417 static void __exit sh_dmac_exit(void)
418 {
419         dmae_irq_free();
420         unregister_dmac(&sh_dmac_info);
421 }
422
423 subsys_initcall(sh_dmac_init);
424 module_exit(sh_dmac_exit);
425
426 MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
427 MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
428 MODULE_LICENSE("GPL");