blob: daa84ee2a187626d37180be863f1bf323c75e24c [file] [log] [blame]
Shawn Guoa580b8c2011-02-27 00:47:42 +08001/*
2 * Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Refer to drivers/dma/imx-sdma.c
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/init.h>
12#include <linux/types.h>
13#include <linux/mm.h>
14#include <linux/interrupt.h>
15#include <linux/clk.h>
16#include <linux/wait.h>
17#include <linux/sched.h>
18#include <linux/semaphore.h>
19#include <linux/device.h>
20#include <linux/dma-mapping.h>
21#include <linux/slab.h>
22#include <linux/platform_device.h>
23#include <linux/dmaengine.h>
24#include <linux/delay.h>
25
26#include <asm/irq.h>
27#include <mach/mxs.h>
28#include <mach/dma.h>
29#include <mach/common.h>
30
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000031#include "dmaengine.h"
32
Shawn Guoa580b8c2011-02-27 00:47:42 +080033/*
34 * NOTE: The term "PIO" throughout the mxs-dma implementation means
35 * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
36 * dma can program the controller registers of peripheral devices.
37 */
38
39#define MXS_DMA_APBH 0
40#define MXS_DMA_APBX 1
41#define dma_is_apbh() (mxs_dma->dev_id == MXS_DMA_APBH)
42
43#define APBH_VERSION_LATEST 3
44#define apbh_is_old() (mxs_dma->version < APBH_VERSION_LATEST)
45
46#define HW_APBHX_CTRL0 0x000
47#define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
48#define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
Shawn Guoa580b8c2011-02-27 00:47:42 +080049#define BP_APBH_CTRL0_RESET_CHANNEL 16
50#define HW_APBHX_CTRL1 0x010
51#define HW_APBHX_CTRL2 0x020
52#define HW_APBHX_CHANNEL_CTRL 0x030
53#define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
54#define HW_APBH_VERSION (cpu_is_mx23() ? 0x3f0 : 0x800)
55#define HW_APBX_VERSION 0x800
56#define BP_APBHX_VERSION_MAJOR 24
57#define HW_APBHX_CHn_NXTCMDAR(n) \
58 (((dma_is_apbh() && apbh_is_old()) ? 0x050 : 0x110) + (n) * 0x70)
59#define HW_APBHX_CHn_SEMA(n) \
60 (((dma_is_apbh() && apbh_is_old()) ? 0x080 : 0x140) + (n) * 0x70)
61
62/*
63 * ccw bits definitions
64 *
65 * COMMAND: 0..1 (2)
66 * CHAIN: 2 (1)
67 * IRQ: 3 (1)
68 * NAND_LOCK: 4 (1) - not implemented
69 * NAND_WAIT4READY: 5 (1) - not implemented
70 * DEC_SEM: 6 (1)
71 * WAIT4END: 7 (1)
72 * HALT_ON_TERMINATE: 8 (1)
73 * TERMINATE_FLUSH: 9 (1)
74 * RESERVED: 10..11 (2)
75 * PIO_NUM: 12..15 (4)
76 */
77#define BP_CCW_COMMAND 0
78#define BM_CCW_COMMAND (3 << 0)
79#define CCW_CHAIN (1 << 2)
80#define CCW_IRQ (1 << 3)
81#define CCW_DEC_SEM (1 << 6)
82#define CCW_WAIT4END (1 << 7)
83#define CCW_HALT_ON_TERM (1 << 8)
84#define CCW_TERM_FLUSH (1 << 9)
85#define BP_CCW_PIO_NUM 12
86#define BM_CCW_PIO_NUM (0xf << 12)
87
88#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
89
90#define MXS_DMA_CMD_NO_XFER 0
91#define MXS_DMA_CMD_WRITE 1
92#define MXS_DMA_CMD_READ 2
93#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
94
95struct mxs_dma_ccw {
96 u32 next;
97 u16 bits;
98 u16 xfer_bytes;
99#define MAX_XFER_BYTES 0xff00
100 u32 bufaddr;
101#define MXS_PIO_WORDS 16
102 u32 pio_words[MXS_PIO_WORDS];
103};
104
105#define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
106
107struct mxs_dma_chan {
108 struct mxs_dma_engine *mxs_dma;
109 struct dma_chan chan;
110 struct dma_async_tx_descriptor desc;
111 struct tasklet_struct tasklet;
112 int chan_irq;
113 struct mxs_dma_ccw *ccw;
114 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100115 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800116 enum dma_status status;
117 unsigned int flags;
118#define MXS_DMA_SG_LOOP (1 << 0)
119};
120
121#define MXS_DMA_CHANNELS 16
122#define MXS_DMA_CHANNELS_MASK 0xffff
123
124struct mxs_dma_engine {
125 int dev_id;
126 unsigned int version;
127 void __iomem *base;
128 struct clk *clk;
129 struct dma_device dma_device;
130 struct device_dma_parameters dma_parms;
131 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
132};
133
134static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
135{
136 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
137 int chan_id = mxs_chan->chan.chan_id;
138
139 if (dma_is_apbh() && apbh_is_old())
140 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
141 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
142 else
143 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
144 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
145}
146
147static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
148{
149 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
150 int chan_id = mxs_chan->chan.chan_id;
151
152 /* set cmd_addr up */
153 writel(mxs_chan->ccw_phys,
154 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(chan_id));
155
Shawn Guoa580b8c2011-02-27 00:47:42 +0800156 /* write 1 to SEMA to kick off the channel */
157 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(chan_id));
158}
159
160static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
161{
Shawn Guoa580b8c2011-02-27 00:47:42 +0800162 mxs_chan->status = DMA_SUCCESS;
163}
164
165static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
166{
167 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
168 int chan_id = mxs_chan->chan.chan_id;
169
170 /* freeze the channel */
171 if (dma_is_apbh() && apbh_is_old())
172 writel(1 << chan_id,
173 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
174 else
175 writel(1 << chan_id,
176 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
177
178 mxs_chan->status = DMA_PAUSED;
179}
180
181static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
182{
183 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
184 int chan_id = mxs_chan->chan.chan_id;
185
186 /* unfreeze the channel */
187 if (dma_is_apbh() && apbh_is_old())
188 writel(1 << chan_id,
189 mxs_dma->base + HW_APBHX_CTRL0 + MXS_CLR_ADDR);
190 else
191 writel(1 << chan_id,
192 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_CLR_ADDR);
193
194 mxs_chan->status = DMA_IN_PROGRESS;
195}
196
197static dma_cookie_t mxs_dma_assign_cookie(struct mxs_dma_chan *mxs_chan)
198{
199 dma_cookie_t cookie = mxs_chan->chan.cookie;
200
201 if (++cookie < 0)
202 cookie = 1;
203
204 mxs_chan->chan.cookie = cookie;
205 mxs_chan->desc.cookie = cookie;
206
207 return cookie;
208}
209
210static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
211{
212 return container_of(chan, struct mxs_dma_chan, chan);
213}
214
215static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
216{
217 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(tx->chan);
218
219 mxs_dma_enable_chan(mxs_chan);
220
221 return mxs_dma_assign_cookie(mxs_chan);
222}
223
224static void mxs_dma_tasklet(unsigned long data)
225{
226 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
227
228 if (mxs_chan->desc.callback)
229 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
230}
231
232static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
233{
234 struct mxs_dma_engine *mxs_dma = dev_id;
235 u32 stat1, stat2;
236
237 /* completion status */
238 stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
239 stat1 &= MXS_DMA_CHANNELS_MASK;
240 writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + MXS_CLR_ADDR);
241
242 /* error status */
243 stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
244 writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + MXS_CLR_ADDR);
245
246 /*
247 * When both completion and error of termination bits set at the
248 * same time, we do not take it as an error. IOW, it only becomes
Lothar Waßmann40031222011-12-08 09:15:41 +0100249 * an error we need to handle here in case of either it's (1) a bus
Shawn Guoa580b8c2011-02-27 00:47:42 +0800250 * error or (2) a termination error with no completion.
251 */
252 stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
253 (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
254
255 /* combine error and completion status for checking */
256 stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
257 while (stat1) {
258 int channel = fls(stat1) - 1;
259 struct mxs_dma_chan *mxs_chan =
260 &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
261
262 if (channel >= MXS_DMA_CHANNELS) {
263 dev_dbg(mxs_dma->dma_device.dev,
264 "%s: error in channel %d\n", __func__,
265 channel - MXS_DMA_CHANNELS);
266 mxs_chan->status = DMA_ERROR;
267 mxs_dma_reset_chan(mxs_chan);
268 } else {
269 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
270 mxs_chan->status = DMA_IN_PROGRESS;
271 else
272 mxs_chan->status = DMA_SUCCESS;
273 }
274
275 stat1 &= ~(1 << channel);
276
277 if (mxs_chan->status == DMA_SUCCESS)
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000278 mxs_chan->chan.completed_cookie = mxs_chan->desc.cookie;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800279
280 /* schedule tasklet on this channel */
281 tasklet_schedule(&mxs_chan->tasklet);
282 }
283
284 return IRQ_HANDLED;
285}
286
287static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
288{
289 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
290 struct mxs_dma_data *data = chan->private;
291 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
292 int ret;
293
294 if (!data)
295 return -EINVAL;
296
297 mxs_chan->chan_irq = data->chan_irq;
298
299 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
300 &mxs_chan->ccw_phys, GFP_KERNEL);
301 if (!mxs_chan->ccw) {
302 ret = -ENOMEM;
303 goto err_alloc;
304 }
305
306 memset(mxs_chan->ccw, 0, PAGE_SIZE);
307
Shawn Guo95bfea12011-06-30 16:06:33 +0800308 if (mxs_chan->chan_irq != NO_IRQ) {
309 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
310 0, "mxs-dma", mxs_dma);
311 if (ret)
312 goto err_irq;
313 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800314
Shawn Guo759a2e32011-12-20 13:54:00 +0800315 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800316 if (ret)
317 goto err_clk;
318
319 mxs_dma_reset_chan(mxs_chan);
320
321 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
322 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
323
324 /* the descriptor is ready */
325 async_tx_ack(&mxs_chan->desc);
326
327 return 0;
328
329err_clk:
330 free_irq(mxs_chan->chan_irq, mxs_dma);
331err_irq:
332 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
333 mxs_chan->ccw, mxs_chan->ccw_phys);
334err_alloc:
335 return ret;
336}
337
338static void mxs_dma_free_chan_resources(struct dma_chan *chan)
339{
340 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
341 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
342
343 mxs_dma_disable_chan(mxs_chan);
344
345 free_irq(mxs_chan->chan_irq, mxs_dma);
346
347 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
348 mxs_chan->ccw, mxs_chan->ccw_phys);
349
Shawn Guo759a2e32011-12-20 13:54:00 +0800350 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800351}
352
353static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
354 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530355 unsigned int sg_len, enum dma_transfer_direction direction,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800356 unsigned long append)
357{
358 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
359 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
360 struct mxs_dma_ccw *ccw;
361 struct scatterlist *sg;
362 int i, j;
363 u32 *pio;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100364 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800365
366 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
367 return NULL;
368
369 if (sg_len + (append ? idx : 0) > NUM_CCW) {
370 dev_err(mxs_dma->dma_device.dev,
371 "maximum number of sg exceeded: %d > %d\n",
372 sg_len, NUM_CCW);
373 goto err_out;
374 }
375
376 mxs_chan->status = DMA_IN_PROGRESS;
377 mxs_chan->flags = 0;
378
379 /*
380 * If the sg is prepared with append flag set, the sg
381 * will be appended to the last prepared sg.
382 */
383 if (append) {
384 BUG_ON(idx < 1);
385 ccw = &mxs_chan->ccw[idx - 1];
386 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
387 ccw->bits |= CCW_CHAIN;
388 ccw->bits &= ~CCW_IRQ;
389 ccw->bits &= ~CCW_DEC_SEM;
390 ccw->bits &= ~CCW_WAIT4END;
391 } else {
392 idx = 0;
393 }
394
Shawn Guo62268ce2011-12-13 23:48:03 +0800395 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800396 ccw = &mxs_chan->ccw[idx++];
397 pio = (u32 *) sgl;
398
399 for (j = 0; j < sg_len;)
400 ccw->pio_words[j++] = *pio++;
401
402 ccw->bits = 0;
403 ccw->bits |= CCW_IRQ;
404 ccw->bits |= CCW_DEC_SEM;
405 ccw->bits |= CCW_WAIT4END;
406 ccw->bits |= CCW_HALT_ON_TERM;
407 ccw->bits |= CCW_TERM_FLUSH;
408 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
409 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
410 } else {
411 for_each_sg(sgl, sg, sg_len, i) {
412 if (sg->length > MAX_XFER_BYTES) {
413 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
414 sg->length, MAX_XFER_BYTES);
415 goto err_out;
416 }
417
418 ccw = &mxs_chan->ccw[idx++];
419
420 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
421 ccw->bufaddr = sg->dma_address;
422 ccw->xfer_bytes = sg->length;
423
424 ccw->bits = 0;
425 ccw->bits |= CCW_CHAIN;
426 ccw->bits |= CCW_HALT_ON_TERM;
427 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530428 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800429 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
430 COMMAND);
431
432 if (i + 1 == sg_len) {
433 ccw->bits &= ~CCW_CHAIN;
434 ccw->bits |= CCW_IRQ;
435 ccw->bits |= CCW_DEC_SEM;
436 ccw->bits |= CCW_WAIT4END;
437 }
438 }
439 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100440 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800441
442 return &mxs_chan->desc;
443
444err_out:
445 mxs_chan->status = DMA_ERROR;
446 return NULL;
447}
448
449static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
450 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530451 size_t period_len, enum dma_transfer_direction direction)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800452{
453 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
454 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
455 int num_periods = buf_len / period_len;
456 int i = 0, buf = 0;
457
458 if (mxs_chan->status == DMA_IN_PROGRESS)
459 return NULL;
460
461 mxs_chan->status = DMA_IN_PROGRESS;
462 mxs_chan->flags |= MXS_DMA_SG_LOOP;
463
464 if (num_periods > NUM_CCW) {
465 dev_err(mxs_dma->dma_device.dev,
466 "maximum number of sg exceeded: %d > %d\n",
467 num_periods, NUM_CCW);
468 goto err_out;
469 }
470
471 if (period_len > MAX_XFER_BYTES) {
472 dev_err(mxs_dma->dma_device.dev,
473 "maximum period size exceeded: %d > %d\n",
474 period_len, MAX_XFER_BYTES);
475 goto err_out;
476 }
477
478 while (buf < buf_len) {
479 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
480
481 if (i + 1 == num_periods)
482 ccw->next = mxs_chan->ccw_phys;
483 else
484 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
485
486 ccw->bufaddr = dma_addr;
487 ccw->xfer_bytes = period_len;
488
489 ccw->bits = 0;
490 ccw->bits |= CCW_CHAIN;
491 ccw->bits |= CCW_IRQ;
492 ccw->bits |= CCW_HALT_ON_TERM;
493 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530494 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800495 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
496
497 dma_addr += period_len;
498 buf += period_len;
499
500 i++;
501 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100502 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800503
504 return &mxs_chan->desc;
505
506err_out:
507 mxs_chan->status = DMA_ERROR;
508 return NULL;
509}
510
511static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
512 unsigned long arg)
513{
514 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
515 int ret = 0;
516
517 switch (cmd) {
518 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800519 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100520 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800521 break;
522 case DMA_PAUSE:
523 mxs_dma_pause_chan(mxs_chan);
524 break;
525 case DMA_RESUME:
526 mxs_dma_resume_chan(mxs_chan);
527 break;
528 default:
529 ret = -ENOSYS;
530 }
531
532 return ret;
533}
534
535static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
536 dma_cookie_t cookie, struct dma_tx_state *txstate)
537{
538 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
539 dma_cookie_t last_used;
540
541 last_used = chan->cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000542 dma_set_tx_state(txstate, chan->completed_cookie, last_used, 0);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800543
544 return mxs_chan->status;
545}
546
547static void mxs_dma_issue_pending(struct dma_chan *chan)
548{
549 /*
550 * Nothing to do. We only have a single descriptor.
551 */
552}
553
554static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
555{
556 int ret;
557
Shawn Guo759a2e32011-12-20 13:54:00 +0800558 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800559 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100560 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800561
562 ret = mxs_reset_block(mxs_dma->base);
563 if (ret)
564 goto err_out;
565
566 /* only major version matters */
567 mxs_dma->version = readl(mxs_dma->base +
568 ((mxs_dma->dev_id == MXS_DMA_APBX) ?
569 HW_APBX_VERSION : HW_APBH_VERSION)) >>
570 BP_APBHX_VERSION_MAJOR;
571
572 /* enable apbh burst */
573 if (dma_is_apbh()) {
574 writel(BM_APBH_CTRL0_APB_BURST_EN,
575 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
576 writel(BM_APBH_CTRL0_APB_BURST8_EN,
577 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
578 }
579
580 /* enable irq for all the channels */
581 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
582 mxs_dma->base + HW_APBHX_CTRL1 + MXS_SET_ADDR);
583
Shawn Guoa580b8c2011-02-27 00:47:42 +0800584err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800585 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800586 return ret;
587}
588
589static int __init mxs_dma_probe(struct platform_device *pdev)
590{
591 const struct platform_device_id *id_entry =
592 platform_get_device_id(pdev);
593 struct mxs_dma_engine *mxs_dma;
594 struct resource *iores;
595 int ret, i;
596
597 mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
598 if (!mxs_dma)
599 return -ENOMEM;
600
601 mxs_dma->dev_id = id_entry->driver_data;
602
603 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
604
605 if (!request_mem_region(iores->start, resource_size(iores),
606 pdev->name)) {
607 ret = -EBUSY;
608 goto err_request_region;
609 }
610
611 mxs_dma->base = ioremap(iores->start, resource_size(iores));
612 if (!mxs_dma->base) {
613 ret = -ENOMEM;
614 goto err_ioremap;
615 }
616
617 mxs_dma->clk = clk_get(&pdev->dev, NULL);
618 if (IS_ERR(mxs_dma->clk)) {
619 ret = PTR_ERR(mxs_dma->clk);
620 goto err_clk;
621 }
622
623 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
624 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
625
626 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
627
628 /* Initialize channel parameters */
629 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
630 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
631
632 mxs_chan->mxs_dma = mxs_dma;
633 mxs_chan->chan.device = &mxs_dma->dma_device;
634
635 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
636 (unsigned long) mxs_chan);
637
638
639 /* Add the channel to mxs_chan list */
640 list_add_tail(&mxs_chan->chan.device_node,
641 &mxs_dma->dma_device.channels);
642 }
643
644 ret = mxs_dma_init(mxs_dma);
645 if (ret)
646 goto err_init;
647
648 mxs_dma->dma_device.dev = &pdev->dev;
649
650 /* mxs_dma gets 65535 bytes maximum sg size */
651 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
652 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
653
654 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
655 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
656 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
657 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
658 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
659 mxs_dma->dma_device.device_control = mxs_dma_control;
660 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
661
662 ret = dma_async_device_register(&mxs_dma->dma_device);
663 if (ret) {
664 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
665 goto err_init;
666 }
667
668 dev_info(mxs_dma->dma_device.dev, "initialized\n");
669
670 return 0;
671
672err_init:
673 clk_put(mxs_dma->clk);
674err_clk:
675 iounmap(mxs_dma->base);
676err_ioremap:
677 release_mem_region(iores->start, resource_size(iores));
678err_request_region:
679 kfree(mxs_dma);
680 return ret;
681}
682
683static struct platform_device_id mxs_dma_type[] = {
684 {
685 .name = "mxs-dma-apbh",
686 .driver_data = MXS_DMA_APBH,
687 }, {
688 .name = "mxs-dma-apbx",
689 .driver_data = MXS_DMA_APBX,
Axel Lin2a9778e2011-07-12 18:53:52 +0800690 }, {
691 /* end of list */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800692 }
693};
694
695static struct platform_driver mxs_dma_driver = {
696 .driver = {
697 .name = "mxs-dma",
698 },
699 .id_table = mxs_dma_type,
700};
701
702static int __init mxs_dma_module_init(void)
703{
704 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
705}
706subsys_initcall(mxs_dma_module_init);