1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018 MediaTek Inc.
5 * Driver for MediaTek High-Speed DMA Controller
7 * Author: Sean Wang <sean.wang@mediatek.com>
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/dmaengine.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/iopoll.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
20 #include <linux/of_dma.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/refcount.h>
24 #include <linux/slab.h>
26 #include "../virt-dma.h"
28 #define MTK_HSDMA_USEC_POLL 20
29 #define MTK_HSDMA_TIMEOUT_POLL 200000
30 #define MTK_HSDMA_DMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
32 /* The default number of virtual channel */
33 #define MTK_HSDMA_NR_VCHANS 3
35 /* Only one physical channel supported */
36 #define MTK_HSDMA_NR_MAX_PCHANS 1
38 /* Macro for physical descriptor (PD) manipulation */
39 /* The number of PD which must be 2 of power */
40 #define MTK_DMA_SIZE 64
41 #define MTK_HSDMA_NEXT_DESP_IDX(x, y) (((x) + 1) & ((y) - 1))
42 #define MTK_HSDMA_LAST_DESP_IDX(x, y) (((x) - 1) & ((y) - 1))
43 #define MTK_HSDMA_MAX_LEN 0x3f80
44 #define MTK_HSDMA_ALIGN_SIZE 4
45 #define MTK_HSDMA_PLEN_MASK 0x3fff
46 #define MTK_HSDMA_DESC_PLEN(x) (((x) & MTK_HSDMA_PLEN_MASK) << 16)
47 #define MTK_HSDMA_DESC_PLEN_GET(x) (((x) >> 16) & MTK_HSDMA_PLEN_MASK)
49 /* Registers for underlying ring manipulation */
50 #define MTK_HSDMA_TX_BASE 0x0
51 #define MTK_HSDMA_TX_CNT 0x4
52 #define MTK_HSDMA_TX_CPU 0x8
53 #define MTK_HSDMA_TX_DMA 0xc
54 #define MTK_HSDMA_RX_BASE 0x100
55 #define MTK_HSDMA_RX_CNT 0x104
56 #define MTK_HSDMA_RX_CPU 0x108
57 #define MTK_HSDMA_RX_DMA 0x10c
59 /* Registers for global setup */
60 #define MTK_HSDMA_GLO 0x204
61 #define MTK_HSDMA_GLO_MULTI_DMA BIT(10)
62 #define MTK_HSDMA_TX_WB_DDONE BIT(6)
63 #define MTK_HSDMA_BURST_64BYTES (0x2 << 4)
64 #define MTK_HSDMA_GLO_RX_BUSY BIT(3)
65 #define MTK_HSDMA_GLO_RX_DMA BIT(2)
66 #define MTK_HSDMA_GLO_TX_BUSY BIT(1)
67 #define MTK_HSDMA_GLO_TX_DMA BIT(0)
68 #define MTK_HSDMA_GLO_DMA (MTK_HSDMA_GLO_TX_DMA | \
70 #define MTK_HSDMA_GLO_BUSY (MTK_HSDMA_GLO_RX_BUSY | \
71 MTK_HSDMA_GLO_TX_BUSY)
72 #define MTK_HSDMA_GLO_DEFAULT (MTK_HSDMA_GLO_TX_DMA | \
73 MTK_HSDMA_GLO_RX_DMA | \
74 MTK_HSDMA_TX_WB_DDONE | \
75 MTK_HSDMA_BURST_64BYTES | \
76 MTK_HSDMA_GLO_MULTI_DMA)
78 /* Registers for reset */
79 #define MTK_HSDMA_RESET 0x208
80 #define MTK_HSDMA_RST_TX BIT(0)
81 #define MTK_HSDMA_RST_RX BIT(16)
83 /* Registers for interrupt control */
84 #define MTK_HSDMA_DLYINT 0x20c
85 #define MTK_HSDMA_RXDLY_INT_EN BIT(15)
87 /* Interrupt fires when the pending number's more than the specified */
88 #define MTK_HSDMA_RXMAX_PINT(x) (((x) & 0x7f) << 8)
90 /* Interrupt fires when the pending time's more than the specified in 20 us */
91 #define MTK_HSDMA_RXMAX_PTIME(x) ((x) & 0x7f)
92 #define MTK_HSDMA_DLYINT_DEFAULT (MTK_HSDMA_RXDLY_INT_EN | \
93 MTK_HSDMA_RXMAX_PINT(20) | \
94 MTK_HSDMA_RXMAX_PTIME(20))
95 #define MTK_HSDMA_INT_STATUS 0x220
96 #define MTK_HSDMA_INT_ENABLE 0x228
97 #define MTK_HSDMA_INT_RXDONE BIT(16)
99 enum mtk_hsdma_vdesc_flag
{
100 MTK_HSDMA_VDESC_FINISHED
= 0x01,
103 #define IS_MTK_HSDMA_VDESC_FINISHED(x) ((x) == MTK_HSDMA_VDESC_FINISHED)
106 * struct mtk_hsdma_pdesc - This is the struct holding info describing physical
107 * descriptor (PD) and its placement must be kept at
108 * 4-bytes alignment in little endian order.
109 * @desc1: | The control pad used to indicate hardware how to
110 * @desc2: | deal with the descriptor such as source and
111 * @desc3: | destination address and data length. The maximum
112 * @desc4: | data length each pdesc can handle is 0x3f80 bytes
114 struct mtk_hsdma_pdesc
{
119 } __packed
__aligned(4);
122 * struct mtk_hsdma_vdesc - This is the struct holding info describing virtual
124 * @vd: An instance for struct virt_dma_desc
125 * @len: The total data size device wants to move
126 * @residue: The remaining data size device will move
127 * @dest: The destination address device wants to move to
128 * @src: The source address device wants to move from
130 struct mtk_hsdma_vdesc
{
131 struct virt_dma_desc vd
;
139 * struct mtk_hsdma_cb - This is the struct holding extra info required for RX
140 * ring to know what relevant VD the PD is being
142 * @vd: Pointer to the relevant VD.
143 * @flag: Flag indicating what action should be taken when VD
146 struct mtk_hsdma_cb
{
147 struct virt_dma_desc
*vd
;
148 enum mtk_hsdma_vdesc_flag flag
;
152 * struct mtk_hsdma_ring - This struct holds info describing underlying ring
154 * @txd: The descriptor TX ring which describes DMA source
156 * @rxd: The descriptor RX ring which describes DMA
157 * destination information
158 * @cb: The extra information pointed at by RX ring
159 * @tphys: The physical addr of TX ring
160 * @rphys: The physical addr of RX ring
161 * @cur_tptr: Pointer to the next free descriptor used by the host
162 * @cur_rptr: Pointer to the last done descriptor by the device
164 struct mtk_hsdma_ring
{
165 struct mtk_hsdma_pdesc
*txd
;
166 struct mtk_hsdma_pdesc
*rxd
;
167 struct mtk_hsdma_cb
*cb
;
175 * struct mtk_hsdma_pchan - This is the struct holding info describing physical
177 * @ring: An instance for the underlying ring
178 * @sz_ring: Total size allocated for the ring
179 * @nr_free: Total number of free rooms in the ring. It would
180 * be accessed and updated frequently between IRQ
181 * context and user context to reflect whether ring
182 * can accept requests from VD.
184 struct mtk_hsdma_pchan
{
185 struct mtk_hsdma_ring ring
;
191 * struct mtk_hsdma_vchan - This is the struct holding info describing virtual
193 * @vc: An instance for struct virt_dma_chan
194 * @issue_completion: The wait for all issued descriptors completited
195 * @issue_synchronize: Bool indicating channel synchronization starts
196 * @desc_hw_processing: List those descriptors the hardware is processing,
197 * which is protected by vc.lock
199 struct mtk_hsdma_vchan
{
200 struct virt_dma_chan vc
;
201 struct completion issue_completion
;
202 bool issue_synchronize
;
203 struct list_head desc_hw_processing
;
207 * struct mtk_hsdma_soc - This is the struct holding differences among SoCs
208 * @ddone: Bit mask for DDONE
209 * @ls0: Bit mask for LS0
211 struct mtk_hsdma_soc
{
217 * struct mtk_hsdma_device - This is the struct holding info describing HSDMA
219 * @ddev: An instance for struct dma_device
220 * @base: The mapped register I/O base
221 * @clk: The clock that device internal is using
222 * @irq: The IRQ that device are using
223 * @dma_requests: The number of VCs the device supports to
224 * @vc: The pointer to all available VCs
225 * @pc: The pointer to the underlying PC
226 * @pc_refcnt: Track how many VCs are using the PC
227 * @lock: Lock protect agaisting multiple VCs access PC
228 * @soc: The pointer to area holding differences among
231 struct mtk_hsdma_device
{
232 struct dma_device ddev
;
238 struct mtk_hsdma_vchan
*vc
;
239 struct mtk_hsdma_pchan
*pc
;
240 refcount_t pc_refcnt
;
242 /* Lock used to protect against multiple VCs access PC */
245 const struct mtk_hsdma_soc
*soc
;
248 static struct mtk_hsdma_device
*to_hsdma_dev(struct dma_chan
*chan
)
250 return container_of(chan
->device
, struct mtk_hsdma_device
, ddev
);
253 static inline struct mtk_hsdma_vchan
*to_hsdma_vchan(struct dma_chan
*chan
)
255 return container_of(chan
, struct mtk_hsdma_vchan
, vc
.chan
);
258 static struct mtk_hsdma_vdesc
*to_hsdma_vdesc(struct virt_dma_desc
*vd
)
260 return container_of(vd
, struct mtk_hsdma_vdesc
, vd
);
263 static struct device
*hsdma2dev(struct mtk_hsdma_device
*hsdma
)
265 return hsdma
->ddev
.dev
;
268 static u32
mtk_dma_read(struct mtk_hsdma_device
*hsdma
, u32 reg
)
270 return readl(hsdma
->base
+ reg
);
273 static void mtk_dma_write(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
275 writel(val
, hsdma
->base
+ reg
);
278 static void mtk_dma_rmw(struct mtk_hsdma_device
*hsdma
, u32 reg
,
283 val
= mtk_dma_read(hsdma
, reg
);
286 mtk_dma_write(hsdma
, reg
, val
);
289 static void mtk_dma_set(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
291 mtk_dma_rmw(hsdma
, reg
, 0, val
);
294 static void mtk_dma_clr(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
296 mtk_dma_rmw(hsdma
, reg
, val
, 0);
299 static void mtk_hsdma_vdesc_free(struct virt_dma_desc
*vd
)
301 kfree(container_of(vd
, struct mtk_hsdma_vdesc
, vd
));
304 static int mtk_hsdma_busy_wait(struct mtk_hsdma_device
*hsdma
)
308 return readl_poll_timeout(hsdma
->base
+ MTK_HSDMA_GLO
, status
,
309 !(status
& MTK_HSDMA_GLO_BUSY
),
311 MTK_HSDMA_TIMEOUT_POLL
);
314 static int mtk_hsdma_alloc_pchan(struct mtk_hsdma_device
*hsdma
,
315 struct mtk_hsdma_pchan
*pc
)
317 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
320 memset(pc
, 0, sizeof(*pc
));
323 * Allocate ring space where [0 ... MTK_DMA_SIZE - 1] is for TX ring
324 * and [MTK_DMA_SIZE ... 2 * MTK_DMA_SIZE - 1] is for RX ring.
326 pc
->sz_ring
= 2 * MTK_DMA_SIZE
* sizeof(*ring
->txd
);
327 ring
->txd
= dma_alloc_coherent(hsdma2dev(hsdma
), pc
->sz_ring
,
328 &ring
->tphys
, GFP_NOWAIT
);
332 ring
->rxd
= &ring
->txd
[MTK_DMA_SIZE
];
333 ring
->rphys
= ring
->tphys
+ MTK_DMA_SIZE
* sizeof(*ring
->txd
);
335 ring
->cur_rptr
= MTK_DMA_SIZE
- 1;
337 ring
->cb
= kcalloc(MTK_DMA_SIZE
, sizeof(*ring
->cb
), GFP_NOWAIT
);
343 atomic_set(&pc
->nr_free
, MTK_DMA_SIZE
- 1);
345 /* Disable HSDMA and wait for the completion */
346 mtk_dma_clr(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
347 err
= mtk_hsdma_busy_wait(hsdma
);
352 mtk_dma_set(hsdma
, MTK_HSDMA_RESET
,
353 MTK_HSDMA_RST_TX
| MTK_HSDMA_RST_RX
);
354 mtk_dma_clr(hsdma
, MTK_HSDMA_RESET
,
355 MTK_HSDMA_RST_TX
| MTK_HSDMA_RST_RX
);
357 /* Setup HSDMA initial pointer in the ring */
358 mtk_dma_write(hsdma
, MTK_HSDMA_TX_BASE
, ring
->tphys
);
359 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CNT
, MTK_DMA_SIZE
);
360 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, ring
->cur_tptr
);
361 mtk_dma_write(hsdma
, MTK_HSDMA_TX_DMA
, 0);
362 mtk_dma_write(hsdma
, MTK_HSDMA_RX_BASE
, ring
->rphys
);
363 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CNT
, MTK_DMA_SIZE
);
364 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, ring
->cur_rptr
);
365 mtk_dma_write(hsdma
, MTK_HSDMA_RX_DMA
, 0);
368 mtk_dma_set(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
370 /* Setup delayed interrupt */
371 mtk_dma_write(hsdma
, MTK_HSDMA_DLYINT
, MTK_HSDMA_DLYINT_DEFAULT
);
373 /* Enable interrupt */
374 mtk_dma_set(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
382 dma_free_coherent(hsdma2dev(hsdma
),
383 pc
->sz_ring
, ring
->txd
, ring
->tphys
);
387 static void mtk_hsdma_free_pchan(struct mtk_hsdma_device
*hsdma
,
388 struct mtk_hsdma_pchan
*pc
)
390 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
392 /* Disable HSDMA and then wait for the completion */
393 mtk_dma_clr(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
394 mtk_hsdma_busy_wait(hsdma
);
396 /* Reset pointer in the ring */
397 mtk_dma_clr(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
398 mtk_dma_write(hsdma
, MTK_HSDMA_TX_BASE
, 0);
399 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CNT
, 0);
400 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, 0);
401 mtk_dma_write(hsdma
, MTK_HSDMA_RX_BASE
, 0);
402 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CNT
, 0);
403 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, MTK_DMA_SIZE
- 1);
407 dma_free_coherent(hsdma2dev(hsdma
),
408 pc
->sz_ring
, ring
->txd
, ring
->tphys
);
411 static int mtk_hsdma_issue_pending_vdesc(struct mtk_hsdma_device
*hsdma
,
412 struct mtk_hsdma_pchan
*pc
,
413 struct mtk_hsdma_vdesc
*hvd
)
415 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
416 struct mtk_hsdma_pdesc
*txd
, *rxd
;
417 u16 reserved
, prev
, tlen
, num_sgs
;
420 /* Protect against PC is accessed by multiple VCs simultaneously */
421 spin_lock_irqsave(&hsdma
->lock
, flags
);
424 * Reserve rooms, where pc->nr_free is used to track how many free
425 * rooms in the ring being updated in user and IRQ context.
427 num_sgs
= DIV_ROUND_UP(hvd
->len
, MTK_HSDMA_MAX_LEN
);
428 reserved
= min_t(u16
, num_sgs
, atomic_read(&pc
->nr_free
));
431 spin_unlock_irqrestore(&hsdma
->lock
, flags
);
435 atomic_sub(reserved
, &pc
->nr_free
);
438 /* Limit size by PD capability for valid data moving */
439 tlen
= (hvd
->len
> MTK_HSDMA_MAX_LEN
) ?
440 MTK_HSDMA_MAX_LEN
: hvd
->len
;
443 * Setup PDs using the remaining VD info mapped on those
444 * reserved rooms. And since RXD is shared memory between the
445 * host and the device allocated by dma_alloc_coherent call,
446 * the helper macro WRITE_ONCE can ensure the data written to
447 * RAM would really happens.
449 txd
= &ring
->txd
[ring
->cur_tptr
];
450 WRITE_ONCE(txd
->desc1
, hvd
->src
);
451 WRITE_ONCE(txd
->desc2
,
452 hsdma
->soc
->ls0
| MTK_HSDMA_DESC_PLEN(tlen
));
454 rxd
= &ring
->rxd
[ring
->cur_tptr
];
455 WRITE_ONCE(rxd
->desc1
, hvd
->dest
);
456 WRITE_ONCE(rxd
->desc2
, MTK_HSDMA_DESC_PLEN(tlen
));
458 /* Associate VD, the PD belonged to */
459 ring
->cb
[ring
->cur_tptr
].vd
= &hvd
->vd
;
461 /* Move forward the pointer of TX ring */
462 ring
->cur_tptr
= MTK_HSDMA_NEXT_DESP_IDX(ring
->cur_tptr
,
465 /* Update VD with remaining data */
472 * Tagging flag for the last PD for VD will be responsible for
476 prev
= MTK_HSDMA_LAST_DESP_IDX(ring
->cur_tptr
, MTK_DMA_SIZE
);
477 ring
->cb
[prev
].flag
= MTK_HSDMA_VDESC_FINISHED
;
480 /* Ensure all changes indeed done before we're going on */
484 * Updating into hardware the pointer of TX ring lets HSDMA to take
485 * action for those pending PDs.
487 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, ring
->cur_tptr
);
489 spin_unlock_irqrestore(&hsdma
->lock
, flags
);
494 static void mtk_hsdma_issue_vchan_pending(struct mtk_hsdma_device
*hsdma
,
495 struct mtk_hsdma_vchan
*hvc
)
497 struct virt_dma_desc
*vd
, *vd2
;
500 lockdep_assert_held(&hvc
->vc
.lock
);
502 list_for_each_entry_safe(vd
, vd2
, &hvc
->vc
.desc_issued
, node
) {
503 struct mtk_hsdma_vdesc
*hvd
;
505 hvd
= to_hsdma_vdesc(vd
);
507 /* Map VD into PC and all VCs shares a single PC */
508 err
= mtk_hsdma_issue_pending_vdesc(hsdma
, hsdma
->pc
, hvd
);
511 * Move VD from desc_issued to desc_hw_processing when entire
512 * VD is fit into available PDs. Otherwise, the uncompleted
513 * VDs would stay in list desc_issued and then restart the
514 * processing as soon as possible once underlying ring space
517 if (err
== -ENOSPC
|| hvd
->len
> 0)
521 * The extra list desc_hw_processing is used because
522 * hardware can't provide sufficient information allowing us
523 * to know what VDs are still working on the underlying ring.
524 * Through the additional list, it can help us to implement
525 * terminate_all, residue calculation and such thing needed
526 * to know detail descriptor status on the hardware.
528 list_move_tail(&vd
->node
, &hvc
->desc_hw_processing
);
532 static void mtk_hsdma_free_rooms_in_ring(struct mtk_hsdma_device
*hsdma
)
534 struct mtk_hsdma_vchan
*hvc
;
535 struct mtk_hsdma_pdesc
*rxd
;
536 struct mtk_hsdma_vdesc
*hvd
;
537 struct mtk_hsdma_pchan
*pc
;
538 struct mtk_hsdma_cb
*cb
;
539 int i
= MTK_DMA_SIZE
;
544 /* Read IRQ status */
545 status
= mtk_dma_read(hsdma
, MTK_HSDMA_INT_STATUS
);
546 if (unlikely(!(status
& MTK_HSDMA_INT_RXDONE
)))
552 * Using a fail-safe loop with iterations of up to MTK_DMA_SIZE to
553 * reclaim these finished descriptors: The most number of PDs the ISR
554 * can handle at one time shouldn't be more than MTK_DMA_SIZE so we
555 * take it as limited count instead of just using a dangerous infinite
559 next
= MTK_HSDMA_NEXT_DESP_IDX(pc
->ring
.cur_rptr
,
561 rxd
= &pc
->ring
.rxd
[next
];
564 * If MTK_HSDMA_DESC_DDONE is no specified, that means data
565 * moving for the PD is still under going.
567 desc2
= READ_ONCE(rxd
->desc2
);
568 if (!(desc2
& hsdma
->soc
->ddone
))
571 cb
= &pc
->ring
.cb
[next
];
572 if (unlikely(!cb
->vd
)) {
573 dev_err(hsdma2dev(hsdma
), "cb->vd cannot be null\n");
577 /* Update residue of VD the associated PD belonged to */
578 hvd
= to_hsdma_vdesc(cb
->vd
);
579 hvd
->residue
-= MTK_HSDMA_DESC_PLEN_GET(rxd
->desc2
);
581 /* Complete VD until the relevant last PD is finished */
582 if (IS_MTK_HSDMA_VDESC_FINISHED(cb
->flag
)) {
583 hvc
= to_hsdma_vchan(cb
->vd
->tx
.chan
);
585 spin_lock(&hvc
->vc
.lock
);
587 /* Remove VD from list desc_hw_processing */
588 list_del(&cb
->vd
->node
);
590 /* Add VD into list desc_completed */
591 vchan_cookie_complete(cb
->vd
);
593 if (hvc
->issue_synchronize
&&
594 list_empty(&hvc
->desc_hw_processing
)) {
595 complete(&hvc
->issue_completion
);
596 hvc
->issue_synchronize
= false;
598 spin_unlock(&hvc
->vc
.lock
);
606 * Recycle the RXD with the helper WRITE_ONCE that can ensure
607 * data written into RAM would really happens.
609 WRITE_ONCE(rxd
->desc1
, 0);
610 WRITE_ONCE(rxd
->desc2
, 0);
611 pc
->ring
.cur_rptr
= next
;
614 atomic_inc(&pc
->nr_free
);
617 /* Ensure all changes indeed done before we're going on */
620 /* Update CPU pointer for those completed PDs */
621 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, pc
->ring
.cur_rptr
);
624 * Acking the pending IRQ allows hardware no longer to keep the used
625 * IRQ line in certain trigger state when software has completed all
626 * the finished physical descriptors.
628 if (atomic_read(&pc
->nr_free
) >= MTK_DMA_SIZE
- 1)
629 mtk_dma_write(hsdma
, MTK_HSDMA_INT_STATUS
, status
);
631 /* ASAP handles pending VDs in all VCs after freeing some rooms */
632 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
634 spin_lock(&hvc
->vc
.lock
);
635 mtk_hsdma_issue_vchan_pending(hsdma
, hvc
);
636 spin_unlock(&hvc
->vc
.lock
);
640 /* All completed PDs are cleaned up, so enable interrupt again */
641 mtk_dma_set(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
644 static irqreturn_t
mtk_hsdma_irq(int irq
, void *devid
)
646 struct mtk_hsdma_device
*hsdma
= devid
;
649 * Disable interrupt until all completed PDs are cleaned up in
650 * mtk_hsdma_free_rooms call.
652 mtk_dma_clr(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
654 mtk_hsdma_free_rooms_in_ring(hsdma
);
659 static struct virt_dma_desc
*mtk_hsdma_find_active_desc(struct dma_chan
*c
,
662 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
663 struct virt_dma_desc
*vd
;
665 list_for_each_entry(vd
, &hvc
->desc_hw_processing
, node
)
666 if (vd
->tx
.cookie
== cookie
)
669 list_for_each_entry(vd
, &hvc
->vc
.desc_issued
, node
)
670 if (vd
->tx
.cookie
== cookie
)
676 static enum dma_status
mtk_hsdma_tx_status(struct dma_chan
*c
,
678 struct dma_tx_state
*txstate
)
680 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
681 struct mtk_hsdma_vdesc
*hvd
;
682 struct virt_dma_desc
*vd
;
687 ret
= dma_cookie_status(c
, cookie
, txstate
);
688 if (ret
== DMA_COMPLETE
|| !txstate
)
691 spin_lock_irqsave(&hvc
->vc
.lock
, flags
);
692 vd
= mtk_hsdma_find_active_desc(c
, cookie
);
693 spin_unlock_irqrestore(&hvc
->vc
.lock
, flags
);
696 hvd
= to_hsdma_vdesc(vd
);
697 bytes
= hvd
->residue
;
700 dma_set_residue(txstate
, bytes
);
705 static void mtk_hsdma_issue_pending(struct dma_chan
*c
)
707 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
708 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
711 spin_lock_irqsave(&hvc
->vc
.lock
, flags
);
713 if (vchan_issue_pending(&hvc
->vc
))
714 mtk_hsdma_issue_vchan_pending(hsdma
, hvc
);
716 spin_unlock_irqrestore(&hvc
->vc
.lock
, flags
);
719 static struct dma_async_tx_descriptor
*
720 mtk_hsdma_prep_dma_memcpy(struct dma_chan
*c
, dma_addr_t dest
,
721 dma_addr_t src
, size_t len
, unsigned long flags
)
723 struct mtk_hsdma_vdesc
*hvd
;
725 hvd
= kzalloc(sizeof(*hvd
), GFP_NOWAIT
);
734 return vchan_tx_prep(to_virt_chan(c
), &hvd
->vd
, flags
);
737 static int mtk_hsdma_free_inactive_desc(struct dma_chan
*c
)
739 struct virt_dma_chan
*vc
= to_virt_chan(c
);
743 spin_lock_irqsave(&vc
->lock
, flags
);
744 list_splice_tail_init(&vc
->desc_allocated
, &head
);
745 list_splice_tail_init(&vc
->desc_submitted
, &head
);
746 list_splice_tail_init(&vc
->desc_issued
, &head
);
747 spin_unlock_irqrestore(&vc
->lock
, flags
);
749 /* At the point, we don't expect users put descriptor into VC again */
750 vchan_dma_desc_free_list(vc
, &head
);
755 static void mtk_hsdma_free_active_desc(struct dma_chan
*c
)
757 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
758 bool sync_needed
= false;
761 * Once issue_synchronize is being set, which means once the hardware
762 * consumes all descriptors for the channel in the ring, the
763 * synchronization must be notified immediately it is completed.
765 spin_lock(&hvc
->vc
.lock
);
766 if (!list_empty(&hvc
->desc_hw_processing
)) {
767 hvc
->issue_synchronize
= true;
770 spin_unlock(&hvc
->vc
.lock
);
773 wait_for_completion(&hvc
->issue_completion
);
775 * At the point, we expect that all remaining descriptors in the ring
776 * for the channel should be all processing done.
778 WARN_ONCE(!list_empty(&hvc
->desc_hw_processing
),
779 "Desc pending still in list desc_hw_processing\n");
781 /* Free all descriptors in list desc_completed */
782 vchan_synchronize(&hvc
->vc
);
784 WARN_ONCE(!list_empty(&hvc
->vc
.desc_completed
),
785 "Desc pending still in list desc_completed\n");
788 static int mtk_hsdma_terminate_all(struct dma_chan
*c
)
791 * Free pending descriptors not processed yet by hardware that have
792 * previously been submitted to the channel.
794 mtk_hsdma_free_inactive_desc(c
);
797 * However, the DMA engine doesn't provide any way to stop these
798 * descriptors being processed currently by hardware. The only way is
799 * to just waiting until these descriptors are all processed completely
800 * through mtk_hsdma_free_active_desc call.
802 mtk_hsdma_free_active_desc(c
);
807 static int mtk_hsdma_alloc_chan_resources(struct dma_chan
*c
)
809 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
813 * Since HSDMA has only one PC, the resource for PC is being allocated
814 * when the first VC is being created and the other VCs would run on
817 if (!refcount_read(&hsdma
->pc_refcnt
)) {
818 err
= mtk_hsdma_alloc_pchan(hsdma
, hsdma
->pc
);
822 * refcount_inc would complain increment on 0; use-after-free.
823 * Thus, we need to explicitly set it as 1 initially.
825 refcount_set(&hsdma
->pc_refcnt
, 1);
827 refcount_inc(&hsdma
->pc_refcnt
);
833 static void mtk_hsdma_free_chan_resources(struct dma_chan
*c
)
835 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
837 /* Free all descriptors in all lists on the VC */
838 mtk_hsdma_terminate_all(c
);
840 /* The resource for PC is not freed until all the VCs are destroyed */
841 if (!refcount_dec_and_test(&hsdma
->pc_refcnt
))
844 mtk_hsdma_free_pchan(hsdma
, hsdma
->pc
);
847 static int mtk_hsdma_hw_init(struct mtk_hsdma_device
*hsdma
)
851 pm_runtime_enable(hsdma2dev(hsdma
));
852 pm_runtime_get_sync(hsdma2dev(hsdma
));
854 err
= clk_prepare_enable(hsdma
->clk
);
858 mtk_dma_write(hsdma
, MTK_HSDMA_INT_ENABLE
, 0);
859 mtk_dma_write(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DEFAULT
);
864 static int mtk_hsdma_hw_deinit(struct mtk_hsdma_device
*hsdma
)
866 mtk_dma_write(hsdma
, MTK_HSDMA_GLO
, 0);
868 clk_disable_unprepare(hsdma
->clk
);
870 pm_runtime_put_sync(hsdma2dev(hsdma
));
871 pm_runtime_disable(hsdma2dev(hsdma
));
876 static const struct mtk_hsdma_soc mt7623_soc
= {
881 static const struct mtk_hsdma_soc mt7622_soc
= {
886 static const struct of_device_id mtk_hsdma_match
[] = {
887 { .compatible
= "mediatek,mt7623-hsdma", .data
= &mt7623_soc
},
888 { .compatible
= "mediatek,mt7622-hsdma", .data
= &mt7622_soc
},
891 MODULE_DEVICE_TABLE(of
, mtk_hsdma_match
);
893 static int mtk_hsdma_probe(struct platform_device
*pdev
)
895 struct mtk_hsdma_device
*hsdma
;
896 struct mtk_hsdma_vchan
*vc
;
897 struct dma_device
*dd
;
900 hsdma
= devm_kzalloc(&pdev
->dev
, sizeof(*hsdma
), GFP_KERNEL
);
906 hsdma
->base
= devm_platform_ioremap_resource(pdev
, 0);
907 if (IS_ERR(hsdma
->base
))
908 return PTR_ERR(hsdma
->base
);
910 hsdma
->soc
= of_device_get_match_data(&pdev
->dev
);
912 dev_err(&pdev
->dev
, "No device match found\n");
916 hsdma
->clk
= devm_clk_get(&pdev
->dev
, "hsdma");
917 if (IS_ERR(hsdma
->clk
)) {
918 dev_err(&pdev
->dev
, "No clock for %s\n",
919 dev_name(&pdev
->dev
));
920 return PTR_ERR(hsdma
->clk
);
923 err
= platform_get_irq(pdev
, 0);
928 refcount_set(&hsdma
->pc_refcnt
, 0);
929 spin_lock_init(&hsdma
->lock
);
931 dma_cap_set(DMA_MEMCPY
, dd
->cap_mask
);
933 dd
->copy_align
= MTK_HSDMA_ALIGN_SIZE
;
934 dd
->device_alloc_chan_resources
= mtk_hsdma_alloc_chan_resources
;
935 dd
->device_free_chan_resources
= mtk_hsdma_free_chan_resources
;
936 dd
->device_tx_status
= mtk_hsdma_tx_status
;
937 dd
->device_issue_pending
= mtk_hsdma_issue_pending
;
938 dd
->device_prep_dma_memcpy
= mtk_hsdma_prep_dma_memcpy
;
939 dd
->device_terminate_all
= mtk_hsdma_terminate_all
;
940 dd
->src_addr_widths
= MTK_HSDMA_DMA_BUSWIDTHS
;
941 dd
->dst_addr_widths
= MTK_HSDMA_DMA_BUSWIDTHS
;
942 dd
->directions
= BIT(DMA_MEM_TO_MEM
);
943 dd
->residue_granularity
= DMA_RESIDUE_GRANULARITY_SEGMENT
;
944 dd
->dev
= &pdev
->dev
;
945 INIT_LIST_HEAD(&dd
->channels
);
947 hsdma
->dma_requests
= MTK_HSDMA_NR_VCHANS
;
948 if (pdev
->dev
.of_node
&& of_property_read_u32(pdev
->dev
.of_node
,
950 &hsdma
->dma_requests
)) {
952 "Using %u as missing dma-requests property\n",
953 MTK_HSDMA_NR_VCHANS
);
956 hsdma
->pc
= devm_kcalloc(&pdev
->dev
, MTK_HSDMA_NR_MAX_PCHANS
,
957 sizeof(*hsdma
->pc
), GFP_KERNEL
);
961 hsdma
->vc
= devm_kcalloc(&pdev
->dev
, hsdma
->dma_requests
,
962 sizeof(*hsdma
->vc
), GFP_KERNEL
);
966 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
968 vc
->vc
.desc_free
= mtk_hsdma_vdesc_free
;
969 vchan_init(&vc
->vc
, dd
);
970 init_completion(&vc
->issue_completion
);
971 INIT_LIST_HEAD(&vc
->desc_hw_processing
);
974 err
= dma_async_device_register(dd
);
978 err
= of_dma_controller_register(pdev
->dev
.of_node
,
979 of_dma_xlate_by_chan_id
, hsdma
);
982 "MediaTek HSDMA OF registration failed %d\n", err
);
986 mtk_hsdma_hw_init(hsdma
);
988 err
= devm_request_irq(&pdev
->dev
, hsdma
->irq
,
990 dev_name(&pdev
->dev
), hsdma
);
993 "request_irq failed with err %d\n", err
);
997 platform_set_drvdata(pdev
, hsdma
);
999 dev_info(&pdev
->dev
, "MediaTek HSDMA driver registered\n");
1004 mtk_hsdma_hw_deinit(hsdma
);
1005 of_dma_controller_free(pdev
->dev
.of_node
);
1007 dma_async_device_unregister(dd
);
1012 static void mtk_hsdma_remove(struct platform_device
*pdev
)
1014 struct mtk_hsdma_device
*hsdma
= platform_get_drvdata(pdev
);
1015 struct mtk_hsdma_vchan
*vc
;
1019 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
1022 list_del(&vc
->vc
.chan
.device_node
);
1023 tasklet_kill(&vc
->vc
.task
);
1026 /* Disable DMA interrupt */
1027 mtk_dma_write(hsdma
, MTK_HSDMA_INT_ENABLE
, 0);
1029 /* Waits for any pending IRQ handlers to complete */
1030 synchronize_irq(hsdma
->irq
);
1032 /* Disable hardware */
1033 mtk_hsdma_hw_deinit(hsdma
);
1035 dma_async_device_unregister(&hsdma
->ddev
);
1036 of_dma_controller_free(pdev
->dev
.of_node
);
1039 static struct platform_driver mtk_hsdma_driver
= {
1040 .probe
= mtk_hsdma_probe
,
1041 .remove
= mtk_hsdma_remove
,
1043 .name
= KBUILD_MODNAME
,
1044 .of_match_table
= mtk_hsdma_match
,
1047 module_platform_driver(mtk_hsdma_driver
);
1049 MODULE_DESCRIPTION("MediaTek High-Speed DMA Controller Driver");
1050 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1051 MODULE_LICENSE("GPL v2");