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>
28 #include <asm/system.h>
30 #include <asm/hardware.h>
33 #include <asm/mach-types.h>
35 #include <asm/arch/tc.h>
40 #define debug_printk(x) printk x
42 #define debug_printk(x)
45 #define OMAP_DMA_ACTIVE 0x01
46 #define OMAP_DMA_CCR_EN (1 << 7)
48 #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
50 static int enable_1510_mode = 0;
58 void (* callback)(int lch, u16 ch_status, void *data);
63 static int dma_chan_count;
65 static spinlock_t dma_chan_lock;
66 static struct omap_dma_lch dma_chan[OMAP_LOGICAL_DMA_CH_COUNT];
68 static const u8 omap1_dma_irq[OMAP_LOGICAL_DMA_CH_COUNT] = {
69 INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
70 INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
71 INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
72 INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
73 INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
76 #define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
79 #ifdef CONFIG_ARCH_OMAP15XX
80 /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
81 int omap_dma_in_1510_mode(void)
83 return enable_1510_mode;
86 #define omap_dma_in_1510_mode() 0
89 #ifdef CONFIG_ARCH_OMAP1
90 static inline int get_gdma_dev(int req)
92 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
93 int shift = ((req - 1) % 5) * 6;
95 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
98 static inline void set_gdma_dev(int req, int dev)
100 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
101 int shift = ((req - 1) % 5) * 6;
105 l &= ~(0x3f << shift);
106 l |= (dev - 1) << shift;
110 #define set_gdma_dev(req, dev) do {} while (0)
113 static void clear_lch_regs(int lch)
116 u32 lch_base = OMAP_DMA_BASE + lch * 0x40;
118 for (i = 0; i < 0x2c; i += 2)
119 omap_writew(0, lch_base + i);
122 void omap_set_dma_priority(int dst_port, int priority)
128 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
129 reg = OMAP_TC_OCPT1_PRIOR;
131 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
132 reg = OMAP_TC_OCPT2_PRIOR;
134 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
135 reg = OMAP_TC_EMIFF_PRIOR;
137 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
138 reg = OMAP_TC_EMIFS_PRIOR;
146 l |= (priority & 0xf) << 8;
150 void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
151 int frame_count, int sync_mode,
152 int dma_trigger, int src_or_dst_synch)
154 OMAP_DMA_CSDP_REG(lch) &= ~0x03;
155 OMAP_DMA_CSDP_REG(lch) |= data_type;
157 if (cpu_class_is_omap1()) {
158 OMAP_DMA_CCR_REG(lch) &= ~(1 << 5);
159 if (sync_mode == OMAP_DMA_SYNC_FRAME)
160 OMAP_DMA_CCR_REG(lch) |= 1 << 5;
162 OMAP1_DMA_CCR2_REG(lch) &= ~(1 << 2);
163 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
164 OMAP1_DMA_CCR2_REG(lch) |= 1 << 2;
167 if (cpu_is_omap24xx() && dma_trigger) {
168 u32 val = OMAP_DMA_CCR_REG(lch);
170 if (dma_trigger > 63)
172 if (dma_trigger > 31)
175 val |= (dma_trigger & 0x1f);
177 if (sync_mode & OMAP_DMA_SYNC_FRAME)
180 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
183 if (src_or_dst_synch)
184 val |= 1 << 24; /* source synch */
186 val &= ~(1 << 24); /* dest synch */
188 OMAP_DMA_CCR_REG(lch) = val;
191 OMAP_DMA_CEN_REG(lch) = elem_count;
192 OMAP_DMA_CFN_REG(lch) = frame_count;
195 void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
199 BUG_ON(omap_dma_in_1510_mode());
201 if (cpu_is_omap24xx()) {
206 w = OMAP1_DMA_CCR2_REG(lch) & ~0x03;
208 case OMAP_DMA_CONSTANT_FILL:
211 case OMAP_DMA_TRANSPARENT_COPY:
214 case OMAP_DMA_COLOR_DIS:
219 OMAP1_DMA_CCR2_REG(lch) = w;
221 w = OMAP1_DMA_LCH_CTRL_REG(lch) & ~0x0f;
222 /* Default is channel type 2D */
224 OMAP1_DMA_COLOR_L_REG(lch) = (u16)color;
225 OMAP1_DMA_COLOR_U_REG(lch) = (u16)(color >> 16);
226 w |= 1; /* Channel type G */
228 OMAP1_DMA_LCH_CTRL_REG(lch) = w;
231 /* Note that src_port is only for omap1 */
232 void omap_set_dma_src_params(int lch, int src_port, int src_amode,
233 unsigned long src_start,
234 int src_ei, int src_fi)
236 if (cpu_class_is_omap1()) {
237 OMAP_DMA_CSDP_REG(lch) &= ~(0x1f << 2);
238 OMAP_DMA_CSDP_REG(lch) |= src_port << 2;
241 OMAP_DMA_CCR_REG(lch) &= ~(0x03 << 12);
242 OMAP_DMA_CCR_REG(lch) |= src_amode << 12;
244 if (cpu_class_is_omap1()) {
245 OMAP1_DMA_CSSA_U_REG(lch) = src_start >> 16;
246 OMAP1_DMA_CSSA_L_REG(lch) = src_start;
249 if (cpu_is_omap24xx())
250 OMAP2_DMA_CSSA_REG(lch) = src_start;
252 OMAP_DMA_CSEI_REG(lch) = src_ei;
253 OMAP_DMA_CSFI_REG(lch) = src_fi;
256 void omap_set_dma_params(int lch, struct omap_dma_channel_params * params)
258 omap_set_dma_transfer_params(lch, params->data_type,
259 params->elem_count, params->frame_count,
260 params->sync_mode, params->trigger,
261 params->src_or_dst_synch);
262 omap_set_dma_src_params(lch, params->src_port,
263 params->src_amode, params->src_start,
264 params->src_ei, params->src_fi);
266 omap_set_dma_dest_params(lch, params->dst_port,
267 params->dst_amode, params->dst_start,
268 params->dst_ei, params->dst_fi);
271 void omap_set_dma_src_index(int lch, int eidx, int fidx)
273 if (cpu_is_omap24xx()) {
277 OMAP_DMA_CSEI_REG(lch) = eidx;
278 OMAP_DMA_CSFI_REG(lch) = fidx;
281 void omap_set_dma_src_data_pack(int lch, int enable)
283 OMAP_DMA_CSDP_REG(lch) &= ~(1 << 6);
285 OMAP_DMA_CSDP_REG(lch) |= (1 << 6);
288 void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
290 OMAP_DMA_CSDP_REG(lch) &= ~(0x03 << 7);
292 switch (burst_mode) {
293 case OMAP_DMA_DATA_BURST_DIS:
295 case OMAP_DMA_DATA_BURST_4:
296 OMAP_DMA_CSDP_REG(lch) |= (0x02 << 7);
298 case OMAP_DMA_DATA_BURST_8:
299 /* not supported by current hardware
308 /* Note that dest_port is only for OMAP1 */
309 void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
310 unsigned long dest_start,
311 int dst_ei, int dst_fi)
313 if (cpu_class_is_omap1()) {
314 OMAP_DMA_CSDP_REG(lch) &= ~(0x1f << 9);
315 OMAP_DMA_CSDP_REG(lch) |= dest_port << 9;
318 OMAP_DMA_CCR_REG(lch) &= ~(0x03 << 14);
319 OMAP_DMA_CCR_REG(lch) |= dest_amode << 14;
321 if (cpu_class_is_omap1()) {
322 OMAP1_DMA_CDSA_U_REG(lch) = dest_start >> 16;
323 OMAP1_DMA_CDSA_L_REG(lch) = dest_start;
326 if (cpu_is_omap24xx())
327 OMAP2_DMA_CDSA_REG(lch) = dest_start;
329 OMAP_DMA_CDEI_REG(lch) = dst_ei;
330 OMAP_DMA_CDFI_REG(lch) = dst_fi;
333 void omap_set_dma_dest_index(int lch, int eidx, int fidx)
335 if (cpu_is_omap24xx()) {
339 OMAP_DMA_CDEI_REG(lch) = eidx;
340 OMAP_DMA_CDFI_REG(lch) = fidx;
343 void omap_set_dma_dest_data_pack(int lch, int enable)
345 OMAP_DMA_CSDP_REG(lch) &= ~(1 << 13);
347 OMAP_DMA_CSDP_REG(lch) |= 1 << 13;
350 void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
352 OMAP_DMA_CSDP_REG(lch) &= ~(0x03 << 14);
354 switch (burst_mode) {
355 case OMAP_DMA_DATA_BURST_DIS:
357 case OMAP_DMA_DATA_BURST_4:
358 OMAP_DMA_CSDP_REG(lch) |= (0x02 << 14);
360 case OMAP_DMA_DATA_BURST_8:
361 OMAP_DMA_CSDP_REG(lch) |= (0x03 << 14);
364 printk(KERN_ERR "Invalid DMA burst mode\n");
370 static inline void omap_enable_channel_irq(int lch)
374 /* Read CSR to make sure it's cleared. */
375 status = OMAP_DMA_CSR_REG(lch);
377 /* Enable some nice interrupts. */
378 OMAP_DMA_CICR_REG(lch) = dma_chan[lch].enabled_irqs;
380 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
383 static void omap_disable_channel_irq(int lch)
385 if (cpu_is_omap24xx())
386 OMAP_DMA_CICR_REG(lch) = 0;
389 void omap_enable_dma_irq(int lch, u16 bits)
391 dma_chan[lch].enabled_irqs |= bits;
394 void omap_disable_dma_irq(int lch, u16 bits)
396 dma_chan[lch].enabled_irqs &= ~bits;
399 static inline void enable_lnk(int lch)
401 if (cpu_class_is_omap1())
402 OMAP_DMA_CLNK_CTRL_REG(lch) &= ~(1 << 14);
404 /* Set the ENABLE_LNK bits */
405 if (dma_chan[lch].next_lch != -1)
406 OMAP_DMA_CLNK_CTRL_REG(lch) =
407 dma_chan[lch].next_lch | (1 << 15);
410 static inline void disable_lnk(int lch)
412 /* Disable interrupts */
413 if (cpu_class_is_omap1()) {
414 OMAP_DMA_CICR_REG(lch) = 0;
415 /* Set the STOP_LNK bit */
416 OMAP_DMA_CLNK_CTRL_REG(lch) |= 1 << 14;
419 if (cpu_is_omap24xx()) {
420 omap_disable_channel_irq(lch);
421 /* Clear the ENABLE_LNK bit */
422 OMAP_DMA_CLNK_CTRL_REG(lch) &= ~(1 << 15);
425 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
428 static inline void omap2_enable_irq_lch(int lch)
432 if (!cpu_is_omap24xx())
435 val = omap_readl(OMAP_DMA4_IRQENABLE_L0);
437 omap_writel(val, OMAP_DMA4_IRQENABLE_L0);
440 int omap_request_dma(int dev_id, const char *dev_name,
441 void (* callback)(int lch, u16 ch_status, void *data),
442 void *data, int *dma_ch_out)
444 int ch, free_ch = -1;
446 struct omap_dma_lch *chan;
448 spin_lock_irqsave(&dma_chan_lock, flags);
449 for (ch = 0; ch < dma_chan_count; ch++) {
450 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
457 spin_unlock_irqrestore(&dma_chan_lock, flags);
460 chan = dma_chan + free_ch;
461 chan->dev_id = dev_id;
463 if (cpu_class_is_omap1())
464 clear_lch_regs(free_ch);
466 if (cpu_is_omap24xx())
467 omap_clear_dma(free_ch);
469 spin_unlock_irqrestore(&dma_chan_lock, flags);
471 chan->dev_name = dev_name;
472 chan->callback = callback;
474 chan->enabled_irqs = OMAP_DMA_TOUT_IRQ | OMAP_DMA_DROP_IRQ |
477 if (cpu_is_omap24xx())
478 chan->enabled_irqs |= OMAP2_DMA_TRANS_ERR_IRQ;
480 if (cpu_is_omap16xx()) {
481 /* If the sync device is set, configure it dynamically. */
483 set_gdma_dev(free_ch + 1, dev_id);
484 dev_id = free_ch + 1;
486 /* Disable the 1510 compatibility mode and set the sync device
488 OMAP_DMA_CCR_REG(free_ch) = dev_id | (1 << 10);
489 } else if (cpu_is_omap730() || cpu_is_omap15xx()) {
490 OMAP_DMA_CCR_REG(free_ch) = dev_id;
493 if (cpu_is_omap24xx()) {
494 omap2_enable_irq_lch(free_ch);
496 omap_enable_channel_irq(free_ch);
497 /* Clear the CSR register and IRQ status register */
498 OMAP_DMA_CSR_REG(free_ch) = 0x0;
499 omap_writel(~0x0, OMAP_DMA4_IRQSTATUS_L0);
502 *dma_ch_out = free_ch;
507 void omap_free_dma(int lch)
511 spin_lock_irqsave(&dma_chan_lock, flags);
512 if (dma_chan[lch].dev_id == -1) {
513 printk("omap_dma: trying to free nonallocated DMA channel %d\n",
515 spin_unlock_irqrestore(&dma_chan_lock, flags);
518 dma_chan[lch].dev_id = -1;
519 dma_chan[lch].next_lch = -1;
520 dma_chan[lch].callback = NULL;
521 spin_unlock_irqrestore(&dma_chan_lock, flags);
523 if (cpu_class_is_omap1()) {
524 /* Disable all DMA interrupts for the channel. */
525 OMAP_DMA_CICR_REG(lch) = 0;
526 /* Make sure the DMA transfer is stopped. */
527 OMAP_DMA_CCR_REG(lch) = 0;
530 if (cpu_is_omap24xx()) {
532 /* Disable interrupts */
533 val = omap_readl(OMAP_DMA4_IRQENABLE_L0);
535 omap_writel(val, OMAP_DMA4_IRQENABLE_L0);
537 /* Clear the CSR register and IRQ status register */
538 OMAP_DMA_CSR_REG(lch) = 0x0;
540 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
542 omap_writel(val, OMAP_DMA4_IRQSTATUS_L0);
544 /* Disable all DMA interrupts for the channel. */
545 OMAP_DMA_CICR_REG(lch) = 0;
547 /* Make sure the DMA transfer is stopped. */
548 OMAP_DMA_CCR_REG(lch) = 0;
554 * Clears any DMA state so the DMA engine is ready to restart with new buffers
555 * through omap_start_dma(). Any buffers in flight are discarded.
557 void omap_clear_dma(int lch)
561 local_irq_save(flags);
563 if (cpu_class_is_omap1()) {
565 OMAP_DMA_CCR_REG(lch) &= ~OMAP_DMA_CCR_EN;
567 /* Clear pending interrupts */
568 status = OMAP_DMA_CSR_REG(lch);
571 if (cpu_is_omap24xx()) {
573 u32 lch_base = OMAP24XX_DMA_BASE + lch * 0x60 + 0x80;
574 for (i = 0; i < 0x44; i += 4)
575 omap_writel(0, lch_base + i);
578 local_irq_restore(flags);
581 void omap_start_dma(int lch)
583 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
584 int next_lch, cur_lch;
585 char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
587 dma_chan_link_map[lch] = 1;
588 /* Set the link register of the first channel */
591 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
592 cur_lch = dma_chan[lch].next_lch;
594 next_lch = dma_chan[cur_lch].next_lch;
596 /* The loop case: we've been here already */
597 if (dma_chan_link_map[cur_lch])
599 /* Mark the current channel */
600 dma_chan_link_map[cur_lch] = 1;
603 omap_enable_channel_irq(cur_lch);
606 } while (next_lch != -1);
607 } else if (cpu_is_omap24xx()) {
608 /* Errata: Need to write lch even if not using chaining */
609 OMAP_DMA_CLNK_CTRL_REG(lch) = lch;
612 omap_enable_channel_irq(lch);
614 /* Errata: On ES2.0 BUFFERING disable must be set.
615 * This will always fail on ES1.0 */
616 if (cpu_is_omap24xx()) {
617 OMAP_DMA_CCR_REG(lch) |= OMAP_DMA_CCR_EN;
620 OMAP_DMA_CCR_REG(lch) |= OMAP_DMA_CCR_EN;
622 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
625 void omap_stop_dma(int lch)
627 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
628 int next_lch, cur_lch = lch;
629 char dma_chan_link_map[OMAP_LOGICAL_DMA_CH_COUNT];
631 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
633 /* The loop case: we've been here already */
634 if (dma_chan_link_map[cur_lch])
636 /* Mark the current channel */
637 dma_chan_link_map[cur_lch] = 1;
639 disable_lnk(cur_lch);
641 next_lch = dma_chan[cur_lch].next_lch;
643 } while (next_lch != -1);
648 /* Disable all interrupts on the channel */
649 if (cpu_class_is_omap1())
650 OMAP_DMA_CICR_REG(lch) = 0;
652 OMAP_DMA_CCR_REG(lch) &= ~OMAP_DMA_CCR_EN;
653 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
657 * Returns current physical source address for the given DMA channel.
658 * If the channel is running the caller must disable interrupts prior calling
659 * this function and process the returned value before re-enabling interrupt to
660 * prevent races with the interrupt handler. Note that in continuous mode there
661 * is a chance for CSSA_L register overflow inbetween the two reads resulting
662 * in incorrect return value.
664 dma_addr_t omap_get_dma_src_pos(int lch)
668 if (cpu_class_is_omap1())
669 offset = (dma_addr_t) (OMAP1_DMA_CSSA_L_REG(lch) |
670 (OMAP1_DMA_CSSA_U_REG(lch) << 16));
672 if (cpu_is_omap24xx())
673 offset = OMAP_DMA_CSAC_REG(lch);
679 * Returns current physical destination address for the given DMA channel.
680 * If the channel is running the caller must disable interrupts prior calling
681 * this function and process the returned value before re-enabling interrupt to
682 * prevent races with the interrupt handler. Note that in continuous mode there
683 * is a chance for CDSA_L register overflow inbetween the two reads resulting
684 * in incorrect return value.
686 dma_addr_t omap_get_dma_dst_pos(int lch)
690 if (cpu_class_is_omap1())
691 offset = (dma_addr_t) (OMAP1_DMA_CDSA_L_REG(lch) |
692 (OMAP1_DMA_CDSA_U_REG(lch) << 16));
694 if (cpu_is_omap24xx())
695 offset = OMAP2_DMA_CDSA_REG(lch);
701 * Returns current source transfer counting for the given DMA channel.
702 * Can be used to monitor the progress of a transfer inside a block.
703 * It must be called with disabled interrupts.
705 int omap_get_dma_src_addr_counter(int lch)
707 return (dma_addr_t) OMAP_DMA_CSAC_REG(lch);
710 int omap_dma_running(void)
714 /* Check if LCD DMA is running */
715 if (cpu_is_omap16xx())
716 if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
719 for (lch = 0; lch < dma_chan_count; lch++)
720 if (OMAP_DMA_CCR_REG(lch) & OMAP_DMA_CCR_EN)
727 * lch_queue DMA will start right after lch_head one is finished.
728 * For this DMA link to start, you still need to start (see omap_start_dma)
729 * the first one. That will fire up the entire queue.
731 void omap_dma_link_lch (int lch_head, int lch_queue)
733 if (omap_dma_in_1510_mode()) {
734 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
739 if ((dma_chan[lch_head].dev_id == -1) ||
740 (dma_chan[lch_queue].dev_id == -1)) {
741 printk(KERN_ERR "omap_dma: trying to link "
742 "non requested channels\n");
746 dma_chan[lch_head].next_lch = lch_queue;
750 * Once the DMA queue is stopped, we can destroy it.
752 void omap_dma_unlink_lch (int lch_head, int lch_queue)
754 if (omap_dma_in_1510_mode()) {
755 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
760 if (dma_chan[lch_head].next_lch != lch_queue ||
761 dma_chan[lch_head].next_lch == -1) {
762 printk(KERN_ERR "omap_dma: trying to unlink "
763 "non linked channels\n");
768 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
769 (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
770 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
771 "before unlinking\n");
775 dma_chan[lch_head].next_lch = -1;
778 /*----------------------------------------------------------------------------*/
780 #ifdef CONFIG_ARCH_OMAP1
782 static int omap1_dma_handle_ch(int ch)
786 if (enable_1510_mode && ch >= 6) {
787 csr = dma_chan[ch].saved_csr;
788 dma_chan[ch].saved_csr = 0;
790 csr = OMAP_DMA_CSR_REG(ch);
791 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
792 dma_chan[ch + 6].saved_csr = csr >> 7;
795 if ((csr & 0x3f) == 0)
797 if (unlikely(dma_chan[ch].dev_id == -1)) {
798 printk(KERN_WARNING "Spurious interrupt from DMA channel "
799 "%d (CSR %04x)\n", ch, csr);
802 if (unlikely(csr & OMAP_DMA_TOUT_IRQ))
803 printk(KERN_WARNING "DMA timeout with device %d\n",
804 dma_chan[ch].dev_id);
805 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
806 printk(KERN_WARNING "DMA synchronization event drop occurred "
807 "with device %d\n", dma_chan[ch].dev_id);
808 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
809 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
810 if (likely(dma_chan[ch].callback != NULL))
811 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
815 static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id,
816 struct pt_regs *regs)
818 int ch = ((int) dev_id) - 1;
824 handled_now += omap1_dma_handle_ch(ch);
825 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
826 handled_now += omap1_dma_handle_ch(ch + 6);
829 handled += handled_now;
832 return handled ? IRQ_HANDLED : IRQ_NONE;
836 #define omap1_dma_irq_handler NULL
839 #ifdef CONFIG_ARCH_OMAP2
841 static int omap2_dma_handle_ch(int ch)
843 u32 status = OMAP_DMA_CSR_REG(ch);
848 if (unlikely(dma_chan[ch].dev_id == -1))
850 /* REVISIT: According to 24xx TRM, there's no TOUT_IE */
851 if (unlikely(status & OMAP_DMA_TOUT_IRQ))
852 printk(KERN_INFO "DMA timeout with device %d\n",
853 dma_chan[ch].dev_id);
854 if (unlikely(status & OMAP_DMA_DROP_IRQ))
856 "DMA synchronization event drop occurred with device "
857 "%d\n", dma_chan[ch].dev_id);
859 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ))
860 printk(KERN_INFO "DMA transaction error with device %d\n",
861 dma_chan[ch].dev_id);
863 OMAP_DMA_CSR_REG(ch) = 0x20;
865 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
866 /* ch in this function is from 0-31 while in register it is 1-32 */
868 omap_writel(val, OMAP_DMA4_IRQSTATUS_L0);
870 if (likely(dma_chan[ch].callback != NULL))
871 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
876 /* STATUS register count is from 1-32 while our is 0-31 */
877 static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id,
878 struct pt_regs *regs)
883 val = omap_readl(OMAP_DMA4_IRQSTATUS_L0);
885 for (i = 1; i <= OMAP_LOGICAL_DMA_CH_COUNT; i++) {
886 int active = val & (1 << (i - 1));
888 omap2_dma_handle_ch(i - 1);
894 static struct irqaction omap24xx_dma_irq = {
896 .handler = omap2_dma_irq_handler,
897 .flags = SA_INTERRUPT
901 static struct irqaction omap24xx_dma_irq;
904 /*----------------------------------------------------------------------------*/
906 static struct lcd_dma_info {
909 void (* callback)(u16 status, void *data);
913 unsigned long addr, size;
914 int rotate, data_type, xres, yres;
923 void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
927 lcd_dma.data_type = data_type;
928 lcd_dma.xres = fb_xres;
929 lcd_dma.yres = fb_yres;
932 void omap_set_lcd_dma_src_port(int port)
934 lcd_dma.src_port = port;
937 void omap_set_lcd_dma_ext_controller(int external)
939 lcd_dma.ext_ctrl = external;
942 void omap_set_lcd_dma_single_transfer(int single)
944 lcd_dma.single_transfer = single;
948 void omap_set_lcd_dma_b1_rotation(int rotate)
950 if (omap_dma_in_1510_mode()) {
951 printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
955 lcd_dma.rotate = rotate;
958 void omap_set_lcd_dma_b1_mirror(int mirror)
960 if (omap_dma_in_1510_mode()) {
961 printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
964 lcd_dma.mirror = mirror;
967 void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
969 if (omap_dma_in_1510_mode()) {
970 printk(KERN_ERR "DMA virtual resulotion is not supported "
974 lcd_dma.vxres = vxres;
977 void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
979 if (omap_dma_in_1510_mode()) {
980 printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
983 lcd_dma.xscale = xscale;
984 lcd_dma.yscale = yscale;
987 static void set_b1_regs(void)
989 unsigned long top, bottom;
992 unsigned long en, fn;
995 unsigned int xscale, yscale;
997 switch (lcd_dma.data_type) {
998 case OMAP_DMA_DATA_TYPE_S8:
1001 case OMAP_DMA_DATA_TYPE_S16:
1004 case OMAP_DMA_DATA_TYPE_S32:
1012 vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
1013 xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
1014 yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
1015 BUG_ON(vxres < lcd_dma.xres);
1016 #define PIXADDR(x,y) (lcd_dma.addr + ((y) * vxres * yscale + (x) * xscale) * es)
1017 #define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
1018 switch (lcd_dma.rotate) {
1020 if (!lcd_dma.mirror) {
1021 top = PIXADDR(0, 0);
1022 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1023 /* 1510 DMA requires the bottom address to be 2 more
1024 * than the actual last memory access location. */
1025 if (omap_dma_in_1510_mode() &&
1026 lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
1028 ei = PIXSTEP(0, 0, 1, 0);
1029 fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
1031 top = PIXADDR(lcd_dma.xres - 1, 0);
1032 bottom = PIXADDR(0, lcd_dma.yres - 1);
1033 ei = PIXSTEP(1, 0, 0, 0);
1034 fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
1040 if (!lcd_dma.mirror) {
1041 top = PIXADDR(0, lcd_dma.yres - 1);
1042 bottom = PIXADDR(lcd_dma.xres - 1, 0);
1043 ei = PIXSTEP(0, 1, 0, 0);
1044 fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
1046 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1047 bottom = PIXADDR(0, 0);
1048 ei = PIXSTEP(0, 1, 0, 0);
1049 fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
1055 if (!lcd_dma.mirror) {
1056 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1057 bottom = PIXADDR(0, 0);
1058 ei = PIXSTEP(1, 0, 0, 0);
1059 fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
1061 top = PIXADDR(0, lcd_dma.yres - 1);
1062 bottom = PIXADDR(lcd_dma.xres - 1, 0);
1063 ei = PIXSTEP(0, 0, 1, 0);
1064 fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
1070 if (!lcd_dma.mirror) {
1071 top = PIXADDR(lcd_dma.xres - 1, 0);
1072 bottom = PIXADDR(0, lcd_dma.yres - 1);
1073 ei = PIXSTEP(0, 0, 0, 1);
1074 fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
1076 top = PIXADDR(0, 0);
1077 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
1078 ei = PIXSTEP(0, 0, 0, 1);
1079 fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
1086 return; /* Supress warning about uninitialized vars */
1089 if (omap_dma_in_1510_mode()) {
1090 u16 l = omap_readw(OMAP1510_DMA_LCD_CTRL);
1092 omap_writew (l, OMAP1510_DMA_LCD_CTRL);
1094 omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
1095 omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
1096 omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
1097 omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
1103 omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
1104 omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
1105 omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
1106 omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
1108 omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
1109 omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
1111 w = omap_readw(OMAP1610_DMA_LCD_CSDP);
1113 w |= lcd_dma.data_type;
1114 omap_writew(w, OMAP1610_DMA_LCD_CSDP);
1116 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1117 /* Always set the source port as SDRAM for now*/
1119 if (lcd_dma.callback != NULL)
1120 w |= 1 << 1; /* Block interrupt enable */
1123 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1125 if (!(lcd_dma.rotate || lcd_dma.mirror ||
1126 lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
1129 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1130 /* Set the double-indexed addressing mode */
1132 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1134 omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
1135 omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
1136 omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
1139 static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id,
1140 struct pt_regs *regs)
1144 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1145 if (unlikely(!(w & (1 << 3)))) {
1146 printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
1151 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1153 if (lcd_dma.callback != NULL)
1154 lcd_dma.callback(w, lcd_dma.cb_data);
1159 int omap_request_lcd_dma(void (* callback)(u16 status, void *data),
1162 spin_lock_irq(&lcd_dma.lock);
1163 if (lcd_dma.reserved) {
1164 spin_unlock_irq(&lcd_dma.lock);
1165 printk(KERN_ERR "LCD DMA channel already reserved\n");
1169 lcd_dma.reserved = 1;
1170 spin_unlock_irq(&lcd_dma.lock);
1171 lcd_dma.callback = callback;
1172 lcd_dma.cb_data = data;
1174 lcd_dma.single_transfer = 0;
1180 lcd_dma.ext_ctrl = 0;
1181 lcd_dma.src_port = 0;
1186 void omap_free_lcd_dma(void)
1188 spin_lock(&lcd_dma.lock);
1189 if (!lcd_dma.reserved) {
1190 spin_unlock(&lcd_dma.lock);
1191 printk(KERN_ERR "LCD DMA is not reserved\n");
1195 if (!enable_1510_mode)
1196 omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1,
1197 OMAP1610_DMA_LCD_CCR);
1198 lcd_dma.reserved = 0;
1199 spin_unlock(&lcd_dma.lock);
1202 void omap_enable_lcd_dma(void)
1206 /* Set the Enable bit only if an external controller is
1207 * connected. Otherwise the OMAP internal controller will
1208 * start the transfer when it gets enabled.
1210 if (enable_1510_mode || !lcd_dma.ext_ctrl)
1213 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1215 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1219 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1221 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1224 void omap_setup_lcd_dma(void)
1226 BUG_ON(lcd_dma.active);
1227 if (!enable_1510_mode) {
1228 /* Set some reasonable defaults */
1229 omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
1230 omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
1231 omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
1234 if (!enable_1510_mode) {
1237 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1238 /* If DMA was already active set the end_prog bit to have
1239 * the programmed register set loaded into the active
1242 w |= 1 << 11; /* End_prog */
1243 if (!lcd_dma.single_transfer)
1244 w |= (3 << 8); /* Auto_init, repeat */
1245 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1249 void omap_stop_lcd_dma(void)
1254 if (enable_1510_mode || !lcd_dma.ext_ctrl)
1257 w = omap_readw(OMAP1610_DMA_LCD_CCR);
1259 omap_writew(w, OMAP1610_DMA_LCD_CCR);
1261 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
1263 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
1266 int omap_lcd_dma_ext_running(void)
1268 return lcd_dma.ext_ctrl && lcd_dma.active;
1271 /*----------------------------------------------------------------------------*/
1273 static int __init omap_init_dma(void)
1277 if (cpu_is_omap15xx()) {
1278 printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
1280 enable_1510_mode = 1;
1281 } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
1282 printk(KERN_INFO "OMAP DMA hardware version %d\n",
1283 omap_readw(OMAP_DMA_HW_ID));
1284 printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
1285 (omap_readw(OMAP_DMA_CAPS_0_U) << 16) |
1286 omap_readw(OMAP_DMA_CAPS_0_L),
1287 (omap_readw(OMAP_DMA_CAPS_1_U) << 16) |
1288 omap_readw(OMAP_DMA_CAPS_1_L),
1289 omap_readw(OMAP_DMA_CAPS_2), omap_readw(OMAP_DMA_CAPS_3),
1290 omap_readw(OMAP_DMA_CAPS_4));
1291 if (!enable_1510_mode) {
1294 /* Disable OMAP 3.0/3.1 compatibility mode. */
1295 w = omap_readw(OMAP_DMA_GSCR);
1297 omap_writew(w, OMAP_DMA_GSCR);
1298 dma_chan_count = 16;
1301 } else if (cpu_is_omap24xx()) {
1302 u8 revision = omap_readb(OMAP_DMA4_REVISION);
1303 printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
1304 revision >> 4, revision & 0xf);
1305 dma_chan_count = OMAP_LOGICAL_DMA_CH_COUNT;
1311 memset(&lcd_dma, 0, sizeof(lcd_dma));
1312 spin_lock_init(&lcd_dma.lock);
1313 spin_lock_init(&dma_chan_lock);
1314 memset(&dma_chan, 0, sizeof(dma_chan));
1316 for (ch = 0; ch < dma_chan_count; ch++) {
1318 dma_chan[ch].dev_id = -1;
1319 dma_chan[ch].next_lch = -1;
1321 if (ch >= 6 && enable_1510_mode)
1324 if (cpu_class_is_omap1()) {
1325 /* request_irq() doesn't like dev_id (ie. ch) being
1326 * zero, so we have to kludge around this. */
1327 r = request_irq(omap1_dma_irq[ch],
1328 omap1_dma_irq_handler, 0, "DMA",
1333 printk(KERN_ERR "unable to request IRQ %d "
1334 "for DMA (error %d)\n",
1335 omap1_dma_irq[ch], r);
1336 for (i = 0; i < ch; i++)
1337 free_irq(omap1_dma_irq[i],
1344 if (cpu_is_omap24xx())
1345 setup_irq(INT_24XX_SDMA_IRQ0, &omap24xx_dma_irq);
1347 /* FIXME: Update LCD DMA to work on 24xx */
1348 if (cpu_class_is_omap1()) {
1349 r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0,
1354 printk(KERN_ERR "unable to request IRQ for LCD DMA "
1356 for (i = 0; i < dma_chan_count; i++)
1357 free_irq(omap1_dma_irq[i], (void *) (i + 1));
1365 arch_initcall(omap_init_dma);
1367 EXPORT_SYMBOL(omap_get_dma_src_pos);
1368 EXPORT_SYMBOL(omap_get_dma_dst_pos);
1369 EXPORT_SYMBOL(omap_get_dma_src_addr_counter);
1370 EXPORT_SYMBOL(omap_clear_dma);
1371 EXPORT_SYMBOL(omap_set_dma_priority);
1372 EXPORT_SYMBOL(omap_request_dma);
1373 EXPORT_SYMBOL(omap_free_dma);
1374 EXPORT_SYMBOL(omap_start_dma);
1375 EXPORT_SYMBOL(omap_stop_dma);
1376 EXPORT_SYMBOL(omap_enable_dma_irq);
1377 EXPORT_SYMBOL(omap_disable_dma_irq);
1379 EXPORT_SYMBOL(omap_set_dma_transfer_params);
1380 EXPORT_SYMBOL(omap_set_dma_color_mode);
1382 EXPORT_SYMBOL(omap_set_dma_src_params);
1383 EXPORT_SYMBOL(omap_set_dma_src_index);
1384 EXPORT_SYMBOL(omap_set_dma_src_data_pack);
1385 EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
1387 EXPORT_SYMBOL(omap_set_dma_dest_params);
1388 EXPORT_SYMBOL(omap_set_dma_dest_index);
1389 EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
1390 EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
1392 EXPORT_SYMBOL(omap_set_dma_params);
1394 EXPORT_SYMBOL(omap_dma_link_lch);
1395 EXPORT_SYMBOL(omap_dma_unlink_lch);
1397 EXPORT_SYMBOL(omap_request_lcd_dma);
1398 EXPORT_SYMBOL(omap_free_lcd_dma);
1399 EXPORT_SYMBOL(omap_enable_lcd_dma);
1400 EXPORT_SYMBOL(omap_setup_lcd_dma);
1401 EXPORT_SYMBOL(omap_stop_lcd_dma);
1402 EXPORT_SYMBOL(omap_lcd_dma_ext_running);
1403 EXPORT_SYMBOL(omap_set_lcd_dma_b1);
1404 EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
1405 EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
1406 EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
1407 EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
1408 EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
1409 EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);