xref: /linux/drivers/dma/imx-sdma.c (revision 824a0a02cd74776461aaa30d792b1ed9111c9aa5)
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>
17*824a0a02SSascha 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>
34580975d7SShawn Guo #include <linux/of_device.h>
359479e17cSShawn Guo #include <linux/of_dma.h>
36b8603d2aSLucas Stach #include <linux/workqueue.h>
371ec1e82fSSascha Hauer 
381ec1e82fSSascha Hauer #include <asm/irq.h>
39c6547c2eSSascha Hauer #include <linux/dma/imx-dma.h>
40d078cd1bSZidan Wang #include <linux/regmap.h>
41d078cd1bSZidan Wang #include <linux/mfd/syscon.h>
42d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
431ec1e82fSSascha Hauer 
44d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
4557b772b8SRobin Gong #include "virt-dma.h"
46d2ebfb33SRussell King - ARM Linux 
471ec1e82fSSascha Hauer /* SDMA registers */
481ec1e82fSSascha Hauer #define SDMA_H_C0PTR		0x000
491ec1e82fSSascha Hauer #define SDMA_H_INTR		0x004
501ec1e82fSSascha Hauer #define SDMA_H_STATSTOP		0x008
511ec1e82fSSascha Hauer #define SDMA_H_START		0x00c
521ec1e82fSSascha Hauer #define SDMA_H_EVTOVR		0x010
531ec1e82fSSascha Hauer #define SDMA_H_DSPOVR		0x014
541ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR		0x018
551ec1e82fSSascha Hauer #define SDMA_H_EVTPEND		0x01c
561ec1e82fSSascha Hauer #define SDMA_H_DSPENBL		0x020
571ec1e82fSSascha Hauer #define SDMA_H_RESET		0x024
581ec1e82fSSascha Hauer #define SDMA_H_EVTERR		0x028
591ec1e82fSSascha Hauer #define SDMA_H_INTRMSK		0x02c
601ec1e82fSSascha Hauer #define SDMA_H_PSW		0x030
611ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG	0x034
621ec1e82fSSascha Hauer #define SDMA_H_CONFIG		0x038
631ec1e82fSSascha Hauer #define SDMA_ONCE_ENB		0x040
641ec1e82fSSascha Hauer #define SDMA_ONCE_DATA		0x044
651ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR		0x048
661ec1e82fSSascha Hauer #define SDMA_ONCE_STAT		0x04c
671ec1e82fSSascha Hauer #define SDMA_ONCE_CMD		0x050
681ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR		0x054
691ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR	0x058
701ec1e82fSSascha Hauer #define SDMA_CHN0ADDR		0x05c
711ec1e82fSSascha Hauer #define SDMA_ONCE_RTB		0x060
721ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1	0x070
731ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2	0x074
7462550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35	0x200
7562550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31	0x080
761ec1e82fSSascha Hauer #define SDMA_CHNPRI_0		0x100
77*824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG	0x1000
781ec1e82fSSascha Hauer 
791ec1e82fSSascha Hauer /*
801ec1e82fSSascha Hauer  * Buffer descriptor status values.
811ec1e82fSSascha Hauer  */
821ec1e82fSSascha Hauer #define BD_DONE  0x01
831ec1e82fSSascha Hauer #define BD_WRAP  0x02
841ec1e82fSSascha Hauer #define BD_CONT  0x04
851ec1e82fSSascha Hauer #define BD_INTR  0x08
861ec1e82fSSascha Hauer #define BD_RROR  0x10
871ec1e82fSSascha Hauer #define BD_LAST  0x20
881ec1e82fSSascha Hauer #define BD_EXTD  0x80
891ec1e82fSSascha Hauer 
901ec1e82fSSascha Hauer /*
911ec1e82fSSascha Hauer  * Data Node descriptor status values.
921ec1e82fSSascha Hauer  */
931ec1e82fSSascha Hauer #define DND_END_OF_FRAME  0x80
941ec1e82fSSascha Hauer #define DND_END_OF_XFER   0x40
951ec1e82fSSascha Hauer #define DND_DONE          0x20
961ec1e82fSSascha Hauer #define DND_UNUSED        0x01
971ec1e82fSSascha Hauer 
981ec1e82fSSascha Hauer /*
991ec1e82fSSascha Hauer  * IPCV2 descriptor status values.
1001ec1e82fSSascha Hauer  */
1011ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME  0x40
1021ec1e82fSSascha Hauer 
1031ec1e82fSSascha Hauer #define IPCV2_MAX_NODES        50
1041ec1e82fSSascha Hauer /*
1051ec1e82fSSascha Hauer  * Error bit set in the CCB status field by the SDMA,
1061ec1e82fSSascha Hauer  * in setbd routine, in case of a transfer error
1071ec1e82fSSascha Hauer  */
1081ec1e82fSSascha Hauer #define DATA_ERROR  0x10000000
1091ec1e82fSSascha Hauer 
1101ec1e82fSSascha Hauer /*
1111ec1e82fSSascha Hauer  * Buffer descriptor commands.
1121ec1e82fSSascha Hauer  */
1131ec1e82fSSascha Hauer #define C0_ADDR             0x01
1141ec1e82fSSascha Hauer #define C0_LOAD             0x02
1151ec1e82fSSascha Hauer #define C0_DUMP             0x03
1161ec1e82fSSascha Hauer #define C0_SETCTX           0x07
1171ec1e82fSSascha Hauer #define C0_GETCTX           0x03
1181ec1e82fSSascha Hauer #define C0_SETDM            0x01
1191ec1e82fSSascha Hauer #define C0_SETPM            0x04
1201ec1e82fSSascha Hauer #define C0_GETDM            0x02
1211ec1e82fSSascha Hauer #define C0_GETPM            0x08
1221ec1e82fSSascha Hauer /*
1231ec1e82fSSascha Hauer  * Change endianness indicator in the BD command field
1241ec1e82fSSascha Hauer  */
1251ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS   0x80
1261ec1e82fSSascha Hauer 
1271ec1e82fSSascha Hauer /*
1288391ecf4SShengjiu Wang  *  p_2_p watermark_level description
1298391ecf4SShengjiu Wang  *	Bits		Name			Description
1308391ecf4SShengjiu Wang  *	0-7		Lower WML		Lower watermark level
1318391ecf4SShengjiu Wang  *	8		PS			1: Pad Swallowing
1328391ecf4SShengjiu Wang  *						0: No Pad Swallowing
1338391ecf4SShengjiu Wang  *	9		PA			1: Pad Adding
1348391ecf4SShengjiu Wang  *						0: No Pad Adding
1358391ecf4SShengjiu Wang  *	10		SPDIF			If this bit is set both source
1368391ecf4SShengjiu Wang  *						and destination are on SPBA
1378391ecf4SShengjiu Wang  *	11		Source Bit(SP)		1: Source on SPBA
1388391ecf4SShengjiu Wang  *						0: Source on AIPS
1398391ecf4SShengjiu Wang  *	12		Destination Bit(DP)	1: Destination on SPBA
1408391ecf4SShengjiu Wang  *						0: Destination on AIPS
1418391ecf4SShengjiu Wang  *	13-15		---------		MUST BE 0
1428391ecf4SShengjiu Wang  *	16-23		Higher WML		HWML
1438391ecf4SShengjiu Wang  *	24-27		N			Total number of samples after
1448391ecf4SShengjiu Wang  *						which Pad adding/Swallowing
1458391ecf4SShengjiu Wang  *						must be done. It must be odd.
1468391ecf4SShengjiu Wang  *	28		Lower WML Event(LWE)	SDMA events reg to check for
1478391ecf4SShengjiu Wang  *						LWML event mask
1488391ecf4SShengjiu Wang  *						0: LWE in EVENTS register
1498391ecf4SShengjiu Wang  *						1: LWE in EVENTS2 register
1508391ecf4SShengjiu Wang  *	29		Higher WML Event(HWE)	SDMA events reg to check for
1518391ecf4SShengjiu Wang  *						HWML event mask
1528391ecf4SShengjiu Wang  *						0: HWE in EVENTS register
1538391ecf4SShengjiu Wang  *						1: HWE in EVENTS2 register
1548391ecf4SShengjiu Wang  *	30		---------		MUST BE 0
1558391ecf4SShengjiu Wang  *	31		CONT			1: Amount of samples to be
1568391ecf4SShengjiu Wang  *						transferred is unknown and
1578391ecf4SShengjiu Wang  *						script will keep on
1588391ecf4SShengjiu Wang  *						transferring samples as long as
1598391ecf4SShengjiu Wang  *						both events are detected and
1608391ecf4SShengjiu Wang  *						script must be manually stopped
1618391ecf4SShengjiu Wang  *						by the application
1628391ecf4SShengjiu Wang  *						0: The amount of samples to be
1638391ecf4SShengjiu Wang  *						transferred is equal to the
1648391ecf4SShengjiu Wang  *						count field of mode word
1658391ecf4SShengjiu Wang  */
1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML	0xFF
1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS		BIT(8)
1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA		BIT(9)
1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF	BIT(10)
1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP		BIT(11)
1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP		BIT(12)
1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML	(0xFF << 16)
1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE	BIT(28)
1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE	BIT(29)
1758391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT	BIT(31)
1768391ecf4SShengjiu Wang 
177f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
178f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
179f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
180f9d4a398SNicolin Chen 
181f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS	(BIT(DMA_DEV_TO_MEM) | \
182f9d4a398SNicolin Chen 				 BIT(DMA_MEM_TO_DEV) | \
183f9d4a398SNicolin Chen 				 BIT(DMA_DEV_TO_DEV))
184f9d4a398SNicolin Chen 
185*824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_N_FIFOS	GENMASK(15, 12)
186*824a0a02SSascha Hauer #define SDMA_WATERMARK_LEVEL_SW_DONE	BIT(23)
187*824a0a02SSascha Hauer 
188*824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_SEL	BIT(7)
189*824a0a02SSascha Hauer #define SDMA_DONE0_CONFIG_DONE_DIS	BIT(6)
190*824a0a02SSascha Hauer 
1918d11cfb0SVladimir Zapolskiy /**
1928d11cfb0SVladimir Zapolskiy  * struct sdma_script_start_addrs - SDMA script start pointers
1938d11cfb0SVladimir Zapolskiy  *
1948d11cfb0SVladimir Zapolskiy  * start addresses of the different functions in the physical
1958d11cfb0SVladimir Zapolskiy  * address space of the SDMA engine.
1968d11cfb0SVladimir Zapolskiy  */
1978d11cfb0SVladimir Zapolskiy struct sdma_script_start_addrs {
1988d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_addr;
1998d11cfb0SVladimir Zapolskiy 	s32 ap_2_bp_addr;
2008d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_fixed_addr;
2018d11cfb0SVladimir Zapolskiy 	s32 bp_2_ap_addr;
2028d11cfb0SVladimir Zapolskiy 	s32 loopback_on_dsp_side_addr;
2038d11cfb0SVladimir Zapolskiy 	s32 mcu_interrupt_only_addr;
2048d11cfb0SVladimir Zapolskiy 	s32 firi_2_per_addr;
2058d11cfb0SVladimir Zapolskiy 	s32 firi_2_mcu_addr;
2068d11cfb0SVladimir Zapolskiy 	s32 per_2_firi_addr;
2078d11cfb0SVladimir Zapolskiy 	s32 mcu_2_firi_addr;
2088d11cfb0SVladimir Zapolskiy 	s32 uart_2_per_addr;
209b98ce2f4SRobin Gong 	s32 uart_2_mcu_ram_addr;
2108d11cfb0SVladimir Zapolskiy 	s32 per_2_app_addr;
2118d11cfb0SVladimir Zapolskiy 	s32 mcu_2_app_addr;
2128d11cfb0SVladimir Zapolskiy 	s32 per_2_per_addr;
2138d11cfb0SVladimir Zapolskiy 	s32 uartsh_2_per_addr;
214b98ce2f4SRobin Gong 	s32 uartsh_2_mcu_ram_addr;
2158d11cfb0SVladimir Zapolskiy 	s32 per_2_shp_addr;
2168d11cfb0SVladimir Zapolskiy 	s32 mcu_2_shp_addr;
2178d11cfb0SVladimir Zapolskiy 	s32 ata_2_mcu_addr;
2188d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ata_addr;
2198d11cfb0SVladimir Zapolskiy 	s32 app_2_per_addr;
2208d11cfb0SVladimir Zapolskiy 	s32 app_2_mcu_addr;
2218d11cfb0SVladimir Zapolskiy 	s32 shp_2_per_addr;
2228d11cfb0SVladimir Zapolskiy 	s32 shp_2_mcu_addr;
2238d11cfb0SVladimir Zapolskiy 	s32 mshc_2_mcu_addr;
2248d11cfb0SVladimir Zapolskiy 	s32 mcu_2_mshc_addr;
2258d11cfb0SVladimir Zapolskiy 	s32 spdif_2_mcu_addr;
2268d11cfb0SVladimir Zapolskiy 	s32 mcu_2_spdif_addr;
2278d11cfb0SVladimir Zapolskiy 	s32 asrc_2_mcu_addr;
2288d11cfb0SVladimir Zapolskiy 	s32 ext_mem_2_ipu_addr;
2298d11cfb0SVladimir Zapolskiy 	s32 descrambler_addr;
2308d11cfb0SVladimir Zapolskiy 	s32 dptc_dvfs_addr;
2318d11cfb0SVladimir Zapolskiy 	s32 utra_addr;
2328d11cfb0SVladimir Zapolskiy 	s32 ram_code_start_addr;
2338d11cfb0SVladimir Zapolskiy 	/* End of v1 array */
2348d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ssish_addr;
2358d11cfb0SVladimir Zapolskiy 	s32 ssish_2_mcu_addr;
2368d11cfb0SVladimir Zapolskiy 	s32 hdmi_dma_addr;
2378d11cfb0SVladimir Zapolskiy 	/* End of v2 array */
2388d11cfb0SVladimir Zapolskiy 	s32 zcanfd_2_mcu_addr;
2398d11cfb0SVladimir Zapolskiy 	s32 zqspi_2_mcu_addr;
2408d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ecspi_addr;
241b98ce2f4SRobin Gong 	s32 mcu_2_sai_addr;
242b98ce2f4SRobin Gong 	s32 sai_2_mcu_addr;
243b98ce2f4SRobin Gong 	s32 uart_2_mcu_addr;
244b98ce2f4SRobin Gong 	s32 uartsh_2_mcu_addr;
2458d11cfb0SVladimir Zapolskiy 	/* End of v3 array */
2468d11cfb0SVladimir Zapolskiy 	s32 mcu_2_zqspi_addr;
2478d11cfb0SVladimir Zapolskiy 	/* End of v4 array */
2488d11cfb0SVladimir Zapolskiy };
2498d11cfb0SVladimir Zapolskiy 
2508391ecf4SShengjiu Wang /*
2511ec1e82fSSascha Hauer  * Mode/Count of data node descriptors - IPCv2
2521ec1e82fSSascha Hauer  */
2531ec1e82fSSascha Hauer struct sdma_mode_count {
2544a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT	0xffff
2551ec1e82fSSascha Hauer 	u32 count   : 16; /* size of the buffer pointed by this BD */
2561ec1e82fSSascha Hauer 	u32 status  :  8; /* E,R,I,C,W,D status bits stored here */
257e4b75760SMartin Kaiser 	u32 command :  8; /* command mostly used for channel 0 */
2581ec1e82fSSascha Hauer };
2591ec1e82fSSascha Hauer 
2601ec1e82fSSascha Hauer /*
2611ec1e82fSSascha Hauer  * Buffer descriptor
2621ec1e82fSSascha Hauer  */
2631ec1e82fSSascha Hauer struct sdma_buffer_descriptor {
2641ec1e82fSSascha Hauer 	struct sdma_mode_count  mode;
2651ec1e82fSSascha Hauer 	u32 buffer_addr;	/* address of the buffer described */
2661ec1e82fSSascha Hauer 	u32 ext_buffer_addr;	/* extended buffer address */
2671ec1e82fSSascha Hauer } __attribute__ ((packed));
2681ec1e82fSSascha Hauer 
2691ec1e82fSSascha Hauer /**
2701ec1e82fSSascha Hauer  * struct sdma_channel_control - Channel control Block
2711ec1e82fSSascha Hauer  *
27224ca312dSRobin Gong  * @current_bd_ptr:	current buffer descriptor processed
27324ca312dSRobin Gong  * @base_bd_ptr:	first element of buffer descriptor array
27424ca312dSRobin Gong  * @unused:		padding. The SDMA engine expects an array of 128 byte
2751ec1e82fSSascha Hauer  *			control blocks
2761ec1e82fSSascha Hauer  */
2771ec1e82fSSascha Hauer struct sdma_channel_control {
2781ec1e82fSSascha Hauer 	u32 current_bd_ptr;
2791ec1e82fSSascha Hauer 	u32 base_bd_ptr;
2801ec1e82fSSascha Hauer 	u32 unused[2];
2811ec1e82fSSascha Hauer } __attribute__ ((packed));
2821ec1e82fSSascha Hauer 
2831ec1e82fSSascha Hauer /**
2841ec1e82fSSascha Hauer  * struct sdma_state_registers - SDMA context for a channel
2851ec1e82fSSascha Hauer  *
2861ec1e82fSSascha Hauer  * @pc:		program counter
28724ca312dSRobin Gong  * @unused1:	unused
2881ec1e82fSSascha Hauer  * @t:		test bit: status of arithmetic & test instruction
2891ec1e82fSSascha Hauer  * @rpc:	return program counter
29024ca312dSRobin Gong  * @unused0:	unused
2911ec1e82fSSascha Hauer  * @sf:		source fault while loading data
2921ec1e82fSSascha Hauer  * @spc:	loop start program counter
29324ca312dSRobin Gong  * @unused2:	unused
2941ec1e82fSSascha Hauer  * @df:		destination fault while storing data
2951ec1e82fSSascha Hauer  * @epc:	loop end program counter
2961ec1e82fSSascha Hauer  * @lm:		loop mode
2971ec1e82fSSascha Hauer  */
2981ec1e82fSSascha Hauer struct sdma_state_registers {
2991ec1e82fSSascha Hauer 	u32 pc     :14;
3001ec1e82fSSascha Hauer 	u32 unused1: 1;
3011ec1e82fSSascha Hauer 	u32 t      : 1;
3021ec1e82fSSascha Hauer 	u32 rpc    :14;
3031ec1e82fSSascha Hauer 	u32 unused0: 1;
3041ec1e82fSSascha Hauer 	u32 sf     : 1;
3051ec1e82fSSascha Hauer 	u32 spc    :14;
3061ec1e82fSSascha Hauer 	u32 unused2: 1;
3071ec1e82fSSascha Hauer 	u32 df     : 1;
3081ec1e82fSSascha Hauer 	u32 epc    :14;
3091ec1e82fSSascha Hauer 	u32 lm     : 2;
3101ec1e82fSSascha Hauer } __attribute__ ((packed));
3111ec1e82fSSascha Hauer 
3121ec1e82fSSascha Hauer /**
3131ec1e82fSSascha Hauer  * struct sdma_context_data - sdma context specific to a channel
3141ec1e82fSSascha Hauer  *
3151ec1e82fSSascha Hauer  * @channel_state:	channel state bits
3161ec1e82fSSascha Hauer  * @gReg:		general registers
3171ec1e82fSSascha Hauer  * @mda:		burst dma destination address register
3181ec1e82fSSascha Hauer  * @msa:		burst dma source address register
3191ec1e82fSSascha Hauer  * @ms:			burst dma status register
3201ec1e82fSSascha Hauer  * @md:			burst dma data register
3211ec1e82fSSascha Hauer  * @pda:		peripheral dma destination address register
3221ec1e82fSSascha Hauer  * @psa:		peripheral dma source address register
3231ec1e82fSSascha Hauer  * @ps:			peripheral dma status register
3241ec1e82fSSascha Hauer  * @pd:			peripheral dma data register
3251ec1e82fSSascha Hauer  * @ca:			CRC polynomial register
3261ec1e82fSSascha Hauer  * @cs:			CRC accumulator register
3271ec1e82fSSascha Hauer  * @dda:		dedicated core destination address register
3281ec1e82fSSascha Hauer  * @dsa:		dedicated core source address register
3291ec1e82fSSascha Hauer  * @ds:			dedicated core status register
3301ec1e82fSSascha Hauer  * @dd:			dedicated core data register
33124ca312dSRobin Gong  * @scratch0:		1st word of dedicated ram for context switch
33224ca312dSRobin Gong  * @scratch1:		2nd word of dedicated ram for context switch
33324ca312dSRobin Gong  * @scratch2:		3rd word of dedicated ram for context switch
33424ca312dSRobin Gong  * @scratch3:		4th word of dedicated ram for context switch
33524ca312dSRobin Gong  * @scratch4:		5th word of dedicated ram for context switch
33624ca312dSRobin Gong  * @scratch5:		6th word of dedicated ram for context switch
33724ca312dSRobin Gong  * @scratch6:		7th word of dedicated ram for context switch
33824ca312dSRobin Gong  * @scratch7:		8th word of dedicated ram for context switch
3391ec1e82fSSascha Hauer  */
3401ec1e82fSSascha Hauer struct sdma_context_data {
3411ec1e82fSSascha Hauer 	struct sdma_state_registers  channel_state;
3421ec1e82fSSascha Hauer 	u32  gReg[8];
3431ec1e82fSSascha Hauer 	u32  mda;
3441ec1e82fSSascha Hauer 	u32  msa;
3451ec1e82fSSascha Hauer 	u32  ms;
3461ec1e82fSSascha Hauer 	u32  md;
3471ec1e82fSSascha Hauer 	u32  pda;
3481ec1e82fSSascha Hauer 	u32  psa;
3491ec1e82fSSascha Hauer 	u32  ps;
3501ec1e82fSSascha Hauer 	u32  pd;
3511ec1e82fSSascha Hauer 	u32  ca;
3521ec1e82fSSascha Hauer 	u32  cs;
3531ec1e82fSSascha Hauer 	u32  dda;
3541ec1e82fSSascha Hauer 	u32  dsa;
3551ec1e82fSSascha Hauer 	u32  ds;
3561ec1e82fSSascha Hauer 	u32  dd;
3571ec1e82fSSascha Hauer 	u32  scratch0;
3581ec1e82fSSascha Hauer 	u32  scratch1;
3591ec1e82fSSascha Hauer 	u32  scratch2;
3601ec1e82fSSascha Hauer 	u32  scratch3;
3611ec1e82fSSascha Hauer 	u32  scratch4;
3621ec1e82fSSascha Hauer 	u32  scratch5;
3631ec1e82fSSascha Hauer 	u32  scratch6;
3641ec1e82fSSascha Hauer 	u32  scratch7;
3651ec1e82fSSascha Hauer } __attribute__ ((packed));
3661ec1e82fSSascha Hauer 
3671ec1e82fSSascha Hauer 
3681ec1e82fSSascha Hauer struct sdma_engine;
3691ec1e82fSSascha Hauer 
3701ec1e82fSSascha Hauer /**
37176c33d27SSascha Hauer  * struct sdma_desc - descriptor structor for one transfer
37224ca312dSRobin Gong  * @vd:			descriptor for virt dma
37324ca312dSRobin Gong  * @num_bd:		number of descriptors currently handling
37424ca312dSRobin Gong  * @bd_phys:		physical address of bd
37524ca312dSRobin Gong  * @buf_tail:		ID of the buffer that was processed
37624ca312dSRobin Gong  * @buf_ptail:		ID of the previous buffer that was processed
37724ca312dSRobin Gong  * @period_len:		period length, used in cyclic.
37824ca312dSRobin Gong  * @chn_real_count:	the real count updated from bd->mode.count
37924ca312dSRobin Gong  * @chn_count:		the transfer count set
38024ca312dSRobin Gong  * @sdmac:		sdma_channel pointer
38124ca312dSRobin Gong  * @bd:			pointer of allocate bd
38276c33d27SSascha Hauer  */
38376c33d27SSascha Hauer struct sdma_desc {
38457b772b8SRobin Gong 	struct virt_dma_desc	vd;
38576c33d27SSascha Hauer 	unsigned int		num_bd;
38676c33d27SSascha Hauer 	dma_addr_t		bd_phys;
38776c33d27SSascha Hauer 	unsigned int		buf_tail;
38876c33d27SSascha Hauer 	unsigned int		buf_ptail;
38976c33d27SSascha Hauer 	unsigned int		period_len;
39076c33d27SSascha Hauer 	unsigned int		chn_real_count;
39176c33d27SSascha Hauer 	unsigned int		chn_count;
39276c33d27SSascha Hauer 	struct sdma_channel	*sdmac;
39376c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd;
39476c33d27SSascha Hauer };
39576c33d27SSascha Hauer 
39676c33d27SSascha Hauer /**
3971ec1e82fSSascha Hauer  * struct sdma_channel - housekeeping for a SDMA channel
3981ec1e82fSSascha Hauer  *
39924ca312dSRobin Gong  * @vc:			virt_dma base structure
40024ca312dSRobin Gong  * @desc:		sdma description including vd and other special member
40124ca312dSRobin Gong  * @sdma:		pointer to the SDMA engine for this channel
40224ca312dSRobin Gong  * @channel:		the channel number, matches dmaengine chan_id + 1
40324ca312dSRobin Gong  * @direction:		transfer type. Needed for setting SDMA script
404d0c4a149SLee Jones  * @slave_config:	Slave configuration
40524ca312dSRobin Gong  * @peripheral_type:	Peripheral type. Needed for setting SDMA script
40624ca312dSRobin Gong  * @event_id0:		aka dma request line
40724ca312dSRobin Gong  * @event_id1:		for channels that use 2 events
40824ca312dSRobin Gong  * @word_size:		peripheral access size
40924ca312dSRobin Gong  * @pc_from_device:	script address for those device_2_memory
41024ca312dSRobin Gong  * @pc_to_device:	script address for those memory_2_device
41124ca312dSRobin Gong  * @device_to_device:	script address for those device_2_device
4120f06c027SRobin Gong  * @pc_to_pc:		script address for those memory_2_memory
41324ca312dSRobin Gong  * @flags:		loop mode or not
41424ca312dSRobin Gong  * @per_address:	peripheral source or destination address in common case
41524ca312dSRobin Gong  *                      destination address in p_2_p case
41624ca312dSRobin Gong  * @per_address2:	peripheral source address in p_2_p case
41724ca312dSRobin Gong  * @event_mask:		event mask used in p_2_p script
41824ca312dSRobin Gong  * @watermark_level:	value for gReg[7], some script will extend it from
41924ca312dSRobin Gong  *			basic watermark such as p_2_p
42024ca312dSRobin Gong  * @shp_addr:		value for gReg[6]
42124ca312dSRobin Gong  * @per_addr:		value for gReg[2]
42224ca312dSRobin Gong  * @status:		status of dma channel
423d0c4a149SLee Jones  * @context_loaded:	ensure context is only loaded once
42424ca312dSRobin Gong  * @data:		specific sdma interface structure
42524ca312dSRobin Gong  * @bd_pool:		dma_pool for bd
426d0c4a149SLee Jones  * @terminate_worker:	used to call back into terminate work function
4271ec1e82fSSascha Hauer  */
4281ec1e82fSSascha Hauer struct sdma_channel {
42957b772b8SRobin Gong 	struct virt_dma_chan		vc;
43076c33d27SSascha Hauer 	struct sdma_desc		*desc;
4311ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
4321ec1e82fSSascha Hauer 	unsigned int			channel;
433db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
434107d0644SVinod Koul 	struct dma_slave_config		slave_config;
4351ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
4361ec1e82fSSascha Hauer 	unsigned int			event_id0;
4371ec1e82fSSascha Hauer 	unsigned int			event_id1;
4381ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
4391ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
4408391ecf4SShengjiu Wang 	unsigned int			device_to_device;
4410f06c027SRobin Gong 	unsigned int                    pc_to_pc;
4421ec1e82fSSascha Hauer 	unsigned long			flags;
4438391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
4440bbc1413SRichard Zhao 	unsigned long			event_mask[2];
4450bbc1413SRichard Zhao 	unsigned long			watermark_level;
4461ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
4471ec1e82fSSascha Hauer 	enum dma_status			status;
4480b351865SNicolin Chen 	struct imx_dma_data		data;
449b8603d2aSLucas Stach 	struct work_struct		terminate_worker;
4504e2b10beSRobin Gong 	struct list_head                terminated;
451e8fafa50SRobin Gong 	bool				is_ram_script;
452*824a0a02SSascha Hauer 	unsigned int			n_fifos_src;
453*824a0a02SSascha Hauer 	unsigned int			n_fifos_dst;
454*824a0a02SSascha Hauer 	bool				sw_done;
4551ec1e82fSSascha Hauer };
4561ec1e82fSSascha Hauer 
4570bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
4581ec1e82fSSascha Hauer 
4591ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
4601ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
4611ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
4621ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
4631ec1e82fSSascha Hauer 
4641ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
4651ec1e82fSSascha Hauer 
4661ec1e82fSSascha Hauer /**
4671ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
4681ec1e82fSSascha Hauer  *
46924ca312dSRobin Gong  * @magic:		"SDMA"
47024ca312dSRobin Gong  * @version_major:	increased whenever layout of struct
47124ca312dSRobin Gong  *			sdma_script_start_addrs changes.
47224ca312dSRobin Gong  * @version_minor:	firmware minor version (for binary compatible changes)
47324ca312dSRobin Gong  * @script_addrs_start:	offset of struct sdma_script_start_addrs in this image
47424ca312dSRobin Gong  * @num_script_addrs:	Number of script addresses in this image
47524ca312dSRobin Gong  * @ram_code_start:	offset of SDMA ram image in this firmware image
47624ca312dSRobin Gong  * @ram_code_size:	size of SDMA ram image
47724ca312dSRobin Gong  * @script_addrs:	Stores the start address of the SDMA scripts
4781ec1e82fSSascha Hauer  *			(in SDMA memory space)
4791ec1e82fSSascha Hauer  */
4801ec1e82fSSascha Hauer struct sdma_firmware_header {
4811ec1e82fSSascha Hauer 	u32	magic;
4821ec1e82fSSascha Hauer 	u32	version_major;
4831ec1e82fSSascha Hauer 	u32	version_minor;
4841ec1e82fSSascha Hauer 	u32	script_addrs_start;
4851ec1e82fSSascha Hauer 	u32	num_script_addrs;
4861ec1e82fSSascha Hauer 	u32	ram_code_start;
4871ec1e82fSSascha Hauer 	u32	ram_code_size;
4881ec1e82fSSascha Hauer };
4891ec1e82fSSascha Hauer 
49017bba72fSSascha Hauer struct sdma_driver_data {
49117bba72fSSascha Hauer 	int chnenbl0;
49217bba72fSSascha Hauer 	int num_events;
493dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
494941acd56SAngus Ainslie (Purism) 	bool check_ratio;
4954852e9a2SRobin Gong 	/*
4964852e9a2SRobin Gong 	 * ecspi ERR009165 fixed should be done in sdma script
4974852e9a2SRobin Gong 	 * and it has been fixed in soc from i.mx6ul.
4984852e9a2SRobin Gong 	 * please get more information from the below link:
4994852e9a2SRobin Gong 	 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
5004852e9a2SRobin Gong 	 */
5014852e9a2SRobin Gong 	bool ecspi_fixed;
50262550cd7SShawn Guo };
50362550cd7SShawn Guo 
5041ec1e82fSSascha Hauer struct sdma_engine {
5051ec1e82fSSascha Hauer 	struct device			*dev;
5061ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
5071ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
5081ec1e82fSSascha Hauer 	void __iomem			*regs;
5091ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
5101ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
5111ec1e82fSSascha Hauer 	struct dma_device		dma_device;
5127560e3f3SSascha Hauer 	struct clk			*clk_ipg;
5137560e3f3SSascha Hauer 	struct clk			*clk_ahb;
5142ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
515cd72b846SNicolin Chen 	u32				script_number;
5161ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
51717bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
5188391ecf4SShengjiu Wang 	u32				spba_start_addr;
5198391ecf4SShengjiu Wang 	u32				spba_end_addr;
5205bb9dbb5SVinod Koul 	unsigned int			irq;
52176c33d27SSascha Hauer 	dma_addr_t			bd0_phys;
52276c33d27SSascha Hauer 	struct sdma_buffer_descriptor	*bd0;
52325aaa75dSAngus Ainslie (Purism) 	/* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/
52425aaa75dSAngus Ainslie (Purism) 	bool				clk_ratio;
525e8fafa50SRobin Gong 	bool                            fw_loaded;
52617bba72fSSascha Hauer };
52717bba72fSSascha Hauer 
528107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
529107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
530107d0644SVinod Koul 		       enum dma_transfer_direction direction);
531107d0644SVinod Koul 
532e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
53317bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
53417bba72fSSascha Hauer 	.num_events = 32,
53517bba72fSSascha Hauer };
53617bba72fSSascha Hauer 
537dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
538dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
539dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
540dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
541dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
542dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
543dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
544dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
545dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
546dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
547dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
548dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
549dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
550dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
551dcfec3c0SSascha Hauer };
552dcfec3c0SSascha Hauer 
553e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
554dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
555dcfec3c0SSascha Hauer 	.num_events = 48,
556dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
557dcfec3c0SSascha Hauer };
558dcfec3c0SSascha Hauer 
559e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
56017bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
56117bba72fSSascha Hauer 	.num_events = 48,
5621ec1e82fSSascha Hauer };
5631ec1e82fSSascha Hauer 
564dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
565dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
566dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
567dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
568dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
569dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
570dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
571dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
572dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
573dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
574dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
575dcfec3c0SSascha Hauer };
576dcfec3c0SSascha Hauer 
577e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
578dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
579dcfec3c0SSascha Hauer 	.num_events = 48,
580dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
581dcfec3c0SSascha Hauer };
582dcfec3c0SSascha Hauer 
583dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
584dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
585dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
586dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
587dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
588dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
589dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
590dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
591dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
592dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
593dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
594dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
595dcfec3c0SSascha Hauer };
596dcfec3c0SSascha Hauer 
597e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
598dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
599dcfec3c0SSascha Hauer 	.num_events = 48,
600dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
601dcfec3c0SSascha Hauer };
602dcfec3c0SSascha Hauer 
603dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
604dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
605dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
606dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
607dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
608dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
609dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
610dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
611dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
612dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
613dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
614dcfec3c0SSascha Hauer };
615dcfec3c0SSascha Hauer 
616e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
617dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
618dcfec3c0SSascha Hauer 	.num_events = 48,
619dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
620dcfec3c0SSascha Hauer };
621dcfec3c0SSascha Hauer 
6224852e9a2SRobin Gong static struct sdma_driver_data sdma_imx6ul = {
6234852e9a2SRobin Gong 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
6244852e9a2SRobin Gong 	.num_events = 48,
6254852e9a2SRobin Gong 	.script_addrs = &sdma_script_imx6q,
6264852e9a2SRobin Gong 	.ecspi_fixed = true,
6274852e9a2SRobin Gong };
6284852e9a2SRobin Gong 
629b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = {
630b7d2648aSFabio Estevam 	.ap_2_ap_addr = 644,
631b7d2648aSFabio Estevam 	.uart_2_mcu_addr = 819,
632b7d2648aSFabio Estevam 	.mcu_2_app_addr = 749,
633b7d2648aSFabio Estevam 	.uartsh_2_mcu_addr = 1034,
634b7d2648aSFabio Estevam 	.mcu_2_shp_addr = 962,
635b7d2648aSFabio Estevam 	.app_2_mcu_addr = 685,
636b7d2648aSFabio Estevam 	.shp_2_mcu_addr = 893,
637b7d2648aSFabio Estevam 	.spdif_2_mcu_addr = 1102,
638b7d2648aSFabio Estevam 	.mcu_2_spdif_addr = 1136,
639b7d2648aSFabio Estevam };
640b7d2648aSFabio Estevam 
641b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = {
642b7d2648aSFabio Estevam 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
643b7d2648aSFabio Estevam 	.num_events = 48,
644b7d2648aSFabio Estevam 	.script_addrs = &sdma_script_imx7d,
645b7d2648aSFabio Estevam };
646b7d2648aSFabio Estevam 
647941acd56SAngus Ainslie (Purism) static struct sdma_driver_data sdma_imx8mq = {
648941acd56SAngus Ainslie (Purism) 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
649941acd56SAngus Ainslie (Purism) 	.num_events = 48,
650941acd56SAngus Ainslie (Purism) 	.script_addrs = &sdma_script_imx7d,
651941acd56SAngus Ainslie (Purism) 	.check_ratio = 1,
652941acd56SAngus Ainslie (Purism) };
653941acd56SAngus Ainslie (Purism) 
654580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
655dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
656dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
657dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
65817bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
659dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
66063edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
661b7d2648aSFabio Estevam 	{ .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
6624852e9a2SRobin Gong 	{ .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, },
663941acd56SAngus Ainslie (Purism) 	{ .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, },
664580975d7SShawn Guo 	{ /* sentinel */ }
665580975d7SShawn Guo };
666580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
667580975d7SShawn Guo 
6680bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
6690bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
6700bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
6711ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
6721ec1e82fSSascha Hauer 
6731ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
6741ec1e82fSSascha Hauer {
67517bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
6761ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
6771ec1e82fSSascha Hauer }
6781ec1e82fSSascha Hauer 
6791ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
6801ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
6811ec1e82fSSascha Hauer {
6821ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6831ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6840bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
6851ec1e82fSSascha Hauer 
6861ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
6871ec1e82fSSascha Hauer 		return -EINVAL;
6881ec1e82fSSascha Hauer 
689c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
690c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
691c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
6921ec1e82fSSascha Hauer 
6931ec1e82fSSascha Hauer 	if (dsp_override)
6940bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
6951ec1e82fSSascha Hauer 	else
6960bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
6971ec1e82fSSascha Hauer 
6981ec1e82fSSascha Hauer 	if (event_override)
6990bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
7001ec1e82fSSascha Hauer 	else
7010bbc1413SRichard Zhao 		__set_bit(channel, &evt);
7021ec1e82fSSascha Hauer 
7031ec1e82fSSascha Hauer 	if (mcu_override)
7040bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
7051ec1e82fSSascha Hauer 	else
7060bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
7071ec1e82fSSascha Hauer 
708c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
709c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
710c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
7111ec1e82fSSascha Hauer 
7121ec1e82fSSascha Hauer 	return 0;
7131ec1e82fSSascha Hauer }
7141ec1e82fSSascha Hauer 
7155b215c28STomasz Moń static int is_sdma_channel_enabled(struct sdma_engine *sdma, int channel)
7165b215c28STomasz Moń {
7175b215c28STomasz Moń 	return !!(readl(sdma->regs + SDMA_H_STATSTOP) & BIT(channel));
7185b215c28STomasz Moń }
7195b215c28STomasz Moń 
720b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
721b9a59166SRichard Zhao {
7220bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
723b9a59166SRichard Zhao }
724b9a59166SRichard Zhao 
7251ec1e82fSSascha Hauer /*
7262ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
7271ec1e82fSSascha Hauer  */
7282ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
7291ec1e82fSSascha Hauer {
7301ec1e82fSSascha Hauer 	int ret;
7311d069bfaSMichael Olbrich 	u32 reg;
7321ec1e82fSSascha Hauer 
7332ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
7341ec1e82fSSascha Hauer 
7351d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
7361d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
7371d069bfaSMichael Olbrich 	if (ret)
7382ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
7391ec1e82fSSascha Hauer 
740855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
74125aaa75dSAngus Ainslie (Purism) 	reg = readl(sdma->regs + SDMA_H_CONFIG);
74225aaa75dSAngus Ainslie (Purism) 	if ((reg & SDMA_H_CONFIG_CSM) == 0) {
74325aaa75dSAngus Ainslie (Purism) 		reg |= SDMA_H_CONFIG_CSM;
74425aaa75dSAngus Ainslie (Purism) 		writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG);
74525aaa75dSAngus Ainslie (Purism) 	}
746855832e4SRobin Gong 
7471d069bfaSMichael Olbrich 	return ret;
7481ec1e82fSSascha Hauer }
7491ec1e82fSSascha Hauer 
7501ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
7511ec1e82fSSascha Hauer 		u32 address)
7521ec1e82fSSascha Hauer {
75376c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
7541ec1e82fSSascha Hauer 	void *buf_virt;
7551ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
7561ec1e82fSSascha Hauer 	int ret;
7572ccaef05SRichard Zhao 	unsigned long flags;
75873eab978SSascha Hauer 
759ceaf5226SAndy Duan 	buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL);
760ef6c1dadSFlavio Suligoi 	if (!buf_virt)
7612ccaef05SRichard Zhao 		return -ENOMEM;
7621ec1e82fSSascha Hauer 
7632ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
7642ccaef05SRichard Zhao 
7651ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
7663f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
7671ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
7681ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
7691ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
7701ec1e82fSSascha Hauer 
7711ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
7721ec1e82fSSascha Hauer 
7732ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
7742ccaef05SRichard Zhao 
7752ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
7761ec1e82fSSascha Hauer 
777ceaf5226SAndy Duan 	dma_free_coherent(sdma->dev, size, buf_virt, buf_phys);
7781ec1e82fSSascha Hauer 
7791ec1e82fSSascha Hauer 	return ret;
7801ec1e82fSSascha Hauer }
7811ec1e82fSSascha Hauer 
7821ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
7831ec1e82fSSascha Hauer {
7841ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7851ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7860bbc1413SRichard Zhao 	unsigned long val;
7871ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
7881ec1e82fSSascha Hauer 
789c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
7900bbc1413SRichard Zhao 	__set_bit(channel, &val);
791c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
792*824a0a02SSascha Hauer 
793*824a0a02SSascha Hauer 	/* Set SDMA_DONEx_CONFIG is sw_done enabled */
794*824a0a02SSascha Hauer 	if (sdmac->sw_done) {
795*824a0a02SSascha Hauer 		val = readl_relaxed(sdma->regs + SDMA_DONE0_CONFIG);
796*824a0a02SSascha Hauer 		val |= SDMA_DONE0_CONFIG_DONE_SEL;
797*824a0a02SSascha Hauer 		val &= ~SDMA_DONE0_CONFIG_DONE_DIS;
798*824a0a02SSascha Hauer 		writel_relaxed(val, sdma->regs + SDMA_DONE0_CONFIG);
799*824a0a02SSascha Hauer 	}
8001ec1e82fSSascha Hauer }
8011ec1e82fSSascha Hauer 
8021ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
8031ec1e82fSSascha Hauer {
8041ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8051ec1e82fSSascha Hauer 	int channel = sdmac->channel;
8061ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
8070bbc1413SRichard Zhao 	unsigned long val;
8081ec1e82fSSascha Hauer 
809c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
8100bbc1413SRichard Zhao 	__clear_bit(channel, &val);
811c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
8121ec1e82fSSascha Hauer }
8131ec1e82fSSascha Hauer 
81457b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
81557b772b8SRobin Gong {
81657b772b8SRobin Gong 	return container_of(t, struct sdma_desc, vd.tx);
81757b772b8SRobin Gong }
81857b772b8SRobin Gong 
81957b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac)
82057b772b8SRobin Gong {
82157b772b8SRobin Gong 	struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
82257b772b8SRobin Gong 	struct sdma_desc *desc;
82357b772b8SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
82457b772b8SRobin Gong 	int channel = sdmac->channel;
82557b772b8SRobin Gong 
82657b772b8SRobin Gong 	if (!vd) {
82757b772b8SRobin Gong 		sdmac->desc = NULL;
82857b772b8SRobin Gong 		return;
82957b772b8SRobin Gong 	}
83057b772b8SRobin Gong 	sdmac->desc = desc = to_sdma_desc(&vd->tx);
83102939cd1SSascha Hauer 
83257b772b8SRobin Gong 	list_del(&vd->node);
83357b772b8SRobin Gong 
83457b772b8SRobin Gong 	sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
83557b772b8SRobin Gong 	sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
83657b772b8SRobin Gong 	sdma_enable_channel(sdma, sdmac->channel);
83757b772b8SRobin Gong }
83857b772b8SRobin Gong 
839d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
840d1a792f3SRussell King - ARM Linux {
8411ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
8425881826dSNandor Han 	int error = 0;
8435881826dSNandor Han 	enum dma_status	old_status = sdmac->status;
8441ec1e82fSSascha Hauer 
8451ec1e82fSSascha Hauer 	/*
8461ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
8471ec1e82fSSascha Hauer 	 * call callback function.
8481ec1e82fSSascha Hauer 	 */
84957b772b8SRobin Gong 	while (sdmac->desc) {
85076c33d27SSascha Hauer 		struct sdma_desc *desc = sdmac->desc;
85176c33d27SSascha Hauer 
85276c33d27SSascha Hauer 		bd = &desc->bd[desc->buf_tail];
8531ec1e82fSSascha Hauer 
8541ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
8551ec1e82fSSascha Hauer 			break;
8561ec1e82fSSascha Hauer 
8575881826dSNandor Han 		if (bd->mode.status & BD_RROR) {
8585881826dSNandor Han 			bd->mode.status &= ~BD_RROR;
8591ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
8605881826dSNandor Han 			error = -EIO;
8615881826dSNandor Han 		}
8621ec1e82fSSascha Hauer 
8635881826dSNandor Han 	       /*
8645881826dSNandor Han 		* We use bd->mode.count to calculate the residue, since contains
8655881826dSNandor Han 		* the number of bytes present in the current buffer descriptor.
8665881826dSNandor Han 		*/
8675881826dSNandor Han 
86876c33d27SSascha Hauer 		desc->chn_real_count = bd->mode.count;
86976c33d27SSascha Hauer 		bd->mode.count = desc->period_len;
87076c33d27SSascha Hauer 		desc->buf_ptail = desc->buf_tail;
87176c33d27SSascha Hauer 		desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
87215f30f51SNandor Han 
87315f30f51SNandor Han 		/*
87415f30f51SNandor Han 		 * The callback is called from the interrupt context in order
87515f30f51SNandor Han 		 * to reduce latency and to avoid the risk of altering the
87615f30f51SNandor Han 		 * SDMA transaction status by the time the client tasklet is
87715f30f51SNandor Han 		 * executed.
87815f30f51SNandor Han 		 */
87957b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
88057b772b8SRobin Gong 		dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
88157b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
88215f30f51SNandor Han 
883177360e0STomasz Moń 		/* Assign buffer ownership to SDMA */
884177360e0STomasz Moń 		bd->mode.status |= BD_DONE;
885177360e0STomasz Moń 
8865881826dSNandor Han 		if (error)
8875881826dSNandor Han 			sdmac->status = old_status;
8881ec1e82fSSascha Hauer 	}
8895b215c28STomasz Moń 
8905b215c28STomasz Moń 	/*
8915b215c28STomasz Moń 	 * SDMA stops cyclic channel when DMA request triggers a channel and no SDMA
8925b215c28STomasz Moń 	 * owned buffer is available (i.e. BD_DONE was set too late).
8935b215c28STomasz Moń 	 */
8945b215c28STomasz Moń 	if (!is_sdma_channel_enabled(sdmac->sdma, sdmac->channel)) {
8955b215c28STomasz Moń 		dev_warn(sdmac->sdma->dev, "restart cyclic channel %d\n", sdmac->channel);
8965b215c28STomasz Moń 		sdma_enable_channel(sdmac->sdma, sdmac->channel);
8975b215c28STomasz Moń 	}
8981ec1e82fSSascha Hauer }
8991ec1e82fSSascha Hauer 
90057b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
9011ec1e82fSSascha Hauer {
90215f30f51SNandor Han 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
9031ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
9041ec1e82fSSascha Hauer 	int i, error = 0;
9051ec1e82fSSascha Hauer 
90676c33d27SSascha Hauer 	sdmac->desc->chn_real_count = 0;
9071ec1e82fSSascha Hauer 	/*
9081ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
9091ec1e82fSSascha Hauer 	 * errors and call callback function
9101ec1e82fSSascha Hauer 	 */
91176c33d27SSascha Hauer 	for (i = 0; i < sdmac->desc->num_bd; i++) {
91276c33d27SSascha Hauer 		bd = &sdmac->desc->bd[i];
9131ec1e82fSSascha Hauer 
9141ec1e82fSSascha Hauer 		if (bd->mode.status & (BD_DONE | BD_RROR))
9151ec1e82fSSascha Hauer 			error = -EIO;
91676c33d27SSascha Hauer 		sdmac->desc->chn_real_count += bd->mode.count;
9171ec1e82fSSascha Hauer 	}
9181ec1e82fSSascha Hauer 
9191ec1e82fSSascha Hauer 	if (error)
9201ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
9211ec1e82fSSascha Hauer 	else
922409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
9231ec1e82fSSascha Hauer }
9241ec1e82fSSascha Hauer 
9251ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
9261ec1e82fSSascha Hauer {
9271ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
9280bbc1413SRichard Zhao 	unsigned long stat;
9291ec1e82fSSascha Hauer 
930c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
931c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
9321d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
9331d069bfaSMichael Olbrich 	stat &= ~1;
9341ec1e82fSSascha Hauer 
9351ec1e82fSSascha Hauer 	while (stat) {
9361ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
9371ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
93857b772b8SRobin Gong 		struct sdma_desc *desc;
9391ec1e82fSSascha Hauer 
94057b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
94157b772b8SRobin Gong 		desc = sdmac->desc;
94257b772b8SRobin Gong 		if (desc) {
94357b772b8SRobin Gong 			if (sdmac->flags & IMX_DMA_SG_LOOP) {
944d1a792f3SRussell King - ARM Linux 				sdma_update_channel_loop(sdmac);
94557b772b8SRobin Gong 			} else {
94657b772b8SRobin Gong 				mxc_sdma_handle_channel_normal(sdmac);
94757b772b8SRobin Gong 				vchan_cookie_complete(&desc->vd);
94857b772b8SRobin Gong 				sdma_start_desc(sdmac);
94957b772b8SRobin Gong 			}
95057b772b8SRobin Gong 		}
9511ec1e82fSSascha Hauer 
95257b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
9530bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
9541ec1e82fSSascha Hauer 	}
9551ec1e82fSSascha Hauer 
9561ec1e82fSSascha Hauer 	return IRQ_HANDLED;
9571ec1e82fSSascha Hauer }
9581ec1e82fSSascha Hauer 
9591ec1e82fSSascha Hauer /*
9601ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
9611ec1e82fSSascha Hauer  */
962625d8936SSascha Hauer static int sdma_get_pc(struct sdma_channel *sdmac,
9631ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
9641ec1e82fSSascha Hauer {
9651ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
9661ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
9671ec1e82fSSascha Hauer 	/*
9681ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
9691ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
9701ec1e82fSSascha Hauer 	 */
9710f06c027SRobin Gong 	int per_2_per = 0, emi_2_emi = 0;
9721ec1e82fSSascha Hauer 
9731ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
9741ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
9758391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
9760f06c027SRobin Gong 	sdmac->pc_to_pc = 0;
977e8fafa50SRobin Gong 	sdmac->is_ram_script = false;
9781ec1e82fSSascha Hauer 
9791ec1e82fSSascha Hauer 	switch (peripheral_type) {
9801ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
9810f06c027SRobin Gong 		emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
9821ec1e82fSSascha Hauer 		break;
9831ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
9841ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
9851ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
9861ec1e82fSSascha Hauer 		break;
9871ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
9881ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
9891ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
9901ec1e82fSSascha Hauer 		break;
9911ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
9921ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
9931ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
9941ec1e82fSSascha Hauer 		break;
9951ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
9961ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
9971ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
9981ec1e82fSSascha Hauer 		break;
9991ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
10001ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
10011ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
10021ec1e82fSSascha Hauer 		break;
10031ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
1004a4965888SRobin Gong 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
10054852e9a2SRobin Gong 
10064852e9a2SRobin Gong 		/* Use rom script mcu_2_app if ERR009165 fixed */
10074852e9a2SRobin Gong 		if (sdmac->sdma->drvdata->ecspi_fixed) {
10084852e9a2SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10094852e9a2SRobin Gong 		} else {
1010a4965888SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr;
1011a4965888SRobin Gong 			sdmac->is_ram_script = true;
10124852e9a2SRobin Gong 		}
10134852e9a2SRobin Gong 
1014a4965888SRobin Gong 		break;
10151ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
10161ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
101729aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
10181ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
10191ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10201ec1e82fSSascha Hauer 		break;
10211a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
10221a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
10231a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
1024e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10251a895578SNicolin Chen 		break;
10261ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
10271ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
10281ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
10291ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
10301ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
10311ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
10321ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
10331ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
10341ec1e82fSSascha Hauer 		break;
10351ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
10361ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
10371ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
10381ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
1039e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10401ec1e82fSSascha Hauer 		break;
1041f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
1042f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1043f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1044f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
1045f892afb0SNicolin Chen 		break;
10461ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
10471ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
10481ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
10491ec1e82fSSascha Hauer 		break;
10501ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
10511ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
10521ec1e82fSSascha Hauer 		break;
10531ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
10541ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
10551ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
10561ec1e82fSSascha Hauer 		break;
10571ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
10581ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
10591ec1e82fSSascha Hauer 		break;
1060*824a0a02SSascha Hauer 	case IMX_DMATYPE_MULTI_SAI:
1061*824a0a02SSascha Hauer 		per_2_emi = sdma->script_addrs->sai_2_mcu_addr;
1062*824a0a02SSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_sai_addr;
1063*824a0a02SSascha Hauer 		break;
10641ec1e82fSSascha Hauer 	default:
1065625d8936SSascha Hauer 		dev_err(sdma->dev, "Unsupported transfer type %d\n",
1066625d8936SSascha Hauer 			peripheral_type);
1067625d8936SSascha Hauer 		return -EINVAL;
10681ec1e82fSSascha Hauer 	}
10691ec1e82fSSascha Hauer 
10701ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
10711ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
10728391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
10730f06c027SRobin Gong 	sdmac->pc_to_pc = emi_2_emi;
1074625d8936SSascha Hauer 
1075625d8936SSascha Hauer 	return 0;
10761ec1e82fSSascha Hauer }
10771ec1e82fSSascha Hauer 
10781ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
10791ec1e82fSSascha Hauer {
10801ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10811ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10821ec1e82fSSascha Hauer 	int load_address;
10831ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
108476c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
10851ec1e82fSSascha Hauer 	int ret;
10862ccaef05SRichard Zhao 	unsigned long flags;
10871ec1e82fSSascha Hauer 
10888391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
10891ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
10908391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
10918391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
10920f06c027SRobin Gong 	else if (sdmac->direction == DMA_MEM_TO_MEM)
10930f06c027SRobin Gong 		load_address = sdmac->pc_to_pc;
10948391ecf4SShengjiu Wang 	else
10951ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
10961ec1e82fSSascha Hauer 
10971ec1e82fSSascha Hauer 	if (load_address < 0)
10981ec1e82fSSascha Hauer 		return load_address;
10991ec1e82fSSascha Hauer 
11001ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
11010bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
11021ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
11031ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
11040bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
11050bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
11061ec1e82fSSascha Hauer 
11072ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
110873eab978SSascha Hauer 
11091ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
11101ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
11111ec1e82fSSascha Hauer 
11121ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
11131ec1e82fSSascha Hauer 	 * and watermark level
11141ec1e82fSSascha Hauer 	 */
11150bbc1413SRichard Zhao 	context->gReg[0] = sdmac->event_mask[1];
11160bbc1413SRichard Zhao 	context->gReg[1] = sdmac->event_mask[0];
11171ec1e82fSSascha Hauer 	context->gReg[2] = sdmac->per_addr;
11181ec1e82fSSascha Hauer 	context->gReg[6] = sdmac->shp_addr;
11191ec1e82fSSascha Hauer 	context->gReg[7] = sdmac->watermark_level;
11201ec1e82fSSascha Hauer 
11211ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
11223f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
11231ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
11241ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
11251ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
11262ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
11271ec1e82fSSascha Hauer 
11282ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
112973eab978SSascha Hauer 
11301ec1e82fSSascha Hauer 	return ret;
11311ec1e82fSSascha Hauer }
11321ec1e82fSSascha Hauer 
11337b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
11341ec1e82fSSascha Hauer {
113557b772b8SRobin Gong 	return container_of(chan, struct sdma_channel, vc.chan);
11367b350ab0SMaxime Ripard }
11377b350ab0SMaxime Ripard 
11387b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
11397b350ab0SMaxime Ripard {
11407b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11411ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11421ec1e82fSSascha Hauer 	int channel = sdmac->channel;
11431ec1e82fSSascha Hauer 
11440bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
11451ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
11467b350ab0SMaxime Ripard 
11477b350ab0SMaxime Ripard 	return 0;
11481ec1e82fSSascha Hauer }
1149b8603d2aSLucas Stach static void sdma_channel_terminate_work(struct work_struct *work)
11507f3ff14bSJiada Wang {
1151b8603d2aSLucas Stach 	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1152b8603d2aSLucas Stach 						  terminate_worker);
11537f3ff14bSJiada Wang 	/*
11547f3ff14bSJiada Wang 	 * According to NXP R&D team a delay of one BD SDMA cost time
11557f3ff14bSJiada Wang 	 * (maximum is 1ms) should be added after disable of the channel
11567f3ff14bSJiada Wang 	 * bit, to ensure SDMA core has really been stopped after SDMA
11577f3ff14bSJiada Wang 	 * clients call .device_terminate_all.
11587f3ff14bSJiada Wang 	 */
1159b8603d2aSLucas Stach 	usleep_range(1000, 2000);
1160b8603d2aSLucas Stach 
11614e2b10beSRobin Gong 	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
1162b8603d2aSLucas Stach }
1163b8603d2aSLucas Stach 
1164a80f2787SSascha Hauer static int sdma_terminate_all(struct dma_chan *chan)
1165b8603d2aSLucas Stach {
1166b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
116702939cd1SSascha Hauer 	unsigned long flags;
116802939cd1SSascha Hauer 
116902939cd1SSascha Hauer 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1170b8603d2aSLucas Stach 
1171b8603d2aSLucas Stach 	sdma_disable_channel(chan);
1172b8603d2aSLucas Stach 
117302939cd1SSascha Hauer 	if (sdmac->desc) {
117402939cd1SSascha Hauer 		vchan_terminate_vdesc(&sdmac->desc->vd);
11754e2b10beSRobin Gong 		/*
11764e2b10beSRobin Gong 		 * move out current descriptor into terminated list so that
11774e2b10beSRobin Gong 		 * it could be free in sdma_channel_terminate_work alone
11784e2b10beSRobin Gong 		 * later without potential involving next descriptor raised
11794e2b10beSRobin Gong 		 * up before the last descriptor terminated.
11804e2b10beSRobin Gong 		 */
11814e2b10beSRobin Gong 		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
118202939cd1SSascha Hauer 		sdmac->desc = NULL;
1183b8603d2aSLucas Stach 		schedule_work(&sdmac->terminate_worker);
118402939cd1SSascha Hauer 	}
118502939cd1SSascha Hauer 
118602939cd1SSascha Hauer 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
11877f3ff14bSJiada Wang 
11887f3ff14bSJiada Wang 	return 0;
11897f3ff14bSJiada Wang }
11907f3ff14bSJiada Wang 
1191b8603d2aSLucas Stach static void sdma_channel_synchronize(struct dma_chan *chan)
1192b8603d2aSLucas Stach {
1193b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1194b8603d2aSLucas Stach 
1195b8603d2aSLucas Stach 	vchan_synchronize(&sdmac->vc);
1196b8603d2aSLucas Stach 
1197b8603d2aSLucas Stach 	flush_work(&sdmac->terminate_worker);
1198b8603d2aSLucas Stach }
1199b8603d2aSLucas Stach 
12008391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
12018391ecf4SShengjiu Wang {
12028391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
12038391ecf4SShengjiu Wang 
12048391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
12058391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
12068391ecf4SShengjiu Wang 
12078391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
12088391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
12098391ecf4SShengjiu Wang 
12108391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
12118391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
12128391ecf4SShengjiu Wang 
12138391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
12148391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
12158391ecf4SShengjiu Wang 
12168391ecf4SShengjiu Wang 	/*
12178391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
12188391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
12198391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
12208391ecf4SShengjiu Wang 	 */
12218391ecf4SShengjiu Wang 	if (lwml > hwml) {
12228391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
12238391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
12248391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
12258391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
12268391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
12278391ecf4SShengjiu Wang 	}
12288391ecf4SShengjiu Wang 
12298391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
12308391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
12318391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
12328391ecf4SShengjiu Wang 
12338391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
12348391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
12358391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
12368391ecf4SShengjiu Wang 
12378391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
12388391ecf4SShengjiu Wang }
12398391ecf4SShengjiu Wang 
1240*824a0a02SSascha Hauer static void sdma_set_watermarklevel_for_sais(struct sdma_channel *sdmac)
1241*824a0a02SSascha Hauer {
1242*824a0a02SSascha Hauer 	unsigned int n_fifos;
1243*824a0a02SSascha Hauer 
1244*824a0a02SSascha Hauer 	if (sdmac->sw_done)
1245*824a0a02SSascha Hauer 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SW_DONE;
1246*824a0a02SSascha Hauer 
1247*824a0a02SSascha Hauer 	if (sdmac->direction == DMA_DEV_TO_MEM)
1248*824a0a02SSascha Hauer 		n_fifos = sdmac->n_fifos_src;
1249*824a0a02SSascha Hauer 	else
1250*824a0a02SSascha Hauer 		n_fifos = sdmac->n_fifos_dst;
1251*824a0a02SSascha Hauer 
1252*824a0a02SSascha Hauer 	sdmac->watermark_level |=
1253*824a0a02SSascha Hauer 			FIELD_PREP(SDMA_WATERMARK_LEVEL_N_FIFOS, n_fifos);
1254*824a0a02SSascha Hauer }
1255*824a0a02SSascha Hauer 
12567b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
12571ec1e82fSSascha Hauer {
12587b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1259625d8936SSascha Hauer 	int ret;
12601ec1e82fSSascha Hauer 
12617b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
12621ec1e82fSSascha Hauer 
12630bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
12640bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
12651ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
12661ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
12671ec1e82fSSascha Hauer 
12681ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
12691ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
12701ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
12711ec1e82fSSascha Hauer 		break;
12721ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
12731ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
12741ec1e82fSSascha Hauer 		break;
12751ec1e82fSSascha Hauer 	default:
12761ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
12771ec1e82fSSascha Hauer 		break;
12781ec1e82fSSascha Hauer 	}
12791ec1e82fSSascha Hauer 
1280625d8936SSascha Hauer 	ret = sdma_get_pc(sdmac, sdmac->peripheral_type);
1281625d8936SSascha Hauer 	if (ret)
1282625d8936SSascha Hauer 		return ret;
12831ec1e82fSSascha Hauer 
12841ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
12851ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
12861ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
12871ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
12888391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
12898391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
12908391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
12911f8595efSFlavio Suligoi 		} else {
1292*824a0a02SSascha Hauer 			if (sdmac->peripheral_type ==
1293*824a0a02SSascha Hauer 					IMX_DMATYPE_MULTI_SAI)
1294*824a0a02SSascha Hauer 				sdma_set_watermarklevel_for_sais(sdmac);
1295*824a0a02SSascha Hauer 
12960bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
12971f8595efSFlavio Suligoi 		}
12988391ecf4SShengjiu Wang 
12991ec1e82fSSascha Hauer 		/* Address */
13001ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
13018391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
13021ec1e82fSSascha Hauer 	} else {
13031ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
13041ec1e82fSSascha Hauer 	}
13051ec1e82fSSascha Hauer 
1306e555a03bSRobin Gong 	return 0;
13071ec1e82fSSascha Hauer }
13081ec1e82fSSascha Hauer 
13091ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
13101ec1e82fSSascha Hauer 				     unsigned int priority)
13111ec1e82fSSascha Hauer {
13121ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
13131ec1e82fSSascha Hauer 	int channel = sdmac->channel;
13141ec1e82fSSascha Hauer 
13151ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
13161ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
13171ec1e82fSSascha Hauer 		return -EINVAL;
13181ec1e82fSSascha Hauer 	}
13191ec1e82fSSascha Hauer 
1320c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
13211ec1e82fSSascha Hauer 
13221ec1e82fSSascha Hauer 	return 0;
13231ec1e82fSSascha Hauer }
13241ec1e82fSSascha Hauer 
132557b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma)
13261ec1e82fSSascha Hauer {
13271ec1e82fSSascha Hauer 	int ret = -EBUSY;
13281ec1e82fSSascha Hauer 
132931ef489aSLinus Torvalds 	sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys,
133057b772b8SRobin Gong 				       GFP_NOWAIT);
133157b772b8SRobin Gong 	if (!sdma->bd0) {
13321ec1e82fSSascha Hauer 		ret = -ENOMEM;
13331ec1e82fSSascha Hauer 		goto out;
13341ec1e82fSSascha Hauer 	}
13351ec1e82fSSascha Hauer 
133657b772b8SRobin Gong 	sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
133757b772b8SRobin Gong 	sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
13381ec1e82fSSascha Hauer 
133957b772b8SRobin Gong 	sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
13401ec1e82fSSascha Hauer 	return 0;
13411ec1e82fSSascha Hauer out:
13421ec1e82fSSascha Hauer 
13431ec1e82fSSascha Hauer 	return ret;
13441ec1e82fSSascha Hauer }
13451ec1e82fSSascha Hauer 
134657b772b8SRobin Gong 
134757b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc)
13481ec1e82fSSascha Hauer {
1349ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
135057b772b8SRobin Gong 	int ret = 0;
13511ec1e82fSSascha Hauer 
135231ef489aSLinus Torvalds 	desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size,
1353ceaf5226SAndy Duan 				      &desc->bd_phys, GFP_NOWAIT);
135457b772b8SRobin Gong 	if (!desc->bd) {
135557b772b8SRobin Gong 		ret = -ENOMEM;
135657b772b8SRobin Gong 		goto out;
135757b772b8SRobin Gong 	}
135857b772b8SRobin Gong out:
135957b772b8SRobin Gong 	return ret;
136057b772b8SRobin Gong }
13611ec1e82fSSascha Hauer 
136257b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc)
136357b772b8SRobin Gong {
1364ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1365ebb853b1SLucas Stach 
1366ceaf5226SAndy Duan 	dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd,
1367ceaf5226SAndy Duan 			  desc->bd_phys);
136857b772b8SRobin Gong }
13691ec1e82fSSascha Hauer 
137057b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd)
137157b772b8SRobin Gong {
137257b772b8SRobin Gong 	struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
137357b772b8SRobin Gong 
137457b772b8SRobin Gong 	sdma_free_bd(desc);
137557b772b8SRobin Gong 	kfree(desc);
13761ec1e82fSSascha Hauer }
13771ec1e82fSSascha Hauer 
13781ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
13791ec1e82fSSascha Hauer {
13801ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13811ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
13820f06c027SRobin Gong 	struct imx_dma_data mem_data;
13831ec1e82fSSascha Hauer 	int prio, ret;
13841ec1e82fSSascha Hauer 
13850f06c027SRobin Gong 	/*
13860f06c027SRobin Gong 	 * MEMCPY may never setup chan->private by filter function such as
13870f06c027SRobin Gong 	 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
13880f06c027SRobin Gong 	 * Please note in any other slave case, you have to setup chan->private
13890f06c027SRobin Gong 	 * with 'struct imx_dma_data' in your own filter function if you want to
13900f06c027SRobin Gong 	 * request dma channel by dma_request_channel() rather than
13910f06c027SRobin Gong 	 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
13920f06c027SRobin Gong 	 * to warn you to correct your filter function.
13930f06c027SRobin Gong 	 */
13940f06c027SRobin Gong 	if (!data) {
13950f06c027SRobin Gong 		dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
13960f06c027SRobin Gong 		mem_data.priority = 2;
13970f06c027SRobin Gong 		mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
13980f06c027SRobin Gong 		mem_data.dma_request = 0;
13990f06c027SRobin Gong 		mem_data.dma_request2 = 0;
14000f06c027SRobin Gong 		data = &mem_data;
14010f06c027SRobin Gong 
1402625d8936SSascha Hauer 		ret = sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
1403625d8936SSascha Hauer 		if (ret)
1404625d8936SSascha Hauer 			return ret;
14050f06c027SRobin Gong 	}
14061ec1e82fSSascha Hauer 
14071ec1e82fSSascha Hauer 	switch (data->priority) {
14081ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
14091ec1e82fSSascha Hauer 		prio = 3;
14101ec1e82fSSascha Hauer 		break;
14111ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
14121ec1e82fSSascha Hauer 		prio = 2;
14131ec1e82fSSascha Hauer 		break;
14141ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
14151ec1e82fSSascha Hauer 	default:
14161ec1e82fSSascha Hauer 		prio = 1;
14171ec1e82fSSascha Hauer 		break;
14181ec1e82fSSascha Hauer 	}
14191ec1e82fSSascha Hauer 
14201ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
14211ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
14228391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1423c2c744d3SRichard Zhao 
1424b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1425b93edcddSFabio Estevam 	if (ret)
1426b93edcddSFabio Estevam 		return ret;
1427b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1428b93edcddSFabio Estevam 	if (ret)
1429b93edcddSFabio Estevam 		goto disable_clk_ipg;
1430c2c744d3SRichard Zhao 
14313bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
14321ec1e82fSSascha Hauer 	if (ret)
1433b93edcddSFabio Estevam 		goto disable_clk_ahb;
14341ec1e82fSSascha Hauer 
14351ec1e82fSSascha Hauer 	return 0;
1436b93edcddSFabio Estevam 
1437b93edcddSFabio Estevam disable_clk_ahb:
1438b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1439b93edcddSFabio Estevam disable_clk_ipg:
1440b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1441b93edcddSFabio Estevam 	return ret;
14421ec1e82fSSascha Hauer }
14431ec1e82fSSascha Hauer 
14441ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
14451ec1e82fSSascha Hauer {
14461ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14471ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
14481ec1e82fSSascha Hauer 
1449a80f2787SSascha Hauer 	sdma_terminate_all(chan);
1450b8603d2aSLucas Stach 
1451b8603d2aSLucas Stach 	sdma_channel_synchronize(chan);
14521ec1e82fSSascha Hauer 
14531ec1e82fSSascha Hauer 	sdma_event_disable(sdmac, sdmac->event_id0);
14541ec1e82fSSascha Hauer 	if (sdmac->event_id1)
14551ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
14561ec1e82fSSascha Hauer 
14571ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
14581ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
14591ec1e82fSSascha Hauer 
14601ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
14611ec1e82fSSascha Hauer 
14627560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
14637560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
14641ec1e82fSSascha Hauer }
14651ec1e82fSSascha Hauer 
146621420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
146721420841SRobin Gong 				enum dma_transfer_direction direction, u32 bds)
146821420841SRobin Gong {
146921420841SRobin Gong 	struct sdma_desc *desc;
147021420841SRobin Gong 
1471e8fafa50SRobin Gong 	if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) {
1472e8fafa50SRobin Gong 		dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n");
1473e8fafa50SRobin Gong 		goto err_out;
1474e8fafa50SRobin Gong 	}
1475e8fafa50SRobin Gong 
147621420841SRobin Gong 	desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
147721420841SRobin Gong 	if (!desc)
147821420841SRobin Gong 		goto err_out;
147921420841SRobin Gong 
148021420841SRobin Gong 	sdmac->status = DMA_IN_PROGRESS;
148121420841SRobin Gong 	sdmac->direction = direction;
148221420841SRobin Gong 	sdmac->flags = 0;
148321420841SRobin Gong 
148421420841SRobin Gong 	desc->chn_count = 0;
148521420841SRobin Gong 	desc->chn_real_count = 0;
148621420841SRobin Gong 	desc->buf_tail = 0;
148721420841SRobin Gong 	desc->buf_ptail = 0;
148821420841SRobin Gong 	desc->sdmac = sdmac;
148921420841SRobin Gong 	desc->num_bd = bds;
149021420841SRobin Gong 
149121420841SRobin Gong 	if (sdma_alloc_bd(desc))
149221420841SRobin Gong 		goto err_desc_out;
149321420841SRobin Gong 
14940f06c027SRobin Gong 	/* No slave_config called in MEMCPY case, so do here */
14950f06c027SRobin Gong 	if (direction == DMA_MEM_TO_MEM)
14960f06c027SRobin Gong 		sdma_config_ownership(sdmac, false, true, false);
14970f06c027SRobin Gong 
149821420841SRobin Gong 	if (sdma_load_context(sdmac))
149921420841SRobin Gong 		goto err_desc_out;
150021420841SRobin Gong 
150121420841SRobin Gong 	return desc;
150221420841SRobin Gong 
150321420841SRobin Gong err_desc_out:
150421420841SRobin Gong 	kfree(desc);
150521420841SRobin Gong err_out:
150621420841SRobin Gong 	return NULL;
150721420841SRobin Gong }
150821420841SRobin Gong 
15090f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy(
15100f06c027SRobin Gong 		struct dma_chan *chan, dma_addr_t dma_dst,
15110f06c027SRobin Gong 		dma_addr_t dma_src, size_t len, unsigned long flags)
15120f06c027SRobin Gong {
15130f06c027SRobin Gong 	struct sdma_channel *sdmac = to_sdma_chan(chan);
15140f06c027SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
15150f06c027SRobin Gong 	int channel = sdmac->channel;
15160f06c027SRobin Gong 	size_t count;
15170f06c027SRobin Gong 	int i = 0, param;
15180f06c027SRobin Gong 	struct sdma_buffer_descriptor *bd;
15190f06c027SRobin Gong 	struct sdma_desc *desc;
15200f06c027SRobin Gong 
15210f06c027SRobin Gong 	if (!chan || !len)
15220f06c027SRobin Gong 		return NULL;
15230f06c027SRobin Gong 
15240f06c027SRobin Gong 	dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
15250f06c027SRobin Gong 		&dma_src, &dma_dst, len, channel);
15260f06c027SRobin Gong 
15270f06c027SRobin Gong 	desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
15280f06c027SRobin Gong 					len / SDMA_BD_MAX_CNT + 1);
15290f06c027SRobin Gong 	if (!desc)
15300f06c027SRobin Gong 		return NULL;
15310f06c027SRobin Gong 
15320f06c027SRobin Gong 	do {
15330f06c027SRobin Gong 		count = min_t(size_t, len, SDMA_BD_MAX_CNT);
15340f06c027SRobin Gong 		bd = &desc->bd[i];
15350f06c027SRobin Gong 		bd->buffer_addr = dma_src;
15360f06c027SRobin Gong 		bd->ext_buffer_addr = dma_dst;
15370f06c027SRobin Gong 		bd->mode.count = count;
15380f06c027SRobin Gong 		desc->chn_count += count;
15390f06c027SRobin Gong 		bd->mode.command = 0;
15400f06c027SRobin Gong 
15410f06c027SRobin Gong 		dma_src += count;
15420f06c027SRobin Gong 		dma_dst += count;
15430f06c027SRobin Gong 		len -= count;
15440f06c027SRobin Gong 		i++;
15450f06c027SRobin Gong 
15460f06c027SRobin Gong 		param = BD_DONE | BD_EXTD | BD_CONT;
15470f06c027SRobin Gong 		/* last bd */
15480f06c027SRobin Gong 		if (!len) {
15490f06c027SRobin Gong 			param |= BD_INTR;
15500f06c027SRobin Gong 			param |= BD_LAST;
15510f06c027SRobin Gong 			param &= ~BD_CONT;
15520f06c027SRobin Gong 		}
15530f06c027SRobin Gong 
15540f06c027SRobin Gong 		dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
15550f06c027SRobin Gong 				i, count, bd->buffer_addr,
15560f06c027SRobin Gong 				param & BD_WRAP ? "wrap" : "",
15570f06c027SRobin Gong 				param & BD_INTR ? " intr" : "");
15580f06c027SRobin Gong 
15590f06c027SRobin Gong 		bd->mode.status = param;
15600f06c027SRobin Gong 	} while (len);
15610f06c027SRobin Gong 
15620f06c027SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
15630f06c027SRobin Gong }
15640f06c027SRobin Gong 
15651ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
15661ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1567db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1568185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
15691ec1e82fSSascha Hauer {
15701ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
15711ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1572ad78b000SVinod Koul 	int i, count;
157323889c63SSascha Hauer 	int channel = sdmac->channel;
15741ec1e82fSSascha Hauer 	struct scatterlist *sg;
157557b772b8SRobin Gong 	struct sdma_desc *desc;
15761ec1e82fSSascha Hauer 
1577107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1578107d0644SVinod Koul 
157921420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, sg_len);
158057b772b8SRobin Gong 	if (!desc)
158157b772b8SRobin Gong 		goto err_out;
158257b772b8SRobin Gong 
15831ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
15841ec1e82fSSascha Hauer 			sg_len, channel);
15851ec1e82fSSascha Hauer 
15861ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
158776c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
15881ec1e82fSSascha Hauer 		int param;
15891ec1e82fSSascha Hauer 
1590d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
15911ec1e82fSSascha Hauer 
1592fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
15931ec1e82fSSascha Hauer 
15944a6b2e8aSRobin Gong 		if (count > SDMA_BD_MAX_CNT) {
15951ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
15964a6b2e8aSRobin Gong 					channel, count, SDMA_BD_MAX_CNT);
159757b772b8SRobin Gong 			goto err_bd_out;
15981ec1e82fSSascha Hauer 		}
15991ec1e82fSSascha Hauer 
16001ec1e82fSSascha Hauer 		bd->mode.count = count;
160176c33d27SSascha Hauer 		desc->chn_count += count;
16021ec1e82fSSascha Hauer 
1603ad78b000SVinod Koul 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
160457b772b8SRobin Gong 			goto err_bd_out;
16051fa81c27SSascha Hauer 
16061fa81c27SSascha Hauer 		switch (sdmac->word_size) {
16071fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
16081ec1e82fSSascha Hauer 			bd->mode.command = 0;
16091fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
161057b772b8SRobin Gong 				goto err_bd_out;
16111fa81c27SSascha Hauer 			break;
16121fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
16131fa81c27SSascha Hauer 			bd->mode.command = 2;
16141fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
161557b772b8SRobin Gong 				goto err_bd_out;
16161fa81c27SSascha Hauer 			break;
16171fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
16181fa81c27SSascha Hauer 			bd->mode.command = 1;
16191fa81c27SSascha Hauer 			break;
16201fa81c27SSascha Hauer 		default:
162157b772b8SRobin Gong 			goto err_bd_out;
16221fa81c27SSascha Hauer 		}
16231ec1e82fSSascha Hauer 
16241ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
16251ec1e82fSSascha Hauer 
1626341b9419SShawn Guo 		if (i + 1 == sg_len) {
16271ec1e82fSSascha Hauer 			param |= BD_INTR;
1628341b9419SShawn Guo 			param |= BD_LAST;
1629341b9419SShawn Guo 			param &= ~BD_CONT;
16301ec1e82fSSascha Hauer 		}
16311ec1e82fSSascha Hauer 
1632c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1633c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
16341ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
16351ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
16361ec1e82fSSascha Hauer 
16371ec1e82fSSascha Hauer 		bd->mode.status = param;
16381ec1e82fSSascha Hauer 	}
16391ec1e82fSSascha Hauer 
164057b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
164157b772b8SRobin Gong err_bd_out:
164257b772b8SRobin Gong 	sdma_free_bd(desc);
164357b772b8SRobin Gong 	kfree(desc);
16441ec1e82fSSascha Hauer err_out:
16454b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
16461ec1e82fSSascha Hauer 	return NULL;
16471ec1e82fSSascha Hauer }
16481ec1e82fSSascha Hauer 
16491ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
16501ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1651185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
165231c1e5a1SLaurent Pinchart 		unsigned long flags)
16531ec1e82fSSascha Hauer {
16541ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16551ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
16561ec1e82fSSascha Hauer 	int num_periods = buf_len / period_len;
165723889c63SSascha Hauer 	int channel = sdmac->channel;
165821420841SRobin Gong 	int i = 0, buf = 0;
165957b772b8SRobin Gong 	struct sdma_desc *desc;
16601ec1e82fSSascha Hauer 
16611ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
16621ec1e82fSSascha Hauer 
1663107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1664107d0644SVinod Koul 
166521420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, num_periods);
166657b772b8SRobin Gong 	if (!desc)
166757b772b8SRobin Gong 		goto err_out;
166857b772b8SRobin Gong 
166976c33d27SSascha Hauer 	desc->period_len = period_len;
16708e2e27c7SRichard Zhao 
16711ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
16721ec1e82fSSascha Hauer 
16734a6b2e8aSRobin Gong 	if (period_len > SDMA_BD_MAX_CNT) {
1674ba6ab3b3SArvind Yadav 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
16754a6b2e8aSRobin Gong 				channel, period_len, SDMA_BD_MAX_CNT);
167657b772b8SRobin Gong 		goto err_bd_out;
16771ec1e82fSSascha Hauer 	}
16781ec1e82fSSascha Hauer 
16791ec1e82fSSascha Hauer 	while (buf < buf_len) {
168076c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
16811ec1e82fSSascha Hauer 		int param;
16821ec1e82fSSascha Hauer 
16831ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
16841ec1e82fSSascha Hauer 
16851ec1e82fSSascha Hauer 		bd->mode.count = period_len;
16861ec1e82fSSascha Hauer 
16871ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
168857b772b8SRobin Gong 			goto err_bd_out;
16891ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
16901ec1e82fSSascha Hauer 			bd->mode.command = 0;
16911ec1e82fSSascha Hauer 		else
16921ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
16931ec1e82fSSascha Hauer 
16941ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
16951ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
16961ec1e82fSSascha Hauer 			param |= BD_WRAP;
16971ec1e82fSSascha Hauer 
1698ba6ab3b3SArvind Yadav 		dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1699c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
17001ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
17011ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
17021ec1e82fSSascha Hauer 
17031ec1e82fSSascha Hauer 		bd->mode.status = param;
17041ec1e82fSSascha Hauer 
17051ec1e82fSSascha Hauer 		dma_addr += period_len;
17061ec1e82fSSascha Hauer 		buf += period_len;
17071ec1e82fSSascha Hauer 
17081ec1e82fSSascha Hauer 		i++;
17091ec1e82fSSascha Hauer 	}
17101ec1e82fSSascha Hauer 
171157b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
171257b772b8SRobin Gong err_bd_out:
171357b772b8SRobin Gong 	sdma_free_bd(desc);
171457b772b8SRobin Gong 	kfree(desc);
17151ec1e82fSSascha Hauer err_out:
17161ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
17171ec1e82fSSascha Hauer 	return NULL;
17181ec1e82fSSascha Hauer }
17191ec1e82fSSascha Hauer 
1720107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
1721107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
1722107d0644SVinod Koul 		       enum dma_transfer_direction direction)
17231ec1e82fSSascha Hauer {
17241ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
17251ec1e82fSSascha Hauer 
1726107d0644SVinod Koul 	if (direction == DMA_DEV_TO_MEM) {
17271ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
172894ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
172994ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
17301ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
1731107d0644SVinod Koul 	} else if (direction == DMA_DEV_TO_DEV) {
17328391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
17338391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
17348391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
17358391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
17368391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
17378391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
17388391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
17391ec1e82fSSascha Hauer 	} else {
17401ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
174194ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
174294ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
17431ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
17441ec1e82fSSascha Hauer 	}
1745107d0644SVinod Koul 	sdmac->direction = direction;
17467b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
17471ec1e82fSSascha Hauer }
17481ec1e82fSSascha Hauer 
1749107d0644SVinod Koul static int sdma_config(struct dma_chan *chan,
1750107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg)
1751107d0644SVinod Koul {
1752107d0644SVinod Koul 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1753*824a0a02SSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1754107d0644SVinod Koul 
1755107d0644SVinod Koul 	memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1756107d0644SVinod Koul 
1757*824a0a02SSascha Hauer 	if (dmaengine_cfg->peripheral_config) {
1758*824a0a02SSascha Hauer 		struct sdma_peripheral_config *sdmacfg = dmaengine_cfg->peripheral_config;
1759*824a0a02SSascha Hauer 		if (dmaengine_cfg->peripheral_size != sizeof(struct sdma_peripheral_config)) {
1760*824a0a02SSascha Hauer 			dev_err(sdma->dev, "Invalid peripheral size %zu, expected %zu\n",
1761*824a0a02SSascha Hauer 				dmaengine_cfg->peripheral_size,
1762*824a0a02SSascha Hauer 				sizeof(struct sdma_peripheral_config));
1763*824a0a02SSascha Hauer 			return -EINVAL;
1764*824a0a02SSascha Hauer 		}
1765*824a0a02SSascha Hauer 		sdmac->n_fifos_src = sdmacfg->n_fifos_src;
1766*824a0a02SSascha Hauer 		sdmac->n_fifos_dst = sdmacfg->n_fifos_dst;
1767*824a0a02SSascha Hauer 		sdmac->sw_done = sdmacfg->sw_done;
1768*824a0a02SSascha Hauer 	}
1769*824a0a02SSascha Hauer 
1770107d0644SVinod Koul 	/* Set ENBLn earlier to make sure dma request triggered after that */
1771107d0644SVinod Koul 	if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1772107d0644SVinod Koul 		return -EINVAL;
1773107d0644SVinod Koul 	sdma_event_enable(sdmac, sdmac->event_id0);
1774107d0644SVinod Koul 
1775107d0644SVinod Koul 	if (sdmac->event_id1) {
1776107d0644SVinod Koul 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1777107d0644SVinod Koul 			return -EINVAL;
1778107d0644SVinod Koul 		sdma_event_enable(sdmac, sdmac->event_id1);
1779107d0644SVinod Koul 	}
1780107d0644SVinod Koul 
1781107d0644SVinod Koul 	return 0;
1782107d0644SVinod Koul }
1783107d0644SVinod Koul 
17841ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
17851ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
17861ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
17871ec1e82fSSascha Hauer {
17881ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1789a1ff6a07SSascha Hauer 	struct sdma_desc *desc = NULL;
1790d1a792f3SRussell King - ARM Linux 	u32 residue;
179157b772b8SRobin Gong 	struct virt_dma_desc *vd;
179257b772b8SRobin Gong 	enum dma_status ret;
179357b772b8SRobin Gong 	unsigned long flags;
1794d1a792f3SRussell King - ARM Linux 
179557b772b8SRobin Gong 	ret = dma_cookie_status(chan, cookie, txstate);
179657b772b8SRobin Gong 	if (ret == DMA_COMPLETE || !txstate)
179757b772b8SRobin Gong 		return ret;
179857b772b8SRobin Gong 
179957b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1800a1ff6a07SSascha Hauer 
180157b772b8SRobin Gong 	vd = vchan_find_desc(&sdmac->vc, cookie);
1802a1ff6a07SSascha Hauer 	if (vd)
180357b772b8SRobin Gong 		desc = to_sdma_desc(&vd->tx);
1804a1ff6a07SSascha Hauer 	else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie)
1805a1ff6a07SSascha Hauer 		desc = sdmac->desc;
1806a1ff6a07SSascha Hauer 
1807a1ff6a07SSascha Hauer 	if (desc) {
1808d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
180976c33d27SSascha Hauer 			residue = (desc->num_bd - desc->buf_ptail) *
181076c33d27SSascha Hauer 				desc->period_len - desc->chn_real_count;
1811d1a792f3SRussell King - ARM Linux 		else
181276c33d27SSascha Hauer 			residue = desc->chn_count - desc->chn_real_count;
181357b772b8SRobin Gong 	} else {
181457b772b8SRobin Gong 		residue = 0;
181557b772b8SRobin Gong 	}
1816a1ff6a07SSascha Hauer 
181757b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
18181ec1e82fSSascha Hauer 
1819e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1820d1a792f3SRussell King - ARM Linux 			 residue);
18211ec1e82fSSascha Hauer 
18228a965911SShawn Guo 	return sdmac->status;
18231ec1e82fSSascha Hauer }
18241ec1e82fSSascha Hauer 
18251ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
18261ec1e82fSSascha Hauer {
18272b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
182857b772b8SRobin Gong 	unsigned long flags;
18292b4f130eSSascha Hauer 
183057b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
183157b772b8SRobin Gong 	if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
183257b772b8SRobin Gong 		sdma_start_desc(sdmac);
183357b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
18341ec1e82fSSascha Hauer }
18351ec1e82fSSascha Hauer 
18365b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1837cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1838b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	45
1839b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4	46
18405b28aa31SSascha Hauer 
18415b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
18425b28aa31SSascha Hauer 			     const struct sdma_script_start_addrs *addr)
18435b28aa31SSascha Hauer {
18445b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
18455b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
18465b28aa31SSascha Hauer 	int i;
18475b28aa31SSascha Hauer 
184870dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
184970dabaedSNicolin Chen 	if (!sdma->script_number)
185070dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
185170dabaedSNicolin Chen 
1852bd73dfabSRobin Gong 	if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1853bd73dfabSRobin Gong 				  / sizeof(s32)) {
1854bd73dfabSRobin Gong 		dev_err(sdma->dev,
1855bd73dfabSRobin Gong 			"SDMA script number %d not match with firmware.\n",
1856bd73dfabSRobin Gong 			sdma->script_number);
1857bd73dfabSRobin Gong 		return;
1858bd73dfabSRobin Gong 	}
1859bd73dfabSRobin Gong 
1860cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
18615b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
18625b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
1863b98ce2f4SRobin Gong 
1864b98ce2f4SRobin Gong 	/*
1865b98ce2f4SRobin Gong 	 * get uart_2_mcu_addr/uartsh_2_mcu_addr rom script specially because
1866b98ce2f4SRobin Gong 	 * they are now replaced by uart_2_mcu_ram_addr/uartsh_2_mcu_ram_addr
1867b98ce2f4SRobin Gong 	 * to be compatible with legacy freescale/nxp sdma firmware, and they
1868b98ce2f4SRobin Gong 	 * are located in the bottom part of sdma_script_start_addrs which are
1869b98ce2f4SRobin Gong 	 * beyond the SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1.
1870b98ce2f4SRobin Gong 	 */
1871b98ce2f4SRobin Gong 	if (addr->uart_2_mcu_addr)
1872b98ce2f4SRobin Gong 		sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_addr;
1873b98ce2f4SRobin Gong 	if (addr->uartsh_2_mcu_addr)
1874b98ce2f4SRobin Gong 		sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_addr;
1875b98ce2f4SRobin Gong 
18765b28aa31SSascha Hauer }
18775b28aa31SSascha Hauer 
18787b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
18795b28aa31SSascha Hauer {
18807b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
18815b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
18825b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
18835b28aa31SSascha Hauer 	unsigned short *ram_code;
18845b28aa31SSascha Hauer 
18857b4b88e0SSascha Hauer 	if (!fw) {
18860f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
18870f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
18887b4b88e0SSascha Hauer 		return;
18897b4b88e0SSascha Hauer 	}
18905b28aa31SSascha Hauer 
18915b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
18925b28aa31SSascha Hauer 		goto err_firmware;
18935b28aa31SSascha Hauer 
18945b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
18955b28aa31SSascha Hauer 
18965b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
18975b28aa31SSascha Hauer 		goto err_firmware;
18985b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
18995b28aa31SSascha Hauer 		goto err_firmware;
1900cd72b846SNicolin Chen 	switch (header->version_major) {
1901cd72b846SNicolin Chen 	case 1:
1902cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1903cd72b846SNicolin Chen 		break;
1904cd72b846SNicolin Chen 	case 2:
1905cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1906cd72b846SNicolin Chen 		break;
1907a572460bSFabio Estevam 	case 3:
1908a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1909a572460bSFabio Estevam 		break;
1910b7d2648aSFabio Estevam 	case 4:
1911b7d2648aSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1912b7d2648aSFabio Estevam 		break;
1913cd72b846SNicolin Chen 	default:
1914cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1915cd72b846SNicolin Chen 		goto err_firmware;
1916cd72b846SNicolin Chen 	}
19175b28aa31SSascha Hauer 
19185b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
19195b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
19205b28aa31SSascha Hauer 
19217560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
19227560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
19235b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
19245b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
19255b28aa31SSascha Hauer 			 header->ram_code_size,
19266866fd3bSSascha Hauer 			 addr->ram_code_start_addr);
19277560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
19287560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
19295b28aa31SSascha Hauer 
19305b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
19315b28aa31SSascha Hauer 
1932e8fafa50SRobin Gong 	sdma->fw_loaded = true;
1933e8fafa50SRobin Gong 
19345b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
19355b28aa31SSascha Hauer 		 header->version_major,
19365b28aa31SSascha Hauer 		 header->version_minor);
19375b28aa31SSascha Hauer 
19385b28aa31SSascha Hauer err_firmware:
19395b28aa31SSascha Hauer 	release_firmware(fw);
19407b4b88e0SSascha Hauer }
19417b4b88e0SSascha Hauer 
1942d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1943d078cd1bSZidan Wang 
194429f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1945d078cd1bSZidan Wang {
1946d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1947d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1948d078cd1bSZidan Wang 	struct property *event_remap;
1949d078cd1bSZidan Wang 	struct regmap *gpr;
1950d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
1951d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
1952d078cd1bSZidan Wang 	int ret = 0;
1953d078cd1bSZidan Wang 
1954d078cd1bSZidan Wang 	if (IS_ERR(np) || IS_ERR(gpr_np))
1955d078cd1bSZidan Wang 		goto out;
1956d078cd1bSZidan Wang 
1957d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
1958d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1959d078cd1bSZidan Wang 	if (!num_map) {
1960ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
1961d078cd1bSZidan Wang 		goto out;
1962d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
1963d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
1964d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
1965d078cd1bSZidan Wang 		ret = -EINVAL;
1966d078cd1bSZidan Wang 		goto out;
1967d078cd1bSZidan Wang 	}
1968d078cd1bSZidan Wang 
1969d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
1970d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
1971d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
1972d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
1973d078cd1bSZidan Wang 		goto out;
1974d078cd1bSZidan Wang 	}
1975d078cd1bSZidan Wang 
1976d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1977d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
1978d078cd1bSZidan Wang 		if (ret) {
1979d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1980d078cd1bSZidan Wang 					propname, i);
1981d078cd1bSZidan Wang 			goto out;
1982d078cd1bSZidan Wang 		}
1983d078cd1bSZidan Wang 
1984d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1985d078cd1bSZidan Wang 		if (ret) {
1986d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1987d078cd1bSZidan Wang 					propname, i + 1);
1988d078cd1bSZidan Wang 			goto out;
1989d078cd1bSZidan Wang 		}
1990d078cd1bSZidan Wang 
1991d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
1992d078cd1bSZidan Wang 		if (ret) {
1993d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1994d078cd1bSZidan Wang 					propname, i + 2);
1995d078cd1bSZidan Wang 			goto out;
1996d078cd1bSZidan Wang 		}
1997d078cd1bSZidan Wang 
1998d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1999d078cd1bSZidan Wang 	}
2000d078cd1bSZidan Wang 
2001d078cd1bSZidan Wang out:
2002d078cd1bSZidan Wang 	if (!IS_ERR(gpr_np))
2003d078cd1bSZidan Wang 		of_node_put(gpr_np);
2004d078cd1bSZidan Wang 
2005d078cd1bSZidan Wang 	return ret;
2006d078cd1bSZidan Wang }
2007d078cd1bSZidan Wang 
2008fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
20097b4b88e0SSascha Hauer 		const char *fw_name)
20107b4b88e0SSascha Hauer {
20117b4b88e0SSascha Hauer 	int ret;
20127b4b88e0SSascha Hauer 
20137b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
20140733d839SShawn Guo 			FW_ACTION_UEVENT, fw_name, sdma->dev,
20157b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
20165b28aa31SSascha Hauer 
20175b28aa31SSascha Hauer 	return ret;
20185b28aa31SSascha Hauer }
20195b28aa31SSascha Hauer 
202019bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
20211ec1e82fSSascha Hauer {
20221ec1e82fSSascha Hauer 	int i, ret;
20231ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
20241ec1e82fSSascha Hauer 
2025b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
2026b93edcddSFabio Estevam 	if (ret)
2027b93edcddSFabio Estevam 		return ret;
2028b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
2029b93edcddSFabio Estevam 	if (ret)
2030b93edcddSFabio Estevam 		goto disable_clk_ipg;
20311ec1e82fSSascha Hauer 
2032941acd56SAngus Ainslie (Purism) 	if (sdma->drvdata->check_ratio &&
2033941acd56SAngus Ainslie (Purism) 	    (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg)))
203425aaa75dSAngus Ainslie (Purism) 		sdma->clk_ratio = 1;
203525aaa75dSAngus Ainslie (Purism) 
20361ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
2037c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
20381ec1e82fSSascha Hauer 
2039ceaf5226SAndy Duan 	sdma->channel_control = dma_alloc_coherent(sdma->dev,
20401ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) +
20411ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
20421ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
20431ec1e82fSSascha Hauer 
20441ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
20451ec1e82fSSascha Hauer 		ret = -ENOMEM;
20461ec1e82fSSascha Hauer 		goto err_dma_alloc;
20471ec1e82fSSascha Hauer 	}
20481ec1e82fSSascha Hauer 
20491ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
20501ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
20511ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
20521ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
20531ec1e82fSSascha Hauer 
20541ec1e82fSSascha Hauer 	/* disable all channels */
205517bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
2056c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
20571ec1e82fSSascha Hauer 
20581ec1e82fSSascha Hauer 	/* All channels have priority 0 */
20591ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
2060c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
20611ec1e82fSSascha Hauer 
206257b772b8SRobin Gong 	ret = sdma_request_channel0(sdma);
20631ec1e82fSSascha Hauer 	if (ret)
20641ec1e82fSSascha Hauer 		goto err_dma_alloc;
20651ec1e82fSSascha Hauer 
20661ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
20671ec1e82fSSascha Hauer 
20681ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
2069c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
20701ec1e82fSSascha Hauer 
20711ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
207225aaa75dSAngus Ainslie (Purism) 	if (sdma->clk_ratio)
207325aaa75dSAngus Ainslie (Purism) 		writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG);
207425aaa75dSAngus Ainslie (Purism) 	else
2075c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
20761ec1e82fSSascha Hauer 
2077c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
20781ec1e82fSSascha Hauer 
20791ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
20801ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
20811ec1e82fSSascha Hauer 
20827560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
20837560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
20841ec1e82fSSascha Hauer 
20851ec1e82fSSascha Hauer 	return 0;
20861ec1e82fSSascha Hauer 
20871ec1e82fSSascha Hauer err_dma_alloc:
20887560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
2089b93edcddSFabio Estevam disable_clk_ipg:
2090b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
20911ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
20921ec1e82fSSascha Hauer 	return ret;
20931ec1e82fSSascha Hauer }
20941ec1e82fSSascha Hauer 
20959479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
20969479e17cSShawn Guo {
20970b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
20989479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
20999479e17cSShawn Guo 
21009479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
21019479e17cSShawn Guo 		return false;
21029479e17cSShawn Guo 
21030b351865SNicolin Chen 	sdmac->data = *data;
21040b351865SNicolin Chen 	chan->private = &sdmac->data;
21059479e17cSShawn Guo 
21069479e17cSShawn Guo 	return true;
21079479e17cSShawn Guo }
21089479e17cSShawn Guo 
21099479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
21109479e17cSShawn Guo 				   struct of_dma *ofdma)
21119479e17cSShawn Guo {
21129479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
21139479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
21149479e17cSShawn Guo 	struct imx_dma_data data;
21159479e17cSShawn Guo 
21169479e17cSShawn Guo 	if (dma_spec->args_count != 3)
21179479e17cSShawn Guo 		return NULL;
21189479e17cSShawn Guo 
21199479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
21209479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
21219479e17cSShawn Guo 	data.priority = dma_spec->args[2];
21228391ecf4SShengjiu Wang 	/*
21238391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
21248391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
21258391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
21268391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
21278391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
21288391ecf4SShengjiu Wang 	 */
21298391ecf4SShengjiu Wang 	data.dma_request2 = 0;
21309479e17cSShawn Guo 
2131990c0b53SBaolin Wang 	return __dma_request_channel(&mask, sdma_filter_fn, &data,
2132990c0b53SBaolin Wang 				     ofdma->of_node);
21339479e17cSShawn Guo }
21349479e17cSShawn Guo 
2135e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
21361ec1e82fSSascha Hauer {
2137580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
21388391ecf4SShengjiu Wang 	struct device_node *spba_bus;
2139580975d7SShawn Guo 	const char *fw_name;
21401ec1e82fSSascha Hauer 	int ret;
21411ec1e82fSSascha Hauer 	int irq;
21421ec1e82fSSascha Hauer 	struct resource *iores;
21438391ecf4SShengjiu Wang 	struct resource spba_res;
21441ec1e82fSSascha Hauer 	int i;
21451ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
214636e2f21aSSascha Hauer 	s32 *saddr_arr;
21471ec1e82fSSascha Hauer 
214842536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
214942536b9fSPhilippe Retornaz 	if (ret)
215042536b9fSPhilippe Retornaz 		return ret;
215142536b9fSPhilippe Retornaz 
21527f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
21531ec1e82fSSascha Hauer 	if (!sdma)
21541ec1e82fSSascha Hauer 		return -ENOMEM;
21551ec1e82fSSascha Hauer 
21562ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
215773eab978SSascha Hauer 
21581ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
215932996419SFabio Estevam 	sdma->drvdata = of_device_get_match_data(sdma->dev);
21601ec1e82fSSascha Hauer 
21611ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
21627f24e0eeSFabio Estevam 	if (irq < 0)
216363c72e02SFabio Estevam 		return irq;
21641ec1e82fSSascha Hauer 
21657f24e0eeSFabio Estevam 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
21667f24e0eeSFabio Estevam 	sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
21677f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
21687f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
21691ec1e82fSSascha Hauer 
21707560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
21717f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
21727f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
21731ec1e82fSSascha Hauer 
21747560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
21757f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
21767f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
21777560e3f3SSascha Hauer 
2178fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ipg);
2179fb9caf37SArvind Yadav 	if (ret)
2180fb9caf37SArvind Yadav 		return ret;
2181fb9caf37SArvind Yadav 
2182fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ahb);
2183fb9caf37SArvind Yadav 	if (ret)
2184fb9caf37SArvind Yadav 		goto err_clk;
21857560e3f3SSascha Hauer 
21867f24e0eeSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
21877f24e0eeSFabio Estevam 			       sdma);
21881ec1e82fSSascha Hauer 	if (ret)
2189fb9caf37SArvind Yadav 		goto err_irq;
21901ec1e82fSSascha Hauer 
21915bb9dbb5SVinod Koul 	sdma->irq = irq;
21925bb9dbb5SVinod Koul 
21935b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2194fb9caf37SArvind Yadav 	if (!sdma->script_addrs) {
2195fb9caf37SArvind Yadav 		ret = -ENOMEM;
2196fb9caf37SArvind Yadav 		goto err_irq;
2197fb9caf37SArvind Yadav 	}
21981ec1e82fSSascha Hauer 
219936e2f21aSSascha Hauer 	/* initially no scripts available */
220036e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
2201be4cf718SSascha Hauer 	for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++)
220236e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
220336e2f21aSSascha Hauer 
22047214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
22057214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
22060f06c027SRobin Gong 	dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
22077214a8b1SSascha Hauer 
22081ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
22091ec1e82fSSascha Hauer 	/* Initialize channel parameters */
22101ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
22111ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
22121ec1e82fSSascha Hauer 
22131ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
22141ec1e82fSSascha Hauer 
22151ec1e82fSSascha Hauer 		sdmac->channel = i;
221657b772b8SRobin Gong 		sdmac->vc.desc_free = sdma_desc_free;
22174e2b10beSRobin Gong 		INIT_LIST_HEAD(&sdmac->terminated);
2218b8603d2aSLucas Stach 		INIT_WORK(&sdmac->terminate_worker,
2219b8603d2aSLucas Stach 				sdma_channel_terminate_work);
222023889c63SSascha Hauer 		/*
222123889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
222223889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
222323889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
222423889c63SSascha Hauer 		 */
222523889c63SSascha Hauer 		if (i)
222657b772b8SRobin Gong 			vchan_init(&sdmac->vc, &sdma->dma_device);
22271ec1e82fSSascha Hauer 	}
22281ec1e82fSSascha Hauer 
22295b28aa31SSascha Hauer 	ret = sdma_init(sdma);
22301ec1e82fSSascha Hauer 	if (ret)
22311ec1e82fSSascha Hauer 		goto err_init;
22321ec1e82fSSascha Hauer 
2233d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
2234d078cd1bSZidan Wang 	if (ret)
2235d078cd1bSZidan Wang 		goto err_init;
2236d078cd1bSZidan Wang 
2237dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
2238dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
22395b28aa31SSascha Hauer 
22401ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
22411ec1e82fSSascha Hauer 
22421ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
22431ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
22441ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
22451ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
22461ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
22477b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
2248a80f2787SSascha Hauer 	sdma->dma_device.device_terminate_all = sdma_terminate_all;
2249b8603d2aSLucas Stach 	sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2250f9d4a398SNicolin Chen 	sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2251f9d4a398SNicolin Chen 	sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2252f9d4a398SNicolin Chen 	sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
22536f3125ceSLucas Stach 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
22540f06c027SRobin Gong 	sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
22551ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
2256a3711d49SAngus Ainslie (Purism) 	sdma->dma_device.copy_align = 2;
22574a6b2e8aSRobin Gong 	dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
22581ec1e82fSSascha Hauer 
225923e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
226023e11811SVignesh Raman 
22611ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
22621ec1e82fSSascha Hauer 	if (ret) {
22631ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
22641ec1e82fSSascha Hauer 		goto err_init;
22651ec1e82fSSascha Hauer 	}
22661ec1e82fSSascha Hauer 
22679479e17cSShawn Guo 	if (np) {
22689479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
22699479e17cSShawn Guo 		if (ret) {
22709479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
22719479e17cSShawn Guo 			goto err_register;
22729479e17cSShawn Guo 		}
22738391ecf4SShengjiu Wang 
22748391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
22758391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
22768391ecf4SShengjiu Wang 		if (!ret) {
22778391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
22788391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
22798391ecf4SShengjiu Wang 		}
22808391ecf4SShengjiu Wang 		of_node_put(spba_bus);
22819479e17cSShawn Guo 	}
22829479e17cSShawn Guo 
22832b8066c3SSven Van Asbroeck 	/*
22842b8066c3SSven Van Asbroeck 	 * Because that device tree does not encode ROM script address,
22852b8066c3SSven Van Asbroeck 	 * the RAM script in firmware is mandatory for device tree
22862b8066c3SSven Van Asbroeck 	 * probe, otherwise it fails.
22872b8066c3SSven Van Asbroeck 	 */
22882b8066c3SSven Van Asbroeck 	ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
22892b8066c3SSven Van Asbroeck 				      &fw_name);
22902b8066c3SSven Van Asbroeck 	if (ret) {
22912b8066c3SSven Van Asbroeck 		dev_warn(&pdev->dev, "failed to get firmware name\n");
22922b8066c3SSven Van Asbroeck 	} else {
22932b8066c3SSven Van Asbroeck 		ret = sdma_get_firmware(sdma, fw_name);
22942b8066c3SSven Van Asbroeck 		if (ret)
22952b8066c3SSven Van Asbroeck 			dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
22962b8066c3SSven Van Asbroeck 	}
22972b8066c3SSven Van Asbroeck 
22981ec1e82fSSascha Hauer 	return 0;
22991ec1e82fSSascha Hauer 
23009479e17cSShawn Guo err_register:
23019479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
23021ec1e82fSSascha Hauer err_init:
23031ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
2304fb9caf37SArvind Yadav err_irq:
2305fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2306fb9caf37SArvind Yadav err_clk:
2307fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2308939fd4f0SShawn Guo 	return ret;
23091ec1e82fSSascha Hauer }
23101ec1e82fSSascha Hauer 
23111d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
23121ec1e82fSSascha Hauer {
231323e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
2314c12fe497SVignesh Raman 	int i;
231523e11811SVignesh Raman 
23165bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
231723e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
231823e11811SVignesh Raman 	kfree(sdma->script_addrs);
2319fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2320fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2321c12fe497SVignesh Raman 	/* Kill the tasklet */
2322c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2323c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
2324c12fe497SVignesh Raman 
232557b772b8SRobin Gong 		tasklet_kill(&sdmac->vc.task);
232657b772b8SRobin Gong 		sdma_free_chan_resources(&sdmac->vc.chan);
2327c12fe497SVignesh Raman 	}
232823e11811SVignesh Raman 
232923e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
233023e11811SVignesh Raman 	return 0;
23311ec1e82fSSascha Hauer }
23321ec1e82fSSascha Hauer 
23331ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
23341ec1e82fSSascha Hauer 	.driver		= {
23351ec1e82fSSascha Hauer 		.name	= "imx-sdma",
2336580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
23371ec1e82fSSascha Hauer 	},
23381d1bbd30SMaxin B. John 	.remove		= sdma_remove,
233923e11811SVignesh Raman 	.probe		= sdma_probe,
23401ec1e82fSSascha Hauer };
23411ec1e82fSSascha Hauer 
234223e11811SVignesh Raman module_platform_driver(sdma_driver);
23431ec1e82fSSascha Hauer 
23441ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
23451ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
2346c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2347c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2348c0879342SNicolas Chauvet #endif
2349c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D)
2350c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2351c0879342SNicolas Chauvet #endif
23521ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
2353