2 * linux/arch/arm/plat-omap/dma.c
4 * Copyright (C) 2003 Nokia Corporation
5 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
6 * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
7 * Graphics DMA and LCD DMA graphics tranformations
8 * by Imre Deak <imre.deak@nokia.com>
9 * OMAP2 support Copyright (C) 2004-2005 Texas Instruments, Inc.
10 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
11 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
13 * Support functions for the OMAP internal DMA channels.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
29 #include <asm/system.h>
30 #include <asm/hardware.h>
34 #include <asm/arch/tc.h>
39 #define debug_printk(x) printk x
41 #define debug_printk(x)
44 #define OMAP_DMA_ACTIVE 0x01
45 #define OMAP_DMA_CCR_EN (1 << 7)
47 #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
49 static int enable_1510_mode = 0;
57 void (* callback)(int lch, u16 ch_status, void *data);
62 static int dma_chan_count;
64 static spinlock_t dma_chan_lock;
65 static struct omap_dma_lch dma_chan[OMAP_LOGICAL_DMA_CH_COUNT];
67 static const u8 omap1_dma_irq[OMAP_LOGICAL_DMA_CH_COUNT] = {
68 INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
69 INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
70 INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
71 INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
72 INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
75 #define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
78 #ifdef CONFIG_ARCH_OMAP15XX
79 /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
80 int omap_dma_in_1510_mode(void)
82 return enable_1510_mode;
85 #define omap_dma_in_1510_mode() 0
88 #ifdef CONFIG_ARCH_OMAP1
89 static inline int get_gdma_dev(int req)
91 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
92 int shift = ((req - 1) % 5) * 6;
94 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
97 static inline void set_gdma_dev(int req, int dev)
99 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
100 int shift = ((req - 1) % 5) * 6;
104 l &= ~(0x3f << shift);
105 l |= (dev - 1) << shift;
109 #define set_gdma_dev(req, dev) do {} while (0)
112 static void clear_lch_regs(int lch)
115 u32 lch_base = OMAP_DMA_BASE + lch * 0x40;
117 for (i = 0; i < 0x2c; i += 2)
118 omap_writew(0, lch_base + i);
121 void omap_set_dma_priority(int dst_port, int priority)
127 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
128 reg = OMAP_TC_OCPT1_PRIOR;
130 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
131 reg = OMAP_TC_OCPT2_PRIOR;
133 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
134 reg = OMAP_TC_EMIFF_PRIOR;
136 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
137 reg = OMAP_TC_EMIFS_PRIOR;
145 l |= (priority & 0xf) << 8;
149 void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
150 int frame_count, int sync_mode,
151 int dma_trigger, int src_or_dst_synch)
153 OMAP_DMA_CSDP_REG(lch) &= ~0x03;
154 OMAP_DMA_CSDP_REG(lch) |= data_type;
156 if (cpu_class_is_omap1()) {
157 OMAP_DMA_CCR_REG(lch) &= ~(1 << 5);
158 if (sync_mode == OMAP_DMA_SYNC_FRAME)
159 OMAP_DMA_CCR_REG(lch) |= 1 << 5;
161 OMAP1_DMA_CCR2_REG(lch) &= ~(1 << 2);
162 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
163 OMAP1_DMA_CCR2_REG(lch) |= 1 << 2;
166 if (cpu_is_omap24xx() && dma_trigger) {
167 u32 val = OMAP_DMA_CCR_REG(lch);
169 if (dma_trigger > 63)
171 if (dma_trigger > 31)
174 val |= (dma_trigger & 0x1f);
176 if (sync_mode & OMAP_DMA_SYNC_FRAME)
179 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
182 if (src_or_dst_synch)
183 val |= 1 << 24; /* source synch */
185 val &= ~(1 << 24); /* dest synch */
187 OMAP_DMA_CCR_REG(lch) = val;
190 OMAP_DMA_CEN_REG(lch) = elem_count;
191 OMAP_DMA_CFN_REG(lch) = frame_count;
194 void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
198 BUG_ON(omap_dma_in_1510_mode());
200 if (cpu_is_omap24xx()) {
205 w = OMAP1_DMA_CCR2_REG(lch) & ~0x03;
207 case OMAP_DMA_CONSTANT_FILL:
210 case OMAP_DMA_TRANSPARENT_COPY:
213 case OMAP_DMA_COLOR_DIS:
218 OMAP1_DMA_CCR2_REG(lch) = w;
220 w = OMAP1_DMA_LCH_CTRL_REG(lch) & ~0x0f;
221 /* Default is channel type 2D */
223 OMAP1_DMA_COLOR_L_REG(lch) = (u16)color;
224 OMAP1_DMA_COLOR_U_REG(lch) = (u16)(color >> 16);
225 w |= 1; /* Channel type G */
227 OMAP1_DMA_LCH_CTRL_REG(lch) = w;
230 /* Note that src_port is only for omap1 */
231 void omap_set_dma_src_params(int lch, int src_port, int src_amode,
232 unsigned long src_start,
233 int src_ei, int src_fi)
235 if (cpu_class_is_omap1()) {
236 OMAP_DMA_CSDP_REG(lch) &= ~(0x1f << 2);
237 OMAP_DMA_CSDP_REG(lch) |= src_port << 2;
240 OMAP_DMA_CCR_REG(lch) &= ~(0x03 << 12);
241 OMAP_DMA_CCR_REG(lch) |= src_amode << 12;
243 if (cpu_class_is_omap1()) {
244 OMAP1_DMA_CSSA_U_REG(lch) = src_start >> 16;
245 OMAP1_DMA_CSSA_L_REG(lch) = src_start;
248 if (cpu_is_omap24xx())
249 OMAP2_DMA_CSSA_REG(lch) = src_start;
251 OMAP_DMA_CSEI_REG(lch) = src_ei;
252 OMAP_DMA_CSFI_REG(lch) = src_fi;
255 void omap_set_dma_params(int lch, struct omap_dma_channel_params * params)
257 omap_set_dma_transfer_params(lch, params->data_type,
258 params->elem_count, params->frame_count,
259 params->sync_mode, params->trigger,
260 params->src_or_dst_synch);
261 omap_set_dma_src_params(lch, params->src_port,
262 params->src_amode, params->src_start,
263 params->src_ei, params->src_fi);
265 omap_set_dma_dest_params(lch, params->dst_port,
266 params->dst_amode, params->dst_start,
267 params->dst_ei, params->dst_fi);
270 void omap_set_dma_src_index(int lch, int eidx, int fidx)
272 if (cpu_is_omap24xx()) {
276 OMAP_DMA_CSEI_REG(lch) = eidx;
277 OMAP_DMA_CSFI_REG(lch) = fidx;
280 void omap_set_dma_src_data_pack(int lch, int enable)
282 OMAP_DMA_CSDP_REG(lch) &= ~(1 << 6);
284 OMAP_DMA_CSDP_REG(lch) |= (1 << 6);
287 void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
289 OMAP_DMA_CSDP_REG(lch) &= ~(0x03 << 7);
291 switch (burst_mode) {
292 case OMAP_DMA_DATA_BURST_DIS:
294 case OMAP_DMA_DATA_BURST_4:
295 OMAP_DMA_CSDP_REG(lch) |= (0x02 << 7);
297 case OMAP_DMA_DATA_BURST_8:
298 /* not supported by current hardware
307 /* Note that dest_port is only for OMAP1 */
308 void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
309 unsigned long dest_start,
310 int dst_ei, int dst_fi)
312 if (cpu_class_is_omap1()) {
313 OMAP_DMA_CSDP_REG(lch) &= ~(0x1f << 9);
314 OMAP_DMA_CSDP_REG(lch) |= dest_port << 9;
317 OMAP_DMA_CCR_REG(lch) &= ~(0x03 << 14);
318 OMAP_DMA_CCR_REG(lch) |= dest_amode << 14;
320 if (cpu_class_is_omap1()) {
321 OMAP1_DMA_CDSA_U_REG(lch) = dest_start >> 16;
322 OMAP1_DMA_CDSA_L_REG(lch) = dest_start;
325 if (cpu_is_omap24xx())
326 OMAP2_DMA_CDSA_REG(lch) = dest_start;
328 OMAP_DMA_CDEI_REG(lch) = dst_ei;
329 OMAP_DMA_CDFI_REG(lch) = dst_fi;
332 void omap_set_dma_dest_index(int lch, int eidx, int fidx)
334 if (cpu_is_omap24xx()) {
338 OMAP_DMA_CDEI_REG(lch) = eidx;
339 OMAP_DMA_CDFI_REG(lch) = fidx;
342 void omap_set_dma_dest_data_pack(int lch, int enable)
344 OMAP_DMA_CSDP_REG(lch) &= ~(1 << 13);
346 OMAP_DMA_CSDP_REG(lch) |= 1 << 13;
349 void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
351 OMAP_DMA_CSDP_REG(lch) &= ~(0x03 << 14);
353 switch (burst_mode) {
354 case OMAP_DMA_DATA_BURST_DIS:
356 case OMAP_DMA_DATA_BURST_4:
357 OMAP_DMA_CSDP_REG(lch) |= (0x02 << 14);
359 case OMAP_DMA_DATA_BURST_8:
360 OMAP_DMA_CSDP_REG(lch) |= (0x03 << 14);
363 printk(KERN_ERR "Invalid DMA burst mode\n");
369 static inline void omap_enable_channel_irq(int lch)
373 /* Read CSR to make sure it's cleared. */
374 status = OMAP_DMA_CSR_REG(lch);
376 /* Enable some nice interrupts. */
377 OMAP_DMA_CICR_REG(lch) = dma_chan[lch].enabled_irqs;
379 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
382 static void omap_disable_channel_irq(int lch)
384 if (cpu_is_omap24xx())
385 OMAP_DMA_CICR_REG(lch) = 0;
388 void omap_enable_dma_irq(int lch, u16 bits)
390 dma_chan[lch].enabled_irqs |= bits;
393 void omap_disable_dma_irq(int lch, u16 bits)
395 dma_chan[lch].enabled_irqs &= ~bits;
398 static inline void enable_lnk(int lch)
400 if (cpu_class_is_omap1())
401 OMAP_DMA_CLNK_CTRL_REG(lch) &= ~(1 << 14);
403 /* Set the ENABLE_LNK bits */
404 if (dma_chan[lch].next_lch != -1)
405 OMAP_DMA_CLNK_CTRL_REG(lch) =
406 dma_chan[lch].next_lch | (1 << 15);
409 static inline void disable_lnk(int lch)
411 /* Disable interrupts */
412 if (cpu_class_is_omap1()) {
413 OMAP_DMA_CICR_REG(lch) = 0;
414 /* Set the STOP_LNK bit */
415 OMAP_DMA_CLNK_CTRL_REG(lch) |= 1 << 14;
418 if (cpu_is_omap24xx()) {
419 omap_disable_channel_irq(lch);
420 /* Clear the ENABLE_LNK bit */
421 OMAP_DMA_CLNK_CTRL_REG(lch) &= ~(1 << 15);
424 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
427 static inline void omap2_enable_irq_lch(int lch)
431 if (!cpu_is_omap24xx())
434 val = omap_readl(OMAP_DMA4_IRQENABLE_L0);
436 omap_writel(val, OMAP_DMA4_IRQENABLE_L0);
439 int omap_request_dma(int dev_id, const char *dev_name,
440 void (* callback)(int lch, u16 ch_status, void *data),
441 void *data, int *dma_ch_out)
443 int ch, free_ch = -1;
445 struct omap_dma_lch *chan;
447 spin_lock_irqsave(&dma_chan_lock, flags);
448 for (ch = 0; ch < dma_chan_count; ch++) {
449 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
456 spin_unlock_irqrestore(&dma_chan_lock, flags);
459 chan = dma_chan + free_ch;
460 chan->dev_id = dev_id;
462 if (cpu_class_is_omap1())
463 clear_lch_regs(free_ch);
465 if (cpu_is_omap24xx())
466 omap_clear_dma(free_ch);
468 spin_unlock_irqrestore(&dma_chan_lock, flags);
470 chan->dev_name = dev_name;
471 chan->callback = callback;
473 chan->enabled_irqs = OMAP_DMA_TOUT_IRQ | OMAP_DMA_DROP_IRQ |
476 if (cpu_is_omap24xx())
477 chan->enabled_irqs |= OMAP2_DMA_TRANS_ERR_IRQ;
479 if (cpu_is_omap16xx()) {
480 /* If the sync device is set, configure it dynamically. */
482 set_gdma_dev(free_ch + 1, dev_id);
483 dev_id = free_ch + 1;
485 /* Disable the 1510 compatibility mode and set the sync device
487 OMAP_DMA_CCR_REG(free_ch) = dev_id | (1 << 10);
488 } else if (cpu_is_omap730() || cpu_is_omap15xx()) {
489 OMAP_DMA_CCR_REG(free_ch) = dev_id;
492 if (cpu_is_omap24xx()) {
493 omap2_enable_irq_lch(free_ch);
495 omap_enable_channel_irq(free_ch);
496 /* Clear the CSR register and IRQ status register */
497 OMAP_DMA_CSR_REG(free_ch) = 0x0;
498 omap_writel(~0x0, OMAP_DMA4_IRQSTATUS_L0);
501 *dma_ch_out = free_ch;
506 void omap_free_dma(int lch)
510 spin_lock_irqsave(&dma_chan_lock, flags);
511 if (dma_chan[lch].dev_id == -1) {
512 printk("omap_dma: trying to free nonallocated DMA channel %d\n",
514 spin_unlock_irqrestore(&dma_chan_lock, flags);
517 dma_chan[lch].dev_id = -1;
518 dma_chan[lch].next_lch = -1;
519 dma_chan[lch].callback = NULL;
520 spin_unlock_irqrestore(&dma_chan_lock, flags);
522 if (cpu_class_is_omap1()) {
523 /* Disable all DMA interrupts for the channel. */
524 OMAP_DMA_CICR_REG(lch) = 0;
525 /* Make sure the DMA transfer is stopped. */
526 OMAP_DMA_CCR_REG(lch) = 0;
529 if (cpu_is_omap24xx()) {
531 /* Disable interrupts */
532 val = omap_readl(OMAP_DMA4_IRQENABLE_L0);
534 omap_writel(val, OMAP_DMA4_IRQENABLE_L0);
536 /* Clear the CSR register and IRQ status register */
537 OMAP_DMA_CSR_REG(lch) = 0x0;
539 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
541 omap_writel(val, OMAP_DMA4_IRQSTATUS_L0);
543 /* Disable all DMA interrupts for the channel. */
544 OMAP_DMA_CICR_REG(lch) = 0;
546 /* Make sure the DMA transfer is stopped. */
547 OMAP_DMA_CCR_REG(lch) = 0;
553 * Clears any DMA state so the DMA engine is ready to restart with new buffers
554 * through omap_start_dma(). Any buffers in flight are discarded.
556 void omap_clear_dma(int lch)
560 local_irq_save(flags);
562 if (cpu_class_is_omap1()) {
564 OMAP_DMA_CCR_REG(lch) &= ~OMAP_DMA_CCR_EN;
566 /* Clear pending interrupts */
567 status = OMAP_DMA_CSR_REG(lch);
570 if (cpu_is_omap24xx()) {
572 u32 lch_base = OMAP24XX_DMA_BASE + lch * 0x60 + 0x80;
573 for (i = 0; i < 0x44; i += 4)
574 omap_writel(0, lch_base + i);
577 local_irq_restore(flags);
580 void omap_start_dma(int lch)
582 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
583 int next_lch, cur_lch;
584 char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
586 dma_chan_link_map[lch] = 1;
587 /* Set the link register of the first channel */
590 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
591 cur_lch = dma_chan[lch].next_lch;
593 next_lch = dma_chan[cur_lch].next_lch;
595 /* The loop case: we've been here already */
596 if (dma_chan_link_map[cur_lch])
598 /* Mark the current channel */
599 dma_chan_link_map[cur_lch] = 1;
602 omap_enable_channel_irq(cur_lch);
605 } while (next_lch != -1);
606 } else if (cpu_is_omap24xx()) {
607 /* Errata: Need to write lch even if not using chaining */
608 OMAP_DMA_CLNK_CTRL_REG(lch) = lch;
611 omap_enable_channel_irq(lch);
613 /* Errata: On ES2.0 BUFFERING disable must be set.
614 * This will always fail on ES1.0 */
615 if (cpu_is_omap24xx()) {
616 OMAP_DMA_CCR_REG(lch) |= OMAP_DMA_CCR_EN;
619 OMAP_DMA_CCR_REG(lch) |= OMAP_DMA_CCR_EN;
621 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
624 void omap_stop_dma(int lch)
626 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
627 int next_lch, cur_lch = lch;
628 char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
630 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
632 /* The loop case: we've been here already */
633 if (dma_chan_link_map[cur_lch])
635 /* Mark the current channel */
636 dma_chan_link_map[cur_lch] = 1;
638 disable_lnk(cur_lch);
640 next_lch = dma_chan[cur_lch].next_lch;
642 } while (next_lch != -1);
647 /* Disable all interrupts on the channel */
648 if (cpu_class_is_omap1())
649 OMAP_DMA_CICR_REG(lch) = 0;
651 OMAP_DMA_CCR_REG(lch) &= ~OMAP_DMA_CCR_EN;
652 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
656 * Returns current physical source address for the given DMA channel.
657 * If the channel is running the caller must disable interrupts prior calling
658 * this function and process the returned value before re-enabling interrupt to
659 * prevent races with the interrupt handler. Note that in continuous mode there
660 * is a chance for CSSA_L register overflow inbetween the two reads resulting
661 * in incorrect return value.
663 dma_addr_t omap_get_dma_src_pos(int lch)
667 if (cpu_class_is_omap1())
668 offset = (dma_addr_t) (OMAP1_DMA_CSSA_L_REG(lch) |
669 (OMAP1_DMA_CSSA_U_REG(lch) << 16));
671 if (cpu_is_omap24xx())
672 offset = OMAP_DMA_CSAC_REG(lch);
678 * Returns current physical destination address for the given DMA channel.
679 * If the channel is running the caller must disable interrupts prior calling
680 * this function and process the returned value before re-enabling interrupt to
681 * prevent races with the interrupt handler. Note that in continuous mode there
682 * is a chance for CDSA_L register overflow inbetween the two reads resulting
683 * in incorrect return value.
685 dma_addr_t omap_get_dma_dst_pos(int lch)
689 if (cpu_class_is_omap1())
690 offset = (dma_addr_t) (OMAP1_DMA_CDSA_L_REG(lch) |
691 (OMAP1_DMA_CDSA_U_REG(lch) << 16));
693 if (cpu_is_omap24xx())
694 offset = OMAP2_DMA_CDSA_REG(lch);
700 * Returns current source transfer counting for the given DMA channel.
701 * Can be used to monitor the progress of a transfer inside a block.
702 * It must be called with disabled interrupts.
704 int omap_get_dma_src_addr_counter(int lch)
706 return (dma_addr_t) OMAP_DMA_CSAC_REG(lch);
709 int omap_dma_running(void)
713 /* Check if LCD DMA is running */
714 if (cpu_is_omap16xx())
715 if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
718 for (lch = 0; lch < dma_chan_count; lch++)
719 if (OMAP_DMA_CCR_REG(lch) & OMAP_DMA_CCR_EN)
726 * lch_queue DMA will start right after lch_head one is finished.
727 * For this DMA link to start, you still need to start (see omap_start_dma)
728 * the first one. That will fire up the entire queue.
730 void omap_dma_link_lch (int lch_head, int lch_queue)
732 if (omap_dma_in_1510_mode()) {
733 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
738 if ((dma_chan[lch_head].dev_id == -1) ||
739 (dma_chan[lch_queue].dev_id == -1)) {
740 printk(KERN_ERR "omap_dma: trying to link "
741 "non requested channels\n");
745 dma_chan[lch_head].next_lch = lch_queue;
749 * Once the DMA queue is stopped, we can destroy it.
751 void omap_dma_unlink_lch (int lch_head, int lch_queue)
753 if (omap_dma_in_1510_mode()) {
754 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
759 if (dma_chan[lch_head].next_lch != lch_queue ||
760 dma_chan[lch_head].next_lch == -1) {
761 printk(KERN_ERR "omap_dma: trying to unlink "
762 "non linked channels\n");
767 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
768 (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
769 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
770 "before unlinking\n");
774 dma_chan[lch_head].next_lch = -1;
777 /*----------------------------------------------------------------------------*/
779 #ifdef CONFIG_ARCH_OMAP1
781 static int omap1_dma_handle_ch(int ch)
785 if (enable_1510_mode && ch >= 6) {
786 csr = dma_chan[ch].saved_csr;
787 dma_chan[ch].saved_csr = 0;
789 csr = OMAP_DMA_CSR_REG(ch);
790 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
791 dma_chan[ch + 6].saved_csr = csr >> 7;
794 if ((csr & 0x3f) == 0)
796 if (unlikely(dma_chan[ch].dev_id == -1)) {
797 printk(KERN_WARNING "Spurious interrupt from DMA channel "
798 "%d (CSR %04x)\n", ch, csr);
801 if (unlikely(csr & OMAP_DMA_TOUT_IRQ))
802 printk(KERN_WARNING "DMA timeout with device %d\n",
803 dma_chan[ch].dev_id);
804 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
805 printk(KERN_WARNING "DMA synchronization event drop occurred "
806 "with device %d\n", dma_chan[ch].dev_id);
807 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
808 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
809 if (likely(dma_chan[ch].callback != NULL))
810 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
814 static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id,
815 struct pt_regs *regs)
817 int ch = ((int) dev_id) - 1;
823 handled_now += omap1_dma_handle_ch(ch);
824 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
825 handled_now += omap1_dma_handle_ch(ch + 6);
828 handled += handled_now;
831 return handled ? IRQ_HANDLED : IRQ_NONE;
835 #define omap1_dma_irq_handler NULL
838 #ifdef CONFIG_ARCH_OMAP2
840 static int omap2_dma_handle_ch(int ch)
842 u32 status = OMAP_DMA_CSR_REG(ch);
847 if (unlikely(dma_chan[ch].dev_id == -1))
849 /* REVISIT: According to 24xx TRM, there's no TOUT_IE */
850 if (unlikely(status & OMAP_DMA_TOUT_IRQ))
851 printk(KERN_INFO "DMA timeout with device %d\n",
852 dma_chan[ch].dev_id);
853 if (unlikely(status & OMAP_DMA_DROP_IRQ))
855 "DMA synchronization event drop occurred with device "
856 "%d\n", dma_chan[ch].dev_id);
858 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ))
859 printk(KERN_INFO "DMA transaction error with device %d\n",
860 dma_chan[ch].dev_id);
862 OMAP_DMA_CSR_REG(ch) = 0x20;
864 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
865 /* ch in this function is from 0-31 while in register it is 1-32 */
867 omap_writel(val, OMAP_DMA4_IRQSTATUS_L0);
869 if (likely(dma_chan[ch].callback != NULL))
870 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
875 /* STATUS register count is from 1-32 while our is 0-31 */
876 static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id,
877 struct pt_regs *regs)
882 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
884 for (i = 1; i <= OMAP_LOGICAL_DMA_CH_COUNT; i++) {
885 int active = val & (1 << (i - 1));
887 omap2_dma_handle_ch(i - 1);
893 static struct irqaction omap24xx_dma_irq = {
895 .handler = omap2_dma_irq_handler,
896 .flags = SA_INTERRUPT
900 static struct irqaction omap24xx_dma_irq;
903 /*----------------------------------------------------------------------------*/
905 static struct lcd_dma_info {
908 void (* callback)(u16 status, void *data);
912 unsigned long addr, size;
913 int rotate, data_type, xres, yres;
922 void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
926 lcd_dma.data_type = data_type;
927 lcd_dma.xres = fb_xres;
928 lcd_dma.yres = fb_yres;
931 void omap_set_lcd_dma_src_port(int port)
933 lcd_dma.src_port = port;
936 void omap_set_lcd_dma_ext_controller(int external)
938 lcd_dma.ext_ctrl = external;
941 void omap_set_lcd_dma_single_transfer(int single)
943 lcd_dma.single_transfer = single;
947 void omap_set_lcd_dma_b1_rotation(int rotate)
949 if (omap_dma_in_1510_mode()) {
950 printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
954 lcd_dma.rotate = rotate;
957 void omap_set_lcd_dma_b1_mirror(int mirror)
959 if (omap_dma_in_1510_mode()) {
960 printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
963 lcd_dma.mirror = mirror;
966 void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
968 if (omap_dma_in_1510_mode()) {
969 printk(KERN_ERR "DMA virtual resulotion is not supported "
973 lcd_dma.vxres = vxres;
976 void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
978 if (omap_dma_in_1510_mode()) {
979 printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
982 lcd_dma.xscale = xscale;
983 lcd_dma.yscale = yscale;
986 static void set_b1_regs(void)
988 unsigned long top, bottom;
991 unsigned long en, fn;
994 unsigned int xscale, yscale;
996 switch (lcd_dma.data_type) {
997 case OMAP_DMA_DATA_TYPE_S8:
1000 case OMAP_DMA_DATA_TYPE_S16:
1003 case OMAP_DMA_DATA_TYPE_S32:
1011 vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
1012 xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
1013 yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
1014 BUG_ON(vxres < lcd_dma.xres);
1015 #define PIXADDR(x,y) (lcd_dma.addr + ((y) * vxres * yscale + (x) * xscale) * es)
1016 #define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
1017 switch (lcd_dma.rotate) {
1019 if (!lcd_dma.mirror) {
1020 top = PIXADDR(0, 0);
1021 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1022 /* 1510 DMA requires the bottom address to be 2 more
1023 * than the actual last memory access location. */
1024 if (omap_dma_in_1510_mode() &&
1025 lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
1027 ei = PIXSTEP(0, 0, 1, 0);
1028 fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
1030 top = PIXADDR(lcd_dma.xres - 1, 0);
1031 bottom = PIXADDR(0, lcd_dma.yres - 1);
1032 ei = PIXSTEP(1, 0, 0, 0);
1033 fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
1039 if (!lcd_dma.mirror) {
1040 top = PIXADDR(0, lcd_dma.yres - 1);
1041 bottom = PIXADDR(lcd_dma.xres - 1, 0);
1042 ei = PIXSTEP(0, 1, 0, 0);
1043 fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
1045 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1046 bottom = PIXADDR(0, 0);
1047 ei = PIXSTEP(0, 1, 0, 0);
1048 fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
1054 if (!lcd_dma.mirror) {
1055 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1056 bottom = PIXADDR(0, 0);
1057 ei = PIXSTEP(1, 0, 0, 0);
1058 fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
1060 top = PIXADDR(0, lcd_dma.yres - 1);
1061 bottom = PIXADDR(lcd_dma.xres - 1, 0);
1062 ei = PIXSTEP(0, 0, 1, 0);
1063 fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
1069 if (!lcd_dma.mirror) {
1070 top = PIXADDR(lcd_dma.xres - 1, 0);
1071 bottom = PIXADDR(0, lcd_dma.yres - 1);
1072 ei = PIXSTEP(0, 0, 0, 1);
1073 fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
1075 top = PIXADDR(0, 0);
1076 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1077 ei = PIXSTEP(0, 0, 0, 1);
1078 fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
1085 return; /* Supress warning about uninitialized vars */
1088 if (omap_dma_in_1510_mode()) {
1089 omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
1090 omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
1091 omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
1092 omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
1098 omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
1099 omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
1100 omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
1101 omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
1103 omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
1104 omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
1106 w = omap_readw(OMAP1610_DMA_LCD_CSDP);
1108 w |= lcd_dma.data_type;
1109 omap_writew(w, OMAP1610_DMA_LCD_CSDP);
1111 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1112 /* Always set the source port as SDRAM for now*/
1114 if (lcd_dma.callback != NULL)
1115 w |= 1 << 1; /* Block interrupt enable */
1118 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1120 if (!(lcd_dma.rotate || lcd_dma.mirror ||
1121 lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
1124 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1125 /* Set the double-indexed addressing mode */
1127 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1129 omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
1130 omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
1131 omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
1134 static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id,
1135 struct pt_regs *regs)
1139 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1140 if (unlikely(!(w & (1 << 3)))) {
1141 printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
1146 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1148 if (lcd_dma.callback != NULL)
1149 lcd_dma.callback(w, lcd_dma.cb_data);
1154 int omap_request_lcd_dma(void (* callback)(u16 status, void *data),
1157 spin_lock_irq(&lcd_dma.lock);
1158 if (lcd_dma.reserved) {
1159 spin_unlock_irq(&lcd_dma.lock);
1160 printk(KERN_ERR "LCD DMA channel already reserved\n");
1164 lcd_dma.reserved = 1;
1165 spin_unlock_irq(&lcd_dma.lock);
1166 lcd_dma.callback = callback;
1167 lcd_dma.cb_data = data;
1169 lcd_dma.single_transfer = 0;
1175 lcd_dma.ext_ctrl = 0;
1176 lcd_dma.src_port = 0;
1181 void omap_free_lcd_dma(void)
1183 spin_lock(&lcd_dma.lock);
1184 if (!lcd_dma.reserved) {
1185 spin_unlock(&lcd_dma.lock);
1186 printk(KERN_ERR "LCD DMA is not reserved\n");
1190 if (!enable_1510_mode)
1191 omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1,
1192 OMAP1610_DMA_LCD_CCR);
1193 lcd_dma.reserved = 0;
1194 spin_unlock(&lcd_dma.lock);
1197 void omap_enable_lcd_dma(void)
1201 /* Set the Enable bit only if an external controller is
1202 * connected. Otherwise the OMAP internal controller will
1203 * start the transfer when it gets enabled.
1205 if (enable_1510_mode || !lcd_dma.ext_ctrl)
1208 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1210 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1214 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1216 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1219 void omap_setup_lcd_dma(void)
1221 BUG_ON(lcd_dma.active);
1222 if (!enable_1510_mode) {
1223 /* Set some reasonable defaults */
1224 omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
1225 omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
1226 omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
1229 if (!enable_1510_mode) {
1232 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1233 /* If DMA was already active set the end_prog bit to have
1234 * the programmed register set loaded into the active
1237 w |= 1 << 11; /* End_prog */
1238 if (!lcd_dma.single_transfer)
1239 w |= (3 << 8); /* Auto_init, repeat */
1240 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1244 void omap_stop_lcd_dma(void)
1249 if (enable_1510_mode || !lcd_dma.ext_ctrl)
1252 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1254 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1256 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1258 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1261 int omap_lcd_dma_ext_running(void)
1263 return lcd_dma.ext_ctrl && lcd_dma.active;
1266 /*----------------------------------------------------------------------------*/
1268 static int __init omap_init_dma(void)
1272 if (cpu_is_omap15xx()) {
1273 printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
1275 enable_1510_mode = 1;
1276 } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
1277 printk(KERN_INFO "OMAP DMA hardware version %d\n",
1278 omap_readw(OMAP_DMA_HW_ID));
1279 printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
1280 (omap_readw(OMAP_DMA_CAPS_0_U) << 16) |
1281 omap_readw(OMAP_DMA_CAPS_0_L),
1282 (omap_readw(OMAP_DMA_CAPS_1_U) << 16) |
1283 omap_readw(OMAP_DMA_CAPS_1_L),
1284 omap_readw(OMAP_DMA_CAPS_2), omap_readw(OMAP_DMA_CAPS_3),
1285 omap_readw(OMAP_DMA_CAPS_4));
1286 if (!enable_1510_mode) {
1289 /* Disable OMAP 3.0/3.1 compatibility mode. */
1290 w = omap_readw(OMAP_DMA_GSCR);
1292 omap_writew(w, OMAP_DMA_GSCR);
1293 dma_chan_count = 16;
1296 } else if (cpu_is_omap24xx()) {
1297 u8 revision = omap_readb(OMAP_DMA4_REVISION);
1298 printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
1299 revision >> 4, revision & 0xf);
1300 dma_chan_count = OMAP_LOGICAL_DMA_CH_COUNT;
1306 memset(&lcd_dma, 0, sizeof(lcd_dma));
1307 spin_lock_init(&lcd_dma.lock);
1308 spin_lock_init(&dma_chan_lock);
1309 memset(&dma_chan, 0, sizeof(dma_chan));
1311 for (ch = 0; ch < dma_chan_count; ch++) {
1313 dma_chan[ch].dev_id = -1;
1314 dma_chan[ch].next_lch = -1;
1316 if (ch >= 6 && enable_1510_mode)
1319 if (cpu_class_is_omap1()) {
1320 /* request_irq() doesn't like dev_id (ie. ch) being
1321 * zero, so we have to kludge around this. */
1322 r = request_irq(omap1_dma_irq[ch],
1323 omap1_dma_irq_handler, 0, "DMA",
1328 printk(KERN_ERR "unable to request IRQ %d "
1329 "for DMA (error %d)\n",
1330 omap1_dma_irq[ch], r);
1331 for (i = 0; i < ch; i++)
1332 free_irq(omap1_dma_irq[i],
1339 if (cpu_is_omap24xx())
1340 setup_irq(INT_24XX_SDMA_IRQ0, &omap24xx_dma_irq);
1342 /* FIXME: Update LCD DMA to work on 24xx */
1343 if (cpu_class_is_omap1()) {
1344 r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0,
1349 printk(KERN_ERR "unable to request IRQ for LCD DMA "
1351 for (i = 0; i < dma_chan_count; i++)
1352 free_irq(omap1_dma_irq[i], (void *) (i + 1));
1360 arch_initcall(omap_init_dma);
1362 EXPORT_SYMBOL(omap_get_dma_src_pos);
1363 EXPORT_SYMBOL(omap_get_dma_dst_pos);
1364 EXPORT_SYMBOL(omap_get_dma_src_addr_counter);
1365 EXPORT_SYMBOL(omap_clear_dma);
1366 EXPORT_SYMBOL(omap_set_dma_priority);
1367 EXPORT_SYMBOL(omap_request_dma);
1368 EXPORT_SYMBOL(omap_free_dma);
1369 EXPORT_SYMBOL(omap_start_dma);
1370 EXPORT_SYMBOL(omap_stop_dma);
1371 EXPORT_SYMBOL(omap_enable_dma_irq);
1372 EXPORT_SYMBOL(omap_disable_dma_irq);
1374 EXPORT_SYMBOL(omap_set_dma_transfer_params);
1375 EXPORT_SYMBOL(omap_set_dma_color_mode);
1377 EXPORT_SYMBOL(omap_set_dma_src_params);
1378 EXPORT_SYMBOL(omap_set_dma_src_index);
1379 EXPORT_SYMBOL(omap_set_dma_src_data_pack);
1380 EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
1382 EXPORT_SYMBOL(omap_set_dma_dest_params);
1383 EXPORT_SYMBOL(omap_set_dma_dest_index);
1384 EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
1385 EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
1387 EXPORT_SYMBOL(omap_set_dma_params);
1389 EXPORT_SYMBOL(omap_dma_link_lch);
1390 EXPORT_SYMBOL(omap_dma_unlink_lch);
1392 EXPORT_SYMBOL(omap_request_lcd_dma);
1393 EXPORT_SYMBOL(omap_free_lcd_dma);
1394 EXPORT_SYMBOL(omap_enable_lcd_dma);
1395 EXPORT_SYMBOL(omap_setup_lcd_dma);
1396 EXPORT_SYMBOL(omap_stop_lcd_dma);
1397 EXPORT_SYMBOL(omap_lcd_dma_ext_running);
1398 EXPORT_SYMBOL(omap_set_lcd_dma_b1);
1399 EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
1400 EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
1401 EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
1402 EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
1403 EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
1404 EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);