blob: f48f87feeca4021fa97a95a7d9fa02127bce3599 [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>
Dong Aisheng90c9abc2012-05-04 20:12:17 +080025#include <linux/module.h>
Dong Aishengf5b7efc2012-05-04 20:12:15 +080026#include <linux/stmp_device.h>
Dong Aisheng90c9abc2012-05-04 20:12:17 +080027#include <linux/of.h>
28#include <linux/of_device.h>
Shawn Guod84f6382013-02-26 09:42:09 +080029#include <linux/of_dma.h>
Markus Pargmannb2d63982013-10-29 08:47:45 +010030#include <linux/list.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080031
32#include <asm/irq.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080033
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000034#include "dmaengine.h"
35
Shawn Guoa580b8c2011-02-27 00:47:42 +080036/*
37 * NOTE: The term "PIO" throughout the mxs-dma implementation means
38 * PIO mode of mxs apbh-dma and apbx-dma. With this working mode,
39 * dma can program the controller registers of peripheral devices.
40 */
41
Shawn Guo8c920132012-05-10 06:23:26 +080042#define dma_is_apbh(mxs_dma) ((mxs_dma)->type == MXS_DMA_APBH)
43#define apbh_is_old(mxs_dma) ((mxs_dma)->dev_id == IMX23_DMA)
Shawn Guoa580b8c2011-02-27 00:47:42 +080044
45#define HW_APBHX_CTRL0 0x000
46#define BM_APBH_CTRL0_APB_BURST8_EN (1 << 29)
47#define BM_APBH_CTRL0_APB_BURST_EN (1 << 28)
Shawn Guoa580b8c2011-02-27 00:47:42 +080048#define BP_APBH_CTRL0_RESET_CHANNEL 16
49#define HW_APBHX_CTRL1 0x010
50#define HW_APBHX_CTRL2 0x020
51#define HW_APBHX_CHANNEL_CTRL 0x030
52#define BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL 16
Shawn Guobb11fb62012-05-07 14:14:08 +080053/*
54 * The offset of NXTCMDAR register is different per both dma type and version,
55 * while stride for each channel is all the same 0x70.
56 */
57#define HW_APBHX_CHn_NXTCMDAR(d, n) \
58 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x050 : 0x110) + (n) * 0x70)
59#define HW_APBHX_CHn_SEMA(d, n) \
60 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x080 : 0x140) + (n) * 0x70)
Markus Pargmann7b113042013-10-29 08:47:46 +010061#define HW_APBHX_CHn_BAR(d, n) \
62 (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x070 : 0x130) + (n) * 0x70)
Markus Pargmann702e94d2013-10-29 08:47:47 +010063#define HW_APBX_CHn_DEBUG1(d, n) (0x150 + (n) * 0x70)
Shawn Guoa580b8c2011-02-27 00:47:42 +080064
65/*
66 * ccw bits definitions
67 *
68 * COMMAND: 0..1 (2)
69 * CHAIN: 2 (1)
70 * IRQ: 3 (1)
71 * NAND_LOCK: 4 (1) - not implemented
72 * NAND_WAIT4READY: 5 (1) - not implemented
73 * DEC_SEM: 6 (1)
74 * WAIT4END: 7 (1)
75 * HALT_ON_TERMINATE: 8 (1)
76 * TERMINATE_FLUSH: 9 (1)
77 * RESERVED: 10..11 (2)
78 * PIO_NUM: 12..15 (4)
79 */
80#define BP_CCW_COMMAND 0
81#define BM_CCW_COMMAND (3 << 0)
82#define CCW_CHAIN (1 << 2)
83#define CCW_IRQ (1 << 3)
84#define CCW_DEC_SEM (1 << 6)
85#define CCW_WAIT4END (1 << 7)
86#define CCW_HALT_ON_TERM (1 << 8)
87#define CCW_TERM_FLUSH (1 << 9)
88#define BP_CCW_PIO_NUM 12
89#define BM_CCW_PIO_NUM (0xf << 12)
90
91#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
92
93#define MXS_DMA_CMD_NO_XFER 0
94#define MXS_DMA_CMD_WRITE 1
95#define MXS_DMA_CMD_READ 2
96#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
97
98struct mxs_dma_ccw {
99 u32 next;
100 u16 bits;
101 u16 xfer_bytes;
102#define MAX_XFER_BYTES 0xff00
103 u32 bufaddr;
104#define MXS_PIO_WORDS 16
105 u32 pio_words[MXS_PIO_WORDS];
106};
107
Marek Vasut5e97fa92012-09-04 06:04:25 +0200108#define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
109#define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800110
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;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200116 unsigned int chan_irq;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800117 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
Shawn Guo8c920132012-05-10 06:23:26 +0800128enum mxs_dma_devtype {
129 MXS_DMA_APBH,
130 MXS_DMA_APBX,
131};
132
133enum mxs_dma_id {
134 IMX23_DMA,
135 IMX28_DMA,
136};
137
Shawn Guoa580b8c2011-02-27 00:47:42 +0800138struct mxs_dma_engine {
Shawn Guo8c920132012-05-10 06:23:26 +0800139 enum mxs_dma_id dev_id;
140 enum mxs_dma_devtype type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800141 void __iomem *base;
142 struct clk *clk;
143 struct dma_device dma_device;
144 struct device_dma_parameters dma_parms;
145 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
Shawn Guod84f6382013-02-26 09:42:09 +0800146 struct platform_device *pdev;
147 unsigned int nr_channels;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800148};
149
Shawn Guo8c920132012-05-10 06:23:26 +0800150struct mxs_dma_type {
151 enum mxs_dma_id id;
152 enum mxs_dma_devtype type;
153};
154
155static struct mxs_dma_type mxs_dma_types[] = {
156 {
157 .id = IMX23_DMA,
158 .type = MXS_DMA_APBH,
159 }, {
160 .id = IMX23_DMA,
161 .type = MXS_DMA_APBX,
162 }, {
163 .id = IMX28_DMA,
164 .type = MXS_DMA_APBH,
165 }, {
166 .id = IMX28_DMA,
167 .type = MXS_DMA_APBX,
168 }
169};
170
171static struct platform_device_id mxs_dma_ids[] = {
172 {
173 .name = "imx23-dma-apbh",
174 .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
175 }, {
176 .name = "imx23-dma-apbx",
177 .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
178 }, {
179 .name = "imx28-dma-apbh",
180 .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
181 }, {
182 .name = "imx28-dma-apbx",
183 .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
184 }, {
185 /* end of list */
186 }
187};
188
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800189static const struct of_device_id mxs_dma_dt_ids[] = {
190 { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
191 { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
192 { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
193 { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
194 { /* sentinel */ }
195};
196MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
197
Shawn Guo8c920132012-05-10 06:23:26 +0800198static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
199{
200 return container_of(chan, struct mxs_dma_chan, chan);
201}
202
Shawn Guoa580b8c2011-02-27 00:47:42 +0800203static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
204{
205 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
206 int chan_id = mxs_chan->chan.chan_id;
207
Markus Pargmann702e94d2013-10-29 08:47:47 +0100208 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800209 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800210 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Markus Pargmann702e94d2013-10-29 08:47:47 +0100211 } else {
212 unsigned long elapsed = 0;
213 const unsigned long max_wait = 50000; /* 50ms */
214 void __iomem *reg_dbg1 = mxs_dma->base +
215 HW_APBX_CHn_DEBUG1(mxs_dma, chan_id);
216
217 /*
218 * On i.MX28 APBX, the DMA channel can stop working if we reset
219 * the channel while it is in READ_FLUSH (0x08) state.
220 * We wait here until we leave the state. Then we trigger the
221 * reset. Waiting a maximum of 50ms, the kernel shouldn't crash
222 * because of this.
223 */
224 while ((readl(reg_dbg1) & 0xf) == 0x8 && elapsed < max_wait) {
225 udelay(100);
226 elapsed += 100;
227 }
228
229 if (elapsed >= max_wait)
230 dev_err(&mxs_chan->mxs_dma->pdev->dev,
231 "Failed waiting for the DMA channel %d to leave state READ_FLUSH, trying to reset channel in READ_FLUSH state now\n",
232 chan_id);
233
234
Shawn Guoa580b8c2011-02-27 00:47:42 +0800235 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800236 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Markus Pargmann702e94d2013-10-29 08:47:47 +0100237 }
Markus Pargmannbb3660f2013-10-29 08:47:48 +0100238
239 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800240}
241
242static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
243{
244 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
245 int chan_id = mxs_chan->chan.chan_id;
246
247 /* set cmd_addr up */
248 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800249 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800250
Shawn Guoa580b8c2011-02-27 00:47:42 +0800251 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800252 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800253}
254
255static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
256{
Vinod Koul27375832013-10-16 20:51:30 +0530257 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800258}
259
260static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
261{
262 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
263 int chan_id = mxs_chan->chan.chan_id;
264
265 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800266 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800267 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800268 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800269 else
270 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800271 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800272
273 mxs_chan->status = DMA_PAUSED;
274}
275
276static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
277{
278 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
279 int chan_id = mxs_chan->chan.chan_id;
280
281 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800282 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800283 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800284 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800285 else
286 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800287 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800288
289 mxs_chan->status = DMA_IN_PROGRESS;
290}
291
Shawn Guoa580b8c2011-02-27 00:47:42 +0800292static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
293{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000294 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800295}
296
297static void mxs_dma_tasklet(unsigned long data)
298{
299 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
300
301 if (mxs_chan->desc.callback)
302 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
303}
304
Markus Pargmannb2d63982013-10-29 08:47:45 +0100305static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
306{
307 int i;
308
309 for (i = 0; i != mxs_dma->nr_channels; ++i)
310 if (mxs_dma->mxs_chans[i].chan_irq == irq)
311 return i;
312
313 return -EINVAL;
314}
315
Shawn Guoa580b8c2011-02-27 00:47:42 +0800316static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
317{
318 struct mxs_dma_engine *mxs_dma = dev_id;
Markus Pargmannb2d63982013-10-29 08:47:45 +0100319 struct mxs_dma_chan *mxs_chan;
320 u32 completed;
321 u32 err;
322 int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
323
324 if (chan < 0)
325 return IRQ_NONE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800326
327 /* completion status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100328 completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
329 completed = (completed >> chan) & 0x1;
330
331 /* Clear interrupt */
332 writel((1 << chan),
333 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800334
335 /* error status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100336 err = readl(mxs_dma->base + HW_APBHX_CTRL2);
337 err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
338
339 /*
340 * error status bit is in the upper 16 bits, error irq bit in the lower
341 * 16 bits. We transform it into a simpler error code:
342 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
343 */
344 err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
345
346 /* Clear error irq */
347 writel((1 << chan),
348 mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800349
350 /*
351 * When both completion and error of termination bits set at the
352 * same time, we do not take it as an error. IOW, it only becomes
Markus Pargmannb2d63982013-10-29 08:47:45 +0100353 * an error we need to handle here in case of either it's a bus
354 * error or a termination error with no completion. 0x01 is termination
355 * error, so we can subtract err & completed to get the real error case.
Shawn Guoa580b8c2011-02-27 00:47:42 +0800356 */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100357 err -= err & completed;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800358
Markus Pargmannb2d63982013-10-29 08:47:45 +0100359 mxs_chan = &mxs_dma->mxs_chans[chan];
Shawn Guoa580b8c2011-02-27 00:47:42 +0800360
Markus Pargmannb2d63982013-10-29 08:47:45 +0100361 if (err) {
362 dev_dbg(mxs_dma->dma_device.dev,
363 "%s: error in channel %d\n", __func__,
364 chan);
365 mxs_chan->status = DMA_ERROR;
366 mxs_dma_reset_chan(mxs_chan);
Markus Pargmannbb3660f2013-10-29 08:47:48 +0100367 } else if (mxs_chan->status != DMA_COMPLETE) {
Markus Pargmannb2d63982013-10-29 08:47:45 +0100368 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
369 mxs_chan->status = DMA_IN_PROGRESS;
370 else
371 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800372 }
373
Markus Pargmannb2d63982013-10-29 08:47:45 +0100374 if (mxs_chan->status == DMA_COMPLETE)
375 dma_cookie_complete(&mxs_chan->desc);
376
377 /* schedule tasklet on this channel */
378 tasklet_schedule(&mxs_chan->tasklet);
379
Shawn Guoa580b8c2011-02-27 00:47:42 +0800380 return IRQ_HANDLED;
381}
382
383static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
384{
385 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800386 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
387 int ret;
388
Marek Vasut5e97fa92012-09-04 06:04:25 +0200389 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
390 CCW_BLOCK_SIZE, &mxs_chan->ccw_phys,
391 GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800392 if (!mxs_chan->ccw) {
393 ret = -ENOMEM;
394 goto err_alloc;
395 }
396
Marek Vasut5e97fa92012-09-04 06:04:25 +0200397 memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800398
Shawn Guo95bfea12011-06-30 16:06:33 +0800399 if (mxs_chan->chan_irq != NO_IRQ) {
400 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
401 0, "mxs-dma", mxs_dma);
402 if (ret)
403 goto err_irq;
404 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800405
Shawn Guo759a2e32011-12-20 13:54:00 +0800406 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800407 if (ret)
408 goto err_clk;
409
410 mxs_dma_reset_chan(mxs_chan);
411
412 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
413 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
414
415 /* the descriptor is ready */
416 async_tx_ack(&mxs_chan->desc);
417
418 return 0;
419
420err_clk:
421 free_irq(mxs_chan->chan_irq, mxs_dma);
422err_irq:
Marek Vasut5e97fa92012-09-04 06:04:25 +0200423 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800424 mxs_chan->ccw, mxs_chan->ccw_phys);
425err_alloc:
426 return ret;
427}
428
429static void mxs_dma_free_chan_resources(struct dma_chan *chan)
430{
431 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
432 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
433
434 mxs_dma_disable_chan(mxs_chan);
435
436 free_irq(mxs_chan->chan_irq, mxs_dma);
437
Marek Vasut5e97fa92012-09-04 06:04:25 +0200438 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800439 mxs_chan->ccw, mxs_chan->ccw_phys);
440
Shawn Guo759a2e32011-12-20 13:54:00 +0800441 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800442}
443
Huang Shijie921de862012-02-16 14:17:33 +0800444/*
445 * How to use the flags for ->device_prep_slave_sg() :
446 * [1] If there is only one DMA command in the DMA chain, the code should be:
447 * ......
448 * ->device_prep_slave_sg(DMA_CTRL_ACK);
449 * ......
450 * [2] If there are two DMA commands in the DMA chain, the code should be
451 * ......
452 * ->device_prep_slave_sg(0);
453 * ......
454 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
455 * ......
456 * [3] If there are more than two DMA commands in the DMA chain, the code
457 * should be:
458 * ......
459 * ->device_prep_slave_sg(0); // First
460 * ......
461 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
462 * ......
463 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
464 * ......
465 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800466static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
467 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530468 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700469 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800470{
471 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
472 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
473 struct mxs_dma_ccw *ccw;
474 struct scatterlist *sg;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200475 u32 i, j;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800476 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800477 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100478 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800479
480 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
481 return NULL;
482
483 if (sg_len + (append ? idx : 0) > NUM_CCW) {
484 dev_err(mxs_dma->dma_device.dev,
485 "maximum number of sg exceeded: %d > %d\n",
486 sg_len, NUM_CCW);
487 goto err_out;
488 }
489
490 mxs_chan->status = DMA_IN_PROGRESS;
491 mxs_chan->flags = 0;
492
493 /*
494 * If the sg is prepared with append flag set, the sg
495 * will be appended to the last prepared sg.
496 */
497 if (append) {
498 BUG_ON(idx < 1);
499 ccw = &mxs_chan->ccw[idx - 1];
500 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
501 ccw->bits |= CCW_CHAIN;
502 ccw->bits &= ~CCW_IRQ;
503 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800504 } else {
505 idx = 0;
506 }
507
Shawn Guo62268ce2011-12-13 23:48:03 +0800508 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800509 ccw = &mxs_chan->ccw[idx++];
510 pio = (u32 *) sgl;
511
512 for (j = 0; j < sg_len;)
513 ccw->pio_words[j++] = *pio++;
514
515 ccw->bits = 0;
516 ccw->bits |= CCW_IRQ;
517 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800518 if (flags & DMA_CTRL_ACK)
519 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800520 ccw->bits |= CCW_HALT_ON_TERM;
521 ccw->bits |= CCW_TERM_FLUSH;
522 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
523 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
524 } else {
525 for_each_sg(sgl, sg, sg_len, i) {
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200526 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800527 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200528 sg_dma_len(sg), MAX_XFER_BYTES);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800529 goto err_out;
530 }
531
532 ccw = &mxs_chan->ccw[idx++];
533
534 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
535 ccw->bufaddr = sg->dma_address;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200536 ccw->xfer_bytes = sg_dma_len(sg);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800537
538 ccw->bits = 0;
539 ccw->bits |= CCW_CHAIN;
540 ccw->bits |= CCW_HALT_ON_TERM;
541 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530542 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800543 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
544 COMMAND);
545
546 if (i + 1 == sg_len) {
547 ccw->bits &= ~CCW_CHAIN;
548 ccw->bits |= CCW_IRQ;
549 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800550 if (flags & DMA_CTRL_ACK)
551 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800552 }
553 }
554 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100555 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800556
557 return &mxs_chan->desc;
558
559err_out:
560 mxs_chan->status = DMA_ERROR;
561 return NULL;
562}
563
564static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
565 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500566 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300567 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800568{
569 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
570 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200571 u32 num_periods = buf_len / period_len;
572 u32 i = 0, buf = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800573
574 if (mxs_chan->status == DMA_IN_PROGRESS)
575 return NULL;
576
577 mxs_chan->status = DMA_IN_PROGRESS;
578 mxs_chan->flags |= MXS_DMA_SG_LOOP;
579
580 if (num_periods > NUM_CCW) {
581 dev_err(mxs_dma->dma_device.dev,
582 "maximum number of sg exceeded: %d > %d\n",
583 num_periods, NUM_CCW);
584 goto err_out;
585 }
586
587 if (period_len > MAX_XFER_BYTES) {
588 dev_err(mxs_dma->dma_device.dev,
589 "maximum period size exceeded: %d > %d\n",
590 period_len, MAX_XFER_BYTES);
591 goto err_out;
592 }
593
594 while (buf < buf_len) {
595 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
596
597 if (i + 1 == num_periods)
598 ccw->next = mxs_chan->ccw_phys;
599 else
600 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
601
602 ccw->bufaddr = dma_addr;
603 ccw->xfer_bytes = period_len;
604
605 ccw->bits = 0;
606 ccw->bits |= CCW_CHAIN;
607 ccw->bits |= CCW_IRQ;
608 ccw->bits |= CCW_HALT_ON_TERM;
609 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530610 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800611 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
612
613 dma_addr += period_len;
614 buf += period_len;
615
616 i++;
617 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100618 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800619
620 return &mxs_chan->desc;
621
622err_out:
623 mxs_chan->status = DMA_ERROR;
624 return NULL;
625}
626
627static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
628 unsigned long arg)
629{
630 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
631 int ret = 0;
632
633 switch (cmd) {
634 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800635 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100636 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800637 break;
638 case DMA_PAUSE:
639 mxs_dma_pause_chan(mxs_chan);
640 break;
641 case DMA_RESUME:
642 mxs_dma_resume_chan(mxs_chan);
643 break;
644 default:
645 ret = -ENOSYS;
646 }
647
648 return ret;
649}
650
651static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
652 dma_cookie_t cookie, struct dma_tx_state *txstate)
653{
654 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Markus Pargmann7b113042013-10-29 08:47:46 +0100655 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
656 u32 residue = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800657
Markus Pargmann7b113042013-10-29 08:47:46 +0100658 if (mxs_chan->status == DMA_IN_PROGRESS &&
659 mxs_chan->flags & MXS_DMA_SG_LOOP) {
660 struct mxs_dma_ccw *last_ccw;
661 u32 bar;
662
663 last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
664 residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
665
666 bar = readl(mxs_dma->base +
667 HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
668 residue -= bar;
669 }
670
671 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
672 residue);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800673
674 return mxs_chan->status;
675}
676
677static void mxs_dma_issue_pending(struct dma_chan *chan)
678{
Shawn Guod04525e2012-04-11 13:29:31 +0800679 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
680
681 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800682}
683
684static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
685{
686 int ret;
687
Shawn Guo759a2e32011-12-20 13:54:00 +0800688 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800689 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100690 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800691
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800692 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800693 if (ret)
694 goto err_out;
695
Shawn Guoa580b8c2011-02-27 00:47:42 +0800696 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800697 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800698 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800699 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800700 writel(BM_APBH_CTRL0_APB_BURST8_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800701 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800702 }
703
704 /* enable irq for all the channels */
705 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800706 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800707
Shawn Guoa580b8c2011-02-27 00:47:42 +0800708err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800709 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800710 return ret;
711}
712
Shawn Guod84f6382013-02-26 09:42:09 +0800713struct mxs_dma_filter_param {
714 struct device_node *of_node;
715 unsigned int chan_id;
716};
717
718static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
719{
720 struct mxs_dma_filter_param *param = fn_param;
721 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
722 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
723 int chan_irq;
724
725 if (mxs_dma->dma_device.dev->of_node != param->of_node)
726 return false;
727
728 if (chan->chan_id != param->chan_id)
729 return false;
730
731 chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
732 if (chan_irq < 0)
733 return false;
734
735 mxs_chan->chan_irq = chan_irq;
736
737 return true;
738}
739
Fabio Estevam3208b372013-05-24 16:37:27 -0300740static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
Shawn Guod84f6382013-02-26 09:42:09 +0800741 struct of_dma *ofdma)
742{
743 struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
744 dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
745 struct mxs_dma_filter_param param;
746
747 if (dma_spec->args_count != 1)
748 return NULL;
749
750 param.of_node = ofdma->of_node;
751 param.chan_id = dma_spec->args[0];
752
753 if (param.chan_id >= mxs_dma->nr_channels)
754 return NULL;
755
756 return dma_request_channel(mask, mxs_dma_filter_fn, &param);
757}
758
Shawn Guoa580b8c2011-02-27 00:47:42 +0800759static int __init mxs_dma_probe(struct platform_device *pdev)
760{
Shawn Guod84f6382013-02-26 09:42:09 +0800761 struct device_node *np = pdev->dev.of_node;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800762 const struct platform_device_id *id_entry;
763 const struct of_device_id *of_id;
764 const struct mxs_dma_type *dma_type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800765 struct mxs_dma_engine *mxs_dma;
766 struct resource *iores;
767 int ret, i;
768
Shawn Guoaaa20512013-02-25 14:57:26 +0800769 mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800770 if (!mxs_dma)
771 return -ENOMEM;
772
Shawn Guod84f6382013-02-26 09:42:09 +0800773 ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
774 if (ret) {
775 dev_err(&pdev->dev, "failed to read dma-channels\n");
776 return ret;
777 }
778
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800779 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
780 if (of_id)
781 id_entry = of_id->data;
782 else
783 id_entry = platform_get_device_id(pdev);
784
785 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
Shawn Guo8c920132012-05-10 06:23:26 +0800786 mxs_dma->type = dma_type->type;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800787 mxs_dma->dev_id = dma_type->id;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800788
789 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guoaaa20512013-02-25 14:57:26 +0800790 mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
791 if (IS_ERR(mxs_dma->base))
792 return PTR_ERR(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800793
Shawn Guoaaa20512013-02-25 14:57:26 +0800794 mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
795 if (IS_ERR(mxs_dma->clk))
796 return PTR_ERR(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800797
798 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
799 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
800
801 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
802
803 /* Initialize channel parameters */
804 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
805 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
806
807 mxs_chan->mxs_dma = mxs_dma;
808 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000809 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800810
811 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
812 (unsigned long) mxs_chan);
813
814
815 /* Add the channel to mxs_chan list */
816 list_add_tail(&mxs_chan->chan.device_node,
817 &mxs_dma->dma_device.channels);
818 }
819
820 ret = mxs_dma_init(mxs_dma);
821 if (ret)
Shawn Guoaaa20512013-02-25 14:57:26 +0800822 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800823
Shawn Guod84f6382013-02-26 09:42:09 +0800824 mxs_dma->pdev = pdev;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800825 mxs_dma->dma_device.dev = &pdev->dev;
826
827 /* mxs_dma gets 65535 bytes maximum sg size */
828 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
829 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
830
831 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
832 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
833 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
834 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
835 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
836 mxs_dma->dma_device.device_control = mxs_dma_control;
837 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
838
839 ret = dma_async_device_register(&mxs_dma->dma_device);
840 if (ret) {
841 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
Shawn Guoaaa20512013-02-25 14:57:26 +0800842 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800843 }
844
Shawn Guod84f6382013-02-26 09:42:09 +0800845 ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
846 if (ret) {
847 dev_err(mxs_dma->dma_device.dev,
848 "failed to register controller\n");
849 dma_async_device_unregister(&mxs_dma->dma_device);
850 }
851
Shawn Guoa580b8c2011-02-27 00:47:42 +0800852 dev_info(mxs_dma->dma_device.dev, "initialized\n");
853
854 return 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800855}
856
Shawn Guoa580b8c2011-02-27 00:47:42 +0800857static struct platform_driver mxs_dma_driver = {
858 .driver = {
859 .name = "mxs-dma",
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800860 .of_match_table = mxs_dma_dt_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800861 },
Shawn Guo8c920132012-05-10 06:23:26 +0800862 .id_table = mxs_dma_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800863};
864
865static int __init mxs_dma_module_init(void)
866{
867 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
868}
869subsys_initcall(mxs_dma_module_init);