171e7d3cbSBinbin Zhou // SPDX-License-Identifier: GPL-2.0-or-later
271e7d3cbSBinbin Zhou /*
371e7d3cbSBinbin Zhou * Driver for the Loongson LS2X APB DMA Controller
471e7d3cbSBinbin Zhou *
571e7d3cbSBinbin Zhou * Copyright (C) 2017-2023 Loongson Corporation
671e7d3cbSBinbin Zhou */
771e7d3cbSBinbin Zhou
871e7d3cbSBinbin Zhou #include <linux/clk.h>
971e7d3cbSBinbin Zhou #include <linux/dma-mapping.h>
1071e7d3cbSBinbin Zhou #include <linux/dmapool.h>
1171e7d3cbSBinbin Zhou #include <linux/interrupt.h>
1271e7d3cbSBinbin Zhou #include <linux/io.h>
1371e7d3cbSBinbin Zhou #include <linux/io-64-nonatomic-lo-hi.h>
1471e7d3cbSBinbin Zhou #include <linux/module.h>
1571e7d3cbSBinbin Zhou #include <linux/of.h>
1671e7d3cbSBinbin Zhou #include <linux/of_dma.h>
1771e7d3cbSBinbin Zhou #include <linux/platform_device.h>
1871e7d3cbSBinbin Zhou #include <linux/slab.h>
1971e7d3cbSBinbin Zhou
2071e7d3cbSBinbin Zhou #include "dmaengine.h"
2171e7d3cbSBinbin Zhou #include "virt-dma.h"
2271e7d3cbSBinbin Zhou
2371e7d3cbSBinbin Zhou /* Global Configuration Register */
2471e7d3cbSBinbin Zhou #define LDMA_ORDER_ERG 0x0
2571e7d3cbSBinbin Zhou
2671e7d3cbSBinbin Zhou /* Bitfield definitions */
2771e7d3cbSBinbin Zhou
2871e7d3cbSBinbin Zhou /* Bitfields in Global Configuration Register */
2971e7d3cbSBinbin Zhou #define LDMA_64BIT_EN BIT(0) /* 1: 64 bit support */
3071e7d3cbSBinbin Zhou #define LDMA_UNCOHERENT_EN BIT(1) /* 0: cache, 1: uncache */
3171e7d3cbSBinbin Zhou #define LDMA_ASK_VALID BIT(2)
3271e7d3cbSBinbin Zhou #define LDMA_START BIT(3) /* DMA start operation */
3371e7d3cbSBinbin Zhou #define LDMA_STOP BIT(4) /* DMA stop operation */
3471e7d3cbSBinbin Zhou #define LDMA_CONFIG_MASK GENMASK(4, 0) /* DMA controller config bits mask */
3571e7d3cbSBinbin Zhou
36*a688efeaSAmit Vadhavana /* Bitfields in ndesc_addr field of HW descriptor */
3771e7d3cbSBinbin Zhou #define LDMA_DESC_EN BIT(0) /*1: The next descriptor is valid */
3871e7d3cbSBinbin Zhou #define LDMA_DESC_ADDR_LOW GENMASK(31, 1)
3971e7d3cbSBinbin Zhou
40*a688efeaSAmit Vadhavana /* Bitfields in cmd field of HW descriptor */
4171e7d3cbSBinbin Zhou #define LDMA_INT BIT(1) /* Enable DMA interrupts */
4271e7d3cbSBinbin Zhou #define LDMA_DATA_DIRECTION BIT(12) /* 1: write to device, 0: read from device */
4371e7d3cbSBinbin Zhou
4471e7d3cbSBinbin Zhou #define LDMA_SLAVE_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
4571e7d3cbSBinbin Zhou BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
4671e7d3cbSBinbin Zhou
4771e7d3cbSBinbin Zhou #define LDMA_MAX_TRANS_LEN U32_MAX
4871e7d3cbSBinbin Zhou
4971e7d3cbSBinbin Zhou /*-- descriptors -----------------------------------------------------*/
5071e7d3cbSBinbin Zhou
5171e7d3cbSBinbin Zhou /*
5271e7d3cbSBinbin Zhou * struct ls2x_dma_hw_desc - DMA HW descriptor
5371e7d3cbSBinbin Zhou * @ndesc_addr: the next descriptor low address.
5471e7d3cbSBinbin Zhou * @mem_addr: memory low address.
5571e7d3cbSBinbin Zhou * @apb_addr: device buffer address.
5671e7d3cbSBinbin Zhou * @len: length of a piece of carried content, in words.
5771e7d3cbSBinbin Zhou * @step_len: length between two moved memory data blocks.
5871e7d3cbSBinbin Zhou * @step_times: number of blocks to be carried in a single DMA operation.
5971e7d3cbSBinbin Zhou * @cmd: descriptor command or state.
6071e7d3cbSBinbin Zhou * @stats: DMA status.
6171e7d3cbSBinbin Zhou * @high_ndesc_addr: the next descriptor high address.
6271e7d3cbSBinbin Zhou * @high_mem_addr: memory high address.
6371e7d3cbSBinbin Zhou * @reserved: reserved
6471e7d3cbSBinbin Zhou */
6571e7d3cbSBinbin Zhou struct ls2x_dma_hw_desc {
6671e7d3cbSBinbin Zhou u32 ndesc_addr;
6771e7d3cbSBinbin Zhou u32 mem_addr;
6871e7d3cbSBinbin Zhou u32 apb_addr;
6971e7d3cbSBinbin Zhou u32 len;
7071e7d3cbSBinbin Zhou u32 step_len;
7171e7d3cbSBinbin Zhou u32 step_times;
7271e7d3cbSBinbin Zhou u32 cmd;
7371e7d3cbSBinbin Zhou u32 stats;
7471e7d3cbSBinbin Zhou u32 high_ndesc_addr;
7571e7d3cbSBinbin Zhou u32 high_mem_addr;
7671e7d3cbSBinbin Zhou u32 reserved[2];
7771e7d3cbSBinbin Zhou } __packed;
7871e7d3cbSBinbin Zhou
7971e7d3cbSBinbin Zhou /*
8071e7d3cbSBinbin Zhou * struct ls2x_dma_sg - ls2x dma scatter gather entry
8171e7d3cbSBinbin Zhou * @hw: the pointer to DMA HW descriptor.
8271e7d3cbSBinbin Zhou * @llp: physical address of the DMA HW descriptor.
8371e7d3cbSBinbin Zhou * @phys: destination or source address(mem).
8471e7d3cbSBinbin Zhou * @len: number of Bytes to read.
8571e7d3cbSBinbin Zhou */
8671e7d3cbSBinbin Zhou struct ls2x_dma_sg {
8771e7d3cbSBinbin Zhou struct ls2x_dma_hw_desc *hw;
8871e7d3cbSBinbin Zhou dma_addr_t llp;
8971e7d3cbSBinbin Zhou dma_addr_t phys;
9071e7d3cbSBinbin Zhou u32 len;
9171e7d3cbSBinbin Zhou };
9271e7d3cbSBinbin Zhou
9371e7d3cbSBinbin Zhou /*
9471e7d3cbSBinbin Zhou * struct ls2x_dma_desc - software descriptor
9571e7d3cbSBinbin Zhou * @vdesc: pointer to the virtual dma descriptor.
9671e7d3cbSBinbin Zhou * @cyclic: flag to dma cyclic
9771e7d3cbSBinbin Zhou * @burst_size: burst size of transaction, in words.
9871e7d3cbSBinbin Zhou * @desc_num: number of sg entries.
9971e7d3cbSBinbin Zhou * @direction: transfer direction, to or from device.
10071e7d3cbSBinbin Zhou * @status: dma controller status.
10171e7d3cbSBinbin Zhou * @sg: array of sgs.
10271e7d3cbSBinbin Zhou */
10371e7d3cbSBinbin Zhou struct ls2x_dma_desc {
10471e7d3cbSBinbin Zhou struct virt_dma_desc vdesc;
10571e7d3cbSBinbin Zhou bool cyclic;
10671e7d3cbSBinbin Zhou size_t burst_size;
10771e7d3cbSBinbin Zhou u32 desc_num;
10871e7d3cbSBinbin Zhou enum dma_transfer_direction direction;
10971e7d3cbSBinbin Zhou enum dma_status status;
11071e7d3cbSBinbin Zhou struct ls2x_dma_sg sg[] __counted_by(desc_num);
11171e7d3cbSBinbin Zhou };
11271e7d3cbSBinbin Zhou
11371e7d3cbSBinbin Zhou /*-- Channels --------------------------------------------------------*/
11471e7d3cbSBinbin Zhou
11571e7d3cbSBinbin Zhou /*
11671e7d3cbSBinbin Zhou * struct ls2x_dma_chan - internal representation of an LS2X APB DMA channel
11771e7d3cbSBinbin Zhou * @vchan: virtual dma channel entry.
11871e7d3cbSBinbin Zhou * @desc: pointer to the ls2x sw dma descriptor.
11971e7d3cbSBinbin Zhou * @pool: hw desc table
12071e7d3cbSBinbin Zhou * @irq: irq line
12171e7d3cbSBinbin Zhou * @sconfig: configuration for slave transfers, passed via .device_config
12271e7d3cbSBinbin Zhou */
12371e7d3cbSBinbin Zhou struct ls2x_dma_chan {
12471e7d3cbSBinbin Zhou struct virt_dma_chan vchan;
12571e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc;
12671e7d3cbSBinbin Zhou void *pool;
12771e7d3cbSBinbin Zhou int irq;
12871e7d3cbSBinbin Zhou struct dma_slave_config sconfig;
12971e7d3cbSBinbin Zhou };
13071e7d3cbSBinbin Zhou
13171e7d3cbSBinbin Zhou /*-- Controller ------------------------------------------------------*/
13271e7d3cbSBinbin Zhou
13371e7d3cbSBinbin Zhou /*
13471e7d3cbSBinbin Zhou * struct ls2x_dma_priv - LS2X APB DMAC specific information
13571e7d3cbSBinbin Zhou * @ddev: dmaengine dma_device object members
13671e7d3cbSBinbin Zhou * @dma_clk: DMAC clock source
13771e7d3cbSBinbin Zhou * @regs: memory mapped register base
13871e7d3cbSBinbin Zhou * @lchan: channel to store ls2x_dma_chan structures
13971e7d3cbSBinbin Zhou */
14071e7d3cbSBinbin Zhou struct ls2x_dma_priv {
14171e7d3cbSBinbin Zhou struct dma_device ddev;
14271e7d3cbSBinbin Zhou struct clk *dma_clk;
14371e7d3cbSBinbin Zhou void __iomem *regs;
14471e7d3cbSBinbin Zhou struct ls2x_dma_chan lchan;
14571e7d3cbSBinbin Zhou };
14671e7d3cbSBinbin Zhou
14771e7d3cbSBinbin Zhou /*-- Helper functions ------------------------------------------------*/
14871e7d3cbSBinbin Zhou
to_ldma_desc(struct virt_dma_desc * vdesc)14971e7d3cbSBinbin Zhou static inline struct ls2x_dma_desc *to_ldma_desc(struct virt_dma_desc *vdesc)
15071e7d3cbSBinbin Zhou {
15171e7d3cbSBinbin Zhou return container_of(vdesc, struct ls2x_dma_desc, vdesc);
15271e7d3cbSBinbin Zhou }
15371e7d3cbSBinbin Zhou
to_ldma_chan(struct dma_chan * chan)15471e7d3cbSBinbin Zhou static inline struct ls2x_dma_chan *to_ldma_chan(struct dma_chan *chan)
15571e7d3cbSBinbin Zhou {
15671e7d3cbSBinbin Zhou return container_of(chan, struct ls2x_dma_chan, vchan.chan);
15771e7d3cbSBinbin Zhou }
15871e7d3cbSBinbin Zhou
to_ldma_priv(struct dma_device * ddev)15971e7d3cbSBinbin Zhou static inline struct ls2x_dma_priv *to_ldma_priv(struct dma_device *ddev)
16071e7d3cbSBinbin Zhou {
16171e7d3cbSBinbin Zhou return container_of(ddev, struct ls2x_dma_priv, ddev);
16271e7d3cbSBinbin Zhou }
16371e7d3cbSBinbin Zhou
chan2dev(struct dma_chan * chan)16471e7d3cbSBinbin Zhou static struct device *chan2dev(struct dma_chan *chan)
16571e7d3cbSBinbin Zhou {
16671e7d3cbSBinbin Zhou return &chan->dev->device;
16771e7d3cbSBinbin Zhou }
16871e7d3cbSBinbin Zhou
ls2x_dma_desc_free(struct virt_dma_desc * vdesc)16971e7d3cbSBinbin Zhou static void ls2x_dma_desc_free(struct virt_dma_desc *vdesc)
17071e7d3cbSBinbin Zhou {
17171e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(vdesc->tx.chan);
17271e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc = to_ldma_desc(vdesc);
17371e7d3cbSBinbin Zhou int i;
17471e7d3cbSBinbin Zhou
17571e7d3cbSBinbin Zhou for (i = 0; i < desc->desc_num; i++) {
17671e7d3cbSBinbin Zhou if (desc->sg[i].hw)
17771e7d3cbSBinbin Zhou dma_pool_free(lchan->pool, desc->sg[i].hw,
17871e7d3cbSBinbin Zhou desc->sg[i].llp);
17971e7d3cbSBinbin Zhou }
18071e7d3cbSBinbin Zhou
18171e7d3cbSBinbin Zhou kfree(desc);
18271e7d3cbSBinbin Zhou }
18371e7d3cbSBinbin Zhou
ls2x_dma_write_cmd(struct ls2x_dma_chan * lchan,bool cmd)18471e7d3cbSBinbin Zhou static void ls2x_dma_write_cmd(struct ls2x_dma_chan *lchan, bool cmd)
18571e7d3cbSBinbin Zhou {
18671e7d3cbSBinbin Zhou struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
18771e7d3cbSBinbin Zhou u64 val;
18871e7d3cbSBinbin Zhou
18971e7d3cbSBinbin Zhou val = lo_hi_readq(priv->regs + LDMA_ORDER_ERG) & ~LDMA_CONFIG_MASK;
19071e7d3cbSBinbin Zhou val |= LDMA_64BIT_EN | cmd;
19171e7d3cbSBinbin Zhou lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
19271e7d3cbSBinbin Zhou }
19371e7d3cbSBinbin Zhou
ls2x_dma_start_transfer(struct ls2x_dma_chan * lchan)19471e7d3cbSBinbin Zhou static void ls2x_dma_start_transfer(struct ls2x_dma_chan *lchan)
19571e7d3cbSBinbin Zhou {
19671e7d3cbSBinbin Zhou struct ls2x_dma_priv *priv = to_ldma_priv(lchan->vchan.chan.device);
19771e7d3cbSBinbin Zhou struct ls2x_dma_sg *ldma_sg;
19871e7d3cbSBinbin Zhou struct virt_dma_desc *vdesc;
19971e7d3cbSBinbin Zhou u64 val;
20071e7d3cbSBinbin Zhou
20171e7d3cbSBinbin Zhou /* Get the next descriptor */
20271e7d3cbSBinbin Zhou vdesc = vchan_next_desc(&lchan->vchan);
20371e7d3cbSBinbin Zhou if (!vdesc) {
20471e7d3cbSBinbin Zhou lchan->desc = NULL;
20571e7d3cbSBinbin Zhou return;
20671e7d3cbSBinbin Zhou }
20771e7d3cbSBinbin Zhou
20871e7d3cbSBinbin Zhou list_del(&vdesc->node);
20971e7d3cbSBinbin Zhou lchan->desc = to_ldma_desc(vdesc);
21071e7d3cbSBinbin Zhou ldma_sg = &lchan->desc->sg[0];
21171e7d3cbSBinbin Zhou
21271e7d3cbSBinbin Zhou /* Start DMA */
21371e7d3cbSBinbin Zhou lo_hi_writeq(0, priv->regs + LDMA_ORDER_ERG);
21471e7d3cbSBinbin Zhou val = (ldma_sg->llp & ~LDMA_CONFIG_MASK) | LDMA_64BIT_EN | LDMA_START;
21571e7d3cbSBinbin Zhou lo_hi_writeq(val, priv->regs + LDMA_ORDER_ERG);
21671e7d3cbSBinbin Zhou }
21771e7d3cbSBinbin Zhou
ls2x_dmac_detect_burst(struct ls2x_dma_chan * lchan)21871e7d3cbSBinbin Zhou static size_t ls2x_dmac_detect_burst(struct ls2x_dma_chan *lchan)
21971e7d3cbSBinbin Zhou {
22071e7d3cbSBinbin Zhou u32 maxburst, buswidth;
22171e7d3cbSBinbin Zhou
22271e7d3cbSBinbin Zhou /* Reject definitely invalid configurations */
22371e7d3cbSBinbin Zhou if ((lchan->sconfig.src_addr_width & LDMA_SLAVE_BUSWIDTHS) &&
22471e7d3cbSBinbin Zhou (lchan->sconfig.dst_addr_width & LDMA_SLAVE_BUSWIDTHS))
22571e7d3cbSBinbin Zhou return 0;
22671e7d3cbSBinbin Zhou
22771e7d3cbSBinbin Zhou if (lchan->sconfig.direction == DMA_MEM_TO_DEV) {
22871e7d3cbSBinbin Zhou maxburst = lchan->sconfig.dst_maxburst;
22971e7d3cbSBinbin Zhou buswidth = lchan->sconfig.dst_addr_width;
23071e7d3cbSBinbin Zhou } else {
23171e7d3cbSBinbin Zhou maxburst = lchan->sconfig.src_maxburst;
23271e7d3cbSBinbin Zhou buswidth = lchan->sconfig.src_addr_width;
23371e7d3cbSBinbin Zhou }
23471e7d3cbSBinbin Zhou
23571e7d3cbSBinbin Zhou /* If maxburst is zero, fallback to LDMA_MAX_TRANS_LEN */
23671e7d3cbSBinbin Zhou return maxburst ? (maxburst * buswidth) >> 2 : LDMA_MAX_TRANS_LEN;
23771e7d3cbSBinbin Zhou }
23871e7d3cbSBinbin Zhou
ls2x_dma_fill_desc(struct ls2x_dma_chan * lchan,u32 sg_index,struct ls2x_dma_desc * desc)23971e7d3cbSBinbin Zhou static void ls2x_dma_fill_desc(struct ls2x_dma_chan *lchan, u32 sg_index,
24071e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc)
24171e7d3cbSBinbin Zhou {
24271e7d3cbSBinbin Zhou struct ls2x_dma_sg *ldma_sg = &desc->sg[sg_index];
24371e7d3cbSBinbin Zhou u32 num_segments, segment_size;
24471e7d3cbSBinbin Zhou
24571e7d3cbSBinbin Zhou if (desc->direction == DMA_MEM_TO_DEV) {
24671e7d3cbSBinbin Zhou ldma_sg->hw->cmd = LDMA_INT | LDMA_DATA_DIRECTION;
24771e7d3cbSBinbin Zhou ldma_sg->hw->apb_addr = lchan->sconfig.dst_addr;
24871e7d3cbSBinbin Zhou } else {
24971e7d3cbSBinbin Zhou ldma_sg->hw->cmd = LDMA_INT;
25071e7d3cbSBinbin Zhou ldma_sg->hw->apb_addr = lchan->sconfig.src_addr;
25171e7d3cbSBinbin Zhou }
25271e7d3cbSBinbin Zhou
25371e7d3cbSBinbin Zhou ldma_sg->hw->mem_addr = lower_32_bits(ldma_sg->phys);
25471e7d3cbSBinbin Zhou ldma_sg->hw->high_mem_addr = upper_32_bits(ldma_sg->phys);
25571e7d3cbSBinbin Zhou
25671e7d3cbSBinbin Zhou /* Split into multiple equally sized segments if necessary */
25771e7d3cbSBinbin Zhou num_segments = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, desc->burst_size);
25871e7d3cbSBinbin Zhou segment_size = DIV_ROUND_UP((ldma_sg->len + 3) >> 2, num_segments);
25971e7d3cbSBinbin Zhou
26071e7d3cbSBinbin Zhou /* Word count register takes input in words */
26171e7d3cbSBinbin Zhou ldma_sg->hw->len = segment_size;
26271e7d3cbSBinbin Zhou ldma_sg->hw->step_times = num_segments;
26371e7d3cbSBinbin Zhou ldma_sg->hw->step_len = 0;
26471e7d3cbSBinbin Zhou
26571e7d3cbSBinbin Zhou /* lets make a link list */
26671e7d3cbSBinbin Zhou if (sg_index) {
26771e7d3cbSBinbin Zhou desc->sg[sg_index - 1].hw->ndesc_addr = ldma_sg->llp | LDMA_DESC_EN;
26871e7d3cbSBinbin Zhou desc->sg[sg_index - 1].hw->high_ndesc_addr = upper_32_bits(ldma_sg->llp);
26971e7d3cbSBinbin Zhou }
27071e7d3cbSBinbin Zhou }
27171e7d3cbSBinbin Zhou
27271e7d3cbSBinbin Zhou /*-- DMA Engine API --------------------------------------------------*/
27371e7d3cbSBinbin Zhou
27471e7d3cbSBinbin Zhou /*
27571e7d3cbSBinbin Zhou * ls2x_dma_alloc_chan_resources - allocate resources for DMA channel
27671e7d3cbSBinbin Zhou * @chan: allocate descriptor resources for this channel
27771e7d3cbSBinbin Zhou *
27871e7d3cbSBinbin Zhou * return - the number of allocated descriptors
27971e7d3cbSBinbin Zhou */
ls2x_dma_alloc_chan_resources(struct dma_chan * chan)28071e7d3cbSBinbin Zhou static int ls2x_dma_alloc_chan_resources(struct dma_chan *chan)
28171e7d3cbSBinbin Zhou {
28271e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
28371e7d3cbSBinbin Zhou
28471e7d3cbSBinbin Zhou /* Create a pool of consistent memory blocks for hardware descriptors */
28571e7d3cbSBinbin Zhou lchan->pool = dma_pool_create(dev_name(chan2dev(chan)),
28671e7d3cbSBinbin Zhou chan->device->dev, PAGE_SIZE,
28771e7d3cbSBinbin Zhou __alignof__(struct ls2x_dma_hw_desc), 0);
28871e7d3cbSBinbin Zhou if (!lchan->pool) {
28971e7d3cbSBinbin Zhou dev_err(chan2dev(chan), "No memory for descriptors\n");
29071e7d3cbSBinbin Zhou return -ENOMEM;
29171e7d3cbSBinbin Zhou }
29271e7d3cbSBinbin Zhou
29371e7d3cbSBinbin Zhou return 1;
29471e7d3cbSBinbin Zhou }
29571e7d3cbSBinbin Zhou
29671e7d3cbSBinbin Zhou /*
29771e7d3cbSBinbin Zhou * ls2x_dma_free_chan_resources - free all channel resources
29871e7d3cbSBinbin Zhou * @chan: DMA channel
29971e7d3cbSBinbin Zhou */
ls2x_dma_free_chan_resources(struct dma_chan * chan)30071e7d3cbSBinbin Zhou static void ls2x_dma_free_chan_resources(struct dma_chan *chan)
30171e7d3cbSBinbin Zhou {
30271e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
30371e7d3cbSBinbin Zhou
30471e7d3cbSBinbin Zhou vchan_free_chan_resources(to_virt_chan(chan));
30571e7d3cbSBinbin Zhou dma_pool_destroy(lchan->pool);
30671e7d3cbSBinbin Zhou lchan->pool = NULL;
30771e7d3cbSBinbin Zhou }
30871e7d3cbSBinbin Zhou
30971e7d3cbSBinbin Zhou /*
31071e7d3cbSBinbin Zhou * ls2x_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
31171e7d3cbSBinbin Zhou * @chan: DMA channel
31271e7d3cbSBinbin Zhou * @sgl: scatterlist to transfer to/from
31371e7d3cbSBinbin Zhou * @sg_len: number of entries in @scatterlist
31471e7d3cbSBinbin Zhou * @direction: DMA direction
31571e7d3cbSBinbin Zhou * @flags: tx descriptor status flags
31671e7d3cbSBinbin Zhou * @context: transaction context (ignored)
31771e7d3cbSBinbin Zhou *
31871e7d3cbSBinbin Zhou * Return: Async transaction descriptor on success and NULL on failure
31971e7d3cbSBinbin Zhou */
32071e7d3cbSBinbin Zhou static struct dma_async_tx_descriptor *
ls2x_dma_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,u32 sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)32171e7d3cbSBinbin Zhou ls2x_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
32271e7d3cbSBinbin Zhou u32 sg_len, enum dma_transfer_direction direction,
32371e7d3cbSBinbin Zhou unsigned long flags, void *context)
32471e7d3cbSBinbin Zhou {
32571e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
32671e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc;
32771e7d3cbSBinbin Zhou struct scatterlist *sg;
32871e7d3cbSBinbin Zhou size_t burst_size;
32971e7d3cbSBinbin Zhou int i;
33071e7d3cbSBinbin Zhou
33171e7d3cbSBinbin Zhou if (unlikely(!sg_len || !is_slave_direction(direction)))
33271e7d3cbSBinbin Zhou return NULL;
33371e7d3cbSBinbin Zhou
33471e7d3cbSBinbin Zhou burst_size = ls2x_dmac_detect_burst(lchan);
33571e7d3cbSBinbin Zhou if (!burst_size)
33671e7d3cbSBinbin Zhou return NULL;
33771e7d3cbSBinbin Zhou
33871e7d3cbSBinbin Zhou desc = kzalloc(struct_size(desc, sg, sg_len), GFP_NOWAIT);
33971e7d3cbSBinbin Zhou if (!desc)
34071e7d3cbSBinbin Zhou return NULL;
34171e7d3cbSBinbin Zhou
34271e7d3cbSBinbin Zhou desc->desc_num = sg_len;
34371e7d3cbSBinbin Zhou desc->direction = direction;
34471e7d3cbSBinbin Zhou desc->burst_size = burst_size;
34571e7d3cbSBinbin Zhou
34671e7d3cbSBinbin Zhou for_each_sg(sgl, sg, sg_len, i) {
34771e7d3cbSBinbin Zhou struct ls2x_dma_sg *ldma_sg = &desc->sg[i];
34871e7d3cbSBinbin Zhou
34971e7d3cbSBinbin Zhou /* Allocate DMA capable memory for hardware descriptor */
35071e7d3cbSBinbin Zhou ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
35171e7d3cbSBinbin Zhou if (!ldma_sg->hw) {
35271e7d3cbSBinbin Zhou desc->desc_num = i;
35371e7d3cbSBinbin Zhou ls2x_dma_desc_free(&desc->vdesc);
35471e7d3cbSBinbin Zhou return NULL;
35571e7d3cbSBinbin Zhou }
35671e7d3cbSBinbin Zhou
35771e7d3cbSBinbin Zhou ldma_sg->phys = sg_dma_address(sg);
35871e7d3cbSBinbin Zhou ldma_sg->len = sg_dma_len(sg);
35971e7d3cbSBinbin Zhou
36071e7d3cbSBinbin Zhou ls2x_dma_fill_desc(lchan, i, desc);
36171e7d3cbSBinbin Zhou }
36271e7d3cbSBinbin Zhou
36371e7d3cbSBinbin Zhou /* Setting the last descriptor enable bit */
36471e7d3cbSBinbin Zhou desc->sg[sg_len - 1].hw->ndesc_addr &= ~LDMA_DESC_EN;
36571e7d3cbSBinbin Zhou desc->status = DMA_IN_PROGRESS;
36671e7d3cbSBinbin Zhou
36771e7d3cbSBinbin Zhou return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
36871e7d3cbSBinbin Zhou }
36971e7d3cbSBinbin Zhou
37071e7d3cbSBinbin Zhou /*
37171e7d3cbSBinbin Zhou * ls2x_dma_prep_dma_cyclic - prepare the cyclic DMA transfer
37271e7d3cbSBinbin Zhou * @chan: the DMA channel to prepare
37371e7d3cbSBinbin Zhou * @buf_addr: physical DMA address where the buffer starts
37471e7d3cbSBinbin Zhou * @buf_len: total number of bytes for the entire buffer
37571e7d3cbSBinbin Zhou * @period_len: number of bytes for each period
37671e7d3cbSBinbin Zhou * @direction: transfer direction, to or from device
37771e7d3cbSBinbin Zhou * @flags: tx descriptor status flags
37871e7d3cbSBinbin Zhou *
37971e7d3cbSBinbin Zhou * Return: Async transaction descriptor on success and NULL on failure
38071e7d3cbSBinbin Zhou */
38171e7d3cbSBinbin Zhou static struct dma_async_tx_descriptor *
ls2x_dma_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t buf_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)38271e7d3cbSBinbin Zhou ls2x_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
38371e7d3cbSBinbin Zhou size_t period_len, enum dma_transfer_direction direction,
38471e7d3cbSBinbin Zhou unsigned long flags)
38571e7d3cbSBinbin Zhou {
38671e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
38771e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc;
38871e7d3cbSBinbin Zhou size_t burst_size;
38971e7d3cbSBinbin Zhou u32 num_periods;
39071e7d3cbSBinbin Zhou int i;
39171e7d3cbSBinbin Zhou
39271e7d3cbSBinbin Zhou if (unlikely(!buf_len || !period_len))
39371e7d3cbSBinbin Zhou return NULL;
39471e7d3cbSBinbin Zhou
39571e7d3cbSBinbin Zhou if (unlikely(!is_slave_direction(direction)))
39671e7d3cbSBinbin Zhou return NULL;
39771e7d3cbSBinbin Zhou
39871e7d3cbSBinbin Zhou burst_size = ls2x_dmac_detect_burst(lchan);
39971e7d3cbSBinbin Zhou if (!burst_size)
40071e7d3cbSBinbin Zhou return NULL;
40171e7d3cbSBinbin Zhou
40271e7d3cbSBinbin Zhou num_periods = buf_len / period_len;
40371e7d3cbSBinbin Zhou desc = kzalloc(struct_size(desc, sg, num_periods), GFP_NOWAIT);
40471e7d3cbSBinbin Zhou if (!desc)
40571e7d3cbSBinbin Zhou return NULL;
40671e7d3cbSBinbin Zhou
40771e7d3cbSBinbin Zhou desc->desc_num = num_periods;
40871e7d3cbSBinbin Zhou desc->direction = direction;
40971e7d3cbSBinbin Zhou desc->burst_size = burst_size;
41071e7d3cbSBinbin Zhou
41171e7d3cbSBinbin Zhou /* Build cyclic linked list */
41271e7d3cbSBinbin Zhou for (i = 0; i < num_periods; i++) {
41371e7d3cbSBinbin Zhou struct ls2x_dma_sg *ldma_sg = &desc->sg[i];
41471e7d3cbSBinbin Zhou
41571e7d3cbSBinbin Zhou /* Allocate DMA capable memory for hardware descriptor */
41671e7d3cbSBinbin Zhou ldma_sg->hw = dma_pool_alloc(lchan->pool, GFP_NOWAIT, &ldma_sg->llp);
41771e7d3cbSBinbin Zhou if (!ldma_sg->hw) {
41871e7d3cbSBinbin Zhou desc->desc_num = i;
41971e7d3cbSBinbin Zhou ls2x_dma_desc_free(&desc->vdesc);
42071e7d3cbSBinbin Zhou return NULL;
42171e7d3cbSBinbin Zhou }
42271e7d3cbSBinbin Zhou
42371e7d3cbSBinbin Zhou ldma_sg->phys = buf_addr + period_len * i;
42471e7d3cbSBinbin Zhou ldma_sg->len = period_len;
42571e7d3cbSBinbin Zhou
42671e7d3cbSBinbin Zhou ls2x_dma_fill_desc(lchan, i, desc);
42771e7d3cbSBinbin Zhou }
42871e7d3cbSBinbin Zhou
42971e7d3cbSBinbin Zhou /* Lets make a cyclic list */
43071e7d3cbSBinbin Zhou desc->sg[num_periods - 1].hw->ndesc_addr = desc->sg[0].llp | LDMA_DESC_EN;
43171e7d3cbSBinbin Zhou desc->sg[num_periods - 1].hw->high_ndesc_addr = upper_32_bits(desc->sg[0].llp);
43271e7d3cbSBinbin Zhou desc->cyclic = true;
43371e7d3cbSBinbin Zhou desc->status = DMA_IN_PROGRESS;
43471e7d3cbSBinbin Zhou
43571e7d3cbSBinbin Zhou return vchan_tx_prep(&lchan->vchan, &desc->vdesc, flags);
43671e7d3cbSBinbin Zhou }
43771e7d3cbSBinbin Zhou
43871e7d3cbSBinbin Zhou /*
43971e7d3cbSBinbin Zhou * ls2x_slave_config - set slave configuration for channel
44071e7d3cbSBinbin Zhou * @chan: dma channel
44171e7d3cbSBinbin Zhou * @cfg: slave configuration
44271e7d3cbSBinbin Zhou *
44371e7d3cbSBinbin Zhou * Sets slave configuration for channel
44471e7d3cbSBinbin Zhou */
ls2x_dma_slave_config(struct dma_chan * chan,struct dma_slave_config * config)44571e7d3cbSBinbin Zhou static int ls2x_dma_slave_config(struct dma_chan *chan,
44671e7d3cbSBinbin Zhou struct dma_slave_config *config)
44771e7d3cbSBinbin Zhou {
44871e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
44971e7d3cbSBinbin Zhou
45071e7d3cbSBinbin Zhou memcpy(&lchan->sconfig, config, sizeof(*config));
45171e7d3cbSBinbin Zhou return 0;
45271e7d3cbSBinbin Zhou }
45371e7d3cbSBinbin Zhou
45471e7d3cbSBinbin Zhou /*
45571e7d3cbSBinbin Zhou * ls2x_dma_issue_pending - push pending transactions to the hardware
45671e7d3cbSBinbin Zhou * @chan: channel
45771e7d3cbSBinbin Zhou *
45871e7d3cbSBinbin Zhou * When this function is called, all pending transactions are pushed to the
45971e7d3cbSBinbin Zhou * hardware and executed.
46071e7d3cbSBinbin Zhou */
ls2x_dma_issue_pending(struct dma_chan * chan)46171e7d3cbSBinbin Zhou static void ls2x_dma_issue_pending(struct dma_chan *chan)
46271e7d3cbSBinbin Zhou {
46371e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
46471e7d3cbSBinbin Zhou unsigned long flags;
46571e7d3cbSBinbin Zhou
46671e7d3cbSBinbin Zhou spin_lock_irqsave(&lchan->vchan.lock, flags);
46771e7d3cbSBinbin Zhou if (vchan_issue_pending(&lchan->vchan) && !lchan->desc)
46871e7d3cbSBinbin Zhou ls2x_dma_start_transfer(lchan);
46971e7d3cbSBinbin Zhou spin_unlock_irqrestore(&lchan->vchan.lock, flags);
47071e7d3cbSBinbin Zhou }
47171e7d3cbSBinbin Zhou
47271e7d3cbSBinbin Zhou /*
47371e7d3cbSBinbin Zhou * ls2x_dma_terminate_all - terminate all transactions
47471e7d3cbSBinbin Zhou * @chan: channel
47571e7d3cbSBinbin Zhou *
47671e7d3cbSBinbin Zhou * Stops all DMA transactions.
47771e7d3cbSBinbin Zhou */
ls2x_dma_terminate_all(struct dma_chan * chan)47871e7d3cbSBinbin Zhou static int ls2x_dma_terminate_all(struct dma_chan *chan)
47971e7d3cbSBinbin Zhou {
48071e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
48171e7d3cbSBinbin Zhou unsigned long flags;
48271e7d3cbSBinbin Zhou LIST_HEAD(head);
48371e7d3cbSBinbin Zhou
48471e7d3cbSBinbin Zhou spin_lock_irqsave(&lchan->vchan.lock, flags);
48571e7d3cbSBinbin Zhou /* Setting stop cmd */
48671e7d3cbSBinbin Zhou ls2x_dma_write_cmd(lchan, LDMA_STOP);
48771e7d3cbSBinbin Zhou if (lchan->desc) {
48871e7d3cbSBinbin Zhou vchan_terminate_vdesc(&lchan->desc->vdesc);
48971e7d3cbSBinbin Zhou lchan->desc = NULL;
49071e7d3cbSBinbin Zhou }
49171e7d3cbSBinbin Zhou
49271e7d3cbSBinbin Zhou vchan_get_all_descriptors(&lchan->vchan, &head);
49371e7d3cbSBinbin Zhou spin_unlock_irqrestore(&lchan->vchan.lock, flags);
49471e7d3cbSBinbin Zhou
49571e7d3cbSBinbin Zhou vchan_dma_desc_free_list(&lchan->vchan, &head);
49671e7d3cbSBinbin Zhou return 0;
49771e7d3cbSBinbin Zhou }
49871e7d3cbSBinbin Zhou
49971e7d3cbSBinbin Zhou /*
50071e7d3cbSBinbin Zhou * ls2x_dma_synchronize - Synchronizes the termination of transfers to the
50171e7d3cbSBinbin Zhou * current context.
50271e7d3cbSBinbin Zhou * @chan: channel
50371e7d3cbSBinbin Zhou */
ls2x_dma_synchronize(struct dma_chan * chan)50471e7d3cbSBinbin Zhou static void ls2x_dma_synchronize(struct dma_chan *chan)
50571e7d3cbSBinbin Zhou {
50671e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
50771e7d3cbSBinbin Zhou
50871e7d3cbSBinbin Zhou vchan_synchronize(&lchan->vchan);
50971e7d3cbSBinbin Zhou }
51071e7d3cbSBinbin Zhou
ls2x_dma_pause(struct dma_chan * chan)51171e7d3cbSBinbin Zhou static int ls2x_dma_pause(struct dma_chan *chan)
51271e7d3cbSBinbin Zhou {
51371e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
51471e7d3cbSBinbin Zhou unsigned long flags;
51571e7d3cbSBinbin Zhou
51671e7d3cbSBinbin Zhou spin_lock_irqsave(&lchan->vchan.lock, flags);
51771e7d3cbSBinbin Zhou if (lchan->desc && lchan->desc->status == DMA_IN_PROGRESS) {
51871e7d3cbSBinbin Zhou ls2x_dma_write_cmd(lchan, LDMA_STOP);
51971e7d3cbSBinbin Zhou lchan->desc->status = DMA_PAUSED;
52071e7d3cbSBinbin Zhou }
52171e7d3cbSBinbin Zhou spin_unlock_irqrestore(&lchan->vchan.lock, flags);
52271e7d3cbSBinbin Zhou
52371e7d3cbSBinbin Zhou return 0;
52471e7d3cbSBinbin Zhou }
52571e7d3cbSBinbin Zhou
ls2x_dma_resume(struct dma_chan * chan)52671e7d3cbSBinbin Zhou static int ls2x_dma_resume(struct dma_chan *chan)
52771e7d3cbSBinbin Zhou {
52871e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = to_ldma_chan(chan);
52971e7d3cbSBinbin Zhou unsigned long flags;
53071e7d3cbSBinbin Zhou
53171e7d3cbSBinbin Zhou spin_lock_irqsave(&lchan->vchan.lock, flags);
53271e7d3cbSBinbin Zhou if (lchan->desc && lchan->desc->status == DMA_PAUSED) {
53371e7d3cbSBinbin Zhou lchan->desc->status = DMA_IN_PROGRESS;
53471e7d3cbSBinbin Zhou ls2x_dma_write_cmd(lchan, LDMA_START);
53571e7d3cbSBinbin Zhou }
53671e7d3cbSBinbin Zhou spin_unlock_irqrestore(&lchan->vchan.lock, flags);
53771e7d3cbSBinbin Zhou
53871e7d3cbSBinbin Zhou return 0;
53971e7d3cbSBinbin Zhou }
54071e7d3cbSBinbin Zhou
54171e7d3cbSBinbin Zhou /*
54271e7d3cbSBinbin Zhou * ls2x_dma_isr - LS2X DMA Interrupt handler
54371e7d3cbSBinbin Zhou * @irq: IRQ number
54471e7d3cbSBinbin Zhou * @dev_id: Pointer to ls2x_dma_chan
54571e7d3cbSBinbin Zhou *
54671e7d3cbSBinbin Zhou * Return: IRQ_HANDLED/IRQ_NONE
54771e7d3cbSBinbin Zhou */
ls2x_dma_isr(int irq,void * dev_id)54871e7d3cbSBinbin Zhou static irqreturn_t ls2x_dma_isr(int irq, void *dev_id)
54971e7d3cbSBinbin Zhou {
55071e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = dev_id;
55171e7d3cbSBinbin Zhou struct ls2x_dma_desc *desc;
55271e7d3cbSBinbin Zhou
55371e7d3cbSBinbin Zhou spin_lock(&lchan->vchan.lock);
55471e7d3cbSBinbin Zhou desc = lchan->desc;
55571e7d3cbSBinbin Zhou if (desc) {
55671e7d3cbSBinbin Zhou if (desc->cyclic) {
55771e7d3cbSBinbin Zhou vchan_cyclic_callback(&desc->vdesc);
55871e7d3cbSBinbin Zhou } else {
55971e7d3cbSBinbin Zhou desc->status = DMA_COMPLETE;
56071e7d3cbSBinbin Zhou vchan_cookie_complete(&desc->vdesc);
56171e7d3cbSBinbin Zhou ls2x_dma_start_transfer(lchan);
56271e7d3cbSBinbin Zhou }
56371e7d3cbSBinbin Zhou
56471e7d3cbSBinbin Zhou /* ls2x_dma_start_transfer() updates lchan->desc */
56571e7d3cbSBinbin Zhou if (!lchan->desc)
56671e7d3cbSBinbin Zhou ls2x_dma_write_cmd(lchan, LDMA_STOP);
56771e7d3cbSBinbin Zhou }
56871e7d3cbSBinbin Zhou spin_unlock(&lchan->vchan.lock);
56971e7d3cbSBinbin Zhou
57071e7d3cbSBinbin Zhou return IRQ_HANDLED;
57171e7d3cbSBinbin Zhou }
57271e7d3cbSBinbin Zhou
ls2x_dma_chan_init(struct platform_device * pdev,struct ls2x_dma_priv * priv)57371e7d3cbSBinbin Zhou static int ls2x_dma_chan_init(struct platform_device *pdev,
57471e7d3cbSBinbin Zhou struct ls2x_dma_priv *priv)
57571e7d3cbSBinbin Zhou {
57671e7d3cbSBinbin Zhou struct ls2x_dma_chan *lchan = &priv->lchan;
57771e7d3cbSBinbin Zhou struct device *dev = &pdev->dev;
57871e7d3cbSBinbin Zhou int ret;
57971e7d3cbSBinbin Zhou
58071e7d3cbSBinbin Zhou lchan->irq = platform_get_irq(pdev, 0);
58171e7d3cbSBinbin Zhou if (lchan->irq < 0)
58271e7d3cbSBinbin Zhou return lchan->irq;
58371e7d3cbSBinbin Zhou
58471e7d3cbSBinbin Zhou ret = devm_request_irq(dev, lchan->irq, ls2x_dma_isr, IRQF_TRIGGER_RISING,
58571e7d3cbSBinbin Zhou dev_name(&pdev->dev), lchan);
58671e7d3cbSBinbin Zhou if (ret)
58771e7d3cbSBinbin Zhou return ret;
58871e7d3cbSBinbin Zhou
58971e7d3cbSBinbin Zhou /* Initialize channels related values */
59071e7d3cbSBinbin Zhou INIT_LIST_HEAD(&priv->ddev.channels);
59171e7d3cbSBinbin Zhou lchan->vchan.desc_free = ls2x_dma_desc_free;
59271e7d3cbSBinbin Zhou vchan_init(&lchan->vchan, &priv->ddev);
59371e7d3cbSBinbin Zhou
59471e7d3cbSBinbin Zhou return 0;
59571e7d3cbSBinbin Zhou }
59671e7d3cbSBinbin Zhou
59771e7d3cbSBinbin Zhou /*
59871e7d3cbSBinbin Zhou * ls2x_dma_probe - Driver probe function
59971e7d3cbSBinbin Zhou * @pdev: Pointer to the platform_device structure
60071e7d3cbSBinbin Zhou *
60171e7d3cbSBinbin Zhou * Return: '0' on success and failure value on error
60271e7d3cbSBinbin Zhou */
ls2x_dma_probe(struct platform_device * pdev)60371e7d3cbSBinbin Zhou static int ls2x_dma_probe(struct platform_device *pdev)
60471e7d3cbSBinbin Zhou {
60571e7d3cbSBinbin Zhou struct device *dev = &pdev->dev;
60671e7d3cbSBinbin Zhou struct ls2x_dma_priv *priv;
60771e7d3cbSBinbin Zhou struct dma_device *ddev;
60871e7d3cbSBinbin Zhou int ret;
60971e7d3cbSBinbin Zhou
61071e7d3cbSBinbin Zhou priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
61171e7d3cbSBinbin Zhou if (!priv)
61271e7d3cbSBinbin Zhou return -ENOMEM;
61371e7d3cbSBinbin Zhou
61471e7d3cbSBinbin Zhou priv->regs = devm_platform_ioremap_resource(pdev, 0);
61571e7d3cbSBinbin Zhou if (IS_ERR(priv->regs))
61671e7d3cbSBinbin Zhou return dev_err_probe(dev, PTR_ERR(priv->regs),
61771e7d3cbSBinbin Zhou "devm_platform_ioremap_resource failed.\n");
61871e7d3cbSBinbin Zhou
61971e7d3cbSBinbin Zhou priv->dma_clk = devm_clk_get(&pdev->dev, NULL);
62071e7d3cbSBinbin Zhou if (IS_ERR(priv->dma_clk))
62171e7d3cbSBinbin Zhou return dev_err_probe(dev, PTR_ERR(priv->dma_clk), "devm_clk_get failed.\n");
62271e7d3cbSBinbin Zhou
62371e7d3cbSBinbin Zhou ret = clk_prepare_enable(priv->dma_clk);
62471e7d3cbSBinbin Zhou if (ret)
62571e7d3cbSBinbin Zhou return dev_err_probe(dev, ret, "clk_prepare_enable failed.\n");
62671e7d3cbSBinbin Zhou
62771e7d3cbSBinbin Zhou ret = ls2x_dma_chan_init(pdev, priv);
62871e7d3cbSBinbin Zhou if (ret)
62971e7d3cbSBinbin Zhou goto disable_clk;
63071e7d3cbSBinbin Zhou
63171e7d3cbSBinbin Zhou ddev = &priv->ddev;
63271e7d3cbSBinbin Zhou ddev->dev = dev;
63371e7d3cbSBinbin Zhou dma_cap_zero(ddev->cap_mask);
63471e7d3cbSBinbin Zhou dma_cap_set(DMA_SLAVE, ddev->cap_mask);
63571e7d3cbSBinbin Zhou dma_cap_set(DMA_CYCLIC, ddev->cap_mask);
63671e7d3cbSBinbin Zhou
63771e7d3cbSBinbin Zhou ddev->device_alloc_chan_resources = ls2x_dma_alloc_chan_resources;
63871e7d3cbSBinbin Zhou ddev->device_free_chan_resources = ls2x_dma_free_chan_resources;
63971e7d3cbSBinbin Zhou ddev->device_tx_status = dma_cookie_status;
64071e7d3cbSBinbin Zhou ddev->device_issue_pending = ls2x_dma_issue_pending;
64171e7d3cbSBinbin Zhou ddev->device_prep_slave_sg = ls2x_dma_prep_slave_sg;
64271e7d3cbSBinbin Zhou ddev->device_prep_dma_cyclic = ls2x_dma_prep_dma_cyclic;
64371e7d3cbSBinbin Zhou ddev->device_config = ls2x_dma_slave_config;
64471e7d3cbSBinbin Zhou ddev->device_terminate_all = ls2x_dma_terminate_all;
64571e7d3cbSBinbin Zhou ddev->device_synchronize = ls2x_dma_synchronize;
64671e7d3cbSBinbin Zhou ddev->device_pause = ls2x_dma_pause;
64771e7d3cbSBinbin Zhou ddev->device_resume = ls2x_dma_resume;
64871e7d3cbSBinbin Zhou
64971e7d3cbSBinbin Zhou ddev->src_addr_widths = LDMA_SLAVE_BUSWIDTHS;
65071e7d3cbSBinbin Zhou ddev->dst_addr_widths = LDMA_SLAVE_BUSWIDTHS;
65171e7d3cbSBinbin Zhou ddev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
65271e7d3cbSBinbin Zhou
65371e7d3cbSBinbin Zhou ret = dma_async_device_register(&priv->ddev);
65471e7d3cbSBinbin Zhou if (ret < 0)
65571e7d3cbSBinbin Zhou goto disable_clk;
65671e7d3cbSBinbin Zhou
65771e7d3cbSBinbin Zhou ret = of_dma_controller_register(dev->of_node, of_dma_xlate_by_chan_id, priv);
65871e7d3cbSBinbin Zhou if (ret < 0)
65971e7d3cbSBinbin Zhou goto unregister_dmac;
66071e7d3cbSBinbin Zhou
66171e7d3cbSBinbin Zhou platform_set_drvdata(pdev, priv);
66271e7d3cbSBinbin Zhou
66371e7d3cbSBinbin Zhou dev_info(dev, "Loongson LS2X APB DMA driver registered successfully.\n");
66471e7d3cbSBinbin Zhou return 0;
66571e7d3cbSBinbin Zhou
66671e7d3cbSBinbin Zhou unregister_dmac:
66771e7d3cbSBinbin Zhou dma_async_device_unregister(&priv->ddev);
66871e7d3cbSBinbin Zhou disable_clk:
66971e7d3cbSBinbin Zhou clk_disable_unprepare(priv->dma_clk);
67071e7d3cbSBinbin Zhou
67171e7d3cbSBinbin Zhou return ret;
67271e7d3cbSBinbin Zhou }
67371e7d3cbSBinbin Zhou
67471e7d3cbSBinbin Zhou /*
67571e7d3cbSBinbin Zhou * ls2x_dma_remove - Driver remove function
67671e7d3cbSBinbin Zhou * @pdev: Pointer to the platform_device structure
67771e7d3cbSBinbin Zhou */
ls2x_dma_remove(struct platform_device * pdev)67871e7d3cbSBinbin Zhou static void ls2x_dma_remove(struct platform_device *pdev)
67971e7d3cbSBinbin Zhou {
68071e7d3cbSBinbin Zhou struct ls2x_dma_priv *priv = platform_get_drvdata(pdev);
68171e7d3cbSBinbin Zhou
68271e7d3cbSBinbin Zhou of_dma_controller_free(pdev->dev.of_node);
68371e7d3cbSBinbin Zhou dma_async_device_unregister(&priv->ddev);
68471e7d3cbSBinbin Zhou clk_disable_unprepare(priv->dma_clk);
68571e7d3cbSBinbin Zhou }
68671e7d3cbSBinbin Zhou
68771e7d3cbSBinbin Zhou static const struct of_device_id ls2x_dma_of_match_table[] = {
68871e7d3cbSBinbin Zhou { .compatible = "loongson,ls2k1000-apbdma" },
68971e7d3cbSBinbin Zhou { /* sentinel */ }
69071e7d3cbSBinbin Zhou };
69171e7d3cbSBinbin Zhou MODULE_DEVICE_TABLE(of, ls2x_dma_of_match_table);
69271e7d3cbSBinbin Zhou
69371e7d3cbSBinbin Zhou static struct platform_driver ls2x_dmac_driver = {
69471e7d3cbSBinbin Zhou .probe = ls2x_dma_probe,
69571e7d3cbSBinbin Zhou .remove_new = ls2x_dma_remove,
69671e7d3cbSBinbin Zhou .driver = {
69771e7d3cbSBinbin Zhou .name = "ls2x-apbdma",
69871e7d3cbSBinbin Zhou .of_match_table = ls2x_dma_of_match_table,
69971e7d3cbSBinbin Zhou },
70071e7d3cbSBinbin Zhou };
70171e7d3cbSBinbin Zhou module_platform_driver(ls2x_dmac_driver);
70271e7d3cbSBinbin Zhou
70371e7d3cbSBinbin Zhou MODULE_DESCRIPTION("Loongson LS2X APB DMA Controller driver");
70471e7d3cbSBinbin Zhou MODULE_AUTHOR("Loongson Technology Corporation Limited");
70571e7d3cbSBinbin Zhou MODULE_LICENSE("GPL");
706