blob: 1eb59003bdecdea32408afb842c800d94ca0d072 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002/*
3 * linux/arch/arm/plat-omap/dma.c
4 *
Tony Lindgren97b7f712008-07-03 12:24:37 +03005 * Copyright (C) 2003 - 2008 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02006 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01007 * 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>
Anand Gadiyarf8151e52007-12-01 12:14:11 -080010 * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000011 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010012 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
13 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070014 * Copyright (C) 2009 Texas Instruments
15 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
16 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010017 * Support functions for the OMAP internal DMA channels.
18 *
Alexander A. Klimove9dbeba2020-07-13 08:48:50 +020019 * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com/
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080020 * Converted DMA library into DMA platform driver.
21 * - G, Manjunath Kondaiah <manjugk@ti.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022 */
23
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>
Thomas Gleixner418ca1f02006-07-01 22:32:41 +010030#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030031#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -070033#include <linux/delay.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010034
Tony Lindgren45c3eb72012-11-30 08:41:50 -080035#include <linux/omap-dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036
Tony Lindgren685e2d02015-05-20 09:01:21 -070037#ifdef CONFIG_ARCH_OMAP1
38#include <mach/soc.h>
39#endif
40
Paul Walmsleybc4d8b52012-04-13 06:34:30 -060041/*
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
46 * DMA channels.)
47 */
48#define MAX_LOGICAL_DMA_CH_COUNT 32
49
Anand Gadiyarf8151e52007-12-01 12:14:11 -080050#undef DEBUG
51
52#ifndef CONFIG_ARCH_OMAP1
53enum { 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
55};
56
57enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000058#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059
Tony Lindgren97b7f712008-07-03 12:24:37 +030060#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020061#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062
Tony Lindgren97b7f712008-07-03 12:24:37 +030063#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010064
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080065static struct omap_system_dma_plat_info *p;
66static struct omap_dma_dev_attr *d;
Tony Lindgren175655b2014-09-16 17:36:28 -070067static void omap_clear_dma(int lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +030068static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080069static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070
Anand Gadiyarf8151e52007-12-01 12:14:11 -080071struct dma_link_info {
72 int *linked_dmach_q;
73 int no_of_lchs_linked;
74
75 int q_count;
76 int q_tail;
77 int q_head;
78
79 int chain_state;
80 int chain_mode;
81
82};
83
Tony Lindgren4d963722008-07-03 12:24:31 +030084static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010085static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -070086static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010087
88static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +030089static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010090
Anand Gadiyarf8151e52007-12-01 12:14:11 -080091static inline void disable_lnk(int lch);
92static void omap_disable_channel_irq(int lch);
93static inline void omap_enable_channel_irq(int lch);
94
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000095#ifdef CONFIG_ARCH_OMAP15XX
96/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -080097static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000098{
99 return enable_1510_mode;
100}
101#else
102#define omap_dma_in_1510_mode() 0
103#endif
104
105#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106static inline void set_gdma_dev(int req, int dev)
107{
108 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
109 int shift = ((req - 1) % 5) * 6;
110 u32 l;
111
112 l = omap_readl(reg);
113 l &= ~(0x3f << shift);
114 l |= (dev - 1) << shift;
115 omap_writel(l, reg);
116}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000117#else
118#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800119#define omap_readl(reg) 0
120#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000121#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122
Tony Lindgren54b693d2012-10-02 13:39:28 -0700123#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300124void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125{
126 unsigned long reg;
127 u32 l;
128
Tony Lindgren82809602012-10-30 11:03:22 -0700129 if (dma_omap1()) {
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300130 switch (dst_port) {
131 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
132 reg = OMAP_TC_OCPT1_PRIOR;
133 break;
134 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
135 reg = OMAP_TC_OCPT2_PRIOR;
136 break;
137 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
138 reg = OMAP_TC_EMIFF_PRIOR;
139 break;
140 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
141 reg = OMAP_TC_EMIFS_PRIOR;
142 break;
143 default:
144 BUG();
145 return;
146 }
147 l = omap_readl(reg);
148 l &= ~(0xf << 8);
149 l |= (priority & 0xf) << 8;
150 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152}
Tony Lindgren54b693d2012-10-02 13:39:28 -0700153#endif
154
155#ifdef CONFIG_ARCH_OMAP2PLUS
156void omap_set_dma_priority(int lch, int dst_port, int priority)
157{
158 u32 ccr;
159
160 ccr = p->dma_read(CCR, lch);
161 if (priority)
162 ccr |= (1 << 6);
163 else
164 ccr &= ~(1 << 6);
165 p->dma_write(ccr, CCR, lch);
166}
167#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300168EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100169
170void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000171 int frame_count, int sync_mode,
172 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100173{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300174 u32 l;
175
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800176 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300177 l &= ~0x03;
178 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800179 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100180
Tony Lindgren82809602012-10-30 11:03:22 -0700181 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300182 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100183
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800184 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300185 ccr &= ~(1 << 5);
186 if (sync_mode == OMAP_DMA_SYNC_FRAME)
187 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800188 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300189
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800190 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300191 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000192 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300193 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800194 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000195 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100196
Tony Lindgren82809602012-10-30 11:03:22 -0700197 if (dma_omap2plus() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300198 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800200 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200202 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300203 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200204 val |= (dma_trigger & ~0x1f) << 14;
205 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000206
207 if (sync_mode & OMAP_DMA_SYNC_FRAME)
208 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700209 else
210 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000211
212 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
213 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700214 else
215 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000216
Samu Onkalo72a11792010-08-02 14:21:40 +0300217 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000218 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300219 val |= (1 << 23); /* Prefetch */
220 } else if (src_or_dst_synch) {
221 val |= 1 << 24; /* source synch */
222 } else {
223 val &= ~(1 << 24); /* dest synch */
224 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800225 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000226 }
227
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800228 p->dma_write(elem_count, CEN, lch);
229 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100230}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300231EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000232
Tony Lindgren0499bde2008-07-03 12:24:36 +0300233void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
234{
Tony Lindgren82809602012-10-30 11:03:22 -0700235 if (dma_omap1() && !dma_omap15xx()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300236 u32 l;
237
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800238 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300239 l &= ~0x7;
240 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800241 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300242 }
243}
244EXPORT_SYMBOL(omap_set_dma_channel_mode);
245
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000246/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100247void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000248 unsigned long src_start,
249 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100250{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300251 u32 l;
252
Tony Lindgren82809602012-10-30 11:03:22 -0700253 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300254 u16 w;
255
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800256 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300257 w &= ~(0x1f << 2);
258 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800259 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300260 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300261
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800262 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300263 l &= ~(0x03 << 12);
264 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800265 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300266
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800267 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100268
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800269 p->dma_write(src_ei, CSEI, lch);
270 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300271}
272EXPORT_SYMBOL(omap_set_dma_src_params);
273
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100274void omap_set_dma_src_data_pack(int lch, int enable)
275{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300276 u32 l;
277
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800278 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300279 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000280 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300281 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800282 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300284EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285
286void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
287{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700288 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300289 u32 l;
290
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800291 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300292 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100293
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100294 switch (burst_mode) {
295 case OMAP_DMA_DATA_BURST_DIS:
296 break;
297 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700298 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700299 burst = 0x1;
300 else
301 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100302 break;
303 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700304 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700305 burst = 0x2;
306 break;
307 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700308 /*
309 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100310 * w |= (0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100311 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500312 fallthrough;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700313 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700314 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700315 burst = 0x3;
316 break;
317 }
Gustavo A. R. Silva3da6bd92019-07-28 18:19:41 -0500318 /* OMAP1 don't support burst 16 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500319 fallthrough;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320 default:
321 BUG();
322 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300323
324 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800325 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300327EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000329/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000331 unsigned long dest_start,
332 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100333{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300334 u32 l;
335
Tony Lindgren82809602012-10-30 11:03:22 -0700336 if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800337 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300338 l &= ~(0x1f << 9);
339 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800340 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000341 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800343 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300344 l &= ~(0x03 << 14);
345 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800346 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800348 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100349
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800350 p->dma_write(dst_ei, CDEI, lch);
351 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300353EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100354
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100355void omap_set_dma_dest_data_pack(int lch, int enable)
356{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300357 u32 l;
358
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800359 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300360 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000361 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300362 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800363 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300365EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366
367void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
368{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700369 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300370 u32 l;
371
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800372 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300373 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100375 switch (burst_mode) {
376 case OMAP_DMA_DATA_BURST_DIS:
377 break;
378 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700379 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700380 burst = 0x1;
381 else
382 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383 break;
384 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700385 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700386 burst = 0x2;
387 else
388 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700390 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700391 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700392 burst = 0x3;
393 break;
394 }
Gustavo A. R. Silva3da6bd92019-07-28 18:19:41 -0500395 /* OMAP1 don't support burst 16 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500396 fallthrough;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397 default:
398 printk(KERN_ERR "Invalid DMA burst mode\n");
399 BUG();
400 return;
401 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300402 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800403 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300405EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000407static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408{
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700409 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700410 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700411 p->dma_read(CSR, lch);
412 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800413 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000414
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100415 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800416 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417}
418
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700419static inline void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100420{
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700421 /* disable channel interrupts */
422 p->dma_write(0, CICR, lch);
423 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700424 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700425 p->dma_read(CSR, lch);
426 else
427 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428}
429
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100430void omap_disable_dma_irq(int lch, u16 bits)
431{
432 dma_chan[lch].enabled_irqs &= ~bits;
433}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300434EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100435
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000436static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300438 u32 l;
439
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800440 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300441
Tony Lindgren82809602012-10-30 11:03:22 -0700442 if (dma_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300443 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100444
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000445 /* Set the ENABLE_LNK bits */
446 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300447 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800448
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800449 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100450}
451
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000452static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300454 u32 l;
455
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800456 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300457
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000458 /* Disable interrupts */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700459 omap_disable_channel_irq(lch);
460
Tony Lindgren82809602012-10-30 11:03:22 -0700461 if (dma_omap1()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000462 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300463 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100464 }
465
Tony Lindgren82809602012-10-30 11:03:22 -0700466 if (dma_omap2plus()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000467 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300468 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000469 }
470
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800471 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000472 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
473}
474
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300476 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 void *data, int *dma_ch_out)
478{
479 int ch, free_ch = -1;
480 unsigned long flags;
481 struct omap_dma_lch *chan;
482
Russell King5c65c362014-06-07 10:47:36 +0100483 WARN(strcmp(dev_name, "DMA engine"), "Using deprecated platform DMA API - please update to DMA engine");
484
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485 spin_lock_irqsave(&dma_chan_lock, flags);
486 for (ch = 0; ch < dma_chan_count; ch++) {
487 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
488 free_ch = ch;
R Sricharan03a6d4a2013-06-13 19:47:09 +0530489 /* Exit after first free channel found */
490 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491 }
492 }
493 if (free_ch == -1) {
494 spin_unlock_irqrestore(&dma_chan_lock, flags);
495 return -EBUSY;
496 }
497 chan = dma_chan + free_ch;
498 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000499
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800500 if (p->clear_lch_regs)
501 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000502
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100503 spin_unlock_irqrestore(&dma_chan_lock, flags);
504
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505 chan->dev_name = dev_name;
506 chan->callback = callback;
507 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800508 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300509
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700510 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000511
Tony Lindgren82809602012-10-30 11:03:22 -0700512 if (dma_omap1())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700513 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100514
Tony Lindgren82809602012-10-30 11:03:22 -0700515 if (dma_omap16xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516 /* If the sync device is set, configure it dynamically. */
517 if (dev_id != 0) {
518 set_gdma_dev(free_ch + 1, dev_id);
519 dev_id = free_ch + 1;
520 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300521 /*
522 * Disable the 1510 compatibility mode and set the sync device
523 * id.
524 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800525 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Tony Lindgren82809602012-10-30 11:03:22 -0700526 } else if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800527 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100528 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000529
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530 *dma_ch_out = free_ch;
531
532 return 0;
533}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300534EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100535
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000536void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537{
538 unsigned long flags;
539
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000540 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300541 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000542 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543 return;
544 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300545
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700546 /* Disable all DMA interrupts for the channel. */
547 omap_disable_channel_irq(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000548
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700549 /* Make sure the DMA transfer is stopped. */
550 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000551
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700552 spin_lock_irqsave(&dma_chan_lock, flags);
553 dma_chan[lch].dev_id = -1;
554 dma_chan[lch].next_lch = -1;
555 dma_chan[lch].callback = NULL;
556 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300558EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000560/*
561 * Clears any DMA state so the DMA engine is ready to restart with new buffers
562 * through omap_start_dma(). Any buffers in flight are discarded.
563 */
Tony Lindgren175655b2014-09-16 17:36:28 -0700564static void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000566 unsigned long flags;
567
568 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800569 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000570 local_irq_restore(flags);
571}
572
573void omap_start_dma(int lch)
574{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300575 u32 l;
576
manjugk manjugk519e6162010-03-04 07:11:56 +0000577 /*
578 * The CPC/CDAC register needs to be initialized to zero
579 * before starting dma transfer.
580 */
Tony Lindgren82809602012-10-30 11:03:22 -0700581 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800582 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000583 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800584 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000585
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000586 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
587 int next_lch, cur_lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600588 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000589
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000590 /* Set the link register of the first channel */
591 enable_lnk(lch);
592
593 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
R Sricharanf0a3ff22013-06-13 19:47:10 +0530594 dma_chan_link_map[lch] = 1;
595
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000596 cur_lch = dma_chan[lch].next_lch;
597 do {
598 next_lch = dma_chan[cur_lch].next_lch;
599
600 /* The loop case: we've been here already */
601 if (dma_chan_link_map[cur_lch])
602 break;
603 /* Mark the current channel */
604 dma_chan_link_map[cur_lch] = 1;
605
606 enable_lnk(cur_lch);
607 omap_enable_channel_irq(cur_lch);
608
609 cur_lch = next_lch;
610 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800611 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800612 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000613
614 omap_enable_channel_irq(lch);
615
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800616 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300617
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800618 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
619 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300620 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800621
Russell King35453582012-04-14 18:57:10 +0100622 /*
623 * As dma_write() uses IO accessors which are weakly ordered, there
624 * is no guarantee that data in coherent DMA memory will be visible
625 * to the DMA device. Add a memory barrier here to ensure that any
626 * such data is visible prior to enabling DMA.
627 */
628 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800629 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000630
631 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
632}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300633EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000634
635void omap_stop_dma(int lch)
636{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300637 u32 l;
638
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700639 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700640 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700641
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800642 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800643 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
644 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700645 int i = 0;
646 u32 sys_cf;
647
648 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800649 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700650 sys_cf = l;
651 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
652 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800653 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700654
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800655 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700656 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800657 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700658
659 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800660 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700661 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
662 OMAP_DMA_CCR_WR_ACTIVE))) {
663 udelay(5);
664 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800665 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700666 }
667 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600668 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700669 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800670 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700671 } else {
672 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800673 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700674 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700675
Russell King35453582012-04-14 18:57:10 +0100676 /*
677 * Ensure that data transferred by DMA is visible to any access
678 * after DMA has been disabled. This is important for coherent
679 * DMA regions.
680 */
681 mb();
682
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000683 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
684 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600685 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000686
687 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
688 do {
689 /* The loop case: we've been here already */
690 if (dma_chan_link_map[cur_lch])
691 break;
692 /* Mark the current channel */
693 dma_chan_link_map[cur_lch] = 1;
694
695 disable_lnk(cur_lch);
696
697 next_lch = dma_chan[cur_lch].next_lch;
698 cur_lch = next_lch;
699 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000700 }
701
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000702 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
703}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300704EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000705
706/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300707 * Allows changing the DMA callback function or data. This may be needed if
708 * the driver shares a single DMA channel for multiple dma triggers.
709 */
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300710/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000711 * Returns current physical source address for the given DMA channel.
712 * If the channel is running the caller must disable interrupts prior calling
713 * this function and process the returned value before re-enabling interrupt to
714 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300715 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000716 * in incorrect return value.
717 */
718dma_addr_t omap_get_dma_src_pos(int lch)
719{
Tony Lindgren0695de32007-05-07 18:24:14 -0700720 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000721
Tony Lindgren82809602012-10-30 11:03:22 -0700722 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800723 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300724 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800725 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000726
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800727 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800728 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300729
Tony Lindgren82809602012-10-30 11:03:22 -0700730 if (!dma_omap15xx()) {
Peter Ujfalusi7ba96682011-12-09 13:38:00 -0800731 /*
732 * CDAC == 0 indicates that the DMA transfer on the channel has
733 * not been started (no data has been transferred so far).
734 * Return the programmed source start address in this case.
735 */
736 if (likely(p->dma_read(CDAC, lch)))
737 offset = p->dma_read(CSAC, lch);
738 else
739 offset = p->dma_read(CSSA, lch);
740 }
741
Tony Lindgren82809602012-10-30 11:03:22 -0700742 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800743 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000744
745 return offset;
746}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300747EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000748
749/*
750 * Returns current physical destination address for the given DMA channel.
751 * If the channel is running the caller must disable interrupts prior calling
752 * this function and process the returned value before re-enabling interrupt to
753 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300754 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000755 * in incorrect return value.
756 */
757dma_addr_t omap_get_dma_dst_pos(int lch)
758{
Tony Lindgren0695de32007-05-07 18:24:14 -0700759 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000760
Tony Lindgren82809602012-10-30 11:03:22 -0700761 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800762 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300763 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800764 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000765
Tony Lindgren0499bde2008-07-03 12:24:36 +0300766 /*
767 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
768 * read before the DMA controller finished disabling the channel.
769 */
Tony Lindgren82809602012-10-30 11:03:22 -0700770 if (!dma_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800771 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -0800772 /*
773 * CDAC == 0 indicates that the DMA transfer on the channel has
774 * not been started (no data has been transferred so far).
775 * Return the programmed destination start address in this case.
776 */
777 if (unlikely(!offset))
778 offset = p->dma_read(CDSA, lch);
779 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300780
Tony Lindgren82809602012-10-30 11:03:22 -0700781 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800782 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000783
784 return offset;
785}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300786EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000787
Tony Lindgren0499bde2008-07-03 12:24:36 +0300788int omap_get_dma_active_status(int lch)
789{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800790 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300791}
792EXPORT_SYMBOL(omap_get_dma_active_status);
793
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000794int omap_dma_running(void)
795{
796 int lch;
797
Tony Lindgren82809602012-10-30 11:03:22 -0700798 if (dma_omap1())
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -0800799 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000800 return 1;
801
802 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800803 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000804 return 1;
805
806 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100807}
808
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000809/*----------------------------------------------------------------------------*/
810
811#ifdef CONFIG_ARCH_OMAP1
812
813static int omap1_dma_handle_ch(int ch)
814{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300815 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000816
817 if (enable_1510_mode && ch >= 6) {
818 csr = dma_chan[ch].saved_csr;
819 dma_chan[ch].saved_csr = 0;
820 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800821 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000822 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
823 dma_chan[ch + 6].saved_csr = csr >> 7;
824 csr &= 0x7f;
825 }
826 if ((csr & 0x3f) == 0)
827 return 0;
828 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600829 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
830 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000831 return 0;
832 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700833 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -0600834 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000835 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -0600836 pr_warn("DMA synchronization event drop occurred with device %d\n",
837 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000838 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
839 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
840 if (likely(dma_chan[ch].callback != NULL))
841 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300842
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000843 return 1;
844}
845
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700846static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000847{
848 int ch = ((int) dev_id) - 1;
849 int handled = 0;
850
851 for (;;) {
852 int handled_now = 0;
853
854 handled_now += omap1_dma_handle_ch(ch);
855 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
856 handled_now += omap1_dma_handle_ch(ch + 6);
857 if (!handled_now)
858 break;
859 handled += handled_now;
860 }
861
862 return handled ? IRQ_HANDLED : IRQ_NONE;
863}
864
865#else
866#define omap1_dma_irq_handler NULL
867#endif
868
Russell King1b416c42013-11-02 13:00:03 +0000869struct omap_system_dma_plat_info *omap_get_plat_info(void)
870{
871 return p;
872}
873EXPORT_SYMBOL_GPL(omap_get_plat_info);
874
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800875static int omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800876{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800877 int ch, ret = 0;
878 int dma_irq;
879 char irq_name[4];
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800880
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800881 p = pdev->dev.platform_data;
882 if (!p) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600883 dev_err(&pdev->dev,
884 "%s: System DMA initialized without platform data\n",
885 __func__);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800886 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800887 }
888
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800889 d = p->dma_attr;
890 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800891
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800892 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Chen Gange78f96062013-01-11 13:39:18 +0800893 && (omap_dma_reserve_channels < d->lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800894 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700895
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800896 dma_lch_count = d->lch_count;
897 dma_chan_count = dma_lch_count;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800898 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +0300899
Russell King9834f812013-11-08 18:10:42 +0000900 dma_chan = devm_kcalloc(&pdev->dev, dma_lch_count,
Markus Elfring16e7ea52017-10-03 20:46:48 +0200901 sizeof(*dma_chan), GFP_KERNEL);
Markus Elfringd6799502017-10-03 13:10:26 +0200902 if (!dma_chan)
Russell King9834f812013-11-08 18:10:42 +0000903 return -ENOMEM;
Russell King9834f812013-11-08 18:10:42 +0000904
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100906 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000907 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700908
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100909 dma_chan[ch].dev_id = -1;
910 dma_chan[ch].next_lch = -1;
911
912 if (ch >= 6 && enable_1510_mode)
913 continue;
914
Tony Lindgren82809602012-10-30 11:03:22 -0700915 if (dma_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300916 /*
917 * request_irq() doesn't like dev_id (ie. ch) being
918 * zero, so we have to kludge around this.
919 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800920 sprintf(&irq_name[0], "%d", ch);
921 dma_irq = platform_get_irq_byname(pdev, irq_name);
922
923 if (dma_irq < 0) {
924 ret = dma_irq;
925 goto exit_dma_irq_fail;
926 }
927
928 /* INT_DMA_LCD is handled in lcd_dma.c */
929 if (dma_irq == INT_DMA_LCD)
930 continue;
931
932 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000933 omap1_dma_irq_handler, 0, "DMA",
934 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800935 if (ret != 0)
936 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000937 }
938 }
939
Tony Lindgren82809602012-10-30 11:03:22 -0700940 /* reserve dma channels 0 and 1 in high security devices on 34xx */
941 if (d->dev_caps & HS_CHANNELS_RESERVED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -0600942 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800943 dma_chan[0].dev_id = 0;
944 dma_chan[1].dev_id = 1;
945 }
946 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100947 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -0700948
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800949exit_dma_irq_fail:
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800950 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100951}
952
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800953static int omap_system_dma_remove(struct platform_device *pdev)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800954{
Tony Lindgren755cbfd2019-12-16 14:41:53 -0800955 int dma_irq, irq_rel = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800956
Tony Lindgren755cbfd2019-12-16 14:41:53 -0800957 if (dma_omap2plus())
958 return 0;
959
960 for ( ; irq_rel < dma_chan_count; irq_rel++) {
961 dma_irq = platform_get_irq(pdev, irq_rel);
962 free_irq(dma_irq, (void *)(irq_rel + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800963 }
Tony Lindgren755cbfd2019-12-16 14:41:53 -0800964
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800965 return 0;
966}
967
968static struct platform_driver omap_system_dma_driver = {
969 .probe = omap_system_dma_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800970 .remove = omap_system_dma_remove,
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800971 .driver = {
972 .name = "omap_dma_system"
973 },
974};
975
976static int __init omap_system_dma_init(void)
977{
978 return platform_driver_register(&omap_system_dma_driver);
979}
980arch_initcall(omap_system_dma_init);
981
982static void __exit omap_system_dma_exit(void)
983{
984 platform_driver_unregister(&omap_system_dma_driver);
985}
986
987MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
988MODULE_LICENSE("GPL");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800989MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700991/*
992 * Reserve the omap SDMA channels using cmdline bootarg
993 * "omap_dma_reserve_ch=". The valid range is 1 to 32
994 */
995static int __init omap_dma_cmdline_reserve_ch(char *str)
996{
997 if (get_option(&str, &omap_dma_reserve_channels) != 1)
998 omap_dma_reserve_channels = 0;
999 return 1;
1000}
1001
1002__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
1003
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004