blob: 49803cc18787fe3fbe0522ccf7a5031bbd5d160e [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
Lokesh Vutla2b6c4e72012-10-15 14:04:53 -070039#include <plat-omap/dma-omap.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010040
Tony Lindgrene4c060d2012-10-05 13:25:59 -070041#include "../mach-omap1/soc.h"
42#include "../mach-omap2/soc.h"
43
Paul Walmsleybc4d8b52012-04-13 06:34:30 -060044/*
45 * MAX_LOGICAL_DMA_CH_COUNT: the maximum number of logical DMA
46 * channels that an instance of the SDMA IP block can support. Used
47 * to size arrays. (The actual maximum on a particular SoC may be less
48 * than this -- for example, OMAP1 SDMA instances only support 17 logical
49 * DMA channels.)
50 */
51#define MAX_LOGICAL_DMA_CH_COUNT 32
52
Anand Gadiyarf8151e52007-12-01 12:14:11 -080053#undef DEBUG
54
55#ifndef CONFIG_ARCH_OMAP1
56enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
57 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
58};
59
60enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000061#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010062
Tony Lindgren97b7f712008-07-03 12:24:37 +030063#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020064#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065
Tony Lindgren97b7f712008-07-03 12:24:37 +030066#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010067
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080068static struct omap_system_dma_plat_info *p;
69static struct omap_dma_dev_attr *d;
70
Tony Lindgren97b7f712008-07-03 12:24:37 +030071static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080072static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010073
Tero Kristof2d11852008-08-28 13:13:31 +000074static struct omap_dma_global_context_registers {
75 u32 dma_irqenable_l0;
76 u32 dma_ocp_sysconfig;
77 u32 dma_gcr;
78} omap_dma_global_context;
79
Anand Gadiyarf8151e52007-12-01 12:14:11 -080080struct dma_link_info {
81 int *linked_dmach_q;
82 int no_of_lchs_linked;
83
84 int q_count;
85 int q_tail;
86 int q_head;
87
88 int chain_state;
89 int chain_mode;
90
91};
92
Tony Lindgren4d963722008-07-03 12:24:31 +030093static struct dma_link_info *dma_linked_lch;
94
95#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080096
97/* Chain handling macros */
98#define OMAP_DMA_CHAIN_QINIT(chain_id) \
99 do { \
100 dma_linked_lch[chain_id].q_head = \
101 dma_linked_lch[chain_id].q_tail = \
102 dma_linked_lch[chain_id].q_count = 0; \
103 } while (0)
104#define OMAP_DMA_CHAIN_QFULL(chain_id) \
105 (dma_linked_lch[chain_id].no_of_lchs_linked == \
106 dma_linked_lch[chain_id].q_count)
107#define OMAP_DMA_CHAIN_QLAST(chain_id) \
108 do { \
109 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
110 dma_linked_lch[chain_id].q_count) \
111 } while (0)
112#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
113 (0 == dma_linked_lch[chain_id].q_count)
114#define __OMAP_DMA_CHAIN_INCQ(end) \
115 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
116#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
117 do { \
118 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
119 dma_linked_lch[chain_id].q_count--; \
120 } while (0)
121
122#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
123 do { \
124 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
125 dma_linked_lch[chain_id].q_count++; \
126 } while (0)
127#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300128
129static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700131static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
133static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300134static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800136static inline void disable_lnk(int lch);
137static void omap_disable_channel_irq(int lch);
138static inline void omap_enable_channel_irq(int lch);
139
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000140#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800141 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142
143#ifdef CONFIG_ARCH_OMAP15XX
144/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800145static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000146{
147 return enable_1510_mode;
148}
149#else
150#define omap_dma_in_1510_mode() 0
151#endif
152
153#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100154static inline int get_gdma_dev(int req)
155{
156 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
157 int shift = ((req - 1) % 5) * 6;
158
159 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
160}
161
162static inline void set_gdma_dev(int req, int dev)
163{
164 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
165 int shift = ((req - 1) % 5) * 6;
166 u32 l;
167
168 l = omap_readl(reg);
169 l &= ~(0x3f << shift);
170 l |= (dev - 1) << shift;
171 omap_writel(l, reg);
172}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000173#else
174#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800175#define omap_readl(reg) 0
176#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000177#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100178
Tony Lindgren54b693d2012-10-02 13:39:28 -0700179#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300180void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100181{
182 unsigned long reg;
183 u32 l;
184
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300185 if (cpu_class_is_omap1()) {
186 switch (dst_port) {
187 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
188 reg = OMAP_TC_OCPT1_PRIOR;
189 break;
190 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
191 reg = OMAP_TC_OCPT2_PRIOR;
192 break;
193 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
194 reg = OMAP_TC_EMIFF_PRIOR;
195 break;
196 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
197 reg = OMAP_TC_EMIFS_PRIOR;
198 break;
199 default:
200 BUG();
201 return;
202 }
203 l = omap_readl(reg);
204 l &= ~(0xf << 8);
205 l |= (priority & 0xf) << 8;
206 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208}
Tony Lindgren54b693d2012-10-02 13:39:28 -0700209#endif
210
211#ifdef CONFIG_ARCH_OMAP2PLUS
212void omap_set_dma_priority(int lch, int dst_port, int priority)
213{
214 u32 ccr;
215
216 ccr = p->dma_read(CCR, lch);
217 if (priority)
218 ccr |= (1 << 6);
219 else
220 ccr &= ~(1 << 6);
221 p->dma_write(ccr, CCR, lch);
222}
223#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300224EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100225
226void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000227 int frame_count, int sync_mode,
228 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100229{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300230 u32 l;
231
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800232 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300233 l &= ~0x03;
234 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800235 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100236
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000237 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300238 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100239
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800240 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300241 ccr &= ~(1 << 5);
242 if (sync_mode == OMAP_DMA_SYNC_FRAME)
243 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800244 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300245
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800246 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300247 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000248 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300249 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800250 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000251 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100252
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800253 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300254 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100255
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800256 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200258 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300259 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200260 val |= (dma_trigger & ~0x1f) << 14;
261 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000262
263 if (sync_mode & OMAP_DMA_SYNC_FRAME)
264 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700265 else
266 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000267
268 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
269 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700270 else
271 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000272
Samu Onkalo72a11792010-08-02 14:21:40 +0300273 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000274 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300275 val |= (1 << 23); /* Prefetch */
276 } else if (src_or_dst_synch) {
277 val |= 1 << 24; /* source synch */
278 } else {
279 val &= ~(1 << 24); /* dest synch */
280 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800281 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000282 }
283
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800284 p->dma_write(elem_count, CEN, lch);
285 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300287EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000288
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
290{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100291 BUG_ON(omap_dma_in_1510_mode());
292
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700293 if (cpu_class_is_omap1()) {
294 u16 w;
295
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800296 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700297 w &= ~0x03;
298
299 switch (mode) {
300 case OMAP_DMA_CONSTANT_FILL:
301 w |= 0x01;
302 break;
303 case OMAP_DMA_TRANSPARENT_COPY:
304 w |= 0x02;
305 break;
306 case OMAP_DMA_COLOR_DIS:
307 break;
308 default:
309 BUG();
310 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800311 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700312
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800313 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700314 w &= ~0x0f;
315 /* Default is channel type 2D */
316 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800317 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700318 w |= 1; /* Channel type G */
319 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800320 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700321 }
322
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800323 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700324 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000325
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800326 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700327 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300328
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700329 switch (mode) {
330 case OMAP_DMA_CONSTANT_FILL:
331 val |= 1 << 16;
332 break;
333 case OMAP_DMA_TRANSPARENT_COPY:
334 val |= 1 << 17;
335 break;
336 case OMAP_DMA_COLOR_DIS:
337 break;
338 default:
339 BUG();
340 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800341 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100342
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700343 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800344 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300347EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300349void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
350{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800351 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300352 u32 csdp;
353
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800354 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300355 csdp &= ~(0x3 << 16);
356 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800357 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300358 }
359}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300360EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300361
Tony Lindgren0499bde2008-07-03 12:24:36 +0300362void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
363{
364 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
365 u32 l;
366
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800367 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300368 l &= ~0x7;
369 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800370 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300371 }
372}
373EXPORT_SYMBOL(omap_set_dma_channel_mode);
374
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000375/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000377 unsigned long src_start,
378 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100379{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300380 u32 l;
381
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000382 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300383 u16 w;
384
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800385 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300386 w &= ~(0x1f << 2);
387 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800388 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300389 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300390
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800391 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300392 l &= ~(0x03 << 12);
393 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800394 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300395
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800396 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800398 p->dma_write(src_ei, CSEI, lch);
399 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300400}
401EXPORT_SYMBOL(omap_set_dma_src_params);
402
403void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000404{
405 omap_set_dma_transfer_params(lch, params->data_type,
406 params->elem_count, params->frame_count,
407 params->sync_mode, params->trigger,
408 params->src_or_dst_synch);
409 omap_set_dma_src_params(lch, params->src_port,
410 params->src_amode, params->src_start,
411 params->src_ei, params->src_fi);
412
413 omap_set_dma_dest_params(lch, params->dst_port,
414 params->dst_amode, params->dst_start,
415 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800416 if (params->read_prio || params->write_prio)
417 omap_dma_set_prio_lch(lch, params->read_prio,
418 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100419}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300420EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100421
422void omap_set_dma_src_index(int lch, int eidx, int fidx)
423{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300424 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000425 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300426
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800427 p->dma_write(eidx, CSEI, lch);
428 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300430EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431
432void omap_set_dma_src_data_pack(int lch, int enable)
433{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300434 u32 l;
435
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800436 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300437 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000438 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300439 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800440 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300442EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443
444void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
445{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700446 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300447 u32 l;
448
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800449 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300450 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100452 switch (burst_mode) {
453 case OMAP_DMA_DATA_BURST_DIS:
454 break;
455 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800456 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700457 burst = 0x1;
458 else
459 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100460 break;
461 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800462 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700463 burst = 0x2;
464 break;
465 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700466 /*
467 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100468 * w |= (0x03 << 7);
469 * fall through
470 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700471 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800472 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700473 burst = 0x3;
474 break;
475 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700476 /*
477 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700478 * fall through
479 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480 default:
481 BUG();
482 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300483
484 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800485 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300487EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000489/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000491 unsigned long dest_start,
492 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300494 u32 l;
495
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000496 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800497 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300498 l &= ~(0x1f << 9);
499 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800500 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000501 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800503 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300504 l &= ~(0x03 << 14);
505 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800506 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800508 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800510 p->dma_write(dst_ei, CDEI, lch);
511 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100512}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300513EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100514
515void omap_set_dma_dest_index(int lch, int eidx, int fidx)
516{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300517 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000518 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300519
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800520 p->dma_write(eidx, CDEI, lch);
521 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300523EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524
525void omap_set_dma_dest_data_pack(int lch, int enable)
526{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300527 u32 l;
528
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800529 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300530 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000531 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300532 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800533 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300535EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536
537void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
538{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700539 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300540 u32 l;
541
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800542 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300543 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100545 switch (burst_mode) {
546 case OMAP_DMA_DATA_BURST_DIS:
547 break;
548 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800549 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700550 burst = 0x1;
551 else
552 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100553 break;
554 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800555 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700556 burst = 0x2;
557 else
558 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700560 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800561 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700562 burst = 0x3;
563 break;
564 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700565 /*
566 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700567 * fall through
568 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569 default:
570 printk(KERN_ERR "Invalid DMA burst mode\n");
571 BUG();
572 return;
573 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300574 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800575 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300577EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000579static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580{
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700581 /* Clear CSR */
582 if (cpu_class_is_omap1())
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700583 p->dma_read(CSR, lch);
584 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800585 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000586
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800588 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589}
590
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700591static inline void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100592{
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700593 /* disable channel interrupts */
594 p->dma_write(0, CICR, lch);
595 /* Clear CSR */
596 if (cpu_class_is_omap1())
597 p->dma_read(CSR, lch);
598 else
599 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100600}
601
602void omap_enable_dma_irq(int lch, u16 bits)
603{
604 dma_chan[lch].enabled_irqs |= bits;
605}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300606EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607
608void omap_disable_dma_irq(int lch, u16 bits)
609{
610 dma_chan[lch].enabled_irqs &= ~bits;
611}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300612EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100613
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000614static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100615{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300616 u32 l;
617
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800618 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300619
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000620 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300621 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100622
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000623 /* Set the ENABLE_LNK bits */
624 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300625 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800626
627#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300628 if (cpu_class_is_omap2())
629 if (dma_chan[lch].next_linked_ch != -1)
630 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800631#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300632
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800633 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100634}
635
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000636static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100637{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300638 u32 l;
639
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800640 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300641
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000642 /* Disable interrupts */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700643 omap_disable_channel_irq(lch);
644
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000645 if (cpu_class_is_omap1()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000646 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300647 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100648 }
649
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800650 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000651 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300652 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000653 }
654
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800655 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000656 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
657}
658
659static inline void omap2_enable_irq_lch(int lch)
660{
661 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800662 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000663
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800664 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000665 return;
666
Tao Huee907322009-11-10 18:55:17 -0800667 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700668 /* clear IRQ STATUS */
669 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
670 /* Enable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800671 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000672 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800673 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800674 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675}
676
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700677static inline void omap2_disable_irq_lch(int lch)
678{
679 u32 val;
680 unsigned long flags;
681
682 if (!cpu_class_is_omap2())
683 return;
684
685 spin_lock_irqsave(&dma_chan_lock, flags);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700686 /* Disable interrupt */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800687 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700688 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800689 p->dma_write(val, IRQENABLE_L0, lch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700690 /* clear IRQ STATUS */
691 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700692 spin_unlock_irqrestore(&dma_chan_lock, flags);
693}
694
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300696 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100697 void *data, int *dma_ch_out)
698{
699 int ch, free_ch = -1;
700 unsigned long flags;
701 struct omap_dma_lch *chan;
702
703 spin_lock_irqsave(&dma_chan_lock, flags);
704 for (ch = 0; ch < dma_chan_count; ch++) {
705 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
706 free_ch = ch;
707 if (dev_id == 0)
708 break;
709 }
710 }
711 if (free_ch == -1) {
712 spin_unlock_irqrestore(&dma_chan_lock, flags);
713 return -EBUSY;
714 }
715 chan = dma_chan + free_ch;
716 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000717
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800718 if (p->clear_lch_regs)
719 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000720
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800721 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000722 omap_clear_dma(free_ch);
723
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100724 spin_unlock_irqrestore(&dma_chan_lock, flags);
725
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 chan->dev_name = dev_name;
727 chan->callback = callback;
728 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800729 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300730
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800731#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300732 if (cpu_class_is_omap2()) {
733 chan->chain_id = -1;
734 chan->next_linked_ch = -1;
735 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800736#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300737
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700738 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000739
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700740 if (cpu_class_is_omap1())
741 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800742 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700743 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
744 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745
746 if (cpu_is_omap16xx()) {
747 /* If the sync device is set, configure it dynamically. */
748 if (dev_id != 0) {
749 set_gdma_dev(free_ch + 1, dev_id);
750 dev_id = free_ch + 1;
751 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300752 /*
753 * Disable the 1510 compatibility mode and set the sync device
754 * id.
755 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800756 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700757 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800758 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000760
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800761 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000762 omap_enable_channel_irq(free_ch);
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700763 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000764 }
765
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766 *dma_ch_out = free_ch;
767
768 return 0;
769}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300770EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100771
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000772void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773{
774 unsigned long flags;
775
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000776 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300777 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000778 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100779 return;
780 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300781
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700782 /* Disable interrupt for logical channel */
783 if (cpu_class_is_omap2())
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700784 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000785
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700786 /* Disable all DMA interrupts for the channel. */
787 omap_disable_channel_irq(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000788
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700789 /* Make sure the DMA transfer is stopped. */
790 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000791
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700792 /* Clear registers */
793 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000794 omap_clear_dma(lch);
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700795
796 spin_lock_irqsave(&dma_chan_lock, flags);
797 dma_chan[lch].dev_id = -1;
798 dma_chan[lch].next_lch = -1;
799 dma_chan[lch].callback = NULL;
800 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100801}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300802EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100803
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800804/**
805 * @brief omap_dma_set_global_params : Set global priority settings for dma
806 *
807 * @param arb_rate
808 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700809 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
810 * DMA_THREAD_RESERVE_ONET
811 * DMA_THREAD_RESERVE_TWOT
812 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800813 */
814void
815omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
816{
817 u32 reg;
818
819 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800820 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800821 return;
822 }
823
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700824 if (max_fifo_depth == 0)
825 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800826 if (arb_rate == 0)
827 arb_rate = 1;
828
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700829 reg = 0xff & max_fifo_depth;
830 reg |= (0x3 & tparams) << 12;
831 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800832
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800833 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800834}
835EXPORT_SYMBOL(omap_dma_set_global_params);
836
837/**
838 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
839 *
840 * @param lch
841 * @param read_prio - Read priority
842 * @param write_prio - Write priority
843 * Both of the above can be set with one of the following values :
844 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
845 */
846int
847omap_dma_set_prio_lch(int lch, unsigned char read_prio,
848 unsigned char write_prio)
849{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300850 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800851
Tony Lindgren4d963722008-07-03 12:24:31 +0300852 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800853 printk(KERN_ERR "Invalid channel id\n");
854 return -EINVAL;
855 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800856 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300857 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkard07c3df2012-04-28 20:19:10 +0530858 if (cpu_class_is_omap2() && !cpu_is_omap242x())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300859 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800860 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300861 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800862
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800863 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300864
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800865 return 0;
866}
867EXPORT_SYMBOL(omap_dma_set_prio_lch);
868
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000869/*
870 * Clears any DMA state so the DMA engine is ready to restart with new buffers
871 * through omap_start_dma(). Any buffers in flight are discarded.
872 */
873void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100874{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000875 unsigned long flags;
876
877 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800878 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000879 local_irq_restore(flags);
880}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300881EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000882
883void omap_start_dma(int lch)
884{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300885 u32 l;
886
manjugk manjugk519e6162010-03-04 07:11:56 +0000887 /*
888 * The CPC/CDAC register needs to be initialized to zero
889 * before starting dma transfer.
890 */
891 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800892 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000893 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800894 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000895
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000896 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
897 int next_lch, cur_lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600898 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000899
900 dma_chan_link_map[lch] = 1;
901 /* Set the link register of the first channel */
902 enable_lnk(lch);
903
904 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
905 cur_lch = dma_chan[lch].next_lch;
906 do {
907 next_lch = dma_chan[cur_lch].next_lch;
908
909 /* The loop case: we've been here already */
910 if (dma_chan_link_map[cur_lch])
911 break;
912 /* Mark the current channel */
913 dma_chan_link_map[cur_lch] = 1;
914
915 enable_lnk(cur_lch);
916 omap_enable_channel_irq(cur_lch);
917
918 cur_lch = next_lch;
919 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800920 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800921 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000922
923 omap_enable_channel_irq(lch);
924
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800925 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300926
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800927 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
928 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300929 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800930
Russell King35453582012-04-14 18:57:10 +0100931 /*
932 * As dma_write() uses IO accessors which are weakly ordered, there
933 * is no guarantee that data in coherent DMA memory will be visible
934 * to the DMA device. Add a memory barrier here to ensure that any
935 * such data is visible prior to enabling DMA.
936 */
937 mb();
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800938 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000939
940 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
941}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300942EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000943
944void omap_stop_dma(int lch)
945{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300946 u32 l;
947
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700948 /* Disable all interrupts on the channel */
Oleg Matcovschibedfb7a2012-05-15 14:35:08 -0700949 omap_disable_channel_irq(lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700950
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800951 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800952 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
953 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700954 int i = 0;
955 u32 sys_cf;
956
957 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800958 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700959 sys_cf = l;
960 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
961 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800962 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700963
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800964 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700965 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800966 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700967
968 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800969 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700970 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
971 OMAP_DMA_CCR_WR_ACTIVE))) {
972 udelay(5);
973 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800974 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700975 }
976 if (i >= 100)
Paul Walmsley7852ec02012-07-26 00:54:26 -0600977 pr_err("DMA drain did not complete on lch %d\n", lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700978 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800979 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700980 } else {
981 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800982 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700983 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700984
Russell King35453582012-04-14 18:57:10 +0100985 /*
986 * Ensure that data transferred by DMA is visible to any access
987 * after DMA has been disabled. This is important for coherent
988 * DMA regions.
989 */
990 mb();
991
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000992 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
993 int next_lch, cur_lch = lch;
Paul Walmsleybc4d8b52012-04-13 06:34:30 -0600994 char dma_chan_link_map[MAX_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000995
996 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
997 do {
998 /* The loop case: we've been here already */
999 if (dma_chan_link_map[cur_lch])
1000 break;
1001 /* Mark the current channel */
1002 dma_chan_link_map[cur_lch] = 1;
1003
1004 disable_lnk(cur_lch);
1005
1006 next_lch = dma_chan[cur_lch].next_lch;
1007 cur_lch = next_lch;
1008 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001009 }
1010
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001011 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1012}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001013EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001014
1015/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001016 * Allows changing the DMA callback function or data. This may be needed if
1017 * the driver shares a single DMA channel for multiple dma triggers.
1018 */
1019int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001020 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001021 void *data)
1022{
1023 unsigned long flags;
1024
1025 if (lch < 0)
1026 return -ENODEV;
1027
1028 spin_lock_irqsave(&dma_chan_lock, flags);
1029 if (dma_chan[lch].dev_id == -1) {
1030 printk(KERN_ERR "DMA callback for not set for free channel\n");
1031 spin_unlock_irqrestore(&dma_chan_lock, flags);
1032 return -EINVAL;
1033 }
1034 dma_chan[lch].callback = callback;
1035 dma_chan[lch].data = data;
1036 spin_unlock_irqrestore(&dma_chan_lock, flags);
1037
1038 return 0;
1039}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001040EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001041
1042/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001043 * Returns current physical source address for the given DMA channel.
1044 * If the channel is running the caller must disable interrupts prior calling
1045 * this function and process the returned value before re-enabling interrupt to
1046 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001047 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001048 * in incorrect return value.
1049 */
1050dma_addr_t omap_get_dma_src_pos(int lch)
1051{
Tony Lindgren0695de32007-05-07 18:24:14 -07001052 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001053
Tony Lindgren0499bde2008-07-03 12:24:36 +03001054 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001055 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001056 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001057 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001058
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001059 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001060 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001061
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001062 if (!cpu_is_omap15xx()) {
1063 /*
1064 * CDAC == 0 indicates that the DMA transfer on the channel has
1065 * not been started (no data has been transferred so far).
1066 * Return the programmed source start address in this case.
1067 */
1068 if (likely(p->dma_read(CDAC, lch)))
1069 offset = p->dma_read(CSAC, lch);
1070 else
1071 offset = p->dma_read(CSSA, lch);
1072 }
1073
Tony Lindgren0499bde2008-07-03 12:24:36 +03001074 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001075 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001076
1077 return offset;
1078}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001079EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001080
1081/*
1082 * Returns current physical destination address for the given DMA channel.
1083 * If the channel is running the caller must disable interrupts prior calling
1084 * this function and process the returned value before re-enabling interrupt to
1085 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001086 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001087 * in incorrect return value.
1088 */
1089dma_addr_t omap_get_dma_dst_pos(int lch)
1090{
Tony Lindgren0695de32007-05-07 18:24:14 -07001091 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001092
Tony Lindgren0499bde2008-07-03 12:24:36 +03001093 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001094 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001095 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001096 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001097
Tony Lindgren0499bde2008-07-03 12:24:36 +03001098 /*
1099 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1100 * read before the DMA controller finished disabling the channel.
1101 */
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001102 if (!cpu_is_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001103 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001104 /*
1105 * CDAC == 0 indicates that the DMA transfer on the channel has
1106 * not been started (no data has been transferred so far).
1107 * Return the programmed destination start address in this case.
1108 */
1109 if (unlikely(!offset))
1110 offset = p->dma_read(CDSA, lch);
1111 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001112
1113 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001114 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001115
1116 return offset;
1117}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001118EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001119
Tony Lindgren0499bde2008-07-03 12:24:36 +03001120int omap_get_dma_active_status(int lch)
1121{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001122 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001123}
1124EXPORT_SYMBOL(omap_get_dma_active_status);
1125
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001126int omap_dma_running(void)
1127{
1128 int lch;
1129
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001130 if (cpu_class_is_omap1())
1131 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001132 return 1;
1133
1134 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001135 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001136 return 1;
1137
1138 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001139}
1140
1141/*
1142 * lch_queue DMA will start right after lch_head one is finished.
1143 * For this DMA link to start, you still need to start (see omap_start_dma)
1144 * the first one. That will fire up the entire queue.
1145 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001146void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001147{
1148 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001149 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001150 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001151 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001152 return;
1153 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001154 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1155 BUG();
1156 return;
1157 }
1158
1159 if ((dma_chan[lch_head].dev_id == -1) ||
1160 (dma_chan[lch_queue].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001161 pr_err("omap_dma: trying to link non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001162 dump_stack();
1163 }
1164
1165 dma_chan[lch_head].next_lch = lch_queue;
1166}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001167EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001168
1169/*
1170 * Once the DMA queue is stopped, we can destroy it.
1171 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001172void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001173{
1174 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001175 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001176 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001177 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001178 return;
1179 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001180 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1181 BUG();
1182 return;
1183 }
1184
1185 if (dma_chan[lch_head].next_lch != lch_queue ||
1186 dma_chan[lch_head].next_lch == -1) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001187 pr_err("omap_dma: trying to unlink non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001188 dump_stack();
1189 }
1190
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001191 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001192 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001193 pr_err("omap_dma: You need to stop the DMA channels before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001194 dump_stack();
1195 }
1196
1197 dma_chan[lch_head].next_lch = -1;
1198}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001199EXPORT_SYMBOL(omap_dma_unlink_lch);
1200
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001201#ifndef CONFIG_ARCH_OMAP1
1202/* Create chain of DMA channesls */
1203static void create_dma_lch_chain(int lch_head, int lch_queue)
1204{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001205 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001206
1207 /* Check if this is the first link in chain */
1208 if (dma_chan[lch_head].next_linked_ch == -1) {
1209 dma_chan[lch_head].next_linked_ch = lch_queue;
1210 dma_chan[lch_head].prev_linked_ch = lch_queue;
1211 dma_chan[lch_queue].next_linked_ch = lch_head;
1212 dma_chan[lch_queue].prev_linked_ch = lch_head;
1213 }
1214
1215 /* a link exists, link the new channel in circular chain */
1216 else {
1217 dma_chan[lch_queue].next_linked_ch =
1218 dma_chan[lch_head].next_linked_ch;
1219 dma_chan[lch_queue].prev_linked_ch = lch_head;
1220 dma_chan[lch_head].next_linked_ch = lch_queue;
1221 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1222 lch_queue;
1223 }
1224
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001225 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001226 l &= ~(0x1f);
1227 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001228 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001229
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001230 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001231 l &= ~(0x1f);
1232 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001233 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001234}
1235
1236/**
1237 * @brief omap_request_dma_chain : Request a chain of DMA channels
1238 *
1239 * @param dev_id - Device id using the dma channel
1240 * @param dev_name - Device name
1241 * @param callback - Call back function
1242 * @chain_id -
1243 * @no_of_chans - Number of channels requested
1244 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1245 * OMAP_DMA_DYNAMIC_CHAIN
1246 * @params - Channel parameters
1247 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001248 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001249 * Failure: -EINVAL/-ENOMEM
1250 */
1251int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001252 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001253 void *data),
1254 int *chain_id, int no_of_chans, int chain_mode,
1255 struct omap_dma_channel_params params)
1256{
1257 int *channels;
1258 int i, err;
1259
1260 /* Is the chain mode valid ? */
1261 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1262 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1263 printk(KERN_ERR "Invalid chain mode requested\n");
1264 return -EINVAL;
1265 }
1266
1267 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001268 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001269 printk(KERN_ERR "Invalid Number of channels requested\n");
1270 return -EINVAL;
1271 }
1272
manjugk manjugkea221a62010-05-14 12:05:25 -07001273 /*
1274 * Allocate a queue to maintain the status of the channels
1275 * in the chain
1276 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001277 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1278 if (channels == NULL) {
1279 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1280 return -ENOMEM;
1281 }
1282
1283 /* request and reserve DMA channels for the chain */
1284 for (i = 0; i < no_of_chans; i++) {
1285 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001286 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001287 if (err < 0) {
1288 int j;
1289 for (j = 0; j < i; j++)
1290 omap_free_dma(channels[j]);
1291 kfree(channels);
1292 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1293 return err;
1294 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001295 dma_chan[channels[i]].prev_linked_ch = -1;
1296 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1297
1298 /*
1299 * Allowing client drivers to set common parameters now,
1300 * so that later only relevant (src_start, dest_start
1301 * and element count) can be set
1302 */
1303 omap_set_dma_params(channels[i], &params);
1304 }
1305
1306 *chain_id = channels[0];
1307 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1308 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1309 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1310 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1311
1312 for (i = 0; i < no_of_chans; i++)
1313 dma_chan[channels[i]].chain_id = *chain_id;
1314
1315 /* Reset the Queue pointers */
1316 OMAP_DMA_CHAIN_QINIT(*chain_id);
1317
1318 /* Set up the chain */
1319 if (no_of_chans == 1)
1320 create_dma_lch_chain(channels[0], channels[0]);
1321 else {
1322 for (i = 0; i < (no_of_chans - 1); i++)
1323 create_dma_lch_chain(channels[i], channels[i + 1]);
1324 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001325
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001326 return 0;
1327}
1328EXPORT_SYMBOL(omap_request_dma_chain);
1329
1330/**
1331 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1332 * params after setting it. Dont do this while dma is running!!
1333 *
1334 * @param chain_id - Chained logical channel id.
1335 * @param params
1336 *
1337 * @return - Success : 0
1338 * Failure : -EINVAL
1339 */
1340int omap_modify_dma_chain_params(int chain_id,
1341 struct omap_dma_channel_params params)
1342{
1343 int *channels;
1344 u32 i;
1345
1346 /* Check for input params */
1347 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001348 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001349 printk(KERN_ERR "Invalid chain id\n");
1350 return -EINVAL;
1351 }
1352
1353 /* Check if the chain exists */
1354 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1355 printk(KERN_ERR "Chain doesn't exists\n");
1356 return -EINVAL;
1357 }
1358 channels = dma_linked_lch[chain_id].linked_dmach_q;
1359
1360 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1361 /*
1362 * Allowing client drivers to set common parameters now,
1363 * so that later only relevant (src_start, dest_start
1364 * and element count) can be set
1365 */
1366 omap_set_dma_params(channels[i], &params);
1367 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001368
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001369 return 0;
1370}
1371EXPORT_SYMBOL(omap_modify_dma_chain_params);
1372
1373/**
1374 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1375 *
1376 * @param chain_id
1377 *
1378 * @return - Success : 0
1379 * Failure : -EINVAL
1380 */
1381int omap_free_dma_chain(int chain_id)
1382{
1383 int *channels;
1384 u32 i;
1385
1386 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001387 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001388 printk(KERN_ERR "Invalid chain id\n");
1389 return -EINVAL;
1390 }
1391
1392 /* Check if the chain exists */
1393 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1394 printk(KERN_ERR "Chain doesn't exists\n");
1395 return -EINVAL;
1396 }
1397
1398 channels = dma_linked_lch[chain_id].linked_dmach_q;
1399 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1400 dma_chan[channels[i]].next_linked_ch = -1;
1401 dma_chan[channels[i]].prev_linked_ch = -1;
1402 dma_chan[channels[i]].chain_id = -1;
1403 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1404 omap_free_dma(channels[i]);
1405 }
1406
1407 kfree(channels);
1408
1409 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1410 dma_linked_lch[chain_id].chain_mode = -1;
1411 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001412
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001413 return (0);
1414}
1415EXPORT_SYMBOL(omap_free_dma_chain);
1416
1417/**
1418 * @brief omap_dma_chain_status - Check if the chain is in
1419 * active / inactive state.
1420 * @param chain_id
1421 *
1422 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1423 * Failure : -EINVAL
1424 */
1425int omap_dma_chain_status(int chain_id)
1426{
1427 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001428 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001429 printk(KERN_ERR "Invalid chain id\n");
1430 return -EINVAL;
1431 }
1432
1433 /* Check if the chain exists */
1434 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1435 printk(KERN_ERR "Chain doesn't exists\n");
1436 return -EINVAL;
1437 }
1438 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1439 dma_linked_lch[chain_id].q_count);
1440
1441 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1442 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001443
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001444 return OMAP_DMA_CHAIN_ACTIVE;
1445}
1446EXPORT_SYMBOL(omap_dma_chain_status);
1447
1448/**
1449 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1450 * set the params and start the transfer.
1451 *
1452 * @param chain_id
1453 * @param src_start - buffer start address
1454 * @param dest_start - Dest address
1455 * @param elem_count
1456 * @param frame_count
1457 * @param callbk_data - channel callback parameter data.
1458 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301459 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001460 * Failure: -EINVAL/-EBUSY
1461 */
1462int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1463 int elem_count, int frame_count, void *callbk_data)
1464{
1465 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001466 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001467 int start_dma = 0;
1468
Tony Lindgren97b7f712008-07-03 12:24:37 +03001469 /*
1470 * if buffer size is less than 1 then there is
1471 * no use of starting the chain
1472 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001473 if (elem_count < 1) {
1474 printk(KERN_ERR "Invalid buffer size\n");
1475 return -EINVAL;
1476 }
1477
1478 /* Check for input params */
1479 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001480 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001481 printk(KERN_ERR "Invalid chain id\n");
1482 return -EINVAL;
1483 }
1484
1485 /* Check if the chain exists */
1486 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1487 printk(KERN_ERR "Chain doesn't exist\n");
1488 return -EINVAL;
1489 }
1490
1491 /* Check if all the channels in chain are in use */
1492 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1493 return -EBUSY;
1494
1495 /* Frame count may be negative in case of indexed transfers */
1496 channels = dma_linked_lch[chain_id].linked_dmach_q;
1497
1498 /* Get a free channel */
1499 lch = channels[dma_linked_lch[chain_id].q_tail];
1500
1501 /* Store the callback data */
1502 dma_chan[lch].data = callbk_data;
1503
1504 /* Increment the q_tail */
1505 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1506
1507 /* Set the params to the free channel */
1508 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001509 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001510 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001511 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001512
1513 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001514 p->dma_write(elem_count, CEN, lch);
1515 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001516
Tony Lindgren97b7f712008-07-03 12:24:37 +03001517 /*
1518 * If the chain is dynamically linked,
1519 * then we may have to start the chain if its not active
1520 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001521 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1522
Tony Lindgren97b7f712008-07-03 12:24:37 +03001523 /*
1524 * In Dynamic chain, if the chain is not started,
1525 * queue the channel
1526 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001527 if (dma_linked_lch[chain_id].chain_state ==
1528 DMA_CHAIN_NOTSTARTED) {
1529 /* Enable the link in previous channel */
1530 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1531 DMA_CH_QUEUED)
1532 enable_lnk(dma_chan[lch].prev_linked_ch);
1533 dma_chan[lch].state = DMA_CH_QUEUED;
1534 }
1535
Tony Lindgren97b7f712008-07-03 12:24:37 +03001536 /*
1537 * Chain is already started, make sure its active,
1538 * if not then start the chain
1539 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001540 else {
1541 start_dma = 1;
1542
1543 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1544 DMA_CH_STARTED) {
1545 enable_lnk(dma_chan[lch].prev_linked_ch);
1546 dma_chan[lch].state = DMA_CH_QUEUED;
1547 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001548 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001549 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001550 disable_lnk(dma_chan[lch].
1551 prev_linked_ch);
1552 pr_debug("\n prev ch is stopped\n");
1553 start_dma = 1;
1554 }
1555 }
1556
1557 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1558 == DMA_CH_QUEUED) {
1559 enable_lnk(dma_chan[lch].prev_linked_ch);
1560 dma_chan[lch].state = DMA_CH_QUEUED;
1561 start_dma = 0;
1562 }
1563 omap_enable_channel_irq(lch);
1564
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001565 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001566
Tony Lindgren0499bde2008-07-03 12:24:36 +03001567 if ((0 == (l & (1 << 24))))
1568 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001569 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001570 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001571 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001572 if (0 == (l & (1 << 7))) {
1573 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001574 dma_chan[lch].state = DMA_CH_STARTED;
1575 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001576 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001577 } else
1578 start_dma = 0;
1579 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001580 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001581 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001582 }
1583 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1584 }
1585 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001586
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301587 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001588}
1589EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1590
1591/**
1592 * @brief omap_start_dma_chain_transfers - Start the chain
1593 *
1594 * @param chain_id
1595 *
1596 * @return - Success : 0
1597 * Failure : -EINVAL/-EBUSY
1598 */
1599int omap_start_dma_chain_transfers(int chain_id)
1600{
1601 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001602 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001603
Tony Lindgren4d963722008-07-03 12:24:31 +03001604 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001605 printk(KERN_ERR "Invalid chain id\n");
1606 return -EINVAL;
1607 }
1608
1609 channels = dma_linked_lch[chain_id].linked_dmach_q;
1610
1611 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1612 printk(KERN_ERR "Chain is already started\n");
1613 return -EBUSY;
1614 }
1615
1616 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1617 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1618 i++) {
1619 enable_lnk(channels[i]);
1620 omap_enable_channel_irq(channels[i]);
1621 }
1622 } else {
1623 omap_enable_channel_irq(channels[0]);
1624 }
1625
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001626 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001627 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001628 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1629 dma_chan[channels[0]].state = DMA_CH_STARTED;
1630
Tony Lindgren0499bde2008-07-03 12:24:36 +03001631 if ((0 == (l & (1 << 24))))
1632 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001633 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001634 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001635 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001636
1637 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001638
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001639 return 0;
1640}
1641EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1642
1643/**
1644 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1645 *
1646 * @param chain_id
1647 *
1648 * @return - Success : 0
1649 * Failure : EINVAL
1650 */
1651int omap_stop_dma_chain_transfers(int chain_id)
1652{
1653 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001654 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001655 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001656
1657 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001658 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001659 printk(KERN_ERR "Invalid chain id\n");
1660 return -EINVAL;
1661 }
1662
1663 /* Check if the chain exists */
1664 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1665 printk(KERN_ERR "Chain doesn't exists\n");
1666 return -EINVAL;
1667 }
1668 channels = dma_linked_lch[chain_id].linked_dmach_q;
1669
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001670 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001671 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001672 l = sys_cf;
1673 /* Middle mode reg set no Standby */
1674 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001675 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001676 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001677
1678 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1679
1680 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001681 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001682 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001683 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001684
1685 /* Disable the link in all the channels */
1686 disable_lnk(channels[i]);
1687 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1688
1689 }
1690 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1691
1692 /* Reset the Queue pointers */
1693 OMAP_DMA_CHAIN_QINIT(chain_id);
1694
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001695 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001696 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001697
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001698 return 0;
1699}
1700EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1701
1702/* Get the index of the ongoing DMA in chain */
1703/**
1704 * @brief omap_get_dma_chain_index - Get the element and frame index
1705 * of the ongoing DMA in chain
1706 *
1707 * @param chain_id
1708 * @param ei - Element index
1709 * @param fi - Frame index
1710 *
1711 * @return - Success : 0
1712 * Failure : -EINVAL
1713 */
1714int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1715{
1716 int lch;
1717 int *channels;
1718
1719 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001720 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001721 printk(KERN_ERR "Invalid chain id\n");
1722 return -EINVAL;
1723 }
1724
1725 /* Check if the chain exists */
1726 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1727 printk(KERN_ERR "Chain doesn't exists\n");
1728 return -EINVAL;
1729 }
1730 if ((!ei) || (!fi))
1731 return -EINVAL;
1732
1733 channels = dma_linked_lch[chain_id].linked_dmach_q;
1734
1735 /* Get the current channel */
1736 lch = channels[dma_linked_lch[chain_id].q_head];
1737
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001738 *ei = p->dma_read(CCEN, lch);
1739 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001740
1741 return 0;
1742}
1743EXPORT_SYMBOL(omap_get_dma_chain_index);
1744
1745/**
1746 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1747 * ongoing DMA in chain
1748 *
1749 * @param chain_id
1750 *
1751 * @return - Success : Destination position
1752 * Failure : -EINVAL
1753 */
1754int omap_get_dma_chain_dst_pos(int chain_id)
1755{
1756 int lch;
1757 int *channels;
1758
1759 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001760 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001761 printk(KERN_ERR "Invalid chain id\n");
1762 return -EINVAL;
1763 }
1764
1765 /* Check if the chain exists */
1766 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1767 printk(KERN_ERR "Chain doesn't exists\n");
1768 return -EINVAL;
1769 }
1770
1771 channels = dma_linked_lch[chain_id].linked_dmach_q;
1772
1773 /* Get the current channel */
1774 lch = channels[dma_linked_lch[chain_id].q_head];
1775
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001776 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001777}
1778EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1779
1780/**
1781 * @brief omap_get_dma_chain_src_pos - Get the source position
1782 * of the ongoing DMA in chain
1783 * @param chain_id
1784 *
1785 * @return - Success : Destination position
1786 * Failure : -EINVAL
1787 */
1788int omap_get_dma_chain_src_pos(int chain_id)
1789{
1790 int lch;
1791 int *channels;
1792
1793 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001794 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001795 printk(KERN_ERR "Invalid chain id\n");
1796 return -EINVAL;
1797 }
1798
1799 /* Check if the chain exists */
1800 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1801 printk(KERN_ERR "Chain doesn't exists\n");
1802 return -EINVAL;
1803 }
1804
1805 channels = dma_linked_lch[chain_id].linked_dmach_q;
1806
1807 /* Get the current channel */
1808 lch = channels[dma_linked_lch[chain_id].q_head];
1809
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001810 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001811}
1812EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001813#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001814
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001815/*----------------------------------------------------------------------------*/
1816
1817#ifdef CONFIG_ARCH_OMAP1
1818
1819static int omap1_dma_handle_ch(int ch)
1820{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001821 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001822
1823 if (enable_1510_mode && ch >= 6) {
1824 csr = dma_chan[ch].saved_csr;
1825 dma_chan[ch].saved_csr = 0;
1826 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001827 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001828 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1829 dma_chan[ch + 6].saved_csr = csr >> 7;
1830 csr &= 0x7f;
1831 }
1832 if ((csr & 0x3f) == 0)
1833 return 0;
1834 if (unlikely(dma_chan[ch].dev_id == -1)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001835 pr_warn("Spurious interrupt from DMA channel %d (CSR %04x)\n",
1836 ch, csr);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001837 return 0;
1838 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001839 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001840 pr_warn("DMA timeout with device %d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001841 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001842 pr_warn("DMA synchronization event drop occurred with device %d\n",
1843 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001844 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1845 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1846 if (likely(dma_chan[ch].callback != NULL))
1847 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001848
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001849 return 1;
1850}
1851
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001852static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001853{
1854 int ch = ((int) dev_id) - 1;
1855 int handled = 0;
1856
1857 for (;;) {
1858 int handled_now = 0;
1859
1860 handled_now += omap1_dma_handle_ch(ch);
1861 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1862 handled_now += omap1_dma_handle_ch(ch + 6);
1863 if (!handled_now)
1864 break;
1865 handled += handled_now;
1866 }
1867
1868 return handled ? IRQ_HANDLED : IRQ_NONE;
1869}
1870
1871#else
1872#define omap1_dma_irq_handler NULL
1873#endif
1874
Tony Lindgren140455f2010-02-12 12:26:48 -08001875#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001876
1877static int omap2_dma_handle_ch(int ch)
1878{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001879 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001880
Juha Yrjola31513692006-12-06 17:13:47 -08001881 if (!status) {
1882 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001883 pr_warn("Spurious DMA IRQ for lch %d\n", ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001884 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001885 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001886 }
1887 if (unlikely(dma_chan[ch].dev_id == -1)) {
1888 if (printk_ratelimit())
Paul Walmsley7852ec02012-07-26 00:54:26 -06001889 pr_warn("IRQ %04x for non-allocated DMA channel %d\n",
1890 status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001891 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001892 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001893 if (unlikely(status & OMAP_DMA_DROP_IRQ))
Paul Walmsley7852ec02012-07-26 00:54:26 -06001894 pr_info("DMA synchronization event drop occurred with device %d\n",
1895 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001896 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001897 printk(KERN_INFO "DMA transaction error with device %d\n",
1898 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001899 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001900 u32 ccr;
1901
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001902 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001903 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001904 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001905 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1906 }
1907 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001908 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1909 printk(KERN_INFO "DMA secure error with device %d\n",
1910 dma_chan[ch].dev_id);
1911 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1912 printk(KERN_INFO "DMA misaligned error with device %d\n",
1913 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001914
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001915 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001916 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001917 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001918 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001919
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001920 /* If the ch is not chained then chain_id will be -1 */
1921 if (dma_chan[ch].chain_id != -1) {
1922 int chain_id = dma_chan[ch].chain_id;
1923 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001924 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001925 dma_chan[dma_chan[ch].next_linked_ch].state =
1926 DMA_CH_STARTED;
1927 if (dma_linked_lch[chain_id].chain_mode ==
1928 OMAP_DMA_DYNAMIC_CHAIN)
1929 disable_lnk(ch);
1930
1931 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1932 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1933
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001934 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001935 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001936 }
1937
Jarkko Nikula538528d2008-02-13 11:47:29 +02001938 if (likely(dma_chan[ch].callback != NULL))
1939 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001940
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001941 return 0;
1942}
1943
1944/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001945static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001946{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001947 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001948 int i;
1949
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001950 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001951 if (val == 0) {
1952 if (printk_ratelimit())
1953 printk(KERN_WARNING "Spurious DMA IRQ\n");
1954 return IRQ_HANDLED;
1955 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001956 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001957 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001958 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001959 if (val & 1)
1960 omap2_dma_handle_ch(i);
1961 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001962 }
1963
1964 return IRQ_HANDLED;
1965}
1966
1967static struct irqaction omap24xx_dma_irq = {
1968 .name = "DMA",
1969 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001970 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001971};
1972
1973#else
1974static struct irqaction omap24xx_dma_irq;
1975#endif
1976
1977/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001978
Tero Kristof2d11852008-08-28 13:13:31 +00001979void omap_dma_global_context_save(void)
1980{
1981 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001982 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001983 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001984 p->dma_read(OCP_SYSCONFIG, 0);
1985 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001986}
1987
1988void omap_dma_global_context_restore(void)
1989{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001990 int ch;
1991
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001992 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1993 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001994 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001995 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001996 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001997
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001998 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001999 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03002000
2001 for (ch = 0; ch < dma_chan_count; ch++)
2002 if (dma_chan[ch].dev_id != -1)
2003 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00002004}
2005
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002006static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002007{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002008 int ch, ret = 0;
2009 int dma_irq;
2010 char irq_name[4];
2011 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002012
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002013 p = pdev->dev.platform_data;
2014 if (!p) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002015 dev_err(&pdev->dev,
2016 "%s: System DMA initialized without platform data\n",
2017 __func__);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002018 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002019 }
2020
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002021 d = p->dma_attr;
2022 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002023
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002024 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002025 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002026 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002027
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002028 dma_lch_count = d->lch_count;
2029 dma_chan_count = dma_lch_count;
2030 dma_chan = d->chan;
2031 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002032
2033 if (cpu_class_is_omap2()) {
2034 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2035 dma_lch_count, GFP_KERNEL);
2036 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002037 ret = -ENOMEM;
2038 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002039 }
2040 }
2041
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002042 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002043 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002044 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002045 if (cpu_class_is_omap2())
2046 omap2_disable_irq_lch(ch);
2047
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002048 dma_chan[ch].dev_id = -1;
2049 dma_chan[ch].next_lch = -1;
2050
2051 if (ch >= 6 && enable_1510_mode)
2052 continue;
2053
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002054 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002055 /*
2056 * request_irq() doesn't like dev_id (ie. ch) being
2057 * zero, so we have to kludge around this.
2058 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002059 sprintf(&irq_name[0], "%d", ch);
2060 dma_irq = platform_get_irq_byname(pdev, irq_name);
2061
2062 if (dma_irq < 0) {
2063 ret = dma_irq;
2064 goto exit_dma_irq_fail;
2065 }
2066
2067 /* INT_DMA_LCD is handled in lcd_dma.c */
2068 if (dma_irq == INT_DMA_LCD)
2069 continue;
2070
2071 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002072 omap1_dma_irq_handler, 0, "DMA",
2073 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002074 if (ret != 0)
2075 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002076 }
2077 }
2078
Santosh Shilimkard07c3df2012-04-28 20:19:10 +05302079 if (cpu_class_is_omap2() && !cpu_is_omap242x())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002080 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2081 DMA_DEFAULT_FIFO_DEPTH, 0);
2082
Santosh Shilimkar44169072009-05-28 14:16:04 -07002083 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002084 strcpy(irq_name, "0");
2085 dma_irq = platform_get_irq_byname(pdev, irq_name);
2086 if (dma_irq < 0) {
2087 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2088 goto exit_dma_lch_fail;
2089 }
2090 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2091 if (ret) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002092 dev_err(&pdev->dev, "set_up failed for IRQ %d for DMA (error %d)\n",
2093 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002094 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002095 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002096 }
2097
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002098 /* reserve dma channels 0 and 1 in high security devices */
2099 if (cpu_is_omap34xx() &&
2100 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06002101 pr_info("Reserving DMA channels 0 and 1 for HS ROM code\n");
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002102 dma_chan[0].dev_id = 0;
2103 dma_chan[1].dev_id = 1;
2104 }
2105 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002106 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002107
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002108exit_dma_irq_fail:
Paul Walmsley7852ec02012-07-26 00:54:26 -06002109 dev_err(&pdev->dev, "unable to request IRQ %d for DMA (error %d)\n",
2110 dma_irq, ret);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002111 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2112 dma_irq = platform_get_irq(pdev, irq_rel);
2113 free_irq(dma_irq, (void *)(irq_rel + 1));
2114 }
2115
2116exit_dma_lch_fail:
2117 kfree(p);
2118 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002119 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002120 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002121}
2122
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002123static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2124{
2125 int dma_irq;
2126
2127 if (cpu_class_is_omap2()) {
2128 char irq_name[4];
2129 strcpy(irq_name, "0");
2130 dma_irq = platform_get_irq_byname(pdev, irq_name);
2131 remove_irq(dma_irq, &omap24xx_dma_irq);
2132 } else {
2133 int irq_rel = 0;
2134 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2135 dma_irq = platform_get_irq(pdev, irq_rel);
2136 free_irq(dma_irq, (void *)(irq_rel + 1));
2137 }
2138 }
2139 kfree(p);
2140 kfree(d);
2141 kfree(dma_chan);
2142 return 0;
2143}
2144
2145static struct platform_driver omap_system_dma_driver = {
2146 .probe = omap_system_dma_probe,
Tony Lindgren3e2e6132012-02-23 14:58:08 -08002147 .remove = __devexit_p(omap_system_dma_remove),
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002148 .driver = {
2149 .name = "omap_dma_system"
2150 },
2151};
2152
2153static int __init omap_system_dma_init(void)
2154{
2155 return platform_driver_register(&omap_system_dma_driver);
2156}
2157arch_initcall(omap_system_dma_init);
2158
2159static void __exit omap_system_dma_exit(void)
2160{
2161 platform_driver_unregister(&omap_system_dma_driver);
2162}
2163
2164MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2165MODULE_LICENSE("GPL");
2166MODULE_ALIAS("platform:" DRIVER_NAME);
2167MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002168
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002169/*
2170 * Reserve the omap SDMA channels using cmdline bootarg
2171 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2172 */
2173static int __init omap_dma_cmdline_reserve_ch(char *str)
2174{
2175 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2176 omap_dma_reserve_channels = 0;
2177 return 1;
2178}
2179
2180__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2181
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002182