blob: b4dcd92a5845f5600ffe1334928dddd6a08e4b76 [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 Lindgrendbc04162012-08-31 10:59:07 -070039#include <plat/cpu.h>
Lokesh Vutla2b6c4e72012-10-15 14:04:53 -070040#include <plat-omap/dma-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010041
Paul Walmsleybc4d8b52012-04-13 06:34:30 -060042/*
43 * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
44 * channels that an instance of the SDMA IP block can support. Used
45 * to size arrays. (The actual maximum on a particular SoC may be less
46 * than this -- for example, OMAP1 SDMA instances only support 17 logical
47 * DMA channels.)
48 */
49#define MAX_LOGICAL_DMA_CH_COUNT 32
50
Anand Gadiyarf8151e52007-12-01 12:14:11 -080051#undef DEBUG
52
53#ifndef CONFIG_ARCH_OMAP1
54enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
55 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
56};
57
58enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000059#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010060
Tony Lindgren97b7f712008-07-03 12:24:37 +030061#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020062#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063
Tony Lindgren97b7f712008-07-03 12:24:37 +030064#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080066static struct omap_system_dma_plat_info *p;
67static struct omap_dma_dev_attr *d;
68
Tony Lindgren97b7f712008-07-03 12:24:37 +030069static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080070static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010071
Tero Kristof2d11852008-08-28 13:13:31 +000072static struct omap_dma_global_context_registers {
73 u32 dma_irqenable_l0;
74 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 Lindgren709eb3e52006-09-25 12:45:45 +0300183 if (cpu_class_is_omap1()) {
184 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 Lindgren1a8bfa12005-11-10 14:26:50 +0000235 if (cpu_class_is_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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800251 if (cpu_class_is_omap2() && 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
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700291 if (cpu_class_is_omap1()) {
292 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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800321 if (cpu_class_is_omap2()) {
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{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800349 if (cpu_class_is_omap2()) {
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{
362 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
363 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 Lindgren1a8bfa12005-11-10 14:26:50 +0000380 if (cpu_class_is_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 Lindgren97b7f712008-07-03 12:24:37 +0300422 if (cpu_class_is_omap2())
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800454 if (cpu_class_is_omap2())
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800460 if (cpu_class_is_omap2()) {
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800470 if (cpu_class_is_omap2()) {
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 Lindgren1a8bfa12005-11-10 14:26:50 +0000494 if (cpu_class_is_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 Lindgren97b7f712008-07-03 12:24:37 +0300515 if (cpu_class_is_omap2())
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800547 if (cpu_class_is_omap2())
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800553 if (cpu_class_is_omap2())
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:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800559 if (cpu_class_is_omap2()) {
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 */
580 if (cpu_class_is_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 */
594 if (cpu_class_is_omap1())
595 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 Lindgren1a8bfa12005-11-10 14:26:50 +0000618 if (cpu_class_is_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 Lindgren97b7f712008-07-03 12:24:37 +0300626 if (cpu_class_is_omap2())
627 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 Lindgren1a8bfa12005-11-10 14:26:50 +0000643 if (cpu_class_is_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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800648 if (cpu_class_is_omap2()) {
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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800662 if (!cpu_class_is_omap2())
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
680 if (!cpu_class_is_omap2())
681 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;
705 if (dev_id == 0)
706 break;
707 }
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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800719 if (cpu_class_is_omap2())
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 Lindgren97b7f712008-07-03 12:24:37 +0300730 if (cpu_class_is_omap2()) {
731 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 Lindgren7ff879d2006-06-26 16:16:15 -0700738 if (cpu_class_is_omap1())
739 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800740 else if (cpu_class_is_omap2())
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
744 if (cpu_is_omap16xx()) {
745 /* 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);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700755 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
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
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800759 if (cpu_class_is_omap2()) {
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 */
781 if (cpu_class_is_omap2())
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 */
791 if (cpu_class_is_omap2())
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
817 if (!cpu_class_is_omap2()) {
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));
Santosh Shilimkard07c3df2012-04-28 20:19:10 +0530856 if (cpu_class_is_omap2() && !cpu_is_omap242x())
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 */
889 if (cpu_is_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
898 dma_chan_link_map[lch] = 1;
899 /* Set the link register of the first channel */
900 enable_lnk(lch);
901
902 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
903 cur_lch = dma_chan[lch].next_lch;
904 do {
905 next_lch = dma_chan[cur_lch].next_lch;
906
907 /* The loop case: we've been here already */
908 if (dma_chan_link_map[cur_lch])
909 break;
910 /* Mark the current channel */
911 dma_chan_link_map[cur_lch] = 1;
912
913 enable_lnk(cur_lch);
914 omap_enable_channel_irq(cur_lch);
915
916 cur_lch = next_lch;
917 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800918 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800919 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000920
921 omap_enable_channel_irq(lch);
922
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800923 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300924
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800925 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
926 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300927 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800928
Russell King35453582012-04-14 18:57:10 +0100929 /*
930 * As dma_write() uses IO accessors which are weakly ordered, there
931 * is no guarantee that data in coherent DMA memory will be visible
932 * to the DMA device. Add a memory barrier here to ensure that any
933 * such data is visible prior to enabling DMA.
934 */
935 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800936 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000937
938 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
939}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300940EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000941
942void omap_stop_dma(int lch)
943{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300944 u32 l;
945
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700946 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700947 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700948
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800949 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800950 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
951 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700952 int i = 0;
953 u32 sys_cf;
954
955 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800956 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700957 sys_cf = l;
958 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
959 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800960 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700961
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800962 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700963 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800964 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700965
966 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800967 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700968 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
969 OMAP_DMA_CCR_WR_ACTIVE))) {
970 udelay(5);
971 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800972 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700973 }
974 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600975 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700976 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800977 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700978 } else {
979 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800980 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700981 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700982
Russell King35453582012-04-14 18:57:10 +0100983 /*
984 * Ensure that data transferred by DMA is visible to any access
985 * after DMA has been disabled. This is important for coherent
986 * DMA regions.
987 */
988 mb();
989
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000990 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
991 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600992 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000993
994 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
995 do {
996 /* The loop case: we've been here already */
997 if (dma_chan_link_map[cur_lch])
998 break;
999 /* Mark the current channel */
1000 dma_chan_link_map[cur_lch] = 1;
1001
1002 disable_lnk(cur_lch);
1003
1004 next_lch = dma_chan[cur_lch].next_lch;
1005 cur_lch = next_lch;
1006 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001007 }
1008
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001009 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1010}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001011EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001012
1013/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001014 * Allows changing the DMA callback function or data. This may be needed if
1015 * the driver shares a single DMA channel for multiple dma triggers.
1016 */
1017int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001018 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001019 void *data)
1020{
1021 unsigned long flags;
1022
1023 if (lch < 0)
1024 return -ENODEV;
1025
1026 spin_lock_irqsave(&dma_chan_lock, flags);
1027 if (dma_chan[lch].dev_id == -1) {
1028 printk(KERN_ERR "DMA callback for not set for free channel\n");
1029 spin_unlock_irqrestore(&dma_chan_lock, flags);
1030 return -EINVAL;
1031 }
1032 dma_chan[lch].callback = callback;
1033 dma_chan[lch].data = data;
1034 spin_unlock_irqrestore(&dma_chan_lock, flags);
1035
1036 return 0;
1037}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001038EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001039
1040/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001041 * Returns current physical source address for the given DMA channel.
1042 * If the channel is running the caller must disable interrupts prior calling
1043 * this function and process the returned value before re-enabling interrupt to
1044 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001045 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001046 * in incorrect return value.
1047 */
1048dma_addr_t omap_get_dma_src_pos(int lch)
1049{
Tony Lindgren0695de32007-05-07 18:24:14 -07001050 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001051
Tony Lindgren0499bde2008-07-03 12:24:36 +03001052 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001053 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001054 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001055 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001056
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001057 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001058 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001059
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001060 if (!cpu_is_omap15xx()) {
1061 /*
1062 * CDAC == 0 indicates that the DMA transfer on the channel has
1063 * not been started (no data has been transferred so far).
1064 * Return the programmed source start address in this case.
1065 */
1066 if (likely(p->dma_read(CDAC, lch)))
1067 offset = p->dma_read(CSAC, lch);
1068 else
1069 offset = p->dma_read(CSSA, lch);
1070 }
1071
Tony Lindgren0499bde2008-07-03 12:24:36 +03001072 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001073 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001074
1075 return offset;
1076}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001077EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001078
1079/*
1080 * Returns current physical destination address for the given DMA channel.
1081 * If the channel is running the caller must disable interrupts prior calling
1082 * this function and process the returned value before re-enabling interrupt to
1083 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001084 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001085 * in incorrect return value.
1086 */
1087dma_addr_t omap_get_dma_dst_pos(int lch)
1088{
Tony Lindgren0695de32007-05-07 18:24:14 -07001089 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001090
Tony Lindgren0499bde2008-07-03 12:24:36 +03001091 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001092 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001093 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001094 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001095
Tony Lindgren0499bde2008-07-03 12:24:36 +03001096 /*
1097 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1098 * read before the DMA controller finished disabling the channel.
1099 */
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001100 if (!cpu_is_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001101 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001102 /*
1103 * CDAC == 0 indicates that the DMA transfer on the channel has
1104 * not been started (no data has been transferred so far).
1105 * Return the programmed destination start address in this case.
1106 */
1107 if (unlikely(!offset))
1108 offset = p->dma_read(CDSA, lch);
1109 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001110
1111 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001112 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001113
1114 return offset;
1115}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001116EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001117
Tony Lindgren0499bde2008-07-03 12:24:36 +03001118int omap_get_dma_active_status(int lch)
1119{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001120 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001121}
1122EXPORT_SYMBOL(omap_get_dma_active_status);
1123
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001124int omap_dma_running(void)
1125{
1126 int lch;
1127
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001128 if (cpu_class_is_omap1())
1129 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001130 return 1;
1131
1132 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001133 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001134 return 1;
1135
1136 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137}
1138
1139/*
1140 * lch_queue DMA will start right after lch_head one is finished.
1141 * For this DMA link to start, you still need to start (see omap_start_dma)
1142 * the first one. That will fire up the entire queue.
1143 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001144void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145{
1146 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001147 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001148 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001149 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001150 return;
1151 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1153 BUG();
1154 return;
1155 }
1156
1157 if ((dma_chan[lch_head].dev_id == -1) ||
1158 (dma_chan[lch_queue].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001159 pr_err("omap_dma: trying to link non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160 dump_stack();
1161 }
1162
1163 dma_chan[lch_head].next_lch = lch_queue;
1164}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001165EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166
1167/*
1168 * Once the DMA queue is stopped, we can destroy it.
1169 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001170void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001171{
1172 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001173 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001174 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001175 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001176 return;
1177 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1179 BUG();
1180 return;
1181 }
1182
1183 if (dma_chan[lch_head].next_lch != lch_queue ||
1184 dma_chan[lch_head].next_lch == -1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001185 pr_err("omap_dma: trying to unlink non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001186 dump_stack();
1187 }
1188
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001189 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001190 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001191 pr_err("omap_dma: You need to stop the DMA channels before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001192 dump_stack();
1193 }
1194
1195 dma_chan[lch_head].next_lch = -1;
1196}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001197EXPORT_SYMBOL(omap_dma_unlink_lch);
1198
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001199#ifndef CONFIG_ARCH_OMAP1
1200/* Create chain of DMA channesls */
1201static void create_dma_lch_chain(int lch_head, int lch_queue)
1202{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001203 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001204
1205 /* Check if this is the first link in chain */
1206 if (dma_chan[lch_head].next_linked_ch == -1) {
1207 dma_chan[lch_head].next_linked_ch = lch_queue;
1208 dma_chan[lch_head].prev_linked_ch = lch_queue;
1209 dma_chan[lch_queue].next_linked_ch = lch_head;
1210 dma_chan[lch_queue].prev_linked_ch = lch_head;
1211 }
1212
1213 /* a link exists, link the new channel in circular chain */
1214 else {
1215 dma_chan[lch_queue].next_linked_ch =
1216 dma_chan[lch_head].next_linked_ch;
1217 dma_chan[lch_queue].prev_linked_ch = lch_head;
1218 dma_chan[lch_head].next_linked_ch = lch_queue;
1219 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1220 lch_queue;
1221 }
1222
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001223 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001224 l &= ~(0x1f);
1225 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001226 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001227
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001228 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001229 l &= ~(0x1f);
1230 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001231 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001232}
1233
1234/**
1235 * @brief omap_request_dma_chain : Request a chain of DMA channels
1236 *
1237 * @param dev_id - Device id using the dma channel
1238 * @param dev_name - Device name
1239 * @param callback - Call back function
1240 * @chain_id -
1241 * @no_of_chans - Number of channels requested
1242 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1243 * OMAP_DMA_DYNAMIC_CHAIN
1244 * @params - Channel parameters
1245 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001246 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001247 * Failure: -EINVAL/-ENOMEM
1248 */
1249int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b918d2009-05-28 13:23:52 -07001250 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001251 void *data),
1252 int *chain_id, int no_of_chans, int chain_mode,
1253 struct omap_dma_channel_params params)
1254{
1255 int *channels;
1256 int i, err;
1257
1258 /* Is the chain mode valid ? */
1259 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1260 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1261 printk(KERN_ERR "Invalid chain mode requested\n");
1262 return -EINVAL;
1263 }
1264
1265 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001266 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001267 printk(KERN_ERR "Invalid Number of channels requested\n");
1268 return -EINVAL;
1269 }
1270
manjugk manjugkea221a62010-05-14 12:05:25 -07001271 /*
1272 * Allocate a queue to maintain the status of the channels
1273 * in the chain
1274 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001275 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1276 if (channels == NULL) {
1277 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1278 return -ENOMEM;
1279 }
1280
1281 /* request and reserve DMA channels for the chain */
1282 for (i = 0; i < no_of_chans; i++) {
1283 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001284 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001285 if (err < 0) {
1286 int j;
1287 for (j = 0; j < i; j++)
1288 omap_free_dma(channels[j]);
1289 kfree(channels);
1290 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1291 return err;
1292 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001293 dma_chan[channels[i]].prev_linked_ch = -1;
1294 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1295
1296 /*
1297 * Allowing client drivers to set common parameters now,
1298 * so that later only relevant (src_start, dest_start
1299 * and element count) can be set
1300 */
1301 omap_set_dma_params(channels[i], &params);
1302 }
1303
1304 *chain_id = channels[0];
1305 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1306 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1307 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1308 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1309
1310 for (i = 0; i < no_of_chans; i++)
1311 dma_chan[channels[i]].chain_id = *chain_id;
1312
1313 /* Reset the Queue pointers */
1314 OMAP_DMA_CHAIN_QINIT(*chain_id);
1315
1316 /* Set up the chain */
1317 if (no_of_chans == 1)
1318 create_dma_lch_chain(channels[0], channels[0]);
1319 else {
1320 for (i = 0; i < (no_of_chans - 1); i++)
1321 create_dma_lch_chain(channels[i], channels[i + 1]);
1322 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001323
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001324 return 0;
1325}
1326EXPORT_SYMBOL(omap_request_dma_chain);
1327
1328/**
1329 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1330 * params after setting it. Dont do this while dma is running!!
1331 *
1332 * @param chain_id - Chained logical channel id.
1333 * @param params
1334 *
1335 * @return - Success : 0
1336 * Failure : -EINVAL
1337 */
1338int omap_modify_dma_chain_params(int chain_id,
1339 struct omap_dma_channel_params params)
1340{
1341 int *channels;
1342 u32 i;
1343
1344 /* Check for input params */
1345 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001346 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001347 printk(KERN_ERR "Invalid chain id\n");
1348 return -EINVAL;
1349 }
1350
1351 /* Check if the chain exists */
1352 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1353 printk(KERN_ERR "Chain doesn't exists\n");
1354 return -EINVAL;
1355 }
1356 channels = dma_linked_lch[chain_id].linked_dmach_q;
1357
1358 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1359 /*
1360 * Allowing client drivers to set common parameters now,
1361 * so that later only relevant (src_start, dest_start
1362 * and element count) can be set
1363 */
1364 omap_set_dma_params(channels[i], &params);
1365 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001366
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001367 return 0;
1368}
1369EXPORT_SYMBOL(omap_modify_dma_chain_params);
1370
1371/**
1372 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1373 *
1374 * @param chain_id
1375 *
1376 * @return - Success : 0
1377 * Failure : -EINVAL
1378 */
1379int omap_free_dma_chain(int chain_id)
1380{
1381 int *channels;
1382 u32 i;
1383
1384 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001385 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001386 printk(KERN_ERR "Invalid chain id\n");
1387 return -EINVAL;
1388 }
1389
1390 /* Check if the chain exists */
1391 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1392 printk(KERN_ERR "Chain doesn't exists\n");
1393 return -EINVAL;
1394 }
1395
1396 channels = dma_linked_lch[chain_id].linked_dmach_q;
1397 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1398 dma_chan[channels[i]].next_linked_ch = -1;
1399 dma_chan[channels[i]].prev_linked_ch = -1;
1400 dma_chan[channels[i]].chain_id = -1;
1401 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1402 omap_free_dma(channels[i]);
1403 }
1404
1405 kfree(channels);
1406
1407 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1408 dma_linked_lch[chain_id].chain_mode = -1;
1409 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001410
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001411 return (0);
1412}
1413EXPORT_SYMBOL(omap_free_dma_chain);
1414
1415/**
1416 * @brief omap_dma_chain_status - Check if the chain is in
1417 * active / inactive state.
1418 * @param chain_id
1419 *
1420 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1421 * Failure : -EINVAL
1422 */
1423int omap_dma_chain_status(int chain_id)
1424{
1425 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001426 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001427 printk(KERN_ERR "Invalid chain id\n");
1428 return -EINVAL;
1429 }
1430
1431 /* Check if the chain exists */
1432 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1433 printk(KERN_ERR "Chain doesn't exists\n");
1434 return -EINVAL;
1435 }
1436 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1437 dma_linked_lch[chain_id].q_count);
1438
1439 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1440 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001441
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001442 return OMAP_DMA_CHAIN_ACTIVE;
1443}
1444EXPORT_SYMBOL(omap_dma_chain_status);
1445
1446/**
1447 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1448 * set the params and start the transfer.
1449 *
1450 * @param chain_id
1451 * @param src_start - buffer start address
1452 * @param dest_start - Dest address
1453 * @param elem_count
1454 * @param frame_count
1455 * @param callbk_data - channel callback parameter data.
1456 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301457 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001458 * Failure: -EINVAL/-EBUSY
1459 */
1460int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1461 int elem_count, int frame_count, void *callbk_data)
1462{
1463 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001464 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001465 int start_dma = 0;
1466
Tony Lindgren97b7f712008-07-03 12:24:37 +03001467 /*
1468 * if buffer size is less than 1 then there is
1469 * no use of starting the chain
1470 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001471 if (elem_count < 1) {
1472 printk(KERN_ERR "Invalid buffer size\n");
1473 return -EINVAL;
1474 }
1475
1476 /* Check for input params */
1477 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001478 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001479 printk(KERN_ERR "Invalid chain id\n");
1480 return -EINVAL;
1481 }
1482
1483 /* Check if the chain exists */
1484 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1485 printk(KERN_ERR "Chain doesn't exist\n");
1486 return -EINVAL;
1487 }
1488
1489 /* Check if all the channels in chain are in use */
1490 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1491 return -EBUSY;
1492
1493 /* Frame count may be negative in case of indexed transfers */
1494 channels = dma_linked_lch[chain_id].linked_dmach_q;
1495
1496 /* Get a free channel */
1497 lch = channels[dma_linked_lch[chain_id].q_tail];
1498
1499 /* Store the callback data */
1500 dma_chan[lch].data = callbk_data;
1501
1502 /* Increment the q_tail */
1503 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1504
1505 /* Set the params to the free channel */
1506 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001507 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001508 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001509 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001510
1511 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001512 p->dma_write(elem_count, CEN, lch);
1513 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001514
Tony Lindgren97b7f712008-07-03 12:24:37 +03001515 /*
1516 * If the chain is dynamically linked,
1517 * then we may have to start the chain if its not active
1518 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001519 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1520
Tony Lindgren97b7f712008-07-03 12:24:37 +03001521 /*
1522 * In Dynamic chain, if the chain is not started,
1523 * queue the channel
1524 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001525 if (dma_linked_lch[chain_id].chain_state ==
1526 DMA_CHAIN_NOTSTARTED) {
1527 /* Enable the link in previous channel */
1528 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1529 DMA_CH_QUEUED)
1530 enable_lnk(dma_chan[lch].prev_linked_ch);
1531 dma_chan[lch].state = DMA_CH_QUEUED;
1532 }
1533
Tony Lindgren97b7f712008-07-03 12:24:37 +03001534 /*
1535 * Chain is already started, make sure its active,
1536 * if not then start the chain
1537 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001538 else {
1539 start_dma = 1;
1540
1541 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1542 DMA_CH_STARTED) {
1543 enable_lnk(dma_chan[lch].prev_linked_ch);
1544 dma_chan[lch].state = DMA_CH_QUEUED;
1545 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001546 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001547 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001548 disable_lnk(dma_chan[lch].
1549 prev_linked_ch);
1550 pr_debug("\n prev ch is stopped\n");
1551 start_dma = 1;
1552 }
1553 }
1554
1555 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1556 == DMA_CH_QUEUED) {
1557 enable_lnk(dma_chan[lch].prev_linked_ch);
1558 dma_chan[lch].state = DMA_CH_QUEUED;
1559 start_dma = 0;
1560 }
1561 omap_enable_channel_irq(lch);
1562
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001563 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001564
Tony Lindgren0499bde2008-07-03 12:24:36 +03001565 if ((0 == (l & (1 << 24))))
1566 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001567 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001568 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001569 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001570 if (0 == (l & (1 << 7))) {
1571 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001572 dma_chan[lch].state = DMA_CH_STARTED;
1573 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001574 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001575 } else
1576 start_dma = 0;
1577 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001578 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001579 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001580 }
1581 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1582 }
1583 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001584
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301585 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001586}
1587EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1588
1589/**
1590 * @brief omap_start_dma_chain_transfers - Start the chain
1591 *
1592 * @param chain_id
1593 *
1594 * @return - Success : 0
1595 * Failure : -EINVAL/-EBUSY
1596 */
1597int omap_start_dma_chain_transfers(int chain_id)
1598{
1599 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001600 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001601
Tony Lindgren4d963722008-07-03 12:24:31 +03001602 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001603 printk(KERN_ERR "Invalid chain id\n");
1604 return -EINVAL;
1605 }
1606
1607 channels = dma_linked_lch[chain_id].linked_dmach_q;
1608
1609 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1610 printk(KERN_ERR "Chain is already started\n");
1611 return -EBUSY;
1612 }
1613
1614 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1615 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1616 i++) {
1617 enable_lnk(channels[i]);
1618 omap_enable_channel_irq(channels[i]);
1619 }
1620 } else {
1621 omap_enable_channel_irq(channels[0]);
1622 }
1623
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001624 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001625 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001626 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1627 dma_chan[channels[0]].state = DMA_CH_STARTED;
1628
Tony Lindgren0499bde2008-07-03 12:24:36 +03001629 if ((0 == (l & (1 << 24))))
1630 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001631 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001632 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001633 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001634
1635 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001636
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001637 return 0;
1638}
1639EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1640
1641/**
1642 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1643 *
1644 * @param chain_id
1645 *
1646 * @return - Success : 0
1647 * Failure : EINVAL
1648 */
1649int omap_stop_dma_chain_transfers(int chain_id)
1650{
1651 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001652 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001653 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001654
1655 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001656 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001657 printk(KERN_ERR "Invalid chain id\n");
1658 return -EINVAL;
1659 }
1660
1661 /* Check if the chain exists */
1662 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1663 printk(KERN_ERR "Chain doesn't exists\n");
1664 return -EINVAL;
1665 }
1666 channels = dma_linked_lch[chain_id].linked_dmach_q;
1667
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001668 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001669 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001670 l = sys_cf;
1671 /* Middle mode reg set no Standby */
1672 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001673 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001674 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001675
1676 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1677
1678 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001679 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001680 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001681 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001682
1683 /* Disable the link in all the channels */
1684 disable_lnk(channels[i]);
1685 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1686
1687 }
1688 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1689
1690 /* Reset the Queue pointers */
1691 OMAP_DMA_CHAIN_QINIT(chain_id);
1692
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001693 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001694 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001695
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001696 return 0;
1697}
1698EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1699
1700/* Get the index of the ongoing DMA in chain */
1701/**
1702 * @brief omap_get_dma_chain_index - Get the element and frame index
1703 * of the ongoing DMA in chain
1704 *
1705 * @param chain_id
1706 * @param ei - Element index
1707 * @param fi - Frame index
1708 *
1709 * @return - Success : 0
1710 * Failure : -EINVAL
1711 */
1712int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1713{
1714 int lch;
1715 int *channels;
1716
1717 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001718 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001719 printk(KERN_ERR "Invalid chain id\n");
1720 return -EINVAL;
1721 }
1722
1723 /* Check if the chain exists */
1724 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1725 printk(KERN_ERR "Chain doesn't exists\n");
1726 return -EINVAL;
1727 }
1728 if ((!ei) || (!fi))
1729 return -EINVAL;
1730
1731 channels = dma_linked_lch[chain_id].linked_dmach_q;
1732
1733 /* Get the current channel */
1734 lch = channels[dma_linked_lch[chain_id].q_head];
1735
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001736 *ei = p->dma_read(CCEN, lch);
1737 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001738
1739 return 0;
1740}
1741EXPORT_SYMBOL(omap_get_dma_chain_index);
1742
1743/**
1744 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1745 * ongoing DMA in chain
1746 *
1747 * @param chain_id
1748 *
1749 * @return - Success : Destination position
1750 * Failure : -EINVAL
1751 */
1752int omap_get_dma_chain_dst_pos(int chain_id)
1753{
1754 int lch;
1755 int *channels;
1756
1757 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001758 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001759 printk(KERN_ERR "Invalid chain id\n");
1760 return -EINVAL;
1761 }
1762
1763 /* Check if the chain exists */
1764 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1765 printk(KERN_ERR "Chain doesn't exists\n");
1766 return -EINVAL;
1767 }
1768
1769 channels = dma_linked_lch[chain_id].linked_dmach_q;
1770
1771 /* Get the current channel */
1772 lch = channels[dma_linked_lch[chain_id].q_head];
1773
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001774 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001775}
1776EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1777
1778/**
1779 * @brief omap_get_dma_chain_src_pos - Get the source position
1780 * of the ongoing DMA in chain
1781 * @param chain_id
1782 *
1783 * @return - Success : Destination position
1784 * Failure : -EINVAL
1785 */
1786int omap_get_dma_chain_src_pos(int chain_id)
1787{
1788 int lch;
1789 int *channels;
1790
1791 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001792 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001793 printk(KERN_ERR "Invalid chain id\n");
1794 return -EINVAL;
1795 }
1796
1797 /* Check if the chain exists */
1798 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1799 printk(KERN_ERR "Chain doesn't exists\n");
1800 return -EINVAL;
1801 }
1802
1803 channels = dma_linked_lch[chain_id].linked_dmach_q;
1804
1805 /* Get the current channel */
1806 lch = channels[dma_linked_lch[chain_id].q_head];
1807
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001808 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001809}
1810EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001811#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001812
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001813/*----------------------------------------------------------------------------*/
1814
1815#ifdef CONFIG_ARCH_OMAP1
1816
1817static int omap1_dma_handle_ch(int ch)
1818{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001819 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001820
1821 if (enable_1510_mode && ch >= 6) {
1822 csr = dma_chan[ch].saved_csr;
1823 dma_chan[ch].saved_csr = 0;
1824 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001825 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001826 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1827 dma_chan[ch + 6].saved_csr = csr >> 7;
1828 csr &= 0x7f;
1829 }
1830 if ((csr & 0x3f) == 0)
1831 return 0;
1832 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001833 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
1834 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001835 return 0;
1836 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001837 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001838 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001839 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001840 pr_warn("DMA synchronization event drop occurred with device %d\n",
1841 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001842 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1843 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1844 if (likely(dma_chan[ch].callback != NULL))
1845 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001846
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001847 return 1;
1848}
1849
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001850static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001851{
1852 int ch = ((int) dev_id) - 1;
1853 int handled = 0;
1854
1855 for (;;) {
1856 int handled_now = 0;
1857
1858 handled_now += omap1_dma_handle_ch(ch);
1859 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1860 handled_now += omap1_dma_handle_ch(ch + 6);
1861 if (!handled_now)
1862 break;
1863 handled += handled_now;
1864 }
1865
1866 return handled ? IRQ_HANDLED : IRQ_NONE;
1867}
1868
1869#else
1870#define omap1_dma_irq_handler NULL
1871#endif
1872
Tony Lindgren140455f2010-02-12 12:26:48 -08001873#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001874
1875static int omap2_dma_handle_ch(int ch)
1876{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001877 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001878
Juha Yrjola31513692006-12-06 17:13:47 -08001879 if (!status) {
1880 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001881 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001882 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001883 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001884 }
1885 if (unlikely(dma_chan[ch].dev_id == -1)) {
1886 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001887 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1888 status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001889 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001890 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001891 if (unlikely(status & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001892 pr_info("DMA synchronization event drop occurred with device %d\n",
1893 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001894 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001895 printk(KERN_INFO "DMA transaction error with device %d\n",
1896 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001897 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001898 u32 ccr;
1899
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001900 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001901 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001902 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001903 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1904 }
1905 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001906 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1907 printk(KERN_INFO "DMA secure error with device %d\n",
1908 dma_chan[ch].dev_id);
1909 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1910 printk(KERN_INFO "DMA misaligned error with device %d\n",
1911 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001912
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001913 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001914 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001915 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001916 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001917
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001918 /* If the ch is not chained then chain_id will be -1 */
1919 if (dma_chan[ch].chain_id != -1) {
1920 int chain_id = dma_chan[ch].chain_id;
1921 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001922 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001923 dma_chan[dma_chan[ch].next_linked_ch].state =
1924 DMA_CH_STARTED;
1925 if (dma_linked_lch[chain_id].chain_mode ==
1926 OMAP_DMA_DYNAMIC_CHAIN)
1927 disable_lnk(ch);
1928
1929 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1930 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1931
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001932 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001933 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001934 }
1935
Jarkko Nikula538528d2008-02-13 11:47:29 +02001936 if (likely(dma_chan[ch].callback != NULL))
1937 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001938
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001939 return 0;
1940}
1941
1942/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001943static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001944{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001945 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001946 int i;
1947
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001948 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001949 if (val == 0) {
1950 if (printk_ratelimit())
1951 printk(KERN_WARNING "Spurious DMA IRQ\n");
1952 return IRQ_HANDLED;
1953 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001954 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001955 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001956 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001957 if (val & 1)
1958 omap2_dma_handle_ch(i);
1959 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001960 }
1961
1962 return IRQ_HANDLED;
1963}
1964
1965static struct irqaction omap24xx_dma_irq = {
1966 .name = "DMA",
1967 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001968 .flags = IRQF_DISABLED
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
Tero Kristof2d11852008-08-28 13:13:31 +00001977void omap_dma_global_context_save(void)
1978{
1979 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001980 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001981 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001982 p->dma_read(OCP_SYSCONFIG, 0);
1983 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001984}
1985
1986void omap_dma_global_context_restore(void)
1987{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001988 int ch;
1989
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001990 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1991 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001992 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001993 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001994 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001995
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001996 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001997 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001998
1999 for (ch = 0; ch < dma_chan_count; ch++)
2000 if (dma_chan[ch].dev_id != -1)
2001 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00002002}
2003
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002004static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002005{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002006 int ch, ret = 0;
2007 int dma_irq;
2008 char irq_name[4];
2009 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002010
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002011 p = pdev->dev.platform_data;
2012 if (!p) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002013 dev_err(&pdev->dev,
2014 "%s: System DMA initialized without platform data\n",
2015 __func__);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002016 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002017 }
2018
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002019 d = p->dma_attr;
2020 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002021
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002022 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002023 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002024 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002025
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002026 dma_lch_count = d->lch_count;
2027 dma_chan_count = dma_lch_count;
2028 dma_chan = d->chan;
2029 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002030
2031 if (cpu_class_is_omap2()) {
2032 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2033 dma_lch_count, GFP_KERNEL);
2034 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002035 ret = -ENOMEM;
2036 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002037 }
2038 }
2039
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002040 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002041 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002042 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002043 if (cpu_class_is_omap2())
2044 omap2_disable_irq_lch(ch);
2045
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002046 dma_chan[ch].dev_id = -1;
2047 dma_chan[ch].next_lch = -1;
2048
2049 if (ch >= 6 && enable_1510_mode)
2050 continue;
2051
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002052 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002053 /*
2054 * request_irq() doesn't like dev_id (ie. ch) being
2055 * zero, so we have to kludge around this.
2056 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002057 sprintf(&irq_name[0], "%d", ch);
2058 dma_irq = platform_get_irq_byname(pdev, irq_name);
2059
2060 if (dma_irq < 0) {
2061 ret = dma_irq;
2062 goto exit_dma_irq_fail;
2063 }
2064
2065 /* INT_DMA_LCD is handled in lcd_dma.c */
2066 if (dma_irq == INT_DMA_LCD)
2067 continue;
2068
2069 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002070 omap1_dma_irq_handler, 0, "DMA",
2071 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002072 if (ret != 0)
2073 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002074 }
2075 }
2076
Santosh Shilimkard07c3df2012-04-28 20:19:10 +05302077 if (cpu_class_is_omap2() && !cpu_is_omap242x())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002078 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2079 DMA_DEFAULT_FIFO_DEPTH, 0);
2080
Santosh Shilimkar44169072009-05-28 14:16:04 -07002081 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002082 strcpy(irq_name, "0");
2083 dma_irq = platform_get_irq_byname(pdev, irq_name);
2084 if (dma_irq < 0) {
2085 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2086 goto exit_dma_lch_fail;
2087 }
2088 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2089 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002090 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
2091 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002092 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002093 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002094 }
2095
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002096 /* reserve dma channels 0 and 1 in high security devices */
2097 if (cpu_is_omap34xx() &&
2098 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002099 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002100 dma_chan[0].dev_id = 0;
2101 dma_chan[1].dev_id = 1;
2102 }
2103 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002104 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002105
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002106exit_dma_irq_fail:
Paul Walmsley7852ec02012-07-26 00:54:26 -06002107 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
2108 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002109 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2110 dma_irq = platform_get_irq(pdev, irq_rel);
2111 free_irq(dma_irq, (void *)(irq_rel + 1));
2112 }
2113
2114exit_dma_lch_fail:
2115 kfree(p);
2116 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002117 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002118 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002119}
2120
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002121static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2122{
2123 int dma_irq;
2124
2125 if (cpu_class_is_omap2()) {
2126 char irq_name[4];
2127 strcpy(irq_name, "0");
2128 dma_irq = platform_get_irq_byname(pdev, irq_name);
2129 remove_irq(dma_irq, &omap24xx_dma_irq);
2130 } else {
2131 int irq_rel = 0;
2132 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2133 dma_irq = platform_get_irq(pdev, irq_rel);
2134 free_irq(dma_irq, (void *)(irq_rel + 1));
2135 }
2136 }
2137 kfree(p);
2138 kfree(d);
2139 kfree(dma_chan);
2140 return 0;
2141}
2142
2143static struct platform_driver omap_system_dma_driver = {
2144 .probe = omap_system_dma_probe,
Tony Lindgren3e2e6132012-02-23 14:58:08 -08002145 .remove = __devexit_p(omap_system_dma_remove),
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002146 .driver = {
2147 .name = "omap_dma_system"
2148 },
2149};
2150
2151static int __init omap_system_dma_init(void)
2152{
2153 return platform_driver_register(&omap_system_dma_driver);
2154}
2155arch_initcall(omap_system_dma_init);
2156
2157static void __exit omap_system_dma_exit(void)
2158{
2159 platform_driver_unregister(&omap_system_dma_driver);
2160}
2161
2162MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2163MODULE_LICENSE("GPL");
2164MODULE_ALIAS("platform:" DRIVER_NAME);
2165MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002166
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002167/*
2168 * Reserve the omap SDMA channels using cmdline bootarg
2169 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2170 */
2171static int __init omap_dma_cmdline_reserve_ch(char *str)
2172{
2173 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2174 omap_dma_reserve_channels = 0;
2175 return 1;
2176}
2177
2178__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2179
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002180