blob: 1b5216f1e78ddc427ce0fd3262324d2637d43203 [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 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/sched.h>
27#include <linux/spinlock.h>
28#include <linux/errno.h>
29#include <linux/interrupt.h>
Thomas Gleixner418ca1f02006-07-01 22:32:41 +010030#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030031#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010032
33#include <asm/system.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036
Tony Lindgrence491cf2009-10-20 09:40:47 -070037#include <plat/tc.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010038
Anand Gadiyarf8151e52007-12-01 12:14:11 -080039#undef DEBUG
40
41#ifndef CONFIG_ARCH_OMAP1
42enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
43 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
44};
45
46enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000047#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010048
Tony Lindgren97b7f712008-07-03 12:24:37 +030049#define OMAP_DMA_ACTIVE 0x01
50#define OMAP_DMA_CCR_EN (1 << 7)
Tony Lindgren7ff879d2006-06-26 16:16:15 -070051#define OMAP2_DMA_CSR_CLEAR_MASK 0xffe
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010052
Tony Lindgren97b7f712008-07-03 12:24:37 +030053#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010054
Tony Lindgren97b7f712008-07-03 12:24:37 +030055static int enable_1510_mode;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056
Tero Kristof2d11852008-08-28 13:13:31 +000057static struct omap_dma_global_context_registers {
58 u32 dma_irqenable_l0;
59 u32 dma_ocp_sysconfig;
60 u32 dma_gcr;
61} omap_dma_global_context;
62
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063struct omap_dma_lch {
64 int next_lch;
65 int dev_id;
66 u16 saved_csr;
67 u16 enabled_irqs;
68 const char *dev_name;
Tony Lindgren97b7f712008-07-03 12:24:37 +030069 void (*callback)(int lch, u16 ch_status, void *data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010070 void *data;
Anand Gadiyarf8151e52007-12-01 12:14:11 -080071
72#ifndef CONFIG_ARCH_OMAP1
73 /* required for Dynamic chaining */
74 int prev_linked_ch;
75 int next_linked_ch;
76 int state;
77 int chain_id;
78
79 int status;
80#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010081 long flags;
82};
83
Anand Gadiyarf8151e52007-12-01 12:14:11 -080084struct dma_link_info {
85 int *linked_dmach_q;
86 int no_of_lchs_linked;
87
88 int q_count;
89 int q_tail;
90 int q_head;
91
92 int chain_state;
93 int chain_mode;
94
95};
96
Tony Lindgren4d963722008-07-03 12:24:31 +030097static struct dma_link_info *dma_linked_lch;
98
99#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800100
101/* Chain handling macros */
102#define OMAP_DMA_CHAIN_QINIT(chain_id) \
103 do { \
104 dma_linked_lch[chain_id].q_head = \
105 dma_linked_lch[chain_id].q_tail = \
106 dma_linked_lch[chain_id].q_count = 0; \
107 } while (0)
108#define OMAP_DMA_CHAIN_QFULL(chain_id) \
109 (dma_linked_lch[chain_id].no_of_lchs_linked == \
110 dma_linked_lch[chain_id].q_count)
111#define OMAP_DMA_CHAIN_QLAST(chain_id) \
112 do { \
113 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
114 dma_linked_lch[chain_id].q_count) \
115 } while (0)
116#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
117 (0 == dma_linked_lch[chain_id].q_count)
118#define __OMAP_DMA_CHAIN_INCQ(end) \
119 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
120#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
121 do { \
122 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
123 dma_linked_lch[chain_id].q_count--; \
124 } while (0)
125
126#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
127 do { \
128 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
129 dma_linked_lch[chain_id].q_count++; \
130 } while (0)
131#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300132
133static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100134static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700135static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136
137static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300138static struct omap_dma_lch *dma_chan;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300139static void __iomem *omap_dma_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100140
Tony Lindgren4d963722008-07-03 12:24:31 +0300141static const u8 omap1_dma_irq[OMAP1_LOGICAL_DMA_CH_COUNT] = {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142 INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
143 INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
144 INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
145 INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
146 INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
147};
148
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800149static inline void disable_lnk(int lch);
150static void omap_disable_channel_irq(int lch);
151static inline void omap_enable_channel_irq(int lch);
152
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000153#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800154 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000155
Tony Lindgren0499bde2008-07-03 12:24:36 +0300156#define dma_read(reg) \
157({ \
158 u32 __val; \
159 if (cpu_class_is_omap1()) \
160 __val = __raw_readw(omap_dma_base + OMAP1_DMA_##reg); \
161 else \
162 __val = __raw_readl(omap_dma_base + OMAP_DMA4_##reg); \
163 __val; \
164})
165
166#define dma_write(val, reg) \
167({ \
168 if (cpu_class_is_omap1()) \
169 __raw_writew((u16)(val), omap_dma_base + OMAP1_DMA_##reg); \
170 else \
171 __raw_writel((val), omap_dma_base + OMAP_DMA4_##reg); \
172})
173
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000174#ifdef CONFIG_ARCH_OMAP15XX
175/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
176int omap_dma_in_1510_mode(void)
177{
178 return enable_1510_mode;
179}
180#else
181#define omap_dma_in_1510_mode() 0
182#endif
183
184#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100185static inline int get_gdma_dev(int req)
186{
187 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
188 int shift = ((req - 1) % 5) * 6;
189
190 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
191}
192
193static inline void set_gdma_dev(int req, int dev)
194{
195 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
196 int shift = ((req - 1) % 5) * 6;
197 u32 l;
198
199 l = omap_readl(reg);
200 l &= ~(0x3f << shift);
201 l |= (dev - 1) << shift;
202 omap_writel(l, reg);
203}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000204#else
205#define set_gdma_dev(req, dev) do {} while (0)
206#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100207
Tony Lindgren0499bde2008-07-03 12:24:36 +0300208/* Omap1 only */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209static void clear_lch_regs(int lch)
210{
211 int i;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300212 void __iomem *lch_base = omap_dma_base + OMAP1_DMA_CH_BASE(lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100213
214 for (i = 0; i < 0x2c; i += 2)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300215 __raw_writew(0, lch_base + i);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216}
217
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300218void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100219{
220 unsigned long reg;
221 u32 l;
222
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300223 if (cpu_class_is_omap1()) {
224 switch (dst_port) {
225 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
226 reg = OMAP_TC_OCPT1_PRIOR;
227 break;
228 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
229 reg = OMAP_TC_OCPT2_PRIOR;
230 break;
231 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
232 reg = OMAP_TC_EMIFF_PRIOR;
233 break;
234 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
235 reg = OMAP_TC_EMIFS_PRIOR;
236 break;
237 default:
238 BUG();
239 return;
240 }
241 l = omap_readl(reg);
242 l &= ~(0xf << 8);
243 l |= (priority & 0xf) << 8;
244 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100245 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300246
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800247 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300248 u32 ccr;
249
250 ccr = dma_read(CCR(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300251 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300252 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300253 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300254 ccr &= ~(1 << 6);
255 dma_write(ccr, CCR(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300256 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100257}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300258EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100259
260void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000261 int frame_count, int sync_mode,
262 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100263{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300264 u32 l;
265
266 l = dma_read(CSDP(lch));
267 l &= ~0x03;
268 l |= data_type;
269 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100270
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000271 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300272 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100273
Tony Lindgren0499bde2008-07-03 12:24:36 +0300274 ccr = dma_read(CCR(lch));
275 ccr &= ~(1 << 5);
276 if (sync_mode == OMAP_DMA_SYNC_FRAME)
277 ccr |= 1 << 5;
278 dma_write(ccr, CCR(lch));
279
280 ccr = dma_read(CCR2(lch));
281 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000282 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300283 ccr |= 1 << 2;
284 dma_write(ccr, CCR2(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000285 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800287 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300288 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289
Tony Lindgren0499bde2008-07-03 12:24:36 +0300290 val = dma_read(CCR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100291
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200292 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
293 val &= ~((3 << 19) | 0x1f);
294 val |= (dma_trigger & ~0x1f) << 14;
295 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000296
297 if (sync_mode & OMAP_DMA_SYNC_FRAME)
298 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700299 else
300 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000301
302 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
303 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700304 else
305 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000306
307 if (src_or_dst_synch)
308 val |= 1 << 24; /* source synch */
309 else
310 val &= ~(1 << 24); /* dest synch */
311
Tony Lindgren0499bde2008-07-03 12:24:36 +0300312 dma_write(val, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000313 }
314
Tony Lindgren0499bde2008-07-03 12:24:36 +0300315 dma_write(elem_count, CEN(lch));
316 dma_write(frame_count, CFN(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100317}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300318EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000319
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100320void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
321{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100322 BUG_ON(omap_dma_in_1510_mode());
323
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700324 if (cpu_class_is_omap1()) {
325 u16 w;
326
327 w = dma_read(CCR2(lch));
328 w &= ~0x03;
329
330 switch (mode) {
331 case OMAP_DMA_CONSTANT_FILL:
332 w |= 0x01;
333 break;
334 case OMAP_DMA_TRANSPARENT_COPY:
335 w |= 0x02;
336 break;
337 case OMAP_DMA_COLOR_DIS:
338 break;
339 default:
340 BUG();
341 }
342 dma_write(w, CCR2(lch));
343
344 w = dma_read(LCH_CTRL(lch));
345 w &= ~0x0f;
346 /* Default is channel type 2D */
347 if (mode) {
348 dma_write((u16)color, COLOR_L(lch));
349 dma_write((u16)(color >> 16), COLOR_U(lch));
350 w |= 1; /* Channel type G */
351 }
352 dma_write(w, LCH_CTRL(lch));
353 }
354
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800355 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700356 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000357
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700358 val = dma_read(CCR(lch));
359 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300360
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700361 switch (mode) {
362 case OMAP_DMA_CONSTANT_FILL:
363 val |= 1 << 16;
364 break;
365 case OMAP_DMA_TRANSPARENT_COPY:
366 val |= 1 << 17;
367 break;
368 case OMAP_DMA_COLOR_DIS:
369 break;
370 default:
371 BUG();
372 }
373 dma_write(val, CCR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100374
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700375 color &= 0xffffff;
376 dma_write(color, COLOR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100378}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300379EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300381void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
382{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800383 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300384 u32 csdp;
385
386 csdp = dma_read(CSDP(lch));
387 csdp &= ~(0x3 << 16);
388 csdp |= (mode << 16);
389 dma_write(csdp, CSDP(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300390 }
391}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300392EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300393
Tony Lindgren0499bde2008-07-03 12:24:36 +0300394void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
395{
396 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
397 u32 l;
398
399 l = dma_read(LCH_CTRL(lch));
400 l &= ~0x7;
401 l |= mode;
402 dma_write(l, LCH_CTRL(lch));
403 }
404}
405EXPORT_SYMBOL(omap_set_dma_channel_mode);
406
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000407/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000409 unsigned long src_start,
410 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100411{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300412 u32 l;
413
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000414 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300415 u16 w;
416
417 w = dma_read(CSDP(lch));
418 w &= ~(0x1f << 2);
419 w |= src_port << 2;
420 dma_write(w, CSDP(lch));
Tony Lindgren97b7f712008-07-03 12:24:37 +0300421 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300422
Tony Lindgren97b7f712008-07-03 12:24:37 +0300423 l = dma_read(CCR(lch));
424 l &= ~(0x03 << 12);
425 l |= src_amode << 12;
426 dma_write(l, CCR(lch));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300427
Tony Lindgren97b7f712008-07-03 12:24:37 +0300428 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300429 dma_write(src_start >> 16, CSSA_U(lch));
430 dma_write((u16)src_start, CSSA_L(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000431 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100432
Tony Lindgren97b7f712008-07-03 12:24:37 +0300433 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300434 dma_write(src_start, CSSA(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000435
Tony Lindgren97b7f712008-07-03 12:24:37 +0300436 dma_write(src_ei, CSEI(lch));
437 dma_write(src_fi, CSFI(lch));
438}
439EXPORT_SYMBOL(omap_set_dma_src_params);
440
441void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000442{
443 omap_set_dma_transfer_params(lch, params->data_type,
444 params->elem_count, params->frame_count,
445 params->sync_mode, params->trigger,
446 params->src_or_dst_synch);
447 omap_set_dma_src_params(lch, params->src_port,
448 params->src_amode, params->src_start,
449 params->src_ei, params->src_fi);
450
451 omap_set_dma_dest_params(lch, params->dst_port,
452 params->dst_amode, params->dst_start,
453 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800454 if (params->read_prio || params->write_prio)
455 omap_dma_set_prio_lch(lch, params->read_prio,
456 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100457}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300458EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100459
460void omap_set_dma_src_index(int lch, int eidx, int fidx)
461{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300462 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000463 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300464
Tony Lindgren0499bde2008-07-03 12:24:36 +0300465 dma_write(eidx, CSEI(lch));
466 dma_write(fidx, CSFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300468EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100469
470void omap_set_dma_src_data_pack(int lch, int enable)
471{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300472 u32 l;
473
474 l = dma_read(CSDP(lch));
475 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000476 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300477 l |= (1 << 6);
478 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100479}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300480EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100481
482void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
483{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700484 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300485 u32 l;
486
487 l = dma_read(CSDP(lch));
488 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100489
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100490 switch (burst_mode) {
491 case OMAP_DMA_DATA_BURST_DIS:
492 break;
493 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800494 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700495 burst = 0x1;
496 else
497 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498 break;
499 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800500 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700501 burst = 0x2;
502 break;
503 }
504 /* not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505 * w |= (0x03 << 7);
506 * fall through
507 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700508 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800509 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700510 burst = 0x3;
511 break;
512 }
513 /* OMAP1 don't support burst 16
514 * fall through
515 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516 default:
517 BUG();
518 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300519
520 l |= (burst << 7);
521 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300523EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000525/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000527 unsigned long dest_start,
528 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300530 u32 l;
531
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000532 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300533 l = dma_read(CSDP(lch));
534 l &= ~(0x1f << 9);
535 l |= dest_port << 9;
536 dma_write(l, CSDP(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000537 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538
Tony Lindgren0499bde2008-07-03 12:24:36 +0300539 l = dma_read(CCR(lch));
540 l &= ~(0x03 << 14);
541 l |= dest_amode << 14;
542 dma_write(l, CCR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000544 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300545 dma_write(dest_start >> 16, CDSA_U(lch));
546 dma_write(dest_start, CDSA_L(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000547 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800549 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300550 dma_write(dest_start, CDSA(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000551
Tony Lindgren0499bde2008-07-03 12:24:36 +0300552 dma_write(dst_ei, CDEI(lch));
553 dma_write(dst_fi, CDFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300555EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556
557void omap_set_dma_dest_index(int lch, int eidx, int fidx)
558{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300559 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000560 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300561
Tony Lindgren0499bde2008-07-03 12:24:36 +0300562 dma_write(eidx, CDEI(lch));
563 dma_write(fidx, CDFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100564}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300565EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100566
567void omap_set_dma_dest_data_pack(int lch, int enable)
568{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300569 u32 l;
570
571 l = dma_read(CSDP(lch));
572 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000573 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300574 l |= 1 << 13;
575 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300577EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578
579void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
580{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700581 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300582 u32 l;
583
584 l = dma_read(CSDP(lch));
585 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100586
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100587 switch (burst_mode) {
588 case OMAP_DMA_DATA_BURST_DIS:
589 break;
590 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800591 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700592 burst = 0x1;
593 else
594 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100595 break;
596 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800597 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700598 burst = 0x2;
599 else
600 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100601 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700602 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800603 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700604 burst = 0x3;
605 break;
606 }
607 /* OMAP1 don't support burst 16
608 * fall through
609 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610 default:
611 printk(KERN_ERR "Invalid DMA burst mode\n");
612 BUG();
613 return;
614 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300615 l |= (burst << 14);
616 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300618EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000620static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100621{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000622 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700624 /* Clear CSR */
625 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300626 status = dma_read(CSR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800627 else if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300628 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000629
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100630 /* Enable some nice interrupts. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300631 dma_write(dma_chan[lch].enabled_irqs, CICR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632}
633
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000634static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100635{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800636 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300637 dma_write(0, CICR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100638}
639
640void omap_enable_dma_irq(int lch, u16 bits)
641{
642 dma_chan[lch].enabled_irqs |= bits;
643}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300644EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100645
646void omap_disable_dma_irq(int lch, u16 bits)
647{
648 dma_chan[lch].enabled_irqs &= ~bits;
649}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300650EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100651
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000652static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100653{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300654 u32 l;
655
656 l = dma_read(CLNK_CTRL(lch));
657
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000658 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300659 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100660
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000661 /* Set the ENABLE_LNK bits */
662 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300663 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800664
665#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300666 if (cpu_class_is_omap2())
667 if (dma_chan[lch].next_linked_ch != -1)
668 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800669#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300670
671 dma_write(l, CLNK_CTRL(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100672}
673
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000674static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300676 u32 l;
677
678 l = dma_read(CLNK_CTRL(lch));
679
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000680 /* Disable interrupts */
681 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300682 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000683 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300684 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685 }
686
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800687 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000688 omap_disable_channel_irq(lch);
689 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300690 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000691 }
692
Tony Lindgren0499bde2008-07-03 12:24:36 +0300693 dma_write(l, CLNK_CTRL(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000694 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
695}
696
697static inline void omap2_enable_irq_lch(int lch)
698{
699 u32 val;
700
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800701 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000702 return;
703
Tony Lindgren0499bde2008-07-03 12:24:36 +0300704 val = dma_read(IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000705 val |= 1 << lch;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300706 dma_write(val, IRQENABLE_L0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100707}
708
709int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300710 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100711 void *data, int *dma_ch_out)
712{
713 int ch, free_ch = -1;
714 unsigned long flags;
715 struct omap_dma_lch *chan;
716
717 spin_lock_irqsave(&dma_chan_lock, flags);
718 for (ch = 0; ch < dma_chan_count; ch++) {
719 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
720 free_ch = ch;
721 if (dev_id == 0)
722 break;
723 }
724 }
725 if (free_ch == -1) {
726 spin_unlock_irqrestore(&dma_chan_lock, flags);
727 return -EBUSY;
728 }
729 chan = dma_chan + free_ch;
730 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000731
732 if (cpu_class_is_omap1())
733 clear_lch_regs(free_ch);
734
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800735 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000736 omap_clear_dma(free_ch);
737
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738 spin_unlock_irqrestore(&dma_chan_lock, flags);
739
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740 chan->dev_name = dev_name;
741 chan->callback = callback;
742 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800743 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300744
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800745#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300746 if (cpu_class_is_omap2()) {
747 chan->chain_id = -1;
748 chan->next_linked_ch = -1;
749 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800750#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300751
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700752 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000753
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700754 if (cpu_class_is_omap1())
755 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800756 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700757 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
758 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759
760 if (cpu_is_omap16xx()) {
761 /* If the sync device is set, configure it dynamically. */
762 if (dev_id != 0) {
763 set_gdma_dev(free_ch + 1, dev_id);
764 dev_id = free_ch + 1;
765 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300766 /*
767 * Disable the 1510 compatibility mode and set the sync device
768 * id.
769 */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300770 dma_write(dev_id | (1 << 10), CCR(free_ch));
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700771 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300772 dma_write(dev_id, CCR(free_ch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100773 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800775 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000776 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000777 omap_enable_channel_irq(free_ch);
778 /* Clear the CSR register and IRQ status register */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300779 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(free_ch));
780 dma_write(1 << free_ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000781 }
782
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783 *dma_ch_out = free_ch;
784
785 return 0;
786}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300787EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000789void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790{
791 unsigned long flags;
792
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000793 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300794 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000795 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796 return;
797 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300798
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000799 if (cpu_class_is_omap1()) {
800 /* Disable all DMA interrupts for the channel. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300801 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000802 /* Make sure the DMA transfer is stopped. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300803 dma_write(0, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000804 }
805
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800806 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000807 u32 val;
808 /* Disable interrupts */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300809 val = dma_read(IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000810 val &= ~(1 << lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300811 dma_write(val, IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000812
813 /* Clear the CSR register and IRQ status register */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300814 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
815 dma_write(1 << lch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000816
817 /* Disable all DMA interrupts for the channel. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300818 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000819
820 /* Make sure the DMA transfer is stopped. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300821 dma_write(0, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000822 omap_clear_dma(lch);
823 }
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700824
825 spin_lock_irqsave(&dma_chan_lock, flags);
826 dma_chan[lch].dev_id = -1;
827 dma_chan[lch].next_lch = -1;
828 dma_chan[lch].callback = NULL;
829 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100830}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300831EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100832
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800833/**
834 * @brief omap_dma_set_global_params : Set global priority settings for dma
835 *
836 * @param arb_rate
837 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700838 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
839 * DMA_THREAD_RESERVE_ONET
840 * DMA_THREAD_RESERVE_TWOT
841 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800842 */
843void
844omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
845{
846 u32 reg;
847
848 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800849 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800850 return;
851 }
852
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700853 if (max_fifo_depth == 0)
854 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800855 if (arb_rate == 0)
856 arb_rate = 1;
857
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700858 reg = 0xff & max_fifo_depth;
859 reg |= (0x3 & tparams) << 12;
860 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800861
Tony Lindgren0499bde2008-07-03 12:24:36 +0300862 dma_write(reg, GCR);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800863}
864EXPORT_SYMBOL(omap_dma_set_global_params);
865
866/**
867 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
868 *
869 * @param lch
870 * @param read_prio - Read priority
871 * @param write_prio - Write priority
872 * Both of the above can be set with one of the following values :
873 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
874 */
875int
876omap_dma_set_prio_lch(int lch, unsigned char read_prio,
877 unsigned char write_prio)
878{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300879 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800880
Tony Lindgren4d963722008-07-03 12:24:31 +0300881 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800882 printk(KERN_ERR "Invalid channel id\n");
883 return -EINVAL;
884 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300885 l = dma_read(CCR(lch));
886 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkar44169072009-05-28 14:16:04 -0700887 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300888 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800889 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300890 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800891
Tony Lindgren0499bde2008-07-03 12:24:36 +0300892 dma_write(l, CCR(lch));
893
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800894 return 0;
895}
896EXPORT_SYMBOL(omap_dma_set_prio_lch);
897
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000898/*
899 * Clears any DMA state so the DMA engine is ready to restart with new buffers
900 * through omap_start_dma(). Any buffers in flight are discarded.
901 */
902void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100903{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000904 unsigned long flags;
905
906 local_irq_save(flags);
907
908 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300909 u32 l;
910
911 l = dma_read(CCR(lch));
912 l &= ~OMAP_DMA_CCR_EN;
913 dma_write(l, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000914
915 /* Clear pending interrupts */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300916 l = dma_read(CSR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000917 }
918
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800919 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000920 int i;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300921 void __iomem *lch_base = omap_dma_base + OMAP_DMA4_CH_BASE(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000922 for (i = 0; i < 0x44; i += 4)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300923 __raw_writel(0, lch_base + i);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000924 }
925
926 local_irq_restore(flags);
927}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300928EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000929
930void omap_start_dma(int lch)
931{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300932 u32 l;
933
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000934 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
935 int next_lch, cur_lch;
Tony Lindgren4d963722008-07-03 12:24:31 +0300936 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000937
938 dma_chan_link_map[lch] = 1;
939 /* Set the link register of the first channel */
940 enable_lnk(lch);
941
942 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
943 cur_lch = dma_chan[lch].next_lch;
944 do {
945 next_lch = dma_chan[cur_lch].next_lch;
946
947 /* The loop case: we've been here already */
948 if (dma_chan_link_map[cur_lch])
949 break;
950 /* Mark the current channel */
951 dma_chan_link_map[cur_lch] = 1;
952
953 enable_lnk(cur_lch);
954 omap_enable_channel_irq(cur_lch);
955
956 cur_lch = next_lch;
957 } while (next_lch != -1);
Vikram Pandita284119c2009-08-10 14:49:50 +0300958 } else if (cpu_is_omap242x() ||
959 (cpu_is_omap243x() && omap_type() <= OMAP2430_REV_ES1_0)) {
960
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000961 /* Errata: Need to write lch even if not using chaining */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300962 dma_write(lch, CLNK_CTRL(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000963 }
964
965 omap_enable_channel_irq(lch);
966
Tony Lindgren0499bde2008-07-03 12:24:36 +0300967 l = dma_read(CCR(lch));
968
Tony Lindgren97b7f712008-07-03 12:24:37 +0300969 /*
970 * Errata: On ES2.0 BUFFERING disable must be set.
971 * This will always fail on ES1.0
972 */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300973 if (cpu_is_omap24xx())
974 l |= OMAP_DMA_CCR_EN;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000975
Tony Lindgren0499bde2008-07-03 12:24:36 +0300976 l |= OMAP_DMA_CCR_EN;
977 dma_write(l, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000978
979 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
980}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300981EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000982
983void omap_stop_dma(int lch)
984{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300985 u32 l;
986
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700987 /* Disable all interrupts on the channel */
988 if (cpu_class_is_omap1())
989 dma_write(0, CICR(lch));
990
991 l = dma_read(CCR(lch));
992 l &= ~OMAP_DMA_CCR_EN;
993 dma_write(l, CCR(lch));
994
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000995 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
996 int next_lch, cur_lch = lch;
Tony Lindgren4d963722008-07-03 12:24:31 +0300997 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000998
999 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
1000 do {
1001 /* The loop case: we've been here already */
1002 if (dma_chan_link_map[cur_lch])
1003 break;
1004 /* Mark the current channel */
1005 dma_chan_link_map[cur_lch] = 1;
1006
1007 disable_lnk(cur_lch);
1008
1009 next_lch = dma_chan[cur_lch].next_lch;
1010 cur_lch = next_lch;
1011 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001012 }
1013
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001014 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
1015}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001016EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001017
1018/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001019 * Allows changing the DMA callback function or data. This may be needed if
1020 * the driver shares a single DMA channel for multiple dma triggers.
1021 */
1022int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +03001023 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001024 void *data)
1025{
1026 unsigned long flags;
1027
1028 if (lch < 0)
1029 return -ENODEV;
1030
1031 spin_lock_irqsave(&dma_chan_lock, flags);
1032 if (dma_chan[lch].dev_id == -1) {
1033 printk(KERN_ERR "DMA callback for not set for free channel\n");
1034 spin_unlock_irqrestore(&dma_chan_lock, flags);
1035 return -EINVAL;
1036 }
1037 dma_chan[lch].callback = callback;
1038 dma_chan[lch].data = data;
1039 spin_unlock_irqrestore(&dma_chan_lock, flags);
1040
1041 return 0;
1042}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001043EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001044
1045/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001046 * Returns current physical source address for the given DMA channel.
1047 * If the channel is running the caller must disable interrupts prior calling
1048 * this function and process the returned value before re-enabling interrupt to
1049 * prevent races with the interrupt handler. Note that in continuous mode there
1050 * is a chance for CSSA_L register overflow inbetween the two reads resulting
1051 * in incorrect return value.
1052 */
1053dma_addr_t omap_get_dma_src_pos(int lch)
1054{
Tony Lindgren0695de32007-05-07 18:24:14 -07001055 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001056
Tony Lindgren0499bde2008-07-03 12:24:36 +03001057 if (cpu_is_omap15xx())
1058 offset = dma_read(CPC(lch));
1059 else
1060 offset = dma_read(CSAC(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001061
Tony Lindgren0499bde2008-07-03 12:24:36 +03001062 /*
1063 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1064 * read before the DMA controller finished disabling the channel.
1065 */
1066 if (!cpu_is_omap15xx() && offset == 0)
1067 offset = dma_read(CSAC(lch));
1068
1069 if (cpu_class_is_omap1())
1070 offset |= (dma_read(CSSA_U(lch)) << 16);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001071
1072 return offset;
1073}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001074EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001075
1076/*
1077 * Returns current physical destination address for the given DMA channel.
1078 * If the channel is running the caller must disable interrupts prior calling
1079 * this function and process the returned value before re-enabling interrupt to
1080 * prevent races with the interrupt handler. Note that in continuous mode there
1081 * is a chance for CDSA_L register overflow inbetween the two reads resulting
1082 * in incorrect return value.
1083 */
1084dma_addr_t omap_get_dma_dst_pos(int lch)
1085{
Tony Lindgren0695de32007-05-07 18:24:14 -07001086 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001087
Tony Lindgren0499bde2008-07-03 12:24:36 +03001088 if (cpu_is_omap15xx())
1089 offset = dma_read(CPC(lch));
1090 else
1091 offset = dma_read(CDAC(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001092
Tony Lindgren0499bde2008-07-03 12:24:36 +03001093 /*
1094 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1095 * read before the DMA controller finished disabling the channel.
1096 */
1097 if (!cpu_is_omap15xx() && offset == 0)
1098 offset = dma_read(CDAC(lch));
1099
1100 if (cpu_class_is_omap1())
1101 offset |= (dma_read(CDSA_U(lch)) << 16);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001102
1103 return offset;
1104}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001105EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001106
Tony Lindgren0499bde2008-07-03 12:24:36 +03001107int omap_get_dma_active_status(int lch)
1108{
1109 return (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN) != 0;
1110}
1111EXPORT_SYMBOL(omap_get_dma_active_status);
1112
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001113int omap_dma_running(void)
1114{
1115 int lch;
1116
1117 /* Check if LCD DMA is running */
1118 if (cpu_is_omap16xx())
1119 if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
1120 return 1;
1121
1122 for (lch = 0; lch < dma_chan_count; lch++)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001123 if (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001124 return 1;
1125
1126 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001127}
1128
1129/*
1130 * lch_queue DMA will start right after lch_head one is finished.
1131 * For this DMA link to start, you still need to start (see omap_start_dma)
1132 * the first one. That will fire up the entire queue.
1133 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001134void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001135{
1136 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001137 if (lch_head == lch_queue) {
1138 dma_write(dma_read(CCR(lch_head)) | (3 << 8),
1139 CCR(lch_head));
1140 return;
1141 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001142 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1143 BUG();
1144 return;
1145 }
1146
1147 if ((dma_chan[lch_head].dev_id == -1) ||
1148 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001149 printk(KERN_ERR "omap_dma: trying to link "
1150 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001151 dump_stack();
1152 }
1153
1154 dma_chan[lch_head].next_lch = lch_queue;
1155}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001156EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157
1158/*
1159 * Once the DMA queue is stopped, we can destroy it.
1160 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001161void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001162{
1163 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001164 if (lch_head == lch_queue) {
1165 dma_write(dma_read(CCR(lch_head)) & ~(3 << 8),
1166 CCR(lch_head));
1167 return;
1168 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001169 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1170 BUG();
1171 return;
1172 }
1173
1174 if (dma_chan[lch_head].next_lch != lch_queue ||
1175 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001176 printk(KERN_ERR "omap_dma: trying to unlink "
1177 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001178 dump_stack();
1179 }
1180
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001181 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
1182 (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001183 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1184 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001185 dump_stack();
1186 }
1187
1188 dma_chan[lch_head].next_lch = -1;
1189}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001190EXPORT_SYMBOL(omap_dma_unlink_lch);
1191
1192/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001193
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001194#ifndef CONFIG_ARCH_OMAP1
1195/* Create chain of DMA channesls */
1196static void create_dma_lch_chain(int lch_head, int lch_queue)
1197{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001198 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001199
1200 /* Check if this is the first link in chain */
1201 if (dma_chan[lch_head].next_linked_ch == -1) {
1202 dma_chan[lch_head].next_linked_ch = lch_queue;
1203 dma_chan[lch_head].prev_linked_ch = lch_queue;
1204 dma_chan[lch_queue].next_linked_ch = lch_head;
1205 dma_chan[lch_queue].prev_linked_ch = lch_head;
1206 }
1207
1208 /* a link exists, link the new channel in circular chain */
1209 else {
1210 dma_chan[lch_queue].next_linked_ch =
1211 dma_chan[lch_head].next_linked_ch;
1212 dma_chan[lch_queue].prev_linked_ch = lch_head;
1213 dma_chan[lch_head].next_linked_ch = lch_queue;
1214 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1215 lch_queue;
1216 }
1217
Tony Lindgren0499bde2008-07-03 12:24:36 +03001218 l = dma_read(CLNK_CTRL(lch_head));
1219 l &= ~(0x1f);
1220 l |= lch_queue;
1221 dma_write(l, CLNK_CTRL(lch_head));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001222
Tony Lindgren0499bde2008-07-03 12:24:36 +03001223 l = dma_read(CLNK_CTRL(lch_queue));
1224 l &= ~(0x1f);
1225 l |= (dma_chan[lch_queue].next_linked_ch);
1226 dma_write(l, CLNK_CTRL(lch_queue));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001227}
1228
1229/**
1230 * @brief omap_request_dma_chain : Request a chain of DMA channels
1231 *
1232 * @param dev_id - Device id using the dma channel
1233 * @param dev_name - Device name
1234 * @param callback - Call back function
1235 * @chain_id -
1236 * @no_of_chans - Number of channels requested
1237 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1238 * OMAP_DMA_DYNAMIC_CHAIN
1239 * @params - Channel parameters
1240 *
1241 * @return - Succes : 0
1242 * Failure: -EINVAL/-ENOMEM
1243 */
1244int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b918d2009-05-28 13:23:52 -07001245 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001246 void *data),
1247 int *chain_id, int no_of_chans, int chain_mode,
1248 struct omap_dma_channel_params params)
1249{
1250 int *channels;
1251 int i, err;
1252
1253 /* Is the chain mode valid ? */
1254 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1255 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1256 printk(KERN_ERR "Invalid chain mode requested\n");
1257 return -EINVAL;
1258 }
1259
1260 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001261 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001262 printk(KERN_ERR "Invalid Number of channels requested\n");
1263 return -EINVAL;
1264 }
1265
1266 /* Allocate a queue to maintain the status of the channels
1267 * in the chain */
1268 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1269 if (channels == NULL) {
1270 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1271 return -ENOMEM;
1272 }
1273
1274 /* request and reserve DMA channels for the chain */
1275 for (i = 0; i < no_of_chans; i++) {
1276 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001277 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001278 if (err < 0) {
1279 int j;
1280 for (j = 0; j < i; j++)
1281 omap_free_dma(channels[j]);
1282 kfree(channels);
1283 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1284 return err;
1285 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001286 dma_chan[channels[i]].prev_linked_ch = -1;
1287 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1288
1289 /*
1290 * Allowing client drivers to set common parameters now,
1291 * so that later only relevant (src_start, dest_start
1292 * and element count) can be set
1293 */
1294 omap_set_dma_params(channels[i], &params);
1295 }
1296
1297 *chain_id = channels[0];
1298 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1299 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1300 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1301 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1302
1303 for (i = 0; i < no_of_chans; i++)
1304 dma_chan[channels[i]].chain_id = *chain_id;
1305
1306 /* Reset the Queue pointers */
1307 OMAP_DMA_CHAIN_QINIT(*chain_id);
1308
1309 /* Set up the chain */
1310 if (no_of_chans == 1)
1311 create_dma_lch_chain(channels[0], channels[0]);
1312 else {
1313 for (i = 0; i < (no_of_chans - 1); i++)
1314 create_dma_lch_chain(channels[i], channels[i + 1]);
1315 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001316
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001317 return 0;
1318}
1319EXPORT_SYMBOL(omap_request_dma_chain);
1320
1321/**
1322 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1323 * params after setting it. Dont do this while dma is running!!
1324 *
1325 * @param chain_id - Chained logical channel id.
1326 * @param params
1327 *
1328 * @return - Success : 0
1329 * Failure : -EINVAL
1330 */
1331int omap_modify_dma_chain_params(int chain_id,
1332 struct omap_dma_channel_params params)
1333{
1334 int *channels;
1335 u32 i;
1336
1337 /* Check for input params */
1338 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001339 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001340 printk(KERN_ERR "Invalid chain id\n");
1341 return -EINVAL;
1342 }
1343
1344 /* Check if the chain exists */
1345 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1346 printk(KERN_ERR "Chain doesn't exists\n");
1347 return -EINVAL;
1348 }
1349 channels = dma_linked_lch[chain_id].linked_dmach_q;
1350
1351 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1352 /*
1353 * Allowing client drivers to set common parameters now,
1354 * so that later only relevant (src_start, dest_start
1355 * and element count) can be set
1356 */
1357 omap_set_dma_params(channels[i], &params);
1358 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001359
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001360 return 0;
1361}
1362EXPORT_SYMBOL(omap_modify_dma_chain_params);
1363
1364/**
1365 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1366 *
1367 * @param chain_id
1368 *
1369 * @return - Success : 0
1370 * Failure : -EINVAL
1371 */
1372int omap_free_dma_chain(int chain_id)
1373{
1374 int *channels;
1375 u32 i;
1376
1377 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001378 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001379 printk(KERN_ERR "Invalid chain id\n");
1380 return -EINVAL;
1381 }
1382
1383 /* Check if the chain exists */
1384 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1385 printk(KERN_ERR "Chain doesn't exists\n");
1386 return -EINVAL;
1387 }
1388
1389 channels = dma_linked_lch[chain_id].linked_dmach_q;
1390 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1391 dma_chan[channels[i]].next_linked_ch = -1;
1392 dma_chan[channels[i]].prev_linked_ch = -1;
1393 dma_chan[channels[i]].chain_id = -1;
1394 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1395 omap_free_dma(channels[i]);
1396 }
1397
1398 kfree(channels);
1399
1400 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1401 dma_linked_lch[chain_id].chain_mode = -1;
1402 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001403
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001404 return (0);
1405}
1406EXPORT_SYMBOL(omap_free_dma_chain);
1407
1408/**
1409 * @brief omap_dma_chain_status - Check if the chain is in
1410 * active / inactive state.
1411 * @param chain_id
1412 *
1413 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1414 * Failure : -EINVAL
1415 */
1416int omap_dma_chain_status(int chain_id)
1417{
1418 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001419 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001420 printk(KERN_ERR "Invalid chain id\n");
1421 return -EINVAL;
1422 }
1423
1424 /* Check if the chain exists */
1425 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1426 printk(KERN_ERR "Chain doesn't exists\n");
1427 return -EINVAL;
1428 }
1429 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1430 dma_linked_lch[chain_id].q_count);
1431
1432 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1433 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001434
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001435 return OMAP_DMA_CHAIN_ACTIVE;
1436}
1437EXPORT_SYMBOL(omap_dma_chain_status);
1438
1439/**
1440 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1441 * set the params and start the transfer.
1442 *
1443 * @param chain_id
1444 * @param src_start - buffer start address
1445 * @param dest_start - Dest address
1446 * @param elem_count
1447 * @param frame_count
1448 * @param callbk_data - channel callback parameter data.
1449 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301450 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001451 * Failure: -EINVAL/-EBUSY
1452 */
1453int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1454 int elem_count, int frame_count, void *callbk_data)
1455{
1456 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001457 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001458 int start_dma = 0;
1459
Tony Lindgren97b7f712008-07-03 12:24:37 +03001460 /*
1461 * if buffer size is less than 1 then there is
1462 * no use of starting the chain
1463 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001464 if (elem_count < 1) {
1465 printk(KERN_ERR "Invalid buffer size\n");
1466 return -EINVAL;
1467 }
1468
1469 /* Check for input params */
1470 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001471 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001472 printk(KERN_ERR "Invalid chain id\n");
1473 return -EINVAL;
1474 }
1475
1476 /* Check if the chain exists */
1477 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1478 printk(KERN_ERR "Chain doesn't exist\n");
1479 return -EINVAL;
1480 }
1481
1482 /* Check if all the channels in chain are in use */
1483 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1484 return -EBUSY;
1485
1486 /* Frame count may be negative in case of indexed transfers */
1487 channels = dma_linked_lch[chain_id].linked_dmach_q;
1488
1489 /* Get a free channel */
1490 lch = channels[dma_linked_lch[chain_id].q_tail];
1491
1492 /* Store the callback data */
1493 dma_chan[lch].data = callbk_data;
1494
1495 /* Increment the q_tail */
1496 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1497
1498 /* Set the params to the free channel */
1499 if (src_start != 0)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001500 dma_write(src_start, CSSA(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001501 if (dest_start != 0)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001502 dma_write(dest_start, CDSA(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001503
1504 /* Write the buffer size */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001505 dma_write(elem_count, CEN(lch));
1506 dma_write(frame_count, CFN(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001507
Tony Lindgren97b7f712008-07-03 12:24:37 +03001508 /*
1509 * If the chain is dynamically linked,
1510 * then we may have to start the chain if its not active
1511 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001512 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1513
Tony Lindgren97b7f712008-07-03 12:24:37 +03001514 /*
1515 * In Dynamic chain, if the chain is not started,
1516 * queue the channel
1517 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001518 if (dma_linked_lch[chain_id].chain_state ==
1519 DMA_CHAIN_NOTSTARTED) {
1520 /* Enable the link in previous channel */
1521 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1522 DMA_CH_QUEUED)
1523 enable_lnk(dma_chan[lch].prev_linked_ch);
1524 dma_chan[lch].state = DMA_CH_QUEUED;
1525 }
1526
Tony Lindgren97b7f712008-07-03 12:24:37 +03001527 /*
1528 * Chain is already started, make sure its active,
1529 * if not then start the chain
1530 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001531 else {
1532 start_dma = 1;
1533
1534 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1535 DMA_CH_STARTED) {
1536 enable_lnk(dma_chan[lch].prev_linked_ch);
1537 dma_chan[lch].state = DMA_CH_QUEUED;
1538 start_dma = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001539 if (0 == ((1 << 7) & dma_read(
1540 CCR(dma_chan[lch].prev_linked_ch)))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001541 disable_lnk(dma_chan[lch].
1542 prev_linked_ch);
1543 pr_debug("\n prev ch is stopped\n");
1544 start_dma = 1;
1545 }
1546 }
1547
1548 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1549 == DMA_CH_QUEUED) {
1550 enable_lnk(dma_chan[lch].prev_linked_ch);
1551 dma_chan[lch].state = DMA_CH_QUEUED;
1552 start_dma = 0;
1553 }
1554 omap_enable_channel_irq(lch);
1555
Tony Lindgren0499bde2008-07-03 12:24:36 +03001556 l = dma_read(CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001557
Tony Lindgren0499bde2008-07-03 12:24:36 +03001558 if ((0 == (l & (1 << 24))))
1559 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001560 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001561 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001562 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001563 if (0 == (l & (1 << 7))) {
1564 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001565 dma_chan[lch].state = DMA_CH_STARTED;
1566 pr_debug("starting %d\n", lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001567 dma_write(l, CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001568 } else
1569 start_dma = 0;
1570 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001571 if (0 == (l & (1 << 7)))
1572 dma_write(l, CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001573 }
1574 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1575 }
1576 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001577
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301578 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001579}
1580EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1581
1582/**
1583 * @brief omap_start_dma_chain_transfers - Start the chain
1584 *
1585 * @param chain_id
1586 *
1587 * @return - Success : 0
1588 * Failure : -EINVAL/-EBUSY
1589 */
1590int omap_start_dma_chain_transfers(int chain_id)
1591{
1592 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001593 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001594
Tony Lindgren4d963722008-07-03 12:24:31 +03001595 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001596 printk(KERN_ERR "Invalid chain id\n");
1597 return -EINVAL;
1598 }
1599
1600 channels = dma_linked_lch[chain_id].linked_dmach_q;
1601
1602 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1603 printk(KERN_ERR "Chain is already started\n");
1604 return -EBUSY;
1605 }
1606
1607 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1608 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1609 i++) {
1610 enable_lnk(channels[i]);
1611 omap_enable_channel_irq(channels[i]);
1612 }
1613 } else {
1614 omap_enable_channel_irq(channels[0]);
1615 }
1616
Tony Lindgren0499bde2008-07-03 12:24:36 +03001617 l = dma_read(CCR(channels[0]));
1618 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001619 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1620 dma_chan[channels[0]].state = DMA_CH_STARTED;
1621
Tony Lindgren0499bde2008-07-03 12:24:36 +03001622 if ((0 == (l & (1 << 24))))
1623 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001624 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001625 l |= (1 << 25);
1626 dma_write(l, CCR(channels[0]));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001627
1628 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001629
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001630 return 0;
1631}
1632EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1633
1634/**
1635 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1636 *
1637 * @param chain_id
1638 *
1639 * @return - Success : 0
1640 * Failure : EINVAL
1641 */
1642int omap_stop_dma_chain_transfers(int chain_id)
1643{
1644 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001645 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001646 u32 sys_cf;
1647
1648 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001649 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001650 printk(KERN_ERR "Invalid chain id\n");
1651 return -EINVAL;
1652 }
1653
1654 /* Check if the chain exists */
1655 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1656 printk(KERN_ERR "Chain doesn't exists\n");
1657 return -EINVAL;
1658 }
1659 channels = dma_linked_lch[chain_id].linked_dmach_q;
1660
Tony Lindgren97b7f712008-07-03 12:24:37 +03001661 /*
1662 * DMA Errata:
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001663 * Special programming model needed to disable DMA before end of block
1664 */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001665 sys_cf = dma_read(OCP_SYSCONFIG);
1666 l = sys_cf;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001667 /* Middle mode reg set no Standby */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001668 l &= ~((1 << 12)|(1 << 13));
1669 dma_write(l, OCP_SYSCONFIG);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001670
1671 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1672
1673 /* Stop the Channel transmission */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001674 l = dma_read(CCR(channels[i]));
1675 l &= ~(1 << 7);
1676 dma_write(l, CCR(channels[i]));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001677
1678 /* Disable the link in all the channels */
1679 disable_lnk(channels[i]);
1680 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1681
1682 }
1683 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1684
1685 /* Reset the Queue pointers */
1686 OMAP_DMA_CHAIN_QINIT(chain_id);
1687
1688 /* Errata - put in the old value */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001689 dma_write(sys_cf, OCP_SYSCONFIG);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001690
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001691 return 0;
1692}
1693EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1694
1695/* Get the index of the ongoing DMA in chain */
1696/**
1697 * @brief omap_get_dma_chain_index - Get the element and frame index
1698 * of the ongoing DMA in chain
1699 *
1700 * @param chain_id
1701 * @param ei - Element index
1702 * @param fi - Frame index
1703 *
1704 * @return - Success : 0
1705 * Failure : -EINVAL
1706 */
1707int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1708{
1709 int lch;
1710 int *channels;
1711
1712 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001713 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001714 printk(KERN_ERR "Invalid chain id\n");
1715 return -EINVAL;
1716 }
1717
1718 /* Check if the chain exists */
1719 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1720 printk(KERN_ERR "Chain doesn't exists\n");
1721 return -EINVAL;
1722 }
1723 if ((!ei) || (!fi))
1724 return -EINVAL;
1725
1726 channels = dma_linked_lch[chain_id].linked_dmach_q;
1727
1728 /* Get the current channel */
1729 lch = channels[dma_linked_lch[chain_id].q_head];
1730
Tony Lindgren0499bde2008-07-03 12:24:36 +03001731 *ei = dma_read(CCEN(lch));
1732 *fi = dma_read(CCFN(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001733
1734 return 0;
1735}
1736EXPORT_SYMBOL(omap_get_dma_chain_index);
1737
1738/**
1739 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1740 * ongoing DMA in chain
1741 *
1742 * @param chain_id
1743 *
1744 * @return - Success : Destination position
1745 * Failure : -EINVAL
1746 */
1747int omap_get_dma_chain_dst_pos(int chain_id)
1748{
1749 int lch;
1750 int *channels;
1751
1752 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001753 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001754 printk(KERN_ERR "Invalid chain id\n");
1755 return -EINVAL;
1756 }
1757
1758 /* Check if the chain exists */
1759 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1760 printk(KERN_ERR "Chain doesn't exists\n");
1761 return -EINVAL;
1762 }
1763
1764 channels = dma_linked_lch[chain_id].linked_dmach_q;
1765
1766 /* Get the current channel */
1767 lch = channels[dma_linked_lch[chain_id].q_head];
1768
Tony Lindgren0499bde2008-07-03 12:24:36 +03001769 return dma_read(CDAC(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001770}
1771EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1772
1773/**
1774 * @brief omap_get_dma_chain_src_pos - Get the source position
1775 * of the ongoing DMA in chain
1776 * @param chain_id
1777 *
1778 * @return - Success : Destination position
1779 * Failure : -EINVAL
1780 */
1781int omap_get_dma_chain_src_pos(int chain_id)
1782{
1783 int lch;
1784 int *channels;
1785
1786 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001787 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001788 printk(KERN_ERR "Invalid chain id\n");
1789 return -EINVAL;
1790 }
1791
1792 /* Check if the chain exists */
1793 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1794 printk(KERN_ERR "Chain doesn't exists\n");
1795 return -EINVAL;
1796 }
1797
1798 channels = dma_linked_lch[chain_id].linked_dmach_q;
1799
1800 /* Get the current channel */
1801 lch = channels[dma_linked_lch[chain_id].q_head];
1802
Tony Lindgren0499bde2008-07-03 12:24:36 +03001803 return dma_read(CSAC(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001804}
1805EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001806#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001807
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001808/*----------------------------------------------------------------------------*/
1809
1810#ifdef CONFIG_ARCH_OMAP1
1811
1812static int omap1_dma_handle_ch(int ch)
1813{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001814 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001815
1816 if (enable_1510_mode && ch >= 6) {
1817 csr = dma_chan[ch].saved_csr;
1818 dma_chan[ch].saved_csr = 0;
1819 } else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001820 csr = dma_read(CSR(ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001821 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1822 dma_chan[ch + 6].saved_csr = csr >> 7;
1823 csr &= 0x7f;
1824 }
1825 if ((csr & 0x3f) == 0)
1826 return 0;
1827 if (unlikely(dma_chan[ch].dev_id == -1)) {
1828 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1829 "%d (CSR %04x)\n", ch, csr);
1830 return 0;
1831 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001832 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001833 printk(KERN_WARNING "DMA timeout with device %d\n",
1834 dma_chan[ch].dev_id);
1835 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1836 printk(KERN_WARNING "DMA synchronization event drop occurred "
1837 "with device %d\n", dma_chan[ch].dev_id);
1838 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1839 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1840 if (likely(dma_chan[ch].callback != NULL))
1841 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001842
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001843 return 1;
1844}
1845
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001846static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001847{
1848 int ch = ((int) dev_id) - 1;
1849 int handled = 0;
1850
1851 for (;;) {
1852 int handled_now = 0;
1853
1854 handled_now += omap1_dma_handle_ch(ch);
1855 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1856 handled_now += omap1_dma_handle_ch(ch + 6);
1857 if (!handled_now)
1858 break;
1859 handled += handled_now;
1860 }
1861
1862 return handled ? IRQ_HANDLED : IRQ_NONE;
1863}
1864
1865#else
1866#define omap1_dma_irq_handler NULL
1867#endif
1868
Santosh Shilimkar44169072009-05-28 14:16:04 -07001869#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3) || \
1870 defined(CONFIG_ARCH_OMAP4)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001871
1872static int omap2_dma_handle_ch(int ch)
1873{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001874 u32 status = dma_read(CSR(ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001875
Juha Yrjola31513692006-12-06 17:13:47 -08001876 if (!status) {
1877 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001878 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1879 ch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001880 dma_write(1 << ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001881 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001882 }
1883 if (unlikely(dma_chan[ch].dev_id == -1)) {
1884 if (printk_ratelimit())
1885 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1886 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001887 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001888 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001889 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1890 printk(KERN_INFO
1891 "DMA synchronization event drop occurred with device "
1892 "%d\n", dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001893 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001894 printk(KERN_INFO "DMA transaction error with device %d\n",
1895 dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001896 if (cpu_class_is_omap2()) {
1897 /* Errata: sDMA Channel is not disabled
1898 * after a transaction error. So we explicitely
1899 * disable the channel
1900 */
1901 u32 ccr;
1902
1903 ccr = dma_read(CCR(ch));
1904 ccr &= ~OMAP_DMA_CCR_EN;
1905 dma_write(ccr, CCR(ch));
1906 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1907 }
1908 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001909 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1910 printk(KERN_INFO "DMA secure error with device %d\n",
1911 dma_chan[ch].dev_id);
1912 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1913 printk(KERN_INFO "DMA misaligned error with device %d\n",
1914 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001915
Tony Lindgren0499bde2008-07-03 12:24:36 +03001916 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(ch));
1917 dma_write(1 << ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001918
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001919 /* If the ch is not chained then chain_id will be -1 */
1920 if (dma_chan[ch].chain_id != -1) {
1921 int chain_id = dma_chan[ch].chain_id;
1922 dma_chan[ch].state = DMA_CH_NOTSTARTED;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001923 if (dma_read(CLNK_CTRL(ch)) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001924 dma_chan[dma_chan[ch].next_linked_ch].state =
1925 DMA_CH_STARTED;
1926 if (dma_linked_lch[chain_id].chain_mode ==
1927 OMAP_DMA_DYNAMIC_CHAIN)
1928 disable_lnk(ch);
1929
1930 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1931 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1932
Tony Lindgren0499bde2008-07-03 12:24:36 +03001933 status = dma_read(CSR(ch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001934 }
1935
Juha Yrjola320ce6f2009-01-29 08:57:12 -08001936 dma_write(status, CSR(ch));
1937
Jarkko Nikula538528d2008-02-13 11:47:29 +02001938 if (likely(dma_chan[ch].callback != NULL))
1939 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001940
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001941 return 0;
1942}
1943
1944/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001945static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001946{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001947 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001948 int i;
1949
Tony Lindgren0499bde2008-07-03 12:24:36 +03001950 val = dma_read(IRQSTATUS_L0);
Juha Yrjola31513692006-12-06 17:13:47 -08001951 if (val == 0) {
1952 if (printk_ratelimit())
1953 printk(KERN_WARNING "Spurious DMA IRQ\n");
1954 return IRQ_HANDLED;
1955 }
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001956 enable_reg = dma_read(IRQENABLE_L0);
1957 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001958 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001959 if (val & 1)
1960 omap2_dma_handle_ch(i);
1961 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001962 }
1963
1964 return IRQ_HANDLED;
1965}
1966
1967static struct irqaction omap24xx_dma_irq = {
1968 .name = "DMA",
1969 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001970 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001971};
1972
1973#else
1974static struct irqaction omap24xx_dma_irq;
1975#endif
1976
1977/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001978
1979static struct lcd_dma_info {
1980 spinlock_t lock;
1981 int reserved;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001982 void (*callback)(u16 status, void *data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001983 void *cb_data;
1984
1985 int active;
1986 unsigned long addr, size;
1987 int rotate, data_type, xres, yres;
1988 int vxres;
1989 int mirror;
1990 int xscale, yscale;
1991 int ext_ctrl;
1992 int src_port;
1993 int single_transfer;
1994} lcd_dma;
1995
1996void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
1997 int data_type)
1998{
1999 lcd_dma.addr = addr;
2000 lcd_dma.data_type = data_type;
2001 lcd_dma.xres = fb_xres;
2002 lcd_dma.yres = fb_yres;
2003}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002004EXPORT_SYMBOL(omap_set_lcd_dma_b1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002005
2006void omap_set_lcd_dma_src_port(int port)
2007{
2008 lcd_dma.src_port = port;
2009}
2010
2011void omap_set_lcd_dma_ext_controller(int external)
2012{
2013 lcd_dma.ext_ctrl = external;
2014}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002015EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002016
2017void omap_set_lcd_dma_single_transfer(int single)
2018{
2019 lcd_dma.single_transfer = single;
2020}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002021EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002022
2023void omap_set_lcd_dma_b1_rotation(int rotate)
2024{
2025 if (omap_dma_in_1510_mode()) {
2026 printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
2027 BUG();
2028 return;
2029 }
2030 lcd_dma.rotate = rotate;
2031}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002032EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002033
2034void omap_set_lcd_dma_b1_mirror(int mirror)
2035{
2036 if (omap_dma_in_1510_mode()) {
2037 printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
2038 BUG();
2039 }
2040 lcd_dma.mirror = mirror;
2041}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002042EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002043
2044void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
2045{
2046 if (omap_dma_in_1510_mode()) {
2047 printk(KERN_ERR "DMA virtual resulotion is not supported "
2048 "in 1510 mode\n");
2049 BUG();
2050 }
2051 lcd_dma.vxres = vxres;
2052}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002053EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002054
2055void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
2056{
2057 if (omap_dma_in_1510_mode()) {
2058 printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
2059 BUG();
2060 }
2061 lcd_dma.xscale = xscale;
2062 lcd_dma.yscale = yscale;
2063}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002064EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002065
2066static void set_b1_regs(void)
2067{
2068 unsigned long top, bottom;
2069 int es;
2070 u16 w;
2071 unsigned long en, fn;
2072 long ei, fi;
2073 unsigned long vxres;
2074 unsigned int xscale, yscale;
2075
2076 switch (lcd_dma.data_type) {
2077 case OMAP_DMA_DATA_TYPE_S8:
2078 es = 1;
2079 break;
2080 case OMAP_DMA_DATA_TYPE_S16:
2081 es = 2;
2082 break;
2083 case OMAP_DMA_DATA_TYPE_S32:
2084 es = 4;
2085 break;
2086 default:
2087 BUG();
2088 return;
2089 }
2090
2091 vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
2092 xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
2093 yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
2094 BUG_ON(vxres < lcd_dma.xres);
Tony Lindgren97b7f712008-07-03 12:24:37 +03002095
2096#define PIXADDR(x, y) (lcd_dma.addr + \
2097 ((y) * vxres * yscale + (x) * xscale) * es)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002098#define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
Tony Lindgren97b7f712008-07-03 12:24:37 +03002099
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002100 switch (lcd_dma.rotate) {
2101 case 0:
2102 if (!lcd_dma.mirror) {
2103 top = PIXADDR(0, 0);
2104 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2105 /* 1510 DMA requires the bottom address to be 2 more
2106 * than the actual last memory access location. */
2107 if (omap_dma_in_1510_mode() &&
Tony Lindgren97b7f712008-07-03 12:24:37 +03002108 lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
2109 bottom += 2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002110 ei = PIXSTEP(0, 0, 1, 0);
2111 fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
2112 } else {
2113 top = PIXADDR(lcd_dma.xres - 1, 0);
2114 bottom = PIXADDR(0, lcd_dma.yres - 1);
2115 ei = PIXSTEP(1, 0, 0, 0);
2116 fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
2117 }
2118 en = lcd_dma.xres;
2119 fn = lcd_dma.yres;
2120 break;
2121 case 90:
2122 if (!lcd_dma.mirror) {
2123 top = PIXADDR(0, lcd_dma.yres - 1);
2124 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2125 ei = PIXSTEP(0, 1, 0, 0);
2126 fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
2127 } else {
2128 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2129 bottom = PIXADDR(0, 0);
2130 ei = PIXSTEP(0, 1, 0, 0);
2131 fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
2132 }
2133 en = lcd_dma.yres;
2134 fn = lcd_dma.xres;
2135 break;
2136 case 180:
2137 if (!lcd_dma.mirror) {
2138 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2139 bottom = PIXADDR(0, 0);
2140 ei = PIXSTEP(1, 0, 0, 0);
2141 fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
2142 } else {
2143 top = PIXADDR(0, lcd_dma.yres - 1);
2144 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2145 ei = PIXSTEP(0, 0, 1, 0);
2146 fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
2147 }
2148 en = lcd_dma.xres;
2149 fn = lcd_dma.yres;
2150 break;
2151 case 270:
2152 if (!lcd_dma.mirror) {
2153 top = PIXADDR(lcd_dma.xres - 1, 0);
2154 bottom = PIXADDR(0, lcd_dma.yres - 1);
2155 ei = PIXSTEP(0, 0, 0, 1);
2156 fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
2157 } else {
2158 top = PIXADDR(0, 0);
2159 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2160 ei = PIXSTEP(0, 0, 0, 1);
2161 fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
2162 }
2163 en = lcd_dma.yres;
2164 fn = lcd_dma.xres;
2165 break;
2166 default:
2167 BUG();
Simon Arlott6cbdc8c2007-05-11 20:40:30 +01002168 return; /* Suppress warning about uninitialized vars */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002169 }
2170
2171 if (omap_dma_in_1510_mode()) {
2172 omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
2173 omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
2174 omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
2175 omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
2176
2177 return;
2178 }
2179
2180 /* 1610 regs */
2181 omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
2182 omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
2183 omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
2184 omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
2185
2186 omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
2187 omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
2188
2189 w = omap_readw(OMAP1610_DMA_LCD_CSDP);
2190 w &= ~0x03;
2191 w |= lcd_dma.data_type;
2192 omap_writew(w, OMAP1610_DMA_LCD_CSDP);
2193
2194 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2195 /* Always set the source port as SDRAM for now*/
2196 w &= ~(0x03 << 6);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002197 if (lcd_dma.callback != NULL)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002198 w |= 1 << 1; /* Block interrupt enable */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002199 else
2200 w &= ~(1 << 1);
2201 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2202
2203 if (!(lcd_dma.rotate || lcd_dma.mirror ||
2204 lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
2205 return;
2206
2207 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2208 /* Set the double-indexed addressing mode */
2209 w |= (0x03 << 12);
2210 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2211
2212 omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
2213 omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
2214 omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
2215}
2216
Linus Torvalds0cd61b62006-10-06 10:53:39 -07002217static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002218{
2219 u16 w;
2220
2221 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2222 if (unlikely(!(w & (1 << 3)))) {
2223 printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
2224 return IRQ_NONE;
2225 }
2226 /* Ack the IRQ */
2227 w |= (1 << 3);
2228 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2229 lcd_dma.active = 0;
2230 if (lcd_dma.callback != NULL)
2231 lcd_dma.callback(w, lcd_dma.cb_data);
2232
2233 return IRQ_HANDLED;
2234}
2235
Tony Lindgren97b7f712008-07-03 12:24:37 +03002236int omap_request_lcd_dma(void (*callback)(u16 status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002237 void *data)
2238{
2239 spin_lock_irq(&lcd_dma.lock);
2240 if (lcd_dma.reserved) {
2241 spin_unlock_irq(&lcd_dma.lock);
2242 printk(KERN_ERR "LCD DMA channel already reserved\n");
2243 BUG();
2244 return -EBUSY;
2245 }
2246 lcd_dma.reserved = 1;
2247 spin_unlock_irq(&lcd_dma.lock);
2248 lcd_dma.callback = callback;
2249 lcd_dma.cb_data = data;
2250 lcd_dma.active = 0;
2251 lcd_dma.single_transfer = 0;
2252 lcd_dma.rotate = 0;
2253 lcd_dma.vxres = 0;
2254 lcd_dma.mirror = 0;
2255 lcd_dma.xscale = 0;
2256 lcd_dma.yscale = 0;
2257 lcd_dma.ext_ctrl = 0;
2258 lcd_dma.src_port = 0;
2259
2260 return 0;
2261}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002262EXPORT_SYMBOL(omap_request_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002263
2264void omap_free_lcd_dma(void)
2265{
2266 spin_lock(&lcd_dma.lock);
2267 if (!lcd_dma.reserved) {
2268 spin_unlock(&lcd_dma.lock);
2269 printk(KERN_ERR "LCD DMA is not reserved\n");
2270 BUG();
2271 return;
2272 }
2273 if (!enable_1510_mode)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002274 omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1,
2275 OMAP1610_DMA_LCD_CCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002276 lcd_dma.reserved = 0;
2277 spin_unlock(&lcd_dma.lock);
2278}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002279EXPORT_SYMBOL(omap_free_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002280
2281void omap_enable_lcd_dma(void)
2282{
2283 u16 w;
2284
Tony Lindgren97b7f712008-07-03 12:24:37 +03002285 /*
2286 * Set the Enable bit only if an external controller is
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002287 * connected. Otherwise the OMAP internal controller will
2288 * start the transfer when it gets enabled.
2289 */
2290 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2291 return;
Tony Lindgrenbb13b5fd2005-07-10 19:58:18 +01002292
2293 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2294 w |= 1 << 8;
2295 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2296
Tony Lindgren92105bb2005-09-07 17:20:26 +01002297 lcd_dma.active = 1;
2298
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002299 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2300 w |= 1 << 7;
2301 omap_writew(w, OMAP1610_DMA_LCD_CCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002302}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002303EXPORT_SYMBOL(omap_enable_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002304
2305void omap_setup_lcd_dma(void)
2306{
2307 BUG_ON(lcd_dma.active);
2308 if (!enable_1510_mode) {
2309 /* Set some reasonable defaults */
2310 omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
2311 omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
2312 omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
2313 }
2314 set_b1_regs();
2315 if (!enable_1510_mode) {
2316 u16 w;
2317
2318 w = omap_readw(OMAP1610_DMA_LCD_CCR);
Tony Lindgren97b7f712008-07-03 12:24:37 +03002319 /*
2320 * If DMA was already active set the end_prog bit to have
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002321 * the programmed register set loaded into the active
2322 * register set.
2323 */
2324 w |= 1 << 11; /* End_prog */
2325 if (!lcd_dma.single_transfer)
Tony Lindgren97b7f712008-07-03 12:24:37 +03002326 w |= (3 << 8); /* Auto_init, repeat */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002327 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2328 }
2329}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002330EXPORT_SYMBOL(omap_setup_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002331
2332void omap_stop_lcd_dma(void)
2333{
Tony Lindgrenbb13b5fd2005-07-10 19:58:18 +01002334 u16 w;
2335
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002336 lcd_dma.active = 0;
Tony Lindgrenbb13b5fd2005-07-10 19:58:18 +01002337 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2338 return;
2339
2340 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2341 w &= ~(1 << 7);
2342 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2343
2344 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2345 w &= ~(1 << 8);
2346 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002347}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002348EXPORT_SYMBOL(omap_stop_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002349
Tero Kristof2d11852008-08-28 13:13:31 +00002350void omap_dma_global_context_save(void)
2351{
2352 omap_dma_global_context.dma_irqenable_l0 =
2353 dma_read(IRQENABLE_L0);
2354 omap_dma_global_context.dma_ocp_sysconfig =
2355 dma_read(OCP_SYSCONFIG);
2356 omap_dma_global_context.dma_gcr = dma_read(GCR);
2357}
2358
2359void omap_dma_global_context_restore(void)
2360{
Tero Kristof2d11852008-08-28 13:13:31 +00002361 dma_write(omap_dma_global_context.dma_gcr, GCR);
2362 dma_write(omap_dma_global_context.dma_ocp_sysconfig,
2363 OCP_SYSCONFIG);
2364 dma_write(omap_dma_global_context.dma_irqenable_l0,
2365 IRQENABLE_L0);
Tero Kristof2d11852008-08-28 13:13:31 +00002366
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002367 /*
2368 * A bug in ROM code leaves IRQ status for channels 0 and 1 uncleared
2369 * after secure sram context save and restore. Hence we need to
2370 * manually clear those IRQs to avoid spurious interrupts. This
2371 * affects only secure devices.
2372 */
2373 if (cpu_is_omap34xx() && (omap_type() != OMAP2_DEVICE_TYPE_GP))
2374 dma_write(0x3 , IRQSTATUS_L0);
Tero Kristof2d11852008-08-28 13:13:31 +00002375}
2376
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002377/*----------------------------------------------------------------------------*/
Tony Lindgrenbb13b5fd2005-07-10 19:58:18 +01002378
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002379static int __init omap_init_dma(void)
2380{
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002381 unsigned long base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002382 int ch, r;
2383
Tony Lindgren0499bde2008-07-03 12:24:36 +03002384 if (cpu_class_is_omap1()) {
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002385 base = OMAP1_DMA_BASE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002386 dma_lch_count = OMAP1_LOGICAL_DMA_CH_COUNT;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002387 } else if (cpu_is_omap24xx()) {
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002388 base = OMAP24XX_DMA4_BASE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002389 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002390 } else if (cpu_is_omap34xx()) {
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002391 base = OMAP34XX_DMA4_BASE;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002392 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
Santosh Shilimkar44169072009-05-28 14:16:04 -07002393 } else if (cpu_is_omap44xx()) {
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002394 base = OMAP44XX_DMA4_BASE;
Santosh Shilimkar44169072009-05-28 14:16:04 -07002395 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002396 } else {
2397 pr_err("DMA init failed for unsupported omap\n");
2398 return -ENODEV;
2399 }
Tony Lindgren4d963722008-07-03 12:24:31 +03002400
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002401 omap_dma_base = ioremap(base, SZ_4K);
2402 BUG_ON(!omap_dma_base);
2403
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002404 if (cpu_class_is_omap2() && omap_dma_reserve_channels
2405 && (omap_dma_reserve_channels <= dma_lch_count))
2406 dma_lch_count = omap_dma_reserve_channels;
2407
Tony Lindgren4d963722008-07-03 12:24:31 +03002408 dma_chan = kzalloc(sizeof(struct omap_dma_lch) * dma_lch_count,
2409 GFP_KERNEL);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002410 if (!dma_chan) {
2411 r = -ENOMEM;
2412 goto out_unmap;
2413 }
Tony Lindgren4d963722008-07-03 12:24:31 +03002414
2415 if (cpu_class_is_omap2()) {
2416 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2417 dma_lch_count, GFP_KERNEL);
2418 if (!dma_linked_lch) {
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002419 r = -ENOMEM;
2420 goto out_free;
Tony Lindgren4d963722008-07-03 12:24:31 +03002421 }
2422 }
2423
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002424 if (cpu_is_omap15xx()) {
2425 printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002426 dma_chan_count = 9;
2427 enable_1510_mode = 1;
Zebediah C. McClure557096f2009-03-23 18:07:44 -07002428 } else if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002429 printk(KERN_INFO "OMAP DMA hardware version %d\n",
Tony Lindgren0499bde2008-07-03 12:24:36 +03002430 dma_read(HW_ID));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002431 printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
Tony Lindgren0499bde2008-07-03 12:24:36 +03002432 (dma_read(CAPS_0_U) << 16) |
2433 dma_read(CAPS_0_L),
2434 (dma_read(CAPS_1_U) << 16) |
2435 dma_read(CAPS_1_L),
2436 dma_read(CAPS_2), dma_read(CAPS_3),
2437 dma_read(CAPS_4));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002438 if (!enable_1510_mode) {
2439 u16 w;
2440
2441 /* Disable OMAP 3.0/3.1 compatibility mode. */
Tony Lindgren0499bde2008-07-03 12:24:36 +03002442 w = dma_read(GSCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002443 w |= 1 << 3;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002444 dma_write(w, GSCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002445 dma_chan_count = 16;
2446 } else
2447 dma_chan_count = 9;
Imre Deakb5beef52006-09-25 12:41:28 +03002448 if (cpu_is_omap16xx()) {
2449 u16 w;
2450
2451 /* this would prevent OMAP sleep */
2452 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2453 w &= ~(1 << 8);
2454 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2455 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002456 } else if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03002457 u8 revision = dma_read(REVISION) & 0xff;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002458 printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
2459 revision >> 4, revision & 0xf);
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002460 dma_chan_count = dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002461 } else {
2462 dma_chan_count = 0;
2463 return 0;
2464 }
2465
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002466 spin_lock_init(&lcd_dma.lock);
2467 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002468
2469 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002470 omap_clear_dma(ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002471 dma_chan[ch].dev_id = -1;
2472 dma_chan[ch].next_lch = -1;
2473
2474 if (ch >= 6 && enable_1510_mode)
2475 continue;
2476
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002477 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002478 /*
2479 * request_irq() doesn't like dev_id (ie. ch) being
2480 * zero, so we have to kludge around this.
2481 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002482 r = request_irq(omap1_dma_irq[ch],
2483 omap1_dma_irq_handler, 0, "DMA",
2484 (void *) (ch + 1));
2485 if (r != 0) {
2486 int i;
2487
2488 printk(KERN_ERR "unable to request IRQ %d "
2489 "for DMA (error %d)\n",
2490 omap1_dma_irq[ch], r);
2491 for (i = 0; i < ch; i++)
2492 free_irq(omap1_dma_irq[i],
2493 (void *) (i + 1));
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002494 goto out_free;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002495 }
2496 }
2497 }
2498
Santosh Shilimkar44169072009-05-28 14:16:04 -07002499 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002500 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2501 DMA_DEFAULT_FIFO_DEPTH, 0);
2502
Santosh Shilimkar44169072009-05-28 14:16:04 -07002503 if (cpu_class_is_omap2()) {
2504 int irq;
2505 if (cpu_is_omap44xx())
2506 irq = INT_44XX_SDMA_IRQ0;
2507 else
2508 irq = INT_24XX_SDMA_IRQ0;
2509 setup_irq(irq, &omap24xx_dma_irq);
2510 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002511
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002512 if (cpu_is_omap34xx()) {
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002513 /* Enable smartidle idlemodes and autoidle */
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002514 u32 v = dma_read(OCP_SYSCONFIG);
2515 v &= ~(DMA_SYSCONFIG_MIDLEMODE_MASK |
2516 DMA_SYSCONFIG_SIDLEMODE_MASK |
2517 DMA_SYSCONFIG_AUTOIDLE);
2518 v |= (DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
2519 DMA_SYSCONFIG_SIDLEMODE(DMA_IDLEMODE_SMARTIDLE) |
2520 DMA_SYSCONFIG_AUTOIDLE);
2521 dma_write(v , OCP_SYSCONFIG);
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002522 /* reserve dma channels 0 and 1 in high security devices */
2523 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
2524 printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
2525 "HS ROM code\n");
2526 dma_chan[0].dev_id = 0;
2527 dma_chan[1].dev_id = 1;
2528 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002529 }
2530
2531
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002532 /* FIXME: Update LCD DMA to work on 24xx */
2533 if (cpu_class_is_omap1()) {
2534 r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0,
2535 "LCD DMA", NULL);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002536 if (r != 0) {
2537 int i;
2538
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002539 printk(KERN_ERR "unable to request IRQ for LCD DMA "
2540 "(error %d)\n", r);
2541 for (i = 0; i < dma_chan_count; i++)
2542 free_irq(omap1_dma_irq[i], (void *) (i + 1));
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002543 goto out_free;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002544 }
2545 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002546
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002547 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002548
2549out_free:
2550 kfree(dma_chan);
2551
2552out_unmap:
2553 iounmap(omap_dma_base);
2554
2555 return r;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002556}
2557
2558arch_initcall(omap_init_dma);
2559
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002560/*
2561 * Reserve the omap SDMA channels using cmdline bootarg
2562 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2563 */
2564static int __init omap_dma_cmdline_reserve_ch(char *str)
2565{
2566 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2567 omap_dma_reserve_channels = 0;
2568 return 1;
2569}
2570
2571__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2572
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002573