blob: ecdb3da0dea93704086df1f83dff342813c3afd3 [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
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010041
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/tc.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010043
Anand Gadiyarf8151e52007-12-01 12:14:11 -080044#undef DEBUG
45
46#ifndef CONFIG_ARCH_OMAP1
47enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
48 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
49};
50
51enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000052#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053
Tony Lindgren97b7f712008-07-03 12:24:37 +030054#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020055#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056
Tony Lindgren97b7f712008-07-03 12:24:37 +030057#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010058
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080059static struct omap_system_dma_plat_info *p;
60static struct omap_dma_dev_attr *d;
61
Tony Lindgren97b7f712008-07-03 12:24:37 +030062static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080063static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010064
Tero Kristof2d11852008-08-28 13:13:31 +000065static struct omap_dma_global_context_registers {
66 u32 dma_irqenable_l0;
67 u32 dma_ocp_sysconfig;
68 u32 dma_gcr;
69} omap_dma_global_context;
70
Anand Gadiyarf8151e52007-12-01 12:14:11 -080071struct dma_link_info {
72 int *linked_dmach_q;
73 int no_of_lchs_linked;
74
75 int q_count;
76 int q_tail;
77 int q_head;
78
79 int chain_state;
80 int chain_mode;
81
82};
83
Tony Lindgren4d963722008-07-03 12:24:31 +030084static struct dma_link_info *dma_linked_lch;
85
86#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080087
88/* Chain handling macros */
89#define OMAP_DMA_CHAIN_QINIT(chain_id) \
90 do { \
91 dma_linked_lch[chain_id].q_head = \
92 dma_linked_lch[chain_id].q_tail = \
93 dma_linked_lch[chain_id].q_count = 0; \
94 } while (0)
95#define OMAP_DMA_CHAIN_QFULL(chain_id) \
96 (dma_linked_lch[chain_id].no_of_lchs_linked == \
97 dma_linked_lch[chain_id].q_count)
98#define OMAP_DMA_CHAIN_QLAST(chain_id) \
99 do { \
100 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
101 dma_linked_lch[chain_id].q_count) \
102 } while (0)
103#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
104 (0 == dma_linked_lch[chain_id].q_count)
105#define __OMAP_DMA_CHAIN_INCQ(end) \
106 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
107#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
108 do { \
109 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
110 dma_linked_lch[chain_id].q_count--; \
111 } while (0)
112
113#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
114 do { \
115 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
116 dma_linked_lch[chain_id].q_count++; \
117 } while (0)
118#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300119
120static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700122static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100123
124static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300125static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100126
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800127static inline void disable_lnk(int lch);
128static void omap_disable_channel_irq(int lch);
129static inline void omap_enable_channel_irq(int lch);
130
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000131#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800132 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000133
134#ifdef CONFIG_ARCH_OMAP15XX
135/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800136static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000137{
138 return enable_1510_mode;
139}
140#else
141#define omap_dma_in_1510_mode() 0
142#endif
143
144#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100145static inline int get_gdma_dev(int req)
146{
147 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
148 int shift = ((req - 1) % 5) * 6;
149
150 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
151}
152
153static inline void set_gdma_dev(int req, int dev)
154{
155 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
156 int shift = ((req - 1) % 5) * 6;
157 u32 l;
158
159 l = omap_readl(reg);
160 l &= ~(0x3f << shift);
161 l |= (dev - 1) << shift;
162 omap_writel(l, reg);
163}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000164#else
165#define set_gdma_dev(req, dev) do {} while (0)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800166#define omap_readl(reg) 0
167#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000168#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100169
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300170void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171{
172 unsigned long reg;
173 u32 l;
174
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300175 if (cpu_class_is_omap1()) {
176 switch (dst_port) {
177 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
178 reg = OMAP_TC_OCPT1_PRIOR;
179 break;
180 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
181 reg = OMAP_TC_OCPT2_PRIOR;
182 break;
183 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
184 reg = OMAP_TC_EMIFF_PRIOR;
185 break;
186 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
187 reg = OMAP_TC_EMIFS_PRIOR;
188 break;
189 default:
190 BUG();
191 return;
192 }
193 l = omap_readl(reg);
194 l &= ~(0xf << 8);
195 l |= (priority & 0xf) << 8;
196 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100197 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300198
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800199 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300200 u32 ccr;
201
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800202 ccr = p->dma_read(CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300203 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300204 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300205 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300206 ccr &= ~(1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800207 p->dma_write(ccr, CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300208 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300210EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100211
212void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000213 int frame_count, int sync_mode,
214 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100215{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300216 u32 l;
217
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800218 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300219 l &= ~0x03;
220 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800221 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000223 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300224 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100225
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800226 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300227 ccr &= ~(1 << 5);
228 if (sync_mode == OMAP_DMA_SYNC_FRAME)
229 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800230 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300231
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800232 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300233 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000234 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300235 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800236 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000237 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100238
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800239 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300240 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100241
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800242 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100243
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200244 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300245 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200246 val |= (dma_trigger & ~0x1f) << 14;
247 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000248
249 if (sync_mode & OMAP_DMA_SYNC_FRAME)
250 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700251 else
252 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000253
254 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
255 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700256 else
257 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000258
Samu Onkalo72a11792010-08-02 14:21:40 +0300259 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000260 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300261 val |= (1 << 23); /* Prefetch */
262 } else if (src_or_dst_synch) {
263 val |= 1 << 24; /* source synch */
264 } else {
265 val &= ~(1 << 24); /* dest synch */
266 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800267 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000268 }
269
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800270 p->dma_write(elem_count, CEN, lch);
271 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100272}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300273EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000274
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100275void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
276{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277 BUG_ON(omap_dma_in_1510_mode());
278
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700279 if (cpu_class_is_omap1()) {
280 u16 w;
281
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800282 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700283 w &= ~0x03;
284
285 switch (mode) {
286 case OMAP_DMA_CONSTANT_FILL:
287 w |= 0x01;
288 break;
289 case OMAP_DMA_TRANSPARENT_COPY:
290 w |= 0x02;
291 break;
292 case OMAP_DMA_COLOR_DIS:
293 break;
294 default:
295 BUG();
296 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800297 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700298
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800299 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700300 w &= ~0x0f;
301 /* Default is channel type 2D */
302 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800303 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700304 w |= 1; /* Channel type G */
305 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800306 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700307 }
308
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800309 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700310 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000311
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800312 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700313 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300314
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700315 switch (mode) {
316 case OMAP_DMA_CONSTANT_FILL:
317 val |= 1 << 16;
318 break;
319 case OMAP_DMA_TRANSPARENT_COPY:
320 val |= 1 << 17;
321 break;
322 case OMAP_DMA_COLOR_DIS:
323 break;
324 default:
325 BUG();
326 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800327 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100328
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700329 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800330 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100331 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300333EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300335void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
336{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800337 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300338 u32 csdp;
339
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800340 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300341 csdp &= ~(0x3 << 16);
342 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800343 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300344 }
345}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300346EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300347
Tony Lindgren0499bde2008-07-03 12:24:36 +0300348void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
349{
350 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
351 u32 l;
352
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800353 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300354 l &= ~0x7;
355 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800356 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300357 }
358}
359EXPORT_SYMBOL(omap_set_dma_channel_mode);
360
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000361/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100362void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000363 unsigned long src_start,
364 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300366 u32 l;
367
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000368 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300369 u16 w;
370
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800371 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300372 w &= ~(0x1f << 2);
373 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800374 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300375 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300376
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800377 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300378 l &= ~(0x03 << 12);
379 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800380 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300381
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800382 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800384 p->dma_write(src_ei, CSEI, lch);
385 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300386}
387EXPORT_SYMBOL(omap_set_dma_src_params);
388
389void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000390{
391 omap_set_dma_transfer_params(lch, params->data_type,
392 params->elem_count, params->frame_count,
393 params->sync_mode, params->trigger,
394 params->src_or_dst_synch);
395 omap_set_dma_src_params(lch, params->src_port,
396 params->src_amode, params->src_start,
397 params->src_ei, params->src_fi);
398
399 omap_set_dma_dest_params(lch, params->dst_port,
400 params->dst_amode, params->dst_start,
401 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800402 if (params->read_prio || params->write_prio)
403 omap_dma_set_prio_lch(lch, params->read_prio,
404 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100405}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300406EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100407
408void omap_set_dma_src_index(int lch, int eidx, int fidx)
409{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300410 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000411 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300412
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800413 p->dma_write(eidx, CSEI, lch);
414 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100415}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300416EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100417
418void omap_set_dma_src_data_pack(int lch, int enable)
419{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300420 u32 l;
421
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800422 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300423 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000424 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300425 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800426 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100427}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300428EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429
430void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
431{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700432 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300433 u32 l;
434
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800435 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300436 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438 switch (burst_mode) {
439 case OMAP_DMA_DATA_BURST_DIS:
440 break;
441 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800442 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700443 burst = 0x1;
444 else
445 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100446 break;
447 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800448 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700449 burst = 0x2;
450 break;
451 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700452 /*
453 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100454 * w |= (0x03 << 7);
455 * fall through
456 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700457 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800458 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700459 burst = 0x3;
460 break;
461 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700462 /*
463 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700464 * fall through
465 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100466 default:
467 BUG();
468 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300469
470 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800471 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100472}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300473EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100474
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000475/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100476void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000477 unsigned long dest_start,
478 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300480 u32 l;
481
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000482 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800483 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300484 l &= ~(0x1f << 9);
485 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800486 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000487 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800489 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300490 l &= ~(0x03 << 14);
491 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800492 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100493
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800494 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100495
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800496 p->dma_write(dst_ei, CDEI, lch);
497 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300499EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500
501void omap_set_dma_dest_index(int lch, int eidx, int fidx)
502{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300503 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000504 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300505
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800506 p->dma_write(eidx, CDEI, lch);
507 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300509EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
511void omap_set_dma_dest_data_pack(int lch, int enable)
512{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300513 u32 l;
514
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800515 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300516 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000517 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300518 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800519 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300521EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522
523void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
524{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700525 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300526 u32 l;
527
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800528 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300529 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531 switch (burst_mode) {
532 case OMAP_DMA_DATA_BURST_DIS:
533 break;
534 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800535 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700536 burst = 0x1;
537 else
538 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100539 break;
540 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800541 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700542 burst = 0x2;
543 else
544 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100545 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700546 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800547 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700548 burst = 0x3;
549 break;
550 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700551 /*
552 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700553 * fall through
554 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100555 default:
556 printk(KERN_ERR "Invalid DMA burst mode\n");
557 BUG();
558 return;
559 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300560 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800561 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100562}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300563EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000565static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000567 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700569 /* Clear CSR */
570 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800571 status = p->dma_read(CSR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800572 else if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800573 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000574
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100575 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800576 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100577}
578
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000579static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100580{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800581 if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800582 p->dma_write(0, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100583}
584
585void omap_enable_dma_irq(int lch, u16 bits)
586{
587 dma_chan[lch].enabled_irqs |= bits;
588}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300589EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100590
591void omap_disable_dma_irq(int lch, u16 bits)
592{
593 dma_chan[lch].enabled_irqs &= ~bits;
594}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300595EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100596
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000597static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100598{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300599 u32 l;
600
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800601 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300602
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000603 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300604 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100605
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000606 /* Set the ENABLE_LNK bits */
607 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300608 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800609
610#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300611 if (cpu_class_is_omap2())
612 if (dma_chan[lch].next_linked_ch != -1)
613 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800614#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300615
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800616 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
618
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000619static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100620{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300621 u32 l;
622
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800623 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300624
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000625 /* Disable interrupts */
626 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800627 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000628 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300629 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630 }
631
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800632 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000633 omap_disable_channel_irq(lch);
634 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300635 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000636 }
637
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800638 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000639 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
640}
641
642static inline void omap2_enable_irq_lch(int lch)
643{
644 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800645 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000646
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800647 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000648 return;
649
Tao Huee907322009-11-10 18:55:17 -0800650 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800651 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000652 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800653 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800654 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100655}
656
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700657static inline void omap2_disable_irq_lch(int lch)
658{
659 u32 val;
660 unsigned long flags;
661
662 if (!cpu_class_is_omap2())
663 return;
664
665 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800666 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700667 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800668 p->dma_write(val, IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700669 spin_unlock_irqrestore(&dma_chan_lock, flags);
670}
671
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300673 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100674 void *data, int *dma_ch_out)
675{
676 int ch, free_ch = -1;
677 unsigned long flags;
678 struct omap_dma_lch *chan;
679
680 spin_lock_irqsave(&dma_chan_lock, flags);
681 for (ch = 0; ch < dma_chan_count; ch++) {
682 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
683 free_ch = ch;
684 if (dev_id == 0)
685 break;
686 }
687 }
688 if (free_ch == -1) {
689 spin_unlock_irqrestore(&dma_chan_lock, flags);
690 return -EBUSY;
691 }
692 chan = dma_chan + free_ch;
693 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000694
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800695 if (p->clear_lch_regs)
696 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000697
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800698 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000699 omap_clear_dma(free_ch);
700
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701 spin_unlock_irqrestore(&dma_chan_lock, flags);
702
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703 chan->dev_name = dev_name;
704 chan->callback = callback;
705 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800706 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300707
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800708#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300709 if (cpu_class_is_omap2()) {
710 chan->chain_id = -1;
711 chan->next_linked_ch = -1;
712 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800713#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300714
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700715 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000716
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700717 if (cpu_class_is_omap1())
718 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800719 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700720 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
721 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722
723 if (cpu_is_omap16xx()) {
724 /* If the sync device is set, configure it dynamically. */
725 if (dev_id != 0) {
726 set_gdma_dev(free_ch + 1, dev_id);
727 dev_id = free_ch + 1;
728 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300729 /*
730 * Disable the 1510 compatibility mode and set the sync device
731 * id.
732 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800733 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700734 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800735 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000737
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800738 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000739 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000740 omap_enable_channel_irq(free_ch);
741 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800742 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, free_ch);
743 p->dma_write(1 << free_ch, IRQSTATUS_L0, 0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000744 }
745
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100746 *dma_ch_out = free_ch;
747
748 return 0;
749}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300750EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100751
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000752void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753{
754 unsigned long flags;
755
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000756 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300757 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000758 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759 return;
760 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300761
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000762 if (cpu_class_is_omap1()) {
763 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800764 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000765 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800766 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000767 }
768
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800769 if (cpu_class_is_omap2()) {
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700770 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000771
772 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800773 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
774 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000775
776 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800777 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000778
779 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800780 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000781 omap_clear_dma(lch);
782 }
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700783
784 spin_lock_irqsave(&dma_chan_lock, flags);
785 dma_chan[lch].dev_id = -1;
786 dma_chan[lch].next_lch = -1;
787 dma_chan[lch].callback = NULL;
788 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300790EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100791
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800792/**
793 * @brief omap_dma_set_global_params : Set global priority settings for dma
794 *
795 * @param arb_rate
796 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700797 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
798 * DMA_THREAD_RESERVE_ONET
799 * DMA_THREAD_RESERVE_TWOT
800 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800801 */
802void
803omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
804{
805 u32 reg;
806
807 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800808 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800809 return;
810 }
811
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700812 if (max_fifo_depth == 0)
813 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800814 if (arb_rate == 0)
815 arb_rate = 1;
816
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700817 reg = 0xff & max_fifo_depth;
818 reg |= (0x3 & tparams) << 12;
819 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800820
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800821 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800822}
823EXPORT_SYMBOL(omap_dma_set_global_params);
824
825/**
826 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
827 *
828 * @param lch
829 * @param read_prio - Read priority
830 * @param write_prio - Write priority
831 * Both of the above can be set with one of the following values :
832 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
833 */
834int
835omap_dma_set_prio_lch(int lch, unsigned char read_prio,
836 unsigned char write_prio)
837{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300838 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800839
Tony Lindgren4d963722008-07-03 12:24:31 +0300840 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800841 printk(KERN_ERR "Invalid channel id\n");
842 return -EINVAL;
843 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800844 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300845 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkar44169072009-05-28 14:16:04 -0700846 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300847 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800848 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300849 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800850
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800851 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300852
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800853 return 0;
854}
855EXPORT_SYMBOL(omap_dma_set_prio_lch);
856
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000857/*
858 * Clears any DMA state so the DMA engine is ready to restart with new buffers
859 * through omap_start_dma(). Any buffers in flight are discarded.
860 */
861void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100862{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000863 unsigned long flags;
864
865 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800866 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000867 local_irq_restore(flags);
868}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300869EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000870
871void omap_start_dma(int lch)
872{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300873 u32 l;
874
manjugk manjugk519e6162010-03-04 07:11:56 +0000875 /*
876 * The CPC/CDAC register needs to be initialized to zero
877 * before starting dma transfer.
878 */
879 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800880 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000881 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800882 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000883
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000884 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
885 int next_lch, cur_lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800886 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000887
888 dma_chan_link_map[lch] = 1;
889 /* Set the link register of the first channel */
890 enable_lnk(lch);
891
892 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
893 cur_lch = dma_chan[lch].next_lch;
894 do {
895 next_lch = dma_chan[cur_lch].next_lch;
896
897 /* The loop case: we've been here already */
898 if (dma_chan_link_map[cur_lch])
899 break;
900 /* Mark the current channel */
901 dma_chan_link_map[cur_lch] = 1;
902
903 enable_lnk(cur_lch);
904 omap_enable_channel_irq(cur_lch);
905
906 cur_lch = next_lch;
907 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800908 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800909 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000910
911 omap_enable_channel_irq(lch);
912
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800913 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300914
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800915 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
916 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300917 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800918
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800919 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000920
921 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
922}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300923EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000924
925void omap_stop_dma(int lch)
926{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300927 u32 l;
928
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700929 /* Disable all interrupts on the channel */
930 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800931 p->dma_write(0, CICR, lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700932
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800933 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800934 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
935 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700936 int i = 0;
937 u32 sys_cf;
938
939 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800940 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700941 sys_cf = l;
942 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
943 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800944 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700945
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800946 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700947 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800948 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700949
950 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800951 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700952 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
953 OMAP_DMA_CCR_WR_ACTIVE))) {
954 udelay(5);
955 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800956 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700957 }
958 if (i >= 100)
959 printk(KERN_ERR "DMA drain did not complete on "
960 "lch %d\n", lch);
961 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800962 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700963 } else {
964 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800965 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700966 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700967
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000968 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
969 int next_lch, cur_lch = lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800970 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000971
972 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
973 do {
974 /* The loop case: we've been here already */
975 if (dma_chan_link_map[cur_lch])
976 break;
977 /* Mark the current channel */
978 dma_chan_link_map[cur_lch] = 1;
979
980 disable_lnk(cur_lch);
981
982 next_lch = dma_chan[cur_lch].next_lch;
983 cur_lch = next_lch;
984 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000985 }
986
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000987 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
988}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300989EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000990
991/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300992 * Allows changing the DMA callback function or data. This may be needed if
993 * the driver shares a single DMA channel for multiple dma triggers.
994 */
995int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300996 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300997 void *data)
998{
999 unsigned long flags;
1000
1001 if (lch < 0)
1002 return -ENODEV;
1003
1004 spin_lock_irqsave(&dma_chan_lock, flags);
1005 if (dma_chan[lch].dev_id == -1) {
1006 printk(KERN_ERR "DMA callback for not set for free channel\n");
1007 spin_unlock_irqrestore(&dma_chan_lock, flags);
1008 return -EINVAL;
1009 }
1010 dma_chan[lch].callback = callback;
1011 dma_chan[lch].data = data;
1012 spin_unlock_irqrestore(&dma_chan_lock, flags);
1013
1014 return 0;
1015}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001016EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001017
1018/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001019 * Returns current physical source address for the given DMA channel.
1020 * If the channel is running the caller must disable interrupts prior calling
1021 * this function and process the returned value before re-enabling interrupt to
1022 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001023 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001024 * in incorrect return value.
1025 */
1026dma_addr_t omap_get_dma_src_pos(int lch)
1027{
Tony Lindgren0695de32007-05-07 18:24:14 -07001028 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001029
Tony Lindgren0499bde2008-07-03 12:24:36 +03001030 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001031 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001032 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001033 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001034
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001035 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001036 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001037
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001038 if (!cpu_is_omap15xx()) {
1039 /*
1040 * CDAC == 0 indicates that the DMA transfer on the channel has
1041 * not been started (no data has been transferred so far).
1042 * Return the programmed source start address in this case.
1043 */
1044 if (likely(p->dma_read(CDAC, lch)))
1045 offset = p->dma_read(CSAC, lch);
1046 else
1047 offset = p->dma_read(CSSA, lch);
1048 }
1049
Tony Lindgren0499bde2008-07-03 12:24:36 +03001050 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001051 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001052
1053 return offset;
1054}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001055EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001056
1057/*
1058 * Returns current physical destination address for the given DMA channel.
1059 * If the channel is running the caller must disable interrupts prior calling
1060 * this function and process the returned value before re-enabling interrupt to
1061 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001062 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001063 * in incorrect return value.
1064 */
1065dma_addr_t omap_get_dma_dst_pos(int lch)
1066{
Tony Lindgren0695de32007-05-07 18:24:14 -07001067 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001068
Tony Lindgren0499bde2008-07-03 12:24:36 +03001069 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001070 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001071 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001072 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001073
Tony Lindgren0499bde2008-07-03 12:24:36 +03001074 /*
1075 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1076 * read before the DMA controller finished disabling the channel.
1077 */
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001078 if (!cpu_is_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001079 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001080 /*
1081 * CDAC == 0 indicates that the DMA transfer on the channel has
1082 * not been started (no data has been transferred so far).
1083 * Return the programmed destination start address in this case.
1084 */
1085 if (unlikely(!offset))
1086 offset = p->dma_read(CDSA, lch);
1087 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001088
1089 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001090 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001091
1092 return offset;
1093}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001094EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001095
Tony Lindgren0499bde2008-07-03 12:24:36 +03001096int omap_get_dma_active_status(int lch)
1097{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001098 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001099}
1100EXPORT_SYMBOL(omap_get_dma_active_status);
1101
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001102int omap_dma_running(void)
1103{
1104 int lch;
1105
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001106 if (cpu_class_is_omap1())
1107 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001108 return 1;
1109
1110 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001111 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001112 return 1;
1113
1114 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001115}
1116
1117/*
1118 * lch_queue DMA will start right after lch_head one is finished.
1119 * For this DMA link to start, you still need to start (see omap_start_dma)
1120 * the first one. That will fire up the entire queue.
1121 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001122void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001123{
1124 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001125 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001126 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001127 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001128 return;
1129 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001130 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1131 BUG();
1132 return;
1133 }
1134
1135 if ((dma_chan[lch_head].dev_id == -1) ||
1136 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001137 printk(KERN_ERR "omap_dma: trying to link "
1138 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001139 dump_stack();
1140 }
1141
1142 dma_chan[lch_head].next_lch = lch_queue;
1143}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001144EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001145
1146/*
1147 * Once the DMA queue is stopped, we can destroy it.
1148 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001149void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001150{
1151 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001152 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001153 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001154 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001155 return;
1156 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1158 BUG();
1159 return;
1160 }
1161
1162 if (dma_chan[lch_head].next_lch != lch_queue ||
1163 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001164 printk(KERN_ERR "omap_dma: trying to unlink "
1165 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001166 dump_stack();
1167 }
1168
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001169 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001170 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001171 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1172 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001173 dump_stack();
1174 }
1175
1176 dma_chan[lch_head].next_lch = -1;
1177}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001178EXPORT_SYMBOL(omap_dma_unlink_lch);
1179
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001180#ifndef CONFIG_ARCH_OMAP1
1181/* Create chain of DMA channesls */
1182static void create_dma_lch_chain(int lch_head, int lch_queue)
1183{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001184 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001185
1186 /* Check if this is the first link in chain */
1187 if (dma_chan[lch_head].next_linked_ch == -1) {
1188 dma_chan[lch_head].next_linked_ch = lch_queue;
1189 dma_chan[lch_head].prev_linked_ch = lch_queue;
1190 dma_chan[lch_queue].next_linked_ch = lch_head;
1191 dma_chan[lch_queue].prev_linked_ch = lch_head;
1192 }
1193
1194 /* a link exists, link the new channel in circular chain */
1195 else {
1196 dma_chan[lch_queue].next_linked_ch =
1197 dma_chan[lch_head].next_linked_ch;
1198 dma_chan[lch_queue].prev_linked_ch = lch_head;
1199 dma_chan[lch_head].next_linked_ch = lch_queue;
1200 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1201 lch_queue;
1202 }
1203
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001204 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001205 l &= ~(0x1f);
1206 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001207 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001208
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001209 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001210 l &= ~(0x1f);
1211 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001212 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001213}
1214
1215/**
1216 * @brief omap_request_dma_chain : Request a chain of DMA channels
1217 *
1218 * @param dev_id - Device id using the dma channel
1219 * @param dev_name - Device name
1220 * @param callback - Call back function
1221 * @chain_id -
1222 * @no_of_chans - Number of channels requested
1223 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1224 * OMAP_DMA_DYNAMIC_CHAIN
1225 * @params - Channel parameters
1226 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001227 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001228 * Failure: -EINVAL/-ENOMEM
1229 */
1230int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001231 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001232 void *data),
1233 int *chain_id, int no_of_chans, int chain_mode,
1234 struct omap_dma_channel_params params)
1235{
1236 int *channels;
1237 int i, err;
1238
1239 /* Is the chain mode valid ? */
1240 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1241 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1242 printk(KERN_ERR "Invalid chain mode requested\n");
1243 return -EINVAL;
1244 }
1245
1246 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001247 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001248 printk(KERN_ERR "Invalid Number of channels requested\n");
1249 return -EINVAL;
1250 }
1251
manjugk manjugkea221a62010-05-14 12:05:25 -07001252 /*
1253 * Allocate a queue to maintain the status of the channels
1254 * in the chain
1255 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001256 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1257 if (channels == NULL) {
1258 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1259 return -ENOMEM;
1260 }
1261
1262 /* request and reserve DMA channels for the chain */
1263 for (i = 0; i < no_of_chans; i++) {
1264 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001265 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001266 if (err < 0) {
1267 int j;
1268 for (j = 0; j < i; j++)
1269 omap_free_dma(channels[j]);
1270 kfree(channels);
1271 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1272 return err;
1273 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001274 dma_chan[channels[i]].prev_linked_ch = -1;
1275 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1276
1277 /*
1278 * Allowing client drivers to set common parameters now,
1279 * so that later only relevant (src_start, dest_start
1280 * and element count) can be set
1281 */
1282 omap_set_dma_params(channels[i], &params);
1283 }
1284
1285 *chain_id = channels[0];
1286 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1287 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1288 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1289 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1290
1291 for (i = 0; i < no_of_chans; i++)
1292 dma_chan[channels[i]].chain_id = *chain_id;
1293
1294 /* Reset the Queue pointers */
1295 OMAP_DMA_CHAIN_QINIT(*chain_id);
1296
1297 /* Set up the chain */
1298 if (no_of_chans == 1)
1299 create_dma_lch_chain(channels[0], channels[0]);
1300 else {
1301 for (i = 0; i < (no_of_chans - 1); i++)
1302 create_dma_lch_chain(channels[i], channels[i + 1]);
1303 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001304
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001305 return 0;
1306}
1307EXPORT_SYMBOL(omap_request_dma_chain);
1308
1309/**
1310 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1311 * params after setting it. Dont do this while dma is running!!
1312 *
1313 * @param chain_id - Chained logical channel id.
1314 * @param params
1315 *
1316 * @return - Success : 0
1317 * Failure : -EINVAL
1318 */
1319int omap_modify_dma_chain_params(int chain_id,
1320 struct omap_dma_channel_params params)
1321{
1322 int *channels;
1323 u32 i;
1324
1325 /* Check for input params */
1326 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001327 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001328 printk(KERN_ERR "Invalid chain id\n");
1329 return -EINVAL;
1330 }
1331
1332 /* Check if the chain exists */
1333 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1334 printk(KERN_ERR "Chain doesn't exists\n");
1335 return -EINVAL;
1336 }
1337 channels = dma_linked_lch[chain_id].linked_dmach_q;
1338
1339 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1340 /*
1341 * Allowing client drivers to set common parameters now,
1342 * so that later only relevant (src_start, dest_start
1343 * and element count) can be set
1344 */
1345 omap_set_dma_params(channels[i], &params);
1346 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001347
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001348 return 0;
1349}
1350EXPORT_SYMBOL(omap_modify_dma_chain_params);
1351
1352/**
1353 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1354 *
1355 * @param chain_id
1356 *
1357 * @return - Success : 0
1358 * Failure : -EINVAL
1359 */
1360int omap_free_dma_chain(int chain_id)
1361{
1362 int *channels;
1363 u32 i;
1364
1365 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001366 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001367 printk(KERN_ERR "Invalid chain id\n");
1368 return -EINVAL;
1369 }
1370
1371 /* Check if the chain exists */
1372 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1373 printk(KERN_ERR "Chain doesn't exists\n");
1374 return -EINVAL;
1375 }
1376
1377 channels = dma_linked_lch[chain_id].linked_dmach_q;
1378 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1379 dma_chan[channels[i]].next_linked_ch = -1;
1380 dma_chan[channels[i]].prev_linked_ch = -1;
1381 dma_chan[channels[i]].chain_id = -1;
1382 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1383 omap_free_dma(channels[i]);
1384 }
1385
1386 kfree(channels);
1387
1388 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1389 dma_linked_lch[chain_id].chain_mode = -1;
1390 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001391
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001392 return (0);
1393}
1394EXPORT_SYMBOL(omap_free_dma_chain);
1395
1396/**
1397 * @brief omap_dma_chain_status - Check if the chain is in
1398 * active / inactive state.
1399 * @param chain_id
1400 *
1401 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1402 * Failure : -EINVAL
1403 */
1404int omap_dma_chain_status(int chain_id)
1405{
1406 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001407 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001408 printk(KERN_ERR "Invalid chain id\n");
1409 return -EINVAL;
1410 }
1411
1412 /* Check if the chain exists */
1413 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1414 printk(KERN_ERR "Chain doesn't exists\n");
1415 return -EINVAL;
1416 }
1417 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1418 dma_linked_lch[chain_id].q_count);
1419
1420 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1421 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001422
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001423 return OMAP_DMA_CHAIN_ACTIVE;
1424}
1425EXPORT_SYMBOL(omap_dma_chain_status);
1426
1427/**
1428 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1429 * set the params and start the transfer.
1430 *
1431 * @param chain_id
1432 * @param src_start - buffer start address
1433 * @param dest_start - Dest address
1434 * @param elem_count
1435 * @param frame_count
1436 * @param callbk_data - channel callback parameter data.
1437 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301438 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001439 * Failure: -EINVAL/-EBUSY
1440 */
1441int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1442 int elem_count, int frame_count, void *callbk_data)
1443{
1444 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001445 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001446 int start_dma = 0;
1447
Tony Lindgren97b7f712008-07-03 12:24:37 +03001448 /*
1449 * if buffer size is less than 1 then there is
1450 * no use of starting the chain
1451 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001452 if (elem_count < 1) {
1453 printk(KERN_ERR "Invalid buffer size\n");
1454 return -EINVAL;
1455 }
1456
1457 /* Check for input params */
1458 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001459 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001460 printk(KERN_ERR "Invalid chain id\n");
1461 return -EINVAL;
1462 }
1463
1464 /* Check if the chain exists */
1465 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1466 printk(KERN_ERR "Chain doesn't exist\n");
1467 return -EINVAL;
1468 }
1469
1470 /* Check if all the channels in chain are in use */
1471 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1472 return -EBUSY;
1473
1474 /* Frame count may be negative in case of indexed transfers */
1475 channels = dma_linked_lch[chain_id].linked_dmach_q;
1476
1477 /* Get a free channel */
1478 lch = channels[dma_linked_lch[chain_id].q_tail];
1479
1480 /* Store the callback data */
1481 dma_chan[lch].data = callbk_data;
1482
1483 /* Increment the q_tail */
1484 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1485
1486 /* Set the params to the free channel */
1487 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001488 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001489 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001490 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001491
1492 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001493 p->dma_write(elem_count, CEN, lch);
1494 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001495
Tony Lindgren97b7f712008-07-03 12:24:37 +03001496 /*
1497 * If the chain is dynamically linked,
1498 * then we may have to start the chain if its not active
1499 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001500 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1501
Tony Lindgren97b7f712008-07-03 12:24:37 +03001502 /*
1503 * In Dynamic chain, if the chain is not started,
1504 * queue the channel
1505 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001506 if (dma_linked_lch[chain_id].chain_state ==
1507 DMA_CHAIN_NOTSTARTED) {
1508 /* Enable the link in previous channel */
1509 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1510 DMA_CH_QUEUED)
1511 enable_lnk(dma_chan[lch].prev_linked_ch);
1512 dma_chan[lch].state = DMA_CH_QUEUED;
1513 }
1514
Tony Lindgren97b7f712008-07-03 12:24:37 +03001515 /*
1516 * Chain is already started, make sure its active,
1517 * if not then start the chain
1518 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001519 else {
1520 start_dma = 1;
1521
1522 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1523 DMA_CH_STARTED) {
1524 enable_lnk(dma_chan[lch].prev_linked_ch);
1525 dma_chan[lch].state = DMA_CH_QUEUED;
1526 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001527 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001528 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001529 disable_lnk(dma_chan[lch].
1530 prev_linked_ch);
1531 pr_debug("\n prev ch is stopped\n");
1532 start_dma = 1;
1533 }
1534 }
1535
1536 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1537 == DMA_CH_QUEUED) {
1538 enable_lnk(dma_chan[lch].prev_linked_ch);
1539 dma_chan[lch].state = DMA_CH_QUEUED;
1540 start_dma = 0;
1541 }
1542 omap_enable_channel_irq(lch);
1543
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001544 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001545
Tony Lindgren0499bde2008-07-03 12:24:36 +03001546 if ((0 == (l & (1 << 24))))
1547 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001548 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001549 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001550 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001551 if (0 == (l & (1 << 7))) {
1552 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001553 dma_chan[lch].state = DMA_CH_STARTED;
1554 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001555 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001556 } else
1557 start_dma = 0;
1558 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001559 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001560 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001561 }
1562 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1563 }
1564 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001565
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301566 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001567}
1568EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1569
1570/**
1571 * @brief omap_start_dma_chain_transfers - Start the chain
1572 *
1573 * @param chain_id
1574 *
1575 * @return - Success : 0
1576 * Failure : -EINVAL/-EBUSY
1577 */
1578int omap_start_dma_chain_transfers(int chain_id)
1579{
1580 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001581 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001582
Tony Lindgren4d963722008-07-03 12:24:31 +03001583 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001584 printk(KERN_ERR "Invalid chain id\n");
1585 return -EINVAL;
1586 }
1587
1588 channels = dma_linked_lch[chain_id].linked_dmach_q;
1589
1590 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1591 printk(KERN_ERR "Chain is already started\n");
1592 return -EBUSY;
1593 }
1594
1595 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1596 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1597 i++) {
1598 enable_lnk(channels[i]);
1599 omap_enable_channel_irq(channels[i]);
1600 }
1601 } else {
1602 omap_enable_channel_irq(channels[0]);
1603 }
1604
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001605 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001606 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001607 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1608 dma_chan[channels[0]].state = DMA_CH_STARTED;
1609
Tony Lindgren0499bde2008-07-03 12:24:36 +03001610 if ((0 == (l & (1 << 24))))
1611 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001612 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001613 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001614 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001615
1616 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001617
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001618 return 0;
1619}
1620EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1621
1622/**
1623 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1624 *
1625 * @param chain_id
1626 *
1627 * @return - Success : 0
1628 * Failure : EINVAL
1629 */
1630int omap_stop_dma_chain_transfers(int chain_id)
1631{
1632 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001633 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001634 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001635
1636 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001637 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001638 printk(KERN_ERR "Invalid chain id\n");
1639 return -EINVAL;
1640 }
1641
1642 /* Check if the chain exists */
1643 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1644 printk(KERN_ERR "Chain doesn't exists\n");
1645 return -EINVAL;
1646 }
1647 channels = dma_linked_lch[chain_id].linked_dmach_q;
1648
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001649 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001650 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001651 l = sys_cf;
1652 /* Middle mode reg set no Standby */
1653 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001654 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001655 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001656
1657 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1658
1659 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001660 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001661 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001662 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001663
1664 /* Disable the link in all the channels */
1665 disable_lnk(channels[i]);
1666 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1667
1668 }
1669 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1670
1671 /* Reset the Queue pointers */
1672 OMAP_DMA_CHAIN_QINIT(chain_id);
1673
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001674 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001675 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001676
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001677 return 0;
1678}
1679EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1680
1681/* Get the index of the ongoing DMA in chain */
1682/**
1683 * @brief omap_get_dma_chain_index - Get the element and frame index
1684 * of the ongoing DMA in chain
1685 *
1686 * @param chain_id
1687 * @param ei - Element index
1688 * @param fi - Frame index
1689 *
1690 * @return - Success : 0
1691 * Failure : -EINVAL
1692 */
1693int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1694{
1695 int lch;
1696 int *channels;
1697
1698 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001699 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001700 printk(KERN_ERR "Invalid chain id\n");
1701 return -EINVAL;
1702 }
1703
1704 /* Check if the chain exists */
1705 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1706 printk(KERN_ERR "Chain doesn't exists\n");
1707 return -EINVAL;
1708 }
1709 if ((!ei) || (!fi))
1710 return -EINVAL;
1711
1712 channels = dma_linked_lch[chain_id].linked_dmach_q;
1713
1714 /* Get the current channel */
1715 lch = channels[dma_linked_lch[chain_id].q_head];
1716
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001717 *ei = p->dma_read(CCEN, lch);
1718 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001719
1720 return 0;
1721}
1722EXPORT_SYMBOL(omap_get_dma_chain_index);
1723
1724/**
1725 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1726 * ongoing DMA in chain
1727 *
1728 * @param chain_id
1729 *
1730 * @return - Success : Destination position
1731 * Failure : -EINVAL
1732 */
1733int omap_get_dma_chain_dst_pos(int chain_id)
1734{
1735 int lch;
1736 int *channels;
1737
1738 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001739 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001740 printk(KERN_ERR "Invalid chain id\n");
1741 return -EINVAL;
1742 }
1743
1744 /* Check if the chain exists */
1745 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1746 printk(KERN_ERR "Chain doesn't exists\n");
1747 return -EINVAL;
1748 }
1749
1750 channels = dma_linked_lch[chain_id].linked_dmach_q;
1751
1752 /* Get the current channel */
1753 lch = channels[dma_linked_lch[chain_id].q_head];
1754
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001755 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001756}
1757EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1758
1759/**
1760 * @brief omap_get_dma_chain_src_pos - Get the source position
1761 * of the ongoing DMA in chain
1762 * @param chain_id
1763 *
1764 * @return - Success : Destination position
1765 * Failure : -EINVAL
1766 */
1767int omap_get_dma_chain_src_pos(int chain_id)
1768{
1769 int lch;
1770 int *channels;
1771
1772 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001773 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001774 printk(KERN_ERR "Invalid chain id\n");
1775 return -EINVAL;
1776 }
1777
1778 /* Check if the chain exists */
1779 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1780 printk(KERN_ERR "Chain doesn't exists\n");
1781 return -EINVAL;
1782 }
1783
1784 channels = dma_linked_lch[chain_id].linked_dmach_q;
1785
1786 /* Get the current channel */
1787 lch = channels[dma_linked_lch[chain_id].q_head];
1788
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001789 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001790}
1791EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001792#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001793
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001794/*----------------------------------------------------------------------------*/
1795
1796#ifdef CONFIG_ARCH_OMAP1
1797
1798static int omap1_dma_handle_ch(int ch)
1799{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001800 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001801
1802 if (enable_1510_mode && ch >= 6) {
1803 csr = dma_chan[ch].saved_csr;
1804 dma_chan[ch].saved_csr = 0;
1805 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001806 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001807 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1808 dma_chan[ch + 6].saved_csr = csr >> 7;
1809 csr &= 0x7f;
1810 }
1811 if ((csr & 0x3f) == 0)
1812 return 0;
1813 if (unlikely(dma_chan[ch].dev_id == -1)) {
1814 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1815 "%d (CSR %04x)\n", ch, csr);
1816 return 0;
1817 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001818 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001819 printk(KERN_WARNING "DMA timeout with device %d\n",
1820 dma_chan[ch].dev_id);
1821 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1822 printk(KERN_WARNING "DMA synchronization event drop occurred "
1823 "with device %d\n", dma_chan[ch].dev_id);
1824 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1825 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1826 if (likely(dma_chan[ch].callback != NULL))
1827 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001828
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001829 return 1;
1830}
1831
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001832static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001833{
1834 int ch = ((int) dev_id) - 1;
1835 int handled = 0;
1836
1837 for (;;) {
1838 int handled_now = 0;
1839
1840 handled_now += omap1_dma_handle_ch(ch);
1841 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1842 handled_now += omap1_dma_handle_ch(ch + 6);
1843 if (!handled_now)
1844 break;
1845 handled += handled_now;
1846 }
1847
1848 return handled ? IRQ_HANDLED : IRQ_NONE;
1849}
1850
1851#else
1852#define omap1_dma_irq_handler NULL
1853#endif
1854
Tony Lindgren140455f2010-02-12 12:26:48 -08001855#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001856
1857static int omap2_dma_handle_ch(int ch)
1858{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001859 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001860
Juha Yrjola31513692006-12-06 17:13:47 -08001861 if (!status) {
1862 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001863 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1864 ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001865 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001866 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001867 }
1868 if (unlikely(dma_chan[ch].dev_id == -1)) {
1869 if (printk_ratelimit())
1870 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1871 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001872 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001873 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001874 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1875 printk(KERN_INFO
1876 "DMA synchronization event drop occurred with device "
1877 "%d\n", dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001878 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001879 printk(KERN_INFO "DMA transaction error with device %d\n",
1880 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001881 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001882 u32 ccr;
1883
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001884 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001885 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001886 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001887 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1888 }
1889 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001890 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1891 printk(KERN_INFO "DMA secure error with device %d\n",
1892 dma_chan[ch].dev_id);
1893 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1894 printk(KERN_INFO "DMA misaligned error with device %d\n",
1895 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001896
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001897 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001898 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001899 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001900 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001901
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001902 /* If the ch is not chained then chain_id will be -1 */
1903 if (dma_chan[ch].chain_id != -1) {
1904 int chain_id = dma_chan[ch].chain_id;
1905 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001906 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001907 dma_chan[dma_chan[ch].next_linked_ch].state =
1908 DMA_CH_STARTED;
1909 if (dma_linked_lch[chain_id].chain_mode ==
1910 OMAP_DMA_DYNAMIC_CHAIN)
1911 disable_lnk(ch);
1912
1913 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1914 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1915
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001916 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001917 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001918 }
1919
Jarkko Nikula538528d2008-02-13 11:47:29 +02001920 if (likely(dma_chan[ch].callback != NULL))
1921 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001922
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001923 return 0;
1924}
1925
1926/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001927static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001928{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001929 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001930 int i;
1931
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001932 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001933 if (val == 0) {
1934 if (printk_ratelimit())
1935 printk(KERN_WARNING "Spurious DMA IRQ\n");
1936 return IRQ_HANDLED;
1937 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001938 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001939 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001940 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001941 if (val & 1)
1942 omap2_dma_handle_ch(i);
1943 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001944 }
1945
1946 return IRQ_HANDLED;
1947}
1948
1949static struct irqaction omap24xx_dma_irq = {
1950 .name = "DMA",
1951 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001952 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001953};
1954
1955#else
1956static struct irqaction omap24xx_dma_irq;
1957#endif
1958
1959/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001960
Tero Kristof2d11852008-08-28 13:13:31 +00001961void omap_dma_global_context_save(void)
1962{
1963 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001964 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001965 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001966 p->dma_read(OCP_SYSCONFIG, 0);
1967 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001968}
1969
1970void omap_dma_global_context_restore(void)
1971{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001972 int ch;
1973
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001974 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1975 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001976 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001977 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001978 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001979
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001980 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001981 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001982
1983 for (ch = 0; ch < dma_chan_count; ch++)
1984 if (dma_chan[ch].dev_id != -1)
1985 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00001986}
1987
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001988static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001989{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001990 int ch, ret = 0;
1991 int dma_irq;
1992 char irq_name[4];
1993 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001994
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001995 p = pdev->dev.platform_data;
1996 if (!p) {
1997 dev_err(&pdev->dev, "%s: System DMA initialized without"
1998 "platform data\n", __func__);
1999 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002000 }
2001
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002002 d = p->dma_attr;
2003 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002004
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002005 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002006 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002007 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002008
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002009 dma_lch_count = d->lch_count;
2010 dma_chan_count = dma_lch_count;
2011 dma_chan = d->chan;
2012 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002013
2014 if (cpu_class_is_omap2()) {
2015 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2016 dma_lch_count, GFP_KERNEL);
2017 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002018 ret = -ENOMEM;
2019 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002020 }
2021 }
2022
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002023 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002024 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002025 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002026 if (cpu_class_is_omap2())
2027 omap2_disable_irq_lch(ch);
2028
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002029 dma_chan[ch].dev_id = -1;
2030 dma_chan[ch].next_lch = -1;
2031
2032 if (ch >= 6 && enable_1510_mode)
2033 continue;
2034
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002035 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002036 /*
2037 * request_irq() doesn't like dev_id (ie. ch) being
2038 * zero, so we have to kludge around this.
2039 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002040 sprintf(&irq_name[0], "%d", ch);
2041 dma_irq = platform_get_irq_byname(pdev, irq_name);
2042
2043 if (dma_irq < 0) {
2044 ret = dma_irq;
2045 goto exit_dma_irq_fail;
2046 }
2047
2048 /* INT_DMA_LCD is handled in lcd_dma.c */
2049 if (dma_irq == INT_DMA_LCD)
2050 continue;
2051
2052 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002053 omap1_dma_irq_handler, 0, "DMA",
2054 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002055 if (ret != 0)
2056 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002057 }
2058 }
2059
Santosh Shilimkar44169072009-05-28 14:16:04 -07002060 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002061 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2062 DMA_DEFAULT_FIFO_DEPTH, 0);
2063
Santosh Shilimkar44169072009-05-28 14:16:04 -07002064 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002065 strcpy(irq_name, "0");
2066 dma_irq = platform_get_irq_byname(pdev, irq_name);
2067 if (dma_irq < 0) {
2068 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2069 goto exit_dma_lch_fail;
2070 }
2071 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2072 if (ret) {
2073 dev_err(&pdev->dev, "set_up failed for IRQ %d"
2074 "for DMA (error %d)\n", dma_irq, ret);
2075 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002076 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002077 }
2078
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002079 /* reserve dma channels 0 and 1 in high security devices */
2080 if (cpu_is_omap34xx() &&
2081 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
2082 printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
2083 "HS ROM code\n");
2084 dma_chan[0].dev_id = 0;
2085 dma_chan[1].dev_id = 1;
2086 }
2087 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002088 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002089
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002090exit_dma_irq_fail:
2091 dev_err(&pdev->dev, "unable to request IRQ %d"
2092 "for DMA (error %d)\n", dma_irq, ret);
2093 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2094 dma_irq = platform_get_irq(pdev, irq_rel);
2095 free_irq(dma_irq, (void *)(irq_rel + 1));
2096 }
2097
2098exit_dma_lch_fail:
2099 kfree(p);
2100 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002101 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002102 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002103}
2104
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002105static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2106{
2107 int dma_irq;
2108
2109 if (cpu_class_is_omap2()) {
2110 char irq_name[4];
2111 strcpy(irq_name, "0");
2112 dma_irq = platform_get_irq_byname(pdev, irq_name);
2113 remove_irq(dma_irq, &omap24xx_dma_irq);
2114 } else {
2115 int irq_rel = 0;
2116 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2117 dma_irq = platform_get_irq(pdev, irq_rel);
2118 free_irq(dma_irq, (void *)(irq_rel + 1));
2119 }
2120 }
2121 kfree(p);
2122 kfree(d);
2123 kfree(dma_chan);
2124 return 0;
2125}
2126
2127static struct platform_driver omap_system_dma_driver = {
2128 .probe = omap_system_dma_probe,
Tony Lindgren3e2e6132012-02-23 14:58:08 -08002129 .remove = __devexit_p(omap_system_dma_remove),
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002130 .driver = {
2131 .name = "omap_dma_system"
2132 },
2133};
2134
2135static int __init omap_system_dma_init(void)
2136{
2137 return platform_driver_register(&omap_system_dma_driver);
2138}
2139arch_initcall(omap_system_dma_init);
2140
2141static void __exit omap_system_dma_exit(void)
2142{
2143 platform_driver_unregister(&omap_system_dma_driver);
2144}
2145
2146MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2147MODULE_LICENSE("GPL");
2148MODULE_ALIAS("platform:" DRIVER_NAME);
2149MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002150
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002151/*
2152 * Reserve the omap SDMA channels using cmdline bootarg
2153 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2154 */
2155static int __init omap_dma_cmdline_reserve_ch(char *str)
2156{
2157 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2158 omap_dma_reserve_channels = 0;
2159 return 1;
2160}
2161
2162__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2163
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002164