dma: mxs-dma: Report correct residue for cyclic DMA
[cascardo/linux.git] / drivers / dma / mxs-dma.c
1 /*
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>
25 #include <linux/module.h>
26 #include <linux/stmp_device.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 #include <linux/of_dma.h>
30 #include <linux/list.h>
31
32 #include <asm/irq.h>
33
34 #include "dmaengine.h"
35
36 /*
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
42 #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)
44
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)
48 #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
53 /*
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)
61 #define HW_APBHX_CHn_BAR(d, n) \
62         (((dma_is_apbh(d) && apbh_is_old(d)) ? 0x070 : 0x130) + (n) * 0x70)
63
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
97 struct 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
107 #define CCW_BLOCK_SIZE  (4 * PAGE_SIZE)
108 #define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
109
110 struct 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;
115         unsigned int                    chan_irq;
116         struct mxs_dma_ccw              *ccw;
117         dma_addr_t                      ccw_phys;
118         int                             desc_count;
119         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
127 enum mxs_dma_devtype {
128         MXS_DMA_APBH,
129         MXS_DMA_APBX,
130 };
131
132 enum mxs_dma_id {
133         IMX23_DMA,
134         IMX28_DMA,
135 };
136
137 struct mxs_dma_engine {
138         enum mxs_dma_id                 dev_id;
139         enum mxs_dma_devtype            type;
140         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];
145         struct platform_device          *pdev;
146         unsigned int                    nr_channels;
147 };
148
149 struct mxs_dma_type {
150         enum mxs_dma_id id;
151         enum mxs_dma_devtype type;
152 };
153
154 static 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
170 static 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
188 static 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 };
195 MODULE_DEVICE_TABLE(of, mxs_dma_dt_ids);
196
197 static 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
202 static 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
207         if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
208                 writel(1 << (chan_id + BP_APBH_CTRL0_RESET_CHANNEL),
209                         mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
210         else
211                 writel(1 << (chan_id + BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL),
212                         mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
213 }
214
215 static 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,
222                 mxs_dma->base + HW_APBHX_CHn_NXTCMDAR(mxs_dma, chan_id));
223
224         /* write 1 to SEMA to kick off the channel */
225         writel(1, mxs_dma->base + HW_APBHX_CHn_SEMA(mxs_dma, chan_id));
226 }
227
228 static void mxs_dma_disable_chan(struct mxs_dma_chan *mxs_chan)
229 {
230         mxs_chan->status = DMA_COMPLETE;
231 }
232
233 static 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 */
239         if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
240                 writel(1 << chan_id,
241                         mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
242         else
243                 writel(1 << chan_id,
244                         mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_SET);
245
246         mxs_chan->status = DMA_PAUSED;
247 }
248
249 static 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 */
255         if (dma_is_apbh(mxs_dma) && apbh_is_old(mxs_dma))
256                 writel(1 << chan_id,
257                         mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_CLR);
258         else
259                 writel(1 << chan_id,
260                         mxs_dma->base + HW_APBHX_CHANNEL_CTRL + STMP_OFFSET_REG_CLR);
261
262         mxs_chan->status = DMA_IN_PROGRESS;
263 }
264
265 static dma_cookie_t mxs_dma_tx_submit(struct dma_async_tx_descriptor *tx)
266 {
267         return dma_cookie_assign(tx);
268 }
269
270 static 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
278 static 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
289 static irqreturn_t mxs_dma_int_handler(int irq, void *dev_id)
290 {
291         struct mxs_dma_engine *mxs_dma = dev_id;
292         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;
299
300         /* completion status */
301         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);
307
308         /* error status */
309         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);
322
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
326          * 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.
329          */
330         err -= err & completed;
331
332         mxs_chan = &mxs_dma->mxs_chans[chan];
333
334         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;
345         }
346
347         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
353         return IRQ_HANDLED;
354 }
355
356 static int mxs_dma_alloc_chan_resources(struct dma_chan *chan)
357 {
358         struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
359         struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
360         int ret;
361
362         mxs_chan->ccw = dma_alloc_coherent(mxs_dma->dma_device.dev,
363                                 CCW_BLOCK_SIZE, &mxs_chan->ccw_phys,
364                                 GFP_KERNEL);
365         if (!mxs_chan->ccw) {
366                 ret = -ENOMEM;
367                 goto err_alloc;
368         }
369
370         memset(mxs_chan->ccw, 0, CCW_BLOCK_SIZE);
371
372         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         }
378
379         ret = clk_prepare_enable(mxs_dma->clk);
380         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
393 err_clk:
394         free_irq(mxs_chan->chan_irq, mxs_dma);
395 err_irq:
396         dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
397                         mxs_chan->ccw, mxs_chan->ccw_phys);
398 err_alloc:
399         return ret;
400 }
401
402 static 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
411         dma_free_coherent(mxs_dma->dma_device.dev, CCW_BLOCK_SIZE,
412                         mxs_chan->ccw, mxs_chan->ccw_phys);
413
414         clk_disable_unprepare(mxs_dma->clk);
415 }
416
417 /*
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  */
439 static struct dma_async_tx_descriptor *mxs_dma_prep_slave_sg(
440                 struct dma_chan *chan, struct scatterlist *sgl,
441                 unsigned int sg_len, enum dma_transfer_direction direction,
442                 unsigned long flags, void *context)
443 {
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;
448         u32 i, j;
449         u32 *pio;
450         bool append = flags & DMA_PREP_INTERRUPT;
451         int idx = append ? mxs_chan->desc_count : 0;
452
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;
477         } else {
478                 idx = 0;
479         }
480
481         if (direction == DMA_TRANS_NONE) {
482                 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;
491                 if (flags & DMA_CTRL_ACK)
492                         ccw->bits |= CCW_WAIT4END;
493                 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) {
499                         if (sg_dma_len(sg) > MAX_XFER_BYTES) {
500                                 dev_err(mxs_dma->dma_device.dev, "maximum bytes for sg entry exceeded: %d > %d\n",
501                                                 sg_dma_len(sg), MAX_XFER_BYTES);
502                                 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;
509                         ccw->xfer_bytes = sg_dma_len(sg);
510
511                         ccw->bits = 0;
512                         ccw->bits |= CCW_CHAIN;
513                         ccw->bits |= CCW_HALT_ON_TERM;
514                         ccw->bits |= CCW_TERM_FLUSH;
515                         ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
516                                         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;
523                                 if (flags & DMA_CTRL_ACK)
524                                         ccw->bits |= CCW_WAIT4END;
525                         }
526                 }
527         }
528         mxs_chan->desc_count = idx;
529
530         return &mxs_chan->desc;
531
532 err_out:
533         mxs_chan->status = DMA_ERROR;
534         return NULL;
535 }
536
537 static struct dma_async_tx_descriptor *mxs_dma_prep_dma_cyclic(
538                 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
539                 size_t period_len, enum dma_transfer_direction direction,
540                 unsigned long flags, void *context)
541 {
542         struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
543         struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
544         u32 num_periods = buf_len / period_len;
545         u32 i = 0, buf = 0;
546
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;
583                 ccw->bits |= BF_CCW(direction == DMA_DEV_TO_MEM ?
584                                 MXS_DMA_CMD_WRITE : MXS_DMA_CMD_READ, COMMAND);
585
586                 dma_addr += period_len;
587                 buf += period_len;
588
589                 i++;
590         }
591         mxs_chan->desc_count = i;
592
593         return &mxs_chan->desc;
594
595 err_out:
596         mxs_chan->status = DMA_ERROR;
597         return NULL;
598 }
599
600 static 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:
608                 mxs_dma_reset_chan(mxs_chan);
609                 mxs_dma_disable_chan(mxs_chan);
610                 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
624 static 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);
628         struct mxs_dma_engine *mxs_dma = mxs_chan->mxs_dma;
629         u32 residue = 0;
630
631         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);
646
647         return mxs_chan->status;
648 }
649
650 static void mxs_dma_issue_pending(struct dma_chan *chan)
651 {
652         struct mxs_dma_chan *mxs_chan = to_mxs_dma_chan(chan);
653
654         mxs_dma_enable_chan(mxs_chan);
655 }
656
657 static int __init mxs_dma_init(struct mxs_dma_engine *mxs_dma)
658 {
659         int ret;
660
661         ret = clk_prepare_enable(mxs_dma->clk);
662         if (ret)
663                 return ret;
664
665         ret = stmp_reset_block(mxs_dma->base);
666         if (ret)
667                 goto err_out;
668
669         /* enable apbh burst */
670         if (dma_is_apbh(mxs_dma)) {
671                 writel(BM_APBH_CTRL0_APB_BURST_EN,
672                         mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
673                 writel(BM_APBH_CTRL0_APB_BURST8_EN,
674                         mxs_dma->base + HW_APBHX_CTRL0 + STMP_OFFSET_REG_SET);
675         }
676
677         /* enable irq for all the channels */
678         writel(MXS_DMA_CHANNELS_MASK << MXS_DMA_CHANNELS,
679                 mxs_dma->base + HW_APBHX_CTRL1 + STMP_OFFSET_REG_SET);
680
681 err_out:
682         clk_disable_unprepare(mxs_dma->clk);
683         return ret;
684 }
685
686 struct mxs_dma_filter_param {
687         struct device_node *of_node;
688         unsigned int chan_id;
689 };
690
691 static 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
713 static struct dma_chan *mxs_dma_xlate(struct of_phandle_args *dma_spec,
714                                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
732 static int __init mxs_dma_probe(struct platform_device *pdev)
733 {
734         struct device_node *np = pdev->dev.of_node;
735         const struct platform_device_id *id_entry;
736         const struct of_device_id *of_id;
737         const struct mxs_dma_type *dma_type;
738         struct mxs_dma_engine *mxs_dma;
739         struct resource *iores;
740         int ret, i;
741
742         mxs_dma = devm_kzalloc(&pdev->dev, sizeof(*mxs_dma), GFP_KERNEL);
743         if (!mxs_dma)
744                 return -ENOMEM;
745
746         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
752         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;
759         mxs_dma->type = dma_type->type;
760         mxs_dma->dev_id = dma_type->id;
761
762         iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
763         mxs_dma->base = devm_ioremap_resource(&pdev->dev, iores);
764         if (IS_ERR(mxs_dma->base))
765                 return PTR_ERR(mxs_dma->base);
766
767         mxs_dma->clk = devm_clk_get(&pdev->dev, NULL);
768         if (IS_ERR(mxs_dma->clk))
769                 return PTR_ERR(mxs_dma->clk);
770
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;
782                 dma_cookie_init(&mxs_chan->chan);
783
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)
795                 return ret;
796
797         mxs_dma->pdev = pdev;
798         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");
815                 return ret;
816         }
817
818         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
825         dev_info(mxs_dma->dma_device.dev, "initialized\n");
826
827         return 0;
828 }
829
830 static struct platform_driver mxs_dma_driver = {
831         .driver         = {
832                 .name   = "mxs-dma",
833                 .of_match_table = mxs_dma_dt_ids,
834         },
835         .id_table       = mxs_dma_ids,
836 };
837
838 static int __init mxs_dma_module_init(void)
839 {
840         return platform_driver_probe(&mxs_dma_driver, mxs_dma_probe);
841 }
842 subsys_initcall(mxs_dma_module_init);