blob: a9983b695a184a2ddd068ffc3ff4a5d2e5130182 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
2 * linux/arch/arm/plat-omap/dma.c
3 *
Tony Lindgren97b7f712008-07-03 12:24:37 +03004 * Copyright (C) 2003 - 2008 Nokia Corporation
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01006 * DMA channel linking for 1610 by Samuel Ortiz <samuel.ortiz@nokia.com>
7 * Graphics DMA and LCD DMA graphics tranformations
8 * by Imre Deak <imre.deak@nokia.com>
Anand Gadiyarf8151e52007-12-01 12:14:11 -08009 * OMAP2/3 support Copyright (C) 2004-2007 Texas Instruments, Inc.
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000010 * Merged to support both OMAP1 and OMAP2 by Tony Lindgren <tony@atomide.com>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010011 * Some functions based on earlier dma-omap.c Copyright (C) 2001 RidgeRun, Inc.
12 *
Santosh Shilimkar44169072009-05-28 14:16:04 -070013 * Copyright (C) 2009 Texas Instruments
14 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
15 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010016 * Support functions for the OMAP internal DMA channels.
17 *
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080018 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
19 * Converted DMA library into DMA platform driver.
20 * - G, Manjunath Kondaiah <manjugk@ti.com>
21 *
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010022 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2 as
24 * published by the Free Software Foundation.
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/sched.h>
31#include <linux/spinlock.h>
32#include <linux/errno.h>
33#include <linux/interrupt.h>
Thomas Gleixner418ca1f2006-07-01 22:32:41 +010034#include <linux/irq.h>
Tony Lindgren97b7f712008-07-03 12:24:37 +030035#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -070037#include <linux/delay.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010038
39#include <asm/system.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/hardware.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070041#include <plat/dma.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010042
Tony Lindgrence491cf2009-10-20 09:40:47 -070043#include <plat/tc.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010044
Anand Gadiyarf8151e52007-12-01 12:14:11 -080045#undef DEBUG
46
47#ifndef CONFIG_ARCH_OMAP1
48enum { DMA_CH_ALLOC_DONE, DMA_CH_PARAMS_SET_DONE, DMA_CH_STARTED,
49 DMA_CH_QUEUED, DMA_CH_NOTSTARTED, DMA_CH_PAUSED, DMA_CH_LINK_ENABLED
50};
51
52enum { DMA_CHAIN_STARTED, DMA_CHAIN_NOTSTARTED };
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000053#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010054
Tony Lindgren97b7f712008-07-03 12:24:37 +030055#define OMAP_DMA_ACTIVE 0x01
Adrian Hunter4fb699b2010-11-24 13:23:21 +020056#define OMAP2_DMA_CSR_CLEAR_MASK 0xffffffff
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010057
Tony Lindgren97b7f712008-07-03 12:24:37 +030058#define OMAP_FUNC_MUX_ARM_BASE (0xfffe1000 + 0xec)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010059
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -080060static struct omap_system_dma_plat_info *p;
61static struct omap_dma_dev_attr *d;
62
Tony Lindgren97b7f712008-07-03 12:24:37 +030063static int enable_1510_mode;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -080064static u32 errata;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010065
Tero Kristof2d11852008-08-28 13:13:31 +000066static struct omap_dma_global_context_registers {
67 u32 dma_irqenable_l0;
68 u32 dma_ocp_sysconfig;
69 u32 dma_gcr;
70} omap_dma_global_context;
71
Anand Gadiyarf8151e52007-12-01 12:14:11 -080072struct dma_link_info {
73 int *linked_dmach_q;
74 int no_of_lchs_linked;
75
76 int q_count;
77 int q_tail;
78 int q_head;
79
80 int chain_state;
81 int chain_mode;
82
83};
84
Tony Lindgren4d963722008-07-03 12:24:31 +030085static struct dma_link_info *dma_linked_lch;
86
87#ifndef CONFIG_ARCH_OMAP1
Anand Gadiyarf8151e52007-12-01 12:14:11 -080088
89/* Chain handling macros */
90#define OMAP_DMA_CHAIN_QINIT(chain_id) \
91 do { \
92 dma_linked_lch[chain_id].q_head = \
93 dma_linked_lch[chain_id].q_tail = \
94 dma_linked_lch[chain_id].q_count = 0; \
95 } while (0)
96#define OMAP_DMA_CHAIN_QFULL(chain_id) \
97 (dma_linked_lch[chain_id].no_of_lchs_linked == \
98 dma_linked_lch[chain_id].q_count)
99#define OMAP_DMA_CHAIN_QLAST(chain_id) \
100 do { \
101 ((dma_linked_lch[chain_id].no_of_lchs_linked-1) == \
102 dma_linked_lch[chain_id].q_count) \
103 } while (0)
104#define OMAP_DMA_CHAIN_QEMPTY(chain_id) \
105 (0 == dma_linked_lch[chain_id].q_count)
106#define __OMAP_DMA_CHAIN_INCQ(end) \
107 ((end) = ((end)+1) % dma_linked_lch[chain_id].no_of_lchs_linked)
108#define OMAP_DMA_CHAIN_INCQHEAD(chain_id) \
109 do { \
110 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_head); \
111 dma_linked_lch[chain_id].q_count--; \
112 } while (0)
113
114#define OMAP_DMA_CHAIN_INCQTAIL(chain_id) \
115 do { \
116 __OMAP_DMA_CHAIN_INCQ(dma_linked_lch[chain_id].q_tail); \
117 dma_linked_lch[chain_id].q_count++; \
118 } while (0)
119#endif
Tony Lindgren4d963722008-07-03 12:24:31 +0300120
121static int dma_lch_count;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122static int dma_chan_count;
Santosh Shilimkar2263f022009-03-23 18:07:48 -0700123static int omap_dma_reserve_channels;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100124
125static spinlock_t dma_chan_lock;
Tony Lindgren4d963722008-07-03 12:24:31 +0300126static struct omap_dma_lch *dma_chan;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100127
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800128static inline void disable_lnk(int lch);
129static void omap_disable_channel_irq(int lch);
130static inline void omap_enable_channel_irq(int lch);
131
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000132#define REVISIT_24XX() printk(KERN_ERR "FIXME: no %s on 24xx\n", \
Harvey Harrison8e86f422008-03-04 15:08:02 -0800133 __func__);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000134
135#ifdef CONFIG_ARCH_OMAP15XX
136/* Returns 1 if the DMA module is in OMAP1510-compatible mode, 0 otherwise */
Aaro Koskinenc7767582011-01-27 16:39:43 -0800137static int omap_dma_in_1510_mode(void)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000138{
139 return enable_1510_mode;
140}
141#else
142#define omap_dma_in_1510_mode() 0
143#endif
144
145#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100146static inline int get_gdma_dev(int req)
147{
148 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
149 int shift = ((req - 1) % 5) * 6;
150
151 return ((omap_readl(reg) >> shift) & 0x3f) + 1;
152}
153
154static inline void set_gdma_dev(int req, int dev)
155{
156 u32 reg = OMAP_FUNC_MUX_ARM_BASE + ((req - 1) / 5) * 4;
157 int shift = ((req - 1) % 5) * 6;
158 u32 l;
159
160 l = omap_readl(reg);
161 l &= ~(0x3f << shift);
162 l |= (dev - 1) << shift;
163 omap_writel(l, reg);
164}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000165#else
166#define set_gdma_dev(req, dev) do {} while (0)
167#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300169void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100170{
171 unsigned long reg;
172 u32 l;
173
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300174 if (cpu_class_is_omap1()) {
175 switch (dst_port) {
176 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
177 reg = OMAP_TC_OCPT1_PRIOR;
178 break;
179 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
180 reg = OMAP_TC_OCPT2_PRIOR;
181 break;
182 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
183 reg = OMAP_TC_EMIFF_PRIOR;
184 break;
185 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
186 reg = OMAP_TC_EMIFS_PRIOR;
187 break;
188 default:
189 BUG();
190 return;
191 }
192 l = omap_readl(reg);
193 l &= ~(0xf << 8);
194 l |= (priority & 0xf) << 8;
195 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100196 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300197
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800198 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300199 u32 ccr;
200
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800201 ccr = p->dma_read(CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300202 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300203 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300204 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300205 ccr &= ~(1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800206 p->dma_write(ccr, CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300207 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100208}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300209EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100210
211void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000212 int frame_count, int sync_mode,
213 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300215 u32 l;
216
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800217 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300218 l &= ~0x03;
219 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800220 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100221
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000222 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300223 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100224
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800225 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300226 ccr &= ~(1 << 5);
227 if (sync_mode == OMAP_DMA_SYNC_FRAME)
228 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800229 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300230
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800231 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300232 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000233 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300234 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800235 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000236 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800238 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300239 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100240
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800241 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200243 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300244 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200245 val |= (dma_trigger & ~0x1f) << 14;
246 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000247
248 if (sync_mode & OMAP_DMA_SYNC_FRAME)
249 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700250 else
251 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000252
253 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
254 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700255 else
256 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000257
Samu Onkalo72a11792010-08-02 14:21:40 +0300258 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000259 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300260 val |= (1 << 23); /* Prefetch */
261 } else if (src_or_dst_synch) {
262 val |= 1 << 24; /* source synch */
263 } else {
264 val &= ~(1 << 24); /* dest synch */
265 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800266 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000267 }
268
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800269 p->dma_write(elem_count, CEN, lch);
270 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100271}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300272EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000273
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100274void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
275{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100276 BUG_ON(omap_dma_in_1510_mode());
277
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700278 if (cpu_class_is_omap1()) {
279 u16 w;
280
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800281 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700282 w &= ~0x03;
283
284 switch (mode) {
285 case OMAP_DMA_CONSTANT_FILL:
286 w |= 0x01;
287 break;
288 case OMAP_DMA_TRANSPARENT_COPY:
289 w |= 0x02;
290 break;
291 case OMAP_DMA_COLOR_DIS:
292 break;
293 default:
294 BUG();
295 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800296 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700297
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800298 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700299 w &= ~0x0f;
300 /* Default is channel type 2D */
301 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800302 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700303 w |= 1; /* Channel type G */
304 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800305 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700306 }
307
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800308 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700309 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000310
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800311 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700312 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300313
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700314 switch (mode) {
315 case OMAP_DMA_CONSTANT_FILL:
316 val |= 1 << 16;
317 break;
318 case OMAP_DMA_TRANSPARENT_COPY:
319 val |= 1 << 17;
320 break;
321 case OMAP_DMA_COLOR_DIS:
322 break;
323 default:
324 BUG();
325 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800326 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700328 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800329 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100330 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100331}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300332EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100333
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300334void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
335{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800336 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300337 u32 csdp;
338
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800339 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300340 csdp &= ~(0x3 << 16);
341 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800342 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300343 }
344}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300345EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300346
Tony Lindgren0499bde2008-07-03 12:24:36 +0300347void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
348{
349 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
350 u32 l;
351
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800352 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300353 l &= ~0x7;
354 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800355 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300356 }
357}
358EXPORT_SYMBOL(omap_set_dma_channel_mode);
359
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000360/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100361void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000362 unsigned long src_start,
363 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300365 u32 l;
366
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000367 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300368 u16 w;
369
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800370 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300371 w &= ~(0x1f << 2);
372 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800373 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300374 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300375
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800376 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300377 l &= ~(0x03 << 12);
378 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800379 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300380
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800381 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800383 p->dma_write(src_ei, CSEI, lch);
384 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300385}
386EXPORT_SYMBOL(omap_set_dma_src_params);
387
388void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000389{
390 omap_set_dma_transfer_params(lch, params->data_type,
391 params->elem_count, params->frame_count,
392 params->sync_mode, params->trigger,
393 params->src_or_dst_synch);
394 omap_set_dma_src_params(lch, params->src_port,
395 params->src_amode, params->src_start,
396 params->src_ei, params->src_fi);
397
398 omap_set_dma_dest_params(lch, params->dst_port,
399 params->dst_amode, params->dst_start,
400 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800401 if (params->read_prio || params->write_prio)
402 omap_dma_set_prio_lch(lch, params->read_prio,
403 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100404}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300405EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406
407void omap_set_dma_src_index(int lch, int eidx, int fidx)
408{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300409 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000410 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300411
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800412 p->dma_write(eidx, CSEI, lch);
413 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300415EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416
417void omap_set_dma_src_data_pack(int lch, int enable)
418{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300419 u32 l;
420
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800421 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300422 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000423 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300424 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800425 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100426}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300427EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428
429void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
430{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700431 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300432 u32 l;
433
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800434 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300435 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100436
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100437 switch (burst_mode) {
438 case OMAP_DMA_DATA_BURST_DIS:
439 break;
440 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800441 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700442 burst = 0x1;
443 else
444 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100445 break;
446 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800447 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700448 burst = 0x2;
449 break;
450 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700451 /*
452 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100453 * w |= (0x03 << 7);
454 * fall through
455 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700456 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800457 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700458 burst = 0x3;
459 break;
460 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700461 /*
462 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700463 * fall through
464 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100465 default:
466 BUG();
467 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300468
469 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800470 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100471}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300472EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000474/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000476 unsigned long dest_start,
477 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300479 u32 l;
480
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000481 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800482 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300483 l &= ~(0x1f << 9);
484 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800485 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000486 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100487
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800488 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300489 l &= ~(0x03 << 14);
490 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800491 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800493 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800495 p->dma_write(dst_ei, CDEI, lch);
496 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100497}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300498EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499
500void omap_set_dma_dest_index(int lch, int eidx, int fidx)
501{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300502 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000503 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300504
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800505 p->dma_write(eidx, CDEI, lch);
506 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300508EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509
510void omap_set_dma_dest_data_pack(int lch, int enable)
511{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300512 u32 l;
513
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800514 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300515 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000516 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300517 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800518 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100519}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300520EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100521
522void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
523{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700524 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300525 u32 l;
526
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800527 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300528 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530 switch (burst_mode) {
531 case OMAP_DMA_DATA_BURST_DIS:
532 break;
533 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800534 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700535 burst = 0x1;
536 else
537 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100538 break;
539 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800540 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700541 burst = 0x2;
542 else
543 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100544 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700545 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800546 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700547 burst = 0x3;
548 break;
549 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700550 /*
551 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700552 * fall through
553 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100554 default:
555 printk(KERN_ERR "Invalid DMA burst mode\n");
556 BUG();
557 return;
558 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300559 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800560 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100561}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300562EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000564static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000566 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700568 /* Clear CSR */
569 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800570 status = p->dma_read(CSR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800571 else if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800572 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000573
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100574 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800575 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576}
577
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000578static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100579{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800580 if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800581 p->dma_write(0, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100582}
583
584void omap_enable_dma_irq(int lch, u16 bits)
585{
586 dma_chan[lch].enabled_irqs |= bits;
587}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300588EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100589
590void omap_disable_dma_irq(int lch, u16 bits)
591{
592 dma_chan[lch].enabled_irqs &= ~bits;
593}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300594EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100595
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000596static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300598 u32 l;
599
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800600 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300601
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000602 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300603 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100604
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000605 /* Set the ENABLE_LNK bits */
606 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300607 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800608
609#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300610 if (cpu_class_is_omap2())
611 if (dma_chan[lch].next_linked_ch != -1)
612 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800613#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300614
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800615 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100616}
617
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000618static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100619{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300620 u32 l;
621
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800622 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300623
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000624 /* Disable interrupts */
625 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800626 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000627 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300628 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100629 }
630
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800631 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000632 omap_disable_channel_irq(lch);
633 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300634 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000635 }
636
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800637 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000638 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
639}
640
641static inline void omap2_enable_irq_lch(int lch)
642{
643 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800644 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000645
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800646 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000647 return;
648
Tao Huee907322009-11-10 18:55:17 -0800649 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800650 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000651 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800652 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800653 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100654}
655
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700656static inline void omap2_disable_irq_lch(int lch)
657{
658 u32 val;
659 unsigned long flags;
660
661 if (!cpu_class_is_omap2())
662 return;
663
664 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800665 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700666 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800667 p->dma_write(val, IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700668 spin_unlock_irqrestore(&dma_chan_lock, flags);
669}
670
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100671int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300672 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673 void *data, int *dma_ch_out)
674{
675 int ch, free_ch = -1;
676 unsigned long flags;
677 struct omap_dma_lch *chan;
678
679 spin_lock_irqsave(&dma_chan_lock, flags);
680 for (ch = 0; ch < dma_chan_count; ch++) {
681 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
682 free_ch = ch;
683 if (dev_id == 0)
684 break;
685 }
686 }
687 if (free_ch == -1) {
688 spin_unlock_irqrestore(&dma_chan_lock, flags);
689 return -EBUSY;
690 }
691 chan = dma_chan + free_ch;
692 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000693
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800694 if (p->clear_lch_regs)
695 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000696
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800697 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000698 omap_clear_dma(free_ch);
699
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100700 spin_unlock_irqrestore(&dma_chan_lock, flags);
701
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702 chan->dev_name = dev_name;
703 chan->callback = callback;
704 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800705 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300706
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800707#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300708 if (cpu_class_is_omap2()) {
709 chan->chain_id = -1;
710 chan->next_linked_ch = -1;
711 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800712#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300713
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700714 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000715
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700716 if (cpu_class_is_omap1())
717 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800718 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700719 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
720 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100721
722 if (cpu_is_omap16xx()) {
723 /* If the sync device is set, configure it dynamically. */
724 if (dev_id != 0) {
725 set_gdma_dev(free_ch + 1, dev_id);
726 dev_id = free_ch + 1;
727 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300728 /*
729 * Disable the 1510 compatibility mode and set the sync device
730 * id.
731 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800732 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700733 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800734 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000736
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800737 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000738 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000739 omap_enable_channel_irq(free_ch);
740 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800741 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, free_ch);
742 p->dma_write(1 << free_ch, IRQSTATUS_L0, 0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000743 }
744
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745 *dma_ch_out = free_ch;
746
747 return 0;
748}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300749EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000751void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752{
753 unsigned long flags;
754
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000755 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300756 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000757 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758 return;
759 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300760
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000761 if (cpu_class_is_omap1()) {
762 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800763 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000764 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800765 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000766 }
767
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800768 if (cpu_class_is_omap2()) {
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700769 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000770
771 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800772 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
773 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774
775 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800776 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000777
778 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800779 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000780 omap_clear_dma(lch);
781 }
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700782
783 spin_lock_irqsave(&dma_chan_lock, flags);
784 dma_chan[lch].dev_id = -1;
785 dma_chan[lch].next_lch = -1;
786 dma_chan[lch].callback = NULL;
787 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300789EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800791/**
792 * @brief omap_dma_set_global_params : Set global priority settings for dma
793 *
794 * @param arb_rate
795 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700796 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
797 * DMA_THREAD_RESERVE_ONET
798 * DMA_THREAD_RESERVE_TWOT
799 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800800 */
801void
802omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
803{
804 u32 reg;
805
806 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800807 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800808 return;
809 }
810
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700811 if (max_fifo_depth == 0)
812 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800813 if (arb_rate == 0)
814 arb_rate = 1;
815
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700816 reg = 0xff & max_fifo_depth;
817 reg |= (0x3 & tparams) << 12;
818 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800819
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800820 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800821}
822EXPORT_SYMBOL(omap_dma_set_global_params);
823
824/**
825 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
826 *
827 * @param lch
828 * @param read_prio - Read priority
829 * @param write_prio - Write priority
830 * Both of the above can be set with one of the following values :
831 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
832 */
833int
834omap_dma_set_prio_lch(int lch, unsigned char read_prio,
835 unsigned char write_prio)
836{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300837 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800838
Tony Lindgren4d963722008-07-03 12:24:31 +0300839 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800840 printk(KERN_ERR "Invalid channel id\n");
841 return -EINVAL;
842 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800843 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300844 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkar44169072009-05-28 14:16:04 -0700845 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300846 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800847 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300848 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800849
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800850 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300851
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800852 return 0;
853}
854EXPORT_SYMBOL(omap_dma_set_prio_lch);
855
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000856/*
857 * Clears any DMA state so the DMA engine is ready to restart with new buffers
858 * through omap_start_dma(). Any buffers in flight are discarded.
859 */
860void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000862 unsigned long flags;
863
864 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800865 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000866 local_irq_restore(flags);
867}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300868EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000869
870void omap_start_dma(int lch)
871{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300872 u32 l;
873
manjugk manjugk519e6162010-03-04 07:11:56 +0000874 /*
875 * The CPC/CDAC register needs to be initialized to zero
876 * before starting dma transfer.
877 */
878 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800879 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000880 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800881 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000882
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000883 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
884 int next_lch, cur_lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800885 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000886
887 dma_chan_link_map[lch] = 1;
888 /* Set the link register of the first channel */
889 enable_lnk(lch);
890
891 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
892 cur_lch = dma_chan[lch].next_lch;
893 do {
894 next_lch = dma_chan[cur_lch].next_lch;
895
896 /* The loop case: we've been here already */
897 if (dma_chan_link_map[cur_lch])
898 break;
899 /* Mark the current channel */
900 dma_chan_link_map[cur_lch] = 1;
901
902 enable_lnk(cur_lch);
903 omap_enable_channel_irq(cur_lch);
904
905 cur_lch = next_lch;
906 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800907 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800908 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000909
910 omap_enable_channel_irq(lch);
911
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800912 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300913
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800914 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
915 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300916 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800917
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800918 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000919
920 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
921}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300922EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000923
924void omap_stop_dma(int lch)
925{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300926 u32 l;
927
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700928 /* Disable all interrupts on the channel */
929 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800930 p->dma_write(0, CICR, lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700931
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800932 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800933 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
934 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700935 int i = 0;
936 u32 sys_cf;
937
938 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800939 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700940 sys_cf = l;
941 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
942 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800943 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700944
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800945 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700946 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800947 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700948
949 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800950 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700951 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
952 OMAP_DMA_CCR_WR_ACTIVE))) {
953 udelay(5);
954 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800955 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700956 }
957 if (i >= 100)
958 printk(KERN_ERR "DMA drain did not complete on "
959 "lch %d\n", lch);
960 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800961 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700962 } else {
963 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800964 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700965 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700966
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000967 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
968 int next_lch, cur_lch = lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800969 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000970
971 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
972 do {
973 /* The loop case: we've been here already */
974 if (dma_chan_link_map[cur_lch])
975 break;
976 /* Mark the current channel */
977 dma_chan_link_map[cur_lch] = 1;
978
979 disable_lnk(cur_lch);
980
981 next_lch = dma_chan[cur_lch].next_lch;
982 cur_lch = next_lch;
983 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000984 }
985
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000986 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
987}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300988EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000989
990/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300991 * Allows changing the DMA callback function or data. This may be needed if
992 * the driver shares a single DMA channel for multiple dma triggers.
993 */
994int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300995 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300996 void *data)
997{
998 unsigned long flags;
999
1000 if (lch < 0)
1001 return -ENODEV;
1002
1003 spin_lock_irqsave(&dma_chan_lock, flags);
1004 if (dma_chan[lch].dev_id == -1) {
1005 printk(KERN_ERR "DMA callback for not set for free channel\n");
1006 spin_unlock_irqrestore(&dma_chan_lock, flags);
1007 return -EINVAL;
1008 }
1009 dma_chan[lch].callback = callback;
1010 dma_chan[lch].data = data;
1011 spin_unlock_irqrestore(&dma_chan_lock, flags);
1012
1013 return 0;
1014}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001015EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001016
1017/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001018 * Returns current physical source address for the given DMA channel.
1019 * If the channel is running the caller must disable interrupts prior calling
1020 * this function and process the returned value before re-enabling interrupt to
1021 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001022 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001023 * in incorrect return value.
1024 */
1025dma_addr_t omap_get_dma_src_pos(int lch)
1026{
Tony Lindgren0695de32007-05-07 18:24:14 -07001027 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001028
Tony Lindgren0499bde2008-07-03 12:24:36 +03001029 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001030 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001031 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001032 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001033
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001034 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001035 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001036
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001037 if (!cpu_is_omap15xx()) {
1038 /*
1039 * CDAC == 0 indicates that the DMA transfer on the channel has
1040 * not been started (no data has been transferred so far).
1041 * Return the programmed source start address in this case.
1042 */
1043 if (likely(p->dma_read(CDAC, lch)))
1044 offset = p->dma_read(CSAC, lch);
1045 else
1046 offset = p->dma_read(CSSA, lch);
1047 }
1048
Tony Lindgren0499bde2008-07-03 12:24:36 +03001049 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001050 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001051
1052 return offset;
1053}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001054EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001055
1056/*
1057 * Returns current physical destination address for the given DMA channel.
1058 * If the channel is running the caller must disable interrupts prior calling
1059 * this function and process the returned value before re-enabling interrupt to
1060 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001061 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001062 * in incorrect return value.
1063 */
1064dma_addr_t omap_get_dma_dst_pos(int lch)
1065{
Tony Lindgren0695de32007-05-07 18:24:14 -07001066 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001067
Tony Lindgren0499bde2008-07-03 12:24:36 +03001068 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001069 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001070 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001071 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001072
Tony Lindgren0499bde2008-07-03 12:24:36 +03001073 /*
1074 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1075 * read before the DMA controller finished disabling the channel.
1076 */
1077 if (!cpu_is_omap15xx() && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001078 offset = p->dma_read(CDAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001079
1080 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001081 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001082
1083 return offset;
1084}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001085EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001086
Tony Lindgren0499bde2008-07-03 12:24:36 +03001087int omap_get_dma_active_status(int lch)
1088{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001089 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001090}
1091EXPORT_SYMBOL(omap_get_dma_active_status);
1092
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001093int omap_dma_running(void)
1094{
1095 int lch;
1096
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001097 if (cpu_class_is_omap1())
1098 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001099 return 1;
1100
1101 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001102 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001103 return 1;
1104
1105 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001106}
1107
1108/*
1109 * lch_queue DMA will start right after lch_head one is finished.
1110 * For this DMA link to start, you still need to start (see omap_start_dma)
1111 * the first one. That will fire up the entire queue.
1112 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001113void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001114{
1115 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001116 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001117 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001118 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001119 return;
1120 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001121 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1122 BUG();
1123 return;
1124 }
1125
1126 if ((dma_chan[lch_head].dev_id == -1) ||
1127 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001128 printk(KERN_ERR "omap_dma: trying to link "
1129 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001130 dump_stack();
1131 }
1132
1133 dma_chan[lch_head].next_lch = lch_queue;
1134}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001135EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001136
1137/*
1138 * Once the DMA queue is stopped, we can destroy it.
1139 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001140void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001141{
1142 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001143 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001144 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001145 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001146 return;
1147 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001148 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1149 BUG();
1150 return;
1151 }
1152
1153 if (dma_chan[lch_head].next_lch != lch_queue ||
1154 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001155 printk(KERN_ERR "omap_dma: trying to unlink "
1156 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001157 dump_stack();
1158 }
1159
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001160 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001161 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001162 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1163 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001164 dump_stack();
1165 }
1166
1167 dma_chan[lch_head].next_lch = -1;
1168}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001169EXPORT_SYMBOL(omap_dma_unlink_lch);
1170
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001171#ifndef CONFIG_ARCH_OMAP1
1172/* Create chain of DMA channesls */
1173static void create_dma_lch_chain(int lch_head, int lch_queue)
1174{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001175 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001176
1177 /* Check if this is the first link in chain */
1178 if (dma_chan[lch_head].next_linked_ch == -1) {
1179 dma_chan[lch_head].next_linked_ch = lch_queue;
1180 dma_chan[lch_head].prev_linked_ch = lch_queue;
1181 dma_chan[lch_queue].next_linked_ch = lch_head;
1182 dma_chan[lch_queue].prev_linked_ch = lch_head;
1183 }
1184
1185 /* a link exists, link the new channel in circular chain */
1186 else {
1187 dma_chan[lch_queue].next_linked_ch =
1188 dma_chan[lch_head].next_linked_ch;
1189 dma_chan[lch_queue].prev_linked_ch = lch_head;
1190 dma_chan[lch_head].next_linked_ch = lch_queue;
1191 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1192 lch_queue;
1193 }
1194
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001195 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001196 l &= ~(0x1f);
1197 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001198 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001199
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001200 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001201 l &= ~(0x1f);
1202 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001203 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001204}
1205
1206/**
1207 * @brief omap_request_dma_chain : Request a chain of DMA channels
1208 *
1209 * @param dev_id - Device id using the dma channel
1210 * @param dev_name - Device name
1211 * @param callback - Call back function
1212 * @chain_id -
1213 * @no_of_chans - Number of channels requested
1214 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1215 * OMAP_DMA_DYNAMIC_CHAIN
1216 * @params - Channel parameters
1217 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001218 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001219 * Failure: -EINVAL/-ENOMEM
1220 */
1221int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001222 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001223 void *data),
1224 int *chain_id, int no_of_chans, int chain_mode,
1225 struct omap_dma_channel_params params)
1226{
1227 int *channels;
1228 int i, err;
1229
1230 /* Is the chain mode valid ? */
1231 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1232 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1233 printk(KERN_ERR "Invalid chain mode requested\n");
1234 return -EINVAL;
1235 }
1236
1237 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001238 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001239 printk(KERN_ERR "Invalid Number of channels requested\n");
1240 return -EINVAL;
1241 }
1242
manjugk manjugkea221a62010-05-14 12:05:25 -07001243 /*
1244 * Allocate a queue to maintain the status of the channels
1245 * in the chain
1246 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001247 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1248 if (channels == NULL) {
1249 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1250 return -ENOMEM;
1251 }
1252
1253 /* request and reserve DMA channels for the chain */
1254 for (i = 0; i < no_of_chans; i++) {
1255 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001256 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001257 if (err < 0) {
1258 int j;
1259 for (j = 0; j < i; j++)
1260 omap_free_dma(channels[j]);
1261 kfree(channels);
1262 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1263 return err;
1264 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001265 dma_chan[channels[i]].prev_linked_ch = -1;
1266 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1267
1268 /*
1269 * Allowing client drivers to set common parameters now,
1270 * so that later only relevant (src_start, dest_start
1271 * and element count) can be set
1272 */
1273 omap_set_dma_params(channels[i], &params);
1274 }
1275
1276 *chain_id = channels[0];
1277 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1278 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1279 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1280 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1281
1282 for (i = 0; i < no_of_chans; i++)
1283 dma_chan[channels[i]].chain_id = *chain_id;
1284
1285 /* Reset the Queue pointers */
1286 OMAP_DMA_CHAIN_QINIT(*chain_id);
1287
1288 /* Set up the chain */
1289 if (no_of_chans == 1)
1290 create_dma_lch_chain(channels[0], channels[0]);
1291 else {
1292 for (i = 0; i < (no_of_chans - 1); i++)
1293 create_dma_lch_chain(channels[i], channels[i + 1]);
1294 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001295
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001296 return 0;
1297}
1298EXPORT_SYMBOL(omap_request_dma_chain);
1299
1300/**
1301 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1302 * params after setting it. Dont do this while dma is running!!
1303 *
1304 * @param chain_id - Chained logical channel id.
1305 * @param params
1306 *
1307 * @return - Success : 0
1308 * Failure : -EINVAL
1309 */
1310int omap_modify_dma_chain_params(int chain_id,
1311 struct omap_dma_channel_params params)
1312{
1313 int *channels;
1314 u32 i;
1315
1316 /* Check for input params */
1317 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001318 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001319 printk(KERN_ERR "Invalid chain id\n");
1320 return -EINVAL;
1321 }
1322
1323 /* Check if the chain exists */
1324 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1325 printk(KERN_ERR "Chain doesn't exists\n");
1326 return -EINVAL;
1327 }
1328 channels = dma_linked_lch[chain_id].linked_dmach_q;
1329
1330 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1331 /*
1332 * Allowing client drivers to set common parameters now,
1333 * so that later only relevant (src_start, dest_start
1334 * and element count) can be set
1335 */
1336 omap_set_dma_params(channels[i], &params);
1337 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001338
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001339 return 0;
1340}
1341EXPORT_SYMBOL(omap_modify_dma_chain_params);
1342
1343/**
1344 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1345 *
1346 * @param chain_id
1347 *
1348 * @return - Success : 0
1349 * Failure : -EINVAL
1350 */
1351int omap_free_dma_chain(int chain_id)
1352{
1353 int *channels;
1354 u32 i;
1355
1356 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001357 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001358 printk(KERN_ERR "Invalid chain id\n");
1359 return -EINVAL;
1360 }
1361
1362 /* Check if the chain exists */
1363 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1364 printk(KERN_ERR "Chain doesn't exists\n");
1365 return -EINVAL;
1366 }
1367
1368 channels = dma_linked_lch[chain_id].linked_dmach_q;
1369 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1370 dma_chan[channels[i]].next_linked_ch = -1;
1371 dma_chan[channels[i]].prev_linked_ch = -1;
1372 dma_chan[channels[i]].chain_id = -1;
1373 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1374 omap_free_dma(channels[i]);
1375 }
1376
1377 kfree(channels);
1378
1379 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1380 dma_linked_lch[chain_id].chain_mode = -1;
1381 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001382
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001383 return (0);
1384}
1385EXPORT_SYMBOL(omap_free_dma_chain);
1386
1387/**
1388 * @brief omap_dma_chain_status - Check if the chain is in
1389 * active / inactive state.
1390 * @param chain_id
1391 *
1392 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1393 * Failure : -EINVAL
1394 */
1395int omap_dma_chain_status(int chain_id)
1396{
1397 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001398 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001399 printk(KERN_ERR "Invalid chain id\n");
1400 return -EINVAL;
1401 }
1402
1403 /* Check if the chain exists */
1404 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1405 printk(KERN_ERR "Chain doesn't exists\n");
1406 return -EINVAL;
1407 }
1408 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1409 dma_linked_lch[chain_id].q_count);
1410
1411 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1412 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001413
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001414 return OMAP_DMA_CHAIN_ACTIVE;
1415}
1416EXPORT_SYMBOL(omap_dma_chain_status);
1417
1418/**
1419 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1420 * set the params and start the transfer.
1421 *
1422 * @param chain_id
1423 * @param src_start - buffer start address
1424 * @param dest_start - Dest address
1425 * @param elem_count
1426 * @param frame_count
1427 * @param callbk_data - channel callback parameter data.
1428 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301429 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001430 * Failure: -EINVAL/-EBUSY
1431 */
1432int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1433 int elem_count, int frame_count, void *callbk_data)
1434{
1435 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001436 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001437 int start_dma = 0;
1438
Tony Lindgren97b7f712008-07-03 12:24:37 +03001439 /*
1440 * if buffer size is less than 1 then there is
1441 * no use of starting the chain
1442 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001443 if (elem_count < 1) {
1444 printk(KERN_ERR "Invalid buffer size\n");
1445 return -EINVAL;
1446 }
1447
1448 /* Check for input params */
1449 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001450 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001451 printk(KERN_ERR "Invalid chain id\n");
1452 return -EINVAL;
1453 }
1454
1455 /* Check if the chain exists */
1456 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1457 printk(KERN_ERR "Chain doesn't exist\n");
1458 return -EINVAL;
1459 }
1460
1461 /* Check if all the channels in chain are in use */
1462 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1463 return -EBUSY;
1464
1465 /* Frame count may be negative in case of indexed transfers */
1466 channels = dma_linked_lch[chain_id].linked_dmach_q;
1467
1468 /* Get a free channel */
1469 lch = channels[dma_linked_lch[chain_id].q_tail];
1470
1471 /* Store the callback data */
1472 dma_chan[lch].data = callbk_data;
1473
1474 /* Increment the q_tail */
1475 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1476
1477 /* Set the params to the free channel */
1478 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001479 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001480 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001481 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001482
1483 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001484 p->dma_write(elem_count, CEN, lch);
1485 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001486
Tony Lindgren97b7f712008-07-03 12:24:37 +03001487 /*
1488 * If the chain is dynamically linked,
1489 * then we may have to start the chain if its not active
1490 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001491 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1492
Tony Lindgren97b7f712008-07-03 12:24:37 +03001493 /*
1494 * In Dynamic chain, if the chain is not started,
1495 * queue the channel
1496 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001497 if (dma_linked_lch[chain_id].chain_state ==
1498 DMA_CHAIN_NOTSTARTED) {
1499 /* Enable the link in previous channel */
1500 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1501 DMA_CH_QUEUED)
1502 enable_lnk(dma_chan[lch].prev_linked_ch);
1503 dma_chan[lch].state = DMA_CH_QUEUED;
1504 }
1505
Tony Lindgren97b7f712008-07-03 12:24:37 +03001506 /*
1507 * Chain is already started, make sure its active,
1508 * if not then start the chain
1509 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001510 else {
1511 start_dma = 1;
1512
1513 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1514 DMA_CH_STARTED) {
1515 enable_lnk(dma_chan[lch].prev_linked_ch);
1516 dma_chan[lch].state = DMA_CH_QUEUED;
1517 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001518 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001519 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001520 disable_lnk(dma_chan[lch].
1521 prev_linked_ch);
1522 pr_debug("\n prev ch is stopped\n");
1523 start_dma = 1;
1524 }
1525 }
1526
1527 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1528 == DMA_CH_QUEUED) {
1529 enable_lnk(dma_chan[lch].prev_linked_ch);
1530 dma_chan[lch].state = DMA_CH_QUEUED;
1531 start_dma = 0;
1532 }
1533 omap_enable_channel_irq(lch);
1534
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001535 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001536
Tony Lindgren0499bde2008-07-03 12:24:36 +03001537 if ((0 == (l & (1 << 24))))
1538 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001539 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001540 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001541 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001542 if (0 == (l & (1 << 7))) {
1543 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001544 dma_chan[lch].state = DMA_CH_STARTED;
1545 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001546 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001547 } else
1548 start_dma = 0;
1549 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001550 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001551 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001552 }
1553 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1554 }
1555 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001556
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301557 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001558}
1559EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1560
1561/**
1562 * @brief omap_start_dma_chain_transfers - Start the chain
1563 *
1564 * @param chain_id
1565 *
1566 * @return - Success : 0
1567 * Failure : -EINVAL/-EBUSY
1568 */
1569int omap_start_dma_chain_transfers(int chain_id)
1570{
1571 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001572 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001573
Tony Lindgren4d963722008-07-03 12:24:31 +03001574 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001575 printk(KERN_ERR "Invalid chain id\n");
1576 return -EINVAL;
1577 }
1578
1579 channels = dma_linked_lch[chain_id].linked_dmach_q;
1580
1581 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1582 printk(KERN_ERR "Chain is already started\n");
1583 return -EBUSY;
1584 }
1585
1586 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1587 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1588 i++) {
1589 enable_lnk(channels[i]);
1590 omap_enable_channel_irq(channels[i]);
1591 }
1592 } else {
1593 omap_enable_channel_irq(channels[0]);
1594 }
1595
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001596 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001597 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001598 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1599 dma_chan[channels[0]].state = DMA_CH_STARTED;
1600
Tony Lindgren0499bde2008-07-03 12:24:36 +03001601 if ((0 == (l & (1 << 24))))
1602 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001603 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001604 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001605 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001606
1607 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001608
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001609 return 0;
1610}
1611EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1612
1613/**
1614 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1615 *
1616 * @param chain_id
1617 *
1618 * @return - Success : 0
1619 * Failure : EINVAL
1620 */
1621int omap_stop_dma_chain_transfers(int chain_id)
1622{
1623 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001624 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001625 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001626
1627 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001628 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001629 printk(KERN_ERR "Invalid chain id\n");
1630 return -EINVAL;
1631 }
1632
1633 /* Check if the chain exists */
1634 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1635 printk(KERN_ERR "Chain doesn't exists\n");
1636 return -EINVAL;
1637 }
1638 channels = dma_linked_lch[chain_id].linked_dmach_q;
1639
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001640 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001641 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001642 l = sys_cf;
1643 /* Middle mode reg set no Standby */
1644 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001645 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001646 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001647
1648 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1649
1650 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001651 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001652 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001653 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001654
1655 /* Disable the link in all the channels */
1656 disable_lnk(channels[i]);
1657 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1658
1659 }
1660 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1661
1662 /* Reset the Queue pointers */
1663 OMAP_DMA_CHAIN_QINIT(chain_id);
1664
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001665 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001666 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001667
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001668 return 0;
1669}
1670EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1671
1672/* Get the index of the ongoing DMA in chain */
1673/**
1674 * @brief omap_get_dma_chain_index - Get the element and frame index
1675 * of the ongoing DMA in chain
1676 *
1677 * @param chain_id
1678 * @param ei - Element index
1679 * @param fi - Frame index
1680 *
1681 * @return - Success : 0
1682 * Failure : -EINVAL
1683 */
1684int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1685{
1686 int lch;
1687 int *channels;
1688
1689 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001690 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001691 printk(KERN_ERR "Invalid chain id\n");
1692 return -EINVAL;
1693 }
1694
1695 /* Check if the chain exists */
1696 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1697 printk(KERN_ERR "Chain doesn't exists\n");
1698 return -EINVAL;
1699 }
1700 if ((!ei) || (!fi))
1701 return -EINVAL;
1702
1703 channels = dma_linked_lch[chain_id].linked_dmach_q;
1704
1705 /* Get the current channel */
1706 lch = channels[dma_linked_lch[chain_id].q_head];
1707
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001708 *ei = p->dma_read(CCEN, lch);
1709 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001710
1711 return 0;
1712}
1713EXPORT_SYMBOL(omap_get_dma_chain_index);
1714
1715/**
1716 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1717 * ongoing DMA in chain
1718 *
1719 * @param chain_id
1720 *
1721 * @return - Success : Destination position
1722 * Failure : -EINVAL
1723 */
1724int omap_get_dma_chain_dst_pos(int chain_id)
1725{
1726 int lch;
1727 int *channels;
1728
1729 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001730 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001731 printk(KERN_ERR "Invalid chain id\n");
1732 return -EINVAL;
1733 }
1734
1735 /* Check if the chain exists */
1736 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1737 printk(KERN_ERR "Chain doesn't exists\n");
1738 return -EINVAL;
1739 }
1740
1741 channels = dma_linked_lch[chain_id].linked_dmach_q;
1742
1743 /* Get the current channel */
1744 lch = channels[dma_linked_lch[chain_id].q_head];
1745
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001746 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001747}
1748EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1749
1750/**
1751 * @brief omap_get_dma_chain_src_pos - Get the source position
1752 * of the ongoing DMA in chain
1753 * @param chain_id
1754 *
1755 * @return - Success : Destination position
1756 * Failure : -EINVAL
1757 */
1758int omap_get_dma_chain_src_pos(int chain_id)
1759{
1760 int lch;
1761 int *channels;
1762
1763 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001764 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001765 printk(KERN_ERR "Invalid chain id\n");
1766 return -EINVAL;
1767 }
1768
1769 /* Check if the chain exists */
1770 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1771 printk(KERN_ERR "Chain doesn't exists\n");
1772 return -EINVAL;
1773 }
1774
1775 channels = dma_linked_lch[chain_id].linked_dmach_q;
1776
1777 /* Get the current channel */
1778 lch = channels[dma_linked_lch[chain_id].q_head];
1779
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001780 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001781}
1782EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001783#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001784
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001785/*----------------------------------------------------------------------------*/
1786
1787#ifdef CONFIG_ARCH_OMAP1
1788
1789static int omap1_dma_handle_ch(int ch)
1790{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001791 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001792
1793 if (enable_1510_mode && ch >= 6) {
1794 csr = dma_chan[ch].saved_csr;
1795 dma_chan[ch].saved_csr = 0;
1796 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001797 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001798 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1799 dma_chan[ch + 6].saved_csr = csr >> 7;
1800 csr &= 0x7f;
1801 }
1802 if ((csr & 0x3f) == 0)
1803 return 0;
1804 if (unlikely(dma_chan[ch].dev_id == -1)) {
1805 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1806 "%d (CSR %04x)\n", ch, csr);
1807 return 0;
1808 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001809 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001810 printk(KERN_WARNING "DMA timeout with device %d\n",
1811 dma_chan[ch].dev_id);
1812 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1813 printk(KERN_WARNING "DMA synchronization event drop occurred "
1814 "with device %d\n", dma_chan[ch].dev_id);
1815 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1816 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1817 if (likely(dma_chan[ch].callback != NULL))
1818 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001819
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001820 return 1;
1821}
1822
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001823static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001824{
1825 int ch = ((int) dev_id) - 1;
1826 int handled = 0;
1827
1828 for (;;) {
1829 int handled_now = 0;
1830
1831 handled_now += omap1_dma_handle_ch(ch);
1832 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1833 handled_now += omap1_dma_handle_ch(ch + 6);
1834 if (!handled_now)
1835 break;
1836 handled += handled_now;
1837 }
1838
1839 return handled ? IRQ_HANDLED : IRQ_NONE;
1840}
1841
1842#else
1843#define omap1_dma_irq_handler NULL
1844#endif
1845
Tony Lindgren140455f2010-02-12 12:26:48 -08001846#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001847
1848static int omap2_dma_handle_ch(int ch)
1849{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001850 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001851
Juha Yrjola31513692006-12-06 17:13:47 -08001852 if (!status) {
1853 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001854 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1855 ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001856 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001857 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001858 }
1859 if (unlikely(dma_chan[ch].dev_id == -1)) {
1860 if (printk_ratelimit())
1861 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1862 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001863 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001864 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001865 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1866 printk(KERN_INFO
1867 "DMA synchronization event drop occurred with device "
1868 "%d\n", dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001869 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001870 printk(KERN_INFO "DMA transaction error with device %d\n",
1871 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001872 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001873 u32 ccr;
1874
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001875 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001876 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001877 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001878 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1879 }
1880 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001881 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1882 printk(KERN_INFO "DMA secure error with device %d\n",
1883 dma_chan[ch].dev_id);
1884 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1885 printk(KERN_INFO "DMA misaligned error with device %d\n",
1886 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001887
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001888 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001889 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001890 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001891 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001892
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001893 /* If the ch is not chained then chain_id will be -1 */
1894 if (dma_chan[ch].chain_id != -1) {
1895 int chain_id = dma_chan[ch].chain_id;
1896 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001897 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001898 dma_chan[dma_chan[ch].next_linked_ch].state =
1899 DMA_CH_STARTED;
1900 if (dma_linked_lch[chain_id].chain_mode ==
1901 OMAP_DMA_DYNAMIC_CHAIN)
1902 disable_lnk(ch);
1903
1904 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1905 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1906
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001907 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001908 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001909 }
1910
Jarkko Nikula538528d2008-02-13 11:47:29 +02001911 if (likely(dma_chan[ch].callback != NULL))
1912 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001913
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001914 return 0;
1915}
1916
1917/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001918static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001919{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001920 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001921 int i;
1922
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001923 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001924 if (val == 0) {
1925 if (printk_ratelimit())
1926 printk(KERN_WARNING "Spurious DMA IRQ\n");
1927 return IRQ_HANDLED;
1928 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001929 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001930 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001931 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001932 if (val & 1)
1933 omap2_dma_handle_ch(i);
1934 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001935 }
1936
1937 return IRQ_HANDLED;
1938}
1939
1940static struct irqaction omap24xx_dma_irq = {
1941 .name = "DMA",
1942 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001943 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001944};
1945
1946#else
1947static struct irqaction omap24xx_dma_irq;
1948#endif
1949
1950/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001951
Tero Kristof2d11852008-08-28 13:13:31 +00001952void omap_dma_global_context_save(void)
1953{
1954 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001955 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001956 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001957 p->dma_read(OCP_SYSCONFIG, 0);
1958 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001959}
1960
1961void omap_dma_global_context_restore(void)
1962{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001963 int ch;
1964
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001965 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1966 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001967 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001968 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001969 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001970
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001971 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001972 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001973
1974 for (ch = 0; ch < dma_chan_count; ch++)
1975 if (dma_chan[ch].dev_id != -1)
1976 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00001977}
1978
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001979static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001980{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001981 int ch, ret = 0;
1982 int dma_irq;
1983 char irq_name[4];
1984 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001985
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001986 p = pdev->dev.platform_data;
1987 if (!p) {
1988 dev_err(&pdev->dev, "%s: System DMA initialized without"
1989 "platform data\n", __func__);
1990 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001991 }
1992
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001993 d = p->dma_attr;
1994 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001995
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001996 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07001997 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001998 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07001999
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002000 dma_lch_count = d->lch_count;
2001 dma_chan_count = dma_lch_count;
2002 dma_chan = d->chan;
2003 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002004
2005 if (cpu_class_is_omap2()) {
2006 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2007 dma_lch_count, GFP_KERNEL);
2008 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002009 ret = -ENOMEM;
2010 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002011 }
2012 }
2013
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002014 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002015 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002016 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002017 if (cpu_class_is_omap2())
2018 omap2_disable_irq_lch(ch);
2019
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002020 dma_chan[ch].dev_id = -1;
2021 dma_chan[ch].next_lch = -1;
2022
2023 if (ch >= 6 && enable_1510_mode)
2024 continue;
2025
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002026 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002027 /*
2028 * request_irq() doesn't like dev_id (ie. ch) being
2029 * zero, so we have to kludge around this.
2030 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002031 sprintf(&irq_name[0], "%d", ch);
2032 dma_irq = platform_get_irq_byname(pdev, irq_name);
2033
2034 if (dma_irq < 0) {
2035 ret = dma_irq;
2036 goto exit_dma_irq_fail;
2037 }
2038
2039 /* INT_DMA_LCD is handled in lcd_dma.c */
2040 if (dma_irq == INT_DMA_LCD)
2041 continue;
2042
2043 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002044 omap1_dma_irq_handler, 0, "DMA",
2045 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002046 if (ret != 0)
2047 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002048 }
2049 }
2050
Santosh Shilimkar44169072009-05-28 14:16:04 -07002051 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002052 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2053 DMA_DEFAULT_FIFO_DEPTH, 0);
2054
Santosh Shilimkar44169072009-05-28 14:16:04 -07002055 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002056 strcpy(irq_name, "0");
2057 dma_irq = platform_get_irq_byname(pdev, irq_name);
2058 if (dma_irq < 0) {
2059 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2060 goto exit_dma_lch_fail;
2061 }
2062 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2063 if (ret) {
2064 dev_err(&pdev->dev, "set_up failed for IRQ %d"
2065 "for DMA (error %d)\n", dma_irq, ret);
2066 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002067 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002068 }
2069
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002070 /* reserve dma channels 0 and 1 in high security devices */
2071 if (cpu_is_omap34xx() &&
2072 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
2073 printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
2074 "HS ROM code\n");
2075 dma_chan[0].dev_id = 0;
2076 dma_chan[1].dev_id = 1;
2077 }
2078 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002079 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002080
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002081exit_dma_irq_fail:
2082 dev_err(&pdev->dev, "unable to request IRQ %d"
2083 "for DMA (error %d)\n", dma_irq, ret);
2084 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2085 dma_irq = platform_get_irq(pdev, irq_rel);
2086 free_irq(dma_irq, (void *)(irq_rel + 1));
2087 }
2088
2089exit_dma_lch_fail:
2090 kfree(p);
2091 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002092 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002093 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002094}
2095
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002096static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2097{
2098 int dma_irq;
2099
2100 if (cpu_class_is_omap2()) {
2101 char irq_name[4];
2102 strcpy(irq_name, "0");
2103 dma_irq = platform_get_irq_byname(pdev, irq_name);
2104 remove_irq(dma_irq, &omap24xx_dma_irq);
2105 } else {
2106 int irq_rel = 0;
2107 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2108 dma_irq = platform_get_irq(pdev, irq_rel);
2109 free_irq(dma_irq, (void *)(irq_rel + 1));
2110 }
2111 }
2112 kfree(p);
2113 kfree(d);
2114 kfree(dma_chan);
2115 return 0;
2116}
2117
2118static struct platform_driver omap_system_dma_driver = {
2119 .probe = omap_system_dma_probe,
2120 .remove = omap_system_dma_remove,
2121 .driver = {
2122 .name = "omap_dma_system"
2123 },
2124};
2125
2126static int __init omap_system_dma_init(void)
2127{
2128 return platform_driver_register(&omap_system_dma_driver);
2129}
2130arch_initcall(omap_system_dma_init);
2131
2132static void __exit omap_system_dma_exit(void)
2133{
2134 platform_driver_unregister(&omap_system_dma_driver);
2135}
2136
2137MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2138MODULE_LICENSE("GPL");
2139MODULE_ALIAS("platform:" DRIVER_NAME);
2140MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002141
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002142/*
2143 * Reserve the omap SDMA channels using cmdline bootarg
2144 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2145 */
2146static int __init omap_dma_cmdline_reserve_ch(char *str)
2147{
2148 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2149 omap_dma_reserve_channels = 0;
2150 return 1;
2151}
2152
2153__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2154
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002155