xref: /openbmc/linux/drivers/dma/imx-sdma.c (revision fb9caf370f4d0457789d13a1a1b110a8db846e5e)
11ec1e82fSSascha Hauer /*
21ec1e82fSSascha Hauer  * drivers/dma/imx-sdma.c
31ec1e82fSSascha Hauer  *
41ec1e82fSSascha Hauer  * This file contains a driver for the Freescale Smart DMA engine
51ec1e82fSSascha Hauer  *
61ec1e82fSSascha Hauer  * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
71ec1e82fSSascha Hauer  *
81ec1e82fSSascha Hauer  * Based on code from Freescale:
91ec1e82fSSascha Hauer  *
101ec1e82fSSascha Hauer  * Copyright 2004-2009 Freescale Semiconductor, Inc. All Rights Reserved.
111ec1e82fSSascha Hauer  *
121ec1e82fSSascha Hauer  * The code contained herein is licensed under the GNU General Public
131ec1e82fSSascha Hauer  * License. You may obtain a copy of the GNU General Public License
141ec1e82fSSascha Hauer  * Version 2 or later at the following locations:
151ec1e82fSSascha Hauer  *
161ec1e82fSSascha Hauer  * http://www.opensource.org/licenses/gpl-license.html
171ec1e82fSSascha Hauer  * http://www.gnu.org/copyleft/gpl.html
181ec1e82fSSascha Hauer  */
191ec1e82fSSascha Hauer 
201ec1e82fSSascha Hauer #include <linux/init.h>
211d069bfaSMichael Olbrich #include <linux/iopoll.h>
22f8de8f4cSAxel Lin #include <linux/module.h>
231ec1e82fSSascha Hauer #include <linux/types.h>
240bbc1413SRichard Zhao #include <linux/bitops.h>
251ec1e82fSSascha Hauer #include <linux/mm.h>
261ec1e82fSSascha Hauer #include <linux/interrupt.h>
271ec1e82fSSascha Hauer #include <linux/clk.h>
282ccaef05SRichard Zhao #include <linux/delay.h>
291ec1e82fSSascha Hauer #include <linux/sched.h>
301ec1e82fSSascha Hauer #include <linux/semaphore.h>
311ec1e82fSSascha Hauer #include <linux/spinlock.h>
321ec1e82fSSascha Hauer #include <linux/device.h>
331ec1e82fSSascha Hauer #include <linux/dma-mapping.h>
341ec1e82fSSascha Hauer #include <linux/firmware.h>
351ec1e82fSSascha Hauer #include <linux/slab.h>
361ec1e82fSSascha Hauer #include <linux/platform_device.h>
371ec1e82fSSascha Hauer #include <linux/dmaengine.h>
38580975d7SShawn Guo #include <linux/of.h>
398391ecf4SShengjiu Wang #include <linux/of_address.h>
40580975d7SShawn Guo #include <linux/of_device.h>
419479e17cSShawn Guo #include <linux/of_dma.h>
421ec1e82fSSascha Hauer 
431ec1e82fSSascha Hauer #include <asm/irq.h>
4482906b13SArnd Bergmann #include <linux/platform_data/dma-imx-sdma.h>
4582906b13SArnd Bergmann #include <linux/platform_data/dma-imx.h>
46d078cd1bSZidan Wang #include <linux/regmap.h>
47d078cd1bSZidan Wang #include <linux/mfd/syscon.h>
48d078cd1bSZidan Wang #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
491ec1e82fSSascha Hauer 
50d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
51d2ebfb33SRussell King - ARM Linux 
521ec1e82fSSascha Hauer /* SDMA registers */
531ec1e82fSSascha Hauer #define SDMA_H_C0PTR		0x000
541ec1e82fSSascha Hauer #define SDMA_H_INTR		0x004
551ec1e82fSSascha Hauer #define SDMA_H_STATSTOP		0x008
561ec1e82fSSascha Hauer #define SDMA_H_START		0x00c
571ec1e82fSSascha Hauer #define SDMA_H_EVTOVR		0x010
581ec1e82fSSascha Hauer #define SDMA_H_DSPOVR		0x014
591ec1e82fSSascha Hauer #define SDMA_H_HOSTOVR		0x018
601ec1e82fSSascha Hauer #define SDMA_H_EVTPEND		0x01c
611ec1e82fSSascha Hauer #define SDMA_H_DSPENBL		0x020
621ec1e82fSSascha Hauer #define SDMA_H_RESET		0x024
631ec1e82fSSascha Hauer #define SDMA_H_EVTERR		0x028
641ec1e82fSSascha Hauer #define SDMA_H_INTRMSK		0x02c
651ec1e82fSSascha Hauer #define SDMA_H_PSW		0x030
661ec1e82fSSascha Hauer #define SDMA_H_EVTERRDBG	0x034
671ec1e82fSSascha Hauer #define SDMA_H_CONFIG		0x038
681ec1e82fSSascha Hauer #define SDMA_ONCE_ENB		0x040
691ec1e82fSSascha Hauer #define SDMA_ONCE_DATA		0x044
701ec1e82fSSascha Hauer #define SDMA_ONCE_INSTR		0x048
711ec1e82fSSascha Hauer #define SDMA_ONCE_STAT		0x04c
721ec1e82fSSascha Hauer #define SDMA_ONCE_CMD		0x050
731ec1e82fSSascha Hauer #define SDMA_EVT_MIRROR		0x054
741ec1e82fSSascha Hauer #define SDMA_ILLINSTADDR	0x058
751ec1e82fSSascha Hauer #define SDMA_CHN0ADDR		0x05c
761ec1e82fSSascha Hauer #define SDMA_ONCE_RTB		0x060
771ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF1	0x070
781ec1e82fSSascha Hauer #define SDMA_XTRIG_CONF2	0x074
7962550cd7SShawn Guo #define SDMA_CHNENBL0_IMX35	0x200
8062550cd7SShawn Guo #define SDMA_CHNENBL0_IMX31	0x080
811ec1e82fSSascha Hauer #define SDMA_CHNPRI_0		0x100
821ec1e82fSSascha Hauer 
831ec1e82fSSascha Hauer /*
841ec1e82fSSascha Hauer  * Buffer descriptor status values.
851ec1e82fSSascha Hauer  */
861ec1e82fSSascha Hauer #define BD_DONE  0x01
871ec1e82fSSascha Hauer #define BD_WRAP  0x02
881ec1e82fSSascha Hauer #define BD_CONT  0x04
891ec1e82fSSascha Hauer #define BD_INTR  0x08
901ec1e82fSSascha Hauer #define BD_RROR  0x10
911ec1e82fSSascha Hauer #define BD_LAST  0x20
921ec1e82fSSascha Hauer #define BD_EXTD  0x80
931ec1e82fSSascha Hauer 
941ec1e82fSSascha Hauer /*
951ec1e82fSSascha Hauer  * Data Node descriptor status values.
961ec1e82fSSascha Hauer  */
971ec1e82fSSascha Hauer #define DND_END_OF_FRAME  0x80
981ec1e82fSSascha Hauer #define DND_END_OF_XFER   0x40
991ec1e82fSSascha Hauer #define DND_DONE          0x20
1001ec1e82fSSascha Hauer #define DND_UNUSED        0x01
1011ec1e82fSSascha Hauer 
1021ec1e82fSSascha Hauer /*
1031ec1e82fSSascha Hauer  * IPCV2 descriptor status values.
1041ec1e82fSSascha Hauer  */
1051ec1e82fSSascha Hauer #define BD_IPCV2_END_OF_FRAME  0x40
1061ec1e82fSSascha Hauer 
1071ec1e82fSSascha Hauer #define IPCV2_MAX_NODES        50
1081ec1e82fSSascha Hauer /*
1091ec1e82fSSascha Hauer  * Error bit set in the CCB status field by the SDMA,
1101ec1e82fSSascha Hauer  * in setbd routine, in case of a transfer error
1111ec1e82fSSascha Hauer  */
1121ec1e82fSSascha Hauer #define DATA_ERROR  0x10000000
1131ec1e82fSSascha Hauer 
1141ec1e82fSSascha Hauer /*
1151ec1e82fSSascha Hauer  * Buffer descriptor commands.
1161ec1e82fSSascha Hauer  */
1171ec1e82fSSascha Hauer #define C0_ADDR             0x01
1181ec1e82fSSascha Hauer #define C0_LOAD             0x02
1191ec1e82fSSascha Hauer #define C0_DUMP             0x03
1201ec1e82fSSascha Hauer #define C0_SETCTX           0x07
1211ec1e82fSSascha Hauer #define C0_GETCTX           0x03
1221ec1e82fSSascha Hauer #define C0_SETDM            0x01
1231ec1e82fSSascha Hauer #define C0_SETPM            0x04
1241ec1e82fSSascha Hauer #define C0_GETDM            0x02
1251ec1e82fSSascha Hauer #define C0_GETPM            0x08
1261ec1e82fSSascha Hauer /*
1271ec1e82fSSascha Hauer  * Change endianness indicator in the BD command field
1281ec1e82fSSascha Hauer  */
1291ec1e82fSSascha Hauer #define CHANGE_ENDIANNESS   0x80
1301ec1e82fSSascha Hauer 
1311ec1e82fSSascha Hauer /*
1328391ecf4SShengjiu Wang  *  p_2_p watermark_level description
1338391ecf4SShengjiu Wang  *	Bits		Name			Description
1348391ecf4SShengjiu Wang  *	0-7		Lower WML		Lower watermark level
1358391ecf4SShengjiu Wang  *	8		PS			1: Pad Swallowing
1368391ecf4SShengjiu Wang  *						0: No Pad Swallowing
1378391ecf4SShengjiu Wang  *	9		PA			1: Pad Adding
1388391ecf4SShengjiu Wang  *						0: No Pad Adding
1398391ecf4SShengjiu Wang  *	10		SPDIF			If this bit is set both source
1408391ecf4SShengjiu Wang  *						and destination are on SPBA
1418391ecf4SShengjiu Wang  *	11		Source Bit(SP)		1: Source on SPBA
1428391ecf4SShengjiu Wang  *						0: Source on AIPS
1438391ecf4SShengjiu Wang  *	12		Destination Bit(DP)	1: Destination on SPBA
1448391ecf4SShengjiu Wang  *						0: Destination on AIPS
1458391ecf4SShengjiu Wang  *	13-15		---------		MUST BE 0
1468391ecf4SShengjiu Wang  *	16-23		Higher WML		HWML
1478391ecf4SShengjiu Wang  *	24-27		N			Total number of samples after
1488391ecf4SShengjiu Wang  *						which Pad adding/Swallowing
1498391ecf4SShengjiu Wang  *						must be done. It must be odd.
1508391ecf4SShengjiu Wang  *	28		Lower WML Event(LWE)	SDMA events reg to check for
1518391ecf4SShengjiu Wang  *						LWML event mask
1528391ecf4SShengjiu Wang  *						0: LWE in EVENTS register
1538391ecf4SShengjiu Wang  *						1: LWE in EVENTS2 register
1548391ecf4SShengjiu Wang  *	29		Higher WML Event(HWE)	SDMA events reg to check for
1558391ecf4SShengjiu Wang  *						HWML event mask
1568391ecf4SShengjiu Wang  *						0: HWE in EVENTS register
1578391ecf4SShengjiu Wang  *						1: HWE in EVENTS2 register
1588391ecf4SShengjiu Wang  *	30		---------		MUST BE 0
1598391ecf4SShengjiu Wang  *	31		CONT			1: Amount of samples to be
1608391ecf4SShengjiu Wang  *						transferred is unknown and
1618391ecf4SShengjiu Wang  *						script will keep on
1628391ecf4SShengjiu Wang  *						transferring samples as long as
1638391ecf4SShengjiu Wang  *						both events are detected and
1648391ecf4SShengjiu Wang  *						script must be manually stopped
1658391ecf4SShengjiu Wang  *						by the application
1668391ecf4SShengjiu Wang  *						0: The amount of samples to be
1678391ecf4SShengjiu Wang  *						transferred is equal to the
1688391ecf4SShengjiu Wang  *						count field of mode word
1698391ecf4SShengjiu Wang  */
1708391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWML	0xFF
1718391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PS		BIT(8)
1728391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_PA		BIT(9)
1738391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SPDIF	BIT(10)
1748391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_SP		BIT(11)
1758391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_DP		BIT(12)
1768391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWML	(0xFF << 16)
1778391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_LWE	BIT(28)
1788391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_HWE	BIT(29)
1798391ecf4SShengjiu Wang #define SDMA_WATERMARK_LEVEL_CONT	BIT(31)
1808391ecf4SShengjiu Wang 
1818391ecf4SShengjiu Wang /*
1821ec1e82fSSascha Hauer  * Mode/Count of data node descriptors - IPCv2
1831ec1e82fSSascha Hauer  */
1841ec1e82fSSascha Hauer struct sdma_mode_count {
1851ec1e82fSSascha Hauer 	u32 count   : 16; /* size of the buffer pointed by this BD */
1861ec1e82fSSascha Hauer 	u32 status  :  8; /* E,R,I,C,W,D status bits stored here */
187e4b75760SMartin Kaiser 	u32 command :  8; /* command mostly used for channel 0 */
1881ec1e82fSSascha Hauer };
1891ec1e82fSSascha Hauer 
1901ec1e82fSSascha Hauer /*
1911ec1e82fSSascha Hauer  * Buffer descriptor
1921ec1e82fSSascha Hauer  */
1931ec1e82fSSascha Hauer struct sdma_buffer_descriptor {
1941ec1e82fSSascha Hauer 	struct sdma_mode_count  mode;
1951ec1e82fSSascha Hauer 	u32 buffer_addr;	/* address of the buffer described */
1961ec1e82fSSascha Hauer 	u32 ext_buffer_addr;	/* extended buffer address */
1971ec1e82fSSascha Hauer } __attribute__ ((packed));
1981ec1e82fSSascha Hauer 
1991ec1e82fSSascha Hauer /**
2001ec1e82fSSascha Hauer  * struct sdma_channel_control - Channel control Block
2011ec1e82fSSascha Hauer  *
2021ec1e82fSSascha Hauer  * @current_bd_ptr	current buffer descriptor processed
2031ec1e82fSSascha Hauer  * @base_bd_ptr		first element of buffer descriptor array
2041ec1e82fSSascha Hauer  * @unused		padding. The SDMA engine expects an array of 128 byte
2051ec1e82fSSascha Hauer  *			control blocks
2061ec1e82fSSascha Hauer  */
2071ec1e82fSSascha Hauer struct sdma_channel_control {
2081ec1e82fSSascha Hauer 	u32 current_bd_ptr;
2091ec1e82fSSascha Hauer 	u32 base_bd_ptr;
2101ec1e82fSSascha Hauer 	u32 unused[2];
2111ec1e82fSSascha Hauer } __attribute__ ((packed));
2121ec1e82fSSascha Hauer 
2131ec1e82fSSascha Hauer /**
2141ec1e82fSSascha Hauer  * struct sdma_state_registers - SDMA context for a channel
2151ec1e82fSSascha Hauer  *
2161ec1e82fSSascha Hauer  * @pc:		program counter
2171ec1e82fSSascha Hauer  * @t:		test bit: status of arithmetic & test instruction
2181ec1e82fSSascha Hauer  * @rpc:	return program counter
2191ec1e82fSSascha Hauer  * @sf:		source fault while loading data
2201ec1e82fSSascha Hauer  * @spc:	loop start program counter
2211ec1e82fSSascha Hauer  * @df:		destination fault while storing data
2221ec1e82fSSascha Hauer  * @epc:	loop end program counter
2231ec1e82fSSascha Hauer  * @lm:		loop mode
2241ec1e82fSSascha Hauer  */
2251ec1e82fSSascha Hauer struct sdma_state_registers {
2261ec1e82fSSascha Hauer 	u32 pc     :14;
2271ec1e82fSSascha Hauer 	u32 unused1: 1;
2281ec1e82fSSascha Hauer 	u32 t      : 1;
2291ec1e82fSSascha Hauer 	u32 rpc    :14;
2301ec1e82fSSascha Hauer 	u32 unused0: 1;
2311ec1e82fSSascha Hauer 	u32 sf     : 1;
2321ec1e82fSSascha Hauer 	u32 spc    :14;
2331ec1e82fSSascha Hauer 	u32 unused2: 1;
2341ec1e82fSSascha Hauer 	u32 df     : 1;
2351ec1e82fSSascha Hauer 	u32 epc    :14;
2361ec1e82fSSascha Hauer 	u32 lm     : 2;
2371ec1e82fSSascha Hauer } __attribute__ ((packed));
2381ec1e82fSSascha Hauer 
2391ec1e82fSSascha Hauer /**
2401ec1e82fSSascha Hauer  * struct sdma_context_data - sdma context specific to a channel
2411ec1e82fSSascha Hauer  *
2421ec1e82fSSascha Hauer  * @channel_state:	channel state bits
2431ec1e82fSSascha Hauer  * @gReg:		general registers
2441ec1e82fSSascha Hauer  * @mda:		burst dma destination address register
2451ec1e82fSSascha Hauer  * @msa:		burst dma source address register
2461ec1e82fSSascha Hauer  * @ms:			burst dma status register
2471ec1e82fSSascha Hauer  * @md:			burst dma data register
2481ec1e82fSSascha Hauer  * @pda:		peripheral dma destination address register
2491ec1e82fSSascha Hauer  * @psa:		peripheral dma source address register
2501ec1e82fSSascha Hauer  * @ps:			peripheral dma status register
2511ec1e82fSSascha Hauer  * @pd:			peripheral dma data register
2521ec1e82fSSascha Hauer  * @ca:			CRC polynomial register
2531ec1e82fSSascha Hauer  * @cs:			CRC accumulator register
2541ec1e82fSSascha Hauer  * @dda:		dedicated core destination address register
2551ec1e82fSSascha Hauer  * @dsa:		dedicated core source address register
2561ec1e82fSSascha Hauer  * @ds:			dedicated core status register
2571ec1e82fSSascha Hauer  * @dd:			dedicated core data register
2581ec1e82fSSascha Hauer  */
2591ec1e82fSSascha Hauer struct sdma_context_data {
2601ec1e82fSSascha Hauer 	struct sdma_state_registers  channel_state;
2611ec1e82fSSascha Hauer 	u32  gReg[8];
2621ec1e82fSSascha Hauer 	u32  mda;
2631ec1e82fSSascha Hauer 	u32  msa;
2641ec1e82fSSascha Hauer 	u32  ms;
2651ec1e82fSSascha Hauer 	u32  md;
2661ec1e82fSSascha Hauer 	u32  pda;
2671ec1e82fSSascha Hauer 	u32  psa;
2681ec1e82fSSascha Hauer 	u32  ps;
2691ec1e82fSSascha Hauer 	u32  pd;
2701ec1e82fSSascha Hauer 	u32  ca;
2711ec1e82fSSascha Hauer 	u32  cs;
2721ec1e82fSSascha Hauer 	u32  dda;
2731ec1e82fSSascha Hauer 	u32  dsa;
2741ec1e82fSSascha Hauer 	u32  ds;
2751ec1e82fSSascha Hauer 	u32  dd;
2761ec1e82fSSascha Hauer 	u32  scratch0;
2771ec1e82fSSascha Hauer 	u32  scratch1;
2781ec1e82fSSascha Hauer 	u32  scratch2;
2791ec1e82fSSascha Hauer 	u32  scratch3;
2801ec1e82fSSascha Hauer 	u32  scratch4;
2811ec1e82fSSascha Hauer 	u32  scratch5;
2821ec1e82fSSascha Hauer 	u32  scratch6;
2831ec1e82fSSascha Hauer 	u32  scratch7;
2841ec1e82fSSascha Hauer } __attribute__ ((packed));
2851ec1e82fSSascha Hauer 
2861ec1e82fSSascha Hauer #define NUM_BD (int)(PAGE_SIZE / sizeof(struct sdma_buffer_descriptor))
2871ec1e82fSSascha Hauer 
2881ec1e82fSSascha Hauer struct sdma_engine;
2891ec1e82fSSascha Hauer 
2901ec1e82fSSascha Hauer /**
2911ec1e82fSSascha Hauer  * struct sdma_channel - housekeeping for a SDMA channel
2921ec1e82fSSascha Hauer  *
2931ec1e82fSSascha Hauer  * @sdma		pointer to the SDMA engine for this channel
29423889c63SSascha Hauer  * @channel		the channel number, matches dmaengine chan_id + 1
2951ec1e82fSSascha Hauer  * @direction		transfer type. Needed for setting SDMA script
2961ec1e82fSSascha Hauer  * @peripheral_type	Peripheral type. Needed for setting SDMA script
2971ec1e82fSSascha Hauer  * @event_id0		aka dma request line
2981ec1e82fSSascha Hauer  * @event_id1		for channels that use 2 events
2991ec1e82fSSascha Hauer  * @word_size		peripheral access size
3001ec1e82fSSascha Hauer  * @buf_tail		ID of the buffer that was processed
30185f57752SNandor Han  * @buf_ptail		ID of the previous buffer that was processed
3021ec1e82fSSascha Hauer  * @num_bd		max NUM_BD. number of descriptors currently handling
3031ec1e82fSSascha Hauer  */
3041ec1e82fSSascha Hauer struct sdma_channel {
3051ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
3061ec1e82fSSascha Hauer 	unsigned int			channel;
307db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
3081ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
3091ec1e82fSSascha Hauer 	unsigned int			event_id0;
3101ec1e82fSSascha Hauer 	unsigned int			event_id1;
3111ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
3121ec1e82fSSascha Hauer 	unsigned int			buf_tail;
31385f57752SNandor Han 	unsigned int			buf_ptail;
3141ec1e82fSSascha Hauer 	unsigned int			num_bd;
315d1a792f3SRussell King - ARM Linux 	unsigned int			period_len;
3161ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor	*bd;
3171ec1e82fSSascha Hauer 	dma_addr_t			bd_phys;
3181ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
3198391ecf4SShengjiu Wang 	unsigned int			device_to_device;
3201ec1e82fSSascha Hauer 	unsigned long			flags;
3218391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
3220bbc1413SRichard Zhao 	unsigned long			event_mask[2];
3230bbc1413SRichard Zhao 	unsigned long			watermark_level;
3241ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
3251ec1e82fSSascha Hauer 	struct dma_chan			chan;
3261ec1e82fSSascha Hauer 	spinlock_t			lock;
3271ec1e82fSSascha Hauer 	struct dma_async_tx_descriptor	desc;
3281ec1e82fSSascha Hauer 	enum dma_status			status;
329ab59a510SHuang Shijie 	unsigned int			chn_count;
330ab59a510SHuang Shijie 	unsigned int			chn_real_count;
331abd9ccc8SHuang Shijie 	struct tasklet_struct		tasklet;
3320b351865SNicolin Chen 	struct imx_dma_data		data;
3331ec1e82fSSascha Hauer };
3341ec1e82fSSascha Hauer 
3350bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
3361ec1e82fSSascha Hauer 
3371ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
3381ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
3391ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
3401ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
3411ec1e82fSSascha Hauer 
3421ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
3431ec1e82fSSascha Hauer 
3441ec1e82fSSascha Hauer /**
3451ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
3461ec1e82fSSascha Hauer  *
3471ec1e82fSSascha Hauer  * @magic		"SDMA"
3481ec1e82fSSascha Hauer  * @version_major	increased whenever layout of struct sdma_script_start_addrs
3491ec1e82fSSascha Hauer  *			changes.
3501ec1e82fSSascha Hauer  * @version_minor	firmware minor version (for binary compatible changes)
3511ec1e82fSSascha Hauer  * @script_addrs_start	offset of struct sdma_script_start_addrs in this image
3521ec1e82fSSascha Hauer  * @num_script_addrs	Number of script addresses in this image
3531ec1e82fSSascha Hauer  * @ram_code_start	offset of SDMA ram image in this firmware image
3541ec1e82fSSascha Hauer  * @ram_code_size	size of SDMA ram image
3551ec1e82fSSascha Hauer  * @script_addrs	Stores the start address of the SDMA scripts
3561ec1e82fSSascha Hauer  *			(in SDMA memory space)
3571ec1e82fSSascha Hauer  */
3581ec1e82fSSascha Hauer struct sdma_firmware_header {
3591ec1e82fSSascha Hauer 	u32	magic;
3601ec1e82fSSascha Hauer 	u32	version_major;
3611ec1e82fSSascha Hauer 	u32	version_minor;
3621ec1e82fSSascha Hauer 	u32	script_addrs_start;
3631ec1e82fSSascha Hauer 	u32	num_script_addrs;
3641ec1e82fSSascha Hauer 	u32	ram_code_start;
3651ec1e82fSSascha Hauer 	u32	ram_code_size;
3661ec1e82fSSascha Hauer };
3671ec1e82fSSascha Hauer 
36817bba72fSSascha Hauer struct sdma_driver_data {
36917bba72fSSascha Hauer 	int chnenbl0;
37017bba72fSSascha Hauer 	int num_events;
371dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
37262550cd7SShawn Guo };
37362550cd7SShawn Guo 
3741ec1e82fSSascha Hauer struct sdma_engine {
3751ec1e82fSSascha Hauer 	struct device			*dev;
376b9b3f82fSSascha Hauer 	struct device_dma_parameters	dma_parms;
3771ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
3781ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
3791ec1e82fSSascha Hauer 	void __iomem			*regs;
3801ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
3811ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
3821ec1e82fSSascha Hauer 	struct dma_device		dma_device;
3837560e3f3SSascha Hauer 	struct clk			*clk_ipg;
3847560e3f3SSascha Hauer 	struct clk			*clk_ahb;
3852ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
386cd72b846SNicolin Chen 	u32				script_number;
3871ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
38817bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
3898391ecf4SShengjiu Wang 	u32				spba_start_addr;
3908391ecf4SShengjiu Wang 	u32				spba_end_addr;
3915bb9dbb5SVinod Koul 	unsigned int			irq;
39217bba72fSSascha Hauer };
39317bba72fSSascha Hauer 
394e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
39517bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
39617bba72fSSascha Hauer 	.num_events = 32,
39717bba72fSSascha Hauer };
39817bba72fSSascha Hauer 
399dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
400dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
401dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
402dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
403dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
404dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
405dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
406dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
407dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
408dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
409dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
410dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
411dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
412dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
413dcfec3c0SSascha Hauer };
414dcfec3c0SSascha Hauer 
415e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
416dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
417dcfec3c0SSascha Hauer 	.num_events = 48,
418dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
419dcfec3c0SSascha Hauer };
420dcfec3c0SSascha Hauer 
421e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
42217bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
42317bba72fSSascha Hauer 	.num_events = 48,
4241ec1e82fSSascha Hauer };
4251ec1e82fSSascha Hauer 
426dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
427dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
428dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
429dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
430dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
431dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
432dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
433dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
434dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
435dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
436dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
437dcfec3c0SSascha Hauer };
438dcfec3c0SSascha Hauer 
439e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
440dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
441dcfec3c0SSascha Hauer 	.num_events = 48,
442dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
443dcfec3c0SSascha Hauer };
444dcfec3c0SSascha Hauer 
445dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
446dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
447dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
448dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
449dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
450dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
451dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
452dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
453dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
454dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
455dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
456dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
457dcfec3c0SSascha Hauer };
458dcfec3c0SSascha Hauer 
459e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
460dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
461dcfec3c0SSascha Hauer 	.num_events = 48,
462dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
463dcfec3c0SSascha Hauer };
464dcfec3c0SSascha Hauer 
465dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
466dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
467dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
468dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
469dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
470dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
471dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
472dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
473dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
474dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
475dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
476dcfec3c0SSascha Hauer };
477dcfec3c0SSascha Hauer 
478e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
479dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
480dcfec3c0SSascha Hauer 	.num_events = 48,
481dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
482dcfec3c0SSascha Hauer };
483dcfec3c0SSascha Hauer 
484b7d2648aSFabio Estevam static struct sdma_script_start_addrs sdma_script_imx7d = {
485b7d2648aSFabio Estevam 	.ap_2_ap_addr = 644,
486b7d2648aSFabio Estevam 	.uart_2_mcu_addr = 819,
487b7d2648aSFabio Estevam 	.mcu_2_app_addr = 749,
488b7d2648aSFabio Estevam 	.uartsh_2_mcu_addr = 1034,
489b7d2648aSFabio Estevam 	.mcu_2_shp_addr = 962,
490b7d2648aSFabio Estevam 	.app_2_mcu_addr = 685,
491b7d2648aSFabio Estevam 	.shp_2_mcu_addr = 893,
492b7d2648aSFabio Estevam 	.spdif_2_mcu_addr = 1102,
493b7d2648aSFabio Estevam 	.mcu_2_spdif_addr = 1136,
494b7d2648aSFabio Estevam };
495b7d2648aSFabio Estevam 
496b7d2648aSFabio Estevam static struct sdma_driver_data sdma_imx7d = {
497b7d2648aSFabio Estevam 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
498b7d2648aSFabio Estevam 	.num_events = 48,
499b7d2648aSFabio Estevam 	.script_addrs = &sdma_script_imx7d,
500b7d2648aSFabio Estevam };
501b7d2648aSFabio Estevam 
502afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = {
50362550cd7SShawn Guo 	{
504dcfec3c0SSascha Hauer 		.name = "imx25-sdma",
505dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx25,
506dcfec3c0SSascha Hauer 	}, {
50762550cd7SShawn Guo 		.name = "imx31-sdma",
50817bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx31,
50962550cd7SShawn Guo 	}, {
51062550cd7SShawn Guo 		.name = "imx35-sdma",
51117bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx35,
51262550cd7SShawn Guo 	}, {
513dcfec3c0SSascha Hauer 		.name = "imx51-sdma",
514dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx51,
515dcfec3c0SSascha Hauer 	}, {
516dcfec3c0SSascha Hauer 		.name = "imx53-sdma",
517dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx53,
518dcfec3c0SSascha Hauer 	}, {
519dcfec3c0SSascha Hauer 		.name = "imx6q-sdma",
520dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx6q,
521dcfec3c0SSascha Hauer 	}, {
522b7d2648aSFabio Estevam 		.name = "imx7d-sdma",
523b7d2648aSFabio Estevam 		.driver_data = (unsigned long)&sdma_imx7d,
524b7d2648aSFabio Estevam 	}, {
52562550cd7SShawn Guo 		/* sentinel */
52662550cd7SShawn Guo 	}
52762550cd7SShawn Guo };
52862550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes);
52962550cd7SShawn Guo 
530580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
531dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
532dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
533dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
53417bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
535dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
53663edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
537b7d2648aSFabio Estevam 	{ .compatible = "fsl,imx7d-sdma", .data = &sdma_imx7d, },
538580975d7SShawn Guo 	{ /* sentinel */ }
539580975d7SShawn Guo };
540580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
541580975d7SShawn Guo 
5420bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
5430bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
5440bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
5451ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
5461ec1e82fSSascha Hauer 
5471ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
5481ec1e82fSSascha Hauer {
54917bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
5501ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
5511ec1e82fSSascha Hauer }
5521ec1e82fSSascha Hauer 
5531ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
5541ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
5551ec1e82fSSascha Hauer {
5561ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
5571ec1e82fSSascha Hauer 	int channel = sdmac->channel;
5580bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
5591ec1e82fSSascha Hauer 
5601ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
5611ec1e82fSSascha Hauer 		return -EINVAL;
5621ec1e82fSSascha Hauer 
563c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
564c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
565c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
5661ec1e82fSSascha Hauer 
5671ec1e82fSSascha Hauer 	if (dsp_override)
5680bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
5691ec1e82fSSascha Hauer 	else
5700bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
5711ec1e82fSSascha Hauer 
5721ec1e82fSSascha Hauer 	if (event_override)
5730bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
5741ec1e82fSSascha Hauer 	else
5750bbc1413SRichard Zhao 		__set_bit(channel, &evt);
5761ec1e82fSSascha Hauer 
5771ec1e82fSSascha Hauer 	if (mcu_override)
5780bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
5791ec1e82fSSascha Hauer 	else
5800bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
5811ec1e82fSSascha Hauer 
582c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
583c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
584c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
5851ec1e82fSSascha Hauer 
5861ec1e82fSSascha Hauer 	return 0;
5871ec1e82fSSascha Hauer }
5881ec1e82fSSascha Hauer 
589b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
590b9a59166SRichard Zhao {
5910bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
592b9a59166SRichard Zhao }
593b9a59166SRichard Zhao 
5941ec1e82fSSascha Hauer /*
5952ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
5961ec1e82fSSascha Hauer  */
5972ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
5981ec1e82fSSascha Hauer {
5991ec1e82fSSascha Hauer 	int ret;
6001d069bfaSMichael Olbrich 	u32 reg;
6011ec1e82fSSascha Hauer 
6022ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
6031ec1e82fSSascha Hauer 
6041d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
6051d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
6061d069bfaSMichael Olbrich 	if (ret)
6072ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
6081ec1e82fSSascha Hauer 
609855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
610855832e4SRobin Gong 	if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
611855832e4SRobin Gong 		writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
612855832e4SRobin Gong 
6131d069bfaSMichael Olbrich 	return ret;
6141ec1e82fSSascha Hauer }
6151ec1e82fSSascha Hauer 
6161ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
6171ec1e82fSSascha Hauer 		u32 address)
6181ec1e82fSSascha Hauer {
6191ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
6201ec1e82fSSascha Hauer 	void *buf_virt;
6211ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
6221ec1e82fSSascha Hauer 	int ret;
6232ccaef05SRichard Zhao 	unsigned long flags;
62473eab978SSascha Hauer 
6251ec1e82fSSascha Hauer 	buf_virt = dma_alloc_coherent(NULL,
6261ec1e82fSSascha Hauer 			size,
6271ec1e82fSSascha Hauer 			&buf_phys, GFP_KERNEL);
62873eab978SSascha Hauer 	if (!buf_virt) {
6292ccaef05SRichard Zhao 		return -ENOMEM;
63073eab978SSascha Hauer 	}
6311ec1e82fSSascha Hauer 
6322ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
6332ccaef05SRichard Zhao 
6341ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
6351ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
6361ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
6371ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
6381ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
6391ec1e82fSSascha Hauer 
6401ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
6411ec1e82fSSascha Hauer 
6422ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
6432ccaef05SRichard Zhao 
6442ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
6451ec1e82fSSascha Hauer 
6461ec1e82fSSascha Hauer 	dma_free_coherent(NULL, size, buf_virt, buf_phys);
6471ec1e82fSSascha Hauer 
6481ec1e82fSSascha Hauer 	return ret;
6491ec1e82fSSascha Hauer }
6501ec1e82fSSascha Hauer 
6511ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
6521ec1e82fSSascha Hauer {
6531ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6541ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6550bbc1413SRichard Zhao 	unsigned long val;
6561ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
6571ec1e82fSSascha Hauer 
658c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
6590bbc1413SRichard Zhao 	__set_bit(channel, &val);
660c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
6611ec1e82fSSascha Hauer }
6621ec1e82fSSascha Hauer 
6631ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
6641ec1e82fSSascha Hauer {
6651ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6661ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6671ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
6680bbc1413SRichard Zhao 	unsigned long val;
6691ec1e82fSSascha Hauer 
670c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
6710bbc1413SRichard Zhao 	__clear_bit(channel, &val);
672c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
6731ec1e82fSSascha Hauer }
6741ec1e82fSSascha Hauer 
675d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
676d1a792f3SRussell King - ARM Linux {
6771ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
6785881826dSNandor Han 	int error = 0;
6795881826dSNandor Han 	enum dma_status	old_status = sdmac->status;
6801ec1e82fSSascha Hauer 
6811ec1e82fSSascha Hauer 	/*
6821ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
6831ec1e82fSSascha Hauer 	 * call callback function.
6841ec1e82fSSascha Hauer 	 */
6851ec1e82fSSascha Hauer 	while (1) {
6861ec1e82fSSascha Hauer 		bd = &sdmac->bd[sdmac->buf_tail];
6871ec1e82fSSascha Hauer 
6881ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
6891ec1e82fSSascha Hauer 			break;
6901ec1e82fSSascha Hauer 
6915881826dSNandor Han 		if (bd->mode.status & BD_RROR) {
6925881826dSNandor Han 			bd->mode.status &= ~BD_RROR;
6931ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
6945881826dSNandor Han 			error = -EIO;
6955881826dSNandor Han 		}
6961ec1e82fSSascha Hauer 
6975881826dSNandor Han 	       /*
6985881826dSNandor Han 		* We use bd->mode.count to calculate the residue, since contains
6995881826dSNandor Han 		* the number of bytes present in the current buffer descriptor.
7005881826dSNandor Han 		*/
7015881826dSNandor Han 
7025881826dSNandor Han 		sdmac->chn_real_count = bd->mode.count;
7031ec1e82fSSascha Hauer 		bd->mode.status |= BD_DONE;
7045881826dSNandor Han 		bd->mode.count = sdmac->period_len;
70585f57752SNandor Han 		sdmac->buf_ptail = sdmac->buf_tail;
70685f57752SNandor Han 		sdmac->buf_tail = (sdmac->buf_tail + 1) % sdmac->num_bd;
70715f30f51SNandor Han 
70815f30f51SNandor Han 		/*
70915f30f51SNandor Han 		 * The callback is called from the interrupt context in order
71015f30f51SNandor Han 		 * to reduce latency and to avoid the risk of altering the
71115f30f51SNandor Han 		 * SDMA transaction status by the time the client tasklet is
71215f30f51SNandor Han 		 * executed.
71315f30f51SNandor Han 		 */
71415f30f51SNandor Han 
715553911c6SLinus Torvalds 		dmaengine_desc_get_callback_invoke(&sdmac->desc, NULL);
71615f30f51SNandor Han 
7175881826dSNandor Han 		if (error)
7185881826dSNandor Han 			sdmac->status = old_status;
7191ec1e82fSSascha Hauer 	}
7201ec1e82fSSascha Hauer }
7211ec1e82fSSascha Hauer 
72215f30f51SNandor Han static void mxc_sdma_handle_channel_normal(unsigned long data)
7231ec1e82fSSascha Hauer {
72415f30f51SNandor Han 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
7251ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
7261ec1e82fSSascha Hauer 	int i, error = 0;
7271ec1e82fSSascha Hauer 
728ab59a510SHuang Shijie 	sdmac->chn_real_count = 0;
7291ec1e82fSSascha Hauer 	/*
7301ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
7311ec1e82fSSascha Hauer 	 * errors and call callback function
7321ec1e82fSSascha Hauer 	 */
7331ec1e82fSSascha Hauer 	for (i = 0; i < sdmac->num_bd; i++) {
7341ec1e82fSSascha Hauer 		bd = &sdmac->bd[i];
7351ec1e82fSSascha Hauer 
7361ec1e82fSSascha Hauer 		 if (bd->mode.status & (BD_DONE | BD_RROR))
7371ec1e82fSSascha Hauer 			error = -EIO;
738ab59a510SHuang Shijie 		 sdmac->chn_real_count += bd->mode.count;
7391ec1e82fSSascha Hauer 	}
7401ec1e82fSSascha Hauer 
7411ec1e82fSSascha Hauer 	if (error)
7421ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
7431ec1e82fSSascha Hauer 	else
744409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
7451ec1e82fSSascha Hauer 
746f7fbce07SRussell King - ARM Linux 	dma_cookie_complete(&sdmac->desc);
74748dc77e2SDave Jiang 
74848dc77e2SDave Jiang 	dmaengine_desc_get_callback_invoke(&sdmac->desc, NULL);
7491ec1e82fSSascha Hauer }
7501ec1e82fSSascha Hauer 
7511ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
7521ec1e82fSSascha Hauer {
7531ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
7540bbc1413SRichard Zhao 	unsigned long stat;
7551ec1e82fSSascha Hauer 
756c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
757c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
7581d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
7591d069bfaSMichael Olbrich 	stat &= ~1;
7601ec1e82fSSascha Hauer 
7611ec1e82fSSascha Hauer 	while (stat) {
7621ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
7631ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
7641ec1e82fSSascha Hauer 
765d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
766d1a792f3SRussell King - ARM Linux 			sdma_update_channel_loop(sdmac);
76715f30f51SNandor Han 		else
768abd9ccc8SHuang Shijie 			tasklet_schedule(&sdmac->tasklet);
7691ec1e82fSSascha Hauer 
7700bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
7711ec1e82fSSascha Hauer 	}
7721ec1e82fSSascha Hauer 
7731ec1e82fSSascha Hauer 	return IRQ_HANDLED;
7741ec1e82fSSascha Hauer }
7751ec1e82fSSascha Hauer 
7761ec1e82fSSascha Hauer /*
7771ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
7781ec1e82fSSascha Hauer  */
7791ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac,
7801ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
7811ec1e82fSSascha Hauer {
7821ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7831ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
7841ec1e82fSSascha Hauer 	/*
7851ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
7861ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
7871ec1e82fSSascha Hauer 	 */
7880d605ba0SVinod Koul 	int per_2_per = 0;
7891ec1e82fSSascha Hauer 
7901ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
7911ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
7928391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
7931ec1e82fSSascha Hauer 
7941ec1e82fSSascha Hauer 	switch (peripheral_type) {
7951ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
7961ec1e82fSSascha Hauer 		break;
7971ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
7981ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
7991ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
8001ec1e82fSSascha Hauer 		break;
8011ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
8021ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
8031ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
8041ec1e82fSSascha Hauer 		break;
8051ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
8061ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
8071ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
8081ec1e82fSSascha Hauer 		break;
8091ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
8101ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
8111ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
8121ec1e82fSSascha Hauer 		break;
8131ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
8141ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
8151ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
8161ec1e82fSSascha Hauer 		break;
8171ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
8181ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
8191ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
82029aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
8211ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
8221ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
8231ec1e82fSSascha Hauer 		break;
8241a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
8251a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
8261a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
8271a895578SNicolin Chen 		break;
8281ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
8291ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
8301ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
8311ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
8321ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
8331ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
8341ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
8351ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
8361ec1e82fSSascha Hauer 		break;
8371ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
8381ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
8391ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
8401ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
8411ec1e82fSSascha Hauer 		break;
842f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
843f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
844f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
845f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
846f892afb0SNicolin Chen 		break;
8471ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
8481ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
8491ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
8501ec1e82fSSascha Hauer 		break;
8511ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
8521ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
8531ec1e82fSSascha Hauer 		break;
8541ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
8551ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
8561ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
8571ec1e82fSSascha Hauer 		break;
8581ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
8591ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
8601ec1e82fSSascha Hauer 		break;
8611ec1e82fSSascha Hauer 	default:
8621ec1e82fSSascha Hauer 		break;
8631ec1e82fSSascha Hauer 	}
8641ec1e82fSSascha Hauer 
8651ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
8661ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
8678391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
8681ec1e82fSSascha Hauer }
8691ec1e82fSSascha Hauer 
8701ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
8711ec1e82fSSascha Hauer {
8721ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8731ec1e82fSSascha Hauer 	int channel = sdmac->channel;
8741ec1e82fSSascha Hauer 	int load_address;
8751ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
8761ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
8771ec1e82fSSascha Hauer 	int ret;
8782ccaef05SRichard Zhao 	unsigned long flags;
8791ec1e82fSSascha Hauer 
8808391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
8811ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
8828391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
8838391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
8848391ecf4SShengjiu Wang 	else
8851ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
8861ec1e82fSSascha Hauer 
8871ec1e82fSSascha Hauer 	if (load_address < 0)
8881ec1e82fSSascha Hauer 		return load_address;
8891ec1e82fSSascha Hauer 
8901ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
8910bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
8921ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
8931ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
8940bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
8950bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
8961ec1e82fSSascha Hauer 
8972ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
89873eab978SSascha Hauer 
8991ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
9001ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
9011ec1e82fSSascha Hauer 
9021ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
9031ec1e82fSSascha Hauer 	 * and watermark level
9041ec1e82fSSascha Hauer 	 */
9050bbc1413SRichard Zhao 	context->gReg[0] = sdmac->event_mask[1];
9060bbc1413SRichard Zhao 	context->gReg[1] = sdmac->event_mask[0];
9071ec1e82fSSascha Hauer 	context->gReg[2] = sdmac->per_addr;
9081ec1e82fSSascha Hauer 	context->gReg[6] = sdmac->shp_addr;
9091ec1e82fSSascha Hauer 	context->gReg[7] = sdmac->watermark_level;
9101ec1e82fSSascha Hauer 
9111ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
9121ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
9131ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
9141ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
9151ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
9162ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
9171ec1e82fSSascha Hauer 
9182ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
91973eab978SSascha Hauer 
9201ec1e82fSSascha Hauer 	return ret;
9211ec1e82fSSascha Hauer }
9221ec1e82fSSascha Hauer 
9237b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
9241ec1e82fSSascha Hauer {
9257b350ab0SMaxime Ripard 	return container_of(chan, struct sdma_channel, chan);
9267b350ab0SMaxime Ripard }
9277b350ab0SMaxime Ripard 
9287b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
9297b350ab0SMaxime Ripard {
9307b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
9311ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
9321ec1e82fSSascha Hauer 	int channel = sdmac->channel;
9331ec1e82fSSascha Hauer 
9340bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
9351ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
9367b350ab0SMaxime Ripard 
9377b350ab0SMaxime Ripard 	return 0;
9381ec1e82fSSascha Hauer }
9391ec1e82fSSascha Hauer 
9407f3ff14bSJiada Wang static int sdma_disable_channel_with_delay(struct dma_chan *chan)
9417f3ff14bSJiada Wang {
9427f3ff14bSJiada Wang 	sdma_disable_channel(chan);
9437f3ff14bSJiada Wang 
9447f3ff14bSJiada Wang 	/*
9457f3ff14bSJiada Wang 	 * According to NXP R&D team a delay of one BD SDMA cost time
9467f3ff14bSJiada Wang 	 * (maximum is 1ms) should be added after disable of the channel
9477f3ff14bSJiada Wang 	 * bit, to ensure SDMA core has really been stopped after SDMA
9487f3ff14bSJiada Wang 	 * clients call .device_terminate_all.
9497f3ff14bSJiada Wang 	 */
9507f3ff14bSJiada Wang 	mdelay(1);
9517f3ff14bSJiada Wang 
9527f3ff14bSJiada Wang 	return 0;
9537f3ff14bSJiada Wang }
9547f3ff14bSJiada Wang 
9558391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
9568391ecf4SShengjiu Wang {
9578391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
9588391ecf4SShengjiu Wang 
9598391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
9608391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
9618391ecf4SShengjiu Wang 
9628391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
9638391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
9648391ecf4SShengjiu Wang 
9658391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
9668391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
9678391ecf4SShengjiu Wang 
9688391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
9698391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
9708391ecf4SShengjiu Wang 
9718391ecf4SShengjiu Wang 	/*
9728391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
9738391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
9748391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
9758391ecf4SShengjiu Wang 	 */
9768391ecf4SShengjiu Wang 	if (lwml > hwml) {
9778391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
9788391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
9798391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
9808391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
9818391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
9828391ecf4SShengjiu Wang 	}
9838391ecf4SShengjiu Wang 
9848391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
9858391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
9868391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
9878391ecf4SShengjiu Wang 
9888391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
9898391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
9908391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
9918391ecf4SShengjiu Wang 
9928391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
9938391ecf4SShengjiu Wang }
9948391ecf4SShengjiu Wang 
9957b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
9961ec1e82fSSascha Hauer {
9977b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
9981ec1e82fSSascha Hauer 	int ret;
9991ec1e82fSSascha Hauer 
10007b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
10011ec1e82fSSascha Hauer 
10020bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
10030bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
10041ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
10051ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
10061ec1e82fSSascha Hauer 
10071ec1e82fSSascha Hauer 	if (sdmac->event_id0) {
100817bba72fSSascha Hauer 		if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
10091ec1e82fSSascha Hauer 			return -EINVAL;
10101ec1e82fSSascha Hauer 		sdma_event_enable(sdmac, sdmac->event_id0);
10111ec1e82fSSascha Hauer 	}
10121ec1e82fSSascha Hauer 
10138391ecf4SShengjiu Wang 	if (sdmac->event_id1) {
10148391ecf4SShengjiu Wang 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
10158391ecf4SShengjiu Wang 			return -EINVAL;
10168391ecf4SShengjiu Wang 		sdma_event_enable(sdmac, sdmac->event_id1);
10178391ecf4SShengjiu Wang 	}
10188391ecf4SShengjiu Wang 
10191ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
10201ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
10211ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
10221ec1e82fSSascha Hauer 		break;
10231ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
10241ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
10251ec1e82fSSascha Hauer 		break;
10261ec1e82fSSascha Hauer 	default:
10271ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
10281ec1e82fSSascha Hauer 		break;
10291ec1e82fSSascha Hauer 	}
10301ec1e82fSSascha Hauer 
10311ec1e82fSSascha Hauer 	sdma_get_pc(sdmac, sdmac->peripheral_type);
10321ec1e82fSSascha Hauer 
10331ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
10341ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
10351ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
10361ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
10378391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
10388391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
10398391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
10408391ecf4SShengjiu Wang 		} else
10410bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
10428391ecf4SShengjiu Wang 
10431ec1e82fSSascha Hauer 		/* Address */
10441ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
10458391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
10461ec1e82fSSascha Hauer 	} else {
10471ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
10481ec1e82fSSascha Hauer 	}
10491ec1e82fSSascha Hauer 
10501ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
10511ec1e82fSSascha Hauer 
10521ec1e82fSSascha Hauer 	return ret;
10531ec1e82fSSascha Hauer }
10541ec1e82fSSascha Hauer 
10551ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
10561ec1e82fSSascha Hauer 		unsigned int priority)
10571ec1e82fSSascha Hauer {
10581ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10591ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10601ec1e82fSSascha Hauer 
10611ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
10621ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
10631ec1e82fSSascha Hauer 		return -EINVAL;
10641ec1e82fSSascha Hauer 	}
10651ec1e82fSSascha Hauer 
1066c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
10671ec1e82fSSascha Hauer 
10681ec1e82fSSascha Hauer 	return 0;
10691ec1e82fSSascha Hauer }
10701ec1e82fSSascha Hauer 
10711ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac)
10721ec1e82fSSascha Hauer {
10731ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10741ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10751ec1e82fSSascha Hauer 	int ret = -EBUSY;
10761ec1e82fSSascha Hauer 
10779f92d223SJoe Perches 	sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
10789f92d223SJoe Perches 					GFP_KERNEL);
10791ec1e82fSSascha Hauer 	if (!sdmac->bd) {
10801ec1e82fSSascha Hauer 		ret = -ENOMEM;
10811ec1e82fSSascha Hauer 		goto out;
10821ec1e82fSSascha Hauer 	}
10831ec1e82fSSascha Hauer 
10841ec1e82fSSascha Hauer 	sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
10851ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
10861ec1e82fSSascha Hauer 
10871ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
10881ec1e82fSSascha Hauer 	return 0;
10891ec1e82fSSascha Hauer out:
10901ec1e82fSSascha Hauer 
10911ec1e82fSSascha Hauer 	return ret;
10921ec1e82fSSascha Hauer }
10931ec1e82fSSascha Hauer 
10941ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
10951ec1e82fSSascha Hauer {
1096f69f2e26SHaitao Zhang 	unsigned long flags;
10971ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
10981ec1e82fSSascha Hauer 	dma_cookie_t cookie;
10991ec1e82fSSascha Hauer 
1100f69f2e26SHaitao Zhang 	spin_lock_irqsave(&sdmac->lock, flags);
11011ec1e82fSSascha Hauer 
1102884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
11031ec1e82fSSascha Hauer 
1104f69f2e26SHaitao Zhang 	spin_unlock_irqrestore(&sdmac->lock, flags);
11051ec1e82fSSascha Hauer 
11061ec1e82fSSascha Hauer 	return cookie;
11071ec1e82fSSascha Hauer }
11081ec1e82fSSascha Hauer 
11091ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
11101ec1e82fSSascha Hauer {
11111ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11121ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
11131ec1e82fSSascha Hauer 	int prio, ret;
11141ec1e82fSSascha Hauer 
11151ec1e82fSSascha Hauer 	if (!data)
11161ec1e82fSSascha Hauer 		return -EINVAL;
11171ec1e82fSSascha Hauer 
11181ec1e82fSSascha Hauer 	switch (data->priority) {
11191ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
11201ec1e82fSSascha Hauer 		prio = 3;
11211ec1e82fSSascha Hauer 		break;
11221ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
11231ec1e82fSSascha Hauer 		prio = 2;
11241ec1e82fSSascha Hauer 		break;
11251ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
11261ec1e82fSSascha Hauer 	default:
11271ec1e82fSSascha Hauer 		prio = 1;
11281ec1e82fSSascha Hauer 		break;
11291ec1e82fSSascha Hauer 	}
11301ec1e82fSSascha Hauer 
11311ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
11321ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
11338391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1134c2c744d3SRichard Zhao 
1135b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1136b93edcddSFabio Estevam 	if (ret)
1137b93edcddSFabio Estevam 		return ret;
1138b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1139b93edcddSFabio Estevam 	if (ret)
1140b93edcddSFabio Estevam 		goto disable_clk_ipg;
1141c2c744d3SRichard Zhao 
11423bb5e7caSRichard Zhao 	ret = sdma_request_channel(sdmac);
11431ec1e82fSSascha Hauer 	if (ret)
1144b93edcddSFabio Estevam 		goto disable_clk_ahb;
11451ec1e82fSSascha Hauer 
11463bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
11471ec1e82fSSascha Hauer 	if (ret)
1148b93edcddSFabio Estevam 		goto disable_clk_ahb;
11491ec1e82fSSascha Hauer 
11501ec1e82fSSascha Hauer 	dma_async_tx_descriptor_init(&sdmac->desc, chan);
11511ec1e82fSSascha Hauer 	sdmac->desc.tx_submit = sdma_tx_submit;
11521ec1e82fSSascha Hauer 	/* txd.flags will be overwritten in prep funcs */
11531ec1e82fSSascha Hauer 	sdmac->desc.flags = DMA_CTRL_ACK;
11541ec1e82fSSascha Hauer 
11551ec1e82fSSascha Hauer 	return 0;
1156b93edcddSFabio Estevam 
1157b93edcddSFabio Estevam disable_clk_ahb:
1158b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1159b93edcddSFabio Estevam disable_clk_ipg:
1160b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1161b93edcddSFabio Estevam 	return ret;
11621ec1e82fSSascha Hauer }
11631ec1e82fSSascha Hauer 
11641ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
11651ec1e82fSSascha Hauer {
11661ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11671ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11681ec1e82fSSascha Hauer 
11697b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
11701ec1e82fSSascha Hauer 
11711ec1e82fSSascha Hauer 	if (sdmac->event_id0)
11721ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id0);
11731ec1e82fSSascha Hauer 	if (sdmac->event_id1)
11741ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
11751ec1e82fSSascha Hauer 
11761ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
11771ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
11781ec1e82fSSascha Hauer 
11791ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
11801ec1e82fSSascha Hauer 
11811ec1e82fSSascha Hauer 	dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
11821ec1e82fSSascha Hauer 
11837560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
11847560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
11851ec1e82fSSascha Hauer }
11861ec1e82fSSascha Hauer 
11871ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
11881ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1189db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1190185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
11911ec1e82fSSascha Hauer {
11921ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11931ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11941ec1e82fSSascha Hauer 	int ret, i, count;
119523889c63SSascha Hauer 	int channel = sdmac->channel;
11961ec1e82fSSascha Hauer 	struct scatterlist *sg;
11971ec1e82fSSascha Hauer 
11981ec1e82fSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
11991ec1e82fSSascha Hauer 		return NULL;
12001ec1e82fSSascha Hauer 	sdmac->status = DMA_IN_PROGRESS;
12011ec1e82fSSascha Hauer 
12021ec1e82fSSascha Hauer 	sdmac->flags = 0;
12031ec1e82fSSascha Hauer 
12048e2e27c7SRichard Zhao 	sdmac->buf_tail = 0;
120585f57752SNandor Han 	sdmac->buf_ptail = 0;
120685f57752SNandor Han 	sdmac->chn_real_count = 0;
12078e2e27c7SRichard Zhao 
12081ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
12091ec1e82fSSascha Hauer 			sg_len, channel);
12101ec1e82fSSascha Hauer 
12111ec1e82fSSascha Hauer 	sdmac->direction = direction;
12121ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
12131ec1e82fSSascha Hauer 	if (ret)
12141ec1e82fSSascha Hauer 		goto err_out;
12151ec1e82fSSascha Hauer 
12161ec1e82fSSascha Hauer 	if (sg_len > NUM_BD) {
12171ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
12181ec1e82fSSascha Hauer 				channel, sg_len, NUM_BD);
12191ec1e82fSSascha Hauer 		ret = -EINVAL;
12201ec1e82fSSascha Hauer 		goto err_out;
12211ec1e82fSSascha Hauer 	}
12221ec1e82fSSascha Hauer 
1223ab59a510SHuang Shijie 	sdmac->chn_count = 0;
12241ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
12251ec1e82fSSascha Hauer 		struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
12261ec1e82fSSascha Hauer 		int param;
12271ec1e82fSSascha Hauer 
1228d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
12291ec1e82fSSascha Hauer 
1230fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
12311ec1e82fSSascha Hauer 
12321ec1e82fSSascha Hauer 		if (count > 0xffff) {
12331ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
12341ec1e82fSSascha Hauer 					channel, count, 0xffff);
12351ec1e82fSSascha Hauer 			ret = -EINVAL;
12361ec1e82fSSascha Hauer 			goto err_out;
12371ec1e82fSSascha Hauer 		}
12381ec1e82fSSascha Hauer 
12391ec1e82fSSascha Hauer 		bd->mode.count = count;
1240ab59a510SHuang Shijie 		sdmac->chn_count += count;
12411ec1e82fSSascha Hauer 
12421ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
12431ec1e82fSSascha Hauer 			ret =  -EINVAL;
12441ec1e82fSSascha Hauer 			goto err_out;
12451ec1e82fSSascha Hauer 		}
12461fa81c27SSascha Hauer 
12471fa81c27SSascha Hauer 		switch (sdmac->word_size) {
12481fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
12491ec1e82fSSascha Hauer 			bd->mode.command = 0;
12501fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
12511fa81c27SSascha Hauer 				return NULL;
12521fa81c27SSascha Hauer 			break;
12531fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
12541fa81c27SSascha Hauer 			bd->mode.command = 2;
12551fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
12561fa81c27SSascha Hauer 				return NULL;
12571fa81c27SSascha Hauer 			break;
12581fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
12591fa81c27SSascha Hauer 			bd->mode.command = 1;
12601fa81c27SSascha Hauer 			break;
12611fa81c27SSascha Hauer 		default:
12621fa81c27SSascha Hauer 			return NULL;
12631fa81c27SSascha Hauer 		}
12641ec1e82fSSascha Hauer 
12651ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
12661ec1e82fSSascha Hauer 
1267341b9419SShawn Guo 		if (i + 1 == sg_len) {
12681ec1e82fSSascha Hauer 			param |= BD_INTR;
1269341b9419SShawn Guo 			param |= BD_LAST;
1270341b9419SShawn Guo 			param &= ~BD_CONT;
12711ec1e82fSSascha Hauer 		}
12721ec1e82fSSascha Hauer 
1273c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1274c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
12751ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
12761ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
12771ec1e82fSSascha Hauer 
12781ec1e82fSSascha Hauer 		bd->mode.status = param;
12791ec1e82fSSascha Hauer 	}
12801ec1e82fSSascha Hauer 
12811ec1e82fSSascha Hauer 	sdmac->num_bd = sg_len;
12821ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
12831ec1e82fSSascha Hauer 
12841ec1e82fSSascha Hauer 	return &sdmac->desc;
12851ec1e82fSSascha Hauer err_out:
12864b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
12871ec1e82fSSascha Hauer 	return NULL;
12881ec1e82fSSascha Hauer }
12891ec1e82fSSascha Hauer 
12901ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
12911ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1292185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
129331c1e5a1SLaurent Pinchart 		unsigned long flags)
12941ec1e82fSSascha Hauer {
12951ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
12961ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
12971ec1e82fSSascha Hauer 	int num_periods = buf_len / period_len;
129823889c63SSascha Hauer 	int channel = sdmac->channel;
12991ec1e82fSSascha Hauer 	int ret, i = 0, buf = 0;
13001ec1e82fSSascha Hauer 
13011ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
13021ec1e82fSSascha Hauer 
13031ec1e82fSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
13041ec1e82fSSascha Hauer 		return NULL;
13051ec1e82fSSascha Hauer 
13061ec1e82fSSascha Hauer 	sdmac->status = DMA_IN_PROGRESS;
13071ec1e82fSSascha Hauer 
13088e2e27c7SRichard Zhao 	sdmac->buf_tail = 0;
130985f57752SNandor Han 	sdmac->buf_ptail = 0;
131085f57752SNandor Han 	sdmac->chn_real_count = 0;
1311d1a792f3SRussell King - ARM Linux 	sdmac->period_len = period_len;
13128e2e27c7SRichard Zhao 
13131ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
13141ec1e82fSSascha Hauer 	sdmac->direction = direction;
13151ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
13161ec1e82fSSascha Hauer 	if (ret)
13171ec1e82fSSascha Hauer 		goto err_out;
13181ec1e82fSSascha Hauer 
13191ec1e82fSSascha Hauer 	if (num_periods > NUM_BD) {
13201ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
13211ec1e82fSSascha Hauer 				channel, num_periods, NUM_BD);
13221ec1e82fSSascha Hauer 		goto err_out;
13231ec1e82fSSascha Hauer 	}
13241ec1e82fSSascha Hauer 
13251ec1e82fSSascha Hauer 	if (period_len > 0xffff) {
13261ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
13271ec1e82fSSascha Hauer 				channel, period_len, 0xffff);
13281ec1e82fSSascha Hauer 		goto err_out;
13291ec1e82fSSascha Hauer 	}
13301ec1e82fSSascha Hauer 
13311ec1e82fSSascha Hauer 	while (buf < buf_len) {
13321ec1e82fSSascha Hauer 		struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
13331ec1e82fSSascha Hauer 		int param;
13341ec1e82fSSascha Hauer 
13351ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
13361ec1e82fSSascha Hauer 
13371ec1e82fSSascha Hauer 		bd->mode.count = period_len;
13381ec1e82fSSascha Hauer 
13391ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
13401ec1e82fSSascha Hauer 			goto err_out;
13411ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
13421ec1e82fSSascha Hauer 			bd->mode.command = 0;
13431ec1e82fSSascha Hauer 		else
13441ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
13451ec1e82fSSascha Hauer 
13461ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
13471ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
13481ec1e82fSSascha Hauer 			param |= BD_WRAP;
13491ec1e82fSSascha Hauer 
1350c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1351c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
13521ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
13531ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
13541ec1e82fSSascha Hauer 
13551ec1e82fSSascha Hauer 		bd->mode.status = param;
13561ec1e82fSSascha Hauer 
13571ec1e82fSSascha Hauer 		dma_addr += period_len;
13581ec1e82fSSascha Hauer 		buf += period_len;
13591ec1e82fSSascha Hauer 
13601ec1e82fSSascha Hauer 		i++;
13611ec1e82fSSascha Hauer 	}
13621ec1e82fSSascha Hauer 
13631ec1e82fSSascha Hauer 	sdmac->num_bd = num_periods;
13641ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
13651ec1e82fSSascha Hauer 
13661ec1e82fSSascha Hauer 	return &sdmac->desc;
13671ec1e82fSSascha Hauer err_out:
13681ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
13691ec1e82fSSascha Hauer 	return NULL;
13701ec1e82fSSascha Hauer }
13711ec1e82fSSascha Hauer 
13727b350ab0SMaxime Ripard static int sdma_config(struct dma_chan *chan,
13737b350ab0SMaxime Ripard 		       struct dma_slave_config *dmaengine_cfg)
13741ec1e82fSSascha Hauer {
13751ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13761ec1e82fSSascha Hauer 
1377db8196dfSVinod Koul 	if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
13781ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
137994ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
138094ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
13811ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
13828391ecf4SShengjiu Wang 	} else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
13838391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
13848391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
13858391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
13868391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
13878391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
13888391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
13898391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
13901ec1e82fSSascha Hauer 	} else {
13911ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
139294ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
139394ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
13941ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
13951ec1e82fSSascha Hauer 	}
1396e6966433SHuang Shijie 	sdmac->direction = dmaengine_cfg->direction;
13977b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
13981ec1e82fSSascha Hauer }
13991ec1e82fSSascha Hauer 
14001ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
14011ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
14021ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
14031ec1e82fSSascha Hauer {
14041ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1405d1a792f3SRussell King - ARM Linux 	u32 residue;
1406d1a792f3SRussell King - ARM Linux 
1407d1a792f3SRussell King - ARM Linux 	if (sdmac->flags & IMX_DMA_SG_LOOP)
140885f57752SNandor Han 		residue = (sdmac->num_bd - sdmac->buf_ptail) *
14095881826dSNandor Han 			   sdmac->period_len - sdmac->chn_real_count;
1410d1a792f3SRussell King - ARM Linux 	else
1411d1a792f3SRussell King - ARM Linux 		residue = sdmac->chn_count - sdmac->chn_real_count;
14121ec1e82fSSascha Hauer 
1413e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1414d1a792f3SRussell King - ARM Linux 			 residue);
14151ec1e82fSSascha Hauer 
14168a965911SShawn Guo 	return sdmac->status;
14171ec1e82fSSascha Hauer }
14181ec1e82fSSascha Hauer 
14191ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
14201ec1e82fSSascha Hauer {
14212b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
14222b4f130eSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
14232b4f130eSSascha Hauer 
14242b4f130eSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
14252b4f130eSSascha Hauer 		sdma_enable_channel(sdma, sdmac->channel);
14261ec1e82fSSascha Hauer }
14271ec1e82fSSascha Hauer 
14285b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1429cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1430a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	41
1431b7d2648aSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4	42
14325b28aa31SSascha Hauer 
14335b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
14345b28aa31SSascha Hauer 		const struct sdma_script_start_addrs *addr)
14355b28aa31SSascha Hauer {
14365b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
14375b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
14385b28aa31SSascha Hauer 	int i;
14395b28aa31SSascha Hauer 
144070dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
144170dabaedSNicolin Chen 	if (!sdma->script_number)
144270dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
144370dabaedSNicolin Chen 
1444cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
14455b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
14465b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
14475b28aa31SSascha Hauer }
14485b28aa31SSascha Hauer 
14497b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
14505b28aa31SSascha Hauer {
14517b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
14525b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
14535b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
14545b28aa31SSascha Hauer 	unsigned short *ram_code;
14555b28aa31SSascha Hauer 
14567b4b88e0SSascha Hauer 	if (!fw) {
14570f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
14580f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
14597b4b88e0SSascha Hauer 		return;
14607b4b88e0SSascha Hauer 	}
14615b28aa31SSascha Hauer 
14625b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
14635b28aa31SSascha Hauer 		goto err_firmware;
14645b28aa31SSascha Hauer 
14655b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
14665b28aa31SSascha Hauer 
14675b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
14685b28aa31SSascha Hauer 		goto err_firmware;
14695b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
14705b28aa31SSascha Hauer 		goto err_firmware;
1471cd72b846SNicolin Chen 	switch (header->version_major) {
1472cd72b846SNicolin Chen 	case 1:
1473cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1474cd72b846SNicolin Chen 		break;
1475cd72b846SNicolin Chen 	case 2:
1476cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1477cd72b846SNicolin Chen 		break;
1478a572460bSFabio Estevam 	case 3:
1479a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1480a572460bSFabio Estevam 		break;
1481b7d2648aSFabio Estevam 	case 4:
1482b7d2648aSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V4;
1483b7d2648aSFabio Estevam 		break;
1484cd72b846SNicolin Chen 	default:
1485cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1486cd72b846SNicolin Chen 		goto err_firmware;
1487cd72b846SNicolin Chen 	}
14885b28aa31SSascha Hauer 
14895b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
14905b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
14915b28aa31SSascha Hauer 
14927560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
14937560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
14945b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
14955b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
14965b28aa31SSascha Hauer 			header->ram_code_size,
14976866fd3bSSascha Hauer 			addr->ram_code_start_addr);
14987560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
14997560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
15005b28aa31SSascha Hauer 
15015b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
15025b28aa31SSascha Hauer 
15035b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
15045b28aa31SSascha Hauer 			header->version_major,
15055b28aa31SSascha Hauer 			header->version_minor);
15065b28aa31SSascha Hauer 
15075b28aa31SSascha Hauer err_firmware:
15085b28aa31SSascha Hauer 	release_firmware(fw);
15097b4b88e0SSascha Hauer }
15107b4b88e0SSascha Hauer 
1511d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1512d078cd1bSZidan Wang 
151329f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1514d078cd1bSZidan Wang {
1515d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1516d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1517d078cd1bSZidan Wang 	struct property *event_remap;
1518d078cd1bSZidan Wang 	struct regmap *gpr;
1519d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
1520d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
1521d078cd1bSZidan Wang 	int ret = 0;
1522d078cd1bSZidan Wang 
1523d078cd1bSZidan Wang 	if (IS_ERR(np) || IS_ERR(gpr_np))
1524d078cd1bSZidan Wang 		goto out;
1525d078cd1bSZidan Wang 
1526d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
1527d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1528d078cd1bSZidan Wang 	if (!num_map) {
1529ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
1530d078cd1bSZidan Wang 		goto out;
1531d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
1532d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
1533d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
1534d078cd1bSZidan Wang 		ret = -EINVAL;
1535d078cd1bSZidan Wang 		goto out;
1536d078cd1bSZidan Wang 	}
1537d078cd1bSZidan Wang 
1538d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
1539d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
1540d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
1541d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
1542d078cd1bSZidan Wang 		goto out;
1543d078cd1bSZidan Wang 	}
1544d078cd1bSZidan Wang 
1545d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1546d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
1547d078cd1bSZidan Wang 		if (ret) {
1548d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1549d078cd1bSZidan Wang 					propname, i);
1550d078cd1bSZidan Wang 			goto out;
1551d078cd1bSZidan Wang 		}
1552d078cd1bSZidan Wang 
1553d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1554d078cd1bSZidan Wang 		if (ret) {
1555d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1556d078cd1bSZidan Wang 					propname, i + 1);
1557d078cd1bSZidan Wang 			goto out;
1558d078cd1bSZidan Wang 		}
1559d078cd1bSZidan Wang 
1560d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
1561d078cd1bSZidan Wang 		if (ret) {
1562d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1563d078cd1bSZidan Wang 					propname, i + 2);
1564d078cd1bSZidan Wang 			goto out;
1565d078cd1bSZidan Wang 		}
1566d078cd1bSZidan Wang 
1567d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1568d078cd1bSZidan Wang 	}
1569d078cd1bSZidan Wang 
1570d078cd1bSZidan Wang out:
1571d078cd1bSZidan Wang 	if (!IS_ERR(gpr_np))
1572d078cd1bSZidan Wang 		of_node_put(gpr_np);
1573d078cd1bSZidan Wang 
1574d078cd1bSZidan Wang 	return ret;
1575d078cd1bSZidan Wang }
1576d078cd1bSZidan Wang 
1577fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
15787b4b88e0SSascha Hauer 		const char *fw_name)
15797b4b88e0SSascha Hauer {
15807b4b88e0SSascha Hauer 	int ret;
15817b4b88e0SSascha Hauer 
15827b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
15837b4b88e0SSascha Hauer 			FW_ACTION_HOTPLUG, fw_name, sdma->dev,
15847b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
15855b28aa31SSascha Hauer 
15865b28aa31SSascha Hauer 	return ret;
15875b28aa31SSascha Hauer }
15885b28aa31SSascha Hauer 
158919bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
15901ec1e82fSSascha Hauer {
15911ec1e82fSSascha Hauer 	int i, ret;
15921ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
15931ec1e82fSSascha Hauer 
1594b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
1595b93edcddSFabio Estevam 	if (ret)
1596b93edcddSFabio Estevam 		return ret;
1597b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
1598b93edcddSFabio Estevam 	if (ret)
1599b93edcddSFabio Estevam 		goto disable_clk_ipg;
16001ec1e82fSSascha Hauer 
16011ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
1602c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
16031ec1e82fSSascha Hauer 
16041ec1e82fSSascha Hauer 	sdma->channel_control = dma_alloc_coherent(NULL,
16051ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
16061ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
16071ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
16081ec1e82fSSascha Hauer 
16091ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
16101ec1e82fSSascha Hauer 		ret = -ENOMEM;
16111ec1e82fSSascha Hauer 		goto err_dma_alloc;
16121ec1e82fSSascha Hauer 	}
16131ec1e82fSSascha Hauer 
16141ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
16151ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
16161ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
16171ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
16181ec1e82fSSascha Hauer 
16191ec1e82fSSascha Hauer 	/* Zero-out the CCB structures array just allocated */
16201ec1e82fSSascha Hauer 	memset(sdma->channel_control, 0,
16211ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
16221ec1e82fSSascha Hauer 
16231ec1e82fSSascha Hauer 	/* disable all channels */
162417bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
1625c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
16261ec1e82fSSascha Hauer 
16271ec1e82fSSascha Hauer 	/* All channels have priority 0 */
16281ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
1629c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
16301ec1e82fSSascha Hauer 
16311ec1e82fSSascha Hauer 	ret = sdma_request_channel(&sdma->channel[0]);
16321ec1e82fSSascha Hauer 	if (ret)
16331ec1e82fSSascha Hauer 		goto err_dma_alloc;
16341ec1e82fSSascha Hauer 
16351ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
16361ec1e82fSSascha Hauer 
16371ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
1638c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
16391ec1e82fSSascha Hauer 
16401ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
16411ec1e82fSSascha Hauer 	/* FIXME: Check whether to set ACR bit depending on clock ratios */
1642c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
16431ec1e82fSSascha Hauer 
1644c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
16451ec1e82fSSascha Hauer 
16461ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
16471ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
16481ec1e82fSSascha Hauer 
16497560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
16507560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
16511ec1e82fSSascha Hauer 
16521ec1e82fSSascha Hauer 	return 0;
16531ec1e82fSSascha Hauer 
16541ec1e82fSSascha Hauer err_dma_alloc:
16557560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
1656b93edcddSFabio Estevam disable_clk_ipg:
1657b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
16581ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
16591ec1e82fSSascha Hauer 	return ret;
16601ec1e82fSSascha Hauer }
16611ec1e82fSSascha Hauer 
16629479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
16639479e17cSShawn Guo {
16640b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16659479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
16669479e17cSShawn Guo 
16679479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
16689479e17cSShawn Guo 		return false;
16699479e17cSShawn Guo 
16700b351865SNicolin Chen 	sdmac->data = *data;
16710b351865SNicolin Chen 	chan->private = &sdmac->data;
16729479e17cSShawn Guo 
16739479e17cSShawn Guo 	return true;
16749479e17cSShawn Guo }
16759479e17cSShawn Guo 
16769479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
16779479e17cSShawn Guo 				   struct of_dma *ofdma)
16789479e17cSShawn Guo {
16799479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
16809479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
16819479e17cSShawn Guo 	struct imx_dma_data data;
16829479e17cSShawn Guo 
16839479e17cSShawn Guo 	if (dma_spec->args_count != 3)
16849479e17cSShawn Guo 		return NULL;
16859479e17cSShawn Guo 
16869479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
16879479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
16889479e17cSShawn Guo 	data.priority = dma_spec->args[2];
16898391ecf4SShengjiu Wang 	/*
16908391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
16918391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
16928391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
16938391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
16948391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
16958391ecf4SShengjiu Wang 	 */
16968391ecf4SShengjiu Wang 	data.dma_request2 = 0;
16979479e17cSShawn Guo 
16989479e17cSShawn Guo 	return dma_request_channel(mask, sdma_filter_fn, &data);
16999479e17cSShawn Guo }
17009479e17cSShawn Guo 
1701e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
17021ec1e82fSSascha Hauer {
1703580975d7SShawn Guo 	const struct of_device_id *of_id =
1704580975d7SShawn Guo 			of_match_device(sdma_dt_ids, &pdev->dev);
1705580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
17068391ecf4SShengjiu Wang 	struct device_node *spba_bus;
1707580975d7SShawn Guo 	const char *fw_name;
17081ec1e82fSSascha Hauer 	int ret;
17091ec1e82fSSascha Hauer 	int irq;
17101ec1e82fSSascha Hauer 	struct resource *iores;
17118391ecf4SShengjiu Wang 	struct resource spba_res;
1712d4adcc01SJingoo Han 	struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
17131ec1e82fSSascha Hauer 	int i;
17141ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
171536e2f21aSSascha Hauer 	s32 *saddr_arr;
171617bba72fSSascha Hauer 	const struct sdma_driver_data *drvdata = NULL;
171717bba72fSSascha Hauer 
171817bba72fSSascha Hauer 	if (of_id)
171917bba72fSSascha Hauer 		drvdata = of_id->data;
172017bba72fSSascha Hauer 	else if (pdev->id_entry)
172117bba72fSSascha Hauer 		drvdata = (void *)pdev->id_entry->driver_data;
172217bba72fSSascha Hauer 
172317bba72fSSascha Hauer 	if (!drvdata) {
172417bba72fSSascha Hauer 		dev_err(&pdev->dev, "unable to find driver data\n");
172517bba72fSSascha Hauer 		return -EINVAL;
172617bba72fSSascha Hauer 	}
17271ec1e82fSSascha Hauer 
172842536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
172942536b9fSPhilippe Retornaz 	if (ret)
173042536b9fSPhilippe Retornaz 		return ret;
173142536b9fSPhilippe Retornaz 
17327f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
17331ec1e82fSSascha Hauer 	if (!sdma)
17341ec1e82fSSascha Hauer 		return -ENOMEM;
17351ec1e82fSSascha Hauer 
17362ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
173773eab978SSascha Hauer 
17381ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
173917bba72fSSascha Hauer 	sdma->drvdata = drvdata;
17401ec1e82fSSascha Hauer 
17411ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
17427f24e0eeSFabio Estevam 	if (irq < 0)
174363c72e02SFabio Estevam 		return irq;
17441ec1e82fSSascha Hauer 
17457f24e0eeSFabio Estevam 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
17467f24e0eeSFabio Estevam 	sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
17477f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
17487f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
17491ec1e82fSSascha Hauer 
17507560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
17517f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
17527f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
17531ec1e82fSSascha Hauer 
17547560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
17557f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
17567f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
17577560e3f3SSascha Hauer 
1758*fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ipg);
1759*fb9caf37SArvind Yadav 	if (ret)
1760*fb9caf37SArvind Yadav 		return ret;
1761*fb9caf37SArvind Yadav 
1762*fb9caf37SArvind Yadav 	ret = clk_prepare(sdma->clk_ahb);
1763*fb9caf37SArvind Yadav 	if (ret)
1764*fb9caf37SArvind Yadav 		goto err_clk;
17657560e3f3SSascha Hauer 
17667f24e0eeSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
17677f24e0eeSFabio Estevam 			       sdma);
17681ec1e82fSSascha Hauer 	if (ret)
1769*fb9caf37SArvind Yadav 		goto err_irq;
17701ec1e82fSSascha Hauer 
17715bb9dbb5SVinod Koul 	sdma->irq = irq;
17725bb9dbb5SVinod Koul 
17735b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
1774*fb9caf37SArvind Yadav 	if (!sdma->script_addrs) {
1775*fb9caf37SArvind Yadav 		ret = -ENOMEM;
1776*fb9caf37SArvind Yadav 		goto err_irq;
1777*fb9caf37SArvind Yadav 	}
17781ec1e82fSSascha Hauer 
177936e2f21aSSascha Hauer 	/* initially no scripts available */
178036e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
178136e2f21aSSascha Hauer 	for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
178236e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
178336e2f21aSSascha Hauer 
17847214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
17857214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
17867214a8b1SSascha Hauer 
17871ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
17881ec1e82fSSascha Hauer 	/* Initialize channel parameters */
17891ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
17901ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
17911ec1e82fSSascha Hauer 
17921ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
17931ec1e82fSSascha Hauer 		spin_lock_init(&sdmac->lock);
17941ec1e82fSSascha Hauer 
17951ec1e82fSSascha Hauer 		sdmac->chan.device = &sdma->dma_device;
17968ac69546SRussell King - ARM Linux 		dma_cookie_init(&sdmac->chan);
17971ec1e82fSSascha Hauer 		sdmac->channel = i;
17981ec1e82fSSascha Hauer 
179915f30f51SNandor Han 		tasklet_init(&sdmac->tasklet, mxc_sdma_handle_channel_normal,
1800abd9ccc8SHuang Shijie 			     (unsigned long) sdmac);
180123889c63SSascha Hauer 		/*
180223889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
180323889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
180423889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
180523889c63SSascha Hauer 		 */
180623889c63SSascha Hauer 		if (i)
180723889c63SSascha Hauer 			list_add_tail(&sdmac->chan.device_node,
180823889c63SSascha Hauer 					&sdma->dma_device.channels);
18091ec1e82fSSascha Hauer 	}
18101ec1e82fSSascha Hauer 
18115b28aa31SSascha Hauer 	ret = sdma_init(sdma);
18121ec1e82fSSascha Hauer 	if (ret)
18131ec1e82fSSascha Hauer 		goto err_init;
18141ec1e82fSSascha Hauer 
1815d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
1816d078cd1bSZidan Wang 	if (ret)
1817d078cd1bSZidan Wang 		goto err_init;
1818d078cd1bSZidan Wang 
1819dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
1820dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
1821580975d7SShawn Guo 	if (pdata && pdata->script_addrs)
18225b28aa31SSascha Hauer 		sdma_add_scripts(sdma, pdata->script_addrs);
18235b28aa31SSascha Hauer 
1824580975d7SShawn Guo 	if (pdata) {
18256d0d7e2dSFabio Estevam 		ret = sdma_get_firmware(sdma, pdata->fw_name);
18266d0d7e2dSFabio Estevam 		if (ret)
1827ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
1828580975d7SShawn Guo 	} else {
1829580975d7SShawn Guo 		/*
1830580975d7SShawn Guo 		 * Because that device tree does not encode ROM script address,
1831580975d7SShawn Guo 		 * the RAM script in firmware is mandatory for device tree
1832580975d7SShawn Guo 		 * probe, otherwise it fails.
1833580975d7SShawn Guo 		 */
1834580975d7SShawn Guo 		ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1835580975d7SShawn Guo 					      &fw_name);
18366602b0ddSFabio Estevam 		if (ret)
1837ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware name\n");
18386602b0ddSFabio Estevam 		else {
1839580975d7SShawn Guo 			ret = sdma_get_firmware(sdma, fw_name);
18406602b0ddSFabio Estevam 			if (ret)
1841ad1122e5SFabio Estevam 				dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
1842580975d7SShawn Guo 		}
1843580975d7SShawn Guo 	}
18445b28aa31SSascha Hauer 
18451ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
18461ec1e82fSSascha Hauer 
18471ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
18481ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
18491ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
18501ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
18511ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
18527b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
18537f3ff14bSJiada Wang 	sdma->dma_device.device_terminate_all = sdma_disable_channel_with_delay;
18541e4a4f50SFabio Estevam 	sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
18551e4a4f50SFabio Estevam 	sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
18561e4a4f50SFabio Estevam 	sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
18576f3125ceSLucas Stach 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
18581ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
1859b9b3f82fSSascha Hauer 	sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1860b9b3f82fSSascha Hauer 	dma_set_max_seg_size(sdma->dma_device.dev, 65535);
18611ec1e82fSSascha Hauer 
186223e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
186323e11811SVignesh Raman 
18641ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
18651ec1e82fSSascha Hauer 	if (ret) {
18661ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
18671ec1e82fSSascha Hauer 		goto err_init;
18681ec1e82fSSascha Hauer 	}
18691ec1e82fSSascha Hauer 
18709479e17cSShawn Guo 	if (np) {
18719479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
18729479e17cSShawn Guo 		if (ret) {
18739479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
18749479e17cSShawn Guo 			goto err_register;
18759479e17cSShawn Guo 		}
18768391ecf4SShengjiu Wang 
18778391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
18788391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
18798391ecf4SShengjiu Wang 		if (!ret) {
18808391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
18818391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
18828391ecf4SShengjiu Wang 		}
18838391ecf4SShengjiu Wang 		of_node_put(spba_bus);
18849479e17cSShawn Guo 	}
18859479e17cSShawn Guo 
18861ec1e82fSSascha Hauer 	return 0;
18871ec1e82fSSascha Hauer 
18889479e17cSShawn Guo err_register:
18899479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
18901ec1e82fSSascha Hauer err_init:
18911ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
1892*fb9caf37SArvind Yadav err_irq:
1893*fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
1894*fb9caf37SArvind Yadav err_clk:
1895*fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
1896939fd4f0SShawn Guo 	return ret;
18971ec1e82fSSascha Hauer }
18981ec1e82fSSascha Hauer 
18991d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
19001ec1e82fSSascha Hauer {
190123e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
1902c12fe497SVignesh Raman 	int i;
190323e11811SVignesh Raman 
19045bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
190523e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
190623e11811SVignesh Raman 	kfree(sdma->script_addrs);
1907*fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ahb);
1908*fb9caf37SArvind Yadav 	clk_unprepare(sdma->clk_ipg);
1909c12fe497SVignesh Raman 	/* Kill the tasklet */
1910c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1911c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
1912c12fe497SVignesh Raman 
1913c12fe497SVignesh Raman 		tasklet_kill(&sdmac->tasklet);
1914c12fe497SVignesh Raman 	}
191523e11811SVignesh Raman 
191623e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
191723e11811SVignesh Raman 	return 0;
19181ec1e82fSSascha Hauer }
19191ec1e82fSSascha Hauer 
19201ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
19211ec1e82fSSascha Hauer 	.driver		= {
19221ec1e82fSSascha Hauer 		.name	= "imx-sdma",
1923580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
19241ec1e82fSSascha Hauer 	},
192562550cd7SShawn Guo 	.id_table	= sdma_devtypes,
19261d1bbd30SMaxin B. John 	.remove		= sdma_remove,
192723e11811SVignesh Raman 	.probe		= sdma_probe,
19281ec1e82fSSascha Hauer };
19291ec1e82fSSascha Hauer 
193023e11811SVignesh Raman module_platform_driver(sdma_driver);
19311ec1e82fSSascha Hauer 
19321ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
19331ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
19341ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
1935