blob: 0ddfd30b56ad21e3ba5d8a127c89d401baf5e84b [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>
Huang Shijie39468602012-02-16 14:17:32 +080025#include <linux/fsl/mxs-dma.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080026
27#include <asm/irq.h>
28#include <mach/mxs.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080029#include <mach/common.h>
30
31/*
32 * NOTE: The term "PIO" throughout the mxs-dma implementation means
33 * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
34 * dma can program the controller registers of peripheral devices.
35 */
36
37#define MXS_DMA_APBH 0
38#define MXS_DMA_APBX 1
39#define dma_is_apbh() (mxs_dma->dev_id == MXS_DMA_APBH)
40
41#define APBH_VERSION_LATEST 3
42#define apbh_is_old() (mxs_dma->version < APBH_VERSION_LATEST)
43
44#define HW_APBHX_CTRL0 0x000
45#define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
46#define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
Shawn Guoa580b8c2011-02-27 00:47:42 +080047#define BP_APBH_CTRL0_RESET_CHANNEL 16
48#define HW_APBHX_CTRL1 0x010
49#define HW_APBHX_CTRL2 0x020
50#define HW_APBHX_CHANNEL_CTRL 0x030
51#define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
52#define HW_APBH_VERSION (cpu_is_mx23() ? 0x3f0 : 0x800)
53#define HW_APBX_VERSION 0x800
54#define BP_APBHX_VERSION_MAJOR 24
55#define HW_APBHX_CHn_NXTCMDAR(n) \
56 (((dma_is_apbh() && apbh_is_old()) ? 0x050 : 0x110) + (n) * 0x70)
57#define HW_APBHX_CHn_SEMA(n) \
58 (((dma_is_apbh() && apbh_is_old()) ? 0x080 : 0x140) + (n) * 0x70)
59
60/*
61 * ccw bits definitions
62 *
63 * COMMAND: 0..1 (2)
64 * CHAIN: 2 (1)
65 * IRQ: 3 (1)
66 * NAND_LOCK: 4 (1) - not implemented
67 * NAND_WAIT4READY: 5 (1) - not implemented
68 * DEC_SEM: 6 (1)
69 * WAIT4END: 7 (1)
70 * HALT_ON_TERMINATE: 8 (1)
71 * TERMINATE_FLUSH: 9 (1)
72 * RESERVED: 10..11 (2)
73 * PIO_NUM: 12..15 (4)
74 */
75#define BP_CCW_COMMAND 0
76#define BM_CCW_COMMAND (3 << 0)
77#define CCW_CHAIN (1 << 2)
78#define CCW_IRQ (1 << 3)
79#define CCW_DEC_SEM (1 << 6)
80#define CCW_WAIT4END (1 << 7)
81#define CCW_HALT_ON_TERM (1 << 8)
82#define CCW_TERM_FLUSH (1 << 9)
83#define BP_CCW_PIO_NUM 12
84#define BM_CCW_PIO_NUM (0xf << 12)
85
86#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
87
88#define MXS_DMA_CMD_NO_XFER 0
89#define MXS_DMA_CMD_WRITE 1
90#define MXS_DMA_CMD_READ 2
91#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
92
93struct mxs_dma_ccw {
94 u32 next;
95 u16 bits;
96 u16 xfer_bytes;
97#define MAX_XFER_BYTES 0xff00
98 u32 bufaddr;
99#define MXS_PIO_WORDS 16
100 u32 pio_words[MXS_PIO_WORDS];
101};
102
103#define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
104
105struct mxs_dma_chan {
106 struct mxs_dma_engine *mxs_dma;
107 struct dma_chan chan;
108 struct dma_async_tx_descriptor desc;
109 struct tasklet_struct tasklet;
110 int chan_irq;
111 struct mxs_dma_ccw *ccw;
112 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100113 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800114 dma_cookie_t last_completed;
115 enum dma_status status;
116 unsigned int flags;
117#define MXS_DMA_SG_LOOP (1 << 0)
118};
119
120#define MXS_DMA_CHANNELS 16
121#define MXS_DMA_CHANNELS_MASK 0xffff
122
123struct mxs_dma_engine {
124 int dev_id;
125 unsigned int version;
126 void __iomem *base;
127 struct clk *clk;
128 struct dma_device dma_device;
129 struct device_dma_parameters dma_parms;
130 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
131};
132
133static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
134{
135 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
136 int chan_id = mxs_chan->chan.chan_id;
137
138 if (dma_is_apbh() && apbh_is_old())
139 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
140 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
141 else
142 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
143 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
144}
145
146static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
147{
148 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
149 int chan_id = mxs_chan->chan.chan_id;
150
151 /* set cmd_addr up */
152 writel(mxs_chan->ccw_phys,
153 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(chan_id));
154
Shawn Guoa580b8c2011-02-27 00:47:42 +0800155 /* write 1 to SEMA to kick off the channel */
156 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(chan_id));
157}
158
159static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
160{
Shawn Guoa580b8c2011-02-27 00:47:42 +0800161 mxs_chan->status = DMA_SUCCESS;
162}
163
164static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
165{
166 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
167 int chan_id = mxs_chan->chan.chan_id;
168
169 /* freeze the channel */
170 if (dma_is_apbh() && apbh_is_old())
171 writel(1 << chan_id,
172 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
173 else
174 writel(1 << chan_id,
175 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_SET_ADDR);
176
177 mxs_chan->status = DMA_PAUSED;
178}
179
180static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
181{
182 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
183 int chan_id = mxs_chan->chan.chan_id;
184
185 /* unfreeze the channel */
186 if (dma_is_apbh() && apbh_is_old())
187 writel(1 << chan_id,
188 mxs_dma->base + HW_APBHX_CTRL0 + MXS_CLR_ADDR);
189 else
190 writel(1 << chan_id,
191 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + MXS_CLR_ADDR);
192
193 mxs_chan->status = DMA_IN_PROGRESS;
194}
195
196static dma_cookie_t mxs_dma_assign_cookie(struct mxs_dma_chan *mxs_chan)
197{
198 dma_cookie_t cookie = mxs_chan->chan.cookie;
199
200 if (++cookie < 0)
201 cookie = 1;
202
203 mxs_chan->chan.cookie = cookie;
204 mxs_chan->desc.cookie = cookie;
205
206 return cookie;
207}
208
209static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
210{
211 return container_of(chan, struct mxs_dma_chan, chan);
212}
213
214static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
215{
216 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(tx->chan);
217
218 mxs_dma_enable_chan(mxs_chan);
219
220 return mxs_dma_assign_cookie(mxs_chan);
221}
222
223static void mxs_dma_tasklet(unsigned long data)
224{
225 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
226
227 if (mxs_chan->desc.callback)
228 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
229}
230
231static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
232{
233 struct mxs_dma_engine *mxs_dma = dev_id;
234 u32 stat1, stat2;
235
236 /* completion status */
237 stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
238 stat1 &= MXS_DMA_CHANNELS_MASK;
239 writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + MXS_CLR_ADDR);
240
241 /* error status */
242 stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
243 writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + MXS_CLR_ADDR);
244
245 /*
246 * When both completion and error of termination bits set at the
247 * same time, we do not take it as an error. IOW, it only becomes
Lothar Waßmann40031222011-12-08 09:15:41 +0100248 * an error we need to handle here in case of either it's (1) a bus
Shawn Guoa580b8c2011-02-27 00:47:42 +0800249 * error or (2) a termination error with no completion.
250 */
251 stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
252 (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
253
254 /* combine error and completion status for checking */
255 stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
256 while (stat1) {
257 int channel = fls(stat1) - 1;
258 struct mxs_dma_chan *mxs_chan =
259 &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
260
261 if (channel >= MXS_DMA_CHANNELS) {
262 dev_dbg(mxs_dma->dma_device.dev,
263 "%s: error in channel %d\n", __func__,
264 channel - MXS_DMA_CHANNELS);
265 mxs_chan->status = DMA_ERROR;
266 mxs_dma_reset_chan(mxs_chan);
267 } else {
268 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
269 mxs_chan->status = DMA_IN_PROGRESS;
270 else
271 mxs_chan->status = DMA_SUCCESS;
272 }
273
274 stat1 &= ~(1 << channel);
275
276 if (mxs_chan->status == DMA_SUCCESS)
277 mxs_chan->last_completed = mxs_chan->desc.cookie;
278
279 /* schedule tasklet on this channel */
280 tasklet_schedule(&mxs_chan->tasklet);
281 }
282
283 return IRQ_HANDLED;
284}
285
286static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
287{
288 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
289 struct mxs_dma_data *data = chan->private;
290 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
291 int ret;
292
293 if (!data)
294 return -EINVAL;
295
296 mxs_chan->chan_irq = data->chan_irq;
297
298 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
299 &mxs_chan->ccw_phys, GFP_KERNEL);
300 if (!mxs_chan->ccw) {
301 ret = -ENOMEM;
302 goto err_alloc;
303 }
304
305 memset(mxs_chan->ccw, 0, PAGE_SIZE);
306
Shawn Guo95bfea12011-06-30 16:06:33 +0800307 if (mxs_chan->chan_irq != NO_IRQ) {
308 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
309 0, "mxs-dma", mxs_dma);
310 if (ret)
311 goto err_irq;
312 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800313
Shawn Guo759a2e32011-12-20 13:54:00 +0800314 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800315 if (ret)
316 goto err_clk;
317
318 mxs_dma_reset_chan(mxs_chan);
319
320 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
321 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
322
323 /* the descriptor is ready */
324 async_tx_ack(&mxs_chan->desc);
325
326 return 0;
327
328err_clk:
329 free_irq(mxs_chan->chan_irq, mxs_dma);
330err_irq:
331 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
332 mxs_chan->ccw, mxs_chan->ccw_phys);
333err_alloc:
334 return ret;
335}
336
337static void mxs_dma_free_chan_resources(struct dma_chan *chan)
338{
339 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
340 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
341
342 mxs_dma_disable_chan(mxs_chan);
343
344 free_irq(mxs_chan->chan_irq, mxs_dma);
345
346 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
347 mxs_chan->ccw, mxs_chan->ccw_phys);
348
Shawn Guo759a2e32011-12-20 13:54:00 +0800349 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800350}
351
Huang Shijie921de862012-02-16 14:17:33 +0800352/*
353 * How to use the flags for ->device_prep_slave_sg() :
354 * [1] If there is only one DMA command in the DMA chain, the code should be:
355 * ......
356 * ->device_prep_slave_sg(DMA_CTRL_ACK);
357 * ......
358 * [2] If there are two DMA commands in the DMA chain, the code should be
359 * ......
360 * ->device_prep_slave_sg(0);
361 * ......
362 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
363 * ......
364 * [3] If there are more than two DMA commands in the DMA chain, the code
365 * should be:
366 * ......
367 * ->device_prep_slave_sg(0); // First
368 * ......
369 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
370 * ......
371 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
372 * ......
373 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800374static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
375 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530376 unsigned int sg_len, enum dma_transfer_direction direction,
Huang Shijie921de862012-02-16 14:17:33 +0800377 unsigned long flags)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800378{
379 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
380 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
381 struct mxs_dma_ccw *ccw;
382 struct scatterlist *sg;
383 int i, j;
384 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800385 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100386 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800387
388 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
389 return NULL;
390
391 if (sg_len + (append ? idx : 0) > NUM_CCW) {
392 dev_err(mxs_dma->dma_device.dev,
393 "maximum number of sg exceeded: %d > %d\n",
394 sg_len, NUM_CCW);
395 goto err_out;
396 }
397
398 mxs_chan->status = DMA_IN_PROGRESS;
399 mxs_chan->flags = 0;
400
401 /*
402 * If the sg is prepared with append flag set, the sg
403 * will be appended to the last prepared sg.
404 */
405 if (append) {
406 BUG_ON(idx < 1);
407 ccw = &mxs_chan->ccw[idx - 1];
408 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
409 ccw->bits |= CCW_CHAIN;
410 ccw->bits &= ~CCW_IRQ;
411 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800412 } else {
413 idx = 0;
414 }
415
Shawn Guo62268ce2011-12-13 23:48:03 +0800416 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800417 ccw = &mxs_chan->ccw[idx++];
418 pio = (u32 *) sgl;
419
420 for (j = 0; j < sg_len;)
421 ccw->pio_words[j++] = *pio++;
422
423 ccw->bits = 0;
424 ccw->bits |= CCW_IRQ;
425 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800426 if (flags & DMA_CTRL_ACK)
427 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800428 ccw->bits |= CCW_HALT_ON_TERM;
429 ccw->bits |= CCW_TERM_FLUSH;
430 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
431 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
432 } else {
433 for_each_sg(sgl, sg, sg_len, i) {
434 if (sg->length > MAX_XFER_BYTES) {
435 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
436 sg->length, MAX_XFER_BYTES);
437 goto err_out;
438 }
439
440 ccw = &mxs_chan->ccw[idx++];
441
442 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
443 ccw->bufaddr = sg->dma_address;
444 ccw->xfer_bytes = sg->length;
445
446 ccw->bits = 0;
447 ccw->bits |= CCW_CHAIN;
448 ccw->bits |= CCW_HALT_ON_TERM;
449 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530450 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800451 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
452 COMMAND);
453
454 if (i + 1 == sg_len) {
455 ccw->bits &= ~CCW_CHAIN;
456 ccw->bits |= CCW_IRQ;
457 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800458 if (flags & DMA_CTRL_ACK)
459 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800460 }
461 }
462 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100463 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800464
465 return &mxs_chan->desc;
466
467err_out:
468 mxs_chan->status = DMA_ERROR;
469 return NULL;
470}
471
472static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
473 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530474 size_t period_len, enum dma_transfer_direction direction)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800475{
476 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
477 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
478 int num_periods = buf_len / period_len;
479 int i = 0, buf = 0;
480
481 if (mxs_chan->status == DMA_IN_PROGRESS)
482 return NULL;
483
484 mxs_chan->status = DMA_IN_PROGRESS;
485 mxs_chan->flags |= MXS_DMA_SG_LOOP;
486
487 if (num_periods > NUM_CCW) {
488 dev_err(mxs_dma->dma_device.dev,
489 "maximum number of sg exceeded: %d > %d\n",
490 num_periods, NUM_CCW);
491 goto err_out;
492 }
493
494 if (period_len > MAX_XFER_BYTES) {
495 dev_err(mxs_dma->dma_device.dev,
496 "maximum period size exceeded: %d > %d\n",
497 period_len, MAX_XFER_BYTES);
498 goto err_out;
499 }
500
501 while (buf < buf_len) {
502 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
503
504 if (i + 1 == num_periods)
505 ccw->next = mxs_chan->ccw_phys;
506 else
507 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
508
509 ccw->bufaddr = dma_addr;
510 ccw->xfer_bytes = period_len;
511
512 ccw->bits = 0;
513 ccw->bits |= CCW_CHAIN;
514 ccw->bits |= CCW_IRQ;
515 ccw->bits |= CCW_HALT_ON_TERM;
516 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530517 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800518 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
519
520 dma_addr += period_len;
521 buf += period_len;
522
523 i++;
524 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100525 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800526
527 return &mxs_chan->desc;
528
529err_out:
530 mxs_chan->status = DMA_ERROR;
531 return NULL;
532}
533
534static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
535 unsigned long arg)
536{
537 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
538 int ret = 0;
539
540 switch (cmd) {
541 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800542 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100543 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800544 break;
545 case DMA_PAUSE:
546 mxs_dma_pause_chan(mxs_chan);
547 break;
548 case DMA_RESUME:
549 mxs_dma_resume_chan(mxs_chan);
550 break;
551 default:
552 ret = -ENOSYS;
553 }
554
555 return ret;
556}
557
558static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
559 dma_cookie_t cookie, struct dma_tx_state *txstate)
560{
561 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
562 dma_cookie_t last_used;
563
564 last_used = chan->cookie;
565 dma_set_tx_state(txstate, mxs_chan->last_completed, last_used, 0);
566
567 return mxs_chan->status;
568}
569
570static void mxs_dma_issue_pending(struct dma_chan *chan)
571{
572 /*
573 * Nothing to do. We only have a single descriptor.
574 */
575}
576
577static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
578{
579 int ret;
580
Shawn Guo759a2e32011-12-20 13:54:00 +0800581 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800582 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100583 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800584
585 ret = mxs_reset_block(mxs_dma->base);
586 if (ret)
587 goto err_out;
588
589 /* only major version matters */
590 mxs_dma->version = readl(mxs_dma->base +
591 ((mxs_dma->dev_id == MXS_DMA_APBX) ?
592 HW_APBX_VERSION : HW_APBH_VERSION)) >>
593 BP_APBHX_VERSION_MAJOR;
594
595 /* enable apbh burst */
596 if (dma_is_apbh()) {
597 writel(BM_APBH_CTRL0_APB_BURST_EN,
598 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
599 writel(BM_APBH_CTRL0_APB_BURST8_EN,
600 mxs_dma->base + HW_APBHX_CTRL0 + MXS_SET_ADDR);
601 }
602
603 /* enable irq for all the channels */
604 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
605 mxs_dma->base + HW_APBHX_CTRL1 + MXS_SET_ADDR);
606
Shawn Guoa580b8c2011-02-27 00:47:42 +0800607err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800608 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800609 return ret;
610}
611
612static int __init mxs_dma_probe(struct platform_device *pdev)
613{
614 const struct platform_device_id *id_entry =
615 platform_get_device_id(pdev);
616 struct mxs_dma_engine *mxs_dma;
617 struct resource *iores;
618 int ret, i;
619
620 mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
621 if (!mxs_dma)
622 return -ENOMEM;
623
624 mxs_dma->dev_id = id_entry->driver_data;
625
626 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
627
628 if (!request_mem_region(iores->start, resource_size(iores),
629 pdev->name)) {
630 ret = -EBUSY;
631 goto err_request_region;
632 }
633
634 mxs_dma->base = ioremap(iores->start, resource_size(iores));
635 if (!mxs_dma->base) {
636 ret = -ENOMEM;
637 goto err_ioremap;
638 }
639
640 mxs_dma->clk = clk_get(&pdev->dev, NULL);
641 if (IS_ERR(mxs_dma->clk)) {
642 ret = PTR_ERR(mxs_dma->clk);
643 goto err_clk;
644 }
645
646 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
647 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
648
649 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
650
651 /* Initialize channel parameters */
652 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
653 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
654
655 mxs_chan->mxs_dma = mxs_dma;
656 mxs_chan->chan.device = &mxs_dma->dma_device;
657
658 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
659 (unsigned long) mxs_chan);
660
661
662 /* Add the channel to mxs_chan list */
663 list_add_tail(&mxs_chan->chan.device_node,
664 &mxs_dma->dma_device.channels);
665 }
666
667 ret = mxs_dma_init(mxs_dma);
668 if (ret)
669 goto err_init;
670
671 mxs_dma->dma_device.dev = &pdev->dev;
672
673 /* mxs_dma gets 65535 bytes maximum sg size */
674 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
675 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
676
677 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
678 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
679 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
680 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
681 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
682 mxs_dma->dma_device.device_control = mxs_dma_control;
683 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
684
685 ret = dma_async_device_register(&mxs_dma->dma_device);
686 if (ret) {
687 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
688 goto err_init;
689 }
690
691 dev_info(mxs_dma->dma_device.dev, "initialized\n");
692
693 return 0;
694
695err_init:
696 clk_put(mxs_dma->clk);
697err_clk:
698 iounmap(mxs_dma->base);
699err_ioremap:
700 release_mem_region(iores->start, resource_size(iores));
701err_request_region:
702 kfree(mxs_dma);
703 return ret;
704}
705
706static struct platform_device_id mxs_dma_type[] = {
707 {
708 .name = "mxs-dma-apbh",
709 .driver_data = MXS_DMA_APBH,
710 }, {
711 .name = "mxs-dma-apbx",
712 .driver_data = MXS_DMA_APBX,
Axel Lin2a9778e2011-07-12 18:53:52 +0800713 }, {
714 /* end of list */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800715 }
716};
717
718static struct platform_driver mxs_dma_driver = {
719 .driver = {
720 .name = "mxs-dma",
721 },
722 .id_table = mxs_dma_type,
723};
724
725static int __init mxs_dma_module_init(void)
726{
727 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
728}
729subsys_initcall(mxs_dma_module_init);