blob: fac8e994f58896ff991feab65ab75601d5ef2f1d [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 *
13 * Support functions for the OMAP internal DMA channels.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 *
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/sched.h>
24#include <linux/spinlock.h>
25#include <linux/errno.h>
26#include <linux/interrupt.h>
Thomas Gleixner418ca1f02006-07-01 22:32:41 +010027#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030028#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
30#include <asm/system.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010031#include <asm/hardware.h>
32#include <asm/dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010033
34#include <asm/arch/tc.h>
35
Anand Gadiyarf8151e52007-12-01 12:14:11 -080036#undef DEBUG
37
38#ifndef CONFIG_ARCH_OMAP1
39enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
40 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
41};
42
43enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000044#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010045
Tony Lindgren97b7f712008-07-03 12:24:37 +030046#define OMAP_DMA_ACTIVE 0x01
47#define OMAP_DMA_CCR_EN (1 << 7)
Tony Lindgren7ff879d2006-06-26 16:16:15 -070048#define OMAP2_DMA_CSR_CLEAR_MASK 0xffe
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010049
Tony Lindgren97b7f712008-07-03 12:24:37 +030050#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010051
Tony Lindgren97b7f712008-07-03 12:24:37 +030052static int enable_1510_mode;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010053
54struct omap_dma_lch {
55 int next_lch;
56 int dev_id;
57 u16 saved_csr;
58 u16 enabled_irqs;
59 const char *dev_name;
Tony Lindgren97b7f712008-07-03 12:24:37 +030060 void (*callback)(int lch, u16 ch_status, void *data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010061 void *data;
Anand Gadiyarf8151e52007-12-01 12:14:11 -080062
63#ifndef CONFIG_ARCH_OMAP1
64 /* required for Dynamic chaining */
65 int prev_linked_ch;
66 int next_linked_ch;
67 int state;
68 int chain_id;
69
70 int status;
71#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010072 long flags;
73};
74
Anand Gadiyarf8151e52007-12-01 12:14:11 -080075struct dma_link_info {
76 int *linked_dmach_q;
77 int no_of_lchs_linked;
78
79 int q_count;
80 int q_tail;
81 int q_head;
82
83 int chain_state;
84 int chain_mode;
85
86};
87
Tony Lindgren4d963722008-07-03 12:24:31 +030088static struct dma_link_info *dma_linked_lch;
89
90#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080091
92/* Chain handling macros */
93#define OMAP_DMA_CHAIN_QINIT(chain_id) \
94 do { \
95 dma_linked_lch[chain_id].q_head = \
96 dma_linked_lch[chain_id].q_tail = \
97 dma_linked_lch[chain_id].q_count = 0; \
98 } while (0)
99#define OMAP_DMA_CHAIN_QFULL(chain_id) \
100 (dma_linked_lch[chain_id].no_of_lchs_linked == \
101 dma_linked_lch[chain_id].q_count)
102#define OMAP_DMA_CHAIN_QLAST(chain_id) \
103 do { \
104 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
105 dma_linked_lch[chain_id].q_count) \
106 } while (0)
107#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
108 (0 == dma_linked_lch[chain_id].q_count)
109#define __OMAP_DMA_CHAIN_INCQ(end) \
110 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
111#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
112 do { \
113 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
114 dma_linked_lch[chain_id].q_count--; \
115 } while (0)
116
117#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
118 do { \
119 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
120 dma_linked_lch[chain_id].q_count++; \
121 } while (0)
122#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300123
124static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125static int dma_chan_count;
126
127static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300128static struct omap_dma_lch *dma_chan;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300129static void __iomem *omap_dma_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130
Tony Lindgren4d963722008-07-03 12:24:31 +0300131static const u8 omap1_dma_irq[OMAP1_LOGICAL_DMA_CH_COUNT] = {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132 INT_DMA_CH0_6, INT_DMA_CH1_7, INT_DMA_CH2_8, INT_DMA_CH3,
133 INT_DMA_CH4, INT_DMA_CH5, INT_1610_DMA_CH6, INT_1610_DMA_CH7,
134 INT_1610_DMA_CH8, INT_1610_DMA_CH9, INT_1610_DMA_CH10,
135 INT_1610_DMA_CH11, INT_1610_DMA_CH12, INT_1610_DMA_CH13,
136 INT_1610_DMA_CH14, INT_1610_DMA_CH15, INT_DMA_LCD
137};
138
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800139static inline void disable_lnk(int lch);
140static void omap_disable_channel_irq(int lch);
141static inline void omap_enable_channel_irq(int lch);
142
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000143#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800144 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000145
Tony Lindgren0499bde2008-07-03 12:24:36 +0300146#define dma_read(reg) \
147({ \
148 u32 __val; \
149 if (cpu_class_is_omap1()) \
150 __val = __raw_readw(omap_dma_base + OMAP1_DMA_##reg); \
151 else \
152 __val = __raw_readl(omap_dma_base + OMAP_DMA4_##reg); \
153 __val; \
154})
155
156#define dma_write(val, reg) \
157({ \
158 if (cpu_class_is_omap1()) \
159 __raw_writew((u16)(val), omap_dma_base + OMAP1_DMA_##reg); \
160 else \
161 __raw_writel((val), omap_dma_base + OMAP_DMA4_##reg); \
162})
163
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000164#ifdef CONFIG_ARCH_OMAP15XX
165/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
166int omap_dma_in_1510_mode(void)
167{
168 return enable_1510_mode;
169}
170#else
171#define omap_dma_in_1510_mode() 0
172#endif
173
174#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100175static inline int get_gdma_dev(int req)
176{
177 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
178 int shift = ((req - 1) % 5) * 6;
179
180 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
181}
182
183static inline void set_gdma_dev(int req, int dev)
184{
185 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
186 int shift = ((req - 1) % 5) * 6;
187 u32 l;
188
189 l = omap_readl(reg);
190 l &= ~(0x3f << shift);
191 l |= (dev - 1) << shift;
192 omap_writel(l, reg);
193}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000194#else
195#define set_gdma_dev(req, dev) do {} while (0)
196#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100197
Tony Lindgren0499bde2008-07-03 12:24:36 +0300198/* Omap1 only */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100199static void clear_lch_regs(int lch)
200{
201 int i;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300202 void __iomem *lch_base = omap_dma_base + OMAP1_DMA_CH_BASE(lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100203
204 for (i = 0; i < 0x2c; i += 2)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300205 __raw_writew(0, lch_base + i);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100206}
207
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300208void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209{
210 unsigned long reg;
211 u32 l;
212
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300213 if (cpu_class_is_omap1()) {
214 switch (dst_port) {
215 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
216 reg = OMAP_TC_OCPT1_PRIOR;
217 break;
218 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
219 reg = OMAP_TC_OCPT2_PRIOR;
220 break;
221 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
222 reg = OMAP_TC_EMIFF_PRIOR;
223 break;
224 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
225 reg = OMAP_TC_EMIFS_PRIOR;
226 break;
227 default:
228 BUG();
229 return;
230 }
231 l = omap_readl(reg);
232 l &= ~(0xf << 8);
233 l |= (priority & 0xf) << 8;
234 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100235 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300236
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800237 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300238 u32 ccr;
239
240 ccr = dma_read(CCR(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300241 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300242 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300243 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300244 ccr &= ~(1 << 6);
245 dma_write(ccr, CCR(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300246 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100247}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300248EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249
250void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000251 int frame_count, int sync_mode,
252 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100253{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300254 u32 l;
255
256 l = dma_read(CSDP(lch));
257 l &= ~0x03;
258 l |= data_type;
259 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100260
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000261 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300262 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100263
Tony Lindgren0499bde2008-07-03 12:24:36 +0300264 ccr = dma_read(CCR(lch));
265 ccr &= ~(1 << 5);
266 if (sync_mode == OMAP_DMA_SYNC_FRAME)
267 ccr |= 1 << 5;
268 dma_write(ccr, CCR(lch));
269
270 ccr = dma_read(CCR2(lch));
271 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000272 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300273 ccr |= 1 << 2;
274 dma_write(ccr, CCR2(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000275 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100276
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800277 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300278 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100279
Tony Lindgren0499bde2008-07-03 12:24:36 +0300280 val = dma_read(CCR(lch));
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700281 val &= ~(3 << 19);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000282 if (dma_trigger > 63)
283 val |= 1 << 20;
284 if (dma_trigger > 31)
285 val |= 1 << 19;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700287 val &= ~(0x1f);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000288 val |= (dma_trigger & 0x1f);
289
290 if (sync_mode & OMAP_DMA_SYNC_FRAME)
291 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700292 else
293 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000294
295 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
296 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700297 else
298 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000299
300 if (src_or_dst_synch)
301 val |= 1 << 24; /* source synch */
302 else
303 val &= ~(1 << 24); /* dest synch */
304
Tony Lindgren0499bde2008-07-03 12:24:36 +0300305 dma_write(val, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000306 }
307
Tony Lindgren0499bde2008-07-03 12:24:36 +0300308 dma_write(elem_count, CEN(lch));
309 dma_write(frame_count, CFN(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100310}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300311EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000312
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
314{
315 u16 w;
316
317 BUG_ON(omap_dma_in_1510_mode());
318
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800319 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000320 REVISIT_24XX();
321 return;
322 }
323
Tony Lindgren0499bde2008-07-03 12:24:36 +0300324 w = dma_read(CCR2(lch));
325 w &= ~0x03;
326
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327 switch (mode) {
328 case OMAP_DMA_CONSTANT_FILL:
329 w |= 0x01;
330 break;
331 case OMAP_DMA_TRANSPARENT_COPY:
332 w |= 0x02;
333 break;
334 case OMAP_DMA_COLOR_DIS:
335 break;
336 default:
337 BUG();
338 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300339 dma_write(w, CCR2(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340
Tony Lindgren0499bde2008-07-03 12:24:36 +0300341 w = dma_read(LCH_CTRL(lch));
342 w &= ~0x0f;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 /* Default is channel type 2D */
344 if (mode) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300345 dma_write((u16)color, COLOR_L(lch));
346 dma_write((u16)(color >> 16), COLOR_U(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347 w |= 1; /* Channel type G */
348 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300349 dma_write(w, LCH_CTRL(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300351EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100352
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300353void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
354{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800355 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300356 u32 csdp;
357
358 csdp = dma_read(CSDP(lch));
359 csdp &= ~(0x3 << 16);
360 csdp |= (mode << 16);
361 dma_write(csdp, CSDP(lch));
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300362 }
363}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300364EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300365
Tony Lindgren0499bde2008-07-03 12:24:36 +0300366void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
367{
368 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
369 u32 l;
370
371 l = dma_read(LCH_CTRL(lch));
372 l &= ~0x7;
373 l |= mode;
374 dma_write(l, LCH_CTRL(lch));
375 }
376}
377EXPORT_SYMBOL(omap_set_dma_channel_mode);
378
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000379/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000381 unsigned long src_start,
382 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300384 u32 l;
385
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000386 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300387 u16 w;
388
389 w = dma_read(CSDP(lch));
390 w &= ~(0x1f << 2);
391 w |= src_port << 2;
392 dma_write(w, CSDP(lch));
Tony Lindgren97b7f712008-07-03 12:24:37 +0300393 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300394
Tony Lindgren97b7f712008-07-03 12:24:37 +0300395 l = dma_read(CCR(lch));
396 l &= ~(0x03 << 12);
397 l |= src_amode << 12;
398 dma_write(l, CCR(lch));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300399
Tony Lindgren97b7f712008-07-03 12:24:37 +0300400 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300401 dma_write(src_start >> 16, CSSA_U(lch));
402 dma_write((u16)src_start, CSSA_L(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000403 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404
Tony Lindgren97b7f712008-07-03 12:24:37 +0300405 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300406 dma_write(src_start, CSSA(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000407
Tony Lindgren97b7f712008-07-03 12:24:37 +0300408 dma_write(src_ei, CSEI(lch));
409 dma_write(src_fi, CSFI(lch));
410}
411EXPORT_SYMBOL(omap_set_dma_src_params);
412
413void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000414{
415 omap_set_dma_transfer_params(lch, params->data_type,
416 params->elem_count, params->frame_count,
417 params->sync_mode, params->trigger,
418 params->src_or_dst_synch);
419 omap_set_dma_src_params(lch, params->src_port,
420 params->src_amode, params->src_start,
421 params->src_ei, params->src_fi);
422
423 omap_set_dma_dest_params(lch, params->dst_port,
424 params->dst_amode, params->dst_start,
425 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800426 if (params->read_prio || params->write_prio)
427 omap_dma_set_prio_lch(lch, params->read_prio,
428 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300430EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431
432void omap_set_dma_src_index(int lch, int eidx, int fidx)
433{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300434 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000435 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300436
Tony Lindgren0499bde2008-07-03 12:24:36 +0300437 dma_write(eidx, CSEI(lch));
438 dma_write(fidx, CSFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100439}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300440EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100441
442void omap_set_dma_src_data_pack(int lch, int enable)
443{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300444 u32 l;
445
446 l = dma_read(CSDP(lch));
447 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000448 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300449 l |= (1 << 6);
450 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100451}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300452EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453
454void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
455{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700456 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300457 u32 l;
458
459 l = dma_read(CSDP(lch));
460 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100461
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100462 switch (burst_mode) {
463 case OMAP_DMA_DATA_BURST_DIS:
464 break;
465 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800466 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700467 burst = 0x1;
468 else
469 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100470 break;
471 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800472 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700473 burst = 0x2;
474 break;
475 }
476 /* not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477 * w |= (0x03 << 7);
478 * fall through
479 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700480 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800481 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700482 burst = 0x3;
483 break;
484 }
485 /* OMAP1 don't support burst 16
486 * fall through
487 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488 default:
489 BUG();
490 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300491
492 l |= (burst << 7);
493 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300495EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000497/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100498void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000499 unsigned long dest_start,
500 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100501{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300502 u32 l;
503
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000504 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300505 l = dma_read(CSDP(lch));
506 l &= ~(0x1f << 9);
507 l |= dest_port << 9;
508 dma_write(l, CSDP(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000509 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
Tony Lindgren0499bde2008-07-03 12:24:36 +0300511 l = dma_read(CCR(lch));
512 l &= ~(0x03 << 14);
513 l |= dest_amode << 14;
514 dma_write(l, CCR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100515
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000516 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300517 dma_write(dest_start >> 16, CDSA_U(lch));
518 dma_write(dest_start, CDSA_L(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000519 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100520
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800521 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300522 dma_write(dest_start, CDSA(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000523
Tony Lindgren0499bde2008-07-03 12:24:36 +0300524 dma_write(dst_ei, CDEI(lch));
525 dma_write(dst_fi, CDFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300527EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100528
529void omap_set_dma_dest_index(int lch, int eidx, int fidx)
530{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300531 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000532 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300533
Tony Lindgren0499bde2008-07-03 12:24:36 +0300534 dma_write(eidx, CDEI(lch));
535 dma_write(fidx, CDFI(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100536}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300537EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538
539void omap_set_dma_dest_data_pack(int lch, int enable)
540{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300541 u32 l;
542
543 l = dma_read(CSDP(lch));
544 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000545 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300546 l |= 1 << 13;
547 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300549EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100550
551void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
552{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700553 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300554 u32 l;
555
556 l = dma_read(CSDP(lch));
557 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100558
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100559 switch (burst_mode) {
560 case OMAP_DMA_DATA_BURST_DIS:
561 break;
562 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800563 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700564 burst = 0x1;
565 else
566 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567 break;
568 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800569 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700570 burst = 0x2;
571 else
572 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100573 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700574 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800575 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700576 burst = 0x3;
577 break;
578 }
579 /* OMAP1 don't support burst 16
580 * fall through
581 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582 default:
583 printk(KERN_ERR "Invalid DMA burst mode\n");
584 BUG();
585 return;
586 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300587 l |= (burst << 14);
588 dma_write(l, CSDP(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300590EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100591
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000592static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100593{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000594 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100595
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700596 /* Clear CSR */
597 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300598 status = dma_read(CSR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800599 else if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300600 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000601
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100602 /* Enable some nice interrupts. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300603 dma_write(dma_chan[lch].enabled_irqs, CICR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604}
605
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000606static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100607{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800608 if (cpu_class_is_omap2())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300609 dma_write(0, CICR(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100610}
611
612void omap_enable_dma_irq(int lch, u16 bits)
613{
614 dma_chan[lch].enabled_irqs |= bits;
615}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300616EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100617
618void omap_disable_dma_irq(int lch, u16 bits)
619{
620 dma_chan[lch].enabled_irqs &= ~bits;
621}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300622EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100623
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000624static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100625{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300626 u32 l;
627
628 l = dma_read(CLNK_CTRL(lch));
629
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000630 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300631 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100632
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000633 /* Set the ENABLE_LNK bits */
634 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300635 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800636
637#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300638 if (cpu_class_is_omap2())
639 if (dma_chan[lch].next_linked_ch != -1)
640 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800641#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300642
643 dma_write(l, CLNK_CTRL(lch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100644}
645
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000646static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100647{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300648 u32 l;
649
650 l = dma_read(CLNK_CTRL(lch));
651
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000652 /* Disable interrupts */
653 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300654 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000655 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300656 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100657 }
658
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800659 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000660 omap_disable_channel_irq(lch);
661 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300662 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000663 }
664
Tony Lindgren0499bde2008-07-03 12:24:36 +0300665 dma_write(l, CLNK_CTRL(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000666 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
667}
668
669static inline void omap2_enable_irq_lch(int lch)
670{
671 u32 val;
672
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800673 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000674 return;
675
Tony Lindgren0499bde2008-07-03 12:24:36 +0300676 val = dma_read(IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000677 val |= 1 << lch;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300678 dma_write(val, IRQENABLE_L0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679}
680
681int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300682 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100683 void *data, int *dma_ch_out)
684{
685 int ch, free_ch = -1;
686 unsigned long flags;
687 struct omap_dma_lch *chan;
688
689 spin_lock_irqsave(&dma_chan_lock, flags);
690 for (ch = 0; ch < dma_chan_count; ch++) {
691 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
692 free_ch = ch;
693 if (dev_id == 0)
694 break;
695 }
696 }
697 if (free_ch == -1) {
698 spin_unlock_irqrestore(&dma_chan_lock, flags);
699 return -EBUSY;
700 }
701 chan = dma_chan + free_ch;
702 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000703
704 if (cpu_class_is_omap1())
705 clear_lch_regs(free_ch);
706
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800707 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000708 omap_clear_dma(free_ch);
709
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100710 spin_unlock_irqrestore(&dma_chan_lock, flags);
711
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712 chan->dev_name = dev_name;
713 chan->callback = callback;
714 chan->data = data;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300715
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800716#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300717 if (cpu_class_is_omap2()) {
718 chan->chain_id = -1;
719 chan->next_linked_ch = -1;
720 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800721#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300722
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700723 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000724
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700725 if (cpu_class_is_omap1())
726 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800727 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700728 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
729 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730
731 if (cpu_is_omap16xx()) {
732 /* If the sync device is set, configure it dynamically. */
733 if (dev_id != 0) {
734 set_gdma_dev(free_ch + 1, dev_id);
735 dev_id = free_ch + 1;
736 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300737 /*
738 * Disable the 1510 compatibility mode and set the sync device
739 * id.
740 */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300741 dma_write(dev_id | (1 << 10), CCR(free_ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000742 } else if (cpu_is_omap730() || cpu_is_omap15xx()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300743 dma_write(dev_id, CCR(free_ch));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100744 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000745
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800746 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000747 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000748 omap_enable_channel_irq(free_ch);
749 /* Clear the CSR register and IRQ status register */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300750 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(free_ch));
751 dma_write(1 << free_ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000752 }
753
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754 *dma_ch_out = free_ch;
755
756 return 0;
757}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300758EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000760void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100761{
762 unsigned long flags;
763
764 spin_lock_irqsave(&dma_chan_lock, flags);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000765 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300766 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000767 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768 spin_unlock_irqrestore(&dma_chan_lock, flags);
769 return;
770 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300771
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000772 dma_chan[lch].dev_id = -1;
773 dma_chan[lch].next_lch = -1;
774 dma_chan[lch].callback = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775 spin_unlock_irqrestore(&dma_chan_lock, flags);
776
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000777 if (cpu_class_is_omap1()) {
778 /* Disable all DMA interrupts for the channel. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300779 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000780 /* Make sure the DMA transfer is stopped. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300781 dma_write(0, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000782 }
783
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800784 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000785 u32 val;
786 /* Disable interrupts */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300787 val = dma_read(IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000788 val &= ~(1 << lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300789 dma_write(val, IRQENABLE_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000790
791 /* Clear the CSR register and IRQ status register */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300792 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(lch));
793 dma_write(1 << lch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000794
795 /* Disable all DMA interrupts for the channel. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300796 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000797
798 /* Make sure the DMA transfer is stopped. */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300799 dma_write(0, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000800 omap_clear_dma(lch);
801 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100802}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300803EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100804
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800805/**
806 * @brief omap_dma_set_global_params : Set global priority settings for dma
807 *
808 * @param arb_rate
809 * @param max_fifo_depth
810 * @param tparams - Number of thereads to reserve : DMA_THREAD_RESERVE_NORM
811 * DMA_THREAD_RESERVE_ONET
812 * DMA_THREAD_RESERVE_TWOT
813 * DMA_THREAD_RESERVE_THREET
814 */
815void
816omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
817{
818 u32 reg;
819
820 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800821 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800822 return;
823 }
824
825 if (arb_rate == 0)
826 arb_rate = 1;
827
828 reg = (arb_rate & 0xff) << 16;
829 reg |= (0xff & max_fifo_depth);
830
Tony Lindgren0499bde2008-07-03 12:24:36 +0300831 dma_write(reg, GCR);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800832}
833EXPORT_SYMBOL(omap_dma_set_global_params);
834
835/**
836 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
837 *
838 * @param lch
839 * @param read_prio - Read priority
840 * @param write_prio - Write priority
841 * Both of the above can be set with one of the following values :
842 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
843 */
844int
845omap_dma_set_prio_lch(int lch, unsigned char read_prio,
846 unsigned char write_prio)
847{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300848 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800849
Tony Lindgren4d963722008-07-03 12:24:31 +0300850 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800851 printk(KERN_ERR "Invalid channel id\n");
852 return -EINVAL;
853 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300854 l = dma_read(CCR(lch));
855 l &= ~((1 << 6) | (1 << 26));
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800856 if (cpu_is_omap2430() || cpu_is_omap34xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300857 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800858 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300859 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800860
Tony Lindgren0499bde2008-07-03 12:24:36 +0300861 dma_write(l, CCR(lch));
862
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800863 return 0;
864}
865EXPORT_SYMBOL(omap_dma_set_prio_lch);
866
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000867/*
868 * Clears any DMA state so the DMA engine is ready to restart with new buffers
869 * through omap_start_dma(). Any buffers in flight are discarded.
870 */
871void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000873 unsigned long flags;
874
875 local_irq_save(flags);
876
877 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300878 u32 l;
879
880 l = dma_read(CCR(lch));
881 l &= ~OMAP_DMA_CCR_EN;
882 dma_write(l, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000883
884 /* Clear pending interrupts */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300885 l = dma_read(CSR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000886 }
887
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800888 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000889 int i;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300890 void __iomem *lch_base = omap_dma_base + OMAP_DMA4_CH_BASE(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000891 for (i = 0; i < 0x44; i += 4)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300892 __raw_writel(0, lch_base + i);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000893 }
894
895 local_irq_restore(flags);
896}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300897EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000898
899void omap_start_dma(int lch)
900{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300901 u32 l;
902
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000903 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
904 int next_lch, cur_lch;
Tony Lindgren4d963722008-07-03 12:24:31 +0300905 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000906
907 dma_chan_link_map[lch] = 1;
908 /* Set the link register of the first channel */
909 enable_lnk(lch);
910
911 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
912 cur_lch = dma_chan[lch].next_lch;
913 do {
914 next_lch = dma_chan[cur_lch].next_lch;
915
916 /* The loop case: we've been here already */
917 if (dma_chan_link_map[cur_lch])
918 break;
919 /* Mark the current channel */
920 dma_chan_link_map[cur_lch] = 1;
921
922 enable_lnk(cur_lch);
923 omap_enable_channel_irq(cur_lch);
924
925 cur_lch = next_lch;
926 } while (next_lch != -1);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800927 } else if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000928 /* Errata: Need to write lch even if not using chaining */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300929 dma_write(lch, CLNK_CTRL(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000930 }
931
932 omap_enable_channel_irq(lch);
933
Tony Lindgren0499bde2008-07-03 12:24:36 +0300934 l = dma_read(CCR(lch));
935
Tony Lindgren97b7f712008-07-03 12:24:37 +0300936 /*
937 * Errata: On ES2.0 BUFFERING disable must be set.
938 * This will always fail on ES1.0
939 */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300940 if (cpu_is_omap24xx())
941 l |= OMAP_DMA_CCR_EN;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000942
Tony Lindgren0499bde2008-07-03 12:24:36 +0300943 l |= OMAP_DMA_CCR_EN;
944 dma_write(l, CCR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000945
946 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
947}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300948EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000949
950void omap_stop_dma(int lch)
951{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300952 u32 l;
953
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000954 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
955 int next_lch, cur_lch = lch;
Tony Lindgren4d963722008-07-03 12:24:31 +0300956 char dma_chan_link_map[OMAP_DMA4_LOGICAL_DMA_CH_COUNT];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000957
958 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
959 do {
960 /* The loop case: we've been here already */
961 if (dma_chan_link_map[cur_lch])
962 break;
963 /* Mark the current channel */
964 dma_chan_link_map[cur_lch] = 1;
965
966 disable_lnk(cur_lch);
967
968 next_lch = dma_chan[cur_lch].next_lch;
969 cur_lch = next_lch;
970 } while (next_lch != -1);
971
972 return;
973 }
974
975 /* Disable all interrupts on the channel */
976 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300977 dma_write(0, CICR(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000978
Tony Lindgren0499bde2008-07-03 12:24:36 +0300979 l = dma_read(CCR(lch));
980 l &= ~OMAP_DMA_CCR_EN;
981 dma_write(l, CCR(lch));
982
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000983 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
984}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300985EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000986
987/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300988 * Allows changing the DMA callback function or data. This may be needed if
989 * the driver shares a single DMA channel for multiple dma triggers.
990 */
991int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300992 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300993 void *data)
994{
995 unsigned long flags;
996
997 if (lch < 0)
998 return -ENODEV;
999
1000 spin_lock_irqsave(&dma_chan_lock, flags);
1001 if (dma_chan[lch].dev_id == -1) {
1002 printk(KERN_ERR "DMA callback for not set for free channel\n");
1003 spin_unlock_irqrestore(&dma_chan_lock, flags);
1004 return -EINVAL;
1005 }
1006 dma_chan[lch].callback = callback;
1007 dma_chan[lch].data = data;
1008 spin_unlock_irqrestore(&dma_chan_lock, flags);
1009
1010 return 0;
1011}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001012EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001013
1014/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001015 * Returns current physical source address for the given DMA channel.
1016 * If the channel is running the caller must disable interrupts prior calling
1017 * this function and process the returned value before re-enabling interrupt to
1018 * prevent races with the interrupt handler. Note that in continuous mode there
1019 * is a chance for CSSA_L register overflow inbetween the two reads resulting
1020 * in incorrect return value.
1021 */
1022dma_addr_t omap_get_dma_src_pos(int lch)
1023{
Tony Lindgren0695de32007-05-07 18:24:14 -07001024 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001025
Tony Lindgren0499bde2008-07-03 12:24:36 +03001026 if (cpu_is_omap15xx())
1027 offset = dma_read(CPC(lch));
1028 else
1029 offset = dma_read(CSAC(lch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001030
Tony Lindgren0499bde2008-07-03 12:24:36 +03001031 /*
1032 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1033 * read before the DMA controller finished disabling the channel.
1034 */
1035 if (!cpu_is_omap15xx() && offset == 0)
1036 offset = dma_read(CSAC(lch));
1037
1038 if (cpu_class_is_omap1())
1039 offset |= (dma_read(CSSA_U(lch)) << 16);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001040
1041 return offset;
1042}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001043EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001044
1045/*
1046 * Returns current physical destination 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 CDSA_L register overflow inbetween the two reads resulting
1051 * in incorrect return value.
1052 */
1053dma_addr_t omap_get_dma_dst_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(CDAC(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(CDAC(lch));
1068
1069 if (cpu_class_is_omap1())
1070 offset |= (dma_read(CDSA_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_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001075
Tony Lindgren0499bde2008-07-03 12:24:36 +03001076int omap_get_dma_active_status(int lch)
1077{
1078 return (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN) != 0;
1079}
1080EXPORT_SYMBOL(omap_get_dma_active_status);
1081
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001082int omap_dma_running(void)
1083{
1084 int lch;
1085
1086 /* Check if LCD DMA is running */
1087 if (cpu_is_omap16xx())
1088 if (omap_readw(OMAP1610_DMA_LCD_CCR) & OMAP_DMA_CCR_EN)
1089 return 1;
1090
1091 for (lch = 0; lch < dma_chan_count; lch++)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001092 if (dma_read(CCR(lch)) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001093 return 1;
1094
1095 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001096}
1097
1098/*
1099 * lch_queue DMA will start right after lch_head one is finished.
1100 * For this DMA link to start, you still need to start (see omap_start_dma)
1101 * the first one. That will fire up the entire queue.
1102 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001103void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001104{
1105 if (omap_dma_in_1510_mode()) {
1106 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1107 BUG();
1108 return;
1109 }
1110
1111 if ((dma_chan[lch_head].dev_id == -1) ||
1112 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001113 printk(KERN_ERR "omap_dma: trying to link "
1114 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001115 dump_stack();
1116 }
1117
1118 dma_chan[lch_head].next_lch = lch_queue;
1119}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001120EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001121
1122/*
1123 * Once the DMA queue is stopped, we can destroy it.
1124 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001125void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001126{
1127 if (omap_dma_in_1510_mode()) {
1128 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1129 BUG();
1130 return;
1131 }
1132
1133 if (dma_chan[lch_head].next_lch != lch_queue ||
1134 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001135 printk(KERN_ERR "omap_dma: trying to unlink "
1136 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001137 dump_stack();
1138 }
1139
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
1141 (dma_chan[lch_head].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001142 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1143 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001144 dump_stack();
1145 }
1146
1147 dma_chan[lch_head].next_lch = -1;
1148}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001149EXPORT_SYMBOL(omap_dma_unlink_lch);
1150
1151/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001152
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001153#ifndef CONFIG_ARCH_OMAP1
1154/* Create chain of DMA channesls */
1155static void create_dma_lch_chain(int lch_head, int lch_queue)
1156{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001157 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001158
1159 /* Check if this is the first link in chain */
1160 if (dma_chan[lch_head].next_linked_ch == -1) {
1161 dma_chan[lch_head].next_linked_ch = lch_queue;
1162 dma_chan[lch_head].prev_linked_ch = lch_queue;
1163 dma_chan[lch_queue].next_linked_ch = lch_head;
1164 dma_chan[lch_queue].prev_linked_ch = lch_head;
1165 }
1166
1167 /* a link exists, link the new channel in circular chain */
1168 else {
1169 dma_chan[lch_queue].next_linked_ch =
1170 dma_chan[lch_head].next_linked_ch;
1171 dma_chan[lch_queue].prev_linked_ch = lch_head;
1172 dma_chan[lch_head].next_linked_ch = lch_queue;
1173 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1174 lch_queue;
1175 }
1176
Tony Lindgren0499bde2008-07-03 12:24:36 +03001177 l = dma_read(CLNK_CTRL(lch_head));
1178 l &= ~(0x1f);
1179 l |= lch_queue;
1180 dma_write(l, CLNK_CTRL(lch_head));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001181
Tony Lindgren0499bde2008-07-03 12:24:36 +03001182 l = dma_read(CLNK_CTRL(lch_queue));
1183 l &= ~(0x1f);
1184 l |= (dma_chan[lch_queue].next_linked_ch);
1185 dma_write(l, CLNK_CTRL(lch_queue));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001186}
1187
1188/**
1189 * @brief omap_request_dma_chain : Request a chain of DMA channels
1190 *
1191 * @param dev_id - Device id using the dma channel
1192 * @param dev_name - Device name
1193 * @param callback - Call back function
1194 * @chain_id -
1195 * @no_of_chans - Number of channels requested
1196 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1197 * OMAP_DMA_DYNAMIC_CHAIN
1198 * @params - Channel parameters
1199 *
1200 * @return - Succes : 0
1201 * Failure: -EINVAL/-ENOMEM
1202 */
1203int omap_request_dma_chain(int dev_id, const char *dev_name,
1204 void (*callback) (int chain_id, u16 ch_status,
1205 void *data),
1206 int *chain_id, int no_of_chans, int chain_mode,
1207 struct omap_dma_channel_params params)
1208{
1209 int *channels;
1210 int i, err;
1211
1212 /* Is the chain mode valid ? */
1213 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1214 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1215 printk(KERN_ERR "Invalid chain mode requested\n");
1216 return -EINVAL;
1217 }
1218
1219 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001220 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001221 printk(KERN_ERR "Invalid Number of channels requested\n");
1222 return -EINVAL;
1223 }
1224
1225 /* Allocate a queue to maintain the status of the channels
1226 * in the chain */
1227 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1228 if (channels == NULL) {
1229 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1230 return -ENOMEM;
1231 }
1232
1233 /* request and reserve DMA channels for the chain */
1234 for (i = 0; i < no_of_chans; i++) {
1235 err = omap_request_dma(dev_id, dev_name,
1236 callback, 0, &channels[i]);
1237 if (err < 0) {
1238 int j;
1239 for (j = 0; j < i; j++)
1240 omap_free_dma(channels[j]);
1241 kfree(channels);
1242 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1243 return err;
1244 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001245 dma_chan[channels[i]].prev_linked_ch = -1;
1246 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1247
1248 /*
1249 * Allowing client drivers to set common parameters now,
1250 * so that later only relevant (src_start, dest_start
1251 * and element count) can be set
1252 */
1253 omap_set_dma_params(channels[i], &params);
1254 }
1255
1256 *chain_id = channels[0];
1257 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1258 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1259 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1260 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1261
1262 for (i = 0; i < no_of_chans; i++)
1263 dma_chan[channels[i]].chain_id = *chain_id;
1264
1265 /* Reset the Queue pointers */
1266 OMAP_DMA_CHAIN_QINIT(*chain_id);
1267
1268 /* Set up the chain */
1269 if (no_of_chans == 1)
1270 create_dma_lch_chain(channels[0], channels[0]);
1271 else {
1272 for (i = 0; i < (no_of_chans - 1); i++)
1273 create_dma_lch_chain(channels[i], channels[i + 1]);
1274 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001275
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001276 return 0;
1277}
1278EXPORT_SYMBOL(omap_request_dma_chain);
1279
1280/**
1281 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1282 * params after setting it. Dont do this while dma is running!!
1283 *
1284 * @param chain_id - Chained logical channel id.
1285 * @param params
1286 *
1287 * @return - Success : 0
1288 * Failure : -EINVAL
1289 */
1290int omap_modify_dma_chain_params(int chain_id,
1291 struct omap_dma_channel_params params)
1292{
1293 int *channels;
1294 u32 i;
1295
1296 /* Check for input params */
1297 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001298 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001299 printk(KERN_ERR "Invalid chain id\n");
1300 return -EINVAL;
1301 }
1302
1303 /* Check if the chain exists */
1304 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1305 printk(KERN_ERR "Chain doesn't exists\n");
1306 return -EINVAL;
1307 }
1308 channels = dma_linked_lch[chain_id].linked_dmach_q;
1309
1310 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1311 /*
1312 * Allowing client drivers to set common parameters now,
1313 * so that later only relevant (src_start, dest_start
1314 * and element count) can be set
1315 */
1316 omap_set_dma_params(channels[i], &params);
1317 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001318
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001319 return 0;
1320}
1321EXPORT_SYMBOL(omap_modify_dma_chain_params);
1322
1323/**
1324 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1325 *
1326 * @param chain_id
1327 *
1328 * @return - Success : 0
1329 * Failure : -EINVAL
1330 */
1331int omap_free_dma_chain(int chain_id)
1332{
1333 int *channels;
1334 u32 i;
1335
1336 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001337 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001338 printk(KERN_ERR "Invalid chain id\n");
1339 return -EINVAL;
1340 }
1341
1342 /* Check if the chain exists */
1343 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1344 printk(KERN_ERR "Chain doesn't exists\n");
1345 return -EINVAL;
1346 }
1347
1348 channels = dma_linked_lch[chain_id].linked_dmach_q;
1349 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1350 dma_chan[channels[i]].next_linked_ch = -1;
1351 dma_chan[channels[i]].prev_linked_ch = -1;
1352 dma_chan[channels[i]].chain_id = -1;
1353 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1354 omap_free_dma(channels[i]);
1355 }
1356
1357 kfree(channels);
1358
1359 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1360 dma_linked_lch[chain_id].chain_mode = -1;
1361 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001362
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001363 return (0);
1364}
1365EXPORT_SYMBOL(omap_free_dma_chain);
1366
1367/**
1368 * @brief omap_dma_chain_status - Check if the chain is in
1369 * active / inactive state.
1370 * @param chain_id
1371 *
1372 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1373 * Failure : -EINVAL
1374 */
1375int omap_dma_chain_status(int chain_id)
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 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1389 dma_linked_lch[chain_id].q_count);
1390
1391 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1392 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001393
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001394 return OMAP_DMA_CHAIN_ACTIVE;
1395}
1396EXPORT_SYMBOL(omap_dma_chain_status);
1397
1398/**
1399 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1400 * set the params and start the transfer.
1401 *
1402 * @param chain_id
1403 * @param src_start - buffer start address
1404 * @param dest_start - Dest address
1405 * @param elem_count
1406 * @param frame_count
1407 * @param callbk_data - channel callback parameter data.
1408 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301409 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001410 * Failure: -EINVAL/-EBUSY
1411 */
1412int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1413 int elem_count, int frame_count, void *callbk_data)
1414{
1415 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001416 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001417 int start_dma = 0;
1418
Tony Lindgren97b7f712008-07-03 12:24:37 +03001419 /*
1420 * if buffer size is less than 1 then there is
1421 * no use of starting the chain
1422 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001423 if (elem_count < 1) {
1424 printk(KERN_ERR "Invalid buffer size\n");
1425 return -EINVAL;
1426 }
1427
1428 /* Check for input params */
1429 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001430 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001431 printk(KERN_ERR "Invalid chain id\n");
1432 return -EINVAL;
1433 }
1434
1435 /* Check if the chain exists */
1436 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1437 printk(KERN_ERR "Chain doesn't exist\n");
1438 return -EINVAL;
1439 }
1440
1441 /* Check if all the channels in chain are in use */
1442 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1443 return -EBUSY;
1444
1445 /* Frame count may be negative in case of indexed transfers */
1446 channels = dma_linked_lch[chain_id].linked_dmach_q;
1447
1448 /* Get a free channel */
1449 lch = channels[dma_linked_lch[chain_id].q_tail];
1450
1451 /* Store the callback data */
1452 dma_chan[lch].data = callbk_data;
1453
1454 /* Increment the q_tail */
1455 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1456
1457 /* Set the params to the free channel */
1458 if (src_start != 0)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001459 dma_write(src_start, CSSA(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001460 if (dest_start != 0)
Tony Lindgren0499bde2008-07-03 12:24:36 +03001461 dma_write(dest_start, CDSA(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001462
1463 /* Write the buffer size */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001464 dma_write(elem_count, CEN(lch));
1465 dma_write(frame_count, CFN(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001466
Tony Lindgren97b7f712008-07-03 12:24:37 +03001467 /*
1468 * If the chain is dynamically linked,
1469 * then we may have to start the chain if its not active
1470 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001471 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1472
Tony Lindgren97b7f712008-07-03 12:24:37 +03001473 /*
1474 * In Dynamic chain, if the chain is not started,
1475 * queue the channel
1476 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001477 if (dma_linked_lch[chain_id].chain_state ==
1478 DMA_CHAIN_NOTSTARTED) {
1479 /* Enable the link in previous channel */
1480 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1481 DMA_CH_QUEUED)
1482 enable_lnk(dma_chan[lch].prev_linked_ch);
1483 dma_chan[lch].state = DMA_CH_QUEUED;
1484 }
1485
Tony Lindgren97b7f712008-07-03 12:24:37 +03001486 /*
1487 * Chain is already started, make sure its active,
1488 * if not then start the chain
1489 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001490 else {
1491 start_dma = 1;
1492
1493 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1494 DMA_CH_STARTED) {
1495 enable_lnk(dma_chan[lch].prev_linked_ch);
1496 dma_chan[lch].state = DMA_CH_QUEUED;
1497 start_dma = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001498 if (0 == ((1 << 7) & dma_read(
1499 CCR(dma_chan[lch].prev_linked_ch)))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001500 disable_lnk(dma_chan[lch].
1501 prev_linked_ch);
1502 pr_debug("\n prev ch is stopped\n");
1503 start_dma = 1;
1504 }
1505 }
1506
1507 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1508 == DMA_CH_QUEUED) {
1509 enable_lnk(dma_chan[lch].prev_linked_ch);
1510 dma_chan[lch].state = DMA_CH_QUEUED;
1511 start_dma = 0;
1512 }
1513 omap_enable_channel_irq(lch);
1514
Tony Lindgren0499bde2008-07-03 12:24:36 +03001515 l = dma_read(CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001516
Tony Lindgren0499bde2008-07-03 12:24:36 +03001517 if ((0 == (l & (1 << 24))))
1518 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001519 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001520 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001521 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001522 if (0 == (l & (1 << 7))) {
1523 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001524 dma_chan[lch].state = DMA_CH_STARTED;
1525 pr_debug("starting %d\n", lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001526 dma_write(l, CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001527 } else
1528 start_dma = 0;
1529 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001530 if (0 == (l & (1 << 7)))
1531 dma_write(l, CCR(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001532 }
1533 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1534 }
1535 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001536
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301537 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001538}
1539EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1540
1541/**
1542 * @brief omap_start_dma_chain_transfers - Start the chain
1543 *
1544 * @param chain_id
1545 *
1546 * @return - Success : 0
1547 * Failure : -EINVAL/-EBUSY
1548 */
1549int omap_start_dma_chain_transfers(int chain_id)
1550{
1551 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001552 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001553
Tony Lindgren4d963722008-07-03 12:24:31 +03001554 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001555 printk(KERN_ERR "Invalid chain id\n");
1556 return -EINVAL;
1557 }
1558
1559 channels = dma_linked_lch[chain_id].linked_dmach_q;
1560
1561 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1562 printk(KERN_ERR "Chain is already started\n");
1563 return -EBUSY;
1564 }
1565
1566 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1567 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1568 i++) {
1569 enable_lnk(channels[i]);
1570 omap_enable_channel_irq(channels[i]);
1571 }
1572 } else {
1573 omap_enable_channel_irq(channels[0]);
1574 }
1575
Tony Lindgren0499bde2008-07-03 12:24:36 +03001576 l = dma_read(CCR(channels[0]));
1577 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001578 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1579 dma_chan[channels[0]].state = DMA_CH_STARTED;
1580
Tony Lindgren0499bde2008-07-03 12:24:36 +03001581 if ((0 == (l & (1 << 24))))
1582 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001583 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001584 l |= (1 << 25);
1585 dma_write(l, CCR(channels[0]));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001586
1587 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001588
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001589 return 0;
1590}
1591EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1592
1593/**
1594 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1595 *
1596 * @param chain_id
1597 *
1598 * @return - Success : 0
1599 * Failure : EINVAL
1600 */
1601int omap_stop_dma_chain_transfers(int chain_id)
1602{
1603 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001604 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001605 u32 sys_cf;
1606
1607 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001608 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001609 printk(KERN_ERR "Invalid chain id\n");
1610 return -EINVAL;
1611 }
1612
1613 /* Check if the chain exists */
1614 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1615 printk(KERN_ERR "Chain doesn't exists\n");
1616 return -EINVAL;
1617 }
1618 channels = dma_linked_lch[chain_id].linked_dmach_q;
1619
Tony Lindgren97b7f712008-07-03 12:24:37 +03001620 /*
1621 * DMA Errata:
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001622 * Special programming model needed to disable DMA before end of block
1623 */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001624 sys_cf = dma_read(OCP_SYSCONFIG);
1625 l = sys_cf;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001626 /* Middle mode reg set no Standby */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001627 l &= ~((1 << 12)|(1 << 13));
1628 dma_write(l, OCP_SYSCONFIG);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001629
1630 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1631
1632 /* Stop the Channel transmission */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001633 l = dma_read(CCR(channels[i]));
1634 l &= ~(1 << 7);
1635 dma_write(l, CCR(channels[i]));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001636
1637 /* Disable the link in all the channels */
1638 disable_lnk(channels[i]);
1639 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1640
1641 }
1642 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1643
1644 /* Reset the Queue pointers */
1645 OMAP_DMA_CHAIN_QINIT(chain_id);
1646
1647 /* Errata - put in the old value */
Tony Lindgren0499bde2008-07-03 12:24:36 +03001648 dma_write(sys_cf, OCP_SYSCONFIG);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001649
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001650 return 0;
1651}
1652EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1653
1654/* Get the index of the ongoing DMA in chain */
1655/**
1656 * @brief omap_get_dma_chain_index - Get the element and frame index
1657 * of the ongoing DMA in chain
1658 *
1659 * @param chain_id
1660 * @param ei - Element index
1661 * @param fi - Frame index
1662 *
1663 * @return - Success : 0
1664 * Failure : -EINVAL
1665 */
1666int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1667{
1668 int lch;
1669 int *channels;
1670
1671 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001672 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001673 printk(KERN_ERR "Invalid chain id\n");
1674 return -EINVAL;
1675 }
1676
1677 /* Check if the chain exists */
1678 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1679 printk(KERN_ERR "Chain doesn't exists\n");
1680 return -EINVAL;
1681 }
1682 if ((!ei) || (!fi))
1683 return -EINVAL;
1684
1685 channels = dma_linked_lch[chain_id].linked_dmach_q;
1686
1687 /* Get the current channel */
1688 lch = channels[dma_linked_lch[chain_id].q_head];
1689
Tony Lindgren0499bde2008-07-03 12:24:36 +03001690 *ei = dma_read(CCEN(lch));
1691 *fi = dma_read(CCFN(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001692
1693 return 0;
1694}
1695EXPORT_SYMBOL(omap_get_dma_chain_index);
1696
1697/**
1698 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1699 * ongoing DMA in chain
1700 *
1701 * @param chain_id
1702 *
1703 * @return - Success : Destination position
1704 * Failure : -EINVAL
1705 */
1706int omap_get_dma_chain_dst_pos(int chain_id)
1707{
1708 int lch;
1709 int *channels;
1710
1711 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001712 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001713 printk(KERN_ERR "Invalid chain id\n");
1714 return -EINVAL;
1715 }
1716
1717 /* Check if the chain exists */
1718 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1719 printk(KERN_ERR "Chain doesn't exists\n");
1720 return -EINVAL;
1721 }
1722
1723 channels = dma_linked_lch[chain_id].linked_dmach_q;
1724
1725 /* Get the current channel */
1726 lch = channels[dma_linked_lch[chain_id].q_head];
1727
Tony Lindgren0499bde2008-07-03 12:24:36 +03001728 return dma_read(CDAC(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001729}
1730EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1731
1732/**
1733 * @brief omap_get_dma_chain_src_pos - Get the source position
1734 * of the ongoing DMA in chain
1735 * @param chain_id
1736 *
1737 * @return - Success : Destination position
1738 * Failure : -EINVAL
1739 */
1740int omap_get_dma_chain_src_pos(int chain_id)
1741{
1742 int lch;
1743 int *channels;
1744
1745 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001746 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001747 printk(KERN_ERR "Invalid chain id\n");
1748 return -EINVAL;
1749 }
1750
1751 /* Check if the chain exists */
1752 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1753 printk(KERN_ERR "Chain doesn't exists\n");
1754 return -EINVAL;
1755 }
1756
1757 channels = dma_linked_lch[chain_id].linked_dmach_q;
1758
1759 /* Get the current channel */
1760 lch = channels[dma_linked_lch[chain_id].q_head];
1761
Tony Lindgren0499bde2008-07-03 12:24:36 +03001762 return dma_read(CSAC(lch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001763}
1764EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001765#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001766
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001767/*----------------------------------------------------------------------------*/
1768
1769#ifdef CONFIG_ARCH_OMAP1
1770
1771static int omap1_dma_handle_ch(int ch)
1772{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001773 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001774
1775 if (enable_1510_mode && ch >= 6) {
1776 csr = dma_chan[ch].saved_csr;
1777 dma_chan[ch].saved_csr = 0;
1778 } else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001779 csr = dma_read(CSR(ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001780 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1781 dma_chan[ch + 6].saved_csr = csr >> 7;
1782 csr &= 0x7f;
1783 }
1784 if ((csr & 0x3f) == 0)
1785 return 0;
1786 if (unlikely(dma_chan[ch].dev_id == -1)) {
1787 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1788 "%d (CSR %04x)\n", ch, csr);
1789 return 0;
1790 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001791 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001792 printk(KERN_WARNING "DMA timeout with device %d\n",
1793 dma_chan[ch].dev_id);
1794 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1795 printk(KERN_WARNING "DMA synchronization event drop occurred "
1796 "with device %d\n", dma_chan[ch].dev_id);
1797 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1798 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1799 if (likely(dma_chan[ch].callback != NULL))
1800 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001801
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001802 return 1;
1803}
1804
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001805static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001806{
1807 int ch = ((int) dev_id) - 1;
1808 int handled = 0;
1809
1810 for (;;) {
1811 int handled_now = 0;
1812
1813 handled_now += omap1_dma_handle_ch(ch);
1814 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1815 handled_now += omap1_dma_handle_ch(ch + 6);
1816 if (!handled_now)
1817 break;
1818 handled += handled_now;
1819 }
1820
1821 return handled ? IRQ_HANDLED : IRQ_NONE;
1822}
1823
1824#else
1825#define omap1_dma_irq_handler NULL
1826#endif
1827
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001828#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001829
1830static int omap2_dma_handle_ch(int ch)
1831{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001832 u32 status = dma_read(CSR(ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001833
Juha Yrjola31513692006-12-06 17:13:47 -08001834 if (!status) {
1835 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001836 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1837 ch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001838 dma_write(1 << ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001839 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001840 }
1841 if (unlikely(dma_chan[ch].dev_id == -1)) {
1842 if (printk_ratelimit())
1843 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1844 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001845 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001846 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001847 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1848 printk(KERN_INFO
1849 "DMA synchronization event drop occurred with device "
1850 "%d\n", dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001851 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ))
1852 printk(KERN_INFO "DMA transaction error with device %d\n",
1853 dma_chan[ch].dev_id);
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001854 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1855 printk(KERN_INFO "DMA secure error with device %d\n",
1856 dma_chan[ch].dev_id);
1857 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1858 printk(KERN_INFO "DMA misaligned error with device %d\n",
1859 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001860
Tony Lindgren0499bde2008-07-03 12:24:36 +03001861 dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR(ch));
1862 dma_write(1 << ch, IRQSTATUS_L0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001863
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001864 /* If the ch is not chained then chain_id will be -1 */
1865 if (dma_chan[ch].chain_id != -1) {
1866 int chain_id = dma_chan[ch].chain_id;
1867 dma_chan[ch].state = DMA_CH_NOTSTARTED;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001868 if (dma_read(CLNK_CTRL(ch)) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001869 dma_chan[dma_chan[ch].next_linked_ch].state =
1870 DMA_CH_STARTED;
1871 if (dma_linked_lch[chain_id].chain_mode ==
1872 OMAP_DMA_DYNAMIC_CHAIN)
1873 disable_lnk(ch);
1874
1875 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1876 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1877
Tony Lindgren0499bde2008-07-03 12:24:36 +03001878 status = dma_read(CSR(ch));
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001879 }
1880
Jarkko Nikula538528d2008-02-13 11:47:29 +02001881 if (likely(dma_chan[ch].callback != NULL))
1882 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001883
Tony Lindgren0499bde2008-07-03 12:24:36 +03001884 dma_write(status, CSR(ch));
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001885
1886 return 0;
1887}
1888
1889/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001890static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001891{
1892 u32 val;
1893 int i;
1894
Tony Lindgren0499bde2008-07-03 12:24:36 +03001895 val = dma_read(IRQSTATUS_L0);
Juha Yrjola31513692006-12-06 17:13:47 -08001896 if (val == 0) {
1897 if (printk_ratelimit())
1898 printk(KERN_WARNING "Spurious DMA IRQ\n");
1899 return IRQ_HANDLED;
1900 }
Tony Lindgren4d963722008-07-03 12:24:31 +03001901 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001902 if (val & 1)
1903 omap2_dma_handle_ch(i);
1904 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001905 }
1906
1907 return IRQ_HANDLED;
1908}
1909
1910static struct irqaction omap24xx_dma_irq = {
1911 .name = "DMA",
1912 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001913 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001914};
1915
1916#else
1917static struct irqaction omap24xx_dma_irq;
1918#endif
1919
1920/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001921
1922static struct lcd_dma_info {
1923 spinlock_t lock;
1924 int reserved;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001925 void (*callback)(u16 status, void *data);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001926 void *cb_data;
1927
1928 int active;
1929 unsigned long addr, size;
1930 int rotate, data_type, xres, yres;
1931 int vxres;
1932 int mirror;
1933 int xscale, yscale;
1934 int ext_ctrl;
1935 int src_port;
1936 int single_transfer;
1937} lcd_dma;
1938
1939void omap_set_lcd_dma_b1(unsigned long addr, u16 fb_xres, u16 fb_yres,
1940 int data_type)
1941{
1942 lcd_dma.addr = addr;
1943 lcd_dma.data_type = data_type;
1944 lcd_dma.xres = fb_xres;
1945 lcd_dma.yres = fb_yres;
1946}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001947EXPORT_SYMBOL(omap_set_lcd_dma_b1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001948
1949void omap_set_lcd_dma_src_port(int port)
1950{
1951 lcd_dma.src_port = port;
1952}
1953
1954void omap_set_lcd_dma_ext_controller(int external)
1955{
1956 lcd_dma.ext_ctrl = external;
1957}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001958EXPORT_SYMBOL(omap_set_lcd_dma_ext_controller);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001959
1960void omap_set_lcd_dma_single_transfer(int single)
1961{
1962 lcd_dma.single_transfer = single;
1963}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001964EXPORT_SYMBOL(omap_set_lcd_dma_single_transfer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001965
1966void omap_set_lcd_dma_b1_rotation(int rotate)
1967{
1968 if (omap_dma_in_1510_mode()) {
1969 printk(KERN_ERR "DMA rotation is not supported in 1510 mode\n");
1970 BUG();
1971 return;
1972 }
1973 lcd_dma.rotate = rotate;
1974}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001975EXPORT_SYMBOL(omap_set_lcd_dma_b1_rotation);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001976
1977void omap_set_lcd_dma_b1_mirror(int mirror)
1978{
1979 if (omap_dma_in_1510_mode()) {
1980 printk(KERN_ERR "DMA mirror is not supported in 1510 mode\n");
1981 BUG();
1982 }
1983 lcd_dma.mirror = mirror;
1984}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001985EXPORT_SYMBOL(omap_set_lcd_dma_b1_mirror);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001986
1987void omap_set_lcd_dma_b1_vxres(unsigned long vxres)
1988{
1989 if (omap_dma_in_1510_mode()) {
1990 printk(KERN_ERR "DMA virtual resulotion is not supported "
1991 "in 1510 mode\n");
1992 BUG();
1993 }
1994 lcd_dma.vxres = vxres;
1995}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001996EXPORT_SYMBOL(omap_set_lcd_dma_b1_vxres);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001997
1998void omap_set_lcd_dma_b1_scale(unsigned int xscale, unsigned int yscale)
1999{
2000 if (omap_dma_in_1510_mode()) {
2001 printk(KERN_ERR "DMA scale is not supported in 1510 mode\n");
2002 BUG();
2003 }
2004 lcd_dma.xscale = xscale;
2005 lcd_dma.yscale = yscale;
2006}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002007EXPORT_SYMBOL(omap_set_lcd_dma_b1_scale);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002008
2009static void set_b1_regs(void)
2010{
2011 unsigned long top, bottom;
2012 int es;
2013 u16 w;
2014 unsigned long en, fn;
2015 long ei, fi;
2016 unsigned long vxres;
2017 unsigned int xscale, yscale;
2018
2019 switch (lcd_dma.data_type) {
2020 case OMAP_DMA_DATA_TYPE_S8:
2021 es = 1;
2022 break;
2023 case OMAP_DMA_DATA_TYPE_S16:
2024 es = 2;
2025 break;
2026 case OMAP_DMA_DATA_TYPE_S32:
2027 es = 4;
2028 break;
2029 default:
2030 BUG();
2031 return;
2032 }
2033
2034 vxres = lcd_dma.vxres ? lcd_dma.vxres : lcd_dma.xres;
2035 xscale = lcd_dma.xscale ? lcd_dma.xscale : 1;
2036 yscale = lcd_dma.yscale ? lcd_dma.yscale : 1;
2037 BUG_ON(vxres < lcd_dma.xres);
Tony Lindgren97b7f712008-07-03 12:24:37 +03002038
2039#define PIXADDR(x, y) (lcd_dma.addr + \
2040 ((y) * vxres * yscale + (x) * xscale) * es)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002041#define PIXSTEP(sx, sy, dx, dy) (PIXADDR(dx, dy) - PIXADDR(sx, sy) - es + 1)
Tony Lindgren97b7f712008-07-03 12:24:37 +03002042
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002043 switch (lcd_dma.rotate) {
2044 case 0:
2045 if (!lcd_dma.mirror) {
2046 top = PIXADDR(0, 0);
2047 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2048 /* 1510 DMA requires the bottom address to be 2 more
2049 * than the actual last memory access location. */
2050 if (omap_dma_in_1510_mode() &&
Tony Lindgren97b7f712008-07-03 12:24:37 +03002051 lcd_dma.data_type == OMAP_DMA_DATA_TYPE_S32)
2052 bottom += 2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002053 ei = PIXSTEP(0, 0, 1, 0);
2054 fi = PIXSTEP(lcd_dma.xres - 1, 0, 0, 1);
2055 } else {
2056 top = PIXADDR(lcd_dma.xres - 1, 0);
2057 bottom = PIXADDR(0, lcd_dma.yres - 1);
2058 ei = PIXSTEP(1, 0, 0, 0);
2059 fi = PIXSTEP(0, 0, lcd_dma.xres - 1, 1);
2060 }
2061 en = lcd_dma.xres;
2062 fn = lcd_dma.yres;
2063 break;
2064 case 90:
2065 if (!lcd_dma.mirror) {
2066 top = PIXADDR(0, lcd_dma.yres - 1);
2067 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2068 ei = PIXSTEP(0, 1, 0, 0);
2069 fi = PIXSTEP(0, 0, 1, lcd_dma.yres - 1);
2070 } else {
2071 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2072 bottom = PIXADDR(0, 0);
2073 ei = PIXSTEP(0, 1, 0, 0);
2074 fi = PIXSTEP(1, 0, 0, lcd_dma.yres - 1);
2075 }
2076 en = lcd_dma.yres;
2077 fn = lcd_dma.xres;
2078 break;
2079 case 180:
2080 if (!lcd_dma.mirror) {
2081 top = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2082 bottom = PIXADDR(0, 0);
2083 ei = PIXSTEP(1, 0, 0, 0);
2084 fi = PIXSTEP(0, 1, lcd_dma.xres - 1, 0);
2085 } else {
2086 top = PIXADDR(0, lcd_dma.yres - 1);
2087 bottom = PIXADDR(lcd_dma.xres - 1, 0);
2088 ei = PIXSTEP(0, 0, 1, 0);
2089 fi = PIXSTEP(lcd_dma.xres - 1, 1, 0, 0);
2090 }
2091 en = lcd_dma.xres;
2092 fn = lcd_dma.yres;
2093 break;
2094 case 270:
2095 if (!lcd_dma.mirror) {
2096 top = PIXADDR(lcd_dma.xres - 1, 0);
2097 bottom = PIXADDR(0, lcd_dma.yres - 1);
2098 ei = PIXSTEP(0, 0, 0, 1);
2099 fi = PIXSTEP(1, lcd_dma.yres - 1, 0, 0);
2100 } else {
2101 top = PIXADDR(0, 0);
2102 bottom = PIXADDR(lcd_dma.xres - 1, lcd_dma.yres - 1);
2103 ei = PIXSTEP(0, 0, 0, 1);
2104 fi = PIXSTEP(0, lcd_dma.yres - 1, 1, 0);
2105 }
2106 en = lcd_dma.yres;
2107 fn = lcd_dma.xres;
2108 break;
2109 default:
2110 BUG();
Simon Arlott6cbdc8c2007-05-11 20:40:30 +01002111 return; /* Suppress warning about uninitialized vars */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002112 }
2113
2114 if (omap_dma_in_1510_mode()) {
2115 omap_writew(top >> 16, OMAP1510_DMA_LCD_TOP_F1_U);
2116 omap_writew(top, OMAP1510_DMA_LCD_TOP_F1_L);
2117 omap_writew(bottom >> 16, OMAP1510_DMA_LCD_BOT_F1_U);
2118 omap_writew(bottom, OMAP1510_DMA_LCD_BOT_F1_L);
2119
2120 return;
2121 }
2122
2123 /* 1610 regs */
2124 omap_writew(top >> 16, OMAP1610_DMA_LCD_TOP_B1_U);
2125 omap_writew(top, OMAP1610_DMA_LCD_TOP_B1_L);
2126 omap_writew(bottom >> 16, OMAP1610_DMA_LCD_BOT_B1_U);
2127 omap_writew(bottom, OMAP1610_DMA_LCD_BOT_B1_L);
2128
2129 omap_writew(en, OMAP1610_DMA_LCD_SRC_EN_B1);
2130 omap_writew(fn, OMAP1610_DMA_LCD_SRC_FN_B1);
2131
2132 w = omap_readw(OMAP1610_DMA_LCD_CSDP);
2133 w &= ~0x03;
2134 w |= lcd_dma.data_type;
2135 omap_writew(w, OMAP1610_DMA_LCD_CSDP);
2136
2137 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2138 /* Always set the source port as SDRAM for now*/
2139 w &= ~(0x03 << 6);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002140 if (lcd_dma.callback != NULL)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002141 w |= 1 << 1; /* Block interrupt enable */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002142 else
2143 w &= ~(1 << 1);
2144 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2145
2146 if (!(lcd_dma.rotate || lcd_dma.mirror ||
2147 lcd_dma.vxres || lcd_dma.xscale || lcd_dma.yscale))
2148 return;
2149
2150 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2151 /* Set the double-indexed addressing mode */
2152 w |= (0x03 << 12);
2153 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2154
2155 omap_writew(ei, OMAP1610_DMA_LCD_SRC_EI_B1);
2156 omap_writew(fi >> 16, OMAP1610_DMA_LCD_SRC_FI_B1_U);
2157 omap_writew(fi, OMAP1610_DMA_LCD_SRC_FI_B1_L);
2158}
2159
Linus Torvalds0cd61b62006-10-06 10:53:39 -07002160static irqreturn_t lcd_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002161{
2162 u16 w;
2163
2164 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2165 if (unlikely(!(w & (1 << 3)))) {
2166 printk(KERN_WARNING "Spurious LCD DMA IRQ\n");
2167 return IRQ_NONE;
2168 }
2169 /* Ack the IRQ */
2170 w |= (1 << 3);
2171 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2172 lcd_dma.active = 0;
2173 if (lcd_dma.callback != NULL)
2174 lcd_dma.callback(w, lcd_dma.cb_data);
2175
2176 return IRQ_HANDLED;
2177}
2178
Tony Lindgren97b7f712008-07-03 12:24:37 +03002179int omap_request_lcd_dma(void (*callback)(u16 status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002180 void *data)
2181{
2182 spin_lock_irq(&lcd_dma.lock);
2183 if (lcd_dma.reserved) {
2184 spin_unlock_irq(&lcd_dma.lock);
2185 printk(KERN_ERR "LCD DMA channel already reserved\n");
2186 BUG();
2187 return -EBUSY;
2188 }
2189 lcd_dma.reserved = 1;
2190 spin_unlock_irq(&lcd_dma.lock);
2191 lcd_dma.callback = callback;
2192 lcd_dma.cb_data = data;
2193 lcd_dma.active = 0;
2194 lcd_dma.single_transfer = 0;
2195 lcd_dma.rotate = 0;
2196 lcd_dma.vxres = 0;
2197 lcd_dma.mirror = 0;
2198 lcd_dma.xscale = 0;
2199 lcd_dma.yscale = 0;
2200 lcd_dma.ext_ctrl = 0;
2201 lcd_dma.src_port = 0;
2202
2203 return 0;
2204}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002205EXPORT_SYMBOL(omap_request_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002206
2207void omap_free_lcd_dma(void)
2208{
2209 spin_lock(&lcd_dma.lock);
2210 if (!lcd_dma.reserved) {
2211 spin_unlock(&lcd_dma.lock);
2212 printk(KERN_ERR "LCD DMA is not reserved\n");
2213 BUG();
2214 return;
2215 }
2216 if (!enable_1510_mode)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002217 omap_writew(omap_readw(OMAP1610_DMA_LCD_CCR) & ~1,
2218 OMAP1610_DMA_LCD_CCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002219 lcd_dma.reserved = 0;
2220 spin_unlock(&lcd_dma.lock);
2221}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002222EXPORT_SYMBOL(omap_free_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002223
2224void omap_enable_lcd_dma(void)
2225{
2226 u16 w;
2227
Tony Lindgren97b7f712008-07-03 12:24:37 +03002228 /*
2229 * Set the Enable bit only if an external controller is
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002230 * connected. Otherwise the OMAP internal controller will
2231 * start the transfer when it gets enabled.
2232 */
2233 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2234 return;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01002235
2236 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2237 w |= 1 << 8;
2238 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2239
Tony Lindgren92105bb2005-09-07 17:20:26 +01002240 lcd_dma.active = 1;
2241
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002242 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2243 w |= 1 << 7;
2244 omap_writew(w, OMAP1610_DMA_LCD_CCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002245}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002246EXPORT_SYMBOL(omap_enable_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002247
2248void omap_setup_lcd_dma(void)
2249{
2250 BUG_ON(lcd_dma.active);
2251 if (!enable_1510_mode) {
2252 /* Set some reasonable defaults */
2253 omap_writew(0x5440, OMAP1610_DMA_LCD_CCR);
2254 omap_writew(0x9102, OMAP1610_DMA_LCD_CSDP);
2255 omap_writew(0x0004, OMAP1610_DMA_LCD_LCH_CTRL);
2256 }
2257 set_b1_regs();
2258 if (!enable_1510_mode) {
2259 u16 w;
2260
2261 w = omap_readw(OMAP1610_DMA_LCD_CCR);
Tony Lindgren97b7f712008-07-03 12:24:37 +03002262 /*
2263 * If DMA was already active set the end_prog bit to have
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002264 * the programmed register set loaded into the active
2265 * register set.
2266 */
2267 w |= 1 << 11; /* End_prog */
2268 if (!lcd_dma.single_transfer)
Tony Lindgren97b7f712008-07-03 12:24:37 +03002269 w |= (3 << 8); /* Auto_init, repeat */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002270 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2271 }
2272}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002273EXPORT_SYMBOL(omap_setup_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002274
2275void omap_stop_lcd_dma(void)
2276{
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01002277 u16 w;
2278
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002279 lcd_dma.active = 0;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01002280 if (enable_1510_mode || !lcd_dma.ext_ctrl)
2281 return;
2282
2283 w = omap_readw(OMAP1610_DMA_LCD_CCR);
2284 w &= ~(1 << 7);
2285 omap_writew(w, OMAP1610_DMA_LCD_CCR);
2286
2287 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2288 w &= ~(1 << 8);
2289 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002290}
Tony Lindgren97b7f712008-07-03 12:24:37 +03002291EXPORT_SYMBOL(omap_stop_lcd_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002292
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002293/*----------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01002294
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002295static int __init omap_init_dma(void)
2296{
2297 int ch, r;
2298
Tony Lindgren0499bde2008-07-03 12:24:36 +03002299 if (cpu_class_is_omap1()) {
2300 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP1_DMA_BASE);
Tony Lindgren4d963722008-07-03 12:24:31 +03002301 dma_lch_count = OMAP1_LOGICAL_DMA_CH_COUNT;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002302 } else if (cpu_is_omap24xx()) {
2303 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP24XX_DMA4_BASE);
Tony Lindgren4d963722008-07-03 12:24:31 +03002304 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002305 } else if (cpu_is_omap34xx()) {
2306 omap_dma_base = (void __iomem *)IO_ADDRESS(OMAP34XX_DMA4_BASE);
2307 dma_lch_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
2308 } else {
2309 pr_err("DMA init failed for unsupported omap\n");
2310 return -ENODEV;
2311 }
Tony Lindgren4d963722008-07-03 12:24:31 +03002312
2313 dma_chan = kzalloc(sizeof(struct omap_dma_lch) * dma_lch_count,
2314 GFP_KERNEL);
2315 if (!dma_chan)
2316 return -ENOMEM;
2317
2318 if (cpu_class_is_omap2()) {
2319 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2320 dma_lch_count, GFP_KERNEL);
2321 if (!dma_linked_lch) {
2322 kfree(dma_chan);
2323 return -ENOMEM;
2324 }
2325 }
2326
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002327 if (cpu_is_omap15xx()) {
2328 printk(KERN_INFO "DMA support for OMAP15xx initialized\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002329 dma_chan_count = 9;
2330 enable_1510_mode = 1;
2331 } else if (cpu_is_omap16xx() || cpu_is_omap730()) {
2332 printk(KERN_INFO "OMAP DMA hardware version %d\n",
Tony Lindgren0499bde2008-07-03 12:24:36 +03002333 dma_read(HW_ID));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002334 printk(KERN_INFO "DMA capabilities: %08x:%08x:%04x:%04x:%04x\n",
Tony Lindgren0499bde2008-07-03 12:24:36 +03002335 (dma_read(CAPS_0_U) << 16) |
2336 dma_read(CAPS_0_L),
2337 (dma_read(CAPS_1_U) << 16) |
2338 dma_read(CAPS_1_L),
2339 dma_read(CAPS_2), dma_read(CAPS_3),
2340 dma_read(CAPS_4));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002341 if (!enable_1510_mode) {
2342 u16 w;
2343
2344 /* Disable OMAP 3.0/3.1 compatibility mode. */
Tony Lindgren0499bde2008-07-03 12:24:36 +03002345 w = dma_read(GSCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002346 w |= 1 << 3;
Tony Lindgren0499bde2008-07-03 12:24:36 +03002347 dma_write(w, GSCR);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002348 dma_chan_count = 16;
2349 } else
2350 dma_chan_count = 9;
Imre Deakb5beef52006-09-25 12:41:28 +03002351 if (cpu_is_omap16xx()) {
2352 u16 w;
2353
2354 /* this would prevent OMAP sleep */
2355 w = omap_readw(OMAP1610_DMA_LCD_CTRL);
2356 w &= ~(1 << 8);
2357 omap_writew(w, OMAP1610_DMA_LCD_CTRL);
2358 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002359 } else if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03002360 u8 revision = dma_read(REVISION) & 0xff;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002361 printk(KERN_INFO "OMAP DMA hardware revision %d.%d\n",
2362 revision >> 4, revision & 0xf);
Tony Lindgren4d963722008-07-03 12:24:31 +03002363 dma_chan_count = OMAP_DMA4_LOGICAL_DMA_CH_COUNT;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002364 } else {
2365 dma_chan_count = 0;
2366 return 0;
2367 }
2368
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002369 spin_lock_init(&lcd_dma.lock);
2370 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002371
2372 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002373 omap_clear_dma(ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002374 dma_chan[ch].dev_id = -1;
2375 dma_chan[ch].next_lch = -1;
2376
2377 if (ch >= 6 && enable_1510_mode)
2378 continue;
2379
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002380 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002381 /*
2382 * request_irq() doesn't like dev_id (ie. ch) being
2383 * zero, so we have to kludge around this.
2384 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002385 r = request_irq(omap1_dma_irq[ch],
2386 omap1_dma_irq_handler, 0, "DMA",
2387 (void *) (ch + 1));
2388 if (r != 0) {
2389 int i;
2390
2391 printk(KERN_ERR "unable to request IRQ %d "
2392 "for DMA (error %d)\n",
2393 omap1_dma_irq[ch], r);
2394 for (i = 0; i < ch; i++)
2395 free_irq(omap1_dma_irq[i],
2396 (void *) (i + 1));
2397 return r;
2398 }
2399 }
2400 }
2401
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002402 if (cpu_is_omap2430() || cpu_is_omap34xx())
2403 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2404 DMA_DEFAULT_FIFO_DEPTH, 0);
2405
2406 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002407 setup_irq(INT_24XX_SDMA_IRQ0, &omap24xx_dma_irq);
2408
2409 /* FIXME: Update LCD DMA to work on 24xx */
2410 if (cpu_class_is_omap1()) {
2411 r = request_irq(INT_DMA_LCD, lcd_dma_irq_handler, 0,
2412 "LCD DMA", NULL);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002413 if (r != 0) {
2414 int i;
2415
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002416 printk(KERN_ERR "unable to request IRQ for LCD DMA "
2417 "(error %d)\n", r);
2418 for (i = 0; i < dma_chan_count; i++)
2419 free_irq(omap1_dma_irq[i], (void *) (i + 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002420 return r;
2421 }
2422 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002423
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002424 return 0;
2425}
2426
2427arch_initcall(omap_init_dma);
2428
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002429