Merge git://git.infradead.org/battery-2.6
[pandora-kernel.git] / arch / blackfin / kernel / bfin_dma_5xx.c
1 /*
2  * bfin_dma_5xx.c - Blackfin DMA implementation
3  *
4  * Copyright 2004-2008 Analog Devices Inc.
5  * Licensed under the GPL-2 or later.
6  */
7
8 #include <linux/errno.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/param.h>
13 #include <linux/proc_fs.h>
14 #include <linux/sched.h>
15 #include <linux/seq_file.h>
16 #include <linux/spinlock.h>
17
18 #include <asm/blackfin.h>
19 #include <asm/cacheflush.h>
20 #include <asm/dma.h>
21 #include <asm/uaccess.h>
22
23 /*
24  * To make sure we work around 05000119 - we always check DMA_DONE bit,
25  * never the DMA_RUN bit
26  */
27
28 struct dma_channel dma_ch[MAX_DMA_CHANNELS];
29 EXPORT_SYMBOL(dma_ch);
30
31 static int __init blackfin_dma_init(void)
32 {
33         int i;
34
35         printk(KERN_INFO "Blackfin DMA Controller\n");
36
37         for (i = 0; i < MAX_DMA_CHANNELS; i++) {
38                 dma_ch[i].chan_status = DMA_CHANNEL_FREE;
39                 dma_ch[i].regs = dma_io_base_addr[i];
40                 mutex_init(&(dma_ch[i].dmalock));
41         }
42         /* Mark MEMDMA Channel 0 as requested since we're using it internally */
43         request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");
44         request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");
45
46 #if defined(CONFIG_DEB_DMA_URGENT)
47         bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()
48                          | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);
49 #endif
50
51         return 0;
52 }
53 arch_initcall(blackfin_dma_init);
54
55 #ifdef CONFIG_PROC_FS
56 static int proc_dma_show(struct seq_file *m, void *v)
57 {
58         int i;
59
60         for (i = 0; i < MAX_DMA_CHANNELS; ++i)
61                 if (dma_ch[i].chan_status != DMA_CHANNEL_FREE)
62                         seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id);
63
64         return 0;
65 }
66
67 static int proc_dma_open(struct inode *inode, struct file *file)
68 {
69         return single_open(file, proc_dma_show, NULL);
70 }
71
72 static const struct file_operations proc_dma_operations = {
73         .open           = proc_dma_open,
74         .read           = seq_read,
75         .llseek         = seq_lseek,
76         .release        = single_release,
77 };
78
79 static int __init proc_dma_init(void)
80 {
81         return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;
82 }
83 late_initcall(proc_dma_init);
84 #endif
85
86 /**
87  *      request_dma - request a DMA channel
88  *
89  * Request the specific DMA channel from the system if it's available.
90  */
91 int request_dma(unsigned int channel, const char *device_id)
92 {
93         pr_debug("request_dma() : BEGIN \n");
94
95         if (device_id == NULL)
96                 printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel);
97
98 #if defined(CONFIG_BF561) && ANOMALY_05000182
99         if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {
100                 if (get_cclk() > 500000000) {
101                         printk(KERN_WARNING
102                                "Request IMDMA failed due to ANOMALY 05000182\n");
103                         return -EFAULT;
104                 }
105         }
106 #endif
107
108         mutex_lock(&(dma_ch[channel].dmalock));
109
110         if ((dma_ch[channel].chan_status == DMA_CHANNEL_REQUESTED)
111             || (dma_ch[channel].chan_status == DMA_CHANNEL_ENABLED)) {
112                 mutex_unlock(&(dma_ch[channel].dmalock));
113                 pr_debug("DMA CHANNEL IN USE  \n");
114                 return -EBUSY;
115         } else {
116                 dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
117                 pr_debug("DMA CHANNEL IS ALLOCATED  \n");
118         }
119
120         mutex_unlock(&(dma_ch[channel].dmalock));
121
122 #ifdef CONFIG_BF54x
123         if (channel >= CH_UART2_RX && channel <= CH_UART3_TX) {
124                 unsigned int per_map;
125                 per_map = dma_ch[channel].regs->peripheral_map & 0xFFF;
126                 if (strncmp(device_id, "BFIN_UART", 9) == 0)
127                         dma_ch[channel].regs->peripheral_map = per_map |
128                                 ((channel - CH_UART2_RX + 0xC)<<12);
129                 else
130                         dma_ch[channel].regs->peripheral_map = per_map |
131                                 ((channel - CH_UART2_RX + 0x6)<<12);
132         }
133 #endif
134
135         dma_ch[channel].device_id = device_id;
136         dma_ch[channel].irq = 0;
137
138         /* This is to be enabled by putting a restriction -
139          * you have to request DMA, before doing any operations on
140          * descriptor/channel
141          */
142         pr_debug("request_dma() : END  \n");
143         return 0;
144 }
145 EXPORT_SYMBOL(request_dma);
146
147 int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)
148 {
149         BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
150                && channel < MAX_DMA_CHANNELS));
151
152         if (callback != NULL) {
153                 int ret;
154                 unsigned int irq = channel2irq(channel);
155
156                 ret = request_irq(irq, callback, IRQF_DISABLED,
157                         dma_ch[channel].device_id, data);
158                 if (ret)
159                         return ret;
160
161                 dma_ch[channel].irq = irq;
162                 dma_ch[channel].data = data;
163         }
164         return 0;
165 }
166 EXPORT_SYMBOL(set_dma_callback);
167
168 /**
169  *      clear_dma_buffer - clear DMA fifos for specified channel
170  *
171  * Set the Buffer Clear bit in the Configuration register of specific DMA
172  * channel. This will stop the descriptor based DMA operation.
173  */
174 static void clear_dma_buffer(unsigned int channel)
175 {
176         dma_ch[channel].regs->cfg |= RESTART;
177         SSYNC();
178         dma_ch[channel].regs->cfg &= ~RESTART;
179 }
180
181 void free_dma(unsigned int channel)
182 {
183         pr_debug("freedma() : BEGIN \n");
184         BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
185                && channel < MAX_DMA_CHANNELS));
186
187         /* Halt the DMA */
188         disable_dma(channel);
189         clear_dma_buffer(channel);
190
191         if (dma_ch[channel].irq)
192                 free_irq(dma_ch[channel].irq, dma_ch[channel].data);
193
194         /* Clear the DMA Variable in the Channel */
195         mutex_lock(&(dma_ch[channel].dmalock));
196         dma_ch[channel].chan_status = DMA_CHANNEL_FREE;
197         mutex_unlock(&(dma_ch[channel].dmalock));
198
199         pr_debug("freedma() : END \n");
200 }
201 EXPORT_SYMBOL(free_dma);
202
203 #ifdef CONFIG_PM
204 # ifndef MAX_DMA_SUSPEND_CHANNELS
205 #  define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS
206 # endif
207 int blackfin_dma_suspend(void)
208 {
209         int i;
210
211         for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i) {
212                 if (dma_ch[i].chan_status == DMA_CHANNEL_ENABLED) {
213                         printk(KERN_ERR "DMA Channel %d failed to suspend\n", i);
214                         return -EBUSY;
215                 }
216
217                 dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;
218         }
219
220         return 0;
221 }
222
223 void blackfin_dma_resume(void)
224 {
225         int i;
226         for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i)
227                 dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;
228 }
229 #endif
230
231 /**
232  *      blackfin_dma_early_init - minimal DMA init
233  *
234  * Setup a few DMA registers so we can safely do DMA transfers early on in
235  * the kernel booting process.  Really this just means using dma_memcpy().
236  */
237 void __init blackfin_dma_early_init(void)
238 {
239         bfin_write_MDMA_S0_CONFIG(0);
240         bfin_write_MDMA_S1_CONFIG(0);
241 }
242
243 void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size)
244 {
245         unsigned long dst = (unsigned long)pdst;
246         unsigned long src = (unsigned long)psrc;
247         struct dma_register *dst_ch, *src_ch;
248
249         /* We assume that everything is 4 byte aligned, so include
250          * a basic sanity check
251          */
252         BUG_ON(dst % 4);
253         BUG_ON(src % 4);
254         BUG_ON(size % 4);
255
256         /* Force a sync in case a previous config reset on this channel
257          * occurred.  This is needed so subsequent writes to DMA registers
258          * are not spuriously lost/corrupted.
259          */
260         __builtin_bfin_ssync();
261
262         src_ch = 0;
263         /* Find an avalible memDMA channel */
264         while (1) {
265                 if (!src_ch || src_ch == (struct dma_register *)MDMA_S1_NEXT_DESC_PTR) {
266                         dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR;
267                         src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR;
268                 } else {
269                         dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR;
270                         src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR;
271                 }
272
273                 if (!bfin_read16(&src_ch->cfg)) {
274                         break;
275                 } else {
276                         if (bfin_read16(&src_ch->irq_status) & DMA_DONE)
277                                 bfin_write16(&src_ch->cfg, 0);
278                 }
279
280         }
281
282         /* Destination */
283         bfin_write32(&dst_ch->start_addr, dst);
284         bfin_write16(&dst_ch->x_count, size >> 2);
285         bfin_write16(&dst_ch->x_modify, 1 << 2);
286         bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR);
287
288         /* Source */
289         bfin_write32(&src_ch->start_addr, src);
290         bfin_write16(&src_ch->x_count, size >> 2);
291         bfin_write16(&src_ch->x_modify, 1 << 2);
292         bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR);
293
294         /* Enable */
295         bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32);
296         bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32);
297
298         /* Since we are atomic now, don't use the workaround ssync */
299         __builtin_bfin_ssync();
300 }
301
302 void __init early_dma_memcpy_done(void)
303 {
304         while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) ||
305                (bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE)))
306                 continue;
307
308         bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
309         bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR);
310         /*
311          * Now that DMA is done, we would normally flush cache, but
312          * i/d cache isn't running this early, so we don't bother,
313          * and just clear out the DMA channel for next time
314          */
315         bfin_write_MDMA_S0_CONFIG(0);
316         bfin_write_MDMA_S1_CONFIG(0);
317         bfin_write_MDMA_D0_CONFIG(0);
318         bfin_write_MDMA_D1_CONFIG(0);
319
320         __builtin_bfin_ssync();
321 }
322
323 /**
324  *      __dma_memcpy - program the MDMA registers
325  *
326  * Actually program MDMA0 and wait for the transfer to finish.  Disable IRQs
327  * while programming registers so that everything is fully configured.  Wait
328  * for DMA to finish with IRQs enabled.  If interrupted, the initial DMA_DONE
329  * check will make sure we don't clobber any existing transfer.
330  */
331 static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
332 {
333         static DEFINE_SPINLOCK(mdma_lock);
334         unsigned long flags;
335
336         spin_lock_irqsave(&mdma_lock, flags);
337
338         /* Force a sync in case a previous config reset on this channel
339          * occurred.  This is needed so subsequent writes to DMA registers
340          * are not spuriously lost/corrupted.  Do it under irq lock and
341          * without the anomaly version (because we are atomic already).
342          */
343         __builtin_bfin_ssync();
344
345         if (bfin_read_MDMA_S0_CONFIG())
346                 while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
347                         continue;
348
349         if (conf & DMA2D) {
350                 /* For larger bit sizes, we've already divided down cnt so it
351                  * is no longer a multiple of 64k.  So we have to break down
352                  * the limit here so it is a multiple of the incoming size.
353                  * There is no limitation here in terms of total size other
354                  * than the hardware though as the bits lost in the shift are
355                  * made up by MODIFY (== we can hit the whole address space).
356                  * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
357                  */
358                 u32 shift = abs(dmod) >> 1;
359                 size_t ycnt = cnt >> (16 - shift);
360                 cnt = 1 << (16 - shift);
361                 bfin_write_MDMA_D0_Y_COUNT(ycnt);
362                 bfin_write_MDMA_S0_Y_COUNT(ycnt);
363                 bfin_write_MDMA_D0_Y_MODIFY(dmod);
364                 bfin_write_MDMA_S0_Y_MODIFY(smod);
365         }
366
367         bfin_write_MDMA_D0_START_ADDR(daddr);
368         bfin_write_MDMA_D0_X_COUNT(cnt);
369         bfin_write_MDMA_D0_X_MODIFY(dmod);
370         bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
371
372         bfin_write_MDMA_S0_START_ADDR(saddr);
373         bfin_write_MDMA_S0_X_COUNT(cnt);
374         bfin_write_MDMA_S0_X_MODIFY(smod);
375         bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
376
377         bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
378         bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
379
380         spin_unlock_irqrestore(&mdma_lock, flags);
381
382         SSYNC();
383
384         while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
385                 if (bfin_read_MDMA_S0_CONFIG())
386                         continue;
387                 else
388                         return;
389
390         bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
391
392         bfin_write_MDMA_S0_CONFIG(0);
393         bfin_write_MDMA_D0_CONFIG(0);
394 }
395
396 /**
397  *      _dma_memcpy - translate C memcpy settings into MDMA settings
398  *
399  * Handle all the high level steps before we touch the MDMA registers.  So
400  * handle direction, tweaking of sizes, and formatting of addresses.
401  */
402 static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
403 {
404         u32 conf, shift;
405         s16 mod;
406         unsigned long dst = (unsigned long)pdst;
407         unsigned long src = (unsigned long)psrc;
408
409         if (size == 0)
410                 return NULL;
411
412         if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
413                 conf = WDSIZE_32;
414                 shift = 2;
415         } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
416                 conf = WDSIZE_16;
417                 shift = 1;
418         } else {
419                 conf = WDSIZE_8;
420                 shift = 0;
421         }
422
423         /* If the two memory regions have a chance of overlapping, make
424          * sure the memcpy still works as expected.  Do this by having the
425          * copy run backwards instead.
426          */
427         mod = 1 << shift;
428         if (src < dst) {
429                 mod *= -1;
430                 dst += size + mod;
431                 src += size + mod;
432         }
433         size >>= shift;
434
435         if (size > 0x10000)
436                 conf |= DMA2D;
437
438         __dma_memcpy(dst, mod, src, mod, size, conf);
439
440         return pdst;
441 }
442
443 /**
444  *      dma_memcpy - DMA memcpy under mutex lock
445  *
446  * Do not check arguments before starting the DMA memcpy.  Break the transfer
447  * up into two pieces.  The first transfer is in multiples of 64k and the
448  * second transfer is the piece smaller than 64k.
449  */
450 void *dma_memcpy(void *pdst, const void *psrc, size_t size)
451 {
452         unsigned long dst = (unsigned long)pdst;
453         unsigned long src = (unsigned long)psrc;
454         size_t bulk, rest;
455
456         if (bfin_addr_dcacheable(src))
457                 blackfin_dcache_flush_range(src, src + size);
458
459         if (bfin_addr_dcacheable(dst))
460                 blackfin_dcache_invalidate_range(dst, dst + size);
461
462         bulk = size & ~0xffff;
463         rest = size - bulk;
464         if (bulk)
465                 _dma_memcpy(pdst, psrc, bulk);
466         _dma_memcpy(pdst + bulk, psrc + bulk, rest);
467         return pdst;
468 }
469 EXPORT_SYMBOL(dma_memcpy);
470
471 /**
472  *      safe_dma_memcpy - DMA memcpy w/argument checking
473  *
474  * Verify arguments are safe before heading to dma_memcpy().
475  */
476 void *safe_dma_memcpy(void *dst, const void *src, size_t size)
477 {
478         if (!access_ok(VERIFY_WRITE, dst, size))
479                 return NULL;
480         if (!access_ok(VERIFY_READ, src, size))
481                 return NULL;
482         return dma_memcpy(dst, src, size);
483 }
484 EXPORT_SYMBOL(safe_dma_memcpy);
485
486 static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
487                      u16 size, u16 dma_size)
488 {
489         blackfin_dcache_flush_range(buf, buf + len * size);
490         __dma_memcpy(addr, 0, buf, size, len, dma_size);
491 }
492
493 static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
494                     u16 size, u16 dma_size)
495 {
496         blackfin_dcache_invalidate_range(buf, buf + len * size);
497         __dma_memcpy(buf, size, addr, 0, len, dma_size);
498 }
499
500 #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
501 void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
502 { \
503         _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
504 } \
505 EXPORT_SYMBOL(dma_##io##s##bwl)
506 MAKE_DMA_IO(out, b, 1,  8, const);
507 MAKE_DMA_IO(in,  b, 1,  8, );
508 MAKE_DMA_IO(out, w, 2, 16, const);
509 MAKE_DMA_IO(in,  w, 2, 16, );
510 MAKE_DMA_IO(out, l, 4, 32, const);
511 MAKE_DMA_IO(in,  l, 4, 32, );