blob: 95c3ed693f656410bd2eb167d63c4656bd8cbe6b [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)
Tony Lindgren2c799ce2012-02-24 10:34:35 -0800167#define omap_readl(reg) 0
168#define omap_writel(val, reg) do {} while (0)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000169#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100170
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300171void omap_set_dma_priority(int lch, int dst_port, int priority)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100172{
173 unsigned long reg;
174 u32 l;
175
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300176 if (cpu_class_is_omap1()) {
177 switch (dst_port) {
178 case OMAP_DMA_PORT_OCP_T1: /* FFFECC00 */
179 reg = OMAP_TC_OCPT1_PRIOR;
180 break;
181 case OMAP_DMA_PORT_OCP_T2: /* FFFECCD0 */
182 reg = OMAP_TC_OCPT2_PRIOR;
183 break;
184 case OMAP_DMA_PORT_EMIFF: /* FFFECC08 */
185 reg = OMAP_TC_EMIFF_PRIOR;
186 break;
187 case OMAP_DMA_PORT_EMIFS: /* FFFECC04 */
188 reg = OMAP_TC_EMIFS_PRIOR;
189 break;
190 default:
191 BUG();
192 return;
193 }
194 l = omap_readl(reg);
195 l &= ~(0xf << 8);
196 l |= (priority & 0xf) << 8;
197 omap_writel(l, reg);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100198 }
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300199
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800200 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300201 u32 ccr;
202
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800203 ccr = p->dma_read(CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300204 if (priority)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300205 ccr |= (1 << 6);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300206 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300207 ccr &= ~(1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800208 p->dma_write(ccr, CCR, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300209 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100210}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300211EXPORT_SYMBOL(omap_set_dma_priority);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100212
213void omap_set_dma_transfer_params(int lch, int data_type, int elem_count,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000214 int frame_count, int sync_mode,
215 int dma_trigger, int src_or_dst_synch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100216{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300217 u32 l;
218
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800219 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300220 l &= ~0x03;
221 l |= data_type;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800222 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100223
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000224 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300225 u16 ccr;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100226
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800227 ccr = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300228 ccr &= ~(1 << 5);
229 if (sync_mode == OMAP_DMA_SYNC_FRAME)
230 ccr |= 1 << 5;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800231 p->dma_write(ccr, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300232
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800233 ccr = p->dma_read(CCR2, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300234 ccr &= ~(1 << 2);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000235 if (sync_mode == OMAP_DMA_SYNC_BLOCK)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300236 ccr |= 1 << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800237 p->dma_write(ccr, CCR2, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000238 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100239
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800240 if (cpu_class_is_omap2() && dma_trigger) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300241 u32 val;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800243 val = p->dma_read(CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100244
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200245 /* DMA_SYNCHRO_CONTROL_UPPER depends on the channel number */
Samu Onkalo72a11792010-08-02 14:21:40 +0300246 val &= ~((1 << 23) | (3 << 19) | 0x1f);
Anand Gadiyar4b3cf442009-01-15 13:09:53 +0200247 val |= (dma_trigger & ~0x1f) << 14;
248 val |= dma_trigger & 0x1f;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000249
250 if (sync_mode & OMAP_DMA_SYNC_FRAME)
251 val |= 1 << 5;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700252 else
253 val &= ~(1 << 5);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000254
255 if (sync_mode & OMAP_DMA_SYNC_BLOCK)
256 val |= 1 << 18;
Peter Ujfalusieca9e562006-06-26 16:16:06 -0700257 else
258 val &= ~(1 << 18);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000259
Samu Onkalo72a11792010-08-02 14:21:40 +0300260 if (src_or_dst_synch == OMAP_DMA_DST_SYNC_PREFETCH) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000261 val &= ~(1 << 24); /* dest synch */
Samu Onkalo72a11792010-08-02 14:21:40 +0300262 val |= (1 << 23); /* Prefetch */
263 } else if (src_or_dst_synch) {
264 val |= 1 << 24; /* source synch */
265 } else {
266 val &= ~(1 << 24); /* dest synch */
267 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800268 p->dma_write(val, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000269 }
270
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800271 p->dma_write(elem_count, CEN, lch);
272 p->dma_write(frame_count, CFN, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100273}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300274EXPORT_SYMBOL(omap_set_dma_transfer_params);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000275
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100276void omap_set_dma_color_mode(int lch, enum omap_dma_color_mode mode, u32 color)
277{
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278 BUG_ON(omap_dma_in_1510_mode());
279
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700280 if (cpu_class_is_omap1()) {
281 u16 w;
282
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800283 w = p->dma_read(CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700284 w &= ~0x03;
285
286 switch (mode) {
287 case OMAP_DMA_CONSTANT_FILL:
288 w |= 0x01;
289 break;
290 case OMAP_DMA_TRANSPARENT_COPY:
291 w |= 0x02;
292 break;
293 case OMAP_DMA_COLOR_DIS:
294 break;
295 default:
296 BUG();
297 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800298 p->dma_write(w, CCR2, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700299
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800300 w = p->dma_read(LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700301 w &= ~0x0f;
302 /* Default is channel type 2D */
303 if (mode) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800304 p->dma_write(color, COLOR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700305 w |= 1; /* Channel type G */
306 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800307 p->dma_write(w, LCH_CTRL, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700308 }
309
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800310 if (cpu_class_is_omap2()) {
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700311 u32 val;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000312
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800313 val = p->dma_read(CCR, lch);
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700314 val &= ~((1 << 17) | (1 << 16));
Tony Lindgren0499bde2008-07-03 12:24:36 +0300315
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700316 switch (mode) {
317 case OMAP_DMA_CONSTANT_FILL:
318 val |= 1 << 16;
319 break;
320 case OMAP_DMA_TRANSPARENT_COPY:
321 val |= 1 << 17;
322 break;
323 case OMAP_DMA_COLOR_DIS:
324 break;
325 default:
326 BUG();
327 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800328 p->dma_write(val, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100329
Tomi Valkeinen0815f8e2009-05-28 13:23:51 -0700330 color &= 0xffffff;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800331 p->dma_write(color, COLOR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100332 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100333}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300334EXPORT_SYMBOL(omap_set_dma_color_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100335
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300336void omap_set_dma_write_mode(int lch, enum omap_dma_write_mode mode)
337{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800338 if (cpu_class_is_omap2()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300339 u32 csdp;
340
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800341 csdp = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300342 csdp &= ~(0x3 << 16);
343 csdp |= (mode << 16);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800344 p->dma_write(csdp, CSDP, lch);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300345 }
346}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300347EXPORT_SYMBOL(omap_set_dma_write_mode);
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300348
Tony Lindgren0499bde2008-07-03 12:24:36 +0300349void omap_set_dma_channel_mode(int lch, enum omap_dma_channel_mode mode)
350{
351 if (cpu_class_is_omap1() && !cpu_is_omap15xx()) {
352 u32 l;
353
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800354 l = p->dma_read(LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300355 l &= ~0x7;
356 l |= mode;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800357 p->dma_write(l, LCH_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300358 }
359}
360EXPORT_SYMBOL(omap_set_dma_channel_mode);
361
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000362/* Note that src_port is only for omap1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100363void omap_set_dma_src_params(int lch, int src_port, int src_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000364 unsigned long src_start,
365 int src_ei, int src_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100366{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300367 u32 l;
368
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000369 if (cpu_class_is_omap1()) {
Tony Lindgren0499bde2008-07-03 12:24:36 +0300370 u16 w;
371
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800372 w = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300373 w &= ~(0x1f << 2);
374 w |= src_port << 2;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800375 p->dma_write(w, CSDP, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300376 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300377
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800378 l = p->dma_read(CCR, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300379 l &= ~(0x03 << 12);
380 l |= src_amode << 12;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800381 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300382
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800383 p->dma_write(src_start, CSSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800385 p->dma_write(src_ei, CSEI, lch);
386 p->dma_write(src_fi, CSFI, lch);
Tony Lindgren97b7f712008-07-03 12:24:37 +0300387}
388EXPORT_SYMBOL(omap_set_dma_src_params);
389
390void omap_set_dma_params(int lch, struct omap_dma_channel_params *params)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000391{
392 omap_set_dma_transfer_params(lch, params->data_type,
393 params->elem_count, params->frame_count,
394 params->sync_mode, params->trigger,
395 params->src_or_dst_synch);
396 omap_set_dma_src_params(lch, params->src_port,
397 params->src_amode, params->src_start,
398 params->src_ei, params->src_fi);
399
400 omap_set_dma_dest_params(lch, params->dst_port,
401 params->dst_amode, params->dst_start,
402 params->dst_ei, params->dst_fi);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800403 if (params->read_prio || params->write_prio)
404 omap_dma_set_prio_lch(lch, params->read_prio,
405 params->write_prio);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100406}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300407EXPORT_SYMBOL(omap_set_dma_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100408
409void omap_set_dma_src_index(int lch, int eidx, int fidx)
410{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300411 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000412 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300413
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800414 p->dma_write(eidx, CSEI, lch);
415 p->dma_write(fidx, CSFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100416}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300417EXPORT_SYMBOL(omap_set_dma_src_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100418
419void omap_set_dma_src_data_pack(int lch, int enable)
420{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300421 u32 l;
422
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800423 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300424 l &= ~(1 << 6);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000425 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300426 l |= (1 << 6);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800427 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100428}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300429EXPORT_SYMBOL(omap_set_dma_src_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100430
431void omap_set_dma_src_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
432{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700433 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300434 u32 l;
435
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800436 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300437 l &= ~(0x03 << 7);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100438
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100439 switch (burst_mode) {
440 case OMAP_DMA_DATA_BURST_DIS:
441 break;
442 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800443 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700444 burst = 0x1;
445 else
446 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100447 break;
448 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800449 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700450 burst = 0x2;
451 break;
452 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700453 /*
454 * not supported by current hardware on OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455 * w |= (0x03 << 7);
456 * fall through
457 */
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700458 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800459 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700460 burst = 0x3;
461 break;
462 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700463 /*
464 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700465 * fall through
466 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467 default:
468 BUG();
469 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300470
471 l |= (burst << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800472 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100473}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300474EXPORT_SYMBOL(omap_set_dma_src_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000476/* Note that dest_port is only for OMAP1 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100477void omap_set_dma_dest_params(int lch, int dest_port, int dest_amode,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000478 unsigned long dest_start,
479 int dst_ei, int dst_fi)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300481 u32 l;
482
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000483 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800484 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300485 l &= ~(0x1f << 9);
486 l |= dest_port << 9;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800487 p->dma_write(l, CSDP, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000488 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100489
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800490 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300491 l &= ~(0x03 << 14);
492 l |= dest_amode << 14;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800493 p->dma_write(l, CCR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800495 p->dma_write(dest_start, CDSA, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100496
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800497 p->dma_write(dst_ei, CDEI, lch);
498 p->dma_write(dst_fi, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300500EXPORT_SYMBOL(omap_set_dma_dest_params);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100501
502void omap_set_dma_dest_index(int lch, int eidx, int fidx)
503{
Tony Lindgren97b7f712008-07-03 12:24:37 +0300504 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000505 return;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300506
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800507 p->dma_write(eidx, CDEI, lch);
508 p->dma_write(fidx, CDFI, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100509}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300510EXPORT_SYMBOL(omap_set_dma_dest_index);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100511
512void omap_set_dma_dest_data_pack(int lch, int enable)
513{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300514 u32 l;
515
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800516 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300517 l &= ~(1 << 13);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000518 if (enable)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300519 l |= 1 << 13;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800520 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100521}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300522EXPORT_SYMBOL(omap_set_dma_dest_data_pack);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100523
524void omap_set_dma_dest_burst_mode(int lch, enum omap_dma_burst_mode burst_mode)
525{
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700526 unsigned int burst = 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300527 u32 l;
528
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800529 l = p->dma_read(CSDP, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300530 l &= ~(0x03 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100531
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100532 switch (burst_mode) {
533 case OMAP_DMA_DATA_BURST_DIS:
534 break;
535 case OMAP_DMA_DATA_BURST_4:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800536 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700537 burst = 0x1;
538 else
539 burst = 0x2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100540 break;
541 case OMAP_DMA_DATA_BURST_8:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800542 if (cpu_class_is_omap2())
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700543 burst = 0x2;
544 else
545 burst = 0x3;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100546 break;
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700547 case OMAP_DMA_DATA_BURST_16:
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800548 if (cpu_class_is_omap2()) {
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700549 burst = 0x3;
550 break;
551 }
manjugk manjugkea221a62010-05-14 12:05:25 -0700552 /*
553 * OMAP1 don't support burst 16
Kyungmin Park6dc3c8f2006-06-26 16:16:14 -0700554 * fall through
555 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556 default:
557 printk(KERN_ERR "Invalid DMA burst mode\n");
558 BUG();
559 return;
560 }
Tony Lindgren0499bde2008-07-03 12:24:36 +0300561 l |= (burst << 14);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800562 p->dma_write(l, CSDP, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100563}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300564EXPORT_SYMBOL(omap_set_dma_dest_burst_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100565
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000566static inline void omap_enable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100567{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000568 u32 status;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100569
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700570 /* Clear CSR */
571 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800572 status = p->dma_read(CSR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800573 else if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800574 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000575
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100576 /* Enable some nice interrupts. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800577 p->dma_write(dma_chan[lch].enabled_irqs, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100578}
579
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000580static void omap_disable_channel_irq(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100581{
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800582 if (cpu_class_is_omap2())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800583 p->dma_write(0, CICR, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100584}
585
586void omap_enable_dma_irq(int lch, u16 bits)
587{
588 dma_chan[lch].enabled_irqs |= bits;
589}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300590EXPORT_SYMBOL(omap_enable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100591
592void omap_disable_dma_irq(int lch, u16 bits)
593{
594 dma_chan[lch].enabled_irqs &= ~bits;
595}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300596EXPORT_SYMBOL(omap_disable_dma_irq);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100597
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000598static inline void enable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100599{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300600 u32 l;
601
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800602 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300603
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000604 if (cpu_class_is_omap1())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300605 l &= ~(1 << 14);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100606
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000607 /* Set the ENABLE_LNK bits */
608 if (dma_chan[lch].next_lch != -1)
Tony Lindgren0499bde2008-07-03 12:24:36 +0300609 l = dma_chan[lch].next_lch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800610
611#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300612 if (cpu_class_is_omap2())
613 if (dma_chan[lch].next_linked_ch != -1)
614 l = dma_chan[lch].next_linked_ch | (1 << 15);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800615#endif
Tony Lindgren0499bde2008-07-03 12:24:36 +0300616
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800617 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100618}
619
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000620static inline void disable_lnk(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100621{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300622 u32 l;
623
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800624 l = p->dma_read(CLNK_CTRL, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300625
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000626 /* Disable interrupts */
627 if (cpu_class_is_omap1()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800628 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000629 /* Set the STOP_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300630 l |= 1 << 14;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100631 }
632
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800633 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000634 omap_disable_channel_irq(lch);
635 /* Clear the ENABLE_LNK bit */
Tony Lindgren0499bde2008-07-03 12:24:36 +0300636 l &= ~(1 << 15);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000637 }
638
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800639 p->dma_write(l, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000640 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
641}
642
643static inline void omap2_enable_irq_lch(int lch)
644{
645 u32 val;
Tao Huee907322009-11-10 18:55:17 -0800646 unsigned long flags;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000647
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800648 if (!cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000649 return;
650
Tao Huee907322009-11-10 18:55:17 -0800651 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800652 val = p->dma_read(IRQENABLE_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000653 val |= 1 << lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800654 p->dma_write(val, IRQENABLE_L0, lch);
Tao Huee907322009-11-10 18:55:17 -0800655 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656}
657
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700658static inline void omap2_disable_irq_lch(int lch)
659{
660 u32 val;
661 unsigned long flags;
662
663 if (!cpu_class_is_omap2())
664 return;
665
666 spin_lock_irqsave(&dma_chan_lock, flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800667 val = p->dma_read(IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700668 val &= ~(1 << lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800669 p->dma_write(val, IRQENABLE_L0, lch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700670 spin_unlock_irqrestore(&dma_chan_lock, flags);
671}
672
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673int omap_request_dma(int dev_id, const char *dev_name,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300674 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675 void *data, int *dma_ch_out)
676{
677 int ch, free_ch = -1;
678 unsigned long flags;
679 struct omap_dma_lch *chan;
680
681 spin_lock_irqsave(&dma_chan_lock, flags);
682 for (ch = 0; ch < dma_chan_count; ch++) {
683 if (free_ch == -1 && dma_chan[ch].dev_id == -1) {
684 free_ch = ch;
685 if (dev_id == 0)
686 break;
687 }
688 }
689 if (free_ch == -1) {
690 spin_unlock_irqrestore(&dma_chan_lock, flags);
691 return -EBUSY;
692 }
693 chan = dma_chan + free_ch;
694 chan->dev_id = dev_id;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000695
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800696 if (p->clear_lch_regs)
697 p->clear_lch_regs(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000698
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800699 if (cpu_class_is_omap2())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000700 omap_clear_dma(free_ch);
701
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702 spin_unlock_irqrestore(&dma_chan_lock, flags);
703
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100704 chan->dev_name = dev_name;
705 chan->callback = callback;
706 chan->data = data;
Jarkko Nikulaa92fda12009-01-29 08:57:12 -0800707 chan->flags = 0;
Tony Lindgren97b7f712008-07-03 12:24:37 +0300708
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800709#ifndef CONFIG_ARCH_OMAP1
Tony Lindgren97b7f712008-07-03 12:24:37 +0300710 if (cpu_class_is_omap2()) {
711 chan->chain_id = -1;
712 chan->next_linked_ch = -1;
713 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800714#endif
Tony Lindgren97b7f712008-07-03 12:24:37 +0300715
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700716 chan->enabled_irqs = OMAP_DMA_DROP_IRQ | OMAP_DMA_BLOCK_IRQ;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000717
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700718 if (cpu_class_is_omap1())
719 chan->enabled_irqs |= OMAP1_DMA_TOUT_IRQ;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800720 else if (cpu_class_is_omap2())
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700721 chan->enabled_irqs |= OMAP2_DMA_MISALIGNED_ERR_IRQ |
722 OMAP2_DMA_TRANS_ERR_IRQ;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723
724 if (cpu_is_omap16xx()) {
725 /* If the sync device is set, configure it dynamically. */
726 if (dev_id != 0) {
727 set_gdma_dev(free_ch + 1, dev_id);
728 dev_id = free_ch + 1;
729 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300730 /*
731 * Disable the 1510 compatibility mode and set the sync device
732 * id.
733 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800734 p->dma_write(dev_id | (1 << 10), CCR, free_ch);
Zebediah C. McClure557096f2009-03-23 18:07:44 -0700735 } else if (cpu_is_omap7xx() || cpu_is_omap15xx()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800736 p->dma_write(dev_id, CCR, free_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100737 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000738
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800739 if (cpu_class_is_omap2()) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000740 omap2_enable_irq_lch(free_ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000741 omap_enable_channel_irq(free_ch);
742 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800743 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, free_ch);
744 p->dma_write(1 << free_ch, IRQSTATUS_L0, 0);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000745 }
746
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747 *dma_ch_out = free_ch;
748
749 return 0;
750}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300751EXPORT_SYMBOL(omap_request_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000753void omap_free_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754{
755 unsigned long flags;
756
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000757 if (dma_chan[lch].dev_id == -1) {
Tony Lindgren97b7f712008-07-03 12:24:37 +0300758 pr_err("omap_dma: trying to free unallocated DMA channel %d\n",
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000759 lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760 return;
761 }
Tony Lindgren97b7f712008-07-03 12:24:37 +0300762
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000763 if (cpu_class_is_omap1()) {
764 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800765 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000766 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800767 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000768 }
769
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800770 if (cpu_class_is_omap2()) {
Mika Westerbergada8d4a2010-05-14 12:05:25 -0700771 omap2_disable_irq_lch(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000772
773 /* Clear the CSR register and IRQ status register */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800774 p->dma_write(OMAP2_DMA_CSR_CLEAR_MASK, CSR, lch);
775 p->dma_write(1 << lch, IRQSTATUS_L0, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000776
777 /* Disable all DMA interrupts for the channel. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800778 p->dma_write(0, CICR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000779
780 /* Make sure the DMA transfer is stopped. */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800781 p->dma_write(0, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000782 omap_clear_dma(lch);
783 }
Santosh Shilimkarda1b94e2009-04-23 11:10:40 -0700784
785 spin_lock_irqsave(&dma_chan_lock, flags);
786 dma_chan[lch].dev_id = -1;
787 dma_chan[lch].next_lch = -1;
788 dma_chan[lch].callback = NULL;
789 spin_unlock_irqrestore(&dma_chan_lock, flags);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100790}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300791EXPORT_SYMBOL(omap_free_dma);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800793/**
794 * @brief omap_dma_set_global_params : Set global priority settings for dma
795 *
796 * @param arb_rate
797 * @param max_fifo_depth
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700798 * @param tparams - Number of threads to reserve : DMA_THREAD_RESERVE_NORM
799 * DMA_THREAD_RESERVE_ONET
800 * DMA_THREAD_RESERVE_TWOT
801 * DMA_THREAD_RESERVE_THREET
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800802 */
803void
804omap_dma_set_global_params(int arb_rate, int max_fifo_depth, int tparams)
805{
806 u32 reg;
807
808 if (!cpu_class_is_omap2()) {
Harvey Harrison8e86f422008-03-04 15:08:02 -0800809 printk(KERN_ERR "FIXME: no %s on 15xx/16xx\n", __func__);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800810 return;
811 }
812
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700813 if (max_fifo_depth == 0)
814 max_fifo_depth = 1;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800815 if (arb_rate == 0)
816 arb_rate = 1;
817
Anuj Aggarwal70cf6442009-10-14 09:56:34 -0700818 reg = 0xff & max_fifo_depth;
819 reg |= (0x3 & tparams) << 12;
820 reg |= (arb_rate & 0xff) << 16;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800821
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800822 p->dma_write(reg, GCR, 0);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800823}
824EXPORT_SYMBOL(omap_dma_set_global_params);
825
826/**
827 * @brief omap_dma_set_prio_lch : Set channel wise priority settings
828 *
829 * @param lch
830 * @param read_prio - Read priority
831 * @param write_prio - Write priority
832 * Both of the above can be set with one of the following values :
833 * DMA_CH_PRIO_HIGH/DMA_CH_PRIO_LOW
834 */
835int
836omap_dma_set_prio_lch(int lch, unsigned char read_prio,
837 unsigned char write_prio)
838{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300839 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800840
Tony Lindgren4d963722008-07-03 12:24:31 +0300841 if (unlikely((lch < 0 || lch >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800842 printk(KERN_ERR "Invalid channel id\n");
843 return -EINVAL;
844 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800845 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300846 l &= ~((1 << 6) | (1 << 26));
Santosh Shilimkar44169072009-05-28 14:16:04 -0700847 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300848 l |= ((read_prio & 0x1) << 6) | ((write_prio & 0x1) << 26);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800849 else
Tony Lindgren0499bde2008-07-03 12:24:36 +0300850 l |= ((read_prio & 0x1) << 6);
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800851
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800852 p->dma_write(l, CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300853
Anand Gadiyarf8151e52007-12-01 12:14:11 -0800854 return 0;
855}
856EXPORT_SYMBOL(omap_dma_set_prio_lch);
857
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000858/*
859 * Clears any DMA state so the DMA engine is ready to restart with new buffers
860 * through omap_start_dma(). Any buffers in flight are discarded.
861 */
862void omap_clear_dma(int lch)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100863{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000864 unsigned long flags;
865
866 local_irq_save(flags);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800867 p->clear_dma(lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000868 local_irq_restore(flags);
869}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300870EXPORT_SYMBOL(omap_clear_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000871
872void omap_start_dma(int lch)
873{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300874 u32 l;
875
manjugk manjugk519e6162010-03-04 07:11:56 +0000876 /*
877 * The CPC/CDAC register needs to be initialized to zero
878 * before starting dma transfer.
879 */
880 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800881 p->dma_write(0, CPC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000882 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800883 p->dma_write(0, CDAC, lch);
manjugk manjugk519e6162010-03-04 07:11:56 +0000884
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000885 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
886 int next_lch, cur_lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800887 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000888
889 dma_chan_link_map[lch] = 1;
890 /* Set the link register of the first channel */
891 enable_lnk(lch);
892
893 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
894 cur_lch = dma_chan[lch].next_lch;
895 do {
896 next_lch = dma_chan[cur_lch].next_lch;
897
898 /* The loop case: we've been here already */
899 if (dma_chan_link_map[cur_lch])
900 break;
901 /* Mark the current channel */
902 dma_chan_link_map[cur_lch] = 1;
903
904 enable_lnk(cur_lch);
905 omap_enable_channel_irq(cur_lch);
906
907 cur_lch = next_lch;
908 } while (next_lch != -1);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800909 } else if (IS_DMA_ERRATA(DMA_ERRATA_PARALLEL_CHANNELS))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800910 p->dma_write(lch, CLNK_CTRL, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000911
912 omap_enable_channel_irq(lch);
913
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800914 l = p->dma_read(CCR, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300915
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800916 if (IS_DMA_ERRATA(DMA_ERRATA_IFRAME_BUFFERING))
917 l |= OMAP_DMA_CCR_BUFFERING_DISABLE;
Tony Lindgren0499bde2008-07-03 12:24:36 +0300918 l |= OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800919
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800920 p->dma_write(l, CCR, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000921
922 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
923}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300924EXPORT_SYMBOL(omap_start_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000925
926void omap_stop_dma(int lch)
927{
Tony Lindgren0499bde2008-07-03 12:24:36 +0300928 u32 l;
929
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700930 /* Disable all interrupts on the channel */
931 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800932 p->dma_write(0, CICR, lch);
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700933
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800934 l = p->dma_read(CCR, lch);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -0800935 if (IS_DMA_ERRATA(DMA_ERRATA_i541) &&
936 (l & OMAP_DMA_CCR_SEL_SRC_DST_SYNC)) {
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700937 int i = 0;
938 u32 sys_cf;
939
940 /* Configure No-Standby */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800941 l = p->dma_read(OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700942 sys_cf = l;
943 l &= ~DMA_SYSCONFIG_MIDLEMODE_MASK;
944 l |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800945 p->dma_write(l , OCP_SYSCONFIG, 0);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700946
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800947 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700948 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800949 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700950
951 /* Wait for sDMA FIFO drain */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800952 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700953 while (i < 100 && (l & (OMAP_DMA_CCR_RD_ACTIVE |
954 OMAP_DMA_CCR_WR_ACTIVE))) {
955 udelay(5);
956 i++;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800957 l = p->dma_read(CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700958 }
959 if (i >= 100)
960 printk(KERN_ERR "DMA drain did not complete on "
961 "lch %d\n", lch);
962 /* Restore OCP_SYSCONFIG */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800963 p->dma_write(sys_cf, OCP_SYSCONFIG, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700964 } else {
965 l &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800966 p->dma_write(l, CCR, lch);
Peter Ujfalusi0e4905c2010-10-11 14:18:56 -0700967 }
Santosh Shilimkar9da65a92009-10-22 14:46:31 -0700968
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000969 if (!omap_dma_in_1510_mode() && dma_chan[lch].next_lch != -1) {
970 int next_lch, cur_lch = lch;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -0800971 char dma_chan_link_map[dma_lch_count];
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000972
973 memset(dma_chan_link_map, 0, sizeof(dma_chan_link_map));
974 do {
975 /* The loop case: we've been here already */
976 if (dma_chan_link_map[cur_lch])
977 break;
978 /* Mark the current channel */
979 dma_chan_link_map[cur_lch] = 1;
980
981 disable_lnk(cur_lch);
982
983 next_lch = dma_chan[cur_lch].next_lch;
984 cur_lch = next_lch;
985 } while (next_lch != -1);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000986 }
987
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000988 dma_chan[lch].flags &= ~OMAP_DMA_ACTIVE;
989}
Tony Lindgren97b7f712008-07-03 12:24:37 +0300990EXPORT_SYMBOL(omap_stop_dma);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000991
992/*
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300993 * Allows changing the DMA callback function or data. This may be needed if
994 * the driver shares a single DMA channel for multiple dma triggers.
995 */
996int omap_set_dma_callback(int lch,
Tony Lindgren97b7f712008-07-03 12:24:37 +0300997 void (*callback)(int lch, u16 ch_status, void *data),
Tony Lindgren709eb3e52006-09-25 12:45:45 +0300998 void *data)
999{
1000 unsigned long flags;
1001
1002 if (lch < 0)
1003 return -ENODEV;
1004
1005 spin_lock_irqsave(&dma_chan_lock, flags);
1006 if (dma_chan[lch].dev_id == -1) {
1007 printk(KERN_ERR "DMA callback for not set for free channel\n");
1008 spin_unlock_irqrestore(&dma_chan_lock, flags);
1009 return -EINVAL;
1010 }
1011 dma_chan[lch].callback = callback;
1012 dma_chan[lch].data = data;
1013 spin_unlock_irqrestore(&dma_chan_lock, flags);
1014
1015 return 0;
1016}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001017EXPORT_SYMBOL(omap_set_dma_callback);
Tony Lindgren709eb3e52006-09-25 12:45:45 +03001018
1019/*
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001020 * Returns current physical source address for the given DMA channel.
1021 * If the channel is running the caller must disable interrupts prior calling
1022 * this function and process the returned value before re-enabling interrupt to
1023 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001024 * is a chance for CSSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001025 * in incorrect return value.
1026 */
1027dma_addr_t omap_get_dma_src_pos(int lch)
1028{
Tony Lindgren0695de32007-05-07 18:24:14 -07001029 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001030
Tony Lindgren0499bde2008-07-03 12:24:36 +03001031 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001032 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001033 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001034 offset = p->dma_read(CSAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001035
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001036 if (IS_DMA_ERRATA(DMA_ERRATA_3_3) && offset == 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001037 offset = p->dma_read(CSAC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001038
Peter Ujfalusi7ba96682011-12-09 13:38:00 -08001039 if (!cpu_is_omap15xx()) {
1040 /*
1041 * CDAC == 0 indicates that the DMA transfer on the channel has
1042 * not been started (no data has been transferred so far).
1043 * Return the programmed source start address in this case.
1044 */
1045 if (likely(p->dma_read(CDAC, lch)))
1046 offset = p->dma_read(CSAC, lch);
1047 else
1048 offset = p->dma_read(CSSA, lch);
1049 }
1050
Tony Lindgren0499bde2008-07-03 12:24:36 +03001051 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001052 offset |= (p->dma_read(CSSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001053
1054 return offset;
1055}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001056EXPORT_SYMBOL(omap_get_dma_src_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001057
1058/*
1059 * Returns current physical destination address for the given DMA channel.
1060 * If the channel is running the caller must disable interrupts prior calling
1061 * this function and process the returned value before re-enabling interrupt to
1062 * prevent races with the interrupt handler. Note that in continuous mode there
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001063 * is a chance for CDSA_L register overflow between the two reads resulting
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001064 * in incorrect return value.
1065 */
1066dma_addr_t omap_get_dma_dst_pos(int lch)
1067{
Tony Lindgren0695de32007-05-07 18:24:14 -07001068 dma_addr_t offset = 0;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001069
Tony Lindgren0499bde2008-07-03 12:24:36 +03001070 if (cpu_is_omap15xx())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001071 offset = p->dma_read(CPC, lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001072 else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001073 offset = p->dma_read(CDAC, lch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001074
Tony Lindgren0499bde2008-07-03 12:24:36 +03001075 /*
1076 * omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
1077 * read before the DMA controller finished disabling the channel.
1078 */
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001079 if (!cpu_is_omap15xx() && offset == 0) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001080 offset = p->dma_read(CDAC, lch);
Peter Ujfalusi06e80772011-12-09 13:38:00 -08001081 /*
1082 * CDAC == 0 indicates that the DMA transfer on the channel has
1083 * not been started (no data has been transferred so far).
1084 * Return the programmed destination start address in this case.
1085 */
1086 if (unlikely(!offset))
1087 offset = p->dma_read(CDSA, lch);
1088 }
Tony Lindgren0499bde2008-07-03 12:24:36 +03001089
1090 if (cpu_class_is_omap1())
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001091 offset |= (p->dma_read(CDSA, lch) & 0xFFFF0000);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001092
1093 return offset;
1094}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001095EXPORT_SYMBOL(omap_get_dma_dst_pos);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001096
Tony Lindgren0499bde2008-07-03 12:24:36 +03001097int omap_get_dma_active_status(int lch)
1098{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001099 return (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN) != 0;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001100}
1101EXPORT_SYMBOL(omap_get_dma_active_status);
1102
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001103int omap_dma_running(void)
1104{
1105 int lch;
1106
Janusz Krzysztofikf8e9e982009-12-11 16:16:33 -08001107 if (cpu_class_is_omap1())
1108 if (omap_lcd_dma_running())
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001109 return 1;
1110
1111 for (lch = 0; lch < dma_chan_count; lch++)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001112 if (p->dma_read(CCR, lch) & OMAP_DMA_CCR_EN)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001113 return 1;
1114
1115 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001116}
1117
1118/*
1119 * lch_queue DMA will start right after lch_head one is finished.
1120 * For this DMA link to start, you still need to start (see omap_start_dma)
1121 * the first one. That will fire up the entire queue.
1122 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001123void omap_dma_link_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001124{
1125 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001126 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001127 p->dma_write(p->dma_read(CCR, lch_head) | (3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001128 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001129 return;
1130 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001131 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1132 BUG();
1133 return;
1134 }
1135
1136 if ((dma_chan[lch_head].dev_id == -1) ||
1137 (dma_chan[lch_queue].dev_id == -1)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001138 printk(KERN_ERR "omap_dma: trying to link "
1139 "non requested channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001140 dump_stack();
1141 }
1142
1143 dma_chan[lch_head].next_lch = lch_queue;
1144}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001145EXPORT_SYMBOL(omap_dma_link_lch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001146
1147/*
1148 * Once the DMA queue is stopped, we can destroy it.
1149 */
Tony Lindgren97b7f712008-07-03 12:24:37 +03001150void omap_dma_unlink_lch(int lch_head, int lch_queue)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001151{
1152 if (omap_dma_in_1510_mode()) {
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001153 if (lch_head == lch_queue) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001154 p->dma_write(p->dma_read(CCR, lch_head) & ~(3 << 8),
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001155 CCR, lch_head);
Janusz Krzysztofik9f0f4ae2009-08-23 17:56:12 +02001156 return;
1157 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001158 printk(KERN_ERR "DMA linking is not supported in 1510 mode\n");
1159 BUG();
1160 return;
1161 }
1162
1163 if (dma_chan[lch_head].next_lch != lch_queue ||
1164 dma_chan[lch_head].next_lch == -1) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001165 printk(KERN_ERR "omap_dma: trying to unlink "
1166 "non linked channels\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001167 dump_stack();
1168 }
1169
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001170 if ((dma_chan[lch_head].flags & OMAP_DMA_ACTIVE) ||
Roel Kluin247421f2010-01-13 18:10:29 -08001171 (dma_chan[lch_queue].flags & OMAP_DMA_ACTIVE)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001172 printk(KERN_ERR "omap_dma: You need to stop the DMA channels "
1173 "before unlinking\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001174 dump_stack();
1175 }
1176
1177 dma_chan[lch_head].next_lch = -1;
1178}
Tony Lindgren97b7f712008-07-03 12:24:37 +03001179EXPORT_SYMBOL(omap_dma_unlink_lch);
1180
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001181#ifndef CONFIG_ARCH_OMAP1
1182/* Create chain of DMA channesls */
1183static void create_dma_lch_chain(int lch_head, int lch_queue)
1184{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001185 u32 l;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001186
1187 /* Check if this is the first link in chain */
1188 if (dma_chan[lch_head].next_linked_ch == -1) {
1189 dma_chan[lch_head].next_linked_ch = lch_queue;
1190 dma_chan[lch_head].prev_linked_ch = lch_queue;
1191 dma_chan[lch_queue].next_linked_ch = lch_head;
1192 dma_chan[lch_queue].prev_linked_ch = lch_head;
1193 }
1194
1195 /* a link exists, link the new channel in circular chain */
1196 else {
1197 dma_chan[lch_queue].next_linked_ch =
1198 dma_chan[lch_head].next_linked_ch;
1199 dma_chan[lch_queue].prev_linked_ch = lch_head;
1200 dma_chan[lch_head].next_linked_ch = lch_queue;
1201 dma_chan[dma_chan[lch_queue].next_linked_ch].prev_linked_ch =
1202 lch_queue;
1203 }
1204
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001205 l = p->dma_read(CLNK_CTRL, lch_head);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001206 l &= ~(0x1f);
1207 l |= lch_queue;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001208 p->dma_write(l, CLNK_CTRL, lch_head);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001209
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001210 l = p->dma_read(CLNK_CTRL, lch_queue);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001211 l &= ~(0x1f);
1212 l |= (dma_chan[lch_queue].next_linked_ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001213 p->dma_write(l, CLNK_CTRL, lch_queue);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001214}
1215
1216/**
1217 * @brief omap_request_dma_chain : Request a chain of DMA channels
1218 *
1219 * @param dev_id - Device id using the dma channel
1220 * @param dev_name - Device name
1221 * @param callback - Call back function
1222 * @chain_id -
1223 * @no_of_chans - Number of channels requested
1224 * @chain_mode - Dynamic or static chaining : OMAP_DMA_STATIC_CHAIN
1225 * OMAP_DMA_DYNAMIC_CHAIN
1226 * @params - Channel parameters
1227 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001228 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001229 * Failure: -EINVAL/-ENOMEM
1230 */
1231int omap_request_dma_chain(int dev_id, const char *dev_name,
Santosh Shilimkar279b9182009-05-28 13:23:52 -07001232 void (*callback) (int lch, u16 ch_status,
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001233 void *data),
1234 int *chain_id, int no_of_chans, int chain_mode,
1235 struct omap_dma_channel_params params)
1236{
1237 int *channels;
1238 int i, err;
1239
1240 /* Is the chain mode valid ? */
1241 if (chain_mode != OMAP_DMA_STATIC_CHAIN
1242 && chain_mode != OMAP_DMA_DYNAMIC_CHAIN) {
1243 printk(KERN_ERR "Invalid chain mode requested\n");
1244 return -EINVAL;
1245 }
1246
1247 if (unlikely((no_of_chans < 1
Tony Lindgren4d963722008-07-03 12:24:31 +03001248 || no_of_chans > dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001249 printk(KERN_ERR "Invalid Number of channels requested\n");
1250 return -EINVAL;
1251 }
1252
manjugk manjugkea221a62010-05-14 12:05:25 -07001253 /*
1254 * Allocate a queue to maintain the status of the channels
1255 * in the chain
1256 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001257 channels = kmalloc(sizeof(*channels) * no_of_chans, GFP_KERNEL);
1258 if (channels == NULL) {
1259 printk(KERN_ERR "omap_dma: No memory for channel queue\n");
1260 return -ENOMEM;
1261 }
1262
1263 /* request and reserve DMA channels for the chain */
1264 for (i = 0; i < no_of_chans; i++) {
1265 err = omap_request_dma(dev_id, dev_name,
Russell Kingc0fc18c52008-09-05 15:10:27 +01001266 callback, NULL, &channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001267 if (err < 0) {
1268 int j;
1269 for (j = 0; j < i; j++)
1270 omap_free_dma(channels[j]);
1271 kfree(channels);
1272 printk(KERN_ERR "omap_dma: Request failed %d\n", err);
1273 return err;
1274 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001275 dma_chan[channels[i]].prev_linked_ch = -1;
1276 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1277
1278 /*
1279 * Allowing client drivers to set common parameters now,
1280 * so that later only relevant (src_start, dest_start
1281 * and element count) can be set
1282 */
1283 omap_set_dma_params(channels[i], &params);
1284 }
1285
1286 *chain_id = channels[0];
1287 dma_linked_lch[*chain_id].linked_dmach_q = channels;
1288 dma_linked_lch[*chain_id].chain_mode = chain_mode;
1289 dma_linked_lch[*chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1290 dma_linked_lch[*chain_id].no_of_lchs_linked = no_of_chans;
1291
1292 for (i = 0; i < no_of_chans; i++)
1293 dma_chan[channels[i]].chain_id = *chain_id;
1294
1295 /* Reset the Queue pointers */
1296 OMAP_DMA_CHAIN_QINIT(*chain_id);
1297
1298 /* Set up the chain */
1299 if (no_of_chans == 1)
1300 create_dma_lch_chain(channels[0], channels[0]);
1301 else {
1302 for (i = 0; i < (no_of_chans - 1); i++)
1303 create_dma_lch_chain(channels[i], channels[i + 1]);
1304 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001305
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001306 return 0;
1307}
1308EXPORT_SYMBOL(omap_request_dma_chain);
1309
1310/**
1311 * @brief omap_modify_dma_chain_param : Modify the chain's params - Modify the
1312 * params after setting it. Dont do this while dma is running!!
1313 *
1314 * @param chain_id - Chained logical channel id.
1315 * @param params
1316 *
1317 * @return - Success : 0
1318 * Failure : -EINVAL
1319 */
1320int omap_modify_dma_chain_params(int chain_id,
1321 struct omap_dma_channel_params params)
1322{
1323 int *channels;
1324 u32 i;
1325
1326 /* Check for input params */
1327 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001328 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001329 printk(KERN_ERR "Invalid chain id\n");
1330 return -EINVAL;
1331 }
1332
1333 /* Check if the chain exists */
1334 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1335 printk(KERN_ERR "Chain doesn't exists\n");
1336 return -EINVAL;
1337 }
1338 channels = dma_linked_lch[chain_id].linked_dmach_q;
1339
1340 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1341 /*
1342 * Allowing client drivers to set common parameters now,
1343 * so that later only relevant (src_start, dest_start
1344 * and element count) can be set
1345 */
1346 omap_set_dma_params(channels[i], &params);
1347 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001348
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001349 return 0;
1350}
1351EXPORT_SYMBOL(omap_modify_dma_chain_params);
1352
1353/**
1354 * @brief omap_free_dma_chain - Free all the logical channels in a chain.
1355 *
1356 * @param chain_id
1357 *
1358 * @return - Success : 0
1359 * Failure : -EINVAL
1360 */
1361int omap_free_dma_chain(int chain_id)
1362{
1363 int *channels;
1364 u32 i;
1365
1366 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001367 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001368 printk(KERN_ERR "Invalid chain id\n");
1369 return -EINVAL;
1370 }
1371
1372 /* Check if the chain exists */
1373 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1374 printk(KERN_ERR "Chain doesn't exists\n");
1375 return -EINVAL;
1376 }
1377
1378 channels = dma_linked_lch[chain_id].linked_dmach_q;
1379 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1380 dma_chan[channels[i]].next_linked_ch = -1;
1381 dma_chan[channels[i]].prev_linked_ch = -1;
1382 dma_chan[channels[i]].chain_id = -1;
1383 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1384 omap_free_dma(channels[i]);
1385 }
1386
1387 kfree(channels);
1388
1389 dma_linked_lch[chain_id].linked_dmach_q = NULL;
1390 dma_linked_lch[chain_id].chain_mode = -1;
1391 dma_linked_lch[chain_id].chain_state = -1;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001392
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001393 return (0);
1394}
1395EXPORT_SYMBOL(omap_free_dma_chain);
1396
1397/**
1398 * @brief omap_dma_chain_status - Check if the chain is in
1399 * active / inactive state.
1400 * @param chain_id
1401 *
1402 * @return - Success : OMAP_DMA_CHAIN_ACTIVE/OMAP_DMA_CHAIN_INACTIVE
1403 * Failure : -EINVAL
1404 */
1405int omap_dma_chain_status(int chain_id)
1406{
1407 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001408 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001409 printk(KERN_ERR "Invalid chain id\n");
1410 return -EINVAL;
1411 }
1412
1413 /* Check if the chain exists */
1414 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1415 printk(KERN_ERR "Chain doesn't exists\n");
1416 return -EINVAL;
1417 }
1418 pr_debug("CHAINID=%d, qcnt=%d\n", chain_id,
1419 dma_linked_lch[chain_id].q_count);
1420
1421 if (OMAP_DMA_CHAIN_QEMPTY(chain_id))
1422 return OMAP_DMA_CHAIN_INACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001423
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001424 return OMAP_DMA_CHAIN_ACTIVE;
1425}
1426EXPORT_SYMBOL(omap_dma_chain_status);
1427
1428/**
1429 * @brief omap_dma_chain_a_transfer - Get a free channel from a chain,
1430 * set the params and start the transfer.
1431 *
1432 * @param chain_id
1433 * @param src_start - buffer start address
1434 * @param dest_start - Dest address
1435 * @param elem_count
1436 * @param frame_count
1437 * @param callbk_data - channel callback parameter data.
1438 *
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301439 * @return - Success : 0
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001440 * Failure: -EINVAL/-EBUSY
1441 */
1442int omap_dma_chain_a_transfer(int chain_id, int src_start, int dest_start,
1443 int elem_count, int frame_count, void *callbk_data)
1444{
1445 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001446 u32 l, lch;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001447 int start_dma = 0;
1448
Tony Lindgren97b7f712008-07-03 12:24:37 +03001449 /*
1450 * if buffer size is less than 1 then there is
1451 * no use of starting the chain
1452 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001453 if (elem_count < 1) {
1454 printk(KERN_ERR "Invalid buffer size\n");
1455 return -EINVAL;
1456 }
1457
1458 /* Check for input params */
1459 if (unlikely((chain_id < 0
Tony Lindgren4d963722008-07-03 12:24:31 +03001460 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001461 printk(KERN_ERR "Invalid chain id\n");
1462 return -EINVAL;
1463 }
1464
1465 /* Check if the chain exists */
1466 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1467 printk(KERN_ERR "Chain doesn't exist\n");
1468 return -EINVAL;
1469 }
1470
1471 /* Check if all the channels in chain are in use */
1472 if (OMAP_DMA_CHAIN_QFULL(chain_id))
1473 return -EBUSY;
1474
1475 /* Frame count may be negative in case of indexed transfers */
1476 channels = dma_linked_lch[chain_id].linked_dmach_q;
1477
1478 /* Get a free channel */
1479 lch = channels[dma_linked_lch[chain_id].q_tail];
1480
1481 /* Store the callback data */
1482 dma_chan[lch].data = callbk_data;
1483
1484 /* Increment the q_tail */
1485 OMAP_DMA_CHAIN_INCQTAIL(chain_id);
1486
1487 /* Set the params to the free channel */
1488 if (src_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001489 p->dma_write(src_start, CSSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001490 if (dest_start != 0)
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001491 p->dma_write(dest_start, CDSA, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001492
1493 /* Write the buffer size */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001494 p->dma_write(elem_count, CEN, lch);
1495 p->dma_write(frame_count, CFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001496
Tony Lindgren97b7f712008-07-03 12:24:37 +03001497 /*
1498 * If the chain is dynamically linked,
1499 * then we may have to start the chain if its not active
1500 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001501 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_DYNAMIC_CHAIN) {
1502
Tony Lindgren97b7f712008-07-03 12:24:37 +03001503 /*
1504 * In Dynamic chain, if the chain is not started,
1505 * queue the channel
1506 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001507 if (dma_linked_lch[chain_id].chain_state ==
1508 DMA_CHAIN_NOTSTARTED) {
1509 /* Enable the link in previous channel */
1510 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1511 DMA_CH_QUEUED)
1512 enable_lnk(dma_chan[lch].prev_linked_ch);
1513 dma_chan[lch].state = DMA_CH_QUEUED;
1514 }
1515
Tony Lindgren97b7f712008-07-03 12:24:37 +03001516 /*
1517 * Chain is already started, make sure its active,
1518 * if not then start the chain
1519 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001520 else {
1521 start_dma = 1;
1522
1523 if (dma_chan[dma_chan[lch].prev_linked_ch].state ==
1524 DMA_CH_STARTED) {
1525 enable_lnk(dma_chan[lch].prev_linked_ch);
1526 dma_chan[lch].state = DMA_CH_QUEUED;
1527 start_dma = 0;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001528 if (0 == ((1 << 7) & p->dma_read(
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001529 CCR, dma_chan[lch].prev_linked_ch))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001530 disable_lnk(dma_chan[lch].
1531 prev_linked_ch);
1532 pr_debug("\n prev ch is stopped\n");
1533 start_dma = 1;
1534 }
1535 }
1536
1537 else if (dma_chan[dma_chan[lch].prev_linked_ch].state
1538 == DMA_CH_QUEUED) {
1539 enable_lnk(dma_chan[lch].prev_linked_ch);
1540 dma_chan[lch].state = DMA_CH_QUEUED;
1541 start_dma = 0;
1542 }
1543 omap_enable_channel_irq(lch);
1544
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001545 l = p->dma_read(CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001546
Tony Lindgren0499bde2008-07-03 12:24:36 +03001547 if ((0 == (l & (1 << 24))))
1548 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001549 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001550 l |= (1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001551 if (start_dma == 1) {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001552 if (0 == (l & (1 << 7))) {
1553 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001554 dma_chan[lch].state = DMA_CH_STARTED;
1555 pr_debug("starting %d\n", lch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001556 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001557 } else
1558 start_dma = 0;
1559 } else {
Tony Lindgren0499bde2008-07-03 12:24:36 +03001560 if (0 == (l & (1 << 7)))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001561 p->dma_write(l, CCR, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001562 }
1563 dma_chan[lch].flags |= OMAP_DMA_ACTIVE;
1564 }
1565 }
Tony Lindgren97b7f712008-07-03 12:24:37 +03001566
Anand Gadiyarf4b6a7e2008-03-11 01:10:35 +05301567 return 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001568}
1569EXPORT_SYMBOL(omap_dma_chain_a_transfer);
1570
1571/**
1572 * @brief omap_start_dma_chain_transfers - Start the chain
1573 *
1574 * @param chain_id
1575 *
1576 * @return - Success : 0
1577 * Failure : -EINVAL/-EBUSY
1578 */
1579int omap_start_dma_chain_transfers(int chain_id)
1580{
1581 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001582 u32 l, i;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001583
Tony Lindgren4d963722008-07-03 12:24:31 +03001584 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001585 printk(KERN_ERR "Invalid chain id\n");
1586 return -EINVAL;
1587 }
1588
1589 channels = dma_linked_lch[chain_id].linked_dmach_q;
1590
1591 if (dma_linked_lch[channels[0]].chain_state == DMA_CHAIN_STARTED) {
1592 printk(KERN_ERR "Chain is already started\n");
1593 return -EBUSY;
1594 }
1595
1596 if (dma_linked_lch[chain_id].chain_mode == OMAP_DMA_STATIC_CHAIN) {
1597 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked;
1598 i++) {
1599 enable_lnk(channels[i]);
1600 omap_enable_channel_irq(channels[i]);
1601 }
1602 } else {
1603 omap_enable_channel_irq(channels[0]);
1604 }
1605
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001606 l = p->dma_read(CCR, channels[0]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001607 l |= (1 << 7);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001608 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_STARTED;
1609 dma_chan[channels[0]].state = DMA_CH_STARTED;
1610
Tony Lindgren0499bde2008-07-03 12:24:36 +03001611 if ((0 == (l & (1 << 24))))
1612 l &= ~(1 << 25);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001613 else
Tony Lindgren0499bde2008-07-03 12:24:36 +03001614 l |= (1 << 25);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001615 p->dma_write(l, CCR, channels[0]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001616
1617 dma_chan[channels[0]].flags |= OMAP_DMA_ACTIVE;
Tony Lindgren97b7f712008-07-03 12:24:37 +03001618
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001619 return 0;
1620}
1621EXPORT_SYMBOL(omap_start_dma_chain_transfers);
1622
1623/**
1624 * @brief omap_stop_dma_chain_transfers - Stop the dma transfer of a chain.
1625 *
1626 * @param chain_id
1627 *
1628 * @return - Success : 0
1629 * Failure : EINVAL
1630 */
1631int omap_stop_dma_chain_transfers(int chain_id)
1632{
1633 int *channels;
Tony Lindgren0499bde2008-07-03 12:24:36 +03001634 u32 l, i;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001635 u32 sys_cf = 0;
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001636
1637 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001638 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001639 printk(KERN_ERR "Invalid chain id\n");
1640 return -EINVAL;
1641 }
1642
1643 /* Check if the chain exists */
1644 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1645 printk(KERN_ERR "Chain doesn't exists\n");
1646 return -EINVAL;
1647 }
1648 channels = dma_linked_lch[chain_id].linked_dmach_q;
1649
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001650 if (IS_DMA_ERRATA(DMA_ERRATA_i88)) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001651 sys_cf = p->dma_read(OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001652 l = sys_cf;
1653 /* Middle mode reg set no Standby */
1654 l &= ~((1 << 12)|(1 << 13));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001655 p->dma_write(l, OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001656 }
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001657
1658 for (i = 0; i < dma_linked_lch[chain_id].no_of_lchs_linked; i++) {
1659
1660 /* Stop the Channel transmission */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001661 l = p->dma_read(CCR, channels[i]);
Tony Lindgren0499bde2008-07-03 12:24:36 +03001662 l &= ~(1 << 7);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001663 p->dma_write(l, CCR, channels[i]);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001664
1665 /* Disable the link in all the channels */
1666 disable_lnk(channels[i]);
1667 dma_chan[channels[i]].state = DMA_CH_NOTSTARTED;
1668
1669 }
1670 dma_linked_lch[chain_id].chain_state = DMA_CHAIN_NOTSTARTED;
1671
1672 /* Reset the Queue pointers */
1673 OMAP_DMA_CHAIN_QINIT(chain_id);
1674
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001675 if (IS_DMA_ERRATA(DMA_ERRATA_i88))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001676 p->dma_write(sys_cf, OCP_SYSCONFIG, 0);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001677
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001678 return 0;
1679}
1680EXPORT_SYMBOL(omap_stop_dma_chain_transfers);
1681
1682/* Get the index of the ongoing DMA in chain */
1683/**
1684 * @brief omap_get_dma_chain_index - Get the element and frame index
1685 * of the ongoing DMA in chain
1686 *
1687 * @param chain_id
1688 * @param ei - Element index
1689 * @param fi - Frame index
1690 *
1691 * @return - Success : 0
1692 * Failure : -EINVAL
1693 */
1694int omap_get_dma_chain_index(int chain_id, int *ei, int *fi)
1695{
1696 int lch;
1697 int *channels;
1698
1699 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001700 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001701 printk(KERN_ERR "Invalid chain id\n");
1702 return -EINVAL;
1703 }
1704
1705 /* Check if the chain exists */
1706 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1707 printk(KERN_ERR "Chain doesn't exists\n");
1708 return -EINVAL;
1709 }
1710 if ((!ei) || (!fi))
1711 return -EINVAL;
1712
1713 channels = dma_linked_lch[chain_id].linked_dmach_q;
1714
1715 /* Get the current channel */
1716 lch = channels[dma_linked_lch[chain_id].q_head];
1717
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001718 *ei = p->dma_read(CCEN, lch);
1719 *fi = p->dma_read(CCFN, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001720
1721 return 0;
1722}
1723EXPORT_SYMBOL(omap_get_dma_chain_index);
1724
1725/**
1726 * @brief omap_get_dma_chain_dst_pos - Get the destination position of the
1727 * ongoing DMA in chain
1728 *
1729 * @param chain_id
1730 *
1731 * @return - Success : Destination position
1732 * Failure : -EINVAL
1733 */
1734int omap_get_dma_chain_dst_pos(int chain_id)
1735{
1736 int lch;
1737 int *channels;
1738
1739 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001740 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001741 printk(KERN_ERR "Invalid chain id\n");
1742 return -EINVAL;
1743 }
1744
1745 /* Check if the chain exists */
1746 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1747 printk(KERN_ERR "Chain doesn't exists\n");
1748 return -EINVAL;
1749 }
1750
1751 channels = dma_linked_lch[chain_id].linked_dmach_q;
1752
1753 /* Get the current channel */
1754 lch = channels[dma_linked_lch[chain_id].q_head];
1755
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001756 return p->dma_read(CDAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001757}
1758EXPORT_SYMBOL(omap_get_dma_chain_dst_pos);
1759
1760/**
1761 * @brief omap_get_dma_chain_src_pos - Get the source position
1762 * of the ongoing DMA in chain
1763 * @param chain_id
1764 *
1765 * @return - Success : Destination position
1766 * Failure : -EINVAL
1767 */
1768int omap_get_dma_chain_src_pos(int chain_id)
1769{
1770 int lch;
1771 int *channels;
1772
1773 /* Check for input params */
Tony Lindgren4d963722008-07-03 12:24:31 +03001774 if (unlikely((chain_id < 0 || chain_id >= dma_lch_count))) {
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001775 printk(KERN_ERR "Invalid chain id\n");
1776 return -EINVAL;
1777 }
1778
1779 /* Check if the chain exists */
1780 if (dma_linked_lch[chain_id].linked_dmach_q == NULL) {
1781 printk(KERN_ERR "Chain doesn't exists\n");
1782 return -EINVAL;
1783 }
1784
1785 channels = dma_linked_lch[chain_id].linked_dmach_q;
1786
1787 /* Get the current channel */
1788 lch = channels[dma_linked_lch[chain_id].q_head];
1789
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001790 return p->dma_read(CSAC, lch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001791}
1792EXPORT_SYMBOL(omap_get_dma_chain_src_pos);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001793#endif /* ifndef CONFIG_ARCH_OMAP1 */
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001794
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001795/*----------------------------------------------------------------------------*/
1796
1797#ifdef CONFIG_ARCH_OMAP1
1798
1799static int omap1_dma_handle_ch(int ch)
1800{
Tony Lindgren0499bde2008-07-03 12:24:36 +03001801 u32 csr;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001802
1803 if (enable_1510_mode && ch >= 6) {
1804 csr = dma_chan[ch].saved_csr;
1805 dma_chan[ch].saved_csr = 0;
1806 } else
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001807 csr = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001808 if (enable_1510_mode && ch <= 2 && (csr >> 7) != 0) {
1809 dma_chan[ch + 6].saved_csr = csr >> 7;
1810 csr &= 0x7f;
1811 }
1812 if ((csr & 0x3f) == 0)
1813 return 0;
1814 if (unlikely(dma_chan[ch].dev_id == -1)) {
1815 printk(KERN_WARNING "Spurious interrupt from DMA channel "
1816 "%d (CSR %04x)\n", ch, csr);
1817 return 0;
1818 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001819 if (unlikely(csr & OMAP1_DMA_TOUT_IRQ))
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001820 printk(KERN_WARNING "DMA timeout with device %d\n",
1821 dma_chan[ch].dev_id);
1822 if (unlikely(csr & OMAP_DMA_DROP_IRQ))
1823 printk(KERN_WARNING "DMA synchronization event drop occurred "
1824 "with device %d\n", dma_chan[ch].dev_id);
1825 if (likely(csr & OMAP_DMA_BLOCK_IRQ))
1826 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1827 if (likely(dma_chan[ch].callback != NULL))
1828 dma_chan[ch].callback(ch, csr, dma_chan[ch].data);
Tony Lindgren97b7f712008-07-03 12:24:37 +03001829
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001830 return 1;
1831}
1832
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001833static irqreturn_t omap1_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001834{
1835 int ch = ((int) dev_id) - 1;
1836 int handled = 0;
1837
1838 for (;;) {
1839 int handled_now = 0;
1840
1841 handled_now += omap1_dma_handle_ch(ch);
1842 if (enable_1510_mode && dma_chan[ch + 6].saved_csr)
1843 handled_now += omap1_dma_handle_ch(ch + 6);
1844 if (!handled_now)
1845 break;
1846 handled += handled_now;
1847 }
1848
1849 return handled ? IRQ_HANDLED : IRQ_NONE;
1850}
1851
1852#else
1853#define omap1_dma_irq_handler NULL
1854#endif
1855
Tony Lindgren140455f2010-02-12 12:26:48 -08001856#ifdef CONFIG_ARCH_OMAP2PLUS
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001857
1858static int omap2_dma_handle_ch(int ch)
1859{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001860 u32 status = p->dma_read(CSR, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001861
Juha Yrjola31513692006-12-06 17:13:47 -08001862 if (!status) {
1863 if (printk_ratelimit())
Tony Lindgren97b7f712008-07-03 12:24:37 +03001864 printk(KERN_WARNING "Spurious DMA IRQ for lch %d\n",
1865 ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001866 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001867 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001868 }
1869 if (unlikely(dma_chan[ch].dev_id == -1)) {
1870 if (printk_ratelimit())
1871 printk(KERN_WARNING "IRQ %04x for non-allocated DMA"
1872 "channel %d\n", status, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001873 return 0;
Juha Yrjola31513692006-12-06 17:13:47 -08001874 }
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001875 if (unlikely(status & OMAP_DMA_DROP_IRQ))
1876 printk(KERN_INFO
1877 "DMA synchronization event drop occurred with device "
1878 "%d\n", dma_chan[ch].dev_id);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001879 if (unlikely(status & OMAP2_DMA_TRANS_ERR_IRQ)) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001880 printk(KERN_INFO "DMA transaction error with device %d\n",
1881 dma_chan[ch].dev_id);
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001882 if (IS_DMA_ERRATA(DMA_ERRATA_i378)) {
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001883 u32 ccr;
1884
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001885 ccr = p->dma_read(CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001886 ccr &= ~OMAP_DMA_CCR_EN;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001887 p->dma_write(ccr, CCR, ch);
Santosh Shilimkara50f18c2008-12-10 17:36:53 -08001888 dma_chan[ch].flags &= ~OMAP_DMA_ACTIVE;
1889 }
1890 }
Tony Lindgren7ff879d2006-06-26 16:16:15 -07001891 if (unlikely(status & OMAP2_DMA_SECURE_ERR_IRQ))
1892 printk(KERN_INFO "DMA secure error with device %d\n",
1893 dma_chan[ch].dev_id);
1894 if (unlikely(status & OMAP2_DMA_MISALIGNED_ERR_IRQ))
1895 printk(KERN_INFO "DMA misaligned error with device %d\n",
1896 dma_chan[ch].dev_id);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001897
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001898 p->dma_write(status, CSR, ch);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001899 p->dma_write(1 << ch, IRQSTATUS_L0, ch);
Mathias Nymane860e6d2010-10-25 14:35:24 +00001900 /* read back the register to flush the write */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001901 p->dma_read(IRQSTATUS_L0, ch);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001902
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001903 /* If the ch is not chained then chain_id will be -1 */
1904 if (dma_chan[ch].chain_id != -1) {
1905 int chain_id = dma_chan[ch].chain_id;
1906 dma_chan[ch].state = DMA_CH_NOTSTARTED;
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001907 if (p->dma_read(CLNK_CTRL, ch) & (1 << 15))
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001908 dma_chan[dma_chan[ch].next_linked_ch].state =
1909 DMA_CH_STARTED;
1910 if (dma_linked_lch[chain_id].chain_mode ==
1911 OMAP_DMA_DYNAMIC_CHAIN)
1912 disable_lnk(ch);
1913
1914 if (!OMAP_DMA_CHAIN_QEMPTY(chain_id))
1915 OMAP_DMA_CHAIN_INCQHEAD(chain_id);
1916
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001917 status = p->dma_read(CSR, ch);
Adrian Hunter4fb699b2010-11-24 13:23:21 +02001918 p->dma_write(status, CSR, ch);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001919 }
1920
Jarkko Nikula538528d2008-02-13 11:47:29 +02001921 if (likely(dma_chan[ch].callback != NULL))
1922 dma_chan[ch].callback(ch, status, dma_chan[ch].data);
Anand Gadiyarf8151e52007-12-01 12:14:11 -08001923
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001924 return 0;
1925}
1926
1927/* STATUS register count is from 1-32 while our is 0-31 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -07001928static irqreturn_t omap2_dma_irq_handler(int irq, void *dev_id)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001929{
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001930 u32 val, enable_reg;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001931 int i;
1932
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001933 val = p->dma_read(IRQSTATUS_L0, 0);
Juha Yrjola31513692006-12-06 17:13:47 -08001934 if (val == 0) {
1935 if (printk_ratelimit())
1936 printk(KERN_WARNING "Spurious DMA IRQ\n");
1937 return IRQ_HANDLED;
1938 }
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001939 enable_reg = p->dma_read(IRQENABLE_L0, 0);
Santosh Shilimkar52176e72009-03-23 18:07:49 -07001940 val &= enable_reg; /* Dispatch only relevant interrupts */
Tony Lindgren4d963722008-07-03 12:24:31 +03001941 for (i = 0; i < dma_lch_count && val != 0; i++) {
Juha Yrjola31513692006-12-06 17:13:47 -08001942 if (val & 1)
1943 omap2_dma_handle_ch(i);
1944 val >>= 1;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001945 }
1946
1947 return IRQ_HANDLED;
1948}
1949
1950static struct irqaction omap24xx_dma_irq = {
1951 .name = "DMA",
1952 .handler = omap2_dma_irq_handler,
Thomas Gleixner52e405e2006-07-03 02:20:05 +02001953 .flags = IRQF_DISABLED
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001954};
1955
1956#else
1957static struct irqaction omap24xx_dma_irq;
1958#endif
1959
1960/*----------------------------------------------------------------------------*/
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001961
Tero Kristof2d11852008-08-28 13:13:31 +00001962void omap_dma_global_context_save(void)
1963{
1964 omap_dma_global_context.dma_irqenable_l0 =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001965 p->dma_read(IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001966 omap_dma_global_context.dma_ocp_sysconfig =
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001967 p->dma_read(OCP_SYSCONFIG, 0);
1968 omap_dma_global_context.dma_gcr = p->dma_read(GCR, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001969}
1970
1971void omap_dma_global_context_restore(void)
1972{
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001973 int ch;
1974
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001975 p->dma_write(omap_dma_global_context.dma_gcr, GCR, 0);
1976 p->dma_write(omap_dma_global_context.dma_ocp_sysconfig,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001977 OCP_SYSCONFIG, 0);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001978 p->dma_write(omap_dma_global_context.dma_irqenable_l0,
G, Manjunath Kondaiaha4c537c2010-12-20 18:27:17 -08001979 IRQENABLE_L0, 0);
Tero Kristof2d11852008-08-28 13:13:31 +00001980
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001981 if (IS_DMA_ERRATA(DMA_ROMCODE_BUG))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001982 p->dma_write(0x3 , IRQSTATUS_L0, 0);
Aaro Koskinenbf07c9f2009-05-20 16:58:30 +03001983
1984 for (ch = 0; ch < dma_chan_count; ch++)
1985 if (dma_chan[ch].dev_id != -1)
1986 omap_clear_dma(ch);
Tero Kristof2d11852008-08-28 13:13:31 +00001987}
1988
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001989static int __devinit omap_system_dma_probe(struct platform_device *pdev)
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001990{
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001991 int ch, ret = 0;
1992 int dma_irq;
1993 char irq_name[4];
1994 int irq_rel;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08001995
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08001996 p = pdev->dev.platform_data;
1997 if (!p) {
1998 dev_err(&pdev->dev, "%s: System DMA initialized without"
1999 "platform data\n", __func__);
2000 return -EINVAL;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002001 }
2002
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002003 d = p->dma_attr;
2004 errata = p->errata;
G, Manjunath Kondaiahd3c9be22010-12-20 18:27:18 -08002005
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002006 if ((d->dev_caps & RESERVE_CHANNEL) && omap_dma_reserve_channels
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002007 && (omap_dma_reserve_channels <= dma_lch_count))
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002008 d->lch_count = omap_dma_reserve_channels;
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002009
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002010 dma_lch_count = d->lch_count;
2011 dma_chan_count = dma_lch_count;
2012 dma_chan = d->chan;
2013 enable_1510_mode = d->dev_caps & ENABLE_1510_MODE;
Tony Lindgren4d963722008-07-03 12:24:31 +03002014
2015 if (cpu_class_is_omap2()) {
2016 dma_linked_lch = kzalloc(sizeof(struct dma_link_info) *
2017 dma_lch_count, GFP_KERNEL);
2018 if (!dma_linked_lch) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002019 ret = -ENOMEM;
2020 goto exit_dma_lch_fail;
Tony Lindgren4d963722008-07-03 12:24:31 +03002021 }
2022 }
2023
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002024 spin_lock_init(&dma_chan_lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002025 for (ch = 0; ch < dma_chan_count; ch++) {
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002026 omap_clear_dma(ch);
Mika Westerbergada8d4a2010-05-14 12:05:25 -07002027 if (cpu_class_is_omap2())
2028 omap2_disable_irq_lch(ch);
2029
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002030 dma_chan[ch].dev_id = -1;
2031 dma_chan[ch].next_lch = -1;
2032
2033 if (ch >= 6 && enable_1510_mode)
2034 continue;
2035
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002036 if (cpu_class_is_omap1()) {
Tony Lindgren97b7f712008-07-03 12:24:37 +03002037 /*
2038 * request_irq() doesn't like dev_id (ie. ch) being
2039 * zero, so we have to kludge around this.
2040 */
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002041 sprintf(&irq_name[0], "%d", ch);
2042 dma_irq = platform_get_irq_byname(pdev, irq_name);
2043
2044 if (dma_irq < 0) {
2045 ret = dma_irq;
2046 goto exit_dma_irq_fail;
2047 }
2048
2049 /* INT_DMA_LCD is handled in lcd_dma.c */
2050 if (dma_irq == INT_DMA_LCD)
2051 continue;
2052
2053 ret = request_irq(dma_irq,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002054 omap1_dma_irq_handler, 0, "DMA",
2055 (void *) (ch + 1));
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002056 if (ret != 0)
2057 goto exit_dma_irq_fail;
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00002058 }
2059 }
2060
Santosh Shilimkar44169072009-05-28 14:16:04 -07002061 if (cpu_is_omap2430() || cpu_is_omap34xx() || cpu_is_omap44xx())
Anand Gadiyarf8151e52007-12-01 12:14:11 -08002062 omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE,
2063 DMA_DEFAULT_FIFO_DEPTH, 0);
2064
Santosh Shilimkar44169072009-05-28 14:16:04 -07002065 if (cpu_class_is_omap2()) {
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002066 strcpy(irq_name, "0");
2067 dma_irq = platform_get_irq_byname(pdev, irq_name);
2068 if (dma_irq < 0) {
2069 dev_err(&pdev->dev, "failed: request IRQ %d", dma_irq);
2070 goto exit_dma_lch_fail;
2071 }
2072 ret = setup_irq(dma_irq, &omap24xx_dma_irq);
2073 if (ret) {
2074 dev_err(&pdev->dev, "set_up failed for IRQ %d"
2075 "for DMA (error %d)\n", dma_irq, ret);
2076 goto exit_dma_lch_fail;
Kalle Jokiniemiba50ea72009-03-26 15:59:00 +02002077 }
Kalle Jokiniemiaecedb92009-06-23 13:30:24 +03002078 }
2079
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002080 /* reserve dma channels 0 and 1 in high security devices */
2081 if (cpu_is_omap34xx() &&
2082 (omap_type() != OMAP2_DEVICE_TYPE_GP)) {
2083 printk(KERN_INFO "Reserving DMA channels 0 and 1 for "
2084 "HS ROM code\n");
2085 dma_chan[0].dev_id = 0;
2086 dma_chan[1].dev_id = 1;
2087 }
2088 p->show_dma_caps();
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002089 return 0;
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002090
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002091exit_dma_irq_fail:
2092 dev_err(&pdev->dev, "unable to request IRQ %d"
2093 "for DMA (error %d)\n", dma_irq, ret);
2094 for (irq_rel = 0; irq_rel < ch; irq_rel++) {
2095 dma_irq = platform_get_irq(pdev, irq_rel);
2096 free_irq(dma_irq, (void *)(irq_rel + 1));
2097 }
2098
2099exit_dma_lch_fail:
2100 kfree(p);
2101 kfree(d);
Tony Lindgren7e9bf842009-10-19 15:25:15 -07002102 kfree(dma_chan);
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002103 return ret;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002104}
2105
G, Manjunath Kondaiahf31cc962010-12-20 18:27:19 -08002106static int __devexit omap_system_dma_remove(struct platform_device *pdev)
2107{
2108 int dma_irq;
2109
2110 if (cpu_class_is_omap2()) {
2111 char irq_name[4];
2112 strcpy(irq_name, "0");
2113 dma_irq = platform_get_irq_byname(pdev, irq_name);
2114 remove_irq(dma_irq, &omap24xx_dma_irq);
2115 } else {
2116 int irq_rel = 0;
2117 for ( ; irq_rel < dma_chan_count; irq_rel++) {
2118 dma_irq = platform_get_irq(pdev, irq_rel);
2119 free_irq(dma_irq, (void *)(irq_rel + 1));
2120 }
2121 }
2122 kfree(p);
2123 kfree(d);
2124 kfree(dma_chan);
2125 return 0;
2126}
2127
2128static struct platform_driver omap_system_dma_driver = {
2129 .probe = omap_system_dma_probe,
2130 .remove = omap_system_dma_remove,
2131 .driver = {
2132 .name = "omap_dma_system"
2133 },
2134};
2135
2136static int __init omap_system_dma_init(void)
2137{
2138 return platform_driver_register(&omap_system_dma_driver);
2139}
2140arch_initcall(omap_system_dma_init);
2141
2142static void __exit omap_system_dma_exit(void)
2143{
2144 platform_driver_unregister(&omap_system_dma_driver);
2145}
2146
2147MODULE_DESCRIPTION("OMAP SYSTEM DMA DRIVER");
2148MODULE_LICENSE("GPL");
2149MODULE_ALIAS("platform:" DRIVER_NAME);
2150MODULE_AUTHOR("Texas Instruments Inc");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002151
Santosh Shilimkar2263f022009-03-23 18:07:48 -07002152/*
2153 * Reserve the omap SDMA channels using cmdline bootarg
2154 * "omap_dma_reserve_ch=". The valid range is 1 to 32
2155 */
2156static int __init omap_dma_cmdline_reserve_ch(char *str)
2157{
2158 if (get_option(&str, &omap_dma_reserve_channels) != 1)
2159 omap_dma_reserve_channels = 0;
2160 return 1;
2161}
2162
2163__setup("omap_dma_reserve_ch=", omap_dma_cmdline_reserve_ch);
2164
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01002165