blob: 1cb9b974493fd34b4e10895ee156de0aff1ff987 [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>
Huang Shijie39468602012-02-16 14:17:32 +080026#include <linux/fsl/mxs-dma.h>
Dong Aishengf5b7efc2012-05-04 20:12:15 +080027#include <linux/stmp_device.h>
Dong Aisheng90c9abc2012-05-04 20:12:17 +080028#include <linux/of.h>
29#include <linux/of_device.h>
Shawn Guoa580b8c2011-02-27 00:47:42 +080030
31#include <asm/irq.h>
32#include <mach/mxs.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
105#define NUM_CCW (int)(PAGE_SIZE / sizeof(struct mxs_dma_ccw))
106
107struct mxs_dma_chan {
108 struct mxs_dma_engine *mxs_dma;
109 struct dma_chan chan;
110 struct dma_async_tx_descriptor desc;
111 struct tasklet_struct tasklet;
112 int chan_irq;
113 struct mxs_dma_ccw *ccw;
114 dma_addr_t ccw_phys;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100115 int desc_count;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800116 enum dma_status status;
117 unsigned int flags;
118#define MXS_DMA_SG_LOOP (1 << 0)
119};
120
121#define MXS_DMA_CHANNELS 16
122#define MXS_DMA_CHANNELS_MASK 0xffff
123
Shawn Guo8c920132012-05-10 06:23:26 +0800124enum mxs_dma_devtype {
125 MXS_DMA_APBH,
126 MXS_DMA_APBX,
127};
128
129enum mxs_dma_id {
130 IMX23_DMA,
131 IMX28_DMA,
132};
133
Shawn Guoa580b8c2011-02-27 00:47:42 +0800134struct mxs_dma_engine {
Shawn Guo8c920132012-05-10 06:23:26 +0800135 enum mxs_dma_id dev_id;
136 enum mxs_dma_devtype type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800137 void __iomem *base;
138 struct clk *clk;
139 struct dma_device dma_device;
140 struct device_dma_parameters dma_parms;
141 struct mxs_dma_chan mxs_chans[MXS_DMA_CHANNELS];
142};
143
Shawn Guo8c920132012-05-10 06:23:26 +0800144struct mxs_dma_type {
145 enum mxs_dma_id id;
146 enum mxs_dma_devtype type;
147};
148
149static struct mxs_dma_type mxs_dma_types[] = {
150 {
151 .id = IMX23_DMA,
152 .type = MXS_DMA_APBH,
153 }, {
154 .id = IMX23_DMA,
155 .type = MXS_DMA_APBX,
156 }, {
157 .id = IMX28_DMA,
158 .type = MXS_DMA_APBH,
159 }, {
160 .id = IMX28_DMA,
161 .type = MXS_DMA_APBX,
162 }
163};
164
165static struct platform_device_id mxs_dma_ids[] = {
166 {
167 .name = "imx23-dma-apbh",
168 .driver_data = (kernel_ulong_t) &mxs_dma_types[0],
169 }, {
170 .name = "imx23-dma-apbx",
171 .driver_data = (kernel_ulong_t) &mxs_dma_types[1],
172 }, {
173 .name = "imx28-dma-apbh",
174 .driver_data = (kernel_ulong_t) &mxs_dma_types[2],
175 }, {
176 .name = "imx28-dma-apbx",
177 .driver_data = (kernel_ulong_t) &mxs_dma_types[3],
178 }, {
179 /* end of list */
180 }
181};
182
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800183static const struct of_device_id mxs_dma_dt_ids[] = {
184 { .compatible = "fsl,imx23-dma-apbh", .data = &mxs_dma_ids[0], },
185 { .compatible = "fsl,imx23-dma-apbx", .data = &mxs_dma_ids[1], },
186 { .compatible = "fsl,imx28-dma-apbh", .data = &mxs_dma_ids[2], },
187 { .compatible = "fsl,imx28-dma-apbx", .data = &mxs_dma_ids[3], },
188 { /* sentinel */ }
189};
190MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
191
Shawn Guo8c920132012-05-10 06:23:26 +0800192static struct mxs_dma_chan *to_mxs_dma_chan(struct dma_chan *chan)
193{
194 return container_of(chan, struct mxs_dma_chan, chan);
195}
196
197int mxs_dma_is_apbh(struct dma_chan *chan)
198{
199 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
200 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
201
202 return dma_is_apbh(mxs_dma);
203}
204
205int mxs_dma_is_apbx(struct dma_chan *chan)
206{
207 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
208 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
209
210 return !dma_is_apbh(mxs_dma);
211}
212
Shawn Guoa580b8c2011-02-27 00:47:42 +0800213static void mxs_dma_reset_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
Shawn Guobb11fb62012-05-07 14:14:08 +0800218 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800219 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800220 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800221 else
222 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800223 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800224}
225
226static void mxs_dma_enable_chan(struct mxs_dma_chan *mxs_chan)
227{
228 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
229 int chan_id = mxs_chan->chan.chan_id;
230
231 /* set cmd_addr up */
232 writel(mxs_chan->ccw_phys,
Shawn Guobb11fb62012-05-07 14:14:08 +0800233 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800234
Shawn Guoa580b8c2011-02-27 00:47:42 +0800235 /* write 1 to SEMA to kick off the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800236 writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
Shawn Guoa580b8c2011-02-27 00:47:42 +0800237}
238
239static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
240{
Shawn Guoa580b8c2011-02-27 00:47:42 +0800241 mxs_chan->status = DMA_SUCCESS;
242}
243
244static void mxs_dma_pause_chan(struct mxs_dma_chan *mxs_chan)
245{
246 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
247 int chan_id = mxs_chan->chan.chan_id;
248
249 /* freeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800250 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800251 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800252 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800253 else
254 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800255 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800256
257 mxs_chan->status = DMA_PAUSED;
258}
259
260static void mxs_dma_resume_chan(struct mxs_dma_chan *mxs_chan)
261{
262 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
263 int chan_id = mxs_chan->chan.chan_id;
264
265 /* unfreeze the channel */
Shawn Guobb11fb62012-05-07 14:14:08 +0800266 if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
Shawn Guoa580b8c2011-02-27 00:47:42 +0800267 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800268 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800269 else
270 writel(1 << chan_id,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800271 mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800272
273 mxs_chan->status = DMA_IN_PROGRESS;
274}
275
Shawn Guoa580b8c2011-02-27 00:47:42 +0800276static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
277{
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000278 return dma_cookie_assign(tx);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800279}
280
281static void mxs_dma_tasklet(unsigned long data)
282{
283 struct mxs_dma_chan *mxs_chan = (struct mxs_dma_chan *) data;
284
285 if (mxs_chan->desc.callback)
286 mxs_chan->desc.callback(mxs_chan->desc.callback_param);
287}
288
289static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
290{
291 struct mxs_dma_engine *mxs_dma = dev_id;
292 u32 stat1, stat2;
293
294 /* completion status */
295 stat1 = readl(mxs_dma->base + HW_APBHX_CTRL1);
296 stat1 &= MXS_DMA_CHANNELS_MASK;
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800297 writel(stat1, mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800298
299 /* error status */
300 stat2 = readl(mxs_dma->base + HW_APBHX_CTRL2);
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800301 writel(stat2, mxs_dma->base + HW_APBHX_CTRL2 + STMP_OFFSET_REG_CLR);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800302
303 /*
304 * When both completion and error of termination bits set at the
305 * same time, we do not take it as an error. IOW, it only becomes
Lothar Waßmann40031222011-12-08 09:15:41 +0100306 * an error we need to handle here in case of either it's (1) a bus
Shawn Guoa580b8c2011-02-27 00:47:42 +0800307 * error or (2) a termination error with no completion.
308 */
309 stat2 = ((stat2 >> MXS_DMA_CHANNELS) & stat2) | /* (1) */
310 (~(stat2 >> MXS_DMA_CHANNELS) & stat2 & ~stat1); /* (2) */
311
312 /* combine error and completion status for checking */
313 stat1 = (stat2 << MXS_DMA_CHANNELS) | stat1;
314 while (stat1) {
315 int channel = fls(stat1) - 1;
316 struct mxs_dma_chan *mxs_chan =
317 &mxs_dma->mxs_chans[channel % MXS_DMA_CHANNELS];
318
319 if (channel >= MXS_DMA_CHANNELS) {
320 dev_dbg(mxs_dma->dma_device.dev,
321 "%s: error in channel %d\n", __func__,
322 channel - MXS_DMA_CHANNELS);
323 mxs_chan->status = DMA_ERROR;
324 mxs_dma_reset_chan(mxs_chan);
325 } else {
326 if (mxs_chan->flags & MXS_DMA_SG_LOOP)
327 mxs_chan->status = DMA_IN_PROGRESS;
328 else
329 mxs_chan->status = DMA_SUCCESS;
330 }
331
332 stat1 &= ~(1 << channel);
333
334 if (mxs_chan->status == DMA_SUCCESS)
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000335 dma_cookie_complete(&mxs_chan->desc);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800336
337 /* schedule tasklet on this channel */
338 tasklet_schedule(&mxs_chan->tasklet);
339 }
340
341 return IRQ_HANDLED;
342}
343
344static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
345{
346 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
347 struct mxs_dma_data *data = chan->private;
348 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
349 int ret;
350
351 if (!data)
352 return -EINVAL;
353
354 mxs_chan->chan_irq = data->chan_irq;
355
356 mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
357 &mxs_chan->ccw_phys, GFP_KERNEL);
358 if (!mxs_chan->ccw) {
359 ret = -ENOMEM;
360 goto err_alloc;
361 }
362
363 memset(mxs_chan->ccw, 0, PAGE_SIZE);
364
Shawn Guo95bfea12011-06-30 16:06:33 +0800365 if (mxs_chan->chan_irq != NO_IRQ) {
366 ret = request_irq(mxs_chan->chan_irq, mxs_dma_int_handler,
367 0, "mxs-dma", mxs_dma);
368 if (ret)
369 goto err_irq;
370 }
Shawn Guoa580b8c2011-02-27 00:47:42 +0800371
Shawn Guo759a2e32011-12-20 13:54:00 +0800372 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800373 if (ret)
374 goto err_clk;
375
376 mxs_dma_reset_chan(mxs_chan);
377
378 dma_async_tx_descriptor_init(&mxs_chan->desc, chan);
379 mxs_chan->desc.tx_submit = mxs_dma_tx_submit;
380
381 /* the descriptor is ready */
382 async_tx_ack(&mxs_chan->desc);
383
384 return 0;
385
386err_clk:
387 free_irq(mxs_chan->chan_irq, mxs_dma);
388err_irq:
389 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
390 mxs_chan->ccw, mxs_chan->ccw_phys);
391err_alloc:
392 return ret;
393}
394
395static void mxs_dma_free_chan_resources(struct dma_chan *chan)
396{
397 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
398 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
399
400 mxs_dma_disable_chan(mxs_chan);
401
402 free_irq(mxs_chan->chan_irq, mxs_dma);
403
404 dma_free_coherent(mxs_dma->dma_device.dev, PAGE_SIZE,
405 mxs_chan->ccw, mxs_chan->ccw_phys);
406
Shawn Guo759a2e32011-12-20 13:54:00 +0800407 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800408}
409
Huang Shijie921de862012-02-16 14:17:33 +0800410/*
411 * How to use the flags for ->device_prep_slave_sg() :
412 * [1] If there is only one DMA command in the DMA chain, the code should be:
413 * ......
414 * ->device_prep_slave_sg(DMA_CTRL_ACK);
415 * ......
416 * [2] If there are two DMA commands in the DMA chain, the code should be
417 * ......
418 * ->device_prep_slave_sg(0);
419 * ......
420 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
421 * ......
422 * [3] If there are more than two DMA commands in the DMA chain, the code
423 * should be:
424 * ......
425 * ->device_prep_slave_sg(0); // First
426 * ......
427 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
428 * ......
429 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
430 * ......
431 */
Shawn Guoa580b8c2011-02-27 00:47:42 +0800432static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
433 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530434 unsigned int sg_len, enum dma_transfer_direction direction,
Linus Torvalds623ff772012-03-30 17:31:56 -0700435 unsigned long flags, void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800436{
437 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
438 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
439 struct mxs_dma_ccw *ccw;
440 struct scatterlist *sg;
441 int i, j;
442 u32 *pio;
Huang Shijie921de862012-02-16 14:17:33 +0800443 bool append = flags & DMA_PREP_INTERRUPT;
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100444 int idx = append ? mxs_chan->desc_count : 0;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800445
446 if (mxs_chan->status == DMA_IN_PROGRESS && !append)
447 return NULL;
448
449 if (sg_len + (append ? idx : 0) > NUM_CCW) {
450 dev_err(mxs_dma->dma_device.dev,
451 "maximum number of sg exceeded: %d > %d\n",
452 sg_len, NUM_CCW);
453 goto err_out;
454 }
455
456 mxs_chan->status = DMA_IN_PROGRESS;
457 mxs_chan->flags = 0;
458
459 /*
460 * If the sg is prepared with append flag set, the sg
461 * will be appended to the last prepared sg.
462 */
463 if (append) {
464 BUG_ON(idx < 1);
465 ccw = &mxs_chan->ccw[idx - 1];
466 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
467 ccw->bits |= CCW_CHAIN;
468 ccw->bits &= ~CCW_IRQ;
469 ccw->bits &= ~CCW_DEC_SEM;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800470 } else {
471 idx = 0;
472 }
473
Shawn Guo62268ce2011-12-13 23:48:03 +0800474 if (direction == DMA_TRANS_NONE) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800475 ccw = &mxs_chan->ccw[idx++];
476 pio = (u32 *) sgl;
477
478 for (j = 0; j < sg_len;)
479 ccw->pio_words[j++] = *pio++;
480
481 ccw->bits = 0;
482 ccw->bits |= CCW_IRQ;
483 ccw->bits |= CCW_DEC_SEM;
Huang Shijie921de862012-02-16 14:17:33 +0800484 if (flags & DMA_CTRL_ACK)
485 ccw->bits |= CCW_WAIT4END;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800486 ccw->bits |= CCW_HALT_ON_TERM;
487 ccw->bits |= CCW_TERM_FLUSH;
488 ccw->bits |= BF_CCW(sg_len, PIO_NUM);
489 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND);
490 } else {
491 for_each_sg(sgl, sg, sg_len, i) {
492 if (sg->length > MAX_XFER_BYTES) {
493 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
494 sg->length, MAX_XFER_BYTES);
495 goto err_out;
496 }
497
498 ccw = &mxs_chan->ccw[idx++];
499
500 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * idx;
501 ccw->bufaddr = sg->dma_address;
502 ccw->xfer_bytes = sg->length;
503
504 ccw->bits = 0;
505 ccw->bits |= CCW_CHAIN;
506 ccw->bits |= CCW_HALT_ON_TERM;
507 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530508 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800509 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ,
510 COMMAND);
511
512 if (i + 1 == sg_len) {
513 ccw->bits &= ~CCW_CHAIN;
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 }
519 }
520 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100521 mxs_chan->desc_count = idx;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800522
523 return &mxs_chan->desc;
524
525err_out:
526 mxs_chan->status = DMA_ERROR;
527 return NULL;
528}
529
530static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
531 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500532 size_t period_len, enum dma_transfer_direction direction,
533 void *context)
Shawn Guoa580b8c2011-02-27 00:47:42 +0800534{
535 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
536 struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
537 int num_periods = buf_len / period_len;
538 int i = 0, buf = 0;
539
540 if (mxs_chan->status == DMA_IN_PROGRESS)
541 return NULL;
542
543 mxs_chan->status = DMA_IN_PROGRESS;
544 mxs_chan->flags |= MXS_DMA_SG_LOOP;
545
546 if (num_periods > NUM_CCW) {
547 dev_err(mxs_dma->dma_device.dev,
548 "maximum number of sg exceeded: %d > %d\n",
549 num_periods, NUM_CCW);
550 goto err_out;
551 }
552
553 if (period_len > MAX_XFER_BYTES) {
554 dev_err(mxs_dma->dma_device.dev,
555 "maximum period size exceeded: %d > %d\n",
556 period_len, MAX_XFER_BYTES);
557 goto err_out;
558 }
559
560 while (buf < buf_len) {
561 struct mxs_dma_ccw *ccw = &mxs_chan->ccw[i];
562
563 if (i + 1 == num_periods)
564 ccw->next = mxs_chan->ccw_phys;
565 else
566 ccw->next = mxs_chan->ccw_phys + sizeof(*ccw) * (i + 1);
567
568 ccw->bufaddr = dma_addr;
569 ccw->xfer_bytes = period_len;
570
571 ccw->bits = 0;
572 ccw->bits |= CCW_CHAIN;
573 ccw->bits |= CCW_IRQ;
574 ccw->bits |= CCW_HALT_ON_TERM;
575 ccw->bits |= CCW_TERM_FLUSH;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530576 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
Shawn Guoa580b8c2011-02-27 00:47:42 +0800577 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
578
579 dma_addr += period_len;
580 buf += period_len;
581
582 i++;
583 }
Lothar Waßmann6d23ea42011-12-08 09:15:43 +0100584 mxs_chan->desc_count = i;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800585
586 return &mxs_chan->desc;
587
588err_out:
589 mxs_chan->status = DMA_ERROR;
590 return NULL;
591}
592
593static int mxs_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
594 unsigned long arg)
595{
596 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
597 int ret = 0;
598
599 switch (cmd) {
600 case DMA_TERMINATE_ALL:
Dong Aishenga62bae92011-07-19 12:09:56 +0800601 mxs_dma_reset_chan(mxs_chan);
Lothar Waßmann7ad7a342011-12-08 09:15:44 +0100602 mxs_dma_disable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800603 break;
604 case DMA_PAUSE:
605 mxs_dma_pause_chan(mxs_chan);
606 break;
607 case DMA_RESUME:
608 mxs_dma_resume_chan(mxs_chan);
609 break;
610 default:
611 ret = -ENOSYS;
612 }
613
614 return ret;
615}
616
617static enum dma_status mxs_dma_tx_status(struct dma_chan *chan,
618 dma_cookie_t cookie, struct dma_tx_state *txstate)
619{
620 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
621 dma_cookie_t last_used;
622
623 last_used = chan->cookie;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000624 dma_set_tx_state(txstate, chan->completed_cookie, last_used, 0);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800625
626 return mxs_chan->status;
627}
628
629static void mxs_dma_issue_pending(struct dma_chan *chan)
630{
Shawn Guod04525e2012-04-11 13:29:31 +0800631 struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
632
633 mxs_dma_enable_chan(mxs_chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800634}
635
636static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
637{
638 int ret;
639
Shawn Guo759a2e32011-12-20 13:54:00 +0800640 ret = clk_prepare_enable(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800641 if (ret)
Lothar Waßmannfeb397d2011-12-08 09:15:42 +0100642 return ret;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800643
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800644 ret = stmp_reset_block(mxs_dma->base);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800645 if (ret)
646 goto err_out;
647
Shawn Guoa580b8c2011-02-27 00:47:42 +0800648 /* enable apbh burst */
Shawn Guobb11fb62012-05-07 14:14:08 +0800649 if (dma_is_apbh(mxs_dma)) {
Shawn Guoa580b8c2011-02-27 00:47:42 +0800650 writel(BM_APBH_CTRL0_APB_BURST_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800651 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800652 writel(BM_APBH_CTRL0_APB_BURST8_EN,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800653 mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800654 }
655
656 /* enable irq for all the channels */
657 writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
Dong Aishengf5b7efc2012-05-04 20:12:15 +0800658 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800659
Shawn Guoa580b8c2011-02-27 00:47:42 +0800660err_out:
Linus Torvalds57f26852012-01-17 18:40:24 -0800661 clk_disable_unprepare(mxs_dma->clk);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800662 return ret;
663}
664
665static int __init mxs_dma_probe(struct platform_device *pdev)
666{
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800667 const struct platform_device_id *id_entry;
668 const struct of_device_id *of_id;
669 const struct mxs_dma_type *dma_type;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800670 struct mxs_dma_engine *mxs_dma;
671 struct resource *iores;
672 int ret, i;
673
674 mxs_dma = kzalloc(sizeof(*mxs_dma), GFP_KERNEL);
675 if (!mxs_dma)
676 return -ENOMEM;
677
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800678 of_id = of_match_device(mxs_dma_dt_ids, &pdev->dev);
679 if (of_id)
680 id_entry = of_id->data;
681 else
682 id_entry = platform_get_device_id(pdev);
683
684 dma_type = (struct mxs_dma_type *)id_entry->driver_data;
Shawn Guo8c920132012-05-10 06:23:26 +0800685 mxs_dma->type = dma_type->type;
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800686 mxs_dma->dev_id = dma_type->id;
Shawn Guoa580b8c2011-02-27 00:47:42 +0800687
688 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
689
690 if (!request_mem_region(iores->start, resource_size(iores),
691 pdev->name)) {
692 ret = -EBUSY;
693 goto err_request_region;
694 }
695
696 mxs_dma->base = ioremap(iores->start, resource_size(iores));
697 if (!mxs_dma->base) {
698 ret = -ENOMEM;
699 goto err_ioremap;
700 }
701
702 mxs_dma->clk = clk_get(&pdev->dev, NULL);
703 if (IS_ERR(mxs_dma->clk)) {
704 ret = PTR_ERR(mxs_dma->clk);
705 goto err_clk;
706 }
707
708 dma_cap_set(DMA_SLAVE, mxs_dma->dma_device.cap_mask);
709 dma_cap_set(DMA_CYCLIC, mxs_dma->dma_device.cap_mask);
710
711 INIT_LIST_HEAD(&mxs_dma->dma_device.channels);
712
713 /* Initialize channel parameters */
714 for (i = 0; i < MXS_DMA_CHANNELS; i++) {
715 struct mxs_dma_chan *mxs_chan = &mxs_dma->mxs_chans[i];
716
717 mxs_chan->mxs_dma = mxs_dma;
718 mxs_chan->chan.device = &mxs_dma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +0000719 dma_cookie_init(&mxs_chan->chan);
Shawn Guoa580b8c2011-02-27 00:47:42 +0800720
721 tasklet_init(&mxs_chan->tasklet, mxs_dma_tasklet,
722 (unsigned long) mxs_chan);
723
724
725 /* Add the channel to mxs_chan list */
726 list_add_tail(&mxs_chan->chan.device_node,
727 &mxs_dma->dma_device.channels);
728 }
729
730 ret = mxs_dma_init(mxs_dma);
731 if (ret)
732 goto err_init;
733
734 mxs_dma->dma_device.dev = &pdev->dev;
735
736 /* mxs_dma gets 65535 bytes maximum sg size */
737 mxs_dma->dma_device.dev->dma_parms = &mxs_dma->dma_parms;
738 dma_set_max_seg_size(mxs_dma->dma_device.dev, MAX_XFER_BYTES);
739
740 mxs_dma->dma_device.device_alloc_chan_resources = mxs_dma_alloc_chan_resources;
741 mxs_dma->dma_device.device_free_chan_resources = mxs_dma_free_chan_resources;
742 mxs_dma->dma_device.device_tx_status = mxs_dma_tx_status;
743 mxs_dma->dma_device.device_prep_slave_sg = mxs_dma_prep_slave_sg;
744 mxs_dma->dma_device.device_prep_dma_cyclic = mxs_dma_prep_dma_cyclic;
745 mxs_dma->dma_device.device_control = mxs_dma_control;
746 mxs_dma->dma_device.device_issue_pending = mxs_dma_issue_pending;
747
748 ret = dma_async_device_register(&mxs_dma->dma_device);
749 if (ret) {
750 dev_err(mxs_dma->dma_device.dev, "unable to register\n");
751 goto err_init;
752 }
753
754 dev_info(mxs_dma->dma_device.dev, "initialized\n");
755
756 return 0;
757
758err_init:
759 clk_put(mxs_dma->clk);
760err_clk:
761 iounmap(mxs_dma->base);
762err_ioremap:
763 release_mem_region(iores->start, resource_size(iores));
764err_request_region:
765 kfree(mxs_dma);
766 return ret;
767}
768
Shawn Guoa580b8c2011-02-27 00:47:42 +0800769static struct platform_driver mxs_dma_driver = {
770 .driver = {
771 .name = "mxs-dma",
Dong Aisheng90c9abc2012-05-04 20:12:17 +0800772 .of_match_table = mxs_dma_dt_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800773 },
Shawn Guo8c920132012-05-10 06:23:26 +0800774 .id_table = mxs_dma_ids,
Shawn Guoa580b8c2011-02-27 00:47:42 +0800775};
776
777static int __init mxs_dma_module_init(void)
778{
779 return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
780}
781subsys_initcall(mxs_dma_module_init);