blob: 612227efe34f8bd6000d7aae35f801589fd1f841 [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 Gleixner418ca1f2006-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
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010041
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/tc.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010043
Paul Walmsleybc4d8b52012-04-13 06:34:30 -060044/*
45 * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
46 * channels that an instance of the SDMA IP block can support. Used
47 * to size arrays. (The actual maximum on a particular SoC may be less
48 * than this -- for example, OMAP1 SDMA instances only support 17 logical
49 * DMA channels.)
50 */
51#define MAX_LOGICAL_DMA_CH_COUNT 32
52
Anand Gadiyarf8151e52007-12-01 12:14:11 -080053#undef DEBUG
54
55#ifndef CONFIG_ARCH_OMAP1
56enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
57 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
58};
59
60enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000061#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062
Tony Lindgren97b7f712008-07-03 12:24:37 +030063#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020064#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065
Tony Lindgren97b7f712008-07-03 12:24:37 +030066#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080068static struct omap_system_dma_plat_info *p;
69static struct omap_dma_dev_attr *d;
70
Tony Lindgren97b7f712008-07-03 12:24:37 +030071static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080072static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010073
Tero Kristof2d11852008-08-28 13:13:31 +000074static struct omap_dma_global_context_registers {
75 u32 dma_irqenable_l0;
76 u32 dma_ocp_sysconfig;
77 u32 dma_gcr;
78} omap_dma_global_context;
79
Anand Gadiyarf8151e52007-12-01 12:14:11 -080080struct dma_link_info {
81 int *linked_dmach_q;
82 int no_of_lchs_linked;
83
84 int q_count;
85 int q_tail;
86 int q_head;
87
88 int chain_state;
89 int chain_mode;
90
91};
92
Tony Lindgren4d963722008-07-03 12:24:31 +030093static struct dma_link_info *dma_linked_lch;
94
95#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080096
97/* Chain handling macros */
98#define OMAP_DMA_CHAIN_QINIT(chain_id) \
99 do { \
100 dma_linked_lch[chain_id].q_head = \
101 dma_linked_lch[chain_id].q_tail = \
102 dma_linked_lch[chain_id].q_count = 0; \
103 } while (0)
104#define OMAP_DMA_CHAIN_QFULL(chain_id) \
105 (dma_linked_lch[chain_id].no_of_lchs_linked == \
106 dma_linked_lch[chain_id].q_count)
107#define OMAP_DMA_CHAIN_QLAST(chain_id) \
108 do { \
109 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
110 dma_linked_lch[chain_id].q_count) \
111 } while (0)
112#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
113 (0 == dma_linked_lch[chain_id].q_count)
114#define __OMAP_DMA_CHAIN_INCQ(end) \
115 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
116#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
117 do { \
118 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
119 dma_linked_lch[chain_id].q_count--; \
120 } while (0)
121
122#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
123 do { \
124 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
125 dma_linked_lch[chain_id].q_count++; \
126 } while (0)
127#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300128
129static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700131static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
133static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300134static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800136static inline void disable_lnk(int lch);
137static void omap_disable_channel_irq(int lch);
138static inline void omap_enable_channel_irq(int lch);
139
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000140#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800141 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142
143#ifdef CONFIG_ARCH_OMAP15XX
144/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800145static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000146{
147 return enable_1510_mode;
148}
149#else
150#define omap_dma_in_1510_mode() 0
151#endif
152
153#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154static inline int get_gdma_dev(int req)
155{
156 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
157 int shift = ((req - 1) % 5) * 6;
158
159 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
160}
161
162static inline void set_gdma_dev(int req, int dev)
163{
164 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
165 int shift = ((req - 1) % 5) * 6;
166 u32 l;
167
168 l = omap_readl(reg);
169 l &= ~(0x3f << shift);
170 l |= (dev - 1) << shift;
171 omap_writel(l, reg);
172}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000173#else
174#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800175#define omap_readl(reg) 0
176#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000177#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100178
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300179void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100180{
181 unsigned long reg;
182 u32 l;
183
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300184 if (cpu_class_is_omap1()) {
185 switch (dst_port) {
186 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
187 reg = OMAP_TC_OCPT1_PRIOR;
188 break;
189 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
190 reg = OMAP_TC_OCPT2_PRIOR;
191 break;
192 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
193 reg = OMAP_TC_EMIFF_PRIOR;
194 break;
195 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
196 reg = OMAP_TC_EMIFS_PRIOR;
197 break;
198 default:
199 BUG();
200 return;
201 }
202 l = omap_readl(reg);
203 l &= ~(0xf << 8);
204 l |= (priority & 0xf) << 8;
205 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300207
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800208 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300209 u32 ccr;
210
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800211 ccr = p->dma_read(CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300212 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300213 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300214 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300215 ccr &= ~(1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800216 p->dma_write(ccr, CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300217 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100218}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300219EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100220
221void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000222 int frame_count, int sync_mode,
223 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100224{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300225 u32 l;
226
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800227 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300228 l &= ~0x03;
229 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800230 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000232 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300233 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100234
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800235 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300236 ccr &= ~(1 << 5);
237 if (sync_mode == OMAP_DMA_SYNC_FRAME)
238 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800239 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300240
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800241 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300242 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000243 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300244 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800245 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000246 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100247
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800248 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300249 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100250
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800251 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100252
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200253 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300254 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200255 val |= (dma_trigger & ~0x1f) << 14;
256 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000257
258 if (sync_mode & OMAP_DMA_SYNC_FRAME)
259 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700260 else
261 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000262
263 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
264 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700265 else
266 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000267
Samu Onkalo72a11792010-08-02 14:21:40 +0300268 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000269 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300270 val |= (1 << 23); /* Prefetch */
271 } else if (src_or_dst_synch) {
272 val |= 1 << 24; /* source synch */
273 } else {
274 val &= ~(1 << 24); /* dest synch */
275 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800276 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000277 }
278
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800279 p->dma_write(elem_count, CEN, lch);
280 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100281}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300282EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000283
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100284void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
285{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286 BUG_ON(omap_dma_in_1510_mode());
287
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700288 if (cpu_class_is_omap1()) {
289 u16 w;
290
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800291 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700292 w &= ~0x03;
293
294 switch (mode) {
295 case OMAP_DMA_CONSTANT_FILL:
296 w |= 0x01;
297 break;
298 case OMAP_DMA_TRANSPARENT_COPY:
299 w |= 0x02;
300 break;
301 case OMAP_DMA_COLOR_DIS:
302 break;
303 default:
304 BUG();
305 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800306 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700307
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800308 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700309 w &= ~0x0f;
310 /* Default is channel type 2D */
311 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800312 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700313 w |= 1; /* Channel type G */
314 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800315 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700316 }
317
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800318 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700319 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000320
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800321 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700322 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300323
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700324 switch (mode) {
325 case OMAP_DMA_CONSTANT_FILL:
326 val |= 1 << 16;
327 break;
328 case OMAP_DMA_TRANSPARENT_COPY:
329 val |= 1 << 17;
330 break;
331 case OMAP_DMA_COLOR_DIS:
332 break;
333 default:
334 BUG();
335 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800336 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100337
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700338 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800339 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100341}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300342EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300344void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
345{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800346 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300347 u32 csdp;
348
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800349 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300350 csdp &= ~(0x3 << 16);
351 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800352 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300353 }
354}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300355EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300356
Tony Lindgren0499bde2008-07-03 12:24:36 +0300357void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
358{
359 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
360 u32 l;
361
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800362 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300363 l &= ~0x7;
364 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800365 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300366 }
367}
368EXPORT_SYMBOL(omap_set_dma_channel_mode);
369
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000370/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100371void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000372 unsigned long src_start,
373 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300375 u32 l;
376
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000377 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300378 u16 w;
379
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800380 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300381 w &= ~(0x1f << 2);
382 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800383 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300384 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300385
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800386 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300387 l &= ~(0x03 << 12);
388 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800389 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300390
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800391 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100392
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800393 p->dma_write(src_ei, CSEI, lch);
394 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300395}
396EXPORT_SYMBOL(omap_set_dma_src_params);
397
398void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000399{
400 omap_set_dma_transfer_params(lch, params->data_type,
401 params->elem_count, params->frame_count,
402 params->sync_mode, params->trigger,
403 params->src_or_dst_synch);
404 omap_set_dma_src_params(lch, params->src_port,
405 params->src_amode, params->src_start,
406 params->src_ei, params->src_fi);
407
408 omap_set_dma_dest_params(lch, params->dst_port,
409 params->dst_amode, params->dst_start,
410 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800411 if (params->read_prio || params->write_prio)
412 omap_dma_set_prio_lch(lch, params->read_prio,
413 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300415EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416
417void omap_set_dma_src_index(int lch, int eidx, int fidx)
418{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300419 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000420 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300421
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800422 p->dma_write(eidx, CSEI, lch);
423 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100424}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300425EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426
427void omap_set_dma_src_data_pack(int lch, int enable)
428{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300429 u32 l;
430
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800431 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300432 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000433 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300434 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800435 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300437EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438
439void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
440{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700441 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300442 u32 l;
443
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800444 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300445 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447 switch (burst_mode) {
448 case OMAP_DMA_DATA_BURST_DIS:
449 break;
450 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800451 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700452 burst = 0x1;
453 else
454 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 break;
456 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800457 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700458 burst = 0x2;
459 break;
460 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700461 /*
462 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100463 * w |= (0x03 << 7);
464 * fall through
465 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700466 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800467 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700468 burst = 0x3;
469 break;
470 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700471 /*
472 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700473 * fall through
474 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475 default:
476 BUG();
477 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300478
479 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800480 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300482EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000484/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000486 unsigned long dest_start,
487 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300489 u32 l;
490
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000491 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800492 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300493 l &= ~(0x1f << 9);
494 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800495 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000496 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100497
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800498 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300499 l &= ~(0x03 << 14);
500 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800501 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800503 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800505 p->dma_write(dst_ei, CDEI, lch);
506 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300508EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509
510void omap_set_dma_dest_index(int lch, int eidx, int fidx)
511{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300512 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000513 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300514
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800515 p->dma_write(eidx, CDEI, lch);
516 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100517}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300518EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519
520void omap_set_dma_dest_data_pack(int lch, int enable)
521{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300522 u32 l;
523
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800524 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300525 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000526 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300527 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800528 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300530EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531
532void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
533{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700534 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300535 u32 l;
536
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800537 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300538 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540 switch (burst_mode) {
541 case OMAP_DMA_DATA_BURST_DIS:
542 break;
543 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800544 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700545 burst = 0x1;
546 else
547 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548 break;
549 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800550 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700551 burst = 0x2;
552 else
553 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700555 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800556 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700557 burst = 0x3;
558 break;
559 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700560 /*
561 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700562 * fall through
563 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564 default:
565 printk(KERN_ERR "Invalid DMA burst mode\n");
566 BUG();
567 return;
568 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300569 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800570 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100571}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300572EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000574static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000576 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700578 /* Clear CSR */
579 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800580 status = p->dma_read(CSR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800581 else if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800582 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000583
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100584 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800585 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586}
587
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000588static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800590 if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800591 p->dma_write(0, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100592}
593
594void omap_enable_dma_irq(int lch, u16 bits)
595{
596 dma_chan[lch].enabled_irqs |= bits;
597}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300598EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599
600void omap_disable_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_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000606static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300608 u32 l;
609
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800610 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300611
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000612 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300613 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100614
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000615 /* Set the ENABLE_LNK bits */
616 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300617 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800618
619#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300620 if (cpu_class_is_omap2())
621 if (dma_chan[lch].next_linked_ch != -1)
622 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800623#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300624
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800625 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100626}
627
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000628static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300630 u32 l;
631
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800632 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300633
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000634 /* Disable interrupts */
635 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800636 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000637 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300638 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100639 }
640
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800641 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000642 omap_disable_channel_irq(lch);
643 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300644 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000645 }
646
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800647 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000648 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
649}
650
651static inline void omap2_enable_irq_lch(int lch)
652{
653 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800654 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000655
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800656 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000657 return;
658
Tao Huee907322009-11-10 18:55:17 -0800659 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800660 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000661 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800662 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800663 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664}
665
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700666static inline void omap2_disable_irq_lch(int lch)
667{
668 u32 val;
669 unsigned long flags;
670
671 if (!cpu_class_is_omap2())
672 return;
673
674 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800675 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700676 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800677 p->dma_write(val, IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700678 spin_unlock_irqrestore(&dma_chan_lock, flags);
679}
680
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100681int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300682 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683 void *data, int *dma_ch_out)
684{
685 int ch, free_ch = -1;
686 unsigned long flags;
687 struct omap_dma_lch *chan;
688
689 spin_lock_irqsave(&dma_chan_lock, flags);
690 for (ch = 0; ch < dma_chan_count; ch++) {
691 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
692 free_ch = ch;
693 if (dev_id == 0)
694 break;
695 }
696 }
697 if (free_ch == -1) {
698 spin_unlock_irqrestore(&dma_chan_lock, flags);
699 return -EBUSY;
700 }
701 chan = dma_chan + free_ch;
702 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000703
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800704 if (p->clear_lch_regs)
705 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000706
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800707 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000708 omap_clear_dma(free_ch);
709
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100710 spin_unlock_irqrestore(&dma_chan_lock, flags);
711
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712 chan->dev_name = dev_name;
713 chan->callback = callback;
714 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800715 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300716
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800717#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300718 if (cpu_class_is_omap2()) {
719 chan->chain_id = -1;
720 chan->next_linked_ch = -1;
721 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800722#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300723
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700724 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000725
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700726 if (cpu_class_is_omap1())
727 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800728 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700729 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
730 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731
732 if (cpu_is_omap16xx()) {
733 /* If the sync device is set, configure it dynamically. */
734 if (dev_id != 0) {
735 set_gdma_dev(free_ch + 1, dev_id);
736 dev_id = free_ch + 1;
737 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300738 /*
739 * Disable the 1510 compatibility mode and set the sync device
740 * id.
741 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800742 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700743 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800744 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000746
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800747 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000748 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000749 omap_enable_channel_irq(free_ch);
750 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800751 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, free_ch);
752 p->dma_write(1 << free_ch, IRQSTATUS_L0, 0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000753 }
754
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100755 *dma_ch_out = free_ch;
756
757 return 0;
758}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300759EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000761void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100762{
763 unsigned long flags;
764
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000765 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300766 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000767 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768 return;
769 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300770
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000771 if (cpu_class_is_omap1()) {
772 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800773 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800775 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000776 }
777
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800778 if (cpu_class_is_omap2()) {
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700779 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000780
781 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800782 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
783 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000784
785 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800786 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000787
788 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800789 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000790 omap_clear_dma(lch);
791 }
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700792
793 spin_lock_irqsave(&dma_chan_lock, flags);
794 dma_chan[lch].dev_id = -1;
795 dma_chan[lch].next_lch = -1;
796 dma_chan[lch].callback = NULL;
797 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100798}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300799EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100800
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800801/**
802 * @brief omap_dma_set_global_params : Set global priority settings for dma
803 *
804 * @param arb_rate
805 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700806 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
807 * DMA_THREAD_RESERVE_ONET
808 * DMA_THREAD_RESERVE_TWOT
809 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800810 */
811void
812omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
813{
814 u32 reg;
815
816 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800817 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800818 return;
819 }
820
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700821 if (max_fifo_depth == 0)
822 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800823 if (arb_rate == 0)
824 arb_rate = 1;
825
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700826 reg = 0xff & max_fifo_depth;
827 reg |= (0x3 & tparams) << 12;
828 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800829
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800830 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800831}
832EXPORT_SYMBOL(omap_dma_set_global_params);
833
834/**
835 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
836 *
837 * @param lch
838 * @param read_prio - Read priority
839 * @param write_prio - Write priority
840 * Both of the above can be set with one of the following values :
841 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
842 */
843int
844omap_dma_set_prio_lch(int lch, unsigned char read_prio,
845 unsigned char write_prio)
846{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300847 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800848
Tony Lindgren4d963722008-07-03 12:24:31 +0300849 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800850 printk(KERN_ERR "Invalid channel id\n");
851 return -EINVAL;
852 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800853 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300854 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkar44169072009-05-28 14:16:04 -0700855 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300856 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800857 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300858 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800859
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800860 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300861
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800862 return 0;
863}
864EXPORT_SYMBOL(omap_dma_set_prio_lch);
865
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000866/*
867 * Clears any DMA state so the DMA engine is ready to restart with new buffers
868 * through omap_start_dma(). Any buffers in flight are discarded.
869 */
870void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100871{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000872 unsigned long flags;
873
874 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800875 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000876 local_irq_restore(flags);
877}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300878EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000879
880void omap_start_dma(int lch)
881{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300882 u32 l;
883
manjugk manjugk519e6162010-03-04 07:11:56 +0000884 /*
885 * The CPC/CDAC register needs to be initialized to zero
886 * before starting dma transfer.
887 */
888 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800889 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000890 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800891 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000892
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000893 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
894 int next_lch, cur_lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600895 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000896
897 dma_chan_link_map[lch] = 1;
898 /* Set the link register of the first channel */
899 enable_lnk(lch);
900
901 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
902 cur_lch = dma_chan[lch].next_lch;
903 do {
904 next_lch = dma_chan[cur_lch].next_lch;
905
906 /* The loop case: we've been here already */
907 if (dma_chan_link_map[cur_lch])
908 break;
909 /* Mark the current channel */
910 dma_chan_link_map[cur_lch] = 1;
911
912 enable_lnk(cur_lch);
913 omap_enable_channel_irq(cur_lch);
914
915 cur_lch = next_lch;
916 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800917 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800918 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000919
920 omap_enable_channel_irq(lch);
921
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800922 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300923
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800924 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
925 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300926 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800927
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800928 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000929
930 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
931}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300932EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000933
934void omap_stop_dma(int lch)
935{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300936 u32 l;
937
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700938 /* Disable all interrupts on the channel */
939 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800940 p->dma_write(0, CICR, lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700941
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800942 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800943 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
944 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700945 int i = 0;
946 u32 sys_cf;
947
948 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800949 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700950 sys_cf = l;
951 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
952 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800953 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700954
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800955 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700956 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800957 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700958
959 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800960 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700961 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
962 OMAP_DMA_CCR_WR_ACTIVE))) {
963 udelay(5);
964 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800965 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700966 }
967 if (i >= 100)
968 printk(KERN_ERR "DMA drain did not complete on "
969 "lch %d\n", lch);
970 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800971 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700972 } else {
973 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800974 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700975 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700976
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000977 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
978 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600979 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000980
981 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
982 do {
983 /* The loop case: we've been here already */
984 if (dma_chan_link_map[cur_lch])
985 break;
986 /* Mark the current channel */
987 dma_chan_link_map[cur_lch] = 1;
988
989 disable_lnk(cur_lch);
990
991 next_lch = dma_chan[cur_lch].next_lch;
992 cur_lch = next_lch;
993 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000994 }
995
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000996 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
997}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300998EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000999
1000/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001001 * Allows changing the DMA callback function or data. This may be needed if
1002 * the driver shares a single DMA channel for multiple dma triggers.
1003 */
1004int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001005 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001006 void *data)
1007{
1008 unsigned long flags;
1009
1010 if (lch < 0)
1011 return -ENODEV;
1012
1013 spin_lock_irqsave(&dma_chan_lock, flags);
1014 if (dma_chan[lch].dev_id == -1) {
1015 printk(KERN_ERR "DMA callback for not set for free channel\n");
1016 spin_unlock_irqrestore(&dma_chan_lock, flags);
1017 return -EINVAL;
1018 }
1019 dma_chan[lch].callback = callback;
1020 dma_chan[lch].data = data;
1021 spin_unlock_irqrestore(&dma_chan_lock, flags);
1022
1023 return 0;
1024}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001025EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001026
1027/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001028 * Returns current physical source address for the given DMA channel.
1029 * If the channel is running the caller must disable interrupts prior calling
1030 * this function and process the returned value before re-enabling interrupt to
1031 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001032 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001033 * in incorrect return value.
1034 */
1035dma_addr_t omap_get_dma_src_pos(int lch)
1036{
Tony Lindgren0695de32007-05-07 18:24:14 -07001037 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001038
Tony Lindgren0499bde2008-07-03 12:24:36 +03001039 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001040 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001041 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001042 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001043
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001044 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001045 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001046
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001047 if (!cpu_is_omap15xx()) {
1048 /*
1049 * CDAC == 0 indicates that the DMA transfer on the channel has
1050 * not been started (no data has been transferred so far).
1051 * Return the programmed source start address in this case.
1052 */
1053 if (likely(p->dma_read(CDAC, lch)))
1054 offset = p->dma_read(CSAC, lch);
1055 else
1056 offset = p->dma_read(CSSA, lch);
1057 }
1058
Tony Lindgren0499bde2008-07-03 12:24:36 +03001059 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001060 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001061
1062 return offset;
1063}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001064EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001065
1066/*
1067 * Returns current physical destination address for the given DMA channel.
1068 * If the channel is running the caller must disable interrupts prior calling
1069 * this function and process the returned value before re-enabling interrupt to
1070 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001071 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001072 * in incorrect return value.
1073 */
1074dma_addr_t omap_get_dma_dst_pos(int lch)
1075{
Tony Lindgren0695de32007-05-07 18:24:14 -07001076 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001077
Tony Lindgren0499bde2008-07-03 12:24:36 +03001078 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001079 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001080 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001081 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001082
Tony Lindgren0499bde2008-07-03 12:24:36 +03001083 /*
1084 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1085 * read before the DMA controller finished disabling the channel.
1086 */
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001087 if (!cpu_is_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001088 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001089 /*
1090 * CDAC == 0 indicates that the DMA transfer on the channel has
1091 * not been started (no data has been transferred so far).
1092 * Return the programmed destination start address in this case.
1093 */
1094 if (unlikely(!offset))
1095 offset = p->dma_read(CDSA, lch);
1096 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001097
1098 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001099 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001100
1101 return offset;
1102}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001103EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001104
Tony Lindgren0499bde2008-07-03 12:24:36 +03001105int omap_get_dma_active_status(int lch)
1106{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001107 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001108}
1109EXPORT_SYMBOL(omap_get_dma_active_status);
1110
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001111int omap_dma_running(void)
1112{
1113 int lch;
1114
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001115 if (cpu_class_is_omap1())
1116 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001117 return 1;
1118
1119 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001120 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001121 return 1;
1122
1123 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001124}
1125
1126/*
1127 * lch_queue DMA will start right after lch_head one is finished.
1128 * For this DMA link to start, you still need to start (see omap_start_dma)
1129 * the first one. That will fire up the entire queue.
1130 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001131void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001132{
1133 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001134 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001135 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001136 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001137 return;
1138 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001139 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1140 BUG();
1141 return;
1142 }
1143
1144 if ((dma_chan[lch_head].dev_id == -1) ||
1145 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001146 printk(KERN_ERR "omap_dma: trying to link "
1147 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001148 dump_stack();
1149 }
1150
1151 dma_chan[lch_head].next_lch = lch_queue;
1152}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001153EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001154
1155/*
1156 * Once the DMA queue is stopped, we can destroy it.
1157 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001158void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001159{
1160 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001161 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001162 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001163 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001164 return;
1165 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1167 BUG();
1168 return;
1169 }
1170
1171 if (dma_chan[lch_head].next_lch != lch_queue ||
1172 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001173 printk(KERN_ERR "omap_dma: trying to unlink "
1174 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001175 dump_stack();
1176 }
1177
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001179 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001180 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1181 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001182 dump_stack();
1183 }
1184
1185 dma_chan[lch_head].next_lch = -1;
1186}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001187EXPORT_SYMBOL(omap_dma_unlink_lch);
1188
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001189#ifndef CONFIG_ARCH_OMAP1
1190/* Create chain of DMA channesls */
1191static void create_dma_lch_chain(int lch_head, int lch_queue)
1192{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001193 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001194
1195 /* Check if this is the first link in chain */
1196 if (dma_chan[lch_head].next_linked_ch == -1) {
1197 dma_chan[lch_head].next_linked_ch = lch_queue;
1198 dma_chan[lch_head].prev_linked_ch = lch_queue;
1199 dma_chan[lch_queue].next_linked_ch = lch_head;
1200 dma_chan[lch_queue].prev_linked_ch = lch_head;
1201 }
1202
1203 /* a link exists, link the new channel in circular chain */
1204 else {
1205 dma_chan[lch_queue].next_linked_ch =
1206 dma_chan[lch_head].next_linked_ch;
1207 dma_chan[lch_queue].prev_linked_ch = lch_head;
1208 dma_chan[lch_head].next_linked_ch = lch_queue;
1209 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1210 lch_queue;
1211 }
1212
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001213 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001214 l &= ~(0x1f);
1215 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001216 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001217
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001218 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001219 l &= ~(0x1f);
1220 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001221 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001222}
1223
1224/**
1225 * @brief omap_request_dma_chain : Request a chain of DMA channels
1226 *
1227 * @param dev_id - Device id using the dma channel
1228 * @param dev_name - Device name
1229 * @param callback - Call back function
1230 * @chain_id -
1231 * @no_of_chans - Number of channels requested
1232 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1233 * OMAP_DMA_DYNAMIC_CHAIN
1234 * @params - Channel parameters
1235 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001236 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001237 * Failure: -EINVAL/-ENOMEM
1238 */
1239int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001240 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001241 void *data),
1242 int *chain_id, int no_of_chans, int chain_mode,
1243 struct omap_dma_channel_params params)
1244{
1245 int *channels;
1246 int i, err;
1247
1248 /* Is the chain mode valid ? */
1249 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1250 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1251 printk(KERN_ERR "Invalid chain mode requested\n");
1252 return -EINVAL;
1253 }
1254
1255 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001256 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001257 printk(KERN_ERR "Invalid Number of channels requested\n");
1258 return -EINVAL;
1259 }
1260
manjugk manjugkea221a62010-05-14 12:05:25 -07001261 /*
1262 * Allocate a queue to maintain the status of the channels
1263 * in the chain
1264 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001265 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1266 if (channels == NULL) {
1267 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1268 return -ENOMEM;
1269 }
1270
1271 /* request and reserve DMA channels for the chain */
1272 for (i = 0; i < no_of_chans; i++) {
1273 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001274 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001275 if (err < 0) {
1276 int j;
1277 for (j = 0; j < i; j++)
1278 omap_free_dma(channels[j]);
1279 kfree(channels);
1280 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1281 return err;
1282 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001283 dma_chan[channels[i]].prev_linked_ch = -1;
1284 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1285
1286 /*
1287 * Allowing client drivers to set common parameters now,
1288 * so that later only relevant (src_start, dest_start
1289 * and element count) can be set
1290 */
1291 omap_set_dma_params(channels[i], &params);
1292 }
1293
1294 *chain_id = channels[0];
1295 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1296 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1297 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1298 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1299
1300 for (i = 0; i < no_of_chans; i++)
1301 dma_chan[channels[i]].chain_id = *chain_id;
1302
1303 /* Reset the Queue pointers */
1304 OMAP_DMA_CHAIN_QINIT(*chain_id);
1305
1306 /* Set up the chain */
1307 if (no_of_chans == 1)
1308 create_dma_lch_chain(channels[0], channels[0]);
1309 else {
1310 for (i = 0; i < (no_of_chans - 1); i++)
1311 create_dma_lch_chain(channels[i], channels[i + 1]);
1312 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001313
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001314 return 0;
1315}
1316EXPORT_SYMBOL(omap_request_dma_chain);
1317
1318/**
1319 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1320 * params after setting it. Dont do this while dma is running!!
1321 *
1322 * @param chain_id - Chained logical channel id.
1323 * @param params
1324 *
1325 * @return - Success : 0
1326 * Failure : -EINVAL
1327 */
1328int omap_modify_dma_chain_params(int chain_id,
1329 struct omap_dma_channel_params params)
1330{
1331 int *channels;
1332 u32 i;
1333
1334 /* Check for input params */
1335 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001336 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001337 printk(KERN_ERR "Invalid chain id\n");
1338 return -EINVAL;
1339 }
1340
1341 /* Check if the chain exists */
1342 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1343 printk(KERN_ERR "Chain doesn't exists\n");
1344 return -EINVAL;
1345 }
1346 channels = dma_linked_lch[chain_id].linked_dmach_q;
1347
1348 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1349 /*
1350 * Allowing client drivers to set common parameters now,
1351 * so that later only relevant (src_start, dest_start
1352 * and element count) can be set
1353 */
1354 omap_set_dma_params(channels[i], &params);
1355 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001356
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001357 return 0;
1358}
1359EXPORT_SYMBOL(omap_modify_dma_chain_params);
1360
1361/**
1362 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1363 *
1364 * @param chain_id
1365 *
1366 * @return - Success : 0
1367 * Failure : -EINVAL
1368 */
1369int omap_free_dma_chain(int chain_id)
1370{
1371 int *channels;
1372 u32 i;
1373
1374 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001375 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001376 printk(KERN_ERR "Invalid chain id\n");
1377 return -EINVAL;
1378 }
1379
1380 /* Check if the chain exists */
1381 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1382 printk(KERN_ERR "Chain doesn't exists\n");
1383 return -EINVAL;
1384 }
1385
1386 channels = dma_linked_lch[chain_id].linked_dmach_q;
1387 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1388 dma_chan[channels[i]].next_linked_ch = -1;
1389 dma_chan[channels[i]].prev_linked_ch = -1;
1390 dma_chan[channels[i]].chain_id = -1;
1391 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1392 omap_free_dma(channels[i]);
1393 }
1394
1395 kfree(channels);
1396
1397 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1398 dma_linked_lch[chain_id].chain_mode = -1;
1399 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001400
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001401 return (0);
1402}
1403EXPORT_SYMBOL(omap_free_dma_chain);
1404
1405/**
1406 * @brief omap_dma_chain_status - Check if the chain is in
1407 * active / inactive state.
1408 * @param chain_id
1409 *
1410 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1411 * Failure : -EINVAL
1412 */
1413int omap_dma_chain_status(int chain_id)
1414{
1415 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001416 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001417 printk(KERN_ERR "Invalid chain id\n");
1418 return -EINVAL;
1419 }
1420
1421 /* Check if the chain exists */
1422 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1423 printk(KERN_ERR "Chain doesn't exists\n");
1424 return -EINVAL;
1425 }
1426 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1427 dma_linked_lch[chain_id].q_count);
1428
1429 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1430 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001431
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001432 return OMAP_DMA_CHAIN_ACTIVE;
1433}
1434EXPORT_SYMBOL(omap_dma_chain_status);
1435
1436/**
1437 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1438 * set the params and start the transfer.
1439 *
1440 * @param chain_id
1441 * @param src_start - buffer start address
1442 * @param dest_start - Dest address
1443 * @param elem_count
1444 * @param frame_count
1445 * @param callbk_data - channel callback parameter data.
1446 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301447 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001448 * Failure: -EINVAL/-EBUSY
1449 */
1450int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1451 int elem_count, int frame_count, void *callbk_data)
1452{
1453 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001454 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001455 int start_dma = 0;
1456
Tony Lindgren97b7f712008-07-03 12:24:37 +03001457 /*
1458 * if buffer size is less than 1 then there is
1459 * no use of starting the chain
1460 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001461 if (elem_count < 1) {
1462 printk(KERN_ERR "Invalid buffer size\n");
1463 return -EINVAL;
1464 }
1465
1466 /* Check for input params */
1467 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001468 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001469 printk(KERN_ERR "Invalid chain id\n");
1470 return -EINVAL;
1471 }
1472
1473 /* Check if the chain exists */
1474 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1475 printk(KERN_ERR "Chain doesn't exist\n");
1476 return -EINVAL;
1477 }
1478
1479 /* Check if all the channels in chain are in use */
1480 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1481 return -EBUSY;
1482
1483 /* Frame count may be negative in case of indexed transfers */
1484 channels = dma_linked_lch[chain_id].linked_dmach_q;
1485
1486 /* Get a free channel */
1487 lch = channels[dma_linked_lch[chain_id].q_tail];
1488
1489 /* Store the callback data */
1490 dma_chan[lch].data = callbk_data;
1491
1492 /* Increment the q_tail */
1493 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1494
1495 /* Set the params to the free channel */
1496 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001497 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001498 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001499 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001500
1501 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001502 p->dma_write(elem_count, CEN, lch);
1503 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001504
Tony Lindgren97b7f712008-07-03 12:24:37 +03001505 /*
1506 * If the chain is dynamically linked,
1507 * then we may have to start the chain if its not active
1508 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001509 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1510
Tony Lindgren97b7f712008-07-03 12:24:37 +03001511 /*
1512 * In Dynamic chain, if the chain is not started,
1513 * queue the channel
1514 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001515 if (dma_linked_lch[chain_id].chain_state ==
1516 DMA_CHAIN_NOTSTARTED) {
1517 /* Enable the link in previous channel */
1518 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1519 DMA_CH_QUEUED)
1520 enable_lnk(dma_chan[lch].prev_linked_ch);
1521 dma_chan[lch].state = DMA_CH_QUEUED;
1522 }
1523
Tony Lindgren97b7f712008-07-03 12:24:37 +03001524 /*
1525 * Chain is already started, make sure its active,
1526 * if not then start the chain
1527 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001528 else {
1529 start_dma = 1;
1530
1531 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1532 DMA_CH_STARTED) {
1533 enable_lnk(dma_chan[lch].prev_linked_ch);
1534 dma_chan[lch].state = DMA_CH_QUEUED;
1535 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001536 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001537 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001538 disable_lnk(dma_chan[lch].
1539 prev_linked_ch);
1540 pr_debug("\n prev ch is stopped\n");
1541 start_dma = 1;
1542 }
1543 }
1544
1545 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1546 == DMA_CH_QUEUED) {
1547 enable_lnk(dma_chan[lch].prev_linked_ch);
1548 dma_chan[lch].state = DMA_CH_QUEUED;
1549 start_dma = 0;
1550 }
1551 omap_enable_channel_irq(lch);
1552
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001553 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001554
Tony Lindgren0499bde2008-07-03 12:24:36 +03001555 if ((0 == (l & (1 << 24))))
1556 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001557 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001558 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001559 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001560 if (0 == (l & (1 << 7))) {
1561 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001562 dma_chan[lch].state = DMA_CH_STARTED;
1563 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001564 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001565 } else
1566 start_dma = 0;
1567 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001568 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001569 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001570 }
1571 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1572 }
1573 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001574
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301575 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001576}
1577EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1578
1579/**
1580 * @brief omap_start_dma_chain_transfers - Start the chain
1581 *
1582 * @param chain_id
1583 *
1584 * @return - Success : 0
1585 * Failure : -EINVAL/-EBUSY
1586 */
1587int omap_start_dma_chain_transfers(int chain_id)
1588{
1589 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001590 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001591
Tony Lindgren4d963722008-07-03 12:24:31 +03001592 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001593 printk(KERN_ERR "Invalid chain id\n");
1594 return -EINVAL;
1595 }
1596
1597 channels = dma_linked_lch[chain_id].linked_dmach_q;
1598
1599 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1600 printk(KERN_ERR "Chain is already started\n");
1601 return -EBUSY;
1602 }
1603
1604 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1605 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1606 i++) {
1607 enable_lnk(channels[i]);
1608 omap_enable_channel_irq(channels[i]);
1609 }
1610 } else {
1611 omap_enable_channel_irq(channels[0]);
1612 }
1613
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001614 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001615 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001616 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1617 dma_chan[channels[0]].state = DMA_CH_STARTED;
1618
Tony Lindgren0499bde2008-07-03 12:24:36 +03001619 if ((0 == (l & (1 << 24))))
1620 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001621 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001622 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001623 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001624
1625 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001626
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001627 return 0;
1628}
1629EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1630
1631/**
1632 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1633 *
1634 * @param chain_id
1635 *
1636 * @return - Success : 0
1637 * Failure : EINVAL
1638 */
1639int omap_stop_dma_chain_transfers(int chain_id)
1640{
1641 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001642 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001643 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001644
1645 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001646 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001647 printk(KERN_ERR "Invalid chain id\n");
1648 return -EINVAL;
1649 }
1650
1651 /* Check if the chain exists */
1652 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1653 printk(KERN_ERR "Chain doesn't exists\n");
1654 return -EINVAL;
1655 }
1656 channels = dma_linked_lch[chain_id].linked_dmach_q;
1657
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001658 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001659 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001660 l = sys_cf;
1661 /* Middle mode reg set no Standby */
1662 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001663 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001664 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001665
1666 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1667
1668 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001669 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001670 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001671 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001672
1673 /* Disable the link in all the channels */
1674 disable_lnk(channels[i]);
1675 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1676
1677 }
1678 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1679
1680 /* Reset the Queue pointers */
1681 OMAP_DMA_CHAIN_QINIT(chain_id);
1682
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001683 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001684 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001685
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001686 return 0;
1687}
1688EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1689
1690/* Get the index of the ongoing DMA in chain */
1691/**
1692 * @brief omap_get_dma_chain_index - Get the element and frame index
1693 * of the ongoing DMA in chain
1694 *
1695 * @param chain_id
1696 * @param ei - Element index
1697 * @param fi - Frame index
1698 *
1699 * @return - Success : 0
1700 * Failure : -EINVAL
1701 */
1702int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1703{
1704 int lch;
1705 int *channels;
1706
1707 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001708 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001709 printk(KERN_ERR "Invalid chain id\n");
1710 return -EINVAL;
1711 }
1712
1713 /* Check if the chain exists */
1714 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1715 printk(KERN_ERR "Chain doesn't exists\n");
1716 return -EINVAL;
1717 }
1718 if ((!ei) || (!fi))
1719 return -EINVAL;
1720
1721 channels = dma_linked_lch[chain_id].linked_dmach_q;
1722
1723 /* Get the current channel */
1724 lch = channels[dma_linked_lch[chain_id].q_head];
1725
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001726 *ei = p->dma_read(CCEN, lch);
1727 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001728
1729 return 0;
1730}
1731EXPORT_SYMBOL(omap_get_dma_chain_index);
1732
1733/**
1734 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1735 * ongoing DMA in chain
1736 *
1737 * @param chain_id
1738 *
1739 * @return - Success : Destination position
1740 * Failure : -EINVAL
1741 */
1742int omap_get_dma_chain_dst_pos(int chain_id)
1743{
1744 int lch;
1745 int *channels;
1746
1747 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001748 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001749 printk(KERN_ERR "Invalid chain id\n");
1750 return -EINVAL;
1751 }
1752
1753 /* Check if the chain exists */
1754 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1755 printk(KERN_ERR "Chain doesn't exists\n");
1756 return -EINVAL;
1757 }
1758
1759 channels = dma_linked_lch[chain_id].linked_dmach_q;
1760
1761 /* Get the current channel */
1762 lch = channels[dma_linked_lch[chain_id].q_head];
1763
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001764 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001765}
1766EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1767
1768/**
1769 * @brief omap_get_dma_chain_src_pos - Get the source position
1770 * of the ongoing DMA in chain
1771 * @param chain_id
1772 *
1773 * @return - Success : Destination position
1774 * Failure : -EINVAL
1775 */
1776int omap_get_dma_chain_src_pos(int chain_id)
1777{
1778 int lch;
1779 int *channels;
1780
1781 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001782 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001783 printk(KERN_ERR "Invalid chain id\n");
1784 return -EINVAL;
1785 }
1786
1787 /* Check if the chain exists */
1788 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1789 printk(KERN_ERR "Chain doesn't exists\n");
1790 return -EINVAL;
1791 }
1792
1793 channels = dma_linked_lch[chain_id].linked_dmach_q;
1794
1795 /* Get the current channel */
1796 lch = channels[dma_linked_lch[chain_id].q_head];
1797
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001798 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001799}
1800EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001801#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001802
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001803/*----------------------------------------------------------------------------*/
1804
1805#ifdef CONFIG_ARCH_OMAP1
1806
1807static int omap1_dma_handle_ch(int ch)
1808{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001809 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001810
1811 if (enable_1510_mode && ch >= 6) {
1812 csr = dma_chan[ch].saved_csr;
1813 dma_chan[ch].saved_csr = 0;
1814 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001815 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001816 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1817 dma_chan[ch + 6].saved_csr = csr >> 7;
1818 csr &= 0x7f;
1819 }
1820 if ((csr & 0x3f) == 0)
1821 return 0;
1822 if (unlikely(dma_chan[ch].dev_id == -1)) {
1823 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1824 "%d (CSR %04x)\n", ch, csr);
1825 return 0;
1826 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001827 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001828 printk(KERN_WARNING "DMA timeout with device %d\n",
1829 dma_chan[ch].dev_id);
1830 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1831 printk(KERN_WARNING "DMA synchronization event drop occurred "
1832 "with device %d\n", dma_chan[ch].dev_id);
1833 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1834 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1835 if (likely(dma_chan[ch].callback != NULL))
1836 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001837
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001838 return 1;
1839}
1840
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001841static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001842{
1843 int ch = ((int) dev_id) - 1;
1844 int handled = 0;
1845
1846 for (;;) {
1847 int handled_now = 0;
1848
1849 handled_now += omap1_dma_handle_ch(ch);
1850 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1851 handled_now += omap1_dma_handle_ch(ch + 6);
1852 if (!handled_now)
1853 break;
1854 handled += handled_now;
1855 }
1856
1857 return handled ? IRQ_HANDLED : IRQ_NONE;
1858}
1859
1860#else
1861#define omap1_dma_irq_handler NULL
1862#endif
1863
Tony Lindgren140455f2010-02-12 12:26:48 -08001864#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001865
1866static int omap2_dma_handle_ch(int ch)
1867{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001868 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001869
Juha Yrjola31513692006-12-06 17:13:47 -08001870 if (!status) {
1871 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001872 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1873 ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001874 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001875 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001876 }
1877 if (unlikely(dma_chan[ch].dev_id == -1)) {
1878 if (printk_ratelimit())
1879 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1880 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001881 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001882 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001883 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1884 printk(KERN_INFO
1885 "DMA synchronization event drop occurred with device "
1886 "%d\n", dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001887 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001888 printk(KERN_INFO "DMA transaction error with device %d\n",
1889 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001890 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001891 u32 ccr;
1892
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001893 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001894 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001895 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001896 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1897 }
1898 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001899 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1900 printk(KERN_INFO "DMA secure error with device %d\n",
1901 dma_chan[ch].dev_id);
1902 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1903 printk(KERN_INFO "DMA misaligned error with device %d\n",
1904 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001905
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001906 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001907 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001908 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001909 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001910
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001911 /* If the ch is not chained then chain_id will be -1 */
1912 if (dma_chan[ch].chain_id != -1) {
1913 int chain_id = dma_chan[ch].chain_id;
1914 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001915 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001916 dma_chan[dma_chan[ch].next_linked_ch].state =
1917 DMA_CH_STARTED;
1918 if (dma_linked_lch[chain_id].chain_mode ==
1919 OMAP_DMA_DYNAMIC_CHAIN)
1920 disable_lnk(ch);
1921
1922 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1923 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1924
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001925 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001926 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001927 }
1928
Jarkko Nikula538528d2008-02-13 11:47:29 +02001929 if (likely(dma_chan[ch].callback != NULL))
1930 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001931
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001932 return 0;
1933}
1934
1935/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001936static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001937{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001938 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001939 int i;
1940
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001941 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001942 if (val == 0) {
1943 if (printk_ratelimit())
1944 printk(KERN_WARNING "Spurious DMA IRQ\n");
1945 return IRQ_HANDLED;
1946 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001947 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001948 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001949 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001950 if (val & 1)
1951 omap2_dma_handle_ch(i);
1952 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001953 }
1954
1955 return IRQ_HANDLED;
1956}
1957
1958static struct irqaction omap24xx_dma_irq = {
1959 .name = "DMA",
1960 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001961 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001962};
1963
1964#else
1965static struct irqaction omap24xx_dma_irq;
1966#endif
1967
1968/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001969
Tero Kristof2d11852008-08-28 13:13:31 +00001970void omap_dma_global_context_save(void)
1971{
1972 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001973 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001974 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001975 p->dma_read(OCP_SYSCONFIG, 0);
1976 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001977}
1978
1979void omap_dma_global_context_restore(void)
1980{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001981 int ch;
1982
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001983 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1984 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001985 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001986 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001987 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001988
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001989 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001990 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001991
1992 for (ch = 0; ch < dma_chan_count; ch++)
1993 if (dma_chan[ch].dev_id != -1)
1994 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00001995}
1996
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001997static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001998{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001999 int ch, ret = 0;
2000 int dma_irq;
2001 char irq_name[4];
2002 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002003
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002004 p = pdev->dev.platform_data;
2005 if (!p) {
2006 dev_err(&pdev->dev, "%s: System DMA initialized without"
2007 "platform data\n", __func__);
2008 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002009 }
2010
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002011 d = p->dma_attr;
2012 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002013
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002014 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002015 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002016 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002017
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002018 dma_lch_count = d->lch_count;
2019 dma_chan_count = dma_lch_count;
2020 dma_chan = d->chan;
2021 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002022
2023 if (cpu_class_is_omap2()) {
2024 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2025 dma_lch_count, GFP_KERNEL);
2026 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002027 ret = -ENOMEM;
2028 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002029 }
2030 }
2031
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002032 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002033 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002034 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002035 if (cpu_class_is_omap2())
2036 omap2_disable_irq_lch(ch);
2037
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002038 dma_chan[ch].dev_id = -1;
2039 dma_chan[ch].next_lch = -1;
2040
2041 if (ch >= 6 && enable_1510_mode)
2042 continue;
2043
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002044 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002045 /*
2046 * request_irq() doesn't like dev_id (ie. ch) being
2047 * zero, so we have to kludge around this.
2048 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002049 sprintf(&irq_name[0], "%d", ch);
2050 dma_irq = platform_get_irq_byname(pdev, irq_name);
2051
2052 if (dma_irq < 0) {
2053 ret = dma_irq;
2054 goto exit_dma_irq_fail;
2055 }
2056
2057 /* INT_DMA_LCD is handled in lcd_dma.c */
2058 if (dma_irq == INT_DMA_LCD)
2059 continue;
2060
2061 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002062 omap1_dma_irq_handler, 0, "DMA",
2063 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002064 if (ret != 0)
2065 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002066 }
2067 }
2068
Santosh Shilimkar44169072009-05-28 14:16:04 -07002069 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002070 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2071 DMA_DEFAULT_FIFO_DEPTH, 0);
2072
Santosh Shilimkar44169072009-05-28 14:16:04 -07002073 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002074 strcpy(irq_name, "0");
2075 dma_irq = platform_get_irq_byname(pdev, irq_name);
2076 if (dma_irq < 0) {
2077 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2078 goto exit_dma_lch_fail;
2079 }
2080 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2081 if (ret) {
2082 dev_err(&pdev->dev, "set_up failed for IRQ %d"
2083 "for DMA (error %d)\n", dma_irq, ret);
2084 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002085 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002086 }
2087
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002088 /* reserve dma channels 0 and 1 in high security devices */
2089 if (cpu_is_omap34xx() &&
2090 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
2091 printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
2092 "HS ROM code\n");
2093 dma_chan[0].dev_id = 0;
2094 dma_chan[1].dev_id = 1;
2095 }
2096 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002097 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002098
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002099exit_dma_irq_fail:
2100 dev_err(&pdev->dev, "unable to request IRQ %d"
2101 "for DMA (error %d)\n", dma_irq, ret);
2102 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2103 dma_irq = platform_get_irq(pdev, irq_rel);
2104 free_irq(dma_irq, (void *)(irq_rel + 1));
2105 }
2106
2107exit_dma_lch_fail:
2108 kfree(p);
2109 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002110 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002111 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002112}
2113
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002114static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2115{
2116 int dma_irq;
2117
2118 if (cpu_class_is_omap2()) {
2119 char irq_name[4];
2120 strcpy(irq_name, "0");
2121 dma_irq = platform_get_irq_byname(pdev, irq_name);
2122 remove_irq(dma_irq, &omap24xx_dma_irq);
2123 } else {
2124 int irq_rel = 0;
2125 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2126 dma_irq = platform_get_irq(pdev, irq_rel);
2127 free_irq(dma_irq, (void *)(irq_rel + 1));
2128 }
2129 }
2130 kfree(p);
2131 kfree(d);
2132 kfree(dma_chan);
2133 return 0;
2134}
2135
2136static struct platform_driver omap_system_dma_driver = {
2137 .probe = omap_system_dma_probe,
Tony Lindgren3e2e6132012-02-23 14:58:08 -08002138 .remove = __devexit_p(omap_system_dma_remove),
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002139 .driver = {
2140 .name = "omap_dma_system"
2141 },
2142};
2143
2144static int __init omap_system_dma_init(void)
2145{
2146 return platform_driver_register(&omap_system_dma_driver);
2147}
2148arch_initcall(omap_system_dma_init);
2149
2150static void __exit omap_system_dma_exit(void)
2151{
2152 platform_driver_unregister(&omap_system_dma_driver);
2153}
2154
2155MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2156MODULE_LICENSE("GPL");
2157MODULE_ALIAS("platform:" DRIVER_NAME);
2158MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002159
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002160/*
2161 * Reserve the omap SDMA channels using cmdline bootarg
2162 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2163 */
2164static int __init omap_dma_cmdline_reserve_ch(char *str)
2165{
2166 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2167 omap_dma_reserve_channels = 0;
2168 return 1;
2169}
2170
2171__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2172
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002173