2 * linux/arch/arm/plat-omap/dma.c
4 * Copyright (C) 2003 - 2008 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/3 support Copyright (C) 2004-2007 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>
30 #include <asm/system.h>
31 #include <asm/hardware.h>
34 #include <asm/arch/tc.h>
38 #ifndef CONFIG_ARCH_OMAP1
39 enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
40 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
43 enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
46 #define OMAP_DMA_ACTIVE 0x01
47 #define OMAP_DMA_CCR_EN (1 << 7)
48 #define OMAP2_DMA_CSR_CLEAR_MASK 0xffe
50 #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
52 static int enable_1510_mode;
60 void (*callback)(int lch, u16 ch_status, void *data);
63 #ifndef CONFIG_ARCH_OMAP1
64 /* required for Dynamic chaining */
75 struct dma_link_info {
77 int no_of_lchs_linked;
88 static struct dma_link_info *dma_linked_lch;
90 #ifndef CONFIG_ARCH_OMAP1
92 /* Chain handling macros */
93 #define OMAP_DMA_CHAIN_QINIT(chain_id) \
95 dma_linked_lch[chain_id].q_head = \
96 dma_linked_lch[chain_id].q_tail = \
97 dma_linked_lch[chain_id].q_count = 0; \
99 #define OMAP_DMA_CHAIN_QFULL(chain_id) \
100 (dma_linked_lch[chain_id].no_of_lchs_linked == \
101 dma_linked_lch[chain_id].q_count)
102 #define OMAP_DMA_CHAIN_QLAST(chain_id) \
104 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
105 dma_linked_lch[chain_id].q_count) \
107 #define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
108 (0 == dma_linked_lch[chain_id].q_count)
109 #define __OMAP_DMA_CHAIN_INCQ(end) \
110 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
111 #define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
113 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
114 dma_linked_lch[chain_id].q_count--; \
117 #define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
119 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
120 dma_linked_lch[chain_id].q_count++; \
124 static int dma_lch_count;
125 static int dma_chan_count;
127 static spinlock_t dma_chan_lock;
128 static struct omap_dma_lch *dma_chan;
129 void __iomem *omap_dma_base;
131 static const u8 omap1_dma_irq[OMAP1_LOGICAL_DMA_CH_COUNT] = {
132 INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
133 INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
134 INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
135 INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
136 INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
139 static inline void disable_lnk(int lch);
140 static void omap_disable_channel_irq(int lch);
141 static inline void omap_enable_channel_irq(int lch);
143 #define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
146 #define dma_read(reg) \
149 if (cpu_class_is_omap1()) \
150 __val = __raw_readw(omap_dma_base + OMAP1_DMA_##reg); \
152 __val = __raw_readl(omap_dma_base + OMAP_DMA4_##reg); \
156 #define dma_write(val, reg) \
158 if (cpu_class_is_omap1()) \
159 __raw_writew((u16)(val), omap_dma_base + OMAP1_DMA_##reg); \
161 __raw_writel((val), omap_dma_base + OMAP_DMA4_##reg); \
164 #ifdef CONFIG_ARCH_OMAP15XX
165 /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
166 int omap_dma_in_1510_mode(void)
168 return enable_1510_mode;
171 #define omap_dma_in_1510_mode() 0
174 #ifdef CONFIG_ARCH_OMAP1
175 static inline int get_gdma_dev(int req)
177 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
178 int shift = ((req - 1) % 5) * 6;
180 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
183 static inline void set_gdma_dev(int req, int dev)
185 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
186 int shift = ((req - 1) % 5) * 6;
190 l &= ~(0x3f << shift);
191 l |= (dev - 1) << shift;
195 #define set_gdma_dev(req, dev) do {} while (0)
199 static void clear_lch_regs(int lch)
202 void __iomem *lch_base = omap_dma_base + OMAP1_DMA_CH_BASE(lch);
204 for (i = 0; i < 0x2c; i += 2)
205 __raw_writew(0, lch_base + i);
208 void omap_set_dma_priority(int lch, int dst_port, int priority)
213 if (cpu_class_is_omap1()) {
215 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
216 reg = OMAP_TC_OCPT1_PRIOR;
218 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
219 reg = OMAP_TC_OCPT2_PRIOR;
221 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
222 reg = OMAP_TC_EMIFF_PRIOR;
224 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
225 reg = OMAP_TC_EMIFS_PRIOR;
233 l |= (priority & 0xf) << 8;
237 if (cpu_class_is_omap2()) {
240 ccr = dma_read(CCR(lch));
245 dma_write(ccr, CCR(lch));
248 EXPORT_SYMBOL(omap_set_dma_priority);
250 void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
251 int frame_count, int sync_mode,
252 int dma_trigger, int src_or_dst_synch)
256 l = dma_read(CSDP(lch));
259 dma_write(l, CSDP(lch));
261 if (cpu_class_is_omap1()) {
264 ccr = dma_read(CCR(lch));
266 if (sync_mode == OMAP_DMA_SYNC_FRAME)
268 dma_write(ccr, CCR(lch));
270 ccr = dma_read(CCR2(lch));
272 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
274 dma_write(ccr, CCR2(lch));
277 if (cpu_class_is_omap2() && dma_trigger) {
280 val = dma_read(CCR(lch));
282 if (dma_trigger > 63)
284 if (dma_trigger > 31)
288 val |= (dma_trigger & 0x1f);
290 if (sync_mode & OMAP_DMA_SYNC_FRAME)
295 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
300 if (src_or_dst_synch)
301 val |= 1 << 24; /* source synch */
303 val &= ~(1 << 24); /* dest synch */
305 dma_write(val, CCR(lch));
308 dma_write(elem_count, CEN(lch));
309 dma_write(frame_count, CFN(lch));
311 EXPORT_SYMBOL(omap_set_dma_transfer_params);
313 void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
317 BUG_ON(omap_dma_in_1510_mode());
319 if (cpu_class_is_omap2()) {
324 w = dma_read(CCR2(lch));
328 case OMAP_DMA_CONSTANT_FILL:
331 case OMAP_DMA_TRANSPARENT_COPY:
334 case OMAP_DMA_COLOR_DIS:
339 dma_write(w, CCR2(lch));
341 w = dma_read(LCH_CTRL(lch));
343 /* Default is channel type 2D */
345 dma_write((u16)color, COLOR_L(lch));
346 dma_write((u16)(color >> 16), COLOR_U(lch));
347 w |= 1; /* Channel type G */
349 dma_write(w, LCH_CTRL(lch));
351 EXPORT_SYMBOL(omap_set_dma_color_mode);
353 void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
355 if (cpu_class_is_omap2()) {
358 csdp = dma_read(CSDP(lch));
359 csdp &= ~(0x3 << 16);
360 csdp |= (mode << 16);
361 dma_write(csdp, CSDP(lch));
364 EXPORT_SYMBOL(omap_set_dma_write_mode);
366 /* Note that src_port is only for omap1 */
367 void omap_set_dma_src_params(int lch, int src_port, int src_amode,
368 unsigned long src_start,
369 int src_ei, int src_fi)
373 if (cpu_class_is_omap1()) {
376 w = dma_read(CSDP(lch));
379 dma_write(w, CSDP(lch));
382 l = dma_read(CCR(lch));
384 l |= src_amode << 12;
385 dma_write(l, CCR(lch));
387 if (cpu_class_is_omap1()) {
388 dma_write(src_start >> 16, CSSA_U(lch));
389 dma_write((u16)src_start, CSSA_L(lch));
392 if (cpu_class_is_omap2())
393 dma_write(src_start, CSSA(lch));
395 dma_write(src_ei, CSEI(lch));
396 dma_write(src_fi, CSFI(lch));
398 EXPORT_SYMBOL(omap_set_dma_src_params);
400 void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
402 omap_set_dma_transfer_params(lch, params->data_type,
403 params->elem_count, params->frame_count,
404 params->sync_mode, params->trigger,
405 params->src_or_dst_synch);
406 omap_set_dma_src_params(lch, params->src_port,
407 params->src_amode, params->src_start,
408 params->src_ei, params->src_fi);
410 omap_set_dma_dest_params(lch, params->dst_port,
411 params->dst_amode, params->dst_start,
412 params->dst_ei, params->dst_fi);
413 if (params->read_prio || params->write_prio)
414 omap_dma_set_prio_lch(lch, params->read_prio,
417 EXPORT_SYMBOL(omap_set_dma_params);
419 void omap_set_dma_src_index(int lch, int eidx, int fidx)
421 if (cpu_class_is_omap2())
424 dma_write(eidx, CSEI(lch));
425 dma_write(fidx, CSFI(lch));
427 EXPORT_SYMBOL(omap_set_dma_src_index);
429 void omap_set_dma_src_data_pack(int lch, int enable)
433 l = dma_read(CSDP(lch));
437 dma_write(l, CSDP(lch));
439 EXPORT_SYMBOL(omap_set_dma_src_data_pack);
441 void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
443 unsigned int burst = 0;
446 l = dma_read(CSDP(lch));
449 switch (burst_mode) {
450 case OMAP_DMA_DATA_BURST_DIS:
452 case OMAP_DMA_DATA_BURST_4:
453 if (cpu_class_is_omap2())
458 case OMAP_DMA_DATA_BURST_8:
459 if (cpu_class_is_omap2()) {
463 /* not supported by current hardware on OMAP1
467 case OMAP_DMA_DATA_BURST_16:
468 if (cpu_class_is_omap2()) {
472 /* OMAP1 don't support burst 16
480 dma_write(l, CSDP(lch));
482 EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
484 /* Note that dest_port is only for OMAP1 */
485 void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
486 unsigned long dest_start,
487 int dst_ei, int dst_fi)
491 if (cpu_class_is_omap1()) {
492 l = dma_read(CSDP(lch));
495 dma_write(l, CSDP(lch));
498 l = dma_read(CCR(lch));
500 l |= dest_amode << 14;
501 dma_write(l, CCR(lch));
503 if (cpu_class_is_omap1()) {
504 dma_write(dest_start >> 16, CDSA_U(lch));
505 dma_write(dest_start, CDSA_L(lch));
508 if (cpu_class_is_omap2())
509 dma_write(dest_start, CDSA(lch));
511 dma_write(dst_ei, CDEI(lch));
512 dma_write(dst_fi, CDFI(lch));
514 EXPORT_SYMBOL(omap_set_dma_dest_params);
516 void omap_set_dma_dest_index(int lch, int eidx, int fidx)
518 if (cpu_class_is_omap2())
521 dma_write(eidx, CDEI(lch));
522 dma_write(fidx, CDFI(lch));
524 EXPORT_SYMBOL(omap_set_dma_dest_index);
526 void omap_set_dma_dest_data_pack(int lch, int enable)
530 l = dma_read(CSDP(lch));
534 dma_write(l, CSDP(lch));
536 EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
538 void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
540 unsigned int burst = 0;
543 l = dma_read(CSDP(lch));
546 switch (burst_mode) {
547 case OMAP_DMA_DATA_BURST_DIS:
549 case OMAP_DMA_DATA_BURST_4:
550 if (cpu_class_is_omap2())
555 case OMAP_DMA_DATA_BURST_8:
556 if (cpu_class_is_omap2())
561 case OMAP_DMA_DATA_BURST_16:
562 if (cpu_class_is_omap2()) {
566 /* OMAP1 don't support burst 16
570 printk(KERN_ERR "Invalid DMA burst mode\n");
575 dma_write(l, CSDP(lch));
577 EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
579 static inline void omap_enable_channel_irq(int lch)
584 if (cpu_class_is_omap1())
585 status = dma_read(CSR(lch));
586 else if (cpu_class_is_omap2())
587 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
589 /* Enable some nice interrupts. */
590 dma_write(dma_chan[lch].enabled_irqs, CICR(lch));
593 static void omap_disable_channel_irq(int lch)
595 if (cpu_class_is_omap2())
596 dma_write(0, CICR(lch));
599 void omap_enable_dma_irq(int lch, u16 bits)
601 dma_chan[lch].enabled_irqs |= bits;
603 EXPORT_SYMBOL(omap_enable_dma_irq);
605 void omap_disable_dma_irq(int lch, u16 bits)
607 dma_chan[lch].enabled_irqs &= ~bits;
609 EXPORT_SYMBOL(omap_disable_dma_irq);
611 static inline void enable_lnk(int lch)
615 l = dma_read(CLNK_CTRL(lch));
617 if (cpu_class_is_omap1())
620 /* Set the ENABLE_LNK bits */
621 if (dma_chan[lch].next_lch != -1)
622 l = dma_chan[lch].next_lch | (1 << 15);
624 #ifndef CONFIG_ARCH_OMAP1
625 if (cpu_class_is_omap2())
626 if (dma_chan[lch].next_linked_ch != -1)
627 l = dma_chan[lch].next_linked_ch | (1 << 15);
630 dma_write(l, CLNK_CTRL(lch));
633 static inline void disable_lnk(int lch)
637 l = dma_read(CLNK_CTRL(lch));
639 /* Disable interrupts */
640 if (cpu_class_is_omap1()) {
641 dma_write(0, CICR(lch));
642 /* Set the STOP_LNK bit */
646 if (cpu_class_is_omap2()) {
647 omap_disable_channel_irq(lch);
648 /* Clear the ENABLE_LNK bit */
652 dma_write(l, CLNK_CTRL(lch));
653 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
656 static inline void omap2_enable_irq_lch(int lch)
660 if (!cpu_class_is_omap2())
663 val = dma_read(IRQENABLE_L0);
665 dma_write(val, IRQENABLE_L0);
668 int omap_request_dma(int dev_id, const char *dev_name,
669 void (*callback)(int lch, u16 ch_status, void *data),
670 void *data, int *dma_ch_out)
672 int ch, free_ch = -1;
674 struct omap_dma_lch *chan;
676 spin_lock_irqsave(&dma_chan_lock, flags);
677 for (ch = 0; ch < dma_chan_count; ch++) {
678 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
685 spin_unlock_irqrestore(&dma_chan_lock, flags);
688 chan = dma_chan + free_ch;
689 chan->dev_id = dev_id;
691 if (cpu_class_is_omap1())
692 clear_lch_regs(free_ch);
694 if (cpu_class_is_omap2())
695 omap_clear_dma(free_ch);
697 spin_unlock_irqrestore(&dma_chan_lock, flags);
699 chan->dev_name = dev_name;
700 chan->callback = callback;
703 #ifndef CONFIG_ARCH_OMAP1
704 if (cpu_class_is_omap2()) {
706 chan->next_linked_ch = -1;
710 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
712 if (cpu_class_is_omap1())
713 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
714 else if (cpu_class_is_omap2())
715 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
716 OMAP2_DMA_TRANS_ERR_IRQ;
718 if (cpu_is_omap16xx()) {
719 /* If the sync device is set, configure it dynamically. */
721 set_gdma_dev(free_ch + 1, dev_id);
722 dev_id = free_ch + 1;
725 * Disable the 1510 compatibility mode and set the sync device
728 dma_write(dev_id | (1 << 10), CCR(free_ch));
729 } else if (cpu_is_omap730() || cpu_is_omap15xx()) {
730 dma_write(dev_id, CCR(free_ch));
733 if (cpu_class_is_omap2()) {
734 omap2_enable_irq_lch(free_ch);
735 omap_enable_channel_irq(free_ch);
736 /* Clear the CSR register and IRQ status register */
737 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(free_ch));
738 dma_write(1 << free_ch, IRQSTATUS_L0);
741 *dma_ch_out = free_ch;
745 EXPORT_SYMBOL(omap_request_dma);
747 void omap_free_dma(int lch)
751 spin_lock_irqsave(&dma_chan_lock, flags);
752 if (dma_chan[lch].dev_id == -1) {
753 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
755 spin_unlock_irqrestore(&dma_chan_lock, flags);
759 dma_chan[lch].dev_id = -1;
760 dma_chan[lch].next_lch = -1;
761 dma_chan[lch].callback = NULL;
762 spin_unlock_irqrestore(&dma_chan_lock, flags);
764 if (cpu_class_is_omap1()) {
765 /* Disable all DMA interrupts for the channel. */
766 dma_write(0, CICR(lch));
767 /* Make sure the DMA transfer is stopped. */
768 dma_write(0, CCR(lch));
771 if (cpu_class_is_omap2()) {
773 /* Disable interrupts */
774 val = dma_read(IRQENABLE_L0);
776 dma_write(val, IRQENABLE_L0);
778 /* Clear the CSR register and IRQ status register */
779 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
780 dma_write(1 << lch, IRQSTATUS_L0);
782 /* Disable all DMA interrupts for the channel. */
783 dma_write(0, CICR(lch));
785 /* Make sure the DMA transfer is stopped. */
786 dma_write(0, CCR(lch));
790 EXPORT_SYMBOL(omap_free_dma);
793 * @brief omap_dma_set_global_params : Set global priority settings for dma
796 * @param max_fifo_depth
797 * @param tparams - Number of thereads to reserve : DMA_THREAD_RESERVE_NORM
798 * DMA_THREAD_RESERVE_ONET
799 * DMA_THREAD_RESERVE_TWOT
800 * DMA_THREAD_RESERVE_THREET
803 omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
807 if (!cpu_class_is_omap2()) {
808 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
815 reg = (arb_rate & 0xff) << 16;
816 reg |= (0xff & max_fifo_depth);
820 EXPORT_SYMBOL(omap_dma_set_global_params);
823 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
826 * @param read_prio - Read priority
827 * @param write_prio - Write priority
828 * Both of the above can be set with one of the following values :
829 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
832 omap_dma_set_prio_lch(int lch, unsigned char read_prio,
833 unsigned char write_prio)
837 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
838 printk(KERN_ERR "Invalid channel id\n");
841 l = dma_read(CCR(lch));
842 l &= ~((1 << 6) | (1 << 26));
843 if (cpu_is_omap2430() || cpu_is_omap34xx())
844 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
846 l |= ((read_prio & 0x1) << 6);
848 dma_write(l, CCR(lch));
852 EXPORT_SYMBOL(omap_dma_set_prio_lch);
855 * Clears any DMA state so the DMA engine is ready to restart with new buffers
856 * through omap_start_dma(). Any buffers in flight are discarded.
858 void omap_clear_dma(int lch)
862 local_irq_save(flags);
864 if (cpu_class_is_omap1()) {
867 l = dma_read(CCR(lch));
868 l &= ~OMAP_DMA_CCR_EN;
869 dma_write(l, CCR(lch));
871 /* Clear pending interrupts */
872 l = dma_read(CSR(lch));
875 if (cpu_class_is_omap2()) {
877 void __iomem *lch_base = omap_dma_base + OMAP_DMA4_CH_BASE(lch);
878 for (i = 0; i < 0x44; i += 4)
879 __raw_writel(0, lch_base + i);
882 local_irq_restore(flags);
884 EXPORT_SYMBOL(omap_clear_dma);
886 void omap_start_dma(int lch)
890 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
891 int next_lch, cur_lch;
892 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
894 dma_chan_link_map[lch] = 1;
895 /* Set the link register of the first channel */
898 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
899 cur_lch = dma_chan[lch].next_lch;
901 next_lch = dma_chan[cur_lch].next_lch;
903 /* The loop case: we've been here already */
904 if (dma_chan_link_map[cur_lch])
906 /* Mark the current channel */
907 dma_chan_link_map[cur_lch] = 1;
910 omap_enable_channel_irq(cur_lch);
913 } while (next_lch != -1);
914 } else if (cpu_class_is_omap2()) {
915 /* Errata: Need to write lch even if not using chaining */
916 dma_write(lch, CLNK_CTRL(lch));
919 omap_enable_channel_irq(lch);
921 l = dma_read(CCR(lch));
924 * Errata: On ES2.0 BUFFERING disable must be set.
925 * This will always fail on ES1.0
927 if (cpu_is_omap24xx())
928 l |= OMAP_DMA_CCR_EN;
930 l |= OMAP_DMA_CCR_EN;
931 dma_write(l, CCR(lch));
933 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
935 EXPORT_SYMBOL(omap_start_dma);
937 void omap_stop_dma(int lch)
941 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
942 int next_lch, cur_lch = lch;
943 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
945 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
947 /* The loop case: we've been here already */
948 if (dma_chan_link_map[cur_lch])
950 /* Mark the current channel */
951 dma_chan_link_map[cur_lch] = 1;
953 disable_lnk(cur_lch);
955 next_lch = dma_chan[cur_lch].next_lch;
957 } while (next_lch != -1);
962 /* Disable all interrupts on the channel */
963 if (cpu_class_is_omap1())
964 dma_write(0, CICR(lch));
966 l = dma_read(CCR(lch));
967 l &= ~OMAP_DMA_CCR_EN;
968 dma_write(l, CCR(lch));
970 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
972 EXPORT_SYMBOL(omap_stop_dma);
975 * Allows changing the DMA callback function or data. This may be needed if
976 * the driver shares a single DMA channel for multiple dma triggers.
978 int omap_set_dma_callback(int lch,
979 void (*callback)(int lch, u16 ch_status, void *data),
987 spin_lock_irqsave(&dma_chan_lock, flags);
988 if (dma_chan[lch].dev_id == -1) {
989 printk(KERN_ERR "DMA callback for not set for free channel\n");
990 spin_unlock_irqrestore(&dma_chan_lock, flags);
993 dma_chan[lch].callback = callback;
994 dma_chan[lch].data = data;
995 spin_unlock_irqrestore(&dma_chan_lock, flags);
999 EXPORT_SYMBOL(omap_set_dma_callback);
1002 * Returns current physical source address for the given DMA channel.
1003 * If the channel is running the caller must disable interrupts prior calling
1004 * this function and process the returned value before re-enabling interrupt to
1005 * prevent races with the interrupt handler. Note that in continuous mode there
1006 * is a chance for CSSA_L register overflow inbetween the two reads resulting
1007 * in incorrect return value.
1009 dma_addr_t omap_get_dma_src_pos(int lch)
1011 dma_addr_t offset = 0;
1013 offset = dma_read(CSAC(lch));
1016 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1017 * read before the DMA controller finished disabling the channel.
1020 offset = dma_read(CSAC(lch));
1022 if (cpu_class_is_omap1())
1023 offset |= (dma_read(CSSA_U(lch)) << 16);
1027 EXPORT_SYMBOL(omap_get_dma_src_pos);
1030 * Returns current physical destination address for the given DMA channel.
1031 * If the channel is running the caller must disable interrupts prior calling
1032 * this function and process the returned value before re-enabling interrupt to
1033 * prevent races with the interrupt handler. Note that in continuous mode there
1034 * is a chance for CDSA_L register overflow inbetween the two reads resulting
1035 * in incorrect return value.
1037 dma_addr_t omap_get_dma_dst_pos(int lch)
1039 dma_addr_t offset = 0;
1041 offset = dma_read(CDAC(lch));
1044 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1045 * read before the DMA controller finished disabling the channel.
1048 offset = dma_read(CDAC(lch));
1050 if (cpu_class_is_omap1())
1051 offset |= (dma_read(CDSA_U(lch)) << 16);
1055 EXPORT_SYMBOL(omap_get_dma_dst_pos);
1057 int omap_get_dma_active_status(int lch)
1059 return (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN) != 0;
1061 EXPORT_SYMBOL(omap_get_dma_active_status);
1063 int omap_dma_running(void)
1067 /* Check if LCD DMA is running */
1068 if (cpu_is_omap16xx())
1069 if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
1072 for (lch = 0; lch < dma_chan_count; lch++)
1073 if (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN)
1080 * lch_queue DMA will start right after lch_head one is finished.
1081 * For this DMA link to start, you still need to start (see omap_start_dma)
1082 * the first one. That will fire up the entire queue.
1084 void omap_dma_link_lch(int lch_head, int lch_queue)
1086 if (omap_dma_in_1510_mode()) {
1087 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1092 if ((dma_chan[lch_head].dev_id == -1) ||
1093 (dma_chan[lch_queue].dev_id == -1)) {
1094 printk(KERN_ERR "omap_dma: trying to link "
1095 "non requested channels\n");
1099 dma_chan[lch_head].next_lch = lch_queue;
1101 EXPORT_SYMBOL(omap_dma_link_lch);
1104 * Once the DMA queue is stopped, we can destroy it.
1106 void omap_dma_unlink_lch(int lch_head, int lch_queue)
1108 if (omap_dma_in_1510_mode()) {
1109 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1114 if (dma_chan[lch_head].next_lch != lch_queue ||
1115 dma_chan[lch_head].next_lch == -1) {
1116 printk(KERN_ERR "omap_dma: trying to unlink "
1117 "non linked channels\n");
1121 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
1122 (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
1123 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1124 "before unlinking\n");
1128 dma_chan[lch_head].next_lch = -1;
1130 EXPORT_SYMBOL(omap_dma_unlink_lch);
1132 /*----------------------------------------------------------------------------*/
1134 #ifndef CONFIG_ARCH_OMAP1
1135 /* Create chain of DMA channesls */
1136 static void create_dma_lch_chain(int lch_head, int lch_queue)
1140 /* Check if this is the first link in chain */
1141 if (dma_chan[lch_head].next_linked_ch == -1) {
1142 dma_chan[lch_head].next_linked_ch = lch_queue;
1143 dma_chan[lch_head].prev_linked_ch = lch_queue;
1144 dma_chan[lch_queue].next_linked_ch = lch_head;
1145 dma_chan[lch_queue].prev_linked_ch = lch_head;
1148 /* a link exists, link the new channel in circular chain */
1150 dma_chan[lch_queue].next_linked_ch =
1151 dma_chan[lch_head].next_linked_ch;
1152 dma_chan[lch_queue].prev_linked_ch = lch_head;
1153 dma_chan[lch_head].next_linked_ch = lch_queue;
1154 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1158 l = dma_read(CLNK_CTRL(lch_head));
1161 dma_write(l, CLNK_CTRL(lch_head));
1163 l = dma_read(CLNK_CTRL(lch_queue));
1165 l |= (dma_chan[lch_queue].next_linked_ch);
1166 dma_write(l, CLNK_CTRL(lch_queue));
1170 * @brief omap_request_dma_chain : Request a chain of DMA channels
1172 * @param dev_id - Device id using the dma channel
1173 * @param dev_name - Device name
1174 * @param callback - Call back function
1176 * @no_of_chans - Number of channels requested
1177 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1178 * OMAP_DMA_DYNAMIC_CHAIN
1179 * @params - Channel parameters
1181 * @return - Succes : 0
1182 * Failure: -EINVAL/-ENOMEM
1184 int omap_request_dma_chain(int dev_id, const char *dev_name,
1185 void (*callback) (int chain_id, u16 ch_status,
1187 int *chain_id, int no_of_chans, int chain_mode,
1188 struct omap_dma_channel_params params)
1193 /* Is the chain mode valid ? */
1194 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1195 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1196 printk(KERN_ERR "Invalid chain mode requested\n");
1200 if (unlikely((no_of_chans < 1
1201 || no_of_chans > dma_lch_count))) {
1202 printk(KERN_ERR "Invalid Number of channels requested\n");
1206 /* Allocate a queue to maintain the status of the channels
1208 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1209 if (channels == NULL) {
1210 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1214 /* request and reserve DMA channels for the chain */
1215 for (i = 0; i < no_of_chans; i++) {
1216 err = omap_request_dma(dev_id, dev_name,
1217 callback, 0, &channels[i]);
1220 for (j = 0; j < i; j++)
1221 omap_free_dma(channels[j]);
1223 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1226 dma_chan[channels[i]].prev_linked_ch = -1;
1227 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1230 * Allowing client drivers to set common parameters now,
1231 * so that later only relevant (src_start, dest_start
1232 * and element count) can be set
1234 omap_set_dma_params(channels[i], ¶ms);
1237 *chain_id = channels[0];
1238 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1239 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1240 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1241 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1243 for (i = 0; i < no_of_chans; i++)
1244 dma_chan[channels[i]].chain_id = *chain_id;
1246 /* Reset the Queue pointers */
1247 OMAP_DMA_CHAIN_QINIT(*chain_id);
1249 /* Set up the chain */
1250 if (no_of_chans == 1)
1251 create_dma_lch_chain(channels[0], channels[0]);
1253 for (i = 0; i < (no_of_chans - 1); i++)
1254 create_dma_lch_chain(channels[i], channels[i + 1]);
1259 EXPORT_SYMBOL(omap_request_dma_chain);
1262 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1263 * params after setting it. Dont do this while dma is running!!
1265 * @param chain_id - Chained logical channel id.
1268 * @return - Success : 0
1271 int omap_modify_dma_chain_params(int chain_id,
1272 struct omap_dma_channel_params params)
1277 /* Check for input params */
1278 if (unlikely((chain_id < 0
1279 || chain_id >= dma_lch_count))) {
1280 printk(KERN_ERR "Invalid chain id\n");
1284 /* Check if the chain exists */
1285 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1286 printk(KERN_ERR "Chain doesn't exists\n");
1289 channels = dma_linked_lch[chain_id].linked_dmach_q;
1291 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1293 * Allowing client drivers to set common parameters now,
1294 * so that later only relevant (src_start, dest_start
1295 * and element count) can be set
1297 omap_set_dma_params(channels[i], ¶ms);
1302 EXPORT_SYMBOL(omap_modify_dma_chain_params);
1305 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1309 * @return - Success : 0
1312 int omap_free_dma_chain(int chain_id)
1317 /* Check for input params */
1318 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1319 printk(KERN_ERR "Invalid chain id\n");
1323 /* Check if the chain exists */
1324 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1325 printk(KERN_ERR "Chain doesn't exists\n");
1329 channels = dma_linked_lch[chain_id].linked_dmach_q;
1330 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1331 dma_chan[channels[i]].next_linked_ch = -1;
1332 dma_chan[channels[i]].prev_linked_ch = -1;
1333 dma_chan[channels[i]].chain_id = -1;
1334 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1335 omap_free_dma(channels[i]);
1340 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1341 dma_linked_lch[chain_id].chain_mode = -1;
1342 dma_linked_lch[chain_id].chain_state = -1;
1346 EXPORT_SYMBOL(omap_free_dma_chain);
1349 * @brief omap_dma_chain_status - Check if the chain is in
1350 * active / inactive state.
1353 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1356 int omap_dma_chain_status(int chain_id)
1358 /* Check for input params */
1359 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1360 printk(KERN_ERR "Invalid chain id\n");
1364 /* Check if the chain exists */
1365 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1366 printk(KERN_ERR "Chain doesn't exists\n");
1369 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1370 dma_linked_lch[chain_id].q_count);
1372 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1373 return OMAP_DMA_CHAIN_INACTIVE;
1375 return OMAP_DMA_CHAIN_ACTIVE;
1377 EXPORT_SYMBOL(omap_dma_chain_status);
1380 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1381 * set the params and start the transfer.
1384 * @param src_start - buffer start address
1385 * @param dest_start - Dest address
1387 * @param frame_count
1388 * @param callbk_data - channel callback parameter data.
1390 * @return - Success : 0
1391 * Failure: -EINVAL/-EBUSY
1393 int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1394 int elem_count, int frame_count, void *callbk_data)
1401 * if buffer size is less than 1 then there is
1402 * no use of starting the chain
1404 if (elem_count < 1) {
1405 printk(KERN_ERR "Invalid buffer size\n");
1409 /* Check for input params */
1410 if (unlikely((chain_id < 0
1411 || chain_id >= dma_lch_count))) {
1412 printk(KERN_ERR "Invalid chain id\n");
1416 /* Check if the chain exists */
1417 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1418 printk(KERN_ERR "Chain doesn't exist\n");
1422 /* Check if all the channels in chain are in use */
1423 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1426 /* Frame count may be negative in case of indexed transfers */
1427 channels = dma_linked_lch[chain_id].linked_dmach_q;
1429 /* Get a free channel */
1430 lch = channels[dma_linked_lch[chain_id].q_tail];
1432 /* Store the callback data */
1433 dma_chan[lch].data = callbk_data;
1435 /* Increment the q_tail */
1436 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1438 /* Set the params to the free channel */
1440 dma_write(src_start, CSSA(lch));
1441 if (dest_start != 0)
1442 dma_write(dest_start, CDSA(lch));
1444 /* Write the buffer size */
1445 dma_write(elem_count, CEN(lch));
1446 dma_write(frame_count, CFN(lch));
1449 * If the chain is dynamically linked,
1450 * then we may have to start the chain if its not active
1452 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1455 * In Dynamic chain, if the chain is not started,
1458 if (dma_linked_lch[chain_id].chain_state ==
1459 DMA_CHAIN_NOTSTARTED) {
1460 /* Enable the link in previous channel */
1461 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1463 enable_lnk(dma_chan[lch].prev_linked_ch);
1464 dma_chan[lch].state = DMA_CH_QUEUED;
1468 * Chain is already started, make sure its active,
1469 * if not then start the chain
1474 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1476 enable_lnk(dma_chan[lch].prev_linked_ch);
1477 dma_chan[lch].state = DMA_CH_QUEUED;
1479 if (0 == ((1 << 7) & dma_read(
1480 CCR(dma_chan[lch].prev_linked_ch)))) {
1481 disable_lnk(dma_chan[lch].
1483 pr_debug("\n prev ch is stopped\n");
1488 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1490 enable_lnk(dma_chan[lch].prev_linked_ch);
1491 dma_chan[lch].state = DMA_CH_QUEUED;
1494 omap_enable_channel_irq(lch);
1496 l = dma_read(CCR(lch));
1498 if ((0 == (l & (1 << 24))))
1502 if (start_dma == 1) {
1503 if (0 == (l & (1 << 7))) {
1505 dma_chan[lch].state = DMA_CH_STARTED;
1506 pr_debug("starting %d\n", lch);
1507 dma_write(l, CCR(lch));
1511 if (0 == (l & (1 << 7)))
1512 dma_write(l, CCR(lch));
1514 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1520 EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1523 * @brief omap_start_dma_chain_transfers - Start the chain
1527 * @return - Success : 0
1528 * Failure : -EINVAL/-EBUSY
1530 int omap_start_dma_chain_transfers(int chain_id)
1535 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1536 printk(KERN_ERR "Invalid chain id\n");
1540 channels = dma_linked_lch[chain_id].linked_dmach_q;
1542 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1543 printk(KERN_ERR "Chain is already started\n");
1547 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1548 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1550 enable_lnk(channels[i]);
1551 omap_enable_channel_irq(channels[i]);
1554 omap_enable_channel_irq(channels[0]);
1557 l = dma_read(CCR(channels[0]));
1559 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1560 dma_chan[channels[0]].state = DMA_CH_STARTED;
1562 if ((0 == (l & (1 << 24))))
1566 dma_write(l, CCR(channels[0]));
1568 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
1572 EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1575 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1579 * @return - Success : 0
1582 int omap_stop_dma_chain_transfers(int chain_id)
1588 /* Check for input params */
1589 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1590 printk(KERN_ERR "Invalid chain id\n");
1594 /* Check if the chain exists */
1595 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1596 printk(KERN_ERR "Chain doesn't exists\n");
1599 channels = dma_linked_lch[chain_id].linked_dmach_q;
1603 * Special programming model needed to disable DMA before end of block
1605 sys_cf = dma_read(OCP_SYSCONFIG);
1607 /* Middle mode reg set no Standby */
1608 l &= ~((1 << 12)|(1 << 13));
1609 dma_write(l, OCP_SYSCONFIG);
1611 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1613 /* Stop the Channel transmission */
1614 l = dma_read(CCR(channels[i]));
1616 dma_write(l, CCR(channels[i]));
1618 /* Disable the link in all the channels */
1619 disable_lnk(channels[i]);
1620 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1623 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1625 /* Reset the Queue pointers */
1626 OMAP_DMA_CHAIN_QINIT(chain_id);
1628 /* Errata - put in the old value */
1629 dma_write(sys_cf, OCP_SYSCONFIG);
1633 EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1635 /* Get the index of the ongoing DMA in chain */
1637 * @brief omap_get_dma_chain_index - Get the element and frame index
1638 * of the ongoing DMA in chain
1641 * @param ei - Element index
1642 * @param fi - Frame index
1644 * @return - Success : 0
1647 int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1652 /* Check for input params */
1653 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1654 printk(KERN_ERR "Invalid chain id\n");
1658 /* Check if the chain exists */
1659 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1660 printk(KERN_ERR "Chain doesn't exists\n");
1666 channels = dma_linked_lch[chain_id].linked_dmach_q;
1668 /* Get the current channel */
1669 lch = channels[dma_linked_lch[chain_id].q_head];
1671 *ei = dma_read(CCEN(lch));
1672 *fi = dma_read(CCFN(lch));
1676 EXPORT_SYMBOL(omap_get_dma_chain_index);
1679 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1680 * ongoing DMA in chain
1684 * @return - Success : Destination position
1687 int omap_get_dma_chain_dst_pos(int chain_id)
1692 /* Check for input params */
1693 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1694 printk(KERN_ERR "Invalid chain id\n");
1698 /* Check if the chain exists */
1699 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1700 printk(KERN_ERR "Chain doesn't exists\n");
1704 channels = dma_linked_lch[chain_id].linked_dmach_q;
1706 /* Get the current channel */
1707 lch = channels[dma_linked_lch[chain_id].q_head];
1709 return dma_read(CDAC(lch));
1711 EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1714 * @brief omap_get_dma_chain_src_pos - Get the source position
1715 * of the ongoing DMA in chain
1718 * @return - Success : Destination position
1721 int omap_get_dma_chain_src_pos(int chain_id)
1726 /* Check for input params */
1727 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
1728 printk(KERN_ERR "Invalid chain id\n");
1732 /* Check if the chain exists */
1733 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1734 printk(KERN_ERR "Chain doesn't exists\n");
1738 channels = dma_linked_lch[chain_id].linked_dmach_q;
1740 /* Get the current channel */
1741 lch = channels[dma_linked_lch[chain_id].q_head];
1743 return dma_read(CSAC(lch));
1745 EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
1746 #endif /* ifndef CONFIG_ARCH_OMAP1 */
1748 /*----------------------------------------------------------------------------*/
1750 #ifdef CONFIG_ARCH_OMAP1
1752 static int omap1_dma_handle_ch(int ch)
1756 if (enable_1510_mode && ch >= 6) {
1757 csr = dma_chan[ch].saved_csr;
1758 dma_chan[ch].saved_csr = 0;
1760 csr = dma_read(CSR(ch));
1761 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1762 dma_chan[ch + 6].saved_csr = csr >> 7;
1765 if ((csr & 0x3f) == 0)
1767 if (unlikely(dma_chan[ch].dev_id == -1)) {
1768 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1769 "%d (CSR %04x)\n", ch, csr);
1772 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
1773 printk(KERN_WARNING "DMA timeout with device %d\n",
1774 dma_chan[ch].dev_id);
1775 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1776 printk(KERN_WARNING "DMA synchronization event drop occurred "
1777 "with device %d\n", dma_chan[ch].dev_id);
1778 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1779 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1780 if (likely(dma_chan[ch].callback != NULL))
1781 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
1786 static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
1788 int ch = ((int) dev_id) - 1;
1792 int handled_now = 0;
1794 handled_now += omap1_dma_handle_ch(ch);
1795 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1796 handled_now += omap1_dma_handle_ch(ch + 6);
1799 handled += handled_now;
1802 return handled ? IRQ_HANDLED : IRQ_NONE;
1806 #define omap1_dma_irq_handler NULL
1809 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1811 static int omap2_dma_handle_ch(int ch)
1813 u32 status = dma_read(CSR(ch));
1816 if (printk_ratelimit())
1817 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1819 dma_write(1 << ch, IRQSTATUS_L0);
1822 if (unlikely(dma_chan[ch].dev_id == -1)) {
1823 if (printk_ratelimit())
1824 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1825 "channel %d\n", status, ch);
1828 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1830 "DMA synchronization event drop occurred with device "
1831 "%d\n", dma_chan[ch].dev_id);
1832 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ))
1833 printk(KERN_INFO "DMA transaction error with device %d\n",
1834 dma_chan[ch].dev_id);
1835 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1836 printk(KERN_INFO "DMA secure error with device %d\n",
1837 dma_chan[ch].dev_id);
1838 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1839 printk(KERN_INFO "DMA misaligned error with device %d\n",
1840 dma_chan[ch].dev_id);
1842 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(ch));
1843 dma_write(1 << ch, IRQSTATUS_L0);
1845 /* If the ch is not chained then chain_id will be -1 */
1846 if (dma_chan[ch].chain_id != -1) {
1847 int chain_id = dma_chan[ch].chain_id;
1848 dma_chan[ch].state = DMA_CH_NOTSTARTED;
1849 if (dma_read(CLNK_CTRL(ch)) & (1 << 15))
1850 dma_chan[dma_chan[ch].next_linked_ch].state =
1852 if (dma_linked_lch[chain_id].chain_mode ==
1853 OMAP_DMA_DYNAMIC_CHAIN)
1856 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1857 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1859 status = dma_read(CSR(ch));
1862 if (likely(dma_chan[ch].callback != NULL))
1863 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
1865 dma_write(status, CSR(ch));
1870 /* STATUS register count is from 1-32 while our is 0-31 */
1871 static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
1876 val = dma_read(IRQSTATUS_L0);
1878 if (printk_ratelimit())
1879 printk(KERN_WARNING "Spurious DMA IRQ\n");
1882 for (i = 0; i < dma_lch_count && val != 0; i++) {
1884 omap2_dma_handle_ch(i);
1891 static struct irqaction omap24xx_dma_irq = {
1893 .handler = omap2_dma_irq_handler,
1894 .flags = IRQF_DISABLED
1898 static struct irqaction omap24xx_dma_irq;
1901 /*----------------------------------------------------------------------------*/
1903 static struct lcd_dma_info {
1906 void (*callback)(u16 status, void *data);
1910 unsigned long addr, size;
1911 int rotate, data_type, xres, yres;
1917 int single_transfer;
1920 void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
1923 lcd_dma.addr = addr;
1924 lcd_dma.data_type = data_type;
1925 lcd_dma.xres = fb_xres;
1926 lcd_dma.yres = fb_yres;
1928 EXPORT_SYMBOL(omap_set_lcd_dma_b1);
1930 void omap_set_lcd_dma_src_port(int port)
1932 lcd_dma.src_port = port;
1935 void omap_set_lcd_dma_ext_controller(int external)
1937 lcd_dma.ext_ctrl = external;
1939 EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
1941 void omap_set_lcd_dma_single_transfer(int single)
1943 lcd_dma.single_transfer = single;
1945 EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
1947 void omap_set_lcd_dma_b1_rotation(int rotate)
1949 if (omap_dma_in_1510_mode()) {
1950 printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
1954 lcd_dma.rotate = rotate;
1956 EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
1958 void omap_set_lcd_dma_b1_mirror(int mirror)
1960 if (omap_dma_in_1510_mode()) {
1961 printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
1964 lcd_dma.mirror = mirror;
1966 EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);
1968 void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
1970 if (omap_dma_in_1510_mode()) {
1971 printk(KERN_ERR "DMA virtual resulotion is not supported "
1975 lcd_dma.vxres = vxres;
1977 EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
1979 void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
1981 if (omap_dma_in_1510_mode()) {
1982 printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
1985 lcd_dma.xscale = xscale;
1986 lcd_dma.yscale = yscale;
1988 EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
1990 static void set_b1_regs(void)
1992 unsigned long top, bottom;
1995 unsigned long en, fn;
1997 unsigned long vxres;
1998 unsigned int xscale, yscale;
2000 switch (lcd_dma.data_type) {
2001 case OMAP_DMA_DATA_TYPE_S8:
2004 case OMAP_DMA_DATA_TYPE_S16:
2007 case OMAP_DMA_DATA_TYPE_S32:
2015 vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
2016 xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
2017 yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
2018 BUG_ON(vxres < lcd_dma.xres);
2020 #define PIXADDR(x, y) (lcd_dma.addr + \
2021 ((y) * vxres * yscale + (x) * xscale) * es)
2022 #define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
2024 switch (lcd_dma.rotate) {
2026 if (!lcd_dma.mirror) {
2027 top = PIXADDR(0, 0);
2028 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2029 /* 1510 DMA requires the bottom address to be 2 more
2030 * than the actual last memory access location. */
2031 if (omap_dma_in_1510_mode() &&
2032 lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
2034 ei = PIXSTEP(0, 0, 1, 0);
2035 fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
2037 top = PIXADDR(lcd_dma.xres - 1, 0);
2038 bottom = PIXADDR(0, lcd_dma.yres - 1);
2039 ei = PIXSTEP(1, 0, 0, 0);
2040 fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
2046 if (!lcd_dma.mirror) {
2047 top = PIXADDR(0, lcd_dma.yres - 1);
2048 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2049 ei = PIXSTEP(0, 1, 0, 0);
2050 fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
2052 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2053 bottom = PIXADDR(0, 0);
2054 ei = PIXSTEP(0, 1, 0, 0);
2055 fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
2061 if (!lcd_dma.mirror) {
2062 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2063 bottom = PIXADDR(0, 0);
2064 ei = PIXSTEP(1, 0, 0, 0);
2065 fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
2067 top = PIXADDR(0, lcd_dma.yres - 1);
2068 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2069 ei = PIXSTEP(0, 0, 1, 0);
2070 fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
2076 if (!lcd_dma.mirror) {
2077 top = PIXADDR(lcd_dma.xres - 1, 0);
2078 bottom = PIXADDR(0, lcd_dma.yres - 1);
2079 ei = PIXSTEP(0, 0, 0, 1);
2080 fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
2082 top = PIXADDR(0, 0);
2083 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2084 ei = PIXSTEP(0, 0, 0, 1);
2085 fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
2092 return; /* Suppress warning about uninitialized vars */
2095 if (omap_dma_in_1510_mode()) {
2096 omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
2097 omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
2098 omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
2099 omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
2105 omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
2106 omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
2107 omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
2108 omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
2110 omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
2111 omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
2113 w = omap_readw(OMAP1610_DMA_LCD_CSDP);
2115 w |= lcd_dma.data_type;
2116 omap_writew(w, OMAP1610_DMA_LCD_CSDP);
2118 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2119 /* Always set the source port as SDRAM for now*/
2121 if (lcd_dma.callback != NULL)
2122 w |= 1 << 1; /* Block interrupt enable */
2125 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2127 if (!(lcd_dma.rotate || lcd_dma.mirror ||
2128 lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
2131 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2132 /* Set the double-indexed addressing mode */
2134 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2136 omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
2137 omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
2138 omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
2141 static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id)
2145 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2146 if (unlikely(!(w & (1 << 3)))) {
2147 printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
2152 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2154 if (lcd_dma.callback != NULL)
2155 lcd_dma.callback(w, lcd_dma.cb_data);
2160 int omap_request_lcd_dma(void (*callback)(u16 status, void *data),
2163 spin_lock_irq(&lcd_dma.lock);
2164 if (lcd_dma.reserved) {
2165 spin_unlock_irq(&lcd_dma.lock);
2166 printk(KERN_ERR "LCD DMA channel already reserved\n");
2170 lcd_dma.reserved = 1;
2171 spin_unlock_irq(&lcd_dma.lock);
2172 lcd_dma.callback = callback;
2173 lcd_dma.cb_data = data;
2175 lcd_dma.single_transfer = 0;
2181 lcd_dma.ext_ctrl = 0;
2182 lcd_dma.src_port = 0;
2186 EXPORT_SYMBOL(omap_request_lcd_dma);
2188 void omap_free_lcd_dma(void)
2190 spin_lock(&lcd_dma.lock);
2191 if (!lcd_dma.reserved) {
2192 spin_unlock(&lcd_dma.lock);
2193 printk(KERN_ERR "LCD DMA is not reserved\n");
2197 if (!enable_1510_mode)
2198 omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1,
2199 OMAP1610_DMA_LCD_CCR);
2200 lcd_dma.reserved = 0;
2201 spin_unlock(&lcd_dma.lock);
2203 EXPORT_SYMBOL(omap_free_lcd_dma);
2205 void omap_enable_lcd_dma(void)
2210 * Set the Enable bit only if an external controller is
2211 * connected. Otherwise the OMAP internal controller will
2212 * start the transfer when it gets enabled.
2214 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2217 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2219 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2223 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2225 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2227 EXPORT_SYMBOL(omap_enable_lcd_dma);
2229 void omap_setup_lcd_dma(void)
2231 BUG_ON(lcd_dma.active);
2232 if (!enable_1510_mode) {
2233 /* Set some reasonable defaults */
2234 omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
2235 omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
2236 omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
2239 if (!enable_1510_mode) {
2242 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2244 * If DMA was already active set the end_prog bit to have
2245 * the programmed register set loaded into the active
2248 w |= 1 << 11; /* End_prog */
2249 if (!lcd_dma.single_transfer)
2250 w |= (3 << 8); /* Auto_init, repeat */
2251 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2254 EXPORT_SYMBOL(omap_setup_lcd_dma);
2256 void omap_stop_lcd_dma(void)
2261 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2264 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2266 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2268 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2270 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2272 EXPORT_SYMBOL(omap_stop_lcd_dma);
2274 /*----------------------------------------------------------------------------*/
2276 static int __init omap_init_dma(void)
2280 if (cpu_class_is_omap1()) {
2281 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP1_DMA_BASE);
2282 dma_lch_count = OMAP1_LOGICAL_DMA_CH_COUNT;
2283 } else if (cpu_is_omap24xx()) {
2284 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP24XX_DMA4_BASE);
2285 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
2286 } else if (cpu_is_omap34xx()) {
2287 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP34XX_DMA4_BASE);
2288 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
2290 pr_err("DMA init failed for unsupported omap\n");
2294 dma_chan = kzalloc(sizeof(struct omap_dma_lch) * dma_lch_count,
2299 if (cpu_class_is_omap2()) {
2300 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2301 dma_lch_count, GFP_KERNEL);
2302 if (!dma_linked_lch) {
2308 if (cpu_is_omap15xx()) {
2309 printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
2311 enable_1510_mode = 1;
2312 } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
2313 printk(KERN_INFO "OMAP DMA hardware version %d\n",
2315 printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
2316 (dma_read(CAPS_0_U) << 16) |
2318 (dma_read(CAPS_1_U) << 16) |
2320 dma_read(CAPS_2), dma_read(CAPS_3),
2322 if (!enable_1510_mode) {
2325 /* Disable OMAP 3.0/3.1 compatibility mode. */
2329 dma_chan_count = 16;
2332 if (cpu_is_omap16xx()) {
2335 /* this would prevent OMAP sleep */
2336 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2338 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2340 } else if (cpu_class_is_omap2()) {
2341 u8 revision = dma_read(REVISION) & 0xff;
2342 printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
2343 revision >> 4, revision & 0xf);
2344 dma_chan_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
2350 spin_lock_init(&lcd_dma.lock);
2351 spin_lock_init(&dma_chan_lock);
2353 for (ch = 0; ch < dma_chan_count; ch++) {
2355 dma_chan[ch].dev_id = -1;
2356 dma_chan[ch].next_lch = -1;
2358 if (ch >= 6 && enable_1510_mode)
2361 if (cpu_class_is_omap1()) {
2363 * request_irq() doesn't like dev_id (ie. ch) being
2364 * zero, so we have to kludge around this.
2366 r = request_irq(omap1_dma_irq[ch],
2367 omap1_dma_irq_handler, 0, "DMA",
2372 printk(KERN_ERR "unable to request IRQ %d "
2373 "for DMA (error %d)\n",
2374 omap1_dma_irq[ch], r);
2375 for (i = 0; i < ch; i++)
2376 free_irq(omap1_dma_irq[i],
2383 if (cpu_is_omap2430() || cpu_is_omap34xx())
2384 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2385 DMA_DEFAULT_FIFO_DEPTH, 0);
2387 if (cpu_class_is_omap2())
2388 setup_irq(INT_24XX_SDMA_IRQ0, &omap24xx_dma_irq);
2390 /* FIXME: Update LCD DMA to work on 24xx */
2391 if (cpu_class_is_omap1()) {
2392 r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0,
2397 printk(KERN_ERR "unable to request IRQ for LCD DMA "
2399 for (i = 0; i < dma_chan_count; i++)
2400 free_irq(omap1_dma_irq[i], (void *) (i + 1));
2408 arch_initcall(omap_init_dma);