blob: 1d1b6e99993d27cc982294dac49b82e00c488ccd [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)
Shawn Guoa580b8c2011-02-27 00:47:42 +080061
62/*
63 * ccw bits definitions
64 *
65 * COMMAND: 0..1 (2)
66 * CHAIN: 2 (1)
67 * IRQ: 3 (1)
68 * NAND_LOCK: 4 (1) - not implemented
69 * NAND_WAIT4READY: 5 (1) - not implemented
70 * DEC_SEM: 6 (1)
71 * WAIT4END: 7 (1)
72 * HALT_ON_TERMINATE: 8 (1)
73 * TERMINATE_FLUSH: 9 (1)
74 * RESERVED: 10..11 (2)
75 * PIO_NUM: 12..15 (4)
76 */
77#define BP_CCW_COMMAND 0
78#define BM_CCW_COMMAND (3 << 0)
79#define CCW_CHAIN (1 << 2)
80#define CCW_IRQ (1 << 3)
81#define CCW_DEC_SEM (1 << 6)
82#define CCW_WAIT4END (1 << 7)
83#define CCW_HALT_ON_TERM (1 << 8)
84#define CCW_TERM_FLUSH (1 << 9)
85#define BP_CCW_PIO_NUM 12
86#define BM_CCW_PIO_NUM (0xf << 12)
87
88#define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
89
90#define MXS_DMA_CMD_NO_XFER 0
91#define MXS_DMA_CMD_WRITE 1
92#define MXS_DMA_CMD_READ 2
93#define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
94
95struct mxs_dma_ccw {
96 u32 next;
97 u16 bits;
98 u16 xfer_bytes;
99#define MAX_XFER_BYTES 0xff00
100 u32 bufaddr;
101#define MXS_PIO_WORDS 16
102 u32 pio_words[MXS_PIO_WORDS];
103};
104
Marek Vasut5e97fa92012-09-04 06:04:25 +0200105#define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
106#define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800107
108struct mxs_dma_chan {
109 struct mxs_dma_engine *mxs_dma;
110 struct dma_chan chan;
111 struct dma_async_tx_descriptor desc;
112 struct tasklet_struct tasklet;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200113 unsigned int chan_irq;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800114 struct mxs_dma_ccw *ccw;
115 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100116 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800117 enum dma_status status;
118 unsigned int flags;
119#define MXS_DMA_SG_LOOP (1 << 0)
120};
121
122#define MXS_DMA_CHANNELS 16
123#define MXS_DMA_CHANNELS_MASK 0xffff
124
Shawn Guo8c920132012-05-10 06:23:26 +0800125enum mxs_dma_devtype {
126 MXS_DMA_APBH,
127 MXS_DMA_APBX,
128};
129
130enum mxs_dma_id {
131 IMX23_DMA,
132 IMX28_DMA,
133};
134
Shawn Guoa580b8c2011-02-27 00:47:42 +0800135struct mxs_dma_engine {
Shawn Guo8c920132012-05-10 06:23:26 +0800136 enum mxs_dma_id dev_id;
137 enum mxs_dma_devtype type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800138 void __iomem *base;
139 struct clk *clk;
140 struct dma_device dma_device;
141 struct device_dma_parameters dma_parms;
142 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
Shawn Guod84f6382013-02-26 09:42:09 +0800143 struct platform_device *pdev;
144 unsigned int nr_channels;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800145};
146
Shawn Guo8c920132012-05-10 06:23:26 +0800147struct mxs_dma_type {
148 enum mxs_dma_id id;
149 enum mxs_dma_devtype type;
150};
151
152static struct mxs_dma_type mxs_dma_types[] = {
153 {
154 .id = IMX23_DMA,
155 .type = MXS_DMA_APBH,
156 }, {
157 .id = IMX23_DMA,
158 .type = MXS_DMA_APBX,
159 }, {
160 .id = IMX28_DMA,
161 .type = MXS_DMA_APBH,
162 }, {
163 .id = IMX28_DMA,
164 .type = MXS_DMA_APBX,
165 }
166};
167
168static struct platform_device_id mxs_dma_ids[] = {
169 {
170 .name = "imx23-dma-apbh",
171 .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
172 }, {
173 .name = "imx23-dma-apbx",
174 .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
175 }, {
176 .name = "imx28-dma-apbh",
177 .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
178 }, {
179 .name = "imx28-dma-apbx",
180 .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
181 }, {
182 /* end of list */
183 }
184};
185
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800186static const struct of_device_id mxs_dma_dt_ids[] = {
187 { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
188 { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
189 { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
190 { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
191 { /* sentinel */ }
192};
193MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
194
Shawn Guo8c920132012-05-10 06:23:26 +0800195static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
196{
197 return container_of(chan, struct mxs_dma_chan, chan);
198}
199
Shawn Guoa580b8c2011-02-27 00:47:42 +0800200static void mxs_dma_reset_chan(struct mxs_dma_chan *mxs_chan)
201{
202 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
203 int chan_id = mxs_chan->chan.chan_id;
204
Shawn Guobb11fb62012-05-07 14:14:08 +0800205 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800206 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800207 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800208 else
209 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800210 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800211}
212
213static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
214{
215 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
216 int chan_id = mxs_chan->chan.chan_id;
217
218 /* set cmd_addr up */
219 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800220 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800221
Shawn Guoa580b8c2011-02-27 00:47:42 +0800222 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800223 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800224}
225
226static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
227{
Vinod Koul27375832013-10-16 20:51:30 +0530228 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800229}
230
231static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
232{
233 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
234 int chan_id = mxs_chan->chan.chan_id;
235
236 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800237 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800238 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800239 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800240 else
241 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800242 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800243
244 mxs_chan->status = DMA_PAUSED;
245}
246
247static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
248{
249 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
250 int chan_id = mxs_chan->chan.chan_id;
251
252 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800253 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800254 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800255 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800256 else
257 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800258 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800259
260 mxs_chan->status = DMA_IN_PROGRESS;
261}
262
Shawn Guoa580b8c2011-02-27 00:47:42 +0800263static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
264{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000265 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800266}
267
268static void mxs_dma_tasklet(unsigned long data)
269{
270 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
271
272 if (mxs_chan->desc.callback)
273 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
274}
275
Markus Pargmannb2d63982013-10-29 08:47:45 +0100276static int mxs_dma_irq_to_chan(struct mxs_dma_engine *mxs_dma, int irq)
277{
278 int i;
279
280 for (i = 0; i != mxs_dma->nr_channels; ++i)
281 if (mxs_dma->mxs_chans[i].chan_irq == irq)
282 return i;
283
284 return -EINVAL;
285}
286
Shawn Guoa580b8c2011-02-27 00:47:42 +0800287static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
288{
289 struct mxs_dma_engine *mxs_dma = dev_id;
Markus Pargmannb2d63982013-10-29 08:47:45 +0100290 struct mxs_dma_chan *mxs_chan;
291 u32 completed;
292 u32 err;
293 int chan = mxs_dma_irq_to_chan(mxs_dma, irq);
294
295 if (chan < 0)
296 return IRQ_NONE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800297
298 /* completion status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100299 completed = readl(mxs_dma->base + HW_APBHX_CTRL1);
300 completed = (completed >> chan) & 0x1;
301
302 /* Clear interrupt */
303 writel((1 << chan),
304 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800305
306 /* error status */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100307 err = readl(mxs_dma->base + HW_APBHX_CTRL2);
308 err &= (1 << (MXS_DMA_CHANNELS + chan)) | (1 << chan);
309
310 /*
311 * error status bit is in the upper 16 bits, error irq bit in the lower
312 * 16 bits. We transform it into a simpler error code:
313 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
314 */
315 err = (err >> (MXS_DMA_CHANNELS + chan)) + (err >> chan);
316
317 /* Clear error irq */
318 writel((1 << chan),
319 mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800320
321 /*
322 * When both completion and error of termination bits set at the
323 * same time, we do not take it as an error. IOW, it only becomes
Markus Pargmannb2d63982013-10-29 08:47:45 +0100324 * an error we need to handle here in case of either it's a bus
325 * error or a termination error with no completion. 0x01 is termination
326 * error, so we can subtract err & completed to get the real error case.
Shawn Guoa580b8c2011-02-27 00:47:42 +0800327 */
Markus Pargmannb2d63982013-10-29 08:47:45 +0100328 err -= err & completed;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800329
Markus Pargmannb2d63982013-10-29 08:47:45 +0100330 mxs_chan = &mxs_dma->mxs_chans[chan];
Shawn Guoa580b8c2011-02-27 00:47:42 +0800331
Markus Pargmannb2d63982013-10-29 08:47:45 +0100332 if (err) {
333 dev_dbg(mxs_dma->dma_device.dev,
334 "%s: error in channel %d\n", __func__,
335 chan);
336 mxs_chan->status = DMA_ERROR;
337 mxs_dma_reset_chan(mxs_chan);
338 } else {
339 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
340 mxs_chan->status = DMA_IN_PROGRESS;
341 else
342 mxs_chan->status = DMA_COMPLETE;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800343 }
344
Markus Pargmannb2d63982013-10-29 08:47:45 +0100345 if (mxs_chan->status == DMA_COMPLETE)
346 dma_cookie_complete(&mxs_chan->desc);
347
348 /* schedule tasklet on this channel */
349 tasklet_schedule(&mxs_chan->tasklet);
350
Shawn Guoa580b8c2011-02-27 00:47:42 +0800351 return IRQ_HANDLED;
352}
353
354static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
355{
356 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800357 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
358 int ret;
359
Marek Vasut5e97fa92012-09-04 06:04:25 +0200360 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
361 CCW_BLOCK_SIZE, &mxs_chan->ccw_phys,
362 GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800363 if (!mxs_chan->ccw) {
364 ret = -ENOMEM;
365 goto err_alloc;
366 }
367
Marek Vasut5e97fa92012-09-04 06:04:25 +0200368 memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800369
Shawn Guo95bfea12011-06-30 16:06:33 +0800370 if (mxs_chan->chan_irq != NO_IRQ) {
371 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
372 0, "mxs-dma", mxs_dma);
373 if (ret)
374 goto err_irq;
375 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800376
Shawn Guo759a2e32011-12-20 13:54:00 +0800377 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800378 if (ret)
379 goto err_clk;
380
381 mxs_dma_reset_chan(mxs_chan);
382
383 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
384 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
385
386 /* the descriptor is ready */
387 async_tx_ack(&mxs_chan->desc);
388
389 return 0;
390
391err_clk:
392 free_irq(mxs_chan->chan_irq, mxs_dma);
393err_irq:
Marek Vasut5e97fa92012-09-04 06:04:25 +0200394 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800395 mxs_chan->ccw, mxs_chan->ccw_phys);
396err_alloc:
397 return ret;
398}
399
400static void mxs_dma_free_chan_resources(struct dma_chan *chan)
401{
402 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
403 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
404
405 mxs_dma_disable_chan(mxs_chan);
406
407 free_irq(mxs_chan->chan_irq, mxs_dma);
408
Marek Vasut5e97fa92012-09-04 06:04:25 +0200409 dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800410 mxs_chan->ccw, mxs_chan->ccw_phys);
411
Shawn Guo759a2e32011-12-20 13:54:00 +0800412 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800413}
414
Huang Shijie921de862012-02-16 14:17:33 +0800415/*
416 * How to use the flags for ->device_prep_slave_sg() :
417 * [1] If there is only one DMA command in the DMA chain, the code should be:
418 * ......
419 * ->device_prep_slave_sg(DMA_CTRL_ACK);
420 * ......
421 * [2] If there are two DMA commands in the DMA chain, the code should be
422 * ......
423 * ->device_prep_slave_sg(0);
424 * ......
425 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
426 * ......
427 * [3] If there are more than two DMA commands in the DMA chain, the code
428 * should be:
429 * ......
430 * ->device_prep_slave_sg(0); // First
431 * ......
432 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
433 * ......
434 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
435 * ......
436 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800437static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
438 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530439 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700440 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800441{
442 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
443 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
444 struct mxs_dma_ccw *ccw;
445 struct scatterlist *sg;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200446 u32 i, j;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800447 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800448 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100449 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800450
451 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
452 return NULL;
453
454 if (sg_len + (append ? idx : 0) > NUM_CCW) {
455 dev_err(mxs_dma->dma_device.dev,
456 "maximum number of sg exceeded: %d > %d\n",
457 sg_len, NUM_CCW);
458 goto err_out;
459 }
460
461 mxs_chan->status = DMA_IN_PROGRESS;
462 mxs_chan->flags = 0;
463
464 /*
465 * If the sg is prepared with append flag set, the sg
466 * will be appended to the last prepared sg.
467 */
468 if (append) {
469 BUG_ON(idx < 1);
470 ccw = &mxs_chan->ccw[idx - 1];
471 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
472 ccw->bits |= CCW_CHAIN;
473 ccw->bits &= ~CCW_IRQ;
474 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800475 } else {
476 idx = 0;
477 }
478
Shawn Guo62268ce2011-12-13 23:48:03 +0800479 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800480 ccw = &mxs_chan->ccw[idx++];
481 pio = (u32 *) sgl;
482
483 for (j = 0; j < sg_len;)
484 ccw->pio_words[j++] = *pio++;
485
486 ccw->bits = 0;
487 ccw->bits |= CCW_IRQ;
488 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800489 if (flags & DMA_CTRL_ACK)
490 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800491 ccw->bits |= CCW_HALT_ON_TERM;
492 ccw->bits |= CCW_TERM_FLUSH;
493 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
494 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
495 } else {
496 for_each_sg(sgl, sg, sg_len, i) {
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200497 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800498 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200499 sg_dma_len(sg), MAX_XFER_BYTES);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800500 goto err_out;
501 }
502
503 ccw = &mxs_chan->ccw[idx++];
504
505 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
506 ccw->bufaddr = sg->dma_address;
Lars-Peter Clausenfdaf9c42012-04-25 20:50:52 +0200507 ccw->xfer_bytes = sg_dma_len(sg);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800508
509 ccw->bits = 0;
510 ccw->bits |= CCW_CHAIN;
511 ccw->bits |= CCW_HALT_ON_TERM;
512 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530513 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800514 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
515 COMMAND);
516
517 if (i + 1 == sg_len) {
518 ccw->bits &= ~CCW_CHAIN;
519 ccw->bits |= CCW_IRQ;
520 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800521 if (flags & DMA_CTRL_ACK)
522 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800523 }
524 }
525 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100526 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800527
528 return &mxs_chan->desc;
529
530err_out:
531 mxs_chan->status = DMA_ERROR;
532 return NULL;
533}
534
535static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
536 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500537 size_t period_len, enum dma_transfer_direction direction,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300538 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800539{
540 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
541 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
Fabio Estevamf2ad6992013-01-07 23:48:39 -0200542 u32 num_periods = buf_len / period_len;
543 u32 i = 0, buf = 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800544
545 if (mxs_chan->status == DMA_IN_PROGRESS)
546 return NULL;
547
548 mxs_chan->status = DMA_IN_PROGRESS;
549 mxs_chan->flags |= MXS_DMA_SG_LOOP;
550
551 if (num_periods > NUM_CCW) {
552 dev_err(mxs_dma->dma_device.dev,
553 "maximum number of sg exceeded: %d > %d\n",
554 num_periods, NUM_CCW);
555 goto err_out;
556 }
557
558 if (period_len > MAX_XFER_BYTES) {
559 dev_err(mxs_dma->dma_device.dev,
560 "maximum period size exceeded: %d > %d\n",
561 period_len, MAX_XFER_BYTES);
562 goto err_out;
563 }
564
565 while (buf < buf_len) {
566 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
567
568 if (i + 1 == num_periods)
569 ccw->next = mxs_chan->ccw_phys;
570 else
571 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
572
573 ccw->bufaddr = dma_addr;
574 ccw->xfer_bytes = period_len;
575
576 ccw->bits = 0;
577 ccw->bits |= CCW_CHAIN;
578 ccw->bits |= CCW_IRQ;
579 ccw->bits |= CCW_HALT_ON_TERM;
580 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530581 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800582 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
583
584 dma_addr += period_len;
585 buf += period_len;
586
587 i++;
588 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100589 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800590
591 return &mxs_chan->desc;
592
593err_out:
594 mxs_chan->status = DMA_ERROR;
595 return NULL;
596}
597
598static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
599 unsigned long arg)
600{
601 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
602 int ret = 0;
603
604 switch (cmd) {
605 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800606 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100607 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800608 break;
609 case DMA_PAUSE:
610 mxs_dma_pause_chan(mxs_chan);
611 break;
612 case DMA_RESUME:
613 mxs_dma_resume_chan(mxs_chan);
614 break;
615 default:
616 ret = -ENOSYS;
617 }
618
619 return ret;
620}
621
622static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
623 dma_cookie_t cookie, struct dma_tx_state *txstate)
624{
625 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800626
Andy Shevchenko09d16692013-05-27 15:14:32 +0300627 dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, 0);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800628
629 return mxs_chan->status;
630}
631
632static void mxs_dma_issue_pending(struct dma_chan *chan)
633{
Shawn Guod04525e2012-04-11 13:29:31 +0800634 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
635
636 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800637}
638
639static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
640{
641 int ret;
642
Shawn Guo759a2e32011-12-20 13:54:00 +0800643 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800644 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100645 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800646
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800647 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800648 if (ret)
649 goto err_out;
650
Shawn Guoa580b8c2011-02-27 00:47:42 +0800651 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800652 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800653 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800654 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800655 writel(BM_APBH_CTRL0_APB_BURST8_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800656 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800657 }
658
659 /* enable irq for all the channels */
660 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800661 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800662
Shawn Guoa580b8c2011-02-27 00:47:42 +0800663err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800664 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800665 return ret;
666}
667
Shawn Guod84f6382013-02-26 09:42:09 +0800668struct mxs_dma_filter_param {
669 struct device_node *of_node;
670 unsigned int chan_id;
671};
672
673static bool mxs_dma_filter_fn(struct dma_chan *chan, void *fn_param)
674{
675 struct mxs_dma_filter_param *param = fn_param;
676 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
677 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
678 int chan_irq;
679
680 if (mxs_dma->dma_device.dev->of_node != param->of_node)
681 return false;
682
683 if (chan->chan_id != param->chan_id)
684 return false;
685
686 chan_irq = platform_get_irq(mxs_dma->pdev, param->chan_id);
687 if (chan_irq < 0)
688 return false;
689
690 mxs_chan->chan_irq = chan_irq;
691
692 return true;
693}
694
Fabio Estevam3208b372013-05-24 16:37:27 -0300695static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
Shawn Guod84f6382013-02-26 09:42:09 +0800696 struct of_dma *ofdma)
697{
698 struct mxs_dma_engine *mxs_dma = ofdma->of_dma_data;
699 dma_cap_mask_t mask = mxs_dma->dma_device.cap_mask;
700 struct mxs_dma_filter_param param;
701
702 if (dma_spec->args_count != 1)
703 return NULL;
704
705 param.of_node = ofdma->of_node;
706 param.chan_id = dma_spec->args[0];
707
708 if (param.chan_id >= mxs_dma->nr_channels)
709 return NULL;
710
711 return dma_request_channel(mask, mxs_dma_filter_fn, &param);
712}
713
Shawn Guoa580b8c2011-02-27 00:47:42 +0800714static int __init mxs_dma_probe(struct platform_device *pdev)
715{
Shawn Guod84f6382013-02-26 09:42:09 +0800716 struct device_node *np = pdev->dev.of_node;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800717 const struct platform_device_id *id_entry;
718 const struct of_device_id *of_id;
719 const struct mxs_dma_type *dma_type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800720 struct mxs_dma_engine *mxs_dma;
721 struct resource *iores;
722 int ret, i;
723
Shawn Guoaaa20512013-02-25 14:57:26 +0800724 mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800725 if (!mxs_dma)
726 return -ENOMEM;
727
Shawn Guod84f6382013-02-26 09:42:09 +0800728 ret = of_property_read_u32(np, "dma-channels", &mxs_dma->nr_channels);
729 if (ret) {
730 dev_err(&pdev->dev, "failed to read dma-channels\n");
731 return ret;
732 }
733
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800734 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
735 if (of_id)
736 id_entry = of_id->data;
737 else
738 id_entry = platform_get_device_id(pdev);
739
740 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
Shawn Guo8c920132012-05-10 06:23:26 +0800741 mxs_dma->type = dma_type->type;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800742 mxs_dma->dev_id = dma_type->id;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800743
744 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shawn Guoaaa20512013-02-25 14:57:26 +0800745 mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
746 if (IS_ERR(mxs_dma->base))
747 return PTR_ERR(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800748
Shawn Guoaaa20512013-02-25 14:57:26 +0800749 mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
750 if (IS_ERR(mxs_dma->clk))
751 return PTR_ERR(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800752
753 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
754 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
755
756 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
757
758 /* Initialize channel parameters */
759 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
760 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
761
762 mxs_chan->mxs_dma = mxs_dma;
763 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000764 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800765
766 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
767 (unsigned long) mxs_chan);
768
769
770 /* Add the channel to mxs_chan list */
771 list_add_tail(&mxs_chan->chan.device_node,
772 &mxs_dma->dma_device.channels);
773 }
774
775 ret = mxs_dma_init(mxs_dma);
776 if (ret)
Shawn Guoaaa20512013-02-25 14:57:26 +0800777 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800778
Shawn Guod84f6382013-02-26 09:42:09 +0800779 mxs_dma->pdev = pdev;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800780 mxs_dma->dma_device.dev = &pdev->dev;
781
782 /* mxs_dma gets 65535 bytes maximum sg size */
783 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
784 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
785
786 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
787 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
788 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
789 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
790 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
791 mxs_dma->dma_device.device_control = mxs_dma_control;
792 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
793
794 ret = dma_async_device_register(&mxs_dma->dma_device);
795 if (ret) {
796 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
Shawn Guoaaa20512013-02-25 14:57:26 +0800797 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800798 }
799
Shawn Guod84f6382013-02-26 09:42:09 +0800800 ret = of_dma_controller_register(np, mxs_dma_xlate, mxs_dma);
801 if (ret) {
802 dev_err(mxs_dma->dma_device.dev,
803 "failed to register controller\n");
804 dma_async_device_unregister(&mxs_dma->dma_device);
805 }
806
Shawn Guoa580b8c2011-02-27 00:47:42 +0800807 dev_info(mxs_dma->dma_device.dev, "initialized\n");
808
809 return 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800810}
811
Shawn Guoa580b8c2011-02-27 00:47:42 +0800812static struct platform_driver mxs_dma_driver = {
813 .driver = {
814 .name = "mxs-dma",
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800815 .of_match_table = mxs_dma_dt_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800816 },
Shawn Guo8c920132012-05-10 06:23:26 +0800817 .id_table = mxs_dma_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800818};
819
820static int __init mxs_dma_module_init(void)
821{
822 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
823}
824subsys_initcall(mxs_dma_module_init);