blob: 01619c2910e364271ab565f49c8644850b9fc70b [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/dma.c
3 *
Tony Lindgren97b7f712008-07-03 12:24:37 +03004 * Copyright (C) 2003 - 2008 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
7 * Graphics DMA and LCD DMA graphics tranformations
8 * by Imre Deak <imre.deak@nokia.com>
Anand Gadiyarf8151e52007-12-01 12:14:11 -08009 * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000010 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010011 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
12 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070013 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
15 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016 * Support functions for the OMAP internal DMA channels.
17 *
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080018 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
19 * Converted DMA library into DMA platform driver.
20 * - G, Manjunath Kondaiah <manjugk@ti.com>
21 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/sched.h>
31#include <linux/spinlock.h>
32#include <linux/errno.h>
33#include <linux/interrupt.h>
Thomas Gleixner418ca1f02006-07-01 22:32:41 +010034#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030035#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -070037#include <linux/delay.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010038
Tony Lindgren45c3eb72012-11-30 08:41:50 -080039#include <linux/omap-dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010040
Paul Walmsleybc4d8b52012-04-13 06:34:30 -060041/*
42 * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
43 * channels that an instance of the SDMA IP block can support. Used
44 * to size arrays. (The actual maximum on a particular SoC may be less
45 * than this -- for example, OMAP1 SDMA instances only support 17 logical
46 * DMA channels.)
47 */
48#define MAX_LOGICAL_DMA_CH_COUNT 32
49
Anand Gadiyarf8151e52007-12-01 12:14:11 -080050#undef DEBUG
51
52#ifndef CONFIG_ARCH_OMAP1
53enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
54 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
55};
56
57enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000058#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059
Tony Lindgren97b7f712008-07-03 12:24:37 +030060#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020061#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062
Tony Lindgren97b7f712008-07-03 12:24:37 +030063#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010064
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080065static struct omap_system_dma_plat_info *p;
66static struct omap_dma_dev_attr *d;
67
Tony Lindgren97b7f712008-07-03 12:24:37 +030068static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080069static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070
Tero Kristof2d11852008-08-28 13:13:31 +000071static struct omap_dma_global_context_registers {
72 u32 dma_irqenable_l0;
73 u32 dma_ocp_sysconfig;
74 u32 dma_gcr;
75} omap_dma_global_context;
76
Anand Gadiyarf8151e52007-12-01 12:14:11 -080077struct dma_link_info {
78 int *linked_dmach_q;
79 int no_of_lchs_linked;
80
81 int q_count;
82 int q_tail;
83 int q_head;
84
85 int chain_state;
86 int chain_mode;
87
88};
89
Tony Lindgren4d963722008-07-03 12:24:31 +030090static struct dma_link_info *dma_linked_lch;
91
92#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080093
94/* Chain handling macros */
95#define OMAP_DMA_CHAIN_QINIT(chain_id) \
96 do { \
97 dma_linked_lch[chain_id].q_head = \
98 dma_linked_lch[chain_id].q_tail = \
99 dma_linked_lch[chain_id].q_count = 0; \
100 } while (0)
101#define OMAP_DMA_CHAIN_QFULL(chain_id) \
102 (dma_linked_lch[chain_id].no_of_lchs_linked == \
103 dma_linked_lch[chain_id].q_count)
104#define OMAP_DMA_CHAIN_QLAST(chain_id) \
105 do { \
106 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
107 dma_linked_lch[chain_id].q_count) \
108 } while (0)
109#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
110 (0 == dma_linked_lch[chain_id].q_count)
111#define __OMAP_DMA_CHAIN_INCQ(end) \
112 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
113#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
114 do { \
115 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
116 dma_linked_lch[chain_id].q_count--; \
117 } while (0)
118
119#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
120 do { \
121 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
122 dma_linked_lch[chain_id].q_count++; \
123 } while (0)
124#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300125
126static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700128static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100129
130static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300131static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800133static inline void disable_lnk(int lch);
134static void omap_disable_channel_irq(int lch);
135static inline void omap_enable_channel_irq(int lch);
136
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000137#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800138 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000139
140#ifdef CONFIG_ARCH_OMAP15XX
141/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800142static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000143{
144 return enable_1510_mode;
145}
146#else
147#define omap_dma_in_1510_mode() 0
148#endif
149
150#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100151static inline int get_gdma_dev(int req)
152{
153 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
154 int shift = ((req - 1) % 5) * 6;
155
156 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
157}
158
159static inline void set_gdma_dev(int req, int dev)
160{
161 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
162 int shift = ((req - 1) % 5) * 6;
163 u32 l;
164
165 l = omap_readl(reg);
166 l &= ~(0x3f << shift);
167 l |= (dev - 1) << shift;
168 omap_writel(l, reg);
169}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000170#else
171#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800172#define omap_readl(reg) 0
173#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000174#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100175
Tony Lindgren54b693d2012-10-02 13:39:28 -0700176#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300177void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100178{
179 unsigned long reg;
180 u32 l;
181
Tony Lindgren82809602012-10-30 11:03:22 -0700182 if (dma_omap1()) {
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300183 switch (dst_port) {
184 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
185 reg = OMAP_TC_OCPT1_PRIOR;
186 break;
187 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
188 reg = OMAP_TC_OCPT2_PRIOR;
189 break;
190 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
191 reg = OMAP_TC_EMIFF_PRIOR;
192 break;
193 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
194 reg = OMAP_TC_EMIFS_PRIOR;
195 break;
196 default:
197 BUG();
198 return;
199 }
200 l = omap_readl(reg);
201 l &= ~(0xf << 8);
202 l |= (priority & 0xf) << 8;
203 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100204 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100205}
Tony Lindgren54b693d2012-10-02 13:39:28 -0700206#endif
207
208#ifdef CONFIG_ARCH_OMAP2PLUS
209void omap_set_dma_priority(int lch, int dst_port, int priority)
210{
211 u32 ccr;
212
213 ccr = p->dma_read(CCR, lch);
214 if (priority)
215 ccr |= (1 << 6);
216 else
217 ccr &= ~(1 << 6);
218 p->dma_write(ccr, CCR, lch);
219}
220#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300221EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222
223void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000224 int frame_count, int sync_mode,
225 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100226{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300227 u32 l;
228
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800229 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300230 l &= ~0x03;
231 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800232 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100233
Tony Lindgren82809602012-10-30 11:03:22 -0700234 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300235 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100236
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800237 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300238 ccr &= ~(1 << 5);
239 if (sync_mode == OMAP_DMA_SYNC_FRAME)
240 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800241 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300242
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800243 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300244 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000245 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300246 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800247 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000248 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249
Tony Lindgren82809602012-10-30 11:03:22 -0700250 if (dma_omap2plus() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300251 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100252
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800253 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100254
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200255 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300256 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200257 val |= (dma_trigger & ~0x1f) << 14;
258 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000259
260 if (sync_mode & OMAP_DMA_SYNC_FRAME)
261 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700262 else
263 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000264
265 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
266 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700267 else
268 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000269
Samu Onkalo72a11792010-08-02 14:21:40 +0300270 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000271 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300272 val |= (1 << 23); /* Prefetch */
273 } else if (src_or_dst_synch) {
274 val |= 1 << 24; /* source synch */
275 } else {
276 val &= ~(1 << 24); /* dest synch */
277 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800278 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000279 }
280
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800281 p->dma_write(elem_count, CEN, lch);
282 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100283}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300284EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000285
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
287{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288 BUG_ON(omap_dma_in_1510_mode());
289
Tony Lindgren82809602012-10-30 11:03:22 -0700290 if (dma_omap1()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700291 u16 w;
292
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800293 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700294 w &= ~0x03;
295
296 switch (mode) {
297 case OMAP_DMA_CONSTANT_FILL:
298 w |= 0x01;
299 break;
300 case OMAP_DMA_TRANSPARENT_COPY:
301 w |= 0x02;
302 break;
303 case OMAP_DMA_COLOR_DIS:
304 break;
305 default:
306 BUG();
307 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800308 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700309
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800310 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700311 w &= ~0x0f;
312 /* Default is channel type 2D */
313 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800314 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700315 w |= 1; /* Channel type G */
316 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800317 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700318 }
319
Tony Lindgren82809602012-10-30 11:03:22 -0700320 if (dma_omap2plus()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700321 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000322
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800323 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700324 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300325
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700326 switch (mode) {
327 case OMAP_DMA_CONSTANT_FILL:
328 val |= 1 << 16;
329 break;
330 case OMAP_DMA_TRANSPARENT_COPY:
331 val |= 1 << 17;
332 break;
333 case OMAP_DMA_COLOR_DIS:
334 break;
335 default:
336 BUG();
337 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800338 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700340 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800341 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300344EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300346void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
347{
Tony Lindgren82809602012-10-30 11:03:22 -0700348 if (dma_omap2plus()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300349 u32 csdp;
350
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800351 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300352 csdp &= ~(0x3 << 16);
353 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800354 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300355 }
356}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300357EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300358
Tony Lindgren0499bde2008-07-03 12:24:36 +0300359void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
360{
Tony Lindgren82809602012-10-30 11:03:22 -0700361 if (dma_omap1() && !dma_omap15xx()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300362 u32 l;
363
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800364 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300365 l &= ~0x7;
366 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800367 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300368 }
369}
370EXPORT_SYMBOL(omap_set_dma_channel_mode);
371
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000372/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100373void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000374 unsigned long src_start,
375 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300377 u32 l;
378
Tony Lindgren82809602012-10-30 11:03:22 -0700379 if (dma_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300380 u16 w;
381
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800382 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300383 w &= ~(0x1f << 2);
384 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800385 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300386 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300387
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800388 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300389 l &= ~(0x03 << 12);
390 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800391 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300392
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800393 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100394
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800395 p->dma_write(src_ei, CSEI, lch);
396 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300397}
398EXPORT_SYMBOL(omap_set_dma_src_params);
399
400void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000401{
402 omap_set_dma_transfer_params(lch, params->data_type,
403 params->elem_count, params->frame_count,
404 params->sync_mode, params->trigger,
405 params->src_or_dst_synch);
406 omap_set_dma_src_params(lch, params->src_port,
407 params->src_amode, params->src_start,
408 params->src_ei, params->src_fi);
409
410 omap_set_dma_dest_params(lch, params->dst_port,
411 params->dst_amode, params->dst_start,
412 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800413 if (params->read_prio || params->write_prio)
414 omap_dma_set_prio_lch(lch, params->read_prio,
415 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300417EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418
419void omap_set_dma_src_index(int lch, int eidx, int fidx)
420{
Tony Lindgren82809602012-10-30 11:03:22 -0700421 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000422 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300423
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800424 p->dma_write(eidx, CSEI, lch);
425 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300427EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428
429void omap_set_dma_src_data_pack(int lch, int enable)
430{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300431 u32 l;
432
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800433 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300434 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000435 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300436 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800437 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300439EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100440
441void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
442{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700443 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300444 u32 l;
445
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800446 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300447 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100448
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100449 switch (burst_mode) {
450 case OMAP_DMA_DATA_BURST_DIS:
451 break;
452 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700453 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700454 burst = 0x1;
455 else
456 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457 break;
458 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700459 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700460 burst = 0x2;
461 break;
462 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700463 /*
464 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 * w |= (0x03 << 7);
466 * fall through
467 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700468 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700469 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700470 burst = 0x3;
471 break;
472 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700473 /*
474 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700475 * fall through
476 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 default:
478 BUG();
479 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300480
481 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800482 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300484EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100485
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000486/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000488 unsigned long dest_start,
489 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300491 u32 l;
492
Tony Lindgren82809602012-10-30 11:03:22 -0700493 if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800494 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300495 l &= ~(0x1f << 9);
496 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800497 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000498 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800500 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300501 l &= ~(0x03 << 14);
502 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800503 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800505 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100506
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800507 p->dma_write(dst_ei, CDEI, lch);
508 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300510EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511
512void omap_set_dma_dest_index(int lch, int eidx, int fidx)
513{
Tony Lindgren82809602012-10-30 11:03:22 -0700514 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000515 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300516
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800517 p->dma_write(eidx, CDEI, lch);
518 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300520EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100521
522void omap_set_dma_dest_data_pack(int lch, int enable)
523{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300524 u32 l;
525
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800526 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300527 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000528 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300529 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800530 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300532EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100533
534void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
535{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700536 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300537 u32 l;
538
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800539 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300540 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100541
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542 switch (burst_mode) {
543 case OMAP_DMA_DATA_BURST_DIS:
544 break;
545 case OMAP_DMA_DATA_BURST_4:
Tony Lindgren82809602012-10-30 11:03:22 -0700546 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700547 burst = 0x1;
548 else
549 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550 break;
551 case OMAP_DMA_DATA_BURST_8:
Tony Lindgren82809602012-10-30 11:03:22 -0700552 if (dma_omap2plus())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700553 burst = 0x2;
554 else
555 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700557 case OMAP_DMA_DATA_BURST_16:
Tony Lindgren82809602012-10-30 11:03:22 -0700558 if (dma_omap2plus()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700559 burst = 0x3;
560 break;
561 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700562 /*
563 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700564 * fall through
565 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566 default:
567 printk(KERN_ERR "Invalid DMA burst mode\n");
568 BUG();
569 return;
570 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300571 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800572 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300574EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000576static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577{
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700578 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700579 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700580 p->dma_read(CSR, lch);
581 else
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
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700588static inline void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589{
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700590 /* disable channel interrupts */
591 p->dma_write(0, CICR, lch);
592 /* Clear CSR */
Tony Lindgren82809602012-10-30 11:03:22 -0700593 if (dma_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700594 p->dma_read(CSR, lch);
595 else
596 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597}
598
599void omap_enable_dma_irq(int lch, u16 bits)
600{
601 dma_chan[lch].enabled_irqs |= bits;
602}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300603EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604
605void omap_disable_dma_irq(int lch, u16 bits)
606{
607 dma_chan[lch].enabled_irqs &= ~bits;
608}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300609EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000611static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100612{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300613 u32 l;
614
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800615 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300616
Tony Lindgren82809602012-10-30 11:03:22 -0700617 if (dma_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300618 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000620 /* Set the ENABLE_LNK bits */
621 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300622 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800623
624#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren82809602012-10-30 11:03:22 -0700625 if (dma_omap2plus())
Tony Lindgren97b7f712008-07-03 12:24:37 +0300626 if (dma_chan[lch].next_linked_ch != -1)
627 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800628#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300629
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800630 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631}
632
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000633static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100634{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300635 u32 l;
636
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800637 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300638
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000639 /* Disable interrupts */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700640 omap_disable_channel_irq(lch);
641
Tony Lindgren82809602012-10-30 11:03:22 -0700642 if (dma_omap1()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000643 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300644 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645 }
646
Tony Lindgren82809602012-10-30 11:03:22 -0700647 if (dma_omap2plus()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000648 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300649 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000650 }
651
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800652 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000653 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
654}
655
656static inline void omap2_enable_irq_lch(int lch)
657{
658 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800659 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000660
Tony Lindgren82809602012-10-30 11:03:22 -0700661 if (dma_omap1())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000662 return;
663
Tao Huee907322009-11-10 18:55:17 -0800664 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700665 /* clear IRQ STATUS */
666 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
667 /* Enable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800668 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000669 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800670 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800671 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672}
673
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700674static inline void omap2_disable_irq_lch(int lch)
675{
676 u32 val;
677 unsigned long flags;
678
Tony Lindgren82809602012-10-30 11:03:22 -0700679 if (dma_omap1())
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700680 return;
681
682 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700683 /* Disable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800684 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700685 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800686 p->dma_write(val, IRQENABLE_L0, lch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700687 /* clear IRQ STATUS */
688 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700689 spin_unlock_irqrestore(&dma_chan_lock, flags);
690}
691
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100692int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300693 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694 void *data, int *dma_ch_out)
695{
696 int ch, free_ch = -1;
697 unsigned long flags;
698 struct omap_dma_lch *chan;
699
700 spin_lock_irqsave(&dma_chan_lock, flags);
701 for (ch = 0; ch < dma_chan_count; ch++) {
702 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
703 free_ch = ch;
R Sricharan03a6d4a2013-06-13 19:47:09 +0530704 /* Exit after first free channel found */
705 break;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706 }
707 }
708 if (free_ch == -1) {
709 spin_unlock_irqrestore(&dma_chan_lock, flags);
710 return -EBUSY;
711 }
712 chan = dma_chan + free_ch;
713 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000714
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800715 if (p->clear_lch_regs)
716 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000717
Tony Lindgren82809602012-10-30 11:03:22 -0700718 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000719 omap_clear_dma(free_ch);
720
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100721 spin_unlock_irqrestore(&dma_chan_lock, flags);
722
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 chan->dev_name = dev_name;
724 chan->callback = callback;
725 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800726 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300727
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800728#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren82809602012-10-30 11:03:22 -0700729 if (dma_omap2plus()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300730 chan->chain_id = -1;
731 chan->next_linked_ch = -1;
732 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800733#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300734
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700735 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000736
Tony Lindgren82809602012-10-30 11:03:22 -0700737 if (dma_omap1())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700738 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Tony Lindgren82809602012-10-30 11:03:22 -0700739 else if (dma_omap2plus())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700740 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
741 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
Tony Lindgren82809602012-10-30 11:03:22 -0700743 if (dma_omap16xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100744 /* If the sync device is set, configure it dynamically. */
745 if (dev_id != 0) {
746 set_gdma_dev(free_ch + 1, dev_id);
747 dev_id = free_ch + 1;
748 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300749 /*
750 * Disable the 1510 compatibility mode and set the sync device
751 * id.
752 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800753 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Tony Lindgren82809602012-10-30 11:03:22 -0700754 } else if (dma_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800755 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000757
Tony Lindgren82809602012-10-30 11:03:22 -0700758 if (dma_omap2plus()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000759 omap_enable_channel_irq(free_ch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700760 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000761 }
762
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763 *dma_ch_out = free_ch;
764
765 return 0;
766}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300767EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000769void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100770{
771 unsigned long flags;
772
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000773 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300774 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000775 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100776 return;
777 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300778
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700779 /* Disable interrupt for logical channel */
Tony Lindgren82809602012-10-30 11:03:22 -0700780 if (dma_omap2plus())
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700781 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000782
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700783 /* Disable all DMA interrupts for the channel. */
784 omap_disable_channel_irq(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000785
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700786 /* Make sure the DMA transfer is stopped. */
787 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000788
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700789 /* Clear registers */
Tony Lindgren82809602012-10-30 11:03:22 -0700790 if (dma_omap2plus())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000791 omap_clear_dma(lch);
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
Tony Lindgren82809602012-10-30 11:03:22 -0700816 if (dma_omap1()) {
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));
Tony Lindgren82809602012-10-30 11:03:22 -0700855 if (d->dev_caps & IS_RW_PRIORITY)
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 */
Tony Lindgren82809602012-10-30 11:03:22 -0700888 if (dma_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
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000897 /* Set the link register of the first channel */
898 enable_lnk(lch);
899
900 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
R Sricharanf0a3ff22013-06-13 19:47:10 +0530901 dma_chan_link_map[lch] = 1;
902
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000903 cur_lch = dma_chan[lch].next_lch;
904 do {
905 next_lch = dma_chan[cur_lch].next_lch;
906
907 /* The loop case: we've been here already */
908 if (dma_chan_link_map[cur_lch])
909 break;
910 /* Mark the current channel */
911 dma_chan_link_map[cur_lch] = 1;
912
913 enable_lnk(cur_lch);
914 omap_enable_channel_irq(cur_lch);
915
916 cur_lch = next_lch;
917 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800918 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800919 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000920
921 omap_enable_channel_irq(lch);
922
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800923 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300924
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800925 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
926 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300927 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800928
Russell King35453582012-04-14 18:57:10 +0100929 /*
930 * As dma_write() uses IO accessors which are weakly ordered, there
931 * is no guarantee that data in coherent DMA memory will be visible
932 * to the DMA device. Add a memory barrier here to ensure that any
933 * such data is visible prior to enabling DMA.
934 */
935 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800936 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000937
938 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
939}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300940EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000941
942void omap_stop_dma(int lch)
943{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300944 u32 l;
945
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700946 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700947 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700948
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800949 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800950 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
951 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700952 int i = 0;
953 u32 sys_cf;
954
955 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800956 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700957 sys_cf = l;
958 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
959 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800960 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700961
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800962 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700963 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800964 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700965
966 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800967 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700968 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
969 OMAP_DMA_CCR_WR_ACTIVE))) {
970 udelay(5);
971 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800972 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700973 }
974 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600975 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700976 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800977 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700978 } else {
979 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800980 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700981 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700982
Russell King35453582012-04-14 18:57:10 +0100983 /*
984 * Ensure that data transferred by DMA is visible to any access
985 * after DMA has been disabled. This is important for coherent
986 * DMA regions.
987 */
988 mb();
989
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000990 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
991 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600992 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000993
994 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
995 do {
996 /* The loop case: we've been here already */
997 if (dma_chan_link_map[cur_lch])
998 break;
999 /* Mark the current channel */
1000 dma_chan_link_map[cur_lch] = 1;
1001
1002 disable_lnk(cur_lch);
1003
1004 next_lch = dma_chan[cur_lch].next_lch;
1005 cur_lch = next_lch;
1006 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001007 }
1008
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001009 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1010}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001011EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001012
1013/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001014 * Allows changing the DMA callback function or data. This may be needed if
1015 * the driver shares a single DMA channel for multiple dma triggers.
1016 */
1017int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001018 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001019 void *data)
1020{
1021 unsigned long flags;
1022
1023 if (lch < 0)
1024 return -ENODEV;
1025
1026 spin_lock_irqsave(&dma_chan_lock, flags);
1027 if (dma_chan[lch].dev_id == -1) {
1028 printk(KERN_ERR "DMA callback for not set for free channel\n");
1029 spin_unlock_irqrestore(&dma_chan_lock, flags);
1030 return -EINVAL;
1031 }
1032 dma_chan[lch].callback = callback;
1033 dma_chan[lch].data = data;
1034 spin_unlock_irqrestore(&dma_chan_lock, flags);
1035
1036 return 0;
1037}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001038EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001039
1040/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001041 * Returns current physical source address for the given DMA channel.
1042 * If the channel is running the caller must disable interrupts prior calling
1043 * this function and process the returned value before re-enabling interrupt to
1044 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001045 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001046 * in incorrect return value.
1047 */
1048dma_addr_t omap_get_dma_src_pos(int lch)
1049{
Tony Lindgren0695de32007-05-07 18:24:14 -07001050 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001051
Tony Lindgren82809602012-10-30 11:03:22 -07001052 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001053 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001054 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001055 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001056
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001057 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001058 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001059
Tony Lindgren82809602012-10-30 11:03:22 -07001060 if (!dma_omap15xx()) {
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001061 /*
1062 * CDAC == 0 indicates that the DMA transfer on the channel has
1063 * not been started (no data has been transferred so far).
1064 * Return the programmed source start address in this case.
1065 */
1066 if (likely(p->dma_read(CDAC, lch)))
1067 offset = p->dma_read(CSAC, lch);
1068 else
1069 offset = p->dma_read(CSSA, lch);
1070 }
1071
Tony Lindgren82809602012-10-30 11:03:22 -07001072 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001073 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001074
1075 return offset;
1076}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001077EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001078
1079/*
1080 * Returns current physical destination address for the given DMA channel.
1081 * If the channel is running the caller must disable interrupts prior calling
1082 * this function and process the returned value before re-enabling interrupt to
1083 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001084 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001085 * in incorrect return value.
1086 */
1087dma_addr_t omap_get_dma_dst_pos(int lch)
1088{
Tony Lindgren0695de32007-05-07 18:24:14 -07001089 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001090
Tony Lindgren82809602012-10-30 11:03:22 -07001091 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001092 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001093 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001094 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001095
Tony Lindgren0499bde2008-07-03 12:24:36 +03001096 /*
1097 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1098 * read before the DMA controller finished disabling the channel.
1099 */
Tony Lindgren82809602012-10-30 11:03:22 -07001100 if (!dma_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001101 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001102 /*
1103 * CDAC == 0 indicates that the DMA transfer on the channel has
1104 * not been started (no data has been transferred so far).
1105 * Return the programmed destination start address in this case.
1106 */
1107 if (unlikely(!offset))
1108 offset = p->dma_read(CDSA, lch);
1109 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001110
Tony Lindgren82809602012-10-30 11:03:22 -07001111 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001112 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001113
1114 return offset;
1115}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001116EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001117
Tony Lindgren0499bde2008-07-03 12:24:36 +03001118int omap_get_dma_active_status(int lch)
1119{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001120 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001121}
1122EXPORT_SYMBOL(omap_get_dma_active_status);
1123
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001124int omap_dma_running(void)
1125{
1126 int lch;
1127
Tony Lindgren82809602012-10-30 11:03:22 -07001128 if (dma_omap1())
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001129 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001130 return 1;
1131
1132 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001133 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001134 return 1;
1135
1136 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137}
1138
1139/*
1140 * lch_queue DMA will start right after lch_head one is finished.
1141 * For this DMA link to start, you still need to start (see omap_start_dma)
1142 * the first one. That will fire up the entire queue.
1143 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001144void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145{
1146 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001147 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001148 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001149 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001150 return;
1151 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1153 BUG();
1154 return;
1155 }
1156
1157 if ((dma_chan[lch_head].dev_id == -1) ||
1158 (dma_chan[lch_queue].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001159 pr_err("omap_dma: trying to link non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160 dump_stack();
1161 }
1162
1163 dma_chan[lch_head].next_lch = lch_queue;
1164}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001165EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166
1167/*
1168 * Once the DMA queue is stopped, we can destroy it.
1169 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001170void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001171{
1172 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001173 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001174 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001175 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001176 return;
1177 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1179 BUG();
1180 return;
1181 }
1182
1183 if (dma_chan[lch_head].next_lch != lch_queue ||
1184 dma_chan[lch_head].next_lch == -1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001185 pr_err("omap_dma: trying to unlink non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001186 dump_stack();
1187 }
1188
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001189 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001190 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001191 pr_err("omap_dma: You need to stop the DMA channels before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001192 dump_stack();
1193 }
1194
1195 dma_chan[lch_head].next_lch = -1;
1196}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001197EXPORT_SYMBOL(omap_dma_unlink_lch);
1198
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001199#ifndef CONFIG_ARCH_OMAP1
1200/* Create chain of DMA channesls */
1201static void create_dma_lch_chain(int lch_head, int lch_queue)
1202{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001203 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001204
1205 /* Check if this is the first link in chain */
1206 if (dma_chan[lch_head].next_linked_ch == -1) {
1207 dma_chan[lch_head].next_linked_ch = lch_queue;
1208 dma_chan[lch_head].prev_linked_ch = lch_queue;
1209 dma_chan[lch_queue].next_linked_ch = lch_head;
1210 dma_chan[lch_queue].prev_linked_ch = lch_head;
1211 }
1212
1213 /* a link exists, link the new channel in circular chain */
1214 else {
1215 dma_chan[lch_queue].next_linked_ch =
1216 dma_chan[lch_head].next_linked_ch;
1217 dma_chan[lch_queue].prev_linked_ch = lch_head;
1218 dma_chan[lch_head].next_linked_ch = lch_queue;
1219 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1220 lch_queue;
1221 }
1222
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001223 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001224 l &= ~(0x1f);
1225 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001226 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001227
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001228 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001229 l &= ~(0x1f);
1230 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001231 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001232}
1233
1234/**
1235 * @brief omap_request_dma_chain : Request a chain of DMA channels
1236 *
1237 * @param dev_id - Device id using the dma channel
1238 * @param dev_name - Device name
1239 * @param callback - Call back function
1240 * @chain_id -
1241 * @no_of_chans - Number of channels requested
1242 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1243 * OMAP_DMA_DYNAMIC_CHAIN
1244 * @params - Channel parameters
1245 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001246 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001247 * Failure: -EINVAL/-ENOMEM
1248 */
1249int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001250 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001251 void *data),
1252 int *chain_id, int no_of_chans, int chain_mode,
1253 struct omap_dma_channel_params params)
1254{
1255 int *channels;
1256 int i, err;
1257
1258 /* Is the chain mode valid ? */
1259 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1260 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1261 printk(KERN_ERR "Invalid chain mode requested\n");
1262 return -EINVAL;
1263 }
1264
1265 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001266 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001267 printk(KERN_ERR "Invalid Number of channels requested\n");
1268 return -EINVAL;
1269 }
1270
manjugk manjugkea221a62010-05-14 12:05:25 -07001271 /*
1272 * Allocate a queue to maintain the status of the channels
1273 * in the chain
1274 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001275 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1276 if (channels == NULL) {
1277 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1278 return -ENOMEM;
1279 }
1280
1281 /* request and reserve DMA channels for the chain */
1282 for (i = 0; i < no_of_chans; i++) {
1283 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001284 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001285 if (err < 0) {
1286 int j;
1287 for (j = 0; j < i; j++)
1288 omap_free_dma(channels[j]);
1289 kfree(channels);
1290 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1291 return err;
1292 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001293 dma_chan[channels[i]].prev_linked_ch = -1;
1294 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1295
1296 /*
1297 * Allowing client drivers to set common parameters now,
1298 * so that later only relevant (src_start, dest_start
1299 * and element count) can be set
1300 */
1301 omap_set_dma_params(channels[i], &params);
1302 }
1303
1304 *chain_id = channels[0];
1305 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1306 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1307 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1308 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1309
1310 for (i = 0; i < no_of_chans; i++)
1311 dma_chan[channels[i]].chain_id = *chain_id;
1312
1313 /* Reset the Queue pointers */
1314 OMAP_DMA_CHAIN_QINIT(*chain_id);
1315
1316 /* Set up the chain */
1317 if (no_of_chans == 1)
1318 create_dma_lch_chain(channels[0], channels[0]);
1319 else {
1320 for (i = 0; i < (no_of_chans - 1); i++)
1321 create_dma_lch_chain(channels[i], channels[i + 1]);
1322 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001323
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001324 return 0;
1325}
1326EXPORT_SYMBOL(omap_request_dma_chain);
1327
1328/**
1329 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1330 * params after setting it. Dont do this while dma is running!!
1331 *
1332 * @param chain_id - Chained logical channel id.
1333 * @param params
1334 *
1335 * @return - Success : 0
1336 * Failure : -EINVAL
1337 */
1338int omap_modify_dma_chain_params(int chain_id,
1339 struct omap_dma_channel_params params)
1340{
1341 int *channels;
1342 u32 i;
1343
1344 /* Check for input params */
1345 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001346 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001347 printk(KERN_ERR "Invalid chain id\n");
1348 return -EINVAL;
1349 }
1350
1351 /* Check if the chain exists */
1352 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1353 printk(KERN_ERR "Chain doesn't exists\n");
1354 return -EINVAL;
1355 }
1356 channels = dma_linked_lch[chain_id].linked_dmach_q;
1357
1358 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1359 /*
1360 * Allowing client drivers to set common parameters now,
1361 * so that later only relevant (src_start, dest_start
1362 * and element count) can be set
1363 */
1364 omap_set_dma_params(channels[i], &params);
1365 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001366
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001367 return 0;
1368}
1369EXPORT_SYMBOL(omap_modify_dma_chain_params);
1370
1371/**
1372 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1373 *
1374 * @param chain_id
1375 *
1376 * @return - Success : 0
1377 * Failure : -EINVAL
1378 */
1379int omap_free_dma_chain(int chain_id)
1380{
1381 int *channels;
1382 u32 i;
1383
1384 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001385 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001386 printk(KERN_ERR "Invalid chain id\n");
1387 return -EINVAL;
1388 }
1389
1390 /* Check if the chain exists */
1391 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1392 printk(KERN_ERR "Chain doesn't exists\n");
1393 return -EINVAL;
1394 }
1395
1396 channels = dma_linked_lch[chain_id].linked_dmach_q;
1397 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1398 dma_chan[channels[i]].next_linked_ch = -1;
1399 dma_chan[channels[i]].prev_linked_ch = -1;
1400 dma_chan[channels[i]].chain_id = -1;
1401 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1402 omap_free_dma(channels[i]);
1403 }
1404
1405 kfree(channels);
1406
1407 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1408 dma_linked_lch[chain_id].chain_mode = -1;
1409 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001410
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001411 return (0);
1412}
1413EXPORT_SYMBOL(omap_free_dma_chain);
1414
1415/**
1416 * @brief omap_dma_chain_status - Check if the chain is in
1417 * active / inactive state.
1418 * @param chain_id
1419 *
1420 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1421 * Failure : -EINVAL
1422 */
1423int omap_dma_chain_status(int chain_id)
1424{
1425 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001426 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001427 printk(KERN_ERR "Invalid chain id\n");
1428 return -EINVAL;
1429 }
1430
1431 /* Check if the chain exists */
1432 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1433 printk(KERN_ERR "Chain doesn't exists\n");
1434 return -EINVAL;
1435 }
1436 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1437 dma_linked_lch[chain_id].q_count);
1438
1439 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1440 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001441
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001442 return OMAP_DMA_CHAIN_ACTIVE;
1443}
1444EXPORT_SYMBOL(omap_dma_chain_status);
1445
1446/**
1447 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1448 * set the params and start the transfer.
1449 *
1450 * @param chain_id
1451 * @param src_start - buffer start address
1452 * @param dest_start - Dest address
1453 * @param elem_count
1454 * @param frame_count
1455 * @param callbk_data - channel callback parameter data.
1456 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301457 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001458 * Failure: -EINVAL/-EBUSY
1459 */
1460int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1461 int elem_count, int frame_count, void *callbk_data)
1462{
1463 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001464 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001465 int start_dma = 0;
1466
Tony Lindgren97b7f712008-07-03 12:24:37 +03001467 /*
1468 * if buffer size is less than 1 then there is
1469 * no use of starting the chain
1470 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001471 if (elem_count < 1) {
1472 printk(KERN_ERR "Invalid buffer size\n");
1473 return -EINVAL;
1474 }
1475
1476 /* Check for input params */
1477 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001478 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001479 printk(KERN_ERR "Invalid chain id\n");
1480 return -EINVAL;
1481 }
1482
1483 /* Check if the chain exists */
1484 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1485 printk(KERN_ERR "Chain doesn't exist\n");
1486 return -EINVAL;
1487 }
1488
1489 /* Check if all the channels in chain are in use */
1490 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1491 return -EBUSY;
1492
1493 /* Frame count may be negative in case of indexed transfers */
1494 channels = dma_linked_lch[chain_id].linked_dmach_q;
1495
1496 /* Get a free channel */
1497 lch = channels[dma_linked_lch[chain_id].q_tail];
1498
1499 /* Store the callback data */
1500 dma_chan[lch].data = callbk_data;
1501
1502 /* Increment the q_tail */
1503 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1504
1505 /* Set the params to the free channel */
1506 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001507 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001508 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001509 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001510
1511 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001512 p->dma_write(elem_count, CEN, lch);
1513 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001514
Tony Lindgren97b7f712008-07-03 12:24:37 +03001515 /*
1516 * If the chain is dynamically linked,
1517 * then we may have to start the chain if its not active
1518 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001519 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1520
Tony Lindgren97b7f712008-07-03 12:24:37 +03001521 /*
1522 * In Dynamic chain, if the chain is not started,
1523 * queue the channel
1524 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001525 if (dma_linked_lch[chain_id].chain_state ==
1526 DMA_CHAIN_NOTSTARTED) {
1527 /* Enable the link in previous channel */
1528 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1529 DMA_CH_QUEUED)
1530 enable_lnk(dma_chan[lch].prev_linked_ch);
1531 dma_chan[lch].state = DMA_CH_QUEUED;
1532 }
1533
Tony Lindgren97b7f712008-07-03 12:24:37 +03001534 /*
1535 * Chain is already started, make sure its active,
1536 * if not then start the chain
1537 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001538 else {
1539 start_dma = 1;
1540
1541 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1542 DMA_CH_STARTED) {
1543 enable_lnk(dma_chan[lch].prev_linked_ch);
1544 dma_chan[lch].state = DMA_CH_QUEUED;
1545 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001546 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001547 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001548 disable_lnk(dma_chan[lch].
1549 prev_linked_ch);
1550 pr_debug("\n prev ch is stopped\n");
1551 start_dma = 1;
1552 }
1553 }
1554
1555 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1556 == DMA_CH_QUEUED) {
1557 enable_lnk(dma_chan[lch].prev_linked_ch);
1558 dma_chan[lch].state = DMA_CH_QUEUED;
1559 start_dma = 0;
1560 }
1561 omap_enable_channel_irq(lch);
1562
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001563 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001564
Tony Lindgren0499bde2008-07-03 12:24:36 +03001565 if ((0 == (l & (1 << 24))))
1566 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001567 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001568 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001569 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001570 if (0 == (l & (1 << 7))) {
1571 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001572 dma_chan[lch].state = DMA_CH_STARTED;
1573 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001574 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001575 } else
1576 start_dma = 0;
1577 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001578 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001579 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001580 }
1581 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1582 }
1583 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001584
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301585 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001586}
1587EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1588
1589/**
1590 * @brief omap_start_dma_chain_transfers - Start the chain
1591 *
1592 * @param chain_id
1593 *
1594 * @return - Success : 0
1595 * Failure : -EINVAL/-EBUSY
1596 */
1597int omap_start_dma_chain_transfers(int chain_id)
1598{
1599 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001600 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001601
Tony Lindgren4d963722008-07-03 12:24:31 +03001602 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001603 printk(KERN_ERR "Invalid chain id\n");
1604 return -EINVAL;
1605 }
1606
1607 channels = dma_linked_lch[chain_id].linked_dmach_q;
1608
1609 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1610 printk(KERN_ERR "Chain is already started\n");
1611 return -EBUSY;
1612 }
1613
1614 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1615 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1616 i++) {
1617 enable_lnk(channels[i]);
1618 omap_enable_channel_irq(channels[i]);
1619 }
1620 } else {
1621 omap_enable_channel_irq(channels[0]);
1622 }
1623
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001624 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001625 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001626 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1627 dma_chan[channels[0]].state = DMA_CH_STARTED;
1628
Tony Lindgren0499bde2008-07-03 12:24:36 +03001629 if ((0 == (l & (1 << 24))))
1630 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001631 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001632 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001633 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001634
1635 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001636
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001637 return 0;
1638}
1639EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1640
1641/**
1642 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1643 *
1644 * @param chain_id
1645 *
1646 * @return - Success : 0
1647 * Failure : EINVAL
1648 */
1649int omap_stop_dma_chain_transfers(int chain_id)
1650{
1651 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001652 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001653 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001654
1655 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001656 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001657 printk(KERN_ERR "Invalid chain id\n");
1658 return -EINVAL;
1659 }
1660
1661 /* Check if the chain exists */
1662 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1663 printk(KERN_ERR "Chain doesn't exists\n");
1664 return -EINVAL;
1665 }
1666 channels = dma_linked_lch[chain_id].linked_dmach_q;
1667
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001668 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001669 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001670 l = sys_cf;
1671 /* Middle mode reg set no Standby */
1672 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001673 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001674 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001675
1676 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1677
1678 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001679 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001680 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001681 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001682
1683 /* Disable the link in all the channels */
1684 disable_lnk(channels[i]);
1685 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1686
1687 }
1688 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1689
1690 /* Reset the Queue pointers */
1691 OMAP_DMA_CHAIN_QINIT(chain_id);
1692
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001693 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001694 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001695
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001696 return 0;
1697}
1698EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1699
1700/* Get the index of the ongoing DMA in chain */
1701/**
1702 * @brief omap_get_dma_chain_index - Get the element and frame index
1703 * of the ongoing DMA in chain
1704 *
1705 * @param chain_id
1706 * @param ei - Element index
1707 * @param fi - Frame index
1708 *
1709 * @return - Success : 0
1710 * Failure : -EINVAL
1711 */
1712int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1713{
1714 int lch;
1715 int *channels;
1716
1717 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001718 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001719 printk(KERN_ERR "Invalid chain id\n");
1720 return -EINVAL;
1721 }
1722
1723 /* Check if the chain exists */
1724 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1725 printk(KERN_ERR "Chain doesn't exists\n");
1726 return -EINVAL;
1727 }
1728 if ((!ei) || (!fi))
1729 return -EINVAL;
1730
1731 channels = dma_linked_lch[chain_id].linked_dmach_q;
1732
1733 /* Get the current channel */
1734 lch = channels[dma_linked_lch[chain_id].q_head];
1735
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001736 *ei = p->dma_read(CCEN, lch);
1737 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001738
1739 return 0;
1740}
1741EXPORT_SYMBOL(omap_get_dma_chain_index);
1742
1743/**
1744 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1745 * ongoing DMA in chain
1746 *
1747 * @param chain_id
1748 *
1749 * @return - Success : Destination position
1750 * Failure : -EINVAL
1751 */
1752int omap_get_dma_chain_dst_pos(int chain_id)
1753{
1754 int lch;
1755 int *channels;
1756
1757 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001758 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001759 printk(KERN_ERR "Invalid chain id\n");
1760 return -EINVAL;
1761 }
1762
1763 /* Check if the chain exists */
1764 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1765 printk(KERN_ERR "Chain doesn't exists\n");
1766 return -EINVAL;
1767 }
1768
1769 channels = dma_linked_lch[chain_id].linked_dmach_q;
1770
1771 /* Get the current channel */
1772 lch = channels[dma_linked_lch[chain_id].q_head];
1773
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001774 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001775}
1776EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1777
1778/**
1779 * @brief omap_get_dma_chain_src_pos - Get the source position
1780 * of the ongoing DMA in chain
1781 * @param chain_id
1782 *
1783 * @return - Success : Destination position
1784 * Failure : -EINVAL
1785 */
1786int omap_get_dma_chain_src_pos(int chain_id)
1787{
1788 int lch;
1789 int *channels;
1790
1791 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001792 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001793 printk(KERN_ERR "Invalid chain id\n");
1794 return -EINVAL;
1795 }
1796
1797 /* Check if the chain exists */
1798 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1799 printk(KERN_ERR "Chain doesn't exists\n");
1800 return -EINVAL;
1801 }
1802
1803 channels = dma_linked_lch[chain_id].linked_dmach_q;
1804
1805 /* Get the current channel */
1806 lch = channels[dma_linked_lch[chain_id].q_head];
1807
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001808 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001809}
1810EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001811#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001812
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001813/*----------------------------------------------------------------------------*/
1814
1815#ifdef CONFIG_ARCH_OMAP1
1816
1817static int omap1_dma_handle_ch(int ch)
1818{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001819 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001820
1821 if (enable_1510_mode && ch >= 6) {
1822 csr = dma_chan[ch].saved_csr;
1823 dma_chan[ch].saved_csr = 0;
1824 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001825 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001826 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1827 dma_chan[ch + 6].saved_csr = csr >> 7;
1828 csr &= 0x7f;
1829 }
1830 if ((csr & 0x3f) == 0)
1831 return 0;
1832 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001833 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
1834 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001835 return 0;
1836 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001837 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001838 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001839 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001840 pr_warn("DMA synchronization event drop occurred with device %d\n",
1841 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001842 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1843 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1844 if (likely(dma_chan[ch].callback != NULL))
1845 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001846
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001847 return 1;
1848}
1849
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001850static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001851{
1852 int ch = ((int) dev_id) - 1;
1853 int handled = 0;
1854
1855 for (;;) {
1856 int handled_now = 0;
1857
1858 handled_now += omap1_dma_handle_ch(ch);
1859 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1860 handled_now += omap1_dma_handle_ch(ch + 6);
1861 if (!handled_now)
1862 break;
1863 handled += handled_now;
1864 }
1865
1866 return handled ? IRQ_HANDLED : IRQ_NONE;
1867}
1868
1869#else
1870#define omap1_dma_irq_handler NULL
1871#endif
1872
Tony Lindgren140455f2010-02-12 12:26:48 -08001873#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001874
1875static int omap2_dma_handle_ch(int ch)
1876{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001877 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001878
Juha Yrjola31513692006-12-06 17:13:47 -08001879 if (!status) {
1880 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001881 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001882 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001883 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001884 }
1885 if (unlikely(dma_chan[ch].dev_id == -1)) {
1886 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001887 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1888 status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001889 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001890 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001891 if (unlikely(status & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001892 pr_info("DMA synchronization event drop occurred with device %d\n",
1893 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001894 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001895 printk(KERN_INFO "DMA transaction error with device %d\n",
1896 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001897 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001898 u32 ccr;
1899
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001900 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001901 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001902 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001903 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1904 }
1905 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001906 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1907 printk(KERN_INFO "DMA secure error with device %d\n",
1908 dma_chan[ch].dev_id);
1909 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1910 printk(KERN_INFO "DMA misaligned error with device %d\n",
1911 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001912
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001913 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001914 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001915 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001916 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001917
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001918 /* If the ch is not chained then chain_id will be -1 */
1919 if (dma_chan[ch].chain_id != -1) {
1920 int chain_id = dma_chan[ch].chain_id;
1921 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001922 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001923 dma_chan[dma_chan[ch].next_linked_ch].state =
1924 DMA_CH_STARTED;
1925 if (dma_linked_lch[chain_id].chain_mode ==
1926 OMAP_DMA_DYNAMIC_CHAIN)
1927 disable_lnk(ch);
1928
1929 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1930 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1931
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001932 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001933 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001934 }
1935
Jarkko Nikula538528d2008-02-13 11:47:29 +02001936 if (likely(dma_chan[ch].callback != NULL))
1937 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001938
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001939 return 0;
1940}
1941
1942/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001943static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001944{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001945 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001946 int i;
1947
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001948 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001949 if (val == 0) {
1950 if (printk_ratelimit())
1951 printk(KERN_WARNING "Spurious DMA IRQ\n");
1952 return IRQ_HANDLED;
1953 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001954 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001955 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001956 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001957 if (val & 1)
1958 omap2_dma_handle_ch(i);
1959 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001960 }
1961
1962 return IRQ_HANDLED;
1963}
1964
1965static struct irqaction omap24xx_dma_irq = {
1966 .name = "DMA",
1967 .handler = omap2_dma_irq_handler,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001968};
1969
1970#else
1971static struct irqaction omap24xx_dma_irq;
1972#endif
1973
1974/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001975
Tero Kristof2d11852008-08-28 13:13:31 +00001976void omap_dma_global_context_save(void)
1977{
1978 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001979 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001980 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001981 p->dma_read(OCP_SYSCONFIG, 0);
1982 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001983}
1984
1985void omap_dma_global_context_restore(void)
1986{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001987 int ch;
1988
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001989 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1990 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001991 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001992 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001993 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001994
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001995 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001996 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001997
1998 for (ch = 0; ch < dma_chan_count; ch++)
1999 if (dma_chan[ch].dev_id != -1)
2000 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00002001}
2002
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002003static int omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002004{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002005 int ch, ret = 0;
2006 int dma_irq;
2007 char irq_name[4];
2008 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002009
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002010 p = pdev->dev.platform_data;
2011 if (!p) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002012 dev_err(&pdev->dev,
2013 "%s: System DMA initialized without platform data\n",
2014 __func__);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002015 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002016 }
2017
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002018 d = p->dma_attr;
2019 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002020
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002021 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Chen Gange78f9602013-01-11 13:39:18 +08002022 && (omap_dma_reserve_channels < d->lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002023 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002024
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002025 dma_lch_count = d->lch_count;
2026 dma_chan_count = dma_lch_count;
2027 dma_chan = d->chan;
2028 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002029
Tony Lindgren82809602012-10-30 11:03:22 -07002030 if (dma_omap2plus()) {
Tony Lindgren4d963722008-07-03 12:24:31 +03002031 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2032 dma_lch_count, GFP_KERNEL);
2033 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002034 ret = -ENOMEM;
2035 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002036 }
2037 }
2038
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002039 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002040 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002041 omap_clear_dma(ch);
Tony Lindgren82809602012-10-30 11:03:22 -07002042 if (dma_omap2plus())
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002043 omap2_disable_irq_lch(ch);
2044
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002045 dma_chan[ch].dev_id = -1;
2046 dma_chan[ch].next_lch = -1;
2047
2048 if (ch >= 6 && enable_1510_mode)
2049 continue;
2050
Tony Lindgren82809602012-10-30 11:03:22 -07002051 if (dma_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002052 /*
2053 * request_irq() doesn't like dev_id (ie. ch) being
2054 * zero, so we have to kludge around this.
2055 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002056 sprintf(&irq_name[0], "%d", ch);
2057 dma_irq = platform_get_irq_byname(pdev, irq_name);
2058
2059 if (dma_irq < 0) {
2060 ret = dma_irq;
2061 goto exit_dma_irq_fail;
2062 }
2063
2064 /* INT_DMA_LCD is handled in lcd_dma.c */
2065 if (dma_irq == INT_DMA_LCD)
2066 continue;
2067
2068 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002069 omap1_dma_irq_handler, 0, "DMA",
2070 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002071 if (ret != 0)
2072 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002073 }
2074 }
2075
Tony Lindgren82809602012-10-30 11:03:22 -07002076 if (d->dev_caps & IS_RW_PRIORITY)
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002077 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2078 DMA_DEFAULT_FIFO_DEPTH, 0);
2079
Tony Lindgren82809602012-10-30 11:03:22 -07002080 if (dma_omap2plus()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002081 strcpy(irq_name, "0");
2082 dma_irq = platform_get_irq_byname(pdev, irq_name);
2083 if (dma_irq < 0) {
2084 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
Wei Yongjun94b1d612013-07-16 20:10:46 +08002085 ret = dma_irq;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002086 goto exit_dma_lch_fail;
2087 }
2088 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2089 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002090 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
2091 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002092 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002093 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002094 }
2095
Tony Lindgren82809602012-10-30 11:03:22 -07002096 /* reserve dma channels 0 and 1 in high security devices on 34xx */
2097 if (d->dev_caps & HS_CHANNELS_RESERVED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002098 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002099 dma_chan[0].dev_id = 0;
2100 dma_chan[1].dev_id = 1;
2101 }
2102 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002103 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002104
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002105exit_dma_irq_fail:
Paul Walmsley7852ec02012-07-26 00:54:26 -06002106 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
2107 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002108 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2109 dma_irq = platform_get_irq(pdev, irq_rel);
2110 free_irq(dma_irq, (void *)(irq_rel + 1));
2111 }
2112
2113exit_dma_lch_fail:
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002114 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002115 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002116}
2117
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002118static int omap_system_dma_remove(struct platform_device *pdev)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002119{
2120 int dma_irq;
2121
Tony Lindgren82809602012-10-30 11:03:22 -07002122 if (dma_omap2plus()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002123 char irq_name[4];
2124 strcpy(irq_name, "0");
2125 dma_irq = platform_get_irq_byname(pdev, irq_name);
2126 remove_irq(dma_irq, &omap24xx_dma_irq);
2127 } else {
2128 int irq_rel = 0;
2129 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2130 dma_irq = platform_get_irq(pdev, irq_rel);
2131 free_irq(dma_irq, (void *)(irq_rel + 1));
2132 }
2133 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002134 kfree(dma_chan);
2135 return 0;
2136}
2137
2138static struct platform_driver omap_system_dma_driver = {
2139 .probe = omap_system_dma_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002140 .remove = omap_system_dma_remove,
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002141 .driver = {
2142 .name = "omap_dma_system"
2143 },
2144};
2145
2146static int __init omap_system_dma_init(void)
2147{
2148 return platform_driver_register(&omap_system_dma_driver);
2149}
2150arch_initcall(omap_system_dma_init);
2151
2152static void __exit omap_system_dma_exit(void)
2153{
2154 platform_driver_unregister(&omap_system_dma_driver);
2155}
2156
2157MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2158MODULE_LICENSE("GPL");
2159MODULE_ALIAS("platform:" DRIVER_NAME);
2160MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002161
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002162/*
2163 * Reserve the omap SDMA channels using cmdline bootarg
2164 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2165 */
2166static int __init omap_dma_cmdline_reserve_ch(char *str)
2167{
2168 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2169 omap_dma_reserve_channels = 0;
2170 return 1;
2171}
2172
2173__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2174
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002175