2 * Copyright (C) 2017 Spreadtrum Communications Inc.
4 * SPDX-License-Identifier: GPL-2.0
8 #include <linux/dma-mapping.h>
9 #include <linux/dma/sprd-dma.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_dma.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
24 #define SPRD_DMA_CHN_REG_OFFSET 0x1000
25 #define SPRD_DMA_CHN_REG_LENGTH 0x40
26 #define SPRD_DMA_MEMCPY_MIN_SIZE 64
28 /* DMA global registers definition */
29 #define SPRD_DMA_GLB_PAUSE 0x0
30 #define SPRD_DMA_GLB_FRAG_WAIT 0x4
31 #define SPRD_DMA_GLB_REQ_PEND0_EN 0x8
32 #define SPRD_DMA_GLB_REQ_PEND1_EN 0xc
33 #define SPRD_DMA_GLB_INT_RAW_STS 0x10
34 #define SPRD_DMA_GLB_INT_MSK_STS 0x14
35 #define SPRD_DMA_GLB_REQ_STS 0x18
36 #define SPRD_DMA_GLB_CHN_EN_STS 0x1c
37 #define SPRD_DMA_GLB_DEBUG_STS 0x20
38 #define SPRD_DMA_GLB_ARB_SEL_STS 0x24
39 #define SPRD_DMA_GLB_REQ_UID(uid) (0x4 * ((uid) - 1))
40 #define SPRD_DMA_GLB_REQ_UID_OFFSET 0x2000
42 /* DMA channel registers definition */
43 #define SPRD_DMA_CHN_PAUSE 0x0
44 #define SPRD_DMA_CHN_REQ 0x4
45 #define SPRD_DMA_CHN_CFG 0x8
46 #define SPRD_DMA_CHN_INTC 0xc
47 #define SPRD_DMA_CHN_SRC_ADDR 0x10
48 #define SPRD_DMA_CHN_DES_ADDR 0x14
49 #define SPRD_DMA_CHN_FRG_LEN 0x18
50 #define SPRD_DMA_CHN_BLK_LEN 0x1c
51 #define SPRD_DMA_CHN_TRSC_LEN 0x20
52 #define SPRD_DMA_CHN_TRSF_STEP 0x24
53 #define SPRD_DMA_CHN_WARP_PTR 0x28
54 #define SPRD_DMA_CHN_WARP_TO 0x2c
55 #define SPRD_DMA_CHN_LLIST_PTR 0x30
56 #define SPRD_DMA_CHN_FRAG_STEP 0x34
57 #define SPRD_DMA_CHN_SRC_BLK_STEP 0x38
58 #define SPRD_DMA_CHN_DES_BLK_STEP 0x3c
60 /* SPRD_DMA_CHN_INTC register definition */
61 #define SPRD_DMA_INT_MASK GENMASK(4, 0)
62 #define SPRD_DMA_INT_CLR_OFFSET 24
63 #define SPRD_DMA_FRAG_INT_EN BIT(0)
64 #define SPRD_DMA_BLK_INT_EN BIT(1)
65 #define SPRD_DMA_TRANS_INT_EN BIT(2)
66 #define SPRD_DMA_LIST_INT_EN BIT(3)
67 #define SPRD_DMA_CFG_ERR_INT_EN BIT(4)
69 /* SPRD_DMA_CHN_CFG register definition */
70 #define SPRD_DMA_CHN_EN BIT(0)
71 #define SPRD_DMA_WAIT_BDONE_OFFSET 24
72 #define SPRD_DMA_DONOT_WAIT_BDONE 1
74 /* SPRD_DMA_CHN_REQ register definition */
75 #define SPRD_DMA_REQ_EN BIT(0)
77 /* SPRD_DMA_CHN_PAUSE register definition */
78 #define SPRD_DMA_PAUSE_EN BIT(0)
79 #define SPRD_DMA_PAUSE_STS BIT(2)
80 #define SPRD_DMA_PAUSE_CNT 0x2000
82 /* DMA_CHN_WARP_* register definition */
83 #define SPRD_DMA_HIGH_ADDR_MASK GENMASK(31, 28)
84 #define SPRD_DMA_LOW_ADDR_MASK GENMASK(31, 0)
85 #define SPRD_DMA_HIGH_ADDR_OFFSET 4
87 /* SPRD_DMA_CHN_INTC register definition */
88 #define SPRD_DMA_FRAG_INT_STS BIT(16)
89 #define SPRD_DMA_BLK_INT_STS BIT(17)
90 #define SPRD_DMA_TRSC_INT_STS BIT(18)
91 #define SPRD_DMA_LIST_INT_STS BIT(19)
92 #define SPRD_DMA_CFGERR_INT_STS BIT(20)
93 #define SPRD_DMA_CHN_INT_STS \
94 (SPRD_DMA_FRAG_INT_STS | SPRD_DMA_BLK_INT_STS | \
95 SPRD_DMA_TRSC_INT_STS | SPRD_DMA_LIST_INT_STS | \
96 SPRD_DMA_CFGERR_INT_STS)
98 /* SPRD_DMA_CHN_FRG_LEN register definition */
99 #define SPRD_DMA_SRC_DATAWIDTH_OFFSET 30
100 #define SPRD_DMA_DES_DATAWIDTH_OFFSET 28
101 #define SPRD_DMA_SWT_MODE_OFFSET 26
102 #define SPRD_DMA_REQ_MODE_OFFSET 24
103 #define SPRD_DMA_REQ_MODE_MASK GENMASK(1, 0)
104 #define SPRD_DMA_FIX_SEL_OFFSET 21
105 #define SPRD_DMA_FIX_EN_OFFSET 20
106 #define SPRD_DMA_LLIST_END_OFFSET 19
107 #define SPRD_DMA_FRG_LEN_MASK GENMASK(16, 0)
109 /* SPRD_DMA_CHN_BLK_LEN register definition */
110 #define SPRD_DMA_BLK_LEN_MASK GENMASK(16, 0)
112 /* SPRD_DMA_CHN_TRSC_LEN register definition */
113 #define SPRD_DMA_TRSC_LEN_MASK GENMASK(27, 0)
115 /* SPRD_DMA_CHN_TRSF_STEP register definition */
116 #define SPRD_DMA_DEST_TRSF_STEP_OFFSET 16
117 #define SPRD_DMA_SRC_TRSF_STEP_OFFSET 0
118 #define SPRD_DMA_TRSF_STEP_MASK GENMASK(15, 0)
120 /* define the DMA transfer step type */
121 #define SPRD_DMA_NONE_STEP 0
122 #define SPRD_DMA_BYTE_STEP 1
123 #define SPRD_DMA_SHORT_STEP 2
124 #define SPRD_DMA_WORD_STEP 4
125 #define SPRD_DMA_DWORD_STEP 8
127 #define SPRD_DMA_SOFTWARE_UID 0
129 /* dma data width values */
130 enum sprd_dma_datawidth
{
131 SPRD_DMA_DATAWIDTH_1_BYTE
,
132 SPRD_DMA_DATAWIDTH_2_BYTES
,
133 SPRD_DMA_DATAWIDTH_4_BYTES
,
134 SPRD_DMA_DATAWIDTH_8_BYTES
,
137 /* dma channel hardware configuration */
138 struct sprd_dma_chn_hw
{
157 /* dma request description */
158 struct sprd_dma_desc
{
159 struct virt_dma_desc vd
;
160 struct sprd_dma_chn_hw chn_hw
;
163 /* dma channel description */
164 struct sprd_dma_chn
{
165 struct virt_dma_chan vc
;
166 void __iomem
*chn_base
;
167 struct dma_slave_config slave_cfg
;
170 struct sprd_dma_desc
*cur_desc
;
173 /* SPRD dma device */
174 struct sprd_dma_dev
{
175 struct dma_device dma_dev
;
176 void __iomem
*glb_base
;
178 struct clk
*ashb_clk
;
181 struct sprd_dma_chn channels
[0];
184 static void sprd_dma_free_desc(struct virt_dma_desc
*vd
);
185 static bool sprd_dma_filter_fn(struct dma_chan
*chan
, void *param
);
186 static struct of_dma_filter_info sprd_dma_info
= {
187 .filter_fn
= sprd_dma_filter_fn
,
190 static inline struct sprd_dma_chn
*to_sprd_dma_chan(struct dma_chan
*c
)
192 return container_of(c
, struct sprd_dma_chn
, vc
.chan
);
195 static inline struct sprd_dma_dev
*to_sprd_dma_dev(struct dma_chan
*c
)
197 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(c
);
199 return container_of(schan
, struct sprd_dma_dev
, channels
[c
->chan_id
]);
202 static inline struct sprd_dma_desc
*to_sprd_dma_desc(struct virt_dma_desc
*vd
)
204 return container_of(vd
, struct sprd_dma_desc
, vd
);
207 static void sprd_dma_chn_update(struct sprd_dma_chn
*schan
, u32 reg
,
210 u32 orig
= readl(schan
->chn_base
+ reg
);
213 tmp
= (orig
& ~mask
) | val
;
214 writel(tmp
, schan
->chn_base
+ reg
);
217 static int sprd_dma_enable(struct sprd_dma_dev
*sdev
)
221 ret
= clk_prepare_enable(sdev
->clk
);
226 * The ashb_clk is optional and only for AGCP DMA controller, so we
227 * need add one condition to check if the ashb_clk need enable.
229 if (!IS_ERR(sdev
->ashb_clk
))
230 ret
= clk_prepare_enable(sdev
->ashb_clk
);
235 static void sprd_dma_disable(struct sprd_dma_dev
*sdev
)
237 clk_disable_unprepare(sdev
->clk
);
240 * Need to check if we need disable the optional ashb_clk for AGCP DMA.
242 if (!IS_ERR(sdev
->ashb_clk
))
243 clk_disable_unprepare(sdev
->ashb_clk
);
246 static void sprd_dma_set_uid(struct sprd_dma_chn
*schan
)
248 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(&schan
->vc
.chan
);
249 u32 dev_id
= schan
->dev_id
;
251 if (dev_id
!= SPRD_DMA_SOFTWARE_UID
) {
252 u32 uid_offset
= SPRD_DMA_GLB_REQ_UID_OFFSET
+
253 SPRD_DMA_GLB_REQ_UID(dev_id
);
255 writel(schan
->chn_num
+ 1, sdev
->glb_base
+ uid_offset
);
259 static void sprd_dma_unset_uid(struct sprd_dma_chn
*schan
)
261 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(&schan
->vc
.chan
);
262 u32 dev_id
= schan
->dev_id
;
264 if (dev_id
!= SPRD_DMA_SOFTWARE_UID
) {
265 u32 uid_offset
= SPRD_DMA_GLB_REQ_UID_OFFSET
+
266 SPRD_DMA_GLB_REQ_UID(dev_id
);
268 writel(0, sdev
->glb_base
+ uid_offset
);
272 static void sprd_dma_clear_int(struct sprd_dma_chn
*schan
)
274 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_INTC
,
275 SPRD_DMA_INT_MASK
<< SPRD_DMA_INT_CLR_OFFSET
,
276 SPRD_DMA_INT_MASK
<< SPRD_DMA_INT_CLR_OFFSET
);
279 static void sprd_dma_enable_chn(struct sprd_dma_chn
*schan
)
281 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_CFG
, SPRD_DMA_CHN_EN
,
285 static void sprd_dma_disable_chn(struct sprd_dma_chn
*schan
)
287 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_CFG
, SPRD_DMA_CHN_EN
, 0);
290 static void sprd_dma_soft_request(struct sprd_dma_chn
*schan
)
292 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_REQ
, SPRD_DMA_REQ_EN
,
296 static void sprd_dma_pause_resume(struct sprd_dma_chn
*schan
, bool enable
)
298 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(&schan
->vc
.chan
);
299 u32 pause
, timeout
= SPRD_DMA_PAUSE_CNT
;
302 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_PAUSE
,
303 SPRD_DMA_PAUSE_EN
, SPRD_DMA_PAUSE_EN
);
306 pause
= readl(schan
->chn_base
+ SPRD_DMA_CHN_PAUSE
);
307 if (pause
& SPRD_DMA_PAUSE_STS
)
311 } while (--timeout
> 0);
314 dev_warn(sdev
->dma_dev
.dev
,
315 "pause dma controller timeout\n");
317 sprd_dma_chn_update(schan
, SPRD_DMA_CHN_PAUSE
,
318 SPRD_DMA_PAUSE_EN
, 0);
322 static void sprd_dma_stop_and_disable(struct sprd_dma_chn
*schan
)
324 u32 cfg
= readl(schan
->chn_base
+ SPRD_DMA_CHN_CFG
);
326 if (!(cfg
& SPRD_DMA_CHN_EN
))
329 sprd_dma_pause_resume(schan
, true);
330 sprd_dma_disable_chn(schan
);
333 static unsigned long sprd_dma_get_dst_addr(struct sprd_dma_chn
*schan
)
335 unsigned long addr
, addr_high
;
337 addr
= readl(schan
->chn_base
+ SPRD_DMA_CHN_DES_ADDR
);
338 addr_high
= readl(schan
->chn_base
+ SPRD_DMA_CHN_WARP_TO
) &
339 SPRD_DMA_HIGH_ADDR_MASK
;
341 return addr
| (addr_high
<< SPRD_DMA_HIGH_ADDR_OFFSET
);
344 static enum sprd_dma_int_type
sprd_dma_get_int_type(struct sprd_dma_chn
*schan
)
346 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(&schan
->vc
.chan
);
347 u32 intc_sts
= readl(schan
->chn_base
+ SPRD_DMA_CHN_INTC
) &
348 SPRD_DMA_CHN_INT_STS
;
351 case SPRD_DMA_CFGERR_INT_STS
:
352 return SPRD_DMA_CFGERR_INT
;
354 case SPRD_DMA_LIST_INT_STS
:
355 return SPRD_DMA_LIST_INT
;
357 case SPRD_DMA_TRSC_INT_STS
:
358 return SPRD_DMA_TRANS_INT
;
360 case SPRD_DMA_BLK_INT_STS
:
361 return SPRD_DMA_BLK_INT
;
363 case SPRD_DMA_FRAG_INT_STS
:
364 return SPRD_DMA_FRAG_INT
;
367 dev_warn(sdev
->dma_dev
.dev
, "incorrect dma interrupt type\n");
368 return SPRD_DMA_NO_INT
;
372 static enum sprd_dma_req_mode
sprd_dma_get_req_type(struct sprd_dma_chn
*schan
)
374 u32 frag_reg
= readl(schan
->chn_base
+ SPRD_DMA_CHN_FRG_LEN
);
376 return (frag_reg
>> SPRD_DMA_REQ_MODE_OFFSET
) & SPRD_DMA_REQ_MODE_MASK
;
379 static void sprd_dma_set_chn_config(struct sprd_dma_chn
*schan
,
380 struct sprd_dma_desc
*sdesc
)
382 struct sprd_dma_chn_hw
*cfg
= &sdesc
->chn_hw
;
384 writel(cfg
->pause
, schan
->chn_base
+ SPRD_DMA_CHN_PAUSE
);
385 writel(cfg
->cfg
, schan
->chn_base
+ SPRD_DMA_CHN_CFG
);
386 writel(cfg
->intc
, schan
->chn_base
+ SPRD_DMA_CHN_INTC
);
387 writel(cfg
->src_addr
, schan
->chn_base
+ SPRD_DMA_CHN_SRC_ADDR
);
388 writel(cfg
->des_addr
, schan
->chn_base
+ SPRD_DMA_CHN_DES_ADDR
);
389 writel(cfg
->frg_len
, schan
->chn_base
+ SPRD_DMA_CHN_FRG_LEN
);
390 writel(cfg
->blk_len
, schan
->chn_base
+ SPRD_DMA_CHN_BLK_LEN
);
391 writel(cfg
->trsc_len
, schan
->chn_base
+ SPRD_DMA_CHN_TRSC_LEN
);
392 writel(cfg
->trsf_step
, schan
->chn_base
+ SPRD_DMA_CHN_TRSF_STEP
);
393 writel(cfg
->wrap_ptr
, schan
->chn_base
+ SPRD_DMA_CHN_WARP_PTR
);
394 writel(cfg
->wrap_to
, schan
->chn_base
+ SPRD_DMA_CHN_WARP_TO
);
395 writel(cfg
->llist_ptr
, schan
->chn_base
+ SPRD_DMA_CHN_LLIST_PTR
);
396 writel(cfg
->frg_step
, schan
->chn_base
+ SPRD_DMA_CHN_FRAG_STEP
);
397 writel(cfg
->src_blk_step
, schan
->chn_base
+ SPRD_DMA_CHN_SRC_BLK_STEP
);
398 writel(cfg
->des_blk_step
, schan
->chn_base
+ SPRD_DMA_CHN_DES_BLK_STEP
);
399 writel(cfg
->req
, schan
->chn_base
+ SPRD_DMA_CHN_REQ
);
402 static void sprd_dma_start(struct sprd_dma_chn
*schan
)
404 struct virt_dma_desc
*vd
= vchan_next_desc(&schan
->vc
);
410 schan
->cur_desc
= to_sprd_dma_desc(vd
);
413 * Copy the DMA configuration from DMA descriptor to this hardware
416 sprd_dma_set_chn_config(schan
, schan
->cur_desc
);
417 sprd_dma_set_uid(schan
);
418 sprd_dma_enable_chn(schan
);
420 if (schan
->dev_id
== SPRD_DMA_SOFTWARE_UID
)
421 sprd_dma_soft_request(schan
);
424 static void sprd_dma_stop(struct sprd_dma_chn
*schan
)
426 sprd_dma_stop_and_disable(schan
);
427 sprd_dma_unset_uid(schan
);
428 sprd_dma_clear_int(schan
);
431 static bool sprd_dma_check_trans_done(struct sprd_dma_desc
*sdesc
,
432 enum sprd_dma_int_type int_type
,
433 enum sprd_dma_req_mode req_mode
)
435 if (int_type
== SPRD_DMA_NO_INT
)
438 if (int_type
>= req_mode
+ 1)
444 static irqreturn_t
dma_irq_handle(int irq
, void *dev_id
)
446 struct sprd_dma_dev
*sdev
= (struct sprd_dma_dev
*)dev_id
;
447 u32 irq_status
= readl(sdev
->glb_base
+ SPRD_DMA_GLB_INT_MSK_STS
);
448 struct sprd_dma_chn
*schan
;
449 struct sprd_dma_desc
*sdesc
;
450 enum sprd_dma_req_mode req_type
;
451 enum sprd_dma_int_type int_type
;
452 bool trans_done
= false;
456 i
= __ffs(irq_status
);
457 irq_status
&= (irq_status
- 1);
458 schan
= &sdev
->channels
[i
];
460 spin_lock(&schan
->vc
.lock
);
461 int_type
= sprd_dma_get_int_type(schan
);
462 req_type
= sprd_dma_get_req_type(schan
);
463 sprd_dma_clear_int(schan
);
465 sdesc
= schan
->cur_desc
;
467 /* Check if the dma request descriptor is done. */
468 trans_done
= sprd_dma_check_trans_done(sdesc
, int_type
,
470 if (trans_done
== true) {
471 vchan_cookie_complete(&sdesc
->vd
);
472 schan
->cur_desc
= NULL
;
473 sprd_dma_start(schan
);
475 spin_unlock(&schan
->vc
.lock
);
481 static int sprd_dma_alloc_chan_resources(struct dma_chan
*chan
)
483 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
486 ret
= pm_runtime_get_sync(chan
->device
->dev
);
490 schan
->dev_id
= SPRD_DMA_SOFTWARE_UID
;
494 static void sprd_dma_free_chan_resources(struct dma_chan
*chan
)
496 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
497 struct virt_dma_desc
*cur_vd
= NULL
;
500 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
502 cur_vd
= &schan
->cur_desc
->vd
;
504 sprd_dma_stop(schan
);
505 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
508 sprd_dma_free_desc(cur_vd
);
510 vchan_free_chan_resources(&schan
->vc
);
511 pm_runtime_put(chan
->device
->dev
);
514 static enum dma_status
sprd_dma_tx_status(struct dma_chan
*chan
,
516 struct dma_tx_state
*txstate
)
518 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
519 struct virt_dma_desc
*vd
;
524 ret
= dma_cookie_status(chan
, cookie
, txstate
);
525 if (ret
== DMA_COMPLETE
|| !txstate
)
528 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
529 vd
= vchan_find_desc(&schan
->vc
, cookie
);
531 struct sprd_dma_desc
*sdesc
= to_sprd_dma_desc(vd
);
532 struct sprd_dma_chn_hw
*hw
= &sdesc
->chn_hw
;
534 if (hw
->trsc_len
> 0)
536 else if (hw
->blk_len
> 0)
538 else if (hw
->frg_len
> 0)
542 } else if (schan
->cur_desc
&& schan
->cur_desc
->vd
.tx
.cookie
== cookie
) {
543 pos
= sprd_dma_get_dst_addr(schan
);
547 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
549 dma_set_residue(txstate
, pos
);
553 static void sprd_dma_issue_pending(struct dma_chan
*chan
)
555 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
558 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
559 if (vchan_issue_pending(&schan
->vc
) && !schan
->cur_desc
)
560 sprd_dma_start(schan
);
561 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
564 static int sprd_dma_get_datawidth(enum dma_slave_buswidth buswidth
)
567 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
568 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
569 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
570 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
571 return ffs(buswidth
) - 1;
578 static int sprd_dma_get_step(enum dma_slave_buswidth buswidth
)
581 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
582 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
583 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
584 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
592 static int sprd_dma_fill_desc(struct dma_chan
*chan
,
593 struct sprd_dma_desc
*sdesc
,
594 dma_addr_t src
, dma_addr_t dst
, u32 len
,
595 enum dma_transfer_direction dir
,
597 struct dma_slave_config
*slave_cfg
)
599 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(chan
);
600 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
601 struct sprd_dma_chn_hw
*hw
= &sdesc
->chn_hw
;
602 u32 req_mode
= (flags
>> SPRD_DMA_REQ_SHIFT
) & SPRD_DMA_REQ_MODE_MASK
;
603 u32 int_mode
= flags
& SPRD_DMA_INT_MASK
;
604 int src_datawidth
, dst_datawidth
, src_step
, dst_step
;
605 u32 temp
, fix_mode
= 0, fix_en
= 0;
607 if (dir
== DMA_MEM_TO_DEV
) {
608 src_step
= sprd_dma_get_step(slave_cfg
->src_addr_width
);
610 dev_err(sdev
->dma_dev
.dev
, "invalid source step\n");
613 dst_step
= SPRD_DMA_NONE_STEP
;
615 dst_step
= sprd_dma_get_step(slave_cfg
->dst_addr_width
);
617 dev_err(sdev
->dma_dev
.dev
, "invalid destination step\n");
620 src_step
= SPRD_DMA_NONE_STEP
;
623 src_datawidth
= sprd_dma_get_datawidth(slave_cfg
->src_addr_width
);
624 if (src_datawidth
< 0) {
625 dev_err(sdev
->dma_dev
.dev
, "invalid source datawidth\n");
626 return src_datawidth
;
629 dst_datawidth
= sprd_dma_get_datawidth(slave_cfg
->dst_addr_width
);
630 if (dst_datawidth
< 0) {
631 dev_err(sdev
->dma_dev
.dev
, "invalid destination datawidth\n");
632 return dst_datawidth
;
635 if (slave_cfg
->slave_id
)
636 schan
->dev_id
= slave_cfg
->slave_id
;
638 hw
->cfg
= SPRD_DMA_DONOT_WAIT_BDONE
<< SPRD_DMA_WAIT_BDONE_OFFSET
;
641 * wrap_ptr and wrap_to will save the high 4 bits source address and
642 * destination address.
644 hw
->wrap_ptr
= (src
>> SPRD_DMA_HIGH_ADDR_OFFSET
) & SPRD_DMA_HIGH_ADDR_MASK
;
645 hw
->wrap_to
= (dst
>> SPRD_DMA_HIGH_ADDR_OFFSET
) & SPRD_DMA_HIGH_ADDR_MASK
;
646 hw
->src_addr
= src
& SPRD_DMA_LOW_ADDR_MASK
;
647 hw
->des_addr
= dst
& SPRD_DMA_LOW_ADDR_MASK
;
650 * If the src step and dst step both are 0 or both are not 0, that means
651 * we can not enable the fix mode. If one is 0 and another one is not,
652 * we can enable the fix mode.
654 if ((src_step
!= 0 && dst_step
!= 0) || (src_step
| dst_step
) == 0) {
664 hw
->intc
= int_mode
| SPRD_DMA_CFG_ERR_INT_EN
;
666 temp
= src_datawidth
<< SPRD_DMA_SRC_DATAWIDTH_OFFSET
;
667 temp
|= dst_datawidth
<< SPRD_DMA_DES_DATAWIDTH_OFFSET
;
668 temp
|= req_mode
<< SPRD_DMA_REQ_MODE_OFFSET
;
669 temp
|= fix_mode
<< SPRD_DMA_FIX_SEL_OFFSET
;
670 temp
|= fix_en
<< SPRD_DMA_FIX_EN_OFFSET
;
671 temp
|= slave_cfg
->src_maxburst
& SPRD_DMA_FRG_LEN_MASK
;
674 hw
->blk_len
= slave_cfg
->src_maxburst
& SPRD_DMA_BLK_LEN_MASK
;
675 hw
->trsc_len
= len
& SPRD_DMA_TRSC_LEN_MASK
;
677 temp
= (dst_step
& SPRD_DMA_TRSF_STEP_MASK
) << SPRD_DMA_DEST_TRSF_STEP_OFFSET
;
678 temp
|= (src_step
& SPRD_DMA_TRSF_STEP_MASK
) << SPRD_DMA_SRC_TRSF_STEP_OFFSET
;
679 hw
->trsf_step
= temp
;
682 hw
->src_blk_step
= 0;
683 hw
->des_blk_step
= 0;
687 static struct dma_async_tx_descriptor
*
688 sprd_dma_prep_dma_memcpy(struct dma_chan
*chan
, dma_addr_t dest
, dma_addr_t src
,
689 size_t len
, unsigned long flags
)
691 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
692 struct sprd_dma_desc
*sdesc
;
693 struct sprd_dma_chn_hw
*hw
;
694 enum sprd_dma_datawidth datawidth
;
697 sdesc
= kzalloc(sizeof(*sdesc
), GFP_NOWAIT
);
703 hw
->cfg
= SPRD_DMA_DONOT_WAIT_BDONE
<< SPRD_DMA_WAIT_BDONE_OFFSET
;
704 hw
->intc
= SPRD_DMA_TRANS_INT
| SPRD_DMA_CFG_ERR_INT_EN
;
705 hw
->src_addr
= src
& SPRD_DMA_LOW_ADDR_MASK
;
706 hw
->des_addr
= dest
& SPRD_DMA_LOW_ADDR_MASK
;
707 hw
->wrap_ptr
= (src
>> SPRD_DMA_HIGH_ADDR_OFFSET
) &
708 SPRD_DMA_HIGH_ADDR_MASK
;
709 hw
->wrap_to
= (dest
>> SPRD_DMA_HIGH_ADDR_OFFSET
) &
710 SPRD_DMA_HIGH_ADDR_MASK
;
712 if (IS_ALIGNED(len
, 8)) {
713 datawidth
= SPRD_DMA_DATAWIDTH_8_BYTES
;
714 step
= SPRD_DMA_DWORD_STEP
;
715 } else if (IS_ALIGNED(len
, 4)) {
716 datawidth
= SPRD_DMA_DATAWIDTH_4_BYTES
;
717 step
= SPRD_DMA_WORD_STEP
;
718 } else if (IS_ALIGNED(len
, 2)) {
719 datawidth
= SPRD_DMA_DATAWIDTH_2_BYTES
;
720 step
= SPRD_DMA_SHORT_STEP
;
722 datawidth
= SPRD_DMA_DATAWIDTH_1_BYTE
;
723 step
= SPRD_DMA_BYTE_STEP
;
726 temp
= datawidth
<< SPRD_DMA_SRC_DATAWIDTH_OFFSET
;
727 temp
|= datawidth
<< SPRD_DMA_DES_DATAWIDTH_OFFSET
;
728 temp
|= SPRD_DMA_TRANS_REQ
<< SPRD_DMA_REQ_MODE_OFFSET
;
729 temp
|= len
& SPRD_DMA_FRG_LEN_MASK
;
732 hw
->blk_len
= len
& SPRD_DMA_BLK_LEN_MASK
;
733 hw
->trsc_len
= len
& SPRD_DMA_TRSC_LEN_MASK
;
735 temp
= (step
& SPRD_DMA_TRSF_STEP_MASK
) << SPRD_DMA_DEST_TRSF_STEP_OFFSET
;
736 temp
|= (step
& SPRD_DMA_TRSF_STEP_MASK
) << SPRD_DMA_SRC_TRSF_STEP_OFFSET
;
737 hw
->trsf_step
= temp
;
739 return vchan_tx_prep(&schan
->vc
, &sdesc
->vd
, flags
);
742 static struct dma_async_tx_descriptor
*
743 sprd_dma_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
744 unsigned int sglen
, enum dma_transfer_direction dir
,
745 unsigned long flags
, void *context
)
747 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
748 struct dma_slave_config
*slave_cfg
= &schan
->slave_cfg
;
749 dma_addr_t src
= 0, dst
= 0;
750 struct sprd_dma_desc
*sdesc
;
751 struct scatterlist
*sg
;
755 /* TODO: now we only support one sg for each DMA configuration. */
756 if (!is_slave_direction(dir
) || sglen
> 1)
759 sdesc
= kzalloc(sizeof(*sdesc
), GFP_NOWAIT
);
763 for_each_sg(sgl
, sg
, sglen
, i
) {
764 len
= sg_dma_len(sg
);
766 if (dir
== DMA_MEM_TO_DEV
) {
767 src
= sg_dma_address(sg
);
768 dst
= slave_cfg
->dst_addr
;
770 src
= slave_cfg
->src_addr
;
771 dst
= sg_dma_address(sg
);
775 ret
= sprd_dma_fill_desc(chan
, sdesc
, src
, dst
, len
, dir
, flags
,
782 return vchan_tx_prep(&schan
->vc
, &sdesc
->vd
, flags
);
785 static int sprd_dma_slave_config(struct dma_chan
*chan
,
786 struct dma_slave_config
*config
)
788 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
789 struct dma_slave_config
*slave_cfg
= &schan
->slave_cfg
;
791 if (!is_slave_direction(config
->direction
))
794 memcpy(slave_cfg
, config
, sizeof(*config
));
798 static int sprd_dma_pause(struct dma_chan
*chan
)
800 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
803 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
804 sprd_dma_pause_resume(schan
, true);
805 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
810 static int sprd_dma_resume(struct dma_chan
*chan
)
812 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
815 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
816 sprd_dma_pause_resume(schan
, false);
817 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
822 static int sprd_dma_terminate_all(struct dma_chan
*chan
)
824 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
825 struct virt_dma_desc
*cur_vd
= NULL
;
829 spin_lock_irqsave(&schan
->vc
.lock
, flags
);
831 cur_vd
= &schan
->cur_desc
->vd
;
833 sprd_dma_stop(schan
);
835 vchan_get_all_descriptors(&schan
->vc
, &head
);
836 spin_unlock_irqrestore(&schan
->vc
.lock
, flags
);
839 sprd_dma_free_desc(cur_vd
);
841 vchan_dma_desc_free_list(&schan
->vc
, &head
);
845 static void sprd_dma_free_desc(struct virt_dma_desc
*vd
)
847 struct sprd_dma_desc
*sdesc
= to_sprd_dma_desc(vd
);
852 static bool sprd_dma_filter_fn(struct dma_chan
*chan
, void *param
)
854 struct sprd_dma_chn
*schan
= to_sprd_dma_chan(chan
);
855 struct sprd_dma_dev
*sdev
= to_sprd_dma_dev(&schan
->vc
.chan
);
856 u32 req
= *(u32
*)param
;
858 if (req
< sdev
->total_chns
)
859 return req
== schan
->chn_num
+ 1;
864 static int sprd_dma_probe(struct platform_device
*pdev
)
866 struct device_node
*np
= pdev
->dev
.of_node
;
867 struct sprd_dma_dev
*sdev
;
868 struct sprd_dma_chn
*dma_chn
;
869 struct resource
*res
;
873 ret
= device_property_read_u32(&pdev
->dev
, "#dma-channels", &chn_count
);
875 dev_err(&pdev
->dev
, "get dma channels count failed\n");
879 sdev
= devm_kzalloc(&pdev
->dev
,
880 struct_size(sdev
, channels
, chn_count
),
885 sdev
->clk
= devm_clk_get(&pdev
->dev
, "enable");
886 if (IS_ERR(sdev
->clk
)) {
887 dev_err(&pdev
->dev
, "get enable clock failed\n");
888 return PTR_ERR(sdev
->clk
);
891 /* ashb clock is optional for AGCP DMA */
892 sdev
->ashb_clk
= devm_clk_get(&pdev
->dev
, "ashb_eb");
893 if (IS_ERR(sdev
->ashb_clk
))
894 dev_warn(&pdev
->dev
, "no optional ashb eb clock\n");
897 * We have three DMA controllers: AP DMA, AON DMA and AGCP DMA. For AGCP
898 * DMA controller, it can or do not request the irq, which will save
899 * system power without resuming system by DMA interrupts if AGCP DMA
900 * does not request the irq. Thus the DMA interrupts property should
903 sdev
->irq
= platform_get_irq(pdev
, 0);
905 ret
= devm_request_irq(&pdev
->dev
, sdev
->irq
, dma_irq_handle
,
906 0, "sprd_dma", (void *)sdev
);
908 dev_err(&pdev
->dev
, "request dma irq failed\n");
912 dev_warn(&pdev
->dev
, "no interrupts for the dma controller\n");
915 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
916 sdev
->glb_base
= devm_ioremap_resource(&pdev
->dev
, res
);
917 if (IS_ERR(sdev
->glb_base
))
918 return PTR_ERR(sdev
->glb_base
);
920 dma_cap_set(DMA_MEMCPY
, sdev
->dma_dev
.cap_mask
);
921 sdev
->total_chns
= chn_count
;
922 sdev
->dma_dev
.chancnt
= chn_count
;
923 INIT_LIST_HEAD(&sdev
->dma_dev
.channels
);
924 INIT_LIST_HEAD(&sdev
->dma_dev
.global_node
);
925 sdev
->dma_dev
.dev
= &pdev
->dev
;
926 sdev
->dma_dev
.device_alloc_chan_resources
= sprd_dma_alloc_chan_resources
;
927 sdev
->dma_dev
.device_free_chan_resources
= sprd_dma_free_chan_resources
;
928 sdev
->dma_dev
.device_tx_status
= sprd_dma_tx_status
;
929 sdev
->dma_dev
.device_issue_pending
= sprd_dma_issue_pending
;
930 sdev
->dma_dev
.device_prep_dma_memcpy
= sprd_dma_prep_dma_memcpy
;
931 sdev
->dma_dev
.device_prep_slave_sg
= sprd_dma_prep_slave_sg
;
932 sdev
->dma_dev
.device_config
= sprd_dma_slave_config
;
933 sdev
->dma_dev
.device_pause
= sprd_dma_pause
;
934 sdev
->dma_dev
.device_resume
= sprd_dma_resume
;
935 sdev
->dma_dev
.device_terminate_all
= sprd_dma_terminate_all
;
937 for (i
= 0; i
< chn_count
; i
++) {
938 dma_chn
= &sdev
->channels
[i
];
939 dma_chn
->chn_num
= i
;
940 dma_chn
->cur_desc
= NULL
;
941 /* get each channel's registers base address. */
942 dma_chn
->chn_base
= sdev
->glb_base
+ SPRD_DMA_CHN_REG_OFFSET
+
943 SPRD_DMA_CHN_REG_LENGTH
* i
;
945 dma_chn
->vc
.desc_free
= sprd_dma_free_desc
;
946 vchan_init(&dma_chn
->vc
, &sdev
->dma_dev
);
949 platform_set_drvdata(pdev
, sdev
);
950 ret
= sprd_dma_enable(sdev
);
954 pm_runtime_set_active(&pdev
->dev
);
955 pm_runtime_enable(&pdev
->dev
);
957 ret
= pm_runtime_get_sync(&pdev
->dev
);
961 ret
= dma_async_device_register(&sdev
->dma_dev
);
963 dev_err(&pdev
->dev
, "register dma device failed:%d\n", ret
);
967 sprd_dma_info
.dma_cap
= sdev
->dma_dev
.cap_mask
;
968 ret
= of_dma_controller_register(np
, of_dma_simple_xlate
,
971 goto err_of_register
;
973 pm_runtime_put(&pdev
->dev
);
977 dma_async_device_unregister(&sdev
->dma_dev
);
979 pm_runtime_put_noidle(&pdev
->dev
);
980 pm_runtime_disable(&pdev
->dev
);
982 sprd_dma_disable(sdev
);
986 static int sprd_dma_remove(struct platform_device
*pdev
)
988 struct sprd_dma_dev
*sdev
= platform_get_drvdata(pdev
);
989 struct sprd_dma_chn
*c
, *cn
;
992 ret
= pm_runtime_get_sync(&pdev
->dev
);
996 /* explicitly free the irq */
998 devm_free_irq(&pdev
->dev
, sdev
->irq
, sdev
);
1000 list_for_each_entry_safe(c
, cn
, &sdev
->dma_dev
.channels
,
1001 vc
.chan
.device_node
) {
1002 list_del(&c
->vc
.chan
.device_node
);
1003 tasklet_kill(&c
->vc
.task
);
1006 of_dma_controller_free(pdev
->dev
.of_node
);
1007 dma_async_device_unregister(&sdev
->dma_dev
);
1008 sprd_dma_disable(sdev
);
1010 pm_runtime_put_noidle(&pdev
->dev
);
1011 pm_runtime_disable(&pdev
->dev
);
1015 static const struct of_device_id sprd_dma_match
[] = {
1016 { .compatible
= "sprd,sc9860-dma", },
1020 static int __maybe_unused
sprd_dma_runtime_suspend(struct device
*dev
)
1022 struct sprd_dma_dev
*sdev
= dev_get_drvdata(dev
);
1024 sprd_dma_disable(sdev
);
1028 static int __maybe_unused
sprd_dma_runtime_resume(struct device
*dev
)
1030 struct sprd_dma_dev
*sdev
= dev_get_drvdata(dev
);
1033 ret
= sprd_dma_enable(sdev
);
1035 dev_err(sdev
->dma_dev
.dev
, "enable dma failed\n");
1040 static const struct dev_pm_ops sprd_dma_pm_ops
= {
1041 SET_RUNTIME_PM_OPS(sprd_dma_runtime_suspend
,
1042 sprd_dma_runtime_resume
,
1046 static struct platform_driver sprd_dma_driver
= {
1047 .probe
= sprd_dma_probe
,
1048 .remove
= sprd_dma_remove
,
1051 .of_match_table
= sprd_dma_match
,
1052 .pm
= &sprd_dma_pm_ops
,
1055 module_platform_driver(sprd_dma_driver
);
1057 MODULE_LICENSE("GPL v2");
1058 MODULE_DESCRIPTION("DMA driver for Spreadtrum");
1059 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
1060 MODULE_ALIAS("platform:sprd-dma");