blob: b5608b1f9fbdf5da32bf354643c1c842a755df83 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/dma.c
3 *
Tony Lindgren97b7f712008-07-03 12:24:37 +03004 * Copyright (C) 2003 - 2008 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 * 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>
Anand Gadiyarf8151e52007-12-01 12:14:11 -08009 * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000010 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010011 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
12 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070013 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
15 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016 * Support functions for the OMAP internal DMA channels.
17 *
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080018 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
19 * Converted DMA library into DMA platform driver.
20 * - G, Manjunath Kondaiah <manjugk@ti.com>
21 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/sched.h>
31#include <linux/spinlock.h>
32#include <linux/errno.h>
33#include <linux/interrupt.h>
Thomas Gleixner418ca1f02006-07-01 22:32:41 +010034#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030035#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -070037#include <linux/delay.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010038
Tony Lindgren45c3eb72012-11-30 08:41:50 -080039#include <linux/omap-dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010040
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;
67
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
Tero Kristof2d11852008-08-28 13:13:31 +000071static struct omap_dma_global_context_registers {
72 u32 dma_irqenable_l0;
Tony Lindgren9ce24822014-05-16 14:05:35 -070073 u32 dma_irqenable_l1;
Tero Kristof2d11852008-08-28 13:13:31 +000074 u32 dma_ocp_sysconfig;
75 u32 dma_gcr;
76} omap_dma_global_context;
77
Anand Gadiyarf8151e52007-12-01 12:14:11 -080078struct dma_link_info {
79 int *linked_dmach_q;
80 int no_of_lchs_linked;
81
82 int q_count;
83 int q_tail;
84 int q_head;
85
86 int chain_state;
87 int chain_mode;
88
89};
90
Tony Lindgren4d963722008-07-03 12:24:31 +030091static struct dma_link_info *dma_linked_lch;
92
93#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080094
95/* Chain handling macros */
96#define OMAP_DMA_CHAIN_QINIT(chain_id) \
97 do { \
98 dma_linked_lch[chain_id].q_head = \
99 dma_linked_lch[chain_id].q_tail = \
100 dma_linked_lch[chain_id].q_count = 0; \
101 } while (0)
102#define OMAP_DMA_CHAIN_QFULL(chain_id) \
103 (dma_linked_lch[chain_id].no_of_lchs_linked == \
104 dma_linked_lch[chain_id].q_count)
105#define OMAP_DMA_CHAIN_QLAST(chain_id) \
106 do { \
107 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
108 dma_linked_lch[chain_id].q_count) \
109 } while (0)
110#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
111 (0 == dma_linked_lch[chain_id].q_count)
112#define __OMAP_DMA_CHAIN_INCQ(end) \
113 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
114#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
115 do { \
116 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
117 dma_linked_lch[chain_id].q_count--; \
118 } while (0)
119
120#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
121 do { \
122 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
123 dma_linked_lch[chain_id].q_count++; \
124 } while (0)
125#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300126
127static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700129static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130
131static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300132static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800134static inline void disable_lnk(int lch);
135static void omap_disable_channel_irq(int lch);
136static inline void omap_enable_channel_irq(int lch);
137
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000138#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800139 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000140
141#ifdef CONFIG_ARCH_OMAP15XX
142/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800143static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000144{
145 return enable_1510_mode;
146}
147#else
148#define omap_dma_in_1510_mode() 0
149#endif
150
151#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100152static inline int get_gdma_dev(int req)
153{
154 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
155 int shift = ((req - 1) % 5) * 6;
156
157 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
158}
159
160static inline void set_gdma_dev(int req, int dev)
161{
162 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
163 int shift = ((req - 1) % 5) * 6;
164 u32 l;
165
166 l = omap_readl(reg);
167 l &= ~(0x3f << shift);
168 l |= (dev - 1) << shift;
169 omap_writel(l, reg);
170}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000171#else
172#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800173#define omap_readl(reg) 0
174#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000175#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100176
Tony Lindgren54b693d2012-10-02 13:39:28 -0700177#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300178void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100179{
180 unsigned long reg;
181 u32 l;
182
Tony Lindgren82809602012-10-30 11:03:22 -0700183 if (dma_omap1()) {
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300184 switch (dst_port) {
185 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
186 reg = OMAP_TC_OCPT1_PRIOR;
187 break;
188 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
189 reg = OMAP_TC_OCPT2_PRIOR;
190 break;
191 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
192 reg = OMAP_TC_EMIFF_PRIOR;
193 break;
194 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
195 reg = OMAP_TC_EMIFS_PRIOR;
196 break;
197 default:
198 BUG();
199 return;
200 }
201 l = omap_readl(reg);
202 l &= ~(0xf << 8);
203 l |= (priority & 0xf) << 8;
204 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206}
Tony Lindgren54b693d2012-10-02 13:39:28 -0700207#endif
208
209#ifdef CONFIG_ARCH_OMAP2PLUS
210void omap_set_dma_priority(int lch, int dst_port, int priority)
211{
212 u32 ccr;
213
214 ccr = p->dma_read(CCR, lch);
215 if (priority)
216 ccr |= (1 << 6);
217 else
218 ccr &= ~(1 << 6);
219 p->dma_write(ccr, CCR, lch);
220}
221#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300222EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100223
224void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000225 int frame_count, int sync_mode,
226 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100227{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300228 u32 l;
229
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800230 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300231 l &= ~0x03;
232 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800233 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234
Tony Lindgren82809602012-10-30 11:03:22 -0700235 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300236 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800238 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300239 ccr &= ~(1 << 5);
240 if (sync_mode == OMAP_DMA_SYNC_FRAME)
241 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800242 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300243
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800244 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300245 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000246 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300247 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800248 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000249 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100250
Tony Lindgren82809602012-10-30 11:03:22 -0700251 if (dma_omap2plus() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300252 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100253
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800254 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100255
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200256 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300257 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200258 val |= (dma_trigger & ~0x1f) << 14;
259 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000260
261 if (sync_mode & OMAP_DMA_SYNC_FRAME)
262 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700263 else
264 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000265
266 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
267 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700268 else
269 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000270
Samu Onkalo72a11792010-08-02 14:21:40 +0300271 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000272 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300273 val |= (1 << 23); /* Prefetch */
274 } else if (src_or_dst_synch) {
275 val |= 1 << 24; /* source synch */
276 } else {
277 val &= ~(1 << 24); /* dest synch */
278 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800279 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000280 }
281
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800282 p->dma_write(elem_count, CEN, lch);
283 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100284}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300285EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000286
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100287void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
288{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 BUG_ON(omap_dma_in_1510_mode());
290
Tony Lindgren82809602012-10-30 11:03:22 -0700291 if (dma_omap1()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700292 u16 w;
293
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800294 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700295 w &= ~0x03;
296
297 switch (mode) {
298 case OMAP_DMA_CONSTANT_FILL:
299 w |= 0x01;
300 break;
301 case OMAP_DMA_TRANSPARENT_COPY:
302 w |= 0x02;
303 break;
304 case OMAP_DMA_COLOR_DIS:
305 break;
306 default:
307 BUG();
308 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800309 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700310
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800311 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700312 w &= ~0x0f;
313 /* Default is channel type 2D */
314 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800315 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700316 w |= 1; /* Channel type G */
317 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800318 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700319 }
320
Tony Lindgren82809602012-10-30 11:03:22 -0700321 if (dma_omap2plus()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700322 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000323
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800324 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700325 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300326
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700327 switch (mode) {
328 case OMAP_DMA_CONSTANT_FILL:
329 val |= 1 << 16;
330 break;
331 case OMAP_DMA_TRANSPARENT_COPY:
332 val |= 1 << 17;
333 break;
334 case OMAP_DMA_COLOR_DIS:
335 break;
336 default:
337 BUG();
338 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800339 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700341 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800342 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300345EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300347void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
348{
Tony Lindgren82809602012-10-30 11:03:22 -0700349 if (dma_omap2plus()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300350 u32 csdp;
351
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800352 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300353 csdp &= ~(0x3 << 16);
354 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800355 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300356 }
357}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300358EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300359
Tony Lindgren0499bde2008-07-03 12:24:36 +0300360void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
361{
Tony Lindgren82809602012-10-30 11:03:22 -0700362 if (dma_omap1() && !dma_omap15xx()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300363 u32 l;
364
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800365 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300366 l &= ~0x7;
367 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800368 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300369 }
370}
371EXPORT_SYMBOL(omap_set_dma_channel_mode);
372
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000373/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000375 unsigned long src_start,
376 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300378 u32 l;
379
Tony Lindgren82809602012-10-30 11:03:22 -0700380 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300381 u16 w;
382
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800383 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300384 w &= ~(0x1f << 2);
385 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800386 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300387 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300388
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800389 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300390 l &= ~(0x03 << 12);
391 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800392 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300393
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800394 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800396 p->dma_write(src_ei, CSEI, lch);
397 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300398}
399EXPORT_SYMBOL(omap_set_dma_src_params);
400
401void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000402{
403 omap_set_dma_transfer_params(lch, params->data_type,
404 params->elem_count, params->frame_count,
405 params->sync_mode, params->trigger,
406 params->src_or_dst_synch);
407 omap_set_dma_src_params(lch, params->src_port,
408 params->src_amode, params->src_start,
409 params->src_ei, params->src_fi);
410
411 omap_set_dma_dest_params(lch, params->dst_port,
412 params->dst_amode, params->dst_start,
413 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800414 if (params->read_prio || params->write_prio)
415 omap_dma_set_prio_lch(lch, params->read_prio,
416 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300418EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419
420void omap_set_dma_src_index(int lch, int eidx, int fidx)
421{
Tony Lindgren82809602012-10-30 11:03:22 -0700422 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000423 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300424
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800425 p->dma_write(eidx, CSEI, lch);
426 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300428EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429
430void omap_set_dma_src_data_pack(int lch, int enable)
431{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300432 u32 l;
433
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800434 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300435 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000436 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300437 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800438 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100439}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300440EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441
442void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
443{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700444 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300445 u32 l;
446
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800447 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300448 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100450 switch (burst_mode) {
451 case OMAP_DMA_DATA_BURST_DIS:
452 break;
453 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700454 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700455 burst = 0x1;
456 else
457 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100458 break;
459 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700460 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700461 burst = 0x2;
462 break;
463 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700464 /*
465 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100466 * w |= (0x03 << 7);
467 * fall through
468 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700469 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700470 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700471 burst = 0x3;
472 break;
473 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700474 /*
475 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700476 * fall through
477 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 default:
479 BUG();
480 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300481
482 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800483 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100484}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300485EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000487/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000489 unsigned long dest_start,
490 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300492 u32 l;
493
Tony Lindgren82809602012-10-30 11:03:22 -0700494 if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800495 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300496 l &= ~(0x1f << 9);
497 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800498 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000499 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800501 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300502 l &= ~(0x03 << 14);
503 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800504 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800506 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800508 p->dma_write(dst_ei, CDEI, lch);
509 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300511EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512
513void omap_set_dma_dest_index(int lch, int eidx, int fidx)
514{
Tony Lindgren82809602012-10-30 11:03:22 -0700515 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000516 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300517
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800518 p->dma_write(eidx, CDEI, lch);
519 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300521EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522
523void omap_set_dma_dest_data_pack(int lch, int enable)
524{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300525 u32 l;
526
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800527 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300528 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000529 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300530 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800531 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100532}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300533EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534
535void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
536{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700537 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300538 u32 l;
539
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800540 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300541 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543 switch (burst_mode) {
544 case OMAP_DMA_DATA_BURST_DIS:
545 break;
546 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700547 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700548 burst = 0x1;
549 else
550 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100551 break;
552 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700553 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700554 burst = 0x2;
555 else
556 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100557 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700558 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700559 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700560 burst = 0x3;
561 break;
562 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700563 /*
564 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700565 * fall through
566 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 default:
568 printk(KERN_ERR "Invalid DMA burst mode\n");
569 BUG();
570 return;
571 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300572 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800573 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300575EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000577static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578{
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700579 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700580 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700581 p->dma_read(CSR, lch);
582 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800583 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000584
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100585 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800586 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587}
588
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700589static inline void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100590{
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700591 /* disable channel interrupts */
592 p->dma_write(0, CICR, lch);
593 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700594 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700595 p->dma_read(CSR, lch);
596 else
597 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100598}
599
600void omap_enable_dma_irq(int lch, u16 bits)
601{
602 dma_chan[lch].enabled_irqs |= bits;
603}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300604EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605
606void omap_disable_dma_irq(int lch, u16 bits)
607{
608 dma_chan[lch].enabled_irqs &= ~bits;
609}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300610EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100611
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000612static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300614 u32 l;
615
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800616 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300617
Tony Lindgren82809602012-10-30 11:03:22 -0700618 if (dma_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300619 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000621 /* Set the ENABLE_LNK bits */
622 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300623 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800624
625#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren82809602012-10-30 11:03:22 -0700626 if (dma_omap2plus())
Tony Lindgren97b7f712008-07-03 12:24:37 +0300627 if (dma_chan[lch].next_linked_ch != -1)
628 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800629#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300630
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800631 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632}
633
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000634static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100635{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300636 u32 l;
637
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800638 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300639
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000640 /* Disable interrupts */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700641 omap_disable_channel_irq(lch);
642
Tony Lindgren82809602012-10-30 11:03:22 -0700643 if (dma_omap1()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000644 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300645 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100646 }
647
Tony Lindgren82809602012-10-30 11:03:22 -0700648 if (dma_omap2plus()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000649 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300650 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000651 }
652
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800653 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000654 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
655}
656
657static inline void omap2_enable_irq_lch(int lch)
658{
659 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800660 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000661
Tony Lindgren82809602012-10-30 11:03:22 -0700662 if (dma_omap1())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000663 return;
664
Tao Huee907322009-11-10 18:55:17 -0800665 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700666 /* clear IRQ STATUS */
667 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
668 /* Enable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800669 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000670 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800671 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800672 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673}
674
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700675static inline void omap2_disable_irq_lch(int lch)
676{
677 u32 val;
678 unsigned long flags;
679
Tony Lindgren82809602012-10-30 11:03:22 -0700680 if (dma_omap1())
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700681 return;
682
683 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700684 /* Disable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800685 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700686 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800687 p->dma_write(val, IRQENABLE_L0, lch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700688 /* clear IRQ STATUS */
689 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700690 spin_unlock_irqrestore(&dma_chan_lock, flags);
691}
692
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300694 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695 void *data, int *dma_ch_out)
696{
697 int ch, free_ch = -1;
698 unsigned long flags;
699 struct omap_dma_lch *chan;
700
701 spin_lock_irqsave(&dma_chan_lock, flags);
702 for (ch = 0; ch < dma_chan_count; ch++) {
703 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
704 free_ch = ch;
R Sricharan03a6d4a2013-06-13 19:47:09 +0530705 /* Exit after first free channel found */
706 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707 }
708 }
709 if (free_ch == -1) {
710 spin_unlock_irqrestore(&dma_chan_lock, flags);
711 return -EBUSY;
712 }
713 chan = dma_chan + free_ch;
714 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000715
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800716 if (p->clear_lch_regs)
717 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000718
Tony Lindgren82809602012-10-30 11:03:22 -0700719 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000720 omap_clear_dma(free_ch);
721
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722 spin_unlock_irqrestore(&dma_chan_lock, flags);
723
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100724 chan->dev_name = dev_name;
725 chan->callback = callback;
726 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800727 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300728
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800729#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren82809602012-10-30 11:03:22 -0700730 if (dma_omap2plus()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300731 chan->chain_id = -1;
732 chan->next_linked_ch = -1;
733 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800734#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300735
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700736 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000737
Tony Lindgren82809602012-10-30 11:03:22 -0700738 if (dma_omap1())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700739 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Tony Lindgren82809602012-10-30 11:03:22 -0700740 else if (dma_omap2plus())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700741 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
742 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100743
Tony Lindgren82809602012-10-30 11:03:22 -0700744 if (dma_omap16xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 /* If the sync device is set, configure it dynamically. */
746 if (dev_id != 0) {
747 set_gdma_dev(free_ch + 1, dev_id);
748 dev_id = free_ch + 1;
749 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300750 /*
751 * Disable the 1510 compatibility mode and set the sync device
752 * id.
753 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800754 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Tony Lindgren82809602012-10-30 11:03:22 -0700755 } else if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800756 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100757 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000758
Tony Lindgren82809602012-10-30 11:03:22 -0700759 if (dma_omap2plus()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000760 omap_enable_channel_irq(free_ch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700761 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000762 }
763
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764 *dma_ch_out = free_ch;
765
766 return 0;
767}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300768EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000770void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771{
772 unsigned long flags;
773
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300775 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000776 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777 return;
778 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300779
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700780 /* Disable interrupt for logical channel */
Tony Lindgren82809602012-10-30 11:03:22 -0700781 if (dma_omap2plus())
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700782 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000783
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700784 /* Disable all DMA interrupts for the channel. */
785 omap_disable_channel_irq(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000786
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700787 /* Make sure the DMA transfer is stopped. */
788 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000789
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700790 /* Clear registers */
Tony Lindgren82809602012-10-30 11:03:22 -0700791 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000792 omap_clear_dma(lch);
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700793
794 spin_lock_irqsave(&dma_chan_lock, flags);
795 dma_chan[lch].dev_id = -1;
796 dma_chan[lch].next_lch = -1;
797 dma_chan[lch].callback = NULL;
798 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300800EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800802/**
803 * @brief omap_dma_set_global_params : Set global priority settings for dma
804 *
805 * @param arb_rate
806 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700807 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
808 * DMA_THREAD_RESERVE_ONET
809 * DMA_THREAD_RESERVE_TWOT
810 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800811 */
812void
813omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
814{
815 u32 reg;
816
Tony Lindgren82809602012-10-30 11:03:22 -0700817 if (dma_omap1()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800818 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800819 return;
820 }
821
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700822 if (max_fifo_depth == 0)
823 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800824 if (arb_rate == 0)
825 arb_rate = 1;
826
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700827 reg = 0xff & max_fifo_depth;
828 reg |= (0x3 & tparams) << 12;
829 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800830
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800831 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800832}
833EXPORT_SYMBOL(omap_dma_set_global_params);
834
835/**
836 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
837 *
838 * @param lch
839 * @param read_prio - Read priority
840 * @param write_prio - Write priority
841 * Both of the above can be set with one of the following values :
842 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
843 */
844int
845omap_dma_set_prio_lch(int lch, unsigned char read_prio,
846 unsigned char write_prio)
847{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300848 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800849
Tony Lindgren4d963722008-07-03 12:24:31 +0300850 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800851 printk(KERN_ERR "Invalid channel id\n");
852 return -EINVAL;
853 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800854 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300855 l &= ~((1 << 6) | (1 << 26));
Tony Lindgren82809602012-10-30 11:03:22 -0700856 if (d->dev_caps & IS_RW_PRIORITY)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300857 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800858 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300859 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800860
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800861 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300862
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800863 return 0;
864}
865EXPORT_SYMBOL(omap_dma_set_prio_lch);
866
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000867/*
868 * Clears any DMA state so the DMA engine is ready to restart with new buffers
869 * through omap_start_dma(). Any buffers in flight are discarded.
870 */
871void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000873 unsigned long flags;
874
875 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800876 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000877 local_irq_restore(flags);
878}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300879EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000880
881void omap_start_dma(int lch)
882{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300883 u32 l;
884
manjugk manjugk519e6162010-03-04 07:11:56 +0000885 /*
886 * The CPC/CDAC register needs to be initialized to zero
887 * before starting dma transfer.
888 */
Tony Lindgren82809602012-10-30 11:03:22 -0700889 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800890 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000891 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800892 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000893
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000894 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
895 int next_lch, cur_lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600896 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000897
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000898 /* Set the link register of the first channel */
899 enable_lnk(lch);
900
901 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
R Sricharanf0a3ff22013-06-13 19:47:10 +0530902 dma_chan_link_map[lch] = 1;
903
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000904 cur_lch = dma_chan[lch].next_lch;
905 do {
906 next_lch = dma_chan[cur_lch].next_lch;
907
908 /* The loop case: we've been here already */
909 if (dma_chan_link_map[cur_lch])
910 break;
911 /* Mark the current channel */
912 dma_chan_link_map[cur_lch] = 1;
913
914 enable_lnk(cur_lch);
915 omap_enable_channel_irq(cur_lch);
916
917 cur_lch = next_lch;
918 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800919 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800920 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000921
922 omap_enable_channel_irq(lch);
923
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800924 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300925
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800926 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
927 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300928 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800929
Russell King35453582012-04-14 18:57:10 +0100930 /*
931 * As dma_write() uses IO accessors which are weakly ordered, there
932 * is no guarantee that data in coherent DMA memory will be visible
933 * to the DMA device. Add a memory barrier here to ensure that any
934 * such data is visible prior to enabling DMA.
935 */
936 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800937 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000938
939 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
940}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300941EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000942
943void omap_stop_dma(int lch)
944{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300945 u32 l;
946
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700947 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700948 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700949
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800950 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800951 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
952 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700953 int i = 0;
954 u32 sys_cf;
955
956 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800957 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700958 sys_cf = l;
959 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
960 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800961 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700962
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800963 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700964 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800965 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700966
967 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800968 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700969 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
970 OMAP_DMA_CCR_WR_ACTIVE))) {
971 udelay(5);
972 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800973 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700974 }
975 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600976 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700977 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800978 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700979 } else {
980 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800981 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700982 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700983
Russell King35453582012-04-14 18:57:10 +0100984 /*
985 * Ensure that data transferred by DMA is visible to any access
986 * after DMA has been disabled. This is important for coherent
987 * DMA regions.
988 */
989 mb();
990
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000991 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
992 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600993 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000994
995 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
996 do {
997 /* The loop case: we've been here already */
998 if (dma_chan_link_map[cur_lch])
999 break;
1000 /* Mark the current channel */
1001 dma_chan_link_map[cur_lch] = 1;
1002
1003 disable_lnk(cur_lch);
1004
1005 next_lch = dma_chan[cur_lch].next_lch;
1006 cur_lch = next_lch;
1007 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001008 }
1009
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001010 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1011}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001012EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001013
1014/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001015 * Allows changing the DMA callback function or data. This may be needed if
1016 * the driver shares a single DMA channel for multiple dma triggers.
1017 */
1018int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001019 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001020 void *data)
1021{
1022 unsigned long flags;
1023
1024 if (lch < 0)
1025 return -ENODEV;
1026
1027 spin_lock_irqsave(&dma_chan_lock, flags);
1028 if (dma_chan[lch].dev_id == -1) {
1029 printk(KERN_ERR "DMA callback for not set for free channel\n");
1030 spin_unlock_irqrestore(&dma_chan_lock, flags);
1031 return -EINVAL;
1032 }
1033 dma_chan[lch].callback = callback;
1034 dma_chan[lch].data = data;
1035 spin_unlock_irqrestore(&dma_chan_lock, flags);
1036
1037 return 0;
1038}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001039EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001040
1041/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001042 * Returns current physical source address for the given DMA channel.
1043 * If the channel is running the caller must disable interrupts prior calling
1044 * this function and process the returned value before re-enabling interrupt to
1045 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001046 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001047 * in incorrect return value.
1048 */
1049dma_addr_t omap_get_dma_src_pos(int lch)
1050{
Tony Lindgren0695de32007-05-07 18:24:14 -07001051 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001052
Tony Lindgren82809602012-10-30 11:03:22 -07001053 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001054 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001055 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001056 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001057
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001058 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001059 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001060
Tony Lindgren82809602012-10-30 11:03:22 -07001061 if (!dma_omap15xx()) {
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001062 /*
1063 * CDAC == 0 indicates that the DMA transfer on the channel has
1064 * not been started (no data has been transferred so far).
1065 * Return the programmed source start address in this case.
1066 */
1067 if (likely(p->dma_read(CDAC, lch)))
1068 offset = p->dma_read(CSAC, lch);
1069 else
1070 offset = p->dma_read(CSSA, lch);
1071 }
1072
Tony Lindgren82809602012-10-30 11:03:22 -07001073 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001074 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001075
1076 return offset;
1077}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001078EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001079
1080/*
1081 * Returns current physical destination address for the given DMA channel.
1082 * If the channel is running the caller must disable interrupts prior calling
1083 * this function and process the returned value before re-enabling interrupt to
1084 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001085 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001086 * in incorrect return value.
1087 */
1088dma_addr_t omap_get_dma_dst_pos(int lch)
1089{
Tony Lindgren0695de32007-05-07 18:24:14 -07001090 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001091
Tony Lindgren82809602012-10-30 11:03:22 -07001092 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001093 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001094 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001095 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001096
Tony Lindgren0499bde2008-07-03 12:24:36 +03001097 /*
1098 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1099 * read before the DMA controller finished disabling the channel.
1100 */
Tony Lindgren82809602012-10-30 11:03:22 -07001101 if (!dma_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001102 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001103 /*
1104 * CDAC == 0 indicates that the DMA transfer on the channel has
1105 * not been started (no data has been transferred so far).
1106 * Return the programmed destination start address in this case.
1107 */
1108 if (unlikely(!offset))
1109 offset = p->dma_read(CDSA, lch);
1110 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001111
Tony Lindgren82809602012-10-30 11:03:22 -07001112 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001113 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001114
1115 return offset;
1116}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001117EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001118
Tony Lindgren0499bde2008-07-03 12:24:36 +03001119int omap_get_dma_active_status(int lch)
1120{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001121 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001122}
1123EXPORT_SYMBOL(omap_get_dma_active_status);
1124
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001125int omap_dma_running(void)
1126{
1127 int lch;
1128
Tony Lindgren82809602012-10-30 11:03:22 -07001129 if (dma_omap1())
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001130 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001131 return 1;
1132
1133 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001134 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001135 return 1;
1136
1137 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001138}
1139
1140/*
1141 * lch_queue DMA will start right after lch_head one is finished.
1142 * For this DMA link to start, you still need to start (see omap_start_dma)
1143 * the first one. That will fire up the entire queue.
1144 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001145void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001146{
1147 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001148 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001149 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001150 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001151 return;
1152 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001153 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1154 BUG();
1155 return;
1156 }
1157
1158 if ((dma_chan[lch_head].dev_id == -1) ||
1159 (dma_chan[lch_queue].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001160 pr_err("omap_dma: trying to link non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001161 dump_stack();
1162 }
1163
1164 dma_chan[lch_head].next_lch = lch_queue;
1165}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001166EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001167
1168/*
1169 * Once the DMA queue is stopped, we can destroy it.
1170 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001171void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001172{
1173 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001174 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001175 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001176 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001177 return;
1178 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001179 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1180 BUG();
1181 return;
1182 }
1183
1184 if (dma_chan[lch_head].next_lch != lch_queue ||
1185 dma_chan[lch_head].next_lch == -1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001186 pr_err("omap_dma: trying to unlink non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001187 dump_stack();
1188 }
1189
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001190 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001191 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001192 pr_err("omap_dma: You need to stop the DMA channels before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001193 dump_stack();
1194 }
1195
1196 dma_chan[lch_head].next_lch = -1;
1197}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001198EXPORT_SYMBOL(omap_dma_unlink_lch);
1199
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001200#ifndef CONFIG_ARCH_OMAP1
1201/* Create chain of DMA channesls */
1202static void create_dma_lch_chain(int lch_head, int lch_queue)
1203{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001204 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001205
1206 /* Check if this is the first link in chain */
1207 if (dma_chan[lch_head].next_linked_ch == -1) {
1208 dma_chan[lch_head].next_linked_ch = lch_queue;
1209 dma_chan[lch_head].prev_linked_ch = lch_queue;
1210 dma_chan[lch_queue].next_linked_ch = lch_head;
1211 dma_chan[lch_queue].prev_linked_ch = lch_head;
1212 }
1213
1214 /* a link exists, link the new channel in circular chain */
1215 else {
1216 dma_chan[lch_queue].next_linked_ch =
1217 dma_chan[lch_head].next_linked_ch;
1218 dma_chan[lch_queue].prev_linked_ch = lch_head;
1219 dma_chan[lch_head].next_linked_ch = lch_queue;
1220 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1221 lch_queue;
1222 }
1223
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001224 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001225 l &= ~(0x1f);
1226 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001227 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001228
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001229 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001230 l &= ~(0x1f);
1231 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001232 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001233}
1234
1235/**
1236 * @brief omap_request_dma_chain : Request a chain of DMA channels
1237 *
1238 * @param dev_id - Device id using the dma channel
1239 * @param dev_name - Device name
1240 * @param callback - Call back function
1241 * @chain_id -
1242 * @no_of_chans - Number of channels requested
1243 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1244 * OMAP_DMA_DYNAMIC_CHAIN
1245 * @params - Channel parameters
1246 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001247 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001248 * Failure: -EINVAL/-ENOMEM
1249 */
1250int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001251 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001252 void *data),
1253 int *chain_id, int no_of_chans, int chain_mode,
1254 struct omap_dma_channel_params params)
1255{
1256 int *channels;
1257 int i, err;
1258
1259 /* Is the chain mode valid ? */
1260 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1261 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1262 printk(KERN_ERR "Invalid chain mode requested\n");
1263 return -EINVAL;
1264 }
1265
1266 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001267 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001268 printk(KERN_ERR "Invalid Number of channels requested\n");
1269 return -EINVAL;
1270 }
1271
manjugk manjugkea221a62010-05-14 12:05:25 -07001272 /*
1273 * Allocate a queue to maintain the status of the channels
1274 * in the chain
1275 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001276 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1277 if (channels == NULL) {
1278 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1279 return -ENOMEM;
1280 }
1281
1282 /* request and reserve DMA channels for the chain */
1283 for (i = 0; i < no_of_chans; i++) {
1284 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001285 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001286 if (err < 0) {
1287 int j;
1288 for (j = 0; j < i; j++)
1289 omap_free_dma(channels[j]);
1290 kfree(channels);
1291 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1292 return err;
1293 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001294 dma_chan[channels[i]].prev_linked_ch = -1;
1295 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1296
1297 /*
1298 * Allowing client drivers to set common parameters now,
1299 * so that later only relevant (src_start, dest_start
1300 * and element count) can be set
1301 */
1302 omap_set_dma_params(channels[i], &params);
1303 }
1304
1305 *chain_id = channels[0];
1306 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1307 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1308 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1309 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1310
1311 for (i = 0; i < no_of_chans; i++)
1312 dma_chan[channels[i]].chain_id = *chain_id;
1313
1314 /* Reset the Queue pointers */
1315 OMAP_DMA_CHAIN_QINIT(*chain_id);
1316
1317 /* Set up the chain */
1318 if (no_of_chans == 1)
1319 create_dma_lch_chain(channels[0], channels[0]);
1320 else {
1321 for (i = 0; i < (no_of_chans - 1); i++)
1322 create_dma_lch_chain(channels[i], channels[i + 1]);
1323 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001324
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001325 return 0;
1326}
1327EXPORT_SYMBOL(omap_request_dma_chain);
1328
1329/**
1330 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1331 * params after setting it. Dont do this while dma is running!!
1332 *
1333 * @param chain_id - Chained logical channel id.
1334 * @param params
1335 *
1336 * @return - Success : 0
1337 * Failure : -EINVAL
1338 */
1339int omap_modify_dma_chain_params(int chain_id,
1340 struct omap_dma_channel_params params)
1341{
1342 int *channels;
1343 u32 i;
1344
1345 /* Check for input params */
1346 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001347 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001348 printk(KERN_ERR "Invalid chain id\n");
1349 return -EINVAL;
1350 }
1351
1352 /* Check if the chain exists */
1353 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1354 printk(KERN_ERR "Chain doesn't exists\n");
1355 return -EINVAL;
1356 }
1357 channels = dma_linked_lch[chain_id].linked_dmach_q;
1358
1359 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1360 /*
1361 * Allowing client drivers to set common parameters now,
1362 * so that later only relevant (src_start, dest_start
1363 * and element count) can be set
1364 */
1365 omap_set_dma_params(channels[i], &params);
1366 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001367
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001368 return 0;
1369}
1370EXPORT_SYMBOL(omap_modify_dma_chain_params);
1371
1372/**
1373 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1374 *
1375 * @param chain_id
1376 *
1377 * @return - Success : 0
1378 * Failure : -EINVAL
1379 */
1380int omap_free_dma_chain(int chain_id)
1381{
1382 int *channels;
1383 u32 i;
1384
1385 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001386 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001387 printk(KERN_ERR "Invalid chain id\n");
1388 return -EINVAL;
1389 }
1390
1391 /* Check if the chain exists */
1392 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1393 printk(KERN_ERR "Chain doesn't exists\n");
1394 return -EINVAL;
1395 }
1396
1397 channels = dma_linked_lch[chain_id].linked_dmach_q;
1398 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1399 dma_chan[channels[i]].next_linked_ch = -1;
1400 dma_chan[channels[i]].prev_linked_ch = -1;
1401 dma_chan[channels[i]].chain_id = -1;
1402 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1403 omap_free_dma(channels[i]);
1404 }
1405
1406 kfree(channels);
1407
1408 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1409 dma_linked_lch[chain_id].chain_mode = -1;
1410 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001411
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001412 return (0);
1413}
1414EXPORT_SYMBOL(omap_free_dma_chain);
1415
1416/**
1417 * @brief omap_dma_chain_status - Check if the chain is in
1418 * active / inactive state.
1419 * @param chain_id
1420 *
1421 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1422 * Failure : -EINVAL
1423 */
1424int omap_dma_chain_status(int chain_id)
1425{
1426 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001427 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001428 printk(KERN_ERR "Invalid chain id\n");
1429 return -EINVAL;
1430 }
1431
1432 /* Check if the chain exists */
1433 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1434 printk(KERN_ERR "Chain doesn't exists\n");
1435 return -EINVAL;
1436 }
1437 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1438 dma_linked_lch[chain_id].q_count);
1439
1440 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1441 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001442
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001443 return OMAP_DMA_CHAIN_ACTIVE;
1444}
1445EXPORT_SYMBOL(omap_dma_chain_status);
1446
1447/**
1448 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1449 * set the params and start the transfer.
1450 *
1451 * @param chain_id
1452 * @param src_start - buffer start address
1453 * @param dest_start - Dest address
1454 * @param elem_count
1455 * @param frame_count
1456 * @param callbk_data - channel callback parameter data.
1457 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301458 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001459 * Failure: -EINVAL/-EBUSY
1460 */
1461int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1462 int elem_count, int frame_count, void *callbk_data)
1463{
1464 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001465 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001466 int start_dma = 0;
1467
Tony Lindgren97b7f712008-07-03 12:24:37 +03001468 /*
1469 * if buffer size is less than 1 then there is
1470 * no use of starting the chain
1471 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001472 if (elem_count < 1) {
1473 printk(KERN_ERR "Invalid buffer size\n");
1474 return -EINVAL;
1475 }
1476
1477 /* Check for input params */
1478 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001479 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001480 printk(KERN_ERR "Invalid chain id\n");
1481 return -EINVAL;
1482 }
1483
1484 /* Check if the chain exists */
1485 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1486 printk(KERN_ERR "Chain doesn't exist\n");
1487 return -EINVAL;
1488 }
1489
1490 /* Check if all the channels in chain are in use */
1491 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1492 return -EBUSY;
1493
1494 /* Frame count may be negative in case of indexed transfers */
1495 channels = dma_linked_lch[chain_id].linked_dmach_q;
1496
1497 /* Get a free channel */
1498 lch = channels[dma_linked_lch[chain_id].q_tail];
1499
1500 /* Store the callback data */
1501 dma_chan[lch].data = callbk_data;
1502
1503 /* Increment the q_tail */
1504 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1505
1506 /* Set the params to the free channel */
1507 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001508 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001509 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001510 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001511
1512 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001513 p->dma_write(elem_count, CEN, lch);
1514 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001515
Tony Lindgren97b7f712008-07-03 12:24:37 +03001516 /*
1517 * If the chain is dynamically linked,
1518 * then we may have to start the chain if its not active
1519 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001520 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1521
Tony Lindgren97b7f712008-07-03 12:24:37 +03001522 /*
1523 * In Dynamic chain, if the chain is not started,
1524 * queue the channel
1525 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001526 if (dma_linked_lch[chain_id].chain_state ==
1527 DMA_CHAIN_NOTSTARTED) {
1528 /* Enable the link in previous channel */
1529 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1530 DMA_CH_QUEUED)
1531 enable_lnk(dma_chan[lch].prev_linked_ch);
1532 dma_chan[lch].state = DMA_CH_QUEUED;
1533 }
1534
Tony Lindgren97b7f712008-07-03 12:24:37 +03001535 /*
1536 * Chain is already started, make sure its active,
1537 * if not then start the chain
1538 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001539 else {
1540 start_dma = 1;
1541
1542 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1543 DMA_CH_STARTED) {
1544 enable_lnk(dma_chan[lch].prev_linked_ch);
1545 dma_chan[lch].state = DMA_CH_QUEUED;
1546 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001547 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001548 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001549 disable_lnk(dma_chan[lch].
1550 prev_linked_ch);
1551 pr_debug("\n prev ch is stopped\n");
1552 start_dma = 1;
1553 }
1554 }
1555
1556 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1557 == DMA_CH_QUEUED) {
1558 enable_lnk(dma_chan[lch].prev_linked_ch);
1559 dma_chan[lch].state = DMA_CH_QUEUED;
1560 start_dma = 0;
1561 }
1562 omap_enable_channel_irq(lch);
1563
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001564 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001565
Tony Lindgren0499bde2008-07-03 12:24:36 +03001566 if ((0 == (l & (1 << 24))))
1567 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001568 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001569 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001570 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001571 if (0 == (l & (1 << 7))) {
1572 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001573 dma_chan[lch].state = DMA_CH_STARTED;
1574 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001575 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001576 } else
1577 start_dma = 0;
1578 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001579 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001580 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001581 }
1582 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1583 }
1584 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001585
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301586 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001587}
1588EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1589
1590/**
1591 * @brief omap_start_dma_chain_transfers - Start the chain
1592 *
1593 * @param chain_id
1594 *
1595 * @return - Success : 0
1596 * Failure : -EINVAL/-EBUSY
1597 */
1598int omap_start_dma_chain_transfers(int chain_id)
1599{
1600 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001601 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001602
Tony Lindgren4d963722008-07-03 12:24:31 +03001603 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001604 printk(KERN_ERR "Invalid chain id\n");
1605 return -EINVAL;
1606 }
1607
1608 channels = dma_linked_lch[chain_id].linked_dmach_q;
1609
1610 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1611 printk(KERN_ERR "Chain is already started\n");
1612 return -EBUSY;
1613 }
1614
1615 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1616 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1617 i++) {
1618 enable_lnk(channels[i]);
1619 omap_enable_channel_irq(channels[i]);
1620 }
1621 } else {
1622 omap_enable_channel_irq(channels[0]);
1623 }
1624
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001625 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001626 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001627 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1628 dma_chan[channels[0]].state = DMA_CH_STARTED;
1629
Tony Lindgren0499bde2008-07-03 12:24:36 +03001630 if ((0 == (l & (1 << 24))))
1631 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001632 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001633 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001634 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001635
1636 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001637
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001638 return 0;
1639}
1640EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1641
1642/**
1643 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1644 *
1645 * @param chain_id
1646 *
1647 * @return - Success : 0
1648 * Failure : EINVAL
1649 */
1650int omap_stop_dma_chain_transfers(int chain_id)
1651{
1652 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001653 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001654 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001655
1656 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001657 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001658 printk(KERN_ERR "Invalid chain id\n");
1659 return -EINVAL;
1660 }
1661
1662 /* Check if the chain exists */
1663 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1664 printk(KERN_ERR "Chain doesn't exists\n");
1665 return -EINVAL;
1666 }
1667 channels = dma_linked_lch[chain_id].linked_dmach_q;
1668
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001669 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001670 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001671 l = sys_cf;
1672 /* Middle mode reg set no Standby */
1673 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001674 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001675 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001676
1677 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1678
1679 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001680 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001681 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001682 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001683
1684 /* Disable the link in all the channels */
1685 disable_lnk(channels[i]);
1686 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1687
1688 }
1689 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1690
1691 /* Reset the Queue pointers */
1692 OMAP_DMA_CHAIN_QINIT(chain_id);
1693
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001694 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001695 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001696
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001697 return 0;
1698}
1699EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1700
1701/* Get the index of the ongoing DMA in chain */
1702/**
1703 * @brief omap_get_dma_chain_index - Get the element and frame index
1704 * of the ongoing DMA in chain
1705 *
1706 * @param chain_id
1707 * @param ei - Element index
1708 * @param fi - Frame index
1709 *
1710 * @return - Success : 0
1711 * Failure : -EINVAL
1712 */
1713int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1714{
1715 int lch;
1716 int *channels;
1717
1718 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001719 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001720 printk(KERN_ERR "Invalid chain id\n");
1721 return -EINVAL;
1722 }
1723
1724 /* Check if the chain exists */
1725 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1726 printk(KERN_ERR "Chain doesn't exists\n");
1727 return -EINVAL;
1728 }
1729 if ((!ei) || (!fi))
1730 return -EINVAL;
1731
1732 channels = dma_linked_lch[chain_id].linked_dmach_q;
1733
1734 /* Get the current channel */
1735 lch = channels[dma_linked_lch[chain_id].q_head];
1736
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001737 *ei = p->dma_read(CCEN, lch);
1738 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001739
1740 return 0;
1741}
1742EXPORT_SYMBOL(omap_get_dma_chain_index);
1743
1744/**
1745 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1746 * ongoing DMA in chain
1747 *
1748 * @param chain_id
1749 *
1750 * @return - Success : Destination position
1751 * Failure : -EINVAL
1752 */
1753int omap_get_dma_chain_dst_pos(int chain_id)
1754{
1755 int lch;
1756 int *channels;
1757
1758 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001759 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001760 printk(KERN_ERR "Invalid chain id\n");
1761 return -EINVAL;
1762 }
1763
1764 /* Check if the chain exists */
1765 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1766 printk(KERN_ERR "Chain doesn't exists\n");
1767 return -EINVAL;
1768 }
1769
1770 channels = dma_linked_lch[chain_id].linked_dmach_q;
1771
1772 /* Get the current channel */
1773 lch = channels[dma_linked_lch[chain_id].q_head];
1774
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001775 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001776}
1777EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1778
1779/**
1780 * @brief omap_get_dma_chain_src_pos - Get the source position
1781 * of the ongoing DMA in chain
1782 * @param chain_id
1783 *
1784 * @return - Success : Destination position
1785 * Failure : -EINVAL
1786 */
1787int omap_get_dma_chain_src_pos(int chain_id)
1788{
1789 int lch;
1790 int *channels;
1791
1792 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001793 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001794 printk(KERN_ERR "Invalid chain id\n");
1795 return -EINVAL;
1796 }
1797
1798 /* Check if the chain exists */
1799 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1800 printk(KERN_ERR "Chain doesn't exists\n");
1801 return -EINVAL;
1802 }
1803
1804 channels = dma_linked_lch[chain_id].linked_dmach_q;
1805
1806 /* Get the current channel */
1807 lch = channels[dma_linked_lch[chain_id].q_head];
1808
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001809 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001810}
1811EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001812#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001813
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001814/*----------------------------------------------------------------------------*/
1815
1816#ifdef CONFIG_ARCH_OMAP1
1817
1818static int omap1_dma_handle_ch(int ch)
1819{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001820 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001821
1822 if (enable_1510_mode && ch >= 6) {
1823 csr = dma_chan[ch].saved_csr;
1824 dma_chan[ch].saved_csr = 0;
1825 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001826 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001827 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1828 dma_chan[ch + 6].saved_csr = csr >> 7;
1829 csr &= 0x7f;
1830 }
1831 if ((csr & 0x3f) == 0)
1832 return 0;
1833 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001834 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
1835 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001836 return 0;
1837 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001838 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001839 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001840 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001841 pr_warn("DMA synchronization event drop occurred with device %d\n",
1842 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001843 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1844 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1845 if (likely(dma_chan[ch].callback != NULL))
1846 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001847
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001848 return 1;
1849}
1850
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001851static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001852{
1853 int ch = ((int) dev_id) - 1;
1854 int handled = 0;
1855
1856 for (;;) {
1857 int handled_now = 0;
1858
1859 handled_now += omap1_dma_handle_ch(ch);
1860 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1861 handled_now += omap1_dma_handle_ch(ch + 6);
1862 if (!handled_now)
1863 break;
1864 handled += handled_now;
1865 }
1866
1867 return handled ? IRQ_HANDLED : IRQ_NONE;
1868}
1869
1870#else
1871#define omap1_dma_irq_handler NULL
1872#endif
1873
Tony Lindgren140455f2010-02-12 12:26:48 -08001874#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001875
1876static int omap2_dma_handle_ch(int ch)
1877{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001878 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001879
Juha Yrjola31513692006-12-06 17:13:47 -08001880 if (!status) {
1881 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001882 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001883 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001884 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001885 }
1886 if (unlikely(dma_chan[ch].dev_id == -1)) {
1887 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001888 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1889 status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001890 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001891 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001892 if (unlikely(status & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001893 pr_info("DMA synchronization event drop occurred with device %d\n",
1894 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001895 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001896 printk(KERN_INFO "DMA transaction error with device %d\n",
1897 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001898 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001899 u32 ccr;
1900
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001901 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001902 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001903 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001904 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1905 }
1906 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001907 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1908 printk(KERN_INFO "DMA secure error with device %d\n",
1909 dma_chan[ch].dev_id);
1910 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1911 printk(KERN_INFO "DMA misaligned error with device %d\n",
1912 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001913
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001914 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001915 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001916 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001917 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001918
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001919 /* If the ch is not chained then chain_id will be -1 */
1920 if (dma_chan[ch].chain_id != -1) {
1921 int chain_id = dma_chan[ch].chain_id;
1922 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001923 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001924 dma_chan[dma_chan[ch].next_linked_ch].state =
1925 DMA_CH_STARTED;
1926 if (dma_linked_lch[chain_id].chain_mode ==
1927 OMAP_DMA_DYNAMIC_CHAIN)
1928 disable_lnk(ch);
1929
1930 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1931 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1932
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001933 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001934 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001935 }
1936
Jarkko Nikula538528d2008-02-13 11:47:29 +02001937 if (likely(dma_chan[ch].callback != NULL))
1938 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001939
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001940 return 0;
1941}
1942
1943/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001944static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001945{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001946 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001947 int i;
1948
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001949 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001950 if (val == 0) {
1951 if (printk_ratelimit())
1952 printk(KERN_WARNING "Spurious DMA IRQ\n");
1953 return IRQ_HANDLED;
1954 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001955 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001956 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001957 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001958 if (val & 1)
1959 omap2_dma_handle_ch(i);
1960 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001961 }
1962
1963 return IRQ_HANDLED;
1964}
1965
1966static struct irqaction omap24xx_dma_irq = {
1967 .name = "DMA",
1968 .handler = omap2_dma_irq_handler,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001969};
1970
1971#else
1972static struct irqaction omap24xx_dma_irq;
1973#endif
1974
1975/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001976
Tony Lindgren9ce24822014-05-16 14:05:35 -07001977/*
1978 * Note that we are currently using only IRQENABLE_L0 and L1.
1979 * As the DSP may be using IRQENABLE_L2 and L3, let's not
1980 * touch those for now.
1981 */
Tero Kristof2d11852008-08-28 13:13:31 +00001982void omap_dma_global_context_save(void)
1983{
1984 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001985 p->dma_read(IRQENABLE_L0, 0);
Tony Lindgren9ce24822014-05-16 14:05:35 -07001986 omap_dma_global_context.dma_irqenable_l1 =
1987 p->dma_read(IRQENABLE_L1, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001988 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001989 p->dma_read(OCP_SYSCONFIG, 0);
1990 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001991}
1992
1993void omap_dma_global_context_restore(void)
1994{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001995 int ch;
1996
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001997 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1998 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001999 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002000 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08002001 IRQENABLE_L0, 0);
Tony Lindgren9ce24822014-05-16 14:05:35 -07002002 p->dma_write(omap_dma_global_context.dma_irqenable_l1,
2003 IRQENABLE_L1, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00002004
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002005 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002006 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03002007
2008 for (ch = 0; ch < dma_chan_count; ch++)
2009 if (dma_chan[ch].dev_id != -1)
2010 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00002011}
2012
Russell King1b416c42013-11-02 13:00:03 +00002013struct omap_system_dma_plat_info *omap_get_plat_info(void)
2014{
2015 return p;
2016}
2017EXPORT_SYMBOL_GPL(omap_get_plat_info);
2018
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002019static int omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002020{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002021 int ch, ret = 0;
2022 int dma_irq;
2023 char irq_name[4];
2024 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002025
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002026 p = pdev->dev.platform_data;
2027 if (!p) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002028 dev_err(&pdev->dev,
2029 "%s: System DMA initialized without platform data\n",
2030 __func__);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002031 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002032 }
2033
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002034 d = p->dma_attr;
2035 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002036
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002037 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Chen Gange78f9602013-01-11 13:39:18 +08002038 && (omap_dma_reserve_channels < d->lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002039 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002040
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002041 dma_lch_count = d->lch_count;
2042 dma_chan_count = dma_lch_count;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002043 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002044
Russell King9834f812013-11-08 18:10:42 +00002045 dma_chan = devm_kcalloc(&pdev->dev, dma_lch_count,
2046 sizeof(struct omap_dma_lch), GFP_KERNEL);
2047 if (!dma_chan) {
2048 dev_err(&pdev->dev, "%s: kzalloc fail\n", __func__);
2049 return -ENOMEM;
2050 }
2051
2052
Tony Lindgren82809602012-10-30 11:03:22 -07002053 if (dma_omap2plus()) {
Tony Lindgren4d963722008-07-03 12:24:31 +03002054 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2055 dma_lch_count, GFP_KERNEL);
2056 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002057 ret = -ENOMEM;
2058 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002059 }
2060 }
2061
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002062 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002063 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002064 omap_clear_dma(ch);
Tony Lindgren82809602012-10-30 11:03:22 -07002065 if (dma_omap2plus())
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002066 omap2_disable_irq_lch(ch);
2067
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002068 dma_chan[ch].dev_id = -1;
2069 dma_chan[ch].next_lch = -1;
2070
2071 if (ch >= 6 && enable_1510_mode)
2072 continue;
2073
Tony Lindgren82809602012-10-30 11:03:22 -07002074 if (dma_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002075 /*
2076 * request_irq() doesn't like dev_id (ie. ch) being
2077 * zero, so we have to kludge around this.
2078 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002079 sprintf(&irq_name[0], "%d", ch);
2080 dma_irq = platform_get_irq_byname(pdev, irq_name);
2081
2082 if (dma_irq < 0) {
2083 ret = dma_irq;
2084 goto exit_dma_irq_fail;
2085 }
2086
2087 /* INT_DMA_LCD is handled in lcd_dma.c */
2088 if (dma_irq == INT_DMA_LCD)
2089 continue;
2090
2091 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002092 omap1_dma_irq_handler, 0, "DMA",
2093 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002094 if (ret != 0)
2095 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002096 }
2097 }
2098
Tony Lindgren82809602012-10-30 11:03:22 -07002099 if (d->dev_caps & IS_RW_PRIORITY)
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002100 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2101 DMA_DEFAULT_FIFO_DEPTH, 0);
2102
Tony Lindgren82809602012-10-30 11:03:22 -07002103 if (dma_omap2plus()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002104 strcpy(irq_name, "0");
2105 dma_irq = platform_get_irq_byname(pdev, irq_name);
2106 if (dma_irq < 0) {
2107 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
Wei Yongjun94b1d612013-07-16 20:10:46 +08002108 ret = dma_irq;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002109 goto exit_dma_lch_fail;
2110 }
2111 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2112 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002113 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
2114 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002115 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002116 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002117 }
2118
Tony Lindgren82809602012-10-30 11:03:22 -07002119 /* reserve dma channels 0 and 1 in high security devices on 34xx */
2120 if (d->dev_caps & HS_CHANNELS_RESERVED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002121 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002122 dma_chan[0].dev_id = 0;
2123 dma_chan[1].dev_id = 1;
2124 }
2125 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002126 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002127
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002128exit_dma_irq_fail:
Paul Walmsley7852ec02012-07-26 00:54:26 -06002129 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
2130 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002131 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2132 dma_irq = platform_get_irq(pdev, irq_rel);
2133 free_irq(dma_irq, (void *)(irq_rel + 1));
2134 }
2135
2136exit_dma_lch_fail:
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002137 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002138}
2139
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002140static int omap_system_dma_remove(struct platform_device *pdev)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002141{
2142 int dma_irq;
2143
Tony Lindgren82809602012-10-30 11:03:22 -07002144 if (dma_omap2plus()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002145 char irq_name[4];
2146 strcpy(irq_name, "0");
2147 dma_irq = platform_get_irq_byname(pdev, irq_name);
2148 remove_irq(dma_irq, &omap24xx_dma_irq);
2149 } else {
2150 int irq_rel = 0;
2151 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2152 dma_irq = platform_get_irq(pdev, irq_rel);
2153 free_irq(dma_irq, (void *)(irq_rel + 1));
2154 }
2155 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002156 return 0;
2157}
2158
2159static struct platform_driver omap_system_dma_driver = {
2160 .probe = omap_system_dma_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002161 .remove = omap_system_dma_remove,
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002162 .driver = {
2163 .name = "omap_dma_system"
2164 },
2165};
2166
2167static int __init omap_system_dma_init(void)
2168{
2169 return platform_driver_register(&omap_system_dma_driver);
2170}
2171arch_initcall(omap_system_dma_init);
2172
2173static void __exit omap_system_dma_exit(void)
2174{
2175 platform_driver_unregister(&omap_system_dma_driver);
2176}
2177
2178MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2179MODULE_LICENSE("GPL");
2180MODULE_ALIAS("platform:" DRIVER_NAME);
2181MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002182
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002183/*
2184 * Reserve the omap SDMA channels using cmdline bootarg
2185 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2186 */
2187static int __init omap_dma_cmdline_reserve_ch(char *str)
2188{
2189 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2190 omap_dma_reserve_channels = 0;
2191 return 1;
2192}
2193
2194__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2195
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002196