xref: /linux/drivers/dma/imx-sdma.c (revision 177360e04b70b1d3937471e42201cdbb831a82da)
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>
170bbc1413SRichard Zhao #include <linux/bitops.h>
181ec1e82fSSascha Hauer #include <linux/mm.h>
191ec1e82fSSascha Hauer #include <linux/interrupt.h>
201ec1e82fSSascha Hauer #include <linux/clk.h>
212ccaef05SRichard Zhao #include <linux/delay.h>
221ec1e82fSSascha Hauer #include <linux/sched.h>
231ec1e82fSSascha Hauer #include <linux/semaphore.h>
241ec1e82fSSascha Hauer #include <linux/spinlock.h>
251ec1e82fSSascha Hauer #include <linux/device.h>
261ec1e82fSSascha Hauer #include <linux/dma-mapping.h>
271ec1e82fSSascha Hauer #include <linux/firmware.h>
281ec1e82fSSascha Hauer #include <linux/slab.h>
291ec1e82fSSascha Hauer #include <linux/platform_device.h>
301ec1e82fSSascha Hauer #include <linux/dmaengine.h>
31580975d7SShawn Guo #include <linux/of.h>
328391ecf4SShengjiu Wang #include <linux/of_address.h>
33580975d7SShawn Guo #include <linux/of_device.h>
349479e17cSShawn Guo #include <linux/of_dma.h>
35b8603d2aSLucas Stach #include <linux/workqueue.h>
361ec1e82fSSascha Hauer 
371ec1e82fSSascha Hauer #include <asm/irq.h>
3882906b13SArnd Bergmann #include <linux/platform_data/dma-imx.h>
39d078cd1bSZidan Wang #include <linux/regmap.h>
40d078cd1bSZidan Wang #include <linux/mfd/syscon.h>
41d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
421ec1e82fSSascha Hauer 
43d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
4457b772b8SRobin Gong #include "virt-dma.h"
45d2ebfb33SRussell King - ARM Linux 
461ec1e82fSSascha Hauer /* SDMA registers */
471ec1e82fSSascha Hauer #define SDMA_H_C0PTR		0x000
481ec1e82fSSascha Hauer #define SDMA_H_INTR		0x004
491ec1e82fSSascha Hauer #define SDMA_H_STATSTOP		0x008
501ec1e82fSSascha Hauer #define SDMA_H_START		0x00c
511ec1e82fSSascha Hauer #define SDMA_H_EVTOVR		0x010
521ec1e82fSSascha Hauer #define SDMA_H_DSPOVR		0x014
531ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR		0x018
541ec1e82fSSascha Hauer #define SDMA_H_EVTPEND		0x01c
551ec1e82fSSascha Hauer #define SDMA_H_DSPENBL		0x020
561ec1e82fSSascha Hauer #define SDMA_H_RESET		0x024
571ec1e82fSSascha Hauer #define SDMA_H_EVTERR		0x028
581ec1e82fSSascha Hauer #define SDMA_H_INTRMSK		0x02c
591ec1e82fSSascha Hauer #define SDMA_H_PSW		0x030
601ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG	0x034
611ec1e82fSSascha Hauer #define SDMA_H_CONFIG		0x038
621ec1e82fSSascha Hauer #define SDMA_ONCE_ENB		0x040
631ec1e82fSSascha Hauer #define SDMA_ONCE_DATA		0x044
641ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR		0x048
651ec1e82fSSascha Hauer #define SDMA_ONCE_STAT		0x04c
661ec1e82fSSascha Hauer #define SDMA_ONCE_CMD		0x050
671ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR		0x054
681ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR	0x058
691ec1e82fSSascha Hauer #define SDMA_CHN0ADDR		0x05c
701ec1e82fSSascha Hauer #define SDMA_ONCE_RTB		0x060
711ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1	0x070
721ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2	0x074
7362550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35	0x200
7462550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31	0x080
751ec1e82fSSascha Hauer #define SDMA_CHNPRI_0		0x100
761ec1e82fSSascha Hauer 
771ec1e82fSSascha Hauer /*
781ec1e82fSSascha Hauer  * Buffer descriptor status values.
791ec1e82fSSascha Hauer  */
801ec1e82fSSascha Hauer #define BD_DONE  0x01
811ec1e82fSSascha Hauer #define BD_WRAP  0x02
821ec1e82fSSascha Hauer #define BD_CONT  0x04
831ec1e82fSSascha Hauer #define BD_INTR  0x08
841ec1e82fSSascha Hauer #define BD_RROR  0x10
851ec1e82fSSascha Hauer #define BD_LAST  0x20
861ec1e82fSSascha Hauer #define BD_EXTD  0x80
871ec1e82fSSascha Hauer 
881ec1e82fSSascha Hauer /*
891ec1e82fSSascha Hauer  * Data Node descriptor status values.
901ec1e82fSSascha Hauer  */
911ec1e82fSSascha Hauer #define DND_END_OF_FRAME  0x80
921ec1e82fSSascha Hauer #define DND_END_OF_XFER   0x40
931ec1e82fSSascha Hauer #define DND_DONE          0x20
941ec1e82fSSascha Hauer #define DND_UNUSED        0x01
951ec1e82fSSascha Hauer 
961ec1e82fSSascha Hauer /*
971ec1e82fSSascha Hauer  * IPCV2 descriptor status values.
981ec1e82fSSascha Hauer  */
991ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME  0x40
1001ec1e82fSSascha Hauer 
1011ec1e82fSSascha Hauer #define IPCV2_MAX_NODES        50
1021ec1e82fSSascha Hauer /*
1031ec1e82fSSascha Hauer  * Error bit set in the CCB status field by the SDMA,
1041ec1e82fSSascha Hauer  * in setbd routine, in case of a transfer error
1051ec1e82fSSascha Hauer  */
1061ec1e82fSSascha Hauer #define DATA_ERROR  0x10000000
1071ec1e82fSSascha Hauer 
1081ec1e82fSSascha Hauer /*
1091ec1e82fSSascha Hauer  * Buffer descriptor commands.
1101ec1e82fSSascha Hauer  */
1111ec1e82fSSascha Hauer #define C0_ADDR             0x01
1121ec1e82fSSascha Hauer #define C0_LOAD             0x02
1131ec1e82fSSascha Hauer #define C0_DUMP             0x03
1141ec1e82fSSascha Hauer #define C0_SETCTX           0x07
1151ec1e82fSSascha Hauer #define C0_GETCTX           0x03
1161ec1e82fSSascha Hauer #define C0_SETDM            0x01
1171ec1e82fSSascha Hauer #define C0_SETPM            0x04
1181ec1e82fSSascha Hauer #define C0_GETDM            0x02
1191ec1e82fSSascha Hauer #define C0_GETPM            0x08
1201ec1e82fSSascha Hauer /*
1211ec1e82fSSascha Hauer  * Change endianness indicator in the BD command field
1221ec1e82fSSascha Hauer  */
1231ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS   0x80
1241ec1e82fSSascha Hauer 
1251ec1e82fSSascha Hauer /*
1268391ecf4SShengjiu Wang  *  p_2_p watermark_level description
1278391ecf4SShengjiu Wang  *	Bits		Name			Description
1288391ecf4SShengjiu Wang  *	0-7		Lower WML		Lower watermark level
1298391ecf4SShengjiu Wang  *	8		PS			1: Pad Swallowing
1308391ecf4SShengjiu Wang  *						0: No Pad Swallowing
1318391ecf4SShengjiu Wang  *	9		PA			1: Pad Adding
1328391ecf4SShengjiu Wang  *						0: No Pad Adding
1338391ecf4SShengjiu Wang  *	10		SPDIF			If this bit is set both source
1348391ecf4SShengjiu Wang  *						and destination are on SPBA
1358391ecf4SShengjiu Wang  *	11		Source Bit(SP)		1: Source on SPBA
1368391ecf4SShengjiu Wang  *						0: Source on AIPS
1378391ecf4SShengjiu Wang  *	12		Destination Bit(DP)	1: Destination on SPBA
1388391ecf4SShengjiu Wang  *						0: Destination on AIPS
1398391ecf4SShengjiu Wang  *	13-15		---------		MUST BE 0
1408391ecf4SShengjiu Wang  *	16-23		Higher WML		HWML
1418391ecf4SShengjiu Wang  *	24-27		N			Total number of samples after
1428391ecf4SShengjiu Wang  *						which Pad adding/Swallowing
1438391ecf4SShengjiu Wang  *						must be done. It must be odd.
1448391ecf4SShengjiu Wang  *	28		Lower WML Event(LWE)	SDMA events reg to check for
1458391ecf4SShengjiu Wang  *						LWML event mask
1468391ecf4SShengjiu Wang  *						0: LWE in EVENTS register
1478391ecf4SShengjiu Wang  *						1: LWE in EVENTS2 register
1488391ecf4SShengjiu Wang  *	29		Higher WML Event(HWE)	SDMA events reg to check for
1498391ecf4SShengjiu Wang  *						HWML event mask
1508391ecf4SShengjiu Wang  *						0: HWE in EVENTS register
1518391ecf4SShengjiu Wang  *						1: HWE in EVENTS2 register
1528391ecf4SShengjiu Wang  *	30		---------		MUST BE 0
1538391ecf4SShengjiu Wang  *	31		CONT			1: Amount of samples to be
1548391ecf4SShengjiu Wang  *						transferred is unknown and
1558391ecf4SShengjiu Wang  *						script will keep on
1568391ecf4SShengjiu Wang  *						transferring samples as long as
1578391ecf4SShengjiu Wang  *						both events are detected and
1588391ecf4SShengjiu Wang  *						script must be manually stopped
1598391ecf4SShengjiu Wang  *						by the application
1608391ecf4SShengjiu Wang  *						0: The amount of samples to be
1618391ecf4SShengjiu Wang  *						transferred is equal to the
1628391ecf4SShengjiu Wang  *						count field of mode word
1638391ecf4SShengjiu Wang  */
1648391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML	0xFF
1658391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS		BIT(8)
1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA		BIT(9)
1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF	BIT(10)
1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP		BIT(11)
1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP		BIT(12)
1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML	(0xFF << 16)
1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE	BIT(28)
1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE	BIT(29)
1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT	BIT(31)
1748391ecf4SShengjiu Wang 
175f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
176f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
177f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
178f9d4a398SNicolin Chen 
179f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS	(BIT(DMA_DEV_TO_MEM) | \
180f9d4a398SNicolin Chen 				 BIT(DMA_MEM_TO_DEV) | \
181f9d4a398SNicolin Chen 				 BIT(DMA_DEV_TO_DEV))
182f9d4a398SNicolin Chen 
1838d11cfb0SVladimir Zapolskiy /**
1848d11cfb0SVladimir Zapolskiy  * struct sdma_script_start_addrs - SDMA script start pointers
1858d11cfb0SVladimir Zapolskiy  *
1868d11cfb0SVladimir Zapolskiy  * start addresses of the different functions in the physical
1878d11cfb0SVladimir Zapolskiy  * address space of the SDMA engine.
1888d11cfb0SVladimir Zapolskiy  */
1898d11cfb0SVladimir Zapolskiy struct sdma_script_start_addrs {
1908d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_addr;
1918d11cfb0SVladimir Zapolskiy 	s32 ap_2_bp_addr;
1928d11cfb0SVladimir Zapolskiy 	s32 ap_2_ap_fixed_addr;
1938d11cfb0SVladimir Zapolskiy 	s32 bp_2_ap_addr;
1948d11cfb0SVladimir Zapolskiy 	s32 loopback_on_dsp_side_addr;
1958d11cfb0SVladimir Zapolskiy 	s32 mcu_interrupt_only_addr;
1968d11cfb0SVladimir Zapolskiy 	s32 firi_2_per_addr;
1978d11cfb0SVladimir Zapolskiy 	s32 firi_2_mcu_addr;
1988d11cfb0SVladimir Zapolskiy 	s32 per_2_firi_addr;
1998d11cfb0SVladimir Zapolskiy 	s32 mcu_2_firi_addr;
2008d11cfb0SVladimir Zapolskiy 	s32 uart_2_per_addr;
201b98ce2f4SRobin Gong 	s32 uart_2_mcu_ram_addr;
2028d11cfb0SVladimir Zapolskiy 	s32 per_2_app_addr;
2038d11cfb0SVladimir Zapolskiy 	s32 mcu_2_app_addr;
2048d11cfb0SVladimir Zapolskiy 	s32 per_2_per_addr;
2058d11cfb0SVladimir Zapolskiy 	s32 uartsh_2_per_addr;
206b98ce2f4SRobin Gong 	s32 uartsh_2_mcu_ram_addr;
2078d11cfb0SVladimir Zapolskiy 	s32 per_2_shp_addr;
2088d11cfb0SVladimir Zapolskiy 	s32 mcu_2_shp_addr;
2098d11cfb0SVladimir Zapolskiy 	s32 ata_2_mcu_addr;
2108d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ata_addr;
2118d11cfb0SVladimir Zapolskiy 	s32 app_2_per_addr;
2128d11cfb0SVladimir Zapolskiy 	s32 app_2_mcu_addr;
2138d11cfb0SVladimir Zapolskiy 	s32 shp_2_per_addr;
2148d11cfb0SVladimir Zapolskiy 	s32 shp_2_mcu_addr;
2158d11cfb0SVladimir Zapolskiy 	s32 mshc_2_mcu_addr;
2168d11cfb0SVladimir Zapolskiy 	s32 mcu_2_mshc_addr;
2178d11cfb0SVladimir Zapolskiy 	s32 spdif_2_mcu_addr;
2188d11cfb0SVladimir Zapolskiy 	s32 mcu_2_spdif_addr;
2198d11cfb0SVladimir Zapolskiy 	s32 asrc_2_mcu_addr;
2208d11cfb0SVladimir Zapolskiy 	s32 ext_mem_2_ipu_addr;
2218d11cfb0SVladimir Zapolskiy 	s32 descrambler_addr;
2228d11cfb0SVladimir Zapolskiy 	s32 dptc_dvfs_addr;
2238d11cfb0SVladimir Zapolskiy 	s32 utra_addr;
2248d11cfb0SVladimir Zapolskiy 	s32 ram_code_start_addr;
2258d11cfb0SVladimir Zapolskiy 	/* End of v1 array */
2268d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ssish_addr;
2278d11cfb0SVladimir Zapolskiy 	s32 ssish_2_mcu_addr;
2288d11cfb0SVladimir Zapolskiy 	s32 hdmi_dma_addr;
2298d11cfb0SVladimir Zapolskiy 	/* End of v2 array */
2308d11cfb0SVladimir Zapolskiy 	s32 zcanfd_2_mcu_addr;
2318d11cfb0SVladimir Zapolskiy 	s32 zqspi_2_mcu_addr;
2328d11cfb0SVladimir Zapolskiy 	s32 mcu_2_ecspi_addr;
233b98ce2f4SRobin Gong 	s32 mcu_2_sai_addr;
234b98ce2f4SRobin Gong 	s32 sai_2_mcu_addr;
235b98ce2f4SRobin Gong 	s32 uart_2_mcu_addr;
236b98ce2f4SRobin Gong 	s32 uartsh_2_mcu_addr;
2378d11cfb0SVladimir Zapolskiy 	/* End of v3 array */
2388d11cfb0SVladimir Zapolskiy 	s32 mcu_2_zqspi_addr;
2398d11cfb0SVladimir Zapolskiy 	/* End of v4 array */
2408d11cfb0SVladimir Zapolskiy };
2418d11cfb0SVladimir Zapolskiy 
2428391ecf4SShengjiu Wang /*
2431ec1e82fSSascha Hauer  * Mode/Count of data node descriptors - IPCv2
2441ec1e82fSSascha Hauer  */
2451ec1e82fSSascha Hauer struct sdma_mode_count {
2464a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT	0xffff
2471ec1e82fSSascha Hauer 	u32 count   : 16; /* size of the buffer pointed by this BD */
2481ec1e82fSSascha Hauer 	u32 status  :  8; /* E,R,I,C,W,D status bits stored here */
249e4b75760SMartin Kaiser 	u32 command :  8; /* command mostly used for channel 0 */
2501ec1e82fSSascha Hauer };
2511ec1e82fSSascha Hauer 
2521ec1e82fSSascha Hauer /*
2531ec1e82fSSascha Hauer  * Buffer descriptor
2541ec1e82fSSascha Hauer  */
2551ec1e82fSSascha Hauer struct sdma_buffer_descriptor {
2561ec1e82fSSascha Hauer 	struct sdma_mode_count  mode;
2571ec1e82fSSascha Hauer 	u32 buffer_addr;	/* address of the buffer described */
2581ec1e82fSSascha Hauer 	u32 ext_buffer_addr;	/* extended buffer address */
2591ec1e82fSSascha Hauer } __attribute__ ((packed));
2601ec1e82fSSascha Hauer 
2611ec1e82fSSascha Hauer /**
2621ec1e82fSSascha Hauer  * struct sdma_channel_control - Channel control Block
2631ec1e82fSSascha Hauer  *
26424ca312dSRobin Gong  * @current_bd_ptr:	current buffer descriptor processed
26524ca312dSRobin Gong  * @base_bd_ptr:	first element of buffer descriptor array
26624ca312dSRobin Gong  * @unused:		padding. The SDMA engine expects an array of 128 byte
2671ec1e82fSSascha Hauer  *			control blocks
2681ec1e82fSSascha Hauer  */
2691ec1e82fSSascha Hauer struct sdma_channel_control {
2701ec1e82fSSascha Hauer 	u32 current_bd_ptr;
2711ec1e82fSSascha Hauer 	u32 base_bd_ptr;
2721ec1e82fSSascha Hauer 	u32 unused[2];
2731ec1e82fSSascha Hauer } __attribute__ ((packed));
2741ec1e82fSSascha Hauer 
2751ec1e82fSSascha Hauer /**
2761ec1e82fSSascha Hauer  * struct sdma_state_registers - SDMA context for a channel
2771ec1e82fSSascha Hauer  *
2781ec1e82fSSascha Hauer  * @pc:		program counter
27924ca312dSRobin Gong  * @unused1:	unused
2801ec1e82fSSascha Hauer  * @t:		test bit: status of arithmetic & test instruction
2811ec1e82fSSascha Hauer  * @rpc:	return program counter
28224ca312dSRobin Gong  * @unused0:	unused
2831ec1e82fSSascha Hauer  * @sf:		source fault while loading data
2841ec1e82fSSascha Hauer  * @spc:	loop start program counter
28524ca312dSRobin Gong  * @unused2:	unused
2861ec1e82fSSascha Hauer  * @df:		destination fault while storing data
2871ec1e82fSSascha Hauer  * @epc:	loop end program counter
2881ec1e82fSSascha Hauer  * @lm:		loop mode
2891ec1e82fSSascha Hauer  */
2901ec1e82fSSascha Hauer struct sdma_state_registers {
2911ec1e82fSSascha Hauer 	u32 pc     :14;
2921ec1e82fSSascha Hauer 	u32 unused1: 1;
2931ec1e82fSSascha Hauer 	u32 t      : 1;
2941ec1e82fSSascha Hauer 	u32 rpc    :14;
2951ec1e82fSSascha Hauer 	u32 unused0: 1;
2961ec1e82fSSascha Hauer 	u32 sf     : 1;
2971ec1e82fSSascha Hauer 	u32 spc    :14;
2981ec1e82fSSascha Hauer 	u32 unused2: 1;
2991ec1e82fSSascha Hauer 	u32 df     : 1;
3001ec1e82fSSascha Hauer 	u32 epc    :14;
3011ec1e82fSSascha Hauer 	u32 lm     : 2;
3021ec1e82fSSascha Hauer } __attribute__ ((packed));
3031ec1e82fSSascha Hauer 
3041ec1e82fSSascha Hauer /**
3051ec1e82fSSascha Hauer  * struct sdma_context_data - sdma context specific to a channel
3061ec1e82fSSascha Hauer  *
3071ec1e82fSSascha Hauer  * @channel_state:	channel state bits
3081ec1e82fSSascha Hauer  * @gReg:		general registers
3091ec1e82fSSascha Hauer  * @mda:		burst dma destination address register
3101ec1e82fSSascha Hauer  * @msa:		burst dma source address register
3111ec1e82fSSascha Hauer  * @ms:			burst dma status register
3121ec1e82fSSascha Hauer  * @md:			burst dma data register
3131ec1e82fSSascha Hauer  * @pda:		peripheral dma destination address register
3141ec1e82fSSascha Hauer  * @psa:		peripheral dma source address register
3151ec1e82fSSascha Hauer  * @ps:			peripheral dma status register
3161ec1e82fSSascha Hauer  * @pd:			peripheral dma data register
3171ec1e82fSSascha Hauer  * @ca:			CRC polynomial register
3181ec1e82fSSascha Hauer  * @cs:			CRC accumulator register
3191ec1e82fSSascha Hauer  * @dda:		dedicated core destination address register
3201ec1e82fSSascha Hauer  * @dsa:		dedicated core source address register
3211ec1e82fSSascha Hauer  * @ds:			dedicated core status register
3221ec1e82fSSascha Hauer  * @dd:			dedicated core data register
32324ca312dSRobin Gong  * @scratch0:		1st word of dedicated ram for context switch
32424ca312dSRobin Gong  * @scratch1:		2nd word of dedicated ram for context switch
32524ca312dSRobin Gong  * @scratch2:		3rd word of dedicated ram for context switch
32624ca312dSRobin Gong  * @scratch3:		4th word of dedicated ram for context switch
32724ca312dSRobin Gong  * @scratch4:		5th word of dedicated ram for context switch
32824ca312dSRobin Gong  * @scratch5:		6th word of dedicated ram for context switch
32924ca312dSRobin Gong  * @scratch6:		7th word of dedicated ram for context switch
33024ca312dSRobin Gong  * @scratch7:		8th word of dedicated ram for context switch
3311ec1e82fSSascha Hauer  */
3321ec1e82fSSascha Hauer struct sdma_context_data {
3331ec1e82fSSascha Hauer 	struct sdma_state_registers  channel_state;
3341ec1e82fSSascha Hauer 	u32  gReg[8];
3351ec1e82fSSascha Hauer 	u32  mda;
3361ec1e82fSSascha Hauer 	u32  msa;
3371ec1e82fSSascha Hauer 	u32  ms;
3381ec1e82fSSascha Hauer 	u32  md;
3391ec1e82fSSascha Hauer 	u32  pda;
3401ec1e82fSSascha Hauer 	u32  psa;
3411ec1e82fSSascha Hauer 	u32  ps;
3421ec1e82fSSascha Hauer 	u32  pd;
3431ec1e82fSSascha Hauer 	u32  ca;
3441ec1e82fSSascha Hauer 	u32  cs;
3451ec1e82fSSascha Hauer 	u32  dda;
3461ec1e82fSSascha Hauer 	u32  dsa;
3471ec1e82fSSascha Hauer 	u32  ds;
3481ec1e82fSSascha Hauer 	u32  dd;
3491ec1e82fSSascha Hauer 	u32  scratch0;
3501ec1e82fSSascha Hauer 	u32  scratch1;
3511ec1e82fSSascha Hauer 	u32  scratch2;
3521ec1e82fSSascha Hauer 	u32  scratch3;
3531ec1e82fSSascha Hauer 	u32  scratch4;
3541ec1e82fSSascha Hauer 	u32  scratch5;
3551ec1e82fSSascha Hauer 	u32  scratch6;
3561ec1e82fSSascha Hauer 	u32  scratch7;
3571ec1e82fSSascha Hauer } __attribute__ ((packed));
3581ec1e82fSSascha Hauer 
3591ec1e82fSSascha Hauer 
3601ec1e82fSSascha Hauer struct sdma_engine;
3611ec1e82fSSascha Hauer 
3621ec1e82fSSascha Hauer /**
36376c33d27SSascha Hauer  * struct sdma_desc - descriptor structor for one transfer
36424ca312dSRobin Gong  * @vd:			descriptor for virt dma
36524ca312dSRobin Gong  * @num_bd:		number of descriptors currently handling
36624ca312dSRobin Gong  * @bd_phys:		physical address of bd
36724ca312dSRobin Gong  * @buf_tail:		ID of the buffer that was processed
36824ca312dSRobin Gong  * @buf_ptail:		ID of the previous buffer that was processed
36924ca312dSRobin Gong  * @period_len:		period length, used in cyclic.
37024ca312dSRobin Gong  * @chn_real_count:	the real count updated from bd->mode.count
37124ca312dSRobin Gong  * @chn_count:		the transfer count set
37224ca312dSRobin Gong  * @sdmac:		sdma_channel pointer
37324ca312dSRobin Gong  * @bd:			pointer of allocate bd
37476c33d27SSascha Hauer  */
37576c33d27SSascha Hauer struct sdma_desc {
37657b772b8SRobin Gong 	struct virt_dma_desc	vd;
37776c33d27SSascha Hauer 	unsigned int		num_bd;
37876c33d27SSascha Hauer 	dma_addr_t		bd_phys;
37976c33d27SSascha Hauer 	unsigned int		buf_tail;
38076c33d27SSascha Hauer 	unsigned int		buf_ptail;
38176c33d27SSascha Hauer 	unsigned int		period_len;
38276c33d27SSascha Hauer 	unsigned int		chn_real_count;
38376c33d27SSascha Hauer 	unsigned int		chn_count;
38476c33d27SSascha Hauer 	struct sdma_channel	*sdmac;
38576c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd;
38676c33d27SSascha Hauer };
38776c33d27SSascha Hauer 
38876c33d27SSascha Hauer /**
3891ec1e82fSSascha Hauer  * struct sdma_channel - housekeeping for a SDMA channel
3901ec1e82fSSascha Hauer  *
39124ca312dSRobin Gong  * @vc:			virt_dma base structure
39224ca312dSRobin Gong  * @desc:		sdma description including vd and other special member
39324ca312dSRobin Gong  * @sdma:		pointer to the SDMA engine for this channel
39424ca312dSRobin Gong  * @channel:		the channel number, matches dmaengine chan_id + 1
39524ca312dSRobin Gong  * @direction:		transfer type. Needed for setting SDMA script
396d0c4a149SLee Jones  * @slave_config:	Slave configuration
39724ca312dSRobin Gong  * @peripheral_type:	Peripheral type. Needed for setting SDMA script
39824ca312dSRobin Gong  * @event_id0:		aka dma request line
39924ca312dSRobin Gong  * @event_id1:		for channels that use 2 events
40024ca312dSRobin Gong  * @word_size:		peripheral access size
40124ca312dSRobin Gong  * @pc_from_device:	script address for those device_2_memory
40224ca312dSRobin Gong  * @pc_to_device:	script address for those memory_2_device
40324ca312dSRobin Gong  * @device_to_device:	script address for those device_2_device
4040f06c027SRobin Gong  * @pc_to_pc:		script address for those memory_2_memory
40524ca312dSRobin Gong  * @flags:		loop mode or not
40624ca312dSRobin Gong  * @per_address:	peripheral source or destination address in common case
40724ca312dSRobin Gong  *                      destination address in p_2_p case
40824ca312dSRobin Gong  * @per_address2:	peripheral source address in p_2_p case
40924ca312dSRobin Gong  * @event_mask:		event mask used in p_2_p script
41024ca312dSRobin Gong  * @watermark_level:	value for gReg[7], some script will extend it from
41124ca312dSRobin Gong  *			basic watermark such as p_2_p
41224ca312dSRobin Gong  * @shp_addr:		value for gReg[6]
41324ca312dSRobin Gong  * @per_addr:		value for gReg[2]
41424ca312dSRobin Gong  * @status:		status of dma channel
415d0c4a149SLee Jones  * @context_loaded:	ensure context is only loaded once
41624ca312dSRobin Gong  * @data:		specific sdma interface structure
41724ca312dSRobin Gong  * @bd_pool:		dma_pool for bd
418d0c4a149SLee Jones  * @terminate_worker:	used to call back into terminate work function
4191ec1e82fSSascha Hauer  */
4201ec1e82fSSascha Hauer struct sdma_channel {
42157b772b8SRobin Gong 	struct virt_dma_chan		vc;
42276c33d27SSascha Hauer 	struct sdma_desc		*desc;
4231ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
4241ec1e82fSSascha Hauer 	unsigned int			channel;
425db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
426107d0644SVinod Koul 	struct dma_slave_config		slave_config;
4271ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
4281ec1e82fSSascha Hauer 	unsigned int			event_id0;
4291ec1e82fSSascha Hauer 	unsigned int			event_id1;
4301ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
4311ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
4328391ecf4SShengjiu Wang 	unsigned int			device_to_device;
4330f06c027SRobin Gong 	unsigned int                    pc_to_pc;
4341ec1e82fSSascha Hauer 	unsigned long			flags;
4358391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
4360bbc1413SRichard Zhao 	unsigned long			event_mask[2];
4370bbc1413SRichard Zhao 	unsigned long			watermark_level;
4381ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
4391ec1e82fSSascha Hauer 	enum dma_status			status;
4400b351865SNicolin Chen 	struct imx_dma_data		data;
441b8603d2aSLucas Stach 	struct work_struct		terminate_worker;
4424e2b10beSRobin Gong 	struct list_head                terminated;
443e8fafa50SRobin Gong 	bool				is_ram_script;
4441ec1e82fSSascha Hauer };
4451ec1e82fSSascha Hauer 
4460bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
4471ec1e82fSSascha Hauer 
4481ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
4491ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
4501ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
4511ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
4521ec1e82fSSascha Hauer 
4531ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
4541ec1e82fSSascha Hauer 
4551ec1e82fSSascha Hauer /**
4561ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
4571ec1e82fSSascha Hauer  *
45824ca312dSRobin Gong  * @magic:		"SDMA"
45924ca312dSRobin Gong  * @version_major:	increased whenever layout of struct
46024ca312dSRobin Gong  *			sdma_script_start_addrs changes.
46124ca312dSRobin Gong  * @version_minor:	firmware minor version (for binary compatible changes)
46224ca312dSRobin Gong  * @script_addrs_start:	offset of struct sdma_script_start_addrs in this image
46324ca312dSRobin Gong  * @num_script_addrs:	Number of script addresses in this image
46424ca312dSRobin Gong  * @ram_code_start:	offset of SDMA ram image in this firmware image
46524ca312dSRobin Gong  * @ram_code_size:	size of SDMA ram image
46624ca312dSRobin Gong  * @script_addrs:	Stores the start address of the SDMA scripts
4671ec1e82fSSascha Hauer  *			(in SDMA memory space)
4681ec1e82fSSascha Hauer  */
4691ec1e82fSSascha Hauer struct sdma_firmware_header {
4701ec1e82fSSascha Hauer 	u32	magic;
4711ec1e82fSSascha Hauer 	u32	version_major;
4721ec1e82fSSascha Hauer 	u32	version_minor;
4731ec1e82fSSascha Hauer 	u32	script_addrs_start;
4741ec1e82fSSascha Hauer 	u32	num_script_addrs;
4751ec1e82fSSascha Hauer 	u32	ram_code_start;
4761ec1e82fSSascha Hauer 	u32	ram_code_size;
4771ec1e82fSSascha Hauer };
4781ec1e82fSSascha Hauer 
47917bba72fSSascha Hauer struct sdma_driver_data {
48017bba72fSSascha Hauer 	int chnenbl0;
48117bba72fSSascha Hauer 	int num_events;
482dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
483941acd56SAngus Ainslie (Purism) 	bool check_ratio;
4844852e9a2SRobin Gong 	/*
4854852e9a2SRobin Gong 	 * ecspi ERR009165 fixed should be done in sdma script
4864852e9a2SRobin Gong 	 * and it has been fixed in soc from i.mx6ul.
4874852e9a2SRobin Gong 	 * please get more information from the below link:
4884852e9a2SRobin Gong 	 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
4894852e9a2SRobin Gong 	 */
4904852e9a2SRobin Gong 	bool ecspi_fixed;
49162550cd7SShawn Guo };
49262550cd7SShawn Guo 
4931ec1e82fSSascha Hauer struct sdma_engine {
4941ec1e82fSSascha Hauer 	struct device			*dev;
4951ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
4961ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
4971ec1e82fSSascha Hauer 	void __iomem			*regs;
4981ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
4991ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
5001ec1e82fSSascha Hauer 	struct dma_device		dma_device;
5017560e3f3SSascha Hauer 	struct clk			*clk_ipg;
5027560e3f3SSascha Hauer 	struct clk			*clk_ahb;
5032ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
504cd72b846SNicolin Chen 	u32				script_number;
5051ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
50617bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
5078391ecf4SShengjiu Wang 	u32				spba_start_addr;
5088391ecf4SShengjiu Wang 	u32				spba_end_addr;
5095bb9dbb5SVinod Koul 	unsigned int			irq;
51076c33d27SSascha Hauer 	dma_addr_t			bd0_phys;
51176c33d27SSascha Hauer 	struct sdma_buffer_descriptor	*bd0;
51225aaa75dSAngus Ainslie (Purism) 	/* clock ratio for AHB:SDMA core. 1:1 is 1, 2:1 is 0*/
51325aaa75dSAngus Ainslie (Purism) 	bool				clk_ratio;
514e8fafa50SRobin Gong 	bool                            fw_loaded;
51517bba72fSSascha Hauer };
51617bba72fSSascha Hauer 
517107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
518107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
519107d0644SVinod Koul 		       enum dma_transfer_direction direction);
520107d0644SVinod Koul 
521e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
52217bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
52317bba72fSSascha Hauer 	.num_events = 32,
52417bba72fSSascha Hauer };
52517bba72fSSascha Hauer 
526dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
527dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
528dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
529dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
530dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
531dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
532dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
533dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
534dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
535dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
536dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
537dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
538dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
539dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
540dcfec3c0SSascha Hauer };
541dcfec3c0SSascha Hauer 
542e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
543dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
544dcfec3c0SSascha Hauer 	.num_events = 48,
545dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
546dcfec3c0SSascha Hauer };
547dcfec3c0SSascha Hauer 
548e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
54917bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
55017bba72fSSascha Hauer 	.num_events = 48,
5511ec1e82fSSascha Hauer };
5521ec1e82fSSascha Hauer 
553dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
554dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
555dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
556dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
557dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
558dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
559dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
560dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
561dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
562dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
563dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
564dcfec3c0SSascha Hauer };
565dcfec3c0SSascha Hauer 
566e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
567dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
568dcfec3c0SSascha Hauer 	.num_events = 48,
569dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
570dcfec3c0SSascha Hauer };
571dcfec3c0SSascha Hauer 
572dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
573dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
574dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
575dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
576dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
577dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
578dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
579dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
580dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
581dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
582dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
583dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
584dcfec3c0SSascha Hauer };
585dcfec3c0SSascha Hauer 
586e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
587dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
588dcfec3c0SSascha Hauer 	.num_events = 48,
589dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
590dcfec3c0SSascha Hauer };
591dcfec3c0SSascha Hauer 
592dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
593dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
594dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
595dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
596dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
597dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
598dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
599dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
600dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
601dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
602dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
603dcfec3c0SSascha Hauer };
604dcfec3c0SSascha Hauer 
605e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
606dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
607dcfec3c0SSascha Hauer 	.num_events = 48,
608dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
609dcfec3c0SSascha Hauer };
610dcfec3c0SSascha Hauer 
6114852e9a2SRobin Gong static struct sdma_driver_data sdma_imx6ul = {
6124852e9a2SRobin Gong 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
6134852e9a2SRobin Gong 	.num_events = 48,
6144852e9a2SRobin Gong 	.script_addrs = &sdma_script_imx6q,
6154852e9a2SRobin Gong 	.ecspi_fixed = true,
6164852e9a2SRobin Gong };
6174852e9a2SRobin Gong 
618b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = {
619b7d2648aSFabio Estevam 	.ap_2_ap_addr = 644,
620b7d2648aSFabio Estevam 	.uart_2_mcu_addr = 819,
621b7d2648aSFabio Estevam 	.mcu_2_app_addr = 749,
622b7d2648aSFabio Estevam 	.uartsh_2_mcu_addr = 1034,
623b7d2648aSFabio Estevam 	.mcu_2_shp_addr = 962,
624b7d2648aSFabio Estevam 	.app_2_mcu_addr = 685,
625b7d2648aSFabio Estevam 	.shp_2_mcu_addr = 893,
626b7d2648aSFabio Estevam 	.spdif_2_mcu_addr = 1102,
627b7d2648aSFabio Estevam 	.mcu_2_spdif_addr = 1136,
628b7d2648aSFabio Estevam };
629b7d2648aSFabio Estevam 
630b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = {
631b7d2648aSFabio Estevam 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
632b7d2648aSFabio Estevam 	.num_events = 48,
633b7d2648aSFabio Estevam 	.script_addrs = &sdma_script_imx7d,
634b7d2648aSFabio Estevam };
635b7d2648aSFabio Estevam 
636941acd56SAngus Ainslie (Purism) static struct sdma_driver_data sdma_imx8mq = {
637941acd56SAngus Ainslie (Purism) 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
638941acd56SAngus Ainslie (Purism) 	.num_events = 48,
639941acd56SAngus Ainslie (Purism) 	.script_addrs = &sdma_script_imx7d,
640941acd56SAngus Ainslie (Purism) 	.check_ratio = 1,
641941acd56SAngus Ainslie (Purism) };
642941acd56SAngus Ainslie (Purism) 
643580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
644dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
645dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
646dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
64717bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
648dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
64963edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
650b7d2648aSFabio Estevam 	{ .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
6514852e9a2SRobin Gong 	{ .compatible = "fsl,imx6ul-sdma", .data = &sdma_imx6ul, },
652941acd56SAngus Ainslie (Purism) 	{ .compatible = "fsl,imx8mq-sdma", .data = &sdma_imx8mq, },
653580975d7SShawn Guo 	{ /* sentinel */ }
654580975d7SShawn Guo };
655580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
656580975d7SShawn Guo 
6570bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
6580bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
6590bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
6601ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
6611ec1e82fSSascha Hauer 
6621ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
6631ec1e82fSSascha Hauer {
66417bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
6651ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
6661ec1e82fSSascha Hauer }
6671ec1e82fSSascha Hauer 
6681ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
6691ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
6701ec1e82fSSascha Hauer {
6711ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6721ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6730bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
6741ec1e82fSSascha Hauer 
6751ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
6761ec1e82fSSascha Hauer 		return -EINVAL;
6771ec1e82fSSascha Hauer 
678c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
679c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
680c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
6811ec1e82fSSascha Hauer 
6821ec1e82fSSascha Hauer 	if (dsp_override)
6830bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
6841ec1e82fSSascha Hauer 	else
6850bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
6861ec1e82fSSascha Hauer 
6871ec1e82fSSascha Hauer 	if (event_override)
6880bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
6891ec1e82fSSascha Hauer 	else
6900bbc1413SRichard Zhao 		__set_bit(channel, &evt);
6911ec1e82fSSascha Hauer 
6921ec1e82fSSascha Hauer 	if (mcu_override)
6930bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
6941ec1e82fSSascha Hauer 	else
6950bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
6961ec1e82fSSascha Hauer 
697c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
698c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
699c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
7001ec1e82fSSascha Hauer 
7011ec1e82fSSascha Hauer 	return 0;
7021ec1e82fSSascha Hauer }
7031ec1e82fSSascha Hauer 
7045b215c28STomasz Moń static int is_sdma_channel_enabled(struct sdma_engine *sdma, int channel)
7055b215c28STomasz Moń {
7065b215c28STomasz Moń 	return !!(readl(sdma->regs + SDMA_H_STATSTOP) & BIT(channel));
7075b215c28STomasz Moń }
7085b215c28STomasz Moń 
709b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
710b9a59166SRichard Zhao {
7110bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
712b9a59166SRichard Zhao }
713b9a59166SRichard Zhao 
7141ec1e82fSSascha Hauer /*
7152ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
7161ec1e82fSSascha Hauer  */
7172ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
7181ec1e82fSSascha Hauer {
7191ec1e82fSSascha Hauer 	int ret;
7201d069bfaSMichael Olbrich 	u32 reg;
7211ec1e82fSSascha Hauer 
7222ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
7231ec1e82fSSascha Hauer 
7241d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
7251d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
7261d069bfaSMichael Olbrich 	if (ret)
7272ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
7281ec1e82fSSascha Hauer 
729855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
73025aaa75dSAngus Ainslie (Purism) 	reg = readl(sdma->regs + SDMA_H_CONFIG);
73125aaa75dSAngus Ainslie (Purism) 	if ((reg & SDMA_H_CONFIG_CSM) == 0) {
73225aaa75dSAngus Ainslie (Purism) 		reg |= SDMA_H_CONFIG_CSM;
73325aaa75dSAngus Ainslie (Purism) 		writel_relaxed(reg, sdma->regs + SDMA_H_CONFIG);
73425aaa75dSAngus Ainslie (Purism) 	}
735855832e4SRobin Gong 
7361d069bfaSMichael Olbrich 	return ret;
7371ec1e82fSSascha Hauer }
7381ec1e82fSSascha Hauer 
7391ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
7401ec1e82fSSascha Hauer 		u32 address)
7411ec1e82fSSascha Hauer {
74276c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
7431ec1e82fSSascha Hauer 	void *buf_virt;
7441ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
7451ec1e82fSSascha Hauer 	int ret;
7462ccaef05SRichard Zhao 	unsigned long flags;
74773eab978SSascha Hauer 
748ceaf5226SAndy Duan 	buf_virt = dma_alloc_coherent(sdma->dev, size, &buf_phys, GFP_KERNEL);
749ef6c1dadSFlavio Suligoi 	if (!buf_virt)
7502ccaef05SRichard Zhao 		return -ENOMEM;
7511ec1e82fSSascha Hauer 
7522ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
7532ccaef05SRichard Zhao 
7541ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
7553f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
7561ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
7571ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
7581ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
7591ec1e82fSSascha Hauer 
7601ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
7611ec1e82fSSascha Hauer 
7622ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
7632ccaef05SRichard Zhao 
7642ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
7651ec1e82fSSascha Hauer 
766ceaf5226SAndy Duan 	dma_free_coherent(sdma->dev, size, buf_virt, buf_phys);
7671ec1e82fSSascha Hauer 
7681ec1e82fSSascha Hauer 	return ret;
7691ec1e82fSSascha Hauer }
7701ec1e82fSSascha Hauer 
7711ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
7721ec1e82fSSascha Hauer {
7731ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7741ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7750bbc1413SRichard Zhao 	unsigned long val;
7761ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
7771ec1e82fSSascha Hauer 
778c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
7790bbc1413SRichard Zhao 	__set_bit(channel, &val);
780c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
7811ec1e82fSSascha Hauer }
7821ec1e82fSSascha Hauer 
7831ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
7841ec1e82fSSascha Hauer {
7851ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7861ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7871ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
7880bbc1413SRichard Zhao 	unsigned long val;
7891ec1e82fSSascha Hauer 
790c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
7910bbc1413SRichard Zhao 	__clear_bit(channel, &val);
792c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
7931ec1e82fSSascha Hauer }
7941ec1e82fSSascha Hauer 
79557b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
79657b772b8SRobin Gong {
79757b772b8SRobin Gong 	return container_of(t, struct sdma_desc, vd.tx);
79857b772b8SRobin Gong }
79957b772b8SRobin Gong 
80057b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac)
80157b772b8SRobin Gong {
80257b772b8SRobin Gong 	struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
80357b772b8SRobin Gong 	struct sdma_desc *desc;
80457b772b8SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
80557b772b8SRobin Gong 	int channel = sdmac->channel;
80657b772b8SRobin Gong 
80757b772b8SRobin Gong 	if (!vd) {
80857b772b8SRobin Gong 		sdmac->desc = NULL;
80957b772b8SRobin Gong 		return;
81057b772b8SRobin Gong 	}
81157b772b8SRobin Gong 	sdmac->desc = desc = to_sdma_desc(&vd->tx);
81202939cd1SSascha Hauer 
81357b772b8SRobin Gong 	list_del(&vd->node);
81457b772b8SRobin Gong 
81557b772b8SRobin Gong 	sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
81657b772b8SRobin Gong 	sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
81757b772b8SRobin Gong 	sdma_enable_channel(sdma, sdmac->channel);
81857b772b8SRobin Gong }
81957b772b8SRobin Gong 
820d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
821d1a792f3SRussell King - ARM Linux {
8221ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
8235881826dSNandor Han 	int error = 0;
8245881826dSNandor Han 	enum dma_status	old_status = sdmac->status;
8251ec1e82fSSascha Hauer 
8261ec1e82fSSascha Hauer 	/*
8271ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
8281ec1e82fSSascha Hauer 	 * call callback function.
8291ec1e82fSSascha Hauer 	 */
83057b772b8SRobin Gong 	while (sdmac->desc) {
83176c33d27SSascha Hauer 		struct sdma_desc *desc = sdmac->desc;
83276c33d27SSascha Hauer 
83376c33d27SSascha Hauer 		bd = &desc->bd[desc->buf_tail];
8341ec1e82fSSascha Hauer 
8351ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
8361ec1e82fSSascha Hauer 			break;
8371ec1e82fSSascha Hauer 
8385881826dSNandor Han 		if (bd->mode.status & BD_RROR) {
8395881826dSNandor Han 			bd->mode.status &= ~BD_RROR;
8401ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
8415881826dSNandor Han 			error = -EIO;
8425881826dSNandor Han 		}
8431ec1e82fSSascha Hauer 
8445881826dSNandor Han 	       /*
8455881826dSNandor Han 		* We use bd->mode.count to calculate the residue, since contains
8465881826dSNandor Han 		* the number of bytes present in the current buffer descriptor.
8475881826dSNandor Han 		*/
8485881826dSNandor Han 
84976c33d27SSascha Hauer 		desc->chn_real_count = bd->mode.count;
85076c33d27SSascha Hauer 		bd->mode.count = desc->period_len;
85176c33d27SSascha Hauer 		desc->buf_ptail = desc->buf_tail;
85276c33d27SSascha Hauer 		desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
85315f30f51SNandor Han 
85415f30f51SNandor Han 		/*
85515f30f51SNandor Han 		 * The callback is called from the interrupt context in order
85615f30f51SNandor Han 		 * to reduce latency and to avoid the risk of altering the
85715f30f51SNandor Han 		 * SDMA transaction status by the time the client tasklet is
85815f30f51SNandor Han 		 * executed.
85915f30f51SNandor Han 		 */
86057b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
86157b772b8SRobin Gong 		dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
86257b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
86315f30f51SNandor Han 
864*177360e0STomasz Moń 		/* Assign buffer ownership to SDMA */
865*177360e0STomasz Moń 		bd->mode.status |= BD_DONE;
866*177360e0STomasz Moń 
8675881826dSNandor Han 		if (error)
8685881826dSNandor Han 			sdmac->status = old_status;
8691ec1e82fSSascha Hauer 	}
8705b215c28STomasz Moń 
8715b215c28STomasz Moń 	/*
8725b215c28STomasz Moń 	 * SDMA stops cyclic channel when DMA request triggers a channel and no SDMA
8735b215c28STomasz Moń 	 * owned buffer is available (i.e. BD_DONE was set too late).
8745b215c28STomasz Moń 	 */
8755b215c28STomasz Moń 	if (!is_sdma_channel_enabled(sdmac->sdma, sdmac->channel)) {
8765b215c28STomasz Moń 		dev_warn(sdmac->sdma->dev, "restart cyclic channel %d\n", sdmac->channel);
8775b215c28STomasz Moń 		sdma_enable_channel(sdmac->sdma, sdmac->channel);
8785b215c28STomasz Moń 	}
8791ec1e82fSSascha Hauer }
8801ec1e82fSSascha Hauer 
88157b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
8821ec1e82fSSascha Hauer {
88315f30f51SNandor Han 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
8841ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
8851ec1e82fSSascha Hauer 	int i, error = 0;
8861ec1e82fSSascha Hauer 
88776c33d27SSascha Hauer 	sdmac->desc->chn_real_count = 0;
8881ec1e82fSSascha Hauer 	/*
8891ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
8901ec1e82fSSascha Hauer 	 * errors and call callback function
8911ec1e82fSSascha Hauer 	 */
89276c33d27SSascha Hauer 	for (i = 0; i < sdmac->desc->num_bd; i++) {
89376c33d27SSascha Hauer 		bd = &sdmac->desc->bd[i];
8941ec1e82fSSascha Hauer 
8951ec1e82fSSascha Hauer 		 if (bd->mode.status & (BD_DONE | BD_RROR))
8961ec1e82fSSascha Hauer 			error = -EIO;
89776c33d27SSascha Hauer 		 sdmac->desc->chn_real_count += bd->mode.count;
8981ec1e82fSSascha Hauer 	}
8991ec1e82fSSascha Hauer 
9001ec1e82fSSascha Hauer 	if (error)
9011ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
9021ec1e82fSSascha Hauer 	else
903409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
9041ec1e82fSSascha Hauer }
9051ec1e82fSSascha Hauer 
9061ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
9071ec1e82fSSascha Hauer {
9081ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
9090bbc1413SRichard Zhao 	unsigned long stat;
9101ec1e82fSSascha Hauer 
911c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
912c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
9131d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
9141d069bfaSMichael Olbrich 	stat &= ~1;
9151ec1e82fSSascha Hauer 
9161ec1e82fSSascha Hauer 	while (stat) {
9171ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
9181ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
91957b772b8SRobin Gong 		struct sdma_desc *desc;
9201ec1e82fSSascha Hauer 
92157b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
92257b772b8SRobin Gong 		desc = sdmac->desc;
92357b772b8SRobin Gong 		if (desc) {
92457b772b8SRobin Gong 			if (sdmac->flags & IMX_DMA_SG_LOOP) {
925d1a792f3SRussell King - ARM Linux 				sdma_update_channel_loop(sdmac);
92657b772b8SRobin Gong 			} else {
92757b772b8SRobin Gong 				mxc_sdma_handle_channel_normal(sdmac);
92857b772b8SRobin Gong 				vchan_cookie_complete(&desc->vd);
92957b772b8SRobin Gong 				sdma_start_desc(sdmac);
93057b772b8SRobin Gong 			}
93157b772b8SRobin Gong 		}
9321ec1e82fSSascha Hauer 
93357b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
9340bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
9351ec1e82fSSascha Hauer 	}
9361ec1e82fSSascha Hauer 
9371ec1e82fSSascha Hauer 	return IRQ_HANDLED;
9381ec1e82fSSascha Hauer }
9391ec1e82fSSascha Hauer 
9401ec1e82fSSascha Hauer /*
9411ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
9421ec1e82fSSascha Hauer  */
9431ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac,
9441ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
9451ec1e82fSSascha Hauer {
9461ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
9471ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
9481ec1e82fSSascha Hauer 	/*
9491ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
9501ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
9511ec1e82fSSascha Hauer 	 */
9520f06c027SRobin Gong 	int per_2_per = 0, emi_2_emi = 0;
9531ec1e82fSSascha Hauer 
9541ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
9551ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
9568391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
9570f06c027SRobin Gong 	sdmac->pc_to_pc = 0;
958e8fafa50SRobin Gong 	sdmac->is_ram_script = false;
9591ec1e82fSSascha Hauer 
9601ec1e82fSSascha Hauer 	switch (peripheral_type) {
9611ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
9620f06c027SRobin Gong 		emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
9631ec1e82fSSascha Hauer 		break;
9641ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
9651ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
9661ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
9671ec1e82fSSascha Hauer 		break;
9681ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
9691ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
9701ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
9711ec1e82fSSascha Hauer 		break;
9721ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
9731ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
9741ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
9751ec1e82fSSascha Hauer 		break;
9761ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
9771ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
9781ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
9791ec1e82fSSascha Hauer 		break;
9801ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
9811ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
9821ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
9831ec1e82fSSascha Hauer 		break;
9841ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
985a4965888SRobin Gong 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
9864852e9a2SRobin Gong 
9874852e9a2SRobin Gong 		/* Use rom script mcu_2_app if ERR009165 fixed */
9884852e9a2SRobin Gong 		if (sdmac->sdma->drvdata->ecspi_fixed) {
9894852e9a2SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_app_addr;
9904852e9a2SRobin Gong 		} else {
991a4965888SRobin Gong 			emi_2_per = sdma->script_addrs->mcu_2_ecspi_addr;
992a4965888SRobin Gong 			sdmac->is_ram_script = true;
9934852e9a2SRobin Gong 		}
9944852e9a2SRobin Gong 
995a4965888SRobin Gong 		break;
9961ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
9971ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
99829aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
9991ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
10001ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
10011ec1e82fSSascha Hauer 		break;
10021a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
10031a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
10041a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
1005e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10061a895578SNicolin Chen 		break;
10071ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
10081ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
10091ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
10101ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
10111ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
10121ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
10131ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
10141ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
10151ec1e82fSSascha Hauer 		break;
10161ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
10171ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
10181ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
10191ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
1020e8fafa50SRobin Gong 		sdmac->is_ram_script = true;
10211ec1e82fSSascha Hauer 		break;
1022f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
1023f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
1024f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
1025f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
1026f892afb0SNicolin Chen 		break;
10271ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
10281ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
10291ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
10301ec1e82fSSascha Hauer 		break;
10311ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
10321ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
10331ec1e82fSSascha Hauer 		break;
10341ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
10351ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
10361ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
10371ec1e82fSSascha Hauer 		break;
10381ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
10391ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
10401ec1e82fSSascha Hauer 		break;
10411ec1e82fSSascha Hauer 	default:
10421ec1e82fSSascha Hauer 		break;
10431ec1e82fSSascha Hauer 	}
10441ec1e82fSSascha Hauer 
10451ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
10461ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
10478391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
10480f06c027SRobin Gong 	sdmac->pc_to_pc = emi_2_emi;
10491ec1e82fSSascha Hauer }
10501ec1e82fSSascha Hauer 
10511ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
10521ec1e82fSSascha Hauer {
10531ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10541ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10551ec1e82fSSascha Hauer 	int load_address;
10561ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
105776c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
10581ec1e82fSSascha Hauer 	int ret;
10592ccaef05SRichard Zhao 	unsigned long flags;
10601ec1e82fSSascha Hauer 
10618391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
10621ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
10638391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
10648391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
10650f06c027SRobin Gong 	else if (sdmac->direction == DMA_MEM_TO_MEM)
10660f06c027SRobin Gong 		load_address = sdmac->pc_to_pc;
10678391ecf4SShengjiu Wang 	else
10681ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
10691ec1e82fSSascha Hauer 
10701ec1e82fSSascha Hauer 	if (load_address < 0)
10711ec1e82fSSascha Hauer 		return load_address;
10721ec1e82fSSascha Hauer 
10731ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
10740bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
10751ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
10761ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
10770bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
10780bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
10791ec1e82fSSascha Hauer 
10802ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
108173eab978SSascha Hauer 
10821ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
10831ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
10841ec1e82fSSascha Hauer 
10851ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
10861ec1e82fSSascha Hauer 	 * and watermark level
10871ec1e82fSSascha Hauer 	 */
10880bbc1413SRichard Zhao 	context->gReg[0] = sdmac->event_mask[1];
10890bbc1413SRichard Zhao 	context->gReg[1] = sdmac->event_mask[0];
10901ec1e82fSSascha Hauer 	context->gReg[2] = sdmac->per_addr;
10911ec1e82fSSascha Hauer 	context->gReg[6] = sdmac->shp_addr;
10921ec1e82fSSascha Hauer 	context->gReg[7] = sdmac->watermark_level;
10931ec1e82fSSascha Hauer 
10941ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
10953f93a4f2SRobin Gong 	bd0->mode.status = BD_DONE | BD_WRAP | BD_EXTD;
10961ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
10971ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
10981ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
10992ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
11001ec1e82fSSascha Hauer 
11012ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
110273eab978SSascha Hauer 
11031ec1e82fSSascha Hauer 	return ret;
11041ec1e82fSSascha Hauer }
11051ec1e82fSSascha Hauer 
11067b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
11071ec1e82fSSascha Hauer {
110857b772b8SRobin Gong 	return container_of(chan, struct sdma_channel, vc.chan);
11097b350ab0SMaxime Ripard }
11107b350ab0SMaxime Ripard 
11117b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
11127b350ab0SMaxime Ripard {
11137b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11141ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11151ec1e82fSSascha Hauer 	int channel = sdmac->channel;
11161ec1e82fSSascha Hauer 
11170bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
11181ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
11197b350ab0SMaxime Ripard 
11207b350ab0SMaxime Ripard 	return 0;
11211ec1e82fSSascha Hauer }
1122b8603d2aSLucas Stach static void sdma_channel_terminate_work(struct work_struct *work)
11237f3ff14bSJiada Wang {
1124b8603d2aSLucas Stach 	struct sdma_channel *sdmac = container_of(work, struct sdma_channel,
1125b8603d2aSLucas Stach 						  terminate_worker);
11267f3ff14bSJiada Wang 	/*
11277f3ff14bSJiada Wang 	 * According to NXP R&D team a delay of one BD SDMA cost time
11287f3ff14bSJiada Wang 	 * (maximum is 1ms) should be added after disable of the channel
11297f3ff14bSJiada Wang 	 * bit, to ensure SDMA core has really been stopped after SDMA
11307f3ff14bSJiada Wang 	 * clients call .device_terminate_all.
11317f3ff14bSJiada Wang 	 */
1132b8603d2aSLucas Stach 	usleep_range(1000, 2000);
1133b8603d2aSLucas Stach 
11344e2b10beSRobin Gong 	vchan_dma_desc_free_list(&sdmac->vc, &sdmac->terminated);
1135b8603d2aSLucas Stach }
1136b8603d2aSLucas Stach 
1137a80f2787SSascha Hauer static int sdma_terminate_all(struct dma_chan *chan)
1138b8603d2aSLucas Stach {
1139b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
114002939cd1SSascha Hauer 	unsigned long flags;
114102939cd1SSascha Hauer 
114202939cd1SSascha Hauer 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1143b8603d2aSLucas Stach 
1144b8603d2aSLucas Stach 	sdma_disable_channel(chan);
1145b8603d2aSLucas Stach 
114602939cd1SSascha Hauer 	if (sdmac->desc) {
114702939cd1SSascha Hauer 		vchan_terminate_vdesc(&sdmac->desc->vd);
11484e2b10beSRobin Gong 		/*
11494e2b10beSRobin Gong 		 * move out current descriptor into terminated list so that
11504e2b10beSRobin Gong 		 * it could be free in sdma_channel_terminate_work alone
11514e2b10beSRobin Gong 		 * later without potential involving next descriptor raised
11524e2b10beSRobin Gong 		 * up before the last descriptor terminated.
11534e2b10beSRobin Gong 		 */
11544e2b10beSRobin Gong 		vchan_get_all_descriptors(&sdmac->vc, &sdmac->terminated);
115502939cd1SSascha Hauer 		sdmac->desc = NULL;
1156b8603d2aSLucas Stach 		schedule_work(&sdmac->terminate_worker);
115702939cd1SSascha Hauer 	}
115802939cd1SSascha Hauer 
115902939cd1SSascha Hauer 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
11607f3ff14bSJiada Wang 
11617f3ff14bSJiada Wang 	return 0;
11627f3ff14bSJiada Wang }
11637f3ff14bSJiada Wang 
1164b8603d2aSLucas Stach static void sdma_channel_synchronize(struct dma_chan *chan)
1165b8603d2aSLucas Stach {
1166b8603d2aSLucas Stach 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1167b8603d2aSLucas Stach 
1168b8603d2aSLucas Stach 	vchan_synchronize(&sdmac->vc);
1169b8603d2aSLucas Stach 
1170b8603d2aSLucas Stach 	flush_work(&sdmac->terminate_worker);
1171b8603d2aSLucas Stach }
1172b8603d2aSLucas Stach 
11738391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
11748391ecf4SShengjiu Wang {
11758391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
11768391ecf4SShengjiu Wang 
11778391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
11788391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
11798391ecf4SShengjiu Wang 
11808391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
11818391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
11828391ecf4SShengjiu Wang 
11838391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
11848391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
11858391ecf4SShengjiu Wang 
11868391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
11878391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
11888391ecf4SShengjiu Wang 
11898391ecf4SShengjiu Wang 	/*
11908391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
11918391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
11928391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
11938391ecf4SShengjiu Wang 	 */
11948391ecf4SShengjiu Wang 	if (lwml > hwml) {
11958391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
11968391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
11978391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
11988391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
11998391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
12008391ecf4SShengjiu Wang 	}
12018391ecf4SShengjiu Wang 
12028391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
12038391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
12048391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
12058391ecf4SShengjiu Wang 
12068391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
12078391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
12088391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
12098391ecf4SShengjiu Wang 
12108391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
12118391ecf4SShengjiu Wang }
12128391ecf4SShengjiu Wang 
12137b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
12141ec1e82fSSascha Hauer {
12157b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
12161ec1e82fSSascha Hauer 
12177b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
12181ec1e82fSSascha Hauer 
12190bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
12200bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
12211ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
12221ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
12231ec1e82fSSascha Hauer 
12241ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
12251ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
12261ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
12271ec1e82fSSascha Hauer 		break;
12281ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
12291ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
12301ec1e82fSSascha Hauer 		break;
12311ec1e82fSSascha Hauer 	default:
12321ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
12331ec1e82fSSascha Hauer 		break;
12341ec1e82fSSascha Hauer 	}
12351ec1e82fSSascha Hauer 
12361ec1e82fSSascha Hauer 	sdma_get_pc(sdmac, sdmac->peripheral_type);
12371ec1e82fSSascha Hauer 
12381ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
12391ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
12401ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
12411ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
12428391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
12438391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
12448391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
12451f8595efSFlavio Suligoi 		} else {
12460bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
12471f8595efSFlavio Suligoi 		}
12488391ecf4SShengjiu Wang 
12491ec1e82fSSascha Hauer 		/* Address */
12501ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
12518391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
12521ec1e82fSSascha Hauer 	} else {
12531ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
12541ec1e82fSSascha Hauer 	}
12551ec1e82fSSascha Hauer 
1256e555a03bSRobin Gong 	return 0;
12571ec1e82fSSascha Hauer }
12581ec1e82fSSascha Hauer 
12591ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
12601ec1e82fSSascha Hauer 				     unsigned int priority)
12611ec1e82fSSascha Hauer {
12621ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
12631ec1e82fSSascha Hauer 	int channel = sdmac->channel;
12641ec1e82fSSascha Hauer 
12651ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
12661ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
12671ec1e82fSSascha Hauer 		return -EINVAL;
12681ec1e82fSSascha Hauer 	}
12691ec1e82fSSascha Hauer 
1270c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
12711ec1e82fSSascha Hauer 
12721ec1e82fSSascha Hauer 	return 0;
12731ec1e82fSSascha Hauer }
12741ec1e82fSSascha Hauer 
127557b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma)
12761ec1e82fSSascha Hauer {
12771ec1e82fSSascha Hauer 	int ret = -EBUSY;
12781ec1e82fSSascha Hauer 
127931ef489aSLinus Torvalds 	sdma->bd0 = dma_alloc_coherent(sdma->dev, PAGE_SIZE, &sdma->bd0_phys,
128057b772b8SRobin Gong 				       GFP_NOWAIT);
128157b772b8SRobin Gong 	if (!sdma->bd0) {
12821ec1e82fSSascha Hauer 		ret = -ENOMEM;
12831ec1e82fSSascha Hauer 		goto out;
12841ec1e82fSSascha Hauer 	}
12851ec1e82fSSascha Hauer 
128657b772b8SRobin Gong 	sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
128757b772b8SRobin Gong 	sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
12881ec1e82fSSascha Hauer 
128957b772b8SRobin Gong 	sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
12901ec1e82fSSascha Hauer 	return 0;
12911ec1e82fSSascha Hauer out:
12921ec1e82fSSascha Hauer 
12931ec1e82fSSascha Hauer 	return ret;
12941ec1e82fSSascha Hauer }
12951ec1e82fSSascha Hauer 
129657b772b8SRobin Gong 
129757b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc)
12981ec1e82fSSascha Hauer {
1299ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
130057b772b8SRobin Gong 	int ret = 0;
13011ec1e82fSSascha Hauer 
130231ef489aSLinus Torvalds 	desc->bd = dma_alloc_coherent(desc->sdmac->sdma->dev, bd_size,
1303ceaf5226SAndy Duan 				      &desc->bd_phys, GFP_NOWAIT);
130457b772b8SRobin Gong 	if (!desc->bd) {
130557b772b8SRobin Gong 		ret = -ENOMEM;
130657b772b8SRobin Gong 		goto out;
130757b772b8SRobin Gong 	}
130857b772b8SRobin Gong out:
130957b772b8SRobin Gong 	return ret;
131057b772b8SRobin Gong }
13111ec1e82fSSascha Hauer 
131257b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc)
131357b772b8SRobin Gong {
1314ebb853b1SLucas Stach 	u32 bd_size = desc->num_bd * sizeof(struct sdma_buffer_descriptor);
1315ebb853b1SLucas Stach 
1316ceaf5226SAndy Duan 	dma_free_coherent(desc->sdmac->sdma->dev, bd_size, desc->bd,
1317ceaf5226SAndy Duan 			  desc->bd_phys);
131857b772b8SRobin Gong }
13191ec1e82fSSascha Hauer 
132057b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd)
132157b772b8SRobin Gong {
132257b772b8SRobin Gong 	struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
132357b772b8SRobin Gong 
132457b772b8SRobin Gong 	sdma_free_bd(desc);
132557b772b8SRobin Gong 	kfree(desc);
13261ec1e82fSSascha Hauer }
13271ec1e82fSSascha Hauer 
13281ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
13291ec1e82fSSascha Hauer {
13301ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13311ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
13320f06c027SRobin Gong 	struct imx_dma_data mem_data;
13331ec1e82fSSascha Hauer 	int prio, ret;
13341ec1e82fSSascha Hauer 
13350f06c027SRobin Gong 	/*
13360f06c027SRobin Gong 	 * MEMCPY may never setup chan->private by filter function such as
13370f06c027SRobin Gong 	 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
13380f06c027SRobin Gong 	 * Please note in any other slave case, you have to setup chan->private
13390f06c027SRobin Gong 	 * with 'struct imx_dma_data' in your own filter function if you want to
13400f06c027SRobin Gong 	 * request dma channel by dma_request_channel() rather than
13410f06c027SRobin Gong 	 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
13420f06c027SRobin Gong 	 * to warn you to correct your filter function.
13430f06c027SRobin Gong 	 */
13440f06c027SRobin Gong 	if (!data) {
13450f06c027SRobin Gong 		dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
13460f06c027SRobin Gong 		mem_data.priority = 2;
13470f06c027SRobin Gong 		mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
13480f06c027SRobin Gong 		mem_data.dma_request = 0;
13490f06c027SRobin Gong 		mem_data.dma_request2 = 0;
13500f06c027SRobin Gong 		data = &mem_data;
13510f06c027SRobin Gong 
13520f06c027SRobin Gong 		sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
13530f06c027SRobin Gong 	}
13541ec1e82fSSascha Hauer 
13551ec1e82fSSascha Hauer 	switch (data->priority) {
13561ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
13571ec1e82fSSascha Hauer 		prio = 3;
13581ec1e82fSSascha Hauer 		break;
13591ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
13601ec1e82fSSascha Hauer 		prio = 2;
13611ec1e82fSSascha Hauer 		break;
13621ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
13631ec1e82fSSascha Hauer 	default:
13641ec1e82fSSascha Hauer 		prio = 1;
13651ec1e82fSSascha Hauer 		break;
13661ec1e82fSSascha Hauer 	}
13671ec1e82fSSascha Hauer 
13681ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
13691ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
13708391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1371c2c744d3SRichard Zhao 
1372b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1373b93edcddSFabio Estevam 	if (ret)
1374b93edcddSFabio Estevam 		return ret;
1375b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1376b93edcddSFabio Estevam 	if (ret)
1377b93edcddSFabio Estevam 		goto disable_clk_ipg;
1378c2c744d3SRichard Zhao 
13793bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
13801ec1e82fSSascha Hauer 	if (ret)
1381b93edcddSFabio Estevam 		goto disable_clk_ahb;
13821ec1e82fSSascha Hauer 
13831ec1e82fSSascha Hauer 	return 0;
1384b93edcddSFabio Estevam 
1385b93edcddSFabio Estevam disable_clk_ahb:
1386b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1387b93edcddSFabio Estevam disable_clk_ipg:
1388b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1389b93edcddSFabio Estevam 	return ret;
13901ec1e82fSSascha Hauer }
13911ec1e82fSSascha Hauer 
13921ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
13931ec1e82fSSascha Hauer {
13941ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13951ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
13961ec1e82fSSascha Hauer 
1397a80f2787SSascha Hauer 	sdma_terminate_all(chan);
1398b8603d2aSLucas Stach 
1399b8603d2aSLucas Stach 	sdma_channel_synchronize(chan);
14001ec1e82fSSascha Hauer 
14011ec1e82fSSascha Hauer 	sdma_event_disable(sdmac, sdmac->event_id0);
14021ec1e82fSSascha Hauer 	if (sdmac->event_id1)
14031ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
14041ec1e82fSSascha Hauer 
14051ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
14061ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
14071ec1e82fSSascha Hauer 
14081ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
14091ec1e82fSSascha Hauer 
14107560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
14117560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
14121ec1e82fSSascha Hauer }
14131ec1e82fSSascha Hauer 
141421420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
141521420841SRobin Gong 				enum dma_transfer_direction direction, u32 bds)
141621420841SRobin Gong {
141721420841SRobin Gong 	struct sdma_desc *desc;
141821420841SRobin Gong 
1419e8fafa50SRobin Gong 	if (!sdmac->sdma->fw_loaded && sdmac->is_ram_script) {
1420e8fafa50SRobin Gong 		dev_warn_once(sdmac->sdma->dev, "sdma firmware not ready!\n");
1421e8fafa50SRobin Gong 		goto err_out;
1422e8fafa50SRobin Gong 	}
1423e8fafa50SRobin Gong 
142421420841SRobin Gong 	desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
142521420841SRobin Gong 	if (!desc)
142621420841SRobin Gong 		goto err_out;
142721420841SRobin Gong 
142821420841SRobin Gong 	sdmac->status = DMA_IN_PROGRESS;
142921420841SRobin Gong 	sdmac->direction = direction;
143021420841SRobin Gong 	sdmac->flags = 0;
143121420841SRobin Gong 
143221420841SRobin Gong 	desc->chn_count = 0;
143321420841SRobin Gong 	desc->chn_real_count = 0;
143421420841SRobin Gong 	desc->buf_tail = 0;
143521420841SRobin Gong 	desc->buf_ptail = 0;
143621420841SRobin Gong 	desc->sdmac = sdmac;
143721420841SRobin Gong 	desc->num_bd = bds;
143821420841SRobin Gong 
143921420841SRobin Gong 	if (sdma_alloc_bd(desc))
144021420841SRobin Gong 		goto err_desc_out;
144121420841SRobin Gong 
14420f06c027SRobin Gong 	/* No slave_config called in MEMCPY case, so do here */
14430f06c027SRobin Gong 	if (direction == DMA_MEM_TO_MEM)
14440f06c027SRobin Gong 		sdma_config_ownership(sdmac, false, true, false);
14450f06c027SRobin Gong 
144621420841SRobin Gong 	if (sdma_load_context(sdmac))
144721420841SRobin Gong 		goto err_desc_out;
144821420841SRobin Gong 
144921420841SRobin Gong 	return desc;
145021420841SRobin Gong 
145121420841SRobin Gong err_desc_out:
145221420841SRobin Gong 	kfree(desc);
145321420841SRobin Gong err_out:
145421420841SRobin Gong 	return NULL;
145521420841SRobin Gong }
145621420841SRobin Gong 
14570f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy(
14580f06c027SRobin Gong 		struct dma_chan *chan, dma_addr_t dma_dst,
14590f06c027SRobin Gong 		dma_addr_t dma_src, size_t len, unsigned long flags)
14600f06c027SRobin Gong {
14610f06c027SRobin Gong 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14620f06c027SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
14630f06c027SRobin Gong 	int channel = sdmac->channel;
14640f06c027SRobin Gong 	size_t count;
14650f06c027SRobin Gong 	int i = 0, param;
14660f06c027SRobin Gong 	struct sdma_buffer_descriptor *bd;
14670f06c027SRobin Gong 	struct sdma_desc *desc;
14680f06c027SRobin Gong 
14690f06c027SRobin Gong 	if (!chan || !len)
14700f06c027SRobin Gong 		return NULL;
14710f06c027SRobin Gong 
14720f06c027SRobin Gong 	dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
14730f06c027SRobin Gong 		&dma_src, &dma_dst, len, channel);
14740f06c027SRobin Gong 
14750f06c027SRobin Gong 	desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
14760f06c027SRobin Gong 					len / SDMA_BD_MAX_CNT + 1);
14770f06c027SRobin Gong 	if (!desc)
14780f06c027SRobin Gong 		return NULL;
14790f06c027SRobin Gong 
14800f06c027SRobin Gong 	do {
14810f06c027SRobin Gong 		count = min_t(size_t, len, SDMA_BD_MAX_CNT);
14820f06c027SRobin Gong 		bd = &desc->bd[i];
14830f06c027SRobin Gong 		bd->buffer_addr = dma_src;
14840f06c027SRobin Gong 		bd->ext_buffer_addr = dma_dst;
14850f06c027SRobin Gong 		bd->mode.count = count;
14860f06c027SRobin Gong 		desc->chn_count += count;
14870f06c027SRobin Gong 		bd->mode.command = 0;
14880f06c027SRobin Gong 
14890f06c027SRobin Gong 		dma_src += count;
14900f06c027SRobin Gong 		dma_dst += count;
14910f06c027SRobin Gong 		len -= count;
14920f06c027SRobin Gong 		i++;
14930f06c027SRobin Gong 
14940f06c027SRobin Gong 		param = BD_DONE | BD_EXTD | BD_CONT;
14950f06c027SRobin Gong 		/* last bd */
14960f06c027SRobin Gong 		if (!len) {
14970f06c027SRobin Gong 			param |= BD_INTR;
14980f06c027SRobin Gong 			param |= BD_LAST;
14990f06c027SRobin Gong 			param &= ~BD_CONT;
15000f06c027SRobin Gong 		}
15010f06c027SRobin Gong 
15020f06c027SRobin Gong 		dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
15030f06c027SRobin Gong 				i, count, bd->buffer_addr,
15040f06c027SRobin Gong 				param & BD_WRAP ? "wrap" : "",
15050f06c027SRobin Gong 				param & BD_INTR ? " intr" : "");
15060f06c027SRobin Gong 
15070f06c027SRobin Gong 		bd->mode.status = param;
15080f06c027SRobin Gong 	} while (len);
15090f06c027SRobin Gong 
15100f06c027SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
15110f06c027SRobin Gong }
15120f06c027SRobin Gong 
15131ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
15141ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1515db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1516185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
15171ec1e82fSSascha Hauer {
15181ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
15191ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1520ad78b000SVinod Koul 	int i, count;
152123889c63SSascha Hauer 	int channel = sdmac->channel;
15221ec1e82fSSascha Hauer 	struct scatterlist *sg;
152357b772b8SRobin Gong 	struct sdma_desc *desc;
15241ec1e82fSSascha Hauer 
1525107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1526107d0644SVinod Koul 
152721420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, sg_len);
152857b772b8SRobin Gong 	if (!desc)
152957b772b8SRobin Gong 		goto err_out;
153057b772b8SRobin Gong 
15311ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
15321ec1e82fSSascha Hauer 			sg_len, channel);
15331ec1e82fSSascha Hauer 
15341ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
153576c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
15361ec1e82fSSascha Hauer 		int param;
15371ec1e82fSSascha Hauer 
1538d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
15391ec1e82fSSascha Hauer 
1540fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
15411ec1e82fSSascha Hauer 
15424a6b2e8aSRobin Gong 		if (count > SDMA_BD_MAX_CNT) {
15431ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
15444a6b2e8aSRobin Gong 					channel, count, SDMA_BD_MAX_CNT);
154557b772b8SRobin Gong 			goto err_bd_out;
15461ec1e82fSSascha Hauer 		}
15471ec1e82fSSascha Hauer 
15481ec1e82fSSascha Hauer 		bd->mode.count = count;
154976c33d27SSascha Hauer 		desc->chn_count += count;
15501ec1e82fSSascha Hauer 
1551ad78b000SVinod Koul 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
155257b772b8SRobin Gong 			goto err_bd_out;
15531fa81c27SSascha Hauer 
15541fa81c27SSascha Hauer 		switch (sdmac->word_size) {
15551fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
15561ec1e82fSSascha Hauer 			bd->mode.command = 0;
15571fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
155857b772b8SRobin Gong 				goto err_bd_out;
15591fa81c27SSascha Hauer 			break;
15601fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
15611fa81c27SSascha Hauer 			bd->mode.command = 2;
15621fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
156357b772b8SRobin Gong 				goto err_bd_out;
15641fa81c27SSascha Hauer 			break;
15651fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
15661fa81c27SSascha Hauer 			bd->mode.command = 1;
15671fa81c27SSascha Hauer 			break;
15681fa81c27SSascha Hauer 		default:
156957b772b8SRobin Gong 			goto err_bd_out;
15701fa81c27SSascha Hauer 		}
15711ec1e82fSSascha Hauer 
15721ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
15731ec1e82fSSascha Hauer 
1574341b9419SShawn Guo 		if (i + 1 == sg_len) {
15751ec1e82fSSascha Hauer 			param |= BD_INTR;
1576341b9419SShawn Guo 			param |= BD_LAST;
1577341b9419SShawn Guo 			param &= ~BD_CONT;
15781ec1e82fSSascha Hauer 		}
15791ec1e82fSSascha Hauer 
1580c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1581c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
15821ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
15831ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
15841ec1e82fSSascha Hauer 
15851ec1e82fSSascha Hauer 		bd->mode.status = param;
15861ec1e82fSSascha Hauer 	}
15871ec1e82fSSascha Hauer 
158857b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
158957b772b8SRobin Gong err_bd_out:
159057b772b8SRobin Gong 	sdma_free_bd(desc);
159157b772b8SRobin Gong 	kfree(desc);
15921ec1e82fSSascha Hauer err_out:
15934b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
15941ec1e82fSSascha Hauer 	return NULL;
15951ec1e82fSSascha Hauer }
15961ec1e82fSSascha Hauer 
15971ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
15981ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1599185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
160031c1e5a1SLaurent Pinchart 		unsigned long flags)
16011ec1e82fSSascha Hauer {
16021ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16031ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
16041ec1e82fSSascha Hauer 	int num_periods = buf_len / period_len;
160523889c63SSascha Hauer 	int channel = sdmac->channel;
160621420841SRobin Gong 	int i = 0, buf = 0;
160757b772b8SRobin Gong 	struct sdma_desc *desc;
16081ec1e82fSSascha Hauer 
16091ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
16101ec1e82fSSascha Hauer 
1611107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1612107d0644SVinod Koul 
161321420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, num_periods);
161457b772b8SRobin Gong 	if (!desc)
161557b772b8SRobin Gong 		goto err_out;
161657b772b8SRobin Gong 
161776c33d27SSascha Hauer 	desc->period_len = period_len;
16188e2e27c7SRichard Zhao 
16191ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
16201ec1e82fSSascha Hauer 
16214a6b2e8aSRobin Gong 	if (period_len > SDMA_BD_MAX_CNT) {
1622ba6ab3b3SArvind Yadav 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
16234a6b2e8aSRobin Gong 				channel, period_len, SDMA_BD_MAX_CNT);
162457b772b8SRobin Gong 		goto err_bd_out;
16251ec1e82fSSascha Hauer 	}
16261ec1e82fSSascha Hauer 
16271ec1e82fSSascha Hauer 	while (buf < buf_len) {
162876c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
16291ec1e82fSSascha Hauer 		int param;
16301ec1e82fSSascha Hauer 
16311ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
16321ec1e82fSSascha Hauer 
16331ec1e82fSSascha Hauer 		bd->mode.count = period_len;
16341ec1e82fSSascha Hauer 
16351ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
163657b772b8SRobin Gong 			goto err_bd_out;
16371ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
16381ec1e82fSSascha Hauer 			bd->mode.command = 0;
16391ec1e82fSSascha Hauer 		else
16401ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
16411ec1e82fSSascha Hauer 
16421ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
16431ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
16441ec1e82fSSascha Hauer 			param |= BD_WRAP;
16451ec1e82fSSascha Hauer 
1646ba6ab3b3SArvind Yadav 		dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1647c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
16481ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
16491ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
16501ec1e82fSSascha Hauer 
16511ec1e82fSSascha Hauer 		bd->mode.status = param;
16521ec1e82fSSascha Hauer 
16531ec1e82fSSascha Hauer 		dma_addr += period_len;
16541ec1e82fSSascha Hauer 		buf += period_len;
16551ec1e82fSSascha Hauer 
16561ec1e82fSSascha Hauer 		i++;
16571ec1e82fSSascha Hauer 	}
16581ec1e82fSSascha Hauer 
165957b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
166057b772b8SRobin Gong err_bd_out:
166157b772b8SRobin Gong 	sdma_free_bd(desc);
166257b772b8SRobin Gong 	kfree(desc);
16631ec1e82fSSascha Hauer err_out:
16641ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
16651ec1e82fSSascha Hauer 	return NULL;
16661ec1e82fSSascha Hauer }
16671ec1e82fSSascha Hauer 
1668107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
1669107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
1670107d0644SVinod Koul 		       enum dma_transfer_direction direction)
16711ec1e82fSSascha Hauer {
16721ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16731ec1e82fSSascha Hauer 
1674107d0644SVinod Koul 	if (direction == DMA_DEV_TO_MEM) {
16751ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
167694ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
167794ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
16781ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
1679107d0644SVinod Koul 	} else if (direction == DMA_DEV_TO_DEV) {
16808391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
16818391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
16828391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
16838391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
16848391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
16858391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
16868391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
16871ec1e82fSSascha Hauer 	} else {
16881ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
168994ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
169094ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
16911ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
16921ec1e82fSSascha Hauer 	}
1693107d0644SVinod Koul 	sdmac->direction = direction;
16947b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
16951ec1e82fSSascha Hauer }
16961ec1e82fSSascha Hauer 
1697107d0644SVinod Koul static int sdma_config(struct dma_chan *chan,
1698107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg)
1699107d0644SVinod Koul {
1700107d0644SVinod Koul 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1701107d0644SVinod Koul 
1702107d0644SVinod Koul 	memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1703107d0644SVinod Koul 
1704107d0644SVinod Koul 	/* Set ENBLn earlier to make sure dma request triggered after that */
1705107d0644SVinod Koul 	if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1706107d0644SVinod Koul 		return -EINVAL;
1707107d0644SVinod Koul 	sdma_event_enable(sdmac, sdmac->event_id0);
1708107d0644SVinod Koul 
1709107d0644SVinod Koul 	if (sdmac->event_id1) {
1710107d0644SVinod Koul 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1711107d0644SVinod Koul 			return -EINVAL;
1712107d0644SVinod Koul 		sdma_event_enable(sdmac, sdmac->event_id1);
1713107d0644SVinod Koul 	}
1714107d0644SVinod Koul 
1715107d0644SVinod Koul 	return 0;
1716107d0644SVinod Koul }
1717107d0644SVinod Koul 
17181ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
17191ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
17201ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
17211ec1e82fSSascha Hauer {
17221ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1723a1ff6a07SSascha Hauer 	struct sdma_desc *desc = NULL;
1724d1a792f3SRussell King - ARM Linux 	u32 residue;
172557b772b8SRobin Gong 	struct virt_dma_desc *vd;
172657b772b8SRobin Gong 	enum dma_status ret;
172757b772b8SRobin Gong 	unsigned long flags;
1728d1a792f3SRussell King - ARM Linux 
172957b772b8SRobin Gong 	ret = dma_cookie_status(chan, cookie, txstate);
173057b772b8SRobin Gong 	if (ret == DMA_COMPLETE || !txstate)
173157b772b8SRobin Gong 		return ret;
173257b772b8SRobin Gong 
173357b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
1734a1ff6a07SSascha Hauer 
173557b772b8SRobin Gong 	vd = vchan_find_desc(&sdmac->vc, cookie);
1736a1ff6a07SSascha Hauer 	if (vd)
173757b772b8SRobin Gong 		desc = to_sdma_desc(&vd->tx);
1738a1ff6a07SSascha Hauer 	else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie)
1739a1ff6a07SSascha Hauer 		desc = sdmac->desc;
1740a1ff6a07SSascha Hauer 
1741a1ff6a07SSascha Hauer 	if (desc) {
1742d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
174376c33d27SSascha Hauer 			residue = (desc->num_bd - desc->buf_ptail) *
174476c33d27SSascha Hauer 				desc->period_len - desc->chn_real_count;
1745d1a792f3SRussell King - ARM Linux 		else
174676c33d27SSascha Hauer 			residue = desc->chn_count - desc->chn_real_count;
174757b772b8SRobin Gong 	} else {
174857b772b8SRobin Gong 		residue = 0;
174957b772b8SRobin Gong 	}
1750a1ff6a07SSascha Hauer 
175157b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
17521ec1e82fSSascha Hauer 
1753e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1754d1a792f3SRussell King - ARM Linux 			 residue);
17551ec1e82fSSascha Hauer 
17568a965911SShawn Guo 	return sdmac->status;
17571ec1e82fSSascha Hauer }
17581ec1e82fSSascha Hauer 
17591ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
17601ec1e82fSSascha Hauer {
17612b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
176257b772b8SRobin Gong 	unsigned long flags;
17632b4f130eSSascha Hauer 
176457b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
176557b772b8SRobin Gong 	if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
176657b772b8SRobin Gong 		sdma_start_desc(sdmac);
176757b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
17681ec1e82fSSascha Hauer }
17691ec1e82fSSascha Hauer 
17705b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1771cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1772b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	45
1773b98ce2f4SRobin Gong #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4	46
17745b28aa31SSascha Hauer 
17755b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
17765b28aa31SSascha Hauer 			     const struct sdma_script_start_addrs *addr)
17775b28aa31SSascha Hauer {
17785b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
17795b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
17805b28aa31SSascha Hauer 	int i;
17815b28aa31SSascha Hauer 
178270dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
178370dabaedSNicolin Chen 	if (!sdma->script_number)
178470dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
178570dabaedSNicolin Chen 
1786bd73dfabSRobin Gong 	if (sdma->script_number > sizeof(struct sdma_script_start_addrs)
1787bd73dfabSRobin Gong 				  / sizeof(s32)) {
1788bd73dfabSRobin Gong 		dev_err(sdma->dev,
1789bd73dfabSRobin Gong 			"SDMA script number %d not match with firmware.\n",
1790bd73dfabSRobin Gong 			sdma->script_number);
1791bd73dfabSRobin Gong 		return;
1792bd73dfabSRobin Gong 	}
1793bd73dfabSRobin Gong 
1794cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
17955b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
17965b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
1797b98ce2f4SRobin Gong 
1798b98ce2f4SRobin Gong 	/*
1799b98ce2f4SRobin Gong 	 * get uart_2_mcu_addr/uartsh_2_mcu_addr rom script specially because
1800b98ce2f4SRobin Gong 	 * they are now replaced by uart_2_mcu_ram_addr/uartsh_2_mcu_ram_addr
1801b98ce2f4SRobin Gong 	 * to be compatible with legacy freescale/nxp sdma firmware, and they
1802b98ce2f4SRobin Gong 	 * are located in the bottom part of sdma_script_start_addrs which are
1803b98ce2f4SRobin Gong 	 * beyond the SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1.
1804b98ce2f4SRobin Gong 	 */
1805b98ce2f4SRobin Gong 	if (addr->uart_2_mcu_addr)
1806b98ce2f4SRobin Gong 		sdma->script_addrs->uart_2_mcu_addr = addr->uart_2_mcu_addr;
1807b98ce2f4SRobin Gong 	if (addr->uartsh_2_mcu_addr)
1808b98ce2f4SRobin Gong 		sdma->script_addrs->uartsh_2_mcu_addr = addr->uartsh_2_mcu_addr;
1809b98ce2f4SRobin Gong 
18105b28aa31SSascha Hauer }
18115b28aa31SSascha Hauer 
18127b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
18135b28aa31SSascha Hauer {
18147b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
18155b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
18165b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
18175b28aa31SSascha Hauer 	unsigned short *ram_code;
18185b28aa31SSascha Hauer 
18197b4b88e0SSascha Hauer 	if (!fw) {
18200f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
18210f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
18227b4b88e0SSascha Hauer 		return;
18237b4b88e0SSascha Hauer 	}
18245b28aa31SSascha Hauer 
18255b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
18265b28aa31SSascha Hauer 		goto err_firmware;
18275b28aa31SSascha Hauer 
18285b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
18295b28aa31SSascha Hauer 
18305b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
18315b28aa31SSascha Hauer 		goto err_firmware;
18325b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
18335b28aa31SSascha Hauer 		goto err_firmware;
1834cd72b846SNicolin Chen 	switch (header->version_major) {
1835cd72b846SNicolin Chen 	case 1:
1836cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1837cd72b846SNicolin Chen 		break;
1838cd72b846SNicolin Chen 	case 2:
1839cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1840cd72b846SNicolin Chen 		break;
1841a572460bSFabio Estevam 	case 3:
1842a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1843a572460bSFabio Estevam 		break;
1844b7d2648aSFabio Estevam 	case 4:
1845b7d2648aSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1846b7d2648aSFabio Estevam 		break;
1847cd72b846SNicolin Chen 	default:
1848cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1849cd72b846SNicolin Chen 		goto err_firmware;
1850cd72b846SNicolin Chen 	}
18515b28aa31SSascha Hauer 
18525b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
18535b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
18545b28aa31SSascha Hauer 
18557560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
18567560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
18575b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
18585b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
18595b28aa31SSascha Hauer 			 header->ram_code_size,
18606866fd3bSSascha Hauer 			 addr->ram_code_start_addr);
18617560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
18627560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
18635b28aa31SSascha Hauer 
18645b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
18655b28aa31SSascha Hauer 
1866e8fafa50SRobin Gong 	sdma->fw_loaded = true;
1867e8fafa50SRobin Gong 
18685b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
18695b28aa31SSascha Hauer 		 header->version_major,
18705b28aa31SSascha Hauer 		 header->version_minor);
18715b28aa31SSascha Hauer 
18725b28aa31SSascha Hauer err_firmware:
18735b28aa31SSascha Hauer 	release_firmware(fw);
18747b4b88e0SSascha Hauer }
18757b4b88e0SSascha Hauer 
1876d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1877d078cd1bSZidan Wang 
187829f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1879d078cd1bSZidan Wang {
1880d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1881d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1882d078cd1bSZidan Wang 	struct property *event_remap;
1883d078cd1bSZidan Wang 	struct regmap *gpr;
1884d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
1885d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
1886d078cd1bSZidan Wang 	int ret = 0;
1887d078cd1bSZidan Wang 
1888d078cd1bSZidan Wang 	if (IS_ERR(np) || IS_ERR(gpr_np))
1889d078cd1bSZidan Wang 		goto out;
1890d078cd1bSZidan Wang 
1891d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
1892d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1893d078cd1bSZidan Wang 	if (!num_map) {
1894ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
1895d078cd1bSZidan Wang 		goto out;
1896d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
1897d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
1898d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
1899d078cd1bSZidan Wang 		ret = -EINVAL;
1900d078cd1bSZidan Wang 		goto out;
1901d078cd1bSZidan Wang 	}
1902d078cd1bSZidan Wang 
1903d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
1904d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
1905d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
1906d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
1907d078cd1bSZidan Wang 		goto out;
1908d078cd1bSZidan Wang 	}
1909d078cd1bSZidan Wang 
1910d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1911d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
1912d078cd1bSZidan Wang 		if (ret) {
1913d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1914d078cd1bSZidan Wang 					propname, i);
1915d078cd1bSZidan Wang 			goto out;
1916d078cd1bSZidan Wang 		}
1917d078cd1bSZidan Wang 
1918d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1919d078cd1bSZidan Wang 		if (ret) {
1920d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1921d078cd1bSZidan Wang 					propname, i + 1);
1922d078cd1bSZidan Wang 			goto out;
1923d078cd1bSZidan Wang 		}
1924d078cd1bSZidan Wang 
1925d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
1926d078cd1bSZidan Wang 		if (ret) {
1927d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1928d078cd1bSZidan Wang 					propname, i + 2);
1929d078cd1bSZidan Wang 			goto out;
1930d078cd1bSZidan Wang 		}
1931d078cd1bSZidan Wang 
1932d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1933d078cd1bSZidan Wang 	}
1934d078cd1bSZidan Wang 
1935d078cd1bSZidan Wang out:
1936d078cd1bSZidan Wang 	if (!IS_ERR(gpr_np))
1937d078cd1bSZidan Wang 		of_node_put(gpr_np);
1938d078cd1bSZidan Wang 
1939d078cd1bSZidan Wang 	return ret;
1940d078cd1bSZidan Wang }
1941d078cd1bSZidan Wang 
1942fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
19437b4b88e0SSascha Hauer 		const char *fw_name)
19447b4b88e0SSascha Hauer {
19457b4b88e0SSascha Hauer 	int ret;
19467b4b88e0SSascha Hauer 
19477b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
19480733d839SShawn Guo 			FW_ACTION_UEVENT, fw_name, sdma->dev,
19497b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
19505b28aa31SSascha Hauer 
19515b28aa31SSascha Hauer 	return ret;
19525b28aa31SSascha Hauer }
19535b28aa31SSascha Hauer 
195419bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
19551ec1e82fSSascha Hauer {
19561ec1e82fSSascha Hauer 	int i, ret;
19571ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
19581ec1e82fSSascha Hauer 
1959b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
1960b93edcddSFabio Estevam 	if (ret)
1961b93edcddSFabio Estevam 		return ret;
1962b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
1963b93edcddSFabio Estevam 	if (ret)
1964b93edcddSFabio Estevam 		goto disable_clk_ipg;
19651ec1e82fSSascha Hauer 
1966941acd56SAngus Ainslie (Purism) 	if (sdma->drvdata->check_ratio &&
1967941acd56SAngus Ainslie (Purism) 	    (clk_get_rate(sdma->clk_ahb) == clk_get_rate(sdma->clk_ipg)))
196825aaa75dSAngus Ainslie (Purism) 		sdma->clk_ratio = 1;
196925aaa75dSAngus Ainslie (Purism) 
19701ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
1971c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
19721ec1e82fSSascha Hauer 
1973ceaf5226SAndy Duan 	sdma->channel_control = dma_alloc_coherent(sdma->dev,
19741ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control) +
19751ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
19761ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
19771ec1e82fSSascha Hauer 
19781ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
19791ec1e82fSSascha Hauer 		ret = -ENOMEM;
19801ec1e82fSSascha Hauer 		goto err_dma_alloc;
19811ec1e82fSSascha Hauer 	}
19821ec1e82fSSascha Hauer 
19831ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
19841ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
19851ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
19861ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof(struct sdma_channel_control);
19871ec1e82fSSascha Hauer 
19881ec1e82fSSascha Hauer 	/* disable all channels */
198917bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
1990c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
19911ec1e82fSSascha Hauer 
19921ec1e82fSSascha Hauer 	/* All channels have priority 0 */
19931ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
1994c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
19951ec1e82fSSascha Hauer 
199657b772b8SRobin Gong 	ret = sdma_request_channel0(sdma);
19971ec1e82fSSascha Hauer 	if (ret)
19981ec1e82fSSascha Hauer 		goto err_dma_alloc;
19991ec1e82fSSascha Hauer 
20001ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
20011ec1e82fSSascha Hauer 
20021ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
2003c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
20041ec1e82fSSascha Hauer 
20051ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
200625aaa75dSAngus Ainslie (Purism) 	if (sdma->clk_ratio)
200725aaa75dSAngus Ainslie (Purism) 		writel_relaxed(SDMA_H_CONFIG_ACR, sdma->regs + SDMA_H_CONFIG);
200825aaa75dSAngus Ainslie (Purism) 	else
2009c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
20101ec1e82fSSascha Hauer 
2011c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
20121ec1e82fSSascha Hauer 
20131ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
20141ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
20151ec1e82fSSascha Hauer 
20167560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
20177560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
20181ec1e82fSSascha Hauer 
20191ec1e82fSSascha Hauer 	return 0;
20201ec1e82fSSascha Hauer 
20211ec1e82fSSascha Hauer err_dma_alloc:
20227560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
2023b93edcddSFabio Estevam disable_clk_ipg:
2024b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
20251ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
20261ec1e82fSSascha Hauer 	return ret;
20271ec1e82fSSascha Hauer }
20281ec1e82fSSascha Hauer 
20299479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
20309479e17cSShawn Guo {
20310b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
20329479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
20339479e17cSShawn Guo 
20349479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
20359479e17cSShawn Guo 		return false;
20369479e17cSShawn Guo 
20370b351865SNicolin Chen 	sdmac->data = *data;
20380b351865SNicolin Chen 	chan->private = &sdmac->data;
20399479e17cSShawn Guo 
20409479e17cSShawn Guo 	return true;
20419479e17cSShawn Guo }
20429479e17cSShawn Guo 
20439479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
20449479e17cSShawn Guo 				   struct of_dma *ofdma)
20459479e17cSShawn Guo {
20469479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
20479479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
20489479e17cSShawn Guo 	struct imx_dma_data data;
20499479e17cSShawn Guo 
20509479e17cSShawn Guo 	if (dma_spec->args_count != 3)
20519479e17cSShawn Guo 		return NULL;
20529479e17cSShawn Guo 
20539479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
20549479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
20559479e17cSShawn Guo 	data.priority = dma_spec->args[2];
20568391ecf4SShengjiu Wang 	/*
20578391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
20588391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
20598391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
20608391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
20618391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
20628391ecf4SShengjiu Wang 	 */
20638391ecf4SShengjiu Wang 	data.dma_request2 = 0;
20649479e17cSShawn Guo 
2065990c0b53SBaolin Wang 	return __dma_request_channel(&mask, sdma_filter_fn, &data,
2066990c0b53SBaolin Wang 				     ofdma->of_node);
20679479e17cSShawn Guo }
20689479e17cSShawn Guo 
2069e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
20701ec1e82fSSascha Hauer {
2071580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
20728391ecf4SShengjiu Wang 	struct device_node *spba_bus;
2073580975d7SShawn Guo 	const char *fw_name;
20741ec1e82fSSascha Hauer 	int ret;
20751ec1e82fSSascha Hauer 	int irq;
20761ec1e82fSSascha Hauer 	struct resource *iores;
20778391ecf4SShengjiu Wang 	struct resource spba_res;
20781ec1e82fSSascha Hauer 	int i;
20791ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
208036e2f21aSSascha Hauer 	s32 *saddr_arr;
20811ec1e82fSSascha Hauer 
208242536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
208342536b9fSPhilippe Retornaz 	if (ret)
208442536b9fSPhilippe Retornaz 		return ret;
208542536b9fSPhilippe Retornaz 
20867f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
20871ec1e82fSSascha Hauer 	if (!sdma)
20881ec1e82fSSascha Hauer 		return -ENOMEM;
20891ec1e82fSSascha Hauer 
20902ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
209173eab978SSascha Hauer 
20921ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
209332996419SFabio Estevam 	sdma->drvdata = of_device_get_match_data(sdma->dev);
20941ec1e82fSSascha Hauer 
20951ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
20967f24e0eeSFabio Estevam 	if (irq < 0)
209763c72e02SFabio Estevam 		return irq;
20981ec1e82fSSascha Hauer 
20997f24e0eeSFabio Estevam 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
21007f24e0eeSFabio Estevam 	sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
21017f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
21027f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
21031ec1e82fSSascha Hauer 
21047560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
21057f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
21067f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
21071ec1e82fSSascha Hauer 
21087560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
21097f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
21107f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
21117560e3f3SSascha Hauer 
2112fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ipg);
2113fb9caf37SArvind Yadav 	if (ret)
2114fb9caf37SArvind Yadav 		return ret;
2115fb9caf37SArvind Yadav 
2116fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ahb);
2117fb9caf37SArvind Yadav 	if (ret)
2118fb9caf37SArvind Yadav 		goto err_clk;
21197560e3f3SSascha Hauer 
21207f24e0eeSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
21217f24e0eeSFabio Estevam 			       sdma);
21221ec1e82fSSascha Hauer 	if (ret)
2123fb9caf37SArvind Yadav 		goto err_irq;
21241ec1e82fSSascha Hauer 
21255bb9dbb5SVinod Koul 	sdma->irq = irq;
21265bb9dbb5SVinod Koul 
21275b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2128fb9caf37SArvind Yadav 	if (!sdma->script_addrs) {
2129fb9caf37SArvind Yadav 		ret = -ENOMEM;
2130fb9caf37SArvind Yadav 		goto err_irq;
2131fb9caf37SArvind Yadav 	}
21321ec1e82fSSascha Hauer 
213336e2f21aSSascha Hauer 	/* initially no scripts available */
213436e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
2135be4cf718SSascha Hauer 	for (i = 0; i < sizeof(*sdma->script_addrs) / sizeof(s32); i++)
213636e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
213736e2f21aSSascha Hauer 
21387214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
21397214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
21400f06c027SRobin Gong 	dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
21417214a8b1SSascha Hauer 
21421ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
21431ec1e82fSSascha Hauer 	/* Initialize channel parameters */
21441ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
21451ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
21461ec1e82fSSascha Hauer 
21471ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
21481ec1e82fSSascha Hauer 
21491ec1e82fSSascha Hauer 		sdmac->channel = i;
215057b772b8SRobin Gong 		sdmac->vc.desc_free = sdma_desc_free;
21514e2b10beSRobin Gong 		INIT_LIST_HEAD(&sdmac->terminated);
2152b8603d2aSLucas Stach 		INIT_WORK(&sdmac->terminate_worker,
2153b8603d2aSLucas Stach 				sdma_channel_terminate_work);
215423889c63SSascha Hauer 		/*
215523889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
215623889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
215723889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
215823889c63SSascha Hauer 		 */
215923889c63SSascha Hauer 		if (i)
216057b772b8SRobin Gong 			vchan_init(&sdmac->vc, &sdma->dma_device);
21611ec1e82fSSascha Hauer 	}
21621ec1e82fSSascha Hauer 
21635b28aa31SSascha Hauer 	ret = sdma_init(sdma);
21641ec1e82fSSascha Hauer 	if (ret)
21651ec1e82fSSascha Hauer 		goto err_init;
21661ec1e82fSSascha Hauer 
2167d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
2168d078cd1bSZidan Wang 	if (ret)
2169d078cd1bSZidan Wang 		goto err_init;
2170d078cd1bSZidan Wang 
2171dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
2172dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
21735b28aa31SSascha Hauer 
21741ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
21751ec1e82fSSascha Hauer 
21761ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
21771ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
21781ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
21791ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
21801ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
21817b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
2182a80f2787SSascha Hauer 	sdma->dma_device.device_terminate_all = sdma_terminate_all;
2183b8603d2aSLucas Stach 	sdma->dma_device.device_synchronize = sdma_channel_synchronize;
2184f9d4a398SNicolin Chen 	sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2185f9d4a398SNicolin Chen 	sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2186f9d4a398SNicolin Chen 	sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
21876f3125ceSLucas Stach 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
21880f06c027SRobin Gong 	sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
21891ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
2190a3711d49SAngus Ainslie (Purism) 	sdma->dma_device.copy_align = 2;
21914a6b2e8aSRobin Gong 	dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
21921ec1e82fSSascha Hauer 
219323e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
219423e11811SVignesh Raman 
21951ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
21961ec1e82fSSascha Hauer 	if (ret) {
21971ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
21981ec1e82fSSascha Hauer 		goto err_init;
21991ec1e82fSSascha Hauer 	}
22001ec1e82fSSascha Hauer 
22019479e17cSShawn Guo 	if (np) {
22029479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
22039479e17cSShawn Guo 		if (ret) {
22049479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
22059479e17cSShawn Guo 			goto err_register;
22069479e17cSShawn Guo 		}
22078391ecf4SShengjiu Wang 
22088391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
22098391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
22108391ecf4SShengjiu Wang 		if (!ret) {
22118391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
22128391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
22138391ecf4SShengjiu Wang 		}
22148391ecf4SShengjiu Wang 		of_node_put(spba_bus);
22159479e17cSShawn Guo 	}
22169479e17cSShawn Guo 
22172b8066c3SSven Van Asbroeck 	/*
22182b8066c3SSven Van Asbroeck 	 * Because that device tree does not encode ROM script address,
22192b8066c3SSven Van Asbroeck 	 * the RAM script in firmware is mandatory for device tree
22202b8066c3SSven Van Asbroeck 	 * probe, otherwise it fails.
22212b8066c3SSven Van Asbroeck 	 */
22222b8066c3SSven Van Asbroeck 	ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
22232b8066c3SSven Van Asbroeck 				      &fw_name);
22242b8066c3SSven Van Asbroeck 	if (ret) {
22252b8066c3SSven Van Asbroeck 		dev_warn(&pdev->dev, "failed to get firmware name\n");
22262b8066c3SSven Van Asbroeck 	} else {
22272b8066c3SSven Van Asbroeck 		ret = sdma_get_firmware(sdma, fw_name);
22282b8066c3SSven Van Asbroeck 		if (ret)
22292b8066c3SSven Van Asbroeck 			dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
22302b8066c3SSven Van Asbroeck 	}
22312b8066c3SSven Van Asbroeck 
22321ec1e82fSSascha Hauer 	return 0;
22331ec1e82fSSascha Hauer 
22349479e17cSShawn Guo err_register:
22359479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
22361ec1e82fSSascha Hauer err_init:
22371ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
2238fb9caf37SArvind Yadav err_irq:
2239fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2240fb9caf37SArvind Yadav err_clk:
2241fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2242939fd4f0SShawn Guo 	return ret;
22431ec1e82fSSascha Hauer }
22441ec1e82fSSascha Hauer 
22451d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
22461ec1e82fSSascha Hauer {
224723e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
2248c12fe497SVignesh Raman 	int i;
224923e11811SVignesh Raman 
22505bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
225123e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
225223e11811SVignesh Raman 	kfree(sdma->script_addrs);
2253fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2254fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2255c12fe497SVignesh Raman 	/* Kill the tasklet */
2256c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2257c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
2258c12fe497SVignesh Raman 
225957b772b8SRobin Gong 		tasklet_kill(&sdmac->vc.task);
226057b772b8SRobin Gong 		sdma_free_chan_resources(&sdmac->vc.chan);
2261c12fe497SVignesh Raman 	}
226223e11811SVignesh Raman 
226323e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
226423e11811SVignesh Raman 	return 0;
22651ec1e82fSSascha Hauer }
22661ec1e82fSSascha Hauer 
22671ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
22681ec1e82fSSascha Hauer 	.driver		= {
22691ec1e82fSSascha Hauer 		.name	= "imx-sdma",
2270580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
22711ec1e82fSSascha Hauer 	},
22721d1bbd30SMaxin B. John 	.remove		= sdma_remove,
227323e11811SVignesh Raman 	.probe		= sdma_probe,
22741ec1e82fSSascha Hauer };
22751ec1e82fSSascha Hauer 
227623e11811SVignesh Raman module_platform_driver(sdma_driver);
22771ec1e82fSSascha Hauer 
22781ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
22791ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
2280c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2281c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2282c0879342SNicolas Chauvet #endif
2283c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D)
2284c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2285c0879342SNicolas Chauvet #endif
22861ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
2287