blob: c93f9fa08cafba87c33d918b1fcaa3128a8b2c0c [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>
Dong Aishengf5b7efc2012-05-04 20:12:15 +080026#include <linux/stmp_device.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080027
28#include <asm/irq.h>
29#include <mach/mxs.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080030
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
Shawn Guobb11fb62012-05-07 14:14:08 +080041#define dma_is_apbh(mxs_dma) ((mxs_dma)->dev_id == MXS_DMA_APBH)
Shawn Guoa580b8c2011-02-27 00:47:42 +080042
43#define APBH_VERSION_LATEST 3
Shawn Guobb11fb62012-05-07 14:14:08 +080044#define apbh_is_old(mxs_dma) ((mxs_dma)->version < APBH_VERSION_LATEST)
Shawn Guoa580b8c2011-02-27 00:47:42 +080045
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
Shawn Guobb11fb62012-05-07 14:14:08 +080057/*
58 * The offset of NXTCMDAR register is different per both dma type and version,
59 * while stride for each channel is all the same 0x70.
60 */
61#define HW_APBHX_CHn_NXTCMDAR(d, n) \
62 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x050 : 0x110) + (n) * 0x70)
63#define HW_APBHX_CHn_SEMA(d, n) \
64 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x080 : 0x140) + (n) * 0x70)
Shawn Guoa580b8c2011-02-27 00:47:42 +080065
66/*
67 * ccw bits definitions
68 *
69 * COMMAND: 0..1 (2)
70 * CHAIN: 2 (1)
71 * IRQ: 3 (1)
72 * NAND_LOCK: 4 (1) - not implemented
73 * NAND_WAIT4READY: 5 (1) - not implemented
74 * DEC_SEM: 6 (1)
75 * WAIT4END: 7 (1)
76 * HALT_ON_TERMINATE: 8 (1)
77 * TERMINATE_FLUSH: 9 (1)
78 * RESERVED: 10..11 (2)
79 * PIO_NUM: 12..15 (4)
80 */
81#define BP_CCW_COMMAND 0
82#define BM_CCW_COMMAND (3 << 0)
83#define CCW_CHAIN (1 << 2)
84#define CCW_IRQ (1 << 3)
85#define CCW_DEC_SEM (1 << 6)
86#define CCW_WAIT4END (1 << 7)
87#define CCW_HALT_ON_TERM (1 << 8)
88#define CCW_TERM_FLUSH (1 << 9)
89#define BP_CCW_PIO_NUM 12
90#define BM_CCW_PIO_NUM (0xf << 12)
91
92#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
93
94#define MXS_DMA_CMD_NO_XFER 0
95#define MXS_DMA_CMD_WRITE 1
96#define MXS_DMA_CMD_READ 2
97#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
98
99struct mxs_dma_ccw {
100 u32 next;
101 u16 bits;
102 u16 xfer_bytes;
103#define MAX_XFER_BYTES 0xff00
104 u32 bufaddr;
105#define MXS_PIO_WORDS 16
106 u32 pio_words[MXS_PIO_WORDS];
107};
108
109#define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
110
111struct mxs_dma_chan {
112 struct mxs_dma_engine *mxs_dma;
113 struct dma_chan chan;
114 struct dma_async_tx_descriptor desc;
115 struct tasklet_struct tasklet;
116 int chan_irq;
117 struct mxs_dma_ccw *ccw;
118 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100119 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800120 enum dma_status status;
121 unsigned int flags;
122#define MXS_DMA_SG_LOOP (1 << 0)
123};
124
125#define MXS_DMA_CHANNELS 16
126#define MXS_DMA_CHANNELS_MASK 0xffff
127
128struct mxs_dma_engine {
129 int dev_id;
130 unsigned int version;
131 void __iomem *base;
132 struct clk *clk;
133 struct dma_device dma_device;
134 struct device_dma_parameters dma_parms;
135 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
136};
137
138static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
139{
140 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
141 int chan_id = mxs_chan->chan.chan_id;
142
Shawn Guobb11fb62012-05-07 14:14:08 +0800143 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800144 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800145 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800146 else
147 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800148 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800149}
150
151static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
152{
153 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
154 int chan_id = mxs_chan->chan.chan_id;
155
156 /* set cmd_addr up */
157 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800158 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800159
Shawn Guoa580b8c2011-02-27 00:47:42 +0800160 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800161 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800162}
163
164static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
165{
Shawn Guoa580b8c2011-02-27 00:47:42 +0800166 mxs_chan->status = DMA_SUCCESS;
167}
168
169static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
170{
171 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
172 int chan_id = mxs_chan->chan.chan_id;
173
174 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800175 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800176 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800177 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800178 else
179 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800180 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800181
182 mxs_chan->status = DMA_PAUSED;
183}
184
185static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
186{
187 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
188 int chan_id = mxs_chan->chan.chan_id;
189
190 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800191 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800192 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800193 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800194 else
195 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800196 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800197
198 mxs_chan->status = DMA_IN_PROGRESS;
199}
200
Shawn Guoa580b8c2011-02-27 00:47:42 +0800201static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
202{
203 return container_of(chan, struct mxs_dma_chan, chan);
204}
205
206static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
207{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000208 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800209}
210
211static void mxs_dma_tasklet(unsigned long data)
212{
213 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
214
215 if (mxs_chan->desc.callback)
216 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
217}
218
219static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
220{
221 struct mxs_dma_engine *mxs_dma = dev_id;
222 u32 stat1, stat2;
223
224 /* completion status */
225 stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
226 stat1 &= MXS_DMA_CHANNELS_MASK;
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800227 writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800228
229 /* error status */
230 stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800231 writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800232
233 /*
234 * When both completion and error of termination bits set at the
235 * same time, we do not take it as an error. IOW, it only becomes
Lothar Waßmann40031222011-12-08 09:15:41 +0100236 * an error we need to handle here in case of either it's (1) a bus
Shawn Guoa580b8c2011-02-27 00:47:42 +0800237 * error or (2) a termination error with no completion.
238 */
239 stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
240 (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
241
242 /* combine error and completion status for checking */
243 stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
244 while (stat1) {
245 int channel = fls(stat1) - 1;
246 struct mxs_dma_chan *mxs_chan =
247 &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
248
249 if (channel >= MXS_DMA_CHANNELS) {
250 dev_dbg(mxs_dma->dma_device.dev,
251 "%s: error in channel %d\n", __func__,
252 channel - MXS_DMA_CHANNELS);
253 mxs_chan->status = DMA_ERROR;
254 mxs_dma_reset_chan(mxs_chan);
255 } else {
256 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
257 mxs_chan->status = DMA_IN_PROGRESS;
258 else
259 mxs_chan->status = DMA_SUCCESS;
260 }
261
262 stat1 &= ~(1 << channel);
263
264 if (mxs_chan->status == DMA_SUCCESS)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000265 dma_cookie_complete(&mxs_chan->desc);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800266
267 /* schedule tasklet on this channel */
268 tasklet_schedule(&mxs_chan->tasklet);
269 }
270
271 return IRQ_HANDLED;
272}
273
274static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
275{
276 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
277 struct mxs_dma_data *data = chan->private;
278 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
279 int ret;
280
281 if (!data)
282 return -EINVAL;
283
284 mxs_chan->chan_irq = data->chan_irq;
285
286 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
287 &mxs_chan->ccw_phys, GFP_KERNEL);
288 if (!mxs_chan->ccw) {
289 ret = -ENOMEM;
290 goto err_alloc;
291 }
292
293 memset(mxs_chan->ccw, 0, PAGE_SIZE);
294
Shawn Guo95bfea12011-06-30 16:06:33 +0800295 if (mxs_chan->chan_irq != NO_IRQ) {
296 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
297 0, "mxs-dma", mxs_dma);
298 if (ret)
299 goto err_irq;
300 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800301
Shawn Guo759a2e32011-12-20 13:54:00 +0800302 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800303 if (ret)
304 goto err_clk;
305
306 mxs_dma_reset_chan(mxs_chan);
307
308 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
309 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
310
311 /* the descriptor is ready */
312 async_tx_ack(&mxs_chan->desc);
313
314 return 0;
315
316err_clk:
317 free_irq(mxs_chan->chan_irq, mxs_dma);
318err_irq:
319 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
320 mxs_chan->ccw, mxs_chan->ccw_phys);
321err_alloc:
322 return ret;
323}
324
325static void mxs_dma_free_chan_resources(struct dma_chan *chan)
326{
327 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
328 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
329
330 mxs_dma_disable_chan(mxs_chan);
331
332 free_irq(mxs_chan->chan_irq, mxs_dma);
333
334 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
335 mxs_chan->ccw, mxs_chan->ccw_phys);
336
Shawn Guo759a2e32011-12-20 13:54:00 +0800337 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800338}
339
Huang Shijie921de862012-02-16 14:17:33 +0800340/*
341 * How to use the flags for ->device_prep_slave_sg() :
342 * [1] If there is only one DMA command in the DMA chain, the code should be:
343 * ......
344 * ->device_prep_slave_sg(DMA_CTRL_ACK);
345 * ......
346 * [2] If there are two DMA commands in the DMA chain, the code should be
347 * ......
348 * ->device_prep_slave_sg(0);
349 * ......
350 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
351 * ......
352 * [3] If there are more than two DMA commands in the DMA chain, the code
353 * should be:
354 * ......
355 * ->device_prep_slave_sg(0); // First
356 * ......
357 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
358 * ......
359 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
360 * ......
361 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800362static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
363 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530364 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700365 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800366{
367 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
368 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
369 struct mxs_dma_ccw *ccw;
370 struct scatterlist *sg;
371 int i, j;
372 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800373 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100374 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800375
376 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
377 return NULL;
378
379 if (sg_len + (append ? idx : 0) > NUM_CCW) {
380 dev_err(mxs_dma->dma_device.dev,
381 "maximum number of sg exceeded: %d > %d\n",
382 sg_len, NUM_CCW);
383 goto err_out;
384 }
385
386 mxs_chan->status = DMA_IN_PROGRESS;
387 mxs_chan->flags = 0;
388
389 /*
390 * If the sg is prepared with append flag set, the sg
391 * will be appended to the last prepared sg.
392 */
393 if (append) {
394 BUG_ON(idx < 1);
395 ccw = &mxs_chan->ccw[idx - 1];
396 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
397 ccw->bits |= CCW_CHAIN;
398 ccw->bits &= ~CCW_IRQ;
399 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800400 } else {
401 idx = 0;
402 }
403
Shawn Guo62268ce2011-12-13 23:48:03 +0800404 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800405 ccw = &mxs_chan->ccw[idx++];
406 pio = (u32 *) sgl;
407
408 for (j = 0; j < sg_len;)
409 ccw->pio_words[j++] = *pio++;
410
411 ccw->bits = 0;
412 ccw->bits |= CCW_IRQ;
413 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800414 if (flags & DMA_CTRL_ACK)
415 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800416 ccw->bits |= CCW_HALT_ON_TERM;
417 ccw->bits |= CCW_TERM_FLUSH;
418 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
419 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
420 } else {
421 for_each_sg(sgl, sg, sg_len, i) {
422 if (sg->length > MAX_XFER_BYTES) {
423 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
424 sg->length, MAX_XFER_BYTES);
425 goto err_out;
426 }
427
428 ccw = &mxs_chan->ccw[idx++];
429
430 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
431 ccw->bufaddr = sg->dma_address;
432 ccw->xfer_bytes = sg->length;
433
434 ccw->bits = 0;
435 ccw->bits |= CCW_CHAIN;
436 ccw->bits |= CCW_HALT_ON_TERM;
437 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530438 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800439 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
440 COMMAND);
441
442 if (i + 1 == sg_len) {
443 ccw->bits &= ~CCW_CHAIN;
444 ccw->bits |= CCW_IRQ;
445 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800446 if (flags & DMA_CTRL_ACK)
447 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800448 }
449 }
450 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100451 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800452
453 return &mxs_chan->desc;
454
455err_out:
456 mxs_chan->status = DMA_ERROR;
457 return NULL;
458}
459
460static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
461 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500462 size_t period_len, enum dma_transfer_direction direction,
463 void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800464{
465 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
466 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
467 int num_periods = buf_len / period_len;
468 int i = 0, buf = 0;
469
470 if (mxs_chan->status == DMA_IN_PROGRESS)
471 return NULL;
472
473 mxs_chan->status = DMA_IN_PROGRESS;
474 mxs_chan->flags |= MXS_DMA_SG_LOOP;
475
476 if (num_periods > NUM_CCW) {
477 dev_err(mxs_dma->dma_device.dev,
478 "maximum number of sg exceeded: %d > %d\n",
479 num_periods, NUM_CCW);
480 goto err_out;
481 }
482
483 if (period_len > MAX_XFER_BYTES) {
484 dev_err(mxs_dma->dma_device.dev,
485 "maximum period size exceeded: %d > %d\n",
486 period_len, MAX_XFER_BYTES);
487 goto err_out;
488 }
489
490 while (buf < buf_len) {
491 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
492
493 if (i + 1 == num_periods)
494 ccw->next = mxs_chan->ccw_phys;
495 else
496 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
497
498 ccw->bufaddr = dma_addr;
499 ccw->xfer_bytes = period_len;
500
501 ccw->bits = 0;
502 ccw->bits |= CCW_CHAIN;
503 ccw->bits |= CCW_IRQ;
504 ccw->bits |= CCW_HALT_ON_TERM;
505 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530506 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800507 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
508
509 dma_addr += period_len;
510 buf += period_len;
511
512 i++;
513 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100514 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800515
516 return &mxs_chan->desc;
517
518err_out:
519 mxs_chan->status = DMA_ERROR;
520 return NULL;
521}
522
523static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
524 unsigned long arg)
525{
526 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
527 int ret = 0;
528
529 switch (cmd) {
530 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800531 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100532 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800533 break;
534 case DMA_PAUSE:
535 mxs_dma_pause_chan(mxs_chan);
536 break;
537 case DMA_RESUME:
538 mxs_dma_resume_chan(mxs_chan);
539 break;
540 default:
541 ret = -ENOSYS;
542 }
543
544 return ret;
545}
546
547static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
548 dma_cookie_t cookie, struct dma_tx_state *txstate)
549{
550 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
551 dma_cookie_t last_used;
552
553 last_used = chan->cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000554 dma_set_tx_state(txstate, chan->completed_cookie, last_used, 0);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800555
556 return mxs_chan->status;
557}
558
559static void mxs_dma_issue_pending(struct dma_chan *chan)
560{
Shawn Guod04525e2012-04-11 13:29:31 +0800561 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
562
563 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800564}
565
566static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
567{
568 int ret;
569
Shawn Guo759a2e32011-12-20 13:54:00 +0800570 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800571 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100572 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800573
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800574 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800575 if (ret)
576 goto err_out;
577
578 /* only major version matters */
579 mxs_dma->version = readl(mxs_dma->base +
580 ((mxs_dma->dev_id == MXS_DMA_APBX) ?
581 HW_APBX_VERSION : HW_APBH_VERSION)) >>
582 BP_APBHX_VERSION_MAJOR;
583
584 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800585 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800586 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800587 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800588 writel(BM_APBH_CTRL0_APB_BURST8_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800589 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800590 }
591
592 /* enable irq for all the channels */
593 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800594 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800595
Shawn Guoa580b8c2011-02-27 00:47:42 +0800596err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800597 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800598 return ret;
599}
600
601static int __init mxs_dma_probe(struct platform_device *pdev)
602{
603 const struct platform_device_id *id_entry =
604 platform_get_device_id(pdev);
605 struct mxs_dma_engine *mxs_dma;
606 struct resource *iores;
607 int ret, i;
608
609 mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
610 if (!mxs_dma)
611 return -ENOMEM;
612
613 mxs_dma->dev_id = id_entry->driver_data;
614
615 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
616
617 if (!request_mem_region(iores->start, resource_size(iores),
618 pdev->name)) {
619 ret = -EBUSY;
620 goto err_request_region;
621 }
622
623 mxs_dma->base = ioremap(iores->start, resource_size(iores));
624 if (!mxs_dma->base) {
625 ret = -ENOMEM;
626 goto err_ioremap;
627 }
628
629 mxs_dma->clk = clk_get(&pdev->dev, NULL);
630 if (IS_ERR(mxs_dma->clk)) {
631 ret = PTR_ERR(mxs_dma->clk);
632 goto err_clk;
633 }
634
635 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
636 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
637
638 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
639
640 /* Initialize channel parameters */
641 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
642 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
643
644 mxs_chan->mxs_dma = mxs_dma;
645 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000646 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800647
648 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
649 (unsigned long) mxs_chan);
650
651
652 /* Add the channel to mxs_chan list */
653 list_add_tail(&mxs_chan->chan.device_node,
654 &mxs_dma->dma_device.channels);
655 }
656
657 ret = mxs_dma_init(mxs_dma);
658 if (ret)
659 goto err_init;
660
661 mxs_dma->dma_device.dev = &pdev->dev;
662
663 /* mxs_dma gets 65535 bytes maximum sg size */
664 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
665 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
666
667 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
668 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
669 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
670 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
671 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
672 mxs_dma->dma_device.device_control = mxs_dma_control;
673 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
674
675 ret = dma_async_device_register(&mxs_dma->dma_device);
676 if (ret) {
677 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
678 goto err_init;
679 }
680
681 dev_info(mxs_dma->dma_device.dev, "initialized\n");
682
683 return 0;
684
685err_init:
686 clk_put(mxs_dma->clk);
687err_clk:
688 iounmap(mxs_dma->base);
689err_ioremap:
690 release_mem_region(iores->start, resource_size(iores));
691err_request_region:
692 kfree(mxs_dma);
693 return ret;
694}
695
696static struct platform_device_id mxs_dma_type[] = {
697 {
698 .name = "mxs-dma-apbh",
699 .driver_data = MXS_DMA_APBH,
700 }, {
701 .name = "mxs-dma-apbx",
702 .driver_data = MXS_DMA_APBX,
Axel Lin2a9778e2011-07-12 18:53:52 +0800703 }, {
704 /* end of list */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800705 }
706};
707
708static struct platform_driver mxs_dma_driver = {
709 .driver = {
710 .name = "mxs-dma",
711 },
712 .id_table = mxs_dma_type,
713};
714
715static int __init mxs_dma_module_init(void)
716{
717 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
718}
719subsys_initcall(mxs_dma_module_init);