2 * Copyright 2011 Freescale Semiconductor, Inc. All Rights Reserved.
4 * Refer to drivers/dma/imx-sdma.c
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.
11 #include <linux/init.h>
12 #include <linux/types.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>
28 #include <linux/of_device.h>
29 #include <linux/of_dma.h>
30 #include <linux/list.h>
34 #include "dmaengine.h"
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.
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)
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
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.
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 #define HW_APBX_CHn_DEBUG1(d, n) (0x150 + (n) * 0x70)
66 * ccw bits definitions
71 * NAND_LOCK: 4 (1) - not implemented
72 * NAND_WAIT4READY: 5 (1) - not implemented
75 * HALT_ON_TERMINATE: 8 (1)
76 * TERMINATE_FLUSH: 9 (1)
77 * RESERVED: 10..11 (2)
80 #define BP_CCW_COMMAND 0
81 #define BM_CCW_COMMAND (3 << 0)
82 #define CCW_CHAIN (1 << 2)
83 #define CCW_IRQ (1 << 3)
84 #define CCW_DEC_SEM (1 << 6)
85 #define CCW_WAIT4END (1 << 7)
86 #define CCW_HALT_ON_TERM (1 << 8)
87 #define CCW_TERM_FLUSH (1 << 9)
88 #define BP_CCW_PIO_NUM 12
89 #define BM_CCW_PIO_NUM (0xf << 12)
91 #define BF_CCW(value, field) (((value) << BP_CCW_##field) & BM_CCW_##field)
93 #define MXS_DMA_CMD_NO_XFER 0
94 #define MXS_DMA_CMD_WRITE 1
95 #define MXS_DMA_CMD_READ 2
96 #define MXS_DMA_CMD_DMA_SENSE 3 /* not implemented */
102 #define MAX_XFER_BYTES 0xff00
104 #define MXS_PIO_WORDS 16
105 u32 pio_words
[MXS_PIO_WORDS
];
108 #define CCW_BLOCK_SIZE (4 * PAGE_SIZE)
109 #define NUM_CCW (int)(CCW_BLOCK_SIZE / sizeof(struct mxs_dma_ccw))
111 struct mxs_dma_chan
{
112 struct mxs_dma_engine
*mxs_dma
;
113 struct dma_chan chan
;
114 struct dma_async_tx_descriptor desc
;
115 struct tasklet_struct tasklet
;
116 unsigned int chan_irq
;
117 struct mxs_dma_ccw
*ccw
;
120 enum dma_status status
;
123 #define MXS_DMA_SG_LOOP (1 << 0)
124 #define MXS_DMA_USE_SEMAPHORE (1 << 1)
127 #define MXS_DMA_CHANNELS 16
128 #define MXS_DMA_CHANNELS_MASK 0xffff
130 enum mxs_dma_devtype
{
140 struct mxs_dma_engine
{
141 enum mxs_dma_id dev_id
;
142 enum mxs_dma_devtype type
;
145 struct dma_device dma_device
;
146 struct device_dma_parameters dma_parms
;
147 struct mxs_dma_chan mxs_chans
[MXS_DMA_CHANNELS
];
148 struct platform_device
*pdev
;
149 unsigned int nr_channels
;
152 struct mxs_dma_type
{
154 enum mxs_dma_devtype type
;
157 static struct mxs_dma_type mxs_dma_types
[] = {
160 .type
= MXS_DMA_APBH
,
163 .type
= MXS_DMA_APBX
,
166 .type
= MXS_DMA_APBH
,
169 .type
= MXS_DMA_APBX
,
173 static const struct platform_device_id mxs_dma_ids
[] = {
175 .name
= "imx23-dma-apbh",
176 .driver_data
= (kernel_ulong_t
) &mxs_dma_types
[0],
178 .name
= "imx23-dma-apbx",
179 .driver_data
= (kernel_ulong_t
) &mxs_dma_types
[1],
181 .name
= "imx28-dma-apbh",
182 .driver_data
= (kernel_ulong_t
) &mxs_dma_types
[2],
184 .name
= "imx28-dma-apbx",
185 .driver_data
= (kernel_ulong_t
) &mxs_dma_types
[3],
191 static const struct of_device_id mxs_dma_dt_ids
[] = {
192 { .compatible
= "fsl,imx23-dma-apbh", .data
= &mxs_dma_ids
[0], },
193 { .compatible
= "fsl,imx23-dma-apbx", .data
= &mxs_dma_ids
[1], },
194 { .compatible
= "fsl,imx28-dma-apbh", .data
= &mxs_dma_ids
[2], },
195 { .compatible
= "fsl,imx28-dma-apbx", .data
= &mxs_dma_ids
[3], },
198 MODULE_DEVICE_TABLE(of
, mxs_dma_dt_ids
);
200 static struct mxs_dma_chan
*to_mxs_dma_chan(struct dma_chan
*chan
)
202 return container_of(chan
, struct mxs_dma_chan
, chan
);
205 static void mxs_dma_reset_chan(struct dma_chan
*chan
)
207 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
208 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
209 int chan_id
= mxs_chan
->chan
.chan_id
;
212 * mxs dma channel resets can cause a channel stall. To recover from a
213 * channel stall, we have to reset the whole DMA engine. To avoid this,
214 * we use cyclic DMA with semaphores, that are enhanced in
215 * mxs_dma_int_handler. To reset the channel, we can simply stop writing
216 * into the semaphore counter.
218 if (mxs_chan
->flags
& MXS_DMA_USE_SEMAPHORE
&&
219 mxs_chan
->flags
& MXS_DMA_SG_LOOP
) {
220 mxs_chan
->reset
= true;
221 } else if (dma_is_apbh(mxs_dma
) && apbh_is_old(mxs_dma
)) {
222 writel(1 << (chan_id
+ BP_APBH_CTRL0_RESET_CHANNEL
),
223 mxs_dma
->base
+ HW_APBHX_CTRL0
+ STMP_OFFSET_REG_SET
);
225 unsigned long elapsed
= 0;
226 const unsigned long max_wait
= 50000; /* 50ms */
227 void __iomem
*reg_dbg1
= mxs_dma
->base
+
228 HW_APBX_CHn_DEBUG1(mxs_dma
, chan_id
);
231 * On i.MX28 APBX, the DMA channel can stop working if we reset
232 * the channel while it is in READ_FLUSH (0x08) state.
233 * We wait here until we leave the state. Then we trigger the
234 * reset. Waiting a maximum of 50ms, the kernel shouldn't crash
237 while ((readl(reg_dbg1
) & 0xf) == 0x8 && elapsed
< max_wait
) {
242 if (elapsed
>= max_wait
)
243 dev_err(&mxs_chan
->mxs_dma
->pdev
->dev
,
244 "Failed waiting for the DMA channel %d to leave state READ_FLUSH, trying to reset channel in READ_FLUSH state now\n",
247 writel(1 << (chan_id
+ BP_APBHX_CHANNEL_CTRL_RESET_CHANNEL
),
248 mxs_dma
->base
+ HW_APBHX_CHANNEL_CTRL
+ STMP_OFFSET_REG_SET
);
251 mxs_chan
->status
= DMA_COMPLETE
;
254 static void mxs_dma_enable_chan(struct dma_chan
*chan
)
256 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
257 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
258 int chan_id
= mxs_chan
->chan
.chan_id
;
260 /* set cmd_addr up */
261 writel(mxs_chan
->ccw_phys
,
262 mxs_dma
->base
+ HW_APBHX_CHn_NXTCMDAR(mxs_dma
, chan_id
));
264 /* write 1 to SEMA to kick off the channel */
265 if (mxs_chan
->flags
& MXS_DMA_USE_SEMAPHORE
&&
266 mxs_chan
->flags
& MXS_DMA_SG_LOOP
) {
267 /* A cyclic DMA consists of at least 2 segments, so initialize
268 * the semaphore with 2 so we have enough time to add 1 to the
269 * semaphore if we need to */
270 writel(2, mxs_dma
->base
+ HW_APBHX_CHn_SEMA(mxs_dma
, chan_id
));
272 writel(1, mxs_dma
->base
+ HW_APBHX_CHn_SEMA(mxs_dma
, chan_id
));
274 mxs_chan
->reset
= false;
277 static void mxs_dma_disable_chan(struct dma_chan
*chan
)
279 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
281 mxs_chan
->status
= DMA_COMPLETE
;
284 static int mxs_dma_pause_chan(struct dma_chan
*chan
)
286 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
287 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
288 int chan_id
= mxs_chan
->chan
.chan_id
;
290 /* freeze the channel */
291 if (dma_is_apbh(mxs_dma
) && apbh_is_old(mxs_dma
))
293 mxs_dma
->base
+ HW_APBHX_CTRL0
+ STMP_OFFSET_REG_SET
);
296 mxs_dma
->base
+ HW_APBHX_CHANNEL_CTRL
+ STMP_OFFSET_REG_SET
);
298 mxs_chan
->status
= DMA_PAUSED
;
302 static int mxs_dma_resume_chan(struct dma_chan
*chan
)
304 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
305 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
306 int chan_id
= mxs_chan
->chan
.chan_id
;
308 /* unfreeze the channel */
309 if (dma_is_apbh(mxs_dma
) && apbh_is_old(mxs_dma
))
311 mxs_dma
->base
+ HW_APBHX_CTRL0
+ STMP_OFFSET_REG_CLR
);
314 mxs_dma
->base
+ HW_APBHX_CHANNEL_CTRL
+ STMP_OFFSET_REG_CLR
);
316 mxs_chan
->status
= DMA_IN_PROGRESS
;
320 static dma_cookie_t
mxs_dma_tx_submit(struct dma_async_tx_descriptor
*tx
)
322 return dma_cookie_assign(tx
);
325 static void mxs_dma_tasklet(unsigned long data
)
327 struct mxs_dma_chan
*mxs_chan
= (struct mxs_dma_chan
*) data
;
329 dmaengine_desc_get_callback_invoke(&mxs_chan
->desc
, NULL
);
332 static int mxs_dma_irq_to_chan(struct mxs_dma_engine
*mxs_dma
, int irq
)
336 for (i
= 0; i
!= mxs_dma
->nr_channels
; ++i
)
337 if (mxs_dma
->mxs_chans
[i
].chan_irq
== irq
)
343 static irqreturn_t
mxs_dma_int_handler(int irq
, void *dev_id
)
345 struct mxs_dma_engine
*mxs_dma
= dev_id
;
346 struct mxs_dma_chan
*mxs_chan
;
349 int chan
= mxs_dma_irq_to_chan(mxs_dma
, irq
);
354 /* completion status */
355 completed
= readl(mxs_dma
->base
+ HW_APBHX_CTRL1
);
356 completed
= (completed
>> chan
) & 0x1;
358 /* Clear interrupt */
360 mxs_dma
->base
+ HW_APBHX_CTRL1
+ STMP_OFFSET_REG_CLR
);
363 err
= readl(mxs_dma
->base
+ HW_APBHX_CTRL2
);
364 err
&= (1 << (MXS_DMA_CHANNELS
+ chan
)) | (1 << chan
);
367 * error status bit is in the upper 16 bits, error irq bit in the lower
368 * 16 bits. We transform it into a simpler error code:
369 * err: 0x00 = no error, 0x01 = TERMINATION, 0x02 = BUS_ERROR
371 err
= (err
>> (MXS_DMA_CHANNELS
+ chan
)) + (err
>> chan
);
373 /* Clear error irq */
375 mxs_dma
->base
+ HW_APBHX_CTRL2
+ STMP_OFFSET_REG_CLR
);
378 * When both completion and error of termination bits set at the
379 * same time, we do not take it as an error. IOW, it only becomes
380 * an error we need to handle here in case of either it's a bus
381 * error or a termination error with no completion. 0x01 is termination
382 * error, so we can subtract err & completed to get the real error case.
384 err
-= err
& completed
;
386 mxs_chan
= &mxs_dma
->mxs_chans
[chan
];
389 dev_dbg(mxs_dma
->dma_device
.dev
,
390 "%s: error in channel %d\n", __func__
,
392 mxs_chan
->status
= DMA_ERROR
;
393 mxs_dma_reset_chan(&mxs_chan
->chan
);
394 } else if (mxs_chan
->status
!= DMA_COMPLETE
) {
395 if (mxs_chan
->flags
& MXS_DMA_SG_LOOP
) {
396 mxs_chan
->status
= DMA_IN_PROGRESS
;
397 if (mxs_chan
->flags
& MXS_DMA_USE_SEMAPHORE
)
398 writel(1, mxs_dma
->base
+
399 HW_APBHX_CHn_SEMA(mxs_dma
, chan
));
401 mxs_chan
->status
= DMA_COMPLETE
;
405 if (mxs_chan
->status
== DMA_COMPLETE
) {
408 dma_cookie_complete(&mxs_chan
->desc
);
411 /* schedule tasklet on this channel */
412 tasklet_schedule(&mxs_chan
->tasklet
);
417 static int mxs_dma_alloc_chan_resources(struct dma_chan
*chan
)
419 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
420 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
423 mxs_chan
->ccw
= dma_zalloc_coherent(mxs_dma
->dma_device
.dev
,
425 &mxs_chan
->ccw_phys
, GFP_KERNEL
);
426 if (!mxs_chan
->ccw
) {
431 ret
= request_irq(mxs_chan
->chan_irq
, mxs_dma_int_handler
,
432 0, "mxs-dma", mxs_dma
);
436 ret
= clk_prepare_enable(mxs_dma
->clk
);
440 mxs_dma_reset_chan(chan
);
442 dma_async_tx_descriptor_init(&mxs_chan
->desc
, chan
);
443 mxs_chan
->desc
.tx_submit
= mxs_dma_tx_submit
;
445 /* the descriptor is ready */
446 async_tx_ack(&mxs_chan
->desc
);
451 free_irq(mxs_chan
->chan_irq
, mxs_dma
);
453 dma_free_coherent(mxs_dma
->dma_device
.dev
, CCW_BLOCK_SIZE
,
454 mxs_chan
->ccw
, mxs_chan
->ccw_phys
);
459 static void mxs_dma_free_chan_resources(struct dma_chan
*chan
)
461 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
462 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
464 mxs_dma_disable_chan(chan
);
466 free_irq(mxs_chan
->chan_irq
, mxs_dma
);
468 dma_free_coherent(mxs_dma
->dma_device
.dev
, CCW_BLOCK_SIZE
,
469 mxs_chan
->ccw
, mxs_chan
->ccw_phys
);
471 clk_disable_unprepare(mxs_dma
->clk
);
475 * How to use the flags for ->device_prep_slave_sg() :
476 * [1] If there is only one DMA command in the DMA chain, the code should be:
478 * ->device_prep_slave_sg(DMA_CTRL_ACK);
480 * [2] If there are two DMA commands in the DMA chain, the code should be
482 * ->device_prep_slave_sg(0);
484 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
486 * [3] If there are more than two DMA commands in the DMA chain, the code
489 * ->device_prep_slave_sg(0); // First
491 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT [| DMA_CTRL_ACK]);
493 * ->device_prep_slave_sg(DMA_PREP_INTERRUPT | DMA_CTRL_ACK); // Last
496 static struct dma_async_tx_descriptor
*mxs_dma_prep_slave_sg(
497 struct dma_chan
*chan
, struct scatterlist
*sgl
,
498 unsigned int sg_len
, enum dma_transfer_direction direction
,
499 unsigned long flags
, void *context
)
501 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
502 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
503 struct mxs_dma_ccw
*ccw
;
504 struct scatterlist
*sg
;
507 bool append
= flags
& DMA_PREP_INTERRUPT
;
508 int idx
= append
? mxs_chan
->desc_count
: 0;
510 if (mxs_chan
->status
== DMA_IN_PROGRESS
&& !append
)
513 if (sg_len
+ (append
? idx
: 0) > NUM_CCW
) {
514 dev_err(mxs_dma
->dma_device
.dev
,
515 "maximum number of sg exceeded: %d > %d\n",
520 mxs_chan
->status
= DMA_IN_PROGRESS
;
524 * If the sg is prepared with append flag set, the sg
525 * will be appended to the last prepared sg.
529 ccw
= &mxs_chan
->ccw
[idx
- 1];
530 ccw
->next
= mxs_chan
->ccw_phys
+ sizeof(*ccw
) * idx
;
531 ccw
->bits
|= CCW_CHAIN
;
532 ccw
->bits
&= ~CCW_IRQ
;
533 ccw
->bits
&= ~CCW_DEC_SEM
;
538 if (direction
== DMA_TRANS_NONE
) {
539 ccw
= &mxs_chan
->ccw
[idx
++];
542 for (j
= 0; j
< sg_len
;)
543 ccw
->pio_words
[j
++] = *pio
++;
546 ccw
->bits
|= CCW_IRQ
;
547 ccw
->bits
|= CCW_DEC_SEM
;
548 if (flags
& DMA_CTRL_ACK
)
549 ccw
->bits
|= CCW_WAIT4END
;
550 ccw
->bits
|= CCW_HALT_ON_TERM
;
551 ccw
->bits
|= CCW_TERM_FLUSH
;
552 ccw
->bits
|= BF_CCW(sg_len
, PIO_NUM
);
553 ccw
->bits
|= BF_CCW(MXS_DMA_CMD_NO_XFER
, COMMAND
);
555 for_each_sg(sgl
, sg
, sg_len
, i
) {
556 if (sg_dma_len(sg
) > MAX_XFER_BYTES
) {
557 dev_err(mxs_dma
->dma_device
.dev
, "maximum bytes for sg entry exceeded: %d > %d\n",
558 sg_dma_len(sg
), MAX_XFER_BYTES
);
562 ccw
= &mxs_chan
->ccw
[idx
++];
564 ccw
->next
= mxs_chan
->ccw_phys
+ sizeof(*ccw
) * idx
;
565 ccw
->bufaddr
= sg
->dma_address
;
566 ccw
->xfer_bytes
= sg_dma_len(sg
);
569 ccw
->bits
|= CCW_CHAIN
;
570 ccw
->bits
|= CCW_HALT_ON_TERM
;
571 ccw
->bits
|= CCW_TERM_FLUSH
;
572 ccw
->bits
|= BF_CCW(direction
== DMA_DEV_TO_MEM
?
573 MXS_DMA_CMD_WRITE
: MXS_DMA_CMD_READ
,
576 if (i
+ 1 == sg_len
) {
577 ccw
->bits
&= ~CCW_CHAIN
;
578 ccw
->bits
|= CCW_IRQ
;
579 ccw
->bits
|= CCW_DEC_SEM
;
580 if (flags
& DMA_CTRL_ACK
)
581 ccw
->bits
|= CCW_WAIT4END
;
585 mxs_chan
->desc_count
= idx
;
587 return &mxs_chan
->desc
;
590 mxs_chan
->status
= DMA_ERROR
;
594 static struct dma_async_tx_descriptor
*mxs_dma_prep_dma_cyclic(
595 struct dma_chan
*chan
, dma_addr_t dma_addr
, size_t buf_len
,
596 size_t period_len
, enum dma_transfer_direction direction
,
599 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
600 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
601 u32 num_periods
= buf_len
/ period_len
;
604 if (mxs_chan
->status
== DMA_IN_PROGRESS
)
607 mxs_chan
->status
= DMA_IN_PROGRESS
;
608 mxs_chan
->flags
|= MXS_DMA_SG_LOOP
;
609 mxs_chan
->flags
|= MXS_DMA_USE_SEMAPHORE
;
611 if (num_periods
> NUM_CCW
) {
612 dev_err(mxs_dma
->dma_device
.dev
,
613 "maximum number of sg exceeded: %d > %d\n",
614 num_periods
, NUM_CCW
);
618 if (period_len
> MAX_XFER_BYTES
) {
619 dev_err(mxs_dma
->dma_device
.dev
,
620 "maximum period size exceeded: %d > %d\n",
621 period_len
, MAX_XFER_BYTES
);
625 while (buf
< buf_len
) {
626 struct mxs_dma_ccw
*ccw
= &mxs_chan
->ccw
[i
];
628 if (i
+ 1 == num_periods
)
629 ccw
->next
= mxs_chan
->ccw_phys
;
631 ccw
->next
= mxs_chan
->ccw_phys
+ sizeof(*ccw
) * (i
+ 1);
633 ccw
->bufaddr
= dma_addr
;
634 ccw
->xfer_bytes
= period_len
;
637 ccw
->bits
|= CCW_CHAIN
;
638 ccw
->bits
|= CCW_IRQ
;
639 ccw
->bits
|= CCW_HALT_ON_TERM
;
640 ccw
->bits
|= CCW_TERM_FLUSH
;
641 ccw
->bits
|= CCW_DEC_SEM
;
642 ccw
->bits
|= BF_CCW(direction
== DMA_DEV_TO_MEM
?
643 MXS_DMA_CMD_WRITE
: MXS_DMA_CMD_READ
, COMMAND
);
645 dma_addr
+= period_len
;
650 mxs_chan
->desc_count
= i
;
652 return &mxs_chan
->desc
;
655 mxs_chan
->status
= DMA_ERROR
;
659 static int mxs_dma_terminate_all(struct dma_chan
*chan
)
661 mxs_dma_reset_chan(chan
);
662 mxs_dma_disable_chan(chan
);
667 static enum dma_status
mxs_dma_tx_status(struct dma_chan
*chan
,
668 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
670 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
671 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
674 if (mxs_chan
->status
== DMA_IN_PROGRESS
&&
675 mxs_chan
->flags
& MXS_DMA_SG_LOOP
) {
676 struct mxs_dma_ccw
*last_ccw
;
679 last_ccw
= &mxs_chan
->ccw
[mxs_chan
->desc_count
- 1];
680 residue
= last_ccw
->xfer_bytes
+ last_ccw
->bufaddr
;
682 bar
= readl(mxs_dma
->base
+
683 HW_APBHX_CHn_BAR(mxs_dma
, chan
->chan_id
));
687 dma_set_tx_state(txstate
, chan
->completed_cookie
, chan
->cookie
,
690 return mxs_chan
->status
;
693 static int __init
mxs_dma_init(struct mxs_dma_engine
*mxs_dma
)
697 ret
= clk_prepare_enable(mxs_dma
->clk
);
701 ret
= stmp_reset_block(mxs_dma
->base
);
705 /* enable apbh burst */
706 if (dma_is_apbh(mxs_dma
)) {
707 writel(BM_APBH_CTRL0_APB_BURST_EN
,
708 mxs_dma
->base
+ HW_APBHX_CTRL0
+ STMP_OFFSET_REG_SET
);
709 writel(BM_APBH_CTRL0_APB_BURST8_EN
,
710 mxs_dma
->base
+ HW_APBHX_CTRL0
+ STMP_OFFSET_REG_SET
);
713 /* enable irq for all the channels */
714 writel(MXS_DMA_CHANNELS_MASK
<< MXS_DMA_CHANNELS
,
715 mxs_dma
->base
+ HW_APBHX_CTRL1
+ STMP_OFFSET_REG_SET
);
718 clk_disable_unprepare(mxs_dma
->clk
);
722 struct mxs_dma_filter_param
{
723 struct device_node
*of_node
;
724 unsigned int chan_id
;
727 static bool mxs_dma_filter_fn(struct dma_chan
*chan
, void *fn_param
)
729 struct mxs_dma_filter_param
*param
= fn_param
;
730 struct mxs_dma_chan
*mxs_chan
= to_mxs_dma_chan(chan
);
731 struct mxs_dma_engine
*mxs_dma
= mxs_chan
->mxs_dma
;
734 if (mxs_dma
->dma_device
.dev
->of_node
!= param
->of_node
)
737 if (chan
->chan_id
!= param
->chan_id
)
740 chan_irq
= platform_get_irq(mxs_dma
->pdev
, param
->chan_id
);
744 mxs_chan
->chan_irq
= chan_irq
;
749 static struct dma_chan
*mxs_dma_xlate(struct of_phandle_args
*dma_spec
,
750 struct of_dma
*ofdma
)
752 struct mxs_dma_engine
*mxs_dma
= ofdma
->of_dma_data
;
753 dma_cap_mask_t mask
= mxs_dma
->dma_device
.cap_mask
;
754 struct mxs_dma_filter_param param
;
756 if (dma_spec
->args_count
!= 1)
759 param
.of_node
= ofdma
->of_node
;
760 param
.chan_id
= dma_spec
->args
[0];
762 if (param
.chan_id
>= mxs_dma
->nr_channels
)
765 return dma_request_channel(mask
, mxs_dma_filter_fn
, ¶m
);
768 static int __init
mxs_dma_probe(struct platform_device
*pdev
)
770 struct device_node
*np
= pdev
->dev
.of_node
;
771 const struct platform_device_id
*id_entry
;
772 const struct of_device_id
*of_id
;
773 const struct mxs_dma_type
*dma_type
;
774 struct mxs_dma_engine
*mxs_dma
;
775 struct resource
*iores
;
778 mxs_dma
= devm_kzalloc(&pdev
->dev
, sizeof(*mxs_dma
), GFP_KERNEL
);
782 ret
= of_property_read_u32(np
, "dma-channels", &mxs_dma
->nr_channels
);
784 dev_err(&pdev
->dev
, "failed to read dma-channels\n");
788 of_id
= of_match_device(mxs_dma_dt_ids
, &pdev
->dev
);
790 id_entry
= of_id
->data
;
792 id_entry
= platform_get_device_id(pdev
);
794 dma_type
= (struct mxs_dma_type
*)id_entry
->driver_data
;
795 mxs_dma
->type
= dma_type
->type
;
796 mxs_dma
->dev_id
= dma_type
->id
;
798 iores
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
799 mxs_dma
->base
= devm_ioremap_resource(&pdev
->dev
, iores
);
800 if (IS_ERR(mxs_dma
->base
))
801 return PTR_ERR(mxs_dma
->base
);
803 mxs_dma
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
804 if (IS_ERR(mxs_dma
->clk
))
805 return PTR_ERR(mxs_dma
->clk
);
807 dma_cap_set(DMA_SLAVE
, mxs_dma
->dma_device
.cap_mask
);
808 dma_cap_set(DMA_CYCLIC
, mxs_dma
->dma_device
.cap_mask
);
810 INIT_LIST_HEAD(&mxs_dma
->dma_device
.channels
);
812 /* Initialize channel parameters */
813 for (i
= 0; i
< MXS_DMA_CHANNELS
; i
++) {
814 struct mxs_dma_chan
*mxs_chan
= &mxs_dma
->mxs_chans
[i
];
816 mxs_chan
->mxs_dma
= mxs_dma
;
817 mxs_chan
->chan
.device
= &mxs_dma
->dma_device
;
818 dma_cookie_init(&mxs_chan
->chan
);
820 tasklet_init(&mxs_chan
->tasklet
, mxs_dma_tasklet
,
821 (unsigned long) mxs_chan
);
824 /* Add the channel to mxs_chan list */
825 list_add_tail(&mxs_chan
->chan
.device_node
,
826 &mxs_dma
->dma_device
.channels
);
829 ret
= mxs_dma_init(mxs_dma
);
833 mxs_dma
->pdev
= pdev
;
834 mxs_dma
->dma_device
.dev
= &pdev
->dev
;
836 /* mxs_dma gets 65535 bytes maximum sg size */
837 mxs_dma
->dma_device
.dev
->dma_parms
= &mxs_dma
->dma_parms
;
838 dma_set_max_seg_size(mxs_dma
->dma_device
.dev
, MAX_XFER_BYTES
);
840 mxs_dma
->dma_device
.device_alloc_chan_resources
= mxs_dma_alloc_chan_resources
;
841 mxs_dma
->dma_device
.device_free_chan_resources
= mxs_dma_free_chan_resources
;
842 mxs_dma
->dma_device
.device_tx_status
= mxs_dma_tx_status
;
843 mxs_dma
->dma_device
.device_prep_slave_sg
= mxs_dma_prep_slave_sg
;
844 mxs_dma
->dma_device
.device_prep_dma_cyclic
= mxs_dma_prep_dma_cyclic
;
845 mxs_dma
->dma_device
.device_pause
= mxs_dma_pause_chan
;
846 mxs_dma
->dma_device
.device_resume
= mxs_dma_resume_chan
;
847 mxs_dma
->dma_device
.device_terminate_all
= mxs_dma_terminate_all
;
848 mxs_dma
->dma_device
.src_addr_widths
= BIT(DMA_SLAVE_BUSWIDTH_4_BYTES
);
849 mxs_dma
->dma_device
.dst_addr_widths
= BIT(DMA_SLAVE_BUSWIDTH_4_BYTES
);
850 mxs_dma
->dma_device
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
851 mxs_dma
->dma_device
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
852 mxs_dma
->dma_device
.device_issue_pending
= mxs_dma_enable_chan
;
854 ret
= dma_async_device_register(&mxs_dma
->dma_device
);
856 dev_err(mxs_dma
->dma_device
.dev
, "unable to register\n");
860 ret
= of_dma_controller_register(np
, mxs_dma_xlate
, mxs_dma
);
862 dev_err(mxs_dma
->dma_device
.dev
,
863 "failed to register controller\n");
864 dma_async_device_unregister(&mxs_dma
->dma_device
);
867 dev_info(mxs_dma
->dma_device
.dev
, "initialized\n");
872 static struct platform_driver mxs_dma_driver
= {
875 .of_match_table
= mxs_dma_dt_ids
,
877 .id_table
= mxs_dma_ids
,
880 static int __init
mxs_dma_module_init(void)
882 return platform_driver_probe(&mxs_dma_driver
, mxs_dma_probe
);
884 subsys_initcall(mxs_dma_module_init
);