xref: /openbmc/linux/drivers/dma/ste_dma40_ll.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28d318a50SLinus Walleij /*
3767a9675SJonas Aaberg  * Copyright (C) ST-Ericsson SA 2007-2010
4d49278e3SPer Forlin  * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5767a9675SJonas Aaberg  * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
68d318a50SLinus Walleij  */
78d318a50SLinus Walleij 
88d318a50SLinus Walleij #include <linux/kernel.h>
9*42ae6f16SLinus Walleij #include <linux/dmaengine.h>
108d318a50SLinus Walleij 
11*42ae6f16SLinus Walleij #include "ste_dma40.h"
128d318a50SLinus Walleij #include "ste_dma40_ll.h"
138d318a50SLinus Walleij 
d40_width_to_bits(enum dma_slave_buswidth width)140161df13SBen Dooks static u8 d40_width_to_bits(enum dma_slave_buswidth width)
1543f2e1a3SLee Jones {
1643f2e1a3SLee Jones 	if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
1743f2e1a3SLee Jones 		return STEDMA40_ESIZE_8_BIT;
1843f2e1a3SLee Jones 	else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
1943f2e1a3SLee Jones 		return STEDMA40_ESIZE_16_BIT;
2043f2e1a3SLee Jones 	else if (width == DMA_SLAVE_BUSWIDTH_8_BYTES)
2143f2e1a3SLee Jones 		return STEDMA40_ESIZE_64_BIT;
2243f2e1a3SLee Jones 	else
2343f2e1a3SLee Jones 		return STEDMA40_ESIZE_32_BIT;
2443f2e1a3SLee Jones }
2543f2e1a3SLee Jones 
268d318a50SLinus Walleij /* Sets up proper LCSP1 and LCSP3 register for a logical channel */
d40_log_cfg(struct stedma40_chan_cfg * cfg,u32 * lcsp1,u32 * lcsp3)278d318a50SLinus Walleij void d40_log_cfg(struct stedma40_chan_cfg *cfg,
288d318a50SLinus Walleij 		 u32 *lcsp1, u32 *lcsp3)
298d318a50SLinus Walleij {
308d318a50SLinus Walleij 	u32 l3 = 0; /* dst */
318d318a50SLinus Walleij 	u32 l1 = 0; /* src */
328d318a50SLinus Walleij 
338d318a50SLinus Walleij 	/* src is mem? -> increase address pos */
342c2b62d5SLee Jones 	if (cfg->dir ==  DMA_MEM_TO_DEV ||
352c2b62d5SLee Jones 	    cfg->dir ==  DMA_MEM_TO_MEM)
3616db3411SLee Jones 		l1 |= BIT(D40_MEM_LCSP1_SCFG_INCR_POS);
378d318a50SLinus Walleij 
388d318a50SLinus Walleij 	/* dst is mem? -> increase address pos */
392c2b62d5SLee Jones 	if (cfg->dir ==  DMA_DEV_TO_MEM ||
402c2b62d5SLee Jones 	    cfg->dir ==  DMA_MEM_TO_MEM)
4116db3411SLee Jones 		l3 |= BIT(D40_MEM_LCSP3_DCFG_INCR_POS);
428d318a50SLinus Walleij 
438d318a50SLinus Walleij 	/* src is hw? -> master port 1 */
442c2b62d5SLee Jones 	if (cfg->dir ==  DMA_DEV_TO_MEM ||
452c2b62d5SLee Jones 	    cfg->dir ==  DMA_DEV_TO_DEV)
4616db3411SLee Jones 		l1 |= BIT(D40_MEM_LCSP1_SCFG_MST_POS);
478d318a50SLinus Walleij 
488d318a50SLinus Walleij 	/* dst is hw? -> master port 1 */
492c2b62d5SLee Jones 	if (cfg->dir ==  DMA_MEM_TO_DEV ||
502c2b62d5SLee Jones 	    cfg->dir ==  DMA_DEV_TO_DEV)
5116db3411SLee Jones 		l3 |= BIT(D40_MEM_LCSP3_DCFG_MST_POS);
528d318a50SLinus Walleij 
5316db3411SLee Jones 	l3 |= BIT(D40_MEM_LCSP3_DCFG_EIM_POS);
548d318a50SLinus Walleij 	l3 |= cfg->dst_info.psize << D40_MEM_LCSP3_DCFG_PSIZE_POS;
5543f2e1a3SLee Jones 	l3 |= d40_width_to_bits(cfg->dst_info.data_width)
5643f2e1a3SLee Jones 		<< D40_MEM_LCSP3_DCFG_ESIZE_POS;
578d318a50SLinus Walleij 
5816db3411SLee Jones 	l1 |= BIT(D40_MEM_LCSP1_SCFG_EIM_POS);
598d318a50SLinus Walleij 	l1 |= cfg->src_info.psize << D40_MEM_LCSP1_SCFG_PSIZE_POS;
6043f2e1a3SLee Jones 	l1 |= d40_width_to_bits(cfg->src_info.data_width)
6143f2e1a3SLee Jones 		<< D40_MEM_LCSP1_SCFG_ESIZE_POS;
628d318a50SLinus Walleij 
638d318a50SLinus Walleij 	*lcsp1 = l1;
648d318a50SLinus Walleij 	*lcsp3 = l3;
658d318a50SLinus Walleij 
668d318a50SLinus Walleij }
678d318a50SLinus Walleij 
d40_phy_cfg(struct stedma40_chan_cfg * cfg,u32 * src_cfg,u32 * dst_cfg)6857e65ad7SLee Jones void d40_phy_cfg(struct stedma40_chan_cfg *cfg, u32 *src_cfg, u32 *dst_cfg)
698d318a50SLinus Walleij {
708d318a50SLinus Walleij 	u32 src = 0;
718d318a50SLinus Walleij 	u32 dst = 0;
728d318a50SLinus Walleij 
732c2b62d5SLee Jones 	if ((cfg->dir == DMA_DEV_TO_MEM) ||
742c2b62d5SLee Jones 	    (cfg->dir == DMA_DEV_TO_DEV)) {
758d318a50SLinus Walleij 		/* Set master port to 1 */
7616db3411SLee Jones 		src |= BIT(D40_SREG_CFG_MST_POS);
7726955c07SLee Jones 		src |= D40_TYPE_TO_EVENT(cfg->dev_type);
788d318a50SLinus Walleij 
798d318a50SLinus Walleij 		if (cfg->src_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
8016db3411SLee Jones 			src |= BIT(D40_SREG_CFG_PHY_TM_POS);
818d318a50SLinus Walleij 		else
828d318a50SLinus Walleij 			src |= 3 << D40_SREG_CFG_PHY_TM_POS;
838d318a50SLinus Walleij 	}
842c2b62d5SLee Jones 	if ((cfg->dir == DMA_MEM_TO_DEV) ||
852c2b62d5SLee Jones 	    (cfg->dir == DMA_DEV_TO_DEV)) {
868d318a50SLinus Walleij 		/* Set master port to 1 */
8716db3411SLee Jones 		dst |= BIT(D40_SREG_CFG_MST_POS);
8826955c07SLee Jones 		dst |= D40_TYPE_TO_EVENT(cfg->dev_type);
898d318a50SLinus Walleij 
908d318a50SLinus Walleij 		if (cfg->dst_info.flow_ctrl == STEDMA40_NO_FLOW_CTRL)
9116db3411SLee Jones 			dst |= BIT(D40_SREG_CFG_PHY_TM_POS);
928d318a50SLinus Walleij 		else
938d318a50SLinus Walleij 			dst |= 3 << D40_SREG_CFG_PHY_TM_POS;
948d318a50SLinus Walleij 	}
958d318a50SLinus Walleij 	/* Interrupt on end of transfer for destination */
9616db3411SLee Jones 	dst |= BIT(D40_SREG_CFG_TIM_POS);
978d318a50SLinus Walleij 
988d318a50SLinus Walleij 	/* Generate interrupt on error */
9916db3411SLee Jones 	src |= BIT(D40_SREG_CFG_EIM_POS);
10016db3411SLee Jones 	dst |= BIT(D40_SREG_CFG_EIM_POS);
1018d318a50SLinus Walleij 
1028d318a50SLinus Walleij 	/* PSIZE */
1038d318a50SLinus Walleij 	if (cfg->src_info.psize != STEDMA40_PSIZE_PHY_1) {
10416db3411SLee Jones 		src |= BIT(D40_SREG_CFG_PHY_PEN_POS);
1058d318a50SLinus Walleij 		src |= cfg->src_info.psize << D40_SREG_CFG_PSIZE_POS;
1068d318a50SLinus Walleij 	}
1078d318a50SLinus Walleij 	if (cfg->dst_info.psize != STEDMA40_PSIZE_PHY_1) {
10816db3411SLee Jones 		dst |= BIT(D40_SREG_CFG_PHY_PEN_POS);
1098d318a50SLinus Walleij 		dst |= cfg->dst_info.psize << D40_SREG_CFG_PSIZE_POS;
1108d318a50SLinus Walleij 	}
1118d318a50SLinus Walleij 
1128d318a50SLinus Walleij 	/* Element size */
11343f2e1a3SLee Jones 	src |= d40_width_to_bits(cfg->src_info.data_width)
11443f2e1a3SLee Jones 		<< D40_SREG_CFG_ESIZE_POS;
11543f2e1a3SLee Jones 	dst |= d40_width_to_bits(cfg->dst_info.data_width)
11643f2e1a3SLee Jones 		<< D40_SREG_CFG_ESIZE_POS;
1178d318a50SLinus Walleij 
1180fd60223SNarayanan 	/* Set the priority bit to high for the physical channel */
1190fd60223SNarayanan 	if (cfg->high_priority) {
12016db3411SLee Jones 		src |= BIT(D40_SREG_CFG_PRI_POS);
12116db3411SLee Jones 		dst |= BIT(D40_SREG_CFG_PRI_POS);
1220fd60223SNarayanan 	}
1238d318a50SLinus Walleij 
12451f5d744SRabin Vincent 	if (cfg->src_info.big_endian)
12516db3411SLee Jones 		src |= BIT(D40_SREG_CFG_LBE_POS);
12651f5d744SRabin Vincent 	if (cfg->dst_info.big_endian)
12716db3411SLee Jones 		dst |= BIT(D40_SREG_CFG_LBE_POS);
1288d318a50SLinus Walleij 
1298d318a50SLinus Walleij 	*src_cfg = src;
1308d318a50SLinus Walleij 	*dst_cfg = dst;
1318d318a50SLinus Walleij }
1328d318a50SLinus Walleij 
d40_phy_fill_lli(struct d40_phy_lli * lli,dma_addr_t data,u32 data_size,dma_addr_t next_lli,u32 reg_cfg,struct stedma40_half_channel_info * info,unsigned int flags)133d49278e3SPer Forlin static int d40_phy_fill_lli(struct d40_phy_lli *lli,
1348d318a50SLinus Walleij 			    dma_addr_t data,
1358d318a50SLinus Walleij 			    u32 data_size,
1368d318a50SLinus Walleij 			    dma_addr_t next_lli,
1378d318a50SLinus Walleij 			    u32 reg_cfg,
1387f933bedSRabin Vincent 			    struct stedma40_half_channel_info *info,
1397f933bedSRabin Vincent 			    unsigned int flags)
1408d318a50SLinus Walleij {
1417f933bedSRabin Vincent 	bool addr_inc = flags & LLI_ADDR_INC;
1427f933bedSRabin Vincent 	bool term_int = flags & LLI_TERM_INT;
143cc31b6f7SRabin Vincent 	unsigned int data_width = info->data_width;
144cc31b6f7SRabin Vincent 	int psize = info->psize;
1458d318a50SLinus Walleij 	int num_elems;
1468d318a50SLinus Walleij 
1478d318a50SLinus Walleij 	if (psize == STEDMA40_PSIZE_PHY_1)
1488d318a50SLinus Walleij 		num_elems = 1;
1498d318a50SLinus Walleij 	else
1508d318a50SLinus Walleij 		num_elems = 2 << psize;
1518d318a50SLinus Walleij 
1528d318a50SLinus Walleij 	/* Must be aligned */
15343f2e1a3SLee Jones 	if (!IS_ALIGNED(data, data_width))
1548d318a50SLinus Walleij 		return -EINVAL;
1558d318a50SLinus Walleij 
1568d318a50SLinus Walleij 	/* Transfer size can't be smaller than (num_elms * elem_size) */
15743f2e1a3SLee Jones 	if (data_size < num_elems * data_width)
1588d318a50SLinus Walleij 		return -EINVAL;
1598d318a50SLinus Walleij 
1608d318a50SLinus Walleij 	/* The number of elements. IE now many chunks */
16143f2e1a3SLee Jones 	lli->reg_elt = (data_size / data_width) << D40_SREG_ELEM_PHY_ECNT_POS;
1628d318a50SLinus Walleij 
1638d318a50SLinus Walleij 	/*
1648d318a50SLinus Walleij 	 * Distance to next element sized entry.
1658d318a50SLinus Walleij 	 * Usually the size of the element unless you want gaps.
1668d318a50SLinus Walleij 	 */
1677f933bedSRabin Vincent 	if (addr_inc)
16843f2e1a3SLee Jones 		lli->reg_elt |= data_width << D40_SREG_ELEM_PHY_EIDX_POS;
1698d318a50SLinus Walleij 
1708d318a50SLinus Walleij 	/* Where the data is */
1718d318a50SLinus Walleij 	lli->reg_ptr = data;
1728d318a50SLinus Walleij 	lli->reg_cfg = reg_cfg;
1738d318a50SLinus Walleij 
1748d318a50SLinus Walleij 	/* If this scatter list entry is the last one, no next link */
1758d318a50SLinus Walleij 	if (next_lli == 0)
17616db3411SLee Jones 		lli->reg_lnk = BIT(D40_SREG_LNK_PHY_TCP_POS);
1778d318a50SLinus Walleij 	else
1788d318a50SLinus Walleij 		lli->reg_lnk = next_lli;
1798d318a50SLinus Walleij 
1808d318a50SLinus Walleij 	/* Set/clear interrupt generation on this link item.*/
1818d318a50SLinus Walleij 	if (term_int)
18216db3411SLee Jones 		lli->reg_cfg |= BIT(D40_SREG_CFG_TIM_POS);
1838d318a50SLinus Walleij 	else
18416db3411SLee Jones 		lli->reg_cfg &= ~BIT(D40_SREG_CFG_TIM_POS);
1858d318a50SLinus Walleij 
1868cc5af12SLee Jones 	/*
1878cc5af12SLee Jones 	 * Post link - D40_SREG_LNK_PHY_PRE_POS = 0
1888cc5af12SLee Jones 	 * Relink happens after transfer completion.
1898cc5af12SLee Jones 	 */
1908d318a50SLinus Walleij 
1918d318a50SLinus Walleij 	return 0;
1928d318a50SLinus Walleij }
1938d318a50SLinus Walleij 
d40_seg_size(int size,int data_width1,int data_width2)194d49278e3SPer Forlin static int d40_seg_size(int size, int data_width1, int data_width2)
195d49278e3SPer Forlin {
196d49278e3SPer Forlin 	u32 max_w = max(data_width1, data_width2);
197d49278e3SPer Forlin 	u32 min_w = min(data_width1, data_width2);
19843f2e1a3SLee Jones 	u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
199d49278e3SPer Forlin 
200d49278e3SPer Forlin 	if (seg_max > STEDMA40_MAX_SEG_SIZE)
20143f2e1a3SLee Jones 		seg_max -= max_w;
202d49278e3SPer Forlin 
203d49278e3SPer Forlin 	if (size <= seg_max)
204d49278e3SPer Forlin 		return size;
205d49278e3SPer Forlin 
206d49278e3SPer Forlin 	if (size <= 2 * seg_max)
20743f2e1a3SLee Jones 		return ALIGN(size / 2, max_w);
208d49278e3SPer Forlin 
209d49278e3SPer Forlin 	return seg_max;
210d49278e3SPer Forlin }
211d49278e3SPer Forlin 
212cc31b6f7SRabin Vincent static struct d40_phy_lli *
d40_phy_buf_to_lli(struct d40_phy_lli * lli,dma_addr_t addr,u32 size,dma_addr_t lli_phys,dma_addr_t first_phys,u32 reg_cfg,struct stedma40_half_channel_info * info,struct stedma40_half_channel_info * otherinfo,unsigned long flags)213cc31b6f7SRabin Vincent d40_phy_buf_to_lli(struct d40_phy_lli *lli, dma_addr_t addr, u32 size,
2140c842b55SRabin Vincent 		   dma_addr_t lli_phys, dma_addr_t first_phys, u32 reg_cfg,
2157f933bedSRabin Vincent 		   struct stedma40_half_channel_info *info,
2167f933bedSRabin Vincent 		   struct stedma40_half_channel_info *otherinfo,
2177f933bedSRabin Vincent 		   unsigned long flags)
218d49278e3SPer Forlin {
2190c842b55SRabin Vincent 	bool lastlink = flags & LLI_LAST_LINK;
2207f933bedSRabin Vincent 	bool addr_inc = flags & LLI_ADDR_INC;
2217f933bedSRabin Vincent 	bool term_int = flags & LLI_TERM_INT;
2220c842b55SRabin Vincent 	bool cyclic = flags & LLI_CYCLIC;
223d49278e3SPer Forlin 	int err;
224d49278e3SPer Forlin 	dma_addr_t next = lli_phys;
225d49278e3SPer Forlin 	int size_rest = size;
226d49278e3SPer Forlin 	int size_seg = 0;
227d49278e3SPer Forlin 
2287f933bedSRabin Vincent 	/*
2297f933bedSRabin Vincent 	 * This piece may be split up based on d40_seg_size(); we only want the
2307f933bedSRabin Vincent 	 * term int on the last part.
2317f933bedSRabin Vincent 	 */
2327f933bedSRabin Vincent 	if (term_int)
2337f933bedSRabin Vincent 		flags &= ~LLI_TERM_INT;
2347f933bedSRabin Vincent 
235d49278e3SPer Forlin 	do {
236cc31b6f7SRabin Vincent 		size_seg = d40_seg_size(size_rest, info->data_width,
237cc31b6f7SRabin Vincent 					otherinfo->data_width);
238d49278e3SPer Forlin 		size_rest -= size_seg;
239d49278e3SPer Forlin 
2400c842b55SRabin Vincent 		if (size_rest == 0 && term_int)
2417f933bedSRabin Vincent 			flags |= LLI_TERM_INT;
2420c842b55SRabin Vincent 
2430c842b55SRabin Vincent 		if (size_rest == 0 && lastlink)
2440c842b55SRabin Vincent 			next = cyclic ? first_phys : 0;
2450c842b55SRabin Vincent 		else
246d49278e3SPer Forlin 			next = ALIGN(next + sizeof(struct d40_phy_lli),
247d49278e3SPer Forlin 				     D40_LLI_ALIGN);
248d49278e3SPer Forlin 
2497f933bedSRabin Vincent 		err = d40_phy_fill_lli(lli, addr, size_seg, next,
2507f933bedSRabin Vincent 				       reg_cfg, info, flags);
251d49278e3SPer Forlin 
252d49278e3SPer Forlin 		if (err)
253d49278e3SPer Forlin 			goto err;
254d49278e3SPer Forlin 
255d49278e3SPer Forlin 		lli++;
2567f933bedSRabin Vincent 		if (addr_inc)
257d49278e3SPer Forlin 			addr += size_seg;
258d49278e3SPer Forlin 	} while (size_rest);
259d49278e3SPer Forlin 
260d49278e3SPer Forlin 	return lli;
261d49278e3SPer Forlin 
262d49278e3SPer Forlin err:
263d49278e3SPer Forlin 	return NULL;
264d49278e3SPer Forlin }
265d49278e3SPer Forlin 
d40_phy_sg_to_lli(struct scatterlist * sg,int sg_len,dma_addr_t target,struct d40_phy_lli * lli_sg,dma_addr_t lli_phys,u32 reg_cfg,struct stedma40_half_channel_info * info,struct stedma40_half_channel_info * otherinfo,unsigned long flags)2668d318a50SLinus Walleij int d40_phy_sg_to_lli(struct scatterlist *sg,
2678d318a50SLinus Walleij 		      int sg_len,
2688d318a50SLinus Walleij 		      dma_addr_t target,
269d49278e3SPer Forlin 		      struct d40_phy_lli *lli_sg,
2708d318a50SLinus Walleij 		      dma_addr_t lli_phys,
2718d318a50SLinus Walleij 		      u32 reg_cfg,
272cc31b6f7SRabin Vincent 		      struct stedma40_half_channel_info *info,
2730c842b55SRabin Vincent 		      struct stedma40_half_channel_info *otherinfo,
2740c842b55SRabin Vincent 		      unsigned long flags)
2758d318a50SLinus Walleij {
2768d318a50SLinus Walleij 	int total_size = 0;
2778d318a50SLinus Walleij 	int i;
2788d318a50SLinus Walleij 	struct scatterlist *current_sg = sg;
279d49278e3SPer Forlin 	struct d40_phy_lli *lli = lli_sg;
280d49278e3SPer Forlin 	dma_addr_t l_phys = lli_phys;
2817f933bedSRabin Vincent 
2827f933bedSRabin Vincent 	if (!target)
2837f933bedSRabin Vincent 		flags |= LLI_ADDR_INC;
2848d318a50SLinus Walleij 
2858d318a50SLinus Walleij 	for_each_sg(sg, current_sg, sg_len, i) {
2867f933bedSRabin Vincent 		dma_addr_t sg_addr = sg_dma_address(current_sg);
2877f933bedSRabin Vincent 		unsigned int len = sg_dma_len(current_sg);
2887f933bedSRabin Vincent 		dma_addr_t dst = target ?: sg_addr;
2898d318a50SLinus Walleij 
2908d318a50SLinus Walleij 		total_size += sg_dma_len(current_sg);
2918d318a50SLinus Walleij 
2927f933bedSRabin Vincent 		if (i == sg_len - 1)
2930c842b55SRabin Vincent 			flags |= LLI_TERM_INT | LLI_LAST_LINK;
2948d318a50SLinus Walleij 
295d49278e3SPer Forlin 		l_phys = ALIGN(lli_phys + (lli - lli_sg) *
296d49278e3SPer Forlin 			       sizeof(struct d40_phy_lli), D40_LLI_ALIGN);
297d49278e3SPer Forlin 
2980c842b55SRabin Vincent 		lli = d40_phy_buf_to_lli(lli, dst, len, l_phys, lli_phys,
2997f933bedSRabin Vincent 					 reg_cfg, info, otherinfo, flags);
3007f933bedSRabin Vincent 
301d49278e3SPer Forlin 		if (lli == NULL)
302d49278e3SPer Forlin 			return -EINVAL;
3038d318a50SLinus Walleij 	}
3048d318a50SLinus Walleij 
3058d318a50SLinus Walleij 	return total_size;
3068d318a50SLinus Walleij }
3078d318a50SLinus Walleij 
3088d318a50SLinus Walleij 
3098d318a50SLinus Walleij /* DMA logical lli operations */
3108d318a50SLinus Walleij 
d40_log_lli_link(struct d40_log_lli * lli_dst,struct d40_log_lli * lli_src,int next,unsigned int flags)311698e4732SJonas Aaberg static void d40_log_lli_link(struct d40_log_lli *lli_dst,
312698e4732SJonas Aaberg 			     struct d40_log_lli *lli_src,
3130c842b55SRabin Vincent 			     int next, unsigned int flags)
314698e4732SJonas Aaberg {
3150c842b55SRabin Vincent 	bool interrupt = flags & LLI_TERM_INT;
316698e4732SJonas Aaberg 	u32 slos = 0;
317698e4732SJonas Aaberg 	u32 dlos = 0;
318698e4732SJonas Aaberg 
319698e4732SJonas Aaberg 	if (next != -EINVAL) {
320698e4732SJonas Aaberg 		slos = next * 2;
321698e4732SJonas Aaberg 		dlos = next * 2 + 1;
3220c842b55SRabin Vincent 	}
3230c842b55SRabin Vincent 
3240c842b55SRabin Vincent 	if (interrupt) {
325698e4732SJonas Aaberg 		lli_dst->lcsp13 |= D40_MEM_LCSP1_SCFG_TIM_MASK;
326698e4732SJonas Aaberg 		lli_dst->lcsp13 |= D40_MEM_LCSP3_DTCP_MASK;
327698e4732SJonas Aaberg 	}
328698e4732SJonas Aaberg 
329698e4732SJonas Aaberg 	lli_src->lcsp13 = (lli_src->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
330698e4732SJonas Aaberg 		(slos << D40_MEM_LCSP1_SLOS_POS);
331698e4732SJonas Aaberg 
332698e4732SJonas Aaberg 	lli_dst->lcsp13 = (lli_dst->lcsp13 & ~D40_MEM_LCSP1_SLOS_MASK) |
333698e4732SJonas Aaberg 		(dlos << D40_MEM_LCSP1_SLOS_POS);
334698e4732SJonas Aaberg }
335698e4732SJonas Aaberg 
d40_log_lli_lcpa_write(struct d40_log_lli_full * lcpa,struct d40_log_lli * lli_dst,struct d40_log_lli * lli_src,int next,unsigned int flags)336698e4732SJonas Aaberg void d40_log_lli_lcpa_write(struct d40_log_lli_full *lcpa,
337698e4732SJonas Aaberg 			   struct d40_log_lli *lli_dst,
338698e4732SJonas Aaberg 			   struct d40_log_lli *lli_src,
3390c842b55SRabin Vincent 			   int next, unsigned int flags)
340698e4732SJonas Aaberg {
3410c842b55SRabin Vincent 	d40_log_lli_link(lli_dst, lli_src, next, flags);
342698e4732SJonas Aaberg 
3438a5d2039SPer Forlin 	writel_relaxed(lli_src->lcsp02, &lcpa[0].lcsp0);
3448a5d2039SPer Forlin 	writel_relaxed(lli_src->lcsp13, &lcpa[0].lcsp1);
3458a5d2039SPer Forlin 	writel_relaxed(lli_dst->lcsp02, &lcpa[0].lcsp2);
3468a5d2039SPer Forlin 	writel_relaxed(lli_dst->lcsp13, &lcpa[0].lcsp3);
347698e4732SJonas Aaberg }
348698e4732SJonas Aaberg 
d40_log_lli_lcla_write(struct d40_log_lli * lcla,struct d40_log_lli * lli_dst,struct d40_log_lli * lli_src,int next,unsigned int flags)349698e4732SJonas Aaberg void d40_log_lli_lcla_write(struct d40_log_lli *lcla,
350698e4732SJonas Aaberg 			   struct d40_log_lli *lli_dst,
351698e4732SJonas Aaberg 			   struct d40_log_lli *lli_src,
3520c842b55SRabin Vincent 			   int next, unsigned int flags)
353698e4732SJonas Aaberg {
3540c842b55SRabin Vincent 	d40_log_lli_link(lli_dst, lli_src, next, flags);
355698e4732SJonas Aaberg 
3568a5d2039SPer Forlin 	writel_relaxed(lli_src->lcsp02, &lcla[0].lcsp02);
3578a5d2039SPer Forlin 	writel_relaxed(lli_src->lcsp13, &lcla[0].lcsp13);
3588a5d2039SPer Forlin 	writel_relaxed(lli_dst->lcsp02, &lcla[1].lcsp02);
3598a5d2039SPer Forlin 	writel_relaxed(lli_dst->lcsp13, &lcla[1].lcsp13);
360698e4732SJonas Aaberg }
361698e4732SJonas Aaberg 
d40_log_fill_lli(struct d40_log_lli * lli,dma_addr_t data,u32 data_size,u32 reg_cfg,u32 data_width,unsigned int flags)362d49278e3SPer Forlin static void d40_log_fill_lli(struct d40_log_lli *lli,
3638d318a50SLinus Walleij 			     dma_addr_t data, u32 data_size,
364698e4732SJonas Aaberg 			     u32 reg_cfg,
3658d318a50SLinus Walleij 			     u32 data_width,
3667f933bedSRabin Vincent 			     unsigned int flags)
3678d318a50SLinus Walleij {
3687f933bedSRabin Vincent 	bool addr_inc = flags & LLI_ADDR_INC;
3697f933bedSRabin Vincent 
3708d318a50SLinus Walleij 	lli->lcsp13 = reg_cfg;
3718d318a50SLinus Walleij 
3728d318a50SLinus Walleij 	/* The number of elements to transfer */
37343f2e1a3SLee Jones 	lli->lcsp02 = ((data_size / data_width) <<
3748d318a50SLinus Walleij 		       D40_MEM_LCSP0_ECNT_POS) & D40_MEM_LCSP0_ECNT_MASK;
375d49278e3SPer Forlin 
37643f2e1a3SLee Jones 	BUG_ON((data_size / data_width) > STEDMA40_MAX_SEG_SIZE);
377d49278e3SPer Forlin 
3788d318a50SLinus Walleij 	/* 16 LSBs address of the current element */
3798d318a50SLinus Walleij 	lli->lcsp02 |= data & D40_MEM_LCSP0_SPTR_MASK;
3808d318a50SLinus Walleij 	/* 16 MSBs address of the current element */
3818d318a50SLinus Walleij 	lli->lcsp13 |= data & D40_MEM_LCSP1_SPTR_MASK;
3828d318a50SLinus Walleij 
3838d318a50SLinus Walleij 	if (addr_inc)
3848d318a50SLinus Walleij 		lli->lcsp13 |= D40_MEM_LCSP1_SCFG_INCR_MASK;
3858d318a50SLinus Walleij 
3868d318a50SLinus Walleij }
3878d318a50SLinus Walleij 
d40_log_buf_to_lli(struct d40_log_lli * lli_sg,dma_addr_t addr,int size,u32 lcsp13,u32 data_width1,u32 data_width2,unsigned int flags)3881f7622caSRabin Vincent static struct d40_log_lli *d40_log_buf_to_lli(struct d40_log_lli *lli_sg,
389d49278e3SPer Forlin 				       dma_addr_t addr,
390d49278e3SPer Forlin 				       int size,
391d49278e3SPer Forlin 				       u32 lcsp13, /* src or dst*/
392d49278e3SPer Forlin 				       u32 data_width1,
393d49278e3SPer Forlin 				       u32 data_width2,
3947f933bedSRabin Vincent 				       unsigned int flags)
395d49278e3SPer Forlin {
3967f933bedSRabin Vincent 	bool addr_inc = flags & LLI_ADDR_INC;
397d49278e3SPer Forlin 	struct d40_log_lli *lli = lli_sg;
398d49278e3SPer Forlin 	int size_rest = size;
399d49278e3SPer Forlin 	int size_seg = 0;
400d49278e3SPer Forlin 
401d49278e3SPer Forlin 	do {
402d49278e3SPer Forlin 		size_seg = d40_seg_size(size_rest, data_width1, data_width2);
403d49278e3SPer Forlin 		size_rest -= size_seg;
404d49278e3SPer Forlin 
405d49278e3SPer Forlin 		d40_log_fill_lli(lli,
406d49278e3SPer Forlin 				 addr,
407d49278e3SPer Forlin 				 size_seg,
408d49278e3SPer Forlin 				 lcsp13, data_width1,
4097f933bedSRabin Vincent 				 flags);
410d49278e3SPer Forlin 		if (addr_inc)
411d49278e3SPer Forlin 			addr += size_seg;
412d49278e3SPer Forlin 		lli++;
413d49278e3SPer Forlin 	} while (size_rest);
414d49278e3SPer Forlin 
415d49278e3SPer Forlin 	return lli;
416d49278e3SPer Forlin }
417d49278e3SPer Forlin 
d40_log_sg_to_lli(struct scatterlist * sg,int sg_len,dma_addr_t dev_addr,struct d40_log_lli * lli_sg,u32 lcsp13,u32 data_width1,u32 data_width2)418698e4732SJonas Aaberg int d40_log_sg_to_lli(struct scatterlist *sg,
4198d318a50SLinus Walleij 		      int sg_len,
4205ed04b85SRabin Vincent 		      dma_addr_t dev_addr,
4218d318a50SLinus Walleij 		      struct d40_log_lli *lli_sg,
4228d318a50SLinus Walleij 		      u32 lcsp13, /* src or dst*/
423d49278e3SPer Forlin 		      u32 data_width1, u32 data_width2)
4248d318a50SLinus Walleij {
4258d318a50SLinus Walleij 	int total_size = 0;
4268d318a50SLinus Walleij 	struct scatterlist *current_sg = sg;
4278d318a50SLinus Walleij 	int i;
428d49278e3SPer Forlin 	struct d40_log_lli *lli = lli_sg;
4297f933bedSRabin Vincent 	unsigned long flags = 0;
4307f933bedSRabin Vincent 
4317f933bedSRabin Vincent 	if (!dev_addr)
4327f933bedSRabin Vincent 		flags |= LLI_ADDR_INC;
4338d318a50SLinus Walleij 
4348d318a50SLinus Walleij 	for_each_sg(sg, current_sg, sg_len, i) {
4355ed04b85SRabin Vincent 		dma_addr_t sg_addr = sg_dma_address(current_sg);
4365ed04b85SRabin Vincent 		unsigned int len = sg_dma_len(current_sg);
4375ed04b85SRabin Vincent 		dma_addr_t addr = dev_addr ?: sg_addr;
4385ed04b85SRabin Vincent 
4398d318a50SLinus Walleij 		total_size += sg_dma_len(current_sg);
4405ed04b85SRabin Vincent 
4415ed04b85SRabin Vincent 		lli = d40_log_buf_to_lli(lli, addr, len,
442d49278e3SPer Forlin 					 lcsp13,
4435ed04b85SRabin Vincent 					 data_width1,
4445ed04b85SRabin Vincent 					 data_width2,
4457f933bedSRabin Vincent 					 flags);
4468d318a50SLinus Walleij 	}
4475ed04b85SRabin Vincent 
4488d318a50SLinus Walleij 	return total_size;
4498d318a50SLinus Walleij }
450