xref: /openbmc/linux/drivers/dma/imx-sdma.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c01faacaSFabio Estevam // SPDX-License-Identifier: GPL-2.0+
2c01faacaSFabio Estevam //
3c01faacaSFabio Estevam // drivers/dma/imx-sdma.c
4c01faacaSFabio Estevam //
5c01faacaSFabio Estevam // This file contains a driver for the Freescale Smart DMA engine
6c01faacaSFabio Estevam //
7c01faacaSFabio Estevam // Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
8c01faacaSFabio Estevam //
9c01faacaSFabio Estevam // Based on code from Freescale:
10c01faacaSFabio Estevam //
11c01faacaSFabio Estevam // Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
121ec1e82fSSascha Hauer 
131ec1e82fSSascha Hauer #include <linux/init.h>
141d069bfaSMichael Olbrich #include <linux/iopoll.h>
15f8de8f4cSAxel Lin #include <linux/module.h>
161ec1e82fSSascha Hauer #include <linux/types.h>
17824a0a02SSascha Hauer #include <linux/bitfield.h>
180bbc1413SRichard Zhao #include <linux/bitops.h>
191ec1e82fSSascha Hauer #include <linux/mm.h>
201ec1e82fSSascha Hauer #include <linux/interrupt.h>
211ec1e82fSSascha Hauer #include <linux/clk.h>
222ccaef05SRichard Zhao #include <linux/delay.h>
231ec1e82fSSascha Hauer #include <linux/sched.h>
241ec1e82fSSascha Hauer #include <linux/semaphore.h>
251ec1e82fSSascha Hauer #include <linux/spinlock.h>
261ec1e82fSSascha Hauer #include <linux/device.h>
271ec1e82fSSascha Hauer #include <linux/dma-mapping.h>
281ec1e82fSSascha Hauer #include <linux/firmware.h>
291ec1e82fSSascha Hauer #include <linux/slab.h>
301ec1e82fSSascha Hauer #include <linux/platform_device.h>
311ec1e82fSSascha Hauer #include <linux/dmaengine.h>
32580975d7SShawn Guo #include <linux/of.h>
338391ecf4SShengjiu Wang #include <linux/of_address.h>
349479e17cSShawn Guo #include <linux/of_dma.h>
35b8603d2aSLucas Stach #include <linux/workqueue.h>
361ec1e82fSSascha Hauer 
371ec1e82fSSascha Hauer #include <asm/irq.h>
38c6547c2eSSascha Hauer #include <linux/dma/imx-dma.h>
39d078cd1bSZidan Wang #include <linux/regmap.h>
40d078cd1bSZidan Wang #include <linux/mfd/syscon.h>
41d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
421ec1e82fSSascha Hauer 
43d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
4457b772b8SRobin Gong #include "virt-dma.h"
45d2ebfb33SRussell King - ARM Linux 
461ec1e82fSSascha Hauer /* SDMA registers */
471ec1e82fSSascha Hauer #define SDMA_H_C0PTR		0x000
481ec1e82fSSascha Hauer #define SDMA_H_INTR		0x004
491ec1e82fSSascha Hauer #define SDMA_H_STATSTOP		0x008
501ec1e82fSSascha Hauer #define SDMA_H_START		0x00c
511ec1e82fSSascha Hauer #define SDMA_H_EVTOVR		0x010
521ec1e82fSSascha Hauer #define SDMA_H_DSPOVR		0x014
531ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR		0x018
541ec1e82fSSascha Hauer #define SDMA_H_EVTPEND		0x01c
551ec1e82fSSascha Hauer #define SDMA_H_DSPENBL		0x020
561ec1e82fSSascha Hauer #define SDMA_H_RESET		0x024
571ec1e82fSSascha Hauer #define SDMA_H_EVTERR		0x028
581ec1e82fSSascha Hauer #define SDMA_H_INTRMSK		0x02c
591ec1e82fSSascha Hauer #define SDMA_H_PSW		0x030
601ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG	0x034
611ec1e82fSSascha Hauer #define SDMA_H_CONFIG		0x038
621ec1e82fSSascha Hauer #define SDMA_ONCE_ENB		0x040
631ec1e82fSSascha Hauer #define SDMA_ONCE_DATA		0x044
641ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR		0x048
651ec1e82fSSascha Hauer #define SDMA_ONCE_STAT		0x04c
661ec1e82fSSascha Hauer #define SDMA_ONCE_CMD		0x050
671ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR		0x054
681ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR	0x058
691ec1e82fSSascha Hauer #define SDMA_CHN0ADDR		0x05c
701ec1e82fSSascha Hauer #define SDMA_ONCE_RTB		0x060
711ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1	0x070
721ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2	0x074
7362550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35	0x200
7462550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31	0x080
751ec1e82fSSascha Hauer #define SDMA_CHNPRI_0		0x100
76824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG	0x1000
771ec1e82fSSascha Hauer 
781ec1e82fSSascha Hauer /*
791ec1e82fSSascha Hauer  * Buffer descriptor status values.
801ec1e82fSSascha Hauer  */
811ec1e82fSSascha Hauer #define BD_DONE  0x01
821ec1e82fSSascha Hauer #define BD_WRAP  0x02
831ec1e82fSSascha Hauer #define BD_CONT  0x04
841ec1e82fSSascha Hauer #define BD_INTR  0x08
851ec1e82fSSascha Hauer #define BD_RROR  0x10
861ec1e82fSSascha Hauer #define BD_LAST  0x20
871ec1e82fSSascha Hauer #define BD_EXTD  0x80
881ec1e82fSSascha Hauer 
891ec1e82fSSascha Hauer /*
901ec1e82fSSascha Hauer  * Data Node descriptor status values.
911ec1e82fSSascha Hauer  */
921ec1e82fSSascha Hauer #define DND_END_OF_FRAME  0x80
931ec1e82fSSascha Hauer #define DND_END_OF_XFER   0x40
941ec1e82fSSascha Hauer #define DND_DONE          0x20
951ec1e82fSSascha Hauer #define DND_UNUSED        0x01
961ec1e82fSSascha Hauer 
971ec1e82fSSascha Hauer /*
981ec1e82fSSascha Hauer  * IPCV2 descriptor status values.
991ec1e82fSSascha Hauer  */
1001ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME  0x40
1011ec1e82fSSascha Hauer 
1021ec1e82fSSascha Hauer #define IPCV2_MAX_NODES        50
1031ec1e82fSSascha Hauer /*
1041ec1e82fSSascha Hauer  * Error bit set in the CCB status field by the SDMA,
1051ec1e82fSSascha Hauer  * in setbd routine, in case of a transfer error
1061ec1e82fSSascha Hauer  */
1071ec1e82fSSascha Hauer #define DATA_ERROR  0x10000000
1081ec1e82fSSascha Hauer 
1091ec1e82fSSascha Hauer /*
1101ec1e82fSSascha Hauer  * Buffer descriptor commands.
1111ec1e82fSSascha Hauer  */
1121ec1e82fSSascha Hauer #define C0_ADDR             0x01
1131ec1e82fSSascha Hauer #define C0_LOAD             0x02
1141ec1e82fSSascha Hauer #define C0_DUMP             0x03
1151ec1e82fSSascha Hauer #define C0_SETCTX           0x07
1161ec1e82fSSascha Hauer #define C0_GETCTX           0x03
1171ec1e82fSSascha Hauer #define C0_SETDM            0x01
1181ec1e82fSSascha Hauer #define C0_SETPM            0x04
1191ec1e82fSSascha Hauer #define C0_GETDM            0x02
1201ec1e82fSSascha Hauer #define C0_GETPM            0x08
1211ec1e82fSSascha Hauer /*
1221ec1e82fSSascha Hauer  * Change endianness indicator in the BD command field
1231ec1e82fSSascha Hauer  */
1241ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS   0x80
1251ec1e82fSSascha Hauer 
1261ec1e82fSSascha Hauer /*
1278391ecf4SShengjiu Wang  *  p_2_p watermark_level description
1288391ecf4SShengjiu Wang  *	Bits		Name			Description
1298391ecf4SShengjiu Wang  *	0-7		Lower WML		Lower watermark level
1308391ecf4SShengjiu Wang  *	8		PS			1: Pad Swallowing
1318391ecf4SShengjiu Wang  *						0: No Pad Swallowing
1328391ecf4SShengjiu Wang  *	9		PA			1: Pad Adding
1338391ecf4SShengjiu Wang  *						0: No Pad Adding
1348391ecf4SShengjiu Wang  *	10		SPDIF			If this bit is set both source
1358391ecf4SShengjiu Wang  *						and destination are on SPBA
1368391ecf4SShengjiu Wang  *	11		Source Bit(SP)		1: Source on SPBA
1378391ecf4SShengjiu Wang  *						0: Source on AIPS
1388391ecf4SShengjiu Wang  *	12		Destination Bit(DP)	1: Destination on SPBA
1398391ecf4SShengjiu Wang  *						0: Destination on AIPS
1408391ecf4SShengjiu Wang  *	13-15		---------		MUST BE 0
1418391ecf4SShengjiu Wang  *	16-23		Higher WML		HWML
1428391ecf4SShengjiu Wang  *	24-27		N			Total number of samples after
1438391ecf4SShengjiu Wang  *						which Pad adding/Swallowing
1448391ecf4SShengjiu Wang  *						must be done. It must be odd.
1458391ecf4SShengjiu Wang  *	28		Lower WML Event(LWE)	SDMA events reg to check for
1468391ecf4SShengjiu Wang  *						LWML event mask
1478391ecf4SShengjiu Wang  *						0: LWE in EVENTS register
1488391ecf4SShengjiu Wang  *						1: LWE in EVENTS2 register
1498391ecf4SShengjiu Wang  *	29		Higher WML Event(HWE)	SDMA events reg to check for
1508391ecf4SShengjiu Wang  *						HWML event mask
1518391ecf4SShengjiu Wang  *						0: HWE in EVENTS register
1528391ecf4SShengjiu Wang  *						1: HWE in EVENTS2 register
1538391ecf4SShengjiu Wang  *	30		---------		MUST BE 0
1548391ecf4SShengjiu Wang  *	31		CONT			1: Amount of samples to be
1558391ecf4SShengjiu Wang  *						transferred is unknown and
1568391ecf4SShengjiu Wang  *						script will keep on
1578391ecf4SShengjiu Wang  *						transferring samples as long as
1588391ecf4SShengjiu Wang  *						both events are detected and
1598391ecf4SShengjiu Wang  *						script must be manually stopped
1608391ecf4SShengjiu Wang  *						by the application
1618391ecf4SShengjiu Wang  *						0: The amount of samples to be
1628391ecf4SShengjiu Wang  *						transferred is equal to the
1638391ecf4SShengjiu Wang  *						count field of mode word
1648391ecf4SShengjiu Wang  */
1658391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML	0xFF
1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS		BIT(8)
1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA		BIT(9)
1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF	BIT(10)
1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP		BIT(11)
1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP		BIT(12)
1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML	(0xFF << 16)
1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE	BIT(28)
1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE	BIT(29)
1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT	BIT(31)
1758391ecf4SShengjiu Wang 
176f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
177f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
178f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
179f9d4a398SNicolin Chen 
180f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS	(BIT(DMA_DEV_TO_MEM) | \
181f9d4a398SNicolin Chen 				 BIT(DMA_MEM_TO_DEV) | \
182f9d4a398SNicolin Chen 				 BIT(DMA_DEV_TO_DEV))
183f9d4a398SNicolin Chen 
184824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_N_FIFOS	GENMASK(15, 12)
185e0c7ea83SShengjiu Wang #define SDMA_WATERMARK_LEVEL_OFF_FIFOS  GENMASK(19, 16)
186e0c7ea83SShengjiu Wang #define SDMA_WATERMARK_LEVEL_WORDS_PER_FIFO   GENMASK(31, 28)
187824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_SW_DONE	BIT(23)
188824a0a02SSascha Hauer 
189824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_SEL	BIT(7)
190824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_DIS	BIT(6)
191824a0a02SSascha Hauer 
19201eafd4bSShengjiu Wang /*
1938d11cfb0SVladimir Zapolskiy  * struct sdma_script_start_addrs - SDMA script start pointers
1948d11cfb0SVladimir Zapolskiy  *
1958d11cfb0SVladimir Zapolskiy  * start addresses of the different functions in the physical
1968d11cfb0SVladimir Zapolskiy  * address space of the SDMA engine.
1978d11cfb0SVladimir Zapolskiy  */
1988d11cfb0SVladimir Zapolskiy struct sdma_script_start_addrs {
1998d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_addr;
2008d11cfb0SVladimir Zapolskiy 	s32 ap_2_bp_addr;
2018d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_fixed_addr;
2028d11cfb0SVladimir Zapolskiy 	s32 bp_2_ap_addr;
2038d11cfb0SVladimir Zapolskiy 	s32 loopback_on_dsp_side_addr;
2048d11cfb0SVladimir Zapolskiy 	s32 mcu_interrupt_only_addr;
2058d11cfb0SVladimir Zapolskiy 	s32 firi_2_per_addr;
2068d11cfb0SVladimir Zapolskiy 	s32 firi_2_mcu_addr;
2078d11cfb0SVladimir Zapolskiy 	s32 per_2_firi_addr;
2088d11cfb0SVladimir Zapolskiy 	s32 mcu_2_firi_addr;
2098d11cfb0SVladimir Zapolskiy 	s32 uart_2_per_addr;
210a3ae97f4SKevin Groeneveld 	s32 uart_2_mcu_addr;
2118d11cfb0SVladimir Zapolskiy 	s32 per_2_app_addr;
2128d11cfb0SVladimir Zapolskiy 	s32 mcu_2_app_addr;
2138d11cfb0SVladimir Zapolskiy 	s32 per_2_per_addr;
2148d11cfb0SVladimir Zapolskiy 	s32 uartsh_2_per_addr;
215a3ae97f4SKevin Groeneveld 	s32 uartsh_2_mcu_addr;
2168d11cfb0SVladimir Zapolskiy 	s32 per_2_shp_addr;
2178d11cfb0SVladimir Zapolskiy 	s32 mcu_2_shp_addr;
2188d11cfb0SVladimir Zapolskiy 	s32 ata_2_mcu_addr;
2198d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ata_addr;
2208d11cfb0SVladimir Zapolskiy 	s32 app_2_per_addr;
2218d11cfb0SVladimir Zapolskiy 	s32 app_2_mcu_addr;
2228d11cfb0SVladimir Zapolskiy 	s32 shp_2_per_addr;
2238d11cfb0SVladimir Zapolskiy 	s32 shp_2_mcu_addr;
2248d11cfb0SVladimir Zapolskiy 	s32 mshc_2_mcu_addr;
2258d11cfb0SVladimir Zapolskiy 	s32 mcu_2_mshc_addr;
2268d11cfb0SVladimir Zapolskiy 	s32 spdif_2_mcu_addr;
2278d11cfb0SVladimir Zapolskiy 	s32 mcu_2_spdif_addr;
2288d11cfb0SVladimir Zapolskiy 	s32 asrc_2_mcu_addr;
2298d11cfb0SVladimir Zapolskiy 	s32 ext_mem_2_ipu_addr;
2308d11cfb0SVladimir Zapolskiy 	s32 descrambler_addr;
2318d11cfb0SVladimir Zapolskiy 	s32 dptc_dvfs_addr;
2328d11cfb0SVladimir Zapolskiy 	s32 utra_addr;
2338d11cfb0SVladimir Zapolskiy 	s32 ram_code_start_addr;
2348d11cfb0SVladimir Zapolskiy 	/* End of v1 array */
2358d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ssish_addr;
2368d11cfb0SVladimir Zapolskiy 	s32 ssish_2_mcu_addr;
2378d11cfb0SVladimir Zapolskiy 	s32 hdmi_dma_addr;
2388d11cfb0SVladimir Zapolskiy 	/* End of v2 array */
2398d11cfb0SVladimir Zapolskiy 	s32 zcanfd_2_mcu_addr;
2408d11cfb0SVladimir Zapolskiy 	s32 zqspi_2_mcu_addr;
2418d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ecspi_addr;
242b98ce2f4SRobin Gong 	s32 mcu_2_sai_addr;
243b98ce2f4SRobin Gong 	s32 sai_2_mcu_addr;
244a3ae97f4SKevin Groeneveld 	s32 uart_2_mcu_rom_addr;
245a3ae97f4SKevin Groeneveld 	s32 uartsh_2_mcu_rom_addr;
2468d11cfb0SVladimir Zapolskiy 	/* End of v3 array */
2478d11cfb0SVladimir Zapolskiy 	s32 mcu_2_zqspi_addr;
2488d11cfb0SVladimir Zapolskiy 	/* End of v4 array */
2498d11cfb0SVladimir Zapolskiy };
2508d11cfb0SVladimir Zapolskiy 
2518391ecf4SShengjiu Wang /*
2521ec1e82fSSascha Hauer  * Mode/Count of data node descriptors - IPCv2
2531ec1e82fSSascha Hauer  */
2541ec1e82fSSascha Hauer struct sdma_mode_count {
2554a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT	0xffff
2561ec1e82fSSascha Hauer 	u32 count   : 16; /* size of the buffer pointed by this BD */
2571ec1e82fSSascha Hauer 	u32 status  :  8; /* E,R,I,C,W,D status bits stored here */
258e4b75760SMartin Kaiser 	u32 command :  8; /* command mostly used for channel 0 */
2591ec1e82fSSascha Hauer };
2601ec1e82fSSascha Hauer 
2611ec1e82fSSascha Hauer /*
2621ec1e82fSSascha Hauer  * Buffer descriptor
2631ec1e82fSSascha Hauer  */
2641ec1e82fSSascha Hauer struct sdma_buffer_descriptor {
2651ec1e82fSSascha Hauer 	struct sdma_mode_count  mode;
2661ec1e82fSSascha Hauer 	u32 buffer_addr;	/* address of the buffer described */
2671ec1e82fSSascha Hauer 	u32 ext_buffer_addr;	/* extended buffer address */
2681ec1e82fSSascha Hauer } __attribute__ ((packed));
2691ec1e82fSSascha Hauer 
2701ec1e82fSSascha Hauer /**
2711ec1e82fSSascha Hauer  * struct sdma_channel_control - Channel control Block
2721ec1e82fSSascha Hauer  *
27324ca312dSRobin Gong  * @current_bd_ptr:	current buffer descriptor processed
27424ca312dSRobin Gong  * @base_bd_ptr:	first element of buffer descriptor array
27524ca312dSRobin Gong  * @unused:		padding. The SDMA engine expects an array of 128 byte
2761ec1e82fSSascha Hauer  *			control blocks
2771ec1e82fSSascha Hauer  */
2781ec1e82fSSascha Hauer struct sdma_channel_control {
2791ec1e82fSSascha Hauer 	u32 current_bd_ptr;
2801ec1e82fSSascha Hauer 	u32 base_bd_ptr;
2811ec1e82fSSascha Hauer 	u32 unused[2];
2821ec1e82fSSascha Hauer } __attribute__ ((packed));
2831ec1e82fSSascha Hauer 
2841ec1e82fSSascha Hauer /**
2851ec1e82fSSascha Hauer  * struct sdma_state_registers - SDMA context for a channel
2861ec1e82fSSascha Hauer  *
2871ec1e82fSSascha Hauer  * @pc:		program counter
28824ca312dSRobin Gong  * @unused1:	unused
2891ec1e82fSSascha Hauer  * @t:		test bit: status of arithmetic & test instruction
2901ec1e82fSSascha Hauer  * @rpc:	return program counter
29124ca312dSRobin Gong  * @unused0:	unused
2921ec1e82fSSascha Hauer  * @sf:		source fault while loading data
2931ec1e82fSSascha Hauer  * @spc:	loop start program counter
29424ca312dSRobin Gong  * @unused2:	unused
2951ec1e82fSSascha Hauer  * @df:		destination fault while storing data
2961ec1e82fSSascha Hauer  * @epc:	loop end program counter
2971ec1e82fSSascha Hauer  * @lm:		loop mode
2981ec1e82fSSascha Hauer  */
2991ec1e82fSSascha Hauer struct sdma_state_registers {
3001ec1e82fSSascha Hauer 	u32 pc     :14;
3011ec1e82fSSascha Hauer 	u32 unused1: 1;
3021ec1e82fSSascha Hauer 	u32 t      : 1;
3031ec1e82fSSascha Hauer 	u32 rpc    :14;
3041ec1e82fSSascha Hauer 	u32 unused0: 1;
3051ec1e82fSSascha Hauer 	u32 sf     : 1;
3061ec1e82fSSascha Hauer 	u32 spc    :14;
3071ec1e82fSSascha Hauer 	u32 unused2: 1;
3081ec1e82fSSascha Hauer 	u32 df     : 1;
3091ec1e82fSSascha Hauer 	u32 epc    :14;
3101ec1e82fSSascha Hauer 	u32 lm     : 2;
3111ec1e82fSSascha Hauer } __attribute__ ((packed));
3121ec1e82fSSascha Hauer 
3131ec1e82fSSascha Hauer /**
3141ec1e82fSSascha Hauer  * struct sdma_context_data - sdma context specific to a channel
3151ec1e82fSSascha Hauer  *
3161ec1e82fSSascha Hauer  * @channel_state:	channel state bits
3171ec1e82fSSascha Hauer  * @gReg:		general registers
3181ec1e82fSSascha Hauer  * @mda:		burst dma destination address register
3191ec1e82fSSascha Hauer  * @msa:		burst dma source address register
3201ec1e82fSSascha Hauer  * @ms:			burst dma status register
3211ec1e82fSSascha Hauer  * @md:			burst dma data register
3221ec1e82fSSascha Hauer  * @pda:		peripheral dma destination address register
3231ec1e82fSSascha Hauer  * @psa:		peripheral dma source address register
3241ec1e82fSSascha Hauer  * @ps:			peripheral dma status register
3251ec1e82fSSascha Hauer  * @pd:			peripheral dma data register
3261ec1e82fSSascha Hauer  * @ca:			CRC polynomial register
3271ec1e82fSSascha Hauer  * @cs:			CRC accumulator register
3281ec1e82fSSascha Hauer  * @dda:		dedicated core destination address register
3291ec1e82fSSascha Hauer  * @dsa:		dedicated core source address register
3301ec1e82fSSascha Hauer  * @ds:			dedicated core status register
3311ec1e82fSSascha Hauer  * @dd:			dedicated core data register
33224ca312dSRobin Gong  * @scratch0:		1st word of dedicated ram for context switch
33324ca312dSRobin Gong  * @scratch1:		2nd word of dedicated ram for context switch
33424ca312dSRobin Gong  * @scratch2:		3rd word of dedicated ram for context switch
33524ca312dSRobin Gong  * @scratch3:		4th word of dedicated ram for context switch
33624ca312dSRobin Gong  * @scratch4:		5th word of dedicated ram for context switch
33724ca312dSRobin Gong  * @scratch5:		6th word of dedicated ram for context switch
33824ca312dSRobin Gong  * @scratch6:		7th word of dedicated ram for context switch
33924ca312dSRobin Gong  * @scratch7:		8th word of dedicated ram for context switch
3401ec1e82fSSascha Hauer  */
3411ec1e82fSSascha Hauer struct sdma_context_data {
3421ec1e82fSSascha Hauer 	struct sdma_state_registers  channel_state;
3431ec1e82fSSascha Hauer 	u32  gReg[8];
3441ec1e82fSSascha Hauer 	u32  mda;
3451ec1e82fSSascha Hauer 	u32  msa;
3461ec1e82fSSascha Hauer 	u32  ms;
3471ec1e82fSSascha Hauer 	u32  md;
3481ec1e82fSSascha Hauer 	u32  pda;
3491ec1e82fSSascha Hauer 	u32  psa;
3501ec1e82fSSascha Hauer 	u32  ps;
3511ec1e82fSSascha Hauer 	u32  pd;
3521ec1e82fSSascha Hauer 	u32  ca;
3531ec1e82fSSascha Hauer 	u32  cs;
3541ec1e82fSSascha Hauer 	u32  dda;
3551ec1e82fSSascha Hauer 	u32  dsa;
3561ec1e82fSSascha Hauer 	u32  ds;
3571ec1e82fSSascha Hauer 	u32  dd;
3581ec1e82fSSascha Hauer 	u32  scratch0;
3591ec1e82fSSascha Hauer 	u32  scratch1;
3601ec1e82fSSascha Hauer 	u32  scratch2;
3611ec1e82fSSascha Hauer 	u32  scratch3;
3621ec1e82fSSascha Hauer 	u32  scratch4;
3631ec1e82fSSascha Hauer 	u32  scratch5;
3641ec1e82fSSascha Hauer 	u32  scratch6;
3651ec1e82fSSascha Hauer 	u32  scratch7;
3661ec1e82fSSascha Hauer } __attribute__ ((packed));
3671ec1e82fSSascha Hauer 
3681ec1e82fSSascha Hauer 
3691ec1e82fSSascha Hauer struct sdma_engine;
3701ec1e82fSSascha Hauer 
3711ec1e82fSSascha Hauer /**
37276c33d27SSascha Hauer  * struct sdma_desc - descriptor structor for one transfer
37324ca312dSRobin Gong  * @vd:			descriptor for virt dma
37424ca312dSRobin Gong  * @num_bd:		number of descriptors currently handling
37524ca312dSRobin Gong  * @bd_phys:		physical address of bd
37624ca312dSRobin Gong  * @buf_tail:		ID of the buffer that was processed
37724ca312dSRobin Gong  * @buf_ptail:		ID of the previous buffer that was processed
37824ca312dSRobin Gong  * @period_len:		period length, used in cyclic.
37924ca312dSRobin Gong  * @chn_real_count:	the real count updated from bd->mode.count
38024ca312dSRobin Gong  * @chn_count:		the transfer count set
38124ca312dSRobin Gong  * @sdmac:		sdma_channel pointer
38224ca312dSRobin Gong  * @bd:			pointer of allocate bd
38376c33d27SSascha Hauer  */
38476c33d27SSascha Hauer struct sdma_desc {
38557b772b8SRobin Gong 	struct virt_dma_desc	vd;
38676c33d27SSascha Hauer 	unsigned int		num_bd;
38776c33d27SSascha Hauer 	dma_addr_t		bd_phys;
38876c33d27SSascha Hauer 	unsigned int		buf_tail;
38976c33d27SSascha Hauer 	unsigned int		buf_ptail;
39076c33d27SSascha Hauer 	unsigned int		period_len;
39176c33d27SSascha Hauer 	unsigned int		chn_real_count;
39276c33d27SSascha Hauer 	unsigned int		chn_count;
39376c33d27SSascha Hauer 	struct sdma_channel	*sdmac;
39476c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd;
39576c33d27SSascha Hauer };
39676c33d27SSascha Hauer 
39776c33d27SSascha Hauer /**
3981ec1e82fSSascha Hauer  * struct sdma_channel - housekeeping for a SDMA channel
3991ec1e82fSSascha Hauer  *
40024ca312dSRobin Gong  * @vc:			virt_dma base structure
40124ca312dSRobin Gong  * @desc:		sdma description including vd and other special member
40224ca312dSRobin Gong  * @sdma:		pointer to the SDMA engine for this channel
40324ca312dSRobin Gong  * @channel:		the channel number, matches dmaengine chan_id + 1
40424ca312dSRobin Gong  * @direction:		transfer type. Needed for setting SDMA script
405d0c4a149SLee Jones  * @slave_config:	Slave configuration
40624ca312dSRobin Gong  * @peripheral_type:	Peripheral type. Needed for setting SDMA script
40724ca312dSRobin Gong  * @event_id0:		aka dma request line
40824ca312dSRobin Gong  * @event_id1:		for channels that use 2 events
40924ca312dSRobin Gong  * @word_size:		peripheral access size
41024ca312dSRobin Gong  * @pc_from_device:	script address for those device_2_memory
41124ca312dSRobin Gong  * @pc_to_device:	script address for those memory_2_device
41224ca312dSRobin Gong  * @device_to_device:	script address for those device_2_device
4130f06c027SRobin Gong  * @pc_to_pc:		script address for those memory_2_memory
41424ca312dSRobin Gong  * @flags:		loop mode or not
41524ca312dSRobin Gong  * @per_address:	peripheral source or destination address in common case
41624ca312dSRobin Gong  *                      destination address in p_2_p case
41724ca312dSRobin Gong  * @per_address2:	peripheral source address in p_2_p case
41824ca312dSRobin Gong  * @event_mask:		event mask used in p_2_p script
41924ca312dSRobin Gong  * @watermark_level:	value for gReg[7], some script will extend it from
42024ca312dSRobin Gong  *			basic watermark such as p_2_p
42124ca312dSRobin Gong  * @shp_addr:		value for gReg[6]
42224ca312dSRobin Gong  * @per_addr:		value for gReg[2]
42324ca312dSRobin Gong  * @status:		status of dma channel
424d0c4a149SLee Jones  * @context_loaded:	ensure context is only loaded once
42524ca312dSRobin Gong  * @data:		specific sdma interface structure
42624ca312dSRobin Gong  * @bd_pool:		dma_pool for bd
427d0c4a149SLee Jones  * @terminate_worker:	used to call back into terminate work function
42801eafd4bSShengjiu Wang  * @terminated:		terminated list
42901eafd4bSShengjiu Wang  * @is_ram_script:	flag for script in ram
43001eafd4bSShengjiu Wang  * @n_fifos_src:	number of source device fifos
43101eafd4bSShengjiu Wang  * @n_fifos_dst:	number of destination device fifos
43201eafd4bSShengjiu Wang  * @sw_done:		software done flag
433e0c7ea83SShengjiu Wang  * @stride_fifos_src:	stride for source device FIFOs
434e0c7ea83SShengjiu Wang  * @stride_fifos_dst:	stride for destination device FIFOs
435e0c7ea83SShengjiu Wang  * @words_per_fifo:	copy number of words one time for one FIFO
4361ec1e82fSSascha Hauer  */
4371ec1e82fSSascha Hauer struct sdma_channel {
43857b772b8SRobin Gong 	struct virt_dma_chan		vc;
43976c33d27SSascha Hauer 	struct sdma_desc		*desc;
4401ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
4411ec1e82fSSascha Hauer 	unsigned int			channel;
442db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
443107d0644SVinod Koul 	struct dma_slave_config		slave_config;
4441ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
4451ec1e82fSSascha Hauer 	unsigned int			event_id0;
4461ec1e82fSSascha Hauer 	unsigned int			event_id1;
4471ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
4481ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
4498391ecf4SShengjiu Wang 	unsigned int			device_to_device;
4500f06c027SRobin Gong 	unsigned int                    pc_to_pc;
4511ec1e82fSSascha Hauer 	unsigned long			flags;
4528391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
4530bbc1413SRichard Zhao 	unsigned long			event_mask[2];
4540bbc1413SRichard Zhao 	unsigned long			watermark_level;
4551ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
4561ec1e82fSSascha Hauer 	enum dma_status			status;
4570b351865SNicolin Chen 	struct imx_dma_data		data;
458b8603d2aSLucas Stach 	struct work_struct		terminate_worker;
4594e2b10beSRobin Gong 	struct list_head                terminated;
460e8fafa50SRobin Gong 	bool				is_ram_script;
461824a0a02SSascha Hauer 	unsigned int			n_fifos_src;
462824a0a02SSascha Hauer 	unsigned int			n_fifos_dst;
463e0c7ea83SShengjiu Wang 	unsigned int			stride_fifos_src;
464e0c7ea83SShengjiu Wang 	unsigned int			stride_fifos_dst;
465e0c7ea83SShengjiu Wang 	unsigned int			words_per_fifo;
466824a0a02SSascha Hauer 	bool				sw_done;
4671ec1e82fSSascha Hauer };
4681ec1e82fSSascha Hauer 
4690bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
4701ec1e82fSSascha Hauer 
4711ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
4721ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
4731ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
4741ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
4751ec1e82fSSascha Hauer 
4761ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
4771ec1e82fSSascha Hauer 
4781ec1e82fSSascha Hauer /**
4791ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
4801ec1e82fSSascha Hauer  *
48124ca312dSRobin Gong  * @magic:		"SDMA"
48224ca312dSRobin Gong  * @version_major:	increased whenever layout of struct
48324ca312dSRobin Gong  *			sdma_script_start_addrs changes.
48424ca312dSRobin Gong  * @version_minor:	firmware minor version (for binary compatible changes)
48524ca312dSRobin Gong  * @script_addrs_start:	offset of struct sdma_script_start_addrs in this image
48624ca312dSRobin Gong  * @num_script_addrs:	Number of script addresses in this image
48724ca312dSRobin Gong  * @ram_code_start:	offset of SDMA ram image in this firmware image
48824ca312dSRobin Gong  * @ram_code_size:	size of SDMA ram image
48924ca312dSRobin Gong  * @script_addrs:	Stores the start address of the SDMA scripts
4901ec1e82fSSascha Hauer  *			(in SDMA memory space)
4911ec1e82fSSascha Hauer  */
4921ec1e82fSSascha Hauer struct sdma_firmware_header {
4931ec1e82fSSascha Hauer 	u32	magic;
4941ec1e82fSSascha Hauer 	u32	version_major;
4951ec1e82fSSascha Hauer 	u32	version_minor;
4961ec1e82fSSascha Hauer 	u32	script_addrs_start;
4971ec1e82fSSascha Hauer 	u32	num_script_addrs;
4981ec1e82fSSascha Hauer 	u32	ram_code_start;
4991ec1e82fSSascha Hauer 	u32	ram_code_size;
5001ec1e82fSSascha Hauer };
5011ec1e82fSSascha Hauer 
50217bba72fSSascha Hauer struct sdma_driver_data {
50317bba72fSSascha Hauer 	int chnenbl0;
50417bba72fSSascha Hauer 	int num_events;
505dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
506941acd56SAngus Ainslie (Purism) 	bool check_ratio;
5074852e9a2SRobin Gong 	/*
5084852e9a2SRobin Gong 	 * ecspi ERR009165 fixed should be done in sdma script
5094852e9a2SRobin Gong 	 * and it has been fixed in soc from i.mx6ul.
5104852e9a2SRobin Gong 	 * please get more information from the below link:
5114852e9a2SRobin Gong 	 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
5124852e9a2SRobin Gong 	 */
5134852e9a2SRobin Gong 	bool ecspi_fixed;
51462550cd7SShawn Guo };
51562550cd7SShawn Guo 
5161ec1e82fSSascha Hauer struct sdma_engine {
5171ec1e82fSSascha Hauer 	struct device			*dev;
5181ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
5191ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
5201ec1e82fSSascha Hauer 	void __iomem			*regs;
5211ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
5221ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
5231ec1e82fSSascha Hauer 	struct dma_device		dma_device;
5247560e3f3SSascha Hauer 	struct clk			*clk_ipg;
5257560e3f3SSascha Hauer 	struct clk			*clk_ahb;
5262ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
527cd72b846SNicolin Chen 	u32				script_number;
5281ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
52917bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
5308391ecf4SShengjiu Wang 	u32				spba_start_addr;
5318391ecf4SShengjiu Wang 	u32				spba_end_addr;
5325bb9dbb5SVinod Koul 	unsigned int			irq;
53376c33d27SSascha Hauer 	dma_addr_t			bd0_phys;
53476c33d27SSascha Hauer 	struct sdma_buffer_descriptor	*bd0;
53525aaa75dSAngus Ainslie (Purism) 	/* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/
53625aaa75dSAngus Ainslie (Purism) 	bool				clk_ratio;
537e8fafa50SRobin Gong 	bool                            fw_loaded;
53817bba72fSSascha Hauer };
53917bba72fSSascha Hauer 
540107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
541107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
542107d0644SVinod Koul 		       enum dma_transfer_direction direction);
543107d0644SVinod Koul 
544e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
54517bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
54617bba72fSSascha Hauer 	.num_events = 32,
54717bba72fSSascha Hauer };
54817bba72fSSascha Hauer 
549dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
550dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
551dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
552dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
553dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
554dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
555dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
556dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
557dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
558dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
559dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
560dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
561dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
562dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
563dcfec3c0SSascha Hauer };
564dcfec3c0SSascha Hauer 
565e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
566dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
567dcfec3c0SSascha Hauer 	.num_events = 48,
568dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
569dcfec3c0SSascha Hauer };
570dcfec3c0SSascha Hauer 
571e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
57217bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
57317bba72fSSascha Hauer 	.num_events = 48,
5741ec1e82fSSascha Hauer };
5751ec1e82fSSascha Hauer 
576dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
577dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
578dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
579dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
580dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
581dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
582dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
583dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
584dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
585dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
586dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
587dcfec3c0SSascha Hauer };
588dcfec3c0SSascha Hauer 
589e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
590dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
591dcfec3c0SSascha Hauer 	.num_events = 48,
592dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
593dcfec3c0SSascha Hauer };
594dcfec3c0SSascha Hauer 
595dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
596dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
597dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
598dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
599dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
600dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
601dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
602dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
603dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
604dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
605dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
606dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
607dcfec3c0SSascha Hauer };
608dcfec3c0SSascha Hauer 
609e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
610dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
611dcfec3c0SSascha Hauer 	.num_events = 48,
612dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
613dcfec3c0SSascha Hauer };
614dcfec3c0SSascha Hauer 
615dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
616dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
617dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
618dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
619dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
620dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
621dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
622dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
623dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
624dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
625dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
626dcfec3c0SSascha Hauer };
627dcfec3c0SSascha Hauer 
628e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
629dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
630dcfec3c0SSascha Hauer 	.num_events = 48,
631dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
632dcfec3c0SSascha Hauer };
633dcfec3c0SSascha Hauer 
6344852e9a2SRobin Gong static struct sdma_driver_data sdma_imx6ul = {
6354852e9a2SRobin Gong 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
6364852e9a2SRobin Gong 	.num_events = 48,
6374852e9a2SRobin Gong 	.script_addrs = &sdma_script_imx6q,
6384852e9a2SRobin Gong 	.ecspi_fixed = true,
6394852e9a2SRobin Gong };
6404852e9a2SRobin Gong 
641b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = {
642b7d2648aSFabio Estevam 	.ap_2_ap_addr = 644,
643b7d2648aSFabio Estevam 	.uart_2_mcu_addr = 819,
644b7d2648aSFabio Estevam 	.mcu_2_app_addr = 749,
645b7d2648aSFabio Estevam 	.uartsh_2_mcu_addr = 1034,
646b7d2648aSFabio Estevam 	.mcu_2_shp_addr = 962,
647b7d2648aSFabio Estevam 	.app_2_mcu_addr = 685,
648b7d2648aSFabio Estevam 	.shp_2_mcu_addr = 893,
649b7d2648aSFabio Estevam 	.spdif_2_mcu_addr = 1102,
650b7d2648aSFabio Estevam 	.mcu_2_spdif_addr = 1136,
651b7d2648aSFabio Estevam };
652b7d2648aSFabio Estevam 
653b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = {
654b7d2648aSFabio Estevam 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
655b7d2648aSFabio Estevam 	.num_events = 48,
656b7d2648aSFabio Estevam 	.script_addrs = &sdma_script_imx7d,
657b7d2648aSFabio Estevam };
658b7d2648aSFabio Estevam 
659941acd56SAngus Ainslie (Purism) static struct sdma_driver_data sdma_imx8mq = {
660941acd56SAngus Ainslie (Purism) 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
661941acd56SAngus Ainslie (Purism) 	.num_events = 48,
662941acd56SAngus Ainslie (Purism) 	.script_addrs = &sdma_script_imx7d,
663941acd56SAngus Ainslie (Purism) 	.check_ratio = 1,
664941acd56SAngus Ainslie (Purism) };
665941acd56SAngus Ainslie (Purism) 
666580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
667dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
668dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
669dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
67017bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
671dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
67263edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
673b7d2648aSFabio Estevam 	{ .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
6744852e9a2SRobin Gong 	{ .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, },
675941acd56SAngus Ainslie (Purism) 	{ .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, },
676580975d7SShawn Guo 	{ /* sentinel */ }
677580975d7SShawn Guo };
678580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
679580975d7SShawn Guo 
6800bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
6810bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
6820bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
6831ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
6841ec1e82fSSascha Hauer 
chnenbl_ofs(struct sdma_engine * sdma,unsigned int event)6851ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
6861ec1e82fSSascha Hauer {
68717bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
6881ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
6891ec1e82fSSascha Hauer }
6901ec1e82fSSascha Hauer 
sdma_config_ownership(struct sdma_channel * sdmac,bool event_override,bool mcu_override,bool dsp_override)6911ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
6921ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
6931ec1e82fSSascha Hauer {
6941ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6951ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6960bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
6971ec1e82fSSascha Hauer 
6981ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
6991ec1e82fSSascha Hauer 		return -EINVAL;
7001ec1e82fSSascha Hauer 
701c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
702c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
703c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
7041ec1e82fSSascha Hauer 
7051ec1e82fSSascha Hauer 	if (dsp_override)
7060bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
7071ec1e82fSSascha Hauer 	else
7080bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
7091ec1e82fSSascha Hauer 
7101ec1e82fSSascha Hauer 	if (event_override)
7110bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
7121ec1e82fSSascha Hauer 	else
7130bbc1413SRichard Zhao 		__set_bit(channel, &evt);
7141ec1e82fSSascha Hauer 
7151ec1e82fSSascha Hauer 	if (mcu_override)
7160bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
7171ec1e82fSSascha Hauer 	else
7180bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
7191ec1e82fSSascha Hauer 
720c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
721c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
722c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
7231ec1e82fSSascha Hauer 
7241ec1e82fSSascha Hauer 	return 0;
7251ec1e82fSSascha Hauer }
7261ec1e82fSSascha Hauer 
is_sdma_channel_enabled(struct sdma_engine * sdma,int channel)7275b215c28STomasz Moń static int is_sdma_channel_enabled(struct sdma_engine *sdma, int channel)
7285b215c28STomasz Moń {
7295b215c28STomasz Moń 	return !!(readl(sdma->regs + SDMA_H_STATSTOP) & BIT(channel));
7305b215c28STomasz Moń }
7315b215c28STomasz Moń 
sdma_enable_channel(struct sdma_engine * sdma,int channel)732b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
733b9a59166SRichard Zhao {
7340bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
735b9a59166SRichard Zhao }
736b9a59166SRichard Zhao 
7371ec1e82fSSascha Hauer /*
7382ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
7391ec1e82fSSascha Hauer  */
sdma_run_channel0(struct sdma_engine * sdma)7402ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
7411ec1e82fSSascha Hauer {
7421ec1e82fSSascha Hauer 	int ret;
7431d069bfaSMichael Olbrich 	u32 reg;
7441ec1e82fSSascha Hauer 
7452ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
7461ec1e82fSSascha Hauer 
7471d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
7481d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
7491d069bfaSMichael Olbrich 	if (ret)
7502ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
7511ec1e82fSSascha Hauer 
752855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
75325aaa75dSAngus Ainslie (Purism) 	reg = readl(sdma->regs + SDMA_H_CONFIG);
75425aaa75dSAngus Ainslie (Purism) 	if ((reg & SDMA_H_CONFIG_CSM) == 0) {
75525aaa75dSAngus Ainslie (Purism) 		reg |= SDMA_H_CONFIG_CSM;
75625aaa75dSAngus Ainslie (Purism) 		writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG);
75725aaa75dSAngus Ainslie (Purism) 	}
758855832e4SRobin Gong 
7591d069bfaSMichael Olbrich 	return ret;
7601ec1e82fSSascha Hauer }
7611ec1e82fSSascha Hauer 
sdma_load_script(struct sdma_engine * sdma,void * buf,int size,u32 address)7621ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
7631ec1e82fSSascha Hauer 		u32 address)
7641ec1e82fSSascha Hauer {
76576c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
7661ec1e82fSSascha Hauer 	void *buf_virt;
7671ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
7681ec1e82fSSascha Hauer 	int ret;
7692ccaef05SRichard Zhao 	unsigned long flags;
77073eab978SSascha Hauer 
771ceaf5226SAndy Duan 	buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL);
772ef6c1dadSFlavio Suligoi 	if (!buf_virt)
7732ccaef05SRichard Zhao 		return -ENOMEM;
7741ec1e82fSSascha Hauer 
7752ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
7762ccaef05SRichard Zhao 
7771ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
7783f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
7791ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
7801ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
7811ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
7821ec1e82fSSascha Hauer 
7831ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
7841ec1e82fSSascha Hauer 
7852ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
7862ccaef05SRichard Zhao 
7872ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
7881ec1e82fSSascha Hauer 
789ceaf5226SAndy Duan 	dma_free_coherent(sdma->dev, size, buf_virt, buf_phys);
7901ec1e82fSSascha Hauer 
7911ec1e82fSSascha Hauer 	return ret;
7921ec1e82fSSascha Hauer }
7931ec1e82fSSascha Hauer 
sdma_event_enable(struct sdma_channel * sdmac,unsigned int event)7941ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
7951ec1e82fSSascha Hauer {
7961ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7971ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7980bbc1413SRichard Zhao 	unsigned long val;
7991ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
8001ec1e82fSSascha Hauer 
801c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
8020bbc1413SRichard Zhao 	__set_bit(channel, &val);
803c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
804824a0a02SSascha Hauer 
805824a0a02SSascha Hauer 	/* Set SDMA_DONEx_CONFIG is sw_done enabled */
806824a0a02SSascha Hauer 	if (sdmac->sw_done) {
807824a0a02SSascha Hauer 		val = readl_relaxed(sdma->regs + SDMA_DONE0_CONFIG);
808824a0a02SSascha Hauer 		val |= SDMA_DONE0_CONFIG_DONE_SEL;
809824a0a02SSascha Hauer 		val &= ~SDMA_DONE0_CONFIG_DONE_DIS;
810824a0a02SSascha Hauer 		writel_relaxed(val, sdma->regs + SDMA_DONE0_CONFIG);
811824a0a02SSascha Hauer 	}
8121ec1e82fSSascha Hauer }
8131ec1e82fSSascha Hauer 
sdma_event_disable(struct sdma_channel * sdmac,unsigned int event)8141ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
8151ec1e82fSSascha Hauer {
8161ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8171ec1e82fSSascha Hauer 	int channel = sdmac->channel;
8181ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
8190bbc1413SRichard Zhao 	unsigned long val;
8201ec1e82fSSascha Hauer 
821c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
8220bbc1413SRichard Zhao 	__clear_bit(channel, &val);
823c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
8241ec1e82fSSascha Hauer }
8251ec1e82fSSascha Hauer 
to_sdma_desc(struct dma_async_tx_descriptor * t)82657b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
82757b772b8SRobin Gong {
82857b772b8SRobin Gong 	return container_of(t, struct sdma_desc, vd.tx);
82957b772b8SRobin Gong }
83057b772b8SRobin Gong 
sdma_start_desc(struct sdma_channel * sdmac)83157b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac)
83257b772b8SRobin Gong {
83357b772b8SRobin Gong 	struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
83457b772b8SRobin Gong 	struct sdma_desc *desc;
83557b772b8SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
83657b772b8SRobin Gong 	int channel = sdmac->channel;
83757b772b8SRobin Gong 
83857b772b8SRobin Gong 	if (!vd) {
83957b772b8SRobin Gong 		sdmac->desc = NULL;
84057b772b8SRobin Gong 		return;
84157b772b8SRobin Gong 	}
84257b772b8SRobin Gong 	sdmac->desc = desc = to_sdma_desc(&vd->tx);
84302939cd1SSascha Hauer 
84457b772b8SRobin Gong 	list_del(&vd->node);
84557b772b8SRobin Gong 
84657b772b8SRobin Gong 	sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
84757b772b8SRobin Gong 	sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
84857b772b8SRobin Gong 	sdma_enable_channel(sdma, sdmac->channel);
84957b772b8SRobin Gong }
85057b772b8SRobin Gong 
sdma_update_channel_loop(struct sdma_channel * sdmac)851d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
852d1a792f3SRussell King - ARM Linux {
8531ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
8545881826dSNandor Han 	int error = 0;
8555881826dSNandor Han 	enum dma_status	old_status = sdmac->status;
8561ec1e82fSSascha Hauer 
8571ec1e82fSSascha Hauer 	/*
8581ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
8591ec1e82fSSascha Hauer 	 * call callback function.
8601ec1e82fSSascha Hauer 	 */
86157b772b8SRobin Gong 	while (sdmac->desc) {
86276c33d27SSascha Hauer 		struct sdma_desc *desc = sdmac->desc;
86376c33d27SSascha Hauer 
86476c33d27SSascha Hauer 		bd = &desc->bd[desc->buf_tail];
8651ec1e82fSSascha Hauer 
8661ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
8671ec1e82fSSascha Hauer 			break;
8681ec1e82fSSascha Hauer 
8695881826dSNandor Han 		if (bd->mode.status & BD_RROR) {
8705881826dSNandor Han 			bd->mode.status &= ~BD_RROR;
8711ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
8725881826dSNandor Han 			error = -EIO;
8735881826dSNandor Han 		}
8741ec1e82fSSascha Hauer 
8755881826dSNandor Han 	       /*
8765881826dSNandor Han 		* We use bd->mode.count to calculate the residue, since contains
8775881826dSNandor Han 		* the number of bytes present in the current buffer descriptor.
8785881826dSNandor Han 		*/
8795881826dSNandor Han 
88076c33d27SSascha Hauer 		desc->chn_real_count = bd->mode.count;
88176c33d27SSascha Hauer 		bd->mode.count = desc->period_len;
88276c33d27SSascha Hauer 		desc->buf_ptail = desc->buf_tail;
88376c33d27SSascha Hauer 		desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
88415f30f51SNandor Han 
88515f30f51SNandor Han 		/*
88615f30f51SNandor Han 		 * The callback is called from the interrupt context in order
88715f30f51SNandor Han 		 * to reduce latency and to avoid the risk of altering the
88815f30f51SNandor Han 		 * SDMA transaction status by the time the client tasklet is
88915f30f51SNandor Han 		 * executed.
89015f30f51SNandor Han 		 */
89157b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
89257b772b8SRobin Gong 		dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
89357b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
89415f30f51SNandor Han 
895177360e0STomasz Moń 		/* Assign buffer ownership to SDMA */
896177360e0STomasz Moń 		bd->mode.status |= BD_DONE;
897177360e0STomasz Moń 
8985881826dSNandor Han 		if (error)
8995881826dSNandor Han 			sdmac->status = old_status;
9001ec1e82fSSascha Hauer 	}
9015b215c28STomasz Moń 
9025b215c28STomasz Moń 	/*
9035b215c28STomasz Moń 	 * SDMA stops cyclic channel when DMA request triggers a channel and no SDMA
9045b215c28STomasz Moń 	 * owned buffer is available (i.e. BD_DONE was set too late).
9055b215c28STomasz Moń 	 */
90609f7b80fSSascha Hauer 	if (sdmac->desc && !is_sdma_channel_enabled(sdmac->sdma, sdmac->channel)) {
9075b215c28STomasz Moń 		dev_warn(sdmac->sdma->dev, "restart cyclic channel %d\n", sdmac->channel);
9085b215c28STomasz Moń 		sdma_enable_channel(sdmac->sdma, sdmac->channel);
9095b215c28STomasz Moń 	}
9101ec1e82fSSascha Hauer }
9111ec1e82fSSascha Hauer 
mxc_sdma_handle_channel_normal(struct sdma_channel * data)91257b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
9131ec1e82fSSascha Hauer {
91415f30f51SNandor Han 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
9151ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
9161ec1e82fSSascha Hauer 	int i, error = 0;
9171ec1e82fSSascha Hauer 
91876c33d27SSascha Hauer 	sdmac->desc->chn_real_count = 0;
9191ec1e82fSSascha Hauer 	/*
9201ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
9211ec1e82fSSascha Hauer 	 * errors and call callback function
9221ec1e82fSSascha Hauer 	 */
92376c33d27SSascha Hauer 	for (i = 0; i < sdmac->desc->num_bd; i++) {
92476c33d27SSascha Hauer 		bd = &sdmac->desc->bd[i];
9251ec1e82fSSascha Hauer 
9261ec1e82fSSascha Hauer 		if (bd->mode.status & (BD_DONE | BD_RROR))
9271ec1e82fSSascha Hauer 			error = -EIO;
92876c33d27SSascha Hauer 		sdmac->desc->chn_real_count += bd->mode.count;
9291ec1e82fSSascha Hauer 	}
9301ec1e82fSSascha Hauer 
9311ec1e82fSSascha Hauer 	if (error)
9321ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
9331ec1e82fSSascha Hauer 	else
934409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
9351ec1e82fSSascha Hauer }
9361ec1e82fSSascha Hauer 
sdma_int_handler(int irq,void * dev_id)9371ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
9381ec1e82fSSascha Hauer {
9391ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
9400bbc1413SRichard Zhao 	unsigned long stat;
9411ec1e82fSSascha Hauer 
942c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
943c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
9441d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
9451d069bfaSMichael Olbrich 	stat &= ~1;
9461ec1e82fSSascha Hauer 
9471ec1e82fSSascha Hauer 	while (stat) {
9481ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
9491ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
95057b772b8SRobin Gong 		struct sdma_desc *desc;
9511ec1e82fSSascha Hauer 
95257b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
95357b772b8SRobin Gong 		desc = sdmac->desc;
95457b772b8SRobin Gong 		if (desc) {
95557b772b8SRobin Gong 			if (sdmac->flags & IMX_DMA_SG_LOOP) {
956e873d432SJoy Zou 				if (sdmac->peripheral_type != IMX_DMATYPE_HDMI)
957d1a792f3SRussell King - ARM Linux 					sdma_update_channel_loop(sdmac);
958e873d432SJoy Zou 				else
959e873d432SJoy Zou 					vchan_cyclic_callback(&desc->vd);
96057b772b8SRobin Gong 			} else {
96157b772b8SRobin Gong 				mxc_sdma_handle_channel_normal(sdmac);
96257b772b8SRobin Gong 				vchan_cookie_complete(&desc->vd);
96357b772b8SRobin Gong 				sdma_start_desc(sdmac);
96457b772b8SRobin Gong 			}
96557b772b8SRobin Gong 		}
9661ec1e82fSSascha Hauer 
96757b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
9680bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
9691ec1e82fSSascha Hauer 	}
9701ec1e82fSSascha Hauer 
9711ec1e82fSSascha Hauer 	return IRQ_HANDLED;
9721ec1e82fSSascha Hauer }
9731ec1e82fSSascha Hauer 
9741ec1e82fSSascha Hauer /*
9751ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
9761ec1e82fSSascha Hauer  */
sdma_get_pc(struct sdma_channel * sdmac,enum sdma_peripheral_type peripheral_type)977625d8936SSascha Hauer static int sdma_get_pc(struct sdma_channel *sdmac,
9781ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
9791ec1e82fSSascha Hauer {
9801ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
9811ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
9821ec1e82fSSascha Hauer 	/*
9831ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
9841ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
9851ec1e82fSSascha Hauer 	 */
9860f06c027SRobin Gong 	int per_2_per = 0, emi_2_emi = 0;
9871ec1e82fSSascha Hauer 
9881ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
9891ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
9908391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
9910f06c027SRobin Gong 	sdmac->pc_to_pc = 0;
992e8fafa50SRobin Gong 	sdmac->is_ram_script = false;
9931ec1e82fSSascha Hauer 
9941ec1e82fSSascha Hauer 	switch (peripheral_type) {
9951ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
9960f06c027SRobin Gong 		emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
9971ec1e82fSSascha Hauer 		break;
9981ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
9991ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
10001ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
10011ec1e82fSSascha Hauer 		break;
10021ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
10031ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
10041ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
10051ec1e82fSSascha Hauer 		break;
10061ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
10071ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
10081ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10091ec1e82fSSascha Hauer 		break;
10101ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
10111ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
10121ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
10131ec1e82fSSascha Hauer 		break;
10141ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
10151ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
10161ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
10171ec1e82fSSascha Hauer 		break;
10181ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
1019a4965888SRobin Gong 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
10204852e9a2SRobin Gong 
10214852e9a2SRobin Gong 		/* Use rom script mcu_2_app if ERR009165 fixed */
10224852e9a2SRobin Gong 		if (sdmac->sdma->drvdata->ecspi_fixed) {
10234852e9a2SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10244852e9a2SRobin Gong 		} else {
1025a4965888SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr;
1026a4965888SRobin Gong 			sdmac->is_ram_script = true;
10274852e9a2SRobin Gong 		}
10284852e9a2SRobin Gong 
1029a4965888SRobin Gong 		break;
10301ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
10311ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
103229aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
10331ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
10341ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10351ec1e82fSSascha Hauer 		break;
10361a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
10371a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
10381a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
1039e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10401a895578SNicolin Chen 		break;
10411ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
10421ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
10431ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
10441ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
10451ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
10461ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
10471ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
10481ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
10491ec1e82fSSascha Hauer 		break;
10501ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
10511ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
10521ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
10531ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
1054e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10551ec1e82fSSascha Hauer 		break;
1056f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
1057f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1058f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1059f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
1060f892afb0SNicolin Chen 		break;
10611ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
10621ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
10631ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
10641ec1e82fSSascha Hauer 		break;
10651ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
10661ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
10671ec1e82fSSascha Hauer 		break;
10681ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
10691ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
10701ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
10711ec1e82fSSascha Hauer 		break;
10721ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
10731ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
10741ec1e82fSSascha Hauer 		break;
1075824a0a02SSascha Hauer 	case IMX_DMATYPE_MULTI_SAI:
1076824a0a02SSascha Hauer 		per_2_emi = sdma->script_addrs->sai_2_mcu_addr;
1077824a0a02SSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_sai_addr;
10781ec1e82fSSascha Hauer 		break;
1079e873d432SJoy Zou 	case IMX_DMATYPE_HDMI:
1080e873d432SJoy Zou 		emi_2_per = sdma->script_addrs->hdmi_dma_addr;
1081e873d432SJoy Zou 		sdmac->is_ram_script = true;
1082e873d432SJoy Zou 		break;
10831ec1e82fSSascha Hauer 	default:
1084625d8936SSascha Hauer 		dev_err(sdma->dev, "Unsupported transfer type %d\n",
1085625d8936SSascha Hauer 			peripheral_type);
1086625d8936SSascha Hauer 		return -EINVAL;
10871ec1e82fSSascha Hauer 	}
10881ec1e82fSSascha Hauer 
10891ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
10901ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
10918391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
10920f06c027SRobin Gong 	sdmac->pc_to_pc = emi_2_emi;
1093625d8936SSascha Hauer 
1094625d8936SSascha Hauer 	return 0;
10951ec1e82fSSascha Hauer }
10961ec1e82fSSascha Hauer 
sdma_load_context(struct sdma_channel * sdmac)10971ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
10981ec1e82fSSascha Hauer {
10991ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11001ec1e82fSSascha Hauer 	int channel = sdmac->channel;
11011ec1e82fSSascha Hauer 	int load_address;
11021ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
110376c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
11041ec1e82fSSascha Hauer 	int ret;
11052ccaef05SRichard Zhao 	unsigned long flags;
11061ec1e82fSSascha Hauer 
11078391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
11081ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
11098391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
11108391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
11110f06c027SRobin Gong 	else if (sdmac->direction == DMA_MEM_TO_MEM)
11120f06c027SRobin Gong 		load_address = sdmac->pc_to_pc;
11138391ecf4SShengjiu Wang 	else
11141ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
11151ec1e82fSSascha Hauer 
11161ec1e82fSSascha Hauer 	if (load_address < 0)
11171ec1e82fSSascha Hauer 		return load_address;
11181ec1e82fSSascha Hauer 
11191ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
11200bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
11211ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
11221ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
11230bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
11240bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
11251ec1e82fSSascha Hauer 
11262ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
112773eab978SSascha Hauer 
11281ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
11291ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
11301ec1e82fSSascha Hauer 
11311ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
11321ec1e82fSSascha Hauer 	 * and watermark level
11331ec1e82fSSascha Hauer 	 */
1134e873d432SJoy Zou 	if (sdmac->peripheral_type == IMX_DMATYPE_HDMI) {
1135e873d432SJoy Zou 		context->gReg[4] = sdmac->per_addr;
1136e873d432SJoy Zou 		context->gReg[6] = sdmac->shp_addr;
1137e873d432SJoy Zou 	} else {
11380bbc1413SRichard Zhao 		context->gReg[0] = sdmac->event_mask[1];
11390bbc1413SRichard Zhao 		context->gReg[1] = sdmac->event_mask[0];
11401ec1e82fSSascha Hauer 		context->gReg[2] = sdmac->per_addr;
11411ec1e82fSSascha Hauer 		context->gReg[6] = sdmac->shp_addr;
11421ec1e82fSSascha Hauer 		context->gReg[7] = sdmac->watermark_level;
1143e873d432SJoy Zou 	}
11441ec1e82fSSascha Hauer 
11451ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
11463f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
11471ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
11481ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
11491ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
11502ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
11511ec1e82fSSascha Hauer 
11522ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
115373eab978SSascha Hauer 
11541ec1e82fSSascha Hauer 	return ret;
11551ec1e82fSSascha Hauer }
11561ec1e82fSSascha Hauer 
to_sdma_chan(struct dma_chan * chan)11577b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
11581ec1e82fSSascha Hauer {
115957b772b8SRobin Gong 	return container_of(chan, struct sdma_channel, vc.chan);
11607b350ab0SMaxime Ripard }
11617b350ab0SMaxime Ripard 
sdma_disable_channel(struct dma_chan * chan)11627b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
11637b350ab0SMaxime Ripard {
11647b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11651ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11661ec1e82fSSascha Hauer 	int channel = sdmac->channel;
11671ec1e82fSSascha Hauer 
11680bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
11691ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
11707b350ab0SMaxime Ripard 
11717b350ab0SMaxime Ripard 	return 0;
11721ec1e82fSSascha Hauer }
sdma_channel_terminate_work(struct work_struct * work)1173b8603d2aSLucas Stach static void sdma_channel_terminate_work(struct work_struct *work)
11747f3ff14bSJiada Wang {
1175b8603d2aSLucas Stach 	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1176b8603d2aSLucas Stach 						  terminate_worker);
11777f3ff14bSJiada Wang 	/*
11787f3ff14bSJiada Wang 	 * According to NXP R&D team a delay of one BD SDMA cost time
11797f3ff14bSJiada Wang 	 * (maximum is 1ms) should be added after disable of the channel
11807f3ff14bSJiada Wang 	 * bit, to ensure SDMA core has really been stopped after SDMA
11817f3ff14bSJiada Wang 	 * clients call .device_terminate_all.
11827f3ff14bSJiada Wang 	 */
1183b8603d2aSLucas Stach 	usleep_range(1000, 2000);
1184b8603d2aSLucas Stach 
11854e2b10beSRobin Gong 	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
1186b8603d2aSLucas Stach }
1187b8603d2aSLucas Stach 
sdma_terminate_all(struct dma_chan * chan)1188a80f2787SSascha Hauer static int sdma_terminate_all(struct dma_chan *chan)
1189b8603d2aSLucas Stach {
1190b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
119102939cd1SSascha Hauer 	unsigned long flags;
119202939cd1SSascha Hauer 
119302939cd1SSascha Hauer 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1194b8603d2aSLucas Stach 
1195b8603d2aSLucas Stach 	sdma_disable_channel(chan);
1196b8603d2aSLucas Stach 
119702939cd1SSascha Hauer 	if (sdmac->desc) {
119802939cd1SSascha Hauer 		vchan_terminate_vdesc(&sdmac->desc->vd);
11994e2b10beSRobin Gong 		/*
12004e2b10beSRobin Gong 		 * move out current descriptor into terminated list so that
12014e2b10beSRobin Gong 		 * it could be free in sdma_channel_terminate_work alone
12024e2b10beSRobin Gong 		 * later without potential involving next descriptor raised
12034e2b10beSRobin Gong 		 * up before the last descriptor terminated.
12044e2b10beSRobin Gong 		 */
12054e2b10beSRobin Gong 		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
120602939cd1SSascha Hauer 		sdmac->desc = NULL;
1207b8603d2aSLucas Stach 		schedule_work(&sdmac->terminate_worker);
120802939cd1SSascha Hauer 	}
120902939cd1SSascha Hauer 
121002939cd1SSascha Hauer 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
12117f3ff14bSJiada Wang 
12127f3ff14bSJiada Wang 	return 0;
12137f3ff14bSJiada Wang }
12147f3ff14bSJiada Wang 
sdma_channel_synchronize(struct dma_chan * chan)1215b8603d2aSLucas Stach static void sdma_channel_synchronize(struct dma_chan *chan)
1216b8603d2aSLucas Stach {
1217b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1218b8603d2aSLucas Stach 
1219b8603d2aSLucas Stach 	vchan_synchronize(&sdmac->vc);
1220b8603d2aSLucas Stach 
1221b8603d2aSLucas Stach 	flush_work(&sdmac->terminate_worker);
1222b8603d2aSLucas Stach }
1223b8603d2aSLucas Stach 
sdma_set_watermarklevel_for_p2p(struct sdma_channel * sdmac)12248391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
12258391ecf4SShengjiu Wang {
12268391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
12278391ecf4SShengjiu Wang 
12288391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
12298391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
12308391ecf4SShengjiu Wang 
12318391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
12328391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
12338391ecf4SShengjiu Wang 
12348391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
12358391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
12368391ecf4SShengjiu Wang 
12378391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
12388391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
12398391ecf4SShengjiu Wang 
12408391ecf4SShengjiu Wang 	/*
12418391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
12428391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
12438391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
12448391ecf4SShengjiu Wang 	 */
12458391ecf4SShengjiu Wang 	if (lwml > hwml) {
12468391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
12478391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
12488391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
12498391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
12508391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
12518391ecf4SShengjiu Wang 	}
12528391ecf4SShengjiu Wang 
12538391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
12548391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
12558391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
12568391ecf4SShengjiu Wang 
12578391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
12588391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
12598391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
12608391ecf4SShengjiu Wang 
12618391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
12628391ecf4SShengjiu Wang }
12638391ecf4SShengjiu Wang 
sdma_set_watermarklevel_for_sais(struct sdma_channel * sdmac)1264824a0a02SSascha Hauer static void sdma_set_watermarklevel_for_sais(struct sdma_channel *sdmac)
1265824a0a02SSascha Hauer {
1266824a0a02SSascha Hauer 	unsigned int n_fifos;
1267e0c7ea83SShengjiu Wang 	unsigned int stride_fifos;
1268e0c7ea83SShengjiu Wang 	unsigned int words_per_fifo;
1269824a0a02SSascha Hauer 
1270824a0a02SSascha Hauer 	if (sdmac->sw_done)
1271824a0a02SSascha Hauer 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SW_DONE;
1272824a0a02SSascha Hauer 
1273e0c7ea83SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM) {
1274824a0a02SSascha Hauer 		n_fifos = sdmac->n_fifos_src;
1275e0c7ea83SShengjiu Wang 		stride_fifos = sdmac->stride_fifos_src;
1276e0c7ea83SShengjiu Wang 	} else {
1277824a0a02SSascha Hauer 		n_fifos = sdmac->n_fifos_dst;
1278e0c7ea83SShengjiu Wang 		stride_fifos = sdmac->stride_fifos_dst;
1279e0c7ea83SShengjiu Wang 	}
1280e0c7ea83SShengjiu Wang 
1281e0c7ea83SShengjiu Wang 	words_per_fifo = sdmac->words_per_fifo;
1282824a0a02SSascha Hauer 
1283824a0a02SSascha Hauer 	sdmac->watermark_level |=
1284824a0a02SSascha Hauer 			FIELD_PREP(SDMA_WATERMARK_LEVEL_N_FIFOS, n_fifos);
1285e0c7ea83SShengjiu Wang 	sdmac->watermark_level |=
1286e0c7ea83SShengjiu Wang 			FIELD_PREP(SDMA_WATERMARK_LEVEL_OFF_FIFOS, stride_fifos);
1287e0c7ea83SShengjiu Wang 	if (words_per_fifo)
1288e0c7ea83SShengjiu Wang 		sdmac->watermark_level |=
1289e0c7ea83SShengjiu Wang 			FIELD_PREP(SDMA_WATERMARK_LEVEL_WORDS_PER_FIFO, (words_per_fifo - 1));
1290824a0a02SSascha Hauer }
1291824a0a02SSascha Hauer 
sdma_config_channel(struct dma_chan * chan)12927b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
12931ec1e82fSSascha Hauer {
12947b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1295625d8936SSascha Hauer 	int ret;
12961ec1e82fSSascha Hauer 
12977b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
12981ec1e82fSSascha Hauer 
12990bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
13000bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
13011ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
13021ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
13031ec1e82fSSascha Hauer 
13041ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
13051ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
13061ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
13071ec1e82fSSascha Hauer 		break;
13081ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
13091ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
13101ec1e82fSSascha Hauer 		break;
13111ec1e82fSSascha Hauer 	default:
13121ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
13131ec1e82fSSascha Hauer 		break;
13141ec1e82fSSascha Hauer 	}
13151ec1e82fSSascha Hauer 
1316625d8936SSascha Hauer 	ret = sdma_get_pc(sdmac, sdmac->peripheral_type);
1317625d8936SSascha Hauer 	if (ret)
1318625d8936SSascha Hauer 		return ret;
13191ec1e82fSSascha Hauer 
13201ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
13211ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
13221ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
13231ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
13248391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
13258391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
13268391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
13271f8595efSFlavio Suligoi 		} else {
1328824a0a02SSascha Hauer 			if (sdmac->peripheral_type ==
1329824a0a02SSascha Hauer 					IMX_DMATYPE_MULTI_SAI)
1330824a0a02SSascha Hauer 				sdma_set_watermarklevel_for_sais(sdmac);
1331824a0a02SSascha Hauer 
13320bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
13331f8595efSFlavio Suligoi 		}
13348391ecf4SShengjiu Wang 
13351ec1e82fSSascha Hauer 		/* Address */
13361ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
13378391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
13381ec1e82fSSascha Hauer 	} else {
13391ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
13401ec1e82fSSascha Hauer 	}
13411ec1e82fSSascha Hauer 
1342e555a03bSRobin Gong 	return 0;
13431ec1e82fSSascha Hauer }
13441ec1e82fSSascha Hauer 
sdma_set_channel_priority(struct sdma_channel * sdmac,unsigned int priority)13451ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
13461ec1e82fSSascha Hauer 				     unsigned int priority)
13471ec1e82fSSascha Hauer {
13481ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
13491ec1e82fSSascha Hauer 	int channel = sdmac->channel;
13501ec1e82fSSascha Hauer 
13511ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
13521ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
13531ec1e82fSSascha Hauer 		return -EINVAL;
13541ec1e82fSSascha Hauer 	}
13551ec1e82fSSascha Hauer 
1356c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
13571ec1e82fSSascha Hauer 
13581ec1e82fSSascha Hauer 	return 0;
13591ec1e82fSSascha Hauer }
13601ec1e82fSSascha Hauer 
sdma_request_channel0(struct sdma_engine * sdma)136157b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma)
13621ec1e82fSSascha Hauer {
13631ec1e82fSSascha Hauer 	int ret = -EBUSY;
13641ec1e82fSSascha Hauer 
136531ef489aSLinus Torvalds 	sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys,
136657b772b8SRobin Gong 				       GFP_NOWAIT);
136757b772b8SRobin Gong 	if (!sdma->bd0) {
13681ec1e82fSSascha Hauer 		ret = -ENOMEM;
13691ec1e82fSSascha Hauer 		goto out;
13701ec1e82fSSascha Hauer 	}
13711ec1e82fSSascha Hauer 
137257b772b8SRobin Gong 	sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
137357b772b8SRobin Gong 	sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
13741ec1e82fSSascha Hauer 
137557b772b8SRobin Gong 	sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
13761ec1e82fSSascha Hauer 	return 0;
13771ec1e82fSSascha Hauer out:
13781ec1e82fSSascha Hauer 
13791ec1e82fSSascha Hauer 	return ret;
13801ec1e82fSSascha Hauer }
13811ec1e82fSSascha Hauer 
138257b772b8SRobin Gong 
sdma_alloc_bd(struct sdma_desc * desc)138357b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc)
13841ec1e82fSSascha Hauer {
1385ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
138657b772b8SRobin Gong 	int ret = 0;
13871ec1e82fSSascha Hauer 
138831ef489aSLinus Torvalds 	desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size,
1389ceaf5226SAndy Duan 				      &desc->bd_phys, GFP_NOWAIT);
139057b772b8SRobin Gong 	if (!desc->bd) {
139157b772b8SRobin Gong 		ret = -ENOMEM;
139257b772b8SRobin Gong 		goto out;
139357b772b8SRobin Gong 	}
139457b772b8SRobin Gong out:
139557b772b8SRobin Gong 	return ret;
139657b772b8SRobin Gong }
13971ec1e82fSSascha Hauer 
sdma_free_bd(struct sdma_desc * desc)139857b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc)
139957b772b8SRobin Gong {
1400ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1401ebb853b1SLucas Stach 
1402ceaf5226SAndy Duan 	dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd,
1403ceaf5226SAndy Duan 			  desc->bd_phys);
140457b772b8SRobin Gong }
14051ec1e82fSSascha Hauer 
sdma_desc_free(struct virt_dma_desc * vd)140657b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd)
140757b772b8SRobin Gong {
140857b772b8SRobin Gong 	struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
140957b772b8SRobin Gong 
141057b772b8SRobin Gong 	sdma_free_bd(desc);
141157b772b8SRobin Gong 	kfree(desc);
14121ec1e82fSSascha Hauer }
14131ec1e82fSSascha Hauer 
sdma_alloc_chan_resources(struct dma_chan * chan)14141ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
14151ec1e82fSSascha Hauer {
14161ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14171ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
14180f06c027SRobin Gong 	struct imx_dma_data mem_data;
14191ec1e82fSSascha Hauer 	int prio, ret;
14201ec1e82fSSascha Hauer 
14210f06c027SRobin Gong 	/*
14220f06c027SRobin Gong 	 * MEMCPY may never setup chan->private by filter function such as
14230f06c027SRobin Gong 	 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
14240f06c027SRobin Gong 	 * Please note in any other slave case, you have to setup chan->private
14250f06c027SRobin Gong 	 * with 'struct imx_dma_data' in your own filter function if you want to
14260f06c027SRobin Gong 	 * request dma channel by dma_request_channel() rather than
14270f06c027SRobin Gong 	 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
14280f06c027SRobin Gong 	 * to warn you to correct your filter function.
14290f06c027SRobin Gong 	 */
14300f06c027SRobin Gong 	if (!data) {
14310f06c027SRobin Gong 		dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
14320f06c027SRobin Gong 		mem_data.priority = 2;
14330f06c027SRobin Gong 		mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
14340f06c027SRobin Gong 		mem_data.dma_request = 0;
14350f06c027SRobin Gong 		mem_data.dma_request2 = 0;
14360f06c027SRobin Gong 		data = &mem_data;
14370f06c027SRobin Gong 
1438625d8936SSascha Hauer 		ret = sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1439625d8936SSascha Hauer 		if (ret)
1440625d8936SSascha Hauer 			return ret;
14410f06c027SRobin Gong 	}
14421ec1e82fSSascha Hauer 
14431ec1e82fSSascha Hauer 	switch (data->priority) {
14441ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
14451ec1e82fSSascha Hauer 		prio = 3;
14461ec1e82fSSascha Hauer 		break;
14471ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
14481ec1e82fSSascha Hauer 		prio = 2;
14491ec1e82fSSascha Hauer 		break;
14501ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
14511ec1e82fSSascha Hauer 	default:
14521ec1e82fSSascha Hauer 		prio = 1;
14531ec1e82fSSascha Hauer 		break;
14541ec1e82fSSascha Hauer 	}
14551ec1e82fSSascha Hauer 
14561ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
14571ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
14588391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1459c2c744d3SRichard Zhao 
1460b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1461b93edcddSFabio Estevam 	if (ret)
1462b93edcddSFabio Estevam 		return ret;
1463b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1464b93edcddSFabio Estevam 	if (ret)
1465b93edcddSFabio Estevam 		goto disable_clk_ipg;
1466c2c744d3SRichard Zhao 
14673bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
14681ec1e82fSSascha Hauer 	if (ret)
1469b93edcddSFabio Estevam 		goto disable_clk_ahb;
14701ec1e82fSSascha Hauer 
14711ec1e82fSSascha Hauer 	return 0;
1472b93edcddSFabio Estevam 
1473b93edcddSFabio Estevam disable_clk_ahb:
1474b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1475b93edcddSFabio Estevam disable_clk_ipg:
1476b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1477b93edcddSFabio Estevam 	return ret;
14781ec1e82fSSascha Hauer }
14791ec1e82fSSascha Hauer 
sdma_free_chan_resources(struct dma_chan * chan)14801ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
14811ec1e82fSSascha Hauer {
14821ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14831ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
14841ec1e82fSSascha Hauer 
1485a80f2787SSascha Hauer 	sdma_terminate_all(chan);
1486b8603d2aSLucas Stach 
1487b8603d2aSLucas Stach 	sdma_channel_synchronize(chan);
14881ec1e82fSSascha Hauer 
14891ec1e82fSSascha Hauer 	sdma_event_disable(sdmac, sdmac->event_id0);
14901ec1e82fSSascha Hauer 	if (sdmac->event_id1)
14911ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
14921ec1e82fSSascha Hauer 
14931ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
14941ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
14951ec1e82fSSascha Hauer 
14961ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
14971ec1e82fSSascha Hauer 
14987560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
14997560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
15001ec1e82fSSascha Hauer }
15011ec1e82fSSascha Hauer 
sdma_transfer_init(struct sdma_channel * sdmac,enum dma_transfer_direction direction,u32 bds)150221420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
150321420841SRobin Gong 				enum dma_transfer_direction direction, u32 bds)
150421420841SRobin Gong {
150521420841SRobin Gong 	struct sdma_desc *desc;
150621420841SRobin Gong 
1507e8fafa50SRobin Gong 	if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) {
1508e8fafa50SRobin Gong 		dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n");
1509e8fafa50SRobin Gong 		goto err_out;
1510e8fafa50SRobin Gong 	}
1511e8fafa50SRobin Gong 
151221420841SRobin Gong 	desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
151321420841SRobin Gong 	if (!desc)
151421420841SRobin Gong 		goto err_out;
151521420841SRobin Gong 
151621420841SRobin Gong 	sdmac->status = DMA_IN_PROGRESS;
151721420841SRobin Gong 	sdmac->direction = direction;
151821420841SRobin Gong 	sdmac->flags = 0;
151921420841SRobin Gong 
152021420841SRobin Gong 	desc->chn_count = 0;
152121420841SRobin Gong 	desc->chn_real_count = 0;
152221420841SRobin Gong 	desc->buf_tail = 0;
152321420841SRobin Gong 	desc->buf_ptail = 0;
152421420841SRobin Gong 	desc->sdmac = sdmac;
152521420841SRobin Gong 	desc->num_bd = bds;
152621420841SRobin Gong 
1527e873d432SJoy Zou 	if (bds && sdma_alloc_bd(desc))
152821420841SRobin Gong 		goto err_desc_out;
152921420841SRobin Gong 
15300f06c027SRobin Gong 	/* No slave_config called in MEMCPY case, so do here */
15310f06c027SRobin Gong 	if (direction == DMA_MEM_TO_MEM)
15320f06c027SRobin Gong 		sdma_config_ownership(sdmac, false, true, false);
15330f06c027SRobin Gong 
153421420841SRobin Gong 	if (sdma_load_context(sdmac))
15351417f59aSHui Wang 		goto err_bd_out;
153621420841SRobin Gong 
153721420841SRobin Gong 	return desc;
153821420841SRobin Gong 
15391417f59aSHui Wang err_bd_out:
15401417f59aSHui Wang 	sdma_free_bd(desc);
154121420841SRobin Gong err_desc_out:
154221420841SRobin Gong 	kfree(desc);
154321420841SRobin Gong err_out:
154421420841SRobin Gong 	return NULL;
154521420841SRobin Gong }
154621420841SRobin Gong 
sdma_prep_memcpy(struct dma_chan * chan,dma_addr_t dma_dst,dma_addr_t dma_src,size_t len,unsigned long flags)15470f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy(
15480f06c027SRobin Gong 		struct dma_chan *chan, dma_addr_t dma_dst,
15490f06c027SRobin Gong 		dma_addr_t dma_src, size_t len, unsigned long flags)
15500f06c027SRobin Gong {
15510f06c027SRobin Gong 	struct sdma_channel *sdmac = to_sdma_chan(chan);
15520f06c027SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
15530f06c027SRobin Gong 	int channel = sdmac->channel;
15540f06c027SRobin Gong 	size_t count;
15550f06c027SRobin Gong 	int i = 0, param;
15560f06c027SRobin Gong 	struct sdma_buffer_descriptor *bd;
15570f06c027SRobin Gong 	struct sdma_desc *desc;
15580f06c027SRobin Gong 
15590f06c027SRobin Gong 	if (!chan || !len)
15600f06c027SRobin Gong 		return NULL;
15610f06c027SRobin Gong 
15620f06c027SRobin Gong 	dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
15630f06c027SRobin Gong 		&dma_src, &dma_dst, len, channel);
15640f06c027SRobin Gong 
15650f06c027SRobin Gong 	desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
15660f06c027SRobin Gong 					len / SDMA_BD_MAX_CNT + 1);
15670f06c027SRobin Gong 	if (!desc)
15680f06c027SRobin Gong 		return NULL;
15690f06c027SRobin Gong 
15700f06c027SRobin Gong 	do {
15710f06c027SRobin Gong 		count = min_t(size_t, len, SDMA_BD_MAX_CNT);
15720f06c027SRobin Gong 		bd = &desc->bd[i];
15730f06c027SRobin Gong 		bd->buffer_addr = dma_src;
15740f06c027SRobin Gong 		bd->ext_buffer_addr = dma_dst;
15750f06c027SRobin Gong 		bd->mode.count = count;
15760f06c027SRobin Gong 		desc->chn_count += count;
15770f06c027SRobin Gong 		bd->mode.command = 0;
15780f06c027SRobin Gong 
15790f06c027SRobin Gong 		dma_src += count;
15800f06c027SRobin Gong 		dma_dst += count;
15810f06c027SRobin Gong 		len -= count;
15820f06c027SRobin Gong 		i++;
15830f06c027SRobin Gong 
15840f06c027SRobin Gong 		param = BD_DONE | BD_EXTD | BD_CONT;
15850f06c027SRobin Gong 		/* last bd */
15860f06c027SRobin Gong 		if (!len) {
15870f06c027SRobin Gong 			param |= BD_INTR;
15880f06c027SRobin Gong 			param |= BD_LAST;
15890f06c027SRobin Gong 			param &= ~BD_CONT;
15900f06c027SRobin Gong 		}
15910f06c027SRobin Gong 
15920f06c027SRobin Gong 		dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
15930f06c027SRobin Gong 				i, count, bd->buffer_addr,
15940f06c027SRobin Gong 				param & BD_WRAP ? "wrap" : "",
15950f06c027SRobin Gong 				param & BD_INTR ? " intr" : "");
15960f06c027SRobin Gong 
15970f06c027SRobin Gong 		bd->mode.status = param;
15980f06c027SRobin Gong 	} while (len);
15990f06c027SRobin Gong 
16000f06c027SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
16010f06c027SRobin Gong }
16020f06c027SRobin Gong 
sdma_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)16031ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
16041ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1605db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1606185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
16071ec1e82fSSascha Hauer {
16081ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16091ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1610ad78b000SVinod Koul 	int i, count;
161123889c63SSascha Hauer 	int channel = sdmac->channel;
16121ec1e82fSSascha Hauer 	struct scatterlist *sg;
161357b772b8SRobin Gong 	struct sdma_desc *desc;
16141ec1e82fSSascha Hauer 
1615107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1616107d0644SVinod Koul 
161721420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, sg_len);
161857b772b8SRobin Gong 	if (!desc)
161957b772b8SRobin Gong 		goto err_out;
162057b772b8SRobin Gong 
16211ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
16221ec1e82fSSascha Hauer 			sg_len, channel);
16231ec1e82fSSascha Hauer 
16241ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
162576c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
16261ec1e82fSSascha Hauer 		int param;
16271ec1e82fSSascha Hauer 
1628d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
16291ec1e82fSSascha Hauer 
1630fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
16311ec1e82fSSascha Hauer 
16324a6b2e8aSRobin Gong 		if (count > SDMA_BD_MAX_CNT) {
16331ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
16344a6b2e8aSRobin Gong 					channel, count, SDMA_BD_MAX_CNT);
163557b772b8SRobin Gong 			goto err_bd_out;
16361ec1e82fSSascha Hauer 		}
16371ec1e82fSSascha Hauer 
16381ec1e82fSSascha Hauer 		bd->mode.count = count;
163976c33d27SSascha Hauer 		desc->chn_count += count;
16401ec1e82fSSascha Hauer 
1641ad78b000SVinod Koul 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
164257b772b8SRobin Gong 			goto err_bd_out;
16431fa81c27SSascha Hauer 
16441fa81c27SSascha Hauer 		switch (sdmac->word_size) {
16451fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
16461ec1e82fSSascha Hauer 			bd->mode.command = 0;
16471fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
164857b772b8SRobin Gong 				goto err_bd_out;
16491fa81c27SSascha Hauer 			break;
16501fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
16511fa81c27SSascha Hauer 			bd->mode.command = 2;
16521fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
165357b772b8SRobin Gong 				goto err_bd_out;
16541fa81c27SSascha Hauer 			break;
16551fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
16561fa81c27SSascha Hauer 			bd->mode.command = 1;
16571fa81c27SSascha Hauer 			break;
16581fa81c27SSascha Hauer 		default:
165957b772b8SRobin Gong 			goto err_bd_out;
16601fa81c27SSascha Hauer 		}
16611ec1e82fSSascha Hauer 
16621ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
16631ec1e82fSSascha Hauer 
1664341b9419SShawn Guo 		if (i + 1 == sg_len) {
16651ec1e82fSSascha Hauer 			param |= BD_INTR;
1666341b9419SShawn Guo 			param |= BD_LAST;
1667341b9419SShawn Guo 			param &= ~BD_CONT;
16681ec1e82fSSascha Hauer 		}
16691ec1e82fSSascha Hauer 
1670c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1671c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
16721ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
16731ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
16741ec1e82fSSascha Hauer 
16751ec1e82fSSascha Hauer 		bd->mode.status = param;
16761ec1e82fSSascha Hauer 	}
16771ec1e82fSSascha Hauer 
167857b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
167957b772b8SRobin Gong err_bd_out:
168057b772b8SRobin Gong 	sdma_free_bd(desc);
168157b772b8SRobin Gong 	kfree(desc);
16821ec1e82fSSascha Hauer err_out:
16834b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
16841ec1e82fSSascha Hauer 	return NULL;
16851ec1e82fSSascha Hauer }
16861ec1e82fSSascha Hauer 
sdma_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t dma_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)16871ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
16881ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1689185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
169031c1e5a1SLaurent Pinchart 		unsigned long flags)
16911ec1e82fSSascha Hauer {
16921ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16931ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1694e873d432SJoy Zou 	int num_periods = 0;
169523889c63SSascha Hauer 	int channel = sdmac->channel;
169621420841SRobin Gong 	int i = 0, buf = 0;
169757b772b8SRobin Gong 	struct sdma_desc *desc;
16981ec1e82fSSascha Hauer 
16991ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
17001ec1e82fSSascha Hauer 
1701e873d432SJoy Zou 	if (sdmac->peripheral_type != IMX_DMATYPE_HDMI)
1702e873d432SJoy Zou 		num_periods = buf_len / period_len;
1703e873d432SJoy Zou 
1704107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1705107d0644SVinod Koul 
170621420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, num_periods);
170757b772b8SRobin Gong 	if (!desc)
170857b772b8SRobin Gong 		goto err_out;
170957b772b8SRobin Gong 
171076c33d27SSascha Hauer 	desc->period_len = period_len;
17118e2e27c7SRichard Zhao 
17121ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
17131ec1e82fSSascha Hauer 
17144a6b2e8aSRobin Gong 	if (period_len > SDMA_BD_MAX_CNT) {
1715ba6ab3b3SArvind Yadav 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
17164a6b2e8aSRobin Gong 				channel, period_len, SDMA_BD_MAX_CNT);
171757b772b8SRobin Gong 		goto err_bd_out;
17181ec1e82fSSascha Hauer 	}
17191ec1e82fSSascha Hauer 
1720e873d432SJoy Zou 	if (sdmac->peripheral_type == IMX_DMATYPE_HDMI)
1721e873d432SJoy Zou 		return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
1722e873d432SJoy Zou 
17231ec1e82fSSascha Hauer 	while (buf < buf_len) {
172476c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
17251ec1e82fSSascha Hauer 		int param;
17261ec1e82fSSascha Hauer 
17271ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
17281ec1e82fSSascha Hauer 
17291ec1e82fSSascha Hauer 		bd->mode.count = period_len;
17301ec1e82fSSascha Hauer 
17311ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
173257b772b8SRobin Gong 			goto err_bd_out;
17331ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
17341ec1e82fSSascha Hauer 			bd->mode.command = 0;
17351ec1e82fSSascha Hauer 		else
17361ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
17371ec1e82fSSascha Hauer 
17381ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
17391ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
17401ec1e82fSSascha Hauer 			param |= BD_WRAP;
17411ec1e82fSSascha Hauer 
1742ba6ab3b3SArvind Yadav 		dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1743c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
17441ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
17451ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
17461ec1e82fSSascha Hauer 
17471ec1e82fSSascha Hauer 		bd->mode.status = param;
17481ec1e82fSSascha Hauer 
17491ec1e82fSSascha Hauer 		dma_addr += period_len;
17501ec1e82fSSascha Hauer 		buf += period_len;
17511ec1e82fSSascha Hauer 
17521ec1e82fSSascha Hauer 		i++;
17531ec1e82fSSascha Hauer 	}
17541ec1e82fSSascha Hauer 
175557b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
175657b772b8SRobin Gong err_bd_out:
175757b772b8SRobin Gong 	sdma_free_bd(desc);
175857b772b8SRobin Gong 	kfree(desc);
17591ec1e82fSSascha Hauer err_out:
17601ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
17611ec1e82fSSascha Hauer 	return NULL;
17621ec1e82fSSascha Hauer }
17631ec1e82fSSascha Hauer 
sdma_config_write(struct dma_chan * chan,struct dma_slave_config * dmaengine_cfg,enum dma_transfer_direction direction)1764107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
1765107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
1766107d0644SVinod Koul 		       enum dma_transfer_direction direction)
17671ec1e82fSSascha Hauer {
17681ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
17691ec1e82fSSascha Hauer 
1770107d0644SVinod Koul 	if (direction == DMA_DEV_TO_MEM) {
17711ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
177294ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
177394ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
17741ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
1775107d0644SVinod Koul 	} else if (direction == DMA_DEV_TO_DEV) {
17768391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
17778391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
17788391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
17798391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
17808391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
17818391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
17828391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
1783e873d432SJoy Zou 	} else if (sdmac->peripheral_type == IMX_DMATYPE_HDMI) {
1784e873d432SJoy Zou 		sdmac->per_address = dmaengine_cfg->dst_addr;
1785e873d432SJoy Zou 		sdmac->per_address2 = dmaengine_cfg->src_addr;
1786e873d432SJoy Zou 		sdmac->watermark_level = 0;
17871ec1e82fSSascha Hauer 	} else {
17881ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
178994ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
179094ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
17911ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
17921ec1e82fSSascha Hauer 	}
1793107d0644SVinod Koul 	sdmac->direction = direction;
17947b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
17951ec1e82fSSascha Hauer }
17961ec1e82fSSascha Hauer 
sdma_config(struct dma_chan * chan,struct dma_slave_config * dmaengine_cfg)1797107d0644SVinod Koul static int sdma_config(struct dma_chan *chan,
1798107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg)
1799107d0644SVinod Koul {
1800107d0644SVinod Koul 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1801824a0a02SSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1802107d0644SVinod Koul 
1803107d0644SVinod Koul 	memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1804107d0644SVinod Koul 
1805824a0a02SSascha Hauer 	if (dmaengine_cfg->peripheral_config) {
1806824a0a02SSascha Hauer 		struct sdma_peripheral_config *sdmacfg = dmaengine_cfg->peripheral_config;
1807824a0a02SSascha Hauer 		if (dmaengine_cfg->peripheral_size != sizeof(struct sdma_peripheral_config)) {
1808824a0a02SSascha Hauer 			dev_err(sdma->dev, "Invalid peripheral size %zu, expected %zu\n",
1809824a0a02SSascha Hauer 				dmaengine_cfg->peripheral_size,
1810824a0a02SSascha Hauer 				sizeof(struct sdma_peripheral_config));
1811824a0a02SSascha Hauer 			return -EINVAL;
1812824a0a02SSascha Hauer 		}
1813824a0a02SSascha Hauer 		sdmac->n_fifos_src = sdmacfg->n_fifos_src;
1814824a0a02SSascha Hauer 		sdmac->n_fifos_dst = sdmacfg->n_fifos_dst;
1815e0c7ea83SShengjiu Wang 		sdmac->stride_fifos_src = sdmacfg->stride_fifos_src;
1816e0c7ea83SShengjiu Wang 		sdmac->stride_fifos_dst = sdmacfg->stride_fifos_dst;
1817e0c7ea83SShengjiu Wang 		sdmac->words_per_fifo = sdmacfg->words_per_fifo;
1818824a0a02SSascha Hauer 		sdmac->sw_done = sdmacfg->sw_done;
1819824a0a02SSascha Hauer 	}
1820824a0a02SSascha Hauer 
1821107d0644SVinod Koul 	/* Set ENBLn earlier to make sure dma request triggered after that */
1822107d0644SVinod Koul 	if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1823107d0644SVinod Koul 		return -EINVAL;
1824107d0644SVinod Koul 	sdma_event_enable(sdmac, sdmac->event_id0);
1825107d0644SVinod Koul 
1826107d0644SVinod Koul 	if (sdmac->event_id1) {
1827107d0644SVinod Koul 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1828107d0644SVinod Koul 			return -EINVAL;
1829107d0644SVinod Koul 		sdma_event_enable(sdmac, sdmac->event_id1);
1830107d0644SVinod Koul 	}
1831107d0644SVinod Koul 
1832107d0644SVinod Koul 	return 0;
1833107d0644SVinod Koul }
1834107d0644SVinod Koul 
sdma_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)18351ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
18361ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
18371ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
18381ec1e82fSSascha Hauer {
18391ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1840a1ff6a07SSascha Hauer 	struct sdma_desc *desc = NULL;
1841d1a792f3SRussell King - ARM Linux 	u32 residue;
184257b772b8SRobin Gong 	struct virt_dma_desc *vd;
184357b772b8SRobin Gong 	enum dma_status ret;
184457b772b8SRobin Gong 	unsigned long flags;
1845d1a792f3SRussell King - ARM Linux 
184657b772b8SRobin Gong 	ret = dma_cookie_status(chan, cookie, txstate);
184757b772b8SRobin Gong 	if (ret == DMA_COMPLETE || !txstate)
184857b772b8SRobin Gong 		return ret;
184957b772b8SRobin Gong 
185057b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1851a1ff6a07SSascha Hauer 
185257b772b8SRobin Gong 	vd = vchan_find_desc(&sdmac->vc, cookie);
1853a1ff6a07SSascha Hauer 	if (vd)
185457b772b8SRobin Gong 		desc = to_sdma_desc(&vd->tx);
1855a1ff6a07SSascha Hauer 	else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie)
1856a1ff6a07SSascha Hauer 		desc = sdmac->desc;
1857a1ff6a07SSascha Hauer 
1858a1ff6a07SSascha Hauer 	if (desc) {
1859d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
186076c33d27SSascha Hauer 			residue = (desc->num_bd - desc->buf_ptail) *
186176c33d27SSascha Hauer 				desc->period_len - desc->chn_real_count;
1862d1a792f3SRussell King - ARM Linux 		else
186376c33d27SSascha Hauer 			residue = desc->chn_count - desc->chn_real_count;
186457b772b8SRobin Gong 	} else {
186557b772b8SRobin Gong 		residue = 0;
186657b772b8SRobin Gong 	}
1867a1ff6a07SSascha Hauer 
186857b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
18691ec1e82fSSascha Hauer 
1870e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1871d1a792f3SRussell King - ARM Linux 			 residue);
18721ec1e82fSSascha Hauer 
18738a965911SShawn Guo 	return sdmac->status;
18741ec1e82fSSascha Hauer }
18751ec1e82fSSascha Hauer 
sdma_issue_pending(struct dma_chan * chan)18761ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
18771ec1e82fSSascha Hauer {
18782b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
187957b772b8SRobin Gong 	unsigned long flags;
18802b4f130eSSascha Hauer 
188157b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
188257b772b8SRobin Gong 	if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
188357b772b8SRobin Gong 		sdma_start_desc(sdmac);
188457b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
18851ec1e82fSSascha Hauer }
18861ec1e82fSSascha Hauer 
18875b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1888cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1889b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	45
1890b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4	46
18915b28aa31SSascha Hauer 
sdma_add_scripts(struct sdma_engine * sdma,const struct sdma_script_start_addrs * addr)18925b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
18935b28aa31SSascha Hauer 			     const struct sdma_script_start_addrs *addr)
18945b28aa31SSascha Hauer {
18955b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
18965b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
18975b28aa31SSascha Hauer 	int i;
18985b28aa31SSascha Hauer 
189970dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
190070dabaedSNicolin Chen 	if (!sdma->script_number)
190170dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
190270dabaedSNicolin Chen 
1903bd73dfabSRobin Gong 	if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1904bd73dfabSRobin Gong 				  / sizeof(s32)) {
1905bd73dfabSRobin Gong 		dev_err(sdma->dev,
1906bd73dfabSRobin Gong 			"SDMA script number %d not match with firmware.\n",
1907bd73dfabSRobin Gong 			sdma->script_number);
1908bd73dfabSRobin Gong 		return;
1909bd73dfabSRobin Gong 	}
1910bd73dfabSRobin Gong 
1911cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
19125b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
19135b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
1914b98ce2f4SRobin Gong 
1915b98ce2f4SRobin Gong 	/*
1916a3ae97f4SKevin Groeneveld 	 * For compatibility with NXP internal legacy kernel before 4.19 which
1917a3ae97f4SKevin Groeneveld 	 * is based on uart ram script and mainline kernel based on uart rom
1918a3ae97f4SKevin Groeneveld 	 * script, both uart ram/rom scripts are present in newer sdma
1919a3ae97f4SKevin Groeneveld 	 * firmware. Use the rom versions if they are present (V3 or newer).
1920b98ce2f4SRobin Gong 	 */
1921a3ae97f4SKevin Groeneveld 	if (sdma->script_number >= SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3) {
1922a3ae97f4SKevin Groeneveld 		if (addr->uart_2_mcu_rom_addr)
1923a3ae97f4SKevin Groeneveld 			sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_rom_addr;
1924a3ae97f4SKevin Groeneveld 		if (addr->uartsh_2_mcu_rom_addr)
1925a3ae97f4SKevin Groeneveld 			sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_rom_addr;
1926a3ae97f4SKevin Groeneveld 	}
19275b28aa31SSascha Hauer }
19285b28aa31SSascha Hauer 
sdma_load_firmware(const struct firmware * fw,void * context)19297b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
19305b28aa31SSascha Hauer {
19317b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
19325b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
19335b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
19345b28aa31SSascha Hauer 	unsigned short *ram_code;
19355b28aa31SSascha Hauer 
19367b4b88e0SSascha Hauer 	if (!fw) {
19370f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
19380f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
19397b4b88e0SSascha Hauer 		return;
19407b4b88e0SSascha Hauer 	}
19415b28aa31SSascha Hauer 
19425b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
19435b28aa31SSascha Hauer 		goto err_firmware;
19445b28aa31SSascha Hauer 
19455b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
19465b28aa31SSascha Hauer 
19475b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
19485b28aa31SSascha Hauer 		goto err_firmware;
19495b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
19505b28aa31SSascha Hauer 		goto err_firmware;
1951cd72b846SNicolin Chen 	switch (header->version_major) {
1952cd72b846SNicolin Chen 	case 1:
1953cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1954cd72b846SNicolin Chen 		break;
1955cd72b846SNicolin Chen 	case 2:
1956cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1957cd72b846SNicolin Chen 		break;
1958a572460bSFabio Estevam 	case 3:
1959a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1960a572460bSFabio Estevam 		break;
1961b7d2648aSFabio Estevam 	case 4:
1962b7d2648aSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1963b7d2648aSFabio Estevam 		break;
1964cd72b846SNicolin Chen 	default:
1965cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1966cd72b846SNicolin Chen 		goto err_firmware;
1967cd72b846SNicolin Chen 	}
19685b28aa31SSascha Hauer 
19695b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
19705b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
19715b28aa31SSascha Hauer 
19727560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
19737560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
19745b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
19755b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
19765b28aa31SSascha Hauer 			 header->ram_code_size,
19776866fd3bSSascha Hauer 			 addr->ram_code_start_addr);
19787560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
19797560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
19805b28aa31SSascha Hauer 
19815b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
19825b28aa31SSascha Hauer 
1983e8fafa50SRobin Gong 	sdma->fw_loaded = true;
1984e8fafa50SRobin Gong 
19855b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
19865b28aa31SSascha Hauer 		 header->version_major,
19875b28aa31SSascha Hauer 		 header->version_minor);
19885b28aa31SSascha Hauer 
19895b28aa31SSascha Hauer err_firmware:
19905b28aa31SSascha Hauer 	release_firmware(fw);
19917b4b88e0SSascha Hauer }
19927b4b88e0SSascha Hauer 
1993d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1994d078cd1bSZidan Wang 
sdma_event_remap(struct sdma_engine * sdma)199529f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1996d078cd1bSZidan Wang {
1997d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1998d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1999d078cd1bSZidan Wang 	struct property *event_remap;
2000d078cd1bSZidan Wang 	struct regmap *gpr;
2001d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
2002d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
2003d078cd1bSZidan Wang 	int ret = 0;
2004d078cd1bSZidan Wang 
20057104b9cbSMiaoqian Lin 	if (IS_ERR(np) || !gpr_np)
2006d078cd1bSZidan Wang 		goto out;
2007d078cd1bSZidan Wang 
2008d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
2009d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
2010d078cd1bSZidan Wang 	if (!num_map) {
2011ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
2012d078cd1bSZidan Wang 		goto out;
2013d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
2014d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
2015d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
2016d078cd1bSZidan Wang 		ret = -EINVAL;
2017d078cd1bSZidan Wang 		goto out;
2018d078cd1bSZidan Wang 	}
2019d078cd1bSZidan Wang 
2020d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
2021d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
2022d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
2023d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
2024d078cd1bSZidan Wang 		goto out;
2025d078cd1bSZidan Wang 	}
2026d078cd1bSZidan Wang 
2027d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
2028d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
2029d078cd1bSZidan Wang 		if (ret) {
2030d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
2031d078cd1bSZidan Wang 					propname, i);
2032d078cd1bSZidan Wang 			goto out;
2033d078cd1bSZidan Wang 		}
2034d078cd1bSZidan Wang 
2035d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
2036d078cd1bSZidan Wang 		if (ret) {
2037d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
2038d078cd1bSZidan Wang 					propname, i + 1);
2039d078cd1bSZidan Wang 			goto out;
2040d078cd1bSZidan Wang 		}
2041d078cd1bSZidan Wang 
2042d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
2043d078cd1bSZidan Wang 		if (ret) {
2044d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
2045d078cd1bSZidan Wang 					propname, i + 2);
2046d078cd1bSZidan Wang 			goto out;
2047d078cd1bSZidan Wang 		}
2048d078cd1bSZidan Wang 
2049d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
2050d078cd1bSZidan Wang 	}
2051d078cd1bSZidan Wang 
2052d078cd1bSZidan Wang out:
20537104b9cbSMiaoqian Lin 	if (gpr_np)
2054d078cd1bSZidan Wang 		of_node_put(gpr_np);
2055d078cd1bSZidan Wang 
2056d078cd1bSZidan Wang 	return ret;
2057d078cd1bSZidan Wang }
2058d078cd1bSZidan Wang 
sdma_get_firmware(struct sdma_engine * sdma,const char * fw_name)2059fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
20607b4b88e0SSascha Hauer 		const char *fw_name)
20617b4b88e0SSascha Hauer {
20627b4b88e0SSascha Hauer 	int ret;
20637b4b88e0SSascha Hauer 
20647b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
20650733d839SShawn Guo 			FW_ACTION_UEVENT, fw_name, sdma->dev,
20667b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
20675b28aa31SSascha Hauer 
20685b28aa31SSascha Hauer 	return ret;
20695b28aa31SSascha Hauer }
20705b28aa31SSascha Hauer 
sdma_init(struct sdma_engine * sdma)207119bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
20721ec1e82fSSascha Hauer {
20731ec1e82fSSascha Hauer 	int i, ret;
20741ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
20751ec1e82fSSascha Hauer 
2076b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
2077b93edcddSFabio Estevam 	if (ret)
2078b93edcddSFabio Estevam 		return ret;
2079b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
2080b93edcddSFabio Estevam 	if (ret)
2081b93edcddSFabio Estevam 		goto disable_clk_ipg;
20821ec1e82fSSascha Hauer 
2083941acd56SAngus Ainslie (Purism) 	if (sdma->drvdata->check_ratio &&
2084941acd56SAngus Ainslie (Purism) 	    (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg)))
208525aaa75dSAngus Ainslie (Purism) 		sdma->clk_ratio = 1;
208625aaa75dSAngus Ainslie (Purism) 
20871ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
2088c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
20891ec1e82fSSascha Hauer 
2090ceaf5226SAndy Duan 	sdma->channel_control = dma_alloc_coherent(sdma->dev,
20911ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) +
20921ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
20931ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
20941ec1e82fSSascha Hauer 
20951ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
20961ec1e82fSSascha Hauer 		ret = -ENOMEM;
20971ec1e82fSSascha Hauer 		goto err_dma_alloc;
20981ec1e82fSSascha Hauer 	}
20991ec1e82fSSascha Hauer 
21001ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
21011ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
21021ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
21031ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
21041ec1e82fSSascha Hauer 
21051ec1e82fSSascha Hauer 	/* disable all channels */
210617bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
2107c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
21081ec1e82fSSascha Hauer 
21091ec1e82fSSascha Hauer 	/* All channels have priority 0 */
21101ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
2111c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
21121ec1e82fSSascha Hauer 
211357b772b8SRobin Gong 	ret = sdma_request_channel0(sdma);
21141ec1e82fSSascha Hauer 	if (ret)
21151ec1e82fSSascha Hauer 		goto err_dma_alloc;
21161ec1e82fSSascha Hauer 
21171ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
21181ec1e82fSSascha Hauer 
21191ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
2120c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
21211ec1e82fSSascha Hauer 
21221ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
212325aaa75dSAngus Ainslie (Purism) 	if (sdma->clk_ratio)
212425aaa75dSAngus Ainslie (Purism) 		writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG);
212525aaa75dSAngus Ainslie (Purism) 	else
2126c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
21271ec1e82fSSascha Hauer 
2128c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
21291ec1e82fSSascha Hauer 
21301ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
21311ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
21321ec1e82fSSascha Hauer 
21337560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
21347560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
21351ec1e82fSSascha Hauer 
21361ec1e82fSSascha Hauer 	return 0;
21371ec1e82fSSascha Hauer 
21381ec1e82fSSascha Hauer err_dma_alloc:
21397560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
2140b93edcddSFabio Estevam disable_clk_ipg:
2141b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
21421ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
21431ec1e82fSSascha Hauer 	return ret;
21441ec1e82fSSascha Hauer }
21451ec1e82fSSascha Hauer 
sdma_filter_fn(struct dma_chan * chan,void * fn_param)21469479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
21479479e17cSShawn Guo {
21480b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
21499479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
21509479e17cSShawn Guo 
21519479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
21529479e17cSShawn Guo 		return false;
21539479e17cSShawn Guo 
21540b351865SNicolin Chen 	sdmac->data = *data;
21550b351865SNicolin Chen 	chan->private = &sdmac->data;
21569479e17cSShawn Guo 
21579479e17cSShawn Guo 	return true;
21589479e17cSShawn Guo }
21599479e17cSShawn Guo 
sdma_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)21609479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
21619479e17cSShawn Guo 				   struct of_dma *ofdma)
21629479e17cSShawn Guo {
21639479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
21649479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
21659479e17cSShawn Guo 	struct imx_dma_data data;
21669479e17cSShawn Guo 
21679479e17cSShawn Guo 	if (dma_spec->args_count != 3)
21689479e17cSShawn Guo 		return NULL;
21699479e17cSShawn Guo 
21709479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
21719479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
21729479e17cSShawn Guo 	data.priority = dma_spec->args[2];
21738391ecf4SShengjiu Wang 	/*
21748391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
21758391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
21768391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
21778391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
21788391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
21798391ecf4SShengjiu Wang 	 */
21808391ecf4SShengjiu Wang 	data.dma_request2 = 0;
21819479e17cSShawn Guo 
2182990c0b53SBaolin Wang 	return __dma_request_channel(&mask, sdma_filter_fn, &data,
2183990c0b53SBaolin Wang 				     ofdma->of_node);
21849479e17cSShawn Guo }
21859479e17cSShawn Guo 
sdma_probe(struct platform_device * pdev)2186e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
21871ec1e82fSSascha Hauer {
2188580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
21898391ecf4SShengjiu Wang 	struct device_node *spba_bus;
2190580975d7SShawn Guo 	const char *fw_name;
21911ec1e82fSSascha Hauer 	int ret;
21921ec1e82fSSascha Hauer 	int irq;
21938391ecf4SShengjiu Wang 	struct resource spba_res;
21941ec1e82fSSascha Hauer 	int i;
21951ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
219636e2f21aSSascha Hauer 	s32 *saddr_arr;
21971ec1e82fSSascha Hauer 
219842536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
219942536b9fSPhilippe Retornaz 	if (ret)
220042536b9fSPhilippe Retornaz 		return ret;
220142536b9fSPhilippe Retornaz 
22027f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
22031ec1e82fSSascha Hauer 	if (!sdma)
22041ec1e82fSSascha Hauer 		return -ENOMEM;
22051ec1e82fSSascha Hauer 
22062ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
220773eab978SSascha Hauer 
22081ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
220932996419SFabio Estevam 	sdma->drvdata = of_device_get_match_data(sdma->dev);
22101ec1e82fSSascha Hauer 
22111ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
22127f24e0eeSFabio Estevam 	if (irq < 0)
221363c72e02SFabio Estevam 		return irq;
22141ec1e82fSSascha Hauer 
22154b23603aSTudor Ambarus 	sdma->regs = devm_platform_ioremap_resource(pdev, 0);
22167f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
22177f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
22181ec1e82fSSascha Hauer 
22197560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
22207f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
22217f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
22221ec1e82fSSascha Hauer 
22237560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
22247f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
22257f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
22267560e3f3SSascha Hauer 
2227fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ipg);
2228fb9caf37SArvind Yadav 	if (ret)
2229fb9caf37SArvind Yadav 		return ret;
2230fb9caf37SArvind Yadav 
2231fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ahb);
2232fb9caf37SArvind Yadav 	if (ret)
2233fb9caf37SArvind Yadav 		goto err_clk;
22347560e3f3SSascha Hauer 
22350951a90eSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0,
22360951a90eSFabio Estevam 				dev_name(&pdev->dev), sdma);
22371ec1e82fSSascha Hauer 	if (ret)
2238fb9caf37SArvind Yadav 		goto err_irq;
22391ec1e82fSSascha Hauer 
22405bb9dbb5SVinod Koul 	sdma->irq = irq;
22415bb9dbb5SVinod Koul 
22425b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2243fb9caf37SArvind Yadav 	if (!sdma->script_addrs) {
2244fb9caf37SArvind Yadav 		ret = -ENOMEM;
2245fb9caf37SArvind Yadav 		goto err_irq;
2246fb9caf37SArvind Yadav 	}
22471ec1e82fSSascha Hauer 
224836e2f21aSSascha Hauer 	/* initially no scripts available */
224936e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
2250be4cf718SSascha Hauer 	for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++)
225136e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
225236e2f21aSSascha Hauer 
22537214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
22547214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
22550f06c027SRobin Gong 	dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
2256*8d1b7bd5SKai-Heng Feng 	dma_cap_set(DMA_PRIVATE, sdma->dma_device.cap_mask);
22577214a8b1SSascha Hauer 
22581ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
22591ec1e82fSSascha Hauer 	/* Initialize channel parameters */
22601ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
22611ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
22621ec1e82fSSascha Hauer 
22631ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
22641ec1e82fSSascha Hauer 
22651ec1e82fSSascha Hauer 		sdmac->channel = i;
226657b772b8SRobin Gong 		sdmac->vc.desc_free = sdma_desc_free;
22674e2b10beSRobin Gong 		INIT_LIST_HEAD(&sdmac->terminated);
2268b8603d2aSLucas Stach 		INIT_WORK(&sdmac->terminate_worker,
2269b8603d2aSLucas Stach 				sdma_channel_terminate_work);
227023889c63SSascha Hauer 		/*
227123889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
227223889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
227323889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
227423889c63SSascha Hauer 		 */
227523889c63SSascha Hauer 		if (i)
227657b772b8SRobin Gong 			vchan_init(&sdmac->vc, &sdma->dma_device);
22771ec1e82fSSascha Hauer 	}
22781ec1e82fSSascha Hauer 
22795b28aa31SSascha Hauer 	ret = sdma_init(sdma);
22801ec1e82fSSascha Hauer 	if (ret)
22811ec1e82fSSascha Hauer 		goto err_init;
22821ec1e82fSSascha Hauer 
2283d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
2284d078cd1bSZidan Wang 	if (ret)
2285d078cd1bSZidan Wang 		goto err_init;
2286d078cd1bSZidan Wang 
2287dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
2288dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
22895b28aa31SSascha Hauer 
22901ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
22911ec1e82fSSascha Hauer 
22921ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
22931ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
22941ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
22951ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
22961ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
22977b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
2298a80f2787SSascha Hauer 	sdma->dma_device.device_terminate_all = sdma_terminate_all;
2299b8603d2aSLucas Stach 	sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2300f9d4a398SNicolin Chen 	sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2301f9d4a398SNicolin Chen 	sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2302f9d4a398SNicolin Chen 	sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
23036f3125ceSLucas Stach 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
23040f06c027SRobin Gong 	sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
23051ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
2306a3711d49SAngus Ainslie (Purism) 	sdma->dma_device.copy_align = 2;
23074a6b2e8aSRobin Gong 	dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
23081ec1e82fSSascha Hauer 
230923e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
231023e11811SVignesh Raman 
23111ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
23121ec1e82fSSascha Hauer 	if (ret) {
23131ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
23141ec1e82fSSascha Hauer 		goto err_init;
23151ec1e82fSSascha Hauer 	}
23161ec1e82fSSascha Hauer 
23179479e17cSShawn Guo 	if (np) {
23189479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
23199479e17cSShawn Guo 		if (ret) {
23209479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
23219479e17cSShawn Guo 			goto err_register;
23229479e17cSShawn Guo 		}
23238391ecf4SShengjiu Wang 
23248391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
23258391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
23268391ecf4SShengjiu Wang 		if (!ret) {
23278391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
23288391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
23298391ecf4SShengjiu Wang 		}
23308391ecf4SShengjiu Wang 		of_node_put(spba_bus);
23319479e17cSShawn Guo 	}
23329479e17cSShawn Guo 
23332b8066c3SSven Van Asbroeck 	/*
23342b8066c3SSven Van Asbroeck 	 * Because that device tree does not encode ROM script address,
23352b8066c3SSven Van Asbroeck 	 * the RAM script in firmware is mandatory for device tree
23362b8066c3SSven Van Asbroeck 	 * probe, otherwise it fails.
23372b8066c3SSven Van Asbroeck 	 */
23382b8066c3SSven Van Asbroeck 	ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
23392b8066c3SSven Van Asbroeck 				      &fw_name);
23402b8066c3SSven Van Asbroeck 	if (ret) {
23412b8066c3SSven Van Asbroeck 		dev_warn(&pdev->dev, "failed to get firmware name\n");
23422b8066c3SSven Van Asbroeck 	} else {
23432b8066c3SSven Van Asbroeck 		ret = sdma_get_firmware(sdma, fw_name);
23442b8066c3SSven Van Asbroeck 		if (ret)
23452b8066c3SSven Van Asbroeck 			dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
23462b8066c3SSven Van Asbroeck 	}
23472b8066c3SSven Van Asbroeck 
23481ec1e82fSSascha Hauer 	return 0;
23491ec1e82fSSascha Hauer 
23509479e17cSShawn Guo err_register:
23519479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
23521ec1e82fSSascha Hauer err_init:
23531ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
2354fb9caf37SArvind Yadav err_irq:
2355fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2356fb9caf37SArvind Yadav err_clk:
2357fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2358939fd4f0SShawn Guo 	return ret;
23591ec1e82fSSascha Hauer }
23601ec1e82fSSascha Hauer 
sdma_remove(struct platform_device * pdev)23611d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
23621ec1e82fSSascha Hauer {
236323e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
2364c12fe497SVignesh Raman 	int i;
236523e11811SVignesh Raman 
23665bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
236723e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
236823e11811SVignesh Raman 	kfree(sdma->script_addrs);
2369fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2370fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2371c12fe497SVignesh Raman 	/* Kill the tasklet */
2372c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2373c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
2374c12fe497SVignesh Raman 
237557b772b8SRobin Gong 		tasklet_kill(&sdmac->vc.task);
237657b772b8SRobin Gong 		sdma_free_chan_resources(&sdmac->vc.chan);
2377c12fe497SVignesh Raman 	}
237823e11811SVignesh Raman 
237923e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
238023e11811SVignesh Raman 	return 0;
23811ec1e82fSSascha Hauer }
23821ec1e82fSSascha Hauer 
23831ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
23841ec1e82fSSascha Hauer 	.driver		= {
23851ec1e82fSSascha Hauer 		.name	= "imx-sdma",
2386580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
23871ec1e82fSSascha Hauer 	},
23881d1bbd30SMaxin B. John 	.remove		= sdma_remove,
238923e11811SVignesh Raman 	.probe		= sdma_probe,
23901ec1e82fSSascha Hauer };
23911ec1e82fSSascha Hauer 
239223e11811SVignesh Raman module_platform_driver(sdma_driver);
23931ec1e82fSSascha Hauer 
23941ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
23951ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
2396c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2397c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2398c0879342SNicolas Chauvet #endif
2399a7cd3cf0SPeter Robinson #if IS_ENABLED(CONFIG_SOC_IMX7D) || IS_ENABLED(CONFIG_SOC_IMX8M)
2400c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2401c0879342SNicolas Chauvet #endif
24021ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
2403