blob: 083e7f1a94f06847c98de47b5af943de0dd4518a [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)
Shawn Guoa580b8c2011-02-27 00:47:42 +080063
64/*
65 * ccw bits definitions
66 *
67 * COMMAND: 0..1 (2)
68 * CHAIN: 2 (1)
69 * IRQ: 3 (1)
70 * NAND_LOCK: 4 (1) - not implemented
71 * NAND_WAIT4READY: 5 (1) - not implemented
72 * DEC_SEM: 6 (1)
73 * WAIT4END: 7 (1)
74 * HALT_ON_TERMINATE: 8 (1)
75 * TERMINATE_FLUSH: 9 (1)
76 * RESERVED: 10..11 (2)
77 * PIO_NUM: 12..15 (4)
78 */
79#define BP_CCW_COMMAND 0
80#define BM_CCW_COMMAND (3 << 0)
81#define CCW_CHAIN (1 << 2)
82#define CCW_IRQ (1 << 3)
83#define CCW_DEC_SEM (1 << 6)
84#define CCW_WAIT4END (1 << 7)
85#define CCW_HALT_ON_TERM (1 << 8)
86#define CCW_TERM_FLUSH (1 << 9)
87#define BP_CCW_PIO_NUM 12
88#define BM_CCW_PIO_NUM (0xf << 12)
89
90#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
91
92#define MXS_DMA_CMD_NO_XFER 0
93#define MXS_DMA_CMD_WRITE 1
94#define MXS_DMA_CMD_READ 2
95#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
96
97struct mxs_dma_ccw {
98 u32 next;
99 u16 bits;
100 u16 xfer_bytes;
101#define MAX_XFER_BYTES 0xff00
102 u32 bufaddr;
103#define MXS_PIO_WORDS 16
104 u32 pio_words[MXS_PIO_WORDS];
105};
106
Marek Vasut5e97fa92012-09-04 06:04:25 +0200107#define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
108#define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800109
110struct mxs_dma_chan {
111 struct mxs_dma_engine *mxs_dma;
112 struct dma_chan chan;
113 struct dma_async_tx_descriptor desc;
114 struct tasklet_struct tasklet;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200115 unsigned int chan_irq;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800116 struct mxs_dma_ccw *ccw;
117 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100118 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800119 enum dma_status status;
120 unsigned int flags;
121#define MXS_DMA_SG_LOOP (1 << 0)
122};
123
124#define MXS_DMA_CHANNELS 16
125#define MXS_DMA_CHANNELS_MASK 0xffff
126
Shawn Guo8c920132012-05-10 06:23:26 +0800127enum mxs_dma_devtype {
128 MXS_DMA_APBH,
129 MXS_DMA_APBX,
130};
131
132enum mxs_dma_id {
133 IMX23_DMA,
134 IMX28_DMA,
135};
136
Shawn Guoa580b8c2011-02-27 00:47:42 +0800137struct mxs_dma_engine {
Shawn Guo8c920132012-05-10 06:23:26 +0800138 enum mxs_dma_id dev_id;
139 enum mxs_dma_devtype type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800140 void __iomem *base;
141 struct clk *clk;
142 struct dma_device dma_device;
143 struct device_dma_parameters dma_parms;
144 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
Shawn Guod84f6382013-02-26 09:42:09 +0800145 struct platform_device *pdev;
146 unsigned int nr_channels;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800147};
148
Shawn Guo8c920132012-05-10 06:23:26 +0800149struct mxs_dma_type {
150 enum mxs_dma_id id;
151 enum mxs_dma_devtype type;
152};
153
154static struct mxs_dma_type mxs_dma_types[] = {
155 {
156 .id = IMX23_DMA,
157 .type = MXS_DMA_APBH,
158 }, {
159 .id = IMX23_DMA,
160 .type = MXS_DMA_APBX,
161 }, {
162 .id = IMX28_DMA,
163 .type = MXS_DMA_APBH,
164 }, {
165 .id = IMX28_DMA,
166 .type = MXS_DMA_APBX,
167 }
168};
169
170static struct platform_device_id mxs_dma_ids[] = {
171 {
172 .name = "imx23-dma-apbh",
173 .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
174 }, {
175 .name = "imx23-dma-apbx",
176 .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
177 }, {
178 .name = "imx28-dma-apbh",
179 .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
180 }, {
181 .name = "imx28-dma-apbx",
182 .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
183 }, {
184 /* end of list */
185 }
186};
187
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800188static const struct of_device_id mxs_dma_dt_ids[] = {
189 { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
190 { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
191 { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
192 { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
193 { /* sentinel */ }
194};
195MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
196
Shawn Guo8c920132012-05-10 06:23:26 +0800197static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
198{
199 return container_of(chan, struct mxs_dma_chan, chan);
200}
201
Shawn Guoa580b8c2011-02-27 00:47:42 +0800202static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
203{
204 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
205 int chan_id = mxs_chan->chan.chan_id;
206
Shawn Guobb11fb62012-05-07 14:14:08 +0800207 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800208 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800209 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800210 else
211 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800212 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800213}
214
215static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
216{
217 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
218 int chan_id = mxs_chan->chan.chan_id;
219
220 /* set cmd_addr up */
221 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800222 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800223
Shawn Guoa580b8c2011-02-27 00:47:42 +0800224 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800225 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800226}
227
228static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
229{
Vinod Koul27375832013-10-16 20:51:30 +0530230 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800231}
232
233static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
234{
235 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
236 int chan_id = mxs_chan->chan.chan_id;
237
238 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800239 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800240 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800241 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800242 else
243 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800244 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800245
246 mxs_chan->status = DMA_PAUSED;
247}
248
249static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
250{
251 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
252 int chan_id = mxs_chan->chan.chan_id;
253
254 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800255 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800256 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800257 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800258 else
259 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800260 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800261
262 mxs_chan->status = DMA_IN_PROGRESS;
263}
264
Shawn Guoa580b8c2011-02-27 00:47:42 +0800265static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
266{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000267 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800268}
269
270static void mxs_dma_tasklet(unsigned long data)
271{
272 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
273
274 if (mxs_chan->desc.callback)
275 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
276}
277
Markus Pargmannb2d63982013-10-29 08:47:45 +0100278static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
279{
280 int i;
281
282 for (i = 0; i != mxs_dma->nr_channels; ++i)
283 if (mxs_dma->mxs_chans[i].chan_irq == irq)
284 return i;
285
286 return -EINVAL;
287}
288
Shawn Guoa580b8c2011-02-27 00:47:42 +0800289static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
290{
291 struct mxs_dma_engine *mxs_dma = dev_id;
Markus Pargmannb2d63982013-10-29 08:47:45 +0100292 struct mxs_dma_chan *mxs_chan;
293 u32 completed;
294 u32 err;
295 int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
296
297 if (chan < 0)
298 return IRQ_NONE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800299
300 /* completion status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100301 completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
302 completed = (completed >> chan) & 0x1;
303
304 /* Clear interrupt */
305 writel((1 << chan),
306 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800307
308 /* error status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100309 err = readl(mxs_dma->base + HW_APBHX_CTRL2);
310 err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
311
312 /*
313 * error status bit is in the upper 16 bits, error irq bit in the lower
314 * 16 bits. We transform it into a simpler error code:
315 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
316 */
317 err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
318
319 /* Clear error irq */
320 writel((1 << chan),
321 mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800322
323 /*
324 * When both completion and error of termination bits set at the
325 * same time, we do not take it as an error. IOW, it only becomes
Markus Pargmannb2d63982013-10-29 08:47:45 +0100326 * an error we need to handle here in case of either it's a bus
327 * error or a termination error with no completion. 0x01 is termination
328 * error, so we can subtract err & completed to get the real error case.
Shawn Guoa580b8c2011-02-27 00:47:42 +0800329 */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100330 err -= err & completed;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800331
Markus Pargmannb2d63982013-10-29 08:47:45 +0100332 mxs_chan = &mxs_dma->mxs_chans[chan];
Shawn Guoa580b8c2011-02-27 00:47:42 +0800333
Markus Pargmannb2d63982013-10-29 08:47:45 +0100334 if (err) {
335 dev_dbg(mxs_dma->dma_device.dev,
336 "%s: error in channel %d\n", __func__,
337 chan);
338 mxs_chan->status = DMA_ERROR;
339 mxs_dma_reset_chan(mxs_chan);
340 } else {
341 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
342 mxs_chan->status = DMA_IN_PROGRESS;
343 else
344 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800345 }
346
Markus Pargmannb2d63982013-10-29 08:47:45 +0100347 if (mxs_chan->status == DMA_COMPLETE)
348 dma_cookie_complete(&mxs_chan->desc);
349
350 /* schedule tasklet on this channel */
351 tasklet_schedule(&mxs_chan->tasklet);
352
Shawn Guoa580b8c2011-02-27 00:47:42 +0800353 return IRQ_HANDLED;
354}
355
356static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
357{
358 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800359 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
360 int ret;
361
Marek Vasut5e97fa92012-09-04 06:04:25 +0200362 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
363 CCW_BLOCK_SIZE, &mxs_chan->ccw_phys,
364 GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800365 if (!mxs_chan->ccw) {
366 ret = -ENOMEM;
367 goto err_alloc;
368 }
369
Marek Vasut5e97fa92012-09-04 06:04:25 +0200370 memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800371
Shawn Guo95bfea12011-06-30 16:06:33 +0800372 if (mxs_chan->chan_irq != NO_IRQ) {
373 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
374 0, "mxs-dma", mxs_dma);
375 if (ret)
376 goto err_irq;
377 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800378
Shawn Guo759a2e32011-12-20 13:54:00 +0800379 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800380 if (ret)
381 goto err_clk;
382
383 mxs_dma_reset_chan(mxs_chan);
384
385 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
386 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
387
388 /* the descriptor is ready */
389 async_tx_ack(&mxs_chan->desc);
390
391 return 0;
392
393err_clk:
394 free_irq(mxs_chan->chan_irq, mxs_dma);
395err_irq:
Marek Vasut5e97fa92012-09-04 06:04:25 +0200396 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800397 mxs_chan->ccw, mxs_chan->ccw_phys);
398err_alloc:
399 return ret;
400}
401
402static void mxs_dma_free_chan_resources(struct dma_chan *chan)
403{
404 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
405 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
406
407 mxs_dma_disable_chan(mxs_chan);
408
409 free_irq(mxs_chan->chan_irq, mxs_dma);
410
Marek Vasut5e97fa92012-09-04 06:04:25 +0200411 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800412 mxs_chan->ccw, mxs_chan->ccw_phys);
413
Shawn Guo759a2e32011-12-20 13:54:00 +0800414 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800415}
416
Huang Shijie921de862012-02-16 14:17:33 +0800417/*
418 * How to use the flags for ->device_prep_slave_sg() :
419 * [1] If there is only one DMA command in the DMA chain, the code should be:
420 * ......
421 * ->device_prep_slave_sg(DMA_CTRL_ACK);
422 * ......
423 * [2] If there are two DMA commands in the DMA chain, the code should be
424 * ......
425 * ->device_prep_slave_sg(0);
426 * ......
427 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
428 * ......
429 * [3] If there are more than two DMA commands in the DMA chain, the code
430 * should be:
431 * ......
432 * ->device_prep_slave_sg(0); // First
433 * ......
434 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
435 * ......
436 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
437 * ......
438 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800439static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
440 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530441 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700442 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800443{
444 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
445 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
446 struct mxs_dma_ccw *ccw;
447 struct scatterlist *sg;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200448 u32 i, j;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800449 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800450 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100451 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800452
453 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
454 return NULL;
455
456 if (sg_len + (append ? idx : 0) > NUM_CCW) {
457 dev_err(mxs_dma->dma_device.dev,
458 "maximum number of sg exceeded: %d > %d\n",
459 sg_len, NUM_CCW);
460 goto err_out;
461 }
462
463 mxs_chan->status = DMA_IN_PROGRESS;
464 mxs_chan->flags = 0;
465
466 /*
467 * If the sg is prepared with append flag set, the sg
468 * will be appended to the last prepared sg.
469 */
470 if (append) {
471 BUG_ON(idx < 1);
472 ccw = &mxs_chan->ccw[idx - 1];
473 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
474 ccw->bits |= CCW_CHAIN;
475 ccw->bits &= ~CCW_IRQ;
476 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800477 } else {
478 idx = 0;
479 }
480
Shawn Guo62268ce2011-12-13 23:48:03 +0800481 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800482 ccw = &mxs_chan->ccw[idx++];
483 pio = (u32 *) sgl;
484
485 for (j = 0; j < sg_len;)
486 ccw->pio_words[j++] = *pio++;
487
488 ccw->bits = 0;
489 ccw->bits |= CCW_IRQ;
490 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800491 if (flags & DMA_CTRL_ACK)
492 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800493 ccw->bits |= CCW_HALT_ON_TERM;
494 ccw->bits |= CCW_TERM_FLUSH;
495 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
496 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
497 } else {
498 for_each_sg(sgl, sg, sg_len, i) {
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200499 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800500 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200501 sg_dma_len(sg), MAX_XFER_BYTES);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800502 goto err_out;
503 }
504
505 ccw = &mxs_chan->ccw[idx++];
506
507 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
508 ccw->bufaddr = sg->dma_address;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200509 ccw->xfer_bytes = sg_dma_len(sg);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800510
511 ccw->bits = 0;
512 ccw->bits |= CCW_CHAIN;
513 ccw->bits |= CCW_HALT_ON_TERM;
514 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530515 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800516 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
517 COMMAND);
518
519 if (i + 1 == sg_len) {
520 ccw->bits &= ~CCW_CHAIN;
521 ccw->bits |= CCW_IRQ;
522 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800523 if (flags & DMA_CTRL_ACK)
524 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800525 }
526 }
527 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100528 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800529
530 return &mxs_chan->desc;
531
532err_out:
533 mxs_chan->status = DMA_ERROR;
534 return NULL;
535}
536
537static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
538 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500539 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300540 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800541{
542 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
543 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200544 u32 num_periods = buf_len / period_len;
545 u32 i = 0, buf = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800546
547 if (mxs_chan->status == DMA_IN_PROGRESS)
548 return NULL;
549
550 mxs_chan->status = DMA_IN_PROGRESS;
551 mxs_chan->flags |= MXS_DMA_SG_LOOP;
552
553 if (num_periods > NUM_CCW) {
554 dev_err(mxs_dma->dma_device.dev,
555 "maximum number of sg exceeded: %d > %d\n",
556 num_periods, NUM_CCW);
557 goto err_out;
558 }
559
560 if (period_len > MAX_XFER_BYTES) {
561 dev_err(mxs_dma->dma_device.dev,
562 "maximum period size exceeded: %d > %d\n",
563 period_len, MAX_XFER_BYTES);
564 goto err_out;
565 }
566
567 while (buf < buf_len) {
568 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
569
570 if (i + 1 == num_periods)
571 ccw->next = mxs_chan->ccw_phys;
572 else
573 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
574
575 ccw->bufaddr = dma_addr;
576 ccw->xfer_bytes = period_len;
577
578 ccw->bits = 0;
579 ccw->bits |= CCW_CHAIN;
580 ccw->bits |= CCW_IRQ;
581 ccw->bits |= CCW_HALT_ON_TERM;
582 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530583 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800584 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
585
586 dma_addr += period_len;
587 buf += period_len;
588
589 i++;
590 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100591 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800592
593 return &mxs_chan->desc;
594
595err_out:
596 mxs_chan->status = DMA_ERROR;
597 return NULL;
598}
599
600static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
601 unsigned long arg)
602{
603 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
604 int ret = 0;
605
606 switch (cmd) {
607 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800608 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100609 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800610 break;
611 case DMA_PAUSE:
612 mxs_dma_pause_chan(mxs_chan);
613 break;
614 case DMA_RESUME:
615 mxs_dma_resume_chan(mxs_chan);
616 break;
617 default:
618 ret = -ENOSYS;
619 }
620
621 return ret;
622}
623
624static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
625 dma_cookie_t cookie, struct dma_tx_state *txstate)
626{
627 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Markus Pargmann7b113042013-10-29 08:47:46 +0100628 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
629 u32 residue = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800630
Markus Pargmann7b113042013-10-29 08:47:46 +0100631 if (mxs_chan->status == DMA_IN_PROGRESS &&
632 mxs_chan->flags & MXS_DMA_SG_LOOP) {
633 struct mxs_dma_ccw *last_ccw;
634 u32 bar;
635
636 last_ccw = &mxs_chan->ccw[mxs_chan->desc_count - 1];
637 residue = last_ccw->xfer_bytes + last_ccw->bufaddr;
638
639 bar = readl(mxs_dma->base +
640 HW_APBHX_CHn_BAR(mxs_dma, chan->chan_id));
641 residue -= bar;
642 }
643
644 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
645 residue);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800646
647 return mxs_chan->status;
648}
649
650static void mxs_dma_issue_pending(struct dma_chan *chan)
651{
Shawn Guod04525e2012-04-11 13:29:31 +0800652 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
653
654 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800655}
656
657static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
658{
659 int ret;
660
Shawn Guo759a2e32011-12-20 13:54:00 +0800661 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800662 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100663 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800664
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800665 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800666 if (ret)
667 goto err_out;
668
Shawn Guoa580b8c2011-02-27 00:47:42 +0800669 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800670 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800671 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800672 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800673 writel(BM_APBH_CTRL0_APB_BURST8_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800674 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800675 }
676
677 /* enable irq for all the channels */
678 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800679 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800680
Shawn Guoa580b8c2011-02-27 00:47:42 +0800681err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800682 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800683 return ret;
684}
685
Shawn Guod84f6382013-02-26 09:42:09 +0800686struct mxs_dma_filter_param {
687 struct device_node *of_node;
688 unsigned int chan_id;
689};
690
691static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
692{
693 struct mxs_dma_filter_param *param = fn_param;
694 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
695 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
696 int chan_irq;
697
698 if (mxs_dma->dma_device.dev->of_node != param->of_node)
699 return false;
700
701 if (chan->chan_id != param->chan_id)
702 return false;
703
704 chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
705 if (chan_irq < 0)
706 return false;
707
708 mxs_chan->chan_irq = chan_irq;
709
710 return true;
711}
712
Fabio Estevam3208b372013-05-24 16:37:27 -0300713static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
Shawn Guod84f6382013-02-26 09:42:09 +0800714 struct of_dma *ofdma)
715{
716 struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
717 dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
718 struct mxs_dma_filter_param param;
719
720 if (dma_spec->args_count != 1)
721 return NULL;
722
723 param.of_node = ofdma->of_node;
724 param.chan_id = dma_spec->args[0];
725
726 if (param.chan_id >= mxs_dma->nr_channels)
727 return NULL;
728
729 return dma_request_channel(mask, mxs_dma_filter_fn, &param);
730}
731
Shawn Guoa580b8c2011-02-27 00:47:42 +0800732static int __init mxs_dma_probe(struct platform_device *pdev)
733{
Shawn Guod84f6382013-02-26 09:42:09 +0800734 struct device_node *np = pdev->dev.of_node;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800735 const struct platform_device_id *id_entry;
736 const struct of_device_id *of_id;
737 const struct mxs_dma_type *dma_type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800738 struct mxs_dma_engine *mxs_dma;
739 struct resource *iores;
740 int ret, i;
741
Shawn Guoaaa20512013-02-25 14:57:26 +0800742 mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800743 if (!mxs_dma)
744 return -ENOMEM;
745
Shawn Guod84f6382013-02-26 09:42:09 +0800746 ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
747 if (ret) {
748 dev_err(&pdev->dev, "failed to read dma-channels\n");
749 return ret;
750 }
751
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800752 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
753 if (of_id)
754 id_entry = of_id->data;
755 else
756 id_entry = platform_get_device_id(pdev);
757
758 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
Shawn Guo8c920132012-05-10 06:23:26 +0800759 mxs_dma->type = dma_type->type;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800760 mxs_dma->dev_id = dma_type->id;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800761
762 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guoaaa20512013-02-25 14:57:26 +0800763 mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
764 if (IS_ERR(mxs_dma->base))
765 return PTR_ERR(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800766
Shawn Guoaaa20512013-02-25 14:57:26 +0800767 mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
768 if (IS_ERR(mxs_dma->clk))
769 return PTR_ERR(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800770
771 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
772 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
773
774 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
775
776 /* Initialize channel parameters */
777 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
778 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
779
780 mxs_chan->mxs_dma = mxs_dma;
781 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000782 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800783
784 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
785 (unsigned long) mxs_chan);
786
787
788 /* Add the channel to mxs_chan list */
789 list_add_tail(&mxs_chan->chan.device_node,
790 &mxs_dma->dma_device.channels);
791 }
792
793 ret = mxs_dma_init(mxs_dma);
794 if (ret)
Shawn Guoaaa20512013-02-25 14:57:26 +0800795 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800796
Shawn Guod84f6382013-02-26 09:42:09 +0800797 mxs_dma->pdev = pdev;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800798 mxs_dma->dma_device.dev = &pdev->dev;
799
800 /* mxs_dma gets 65535 bytes maximum sg size */
801 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
802 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
803
804 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
805 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
806 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
807 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
808 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
809 mxs_dma->dma_device.device_control = mxs_dma_control;
810 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
811
812 ret = dma_async_device_register(&mxs_dma->dma_device);
813 if (ret) {
814 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
Shawn Guoaaa20512013-02-25 14:57:26 +0800815 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800816 }
817
Shawn Guod84f6382013-02-26 09:42:09 +0800818 ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
819 if (ret) {
820 dev_err(mxs_dma->dma_device.dev,
821 "failed to register controller\n");
822 dma_async_device_unregister(&mxs_dma->dma_device);
823 }
824
Shawn Guoa580b8c2011-02-27 00:47:42 +0800825 dev_info(mxs_dma->dma_device.dev, "initialized\n");
826
827 return 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800828}
829
Shawn Guoa580b8c2011-02-27 00:47:42 +0800830static struct platform_driver mxs_dma_driver = {
831 .driver = {
832 .name = "mxs-dma",
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800833 .of_match_table = mxs_dma_dt_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800834 },
Shawn Guo8c920132012-05-10 06:23:26 +0800835 .id_table = mxs_dma_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800836};
837
838static int __init mxs_dma_module_init(void)
839{
840 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
841}
842subsys_initcall(mxs_dma_module_init);