xref: /openbmc/linux/drivers/dma/imx-sdma.c (revision 107d06441b709d31ce592535086992799ee51e17)
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>
27fe5b85c6SRobin Gong #include <linux/dmapool.h>
281ec1e82fSSascha Hauer #include <linux/firmware.h>
291ec1e82fSSascha Hauer #include <linux/slab.h>
301ec1e82fSSascha Hauer #include <linux/platform_device.h>
311ec1e82fSSascha Hauer #include <linux/dmaengine.h>
32580975d7SShawn Guo #include <linux/of.h>
338391ecf4SShengjiu Wang #include <linux/of_address.h>
34580975d7SShawn Guo #include <linux/of_device.h>
359479e17cSShawn Guo #include <linux/of_dma.h>
361ec1e82fSSascha Hauer 
371ec1e82fSSascha Hauer #include <asm/irq.h>
3882906b13SArnd Bergmann #include <linux/platform_data/dma-imx-sdma.h>
3982906b13SArnd Bergmann #include <linux/platform_data/dma-imx.h>
40d078cd1bSZidan Wang #include <linux/regmap.h>
41d078cd1bSZidan Wang #include <linux/mfd/syscon.h>
42d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
431ec1e82fSSascha Hauer 
44d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
4557b772b8SRobin Gong #include "virt-dma.h"
46d2ebfb33SRussell King - ARM Linux 
471ec1e82fSSascha Hauer /* SDMA registers */
481ec1e82fSSascha Hauer #define SDMA_H_C0PTR		0x000
491ec1e82fSSascha Hauer #define SDMA_H_INTR		0x004
501ec1e82fSSascha Hauer #define SDMA_H_STATSTOP		0x008
511ec1e82fSSascha Hauer #define SDMA_H_START		0x00c
521ec1e82fSSascha Hauer #define SDMA_H_EVTOVR		0x010
531ec1e82fSSascha Hauer #define SDMA_H_DSPOVR		0x014
541ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR		0x018
551ec1e82fSSascha Hauer #define SDMA_H_EVTPEND		0x01c
561ec1e82fSSascha Hauer #define SDMA_H_DSPENBL		0x020
571ec1e82fSSascha Hauer #define SDMA_H_RESET		0x024
581ec1e82fSSascha Hauer #define SDMA_H_EVTERR		0x028
591ec1e82fSSascha Hauer #define SDMA_H_INTRMSK		0x02c
601ec1e82fSSascha Hauer #define SDMA_H_PSW		0x030
611ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG	0x034
621ec1e82fSSascha Hauer #define SDMA_H_CONFIG		0x038
631ec1e82fSSascha Hauer #define SDMA_ONCE_ENB		0x040
641ec1e82fSSascha Hauer #define SDMA_ONCE_DATA		0x044
651ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR		0x048
661ec1e82fSSascha Hauer #define SDMA_ONCE_STAT		0x04c
671ec1e82fSSascha Hauer #define SDMA_ONCE_CMD		0x050
681ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR		0x054
691ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR	0x058
701ec1e82fSSascha Hauer #define SDMA_CHN0ADDR		0x05c
711ec1e82fSSascha Hauer #define SDMA_ONCE_RTB		0x060
721ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1	0x070
731ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2	0x074
7462550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35	0x200
7562550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31	0x080
761ec1e82fSSascha Hauer #define SDMA_CHNPRI_0		0x100
771ec1e82fSSascha Hauer 
781ec1e82fSSascha Hauer /*
791ec1e82fSSascha Hauer  * Buffer descriptor status values.
801ec1e82fSSascha Hauer  */
811ec1e82fSSascha Hauer #define BD_DONE  0x01
821ec1e82fSSascha Hauer #define BD_WRAP  0x02
831ec1e82fSSascha Hauer #define BD_CONT  0x04
841ec1e82fSSascha Hauer #define BD_INTR  0x08
851ec1e82fSSascha Hauer #define BD_RROR  0x10
861ec1e82fSSascha Hauer #define BD_LAST  0x20
871ec1e82fSSascha Hauer #define BD_EXTD  0x80
881ec1e82fSSascha Hauer 
891ec1e82fSSascha Hauer /*
901ec1e82fSSascha Hauer  * Data Node descriptor status values.
911ec1e82fSSascha Hauer  */
921ec1e82fSSascha Hauer #define DND_END_OF_FRAME  0x80
931ec1e82fSSascha Hauer #define DND_END_OF_XFER   0x40
941ec1e82fSSascha Hauer #define DND_DONE          0x20
951ec1e82fSSascha Hauer #define DND_UNUSED        0x01
961ec1e82fSSascha Hauer 
971ec1e82fSSascha Hauer /*
981ec1e82fSSascha Hauer  * IPCV2 descriptor status values.
991ec1e82fSSascha Hauer  */
1001ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME  0x40
1011ec1e82fSSascha Hauer 
1021ec1e82fSSascha Hauer #define IPCV2_MAX_NODES        50
1031ec1e82fSSascha Hauer /*
1041ec1e82fSSascha Hauer  * Error bit set in the CCB status field by the SDMA,
1051ec1e82fSSascha Hauer  * in setbd routine, in case of a transfer error
1061ec1e82fSSascha Hauer  */
1071ec1e82fSSascha Hauer #define DATA_ERROR  0x10000000
1081ec1e82fSSascha Hauer 
1091ec1e82fSSascha Hauer /*
1101ec1e82fSSascha Hauer  * Buffer descriptor commands.
1111ec1e82fSSascha Hauer  */
1121ec1e82fSSascha Hauer #define C0_ADDR             0x01
1131ec1e82fSSascha Hauer #define C0_LOAD             0x02
1141ec1e82fSSascha Hauer #define C0_DUMP             0x03
1151ec1e82fSSascha Hauer #define C0_SETCTX           0x07
1161ec1e82fSSascha Hauer #define C0_GETCTX           0x03
1171ec1e82fSSascha Hauer #define C0_SETDM            0x01
1181ec1e82fSSascha Hauer #define C0_SETPM            0x04
1191ec1e82fSSascha Hauer #define C0_GETDM            0x02
1201ec1e82fSSascha Hauer #define C0_GETPM            0x08
1211ec1e82fSSascha Hauer /*
1221ec1e82fSSascha Hauer  * Change endianness indicator in the BD command field
1231ec1e82fSSascha Hauer  */
1241ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS   0x80
1251ec1e82fSSascha Hauer 
1261ec1e82fSSascha Hauer /*
1278391ecf4SShengjiu Wang  *  p_2_p watermark_level description
1288391ecf4SShengjiu Wang  *	Bits		Name			Description
1298391ecf4SShengjiu Wang  *	0-7		Lower WML		Lower watermark level
1308391ecf4SShengjiu Wang  *	8		PS			1: Pad Swallowing
1318391ecf4SShengjiu Wang  *						0: No Pad Swallowing
1328391ecf4SShengjiu Wang  *	9		PA			1: Pad Adding
1338391ecf4SShengjiu Wang  *						0: No Pad Adding
1348391ecf4SShengjiu Wang  *	10		SPDIF			If this bit is set both source
1358391ecf4SShengjiu Wang  *						and destination are on SPBA
1368391ecf4SShengjiu Wang  *	11		Source Bit(SP)		1: Source on SPBA
1378391ecf4SShengjiu Wang  *						0: Source on AIPS
1388391ecf4SShengjiu Wang  *	12		Destination Bit(DP)	1: Destination on SPBA
1398391ecf4SShengjiu Wang  *						0: Destination on AIPS
1408391ecf4SShengjiu Wang  *	13-15		---------		MUST BE 0
1418391ecf4SShengjiu Wang  *	16-23		Higher WML		HWML
1428391ecf4SShengjiu Wang  *	24-27		N			Total number of samples after
1438391ecf4SShengjiu Wang  *						which Pad adding/Swallowing
1448391ecf4SShengjiu Wang  *						must be done. It must be odd.
1458391ecf4SShengjiu Wang  *	28		Lower WML Event(LWE)	SDMA events reg to check for
1468391ecf4SShengjiu Wang  *						LWML event mask
1478391ecf4SShengjiu Wang  *						0: LWE in EVENTS register
1488391ecf4SShengjiu Wang  *						1: LWE in EVENTS2 register
1498391ecf4SShengjiu Wang  *	29		Higher WML Event(HWE)	SDMA events reg to check for
1508391ecf4SShengjiu Wang  *						HWML event mask
1518391ecf4SShengjiu Wang  *						0: HWE in EVENTS register
1528391ecf4SShengjiu Wang  *						1: HWE in EVENTS2 register
1538391ecf4SShengjiu Wang  *	30		---------		MUST BE 0
1548391ecf4SShengjiu Wang  *	31		CONT			1: Amount of samples to be
1558391ecf4SShengjiu Wang  *						transferred is unknown and
1568391ecf4SShengjiu Wang  *						script will keep on
1578391ecf4SShengjiu Wang  *						transferring samples as long as
1588391ecf4SShengjiu Wang  *						both events are detected and
1598391ecf4SShengjiu Wang  *						script must be manually stopped
1608391ecf4SShengjiu Wang  *						by the application
1618391ecf4SShengjiu Wang  *						0: The amount of samples to be
1628391ecf4SShengjiu Wang  *						transferred is equal to the
1638391ecf4SShengjiu Wang  *						count field of mode word
1648391ecf4SShengjiu Wang  */
1658391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML	0xFF
1668391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS		BIT(8)
1678391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA		BIT(9)
1688391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF	BIT(10)
1698391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP		BIT(11)
1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP		BIT(12)
1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML	(0xFF << 16)
1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE	BIT(28)
1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE	BIT(29)
1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT	BIT(31)
1758391ecf4SShengjiu Wang 
176f9d4a398SNicolin Chen #define SDMA_DMA_BUSWIDTHS	(BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
177f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
178f9d4a398SNicolin Chen 				 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
179f9d4a398SNicolin Chen 
180f9d4a398SNicolin Chen #define SDMA_DMA_DIRECTIONS	(BIT(DMA_DEV_TO_MEM) | \
181f9d4a398SNicolin Chen 				 BIT(DMA_MEM_TO_DEV) | \
182f9d4a398SNicolin Chen 				 BIT(DMA_DEV_TO_DEV))
183f9d4a398SNicolin Chen 
1848391ecf4SShengjiu Wang /*
1851ec1e82fSSascha Hauer  * Mode/Count of data node descriptors - IPCv2
1861ec1e82fSSascha Hauer  */
1871ec1e82fSSascha Hauer struct sdma_mode_count {
1884a6b2e8aSRobin Gong #define SDMA_BD_MAX_CNT	0xffff
1891ec1e82fSSascha Hauer 	u32 count   : 16; /* size of the buffer pointed by this BD */
1901ec1e82fSSascha Hauer 	u32 status  :  8; /* E,R,I,C,W,D status bits stored here */
191e4b75760SMartin Kaiser 	u32 command :  8; /* command mostly used for channel 0 */
1921ec1e82fSSascha Hauer };
1931ec1e82fSSascha Hauer 
1941ec1e82fSSascha Hauer /*
1951ec1e82fSSascha Hauer  * Buffer descriptor
1961ec1e82fSSascha Hauer  */
1971ec1e82fSSascha Hauer struct sdma_buffer_descriptor {
1981ec1e82fSSascha Hauer 	struct sdma_mode_count  mode;
1991ec1e82fSSascha Hauer 	u32 buffer_addr;	/* address of the buffer described */
2001ec1e82fSSascha Hauer 	u32 ext_buffer_addr;	/* extended buffer address */
2011ec1e82fSSascha Hauer } __attribute__ ((packed));
2021ec1e82fSSascha Hauer 
2031ec1e82fSSascha Hauer /**
2041ec1e82fSSascha Hauer  * struct sdma_channel_control - Channel control Block
2051ec1e82fSSascha Hauer  *
20624ca312dSRobin Gong  * @current_bd_ptr:	current buffer descriptor processed
20724ca312dSRobin Gong  * @base_bd_ptr:	first element of buffer descriptor array
20824ca312dSRobin Gong  * @unused:		padding. The SDMA engine expects an array of 128 byte
2091ec1e82fSSascha Hauer  *			control blocks
2101ec1e82fSSascha Hauer  */
2111ec1e82fSSascha Hauer struct sdma_channel_control {
2121ec1e82fSSascha Hauer 	u32 current_bd_ptr;
2131ec1e82fSSascha Hauer 	u32 base_bd_ptr;
2141ec1e82fSSascha Hauer 	u32 unused[2];
2151ec1e82fSSascha Hauer } __attribute__ ((packed));
2161ec1e82fSSascha Hauer 
2171ec1e82fSSascha Hauer /**
2181ec1e82fSSascha Hauer  * struct sdma_state_registers - SDMA context for a channel
2191ec1e82fSSascha Hauer  *
2201ec1e82fSSascha Hauer  * @pc:		program counter
22124ca312dSRobin Gong  * @unused1:	unused
2221ec1e82fSSascha Hauer  * @t:		test bit: status of arithmetic & test instruction
2231ec1e82fSSascha Hauer  * @rpc:	return program counter
22424ca312dSRobin Gong  * @unused0:	unused
2251ec1e82fSSascha Hauer  * @sf:		source fault while loading data
2261ec1e82fSSascha Hauer  * @spc:	loop start program counter
22724ca312dSRobin Gong  * @unused2:	unused
2281ec1e82fSSascha Hauer  * @df:		destination fault while storing data
2291ec1e82fSSascha Hauer  * @epc:	loop end program counter
2301ec1e82fSSascha Hauer  * @lm:		loop mode
2311ec1e82fSSascha Hauer  */
2321ec1e82fSSascha Hauer struct sdma_state_registers {
2331ec1e82fSSascha Hauer 	u32 pc     :14;
2341ec1e82fSSascha Hauer 	u32 unused1: 1;
2351ec1e82fSSascha Hauer 	u32 t      : 1;
2361ec1e82fSSascha Hauer 	u32 rpc    :14;
2371ec1e82fSSascha Hauer 	u32 unused0: 1;
2381ec1e82fSSascha Hauer 	u32 sf     : 1;
2391ec1e82fSSascha Hauer 	u32 spc    :14;
2401ec1e82fSSascha Hauer 	u32 unused2: 1;
2411ec1e82fSSascha Hauer 	u32 df     : 1;
2421ec1e82fSSascha Hauer 	u32 epc    :14;
2431ec1e82fSSascha Hauer 	u32 lm     : 2;
2441ec1e82fSSascha Hauer } __attribute__ ((packed));
2451ec1e82fSSascha Hauer 
2461ec1e82fSSascha Hauer /**
2471ec1e82fSSascha Hauer  * struct sdma_context_data - sdma context specific to a channel
2481ec1e82fSSascha Hauer  *
2491ec1e82fSSascha Hauer  * @channel_state:	channel state bits
2501ec1e82fSSascha Hauer  * @gReg:		general registers
2511ec1e82fSSascha Hauer  * @mda:		burst dma destination address register
2521ec1e82fSSascha Hauer  * @msa:		burst dma source address register
2531ec1e82fSSascha Hauer  * @ms:			burst dma status register
2541ec1e82fSSascha Hauer  * @md:			burst dma data register
2551ec1e82fSSascha Hauer  * @pda:		peripheral dma destination address register
2561ec1e82fSSascha Hauer  * @psa:		peripheral dma source address register
2571ec1e82fSSascha Hauer  * @ps:			peripheral dma status register
2581ec1e82fSSascha Hauer  * @pd:			peripheral dma data register
2591ec1e82fSSascha Hauer  * @ca:			CRC polynomial register
2601ec1e82fSSascha Hauer  * @cs:			CRC accumulator register
2611ec1e82fSSascha Hauer  * @dda:		dedicated core destination address register
2621ec1e82fSSascha Hauer  * @dsa:		dedicated core source address register
2631ec1e82fSSascha Hauer  * @ds:			dedicated core status register
2641ec1e82fSSascha Hauer  * @dd:			dedicated core data register
26524ca312dSRobin Gong  * @scratch0:		1st word of dedicated ram for context switch
26624ca312dSRobin Gong  * @scratch1:		2nd word of dedicated ram for context switch
26724ca312dSRobin Gong  * @scratch2:		3rd word of dedicated ram for context switch
26824ca312dSRobin Gong  * @scratch3:		4th word of dedicated ram for context switch
26924ca312dSRobin Gong  * @scratch4:		5th word of dedicated ram for context switch
27024ca312dSRobin Gong  * @scratch5:		6th word of dedicated ram for context switch
27124ca312dSRobin Gong  * @scratch6:		7th word of dedicated ram for context switch
27224ca312dSRobin Gong  * @scratch7:		8th word of dedicated ram for context switch
2731ec1e82fSSascha Hauer  */
2741ec1e82fSSascha Hauer struct sdma_context_data {
2751ec1e82fSSascha Hauer 	struct sdma_state_registers  channel_state;
2761ec1e82fSSascha Hauer 	u32  gReg[8];
2771ec1e82fSSascha Hauer 	u32  mda;
2781ec1e82fSSascha Hauer 	u32  msa;
2791ec1e82fSSascha Hauer 	u32  ms;
2801ec1e82fSSascha Hauer 	u32  md;
2811ec1e82fSSascha Hauer 	u32  pda;
2821ec1e82fSSascha Hauer 	u32  psa;
2831ec1e82fSSascha Hauer 	u32  ps;
2841ec1e82fSSascha Hauer 	u32  pd;
2851ec1e82fSSascha Hauer 	u32  ca;
2861ec1e82fSSascha Hauer 	u32  cs;
2871ec1e82fSSascha Hauer 	u32  dda;
2881ec1e82fSSascha Hauer 	u32  dsa;
2891ec1e82fSSascha Hauer 	u32  ds;
2901ec1e82fSSascha Hauer 	u32  dd;
2911ec1e82fSSascha Hauer 	u32  scratch0;
2921ec1e82fSSascha Hauer 	u32  scratch1;
2931ec1e82fSSascha Hauer 	u32  scratch2;
2941ec1e82fSSascha Hauer 	u32  scratch3;
2951ec1e82fSSascha Hauer 	u32  scratch4;
2961ec1e82fSSascha Hauer 	u32  scratch5;
2971ec1e82fSSascha Hauer 	u32  scratch6;
2981ec1e82fSSascha Hauer 	u32  scratch7;
2991ec1e82fSSascha Hauer } __attribute__ ((packed));
3001ec1e82fSSascha Hauer 
3011ec1e82fSSascha Hauer 
3021ec1e82fSSascha Hauer struct sdma_engine;
3031ec1e82fSSascha Hauer 
3041ec1e82fSSascha Hauer /**
30576c33d27SSascha Hauer  * struct sdma_desc - descriptor structor for one transfer
30624ca312dSRobin Gong  * @vd:			descriptor for virt dma
30724ca312dSRobin Gong  * @num_bd:		number of descriptors currently handling
30824ca312dSRobin Gong  * @bd_phys:		physical address of bd
30924ca312dSRobin Gong  * @buf_tail:		ID of the buffer that was processed
31024ca312dSRobin Gong  * @buf_ptail:		ID of the previous buffer that was processed
31124ca312dSRobin Gong  * @period_len:		period length, used in cyclic.
31224ca312dSRobin Gong  * @chn_real_count:	the real count updated from bd->mode.count
31324ca312dSRobin Gong  * @chn_count:		the transfer count set
31424ca312dSRobin Gong  * @sdmac:		sdma_channel pointer
31524ca312dSRobin Gong  * @bd:			pointer of allocate bd
31676c33d27SSascha Hauer  */
31776c33d27SSascha Hauer struct sdma_desc {
31857b772b8SRobin Gong 	struct virt_dma_desc	vd;
31976c33d27SSascha Hauer 	unsigned int		num_bd;
32076c33d27SSascha Hauer 	dma_addr_t		bd_phys;
32176c33d27SSascha Hauer 	unsigned int		buf_tail;
32276c33d27SSascha Hauer 	unsigned int		buf_ptail;
32376c33d27SSascha Hauer 	unsigned int		period_len;
32476c33d27SSascha Hauer 	unsigned int		chn_real_count;
32576c33d27SSascha Hauer 	unsigned int		chn_count;
32676c33d27SSascha Hauer 	struct sdma_channel	*sdmac;
32776c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd;
32876c33d27SSascha Hauer };
32976c33d27SSascha Hauer 
33076c33d27SSascha Hauer /**
3311ec1e82fSSascha Hauer  * struct sdma_channel - housekeeping for a SDMA channel
3321ec1e82fSSascha Hauer  *
33324ca312dSRobin Gong  * @vc:			virt_dma base structure
33424ca312dSRobin Gong  * @desc:		sdma description including vd and other special member
33524ca312dSRobin Gong  * @sdma:		pointer to the SDMA engine for this channel
33624ca312dSRobin Gong  * @channel:		the channel number, matches dmaengine chan_id + 1
33724ca312dSRobin Gong  * @direction:		transfer type. Needed for setting SDMA script
338*107d0644SVinod Koul  * @slave_config	Slave configuration
33924ca312dSRobin Gong  * @peripheral_type:	Peripheral type. Needed for setting SDMA script
34024ca312dSRobin Gong  * @event_id0:		aka dma request line
34124ca312dSRobin Gong  * @event_id1:		for channels that use 2 events
34224ca312dSRobin Gong  * @word_size:		peripheral access size
34324ca312dSRobin Gong  * @pc_from_device:	script address for those device_2_memory
34424ca312dSRobin Gong  * @pc_to_device:	script address for those memory_2_device
34524ca312dSRobin Gong  * @device_to_device:	script address for those device_2_device
3460f06c027SRobin Gong  * @pc_to_pc:		script address for those memory_2_memory
34724ca312dSRobin Gong  * @flags:		loop mode or not
34824ca312dSRobin Gong  * @per_address:	peripheral source or destination address in common case
34924ca312dSRobin Gong  *                      destination address in p_2_p case
35024ca312dSRobin Gong  * @per_address2:	peripheral source address in p_2_p case
35124ca312dSRobin Gong  * @event_mask:		event mask used in p_2_p script
35224ca312dSRobin Gong  * @watermark_level:	value for gReg[7], some script will extend it from
35324ca312dSRobin Gong  *			basic watermark such as p_2_p
35424ca312dSRobin Gong  * @shp_addr:		value for gReg[6]
35524ca312dSRobin Gong  * @per_addr:		value for gReg[2]
35624ca312dSRobin Gong  * @status:		status of dma channel
35724ca312dSRobin Gong  * @data:		specific sdma interface structure
35824ca312dSRobin Gong  * @bd_pool:		dma_pool for bd
3591ec1e82fSSascha Hauer  */
3601ec1e82fSSascha Hauer struct sdma_channel {
36157b772b8SRobin Gong 	struct virt_dma_chan		vc;
36276c33d27SSascha Hauer 	struct sdma_desc		*desc;
3631ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
3641ec1e82fSSascha Hauer 	unsigned int			channel;
365db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
366*107d0644SVinod Koul 	struct dma_slave_config		slave_config;
3671ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
3681ec1e82fSSascha Hauer 	unsigned int			event_id0;
3691ec1e82fSSascha Hauer 	unsigned int			event_id1;
3701ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
3711ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
3728391ecf4SShengjiu Wang 	unsigned int			device_to_device;
3730f06c027SRobin Gong 	unsigned int                    pc_to_pc;
3741ec1e82fSSascha Hauer 	unsigned long			flags;
3758391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
3760bbc1413SRichard Zhao 	unsigned long			event_mask[2];
3770bbc1413SRichard Zhao 	unsigned long			watermark_level;
3781ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
3791ec1e82fSSascha Hauer 	enum dma_status			status;
3800b351865SNicolin Chen 	struct imx_dma_data		data;
381fe5b85c6SRobin Gong 	struct dma_pool			*bd_pool;
3821ec1e82fSSascha Hauer };
3831ec1e82fSSascha Hauer 
3840bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
3851ec1e82fSSascha Hauer 
3861ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
3871ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
3881ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
3891ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
3901ec1e82fSSascha Hauer 
3911ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
3921ec1e82fSSascha Hauer 
3931ec1e82fSSascha Hauer /**
3941ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
3951ec1e82fSSascha Hauer  *
39624ca312dSRobin Gong  * @magic:		"SDMA"
39724ca312dSRobin Gong  * @version_major:	increased whenever layout of struct
39824ca312dSRobin Gong  *			sdma_script_start_addrs changes.
39924ca312dSRobin Gong  * @version_minor:	firmware minor version (for binary compatible changes)
40024ca312dSRobin Gong  * @script_addrs_start:	offset of struct sdma_script_start_addrs in this image
40124ca312dSRobin Gong  * @num_script_addrs:	Number of script addresses in this image
40224ca312dSRobin Gong  * @ram_code_start:	offset of SDMA ram image in this firmware image
40324ca312dSRobin Gong  * @ram_code_size:	size of SDMA ram image
40424ca312dSRobin Gong  * @script_addrs:	Stores the start address of the SDMA scripts
4051ec1e82fSSascha Hauer  *			(in SDMA memory space)
4061ec1e82fSSascha Hauer  */
4071ec1e82fSSascha Hauer struct sdma_firmware_header {
4081ec1e82fSSascha Hauer 	u32	magic;
4091ec1e82fSSascha Hauer 	u32	version_major;
4101ec1e82fSSascha Hauer 	u32	version_minor;
4111ec1e82fSSascha Hauer 	u32	script_addrs_start;
4121ec1e82fSSascha Hauer 	u32	num_script_addrs;
4131ec1e82fSSascha Hauer 	u32	ram_code_start;
4141ec1e82fSSascha Hauer 	u32	ram_code_size;
4151ec1e82fSSascha Hauer };
4161ec1e82fSSascha Hauer 
41717bba72fSSascha Hauer struct sdma_driver_data {
41817bba72fSSascha Hauer 	int chnenbl0;
41917bba72fSSascha Hauer 	int num_events;
420dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
42162550cd7SShawn Guo };
42262550cd7SShawn Guo 
4231ec1e82fSSascha Hauer struct sdma_engine {
4241ec1e82fSSascha Hauer 	struct device			*dev;
425b9b3f82fSSascha Hauer 	struct device_dma_parameters	dma_parms;
4261ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
4271ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
4281ec1e82fSSascha Hauer 	void __iomem			*regs;
4291ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
4301ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
4311ec1e82fSSascha Hauer 	struct dma_device		dma_device;
4327560e3f3SSascha Hauer 	struct clk			*clk_ipg;
4337560e3f3SSascha Hauer 	struct clk			*clk_ahb;
4342ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
435cd72b846SNicolin Chen 	u32				script_number;
4361ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
43717bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
4388391ecf4SShengjiu Wang 	u32				spba_start_addr;
4398391ecf4SShengjiu Wang 	u32				spba_end_addr;
4405bb9dbb5SVinod Koul 	unsigned int			irq;
44176c33d27SSascha Hauer 	dma_addr_t			bd0_phys;
44276c33d27SSascha Hauer 	struct sdma_buffer_descriptor	*bd0;
44317bba72fSSascha Hauer };
44417bba72fSSascha Hauer 
445*107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
446*107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
447*107d0644SVinod Koul 		       enum dma_transfer_direction direction);
448*107d0644SVinod Koul 
449e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
45017bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
45117bba72fSSascha Hauer 	.num_events = 32,
45217bba72fSSascha Hauer };
45317bba72fSSascha Hauer 
454dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
455dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
456dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
457dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
458dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
459dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
460dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
461dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
462dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
463dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
464dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
465dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
466dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
467dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
468dcfec3c0SSascha Hauer };
469dcfec3c0SSascha Hauer 
470e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
471dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
472dcfec3c0SSascha Hauer 	.num_events = 48,
473dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
474dcfec3c0SSascha Hauer };
475dcfec3c0SSascha Hauer 
476e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
47717bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
47817bba72fSSascha Hauer 	.num_events = 48,
4791ec1e82fSSascha Hauer };
4801ec1e82fSSascha Hauer 
481dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
482dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
483dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
484dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
485dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
486dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
487dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
488dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
489dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
490dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
491dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
492dcfec3c0SSascha Hauer };
493dcfec3c0SSascha Hauer 
494e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
495dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
496dcfec3c0SSascha Hauer 	.num_events = 48,
497dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
498dcfec3c0SSascha Hauer };
499dcfec3c0SSascha Hauer 
500dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
501dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
502dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
503dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
504dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
505dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
506dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
507dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
508dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
509dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
510dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
511dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
512dcfec3c0SSascha Hauer };
513dcfec3c0SSascha Hauer 
514e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
515dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
516dcfec3c0SSascha Hauer 	.num_events = 48,
517dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
518dcfec3c0SSascha Hauer };
519dcfec3c0SSascha Hauer 
520dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
521dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
522dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
523dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
524dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
525dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
526dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
527dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
528dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
529dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
530dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
531dcfec3c0SSascha Hauer };
532dcfec3c0SSascha Hauer 
533e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
534dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
535dcfec3c0SSascha Hauer 	.num_events = 48,
536dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
537dcfec3c0SSascha Hauer };
538dcfec3c0SSascha Hauer 
539b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = {
540b7d2648aSFabio Estevam 	.ap_2_ap_addr = 644,
541b7d2648aSFabio Estevam 	.uart_2_mcu_addr = 819,
542b7d2648aSFabio Estevam 	.mcu_2_app_addr = 749,
543b7d2648aSFabio Estevam 	.uartsh_2_mcu_addr = 1034,
544b7d2648aSFabio Estevam 	.mcu_2_shp_addr = 962,
545b7d2648aSFabio Estevam 	.app_2_mcu_addr = 685,
546b7d2648aSFabio Estevam 	.shp_2_mcu_addr = 893,
547b7d2648aSFabio Estevam 	.spdif_2_mcu_addr = 1102,
548b7d2648aSFabio Estevam 	.mcu_2_spdif_addr = 1136,
549b7d2648aSFabio Estevam };
550b7d2648aSFabio Estevam 
551b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = {
552b7d2648aSFabio Estevam 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
553b7d2648aSFabio Estevam 	.num_events = 48,
554b7d2648aSFabio Estevam 	.script_addrs = &sdma_script_imx7d,
555b7d2648aSFabio Estevam };
556b7d2648aSFabio Estevam 
557afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = {
55862550cd7SShawn Guo 	{
559dcfec3c0SSascha Hauer 		.name = "imx25-sdma",
560dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx25,
561dcfec3c0SSascha Hauer 	}, {
56262550cd7SShawn Guo 		.name = "imx31-sdma",
56317bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx31,
56462550cd7SShawn Guo 	}, {
56562550cd7SShawn Guo 		.name = "imx35-sdma",
56617bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx35,
56762550cd7SShawn Guo 	}, {
568dcfec3c0SSascha Hauer 		.name = "imx51-sdma",
569dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx51,
570dcfec3c0SSascha Hauer 	}, {
571dcfec3c0SSascha Hauer 		.name = "imx53-sdma",
572dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx53,
573dcfec3c0SSascha Hauer 	}, {
574dcfec3c0SSascha Hauer 		.name = "imx6q-sdma",
575dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx6q,
576dcfec3c0SSascha Hauer 	}, {
577b7d2648aSFabio Estevam 		.name = "imx7d-sdma",
578b7d2648aSFabio Estevam 		.driver_data = (unsigned long)&sdma_imx7d,
579b7d2648aSFabio Estevam 	}, {
58062550cd7SShawn Guo 		/* sentinel */
58162550cd7SShawn Guo 	}
58262550cd7SShawn Guo };
58362550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes);
58462550cd7SShawn Guo 
585580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
586dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
587dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
588dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
58917bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
590dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
59163edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
592b7d2648aSFabio Estevam 	{ .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
593580975d7SShawn Guo 	{ /* sentinel */ }
594580975d7SShawn Guo };
595580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
596580975d7SShawn Guo 
5970bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
5980bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
5990bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
6001ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
6011ec1e82fSSascha Hauer 
6021ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
6031ec1e82fSSascha Hauer {
60417bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
6051ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
6061ec1e82fSSascha Hauer }
6071ec1e82fSSascha Hauer 
6081ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
6091ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
6101ec1e82fSSascha Hauer {
6111ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6121ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6130bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
6141ec1e82fSSascha Hauer 
6151ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
6161ec1e82fSSascha Hauer 		return -EINVAL;
6171ec1e82fSSascha Hauer 
618c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
619c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
620c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
6211ec1e82fSSascha Hauer 
6221ec1e82fSSascha Hauer 	if (dsp_override)
6230bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
6241ec1e82fSSascha Hauer 	else
6250bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
6261ec1e82fSSascha Hauer 
6271ec1e82fSSascha Hauer 	if (event_override)
6280bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
6291ec1e82fSSascha Hauer 	else
6300bbc1413SRichard Zhao 		__set_bit(channel, &evt);
6311ec1e82fSSascha Hauer 
6321ec1e82fSSascha Hauer 	if (mcu_override)
6330bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
6341ec1e82fSSascha Hauer 	else
6350bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
6361ec1e82fSSascha Hauer 
637c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
638c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
639c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
6401ec1e82fSSascha Hauer 
6411ec1e82fSSascha Hauer 	return 0;
6421ec1e82fSSascha Hauer }
6431ec1e82fSSascha Hauer 
644b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
645b9a59166SRichard Zhao {
6460bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
647b9a59166SRichard Zhao }
648b9a59166SRichard Zhao 
6491ec1e82fSSascha Hauer /*
6502ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
6511ec1e82fSSascha Hauer  */
6522ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
6531ec1e82fSSascha Hauer {
6541ec1e82fSSascha Hauer 	int ret;
6551d069bfaSMichael Olbrich 	u32 reg;
6561ec1e82fSSascha Hauer 
6572ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
6581ec1e82fSSascha Hauer 
6591d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
6601d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
6611d069bfaSMichael Olbrich 	if (ret)
6622ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
6631ec1e82fSSascha Hauer 
664855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
665855832e4SRobin Gong 	if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
666855832e4SRobin Gong 		writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
667855832e4SRobin Gong 
6681d069bfaSMichael Olbrich 	return ret;
6691ec1e82fSSascha Hauer }
6701ec1e82fSSascha Hauer 
6711ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
6721ec1e82fSSascha Hauer 		u32 address)
6731ec1e82fSSascha Hauer {
67476c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
6751ec1e82fSSascha Hauer 	void *buf_virt;
6761ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
6771ec1e82fSSascha Hauer 	int ret;
6782ccaef05SRichard Zhao 	unsigned long flags;
67973eab978SSascha Hauer 
6801ec1e82fSSascha Hauer 	buf_virt = dma_alloc_coherent(NULL,
6811ec1e82fSSascha Hauer 			size,
6821ec1e82fSSascha Hauer 			&buf_phys, GFP_KERNEL);
68373eab978SSascha Hauer 	if (!buf_virt) {
6842ccaef05SRichard Zhao 		return -ENOMEM;
68573eab978SSascha Hauer 	}
6861ec1e82fSSascha Hauer 
6872ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
6882ccaef05SRichard Zhao 
6891ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
6901ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
6911ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
6921ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
6931ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
6941ec1e82fSSascha Hauer 
6951ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
6961ec1e82fSSascha Hauer 
6972ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
6982ccaef05SRichard Zhao 
6992ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
7001ec1e82fSSascha Hauer 
7011ec1e82fSSascha Hauer 	dma_free_coherent(NULL, size, buf_virt, buf_phys);
7021ec1e82fSSascha Hauer 
7031ec1e82fSSascha Hauer 	return ret;
7041ec1e82fSSascha Hauer }
7051ec1e82fSSascha Hauer 
7061ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
7071ec1e82fSSascha Hauer {
7081ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7091ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7100bbc1413SRichard Zhao 	unsigned long val;
7111ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
7121ec1e82fSSascha Hauer 
713c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
7140bbc1413SRichard Zhao 	__set_bit(channel, &val);
715c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
7161ec1e82fSSascha Hauer }
7171ec1e82fSSascha Hauer 
7181ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
7191ec1e82fSSascha Hauer {
7201ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7211ec1e82fSSascha Hauer 	int channel = sdmac->channel;
7221ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
7230bbc1413SRichard Zhao 	unsigned long val;
7241ec1e82fSSascha Hauer 
725c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
7260bbc1413SRichard Zhao 	__clear_bit(channel, &val);
727c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
7281ec1e82fSSascha Hauer }
7291ec1e82fSSascha Hauer 
73057b772b8SRobin Gong static struct sdma_desc *to_sdma_desc(struct dma_async_tx_descriptor *t)
73157b772b8SRobin Gong {
73257b772b8SRobin Gong 	return container_of(t, struct sdma_desc, vd.tx);
73357b772b8SRobin Gong }
73457b772b8SRobin Gong 
73557b772b8SRobin Gong static void sdma_start_desc(struct sdma_channel *sdmac)
73657b772b8SRobin Gong {
73757b772b8SRobin Gong 	struct virt_dma_desc *vd = vchan_next_desc(&sdmac->vc);
73857b772b8SRobin Gong 	struct sdma_desc *desc;
73957b772b8SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
74057b772b8SRobin Gong 	int channel = sdmac->channel;
74157b772b8SRobin Gong 
74257b772b8SRobin Gong 	if (!vd) {
74357b772b8SRobin Gong 		sdmac->desc = NULL;
74457b772b8SRobin Gong 		return;
74557b772b8SRobin Gong 	}
74657b772b8SRobin Gong 	sdmac->desc = desc = to_sdma_desc(&vd->tx);
74757b772b8SRobin Gong 	/*
74857b772b8SRobin Gong 	 * Do not delete the node in desc_issued list in cyclic mode, otherwise
749680302c4SVinod Koul 	 * the desc allocated will never be freed in vchan_dma_desc_free_list
75057b772b8SRobin Gong 	 */
75157b772b8SRobin Gong 	if (!(sdmac->flags & IMX_DMA_SG_LOOP))
75257b772b8SRobin Gong 		list_del(&vd->node);
75357b772b8SRobin Gong 
75457b772b8SRobin Gong 	sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
75557b772b8SRobin Gong 	sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
75657b772b8SRobin Gong 	sdma_enable_channel(sdma, sdmac->channel);
75757b772b8SRobin Gong }
75857b772b8SRobin Gong 
759d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
760d1a792f3SRussell King - ARM Linux {
7611ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
7625881826dSNandor Han 	int error = 0;
7635881826dSNandor Han 	enum dma_status	old_status = sdmac->status;
7641ec1e82fSSascha Hauer 
7651ec1e82fSSascha Hauer 	/*
7661ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
7671ec1e82fSSascha Hauer 	 * call callback function.
7681ec1e82fSSascha Hauer 	 */
76957b772b8SRobin Gong 	while (sdmac->desc) {
77076c33d27SSascha Hauer 		struct sdma_desc *desc = sdmac->desc;
77176c33d27SSascha Hauer 
77276c33d27SSascha Hauer 		bd = &desc->bd[desc->buf_tail];
7731ec1e82fSSascha Hauer 
7741ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
7751ec1e82fSSascha Hauer 			break;
7761ec1e82fSSascha Hauer 
7775881826dSNandor Han 		if (bd->mode.status & BD_RROR) {
7785881826dSNandor Han 			bd->mode.status &= ~BD_RROR;
7791ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
7805881826dSNandor Han 			error = -EIO;
7815881826dSNandor Han 		}
7821ec1e82fSSascha Hauer 
7835881826dSNandor Han 	       /*
7845881826dSNandor Han 		* We use bd->mode.count to calculate the residue, since contains
7855881826dSNandor Han 		* the number of bytes present in the current buffer descriptor.
7865881826dSNandor Han 		*/
7875881826dSNandor Han 
78876c33d27SSascha Hauer 		desc->chn_real_count = bd->mode.count;
7891ec1e82fSSascha Hauer 		bd->mode.status |= BD_DONE;
79076c33d27SSascha Hauer 		bd->mode.count = desc->period_len;
79176c33d27SSascha Hauer 		desc->buf_ptail = desc->buf_tail;
79276c33d27SSascha Hauer 		desc->buf_tail = (desc->buf_tail + 1) % desc->num_bd;
79315f30f51SNandor Han 
79415f30f51SNandor Han 		/*
79515f30f51SNandor Han 		 * The callback is called from the interrupt context in order
79615f30f51SNandor Han 		 * to reduce latency and to avoid the risk of altering the
79715f30f51SNandor Han 		 * SDMA transaction status by the time the client tasklet is
79815f30f51SNandor Han 		 * executed.
79915f30f51SNandor Han 		 */
80057b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
80157b772b8SRobin Gong 		dmaengine_desc_get_callback_invoke(&desc->vd.tx, NULL);
80257b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
80315f30f51SNandor Han 
8045881826dSNandor Han 		if (error)
8055881826dSNandor Han 			sdmac->status = old_status;
8061ec1e82fSSascha Hauer 	}
8071ec1e82fSSascha Hauer }
8081ec1e82fSSascha Hauer 
80957b772b8SRobin Gong static void mxc_sdma_handle_channel_normal(struct sdma_channel *data)
8101ec1e82fSSascha Hauer {
81115f30f51SNandor Han 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
8121ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
8131ec1e82fSSascha Hauer 	int i, error = 0;
8141ec1e82fSSascha Hauer 
81576c33d27SSascha Hauer 	sdmac->desc->chn_real_count = 0;
8161ec1e82fSSascha Hauer 	/*
8171ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
8181ec1e82fSSascha Hauer 	 * errors and call callback function
8191ec1e82fSSascha Hauer 	 */
82076c33d27SSascha Hauer 	for (i = 0; i < sdmac->desc->num_bd; i++) {
82176c33d27SSascha Hauer 		bd = &sdmac->desc->bd[i];
8221ec1e82fSSascha Hauer 
8231ec1e82fSSascha Hauer 		 if (bd->mode.status & (BD_DONE | BD_RROR))
8241ec1e82fSSascha Hauer 			error = -EIO;
82576c33d27SSascha Hauer 		 sdmac->desc->chn_real_count += bd->mode.count;
8261ec1e82fSSascha Hauer 	}
8271ec1e82fSSascha Hauer 
8281ec1e82fSSascha Hauer 	if (error)
8291ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
8301ec1e82fSSascha Hauer 	else
831409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
8321ec1e82fSSascha Hauer }
8331ec1e82fSSascha Hauer 
8341ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
8351ec1e82fSSascha Hauer {
8361ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
8370bbc1413SRichard Zhao 	unsigned long stat;
8381ec1e82fSSascha Hauer 
839c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
840c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
8411d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
8421d069bfaSMichael Olbrich 	stat &= ~1;
8431ec1e82fSSascha Hauer 
8441ec1e82fSSascha Hauer 	while (stat) {
8451ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
8461ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
84757b772b8SRobin Gong 		struct sdma_desc *desc;
8481ec1e82fSSascha Hauer 
84957b772b8SRobin Gong 		spin_lock(&sdmac->vc.lock);
85057b772b8SRobin Gong 		desc = sdmac->desc;
85157b772b8SRobin Gong 		if (desc) {
85257b772b8SRobin Gong 			if (sdmac->flags & IMX_DMA_SG_LOOP) {
853d1a792f3SRussell King - ARM Linux 				sdma_update_channel_loop(sdmac);
85457b772b8SRobin Gong 			} else {
85557b772b8SRobin Gong 				mxc_sdma_handle_channel_normal(sdmac);
85657b772b8SRobin Gong 				vchan_cookie_complete(&desc->vd);
85757b772b8SRobin Gong 				sdma_start_desc(sdmac);
85857b772b8SRobin Gong 			}
85957b772b8SRobin Gong 		}
8601ec1e82fSSascha Hauer 
86157b772b8SRobin Gong 		spin_unlock(&sdmac->vc.lock);
8620bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
8631ec1e82fSSascha Hauer 	}
8641ec1e82fSSascha Hauer 
8651ec1e82fSSascha Hauer 	return IRQ_HANDLED;
8661ec1e82fSSascha Hauer }
8671ec1e82fSSascha Hauer 
8681ec1e82fSSascha Hauer /*
8691ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
8701ec1e82fSSascha Hauer  */
8711ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac,
8721ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
8731ec1e82fSSascha Hauer {
8741ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8751ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
8761ec1e82fSSascha Hauer 	/*
8771ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
8781ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
8791ec1e82fSSascha Hauer 	 */
8800f06c027SRobin Gong 	int per_2_per = 0, emi_2_emi = 0;
8811ec1e82fSSascha Hauer 
8821ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
8831ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
8848391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
8850f06c027SRobin Gong 	sdmac->pc_to_pc = 0;
8861ec1e82fSSascha Hauer 
8871ec1e82fSSascha Hauer 	switch (peripheral_type) {
8881ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
8890f06c027SRobin Gong 		emi_2_emi = sdma->script_addrs->ap_2_ap_addr;
8901ec1e82fSSascha Hauer 		break;
8911ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
8921ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
8931ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
8941ec1e82fSSascha Hauer 		break;
8951ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
8961ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
8971ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
8981ec1e82fSSascha Hauer 		break;
8991ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
9001ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
9011ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
9021ec1e82fSSascha Hauer 		break;
9031ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
9041ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
9051ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
9061ec1e82fSSascha Hauer 		break;
9071ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
9081ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
9091ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
9101ec1e82fSSascha Hauer 		break;
9111ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
9121ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
9131ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
91429aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
9151ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
9161ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
9171ec1e82fSSascha Hauer 		break;
9181a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
9191a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
9201a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
9211a895578SNicolin Chen 		break;
9221ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
9231ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
9241ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
9251ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
9261ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
9271ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
9281ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
9291ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
9301ec1e82fSSascha Hauer 		break;
9311ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
9321ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
9331ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
9341ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
9351ec1e82fSSascha Hauer 		break;
936f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
937f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
938f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
939f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
940f892afb0SNicolin Chen 		break;
9411ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
9421ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
9431ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
9441ec1e82fSSascha Hauer 		break;
9451ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
9461ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
9471ec1e82fSSascha Hauer 		break;
9481ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
9491ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
9501ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
9511ec1e82fSSascha Hauer 		break;
9521ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
9531ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
9541ec1e82fSSascha Hauer 		break;
9551ec1e82fSSascha Hauer 	default:
9561ec1e82fSSascha Hauer 		break;
9571ec1e82fSSascha Hauer 	}
9581ec1e82fSSascha Hauer 
9591ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
9601ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
9618391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
9620f06c027SRobin Gong 	sdmac->pc_to_pc = emi_2_emi;
9631ec1e82fSSascha Hauer }
9641ec1e82fSSascha Hauer 
9651ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
9661ec1e82fSSascha Hauer {
9671ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
9681ec1e82fSSascha Hauer 	int channel = sdmac->channel;
9691ec1e82fSSascha Hauer 	int load_address;
9701ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
97176c33d27SSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->bd0;
9721ec1e82fSSascha Hauer 	int ret;
9732ccaef05SRichard Zhao 	unsigned long flags;
9741ec1e82fSSascha Hauer 
9758391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
9761ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
9778391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
9788391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
9790f06c027SRobin Gong 	else if (sdmac->direction == DMA_MEM_TO_MEM)
9800f06c027SRobin Gong 		load_address = sdmac->pc_to_pc;
9818391ecf4SShengjiu Wang 	else
9821ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
9831ec1e82fSSascha Hauer 
9841ec1e82fSSascha Hauer 	if (load_address < 0)
9851ec1e82fSSascha Hauer 		return load_address;
9861ec1e82fSSascha Hauer 
9871ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
9880bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
9891ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
9901ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
9910bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
9920bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
9931ec1e82fSSascha Hauer 
9942ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
99573eab978SSascha Hauer 
9961ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
9971ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
9981ec1e82fSSascha Hauer 
9991ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
10001ec1e82fSSascha Hauer 	 * and watermark level
10011ec1e82fSSascha Hauer 	 */
10020bbc1413SRichard Zhao 	context->gReg[0] = sdmac->event_mask[1];
10030bbc1413SRichard Zhao 	context->gReg[1] = sdmac->event_mask[0];
10041ec1e82fSSascha Hauer 	context->gReg[2] = sdmac->per_addr;
10051ec1e82fSSascha Hauer 	context->gReg[6] = sdmac->shp_addr;
10061ec1e82fSSascha Hauer 	context->gReg[7] = sdmac->watermark_level;
10071ec1e82fSSascha Hauer 
10081ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
10091ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
10101ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
10111ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
10121ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
10132ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
10141ec1e82fSSascha Hauer 
10152ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
101673eab978SSascha Hauer 
10171ec1e82fSSascha Hauer 	return ret;
10181ec1e82fSSascha Hauer }
10191ec1e82fSSascha Hauer 
10207b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
10211ec1e82fSSascha Hauer {
102257b772b8SRobin Gong 	return container_of(chan, struct sdma_channel, vc.chan);
10237b350ab0SMaxime Ripard }
10247b350ab0SMaxime Ripard 
10257b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
10267b350ab0SMaxime Ripard {
10277b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
10281ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10291ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10301ec1e82fSSascha Hauer 
10310bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
10321ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
10337b350ab0SMaxime Ripard 
10347b350ab0SMaxime Ripard 	return 0;
10351ec1e82fSSascha Hauer }
10361ec1e82fSSascha Hauer 
10377f3ff14bSJiada Wang static int sdma_disable_channel_with_delay(struct dma_chan *chan)
10387f3ff14bSJiada Wang {
103957b772b8SRobin Gong 	struct sdma_channel *sdmac = to_sdma_chan(chan);
104057b772b8SRobin Gong 	unsigned long flags;
104157b772b8SRobin Gong 	LIST_HEAD(head);
104257b772b8SRobin Gong 
10437f3ff14bSJiada Wang 	sdma_disable_channel(chan);
104457b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
104557b772b8SRobin Gong 	vchan_get_all_descriptors(&sdmac->vc, &head);
104657b772b8SRobin Gong 	sdmac->desc = NULL;
104757b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
104857b772b8SRobin Gong 	vchan_dma_desc_free_list(&sdmac->vc, &head);
10497f3ff14bSJiada Wang 
10507f3ff14bSJiada Wang 	/*
10517f3ff14bSJiada Wang 	 * According to NXP R&D team a delay of one BD SDMA cost time
10527f3ff14bSJiada Wang 	 * (maximum is 1ms) should be added after disable of the channel
10537f3ff14bSJiada Wang 	 * bit, to ensure SDMA core has really been stopped after SDMA
10547f3ff14bSJiada Wang 	 * clients call .device_terminate_all.
10557f3ff14bSJiada Wang 	 */
10567f3ff14bSJiada Wang 	mdelay(1);
10577f3ff14bSJiada Wang 
10587f3ff14bSJiada Wang 	return 0;
10597f3ff14bSJiada Wang }
10607f3ff14bSJiada Wang 
10618391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
10628391ecf4SShengjiu Wang {
10638391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
10648391ecf4SShengjiu Wang 
10658391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
10668391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
10678391ecf4SShengjiu Wang 
10688391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
10698391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
10708391ecf4SShengjiu Wang 
10718391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
10728391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
10738391ecf4SShengjiu Wang 
10748391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
10758391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
10768391ecf4SShengjiu Wang 
10778391ecf4SShengjiu Wang 	/*
10788391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
10798391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
10808391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
10818391ecf4SShengjiu Wang 	 */
10828391ecf4SShengjiu Wang 	if (lwml > hwml) {
10838391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
10848391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
10858391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
10868391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
10878391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
10888391ecf4SShengjiu Wang 	}
10898391ecf4SShengjiu Wang 
10908391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
10918391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
10928391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
10938391ecf4SShengjiu Wang 
10948391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
10958391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
10968391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
10978391ecf4SShengjiu Wang 
10988391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
10998391ecf4SShengjiu Wang }
11008391ecf4SShengjiu Wang 
11017b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
11021ec1e82fSSascha Hauer {
11037b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11041ec1e82fSSascha Hauer 	int ret;
11051ec1e82fSSascha Hauer 
11067b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
11071ec1e82fSSascha Hauer 
11080bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
11090bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
11101ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
11111ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
11121ec1e82fSSascha Hauer 
11131ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
11141ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
11151ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
11161ec1e82fSSascha Hauer 		break;
11171ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
11181ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
11191ec1e82fSSascha Hauer 		break;
11201ec1e82fSSascha Hauer 	default:
11211ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
11221ec1e82fSSascha Hauer 		break;
11231ec1e82fSSascha Hauer 	}
11241ec1e82fSSascha Hauer 
11251ec1e82fSSascha Hauer 	sdma_get_pc(sdmac, sdmac->peripheral_type);
11261ec1e82fSSascha Hauer 
11271ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
11281ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
11291ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
11301ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
11318391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
11328391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
11338391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
11348391ecf4SShengjiu Wang 		} else
11350bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
11368391ecf4SShengjiu Wang 
11371ec1e82fSSascha Hauer 		/* Address */
11381ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
11398391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
11401ec1e82fSSascha Hauer 	} else {
11411ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
11421ec1e82fSSascha Hauer 	}
11431ec1e82fSSascha Hauer 
11441ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
11451ec1e82fSSascha Hauer 
11461ec1e82fSSascha Hauer 	return ret;
11471ec1e82fSSascha Hauer }
11481ec1e82fSSascha Hauer 
11491ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
11501ec1e82fSSascha Hauer 		unsigned int priority)
11511ec1e82fSSascha Hauer {
11521ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11531ec1e82fSSascha Hauer 	int channel = sdmac->channel;
11541ec1e82fSSascha Hauer 
11551ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
11561ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
11571ec1e82fSSascha Hauer 		return -EINVAL;
11581ec1e82fSSascha Hauer 	}
11591ec1e82fSSascha Hauer 
1160c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
11611ec1e82fSSascha Hauer 
11621ec1e82fSSascha Hauer 	return 0;
11631ec1e82fSSascha Hauer }
11641ec1e82fSSascha Hauer 
116557b772b8SRobin Gong static int sdma_request_channel0(struct sdma_engine *sdma)
11661ec1e82fSSascha Hauer {
11671ec1e82fSSascha Hauer 	int ret = -EBUSY;
11681ec1e82fSSascha Hauer 
116957b772b8SRobin Gong 	sdma->bd0 = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdma->bd0_phys,
117057b772b8SRobin Gong 					GFP_NOWAIT);
117157b772b8SRobin Gong 	if (!sdma->bd0) {
11721ec1e82fSSascha Hauer 		ret = -ENOMEM;
11731ec1e82fSSascha Hauer 		goto out;
11741ec1e82fSSascha Hauer 	}
11751ec1e82fSSascha Hauer 
117657b772b8SRobin Gong 	sdma->channel_control[0].base_bd_ptr = sdma->bd0_phys;
117757b772b8SRobin Gong 	sdma->channel_control[0].current_bd_ptr = sdma->bd0_phys;
11781ec1e82fSSascha Hauer 
117957b772b8SRobin Gong 	sdma_set_channel_priority(&sdma->channel[0], MXC_SDMA_DEFAULT_PRIORITY);
11801ec1e82fSSascha Hauer 	return 0;
11811ec1e82fSSascha Hauer out:
11821ec1e82fSSascha Hauer 
11831ec1e82fSSascha Hauer 	return ret;
11841ec1e82fSSascha Hauer }
11851ec1e82fSSascha Hauer 
118657b772b8SRobin Gong 
118757b772b8SRobin Gong static int sdma_alloc_bd(struct sdma_desc *desc)
11881ec1e82fSSascha Hauer {
118957b772b8SRobin Gong 	int ret = 0;
11901ec1e82fSSascha Hauer 
1191c1199875SVinod Koul 	desc->bd = dma_pool_alloc(desc->sdmac->bd_pool, GFP_NOWAIT,
1192fe5b85c6SRobin Gong 				  &desc->bd_phys);
119357b772b8SRobin Gong 	if (!desc->bd) {
119457b772b8SRobin Gong 		ret = -ENOMEM;
119557b772b8SRobin Gong 		goto out;
119657b772b8SRobin Gong 	}
119757b772b8SRobin Gong out:
119857b772b8SRobin Gong 	return ret;
119957b772b8SRobin Gong }
12001ec1e82fSSascha Hauer 
120157b772b8SRobin Gong static void sdma_free_bd(struct sdma_desc *desc)
120257b772b8SRobin Gong {
1203fe5b85c6SRobin Gong 	dma_pool_free(desc->sdmac->bd_pool, desc->bd, desc->bd_phys);
120457b772b8SRobin Gong }
12051ec1e82fSSascha Hauer 
120657b772b8SRobin Gong static void sdma_desc_free(struct virt_dma_desc *vd)
120757b772b8SRobin Gong {
120857b772b8SRobin Gong 	struct sdma_desc *desc = container_of(vd, struct sdma_desc, vd);
120957b772b8SRobin Gong 
121057b772b8SRobin Gong 	sdma_free_bd(desc);
121157b772b8SRobin Gong 	kfree(desc);
12121ec1e82fSSascha Hauer }
12131ec1e82fSSascha Hauer 
12141ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
12151ec1e82fSSascha Hauer {
12161ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
12171ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
12180f06c027SRobin Gong 	struct imx_dma_data mem_data;
12191ec1e82fSSascha Hauer 	int prio, ret;
12201ec1e82fSSascha Hauer 
12210f06c027SRobin Gong 	/*
12220f06c027SRobin Gong 	 * MEMCPY may never setup chan->private by filter function such as
12230f06c027SRobin Gong 	 * dmatest, thus create 'struct imx_dma_data mem_data' for this case.
12240f06c027SRobin Gong 	 * Please note in any other slave case, you have to setup chan->private
12250f06c027SRobin Gong 	 * with 'struct imx_dma_data' in your own filter function if you want to
12260f06c027SRobin Gong 	 * request dma channel by dma_request_channel() rather than
12270f06c027SRobin Gong 	 * dma_request_slave_channel(). Othwise, 'MEMCPY in case?' will appear
12280f06c027SRobin Gong 	 * to warn you to correct your filter function.
12290f06c027SRobin Gong 	 */
12300f06c027SRobin Gong 	if (!data) {
12310f06c027SRobin Gong 		dev_dbg(sdmac->sdma->dev, "MEMCPY in case?\n");
12320f06c027SRobin Gong 		mem_data.priority = 2;
12330f06c027SRobin Gong 		mem_data.peripheral_type = IMX_DMATYPE_MEMORY;
12340f06c027SRobin Gong 		mem_data.dma_request = 0;
12350f06c027SRobin Gong 		mem_data.dma_request2 = 0;
12360f06c027SRobin Gong 		data = &mem_data;
12370f06c027SRobin Gong 
12380f06c027SRobin Gong 		sdma_get_pc(sdmac, IMX_DMATYPE_MEMORY);
12390f06c027SRobin Gong 	}
12401ec1e82fSSascha Hauer 
12411ec1e82fSSascha Hauer 	switch (data->priority) {
12421ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
12431ec1e82fSSascha Hauer 		prio = 3;
12441ec1e82fSSascha Hauer 		break;
12451ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
12461ec1e82fSSascha Hauer 		prio = 2;
12471ec1e82fSSascha Hauer 		break;
12481ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
12491ec1e82fSSascha Hauer 	default:
12501ec1e82fSSascha Hauer 		prio = 1;
12511ec1e82fSSascha Hauer 		break;
12521ec1e82fSSascha Hauer 	}
12531ec1e82fSSascha Hauer 
12541ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
12551ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
12568391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1257c2c744d3SRichard Zhao 
1258b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1259b93edcddSFabio Estevam 	if (ret)
1260b93edcddSFabio Estevam 		return ret;
1261b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1262b93edcddSFabio Estevam 	if (ret)
1263b93edcddSFabio Estevam 		goto disable_clk_ipg;
1264c2c744d3SRichard Zhao 
12653bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
12661ec1e82fSSascha Hauer 	if (ret)
1267b93edcddSFabio Estevam 		goto disable_clk_ahb;
12681ec1e82fSSascha Hauer 
1269fe5b85c6SRobin Gong 	sdmac->bd_pool = dma_pool_create("bd_pool", chan->device->dev,
1270fe5b85c6SRobin Gong 				sizeof(struct sdma_buffer_descriptor),
1271fe5b85c6SRobin Gong 				32, 0);
1272fe5b85c6SRobin Gong 
12731ec1e82fSSascha Hauer 	return 0;
1274b93edcddSFabio Estevam 
1275b93edcddSFabio Estevam disable_clk_ahb:
1276b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1277b93edcddSFabio Estevam disable_clk_ipg:
1278b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1279b93edcddSFabio Estevam 	return ret;
12801ec1e82fSSascha Hauer }
12811ec1e82fSSascha Hauer 
12821ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
12831ec1e82fSSascha Hauer {
12841ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
12851ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
12861ec1e82fSSascha Hauer 
128757b772b8SRobin Gong 	sdma_disable_channel_with_delay(chan);
12881ec1e82fSSascha Hauer 
12891ec1e82fSSascha Hauer 	if (sdmac->event_id0)
12901ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id0);
12911ec1e82fSSascha Hauer 	if (sdmac->event_id1)
12921ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
12931ec1e82fSSascha Hauer 
12941ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
12951ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
12961ec1e82fSSascha Hauer 
12971ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
12981ec1e82fSSascha Hauer 
12997560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
13007560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
1301fe5b85c6SRobin Gong 
1302fe5b85c6SRobin Gong 	dma_pool_destroy(sdmac->bd_pool);
1303fe5b85c6SRobin Gong 	sdmac->bd_pool = NULL;
13041ec1e82fSSascha Hauer }
13051ec1e82fSSascha Hauer 
130621420841SRobin Gong static struct sdma_desc *sdma_transfer_init(struct sdma_channel *sdmac,
130721420841SRobin Gong 				enum dma_transfer_direction direction, u32 bds)
130821420841SRobin Gong {
130921420841SRobin Gong 	struct sdma_desc *desc;
131021420841SRobin Gong 
131121420841SRobin Gong 	desc = kzalloc((sizeof(*desc)), GFP_NOWAIT);
131221420841SRobin Gong 	if (!desc)
131321420841SRobin Gong 		goto err_out;
131421420841SRobin Gong 
131521420841SRobin Gong 	sdmac->status = DMA_IN_PROGRESS;
131621420841SRobin Gong 	sdmac->direction = direction;
131721420841SRobin Gong 	sdmac->flags = 0;
131821420841SRobin Gong 
131921420841SRobin Gong 	desc->chn_count = 0;
132021420841SRobin Gong 	desc->chn_real_count = 0;
132121420841SRobin Gong 	desc->buf_tail = 0;
132221420841SRobin Gong 	desc->buf_ptail = 0;
132321420841SRobin Gong 	desc->sdmac = sdmac;
132421420841SRobin Gong 	desc->num_bd = bds;
132521420841SRobin Gong 
132621420841SRobin Gong 	if (sdma_alloc_bd(desc))
132721420841SRobin Gong 		goto err_desc_out;
132821420841SRobin Gong 
13290f06c027SRobin Gong 	/* No slave_config called in MEMCPY case, so do here */
13300f06c027SRobin Gong 	if (direction == DMA_MEM_TO_MEM)
13310f06c027SRobin Gong 		sdma_config_ownership(sdmac, false, true, false);
13320f06c027SRobin Gong 
133321420841SRobin Gong 	if (sdma_load_context(sdmac))
133421420841SRobin Gong 		goto err_desc_out;
133521420841SRobin Gong 
133621420841SRobin Gong 	return desc;
133721420841SRobin Gong 
133821420841SRobin Gong err_desc_out:
133921420841SRobin Gong 	kfree(desc);
134021420841SRobin Gong err_out:
134121420841SRobin Gong 	return NULL;
134221420841SRobin Gong }
134321420841SRobin Gong 
13440f06c027SRobin Gong static struct dma_async_tx_descriptor *sdma_prep_memcpy(
13450f06c027SRobin Gong 		struct dma_chan *chan, dma_addr_t dma_dst,
13460f06c027SRobin Gong 		dma_addr_t dma_src, size_t len, unsigned long flags)
13470f06c027SRobin Gong {
13480f06c027SRobin Gong 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13490f06c027SRobin Gong 	struct sdma_engine *sdma = sdmac->sdma;
13500f06c027SRobin Gong 	int channel = sdmac->channel;
13510f06c027SRobin Gong 	size_t count;
13520f06c027SRobin Gong 	int i = 0, param;
13530f06c027SRobin Gong 	struct sdma_buffer_descriptor *bd;
13540f06c027SRobin Gong 	struct sdma_desc *desc;
13550f06c027SRobin Gong 
13560f06c027SRobin Gong 	if (!chan || !len)
13570f06c027SRobin Gong 		return NULL;
13580f06c027SRobin Gong 
13590f06c027SRobin Gong 	dev_dbg(sdma->dev, "memcpy: %pad->%pad, len=%zu, channel=%d.\n",
13600f06c027SRobin Gong 		&dma_src, &dma_dst, len, channel);
13610f06c027SRobin Gong 
13620f06c027SRobin Gong 	desc = sdma_transfer_init(sdmac, DMA_MEM_TO_MEM,
13630f06c027SRobin Gong 					len / SDMA_BD_MAX_CNT + 1);
13640f06c027SRobin Gong 	if (!desc)
13650f06c027SRobin Gong 		return NULL;
13660f06c027SRobin Gong 
13670f06c027SRobin Gong 	do {
13680f06c027SRobin Gong 		count = min_t(size_t, len, SDMA_BD_MAX_CNT);
13690f06c027SRobin Gong 		bd = &desc->bd[i];
13700f06c027SRobin Gong 		bd->buffer_addr = dma_src;
13710f06c027SRobin Gong 		bd->ext_buffer_addr = dma_dst;
13720f06c027SRobin Gong 		bd->mode.count = count;
13730f06c027SRobin Gong 		desc->chn_count += count;
13740f06c027SRobin Gong 		bd->mode.command = 0;
13750f06c027SRobin Gong 
13760f06c027SRobin Gong 		dma_src += count;
13770f06c027SRobin Gong 		dma_dst += count;
13780f06c027SRobin Gong 		len -= count;
13790f06c027SRobin Gong 		i++;
13800f06c027SRobin Gong 
13810f06c027SRobin Gong 		param = BD_DONE | BD_EXTD | BD_CONT;
13820f06c027SRobin Gong 		/* last bd */
13830f06c027SRobin Gong 		if (!len) {
13840f06c027SRobin Gong 			param |= BD_INTR;
13850f06c027SRobin Gong 			param |= BD_LAST;
13860f06c027SRobin Gong 			param &= ~BD_CONT;
13870f06c027SRobin Gong 		}
13880f06c027SRobin Gong 
13890f06c027SRobin Gong 		dev_dbg(sdma->dev, "entry %d: count: %zd dma: 0x%x %s%s\n",
13900f06c027SRobin Gong 				i, count, bd->buffer_addr,
13910f06c027SRobin Gong 				param & BD_WRAP ? "wrap" : "",
13920f06c027SRobin Gong 				param & BD_INTR ? " intr" : "");
13930f06c027SRobin Gong 
13940f06c027SRobin Gong 		bd->mode.status = param;
13950f06c027SRobin Gong 	} while (len);
13960f06c027SRobin Gong 
13970f06c027SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
13980f06c027SRobin Gong }
13990f06c027SRobin Gong 
14001ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
14011ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1402db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1403185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
14041ec1e82fSSascha Hauer {
14051ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14061ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
1407ad78b000SVinod Koul 	int i, count;
140823889c63SSascha Hauer 	int channel = sdmac->channel;
14091ec1e82fSSascha Hauer 	struct scatterlist *sg;
141057b772b8SRobin Gong 	struct sdma_desc *desc;
14111ec1e82fSSascha Hauer 
1412*107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1413*107d0644SVinod Koul 
141421420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, sg_len);
141557b772b8SRobin Gong 	if (!desc)
141657b772b8SRobin Gong 		goto err_out;
141757b772b8SRobin Gong 
14181ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
14191ec1e82fSSascha Hauer 			sg_len, channel);
14201ec1e82fSSascha Hauer 
14211ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
142276c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
14231ec1e82fSSascha Hauer 		int param;
14241ec1e82fSSascha Hauer 
1425d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
14261ec1e82fSSascha Hauer 
1427fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
14281ec1e82fSSascha Hauer 
14294a6b2e8aSRobin Gong 		if (count > SDMA_BD_MAX_CNT) {
14301ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
14314a6b2e8aSRobin Gong 					channel, count, SDMA_BD_MAX_CNT);
143257b772b8SRobin Gong 			goto err_bd_out;
14331ec1e82fSSascha Hauer 		}
14341ec1e82fSSascha Hauer 
14351ec1e82fSSascha Hauer 		bd->mode.count = count;
143676c33d27SSascha Hauer 		desc->chn_count += count;
14371ec1e82fSSascha Hauer 
1438ad78b000SVinod Koul 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
143957b772b8SRobin Gong 			goto err_bd_out;
14401fa81c27SSascha Hauer 
14411fa81c27SSascha Hauer 		switch (sdmac->word_size) {
14421fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
14431ec1e82fSSascha Hauer 			bd->mode.command = 0;
14441fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
144557b772b8SRobin Gong 				goto err_bd_out;
14461fa81c27SSascha Hauer 			break;
14471fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
14481fa81c27SSascha Hauer 			bd->mode.command = 2;
14491fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
145057b772b8SRobin Gong 				goto err_bd_out;
14511fa81c27SSascha Hauer 			break;
14521fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
14531fa81c27SSascha Hauer 			bd->mode.command = 1;
14541fa81c27SSascha Hauer 			break;
14551fa81c27SSascha Hauer 		default:
145657b772b8SRobin Gong 			goto err_bd_out;
14571fa81c27SSascha Hauer 		}
14581ec1e82fSSascha Hauer 
14591ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
14601ec1e82fSSascha Hauer 
1461341b9419SShawn Guo 		if (i + 1 == sg_len) {
14621ec1e82fSSascha Hauer 			param |= BD_INTR;
1463341b9419SShawn Guo 			param |= BD_LAST;
1464341b9419SShawn Guo 			param &= ~BD_CONT;
14651ec1e82fSSascha Hauer 		}
14661ec1e82fSSascha Hauer 
1467c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1468c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
14691ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
14701ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
14711ec1e82fSSascha Hauer 
14721ec1e82fSSascha Hauer 		bd->mode.status = param;
14731ec1e82fSSascha Hauer 	}
14741ec1e82fSSascha Hauer 
147557b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
147657b772b8SRobin Gong err_bd_out:
147757b772b8SRobin Gong 	sdma_free_bd(desc);
147857b772b8SRobin Gong 	kfree(desc);
14791ec1e82fSSascha Hauer err_out:
14804b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
14811ec1e82fSSascha Hauer 	return NULL;
14821ec1e82fSSascha Hauer }
14831ec1e82fSSascha Hauer 
14841ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
14851ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1486185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
148731c1e5a1SLaurent Pinchart 		unsigned long flags)
14881ec1e82fSSascha Hauer {
14891ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14901ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
14911ec1e82fSSascha Hauer 	int num_periods = buf_len / period_len;
149223889c63SSascha Hauer 	int channel = sdmac->channel;
149321420841SRobin Gong 	int i = 0, buf = 0;
149457b772b8SRobin Gong 	struct sdma_desc *desc;
14951ec1e82fSSascha Hauer 
14961ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
14971ec1e82fSSascha Hauer 
1498*107d0644SVinod Koul 	sdma_config_write(chan, &sdmac->slave_config, direction);
1499*107d0644SVinod Koul 
150021420841SRobin Gong 	desc = sdma_transfer_init(sdmac, direction, num_periods);
150157b772b8SRobin Gong 	if (!desc)
150257b772b8SRobin Gong 		goto err_out;
150357b772b8SRobin Gong 
150476c33d27SSascha Hauer 	desc->period_len = period_len;
15058e2e27c7SRichard Zhao 
15061ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
15071ec1e82fSSascha Hauer 
15084a6b2e8aSRobin Gong 	if (period_len > SDMA_BD_MAX_CNT) {
1509ba6ab3b3SArvind Yadav 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %zu > %d\n",
15104a6b2e8aSRobin Gong 				channel, period_len, SDMA_BD_MAX_CNT);
151157b772b8SRobin Gong 		goto err_bd_out;
15121ec1e82fSSascha Hauer 	}
15131ec1e82fSSascha Hauer 
15141ec1e82fSSascha Hauer 	while (buf < buf_len) {
151576c33d27SSascha Hauer 		struct sdma_buffer_descriptor *bd = &desc->bd[i];
15161ec1e82fSSascha Hauer 		int param;
15171ec1e82fSSascha Hauer 
15181ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
15191ec1e82fSSascha Hauer 
15201ec1e82fSSascha Hauer 		bd->mode.count = period_len;
15211ec1e82fSSascha Hauer 
15221ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
152357b772b8SRobin Gong 			goto err_bd_out;
15241ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
15251ec1e82fSSascha Hauer 			bd->mode.command = 0;
15261ec1e82fSSascha Hauer 		else
15271ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
15281ec1e82fSSascha Hauer 
15291ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
15301ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
15311ec1e82fSSascha Hauer 			param |= BD_WRAP;
15321ec1e82fSSascha Hauer 
1533ba6ab3b3SArvind Yadav 		dev_dbg(sdma->dev, "entry %d: count: %zu dma: %#llx %s%s\n",
1534c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
15351ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
15361ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
15371ec1e82fSSascha Hauer 
15381ec1e82fSSascha Hauer 		bd->mode.status = param;
15391ec1e82fSSascha Hauer 
15401ec1e82fSSascha Hauer 		dma_addr += period_len;
15411ec1e82fSSascha Hauer 		buf += period_len;
15421ec1e82fSSascha Hauer 
15431ec1e82fSSascha Hauer 		i++;
15441ec1e82fSSascha Hauer 	}
15451ec1e82fSSascha Hauer 
154657b772b8SRobin Gong 	return vchan_tx_prep(&sdmac->vc, &desc->vd, flags);
154757b772b8SRobin Gong err_bd_out:
154857b772b8SRobin Gong 	sdma_free_bd(desc);
154957b772b8SRobin Gong 	kfree(desc);
15501ec1e82fSSascha Hauer err_out:
15511ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
15521ec1e82fSSascha Hauer 	return NULL;
15531ec1e82fSSascha Hauer }
15541ec1e82fSSascha Hauer 
1555*107d0644SVinod Koul static int sdma_config_write(struct dma_chan *chan,
1556*107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg,
1557*107d0644SVinod Koul 		       enum dma_transfer_direction direction)
15581ec1e82fSSascha Hauer {
15591ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
15601ec1e82fSSascha Hauer 
1561*107d0644SVinod Koul 	if (direction == DMA_DEV_TO_MEM) {
15621ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
156394ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
156494ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
15651ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
1566*107d0644SVinod Koul 	} else if (direction == DMA_DEV_TO_DEV) {
15678391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
15688391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
15698391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
15708391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
15718391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
15728391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
15738391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
15741ec1e82fSSascha Hauer 	} else {
15751ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
157694ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
157794ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
15781ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
15791ec1e82fSSascha Hauer 	}
1580*107d0644SVinod Koul 	sdmac->direction = direction;
15817b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
15821ec1e82fSSascha Hauer }
15831ec1e82fSSascha Hauer 
1584*107d0644SVinod Koul static int sdma_config(struct dma_chan *chan,
1585*107d0644SVinod Koul 		       struct dma_slave_config *dmaengine_cfg)
1586*107d0644SVinod Koul {
1587*107d0644SVinod Koul 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1588*107d0644SVinod Koul 
1589*107d0644SVinod Koul 	memcpy(&sdmac->slave_config, dmaengine_cfg, sizeof(*dmaengine_cfg));
1590*107d0644SVinod Koul 
1591*107d0644SVinod Koul 	/* Set ENBLn earlier to make sure dma request triggered after that */
1592*107d0644SVinod Koul 	if (sdmac->event_id0) {
1593*107d0644SVinod Koul 		if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
1594*107d0644SVinod Koul 			return -EINVAL;
1595*107d0644SVinod Koul 		sdma_event_enable(sdmac, sdmac->event_id0);
1596*107d0644SVinod Koul 	}
1597*107d0644SVinod Koul 
1598*107d0644SVinod Koul 	if (sdmac->event_id1) {
1599*107d0644SVinod Koul 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
1600*107d0644SVinod Koul 			return -EINVAL;
1601*107d0644SVinod Koul 		sdma_event_enable(sdmac, sdmac->event_id1);
1602*107d0644SVinod Koul 	}
1603*107d0644SVinod Koul 
1604*107d0644SVinod Koul 	return 0;
1605*107d0644SVinod Koul }
1606*107d0644SVinod Koul 
16071ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
16081ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
16091ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
16101ec1e82fSSascha Hauer {
16111ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
161257b772b8SRobin Gong 	struct sdma_desc *desc;
1613d1a792f3SRussell King - ARM Linux 	u32 residue;
161457b772b8SRobin Gong 	struct virt_dma_desc *vd;
161557b772b8SRobin Gong 	enum dma_status ret;
161657b772b8SRobin Gong 	unsigned long flags;
1617d1a792f3SRussell King - ARM Linux 
161857b772b8SRobin Gong 	ret = dma_cookie_status(chan, cookie, txstate);
161957b772b8SRobin Gong 	if (ret == DMA_COMPLETE || !txstate)
162057b772b8SRobin Gong 		return ret;
162157b772b8SRobin Gong 
162257b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
162357b772b8SRobin Gong 	vd = vchan_find_desc(&sdmac->vc, cookie);
162457b772b8SRobin Gong 	if (vd) {
162557b772b8SRobin Gong 		desc = to_sdma_desc(&vd->tx);
1626d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
162776c33d27SSascha Hauer 			residue = (desc->num_bd - desc->buf_ptail) *
162876c33d27SSascha Hauer 				desc->period_len - desc->chn_real_count;
1629d1a792f3SRussell King - ARM Linux 		else
163076c33d27SSascha Hauer 			residue = desc->chn_count - desc->chn_real_count;
163157b772b8SRobin Gong 	} else if (sdmac->desc && sdmac->desc->vd.tx.cookie == cookie) {
163257b772b8SRobin Gong 		residue = sdmac->desc->chn_count - sdmac->desc->chn_real_count;
163357b772b8SRobin Gong 	} else {
163457b772b8SRobin Gong 		residue = 0;
163557b772b8SRobin Gong 	}
163657b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
16371ec1e82fSSascha Hauer 
1638e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1639d1a792f3SRussell King - ARM Linux 			 residue);
16401ec1e82fSSascha Hauer 
16418a965911SShawn Guo 	return sdmac->status;
16421ec1e82fSSascha Hauer }
16431ec1e82fSSascha Hauer 
16441ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
16451ec1e82fSSascha Hauer {
16462b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
164757b772b8SRobin Gong 	unsigned long flags;
16482b4f130eSSascha Hauer 
164957b772b8SRobin Gong 	spin_lock_irqsave(&sdmac->vc.lock, flags);
165057b772b8SRobin Gong 	if (vchan_issue_pending(&sdmac->vc) && !sdmac->desc)
165157b772b8SRobin Gong 		sdma_start_desc(sdmac);
165257b772b8SRobin Gong 	spin_unlock_irqrestore(&sdmac->vc.lock, flags);
16531ec1e82fSSascha Hauer }
16541ec1e82fSSascha Hauer 
16555b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1656cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1657a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	41
1658b7d2648aSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4	42
16595b28aa31SSascha Hauer 
16605b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
16615b28aa31SSascha Hauer 		const struct sdma_script_start_addrs *addr)
16625b28aa31SSascha Hauer {
16635b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
16645b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
16655b28aa31SSascha Hauer 	int i;
16665b28aa31SSascha Hauer 
166770dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
166870dabaedSNicolin Chen 	if (!sdma->script_number)
166970dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
167070dabaedSNicolin Chen 
1671cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
16725b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
16735b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
16745b28aa31SSascha Hauer }
16755b28aa31SSascha Hauer 
16767b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
16775b28aa31SSascha Hauer {
16787b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
16795b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
16805b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
16815b28aa31SSascha Hauer 	unsigned short *ram_code;
16825b28aa31SSascha Hauer 
16837b4b88e0SSascha Hauer 	if (!fw) {
16840f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
16850f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
16867b4b88e0SSascha Hauer 		return;
16877b4b88e0SSascha Hauer 	}
16885b28aa31SSascha Hauer 
16895b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
16905b28aa31SSascha Hauer 		goto err_firmware;
16915b28aa31SSascha Hauer 
16925b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
16935b28aa31SSascha Hauer 
16945b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
16955b28aa31SSascha Hauer 		goto err_firmware;
16965b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
16975b28aa31SSascha Hauer 		goto err_firmware;
1698cd72b846SNicolin Chen 	switch (header->version_major) {
1699cd72b846SNicolin Chen 	case 1:
1700cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1701cd72b846SNicolin Chen 		break;
1702cd72b846SNicolin Chen 	case 2:
1703cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1704cd72b846SNicolin Chen 		break;
1705a572460bSFabio Estevam 	case 3:
1706a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1707a572460bSFabio Estevam 		break;
1708b7d2648aSFabio Estevam 	case 4:
1709b7d2648aSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1710b7d2648aSFabio Estevam 		break;
1711cd72b846SNicolin Chen 	default:
1712cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1713cd72b846SNicolin Chen 		goto err_firmware;
1714cd72b846SNicolin Chen 	}
17155b28aa31SSascha Hauer 
17165b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
17175b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
17185b28aa31SSascha Hauer 
17197560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
17207560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
17215b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
17225b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
17235b28aa31SSascha Hauer 			header->ram_code_size,
17246866fd3bSSascha Hauer 			addr->ram_code_start_addr);
17257560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
17267560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
17275b28aa31SSascha Hauer 
17285b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
17295b28aa31SSascha Hauer 
17305b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
17315b28aa31SSascha Hauer 			header->version_major,
17325b28aa31SSascha Hauer 			header->version_minor);
17335b28aa31SSascha Hauer 
17345b28aa31SSascha Hauer err_firmware:
17355b28aa31SSascha Hauer 	release_firmware(fw);
17367b4b88e0SSascha Hauer }
17377b4b88e0SSascha Hauer 
1738d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1739d078cd1bSZidan Wang 
174029f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1741d078cd1bSZidan Wang {
1742d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1743d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1744d078cd1bSZidan Wang 	struct property *event_remap;
1745d078cd1bSZidan Wang 	struct regmap *gpr;
1746d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
1747d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
1748d078cd1bSZidan Wang 	int ret = 0;
1749d078cd1bSZidan Wang 
1750d078cd1bSZidan Wang 	if (IS_ERR(np) || IS_ERR(gpr_np))
1751d078cd1bSZidan Wang 		goto out;
1752d078cd1bSZidan Wang 
1753d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
1754d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1755d078cd1bSZidan Wang 	if (!num_map) {
1756ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
1757d078cd1bSZidan Wang 		goto out;
1758d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
1759d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
1760d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
1761d078cd1bSZidan Wang 		ret = -EINVAL;
1762d078cd1bSZidan Wang 		goto out;
1763d078cd1bSZidan Wang 	}
1764d078cd1bSZidan Wang 
1765d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
1766d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
1767d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
1768d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
1769d078cd1bSZidan Wang 		goto out;
1770d078cd1bSZidan Wang 	}
1771d078cd1bSZidan Wang 
1772d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1773d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
1774d078cd1bSZidan Wang 		if (ret) {
1775d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1776d078cd1bSZidan Wang 					propname, i);
1777d078cd1bSZidan Wang 			goto out;
1778d078cd1bSZidan Wang 		}
1779d078cd1bSZidan Wang 
1780d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1781d078cd1bSZidan Wang 		if (ret) {
1782d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1783d078cd1bSZidan Wang 					propname, i + 1);
1784d078cd1bSZidan Wang 			goto out;
1785d078cd1bSZidan Wang 		}
1786d078cd1bSZidan Wang 
1787d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
1788d078cd1bSZidan Wang 		if (ret) {
1789d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1790d078cd1bSZidan Wang 					propname, i + 2);
1791d078cd1bSZidan Wang 			goto out;
1792d078cd1bSZidan Wang 		}
1793d078cd1bSZidan Wang 
1794d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1795d078cd1bSZidan Wang 	}
1796d078cd1bSZidan Wang 
1797d078cd1bSZidan Wang out:
1798d078cd1bSZidan Wang 	if (!IS_ERR(gpr_np))
1799d078cd1bSZidan Wang 		of_node_put(gpr_np);
1800d078cd1bSZidan Wang 
1801d078cd1bSZidan Wang 	return ret;
1802d078cd1bSZidan Wang }
1803d078cd1bSZidan Wang 
1804fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
18057b4b88e0SSascha Hauer 		const char *fw_name)
18067b4b88e0SSascha Hauer {
18077b4b88e0SSascha Hauer 	int ret;
18087b4b88e0SSascha Hauer 
18097b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
18107b4b88e0SSascha Hauer 			FW_ACTION_HOTPLUG, fw_name, sdma->dev,
18117b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
18125b28aa31SSascha Hauer 
18135b28aa31SSascha Hauer 	return ret;
18145b28aa31SSascha Hauer }
18155b28aa31SSascha Hauer 
181619bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
18171ec1e82fSSascha Hauer {
18181ec1e82fSSascha Hauer 	int i, ret;
18191ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
18201ec1e82fSSascha Hauer 
1821b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
1822b93edcddSFabio Estevam 	if (ret)
1823b93edcddSFabio Estevam 		return ret;
1824b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
1825b93edcddSFabio Estevam 	if (ret)
1826b93edcddSFabio Estevam 		goto disable_clk_ipg;
18271ec1e82fSSascha Hauer 
18281ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
1829c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
18301ec1e82fSSascha Hauer 
18311ec1e82fSSascha Hauer 	sdma->channel_control = dma_alloc_coherent(NULL,
18321ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
18331ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
18341ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
18351ec1e82fSSascha Hauer 
18361ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
18371ec1e82fSSascha Hauer 		ret = -ENOMEM;
18381ec1e82fSSascha Hauer 		goto err_dma_alloc;
18391ec1e82fSSascha Hauer 	}
18401ec1e82fSSascha Hauer 
18411ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
18421ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
18431ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
18441ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
18451ec1e82fSSascha Hauer 
18461ec1e82fSSascha Hauer 	/* Zero-out the CCB structures array just allocated */
18471ec1e82fSSascha Hauer 	memset(sdma->channel_control, 0,
18481ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
18491ec1e82fSSascha Hauer 
18501ec1e82fSSascha Hauer 	/* disable all channels */
185117bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
1852c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
18531ec1e82fSSascha Hauer 
18541ec1e82fSSascha Hauer 	/* All channels have priority 0 */
18551ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
1856c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
18571ec1e82fSSascha Hauer 
185857b772b8SRobin Gong 	ret = sdma_request_channel0(sdma);
18591ec1e82fSSascha Hauer 	if (ret)
18601ec1e82fSSascha Hauer 		goto err_dma_alloc;
18611ec1e82fSSascha Hauer 
18621ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
18631ec1e82fSSascha Hauer 
18641ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
1865c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
18661ec1e82fSSascha Hauer 
18671ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
18681ec1e82fSSascha Hauer 	/* FIXME: Check whether to set ACR bit depending on clock ratios */
1869c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
18701ec1e82fSSascha Hauer 
1871c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
18721ec1e82fSSascha Hauer 
18731ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
18741ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
18751ec1e82fSSascha Hauer 
18767560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
18777560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
18781ec1e82fSSascha Hauer 
18791ec1e82fSSascha Hauer 	return 0;
18801ec1e82fSSascha Hauer 
18811ec1e82fSSascha Hauer err_dma_alloc:
18827560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
1883b93edcddSFabio Estevam disable_clk_ipg:
1884b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
18851ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
18861ec1e82fSSascha Hauer 	return ret;
18871ec1e82fSSascha Hauer }
18881ec1e82fSSascha Hauer 
18899479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
18909479e17cSShawn Guo {
18910b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
18929479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
18939479e17cSShawn Guo 
18949479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
18959479e17cSShawn Guo 		return false;
18969479e17cSShawn Guo 
18970b351865SNicolin Chen 	sdmac->data = *data;
18980b351865SNicolin Chen 	chan->private = &sdmac->data;
18999479e17cSShawn Guo 
19009479e17cSShawn Guo 	return true;
19019479e17cSShawn Guo }
19029479e17cSShawn Guo 
19039479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
19049479e17cSShawn Guo 				   struct of_dma *ofdma)
19059479e17cSShawn Guo {
19069479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
19079479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
19089479e17cSShawn Guo 	struct imx_dma_data data;
19099479e17cSShawn Guo 
19109479e17cSShawn Guo 	if (dma_spec->args_count != 3)
19119479e17cSShawn Guo 		return NULL;
19129479e17cSShawn Guo 
19139479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
19149479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
19159479e17cSShawn Guo 	data.priority = dma_spec->args[2];
19168391ecf4SShengjiu Wang 	/*
19178391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
19188391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
19198391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
19208391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
19218391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
19228391ecf4SShengjiu Wang 	 */
19238391ecf4SShengjiu Wang 	data.dma_request2 = 0;
19249479e17cSShawn Guo 
19259479e17cSShawn Guo 	return dma_request_channel(mask, sdma_filter_fn, &data);
19269479e17cSShawn Guo }
19279479e17cSShawn Guo 
1928e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
19291ec1e82fSSascha Hauer {
1930580975d7SShawn Guo 	const struct of_device_id *of_id =
1931580975d7SShawn Guo 			of_match_device(sdma_dt_ids, &pdev->dev);
1932580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
19338391ecf4SShengjiu Wang 	struct device_node *spba_bus;
1934580975d7SShawn Guo 	const char *fw_name;
19351ec1e82fSSascha Hauer 	int ret;
19361ec1e82fSSascha Hauer 	int irq;
19371ec1e82fSSascha Hauer 	struct resource *iores;
19388391ecf4SShengjiu Wang 	struct resource spba_res;
1939d4adcc01SJingoo Han 	struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
19401ec1e82fSSascha Hauer 	int i;
19411ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
194236e2f21aSSascha Hauer 	s32 *saddr_arr;
194317bba72fSSascha Hauer 	const struct sdma_driver_data *drvdata = NULL;
194417bba72fSSascha Hauer 
194517bba72fSSascha Hauer 	if (of_id)
194617bba72fSSascha Hauer 		drvdata = of_id->data;
194717bba72fSSascha Hauer 	else if (pdev->id_entry)
194817bba72fSSascha Hauer 		drvdata = (void *)pdev->id_entry->driver_data;
194917bba72fSSascha Hauer 
195017bba72fSSascha Hauer 	if (!drvdata) {
195117bba72fSSascha Hauer 		dev_err(&pdev->dev, "unable to find driver data\n");
195217bba72fSSascha Hauer 		return -EINVAL;
195317bba72fSSascha Hauer 	}
19541ec1e82fSSascha Hauer 
195542536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
195642536b9fSPhilippe Retornaz 	if (ret)
195742536b9fSPhilippe Retornaz 		return ret;
195842536b9fSPhilippe Retornaz 
19597f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
19601ec1e82fSSascha Hauer 	if (!sdma)
19611ec1e82fSSascha Hauer 		return -ENOMEM;
19621ec1e82fSSascha Hauer 
19632ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
196473eab978SSascha Hauer 
19651ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
196617bba72fSSascha Hauer 	sdma->drvdata = drvdata;
19671ec1e82fSSascha Hauer 
19681ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
19697f24e0eeSFabio Estevam 	if (irq < 0)
197063c72e02SFabio Estevam 		return irq;
19711ec1e82fSSascha Hauer 
19727f24e0eeSFabio Estevam 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
19737f24e0eeSFabio Estevam 	sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
19747f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
19757f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
19761ec1e82fSSascha Hauer 
19777560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
19787f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
19797f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
19801ec1e82fSSascha Hauer 
19817560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
19827f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
19837f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
19847560e3f3SSascha Hauer 
1985fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ipg);
1986fb9caf37SArvind Yadav 	if (ret)
1987fb9caf37SArvind Yadav 		return ret;
1988fb9caf37SArvind Yadav 
1989fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ahb);
1990fb9caf37SArvind Yadav 	if (ret)
1991fb9caf37SArvind Yadav 		goto err_clk;
19927560e3f3SSascha Hauer 
19937f24e0eeSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
19947f24e0eeSFabio Estevam 			       sdma);
19951ec1e82fSSascha Hauer 	if (ret)
1996fb9caf37SArvind Yadav 		goto err_irq;
19971ec1e82fSSascha Hauer 
19985bb9dbb5SVinod Koul 	sdma->irq = irq;
19995bb9dbb5SVinod Koul 
20005b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
2001fb9caf37SArvind Yadav 	if (!sdma->script_addrs) {
2002fb9caf37SArvind Yadav 		ret = -ENOMEM;
2003fb9caf37SArvind Yadav 		goto err_irq;
2004fb9caf37SArvind Yadav 	}
20051ec1e82fSSascha Hauer 
200636e2f21aSSascha Hauer 	/* initially no scripts available */
200736e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
200836e2f21aSSascha Hauer 	for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
200936e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
201036e2f21aSSascha Hauer 
20117214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
20127214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
20130f06c027SRobin Gong 	dma_cap_set(DMA_MEMCPY, sdma->dma_device.cap_mask);
20147214a8b1SSascha Hauer 
20151ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
20161ec1e82fSSascha Hauer 	/* Initialize channel parameters */
20171ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
20181ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
20191ec1e82fSSascha Hauer 
20201ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
20211ec1e82fSSascha Hauer 
20221ec1e82fSSascha Hauer 		sdmac->channel = i;
202357b772b8SRobin Gong 		sdmac->vc.desc_free = sdma_desc_free;
202423889c63SSascha Hauer 		/*
202523889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
202623889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
202723889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
202823889c63SSascha Hauer 		 */
202923889c63SSascha Hauer 		if (i)
203057b772b8SRobin Gong 			vchan_init(&sdmac->vc, &sdma->dma_device);
20311ec1e82fSSascha Hauer 	}
20321ec1e82fSSascha Hauer 
20335b28aa31SSascha Hauer 	ret = sdma_init(sdma);
20341ec1e82fSSascha Hauer 	if (ret)
20351ec1e82fSSascha Hauer 		goto err_init;
20361ec1e82fSSascha Hauer 
2037d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
2038d078cd1bSZidan Wang 	if (ret)
2039d078cd1bSZidan Wang 		goto err_init;
2040d078cd1bSZidan Wang 
2041dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
2042dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
2043580975d7SShawn Guo 	if (pdata && pdata->script_addrs)
20445b28aa31SSascha Hauer 		sdma_add_scripts(sdma, pdata->script_addrs);
20455b28aa31SSascha Hauer 
2046580975d7SShawn Guo 	if (pdata) {
20476d0d7e2dSFabio Estevam 		ret = sdma_get_firmware(sdma, pdata->fw_name);
20486d0d7e2dSFabio Estevam 		if (ret)
2049ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
2050580975d7SShawn Guo 	} else {
2051580975d7SShawn Guo 		/*
2052580975d7SShawn Guo 		 * Because that device tree does not encode ROM script address,
2053580975d7SShawn Guo 		 * the RAM script in firmware is mandatory for device tree
2054580975d7SShawn Guo 		 * probe, otherwise it fails.
2055580975d7SShawn Guo 		 */
2056580975d7SShawn Guo 		ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
2057580975d7SShawn Guo 					      &fw_name);
20586602b0ddSFabio Estevam 		if (ret)
2059ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware name\n");
20606602b0ddSFabio Estevam 		else {
2061580975d7SShawn Guo 			ret = sdma_get_firmware(sdma, fw_name);
20626602b0ddSFabio Estevam 			if (ret)
2063ad1122e5SFabio Estevam 				dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
2064580975d7SShawn Guo 		}
2065580975d7SShawn Guo 	}
20665b28aa31SSascha Hauer 
20671ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
20681ec1e82fSSascha Hauer 
20691ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
20701ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
20711ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
20721ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
20731ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
20747b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
20757f3ff14bSJiada Wang 	sdma->dma_device.device_terminate_all = sdma_disable_channel_with_delay;
2076f9d4a398SNicolin Chen 	sdma->dma_device.src_addr_widths = SDMA_DMA_BUSWIDTHS;
2077f9d4a398SNicolin Chen 	sdma->dma_device.dst_addr_widths = SDMA_DMA_BUSWIDTHS;
2078f9d4a398SNicolin Chen 	sdma->dma_device.directions = SDMA_DMA_DIRECTIONS;
20796f3125ceSLucas Stach 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
20800f06c027SRobin Gong 	sdma->dma_device.device_prep_dma_memcpy = sdma_prep_memcpy;
20811ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
2082b9b3f82fSSascha Hauer 	sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
20834a6b2e8aSRobin Gong 	dma_set_max_seg_size(sdma->dma_device.dev, SDMA_BD_MAX_CNT);
20841ec1e82fSSascha Hauer 
208523e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
208623e11811SVignesh Raman 
20871ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
20881ec1e82fSSascha Hauer 	if (ret) {
20891ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
20901ec1e82fSSascha Hauer 		goto err_init;
20911ec1e82fSSascha Hauer 	}
20921ec1e82fSSascha Hauer 
20939479e17cSShawn Guo 	if (np) {
20949479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
20959479e17cSShawn Guo 		if (ret) {
20969479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
20979479e17cSShawn Guo 			goto err_register;
20989479e17cSShawn Guo 		}
20998391ecf4SShengjiu Wang 
21008391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
21018391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
21028391ecf4SShengjiu Wang 		if (!ret) {
21038391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
21048391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
21058391ecf4SShengjiu Wang 		}
21068391ecf4SShengjiu Wang 		of_node_put(spba_bus);
21079479e17cSShawn Guo 	}
21089479e17cSShawn Guo 
21091ec1e82fSSascha Hauer 	return 0;
21101ec1e82fSSascha Hauer 
21119479e17cSShawn Guo err_register:
21129479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
21131ec1e82fSSascha Hauer err_init:
21141ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
2115fb9caf37SArvind Yadav err_irq:
2116fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2117fb9caf37SArvind Yadav err_clk:
2118fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2119939fd4f0SShawn Guo 	return ret;
21201ec1e82fSSascha Hauer }
21211ec1e82fSSascha Hauer 
21221d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
21231ec1e82fSSascha Hauer {
212423e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
2125c12fe497SVignesh Raman 	int i;
212623e11811SVignesh Raman 
21275bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
212823e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
212923e11811SVignesh Raman 	kfree(sdma->script_addrs);
2130fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
2131fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
2132c12fe497SVignesh Raman 	/* Kill the tasklet */
2133c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
2134c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
2135c12fe497SVignesh Raman 
213657b772b8SRobin Gong 		tasklet_kill(&sdmac->vc.task);
213757b772b8SRobin Gong 		sdma_free_chan_resources(&sdmac->vc.chan);
2138c12fe497SVignesh Raman 	}
213923e11811SVignesh Raman 
214023e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
214123e11811SVignesh Raman 	return 0;
21421ec1e82fSSascha Hauer }
21431ec1e82fSSascha Hauer 
21441ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
21451ec1e82fSSascha Hauer 	.driver		= {
21461ec1e82fSSascha Hauer 		.name	= "imx-sdma",
2147580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
21481ec1e82fSSascha Hauer 	},
214962550cd7SShawn Guo 	.id_table	= sdma_devtypes,
21501d1bbd30SMaxin B. John 	.remove		= sdma_remove,
215123e11811SVignesh Raman 	.probe		= sdma_probe,
21521ec1e82fSSascha Hauer };
21531ec1e82fSSascha Hauer 
215423e11811SVignesh Raman module_platform_driver(sdma_driver);
21551ec1e82fSSascha Hauer 
21561ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
21571ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
2158c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX6Q)
2159c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx6q.bin");
2160c0879342SNicolas Chauvet #endif
2161c0879342SNicolas Chauvet #if IS_ENABLED(CONFIG_SOC_IMX7D)
2162c0879342SNicolas Chauvet MODULE_FIRMWARE("imx/sdma/sdma-imx7d.bin");
2163c0879342SNicolas Chauvet #endif
21641ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
2165