blob: 530267068061005dbd762bb97bdca44bfae11f9b [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 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800238}
239
240static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
241{
242 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
243 int chan_id = mxs_chan->chan.chan_id;
244
245 /* set cmd_addr up */
246 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800247 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800248
Shawn Guoa580b8c2011-02-27 00:47:42 +0800249 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800250 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800251}
252
253static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
254{
Vinod Koul27375832013-10-16 20:51:30 +0530255 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800256}
257
258static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
259{
260 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
261 int chan_id = mxs_chan->chan.chan_id;
262
263 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800264 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800265 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800266 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800267 else
268 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800269 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800270
271 mxs_chan->status = DMA_PAUSED;
272}
273
274static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
275{
276 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
277 int chan_id = mxs_chan->chan.chan_id;
278
279 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800280 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800281 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800282 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800283 else
284 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800285 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800286
287 mxs_chan->status = DMA_IN_PROGRESS;
288}
289
Shawn Guoa580b8c2011-02-27 00:47:42 +0800290static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
291{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000292 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800293}
294
295static void mxs_dma_tasklet(unsigned long data)
296{
297 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
298
299 if (mxs_chan->desc.callback)
300 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
301}
302
Markus Pargmannb2d63982013-10-29 08:47:45 +0100303static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
304{
305 int i;
306
307 for (i = 0; i != mxs_dma->nr_channels; ++i)
308 if (mxs_dma->mxs_chans[i].chan_irq == irq)
309 return i;
310
311 return -EINVAL;
312}
313
Shawn Guoa580b8c2011-02-27 00:47:42 +0800314static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
315{
316 struct mxs_dma_engine *mxs_dma = dev_id;
Markus Pargmannb2d63982013-10-29 08:47:45 +0100317 struct mxs_dma_chan *mxs_chan;
318 u32 completed;
319 u32 err;
320 int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
321
322 if (chan < 0)
323 return IRQ_NONE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800324
325 /* completion status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100326 completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
327 completed = (completed >> chan) & 0x1;
328
329 /* Clear interrupt */
330 writel((1 << chan),
331 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800332
333 /* error status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100334 err = readl(mxs_dma->base + HW_APBHX_CTRL2);
335 err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
336
337 /*
338 * error status bit is in the upper 16 bits, error irq bit in the lower
339 * 16 bits. We transform it into a simpler error code:
340 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
341 */
342 err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
343
344 /* Clear error irq */
345 writel((1 << chan),
346 mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800347
348 /*
349 * When both completion and error of termination bits set at the
350 * same time, we do not take it as an error. IOW, it only becomes
Markus Pargmannb2d63982013-10-29 08:47:45 +0100351 * an error we need to handle here in case of either it's a bus
352 * error or a termination error with no completion. 0x01 is termination
353 * error, so we can subtract err & completed to get the real error case.
Shawn Guoa580b8c2011-02-27 00:47:42 +0800354 */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100355 err -= err & completed;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800356
Markus Pargmannb2d63982013-10-29 08:47:45 +0100357 mxs_chan = &mxs_dma->mxs_chans[chan];
Shawn Guoa580b8c2011-02-27 00:47:42 +0800358
Markus Pargmannb2d63982013-10-29 08:47:45 +0100359 if (err) {
360 dev_dbg(mxs_dma->dma_device.dev,
361 "%s: error in channel %d\n", __func__,
362 chan);
363 mxs_chan->status = DMA_ERROR;
364 mxs_dma_reset_chan(mxs_chan);
365 } else {
366 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
367 mxs_chan->status = DMA_IN_PROGRESS;
368 else
369 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800370 }
371
Markus Pargmannb2d63982013-10-29 08:47:45 +0100372 if (mxs_chan->status == DMA_COMPLETE)
373 dma_cookie_complete(&mxs_chan->desc);
374
375 /* schedule tasklet on this channel */
376 tasklet_schedule(&mxs_chan->tasklet);
377
Shawn Guoa580b8c2011-02-27 00:47:42 +0800378 return IRQ_HANDLED;
379}
380
381static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
382{
383 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800384 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
385 int ret;
386
Marek Vasut5e97fa92012-09-04 06:04:25 +0200387 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
388 CCW_BLOCK_SIZE, &mxs_chan->ccw_phys,
389 GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800390 if (!mxs_chan->ccw) {
391 ret = -ENOMEM;
392 goto err_alloc;
393 }
394
Marek Vasut5e97fa92012-09-04 06:04:25 +0200395 memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800396
Shawn Guo95bfea12011-06-30 16:06:33 +0800397 if (mxs_chan->chan_irq != NO_IRQ) {
398 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
399 0, "mxs-dma", mxs_dma);
400 if (ret)
401 goto err_irq;
402 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800403
Shawn Guo759a2e32011-12-20 13:54:00 +0800404 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800405 if (ret)
406 goto err_clk;
407
408 mxs_dma_reset_chan(mxs_chan);
409
410 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
411 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
412
413 /* the descriptor is ready */
414 async_tx_ack(&mxs_chan->desc);
415
416 return 0;
417
418err_clk:
419 free_irq(mxs_chan->chan_irq, mxs_dma);
420err_irq:
Marek Vasut5e97fa92012-09-04 06:04:25 +0200421 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800422 mxs_chan->ccw, mxs_chan->ccw_phys);
423err_alloc:
424 return ret;
425}
426
427static void mxs_dma_free_chan_resources(struct dma_chan *chan)
428{
429 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
430 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
431
432 mxs_dma_disable_chan(mxs_chan);
433
434 free_irq(mxs_chan->chan_irq, mxs_dma);
435
Marek Vasut5e97fa92012-09-04 06:04:25 +0200436 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800437 mxs_chan->ccw, mxs_chan->ccw_phys);
438
Shawn Guo759a2e32011-12-20 13:54:00 +0800439 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800440}
441
Huang Shijie921de862012-02-16 14:17:33 +0800442/*
443 * How to use the flags for ->device_prep_slave_sg() :
444 * [1] If there is only one DMA command in the DMA chain, the code should be:
445 * ......
446 * ->device_prep_slave_sg(DMA_CTRL_ACK);
447 * ......
448 * [2] If there are two DMA commands in the DMA chain, the code should be
449 * ......
450 * ->device_prep_slave_sg(0);
451 * ......
452 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
453 * ......
454 * [3] If there are more than two DMA commands in the DMA chain, the code
455 * should be:
456 * ......
457 * ->device_prep_slave_sg(0); // First
458 * ......
459 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
460 * ......
461 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
462 * ......
463 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800464static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
465 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530466 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700467 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800468{
469 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
470 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
471 struct mxs_dma_ccw *ccw;
472 struct scatterlist *sg;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200473 u32 i, j;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800474 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800475 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100476 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800477
478 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
479 return NULL;
480
481 if (sg_len + (append ? idx : 0) > NUM_CCW) {
482 dev_err(mxs_dma->dma_device.dev,
483 "maximum number of sg exceeded: %d > %d\n",
484 sg_len, NUM_CCW);
485 goto err_out;
486 }
487
488 mxs_chan->status = DMA_IN_PROGRESS;
489 mxs_chan->flags = 0;
490
491 /*
492 * If the sg is prepared with append flag set, the sg
493 * will be appended to the last prepared sg.
494 */
495 if (append) {
496 BUG_ON(idx < 1);
497 ccw = &mxs_chan->ccw[idx - 1];
498 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
499 ccw->bits |= CCW_CHAIN;
500 ccw->bits &= ~CCW_IRQ;
501 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800502 } else {
503 idx = 0;
504 }
505
Shawn Guo62268ce2011-12-13 23:48:03 +0800506 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800507 ccw = &mxs_chan->ccw[idx++];
508 pio = (u32 *) sgl;
509
510 for (j = 0; j < sg_len;)
511 ccw->pio_words[j++] = *pio++;
512
513 ccw->bits = 0;
514 ccw->bits |= CCW_IRQ;
515 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800516 if (flags & DMA_CTRL_ACK)
517 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800518 ccw->bits |= CCW_HALT_ON_TERM;
519 ccw->bits |= CCW_TERM_FLUSH;
520 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
521 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
522 } else {
523 for_each_sg(sgl, sg, sg_len, i) {
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200524 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800525 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200526 sg_dma_len(sg), MAX_XFER_BYTES);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800527 goto err_out;
528 }
529
530 ccw = &mxs_chan->ccw[idx++];
531
532 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
533 ccw->bufaddr = sg->dma_address;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200534 ccw->xfer_bytes = sg_dma_len(sg);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800535
536 ccw->bits = 0;
537 ccw->bits |= CCW_CHAIN;
538 ccw->bits |= CCW_HALT_ON_TERM;
539 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530540 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800541 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
542 COMMAND);
543
544 if (i + 1 == sg_len) {
545 ccw->bits &= ~CCW_CHAIN;
546 ccw->bits |= CCW_IRQ;
547 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800548 if (flags & DMA_CTRL_ACK)
549 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800550 }
551 }
552 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100553 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800554
555 return &mxs_chan->desc;
556
557err_out:
558 mxs_chan->status = DMA_ERROR;
559 return NULL;
560}
561
562static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
563 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500564 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300565 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800566{
567 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
568 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200569 u32 num_periods = buf_len / period_len;
570 u32 i = 0, buf = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800571
572 if (mxs_chan->status == DMA_IN_PROGRESS)
573 return NULL;
574
575 mxs_chan->status = DMA_IN_PROGRESS;
576 mxs_chan->flags |= MXS_DMA_SG_LOOP;
577
578 if (num_periods > NUM_CCW) {
579 dev_err(mxs_dma->dma_device.dev,
580 "maximum number of sg exceeded: %d > %d\n",
581 num_periods, NUM_CCW);
582 goto err_out;
583 }
584
585 if (period_len > MAX_XFER_BYTES) {
586 dev_err(mxs_dma->dma_device.dev,
587 "maximum period size exceeded: %d > %d\n",
588 period_len, MAX_XFER_BYTES);
589 goto err_out;
590 }
591
592 while (buf < buf_len) {
593 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
594
595 if (i + 1 == num_periods)
596 ccw->next = mxs_chan->ccw_phys;
597 else
598 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
599
600 ccw->bufaddr = dma_addr;
601 ccw->xfer_bytes = period_len;
602
603 ccw->bits = 0;
604 ccw->bits |= CCW_CHAIN;
605 ccw->bits |= CCW_IRQ;
606 ccw->bits |= CCW_HALT_ON_TERM;
607 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530608 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800609 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
610
611 dma_addr += period_len;
612 buf += period_len;
613
614 i++;
615 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100616 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800617
618 return &mxs_chan->desc;
619
620err_out:
621 mxs_chan->status = DMA_ERROR;
622 return NULL;
623}
624
625static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
626 unsigned long arg)
627{
628 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
629 int ret = 0;
630
631 switch (cmd) {
632 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800633 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100634 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800635 break;
636 case DMA_PAUSE:
637 mxs_dma_pause_chan(mxs_chan);
638 break;
639 case DMA_RESUME:
640 mxs_dma_resume_chan(mxs_chan);
641 break;
642 default:
643 ret = -ENOSYS;
644 }
645
646 return ret;
647}
648
649static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
650 dma_cookie_t cookie, struct dma_tx_state *txstate)
651{
652 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Markus Pargmann7b113042013-10-29 08:47:46 +0100653 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
654 u32 residue = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800655
Markus Pargmann7b113042013-10-29 08:47:46 +0100656 if (mxs_chan->status == DMA_IN_PROGRESS &&
657 mxs_chan->flags & MXS_DMA_SG_LOOP) {
658 struct mxs_dma_ccw *last_ccw;
659 u32 bar;
660
661 last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
662 residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
663
664 bar = readl(mxs_dma->base +
665 HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
666 residue -= bar;
667 }
668
669 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
670 residue);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800671
672 return mxs_chan->status;
673}
674
675static void mxs_dma_issue_pending(struct dma_chan *chan)
676{
Shawn Guod04525e2012-04-11 13:29:31 +0800677 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
678
679 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800680}
681
682static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
683{
684 int ret;
685
Shawn Guo759a2e32011-12-20 13:54:00 +0800686 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800687 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100688 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800689
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800690 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800691 if (ret)
692 goto err_out;
693
Shawn Guoa580b8c2011-02-27 00:47:42 +0800694 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800695 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800696 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800697 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800698 writel(BM_APBH_CTRL0_APB_BURST8_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 }
701
702 /* enable irq for all the channels */
703 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800704 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800705
Shawn Guoa580b8c2011-02-27 00:47:42 +0800706err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800707 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800708 return ret;
709}
710
Shawn Guod84f6382013-02-26 09:42:09 +0800711struct mxs_dma_filter_param {
712 struct device_node *of_node;
713 unsigned int chan_id;
714};
715
716static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
717{
718 struct mxs_dma_filter_param *param = fn_param;
719 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
720 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
721 int chan_irq;
722
723 if (mxs_dma->dma_device.dev->of_node != param->of_node)
724 return false;
725
726 if (chan->chan_id != param->chan_id)
727 return false;
728
729 chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
730 if (chan_irq < 0)
731 return false;
732
733 mxs_chan->chan_irq = chan_irq;
734
735 return true;
736}
737
Fabio Estevam3208b372013-05-24 16:37:27 -0300738static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
Shawn Guod84f6382013-02-26 09:42:09 +0800739 struct of_dma *ofdma)
740{
741 struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
742 dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
743 struct mxs_dma_filter_param param;
744
745 if (dma_spec->args_count != 1)
746 return NULL;
747
748 param.of_node = ofdma->of_node;
749 param.chan_id = dma_spec->args[0];
750
751 if (param.chan_id >= mxs_dma->nr_channels)
752 return NULL;
753
754 return dma_request_channel(mask, mxs_dma_filter_fn, &param);
755}
756
Shawn Guoa580b8c2011-02-27 00:47:42 +0800757static int __init mxs_dma_probe(struct platform_device *pdev)
758{
Shawn Guod84f6382013-02-26 09:42:09 +0800759 struct device_node *np = pdev->dev.of_node;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800760 const struct platform_device_id *id_entry;
761 const struct of_device_id *of_id;
762 const struct mxs_dma_type *dma_type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800763 struct mxs_dma_engine *mxs_dma;
764 struct resource *iores;
765 int ret, i;
766
Shawn Guoaaa20512013-02-25 14:57:26 +0800767 mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800768 if (!mxs_dma)
769 return -ENOMEM;
770
Shawn Guod84f6382013-02-26 09:42:09 +0800771 ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
772 if (ret) {
773 dev_err(&pdev->dev, "failed to read dma-channels\n");
774 return ret;
775 }
776
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800777 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
778 if (of_id)
779 id_entry = of_id->data;
780 else
781 id_entry = platform_get_device_id(pdev);
782
783 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
Shawn Guo8c920132012-05-10 06:23:26 +0800784 mxs_dma->type = dma_type->type;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800785 mxs_dma->dev_id = dma_type->id;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800786
787 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guoaaa20512013-02-25 14:57:26 +0800788 mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
789 if (IS_ERR(mxs_dma->base))
790 return PTR_ERR(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800791
Shawn Guoaaa20512013-02-25 14:57:26 +0800792 mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
793 if (IS_ERR(mxs_dma->clk))
794 return PTR_ERR(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800795
796 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
797 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
798
799 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
800
801 /* Initialize channel parameters */
802 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
803 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
804
805 mxs_chan->mxs_dma = mxs_dma;
806 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000807 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800808
809 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
810 (unsigned long) mxs_chan);
811
812
813 /* Add the channel to mxs_chan list */
814 list_add_tail(&mxs_chan->chan.device_node,
815 &mxs_dma->dma_device.channels);
816 }
817
818 ret = mxs_dma_init(mxs_dma);
819 if (ret)
Shawn Guoaaa20512013-02-25 14:57:26 +0800820 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800821
Shawn Guod84f6382013-02-26 09:42:09 +0800822 mxs_dma->pdev = pdev;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800823 mxs_dma->dma_device.dev = &pdev->dev;
824
825 /* mxs_dma gets 65535 bytes maximum sg size */
826 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
827 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
828
829 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
830 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
831 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
832 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
833 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
834 mxs_dma->dma_device.device_control = mxs_dma_control;
835 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
836
837 ret = dma_async_device_register(&mxs_dma->dma_device);
838 if (ret) {
839 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
Shawn Guoaaa20512013-02-25 14:57:26 +0800840 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800841 }
842
Shawn Guod84f6382013-02-26 09:42:09 +0800843 ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
844 if (ret) {
845 dev_err(mxs_dma->dma_device.dev,
846 "failed to register controller\n");
847 dma_async_device_unregister(&mxs_dma->dma_device);
848 }
849
Shawn Guoa580b8c2011-02-27 00:47:42 +0800850 dev_info(mxs_dma->dma_device.dev, "initialized\n");
851
852 return 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800853}
854
Shawn Guoa580b8c2011-02-27 00:47:42 +0800855static struct platform_driver mxs_dma_driver = {
856 .driver = {
857 .name = "mxs-dma",
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800858 .of_match_table = mxs_dma_dt_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800859 },
Shawn Guo8c920132012-05-10 06:23:26 +0800860 .id_table = mxs_dma_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800861};
862
863static int __init mxs_dma_module_init(void)
864{
865 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
866}
867subsys_initcall(mxs_dma_module_init);