blob: e06c34bdc34aa11c2bcac80097391dddfb552f5d [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;
704 if (dev_id == 0)
705 break;
706 }
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
897 dma_chan_link_map[lch] = 1;
898 /* Set the link register of the first channel */
899 enable_lnk(lch);
900
901 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
902 cur_lch = dma_chan[lch].next_lch;
903 do {
904 next_lch = dma_chan[cur_lch].next_lch;
905
906 /* The loop case: we've been here already */
907 if (dma_chan_link_map[cur_lch])
908 break;
909 /* Mark the current channel */
910 dma_chan_link_map[cur_lch] = 1;
911
912 enable_lnk(cur_lch);
913 omap_enable_channel_irq(cur_lch);
914
915 cur_lch = next_lch;
916 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800917 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800918 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000919
920 omap_enable_channel_irq(lch);
921
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800922 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300923
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800924 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
925 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300926 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800927
Russell King35453582012-04-14 18:57:10 +0100928 /*
929 * As dma_write() uses IO accessors which are weakly ordered, there
930 * is no guarantee that data in coherent DMA memory will be visible
931 * to the DMA device. Add a memory barrier here to ensure that any
932 * such data is visible prior to enabling DMA.
933 */
934 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800935 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000936
937 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
938}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300939EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000940
941void omap_stop_dma(int lch)
942{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300943 u32 l;
944
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700945 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700946 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700947
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800948 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800949 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
950 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700951 int i = 0;
952 u32 sys_cf;
953
954 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800955 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700956 sys_cf = l;
957 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
958 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800959 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700960
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800961 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700962 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800963 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700964
965 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800966 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700967 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
968 OMAP_DMA_CCR_WR_ACTIVE))) {
969 udelay(5);
970 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800971 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700972 }
973 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600974 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700975 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800976 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700977 } else {
978 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800979 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700980 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700981
Russell King35453582012-04-14 18:57:10 +0100982 /*
983 * Ensure that data transferred by DMA is visible to any access
984 * after DMA has been disabled. This is important for coherent
985 * DMA regions.
986 */
987 mb();
988
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000989 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
990 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600991 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000992
993 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
994 do {
995 /* The loop case: we've been here already */
996 if (dma_chan_link_map[cur_lch])
997 break;
998 /* Mark the current channel */
999 dma_chan_link_map[cur_lch] = 1;
1000
1001 disable_lnk(cur_lch);
1002
1003 next_lch = dma_chan[cur_lch].next_lch;
1004 cur_lch = next_lch;
1005 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001006 }
1007
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001008 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1009}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001010EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001011
1012/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001013 * Allows changing the DMA callback function or data. This may be needed if
1014 * the driver shares a single DMA channel for multiple dma triggers.
1015 */
1016int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001017 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001018 void *data)
1019{
1020 unsigned long flags;
1021
1022 if (lch < 0)
1023 return -ENODEV;
1024
1025 spin_lock_irqsave(&dma_chan_lock, flags);
1026 if (dma_chan[lch].dev_id == -1) {
1027 printk(KERN_ERR "DMA callback for not set for free channel\n");
1028 spin_unlock_irqrestore(&dma_chan_lock, flags);
1029 return -EINVAL;
1030 }
1031 dma_chan[lch].callback = callback;
1032 dma_chan[lch].data = data;
1033 spin_unlock_irqrestore(&dma_chan_lock, flags);
1034
1035 return 0;
1036}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001037EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001038
1039/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001040 * Returns current physical source address for the given DMA channel.
1041 * If the channel is running the caller must disable interrupts prior calling
1042 * this function and process the returned value before re-enabling interrupt to
1043 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001044 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001045 * in incorrect return value.
1046 */
1047dma_addr_t omap_get_dma_src_pos(int lch)
1048{
Tony Lindgren0695de32007-05-07 18:24:14 -07001049 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001050
Tony Lindgren82809602012-10-30 11:03:22 -07001051 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001052 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001053 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001054 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001055
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001056 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001057 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001058
Tony Lindgren82809602012-10-30 11:03:22 -07001059 if (!dma_omap15xx()) {
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001060 /*
1061 * CDAC == 0 indicates that the DMA transfer on the channel has
1062 * not been started (no data has been transferred so far).
1063 * Return the programmed source start address in this case.
1064 */
1065 if (likely(p->dma_read(CDAC, lch)))
1066 offset = p->dma_read(CSAC, lch);
1067 else
1068 offset = p->dma_read(CSSA, lch);
1069 }
1070
Tony Lindgren82809602012-10-30 11:03:22 -07001071 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001072 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001073
1074 return offset;
1075}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001076EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001077
1078/*
1079 * Returns current physical destination address for the given DMA channel.
1080 * If the channel is running the caller must disable interrupts prior calling
1081 * this function and process the returned value before re-enabling interrupt to
1082 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001083 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001084 * in incorrect return value.
1085 */
1086dma_addr_t omap_get_dma_dst_pos(int lch)
1087{
Tony Lindgren0695de32007-05-07 18:24:14 -07001088 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001089
Tony Lindgren82809602012-10-30 11:03:22 -07001090 if (dma_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001091 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001092 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001093 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001094
Tony Lindgren0499bde2008-07-03 12:24:36 +03001095 /*
1096 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1097 * read before the DMA controller finished disabling the channel.
1098 */
Tony Lindgren82809602012-10-30 11:03:22 -07001099 if (!dma_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001100 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001101 /*
1102 * CDAC == 0 indicates that the DMA transfer on the channel has
1103 * not been started (no data has been transferred so far).
1104 * Return the programmed destination start address in this case.
1105 */
1106 if (unlikely(!offset))
1107 offset = p->dma_read(CDSA, lch);
1108 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001109
Tony Lindgren82809602012-10-30 11:03:22 -07001110 if (dma_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001111 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001112
1113 return offset;
1114}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001115EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001116
Tony Lindgren0499bde2008-07-03 12:24:36 +03001117int omap_get_dma_active_status(int lch)
1118{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001119 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001120}
1121EXPORT_SYMBOL(omap_get_dma_active_status);
1122
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001123int omap_dma_running(void)
1124{
1125 int lch;
1126
Tony Lindgren82809602012-10-30 11:03:22 -07001127 if (dma_omap1())
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001128 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001129 return 1;
1130
1131 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001132 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001133 return 1;
1134
1135 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001136}
1137
1138/*
1139 * lch_queue DMA will start right after lch_head one is finished.
1140 * For this DMA link to start, you still need to start (see omap_start_dma)
1141 * the first one. That will fire up the entire queue.
1142 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001143void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001144{
1145 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001146 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001147 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001148 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001149 return;
1150 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001151 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1152 BUG();
1153 return;
1154 }
1155
1156 if ((dma_chan[lch_head].dev_id == -1) ||
1157 (dma_chan[lch_queue].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001158 pr_err("omap_dma: trying to link non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001159 dump_stack();
1160 }
1161
1162 dma_chan[lch_head].next_lch = lch_queue;
1163}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001164EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001165
1166/*
1167 * Once the DMA queue is stopped, we can destroy it.
1168 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001169void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001170{
1171 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001172 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001173 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001174 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001175 return;
1176 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001177 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1178 BUG();
1179 return;
1180 }
1181
1182 if (dma_chan[lch_head].next_lch != lch_queue ||
1183 dma_chan[lch_head].next_lch == -1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001184 pr_err("omap_dma: trying to unlink non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001185 dump_stack();
1186 }
1187
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001188 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001189 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001190 pr_err("omap_dma: You need to stop the DMA channels before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001191 dump_stack();
1192 }
1193
1194 dma_chan[lch_head].next_lch = -1;
1195}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001196EXPORT_SYMBOL(omap_dma_unlink_lch);
1197
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001198#ifndef CONFIG_ARCH_OMAP1
1199/* Create chain of DMA channesls */
1200static void create_dma_lch_chain(int lch_head, int lch_queue)
1201{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001202 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001203
1204 /* Check if this is the first link in chain */
1205 if (dma_chan[lch_head].next_linked_ch == -1) {
1206 dma_chan[lch_head].next_linked_ch = lch_queue;
1207 dma_chan[lch_head].prev_linked_ch = lch_queue;
1208 dma_chan[lch_queue].next_linked_ch = lch_head;
1209 dma_chan[lch_queue].prev_linked_ch = lch_head;
1210 }
1211
1212 /* a link exists, link the new channel in circular chain */
1213 else {
1214 dma_chan[lch_queue].next_linked_ch =
1215 dma_chan[lch_head].next_linked_ch;
1216 dma_chan[lch_queue].prev_linked_ch = lch_head;
1217 dma_chan[lch_head].next_linked_ch = lch_queue;
1218 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1219 lch_queue;
1220 }
1221
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001222 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001223 l &= ~(0x1f);
1224 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001225 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001226
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001227 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001228 l &= ~(0x1f);
1229 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001230 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001231}
1232
1233/**
1234 * @brief omap_request_dma_chain : Request a chain of DMA channels
1235 *
1236 * @param dev_id - Device id using the dma channel
1237 * @param dev_name - Device name
1238 * @param callback - Call back function
1239 * @chain_id -
1240 * @no_of_chans - Number of channels requested
1241 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1242 * OMAP_DMA_DYNAMIC_CHAIN
1243 * @params - Channel parameters
1244 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001245 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001246 * Failure: -EINVAL/-ENOMEM
1247 */
1248int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001249 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001250 void *data),
1251 int *chain_id, int no_of_chans, int chain_mode,
1252 struct omap_dma_channel_params params)
1253{
1254 int *channels;
1255 int i, err;
1256
1257 /* Is the chain mode valid ? */
1258 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1259 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1260 printk(KERN_ERR "Invalid chain mode requested\n");
1261 return -EINVAL;
1262 }
1263
1264 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001265 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001266 printk(KERN_ERR "Invalid Number of channels requested\n");
1267 return -EINVAL;
1268 }
1269
manjugk manjugkea221a62010-05-14 12:05:25 -07001270 /*
1271 * Allocate a queue to maintain the status of the channels
1272 * in the chain
1273 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001274 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1275 if (channels == NULL) {
1276 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1277 return -ENOMEM;
1278 }
1279
1280 /* request and reserve DMA channels for the chain */
1281 for (i = 0; i < no_of_chans; i++) {
1282 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001283 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001284 if (err < 0) {
1285 int j;
1286 for (j = 0; j < i; j++)
1287 omap_free_dma(channels[j]);
1288 kfree(channels);
1289 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1290 return err;
1291 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001292 dma_chan[channels[i]].prev_linked_ch = -1;
1293 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1294
1295 /*
1296 * Allowing client drivers to set common parameters now,
1297 * so that later only relevant (src_start, dest_start
1298 * and element count) can be set
1299 */
1300 omap_set_dma_params(channels[i], &params);
1301 }
1302
1303 *chain_id = channels[0];
1304 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1305 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1306 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1307 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1308
1309 for (i = 0; i < no_of_chans; i++)
1310 dma_chan[channels[i]].chain_id = *chain_id;
1311
1312 /* Reset the Queue pointers */
1313 OMAP_DMA_CHAIN_QINIT(*chain_id);
1314
1315 /* Set up the chain */
1316 if (no_of_chans == 1)
1317 create_dma_lch_chain(channels[0], channels[0]);
1318 else {
1319 for (i = 0; i < (no_of_chans - 1); i++)
1320 create_dma_lch_chain(channels[i], channels[i + 1]);
1321 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001322
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001323 return 0;
1324}
1325EXPORT_SYMBOL(omap_request_dma_chain);
1326
1327/**
1328 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1329 * params after setting it. Dont do this while dma is running!!
1330 *
1331 * @param chain_id - Chained logical channel id.
1332 * @param params
1333 *
1334 * @return - Success : 0
1335 * Failure : -EINVAL
1336 */
1337int omap_modify_dma_chain_params(int chain_id,
1338 struct omap_dma_channel_params params)
1339{
1340 int *channels;
1341 u32 i;
1342
1343 /* Check for input params */
1344 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001345 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001346 printk(KERN_ERR "Invalid chain id\n");
1347 return -EINVAL;
1348 }
1349
1350 /* Check if the chain exists */
1351 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1352 printk(KERN_ERR "Chain doesn't exists\n");
1353 return -EINVAL;
1354 }
1355 channels = dma_linked_lch[chain_id].linked_dmach_q;
1356
1357 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1358 /*
1359 * Allowing client drivers to set common parameters now,
1360 * so that later only relevant (src_start, dest_start
1361 * and element count) can be set
1362 */
1363 omap_set_dma_params(channels[i], &params);
1364 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001365
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001366 return 0;
1367}
1368EXPORT_SYMBOL(omap_modify_dma_chain_params);
1369
1370/**
1371 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1372 *
1373 * @param chain_id
1374 *
1375 * @return - Success : 0
1376 * Failure : -EINVAL
1377 */
1378int omap_free_dma_chain(int chain_id)
1379{
1380 int *channels;
1381 u32 i;
1382
1383 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001384 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001385 printk(KERN_ERR "Invalid chain id\n");
1386 return -EINVAL;
1387 }
1388
1389 /* Check if the chain exists */
1390 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1391 printk(KERN_ERR "Chain doesn't exists\n");
1392 return -EINVAL;
1393 }
1394
1395 channels = dma_linked_lch[chain_id].linked_dmach_q;
1396 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1397 dma_chan[channels[i]].next_linked_ch = -1;
1398 dma_chan[channels[i]].prev_linked_ch = -1;
1399 dma_chan[channels[i]].chain_id = -1;
1400 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1401 omap_free_dma(channels[i]);
1402 }
1403
1404 kfree(channels);
1405
1406 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1407 dma_linked_lch[chain_id].chain_mode = -1;
1408 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001409
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001410 return (0);
1411}
1412EXPORT_SYMBOL(omap_free_dma_chain);
1413
1414/**
1415 * @brief omap_dma_chain_status - Check if the chain is in
1416 * active / inactive state.
1417 * @param chain_id
1418 *
1419 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1420 * Failure : -EINVAL
1421 */
1422int omap_dma_chain_status(int chain_id)
1423{
1424 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001425 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001426 printk(KERN_ERR "Invalid chain id\n");
1427 return -EINVAL;
1428 }
1429
1430 /* Check if the chain exists */
1431 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1432 printk(KERN_ERR "Chain doesn't exists\n");
1433 return -EINVAL;
1434 }
1435 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1436 dma_linked_lch[chain_id].q_count);
1437
1438 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1439 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001440
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001441 return OMAP_DMA_CHAIN_ACTIVE;
1442}
1443EXPORT_SYMBOL(omap_dma_chain_status);
1444
1445/**
1446 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1447 * set the params and start the transfer.
1448 *
1449 * @param chain_id
1450 * @param src_start - buffer start address
1451 * @param dest_start - Dest address
1452 * @param elem_count
1453 * @param frame_count
1454 * @param callbk_data - channel callback parameter data.
1455 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301456 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001457 * Failure: -EINVAL/-EBUSY
1458 */
1459int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1460 int elem_count, int frame_count, void *callbk_data)
1461{
1462 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001463 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001464 int start_dma = 0;
1465
Tony Lindgren97b7f712008-07-03 12:24:37 +03001466 /*
1467 * if buffer size is less than 1 then there is
1468 * no use of starting the chain
1469 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001470 if (elem_count < 1) {
1471 printk(KERN_ERR "Invalid buffer size\n");
1472 return -EINVAL;
1473 }
1474
1475 /* Check for input params */
1476 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001477 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001478 printk(KERN_ERR "Invalid chain id\n");
1479 return -EINVAL;
1480 }
1481
1482 /* Check if the chain exists */
1483 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1484 printk(KERN_ERR "Chain doesn't exist\n");
1485 return -EINVAL;
1486 }
1487
1488 /* Check if all the channels in chain are in use */
1489 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1490 return -EBUSY;
1491
1492 /* Frame count may be negative in case of indexed transfers */
1493 channels = dma_linked_lch[chain_id].linked_dmach_q;
1494
1495 /* Get a free channel */
1496 lch = channels[dma_linked_lch[chain_id].q_tail];
1497
1498 /* Store the callback data */
1499 dma_chan[lch].data = callbk_data;
1500
1501 /* Increment the q_tail */
1502 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1503
1504 /* Set the params to the free channel */
1505 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001506 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001507 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001508 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001509
1510 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001511 p->dma_write(elem_count, CEN, lch);
1512 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001513
Tony Lindgren97b7f712008-07-03 12:24:37 +03001514 /*
1515 * If the chain is dynamically linked,
1516 * then we may have to start the chain if its not active
1517 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001518 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1519
Tony Lindgren97b7f712008-07-03 12:24:37 +03001520 /*
1521 * In Dynamic chain, if the chain is not started,
1522 * queue the channel
1523 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001524 if (dma_linked_lch[chain_id].chain_state ==
1525 DMA_CHAIN_NOTSTARTED) {
1526 /* Enable the link in previous channel */
1527 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1528 DMA_CH_QUEUED)
1529 enable_lnk(dma_chan[lch].prev_linked_ch);
1530 dma_chan[lch].state = DMA_CH_QUEUED;
1531 }
1532
Tony Lindgren97b7f712008-07-03 12:24:37 +03001533 /*
1534 * Chain is already started, make sure its active,
1535 * if not then start the chain
1536 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001537 else {
1538 start_dma = 1;
1539
1540 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1541 DMA_CH_STARTED) {
1542 enable_lnk(dma_chan[lch].prev_linked_ch);
1543 dma_chan[lch].state = DMA_CH_QUEUED;
1544 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001545 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001546 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001547 disable_lnk(dma_chan[lch].
1548 prev_linked_ch);
1549 pr_debug("\n prev ch is stopped\n");
1550 start_dma = 1;
1551 }
1552 }
1553
1554 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1555 == DMA_CH_QUEUED) {
1556 enable_lnk(dma_chan[lch].prev_linked_ch);
1557 dma_chan[lch].state = DMA_CH_QUEUED;
1558 start_dma = 0;
1559 }
1560 omap_enable_channel_irq(lch);
1561
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001562 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001563
Tony Lindgren0499bde2008-07-03 12:24:36 +03001564 if ((0 == (l & (1 << 24))))
1565 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001566 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001567 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001568 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001569 if (0 == (l & (1 << 7))) {
1570 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001571 dma_chan[lch].state = DMA_CH_STARTED;
1572 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001573 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001574 } else
1575 start_dma = 0;
1576 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001577 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001578 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001579 }
1580 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1581 }
1582 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001583
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301584 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001585}
1586EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1587
1588/**
1589 * @brief omap_start_dma_chain_transfers - Start the chain
1590 *
1591 * @param chain_id
1592 *
1593 * @return - Success : 0
1594 * Failure : -EINVAL/-EBUSY
1595 */
1596int omap_start_dma_chain_transfers(int chain_id)
1597{
1598 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001599 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001600
Tony Lindgren4d963722008-07-03 12:24:31 +03001601 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001602 printk(KERN_ERR "Invalid chain id\n");
1603 return -EINVAL;
1604 }
1605
1606 channels = dma_linked_lch[chain_id].linked_dmach_q;
1607
1608 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1609 printk(KERN_ERR "Chain is already started\n");
1610 return -EBUSY;
1611 }
1612
1613 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1614 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1615 i++) {
1616 enable_lnk(channels[i]);
1617 omap_enable_channel_irq(channels[i]);
1618 }
1619 } else {
1620 omap_enable_channel_irq(channels[0]);
1621 }
1622
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001623 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001624 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001625 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1626 dma_chan[channels[0]].state = DMA_CH_STARTED;
1627
Tony Lindgren0499bde2008-07-03 12:24:36 +03001628 if ((0 == (l & (1 << 24))))
1629 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001630 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001631 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001632 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001633
1634 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001635
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001636 return 0;
1637}
1638EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1639
1640/**
1641 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1642 *
1643 * @param chain_id
1644 *
1645 * @return - Success : 0
1646 * Failure : EINVAL
1647 */
1648int omap_stop_dma_chain_transfers(int chain_id)
1649{
1650 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001651 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001652 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001653
1654 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001655 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001656 printk(KERN_ERR "Invalid chain id\n");
1657 return -EINVAL;
1658 }
1659
1660 /* Check if the chain exists */
1661 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1662 printk(KERN_ERR "Chain doesn't exists\n");
1663 return -EINVAL;
1664 }
1665 channels = dma_linked_lch[chain_id].linked_dmach_q;
1666
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001667 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001668 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001669 l = sys_cf;
1670 /* Middle mode reg set no Standby */
1671 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001672 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001673 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001674
1675 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1676
1677 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001678 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001679 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001680 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001681
1682 /* Disable the link in all the channels */
1683 disable_lnk(channels[i]);
1684 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1685
1686 }
1687 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1688
1689 /* Reset the Queue pointers */
1690 OMAP_DMA_CHAIN_QINIT(chain_id);
1691
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001692 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001693 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001694
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001695 return 0;
1696}
1697EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1698
1699/* Get the index of the ongoing DMA in chain */
1700/**
1701 * @brief omap_get_dma_chain_index - Get the element and frame index
1702 * of the ongoing DMA in chain
1703 *
1704 * @param chain_id
1705 * @param ei - Element index
1706 * @param fi - Frame index
1707 *
1708 * @return - Success : 0
1709 * Failure : -EINVAL
1710 */
1711int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1712{
1713 int lch;
1714 int *channels;
1715
1716 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001717 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001718 printk(KERN_ERR "Invalid chain id\n");
1719 return -EINVAL;
1720 }
1721
1722 /* Check if the chain exists */
1723 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1724 printk(KERN_ERR "Chain doesn't exists\n");
1725 return -EINVAL;
1726 }
1727 if ((!ei) || (!fi))
1728 return -EINVAL;
1729
1730 channels = dma_linked_lch[chain_id].linked_dmach_q;
1731
1732 /* Get the current channel */
1733 lch = channels[dma_linked_lch[chain_id].q_head];
1734
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001735 *ei = p->dma_read(CCEN, lch);
1736 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001737
1738 return 0;
1739}
1740EXPORT_SYMBOL(omap_get_dma_chain_index);
1741
1742/**
1743 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1744 * ongoing DMA in chain
1745 *
1746 * @param chain_id
1747 *
1748 * @return - Success : Destination position
1749 * Failure : -EINVAL
1750 */
1751int omap_get_dma_chain_dst_pos(int chain_id)
1752{
1753 int lch;
1754 int *channels;
1755
1756 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001757 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001758 printk(KERN_ERR "Invalid chain id\n");
1759 return -EINVAL;
1760 }
1761
1762 /* Check if the chain exists */
1763 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1764 printk(KERN_ERR "Chain doesn't exists\n");
1765 return -EINVAL;
1766 }
1767
1768 channels = dma_linked_lch[chain_id].linked_dmach_q;
1769
1770 /* Get the current channel */
1771 lch = channels[dma_linked_lch[chain_id].q_head];
1772
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001773 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001774}
1775EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1776
1777/**
1778 * @brief omap_get_dma_chain_src_pos - Get the source position
1779 * of the ongoing DMA in chain
1780 * @param chain_id
1781 *
1782 * @return - Success : Destination position
1783 * Failure : -EINVAL
1784 */
1785int omap_get_dma_chain_src_pos(int chain_id)
1786{
1787 int lch;
1788 int *channels;
1789
1790 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001791 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001792 printk(KERN_ERR "Invalid chain id\n");
1793 return -EINVAL;
1794 }
1795
1796 /* Check if the chain exists */
1797 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1798 printk(KERN_ERR "Chain doesn't exists\n");
1799 return -EINVAL;
1800 }
1801
1802 channels = dma_linked_lch[chain_id].linked_dmach_q;
1803
1804 /* Get the current channel */
1805 lch = channels[dma_linked_lch[chain_id].q_head];
1806
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001807 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001808}
1809EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001810#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001811
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001812/*----------------------------------------------------------------------------*/
1813
1814#ifdef CONFIG_ARCH_OMAP1
1815
1816static int omap1_dma_handle_ch(int ch)
1817{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001818 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001819
1820 if (enable_1510_mode && ch >= 6) {
1821 csr = dma_chan[ch].saved_csr;
1822 dma_chan[ch].saved_csr = 0;
1823 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001824 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001825 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1826 dma_chan[ch + 6].saved_csr = csr >> 7;
1827 csr &= 0x7f;
1828 }
1829 if ((csr & 0x3f) == 0)
1830 return 0;
1831 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001832 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
1833 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001834 return 0;
1835 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001836 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001837 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001838 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001839 pr_warn("DMA synchronization event drop occurred with device %d\n",
1840 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001841 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1842 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1843 if (likely(dma_chan[ch].callback != NULL))
1844 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001845
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001846 return 1;
1847}
1848
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001849static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001850{
1851 int ch = ((int) dev_id) - 1;
1852 int handled = 0;
1853
1854 for (;;) {
1855 int handled_now = 0;
1856
1857 handled_now += omap1_dma_handle_ch(ch);
1858 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1859 handled_now += omap1_dma_handle_ch(ch + 6);
1860 if (!handled_now)
1861 break;
1862 handled += handled_now;
1863 }
1864
1865 return handled ? IRQ_HANDLED : IRQ_NONE;
1866}
1867
1868#else
1869#define omap1_dma_irq_handler NULL
1870#endif
1871
Tony Lindgren140455f2010-02-12 12:26:48 -08001872#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001873
1874static int omap2_dma_handle_ch(int ch)
1875{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001876 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001877
Juha Yrjola31513692006-12-06 17:13:47 -08001878 if (!status) {
1879 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001880 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001881 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001882 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001883 }
1884 if (unlikely(dma_chan[ch].dev_id == -1)) {
1885 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001886 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1887 status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001888 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001889 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001890 if (unlikely(status & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001891 pr_info("DMA synchronization event drop occurred with device %d\n",
1892 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001893 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001894 printk(KERN_INFO "DMA transaction error with device %d\n",
1895 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001896 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001897 u32 ccr;
1898
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001899 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001900 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001901 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001902 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1903 }
1904 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001905 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1906 printk(KERN_INFO "DMA secure error with device %d\n",
1907 dma_chan[ch].dev_id);
1908 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1909 printk(KERN_INFO "DMA misaligned error with device %d\n",
1910 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001911
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001912 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001913 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001914 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001915 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001916
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001917 /* If the ch is not chained then chain_id will be -1 */
1918 if (dma_chan[ch].chain_id != -1) {
1919 int chain_id = dma_chan[ch].chain_id;
1920 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001921 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001922 dma_chan[dma_chan[ch].next_linked_ch].state =
1923 DMA_CH_STARTED;
1924 if (dma_linked_lch[chain_id].chain_mode ==
1925 OMAP_DMA_DYNAMIC_CHAIN)
1926 disable_lnk(ch);
1927
1928 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1929 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1930
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001931 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001932 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001933 }
1934
Jarkko Nikula538528d2008-02-13 11:47:29 +02001935 if (likely(dma_chan[ch].callback != NULL))
1936 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001937
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001938 return 0;
1939}
1940
1941/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001942static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001943{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001944 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001945 int i;
1946
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001947 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001948 if (val == 0) {
1949 if (printk_ratelimit())
1950 printk(KERN_WARNING "Spurious DMA IRQ\n");
1951 return IRQ_HANDLED;
1952 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001953 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001954 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001955 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001956 if (val & 1)
1957 omap2_dma_handle_ch(i);
1958 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001959 }
1960
1961 return IRQ_HANDLED;
1962}
1963
1964static struct irqaction omap24xx_dma_irq = {
1965 .name = "DMA",
1966 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001967 .flags = IRQF_DISABLED
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);
2085 goto exit_dma_lch_fail;
2086 }
2087 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2088 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002089 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
2090 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002091 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002092 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002093 }
2094
Tony Lindgren82809602012-10-30 11:03:22 -07002095 /* reserve dma channels 0 and 1 in high security devices on 34xx */
2096 if (d->dev_caps & HS_CHANNELS_RESERVED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002097 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002098 dma_chan[0].dev_id = 0;
2099 dma_chan[1].dev_id = 1;
2100 }
2101 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002102 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002103
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002104exit_dma_irq_fail:
Paul Walmsley7852ec02012-07-26 00:54:26 -06002105 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
2106 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002107 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2108 dma_irq = platform_get_irq(pdev, irq_rel);
2109 free_irq(dma_irq, (void *)(irq_rel + 1));
2110 }
2111
2112exit_dma_lch_fail:
2113 kfree(p);
2114 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002115 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002116 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002117}
2118
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002119static int omap_system_dma_remove(struct platform_device *pdev)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002120{
2121 int dma_irq;
2122
Tony Lindgren82809602012-10-30 11:03:22 -07002123 if (dma_omap2plus()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002124 char irq_name[4];
2125 strcpy(irq_name, "0");
2126 dma_irq = platform_get_irq_byname(pdev, irq_name);
2127 remove_irq(dma_irq, &omap24xx_dma_irq);
2128 } else {
2129 int irq_rel = 0;
2130 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2131 dma_irq = platform_get_irq(pdev, irq_rel);
2132 free_irq(dma_irq, (void *)(irq_rel + 1));
2133 }
2134 }
2135 kfree(p);
2136 kfree(d);
2137 kfree(dma_chan);
2138 return 0;
2139}
2140
2141static struct platform_driver omap_system_dma_driver = {
2142 .probe = omap_system_dma_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002143 .remove = omap_system_dma_remove,
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002144 .driver = {
2145 .name = "omap_dma_system"
2146 },
2147};
2148
2149static int __init omap_system_dma_init(void)
2150{
2151 return platform_driver_register(&omap_system_dma_driver);
2152}
2153arch_initcall(omap_system_dma_init);
2154
2155static void __exit omap_system_dma_exit(void)
2156{
2157 platform_driver_unregister(&omap_system_dma_driver);
2158}
2159
2160MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2161MODULE_LICENSE("GPL");
2162MODULE_ALIAS("platform:" DRIVER_NAME);
2163MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002164
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002165/*
2166 * Reserve the omap SDMA channels using cmdline bootarg
2167 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2168 */
2169static int __init omap_dma_cmdline_reserve_ch(char *str)
2170{
2171 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2172 omap_dma_reserve_channels = 0;
2173 return 1;
2174}
2175
2176__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2177
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002178