blob: f8aa5be0eb150617599884145825995cd103bc3d [file] [log] [blame]
Sascha Hauer58a85f42008-09-09 10:19:44 +02001/*
Uwe Kleine-König9f72ffe2010-06-14 17:56:52 +02002 * linux/arch/arm/plat-mxc/dma-v1.c
Sascha Hauer58a85f42008-09-09 10:19:44 +02003 *
4 * i.MX DMA registration and IRQ dispatching
5 *
6 * Copyright 2006 Pavel Pisa <pisa@cmp.felk.cvut.cz>
7 * Copyright 2008 Juergen Beisert, <kernel@pengutronix.de>
8 * Copyright 2008 Sascha Hauer, <s.hauer@pengutronix.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/interrupt.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000029#include <linux/err.h>
Sascha Hauer58a85f42008-09-09 10:19:44 +020030#include <linux/errno.h>
31#include <linux/clk.h>
32#include <linux/scatterlist.h>
33#include <linux/io.h>
34
35#include <asm/system.h>
36#include <asm/irq.h>
37#include <mach/hardware.h>
Uwe Kleine-König9f72ffe2010-06-14 17:56:52 +020038#include <mach/dma-v1.h>
Sascha Hauer58a85f42008-09-09 10:19:44 +020039
40#define DMA_DCR 0x00 /* Control Register */
41#define DMA_DISR 0x04 /* Interrupt status Register */
42#define DMA_DIMR 0x08 /* Interrupt mask Register */
43#define DMA_DBTOSR 0x0c /* Burst timeout status Register */
44#define DMA_DRTOSR 0x10 /* Request timeout Register */
45#define DMA_DSESR 0x14 /* Transfer Error Status Register */
46#define DMA_DBOSR 0x18 /* Buffer overflow status Register */
47#define DMA_DBTOCR 0x1c /* Burst timeout control Register */
48#define DMA_WSRA 0x40 /* W-Size Register A */
49#define DMA_XSRA 0x44 /* X-Size Register A */
50#define DMA_YSRA 0x48 /* Y-Size Register A */
51#define DMA_WSRB 0x4c /* W-Size Register B */
52#define DMA_XSRB 0x50 /* X-Size Register B */
53#define DMA_YSRB 0x54 /* Y-Size Register B */
54#define DMA_SAR(x) (0x80 + ((x) << 6)) /* Source Address Registers */
55#define DMA_DAR(x) (0x84 + ((x) << 6)) /* Destination Address Registers */
56#define DMA_CNTR(x) (0x88 + ((x) << 6)) /* Count Registers */
57#define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
58#define DMA_RSSR(x) (0x90 + ((x) << 6)) /* Request source select Registers */
59#define DMA_BLR(x) (0x94 + ((x) << 6)) /* Burst length Registers */
60#define DMA_RTOR(x) (0x98 + ((x) << 6)) /* Request timeout Registers */
61#define DMA_BUCR(x) (0x98 + ((x) << 6)) /* Bus Utilization Registers */
62#define DMA_CCNR(x) (0x9C + ((x) << 6)) /* Channel counter Registers */
63
64#define DCR_DRST (1<<1)
65#define DCR_DEN (1<<0)
66#define DBTOCR_EN (1<<15)
67#define DBTOCR_CNT(x) ((x) & 0x7fff)
68#define CNTR_CNT(x) ((x) & 0xffffff)
69#define CCR_ACRPT (1<<14)
70#define CCR_DMOD_LINEAR (0x0 << 12)
71#define CCR_DMOD_2D (0x1 << 12)
72#define CCR_DMOD_FIFO (0x2 << 12)
73#define CCR_DMOD_EOBFIFO (0x3 << 12)
74#define CCR_SMOD_LINEAR (0x0 << 10)
75#define CCR_SMOD_2D (0x1 << 10)
76#define CCR_SMOD_FIFO (0x2 << 10)
77#define CCR_SMOD_EOBFIFO (0x3 << 10)
78#define CCR_MDIR_DEC (1<<9)
79#define CCR_MSEL_B (1<<8)
80#define CCR_DSIZ_32 (0x0 << 6)
81#define CCR_DSIZ_8 (0x1 << 6)
82#define CCR_DSIZ_16 (0x2 << 6)
83#define CCR_SSIZ_32 (0x0 << 4)
84#define CCR_SSIZ_8 (0x1 << 4)
85#define CCR_SSIZ_16 (0x2 << 4)
86#define CCR_REN (1<<3)
87#define CCR_RPT (1<<2)
88#define CCR_FRC (1<<1)
89#define CCR_CEN (1<<0)
90#define RTOR_EN (1<<15)
91#define RTOR_CLK (1<<14)
92#define RTOR_PSC (1<<13)
93
94/*
95 * struct imx_dma_channel - i.MX specific DMA extension
96 * @name: name specified by DMA client
97 * @irq_handler: client callback for end of transfer
98 * @err_handler: client callback for error condition
99 * @data: clients context data for callbacks
100 * @dma_mode: direction of the transfer %DMA_MODE_READ or %DMA_MODE_WRITE
101 * @sg: pointer to the actual read/written chunk for scatter-gather emulation
102 * @resbytes: total residual number of bytes to transfer
103 * (it can be lower or same as sum of SG mapped chunk sizes)
104 * @sgcount: number of chunks to be read/written
105 *
106 * Structure is used for IMX DMA processing. It would be probably good
107 * @struct dma_struct in the future for external interfacing and use
108 * @struct imx_dma_channel only as extension to it.
109 */
110
111struct imx_dma_channel {
112 const char *name;
113 void (*irq_handler) (int, void *);
114 void (*err_handler) (int, void *, int errcode);
115 void (*prog_handler) (int, void *, struct scatterlist *);
116 void *data;
Russell King4e57ea92009-01-02 12:34:31 +0000117 unsigned int dma_mode;
Sascha Hauer58a85f42008-09-09 10:19:44 +0200118 struct scatterlist *sg;
119 unsigned int resbytes;
120 int dma_num;
121
122 int in_use;
123
124 u32 ccr_from_device;
125 u32 ccr_to_device;
126
127 struct timer_list watchdog;
128
129 int hw_chaining;
130};
131
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100132static void __iomem *imx_dmav1_baseaddr;
133
134static void imx_dmav1_writel(unsigned val, unsigned offset)
135{
136 __raw_writel(val, imx_dmav1_baseaddr + offset);
137}
138
139static unsigned imx_dmav1_readl(unsigned offset)
140{
141 return __raw_readl(imx_dmav1_baseaddr + offset);
142}
143
Sascha Hauer58a85f42008-09-09 10:19:44 +0200144static struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
145
146static struct clk *dma_clk;
147
148static int imx_dma_hw_chain(struct imx_dma_channel *imxdma)
149{
150 if (cpu_is_mx27())
151 return imxdma->hw_chaining;
152 else
153 return 0;
154}
155
Sascha Hauer58a85f42008-09-09 10:19:44 +0200156/*
157 * imx_dma_sg_next - prepare next chunk for scatter-gather DMA emulation
158 */
159static inline int imx_dma_sg_next(int channel, struct scatterlist *sg)
160{
161 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
162 unsigned long now;
163
164 if (!imxdma->name) {
165 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
166 __func__, channel);
167 return 0;
168 }
169
170 now = min(imxdma->resbytes, sg->length);
Sascha Hauer4998f1a2009-11-18 15:21:44 +0100171 if (imxdma->resbytes != IMX_DMA_LENGTH_LOOP)
172 imxdma->resbytes -= now;
Sascha Hauer58a85f42008-09-09 10:19:44 +0200173
174 if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100175 imx_dmav1_writel(sg->dma_address, DMA_DAR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200176 else
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100177 imx_dmav1_writel(sg->dma_address, DMA_SAR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200178
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100179 imx_dmav1_writel(now, DMA_CNTR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200180
181 pr_debug("imxdma%d: next sg chunk dst 0x%08x, src 0x%08x, "
182 "size 0x%08x\n", channel,
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100183 imx_dmav1_readl(DMA_DAR(channel)),
184 imx_dmav1_readl(DMA_SAR(channel)),
185 imx_dmav1_readl(DMA_CNTR(channel)));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200186
187 return now;
188}
189
190/**
191 * imx_dma_setup_single - setup i.MX DMA channel for linear memory to/from
192 * device transfer
193 *
194 * @channel: i.MX DMA channel number
195 * @dma_address: the DMA/physical memory address of the linear data block
196 * to transfer
197 * @dma_length: length of the data block in bytes
198 * @dev_addr: physical device port address
199 * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
200 * or %DMA_MODE_WRITE from memory to the device
201 *
202 * Return value: if incorrect parameters are provided -%EINVAL.
203 * Zero indicates success.
204 */
205int
206imx_dma_setup_single(int channel, dma_addr_t dma_address,
207 unsigned int dma_length, unsigned int dev_addr,
Sascha Hauer33ebc192008-12-16 12:17:47 +0100208 unsigned int dmamode)
Sascha Hauer58a85f42008-09-09 10:19:44 +0200209{
210 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
211
212 imxdma->sg = NULL;
213 imxdma->dma_mode = dmamode;
214
215 if (!dma_address) {
216 printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
217 channel);
218 return -EINVAL;
219 }
220
221 if (!dma_length) {
222 printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
223 channel);
224 return -EINVAL;
225 }
226
227 if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
228 pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
229 "dev_addr=0x%08x for read\n",
230 channel, __func__, (unsigned int)dma_address,
231 dma_length, dev_addr);
232
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100233 imx_dmav1_writel(dev_addr, DMA_SAR(channel));
234 imx_dmav1_writel(dma_address, DMA_DAR(channel));
235 imx_dmav1_writel(imxdma->ccr_from_device, DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200236 } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
237 pr_debug("imxdma%d: %s dma_addressg=0x%08x dma_length=%d "
238 "dev_addr=0x%08x for write\n",
239 channel, __func__, (unsigned int)dma_address,
240 dma_length, dev_addr);
241
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100242 imx_dmav1_writel(dma_address, DMA_SAR(channel));
243 imx_dmav1_writel(dev_addr, DMA_DAR(channel));
244 imx_dmav1_writel(imxdma->ccr_to_device,
245 DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200246 } else {
247 printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
248 channel);
249 return -EINVAL;
250 }
251
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100252 imx_dmav1_writel(dma_length, DMA_CNTR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200253
254 return 0;
255}
256EXPORT_SYMBOL(imx_dma_setup_single);
257
258/**
259 * imx_dma_setup_sg - setup i.MX DMA channel SG list to/from device transfer
260 * @channel: i.MX DMA channel number
261 * @sg: pointer to the scatter-gather list/vector
262 * @sgcount: scatter-gather list hungs count
263 * @dma_length: total length of the transfer request in bytes
264 * @dev_addr: physical device port address
265 * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
266 * or %DMA_MODE_WRITE from memory to the device
267 *
268 * The function sets up DMA channel state and registers to be ready for
269 * transfer specified by provided parameters. The scatter-gather emulation
270 * is set up according to the parameters.
271 *
272 * The full preparation of the transfer requires setup of more register
273 * by the caller before imx_dma_enable() can be called.
274 *
275 * %BLR(channel) holds transfer burst length in bytes, 0 means 64 bytes
276 *
277 * %RSSR(channel) has to be set to the DMA request line source %DMA_REQ_xxx
278 *
279 * %CCR(channel) has to specify transfer parameters, the next settings is
280 * typical for linear or simple scatter-gather transfers if %DMA_MODE_READ is
281 * specified
282 *
283 * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
284 *
285 * The typical setup for %DMA_MODE_WRITE is specified by next options
286 * combination
287 *
288 * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
289 *
290 * Be careful here and do not mistakenly mix source and target device
291 * port sizes constants, they are really different:
292 * %CCR_SSIZ_8, %CCR_SSIZ_16, %CCR_SSIZ_32,
293 * %CCR_DSIZ_8, %CCR_DSIZ_16, %CCR_DSIZ_32
294 *
295 * Return value: if incorrect parameters are provided -%EINVAL.
296 * Zero indicates success.
297 */
298int
299imx_dma_setup_sg(int channel,
300 struct scatterlist *sg, unsigned int sgcount,
301 unsigned int dma_length, unsigned int dev_addr,
Sascha Hauer33ebc192008-12-16 12:17:47 +0100302 unsigned int dmamode)
Sascha Hauer58a85f42008-09-09 10:19:44 +0200303{
304 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
305
306 if (imxdma->in_use)
307 return -EBUSY;
308
309 imxdma->sg = sg;
310 imxdma->dma_mode = dmamode;
311 imxdma->resbytes = dma_length;
312
313 if (!sg || !sgcount) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +0200314 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg empty sg list\n",
Sascha Hauer58a85f42008-09-09 10:19:44 +0200315 channel);
316 return -EINVAL;
317 }
318
319 if (!sg->length) {
320 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
321 channel);
322 return -EINVAL;
323 }
324
325 if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
326 pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
327 "dev_addr=0x%08x for read\n",
328 channel, __func__, sg, sgcount, dma_length, dev_addr);
329
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100330 imx_dmav1_writel(dev_addr, DMA_SAR(channel));
331 imx_dmav1_writel(imxdma->ccr_from_device, DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200332 } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
333 pr_debug("imxdma%d: %s sg=%p sgcount=%d total length=%d "
334 "dev_addr=0x%08x for write\n",
335 channel, __func__, sg, sgcount, dma_length, dev_addr);
336
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100337 imx_dmav1_writel(dev_addr, DMA_DAR(channel));
338 imx_dmav1_writel(imxdma->ccr_to_device, DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200339 } else {
340 printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
341 channel);
342 return -EINVAL;
343 }
344
345 imx_dma_sg_next(channel, sg);
346
347 return 0;
348}
349EXPORT_SYMBOL(imx_dma_setup_sg);
350
351int
352imx_dma_config_channel(int channel, unsigned int config_port,
353 unsigned int config_mem, unsigned int dmareq, int hw_chaining)
354{
355 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
356 u32 dreq = 0;
357
358 imxdma->hw_chaining = 0;
359
360 if (hw_chaining) {
361 imxdma->hw_chaining = 1;
362 if (!imx_dma_hw_chain(imxdma))
363 return -EINVAL;
364 }
365
366 if (dmareq)
367 dreq = CCR_REN;
368
369 imxdma->ccr_from_device = config_port | (config_mem << 2) | dreq;
370 imxdma->ccr_to_device = config_mem | (config_port << 2) | dreq;
371
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100372 imx_dmav1_writel(dmareq, DMA_RSSR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200373
374 return 0;
375}
376EXPORT_SYMBOL(imx_dma_config_channel);
377
378void imx_dma_config_burstlen(int channel, unsigned int burstlen)
379{
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100380 imx_dmav1_writel(burstlen, DMA_BLR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200381}
382EXPORT_SYMBOL(imx_dma_config_burstlen);
383
384/**
385 * imx_dma_setup_handlers - setup i.MX DMA channel end and error notification
386 * handlers
387 * @channel: i.MX DMA channel number
388 * @irq_handler: the pointer to the function called if the transfer
389 * ends successfully
390 * @err_handler: the pointer to the function called if the premature
391 * end caused by error occurs
392 * @data: user specified value to be passed to the handlers
393 */
394int
395imx_dma_setup_handlers(int channel,
396 void (*irq_handler) (int, void *),
397 void (*err_handler) (int, void *, int),
398 void *data)
399{
400 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
401 unsigned long flags;
402
403 if (!imxdma->name) {
404 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
405 __func__, channel);
406 return -ENODEV;
407 }
408
409 local_irq_save(flags);
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100410 imx_dmav1_writel(1 << channel, DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200411 imxdma->irq_handler = irq_handler;
412 imxdma->err_handler = err_handler;
413 imxdma->data = data;
414 local_irq_restore(flags);
415 return 0;
416}
417EXPORT_SYMBOL(imx_dma_setup_handlers);
418
419/**
420 * imx_dma_setup_progression_handler - setup i.MX DMA channel progression
421 * handlers
422 * @channel: i.MX DMA channel number
423 * @prog_handler: the pointer to the function called if the transfer progresses
424 */
425int
426imx_dma_setup_progression_handler(int channel,
427 void (*prog_handler) (int, void*, struct scatterlist*))
428{
429 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
430 unsigned long flags;
431
432 if (!imxdma->name) {
433 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
434 __func__, channel);
435 return -ENODEV;
436 }
437
438 local_irq_save(flags);
439 imxdma->prog_handler = prog_handler;
440 local_irq_restore(flags);
441 return 0;
442}
443EXPORT_SYMBOL(imx_dma_setup_progression_handler);
444
445/**
446 * imx_dma_enable - function to start i.MX DMA channel operation
447 * @channel: i.MX DMA channel number
448 *
449 * The channel has to be allocated by driver through imx_dma_request()
450 * or imx_dma_request_by_prio() function.
451 * The transfer parameters has to be set to the channel registers through
452 * call of the imx_dma_setup_single() or imx_dma_setup_sg() function
453 * and registers %BLR(channel), %RSSR(channel) and %CCR(channel) has to
454 * be set prior this function call by the channel user.
455 */
456void imx_dma_enable(int channel)
457{
458 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
459 unsigned long flags;
460
461 pr_debug("imxdma%d: imx_dma_enable\n", channel);
462
463 if (!imxdma->name) {
464 printk(KERN_CRIT "%s: called for not allocated channel %d\n",
465 __func__, channel);
466 return;
467 }
468
469 if (imxdma->in_use)
470 return;
471
472 local_irq_save(flags);
473
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100474 imx_dmav1_writel(1 << channel, DMA_DISR);
475 imx_dmav1_writel(imx_dmav1_readl(DMA_DIMR) & ~(1 << channel), DMA_DIMR);
476 imx_dmav1_writel(imx_dmav1_readl(DMA_CCR(channel)) | CCR_CEN |
477 CCR_ACRPT, DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200478
479#ifdef CONFIG_ARCH_MX2
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100480 if ((cpu_is_mx21() || cpu_is_mx27()) &&
481 imxdma->sg && imx_dma_hw_chain(imxdma)) {
Sascha Hauer58a85f42008-09-09 10:19:44 +0200482 imxdma->sg = sg_next(imxdma->sg);
483 if (imxdma->sg) {
484 u32 tmp;
485 imx_dma_sg_next(channel, imxdma->sg);
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100486 tmp = imx_dmav1_readl(DMA_CCR(channel));
487 imx_dmav1_writel(tmp | CCR_RPT | CCR_ACRPT,
488 DMA_CCR(channel));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200489 }
490 }
491#endif
492 imxdma->in_use = 1;
493
494 local_irq_restore(flags);
495}
496EXPORT_SYMBOL(imx_dma_enable);
497
498/**
499 * imx_dma_disable - stop, finish i.MX DMA channel operatin
500 * @channel: i.MX DMA channel number
501 */
502void imx_dma_disable(int channel)
503{
504 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
505 unsigned long flags;
506
507 pr_debug("imxdma%d: imx_dma_disable\n", channel);
508
509 if (imx_dma_hw_chain(imxdma))
510 del_timer(&imxdma->watchdog);
511
512 local_irq_save(flags);
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100513 imx_dmav1_writel(imx_dmav1_readl(DMA_DIMR) | (1 << channel), DMA_DIMR);
514 imx_dmav1_writel(imx_dmav1_readl(DMA_CCR(channel)) & ~CCR_CEN,
515 DMA_CCR(channel));
516 imx_dmav1_writel(1 << channel, DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200517 imxdma->in_use = 0;
518 local_irq_restore(flags);
519}
520EXPORT_SYMBOL(imx_dma_disable);
521
Sascha Hauer7c995022008-12-18 10:01:49 +0100522#ifdef CONFIG_ARCH_MX2
Sascha Hauer58a85f42008-09-09 10:19:44 +0200523static void imx_dma_watchdog(unsigned long chno)
524{
525 struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
526
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100527 imx_dmav1_writel(0, DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200528 imxdma->in_use = 0;
529 imxdma->sg = NULL;
530
531 if (imxdma->err_handler)
532 imxdma->err_handler(chno, imxdma->data, IMX_DMA_ERR_TIMEOUT);
533}
Sascha Hauer7c995022008-12-18 10:01:49 +0100534#endif
Sascha Hauer58a85f42008-09-09 10:19:44 +0200535
536static irqreturn_t dma_err_handler(int irq, void *dev_id)
537{
538 int i, disr;
539 struct imx_dma_channel *imxdma;
540 unsigned int err_mask;
541 int errcode;
542
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100543 disr = imx_dmav1_readl(DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200544
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100545 err_mask = imx_dmav1_readl(DMA_DBTOSR) |
546 imx_dmav1_readl(DMA_DRTOSR) |
547 imx_dmav1_readl(DMA_DSESR) |
548 imx_dmav1_readl(DMA_DBOSR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200549
550 if (!err_mask)
551 return IRQ_HANDLED;
552
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100553 imx_dmav1_writel(disr & err_mask, DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200554
555 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
556 if (!(err_mask & (1 << i)))
557 continue;
558 imxdma = &imx_dma_channels[i];
559 errcode = 0;
560
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100561 if (imx_dmav1_readl(DMA_DBTOSR) & (1 << i)) {
562 imx_dmav1_writel(1 << i, DMA_DBTOSR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200563 errcode |= IMX_DMA_ERR_BURST;
564 }
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100565 if (imx_dmav1_readl(DMA_DRTOSR) & (1 << i)) {
566 imx_dmav1_writel(1 << i, DMA_DRTOSR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200567 errcode |= IMX_DMA_ERR_REQUEST;
568 }
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100569 if (imx_dmav1_readl(DMA_DSESR) & (1 << i)) {
570 imx_dmav1_writel(1 << i, DMA_DSESR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200571 errcode |= IMX_DMA_ERR_TRANSFER;
572 }
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100573 if (imx_dmav1_readl(DMA_DBOSR) & (1 << i)) {
574 imx_dmav1_writel(1 << i, DMA_DBOSR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200575 errcode |= IMX_DMA_ERR_BUFFER;
576 }
577 if (imxdma->name && imxdma->err_handler) {
578 imxdma->err_handler(i, imxdma->data, errcode);
579 continue;
580 }
581
582 imx_dma_channels[i].sg = NULL;
583
584 printk(KERN_WARNING
585 "DMA timeout on channel %d (%s) -%s%s%s%s\n",
586 i, imxdma->name,
587 errcode & IMX_DMA_ERR_BURST ? " burst" : "",
588 errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
589 errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
590 errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
591 }
592 return IRQ_HANDLED;
593}
594
595static void dma_irq_handle_channel(int chno)
596{
597 struct imx_dma_channel *imxdma = &imx_dma_channels[chno];
598
599 if (!imxdma->name) {
600 /*
601 * IRQ for an unregistered DMA channel:
602 * let's clear the interrupts and disable it.
603 */
604 printk(KERN_WARNING
605 "spurious IRQ for DMA channel %d\n", chno);
606 return;
607 }
608
609 if (imxdma->sg) {
610 u32 tmp;
611 struct scatterlist *current_sg = imxdma->sg;
612 imxdma->sg = sg_next(imxdma->sg);
613
614 if (imxdma->sg) {
615 imx_dma_sg_next(chno, imxdma->sg);
616
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100617 tmp = imx_dmav1_readl(DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200618
619 if (imx_dma_hw_chain(imxdma)) {
620 /* FIXME: The timeout should probably be
621 * configurable
622 */
623 mod_timer(&imxdma->watchdog,
624 jiffies + msecs_to_jiffies(500));
625
626 tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100627 imx_dmav1_writel(tmp, DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200628 } else {
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100629 imx_dmav1_writel(tmp & ~CCR_CEN, DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200630 tmp |= CCR_CEN;
631 }
632
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100633 imx_dmav1_writel(tmp, DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200634
635 if (imxdma->prog_handler)
636 imxdma->prog_handler(chno, imxdma->data,
637 current_sg);
638
639 return;
640 }
641
642 if (imx_dma_hw_chain(imxdma)) {
643 del_timer(&imxdma->watchdog);
644 return;
645 }
646 }
647
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100648 imx_dmav1_writel(0, DMA_CCR(chno));
Sascha Hauer58a85f42008-09-09 10:19:44 +0200649 imxdma->in_use = 0;
650 if (imxdma->irq_handler)
651 imxdma->irq_handler(chno, imxdma->data);
652}
653
654static irqreturn_t dma_irq_handler(int irq, void *dev_id)
655{
656 int i, disr;
657
658#ifdef CONFIG_ARCH_MX2
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100659 if (cpu_is_mx21() || cpu_is_mx27())
660 dma_err_handler(irq, dev_id);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200661#endif
662
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100663 disr = imx_dmav1_readl(DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200664
665 pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
666 disr);
667
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100668 imx_dmav1_writel(disr, DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200669 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
670 if (disr & (1 << i))
671 dma_irq_handle_channel(i);
672 }
673
674 return IRQ_HANDLED;
675}
676
677/**
678 * imx_dma_request - request/allocate specified channel number
679 * @channel: i.MX DMA channel number
680 * @name: the driver/caller own non-%NULL identification
681 */
682int imx_dma_request(int channel, const char *name)
683{
684 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
685 unsigned long flags;
Sascha Hauer7c995022008-12-18 10:01:49 +0100686 int ret = 0;
Sascha Hauer58a85f42008-09-09 10:19:44 +0200687
688 /* basic sanity checks */
689 if (!name)
690 return -EINVAL;
691
692 if (channel >= IMX_DMA_CHANNELS) {
693 printk(KERN_CRIT "%s: called for non-existed channel %d\n",
694 __func__, channel);
695 return -EINVAL;
696 }
697
698 local_irq_save(flags);
699 if (imxdma->name) {
700 local_irq_restore(flags);
701 return -EBUSY;
702 }
Ilia Mirkinf7fdaca2011-02-20 17:10:51 -0500703 memset(imxdma, 0, sizeof(*imxdma));
Martin Fuzzeyf2292532009-03-26 22:27:52 +0100704 imxdma->name = name;
705 local_irq_restore(flags); /* request_irq() can block */
Sascha Hauer58a85f42008-09-09 10:19:44 +0200706
707#ifdef CONFIG_ARCH_MX2
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100708 if (cpu_is_mx21() || cpu_is_mx27()) {
709 ret = request_irq(MX2x_INT_DMACH0 + channel,
710 dma_irq_handler, 0, "DMA", NULL);
711 if (ret) {
712 imxdma->name = NULL;
713 pr_crit("Can't register IRQ %d for DMA channel %d\n",
714 MX2x_INT_DMACH0 + channel, channel);
715 return ret;
716 }
717 init_timer(&imxdma->watchdog);
718 imxdma->watchdog.function = &imx_dma_watchdog;
719 imxdma->watchdog.data = channel;
Sascha Hauer58a85f42008-09-09 10:19:44 +0200720 }
Sascha Hauer58a85f42008-09-09 10:19:44 +0200721#endif
722
Sascha Hauer7c995022008-12-18 10:01:49 +0100723 return ret;
Sascha Hauer58a85f42008-09-09 10:19:44 +0200724}
725EXPORT_SYMBOL(imx_dma_request);
726
727/**
728 * imx_dma_free - release previously acquired channel
729 * @channel: i.MX DMA channel number
730 */
731void imx_dma_free(int channel)
732{
733 unsigned long flags;
734 struct imx_dma_channel *imxdma = &imx_dma_channels[channel];
735
736 if (!imxdma->name) {
737 printk(KERN_CRIT
738 "%s: trying to free free channel %d\n",
739 __func__, channel);
740 return;
741 }
742
743 local_irq_save(flags);
744 /* Disable interrupts */
Martin Fuzzeyde0096d2009-03-26 22:21:16 +0100745 imx_dma_disable(channel);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200746 imxdma->name = NULL;
747
748#ifdef CONFIG_ARCH_MX2
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100749 if (cpu_is_mx21() || cpu_is_mx27())
750 free_irq(MX2x_INT_DMACH0 + channel, NULL);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200751#endif
752
753 local_irq_restore(flags);
754}
755EXPORT_SYMBOL(imx_dma_free);
756
757/**
758 * imx_dma_request_by_prio - find and request some of free channels best
759 * suiting requested priority
760 * @channel: i.MX DMA channel number
761 * @name: the driver/caller own non-%NULL identification
762 *
763 * This function tries to find a free channel in the specified priority group
Sascha Hauer58a85f42008-09-09 10:19:44 +0200764 * if the priority cannot be achieved it tries to look for free channel
765 * in the higher and then even lower priority groups.
766 *
767 * Return value: If there is no free channel to allocate, -%ENODEV is returned.
768 * On successful allocation channel is returned.
769 */
770int imx_dma_request_by_prio(const char *name, enum imx_dma_prio prio)
771{
772 int i;
773 int best;
774
775 switch (prio) {
776 case (DMA_PRIO_HIGH):
777 best = 8;
778 break;
779 case (DMA_PRIO_MEDIUM):
780 best = 4;
781 break;
782 case (DMA_PRIO_LOW):
783 default:
784 best = 0;
785 break;
786 }
787
788 for (i = best; i < IMX_DMA_CHANNELS; i++)
789 if (!imx_dma_request(i, name))
790 return i;
791
792 for (i = best - 1; i >= 0; i--)
793 if (!imx_dma_request(i, name))
794 return i;
795
796 printk(KERN_ERR "%s: no free DMA channel found\n", __func__);
797
798 return -ENODEV;
799}
800EXPORT_SYMBOL(imx_dma_request_by_prio);
801
802static int __init imx_dma_init(void)
803{
804 int ret = 0;
805 int i;
806
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100807#ifdef CONFIG_ARCH_MX1
808 if (cpu_is_mx1())
809 imx_dmav1_baseaddr = MX1_IO_ADDRESS(MX1_DMA_BASE_ADDR);
810 else
811#endif
812#ifdef CONFIG_MACH_MX21
813 if (cpu_is_mx21())
814 imx_dmav1_baseaddr = MX21_IO_ADDRESS(MX21_DMA_BASE_ADDR);
815 else
816#endif
817#ifdef CONFIG_MACH_MX27
818 if (cpu_is_mx27())
819 imx_dmav1_baseaddr = MX27_IO_ADDRESS(MX27_DMA_BASE_ADDR);
820 else
821#endif
Sascha Hauer8bffb772010-11-04 23:07:56 +0100822 return 0;
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100823
Sascha Hauere65fb002009-02-16 14:29:10 +0100824 dma_clk = clk_get(NULL, "dma");
Vasiliy Kulikovabebbb42010-11-26 20:05:55 +0300825 if (IS_ERR(dma_clk))
826 return PTR_ERR(dma_clk);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200827 clk_enable(dma_clk);
828
829 /* reset DMA module */
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100830 imx_dmav1_writel(DCR_DRST, DMA_DCR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200831
832#ifdef CONFIG_ARCH_MX1
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100833 if (cpu_is_mx1()) {
834 ret = request_irq(MX1_DMA_INT, dma_irq_handler, 0, "DMA", NULL);
835 if (ret) {
836 pr_crit("Wow! Can't register IRQ for DMA\n");
837 return ret;
838 }
Sascha Hauer58a85f42008-09-09 10:19:44 +0200839
Uwe Kleine-König8735fde2010-02-17 22:17:43 +0100840 ret = request_irq(MX1_DMA_ERR, dma_err_handler, 0, "DMA", NULL);
841 if (ret) {
842 pr_crit("Wow! Can't register ERRIRQ for DMA\n");
843 free_irq(MX1_DMA_INT, NULL);
844 return ret;
845 }
Sascha Hauer58a85f42008-09-09 10:19:44 +0200846 }
847#endif
848 /* enable DMA module */
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100849 imx_dmav1_writel(DCR_DEN, DMA_DCR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200850
851 /* clear all interrupts */
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100852 imx_dmav1_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_DISR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200853
854 /* disable interrupts */
Uwe Kleine-Könige9f49d42010-02-17 19:37:05 +0100855 imx_dmav1_writel((1 << IMX_DMA_CHANNELS) - 1, DMA_DIMR);
Sascha Hauer58a85f42008-09-09 10:19:44 +0200856
857 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
858 imx_dma_channels[i].sg = NULL;
859 imx_dma_channels[i].dma_num = i;
860 }
861
862 return ret;
863}
864
865arch_initcall(imx_dma_init);