2 * Copyright (c) 2013 - 2015 Linaro Ltd.
3 * Copyright (c) 2013 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 #include <linux/sched.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmapool.h>
13 #include <linux/dmaengine.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/of_device.h>
23 #include <linux/clk.h>
24 #include <linux/of_dma.h>
28 #define DRIVER_NAME "k3-dma"
29 #define DMA_MAX_SIZE 0x1ffc
30 #define DMA_CYCLIC_MAX_PERIOD 0x1000
31 #define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
38 #define INT_TC1_MASK 0x18
39 #define INT_TC2_MASK 0x1c
40 #define INT_ERR1_MASK 0x20
41 #define INT_ERR2_MASK 0x24
42 #define INT_TC1_RAW 0x600
43 #define INT_TC2_RAW 0x608
44 #define INT_ERR1_RAW 0x610
45 #define INT_ERR2_RAW 0x618
48 #define CX_CUR_CNT 0x704
56 #define AXI_CFG_DEFAULT 0x201201
58 #define CX_LLI_CHAIN_EN 0x2
60 #define CX_CFG_NODEIRQ BIT(1)
61 #define CX_CFG_MEM2PER (0x1 << 2)
62 #define CX_CFG_PER2MEM (0x2 << 2)
63 #define CX_CFG_SRCINCR (0x1 << 31)
64 #define CX_CFG_DSTINCR (0x1 << 30)
75 struct k3_dma_desc_sw
{
76 struct virt_dma_desc vd
;
77 dma_addr_t desc_hw_lli
;
80 struct k3_desc_hw
*desc_hw
;
87 struct virt_dma_chan vc
;
88 struct k3_dma_phy
*phy
;
89 struct list_head node
;
90 enum dma_transfer_direction dir
;
92 enum dma_status status
;
99 struct k3_dma_chan
*vchan
;
100 struct k3_dma_desc_sw
*ds_run
;
101 struct k3_dma_desc_sw
*ds_done
;
105 struct dma_device slave
;
107 struct tasklet_struct task
;
109 struct list_head chan_pending
;
110 struct k3_dma_phy
*phy
;
111 struct k3_dma_chan
*chans
;
113 struct dma_pool
*pool
;
119 #define to_k3_dma(dmadev) container_of(dmadev, struct k3_dma_dev, slave)
121 static struct k3_dma_chan
*to_k3_chan(struct dma_chan
*chan
)
123 return container_of(chan
, struct k3_dma_chan
, vc
.chan
);
126 static void k3_dma_pause_dma(struct k3_dma_phy
*phy
, bool on
)
131 val
= readl_relaxed(phy
->base
+ CX_CFG
);
133 writel_relaxed(val
, phy
->base
+ CX_CFG
);
135 val
= readl_relaxed(phy
->base
+ CX_CFG
);
137 writel_relaxed(val
, phy
->base
+ CX_CFG
);
141 static void k3_dma_terminate_chan(struct k3_dma_phy
*phy
, struct k3_dma_dev
*d
)
145 k3_dma_pause_dma(phy
, false);
147 val
= 0x1 << phy
->idx
;
148 writel_relaxed(val
, d
->base
+ INT_TC1_RAW
);
149 writel_relaxed(val
, d
->base
+ INT_TC2_RAW
);
150 writel_relaxed(val
, d
->base
+ INT_ERR1_RAW
);
151 writel_relaxed(val
, d
->base
+ INT_ERR2_RAW
);
154 static void k3_dma_set_desc(struct k3_dma_phy
*phy
, struct k3_desc_hw
*hw
)
156 writel_relaxed(hw
->lli
, phy
->base
+ CX_LLI
);
157 writel_relaxed(hw
->count
, phy
->base
+ CX_CNT0
);
158 writel_relaxed(hw
->saddr
, phy
->base
+ CX_SRC
);
159 writel_relaxed(hw
->daddr
, phy
->base
+ CX_DST
);
160 writel_relaxed(AXI_CFG_DEFAULT
, phy
->base
+ AXI_CFG
);
161 writel_relaxed(hw
->config
, phy
->base
+ CX_CFG
);
164 static u32
k3_dma_get_curr_cnt(struct k3_dma_dev
*d
, struct k3_dma_phy
*phy
)
168 cnt
= readl_relaxed(d
->base
+ CX_CUR_CNT
+ phy
->idx
* 0x10);
173 static u32
k3_dma_get_curr_lli(struct k3_dma_phy
*phy
)
175 return readl_relaxed(phy
->base
+ CX_LLI
);
178 static u32
k3_dma_get_chan_stat(struct k3_dma_dev
*d
)
180 return readl_relaxed(d
->base
+ CH_STAT
);
183 static void k3_dma_enable_dma(struct k3_dma_dev
*d
, bool on
)
186 /* set same priority */
187 writel_relaxed(0x0, d
->base
+ CH_PRI
);
190 writel_relaxed(0xffff, d
->base
+ INT_TC1_MASK
);
191 writel_relaxed(0xffff, d
->base
+ INT_TC2_MASK
);
192 writel_relaxed(0xffff, d
->base
+ INT_ERR1_MASK
);
193 writel_relaxed(0xffff, d
->base
+ INT_ERR2_MASK
);
196 writel_relaxed(0x0, d
->base
+ INT_TC1_MASK
);
197 writel_relaxed(0x0, d
->base
+ INT_TC2_MASK
);
198 writel_relaxed(0x0, d
->base
+ INT_ERR1_MASK
);
199 writel_relaxed(0x0, d
->base
+ INT_ERR2_MASK
);
203 static irqreturn_t
k3_dma_int_handler(int irq
, void *dev_id
)
205 struct k3_dma_dev
*d
= (struct k3_dma_dev
*)dev_id
;
206 struct k3_dma_phy
*p
;
207 struct k3_dma_chan
*c
;
208 u32 stat
= readl_relaxed(d
->base
+ INT_STAT
);
209 u32 tc1
= readl_relaxed(d
->base
+ INT_TC1
);
210 u32 tc2
= readl_relaxed(d
->base
+ INT_TC2
);
211 u32 err1
= readl_relaxed(d
->base
+ INT_ERR1
);
212 u32 err2
= readl_relaxed(d
->base
+ INT_ERR2
);
218 if (likely(tc1
& BIT(i
)) || (tc2
& BIT(i
))) {
223 if (c
&& (tc1
& BIT(i
))) {
224 spin_lock_irqsave(&c
->vc
.lock
, flags
);
225 vchan_cookie_complete(&p
->ds_run
->vd
);
226 p
->ds_done
= p
->ds_run
;
228 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
230 if (c
&& (tc2
& BIT(i
))) {
231 spin_lock_irqsave(&c
->vc
.lock
, flags
);
232 if (p
->ds_run
!= NULL
)
233 vchan_cyclic_callback(&p
->ds_run
->vd
);
234 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
238 if (unlikely((err1
& BIT(i
)) || (err2
& BIT(i
))))
239 dev_warn(d
->slave
.dev
, "DMA ERR\n");
242 writel_relaxed(irq_chan
, d
->base
+ INT_TC1_RAW
);
243 writel_relaxed(irq_chan
, d
->base
+ INT_TC2_RAW
);
244 writel_relaxed(err1
, d
->base
+ INT_ERR1_RAW
);
245 writel_relaxed(err2
, d
->base
+ INT_ERR2_RAW
);
248 tasklet_schedule(&d
->task
);
250 if (irq_chan
|| err1
|| err2
)
256 static int k3_dma_start_txd(struct k3_dma_chan
*c
)
258 struct k3_dma_dev
*d
= to_k3_dma(c
->vc
.chan
.device
);
259 struct virt_dma_desc
*vd
= vchan_next_desc(&c
->vc
);
264 if (BIT(c
->phy
->idx
) & k3_dma_get_chan_stat(d
))
268 struct k3_dma_desc_sw
*ds
=
269 container_of(vd
, struct k3_dma_desc_sw
, vd
);
271 * fetch and remove request from vc->desc_issued
272 * so vc->desc_issued only contains desc pending
274 list_del(&ds
->vd
.node
);
277 c
->phy
->ds_done
= NULL
;
279 k3_dma_set_desc(c
->phy
, &ds
->desc_hw
[0]);
282 c
->phy
->ds_run
= NULL
;
283 c
->phy
->ds_done
= NULL
;
287 static void k3_dma_tasklet(unsigned long arg
)
289 struct k3_dma_dev
*d
= (struct k3_dma_dev
*)arg
;
290 struct k3_dma_phy
*p
;
291 struct k3_dma_chan
*c
, *cn
;
292 unsigned pch
, pch_alloc
= 0;
294 /* check new dma request of running channel in vc->desc_issued */
295 list_for_each_entry_safe(c
, cn
, &d
->slave
.channels
, vc
.chan
.device_node
) {
296 spin_lock_irq(&c
->vc
.lock
);
298 if (p
&& p
->ds_done
) {
299 if (k3_dma_start_txd(c
)) {
300 /* No current txd associated with this channel */
301 dev_dbg(d
->slave
.dev
, "pchan %u: free\n", p
->idx
);
302 /* Mark this channel free */
307 spin_unlock_irq(&c
->vc
.lock
);
310 /* check new channel request in d->chan_pending */
311 spin_lock_irq(&d
->lock
);
312 for (pch
= 0; pch
< d
->dma_channels
; pch
++) {
315 if (p
->vchan
== NULL
&& !list_empty(&d
->chan_pending
)) {
316 c
= list_first_entry(&d
->chan_pending
,
317 struct k3_dma_chan
, node
);
318 /* remove from d->chan_pending */
319 list_del_init(&c
->node
);
320 pch_alloc
|= 1 << pch
;
321 /* Mark this channel allocated */
324 dev_dbg(d
->slave
.dev
, "pchan %u: alloc vchan %p\n", pch
, &c
->vc
);
327 spin_unlock_irq(&d
->lock
);
329 for (pch
= 0; pch
< d
->dma_channels
; pch
++) {
330 if (pch_alloc
& (1 << pch
)) {
334 spin_lock_irq(&c
->vc
.lock
);
336 spin_unlock_irq(&c
->vc
.lock
);
342 static void k3_dma_free_chan_resources(struct dma_chan
*chan
)
344 struct k3_dma_chan
*c
= to_k3_chan(chan
);
345 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
348 spin_lock_irqsave(&d
->lock
, flags
);
349 list_del_init(&c
->node
);
350 spin_unlock_irqrestore(&d
->lock
, flags
);
352 vchan_free_chan_resources(&c
->vc
);
356 static enum dma_status
k3_dma_tx_status(struct dma_chan
*chan
,
357 dma_cookie_t cookie
, struct dma_tx_state
*state
)
359 struct k3_dma_chan
*c
= to_k3_chan(chan
);
360 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
361 struct k3_dma_phy
*p
;
362 struct virt_dma_desc
*vd
;
367 ret
= dma_cookie_status(&c
->vc
.chan
, cookie
, state
);
368 if (ret
== DMA_COMPLETE
)
371 spin_lock_irqsave(&c
->vc
.lock
, flags
);
376 * If the cookie is on our issue queue, then the residue is
379 vd
= vchan_find_desc(&c
->vc
, cookie
);
380 if (vd
&& !c
->cyclic
) {
381 bytes
= container_of(vd
, struct k3_dma_desc_sw
, vd
)->size
;
382 } else if ((!p
) || (!p
->ds_run
)) {
385 struct k3_dma_desc_sw
*ds
= p
->ds_run
;
386 u32 clli
= 0, index
= 0;
388 bytes
= k3_dma_get_curr_cnt(d
, p
);
389 clli
= k3_dma_get_curr_lli(p
);
390 index
= ((clli
- ds
->desc_hw_lli
) /
391 sizeof(struct k3_desc_hw
)) + 1;
392 for (; index
< ds
->desc_num
; index
++) {
393 bytes
+= ds
->desc_hw
[index
].count
;
395 if (!ds
->desc_hw
[index
].lli
)
399 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
400 dma_set_residue(state
, bytes
);
404 static void k3_dma_issue_pending(struct dma_chan
*chan
)
406 struct k3_dma_chan
*c
= to_k3_chan(chan
);
407 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
410 spin_lock_irqsave(&c
->vc
.lock
, flags
);
411 /* add request to vc->desc_issued */
412 if (vchan_issue_pending(&c
->vc
)) {
415 if (list_empty(&c
->node
)) {
416 /* if new channel, add chan_pending */
417 list_add_tail(&c
->node
, &d
->chan_pending
);
418 /* check in tasklet */
419 tasklet_schedule(&d
->task
);
420 dev_dbg(d
->slave
.dev
, "vchan %p: issued\n", &c
->vc
);
423 spin_unlock(&d
->lock
);
425 dev_dbg(d
->slave
.dev
, "vchan %p: nothing to issue\n", &c
->vc
);
426 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
429 static void k3_dma_fill_desc(struct k3_dma_desc_sw
*ds
, dma_addr_t dst
,
430 dma_addr_t src
, size_t len
, u32 num
, u32 ccfg
)
432 if (num
!= ds
->desc_num
- 1)
433 ds
->desc_hw
[num
].lli
= ds
->desc_hw_lli
+ (num
+ 1) *
434 sizeof(struct k3_desc_hw
);
436 ds
->desc_hw
[num
].lli
|= CX_LLI_CHAIN_EN
;
437 ds
->desc_hw
[num
].count
= len
;
438 ds
->desc_hw
[num
].saddr
= src
;
439 ds
->desc_hw
[num
].daddr
= dst
;
440 ds
->desc_hw
[num
].config
= ccfg
;
443 static struct k3_dma_desc_sw
*k3_dma_alloc_desc_resource(int num
,
444 struct dma_chan
*chan
)
446 struct k3_dma_chan
*c
= to_k3_chan(chan
);
447 struct k3_dma_desc_sw
*ds
;
448 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
449 int lli_limit
= LLI_BLOCK_SIZE
/ sizeof(struct k3_desc_hw
);
451 if (num
> lli_limit
) {
452 dev_dbg(chan
->device
->dev
, "vch %p: sg num %d exceed max %d\n",
453 &c
->vc
, num
, lli_limit
);
457 ds
= kzalloc(sizeof(*ds
), GFP_NOWAIT
);
461 ds
->desc_hw
= dma_pool_zalloc(d
->pool
, GFP_NOWAIT
, &ds
->desc_hw_lli
);
463 dev_dbg(chan
->device
->dev
, "vch %p: dma alloc fail\n", &c
->vc
);
471 static struct dma_async_tx_descriptor
*k3_dma_prep_memcpy(
472 struct dma_chan
*chan
, dma_addr_t dst
, dma_addr_t src
,
473 size_t len
, unsigned long flags
)
475 struct k3_dma_chan
*c
= to_k3_chan(chan
);
476 struct k3_dma_desc_sw
*ds
;
483 num
= DIV_ROUND_UP(len
, DMA_MAX_SIZE
);
485 ds
= k3_dma_alloc_desc_resource(num
, chan
);
494 /* default is memtomem, without calling device_config */
495 c
->ccfg
= CX_CFG_SRCINCR
| CX_CFG_DSTINCR
| CX_CFG_EN
;
496 c
->ccfg
|= (0xf << 20) | (0xf << 24); /* burst = 16 */
497 c
->ccfg
|= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
501 copy
= min_t(size_t, len
, DMA_MAX_SIZE
);
502 k3_dma_fill_desc(ds
, dst
, src
, copy
, num
++, c
->ccfg
);
504 if (c
->dir
== DMA_MEM_TO_DEV
) {
506 } else if (c
->dir
== DMA_DEV_TO_MEM
) {
515 ds
->desc_hw
[num
-1].lli
= 0; /* end of link */
516 return vchan_tx_prep(&c
->vc
, &ds
->vd
, flags
);
519 static struct dma_async_tx_descriptor
*k3_dma_prep_slave_sg(
520 struct dma_chan
*chan
, struct scatterlist
*sgl
, unsigned int sglen
,
521 enum dma_transfer_direction dir
, unsigned long flags
, void *context
)
523 struct k3_dma_chan
*c
= to_k3_chan(chan
);
524 struct k3_dma_desc_sw
*ds
;
525 size_t len
, avail
, total
= 0;
526 struct scatterlist
*sg
;
527 dma_addr_t addr
, src
= 0, dst
= 0;
535 for_each_sg(sgl
, sg
, sglen
, i
) {
536 avail
= sg_dma_len(sg
);
537 if (avail
> DMA_MAX_SIZE
)
538 num
+= DIV_ROUND_UP(avail
, DMA_MAX_SIZE
) - 1;
541 ds
= k3_dma_alloc_desc_resource(num
, chan
);
546 for_each_sg(sgl
, sg
, sglen
, i
) {
547 addr
= sg_dma_address(sg
);
548 avail
= sg_dma_len(sg
);
552 len
= min_t(size_t, avail
, DMA_MAX_SIZE
);
554 if (dir
== DMA_MEM_TO_DEV
) {
557 } else if (dir
== DMA_DEV_TO_MEM
) {
562 k3_dma_fill_desc(ds
, dst
, src
, len
, num
++, c
->ccfg
);
569 ds
->desc_hw
[num
-1].lli
= 0; /* end of link */
571 return vchan_tx_prep(&c
->vc
, &ds
->vd
, flags
);
574 static struct dma_async_tx_descriptor
*
575 k3_dma_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t buf_addr
,
576 size_t buf_len
, size_t period_len
,
577 enum dma_transfer_direction dir
,
580 struct k3_dma_chan
*c
= to_k3_chan(chan
);
581 struct k3_dma_desc_sw
*ds
;
582 size_t len
, avail
, total
= 0;
583 dma_addr_t addr
, src
= 0, dst
= 0;
584 int num
= 1, since
= 0;
585 size_t modulo
= DMA_CYCLIC_MAX_PERIOD
;
588 dev_dbg(chan
->device
->dev
, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
589 __func__
, &buf_addr
, &to_k3_chan(chan
)->dev_addr
,
590 buf_len
, period_len
, (int)dir
);
594 num
+= DIV_ROUND_UP(avail
, modulo
) - 1;
596 ds
= k3_dma_alloc_desc_resource(num
, chan
);
606 if (period_len
< modulo
)
610 len
= min_t(size_t, avail
, modulo
);
612 if (dir
== DMA_MEM_TO_DEV
) {
615 } else if (dir
== DMA_DEV_TO_MEM
) {
620 if (since
>= period_len
) {
621 /* descriptor asks for TC2 interrupt on completion */
622 en_tc2
= CX_CFG_NODEIRQ
;
627 k3_dma_fill_desc(ds
, dst
, src
, len
, num
++, c
->ccfg
| en_tc2
);
633 /* "Cyclic" == end of link points back to start of link */
634 ds
->desc_hw
[num
- 1].lli
|= ds
->desc_hw_lli
;
638 return vchan_tx_prep(&c
->vc
, &ds
->vd
, flags
);
641 static int k3_dma_config(struct dma_chan
*chan
,
642 struct dma_slave_config
*cfg
)
644 struct k3_dma_chan
*c
= to_k3_chan(chan
);
645 u32 maxburst
= 0, val
= 0;
646 enum dma_slave_buswidth width
= DMA_SLAVE_BUSWIDTH_UNDEFINED
;
650 c
->dir
= cfg
->direction
;
651 if (c
->dir
== DMA_DEV_TO_MEM
) {
652 c
->ccfg
= CX_CFG_DSTINCR
;
653 c
->dev_addr
= cfg
->src_addr
;
654 maxburst
= cfg
->src_maxburst
;
655 width
= cfg
->src_addr_width
;
656 } else if (c
->dir
== DMA_MEM_TO_DEV
) {
657 c
->ccfg
= CX_CFG_SRCINCR
;
658 c
->dev_addr
= cfg
->dst_addr
;
659 maxburst
= cfg
->dst_maxburst
;
660 width
= cfg
->dst_addr_width
;
663 case DMA_SLAVE_BUSWIDTH_1_BYTE
:
664 case DMA_SLAVE_BUSWIDTH_2_BYTES
:
665 case DMA_SLAVE_BUSWIDTH_4_BYTES
:
666 case DMA_SLAVE_BUSWIDTH_8_BYTES
:
673 c
->ccfg
|= (val
<< 12) | (val
<< 16);
675 if ((maxburst
== 0) || (maxburst
> 16))
679 c
->ccfg
|= (val
<< 20) | (val
<< 24);
680 c
->ccfg
|= CX_CFG_MEM2PER
| CX_CFG_EN
;
682 /* specific request line */
683 c
->ccfg
|= c
->vc
.chan
.chan_id
<< 4;
688 static void k3_dma_free_desc(struct virt_dma_desc
*vd
)
690 struct k3_dma_desc_sw
*ds
=
691 container_of(vd
, struct k3_dma_desc_sw
, vd
);
692 struct k3_dma_dev
*d
= to_k3_dma(vd
->tx
.chan
->device
);
694 dma_pool_free(d
->pool
, ds
->desc_hw
, ds
->desc_hw_lli
);
698 static int k3_dma_terminate_all(struct dma_chan
*chan
)
700 struct k3_dma_chan
*c
= to_k3_chan(chan
);
701 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
702 struct k3_dma_phy
*p
= c
->phy
;
706 dev_dbg(d
->slave
.dev
, "vchan %p: terminate all\n", &c
->vc
);
708 /* Prevent this channel being scheduled */
710 list_del_init(&c
->node
);
711 spin_unlock(&d
->lock
);
713 /* Clear the tx descriptor lists */
714 spin_lock_irqsave(&c
->vc
.lock
, flags
);
715 vchan_get_all_descriptors(&c
->vc
, &head
);
717 /* vchan is assigned to a pchan - stop the channel */
718 k3_dma_terminate_chan(p
, d
);
722 vchan_terminate_vdesc(&p
->ds_run
->vd
);
727 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
728 vchan_dma_desc_free_list(&c
->vc
, &head
);
733 static void k3_dma_synchronize(struct dma_chan
*chan
)
735 struct k3_dma_chan
*c
= to_k3_chan(chan
);
737 vchan_synchronize(&c
->vc
);
740 static int k3_dma_transfer_pause(struct dma_chan
*chan
)
742 struct k3_dma_chan
*c
= to_k3_chan(chan
);
743 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
744 struct k3_dma_phy
*p
= c
->phy
;
746 dev_dbg(d
->slave
.dev
, "vchan %p: pause\n", &c
->vc
);
747 if (c
->status
== DMA_IN_PROGRESS
) {
748 c
->status
= DMA_PAUSED
;
750 k3_dma_pause_dma(p
, false);
753 list_del_init(&c
->node
);
754 spin_unlock(&d
->lock
);
761 static int k3_dma_transfer_resume(struct dma_chan
*chan
)
763 struct k3_dma_chan
*c
= to_k3_chan(chan
);
764 struct k3_dma_dev
*d
= to_k3_dma(chan
->device
);
765 struct k3_dma_phy
*p
= c
->phy
;
768 dev_dbg(d
->slave
.dev
, "vchan %p: resume\n", &c
->vc
);
769 spin_lock_irqsave(&c
->vc
.lock
, flags
);
770 if (c
->status
== DMA_PAUSED
) {
771 c
->status
= DMA_IN_PROGRESS
;
773 k3_dma_pause_dma(p
, true);
774 } else if (!list_empty(&c
->vc
.desc_issued
)) {
776 list_add_tail(&c
->node
, &d
->chan_pending
);
777 spin_unlock(&d
->lock
);
780 spin_unlock_irqrestore(&c
->vc
.lock
, flags
);
785 static const struct of_device_id k3_pdma_dt_ids
[] = {
786 { .compatible
= "hisilicon,k3-dma-1.0", },
789 MODULE_DEVICE_TABLE(of
, k3_pdma_dt_ids
);
791 static struct dma_chan
*k3_of_dma_simple_xlate(struct of_phandle_args
*dma_spec
,
792 struct of_dma
*ofdma
)
794 struct k3_dma_dev
*d
= ofdma
->of_dma_data
;
795 unsigned int request
= dma_spec
->args
[0];
797 if (request
>= d
->dma_requests
)
800 return dma_get_slave_channel(&(d
->chans
[request
].vc
.chan
));
803 static int k3_dma_probe(struct platform_device
*op
)
805 struct k3_dma_dev
*d
;
806 const struct of_device_id
*of_id
;
807 struct resource
*iores
;
810 iores
= platform_get_resource(op
, IORESOURCE_MEM
, 0);
814 d
= devm_kzalloc(&op
->dev
, sizeof(*d
), GFP_KERNEL
);
818 d
->base
= devm_ioremap_resource(&op
->dev
, iores
);
820 return PTR_ERR(d
->base
);
822 of_id
= of_match_device(k3_pdma_dt_ids
, &op
->dev
);
824 of_property_read_u32((&op
->dev
)->of_node
,
825 "dma-channels", &d
->dma_channels
);
826 of_property_read_u32((&op
->dev
)->of_node
,
827 "dma-requests", &d
->dma_requests
);
830 d
->clk
= devm_clk_get(&op
->dev
, NULL
);
831 if (IS_ERR(d
->clk
)) {
832 dev_err(&op
->dev
, "no dma clk\n");
833 return PTR_ERR(d
->clk
);
836 irq
= platform_get_irq(op
, 0);
837 ret
= devm_request_irq(&op
->dev
, irq
,
838 k3_dma_int_handler
, 0, DRIVER_NAME
, d
);
844 /* A DMA memory pool for LLIs, align on 32-byte boundary */
845 d
->pool
= dmam_pool_create(DRIVER_NAME
, &op
->dev
,
846 LLI_BLOCK_SIZE
, 32, 0);
850 /* init phy channel */
851 d
->phy
= devm_kcalloc(&op
->dev
,
852 d
->dma_channels
, sizeof(struct k3_dma_phy
), GFP_KERNEL
);
856 for (i
= 0; i
< d
->dma_channels
; i
++) {
857 struct k3_dma_phy
*p
= &d
->phy
[i
];
860 p
->base
= d
->base
+ i
* 0x40;
863 INIT_LIST_HEAD(&d
->slave
.channels
);
864 dma_cap_set(DMA_SLAVE
, d
->slave
.cap_mask
);
865 dma_cap_set(DMA_MEMCPY
, d
->slave
.cap_mask
);
866 dma_cap_set(DMA_CYCLIC
, d
->slave
.cap_mask
);
867 d
->slave
.dev
= &op
->dev
;
868 d
->slave
.device_free_chan_resources
= k3_dma_free_chan_resources
;
869 d
->slave
.device_tx_status
= k3_dma_tx_status
;
870 d
->slave
.device_prep_dma_memcpy
= k3_dma_prep_memcpy
;
871 d
->slave
.device_prep_slave_sg
= k3_dma_prep_slave_sg
;
872 d
->slave
.device_prep_dma_cyclic
= k3_dma_prep_dma_cyclic
;
873 d
->slave
.device_issue_pending
= k3_dma_issue_pending
;
874 d
->slave
.device_config
= k3_dma_config
;
875 d
->slave
.device_pause
= k3_dma_transfer_pause
;
876 d
->slave
.device_resume
= k3_dma_transfer_resume
;
877 d
->slave
.device_terminate_all
= k3_dma_terminate_all
;
878 d
->slave
.device_synchronize
= k3_dma_synchronize
;
879 d
->slave
.copy_align
= DMAENGINE_ALIGN_8_BYTES
;
881 /* init virtual channel */
882 d
->chans
= devm_kcalloc(&op
->dev
,
883 d
->dma_requests
, sizeof(struct k3_dma_chan
), GFP_KERNEL
);
884 if (d
->chans
== NULL
)
887 for (i
= 0; i
< d
->dma_requests
; i
++) {
888 struct k3_dma_chan
*c
= &d
->chans
[i
];
890 c
->status
= DMA_IN_PROGRESS
;
891 INIT_LIST_HEAD(&c
->node
);
892 c
->vc
.desc_free
= k3_dma_free_desc
;
893 vchan_init(&c
->vc
, &d
->slave
);
896 /* Enable clock before accessing registers */
897 ret
= clk_prepare_enable(d
->clk
);
899 dev_err(&op
->dev
, "clk_prepare_enable failed: %d\n", ret
);
903 k3_dma_enable_dma(d
, true);
905 ret
= dma_async_device_register(&d
->slave
);
907 goto dma_async_register_fail
;
909 ret
= of_dma_controller_register((&op
->dev
)->of_node
,
910 k3_of_dma_simple_xlate
, d
);
912 goto of_dma_register_fail
;
914 spin_lock_init(&d
->lock
);
915 INIT_LIST_HEAD(&d
->chan_pending
);
916 tasklet_init(&d
->task
, k3_dma_tasklet
, (unsigned long)d
);
917 platform_set_drvdata(op
, d
);
918 dev_info(&op
->dev
, "initialized\n");
922 of_dma_register_fail
:
923 dma_async_device_unregister(&d
->slave
);
924 dma_async_register_fail
:
925 clk_disable_unprepare(d
->clk
);
929 static int k3_dma_remove(struct platform_device
*op
)
931 struct k3_dma_chan
*c
, *cn
;
932 struct k3_dma_dev
*d
= platform_get_drvdata(op
);
934 dma_async_device_unregister(&d
->slave
);
935 of_dma_controller_free((&op
->dev
)->of_node
);
937 devm_free_irq(&op
->dev
, d
->irq
, d
);
939 list_for_each_entry_safe(c
, cn
, &d
->slave
.channels
, vc
.chan
.device_node
) {
940 list_del(&c
->vc
.chan
.device_node
);
941 tasklet_kill(&c
->vc
.task
);
943 tasklet_kill(&d
->task
);
944 clk_disable_unprepare(d
->clk
);
948 #ifdef CONFIG_PM_SLEEP
949 static int k3_dma_suspend_dev(struct device
*dev
)
951 struct k3_dma_dev
*d
= dev_get_drvdata(dev
);
954 stat
= k3_dma_get_chan_stat(d
);
956 dev_warn(d
->slave
.dev
,
957 "chan %d is running fail to suspend\n", stat
);
960 k3_dma_enable_dma(d
, false);
961 clk_disable_unprepare(d
->clk
);
965 static int k3_dma_resume_dev(struct device
*dev
)
967 struct k3_dma_dev
*d
= dev_get_drvdata(dev
);
970 ret
= clk_prepare_enable(d
->clk
);
972 dev_err(d
->slave
.dev
, "clk_prepare_enable failed: %d\n", ret
);
975 k3_dma_enable_dma(d
, true);
980 static SIMPLE_DEV_PM_OPS(k3_dma_pmops
, k3_dma_suspend_dev
, k3_dma_resume_dev
);
982 static struct platform_driver k3_pdma_driver
= {
986 .of_match_table
= k3_pdma_dt_ids
,
988 .probe
= k3_dma_probe
,
989 .remove
= k3_dma_remove
,
992 module_platform_driver(k3_pdma_driver
);
994 MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
995 MODULE_ALIAS("platform:k3dma");
996 MODULE_LICENSE("GPL v2");