1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Loongson-2 APB DMA Controller
5 * Copyright (C) 2017-2023 Loongson Corporation
9 #include <linux/dma-mapping.h>
10 #include <linux/dmapool.h>
11 #include <linux/interrupt.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/module.h>
16 #include <linux/of_dma.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include "dmaengine.h"
23 /* Global Configuration Register */
24 #define LDMA_ORDER_ERG 0x0
26 /* Bitfield definitions */
28 /* Bitfields in Global Configuration Register */
29 #define LDMA_64BIT_EN BIT(0) /* 1: 64 bit support */
30 #define LDMA_UNCOHERENT_EN BIT(1) /* 0: cache, 1: uncache */
31 #define LDMA_ASK_VALID BIT(2)
32 #define LDMA_START BIT(3) /* DMA start operation */
33 #define LDMA_STOP BIT(4) /* DMA stop operation */
34 #define LDMA_CONFIG_MASK GENMASK(4, 0) /* DMA controller config bits mask */
36 /* Bitfields in ndesc_addr field of HW descriptor */
37 #define LDMA_DESC_EN BIT(0) /*1: The next descriptor is valid */
38 #define LDMA_DESC_ADDR_LOW GENMASK(31, 1)
40 /* Bitfields in cmd field of HW descriptor */
41 #define LDMA_INT BIT(1) /* Enable DMA interrupts */
42 #define LDMA_DATA_DIRECTION BIT(12) /* 1: write to device, 0: read from device */
44 #define LDMA_SLAVE_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
45 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
47 #define LDMA_MAX_TRANS_LEN U32_MAX
49 /*-- descriptors -----------------------------------------------------*/
52 * struct ls2x_dma_hw_desc - DMA HW descriptor
53 * @ndesc_addr: the next descriptor low address.
54 * @mem_addr: memory low address.
55 * @apb_addr: device buffer address.
56 * @len: length of a piece of carried content, in words.
57 * @step_len: length between two moved memory data blocks.
58 * @step_times: number of blocks to be carried in a single DMA operation.
59 * @cmd: descriptor command or state.
61 * @high_ndesc_addr: the next descriptor high address.
62 * @high_mem_addr: memory high address.
65 struct ls2x_dma_hw_desc
{
80 * struct ls2x_dma_sg - ls2x dma scatter gather entry
81 * @hw: the pointer to DMA HW descriptor.
82 * @llp: physical address of the DMA HW descriptor.
83 * @phys: destination or source address(mem).
84 * @len: number of Bytes to read.
87 struct ls2x_dma_hw_desc
*hw
;
94 * struct ls2x_dma_desc - software descriptor
95 * @vdesc: pointer to the virtual dma descriptor.
96 * @cyclic: flag to dma cyclic
97 * @burst_size: burst size of transaction, in words.
98 * @desc_num: number of sg entries.
99 * @direction: transfer direction, to or from device.
100 * @status: dma controller status.
103 struct ls2x_dma_desc
{
104 struct virt_dma_desc vdesc
;
108 enum dma_transfer_direction direction
;
109 enum dma_status status
;
110 struct ls2x_dma_sg sg
[] __counted_by(desc_num
);
113 /*-- Channels --------------------------------------------------------*/
116 * struct ls2x_dma_chan - internal representation of an LS2X APB DMA channel
117 * @vchan: virtual dma channel entry.
118 * @desc: pointer to the ls2x sw dma descriptor.
119 * @pool: hw desc table
121 * @sconfig: configuration for slave transfers, passed via .device_config
123 struct ls2x_dma_chan
{
124 struct virt_dma_chan vchan
;
125 struct ls2x_dma_desc
*desc
;
128 struct dma_slave_config sconfig
;
131 /*-- Controller ------------------------------------------------------*/
134 * struct ls2x_dma_priv - LS2X APB DMAC specific information
135 * @ddev: dmaengine dma_device object members
136 * @dma_clk: DMAC clock source
137 * @regs: memory mapped register base
138 * @lchan: channel to store ls2x_dma_chan structures
140 struct ls2x_dma_priv
{
141 struct dma_device ddev
;
144 struct ls2x_dma_chan lchan
;
147 /*-- Helper functions ------------------------------------------------*/
149 static inline struct ls2x_dma_desc
*to_ldma_desc(struct virt_dma_desc
*vdesc
)
151 return container_of(vdesc
, struct ls2x_dma_desc
, vdesc
);
154 static inline struct ls2x_dma_chan
*to_ldma_chan(struct dma_chan
*chan
)
156 return container_of(chan
, struct ls2x_dma_chan
, vchan
.chan
);
159 static inline struct ls2x_dma_priv
*to_ldma_priv(struct dma_device
*ddev
)
161 return container_of(ddev
, struct ls2x_dma_priv
, ddev
);
164 static struct device
*chan2dev(struct dma_chan
*chan
)
166 return &chan
->dev
->device
;
169 static void ls2x_dma_desc_free(struct virt_dma_desc
*vdesc
)
171 struct ls2x_dma_chan
*lchan
= to_ldma_chan(vdesc
->tx
.chan
);
172 struct ls2x_dma_desc
*desc
= to_ldma_desc(vdesc
);
175 for (i
= 0; i
< desc
->desc_num
; i
++) {
177 dma_pool_free(lchan
->pool
, desc
->sg
[i
].hw
,
184 static void ls2x_dma_write_cmd(struct ls2x_dma_chan
*lchan
, bool cmd
)
186 struct ls2x_dma_priv
*priv
= to_ldma_priv(lchan
->vchan
.chan
.device
);
189 val
= lo_hi_readq(priv
->regs
+ LDMA_ORDER_ERG
) & ~LDMA_CONFIG_MASK
;
190 val
|= LDMA_64BIT_EN
| cmd
;
191 lo_hi_writeq(val
, priv
->regs
+ LDMA_ORDER_ERG
);
194 static void ls2x_dma_start_transfer(struct ls2x_dma_chan
*lchan
)
196 struct ls2x_dma_priv
*priv
= to_ldma_priv(lchan
->vchan
.chan
.device
);
197 struct ls2x_dma_sg
*ldma_sg
;
198 struct virt_dma_desc
*vdesc
;
201 /* Get the next descriptor */
202 vdesc
= vchan_next_desc(&lchan
->vchan
);
208 list_del(&vdesc
->node
);
209 lchan
->desc
= to_ldma_desc(vdesc
);
210 ldma_sg
= &lchan
->desc
->sg
[0];
213 lo_hi_writeq(0, priv
->regs
+ LDMA_ORDER_ERG
);
214 val
= (ldma_sg
->llp
& ~LDMA_CONFIG_MASK
) | LDMA_64BIT_EN
| LDMA_START
;
215 lo_hi_writeq(val
, priv
->regs
+ LDMA_ORDER_ERG
);
218 static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan
*lchan
)
220 u32 maxburst
, buswidth
;
222 /* Reject definitely invalid configurations */
223 if ((lchan
->sconfig
.src_addr_width
& LDMA_SLAVE_BUSWIDTHS
) &&
224 (lchan
->sconfig
.dst_addr_width
& LDMA_SLAVE_BUSWIDTHS
))
227 if (lchan
->sconfig
.direction
== DMA_MEM_TO_DEV
) {
228 maxburst
= lchan
->sconfig
.dst_maxburst
;
229 buswidth
= lchan
->sconfig
.dst_addr_width
;
231 maxburst
= lchan
->sconfig
.src_maxburst
;
232 buswidth
= lchan
->sconfig
.src_addr_width
;
235 /* If maxburst is zero, fallback to LDMA_MAX_TRANS_LEN */
236 return maxburst
? (maxburst
* buswidth
) >> 2 : LDMA_MAX_TRANS_LEN
;
239 static void ls2x_dma_fill_desc(struct ls2x_dma_chan
*lchan
, u32 sg_index
,
240 struct ls2x_dma_desc
*desc
)
242 struct ls2x_dma_sg
*ldma_sg
= &desc
->sg
[sg_index
];
243 u32 num_segments
, segment_size
;
245 if (desc
->direction
== DMA_MEM_TO_DEV
) {
246 ldma_sg
->hw
->cmd
= LDMA_INT
| LDMA_DATA_DIRECTION
;
247 ldma_sg
->hw
->apb_addr
= lchan
->sconfig
.dst_addr
;
249 ldma_sg
->hw
->cmd
= LDMA_INT
;
250 ldma_sg
->hw
->apb_addr
= lchan
->sconfig
.src_addr
;
253 ldma_sg
->hw
->mem_addr
= lower_32_bits(ldma_sg
->phys
);
254 ldma_sg
->hw
->high_mem_addr
= upper_32_bits(ldma_sg
->phys
);
256 /* Split into multiple equally sized segments if necessary */
257 num_segments
= DIV_ROUND_UP((ldma_sg
->len
+ 3) >> 2, desc
->burst_size
);
258 segment_size
= DIV_ROUND_UP((ldma_sg
->len
+ 3) >> 2, num_segments
);
260 /* Word count register takes input in words */
261 ldma_sg
->hw
->len
= segment_size
;
262 ldma_sg
->hw
->step_times
= num_segments
;
263 ldma_sg
->hw
->step_len
= 0;
265 /* lets make a link list */
267 desc
->sg
[sg_index
- 1].hw
->ndesc_addr
= ldma_sg
->llp
| LDMA_DESC_EN
;
268 desc
->sg
[sg_index
- 1].hw
->high_ndesc_addr
= upper_32_bits(ldma_sg
->llp
);
272 /*-- DMA Engine API --------------------------------------------------*/
275 * ls2x_dma_alloc_chan_resources - allocate resources for DMA channel
276 * @chan: allocate descriptor resources for this channel
278 * return - the number of allocated descriptors
280 static int ls2x_dma_alloc_chan_resources(struct dma_chan
*chan
)
282 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
284 /* Create a pool of consistent memory blocks for hardware descriptors */
285 lchan
->pool
= dma_pool_create(dev_name(chan2dev(chan
)),
286 chan
->device
->dev
, PAGE_SIZE
,
287 __alignof__(struct ls2x_dma_hw_desc
), 0);
289 dev_err(chan2dev(chan
), "No memory for descriptors\n");
297 * ls2x_dma_free_chan_resources - free all channel resources
300 static void ls2x_dma_free_chan_resources(struct dma_chan
*chan
)
302 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
304 vchan_free_chan_resources(to_virt_chan(chan
));
305 dma_pool_destroy(lchan
->pool
);
310 * ls2x_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
312 * @sgl: scatterlist to transfer to/from
313 * @sg_len: number of entries in @scatterlist
314 * @direction: DMA direction
315 * @flags: tx descriptor status flags
316 * @context: transaction context (ignored)
318 * Return: Async transaction descriptor on success and NULL on failure
320 static struct dma_async_tx_descriptor
*
321 ls2x_dma_prep_slave_sg(struct dma_chan
*chan
, struct scatterlist
*sgl
,
322 u32 sg_len
, enum dma_transfer_direction direction
,
323 unsigned long flags
, void *context
)
325 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
326 struct ls2x_dma_desc
*desc
;
327 struct scatterlist
*sg
;
331 if (unlikely(!sg_len
|| !is_slave_direction(direction
)))
334 burst_size
= ls2x_dmac_detect_burst(lchan
);
338 desc
= kzalloc(struct_size(desc
, sg
, sg_len
), GFP_NOWAIT
);
342 desc
->desc_num
= sg_len
;
343 desc
->direction
= direction
;
344 desc
->burst_size
= burst_size
;
346 for_each_sg(sgl
, sg
, sg_len
, i
) {
347 struct ls2x_dma_sg
*ldma_sg
= &desc
->sg
[i
];
349 /* Allocate DMA capable memory for hardware descriptor */
350 ldma_sg
->hw
= dma_pool_alloc(lchan
->pool
, GFP_NOWAIT
, &ldma_sg
->llp
);
353 ls2x_dma_desc_free(&desc
->vdesc
);
357 ldma_sg
->phys
= sg_dma_address(sg
);
358 ldma_sg
->len
= sg_dma_len(sg
);
360 ls2x_dma_fill_desc(lchan
, i
, desc
);
363 /* Setting the last descriptor enable bit */
364 desc
->sg
[sg_len
- 1].hw
->ndesc_addr
&= ~LDMA_DESC_EN
;
365 desc
->status
= DMA_IN_PROGRESS
;
367 return vchan_tx_prep(&lchan
->vchan
, &desc
->vdesc
, flags
);
371 * ls2x_dma_prep_dma_cyclic - prepare the cyclic DMA transfer
372 * @chan: the DMA channel to prepare
373 * @buf_addr: physical DMA address where the buffer starts
374 * @buf_len: total number of bytes for the entire buffer
375 * @period_len: number of bytes for each period
376 * @direction: transfer direction, to or from device
377 * @flags: tx descriptor status flags
379 * Return: Async transaction descriptor on success and NULL on failure
381 static struct dma_async_tx_descriptor
*
382 ls2x_dma_prep_dma_cyclic(struct dma_chan
*chan
, dma_addr_t buf_addr
, size_t buf_len
,
383 size_t period_len
, enum dma_transfer_direction direction
,
386 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
387 struct ls2x_dma_desc
*desc
;
392 if (unlikely(!buf_len
|| !period_len
))
395 if (unlikely(!is_slave_direction(direction
)))
398 burst_size
= ls2x_dmac_detect_burst(lchan
);
402 num_periods
= buf_len
/ period_len
;
403 desc
= kzalloc(struct_size(desc
, sg
, num_periods
), GFP_NOWAIT
);
407 desc
->desc_num
= num_periods
;
408 desc
->direction
= direction
;
409 desc
->burst_size
= burst_size
;
411 /* Build cyclic linked list */
412 for (i
= 0; i
< num_periods
; i
++) {
413 struct ls2x_dma_sg
*ldma_sg
= &desc
->sg
[i
];
415 /* Allocate DMA capable memory for hardware descriptor */
416 ldma_sg
->hw
= dma_pool_alloc(lchan
->pool
, GFP_NOWAIT
, &ldma_sg
->llp
);
419 ls2x_dma_desc_free(&desc
->vdesc
);
423 ldma_sg
->phys
= buf_addr
+ period_len
* i
;
424 ldma_sg
->len
= period_len
;
426 ls2x_dma_fill_desc(lchan
, i
, desc
);
429 /* Lets make a cyclic list */
430 desc
->sg
[num_periods
- 1].hw
->ndesc_addr
= desc
->sg
[0].llp
| LDMA_DESC_EN
;
431 desc
->sg
[num_periods
- 1].hw
->high_ndesc_addr
= upper_32_bits(desc
->sg
[0].llp
);
433 desc
->status
= DMA_IN_PROGRESS
;
435 return vchan_tx_prep(&lchan
->vchan
, &desc
->vdesc
, flags
);
439 * ls2x_slave_config - set slave configuration for channel
441 * @cfg: slave configuration
443 * Sets slave configuration for channel
445 static int ls2x_dma_slave_config(struct dma_chan
*chan
,
446 struct dma_slave_config
*config
)
448 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
450 memcpy(&lchan
->sconfig
, config
, sizeof(*config
));
455 * ls2x_dma_issue_pending - push pending transactions to the hardware
458 * When this function is called, all pending transactions are pushed to the
459 * hardware and executed.
461 static void ls2x_dma_issue_pending(struct dma_chan
*chan
)
463 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
466 spin_lock_irqsave(&lchan
->vchan
.lock
, flags
);
467 if (vchan_issue_pending(&lchan
->vchan
) && !lchan
->desc
)
468 ls2x_dma_start_transfer(lchan
);
469 spin_unlock_irqrestore(&lchan
->vchan
.lock
, flags
);
473 * ls2x_dma_terminate_all - terminate all transactions
476 * Stops all DMA transactions.
478 static int ls2x_dma_terminate_all(struct dma_chan
*chan
)
480 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
484 spin_lock_irqsave(&lchan
->vchan
.lock
, flags
);
485 /* Setting stop cmd */
486 ls2x_dma_write_cmd(lchan
, LDMA_STOP
);
488 vchan_terminate_vdesc(&lchan
->desc
->vdesc
);
492 vchan_get_all_descriptors(&lchan
->vchan
, &head
);
493 spin_unlock_irqrestore(&lchan
->vchan
.lock
, flags
);
495 vchan_dma_desc_free_list(&lchan
->vchan
, &head
);
500 * ls2x_dma_synchronize - Synchronizes the termination of transfers to the
504 static void ls2x_dma_synchronize(struct dma_chan
*chan
)
506 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
508 vchan_synchronize(&lchan
->vchan
);
511 static int ls2x_dma_pause(struct dma_chan
*chan
)
513 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
516 spin_lock_irqsave(&lchan
->vchan
.lock
, flags
);
517 if (lchan
->desc
&& lchan
->desc
->status
== DMA_IN_PROGRESS
) {
518 ls2x_dma_write_cmd(lchan
, LDMA_STOP
);
519 lchan
->desc
->status
= DMA_PAUSED
;
521 spin_unlock_irqrestore(&lchan
->vchan
.lock
, flags
);
526 static int ls2x_dma_resume(struct dma_chan
*chan
)
528 struct ls2x_dma_chan
*lchan
= to_ldma_chan(chan
);
531 spin_lock_irqsave(&lchan
->vchan
.lock
, flags
);
532 if (lchan
->desc
&& lchan
->desc
->status
== DMA_PAUSED
) {
533 lchan
->desc
->status
= DMA_IN_PROGRESS
;
534 ls2x_dma_write_cmd(lchan
, LDMA_START
);
536 spin_unlock_irqrestore(&lchan
->vchan
.lock
, flags
);
542 * ls2x_dma_isr - LS2X DMA Interrupt handler
544 * @dev_id: Pointer to ls2x_dma_chan
546 * Return: IRQ_HANDLED/IRQ_NONE
548 static irqreturn_t
ls2x_dma_isr(int irq
, void *dev_id
)
550 struct ls2x_dma_chan
*lchan
= dev_id
;
551 struct ls2x_dma_desc
*desc
;
553 spin_lock(&lchan
->vchan
.lock
);
557 vchan_cyclic_callback(&desc
->vdesc
);
559 desc
->status
= DMA_COMPLETE
;
560 vchan_cookie_complete(&desc
->vdesc
);
561 ls2x_dma_start_transfer(lchan
);
564 /* ls2x_dma_start_transfer() updates lchan->desc */
566 ls2x_dma_write_cmd(lchan
, LDMA_STOP
);
568 spin_unlock(&lchan
->vchan
.lock
);
573 static int ls2x_dma_chan_init(struct platform_device
*pdev
,
574 struct ls2x_dma_priv
*priv
)
576 struct ls2x_dma_chan
*lchan
= &priv
->lchan
;
577 struct device
*dev
= &pdev
->dev
;
580 lchan
->irq
= platform_get_irq(pdev
, 0);
584 ret
= devm_request_irq(dev
, lchan
->irq
, ls2x_dma_isr
, IRQF_TRIGGER_RISING
,
585 dev_name(&pdev
->dev
), lchan
);
589 /* Initialize channels related values */
590 INIT_LIST_HEAD(&priv
->ddev
.channels
);
591 lchan
->vchan
.desc_free
= ls2x_dma_desc_free
;
592 vchan_init(&lchan
->vchan
, &priv
->ddev
);
598 * ls2x_dma_probe - Driver probe function
599 * @pdev: Pointer to the platform_device structure
601 * Return: '0' on success and failure value on error
603 static int ls2x_dma_probe(struct platform_device
*pdev
)
605 struct device
*dev
= &pdev
->dev
;
606 struct ls2x_dma_priv
*priv
;
607 struct dma_device
*ddev
;
610 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
614 priv
->regs
= devm_platform_ioremap_resource(pdev
, 0);
615 if (IS_ERR(priv
->regs
))
616 return dev_err_probe(dev
, PTR_ERR(priv
->regs
),
617 "devm_platform_ioremap_resource failed.\n");
619 priv
->dma_clk
= devm_clk_get(&pdev
->dev
, NULL
);
620 if (IS_ERR(priv
->dma_clk
))
621 return dev_err_probe(dev
, PTR_ERR(priv
->dma_clk
), "devm_clk_get failed.\n");
623 ret
= clk_prepare_enable(priv
->dma_clk
);
625 return dev_err_probe(dev
, ret
, "clk_prepare_enable failed.\n");
627 ret
= ls2x_dma_chan_init(pdev
, priv
);
633 dma_cap_zero(ddev
->cap_mask
);
634 dma_cap_set(DMA_SLAVE
, ddev
->cap_mask
);
635 dma_cap_set(DMA_CYCLIC
, ddev
->cap_mask
);
637 ddev
->device_alloc_chan_resources
= ls2x_dma_alloc_chan_resources
;
638 ddev
->device_free_chan_resources
= ls2x_dma_free_chan_resources
;
639 ddev
->device_tx_status
= dma_cookie_status
;
640 ddev
->device_issue_pending
= ls2x_dma_issue_pending
;
641 ddev
->device_prep_slave_sg
= ls2x_dma_prep_slave_sg
;
642 ddev
->device_prep_dma_cyclic
= ls2x_dma_prep_dma_cyclic
;
643 ddev
->device_config
= ls2x_dma_slave_config
;
644 ddev
->device_terminate_all
= ls2x_dma_terminate_all
;
645 ddev
->device_synchronize
= ls2x_dma_synchronize
;
646 ddev
->device_pause
= ls2x_dma_pause
;
647 ddev
->device_resume
= ls2x_dma_resume
;
649 ddev
->src_addr_widths
= LDMA_SLAVE_BUSWIDTHS
;
650 ddev
->dst_addr_widths
= LDMA_SLAVE_BUSWIDTHS
;
651 ddev
->directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
653 ret
= dma_async_device_register(&priv
->ddev
);
657 ret
= of_dma_controller_register(dev
->of_node
, of_dma_xlate_by_chan_id
, priv
);
659 goto unregister_dmac
;
661 platform_set_drvdata(pdev
, priv
);
663 dev_info(dev
, "Loongson LS2X APB DMA driver registered successfully.\n");
667 dma_async_device_unregister(&priv
->ddev
);
669 clk_disable_unprepare(priv
->dma_clk
);
675 * ls2x_dma_remove - Driver remove function
676 * @pdev: Pointer to the platform_device structure
678 static void ls2x_dma_remove(struct platform_device
*pdev
)
680 struct ls2x_dma_priv
*priv
= platform_get_drvdata(pdev
);
682 of_dma_controller_free(pdev
->dev
.of_node
);
683 dma_async_device_unregister(&priv
->ddev
);
684 clk_disable_unprepare(priv
->dma_clk
);
687 static const struct of_device_id ls2x_dma_of_match_table
[] = {
688 { .compatible
= "loongson,ls2k1000-apbdma" },
691 MODULE_DEVICE_TABLE(of
, ls2x_dma_of_match_table
);
693 static struct platform_driver ls2x_dmac_driver
= {
694 .probe
= ls2x_dma_probe
,
695 .remove
= ls2x_dma_remove
,
697 .name
= "ls2x-apbdma",
698 .of_match_table
= ls2x_dma_of_match_table
,
701 module_platform_driver(ls2x_dmac_driver
);
703 MODULE_DESCRIPTION("Loongson-2 APB DMA Controller driver");
704 MODULE_AUTHOR("Loongson Technology Corporation Limited");
705 MODULE_LICENSE("GPL");