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_device.h>
21 #include <linux/of_dma.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/refcount.h>
25 #include <linux/slab.h>
27 #include "../virt-dma.h"
29 #define MTK_HSDMA_USEC_POLL 20
30 #define MTK_HSDMA_TIMEOUT_POLL 200000
31 #define MTK_HSDMA_DMA_BUSWIDTHS BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
33 /* The default number of virtual channel */
34 #define MTK_HSDMA_NR_VCHANS 3
36 /* Only one physical channel supported */
37 #define MTK_HSDMA_NR_MAX_PCHANS 1
39 /* Macro for physical descriptor (PD) manipulation */
40 /* The number of PD which must be 2 of power */
41 #define MTK_DMA_SIZE 64
42 #define MTK_HSDMA_NEXT_DESP_IDX(x, y) (((x) + 1) & ((y) - 1))
43 #define MTK_HSDMA_LAST_DESP_IDX(x, y) (((x) - 1) & ((y) - 1))
44 #define MTK_HSDMA_MAX_LEN 0x3f80
45 #define MTK_HSDMA_ALIGN_SIZE 4
46 #define MTK_HSDMA_PLEN_MASK 0x3fff
47 #define MTK_HSDMA_DESC_PLEN(x) (((x) & MTK_HSDMA_PLEN_MASK) << 16)
48 #define MTK_HSDMA_DESC_PLEN_GET(x) (((x) >> 16) & MTK_HSDMA_PLEN_MASK)
50 /* Registers for underlying ring manipulation */
51 #define MTK_HSDMA_TX_BASE 0x0
52 #define MTK_HSDMA_TX_CNT 0x4
53 #define MTK_HSDMA_TX_CPU 0x8
54 #define MTK_HSDMA_TX_DMA 0xc
55 #define MTK_HSDMA_RX_BASE 0x100
56 #define MTK_HSDMA_RX_CNT 0x104
57 #define MTK_HSDMA_RX_CPU 0x108
58 #define MTK_HSDMA_RX_DMA 0x10c
60 /* Registers for global setup */
61 #define MTK_HSDMA_GLO 0x204
62 #define MTK_HSDMA_GLO_MULTI_DMA BIT(10)
63 #define MTK_HSDMA_TX_WB_DDONE BIT(6)
64 #define MTK_HSDMA_BURST_64BYTES (0x2 << 4)
65 #define MTK_HSDMA_GLO_RX_BUSY BIT(3)
66 #define MTK_HSDMA_GLO_RX_DMA BIT(2)
67 #define MTK_HSDMA_GLO_TX_BUSY BIT(1)
68 #define MTK_HSDMA_GLO_TX_DMA BIT(0)
69 #define MTK_HSDMA_GLO_DMA (MTK_HSDMA_GLO_TX_DMA | \
71 #define MTK_HSDMA_GLO_BUSY (MTK_HSDMA_GLO_RX_BUSY | \
72 MTK_HSDMA_GLO_TX_BUSY)
73 #define MTK_HSDMA_GLO_DEFAULT (MTK_HSDMA_GLO_TX_DMA | \
74 MTK_HSDMA_GLO_RX_DMA | \
75 MTK_HSDMA_TX_WB_DDONE | \
76 MTK_HSDMA_BURST_64BYTES | \
77 MTK_HSDMA_GLO_MULTI_DMA)
79 /* Registers for reset */
80 #define MTK_HSDMA_RESET 0x208
81 #define MTK_HSDMA_RST_TX BIT(0)
82 #define MTK_HSDMA_RST_RX BIT(16)
84 /* Registers for interrupt control */
85 #define MTK_HSDMA_DLYINT 0x20c
86 #define MTK_HSDMA_RXDLY_INT_EN BIT(15)
88 /* Interrupt fires when the pending number's more than the specified */
89 #define MTK_HSDMA_RXMAX_PINT(x) (((x) & 0x7f) << 8)
91 /* Interrupt fires when the pending time's more than the specified in 20 us */
92 #define MTK_HSDMA_RXMAX_PTIME(x) ((x) & 0x7f)
93 #define MTK_HSDMA_DLYINT_DEFAULT (MTK_HSDMA_RXDLY_INT_EN | \
94 MTK_HSDMA_RXMAX_PINT(20) | \
95 MTK_HSDMA_RXMAX_PTIME(20))
96 #define MTK_HSDMA_INT_STATUS 0x220
97 #define MTK_HSDMA_INT_ENABLE 0x228
98 #define MTK_HSDMA_INT_RXDONE BIT(16)
100 enum mtk_hsdma_vdesc_flag
{
101 MTK_HSDMA_VDESC_FINISHED
= 0x01,
104 #define IS_MTK_HSDMA_VDESC_FINISHED(x) ((x) == MTK_HSDMA_VDESC_FINISHED)
107 * struct mtk_hsdma_pdesc - This is the struct holding info describing physical
108 * descriptor (PD) and its placement must be kept at
109 * 4-bytes alignment in little endian order.
110 * @desc[1-4]: The control pad used to indicate hardware how to
111 * deal with the descriptor such as source and
112 * destination address and data length. The maximum
113 * data length each pdesc can handle is 0x3f80 bytes
115 struct mtk_hsdma_pdesc
{
120 } __packed
__aligned(4);
123 * struct mtk_hsdma_vdesc - This is the struct holding info describing virtual
125 * @vd: An instance for struct virt_dma_desc
126 * @len: The total data size device wants to move
127 * @residue: The remaining data size device will move
128 * @dest: The destination address device wants to move to
129 * @src: The source address device wants to move from
131 struct mtk_hsdma_vdesc
{
132 struct virt_dma_desc vd
;
140 * struct mtk_hsdma_cb - This is the struct holding extra info required for RX
141 * ring to know what relevant VD the the PD is being
143 * @vd: Pointer to the relevant VD.
144 * @flag: Flag indicating what action should be taken when VD
147 struct mtk_hsdma_cb
{
148 struct virt_dma_desc
*vd
;
149 enum mtk_hsdma_vdesc_flag flag
;
153 * struct mtk_hsdma_ring - This struct holds info describing underlying ring
155 * @txd: The descriptor TX ring which describes DMA source
157 * @rxd: The descriptor RX ring which describes DMA
158 * destination information
159 * @cb: The extra information pointed at by RX ring
160 * @tphys: The physical addr of TX ring
161 * @rphys: The physical addr of RX ring
162 * @cur_tptr: Pointer to the next free descriptor used by the host
163 * @cur_rptr: Pointer to the last done descriptor by the device
165 struct mtk_hsdma_ring
{
166 struct mtk_hsdma_pdesc
*txd
;
167 struct mtk_hsdma_pdesc
*rxd
;
168 struct mtk_hsdma_cb
*cb
;
176 * struct mtk_hsdma_pchan - This is the struct holding info describing physical
178 * @ring: An instance for the underlying ring
179 * @sz_ring: Total size allocated for the ring
180 * @nr_free: Total number of free rooms in the ring. It would
181 * be accessed and updated frequently between IRQ
182 * context and user context to reflect whether ring
183 * can accept requests from VD.
185 struct mtk_hsdma_pchan
{
186 struct mtk_hsdma_ring ring
;
192 * struct mtk_hsdma_vchan - This is the struct holding info describing virtual
194 * @vc: An instance for struct virt_dma_chan
195 * @issue_completion: The wait for all issued descriptors completited
196 * @issue_synchronize: Bool indicating channel synchronization starts
197 * @desc_hw_processing: List those descriptors the hardware is processing,
198 * which is protected by vc.lock
200 struct mtk_hsdma_vchan
{
201 struct virt_dma_chan vc
;
202 struct completion issue_completion
;
203 bool issue_synchronize
;
204 struct list_head desc_hw_processing
;
208 * struct mtk_hsdma_soc - This is the struct holding differences among SoCs
209 * @ddone: Bit mask for DDONE
210 * @ls0: Bit mask for LS0
212 struct mtk_hsdma_soc
{
218 * struct mtk_hsdma_device - This is the struct holding info describing HSDMA
220 * @ddev: An instance for struct dma_device
221 * @base: The mapped register I/O base
222 * @clk: The clock that device internal is using
223 * @irq: The IRQ that device are using
224 * @dma_requests: The number of VCs the device supports to
225 * @vc: The pointer to all available VCs
226 * @pc: The pointer to the underlying PC
227 * @pc_refcnt: Track how many VCs are using the PC
228 * @lock: Lock protect agaisting multiple VCs access PC
229 * @soc: The pointer to area holding differences among
232 struct mtk_hsdma_device
{
233 struct dma_device ddev
;
239 struct mtk_hsdma_vchan
*vc
;
240 struct mtk_hsdma_pchan
*pc
;
241 refcount_t pc_refcnt
;
243 /* Lock used to protect against multiple VCs access PC */
246 const struct mtk_hsdma_soc
*soc
;
249 static struct mtk_hsdma_device
*to_hsdma_dev(struct dma_chan
*chan
)
251 return container_of(chan
->device
, struct mtk_hsdma_device
, ddev
);
254 static inline struct mtk_hsdma_vchan
*to_hsdma_vchan(struct dma_chan
*chan
)
256 return container_of(chan
, struct mtk_hsdma_vchan
, vc
.chan
);
259 static struct mtk_hsdma_vdesc
*to_hsdma_vdesc(struct virt_dma_desc
*vd
)
261 return container_of(vd
, struct mtk_hsdma_vdesc
, vd
);
264 static struct device
*hsdma2dev(struct mtk_hsdma_device
*hsdma
)
266 return hsdma
->ddev
.dev
;
269 static u32
mtk_dma_read(struct mtk_hsdma_device
*hsdma
, u32 reg
)
271 return readl(hsdma
->base
+ reg
);
274 static void mtk_dma_write(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
276 writel(val
, hsdma
->base
+ reg
);
279 static void mtk_dma_rmw(struct mtk_hsdma_device
*hsdma
, u32 reg
,
284 val
= mtk_dma_read(hsdma
, reg
);
287 mtk_dma_write(hsdma
, reg
, val
);
290 static void mtk_dma_set(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
292 mtk_dma_rmw(hsdma
, reg
, 0, val
);
295 static void mtk_dma_clr(struct mtk_hsdma_device
*hsdma
, u32 reg
, u32 val
)
297 mtk_dma_rmw(hsdma
, reg
, val
, 0);
300 static void mtk_hsdma_vdesc_free(struct virt_dma_desc
*vd
)
302 kfree(container_of(vd
, struct mtk_hsdma_vdesc
, vd
));
305 static int mtk_hsdma_busy_wait(struct mtk_hsdma_device
*hsdma
)
309 return readl_poll_timeout(hsdma
->base
+ MTK_HSDMA_GLO
, status
,
310 !(status
& MTK_HSDMA_GLO_BUSY
),
312 MTK_HSDMA_TIMEOUT_POLL
);
315 static int mtk_hsdma_alloc_pchan(struct mtk_hsdma_device
*hsdma
,
316 struct mtk_hsdma_pchan
*pc
)
318 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
321 memset(pc
, 0, sizeof(*pc
));
324 * Allocate ring space where [0 ... MTK_DMA_SIZE - 1] is for TX ring
325 * and [MTK_DMA_SIZE ... 2 * MTK_DMA_SIZE - 1] is for RX ring.
327 pc
->sz_ring
= 2 * MTK_DMA_SIZE
* sizeof(*ring
->txd
);
328 ring
->txd
= dma_alloc_coherent(hsdma2dev(hsdma
), pc
->sz_ring
,
329 &ring
->tphys
, GFP_NOWAIT
);
333 ring
->rxd
= &ring
->txd
[MTK_DMA_SIZE
];
334 ring
->rphys
= ring
->tphys
+ MTK_DMA_SIZE
* sizeof(*ring
->txd
);
336 ring
->cur_rptr
= MTK_DMA_SIZE
- 1;
338 ring
->cb
= kcalloc(MTK_DMA_SIZE
, sizeof(*ring
->cb
), GFP_NOWAIT
);
344 atomic_set(&pc
->nr_free
, MTK_DMA_SIZE
- 1);
346 /* Disable HSDMA and wait for the completion */
347 mtk_dma_clr(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
348 err
= mtk_hsdma_busy_wait(hsdma
);
353 mtk_dma_set(hsdma
, MTK_HSDMA_RESET
,
354 MTK_HSDMA_RST_TX
| MTK_HSDMA_RST_RX
);
355 mtk_dma_clr(hsdma
, MTK_HSDMA_RESET
,
356 MTK_HSDMA_RST_TX
| MTK_HSDMA_RST_RX
);
358 /* Setup HSDMA initial pointer in the ring */
359 mtk_dma_write(hsdma
, MTK_HSDMA_TX_BASE
, ring
->tphys
);
360 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CNT
, MTK_DMA_SIZE
);
361 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, ring
->cur_tptr
);
362 mtk_dma_write(hsdma
, MTK_HSDMA_TX_DMA
, 0);
363 mtk_dma_write(hsdma
, MTK_HSDMA_RX_BASE
, ring
->rphys
);
364 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CNT
, MTK_DMA_SIZE
);
365 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, ring
->cur_rptr
);
366 mtk_dma_write(hsdma
, MTK_HSDMA_RX_DMA
, 0);
369 mtk_dma_set(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
371 /* Setup delayed interrupt */
372 mtk_dma_write(hsdma
, MTK_HSDMA_DLYINT
, MTK_HSDMA_DLYINT_DEFAULT
);
374 /* Enable interrupt */
375 mtk_dma_set(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
383 dma_free_coherent(hsdma2dev(hsdma
),
384 pc
->sz_ring
, ring
->txd
, ring
->tphys
);
388 static void mtk_hsdma_free_pchan(struct mtk_hsdma_device
*hsdma
,
389 struct mtk_hsdma_pchan
*pc
)
391 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
393 /* Disable HSDMA and then wait for the completion */
394 mtk_dma_clr(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DMA
);
395 mtk_hsdma_busy_wait(hsdma
);
397 /* Reset pointer in the ring */
398 mtk_dma_clr(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
399 mtk_dma_write(hsdma
, MTK_HSDMA_TX_BASE
, 0);
400 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CNT
, 0);
401 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, 0);
402 mtk_dma_write(hsdma
, MTK_HSDMA_RX_BASE
, 0);
403 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CNT
, 0);
404 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, MTK_DMA_SIZE
- 1);
408 dma_free_coherent(hsdma2dev(hsdma
),
409 pc
->sz_ring
, ring
->txd
, ring
->tphys
);
412 static int mtk_hsdma_issue_pending_vdesc(struct mtk_hsdma_device
*hsdma
,
413 struct mtk_hsdma_pchan
*pc
,
414 struct mtk_hsdma_vdesc
*hvd
)
416 struct mtk_hsdma_ring
*ring
= &pc
->ring
;
417 struct mtk_hsdma_pdesc
*txd
, *rxd
;
418 u16 reserved
, prev
, tlen
, num_sgs
;
421 /* Protect against PC is accessed by multiple VCs simultaneously */
422 spin_lock_irqsave(&hsdma
->lock
, flags
);
425 * Reserve rooms, where pc->nr_free is used to track how many free
426 * rooms in the ring being updated in user and IRQ context.
428 num_sgs
= DIV_ROUND_UP(hvd
->len
, MTK_HSDMA_MAX_LEN
);
429 reserved
= min_t(u16
, num_sgs
, atomic_read(&pc
->nr_free
));
432 spin_unlock_irqrestore(&hsdma
->lock
, flags
);
436 atomic_sub(reserved
, &pc
->nr_free
);
439 /* Limit size by PD capability for valid data moving */
440 tlen
= (hvd
->len
> MTK_HSDMA_MAX_LEN
) ?
441 MTK_HSDMA_MAX_LEN
: hvd
->len
;
444 * Setup PDs using the remaining VD info mapped on those
445 * reserved rooms. And since RXD is shared memory between the
446 * host and the device allocated by dma_alloc_coherent call,
447 * the helper macro WRITE_ONCE can ensure the data written to
448 * RAM would really happens.
450 txd
= &ring
->txd
[ring
->cur_tptr
];
451 WRITE_ONCE(txd
->desc1
, hvd
->src
);
452 WRITE_ONCE(txd
->desc2
,
453 hsdma
->soc
->ls0
| MTK_HSDMA_DESC_PLEN(tlen
));
455 rxd
= &ring
->rxd
[ring
->cur_tptr
];
456 WRITE_ONCE(rxd
->desc1
, hvd
->dest
);
457 WRITE_ONCE(rxd
->desc2
, MTK_HSDMA_DESC_PLEN(tlen
));
459 /* Associate VD, the PD belonged to */
460 ring
->cb
[ring
->cur_tptr
].vd
= &hvd
->vd
;
462 /* Move forward the pointer of TX ring */
463 ring
->cur_tptr
= MTK_HSDMA_NEXT_DESP_IDX(ring
->cur_tptr
,
466 /* Update VD with remaining data */
473 * Tagging flag for the last PD for VD will be responsible for
477 prev
= MTK_HSDMA_LAST_DESP_IDX(ring
->cur_tptr
, MTK_DMA_SIZE
);
478 ring
->cb
[prev
].flag
= MTK_HSDMA_VDESC_FINISHED
;
481 /* Ensure all changes indeed done before we're going on */
485 * Updating into hardware the pointer of TX ring lets HSDMA to take
486 * action for those pending PDs.
488 mtk_dma_write(hsdma
, MTK_HSDMA_TX_CPU
, ring
->cur_tptr
);
490 spin_unlock_irqrestore(&hsdma
->lock
, flags
);
495 static void mtk_hsdma_issue_vchan_pending(struct mtk_hsdma_device
*hsdma
,
496 struct mtk_hsdma_vchan
*hvc
)
498 struct virt_dma_desc
*vd
, *vd2
;
501 lockdep_assert_held(&hvc
->vc
.lock
);
503 list_for_each_entry_safe(vd
, vd2
, &hvc
->vc
.desc_issued
, node
) {
504 struct mtk_hsdma_vdesc
*hvd
;
506 hvd
= to_hsdma_vdesc(vd
);
508 /* Map VD into PC and all VCs shares a single PC */
509 err
= mtk_hsdma_issue_pending_vdesc(hsdma
, hsdma
->pc
, hvd
);
512 * Move VD from desc_issued to desc_hw_processing when entire
513 * VD is fit into available PDs. Otherwise, the uncompleted
514 * VDs would stay in list desc_issued and then restart the
515 * processing as soon as possible once underlying ring space
518 if (err
== -ENOSPC
|| hvd
->len
> 0)
522 * The extra list desc_hw_processing is used because
523 * hardware can't provide sufficient information allowing us
524 * to know what VDs are still working on the underlying ring.
525 * Through the additional list, it can help us to implement
526 * terminate_all, residue calculation and such thing needed
527 * to know detail descriptor status on the hardware.
529 list_move_tail(&vd
->node
, &hvc
->desc_hw_processing
);
533 static void mtk_hsdma_free_rooms_in_ring(struct mtk_hsdma_device
*hsdma
)
535 struct mtk_hsdma_vchan
*hvc
;
536 struct mtk_hsdma_pdesc
*rxd
;
537 struct mtk_hsdma_vdesc
*hvd
;
538 struct mtk_hsdma_pchan
*pc
;
539 struct mtk_hsdma_cb
*cb
;
540 int i
= MTK_DMA_SIZE
;
545 /* Read IRQ status */
546 status
= mtk_dma_read(hsdma
, MTK_HSDMA_INT_STATUS
);
547 if (unlikely(!(status
& MTK_HSDMA_INT_RXDONE
)))
553 * Using a fail-safe loop with iterations of up to MTK_DMA_SIZE to
554 * reclaim these finished descriptors: The most number of PDs the ISR
555 * can handle at one time shouldn't be more than MTK_DMA_SIZE so we
556 * take it as limited count instead of just using a dangerous infinite
560 next
= MTK_HSDMA_NEXT_DESP_IDX(pc
->ring
.cur_rptr
,
562 rxd
= &pc
->ring
.rxd
[next
];
565 * If MTK_HSDMA_DESC_DDONE is no specified, that means data
566 * moving for the PD is still under going.
568 desc2
= READ_ONCE(rxd
->desc2
);
569 if (!(desc2
& hsdma
->soc
->ddone
))
572 cb
= &pc
->ring
.cb
[next
];
573 if (unlikely(!cb
->vd
)) {
574 dev_err(hsdma2dev(hsdma
), "cb->vd cannot be null\n");
578 /* Update residue of VD the associated PD belonged to */
579 hvd
= to_hsdma_vdesc(cb
->vd
);
580 hvd
->residue
-= MTK_HSDMA_DESC_PLEN_GET(rxd
->desc2
);
582 /* Complete VD until the relevant last PD is finished */
583 if (IS_MTK_HSDMA_VDESC_FINISHED(cb
->flag
)) {
584 hvc
= to_hsdma_vchan(cb
->vd
->tx
.chan
);
586 spin_lock(&hvc
->vc
.lock
);
588 /* Remove VD from list desc_hw_processing */
589 list_del(&cb
->vd
->node
);
591 /* Add VD into list desc_completed */
592 vchan_cookie_complete(cb
->vd
);
594 if (hvc
->issue_synchronize
&&
595 list_empty(&hvc
->desc_hw_processing
)) {
596 complete(&hvc
->issue_completion
);
597 hvc
->issue_synchronize
= false;
599 spin_unlock(&hvc
->vc
.lock
);
607 * Recycle the RXD with the helper WRITE_ONCE that can ensure
608 * data written into RAM would really happens.
610 WRITE_ONCE(rxd
->desc1
, 0);
611 WRITE_ONCE(rxd
->desc2
, 0);
612 pc
->ring
.cur_rptr
= next
;
615 atomic_inc(&pc
->nr_free
);
618 /* Ensure all changes indeed done before we're going on */
621 /* Update CPU pointer for those completed PDs */
622 mtk_dma_write(hsdma
, MTK_HSDMA_RX_CPU
, pc
->ring
.cur_rptr
);
625 * Acking the pending IRQ allows hardware no longer to keep the used
626 * IRQ line in certain trigger state when software has completed all
627 * the finished physical descriptors.
629 if (atomic_read(&pc
->nr_free
) >= MTK_DMA_SIZE
- 1)
630 mtk_dma_write(hsdma
, MTK_HSDMA_INT_STATUS
, status
);
632 /* ASAP handles pending VDs in all VCs after freeing some rooms */
633 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
635 spin_lock(&hvc
->vc
.lock
);
636 mtk_hsdma_issue_vchan_pending(hsdma
, hvc
);
637 spin_unlock(&hvc
->vc
.lock
);
641 /* All completed PDs are cleaned up, so enable interrupt again */
642 mtk_dma_set(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
645 static irqreturn_t
mtk_hsdma_irq(int irq
, void *devid
)
647 struct mtk_hsdma_device
*hsdma
= devid
;
650 * Disable interrupt until all completed PDs are cleaned up in
651 * mtk_hsdma_free_rooms call.
653 mtk_dma_clr(hsdma
, MTK_HSDMA_INT_ENABLE
, MTK_HSDMA_INT_RXDONE
);
655 mtk_hsdma_free_rooms_in_ring(hsdma
);
660 static struct virt_dma_desc
*mtk_hsdma_find_active_desc(struct dma_chan
*c
,
663 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
664 struct virt_dma_desc
*vd
;
666 list_for_each_entry(vd
, &hvc
->desc_hw_processing
, node
)
667 if (vd
->tx
.cookie
== cookie
)
670 list_for_each_entry(vd
, &hvc
->vc
.desc_issued
, node
)
671 if (vd
->tx
.cookie
== cookie
)
677 static enum dma_status
mtk_hsdma_tx_status(struct dma_chan
*c
,
679 struct dma_tx_state
*txstate
)
681 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
682 struct mtk_hsdma_vdesc
*hvd
;
683 struct virt_dma_desc
*vd
;
688 ret
= dma_cookie_status(c
, cookie
, txstate
);
689 if (ret
== DMA_COMPLETE
|| !txstate
)
692 spin_lock_irqsave(&hvc
->vc
.lock
, flags
);
693 vd
= mtk_hsdma_find_active_desc(c
, cookie
);
694 spin_unlock_irqrestore(&hvc
->vc
.lock
, flags
);
697 hvd
= to_hsdma_vdesc(vd
);
698 bytes
= hvd
->residue
;
701 dma_set_residue(txstate
, bytes
);
706 static void mtk_hsdma_issue_pending(struct dma_chan
*c
)
708 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
709 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
712 spin_lock_irqsave(&hvc
->vc
.lock
, flags
);
714 if (vchan_issue_pending(&hvc
->vc
))
715 mtk_hsdma_issue_vchan_pending(hsdma
, hvc
);
717 spin_unlock_irqrestore(&hvc
->vc
.lock
, flags
);
720 static struct dma_async_tx_descriptor
*
721 mtk_hsdma_prep_dma_memcpy(struct dma_chan
*c
, dma_addr_t dest
,
722 dma_addr_t src
, size_t len
, unsigned long flags
)
724 struct mtk_hsdma_vdesc
*hvd
;
726 hvd
= kzalloc(sizeof(*hvd
), GFP_NOWAIT
);
735 return vchan_tx_prep(to_virt_chan(c
), &hvd
->vd
, flags
);
738 static int mtk_hsdma_free_inactive_desc(struct dma_chan
*c
)
740 struct virt_dma_chan
*vc
= to_virt_chan(c
);
744 spin_lock_irqsave(&vc
->lock
, flags
);
745 list_splice_tail_init(&vc
->desc_allocated
, &head
);
746 list_splice_tail_init(&vc
->desc_submitted
, &head
);
747 list_splice_tail_init(&vc
->desc_issued
, &head
);
748 spin_unlock_irqrestore(&vc
->lock
, flags
);
750 /* At the point, we don't expect users put descriptor into VC again */
751 vchan_dma_desc_free_list(vc
, &head
);
756 static void mtk_hsdma_free_active_desc(struct dma_chan
*c
)
758 struct mtk_hsdma_vchan
*hvc
= to_hsdma_vchan(c
);
759 bool sync_needed
= false;
762 * Once issue_synchronize is being set, which means once the hardware
763 * consumes all descriptors for the channel in the ring, the
764 * synchronization must be be notified immediately it is completed.
766 spin_lock(&hvc
->vc
.lock
);
767 if (!list_empty(&hvc
->desc_hw_processing
)) {
768 hvc
->issue_synchronize
= true;
771 spin_unlock(&hvc
->vc
.lock
);
774 wait_for_completion(&hvc
->issue_completion
);
776 * At the point, we expect that all remaining descriptors in the ring
777 * for the channel should be all processing done.
779 WARN_ONCE(!list_empty(&hvc
->desc_hw_processing
),
780 "Desc pending still in list desc_hw_processing\n");
782 /* Free all descriptors in list desc_completed */
783 vchan_synchronize(&hvc
->vc
);
785 WARN_ONCE(!list_empty(&hvc
->vc
.desc_completed
),
786 "Desc pending still in list desc_completed\n");
789 static int mtk_hsdma_terminate_all(struct dma_chan
*c
)
792 * Free pending descriptors not processed yet by hardware that have
793 * previously been submitted to the channel.
795 mtk_hsdma_free_inactive_desc(c
);
798 * However, the DMA engine doesn't provide any way to stop these
799 * descriptors being processed currently by hardware. The only way is
800 * to just waiting until these descriptors are all processed completely
801 * through mtk_hsdma_free_active_desc call.
803 mtk_hsdma_free_active_desc(c
);
808 static int mtk_hsdma_alloc_chan_resources(struct dma_chan
*c
)
810 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
814 * Since HSDMA has only one PC, the resource for PC is being allocated
815 * when the first VC is being created and the other VCs would run on
818 if (!refcount_read(&hsdma
->pc_refcnt
)) {
819 err
= mtk_hsdma_alloc_pchan(hsdma
, hsdma
->pc
);
823 * refcount_inc would complain increment on 0; use-after-free.
824 * Thus, we need to explicitly set it as 1 initially.
826 refcount_set(&hsdma
->pc_refcnt
, 1);
828 refcount_inc(&hsdma
->pc_refcnt
);
834 static void mtk_hsdma_free_chan_resources(struct dma_chan
*c
)
836 struct mtk_hsdma_device
*hsdma
= to_hsdma_dev(c
);
838 /* Free all descriptors in all lists on the VC */
839 mtk_hsdma_terminate_all(c
);
841 /* The resource for PC is not freed until all the VCs are destroyed */
842 if (!refcount_dec_and_test(&hsdma
->pc_refcnt
))
845 mtk_hsdma_free_pchan(hsdma
, hsdma
->pc
);
848 static int mtk_hsdma_hw_init(struct mtk_hsdma_device
*hsdma
)
852 pm_runtime_enable(hsdma2dev(hsdma
));
853 pm_runtime_get_sync(hsdma2dev(hsdma
));
855 err
= clk_prepare_enable(hsdma
->clk
);
859 mtk_dma_write(hsdma
, MTK_HSDMA_INT_ENABLE
, 0);
860 mtk_dma_write(hsdma
, MTK_HSDMA_GLO
, MTK_HSDMA_GLO_DEFAULT
);
865 static int mtk_hsdma_hw_deinit(struct mtk_hsdma_device
*hsdma
)
867 mtk_dma_write(hsdma
, MTK_HSDMA_GLO
, 0);
869 clk_disable_unprepare(hsdma
->clk
);
871 pm_runtime_put_sync(hsdma2dev(hsdma
));
872 pm_runtime_disable(hsdma2dev(hsdma
));
877 static const struct mtk_hsdma_soc mt7623_soc
= {
882 static const struct mtk_hsdma_soc mt7622_soc
= {
887 static const struct of_device_id mtk_hsdma_match
[] = {
888 { .compatible
= "mediatek,mt7623-hsdma", .data
= &mt7623_soc
},
889 { .compatible
= "mediatek,mt7622-hsdma", .data
= &mt7622_soc
},
892 MODULE_DEVICE_TABLE(of
, mtk_hsdma_match
);
894 static int mtk_hsdma_probe(struct platform_device
*pdev
)
896 struct mtk_hsdma_device
*hsdma
;
897 struct mtk_hsdma_vchan
*vc
;
898 struct dma_device
*dd
;
899 struct resource
*res
;
902 hsdma
= devm_kzalloc(&pdev
->dev
, sizeof(*hsdma
), GFP_KERNEL
);
908 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
909 hsdma
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
910 if (IS_ERR(hsdma
->base
))
911 return PTR_ERR(hsdma
->base
);
913 hsdma
->soc
= of_device_get_match_data(&pdev
->dev
);
915 dev_err(&pdev
->dev
, "No device match found\n");
919 hsdma
->clk
= devm_clk_get(&pdev
->dev
, "hsdma");
920 if (IS_ERR(hsdma
->clk
)) {
921 dev_err(&pdev
->dev
, "No clock for %s\n",
922 dev_name(&pdev
->dev
));
923 return PTR_ERR(hsdma
->clk
);
926 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
928 dev_err(&pdev
->dev
, "No irq resource for %s\n",
929 dev_name(&pdev
->dev
));
932 hsdma
->irq
= res
->start
;
934 refcount_set(&hsdma
->pc_refcnt
, 0);
935 spin_lock_init(&hsdma
->lock
);
937 dma_cap_set(DMA_MEMCPY
, dd
->cap_mask
);
939 dd
->copy_align
= MTK_HSDMA_ALIGN_SIZE
;
940 dd
->device_alloc_chan_resources
= mtk_hsdma_alloc_chan_resources
;
941 dd
->device_free_chan_resources
= mtk_hsdma_free_chan_resources
;
942 dd
->device_tx_status
= mtk_hsdma_tx_status
;
943 dd
->device_issue_pending
= mtk_hsdma_issue_pending
;
944 dd
->device_prep_dma_memcpy
= mtk_hsdma_prep_dma_memcpy
;
945 dd
->device_terminate_all
= mtk_hsdma_terminate_all
;
946 dd
->src_addr_widths
= MTK_HSDMA_DMA_BUSWIDTHS
;
947 dd
->dst_addr_widths
= MTK_HSDMA_DMA_BUSWIDTHS
;
948 dd
->directions
= BIT(DMA_MEM_TO_MEM
);
949 dd
->residue_granularity
= DMA_RESIDUE_GRANULARITY_SEGMENT
;
950 dd
->dev
= &pdev
->dev
;
951 INIT_LIST_HEAD(&dd
->channels
);
953 hsdma
->dma_requests
= MTK_HSDMA_NR_VCHANS
;
954 if (pdev
->dev
.of_node
&& of_property_read_u32(pdev
->dev
.of_node
,
956 &hsdma
->dma_requests
)) {
958 "Using %u as missing dma-requests property\n",
959 MTK_HSDMA_NR_VCHANS
);
962 hsdma
->pc
= devm_kcalloc(&pdev
->dev
, MTK_HSDMA_NR_MAX_PCHANS
,
963 sizeof(*hsdma
->pc
), GFP_KERNEL
);
967 hsdma
->vc
= devm_kcalloc(&pdev
->dev
, hsdma
->dma_requests
,
968 sizeof(*hsdma
->vc
), GFP_KERNEL
);
972 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
974 vc
->vc
.desc_free
= mtk_hsdma_vdesc_free
;
975 vchan_init(&vc
->vc
, dd
);
976 init_completion(&vc
->issue_completion
);
977 INIT_LIST_HEAD(&vc
->desc_hw_processing
);
980 err
= dma_async_device_register(dd
);
984 err
= of_dma_controller_register(pdev
->dev
.of_node
,
985 of_dma_xlate_by_chan_id
, hsdma
);
988 "MediaTek HSDMA OF registration failed %d\n", err
);
992 mtk_hsdma_hw_init(hsdma
);
994 err
= devm_request_irq(&pdev
->dev
, hsdma
->irq
,
996 dev_name(&pdev
->dev
), hsdma
);
999 "request_irq failed with err %d\n", err
);
1003 platform_set_drvdata(pdev
, hsdma
);
1005 dev_info(&pdev
->dev
, "MediaTek HSDMA driver registered\n");
1010 of_dma_controller_free(pdev
->dev
.of_node
);
1012 dma_async_device_unregister(dd
);
1017 static int mtk_hsdma_remove(struct platform_device
*pdev
)
1019 struct mtk_hsdma_device
*hsdma
= platform_get_drvdata(pdev
);
1020 struct mtk_hsdma_vchan
*vc
;
1024 for (i
= 0; i
< hsdma
->dma_requests
; i
++) {
1027 list_del(&vc
->vc
.chan
.device_node
);
1028 tasklet_kill(&vc
->vc
.task
);
1031 /* Disable DMA interrupt */
1032 mtk_dma_write(hsdma
, MTK_HSDMA_INT_ENABLE
, 0);
1034 /* Waits for any pending IRQ handlers to complete */
1035 synchronize_irq(hsdma
->irq
);
1037 /* Disable hardware */
1038 mtk_hsdma_hw_deinit(hsdma
);
1040 dma_async_device_unregister(&hsdma
->ddev
);
1041 of_dma_controller_free(pdev
->dev
.of_node
);
1046 static struct platform_driver mtk_hsdma_driver
= {
1047 .probe
= mtk_hsdma_probe
,
1048 .remove
= mtk_hsdma_remove
,
1050 .name
= KBUILD_MODNAME
,
1051 .of_match_table
= mtk_hsdma_match
,
1054 module_platform_driver(mtk_hsdma_driver
);
1056 MODULE_DESCRIPTION("MediaTek High-Speed DMA Controller Driver");
1057 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
1058 MODULE_LICENSE("GPL v2");