1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/plat-omap/dma.c
5 * Copyright (C) 2003 - 2008 Nokia Corporation
6 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
7 * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
8 * Graphics DMA and LCD DMA graphics tranformations
9 * by Imre Deak <imre.deak@nokia.com>
10 * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
11 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
12 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
14 * Copyright (C) 2009 Texas Instruments
15 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
17 * Support functions for the OMAP internal DMA channels.
19 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
20 * Converted DMA library into DMA platform driver.
21 * - G, Manjunath Kondaiah <manjugk@ti.com>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/irq.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
35 #include <linux/omap-dma.h>
37 #ifdef CONFIG_ARCH_OMAP1
42 * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
43 * channels that an instance of the SDMA IP block can support. Used
44 * to size arrays. (The actual maximum on a particular SoC may be less
45 * than this -- for example, OMAP1 SDMA instances only support 17 logical
48 #define MAX_LOGICAL_DMA_CH_COUNT 32
52 #ifndef CONFIG_ARCH_OMAP1
53 enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
54 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
57 enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
60 #define OMAP_DMA_ACTIVE 0x01
61 #define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
63 #define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
65 static struct omap_system_dma_plat_info *p;
66 static struct omap_dma_dev_attr *d;
67 static void omap_clear_dma(int lch);
68 static int enable_1510_mode;
71 static struct omap_dma_global_context_registers {
74 u32 dma_ocp_sysconfig;
76 } omap_dma_global_context;
78 struct dma_link_info {
80 int no_of_lchs_linked;
91 static struct dma_link_info *dma_linked_lch;
93 #ifndef CONFIG_ARCH_OMAP1
95 /* Chain handling macros */
96 #define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
97 (0 == dma_linked_lch[chain_id].q_count)
98 #define __OMAP_DMA_CHAIN_INCQ(end) \
99 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
100 #define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
102 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
103 dma_linked_lch[chain_id].q_count--; \
107 static int dma_lch_count;
108 static int dma_chan_count;
109 static int omap_dma_reserve_channels;
111 static spinlock_t dma_chan_lock;
112 static struct omap_dma_lch *dma_chan;
114 static inline void disable_lnk(int lch);
115 static void omap_disable_channel_irq(int lch);
116 static inline void omap_enable_channel_irq(int lch);
118 #ifdef CONFIG_ARCH_OMAP15XX
119 /* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
120 static int omap_dma_in_1510_mode(void)
122 return enable_1510_mode;
125 #define omap_dma_in_1510_mode() 0
128 #ifdef CONFIG_ARCH_OMAP1
129 static inline void set_gdma_dev(int req, int dev)
131 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
132 int shift = ((req - 1) % 5) * 6;
136 l &= ~(0x3f << shift);
137 l |= (dev - 1) << shift;
141 #define set_gdma_dev(req, dev) do {} while (0)
142 #define omap_readl(reg) 0
143 #define omap_writel(val, reg) do {} while (0)
146 #ifdef CONFIG_ARCH_OMAP1
147 void omap_set_dma_priority(int lch, int dst_port, int priority)
154 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
155 reg = OMAP_TC_OCPT1_PRIOR;
157 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
158 reg = OMAP_TC_OCPT2_PRIOR;
160 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
161 reg = OMAP_TC_EMIFF_PRIOR;
163 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
164 reg = OMAP_TC_EMIFS_PRIOR;
172 l |= (priority & 0xf) << 8;
178 #ifdef CONFIG_ARCH_OMAP2PLUS
179 void omap_set_dma_priority(int lch, int dst_port, int priority)
183 ccr = p->dma_read(CCR, lch);
188 p->dma_write(ccr, CCR, lch);
191 EXPORT_SYMBOL(omap_set_dma_priority);
193 void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
194 int frame_count, int sync_mode,
195 int dma_trigger, int src_or_dst_synch)
199 l = p->dma_read(CSDP, lch);
202 p->dma_write(l, CSDP, lch);
207 ccr = p->dma_read(CCR, lch);
209 if (sync_mode == OMAP_DMA_SYNC_FRAME)
211 p->dma_write(ccr, CCR, lch);
213 ccr = p->dma_read(CCR2, lch);
215 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
217 p->dma_write(ccr, CCR2, lch);
220 if (dma_omap2plus() && dma_trigger) {
223 val = p->dma_read(CCR, lch);
225 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
226 val &= ~((1 << 23) | (3 << 19) | 0x1f);
227 val |= (dma_trigger & ~0x1f) << 14;
228 val |= dma_trigger & 0x1f;
230 if (sync_mode & OMAP_DMA_SYNC_FRAME)
235 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
240 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
241 val &= ~(1 << 24); /* dest synch */
242 val |= (1 << 23); /* Prefetch */
243 } else if (src_or_dst_synch) {
244 val |= 1 << 24; /* source synch */
246 val &= ~(1 << 24); /* dest synch */
248 p->dma_write(val, CCR, lch);
251 p->dma_write(elem_count, CEN, lch);
252 p->dma_write(frame_count, CFN, lch);
254 EXPORT_SYMBOL(omap_set_dma_transfer_params);
256 void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
258 if (dma_omap1() && !dma_omap15xx()) {
261 l = p->dma_read(LCH_CTRL, lch);
264 p->dma_write(l, LCH_CTRL, lch);
267 EXPORT_SYMBOL(omap_set_dma_channel_mode);
269 /* Note that src_port is only for omap1 */
270 void omap_set_dma_src_params(int lch, int src_port, int src_amode,
271 unsigned long src_start,
272 int src_ei, int src_fi)
279 w = p->dma_read(CSDP, lch);
282 p->dma_write(w, CSDP, lch);
285 l = p->dma_read(CCR, lch);
287 l |= src_amode << 12;
288 p->dma_write(l, CCR, lch);
290 p->dma_write(src_start, CSSA, lch);
292 p->dma_write(src_ei, CSEI, lch);
293 p->dma_write(src_fi, CSFI, lch);
295 EXPORT_SYMBOL(omap_set_dma_src_params);
297 void omap_set_dma_src_data_pack(int lch, int enable)
301 l = p->dma_read(CSDP, lch);
305 p->dma_write(l, CSDP, lch);
307 EXPORT_SYMBOL(omap_set_dma_src_data_pack);
309 void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
311 unsigned int burst = 0;
314 l = p->dma_read(CSDP, lch);
317 switch (burst_mode) {
318 case OMAP_DMA_DATA_BURST_DIS:
320 case OMAP_DMA_DATA_BURST_4:
326 case OMAP_DMA_DATA_BURST_8:
327 if (dma_omap2plus()) {
332 * not supported by current hardware on OMAP1
336 case OMAP_DMA_DATA_BURST_16:
337 if (dma_omap2plus()) {
341 /* OMAP1 don't support burst 16 */
348 p->dma_write(l, CSDP, lch);
350 EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
352 /* Note that dest_port is only for OMAP1 */
353 void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
354 unsigned long dest_start,
355 int dst_ei, int dst_fi)
360 l = p->dma_read(CSDP, lch);
363 p->dma_write(l, CSDP, lch);
366 l = p->dma_read(CCR, lch);
368 l |= dest_amode << 14;
369 p->dma_write(l, CCR, lch);
371 p->dma_write(dest_start, CDSA, lch);
373 p->dma_write(dst_ei, CDEI, lch);
374 p->dma_write(dst_fi, CDFI, lch);
376 EXPORT_SYMBOL(omap_set_dma_dest_params);
378 void omap_set_dma_dest_data_pack(int lch, int enable)
382 l = p->dma_read(CSDP, lch);
386 p->dma_write(l, CSDP, lch);
388 EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
390 void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
392 unsigned int burst = 0;
395 l = p->dma_read(CSDP, lch);
398 switch (burst_mode) {
399 case OMAP_DMA_DATA_BURST_DIS:
401 case OMAP_DMA_DATA_BURST_4:
407 case OMAP_DMA_DATA_BURST_8:
413 case OMAP_DMA_DATA_BURST_16:
414 if (dma_omap2plus()) {
418 /* OMAP1 don't support burst 16 */
421 printk(KERN_ERR "Invalid DMA burst mode\n");
426 p->dma_write(l, CSDP, lch);
428 EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
430 static inline void omap_enable_channel_irq(int lch)
434 p->dma_read(CSR, lch);
436 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
438 /* Enable some nice interrupts. */
439 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
442 static inline void omap_disable_channel_irq(int lch)
444 /* disable channel interrupts */
445 p->dma_write(0, CICR, lch);
448 p->dma_read(CSR, lch);
450 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
453 void omap_disable_dma_irq(int lch, u16 bits)
455 dma_chan[lch].enabled_irqs &= ~bits;
457 EXPORT_SYMBOL(omap_disable_dma_irq);
459 static inline void enable_lnk(int lch)
463 l = p->dma_read(CLNK_CTRL, lch);
468 /* Set the ENABLE_LNK bits */
469 if (dma_chan[lch].next_lch != -1)
470 l = dma_chan[lch].next_lch | (1 << 15);
472 #ifndef CONFIG_ARCH_OMAP1
474 if (dma_chan[lch].next_linked_ch != -1)
475 l = dma_chan[lch].next_linked_ch | (1 << 15);
478 p->dma_write(l, CLNK_CTRL, lch);
481 static inline void disable_lnk(int lch)
485 l = p->dma_read(CLNK_CTRL, lch);
487 /* Disable interrupts */
488 omap_disable_channel_irq(lch);
491 /* Set the STOP_LNK bit */
495 if (dma_omap2plus()) {
496 /* Clear the ENABLE_LNK bit */
500 p->dma_write(l, CLNK_CTRL, lch);
501 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
504 static inline void omap2_enable_irq_lch(int lch)
512 spin_lock_irqsave(&dma_chan_lock, flags);
513 /* clear IRQ STATUS */
514 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
515 /* Enable interrupt */
516 val = p->dma_read(IRQENABLE_L0, lch);
518 p->dma_write(val, IRQENABLE_L0, lch);
519 spin_unlock_irqrestore(&dma_chan_lock, flags);
522 static inline void omap2_disable_irq_lch(int lch)
530 spin_lock_irqsave(&dma_chan_lock, flags);
531 /* Disable interrupt */
532 val = p->dma_read(IRQENABLE_L0, lch);
534 p->dma_write(val, IRQENABLE_L0, lch);
535 /* clear IRQ STATUS */
536 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
537 spin_unlock_irqrestore(&dma_chan_lock, flags);
540 int omap_request_dma(int dev_id, const char *dev_name,
541 void (*callback)(int lch, u16 ch_status, void *data),
542 void *data, int *dma_ch_out)
544 int ch, free_ch = -1;
546 struct omap_dma_lch *chan;
548 WARN(strcmp(dev_name, "DMA engine"), "Using deprecated platform DMA API - please update to DMA engine");
550 spin_lock_irqsave(&dma_chan_lock, flags);
551 for (ch = 0; ch < dma_chan_count; ch++) {
552 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
554 /* Exit after first free channel found */
559 spin_unlock_irqrestore(&dma_chan_lock, flags);
562 chan = dma_chan + free_ch;
563 chan->dev_id = dev_id;
565 if (p->clear_lch_regs)
566 p->clear_lch_regs(free_ch);
569 omap_clear_dma(free_ch);
571 spin_unlock_irqrestore(&dma_chan_lock, flags);
573 chan->dev_name = dev_name;
574 chan->callback = callback;
578 #ifndef CONFIG_ARCH_OMAP1
579 if (dma_omap2plus()) {
581 chan->next_linked_ch = -1;
585 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
588 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
589 else if (dma_omap2plus())
590 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
591 OMAP2_DMA_TRANS_ERR_IRQ;
593 if (dma_omap16xx()) {
594 /* If the sync device is set, configure it dynamically. */
596 set_gdma_dev(free_ch + 1, dev_id);
597 dev_id = free_ch + 1;
600 * Disable the 1510 compatibility mode and set the sync device
603 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
604 } else if (dma_omap1()) {
605 p->dma_write(dev_id, CCR, free_ch);
608 if (dma_omap2plus()) {
609 omap_enable_channel_irq(free_ch);
610 omap2_enable_irq_lch(free_ch);
613 *dma_ch_out = free_ch;
617 EXPORT_SYMBOL(omap_request_dma);
619 void omap_free_dma(int lch)
623 if (dma_chan[lch].dev_id == -1) {
624 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
629 /* Disable interrupt for logical channel */
631 omap2_disable_irq_lch(lch);
633 /* Disable all DMA interrupts for the channel. */
634 omap_disable_channel_irq(lch);
636 /* Make sure the DMA transfer is stopped. */
637 p->dma_write(0, CCR, lch);
639 /* Clear registers */
643 spin_lock_irqsave(&dma_chan_lock, flags);
644 dma_chan[lch].dev_id = -1;
645 dma_chan[lch].next_lch = -1;
646 dma_chan[lch].callback = NULL;
647 spin_unlock_irqrestore(&dma_chan_lock, flags);
649 EXPORT_SYMBOL(omap_free_dma);
652 * @brief omap_dma_set_global_params : Set global priority settings for dma
655 * @param max_fifo_depth
656 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
657 * DMA_THREAD_RESERVE_ONET
658 * DMA_THREAD_RESERVE_TWOT
659 * DMA_THREAD_RESERVE_THREET
662 omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
667 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
671 if (max_fifo_depth == 0)
676 reg = 0xff & max_fifo_depth;
677 reg |= (0x3 & tparams) << 12;
678 reg |= (arb_rate & 0xff) << 16;
680 p->dma_write(reg, GCR, 0);
684 * Clears any DMA state so the DMA engine is ready to restart with new buffers
685 * through omap_start_dma(). Any buffers in flight are discarded.
687 static void omap_clear_dma(int lch)
691 local_irq_save(flags);
693 local_irq_restore(flags);
696 void omap_start_dma(int lch)
701 * The CPC/CDAC register needs to be initialized to zero
702 * before starting dma transfer.
705 p->dma_write(0, CPC, lch);
707 p->dma_write(0, CDAC, lch);
709 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
710 int next_lch, cur_lch;
711 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
713 /* Set the link register of the first channel */
716 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
717 dma_chan_link_map[lch] = 1;
719 cur_lch = dma_chan[lch].next_lch;
721 next_lch = dma_chan[cur_lch].next_lch;
723 /* The loop case: we've been here already */
724 if (dma_chan_link_map[cur_lch])
726 /* Mark the current channel */
727 dma_chan_link_map[cur_lch] = 1;
730 omap_enable_channel_irq(cur_lch);
733 } while (next_lch != -1);
734 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
735 p->dma_write(lch, CLNK_CTRL, lch);
737 omap_enable_channel_irq(lch);
739 l = p->dma_read(CCR, lch);
741 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
742 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
743 l |= OMAP_DMA_CCR_EN;
746 * As dma_write() uses IO accessors which are weakly ordered, there
747 * is no guarantee that data in coherent DMA memory will be visible
748 * to the DMA device. Add a memory barrier here to ensure that any
749 * such data is visible prior to enabling DMA.
752 p->dma_write(l, CCR, lch);
754 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
756 EXPORT_SYMBOL(omap_start_dma);
758 void omap_stop_dma(int lch)
762 /* Disable all interrupts on the channel */
763 omap_disable_channel_irq(lch);
765 l = p->dma_read(CCR, lch);
766 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
767 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
771 /* Configure No-Standby */
772 l = p->dma_read(OCP_SYSCONFIG, lch);
774 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
775 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
776 p->dma_write(l , OCP_SYSCONFIG, 0);
778 l = p->dma_read(CCR, lch);
779 l &= ~OMAP_DMA_CCR_EN;
780 p->dma_write(l, CCR, lch);
782 /* Wait for sDMA FIFO drain */
783 l = p->dma_read(CCR, lch);
784 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
785 OMAP_DMA_CCR_WR_ACTIVE))) {
788 l = p->dma_read(CCR, lch);
791 pr_err("DMA drain did not complete on lch %d\n", lch);
792 /* Restore OCP_SYSCONFIG */
793 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
795 l &= ~OMAP_DMA_CCR_EN;
796 p->dma_write(l, CCR, lch);
800 * Ensure that data transferred by DMA is visible to any access
801 * after DMA has been disabled. This is important for coherent
806 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
807 int next_lch, cur_lch = lch;
808 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
810 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
812 /* The loop case: we've been here already */
813 if (dma_chan_link_map[cur_lch])
815 /* Mark the current channel */
816 dma_chan_link_map[cur_lch] = 1;
818 disable_lnk(cur_lch);
820 next_lch = dma_chan[cur_lch].next_lch;
822 } while (next_lch != -1);
825 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
827 EXPORT_SYMBOL(omap_stop_dma);
830 * Allows changing the DMA callback function or data. This may be needed if
831 * the driver shares a single DMA channel for multiple dma triggers.
834 * Returns current physical source address for the given DMA channel.
835 * If the channel is running the caller must disable interrupts prior calling
836 * this function and process the returned value before re-enabling interrupt to
837 * prevent races with the interrupt handler. Note that in continuous mode there
838 * is a chance for CSSA_L register overflow between the two reads resulting
839 * in incorrect return value.
841 dma_addr_t omap_get_dma_src_pos(int lch)
843 dma_addr_t offset = 0;
846 offset = p->dma_read(CPC, lch);
848 offset = p->dma_read(CSAC, lch);
850 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
851 offset = p->dma_read(CSAC, lch);
853 if (!dma_omap15xx()) {
855 * CDAC == 0 indicates that the DMA transfer on the channel has
856 * not been started (no data has been transferred so far).
857 * Return the programmed source start address in this case.
859 if (likely(p->dma_read(CDAC, lch)))
860 offset = p->dma_read(CSAC, lch);
862 offset = p->dma_read(CSSA, lch);
866 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
870 EXPORT_SYMBOL(omap_get_dma_src_pos);
873 * Returns current physical destination address for the given DMA channel.
874 * If the channel is running the caller must disable interrupts prior calling
875 * this function and process the returned value before re-enabling interrupt to
876 * prevent races with the interrupt handler. Note that in continuous mode there
877 * is a chance for CDSA_L register overflow between the two reads resulting
878 * in incorrect return value.
880 dma_addr_t omap_get_dma_dst_pos(int lch)
882 dma_addr_t offset = 0;
885 offset = p->dma_read(CPC, lch);
887 offset = p->dma_read(CDAC, lch);
890 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
891 * read before the DMA controller finished disabling the channel.
893 if (!dma_omap15xx() && offset == 0) {
894 offset = p->dma_read(CDAC, lch);
896 * CDAC == 0 indicates that the DMA transfer on the channel has
897 * not been started (no data has been transferred so far).
898 * Return the programmed destination start address in this case.
900 if (unlikely(!offset))
901 offset = p->dma_read(CDSA, lch);
905 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
909 EXPORT_SYMBOL(omap_get_dma_dst_pos);
911 int omap_get_dma_active_status(int lch)
913 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
915 EXPORT_SYMBOL(omap_get_dma_active_status);
917 int omap_dma_running(void)
922 if (omap_lcd_dma_running())
925 for (lch = 0; lch < dma_chan_count; lch++)
926 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
932 /*----------------------------------------------------------------------------*/
934 #ifdef CONFIG_ARCH_OMAP1
936 static int omap1_dma_handle_ch(int ch)
940 if (enable_1510_mode && ch >= 6) {
941 csr = dma_chan[ch].saved_csr;
942 dma_chan[ch].saved_csr = 0;
944 csr = p->dma_read(CSR, ch);
945 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
946 dma_chan[ch + 6].saved_csr = csr >> 7;
949 if ((csr & 0x3f) == 0)
951 if (unlikely(dma_chan[ch].dev_id == -1)) {
952 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
956 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
957 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
958 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
959 pr_warn("DMA synchronization event drop occurred with device %d\n",
960 dma_chan[ch].dev_id);
961 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
962 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
963 if (likely(dma_chan[ch].callback != NULL))
964 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
969 static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
971 int ch = ((int) dev_id) - 1;
977 handled_now += omap1_dma_handle_ch(ch);
978 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
979 handled_now += omap1_dma_handle_ch(ch + 6);
982 handled += handled_now;
985 return handled ? IRQ_HANDLED : IRQ_NONE;
989 #define omap1_dma_irq_handler NULL
992 #ifdef CONFIG_ARCH_OMAP2PLUS
994 static int omap2_dma_handle_ch(int ch)
996 u32 status = p->dma_read(CSR, ch);
999 if (printk_ratelimit())
1000 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
1001 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
1004 if (unlikely(dma_chan[ch].dev_id == -1)) {
1005 if (printk_ratelimit())
1006 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1010 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1011 pr_info("DMA synchronization event drop occurred with device %d\n",
1012 dma_chan[ch].dev_id);
1013 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
1014 printk(KERN_INFO "DMA transaction error with device %d\n",
1015 dma_chan[ch].dev_id);
1016 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
1019 ccr = p->dma_read(CCR, ch);
1020 ccr &= ~OMAP_DMA_CCR_EN;
1021 p->dma_write(ccr, CCR, ch);
1022 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1025 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1026 printk(KERN_INFO "DMA secure error with device %d\n",
1027 dma_chan[ch].dev_id);
1028 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1029 printk(KERN_INFO "DMA misaligned error with device %d\n",
1030 dma_chan[ch].dev_id);
1032 p->dma_write(status, CSR, ch);
1033 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
1034 /* read back the register to flush the write */
1035 p->dma_read(IRQSTATUS_L0, ch);
1037 /* If the ch is not chained then chain_id will be -1 */
1038 if (dma_chan[ch].chain_id != -1) {
1039 int chain_id = dma_chan[ch].chain_id;
1040 dma_chan[ch].state = DMA_CH_NOTSTARTED;
1041 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
1042 dma_chan[dma_chan[ch].next_linked_ch].state =
1044 if (dma_linked_lch[chain_id].chain_mode ==
1045 OMAP_DMA_DYNAMIC_CHAIN)
1048 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1049 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1051 status = p->dma_read(CSR, ch);
1052 p->dma_write(status, CSR, ch);
1055 if (likely(dma_chan[ch].callback != NULL))
1056 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
1061 /* STATUS register count is from 1-32 while our is 0-31 */
1062 static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
1064 u32 val, enable_reg;
1067 val = p->dma_read(IRQSTATUS_L0, 0);
1069 if (printk_ratelimit())
1070 printk(KERN_WARNING "Spurious DMA IRQ\n");
1073 enable_reg = p->dma_read(IRQENABLE_L0, 0);
1074 val &= enable_reg; /* Dispatch only relevant interrupts */
1075 for (i = 0; i < dma_lch_count && val != 0; i++) {
1077 omap2_dma_handle_ch(i);
1084 static struct irqaction omap24xx_dma_irq = {
1086 .handler = omap2_dma_irq_handler,
1090 static struct irqaction omap24xx_dma_irq;
1093 /*----------------------------------------------------------------------------*/
1096 * Note that we are currently using only IRQENABLE_L0 and L1.
1097 * As the DSP may be using IRQENABLE_L2 and L3, let's not
1098 * touch those for now.
1100 void omap_dma_global_context_save(void)
1102 omap_dma_global_context.dma_irqenable_l0 =
1103 p->dma_read(IRQENABLE_L0, 0);
1104 omap_dma_global_context.dma_irqenable_l1 =
1105 p->dma_read(IRQENABLE_L1, 0);
1106 omap_dma_global_context.dma_ocp_sysconfig =
1107 p->dma_read(OCP_SYSCONFIG, 0);
1108 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
1111 void omap_dma_global_context_restore(void)
1115 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1116 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
1118 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
1120 p->dma_write(omap_dma_global_context.dma_irqenable_l1,
1123 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
1124 p->dma_write(0x3 , IRQSTATUS_L0, 0);
1126 for (ch = 0; ch < dma_chan_count; ch++)
1127 if (dma_chan[ch].dev_id != -1)
1131 struct omap_system_dma_plat_info *omap_get_plat_info(void)
1135 EXPORT_SYMBOL_GPL(omap_get_plat_info);
1137 static int omap_system_dma_probe(struct platform_device *pdev)
1144 p = pdev->dev.platform_data;
1147 "%s: System DMA initialized without platform data\n",
1155 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
1156 && (omap_dma_reserve_channels < d->lch_count))
1157 d->lch_count = omap_dma_reserve_channels;
1159 dma_lch_count = d->lch_count;
1160 dma_chan_count = dma_lch_count;
1161 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
1163 dma_chan = devm_kcalloc(&pdev->dev, dma_lch_count,
1164 sizeof(*dma_chan), GFP_KERNEL);
1168 if (dma_omap2plus()) {
1169 dma_linked_lch = kcalloc(dma_lch_count,
1170 sizeof(*dma_linked_lch),
1172 if (!dma_linked_lch) {
1174 goto exit_dma_lch_fail;
1178 spin_lock_init(&dma_chan_lock);
1179 for (ch = 0; ch < dma_chan_count; ch++) {
1181 if (dma_omap2plus())
1182 omap2_disable_irq_lch(ch);
1184 dma_chan[ch].dev_id = -1;
1185 dma_chan[ch].next_lch = -1;
1187 if (ch >= 6 && enable_1510_mode)
1192 * request_irq() doesn't like dev_id (ie. ch) being
1193 * zero, so we have to kludge around this.
1195 sprintf(&irq_name[0], "%d", ch);
1196 dma_irq = platform_get_irq_byname(pdev, irq_name);
1200 goto exit_dma_irq_fail;
1203 /* INT_DMA_LCD is handled in lcd_dma.c */
1204 if (dma_irq == INT_DMA_LCD)
1207 ret = request_irq(dma_irq,
1208 omap1_dma_irq_handler, 0, "DMA",
1211 goto exit_dma_irq_fail;
1215 if (d->dev_caps & IS_RW_PRIORITY)
1216 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
1217 DMA_DEFAULT_FIFO_DEPTH, 0);
1219 if (dma_omap2plus() && !(d->dev_caps & DMA_ENGINE_HANDLE_IRQ)) {
1220 strcpy(irq_name, "0");
1221 dma_irq = platform_get_irq_byname(pdev, irq_name);
1223 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
1225 goto exit_dma_lch_fail;
1227 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
1229 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
1231 goto exit_dma_lch_fail;
1235 /* reserve dma channels 0 and 1 in high security devices on 34xx */
1236 if (d->dev_caps & HS_CHANNELS_RESERVED) {
1237 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
1238 dma_chan[0].dev_id = 0;
1239 dma_chan[1].dev_id = 1;
1245 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
1247 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
1248 dma_irq = platform_get_irq(pdev, irq_rel);
1249 free_irq(dma_irq, (void *)(irq_rel + 1));
1256 static int omap_system_dma_remove(struct platform_device *pdev)
1260 if (dma_omap2plus()) {
1262 strcpy(irq_name, "0");
1263 dma_irq = platform_get_irq_byname(pdev, irq_name);
1265 remove_irq(dma_irq, &omap24xx_dma_irq);
1268 for ( ; irq_rel < dma_chan_count; irq_rel++) {
1269 dma_irq = platform_get_irq(pdev, irq_rel);
1270 free_irq(dma_irq, (void *)(irq_rel + 1));
1276 static struct platform_driver omap_system_dma_driver = {
1277 .probe = omap_system_dma_probe,
1278 .remove = omap_system_dma_remove,
1280 .name = "omap_dma_system"
1284 static int __init omap_system_dma_init(void)
1286 return platform_driver_register(&omap_system_dma_driver);
1288 arch_initcall(omap_system_dma_init);
1290 static void __exit omap_system_dma_exit(void)
1292 platform_driver_unregister(&omap_system_dma_driver);
1295 MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
1296 MODULE_LICENSE("GPL");
1297 MODULE_AUTHOR("Texas Instruments Inc");
1300 * Reserve the omap SDMA channels using cmdline bootarg
1301 * "omap_dma_reserve_ch=". The valid range is 1 to 32
1303 static int __init omap_dma_cmdline_reserve_ch(char *str)
1305 if (get_option(&str, &omap_dma_reserve_channels) != 1)
1306 omap_dma_reserve_channels = 0;
1310 __setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);