Add linux-next specific files for 20110831
[linux-2.6/next.git] / arch / blackfin / kernel / bfin_dma_5xx.c
blob71dbaa4a48afc1263b23af017702ea82aedb2557
1 /*
2 * bfin_dma_5xx.c - Blackfin DMA implementation
4 * Copyright 2004-2008 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/param.h>
14 #include <linux/proc_fs.h>
15 #include <linux/sched.h>
16 #include <linux/seq_file.h>
17 #include <linux/spinlock.h>
19 #include <asm/blackfin.h>
20 #include <asm/cacheflush.h>
21 #include <asm/dma.h>
22 #include <asm/uaccess.h>
23 #include <asm/early_printk.h>
26 * To make sure we work around 05000119 - we always check DMA_DONE bit,
27 * never the DMA_RUN bit
30 struct dma_channel dma_ch[MAX_DMA_CHANNELS];
31 EXPORT_SYMBOL(dma_ch);
33 static int __init blackfin_dma_init(void)
35 int i;
37 printk(KERN_INFO "Blackfin DMA Controller\n");
40 #if ANOMALY_05000480
41 bfin_write_DMAC_TC_PER(0x0111);
42 #endif
44 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
45 atomic_set(&dma_ch[i].chan_status, 0);
46 dma_ch[i].regs = dma_io_base_addr[i];
48 /* Mark MEMDMA Channel 0 as requested since we're using it internally */
49 request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");
50 request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");
52 #if defined(CONFIG_DEB_DMA_URGENT)
53 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()
54 | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);
55 #endif
57 return 0;
59 arch_initcall(blackfin_dma_init);
61 #ifdef CONFIG_PROC_FS
62 static int proc_dma_show(struct seq_file *m, void *v)
64 int i;
66 for (i = 0; i < MAX_DMA_CHANNELS; ++i)
67 if (dma_channel_active(i))
68 seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id);
70 return 0;
73 static int proc_dma_open(struct inode *inode, struct file *file)
75 return single_open(file, proc_dma_show, NULL);
78 static const struct file_operations proc_dma_operations = {
79 .open = proc_dma_open,
80 .read = seq_read,
81 .llseek = seq_lseek,
82 .release = single_release,
85 static int __init proc_dma_init(void)
87 return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;
89 late_initcall(proc_dma_init);
90 #endif
92 static void set_dma_peripheral_map(unsigned int channel, const char *device_id)
94 #ifdef CONFIG_BF54x
95 unsigned int per_map;
97 switch (channel) {
98 case CH_UART2_RX: per_map = 0xC << 12; break;
99 case CH_UART2_TX: per_map = 0xD << 12; break;
100 case CH_UART3_RX: per_map = 0xE << 12; break;
101 case CH_UART3_TX: per_map = 0xF << 12; break;
102 default: return;
105 if (strncmp(device_id, "BFIN_UART", 9) == 0)
106 dma_ch[channel].regs->peripheral_map = per_map;
107 #endif
111 * request_dma - request a DMA channel
113 * Request the specific DMA channel from the system if it's available.
115 int request_dma(unsigned int channel, const char *device_id)
117 pr_debug("request_dma() : BEGIN\n");
119 if (device_id == NULL)
120 printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel);
122 #if defined(CONFIG_BF561) && ANOMALY_05000182
123 if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {
124 if (get_cclk() > 500000000) {
125 printk(KERN_WARNING
126 "Request IMDMA failed due to ANOMALY 05000182\n");
127 return -EFAULT;
130 #endif
132 if (atomic_cmpxchg(&dma_ch[channel].chan_status, 0, 1)) {
133 pr_debug("DMA CHANNEL IN USE\n");
134 return -EBUSY;
137 set_dma_peripheral_map(channel, device_id);
138 dma_ch[channel].device_id = device_id;
139 dma_ch[channel].irq = 0;
141 /* This is to be enabled by putting a restriction -
142 * you have to request DMA, before doing any operations on
143 * descriptor/channel
145 pr_debug("request_dma() : END\n");
146 return 0;
148 EXPORT_SYMBOL(request_dma);
150 int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)
152 int ret;
153 unsigned int irq;
155 BUG_ON(channel >= MAX_DMA_CHANNELS || !callback ||
156 !atomic_read(&dma_ch[channel].chan_status));
158 irq = channel2irq(channel);
159 ret = request_irq(irq, callback, 0, dma_ch[channel].device_id, data);
160 if (ret)
161 return ret;
163 dma_ch[channel].irq = irq;
164 dma_ch[channel].data = data;
166 return 0;
168 EXPORT_SYMBOL(set_dma_callback);
171 * clear_dma_buffer - clear DMA fifos for specified channel
173 * Set the Buffer Clear bit in the Configuration register of specific DMA
174 * channel. This will stop the descriptor based DMA operation.
176 static void clear_dma_buffer(unsigned int channel)
178 dma_ch[channel].regs->cfg |= RESTART;
179 SSYNC();
180 dma_ch[channel].regs->cfg &= ~RESTART;
183 void free_dma(unsigned int channel)
185 pr_debug("freedma() : BEGIN\n");
186 BUG_ON(channel >= MAX_DMA_CHANNELS ||
187 !atomic_read(&dma_ch[channel].chan_status));
189 /* Halt the DMA */
190 disable_dma(channel);
191 clear_dma_buffer(channel);
193 if (dma_ch[channel].irq)
194 free_irq(dma_ch[channel].irq, dma_ch[channel].data);
196 /* Clear the DMA Variable in the Channel */
197 atomic_set(&dma_ch[channel].chan_status, 0);
199 pr_debug("freedma() : END\n");
201 EXPORT_SYMBOL(free_dma);
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)
209 int i;
211 for (i = 0; i < MAX_DMA_CHANNELS; ++i) {
212 if (dma_ch[i].regs->cfg & DMAEN) {
213 printk(KERN_ERR "DMA Channel %d failed to suspend\n", i);
214 return -EBUSY;
217 if (i < MAX_DMA_SUSPEND_CHANNELS)
218 dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;
221 return 0;
224 void blackfin_dma_resume(void)
226 int i;
228 for (i = 0; i < MAX_DMA_CHANNELS; ++i) {
229 dma_ch[i].regs->cfg = 0;
231 if (i < MAX_DMA_SUSPEND_CHANNELS)
232 dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;
235 #endif
238 * blackfin_dma_early_init - minimal DMA init
240 * Setup a few DMA registers so we can safely do DMA transfers early on in
241 * the kernel booting process. Really this just means using dma_memcpy().
243 void __init blackfin_dma_early_init(void)
245 early_shadow_stamp();
246 bfin_write_MDMA_S0_CONFIG(0);
247 bfin_write_MDMA_S1_CONFIG(0);
250 void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size)
252 unsigned long dst = (unsigned long)pdst;
253 unsigned long src = (unsigned long)psrc;
254 struct dma_register *dst_ch, *src_ch;
256 early_shadow_stamp();
258 /* We assume that everything is 4 byte aligned, so include
259 * a basic sanity check
261 BUG_ON(dst % 4);
262 BUG_ON(src % 4);
263 BUG_ON(size % 4);
265 src_ch = 0;
266 /* Find an avalible memDMA channel */
267 while (1) {
268 if (src_ch == (struct dma_register *)MDMA_S0_NEXT_DESC_PTR) {
269 dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR;
270 src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR;
271 } else {
272 dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR;
273 src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR;
276 if (!bfin_read16(&src_ch->cfg))
277 break;
278 else if (bfin_read16(&dst_ch->irq_status) & DMA_DONE) {
279 bfin_write16(&src_ch->cfg, 0);
280 break;
284 /* Force a sync in case a previous config reset on this channel
285 * occurred. This is needed so subsequent writes to DMA registers
286 * are not spuriously lost/corrupted.
288 __builtin_bfin_ssync();
290 /* Destination */
291 bfin_write32(&dst_ch->start_addr, dst);
292 bfin_write16(&dst_ch->x_count, size >> 2);
293 bfin_write16(&dst_ch->x_modify, 1 << 2);
294 bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR);
296 /* Source */
297 bfin_write32(&src_ch->start_addr, src);
298 bfin_write16(&src_ch->x_count, size >> 2);
299 bfin_write16(&src_ch->x_modify, 1 << 2);
300 bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR);
302 /* Enable */
303 bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32);
304 bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32);
306 /* Since we are atomic now, don't use the workaround ssync */
307 __builtin_bfin_ssync();
310 void __init early_dma_memcpy_done(void)
312 early_shadow_stamp();
314 while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) ||
315 (bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE)))
316 continue;
318 bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
319 bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR);
321 * Now that DMA is done, we would normally flush cache, but
322 * i/d cache isn't running this early, so we don't bother,
323 * and just clear out the DMA channel for next time
325 bfin_write_MDMA_S0_CONFIG(0);
326 bfin_write_MDMA_S1_CONFIG(0);
327 bfin_write_MDMA_D0_CONFIG(0);
328 bfin_write_MDMA_D1_CONFIG(0);
330 __builtin_bfin_ssync();
334 * __dma_memcpy - program the MDMA registers
336 * Actually program MDMA0 and wait for the transfer to finish. Disable IRQs
337 * while programming registers so that everything is fully configured. Wait
338 * for DMA to finish with IRQs enabled. If interrupted, the initial DMA_DONE
339 * check will make sure we don't clobber any existing transfer.
341 static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
343 static DEFINE_SPINLOCK(mdma_lock);
344 unsigned long flags;
346 spin_lock_irqsave(&mdma_lock, flags);
348 /* Force a sync in case a previous config reset on this channel
349 * occurred. This is needed so subsequent writes to DMA registers
350 * are not spuriously lost/corrupted. Do it under irq lock and
351 * without the anomaly version (because we are atomic already).
353 __builtin_bfin_ssync();
355 if (bfin_read_MDMA_S0_CONFIG())
356 while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
357 continue;
359 if (conf & DMA2D) {
360 /* For larger bit sizes, we've already divided down cnt so it
361 * is no longer a multiple of 64k. So we have to break down
362 * the limit here so it is a multiple of the incoming size.
363 * There is no limitation here in terms of total size other
364 * than the hardware though as the bits lost in the shift are
365 * made up by MODIFY (== we can hit the whole address space).
366 * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
368 u32 shift = abs(dmod) >> 1;
369 size_t ycnt = cnt >> (16 - shift);
370 cnt = 1 << (16 - shift);
371 bfin_write_MDMA_D0_Y_COUNT(ycnt);
372 bfin_write_MDMA_S0_Y_COUNT(ycnt);
373 bfin_write_MDMA_D0_Y_MODIFY(dmod);
374 bfin_write_MDMA_S0_Y_MODIFY(smod);
377 bfin_write_MDMA_D0_START_ADDR(daddr);
378 bfin_write_MDMA_D0_X_COUNT(cnt);
379 bfin_write_MDMA_D0_X_MODIFY(dmod);
380 bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
382 bfin_write_MDMA_S0_START_ADDR(saddr);
383 bfin_write_MDMA_S0_X_COUNT(cnt);
384 bfin_write_MDMA_S0_X_MODIFY(smod);
385 bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
387 bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
388 bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
390 spin_unlock_irqrestore(&mdma_lock, flags);
392 SSYNC();
394 while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
395 if (bfin_read_MDMA_S0_CONFIG())
396 continue;
397 else
398 return;
400 bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
402 bfin_write_MDMA_S0_CONFIG(0);
403 bfin_write_MDMA_D0_CONFIG(0);
407 * _dma_memcpy - translate C memcpy settings into MDMA settings
409 * Handle all the high level steps before we touch the MDMA registers. So
410 * handle direction, tweaking of sizes, and formatting of addresses.
412 static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
414 u32 conf, shift;
415 s16 mod;
416 unsigned long dst = (unsigned long)pdst;
417 unsigned long src = (unsigned long)psrc;
419 if (size == 0)
420 return NULL;
422 if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
423 conf = WDSIZE_32;
424 shift = 2;
425 } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
426 conf = WDSIZE_16;
427 shift = 1;
428 } else {
429 conf = WDSIZE_8;
430 shift = 0;
433 /* If the two memory regions have a chance of overlapping, make
434 * sure the memcpy still works as expected. Do this by having the
435 * copy run backwards instead.
437 mod = 1 << shift;
438 if (src < dst) {
439 mod *= -1;
440 dst += size + mod;
441 src += size + mod;
443 size >>= shift;
445 if (size > 0x10000)
446 conf |= DMA2D;
448 __dma_memcpy(dst, mod, src, mod, size, conf);
450 return pdst;
454 * dma_memcpy - DMA memcpy under mutex lock
456 * Do not check arguments before starting the DMA memcpy. Break the transfer
457 * up into two pieces. The first transfer is in multiples of 64k and the
458 * second transfer is the piece smaller than 64k.
460 void *dma_memcpy(void *pdst, const void *psrc, size_t size)
462 unsigned long dst = (unsigned long)pdst;
463 unsigned long src = (unsigned long)psrc;
465 if (bfin_addr_dcacheable(src))
466 blackfin_dcache_flush_range(src, src + size);
468 if (bfin_addr_dcacheable(dst))
469 blackfin_dcache_invalidate_range(dst, dst + size);
471 return dma_memcpy_nocache(pdst, psrc, size);
473 EXPORT_SYMBOL(dma_memcpy);
476 * dma_memcpy_nocache - DMA memcpy under mutex lock
477 * - No cache flush/invalidate
479 * Do not check arguments before starting the DMA memcpy. Break the transfer
480 * up into two pieces. The first transfer is in multiples of 64k and the
481 * second transfer is the piece smaller than 64k.
483 void *dma_memcpy_nocache(void *pdst, const void *psrc, size_t size)
485 size_t bulk, rest;
487 bulk = size & ~0xffff;
488 rest = size - bulk;
489 if (bulk)
490 _dma_memcpy(pdst, psrc, bulk);
491 _dma_memcpy(pdst + bulk, psrc + bulk, rest);
492 return pdst;
494 EXPORT_SYMBOL(dma_memcpy_nocache);
497 * safe_dma_memcpy - DMA memcpy w/argument checking
499 * Verify arguments are safe before heading to dma_memcpy().
501 void *safe_dma_memcpy(void *dst, const void *src, size_t size)
503 if (!access_ok(VERIFY_WRITE, dst, size))
504 return NULL;
505 if (!access_ok(VERIFY_READ, src, size))
506 return NULL;
507 return dma_memcpy(dst, src, size);
509 EXPORT_SYMBOL(safe_dma_memcpy);
511 static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
512 u16 size, u16 dma_size)
514 blackfin_dcache_flush_range(buf, buf + len * size);
515 __dma_memcpy(addr, 0, buf, size, len, dma_size);
518 static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
519 u16 size, u16 dma_size)
521 blackfin_dcache_invalidate_range(buf, buf + len * size);
522 __dma_memcpy(buf, size, addr, 0, len, dma_size);
525 #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
526 void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
528 _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
530 EXPORT_SYMBOL(dma_##io##s##bwl)
531 MAKE_DMA_IO(out, b, 1, 8, const);
532 MAKE_DMA_IO(in, b, 1, 8, );
533 MAKE_DMA_IO(out, w, 2, 16, const);
534 MAKE_DMA_IO(in, w, 2, 16, );
535 MAKE_DMA_IO(out, l, 4, 32, const);
536 MAKE_DMA_IO(in, l, 4, 32, );