2 * bfin_dma_5xx.c - Blackfin DMA implementation
4 * Copyright 2004-2006 Analog Devices Inc.
5 * Licensed under the GPL-2 or later.
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>
18 #include <asm/blackfin.h>
19 #include <asm/cacheflush.h>
21 #include <asm/uaccess.h>
23 /**************************************************************************
25 ***************************************************************************/
27 static struct dma_channel dma_ch[MAX_DMA_CHANNELS];
29 /*------------------------------------------------------------------------------
30 * Set the Buffer Clear bit in the Configuration register of specific DMA
31 * channel. This will stop the descriptor based DMA operation.
32 *-----------------------------------------------------------------------------*/
33 static void clear_dma_buffer(unsigned int channel)
35 dma_ch[channel].regs->cfg |= RESTART;
37 dma_ch[channel].regs->cfg &= ~RESTART;
40 static int __init blackfin_dma_init(void)
44 printk(KERN_INFO "Blackfin DMA Controller\n");
46 for (i = 0; i < MAX_DMA_CHANNELS; i++) {
47 dma_ch[i].chan_status = DMA_CHANNEL_FREE;
48 dma_ch[i].regs = dma_io_base_addr[i];
49 mutex_init(&(dma_ch[i].dmalock));
51 /* Mark MEMDMA Channel 0 as requested since we're using it internally */
52 request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");
53 request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");
55 #if defined(CONFIG_DEB_DMA_URGENT)
56 bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()
57 | DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);
62 arch_initcall(blackfin_dma_init);
65 static int proc_dma_show(struct seq_file *m, void *v)
69 for (i = 0; i < MAX_DMA_CHANNELS; ++i)
70 if (dma_ch[i].chan_status != DMA_CHANNEL_FREE)
71 seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id);
76 static int proc_dma_open(struct inode *inode, struct file *file)
78 return single_open(file, proc_dma_show, NULL);
81 static const struct file_operations proc_dma_operations = {
82 .open = proc_dma_open,
85 .release = single_release,
88 static int __init proc_dma_init(void)
90 return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;
92 late_initcall(proc_dma_init);
95 /*------------------------------------------------------------------------------
96 * Request the specific DMA channel from the system.
97 *-----------------------------------------------------------------------------*/
98 int request_dma(unsigned int channel, const char *device_id)
100 pr_debug("request_dma() : BEGIN \n");
102 if (device_id == NULL)
103 printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel);
105 #if defined(CONFIG_BF561) && ANOMALY_05000182
106 if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {
107 if (get_cclk() > 500000000) {
109 "Request IMDMA failed due to ANOMALY 05000182\n");
115 mutex_lock(&(dma_ch[channel].dmalock));
117 if ((dma_ch[channel].chan_status == DMA_CHANNEL_REQUESTED)
118 || (dma_ch[channel].chan_status == DMA_CHANNEL_ENABLED)) {
119 mutex_unlock(&(dma_ch[channel].dmalock));
120 pr_debug("DMA CHANNEL IN USE \n");
123 dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
124 pr_debug("DMA CHANNEL IS ALLOCATED \n");
127 mutex_unlock(&(dma_ch[channel].dmalock));
130 if (channel >= CH_UART2_RX && channel <= CH_UART3_TX) {
131 unsigned int per_map;
132 per_map = dma_ch[channel].regs->peripheral_map & 0xFFF;
133 if (strncmp(device_id, "BFIN_UART", 9) == 0)
134 dma_ch[channel].regs->peripheral_map = per_map |
135 ((channel - CH_UART2_RX + 0xC)<<12);
137 dma_ch[channel].regs->peripheral_map = per_map |
138 ((channel - CH_UART2_RX + 0x6)<<12);
142 dma_ch[channel].device_id = device_id;
143 dma_ch[channel].irq = 0;
145 /* This is to be enabled by putting a restriction -
146 * you have to request DMA, before doing any operations on
149 pr_debug("request_dma() : END \n");
152 EXPORT_SYMBOL(request_dma);
154 int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)
156 BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
157 && channel < MAX_DMA_CHANNELS));
159 if (callback != NULL) {
161 unsigned int irq = channel2irq(channel);
163 ret = request_irq(irq, callback, IRQF_DISABLED,
164 dma_ch[channel].device_id, data);
168 dma_ch[channel].irq = irq;
169 dma_ch[channel].data = data;
173 EXPORT_SYMBOL(set_dma_callback);
175 void free_dma(unsigned int channel)
177 pr_debug("freedma() : BEGIN \n");
178 BUG_ON(!(dma_ch[channel].chan_status != DMA_CHANNEL_FREE
179 && channel < MAX_DMA_CHANNELS));
182 disable_dma(channel);
183 clear_dma_buffer(channel);
185 if (dma_ch[channel].irq)
186 free_irq(dma_ch[channel].irq, dma_ch[channel].data);
188 /* Clear the DMA Variable in the Channel */
189 mutex_lock(&(dma_ch[channel].dmalock));
190 dma_ch[channel].chan_status = DMA_CHANNEL_FREE;
191 mutex_unlock(&(dma_ch[channel].dmalock));
193 pr_debug("freedma() : END \n");
195 EXPORT_SYMBOL(free_dma);
197 void dma_enable_irq(unsigned int channel)
199 pr_debug("dma_enable_irq() : BEGIN \n");
200 enable_irq(dma_ch[channel].irq);
202 EXPORT_SYMBOL(dma_enable_irq);
204 void dma_disable_irq(unsigned int channel)
206 pr_debug("dma_disable_irq() : BEGIN \n");
207 disable_irq(dma_ch[channel].irq);
209 EXPORT_SYMBOL(dma_disable_irq);
211 int dma_channel_active(unsigned int channel)
213 if (dma_ch[channel].chan_status == DMA_CHANNEL_FREE) {
219 EXPORT_SYMBOL(dma_channel_active);
221 /*------------------------------------------------------------------------------
222 * stop the specific DMA channel.
223 *-----------------------------------------------------------------------------*/
224 void disable_dma(unsigned int channel)
226 pr_debug("stop_dma() : BEGIN \n");
227 dma_ch[channel].regs->cfg &= ~DMAEN; /* Clean the enable bit */
229 dma_ch[channel].chan_status = DMA_CHANNEL_REQUESTED;
230 /* Needs to be enabled Later */
231 pr_debug("stop_dma() : END \n");
234 EXPORT_SYMBOL(disable_dma);
236 void enable_dma(unsigned int channel)
238 pr_debug("enable_dma() : BEGIN \n");
239 dma_ch[channel].chan_status = DMA_CHANNEL_ENABLED;
240 dma_ch[channel].regs->curr_x_count = 0;
241 dma_ch[channel].regs->curr_y_count = 0;
243 dma_ch[channel].regs->cfg |= DMAEN; /* Set the enable bit */
244 pr_debug("enable_dma() : END \n");
247 EXPORT_SYMBOL(enable_dma);
249 /*------------------------------------------------------------------------------
250 * Set the Start Address register for the specific DMA channel
251 * This function can be used for register based DMA,
252 * to setup the start address
253 * addr: Starting address of the DMA Data to be transferred.
254 *-----------------------------------------------------------------------------*/
255 void set_dma_start_addr(unsigned int channel, unsigned long addr)
257 pr_debug("set_dma_start_addr() : BEGIN \n");
258 dma_ch[channel].regs->start_addr = addr;
259 pr_debug("set_dma_start_addr() : END\n");
261 EXPORT_SYMBOL(set_dma_start_addr);
263 void set_dma_next_desc_addr(unsigned int channel, unsigned long addr)
265 pr_debug("set_dma_next_desc_addr() : BEGIN \n");
266 dma_ch[channel].regs->next_desc_ptr = addr;
267 pr_debug("set_dma_next_desc_addr() : END\n");
269 EXPORT_SYMBOL(set_dma_next_desc_addr);
271 void set_dma_curr_desc_addr(unsigned int channel, unsigned long addr)
273 pr_debug("set_dma_curr_desc_addr() : BEGIN \n");
274 dma_ch[channel].regs->curr_desc_ptr = addr;
275 pr_debug("set_dma_curr_desc_addr() : END\n");
277 EXPORT_SYMBOL(set_dma_curr_desc_addr);
279 void set_dma_x_count(unsigned int channel, unsigned short x_count)
281 dma_ch[channel].regs->x_count = x_count;
283 EXPORT_SYMBOL(set_dma_x_count);
285 void set_dma_y_count(unsigned int channel, unsigned short y_count)
287 dma_ch[channel].regs->y_count = y_count;
289 EXPORT_SYMBOL(set_dma_y_count);
291 void set_dma_x_modify(unsigned int channel, short x_modify)
293 dma_ch[channel].regs->x_modify = x_modify;
295 EXPORT_SYMBOL(set_dma_x_modify);
297 void set_dma_y_modify(unsigned int channel, short y_modify)
299 dma_ch[channel].regs->y_modify = y_modify;
301 EXPORT_SYMBOL(set_dma_y_modify);
303 void set_dma_config(unsigned int channel, unsigned short config)
305 dma_ch[channel].regs->cfg = config;
307 EXPORT_SYMBOL(set_dma_config);
310 set_bfin_dma_config(char direction, char flow_mode,
311 char intr_mode, char dma_mode, char width, char syncmode)
313 unsigned short config;
316 ((direction << 1) | (width << 2) | (dma_mode << 4) |
317 (intr_mode << 6) | (flow_mode << 12) | (syncmode << 5));
320 EXPORT_SYMBOL(set_bfin_dma_config);
322 void set_dma_sg(unsigned int channel, struct dmasg *sg, int nr_sg)
324 dma_ch[channel].regs->cfg |= ((nr_sg & 0x0F) << 8);
325 dma_ch[channel].regs->next_desc_ptr = (unsigned int)sg;
327 EXPORT_SYMBOL(set_dma_sg);
329 void set_dma_curr_addr(unsigned int channel, unsigned long addr)
331 dma_ch[channel].regs->curr_addr_ptr = addr;
333 EXPORT_SYMBOL(set_dma_curr_addr);
335 /*------------------------------------------------------------------------------
336 * Get the DMA status of a specific DMA channel from the system.
337 *-----------------------------------------------------------------------------*/
338 unsigned short get_dma_curr_irqstat(unsigned int channel)
340 return dma_ch[channel].regs->irq_status;
342 EXPORT_SYMBOL(get_dma_curr_irqstat);
344 /*------------------------------------------------------------------------------
345 * Clear the DMA_DONE bit in DMA status. Stop the DMA completion interrupt.
346 *-----------------------------------------------------------------------------*/
347 void clear_dma_irqstat(unsigned int channel)
349 dma_ch[channel].regs->irq_status |= 3;
351 EXPORT_SYMBOL(clear_dma_irqstat);
353 /*------------------------------------------------------------------------------
354 * Get current DMA xcount of a specific DMA channel from the system.
355 *-----------------------------------------------------------------------------*/
356 unsigned short get_dma_curr_xcount(unsigned int channel)
358 return dma_ch[channel].regs->curr_x_count;
360 EXPORT_SYMBOL(get_dma_curr_xcount);
362 /*------------------------------------------------------------------------------
363 * Get current DMA ycount of a specific DMA channel from the system.
364 *-----------------------------------------------------------------------------*/
365 unsigned short get_dma_curr_ycount(unsigned int channel)
367 return dma_ch[channel].regs->curr_y_count;
369 EXPORT_SYMBOL(get_dma_curr_ycount);
371 unsigned long get_dma_next_desc_ptr(unsigned int channel)
373 return dma_ch[channel].regs->next_desc_ptr;
375 EXPORT_SYMBOL(get_dma_next_desc_ptr);
377 unsigned long get_dma_curr_desc_ptr(unsigned int channel)
379 return dma_ch[channel].regs->curr_desc_ptr;
381 EXPORT_SYMBOL(get_dma_curr_desc_ptr);
383 unsigned long get_dma_curr_addr(unsigned int channel)
385 return dma_ch[channel].regs->curr_addr_ptr;
387 EXPORT_SYMBOL(get_dma_curr_addr);
390 # ifndef MAX_DMA_SUSPEND_CHANNELS
391 # define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS
393 int blackfin_dma_suspend(void)
397 for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i) {
398 if (dma_ch[i].chan_status == DMA_CHANNEL_ENABLED) {
399 printk(KERN_ERR "DMA Channel %d failed to suspend\n", i);
403 dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;
409 void blackfin_dma_resume(void)
412 for (i = 0; i < MAX_DMA_SUSPEND_CHANNELS; ++i)
413 dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;
418 * blackfin_dma_early_init - minimal DMA init
420 * Setup a few DMA registers so we can safely do DMA transfers early on in
421 * the kernel booting process. Really this just means using dma_memcpy().
423 void __init blackfin_dma_early_init(void)
425 bfin_write_MDMA_S0_CONFIG(0);
429 * __dma_memcpy - program the MDMA registers
431 * Actually program MDMA0 and wait for the transfer to finish. Disable IRQs
432 * while programming registers so that everything is fully configured. Wait
433 * for DMA to finish with IRQs enabled. If interrupted, the initial DMA_DONE
434 * check will make sure we don't clobber any existing transfer.
436 static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)
438 static DEFINE_SPINLOCK(mdma_lock);
441 spin_lock_irqsave(&mdma_lock, flags);
443 if (bfin_read_MDMA_S0_CONFIG())
444 while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
448 /* For larger bit sizes, we've already divided down cnt so it
449 * is no longer a multiple of 64k. So we have to break down
450 * the limit here so it is a multiple of the incoming size.
451 * There is no limitation here in terms of total size other
452 * than the hardware though as the bits lost in the shift are
453 * made up by MODIFY (== we can hit the whole address space).
454 * X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4
456 u32 shift = abs(dmod) >> 1;
457 size_t ycnt = cnt >> (16 - shift);
458 cnt = 1 << (16 - shift);
459 bfin_write_MDMA_D0_Y_COUNT(ycnt);
460 bfin_write_MDMA_S0_Y_COUNT(ycnt);
461 bfin_write_MDMA_D0_Y_MODIFY(dmod);
462 bfin_write_MDMA_S0_Y_MODIFY(smod);
465 bfin_write_MDMA_D0_START_ADDR(daddr);
466 bfin_write_MDMA_D0_X_COUNT(cnt);
467 bfin_write_MDMA_D0_X_MODIFY(dmod);
468 bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
470 bfin_write_MDMA_S0_START_ADDR(saddr);
471 bfin_write_MDMA_S0_X_COUNT(cnt);
472 bfin_write_MDMA_S0_X_MODIFY(smod);
473 bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);
475 bfin_write_MDMA_S0_CONFIG(DMAEN | conf);
476 bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);
478 spin_unlock_irqrestore(&mdma_lock, flags);
482 while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))
483 if (bfin_read_MDMA_S0_CONFIG())
488 bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);
490 bfin_write_MDMA_S0_CONFIG(0);
491 bfin_write_MDMA_D0_CONFIG(0);
495 * _dma_memcpy - translate C memcpy settings into MDMA settings
497 * Handle all the high level steps before we touch the MDMA registers. So
498 * handle caching, tweaking of sizes, and formatting of addresses.
500 static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)
504 unsigned long dst = (unsigned long)pdst;
505 unsigned long src = (unsigned long)psrc;
510 if (bfin_addr_dcachable(src))
511 blackfin_dcache_flush_range(src, src + size);
513 if (bfin_addr_dcachable(dst))
514 blackfin_dcache_invalidate_range(dst, dst + size);
516 if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {
519 } else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {
527 /* If the two memory regions have a chance of overlapping, make
528 * sure the memcpy still works as expected. Do this by having the
529 * copy run backwards instead.
542 __dma_memcpy(dst, mod, src, mod, size, conf);
548 * dma_memcpy - DMA memcpy under mutex lock
550 * Do not check arguments before starting the DMA memcpy. Break the transfer
551 * up into two pieces. The first transfer is in multiples of 64k and the
552 * second transfer is the piece smaller than 64k.
554 void *dma_memcpy(void *dst, const void *src, size_t size)
557 bulk = size & ~0xffff;
560 _dma_memcpy(dst, src, bulk);
561 _dma_memcpy(dst + bulk, src + bulk, rest);
564 EXPORT_SYMBOL(dma_memcpy);
567 * safe_dma_memcpy - DMA memcpy w/argument checking
569 * Verify arguments are safe before heading to dma_memcpy().
571 void *safe_dma_memcpy(void *dst, const void *src, size_t size)
573 if (!access_ok(VERIFY_WRITE, dst, size))
575 if (!access_ok(VERIFY_READ, src, size))
577 return dma_memcpy(dst, src, size);
579 EXPORT_SYMBOL(safe_dma_memcpy);
581 static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,
582 u16 size, u16 dma_size)
584 blackfin_dcache_flush_range(buf, buf + len * size);
585 __dma_memcpy(addr, 0, buf, size, len, dma_size);
588 static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,
589 u16 size, u16 dma_size)
591 blackfin_dcache_invalidate_range(buf, buf + len * size);
592 __dma_memcpy(buf, size, addr, 0, len, dma_size);
595 #define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \
596 void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \
598 _dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \
600 EXPORT_SYMBOL(dma_##io##s##bwl)
601 MAKE_DMA_IO(out, b, 1, 8, const);
602 MAKE_DMA_IO(in, b, 1, 8, );
603 MAKE_DMA_IO(out, w, 2, 16, const);
604 MAKE_DMA_IO(in, w, 2, 16, );
605 MAKE_DMA_IO(out, l, 4, 32, const);
606 MAKE_DMA_IO(in, l, 4, 32, );