xref: /openbmc/linux/drivers/dma/imx-sdma.c (revision 0d605ba0b8ce7f5963124853774cc9bd84589a99)
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 */
1871ec1e82fSSascha Hauer 	u32 command :  8; /* command mostlky 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
3011ec1e82fSSascha Hauer  * @num_bd		max NUM_BD. number of descriptors currently handling
3021ec1e82fSSascha Hauer  */
3031ec1e82fSSascha Hauer struct sdma_channel {
3041ec1e82fSSascha Hauer 	struct sdma_engine		*sdma;
3051ec1e82fSSascha Hauer 	unsigned int			channel;
306db8196dfSVinod Koul 	enum dma_transfer_direction		direction;
3071ec1e82fSSascha Hauer 	enum sdma_peripheral_type	peripheral_type;
3081ec1e82fSSascha Hauer 	unsigned int			event_id0;
3091ec1e82fSSascha Hauer 	unsigned int			event_id1;
3101ec1e82fSSascha Hauer 	enum dma_slave_buswidth		word_size;
3111ec1e82fSSascha Hauer 	unsigned int			buf_tail;
3121ec1e82fSSascha Hauer 	unsigned int			num_bd;
313d1a792f3SRussell King - ARM Linux 	unsigned int			period_len;
3141ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor	*bd;
3151ec1e82fSSascha Hauer 	dma_addr_t			bd_phys;
3161ec1e82fSSascha Hauer 	unsigned int			pc_from_device, pc_to_device;
3178391ecf4SShengjiu Wang 	unsigned int			device_to_device;
3181ec1e82fSSascha Hauer 	unsigned long			flags;
3198391ecf4SShengjiu Wang 	dma_addr_t			per_address, per_address2;
3200bbc1413SRichard Zhao 	unsigned long			event_mask[2];
3210bbc1413SRichard Zhao 	unsigned long			watermark_level;
3221ec1e82fSSascha Hauer 	u32				shp_addr, per_addr;
3231ec1e82fSSascha Hauer 	struct dma_chan			chan;
3241ec1e82fSSascha Hauer 	spinlock_t			lock;
3251ec1e82fSSascha Hauer 	struct dma_async_tx_descriptor	desc;
3261ec1e82fSSascha Hauer 	enum dma_status			status;
327ab59a510SHuang Shijie 	unsigned int			chn_count;
328ab59a510SHuang Shijie 	unsigned int			chn_real_count;
329abd9ccc8SHuang Shijie 	struct tasklet_struct		tasklet;
3300b351865SNicolin Chen 	struct imx_dma_data		data;
3311ec1e82fSSascha Hauer };
3321ec1e82fSSascha Hauer 
3330bbc1413SRichard Zhao #define IMX_DMA_SG_LOOP		BIT(0)
3341ec1e82fSSascha Hauer 
3351ec1e82fSSascha Hauer #define MAX_DMA_CHANNELS 32
3361ec1e82fSSascha Hauer #define MXC_SDMA_DEFAULT_PRIORITY 1
3371ec1e82fSSascha Hauer #define MXC_SDMA_MIN_PRIORITY 1
3381ec1e82fSSascha Hauer #define MXC_SDMA_MAX_PRIORITY 7
3391ec1e82fSSascha Hauer 
3401ec1e82fSSascha Hauer #define SDMA_FIRMWARE_MAGIC 0x414d4453
3411ec1e82fSSascha Hauer 
3421ec1e82fSSascha Hauer /**
3431ec1e82fSSascha Hauer  * struct sdma_firmware_header - Layout of the firmware image
3441ec1e82fSSascha Hauer  *
3451ec1e82fSSascha Hauer  * @magic		"SDMA"
3461ec1e82fSSascha Hauer  * @version_major	increased whenever layout of struct sdma_script_start_addrs
3471ec1e82fSSascha Hauer  *			changes.
3481ec1e82fSSascha Hauer  * @version_minor	firmware minor version (for binary compatible changes)
3491ec1e82fSSascha Hauer  * @script_addrs_start	offset of struct sdma_script_start_addrs in this image
3501ec1e82fSSascha Hauer  * @num_script_addrs	Number of script addresses in this image
3511ec1e82fSSascha Hauer  * @ram_code_start	offset of SDMA ram image in this firmware image
3521ec1e82fSSascha Hauer  * @ram_code_size	size of SDMA ram image
3531ec1e82fSSascha Hauer  * @script_addrs	Stores the start address of the SDMA scripts
3541ec1e82fSSascha Hauer  *			(in SDMA memory space)
3551ec1e82fSSascha Hauer  */
3561ec1e82fSSascha Hauer struct sdma_firmware_header {
3571ec1e82fSSascha Hauer 	u32	magic;
3581ec1e82fSSascha Hauer 	u32	version_major;
3591ec1e82fSSascha Hauer 	u32	version_minor;
3601ec1e82fSSascha Hauer 	u32	script_addrs_start;
3611ec1e82fSSascha Hauer 	u32	num_script_addrs;
3621ec1e82fSSascha Hauer 	u32	ram_code_start;
3631ec1e82fSSascha Hauer 	u32	ram_code_size;
3641ec1e82fSSascha Hauer };
3651ec1e82fSSascha Hauer 
36617bba72fSSascha Hauer struct sdma_driver_data {
36717bba72fSSascha Hauer 	int chnenbl0;
36817bba72fSSascha Hauer 	int num_events;
369dcfec3c0SSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
37062550cd7SShawn Guo };
37162550cd7SShawn Guo 
3721ec1e82fSSascha Hauer struct sdma_engine {
3731ec1e82fSSascha Hauer 	struct device			*dev;
374b9b3f82fSSascha Hauer 	struct device_dma_parameters	dma_parms;
3751ec1e82fSSascha Hauer 	struct sdma_channel		channel[MAX_DMA_CHANNELS];
3761ec1e82fSSascha Hauer 	struct sdma_channel_control	*channel_control;
3771ec1e82fSSascha Hauer 	void __iomem			*regs;
3781ec1e82fSSascha Hauer 	struct sdma_context_data	*context;
3791ec1e82fSSascha Hauer 	dma_addr_t			context_phys;
3801ec1e82fSSascha Hauer 	struct dma_device		dma_device;
3817560e3f3SSascha Hauer 	struct clk			*clk_ipg;
3827560e3f3SSascha Hauer 	struct clk			*clk_ahb;
3832ccaef05SRichard Zhao 	spinlock_t			channel_0_lock;
384cd72b846SNicolin Chen 	u32				script_number;
3851ec1e82fSSascha Hauer 	struct sdma_script_start_addrs	*script_addrs;
38617bba72fSSascha Hauer 	const struct sdma_driver_data	*drvdata;
3878391ecf4SShengjiu Wang 	u32				spba_start_addr;
3888391ecf4SShengjiu Wang 	u32				spba_end_addr;
3895bb9dbb5SVinod Koul 	unsigned int			irq;
39017bba72fSSascha Hauer };
39117bba72fSSascha Hauer 
392e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx31 = {
39317bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX31,
39417bba72fSSascha Hauer 	.num_events = 32,
39517bba72fSSascha Hauer };
39617bba72fSSascha Hauer 
397dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx25 = {
398dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 729,
399dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 904,
400dcfec3c0SSascha Hauer 	.per_2_app_addr = 1255,
401dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 834,
402dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1120,
403dcfec3c0SSascha Hauer 	.per_2_shp_addr = 1329,
404dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 1048,
405dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1560,
406dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1479,
407dcfec3c0SSascha Hauer 	.app_2_per_addr = 1189,
408dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 770,
409dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1407,
410dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 979,
411dcfec3c0SSascha Hauer };
412dcfec3c0SSascha Hauer 
413e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx25 = {
414dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
415dcfec3c0SSascha Hauer 	.num_events = 48,
416dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx25,
417dcfec3c0SSascha Hauer };
418dcfec3c0SSascha Hauer 
419e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx35 = {
42017bba72fSSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
42117bba72fSSascha Hauer 	.num_events = 48,
4221ec1e82fSSascha Hauer };
4231ec1e82fSSascha Hauer 
424dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx51 = {
425dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
426dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
427dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
428dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 961,
429dcfec3c0SSascha Hauer 	.ata_2_mcu_addr = 1473,
430dcfec3c0SSascha Hauer 	.mcu_2_ata_addr = 1392,
431dcfec3c0SSascha Hauer 	.app_2_per_addr = 1033,
432dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
433dcfec3c0SSascha Hauer 	.shp_2_per_addr = 1251,
434dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 892,
435dcfec3c0SSascha Hauer };
436dcfec3c0SSascha Hauer 
437e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx51 = {
438dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
439dcfec3c0SSascha Hauer 	.num_events = 48,
440dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx51,
441dcfec3c0SSascha Hauer };
442dcfec3c0SSascha Hauer 
443dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx53 = {
444dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
445dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
446dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
447dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
448dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
449dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
450dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
451dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
452dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
453dcfec3c0SSascha Hauer 	.firi_2_mcu_addr = 1193,
454dcfec3c0SSascha Hauer 	.mcu_2_firi_addr = 1290,
455dcfec3c0SSascha Hauer };
456dcfec3c0SSascha Hauer 
457e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx53 = {
458dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
459dcfec3c0SSascha Hauer 	.num_events = 48,
460dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx53,
461dcfec3c0SSascha Hauer };
462dcfec3c0SSascha Hauer 
463dcfec3c0SSascha Hauer static struct sdma_script_start_addrs sdma_script_imx6q = {
464dcfec3c0SSascha Hauer 	.ap_2_ap_addr = 642,
465dcfec3c0SSascha Hauer 	.uart_2_mcu_addr = 817,
466dcfec3c0SSascha Hauer 	.mcu_2_app_addr = 747,
467dcfec3c0SSascha Hauer 	.per_2_per_addr = 6331,
468dcfec3c0SSascha Hauer 	.uartsh_2_mcu_addr = 1032,
469dcfec3c0SSascha Hauer 	.mcu_2_shp_addr = 960,
470dcfec3c0SSascha Hauer 	.app_2_mcu_addr = 683,
471dcfec3c0SSascha Hauer 	.shp_2_mcu_addr = 891,
472dcfec3c0SSascha Hauer 	.spdif_2_mcu_addr = 1100,
473dcfec3c0SSascha Hauer 	.mcu_2_spdif_addr = 1134,
474dcfec3c0SSascha Hauer };
475dcfec3c0SSascha Hauer 
476e9fd58deSFabio Estevam static struct sdma_driver_data sdma_imx6q = {
477dcfec3c0SSascha Hauer 	.chnenbl0 = SDMA_CHNENBL0_IMX35,
478dcfec3c0SSascha Hauer 	.num_events = 48,
479dcfec3c0SSascha Hauer 	.script_addrs = &sdma_script_imx6q,
480dcfec3c0SSascha Hauer };
481dcfec3c0SSascha Hauer 
482afe7cdedSKrzysztof Kozlowski static const struct platform_device_id sdma_devtypes[] = {
48362550cd7SShawn Guo 	{
484dcfec3c0SSascha Hauer 		.name = "imx25-sdma",
485dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx25,
486dcfec3c0SSascha Hauer 	}, {
48762550cd7SShawn Guo 		.name = "imx31-sdma",
48817bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx31,
48962550cd7SShawn Guo 	}, {
49062550cd7SShawn Guo 		.name = "imx35-sdma",
49117bba72fSSascha Hauer 		.driver_data = (unsigned long)&sdma_imx35,
49262550cd7SShawn Guo 	}, {
493dcfec3c0SSascha Hauer 		.name = "imx51-sdma",
494dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx51,
495dcfec3c0SSascha Hauer 	}, {
496dcfec3c0SSascha Hauer 		.name = "imx53-sdma",
497dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx53,
498dcfec3c0SSascha Hauer 	}, {
499dcfec3c0SSascha Hauer 		.name = "imx6q-sdma",
500dcfec3c0SSascha Hauer 		.driver_data = (unsigned long)&sdma_imx6q,
501dcfec3c0SSascha Hauer 	}, {
50262550cd7SShawn Guo 		/* sentinel */
50362550cd7SShawn Guo 	}
50462550cd7SShawn Guo };
50562550cd7SShawn Guo MODULE_DEVICE_TABLE(platform, sdma_devtypes);
50662550cd7SShawn Guo 
507580975d7SShawn Guo static const struct of_device_id sdma_dt_ids[] = {
508dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx6q-sdma", .data = &sdma_imx6q, },
509dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx53-sdma", .data = &sdma_imx53, },
510dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx51-sdma", .data = &sdma_imx51, },
51117bba72fSSascha Hauer 	{ .compatible = "fsl,imx35-sdma", .data = &sdma_imx35, },
512dcfec3c0SSascha Hauer 	{ .compatible = "fsl,imx31-sdma", .data = &sdma_imx31, },
51363edea16SMarkus Pargmann 	{ .compatible = "fsl,imx25-sdma", .data = &sdma_imx25, },
514580975d7SShawn Guo 	{ /* sentinel */ }
515580975d7SShawn Guo };
516580975d7SShawn Guo MODULE_DEVICE_TABLE(of, sdma_dt_ids);
517580975d7SShawn Guo 
5180bbc1413SRichard Zhao #define SDMA_H_CONFIG_DSPDMA	BIT(12) /* indicates if the DSPDMA is used */
5190bbc1413SRichard Zhao #define SDMA_H_CONFIG_RTD_PINS	BIT(11) /* indicates if Real-Time Debug pins are enabled */
5200bbc1413SRichard Zhao #define SDMA_H_CONFIG_ACR	BIT(4)  /* indicates if AHB freq /core freq = 2 or 1 */
5211ec1e82fSSascha Hauer #define SDMA_H_CONFIG_CSM	(3)       /* indicates which context switch mode is selected*/
5221ec1e82fSSascha Hauer 
5231ec1e82fSSascha Hauer static inline u32 chnenbl_ofs(struct sdma_engine *sdma, unsigned int event)
5241ec1e82fSSascha Hauer {
52517bba72fSSascha Hauer 	u32 chnenbl0 = sdma->drvdata->chnenbl0;
5261ec1e82fSSascha Hauer 	return chnenbl0 + event * 4;
5271ec1e82fSSascha Hauer }
5281ec1e82fSSascha Hauer 
5291ec1e82fSSascha Hauer static int sdma_config_ownership(struct sdma_channel *sdmac,
5301ec1e82fSSascha Hauer 		bool event_override, bool mcu_override, bool dsp_override)
5311ec1e82fSSascha Hauer {
5321ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
5331ec1e82fSSascha Hauer 	int channel = sdmac->channel;
5340bbc1413SRichard Zhao 	unsigned long evt, mcu, dsp;
5351ec1e82fSSascha Hauer 
5361ec1e82fSSascha Hauer 	if (event_override && mcu_override && dsp_override)
5371ec1e82fSSascha Hauer 		return -EINVAL;
5381ec1e82fSSascha Hauer 
539c4b56857SRichard Zhao 	evt = readl_relaxed(sdma->regs + SDMA_H_EVTOVR);
540c4b56857SRichard Zhao 	mcu = readl_relaxed(sdma->regs + SDMA_H_HOSTOVR);
541c4b56857SRichard Zhao 	dsp = readl_relaxed(sdma->regs + SDMA_H_DSPOVR);
5421ec1e82fSSascha Hauer 
5431ec1e82fSSascha Hauer 	if (dsp_override)
5440bbc1413SRichard Zhao 		__clear_bit(channel, &dsp);
5451ec1e82fSSascha Hauer 	else
5460bbc1413SRichard Zhao 		__set_bit(channel, &dsp);
5471ec1e82fSSascha Hauer 
5481ec1e82fSSascha Hauer 	if (event_override)
5490bbc1413SRichard Zhao 		__clear_bit(channel, &evt);
5501ec1e82fSSascha Hauer 	else
5510bbc1413SRichard Zhao 		__set_bit(channel, &evt);
5521ec1e82fSSascha Hauer 
5531ec1e82fSSascha Hauer 	if (mcu_override)
5540bbc1413SRichard Zhao 		__clear_bit(channel, &mcu);
5551ec1e82fSSascha Hauer 	else
5560bbc1413SRichard Zhao 		__set_bit(channel, &mcu);
5571ec1e82fSSascha Hauer 
558c4b56857SRichard Zhao 	writel_relaxed(evt, sdma->regs + SDMA_H_EVTOVR);
559c4b56857SRichard Zhao 	writel_relaxed(mcu, sdma->regs + SDMA_H_HOSTOVR);
560c4b56857SRichard Zhao 	writel_relaxed(dsp, sdma->regs + SDMA_H_DSPOVR);
5611ec1e82fSSascha Hauer 
5621ec1e82fSSascha Hauer 	return 0;
5631ec1e82fSSascha Hauer }
5641ec1e82fSSascha Hauer 
565b9a59166SRichard Zhao static void sdma_enable_channel(struct sdma_engine *sdma, int channel)
566b9a59166SRichard Zhao {
5670bbc1413SRichard Zhao 	writel(BIT(channel), sdma->regs + SDMA_H_START);
568b9a59166SRichard Zhao }
569b9a59166SRichard Zhao 
5701ec1e82fSSascha Hauer /*
5712ccaef05SRichard Zhao  * sdma_run_channel0 - run a channel and wait till it's done
5721ec1e82fSSascha Hauer  */
5732ccaef05SRichard Zhao static int sdma_run_channel0(struct sdma_engine *sdma)
5741ec1e82fSSascha Hauer {
5751ec1e82fSSascha Hauer 	int ret;
5761d069bfaSMichael Olbrich 	u32 reg;
5771ec1e82fSSascha Hauer 
5782ccaef05SRichard Zhao 	sdma_enable_channel(sdma, 0);
5791ec1e82fSSascha Hauer 
5801d069bfaSMichael Olbrich 	ret = readl_relaxed_poll_timeout_atomic(sdma->regs + SDMA_H_STATSTOP,
5811d069bfaSMichael Olbrich 						reg, !(reg & 1), 1, 500);
5821d069bfaSMichael Olbrich 	if (ret)
5832ccaef05SRichard Zhao 		dev_err(sdma->dev, "Timeout waiting for CH0 ready\n");
5841ec1e82fSSascha Hauer 
585855832e4SRobin Gong 	/* Set bits of CONFIG register with dynamic context switching */
586855832e4SRobin Gong 	if (readl(sdma->regs + SDMA_H_CONFIG) == 0)
587855832e4SRobin Gong 		writel_relaxed(SDMA_H_CONFIG_CSM, sdma->regs + SDMA_H_CONFIG);
588855832e4SRobin Gong 
5891d069bfaSMichael Olbrich 	return ret;
5901ec1e82fSSascha Hauer }
5911ec1e82fSSascha Hauer 
5921ec1e82fSSascha Hauer static int sdma_load_script(struct sdma_engine *sdma, void *buf, int size,
5931ec1e82fSSascha Hauer 		u32 address)
5941ec1e82fSSascha Hauer {
5951ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
5961ec1e82fSSascha Hauer 	void *buf_virt;
5971ec1e82fSSascha Hauer 	dma_addr_t buf_phys;
5981ec1e82fSSascha Hauer 	int ret;
5992ccaef05SRichard Zhao 	unsigned long flags;
60073eab978SSascha Hauer 
6011ec1e82fSSascha Hauer 	buf_virt = dma_alloc_coherent(NULL,
6021ec1e82fSSascha Hauer 			size,
6031ec1e82fSSascha Hauer 			&buf_phys, GFP_KERNEL);
60473eab978SSascha Hauer 	if (!buf_virt) {
6052ccaef05SRichard Zhao 		return -ENOMEM;
60673eab978SSascha Hauer 	}
6071ec1e82fSSascha Hauer 
6082ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
6092ccaef05SRichard Zhao 
6101ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETPM;
6111ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
6121ec1e82fSSascha Hauer 	bd0->mode.count = size / 2;
6131ec1e82fSSascha Hauer 	bd0->buffer_addr = buf_phys;
6141ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = address;
6151ec1e82fSSascha Hauer 
6161ec1e82fSSascha Hauer 	memcpy(buf_virt, buf, size);
6171ec1e82fSSascha Hauer 
6182ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
6192ccaef05SRichard Zhao 
6202ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
6211ec1e82fSSascha Hauer 
6221ec1e82fSSascha Hauer 	dma_free_coherent(NULL, size, buf_virt, buf_phys);
6231ec1e82fSSascha Hauer 
6241ec1e82fSSascha Hauer 	return ret;
6251ec1e82fSSascha Hauer }
6261ec1e82fSSascha Hauer 
6271ec1e82fSSascha Hauer static void sdma_event_enable(struct sdma_channel *sdmac, unsigned int event)
6281ec1e82fSSascha Hauer {
6291ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6301ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6310bbc1413SRichard Zhao 	unsigned long val;
6321ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
6331ec1e82fSSascha Hauer 
634c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
6350bbc1413SRichard Zhao 	__set_bit(channel, &val);
636c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
6371ec1e82fSSascha Hauer }
6381ec1e82fSSascha Hauer 
6391ec1e82fSSascha Hauer static void sdma_event_disable(struct sdma_channel *sdmac, unsigned int event)
6401ec1e82fSSascha Hauer {
6411ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
6421ec1e82fSSascha Hauer 	int channel = sdmac->channel;
6431ec1e82fSSascha Hauer 	u32 chnenbl = chnenbl_ofs(sdma, event);
6440bbc1413SRichard Zhao 	unsigned long val;
6451ec1e82fSSascha Hauer 
646c4b56857SRichard Zhao 	val = readl_relaxed(sdma->regs + chnenbl);
6470bbc1413SRichard Zhao 	__clear_bit(channel, &val);
648c4b56857SRichard Zhao 	writel_relaxed(val, sdma->regs + chnenbl);
6491ec1e82fSSascha Hauer }
6501ec1e82fSSascha Hauer 
6511ec1e82fSSascha Hauer static void sdma_handle_channel_loop(struct sdma_channel *sdmac)
6521ec1e82fSSascha Hauer {
653d1a792f3SRussell King - ARM Linux 	if (sdmac->desc.callback)
654d1a792f3SRussell King - ARM Linux 		sdmac->desc.callback(sdmac->desc.callback_param);
655d1a792f3SRussell King - ARM Linux }
656d1a792f3SRussell King - ARM Linux 
657d1a792f3SRussell King - ARM Linux static void sdma_update_channel_loop(struct sdma_channel *sdmac)
658d1a792f3SRussell King - ARM Linux {
6591ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
6601ec1e82fSSascha Hauer 
6611ec1e82fSSascha Hauer 	/*
6621ec1e82fSSascha Hauer 	 * loop mode. Iterate over descriptors, re-setup them and
6631ec1e82fSSascha Hauer 	 * call callback function.
6641ec1e82fSSascha Hauer 	 */
6651ec1e82fSSascha Hauer 	while (1) {
6661ec1e82fSSascha Hauer 		bd = &sdmac->bd[sdmac->buf_tail];
6671ec1e82fSSascha Hauer 
6681ec1e82fSSascha Hauer 		if (bd->mode.status & BD_DONE)
6691ec1e82fSSascha Hauer 			break;
6701ec1e82fSSascha Hauer 
6711ec1e82fSSascha Hauer 		if (bd->mode.status & BD_RROR)
6721ec1e82fSSascha Hauer 			sdmac->status = DMA_ERROR;
6731ec1e82fSSascha Hauer 
6741ec1e82fSSascha Hauer 		bd->mode.status |= BD_DONE;
6751ec1e82fSSascha Hauer 		sdmac->buf_tail++;
6761ec1e82fSSascha Hauer 		sdmac->buf_tail %= sdmac->num_bd;
6771ec1e82fSSascha Hauer 	}
6781ec1e82fSSascha Hauer }
6791ec1e82fSSascha Hauer 
6801ec1e82fSSascha Hauer static void mxc_sdma_handle_channel_normal(struct sdma_channel *sdmac)
6811ec1e82fSSascha Hauer {
6821ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd;
6831ec1e82fSSascha Hauer 	int i, error = 0;
6841ec1e82fSSascha Hauer 
685ab59a510SHuang Shijie 	sdmac->chn_real_count = 0;
6861ec1e82fSSascha Hauer 	/*
6871ec1e82fSSascha Hauer 	 * non loop mode. Iterate over all descriptors, collect
6881ec1e82fSSascha Hauer 	 * errors and call callback function
6891ec1e82fSSascha Hauer 	 */
6901ec1e82fSSascha Hauer 	for (i = 0; i < sdmac->num_bd; i++) {
6911ec1e82fSSascha Hauer 		bd = &sdmac->bd[i];
6921ec1e82fSSascha Hauer 
6931ec1e82fSSascha Hauer 		 if (bd->mode.status & (BD_DONE | BD_RROR))
6941ec1e82fSSascha Hauer 			error = -EIO;
695ab59a510SHuang Shijie 		 sdmac->chn_real_count += bd->mode.count;
6961ec1e82fSSascha Hauer 	}
6971ec1e82fSSascha Hauer 
6981ec1e82fSSascha Hauer 	if (error)
6991ec1e82fSSascha Hauer 		sdmac->status = DMA_ERROR;
7001ec1e82fSSascha Hauer 	else
701409bff6aSVinod Koul 		sdmac->status = DMA_COMPLETE;
7021ec1e82fSSascha Hauer 
703f7fbce07SRussell King - ARM Linux 	dma_cookie_complete(&sdmac->desc);
7041ec1e82fSSascha Hauer 	if (sdmac->desc.callback)
7051ec1e82fSSascha Hauer 		sdmac->desc.callback(sdmac->desc.callback_param);
7061ec1e82fSSascha Hauer }
7071ec1e82fSSascha Hauer 
708abd9ccc8SHuang Shijie static void sdma_tasklet(unsigned long data)
7091ec1e82fSSascha Hauer {
710abd9ccc8SHuang Shijie 	struct sdma_channel *sdmac = (struct sdma_channel *) data;
711abd9ccc8SHuang Shijie 
7121ec1e82fSSascha Hauer 	if (sdmac->flags & IMX_DMA_SG_LOOP)
7131ec1e82fSSascha Hauer 		sdma_handle_channel_loop(sdmac);
7141ec1e82fSSascha Hauer 	else
7151ec1e82fSSascha Hauer 		mxc_sdma_handle_channel_normal(sdmac);
7161ec1e82fSSascha Hauer }
7171ec1e82fSSascha Hauer 
7181ec1e82fSSascha Hauer static irqreturn_t sdma_int_handler(int irq, void *dev_id)
7191ec1e82fSSascha Hauer {
7201ec1e82fSSascha Hauer 	struct sdma_engine *sdma = dev_id;
7210bbc1413SRichard Zhao 	unsigned long stat;
7221ec1e82fSSascha Hauer 
723c4b56857SRichard Zhao 	stat = readl_relaxed(sdma->regs + SDMA_H_INTR);
724c4b56857SRichard Zhao 	writel_relaxed(stat, sdma->regs + SDMA_H_INTR);
7251d069bfaSMichael Olbrich 	/* channel 0 is special and not handled here, see run_channel0() */
7261d069bfaSMichael Olbrich 	stat &= ~1;
7271ec1e82fSSascha Hauer 
7281ec1e82fSSascha Hauer 	while (stat) {
7291ec1e82fSSascha Hauer 		int channel = fls(stat) - 1;
7301ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[channel];
7311ec1e82fSSascha Hauer 
732d1a792f3SRussell King - ARM Linux 		if (sdmac->flags & IMX_DMA_SG_LOOP)
733d1a792f3SRussell King - ARM Linux 			sdma_update_channel_loop(sdmac);
734d1a792f3SRussell King - ARM Linux 
735abd9ccc8SHuang Shijie 		tasklet_schedule(&sdmac->tasklet);
7361ec1e82fSSascha Hauer 
7370bbc1413SRichard Zhao 		__clear_bit(channel, &stat);
7381ec1e82fSSascha Hauer 	}
7391ec1e82fSSascha Hauer 
7401ec1e82fSSascha Hauer 	return IRQ_HANDLED;
7411ec1e82fSSascha Hauer }
7421ec1e82fSSascha Hauer 
7431ec1e82fSSascha Hauer /*
7441ec1e82fSSascha Hauer  * sets the pc of SDMA script according to the peripheral type
7451ec1e82fSSascha Hauer  */
7461ec1e82fSSascha Hauer static void sdma_get_pc(struct sdma_channel *sdmac,
7471ec1e82fSSascha Hauer 		enum sdma_peripheral_type peripheral_type)
7481ec1e82fSSascha Hauer {
7491ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
7501ec1e82fSSascha Hauer 	int per_2_emi = 0, emi_2_per = 0;
7511ec1e82fSSascha Hauer 	/*
7521ec1e82fSSascha Hauer 	 * These are needed once we start to support transfers between
7531ec1e82fSSascha Hauer 	 * two peripherals or memory-to-memory transfers
7541ec1e82fSSascha Hauer 	 */
755*0d605ba0SVinod Koul 	int per_2_per = 0;
7561ec1e82fSSascha Hauer 
7571ec1e82fSSascha Hauer 	sdmac->pc_from_device = 0;
7581ec1e82fSSascha Hauer 	sdmac->pc_to_device = 0;
7598391ecf4SShengjiu Wang 	sdmac->device_to_device = 0;
7601ec1e82fSSascha Hauer 
7611ec1e82fSSascha Hauer 	switch (peripheral_type) {
7621ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
7631ec1e82fSSascha Hauer 		break;
7641ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
7651ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->bp_2_ap_addr;
7661ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ap_2_bp_addr;
7671ec1e82fSSascha Hauer 		break;
7681ec1e82fSSascha Hauer 	case IMX_DMATYPE_FIRI:
7691ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->firi_2_mcu_addr;
7701ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_firi_addr;
7711ec1e82fSSascha Hauer 		break;
7721ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART:
7731ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uart_2_mcu_addr;
7741ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
7751ec1e82fSSascha Hauer 		break;
7761ec1e82fSSascha Hauer 	case IMX_DMATYPE_UART_SP:
7771ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->uartsh_2_mcu_addr;
7781ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
7791ec1e82fSSascha Hauer 		break;
7801ec1e82fSSascha Hauer 	case IMX_DMATYPE_ATA:
7811ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->ata_2_mcu_addr;
7821ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_ata_addr;
7831ec1e82fSSascha Hauer 		break;
7841ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI:
7851ec1e82fSSascha Hauer 	case IMX_DMATYPE_EXT:
7861ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI:
78729aebfdeSNicolin Chen 	case IMX_DMATYPE_SAI:
7881ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->app_2_mcu_addr;
7891ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_app_addr;
7901ec1e82fSSascha Hauer 		break;
7911a895578SNicolin Chen 	case IMX_DMATYPE_SSI_DUAL:
7921a895578SNicolin Chen 		per_2_emi = sdma->script_addrs->ssish_2_mcu_addr;
7931a895578SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_ssish_addr;
7941a895578SNicolin Chen 		break;
7951ec1e82fSSascha Hauer 	case IMX_DMATYPE_SSI_SP:
7961ec1e82fSSascha Hauer 	case IMX_DMATYPE_MMC:
7971ec1e82fSSascha Hauer 	case IMX_DMATYPE_SDHC:
7981ec1e82fSSascha Hauer 	case IMX_DMATYPE_CSPI_SP:
7991ec1e82fSSascha Hauer 	case IMX_DMATYPE_ESAI:
8001ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC_SP:
8011ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
8021ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
8031ec1e82fSSascha Hauer 		break;
8041ec1e82fSSascha Hauer 	case IMX_DMATYPE_ASRC:
8051ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->asrc_2_mcu_addr;
8061ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->asrc_2_mcu_addr;
8071ec1e82fSSascha Hauer 		per_2_per = sdma->script_addrs->per_2_per_addr;
8081ec1e82fSSascha Hauer 		break;
809f892afb0SNicolin Chen 	case IMX_DMATYPE_ASRC_SP:
810f892afb0SNicolin Chen 		per_2_emi = sdma->script_addrs->shp_2_mcu_addr;
811f892afb0SNicolin Chen 		emi_2_per = sdma->script_addrs->mcu_2_shp_addr;
812f892afb0SNicolin Chen 		per_2_per = sdma->script_addrs->per_2_per_addr;
813f892afb0SNicolin Chen 		break;
8141ec1e82fSSascha Hauer 	case IMX_DMATYPE_MSHC:
8151ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->mshc_2_mcu_addr;
8161ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_mshc_addr;
8171ec1e82fSSascha Hauer 		break;
8181ec1e82fSSascha Hauer 	case IMX_DMATYPE_CCM:
8191ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->dptc_dvfs_addr;
8201ec1e82fSSascha Hauer 		break;
8211ec1e82fSSascha Hauer 	case IMX_DMATYPE_SPDIF:
8221ec1e82fSSascha Hauer 		per_2_emi = sdma->script_addrs->spdif_2_mcu_addr;
8231ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->mcu_2_spdif_addr;
8241ec1e82fSSascha Hauer 		break;
8251ec1e82fSSascha Hauer 	case IMX_DMATYPE_IPU_MEMORY:
8261ec1e82fSSascha Hauer 		emi_2_per = sdma->script_addrs->ext_mem_2_ipu_addr;
8271ec1e82fSSascha Hauer 		break;
8281ec1e82fSSascha Hauer 	default:
8291ec1e82fSSascha Hauer 		break;
8301ec1e82fSSascha Hauer 	}
8311ec1e82fSSascha Hauer 
8321ec1e82fSSascha Hauer 	sdmac->pc_from_device = per_2_emi;
8331ec1e82fSSascha Hauer 	sdmac->pc_to_device = emi_2_per;
8348391ecf4SShengjiu Wang 	sdmac->device_to_device = per_2_per;
8351ec1e82fSSascha Hauer }
8361ec1e82fSSascha Hauer 
8371ec1e82fSSascha Hauer static int sdma_load_context(struct sdma_channel *sdmac)
8381ec1e82fSSascha Hauer {
8391ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8401ec1e82fSSascha Hauer 	int channel = sdmac->channel;
8411ec1e82fSSascha Hauer 	int load_address;
8421ec1e82fSSascha Hauer 	struct sdma_context_data *context = sdma->context;
8431ec1e82fSSascha Hauer 	struct sdma_buffer_descriptor *bd0 = sdma->channel[0].bd;
8441ec1e82fSSascha Hauer 	int ret;
8452ccaef05SRichard Zhao 	unsigned long flags;
8461ec1e82fSSascha Hauer 
8478391ecf4SShengjiu Wang 	if (sdmac->direction == DMA_DEV_TO_MEM)
8481ec1e82fSSascha Hauer 		load_address = sdmac->pc_from_device;
8498391ecf4SShengjiu Wang 	else if (sdmac->direction == DMA_DEV_TO_DEV)
8508391ecf4SShengjiu Wang 		load_address = sdmac->device_to_device;
8518391ecf4SShengjiu Wang 	else
8521ec1e82fSSascha Hauer 		load_address = sdmac->pc_to_device;
8531ec1e82fSSascha Hauer 
8541ec1e82fSSascha Hauer 	if (load_address < 0)
8551ec1e82fSSascha Hauer 		return load_address;
8561ec1e82fSSascha Hauer 
8571ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "load_address = %d\n", load_address);
8580bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "wml = 0x%08x\n", (u32)sdmac->watermark_level);
8591ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "shp_addr = 0x%08x\n", sdmac->shp_addr);
8601ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "per_addr = 0x%08x\n", sdmac->per_addr);
8610bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask0 = 0x%08x\n", (u32)sdmac->event_mask[0]);
8620bbc1413SRichard Zhao 	dev_dbg(sdma->dev, "event_mask1 = 0x%08x\n", (u32)sdmac->event_mask[1]);
8631ec1e82fSSascha Hauer 
8642ccaef05SRichard Zhao 	spin_lock_irqsave(&sdma->channel_0_lock, flags);
86573eab978SSascha Hauer 
8661ec1e82fSSascha Hauer 	memset(context, 0, sizeof(*context));
8671ec1e82fSSascha Hauer 	context->channel_state.pc = load_address;
8681ec1e82fSSascha Hauer 
8691ec1e82fSSascha Hauer 	/* Send by context the event mask,base address for peripheral
8701ec1e82fSSascha Hauer 	 * and watermark level
8711ec1e82fSSascha Hauer 	 */
8720bbc1413SRichard Zhao 	context->gReg[0] = sdmac->event_mask[1];
8730bbc1413SRichard Zhao 	context->gReg[1] = sdmac->event_mask[0];
8741ec1e82fSSascha Hauer 	context->gReg[2] = sdmac->per_addr;
8751ec1e82fSSascha Hauer 	context->gReg[6] = sdmac->shp_addr;
8761ec1e82fSSascha Hauer 	context->gReg[7] = sdmac->watermark_level;
8771ec1e82fSSascha Hauer 
8781ec1e82fSSascha Hauer 	bd0->mode.command = C0_SETDM;
8791ec1e82fSSascha Hauer 	bd0->mode.status = BD_DONE | BD_INTR | BD_WRAP | BD_EXTD;
8801ec1e82fSSascha Hauer 	bd0->mode.count = sizeof(*context) / 4;
8811ec1e82fSSascha Hauer 	bd0->buffer_addr = sdma->context_phys;
8821ec1e82fSSascha Hauer 	bd0->ext_buffer_addr = 2048 + (sizeof(*context) / 4) * channel;
8832ccaef05SRichard Zhao 	ret = sdma_run_channel0(sdma);
8841ec1e82fSSascha Hauer 
8852ccaef05SRichard Zhao 	spin_unlock_irqrestore(&sdma->channel_0_lock, flags);
88673eab978SSascha Hauer 
8871ec1e82fSSascha Hauer 	return ret;
8881ec1e82fSSascha Hauer }
8891ec1e82fSSascha Hauer 
8907b350ab0SMaxime Ripard static struct sdma_channel *to_sdma_chan(struct dma_chan *chan)
8911ec1e82fSSascha Hauer {
8927b350ab0SMaxime Ripard 	return container_of(chan, struct sdma_channel, chan);
8937b350ab0SMaxime Ripard }
8947b350ab0SMaxime Ripard 
8957b350ab0SMaxime Ripard static int sdma_disable_channel(struct dma_chan *chan)
8967b350ab0SMaxime Ripard {
8977b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
8981ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
8991ec1e82fSSascha Hauer 	int channel = sdmac->channel;
9001ec1e82fSSascha Hauer 
9010bbc1413SRichard Zhao 	writel_relaxed(BIT(channel), sdma->regs + SDMA_H_STATSTOP);
9021ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
9037b350ab0SMaxime Ripard 
9047b350ab0SMaxime Ripard 	return 0;
9051ec1e82fSSascha Hauer }
9061ec1e82fSSascha Hauer 
9078391ecf4SShengjiu Wang static void sdma_set_watermarklevel_for_p2p(struct sdma_channel *sdmac)
9088391ecf4SShengjiu Wang {
9098391ecf4SShengjiu Wang 	struct sdma_engine *sdma = sdmac->sdma;
9108391ecf4SShengjiu Wang 
9118391ecf4SShengjiu Wang 	int lwml = sdmac->watermark_level & SDMA_WATERMARK_LEVEL_LWML;
9128391ecf4SShengjiu Wang 	int hwml = (sdmac->watermark_level & SDMA_WATERMARK_LEVEL_HWML) >> 16;
9138391ecf4SShengjiu Wang 
9148391ecf4SShengjiu Wang 	set_bit(sdmac->event_id0 % 32, &sdmac->event_mask[1]);
9158391ecf4SShengjiu Wang 	set_bit(sdmac->event_id1 % 32, &sdmac->event_mask[0]);
9168391ecf4SShengjiu Wang 
9178391ecf4SShengjiu Wang 	if (sdmac->event_id0 > 31)
9188391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_LWE;
9198391ecf4SShengjiu Wang 
9208391ecf4SShengjiu Wang 	if (sdmac->event_id1 > 31)
9218391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_HWE;
9228391ecf4SShengjiu Wang 
9238391ecf4SShengjiu Wang 	/*
9248391ecf4SShengjiu Wang 	 * If LWML(src_maxburst) > HWML(dst_maxburst), we need
9258391ecf4SShengjiu Wang 	 * swap LWML and HWML of INFO(A.3.2.5.1), also need swap
9268391ecf4SShengjiu Wang 	 * r0(event_mask[1]) and r1(event_mask[0]).
9278391ecf4SShengjiu Wang 	 */
9288391ecf4SShengjiu Wang 	if (lwml > hwml) {
9298391ecf4SShengjiu Wang 		sdmac->watermark_level &= ~(SDMA_WATERMARK_LEVEL_LWML |
9308391ecf4SShengjiu Wang 						SDMA_WATERMARK_LEVEL_HWML);
9318391ecf4SShengjiu Wang 		sdmac->watermark_level |= hwml;
9328391ecf4SShengjiu Wang 		sdmac->watermark_level |= lwml << 16;
9338391ecf4SShengjiu Wang 		swap(sdmac->event_mask[0], sdmac->event_mask[1]);
9348391ecf4SShengjiu Wang 	}
9358391ecf4SShengjiu Wang 
9368391ecf4SShengjiu Wang 	if (sdmac->per_address2 >= sdma->spba_start_addr &&
9378391ecf4SShengjiu Wang 			sdmac->per_address2 <= sdma->spba_end_addr)
9388391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_SP;
9398391ecf4SShengjiu Wang 
9408391ecf4SShengjiu Wang 	if (sdmac->per_address >= sdma->spba_start_addr &&
9418391ecf4SShengjiu Wang 			sdmac->per_address <= sdma->spba_end_addr)
9428391ecf4SShengjiu Wang 		sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_DP;
9438391ecf4SShengjiu Wang 
9448391ecf4SShengjiu Wang 	sdmac->watermark_level |= SDMA_WATERMARK_LEVEL_CONT;
9458391ecf4SShengjiu Wang }
9468391ecf4SShengjiu Wang 
9477b350ab0SMaxime Ripard static int sdma_config_channel(struct dma_chan *chan)
9481ec1e82fSSascha Hauer {
9497b350ab0SMaxime Ripard 	struct sdma_channel *sdmac = to_sdma_chan(chan);
9501ec1e82fSSascha Hauer 	int ret;
9511ec1e82fSSascha Hauer 
9527b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
9531ec1e82fSSascha Hauer 
9540bbc1413SRichard Zhao 	sdmac->event_mask[0] = 0;
9550bbc1413SRichard Zhao 	sdmac->event_mask[1] = 0;
9561ec1e82fSSascha Hauer 	sdmac->shp_addr = 0;
9571ec1e82fSSascha Hauer 	sdmac->per_addr = 0;
9581ec1e82fSSascha Hauer 
9591ec1e82fSSascha Hauer 	if (sdmac->event_id0) {
96017bba72fSSascha Hauer 		if (sdmac->event_id0 >= sdmac->sdma->drvdata->num_events)
9611ec1e82fSSascha Hauer 			return -EINVAL;
9621ec1e82fSSascha Hauer 		sdma_event_enable(sdmac, sdmac->event_id0);
9631ec1e82fSSascha Hauer 	}
9641ec1e82fSSascha Hauer 
9658391ecf4SShengjiu Wang 	if (sdmac->event_id1) {
9668391ecf4SShengjiu Wang 		if (sdmac->event_id1 >= sdmac->sdma->drvdata->num_events)
9678391ecf4SShengjiu Wang 			return -EINVAL;
9688391ecf4SShengjiu Wang 		sdma_event_enable(sdmac, sdmac->event_id1);
9698391ecf4SShengjiu Wang 	}
9708391ecf4SShengjiu Wang 
9711ec1e82fSSascha Hauer 	switch (sdmac->peripheral_type) {
9721ec1e82fSSascha Hauer 	case IMX_DMATYPE_DSP:
9731ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, true);
9741ec1e82fSSascha Hauer 		break;
9751ec1e82fSSascha Hauer 	case IMX_DMATYPE_MEMORY:
9761ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, false, true, false);
9771ec1e82fSSascha Hauer 		break;
9781ec1e82fSSascha Hauer 	default:
9791ec1e82fSSascha Hauer 		sdma_config_ownership(sdmac, true, true, false);
9801ec1e82fSSascha Hauer 		break;
9811ec1e82fSSascha Hauer 	}
9821ec1e82fSSascha Hauer 
9831ec1e82fSSascha Hauer 	sdma_get_pc(sdmac, sdmac->peripheral_type);
9841ec1e82fSSascha Hauer 
9851ec1e82fSSascha Hauer 	if ((sdmac->peripheral_type != IMX_DMATYPE_MEMORY) &&
9861ec1e82fSSascha Hauer 			(sdmac->peripheral_type != IMX_DMATYPE_DSP)) {
9871ec1e82fSSascha Hauer 		/* Handle multiple event channels differently */
9881ec1e82fSSascha Hauer 		if (sdmac->event_id1) {
9898391ecf4SShengjiu Wang 			if (sdmac->peripheral_type == IMX_DMATYPE_ASRC_SP ||
9908391ecf4SShengjiu Wang 			    sdmac->peripheral_type == IMX_DMATYPE_ASRC)
9918391ecf4SShengjiu Wang 				sdma_set_watermarklevel_for_p2p(sdmac);
9928391ecf4SShengjiu Wang 		} else
9930bbc1413SRichard Zhao 			__set_bit(sdmac->event_id0, sdmac->event_mask);
9948391ecf4SShengjiu Wang 
9951ec1e82fSSascha Hauer 		/* Address */
9961ec1e82fSSascha Hauer 		sdmac->shp_addr = sdmac->per_address;
9978391ecf4SShengjiu Wang 		sdmac->per_addr = sdmac->per_address2;
9981ec1e82fSSascha Hauer 	} else {
9991ec1e82fSSascha Hauer 		sdmac->watermark_level = 0; /* FIXME: M3_BASE_ADDRESS */
10001ec1e82fSSascha Hauer 	}
10011ec1e82fSSascha Hauer 
10021ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
10031ec1e82fSSascha Hauer 
10041ec1e82fSSascha Hauer 	return ret;
10051ec1e82fSSascha Hauer }
10061ec1e82fSSascha Hauer 
10071ec1e82fSSascha Hauer static int sdma_set_channel_priority(struct sdma_channel *sdmac,
10081ec1e82fSSascha Hauer 		unsigned int priority)
10091ec1e82fSSascha Hauer {
10101ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10111ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10121ec1e82fSSascha Hauer 
10131ec1e82fSSascha Hauer 	if (priority < MXC_SDMA_MIN_PRIORITY
10141ec1e82fSSascha Hauer 	    || priority > MXC_SDMA_MAX_PRIORITY) {
10151ec1e82fSSascha Hauer 		return -EINVAL;
10161ec1e82fSSascha Hauer 	}
10171ec1e82fSSascha Hauer 
1018c4b56857SRichard Zhao 	writel_relaxed(priority, sdma->regs + SDMA_CHNPRI_0 + 4 * channel);
10191ec1e82fSSascha Hauer 
10201ec1e82fSSascha Hauer 	return 0;
10211ec1e82fSSascha Hauer }
10221ec1e82fSSascha Hauer 
10231ec1e82fSSascha Hauer static int sdma_request_channel(struct sdma_channel *sdmac)
10241ec1e82fSSascha Hauer {
10251ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
10261ec1e82fSSascha Hauer 	int channel = sdmac->channel;
10271ec1e82fSSascha Hauer 	int ret = -EBUSY;
10281ec1e82fSSascha Hauer 
10299f92d223SJoe Perches 	sdmac->bd = dma_zalloc_coherent(NULL, PAGE_SIZE, &sdmac->bd_phys,
10309f92d223SJoe Perches 					GFP_KERNEL);
10311ec1e82fSSascha Hauer 	if (!sdmac->bd) {
10321ec1e82fSSascha Hauer 		ret = -ENOMEM;
10331ec1e82fSSascha Hauer 		goto out;
10341ec1e82fSSascha Hauer 	}
10351ec1e82fSSascha Hauer 
10361ec1e82fSSascha Hauer 	sdma->channel_control[channel].base_bd_ptr = sdmac->bd_phys;
10371ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
10381ec1e82fSSascha Hauer 
10391ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, MXC_SDMA_DEFAULT_PRIORITY);
10401ec1e82fSSascha Hauer 	return 0;
10411ec1e82fSSascha Hauer out:
10421ec1e82fSSascha Hauer 
10431ec1e82fSSascha Hauer 	return ret;
10441ec1e82fSSascha Hauer }
10451ec1e82fSSascha Hauer 
10461ec1e82fSSascha Hauer static dma_cookie_t sdma_tx_submit(struct dma_async_tx_descriptor *tx)
10471ec1e82fSSascha Hauer {
1048f69f2e26SHaitao Zhang 	unsigned long flags;
10491ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(tx->chan);
10501ec1e82fSSascha Hauer 	dma_cookie_t cookie;
10511ec1e82fSSascha Hauer 
1052f69f2e26SHaitao Zhang 	spin_lock_irqsave(&sdmac->lock, flags);
10531ec1e82fSSascha Hauer 
1054884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
10551ec1e82fSSascha Hauer 
1056f69f2e26SHaitao Zhang 	spin_unlock_irqrestore(&sdmac->lock, flags);
10571ec1e82fSSascha Hauer 
10581ec1e82fSSascha Hauer 	return cookie;
10591ec1e82fSSascha Hauer }
10601ec1e82fSSascha Hauer 
10611ec1e82fSSascha Hauer static int sdma_alloc_chan_resources(struct dma_chan *chan)
10621ec1e82fSSascha Hauer {
10631ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
10641ec1e82fSSascha Hauer 	struct imx_dma_data *data = chan->private;
10651ec1e82fSSascha Hauer 	int prio, ret;
10661ec1e82fSSascha Hauer 
10671ec1e82fSSascha Hauer 	if (!data)
10681ec1e82fSSascha Hauer 		return -EINVAL;
10691ec1e82fSSascha Hauer 
10701ec1e82fSSascha Hauer 	switch (data->priority) {
10711ec1e82fSSascha Hauer 	case DMA_PRIO_HIGH:
10721ec1e82fSSascha Hauer 		prio = 3;
10731ec1e82fSSascha Hauer 		break;
10741ec1e82fSSascha Hauer 	case DMA_PRIO_MEDIUM:
10751ec1e82fSSascha Hauer 		prio = 2;
10761ec1e82fSSascha Hauer 		break;
10771ec1e82fSSascha Hauer 	case DMA_PRIO_LOW:
10781ec1e82fSSascha Hauer 	default:
10791ec1e82fSSascha Hauer 		prio = 1;
10801ec1e82fSSascha Hauer 		break;
10811ec1e82fSSascha Hauer 	}
10821ec1e82fSSascha Hauer 
10831ec1e82fSSascha Hauer 	sdmac->peripheral_type = data->peripheral_type;
10841ec1e82fSSascha Hauer 	sdmac->event_id0 = data->dma_request;
10858391ecf4SShengjiu Wang 	sdmac->event_id1 = data->dma_request2;
1086c2c744d3SRichard Zhao 
1087b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ipg);
1088b93edcddSFabio Estevam 	if (ret)
1089b93edcddSFabio Estevam 		return ret;
1090b93edcddSFabio Estevam 	ret = clk_enable(sdmac->sdma->clk_ahb);
1091b93edcddSFabio Estevam 	if (ret)
1092b93edcddSFabio Estevam 		goto disable_clk_ipg;
1093c2c744d3SRichard Zhao 
10943bb5e7caSRichard Zhao 	ret = sdma_request_channel(sdmac);
10951ec1e82fSSascha Hauer 	if (ret)
1096b93edcddSFabio Estevam 		goto disable_clk_ahb;
10971ec1e82fSSascha Hauer 
10983bb5e7caSRichard Zhao 	ret = sdma_set_channel_priority(sdmac, prio);
10991ec1e82fSSascha Hauer 	if (ret)
1100b93edcddSFabio Estevam 		goto disable_clk_ahb;
11011ec1e82fSSascha Hauer 
11021ec1e82fSSascha Hauer 	dma_async_tx_descriptor_init(&sdmac->desc, chan);
11031ec1e82fSSascha Hauer 	sdmac->desc.tx_submit = sdma_tx_submit;
11041ec1e82fSSascha Hauer 	/* txd.flags will be overwritten in prep funcs */
11051ec1e82fSSascha Hauer 	sdmac->desc.flags = DMA_CTRL_ACK;
11061ec1e82fSSascha Hauer 
11071ec1e82fSSascha Hauer 	return 0;
1108b93edcddSFabio Estevam 
1109b93edcddSFabio Estevam disable_clk_ahb:
1110b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ahb);
1111b93edcddSFabio Estevam disable_clk_ipg:
1112b93edcddSFabio Estevam 	clk_disable(sdmac->sdma->clk_ipg);
1113b93edcddSFabio Estevam 	return ret;
11141ec1e82fSSascha Hauer }
11151ec1e82fSSascha Hauer 
11161ec1e82fSSascha Hauer static void sdma_free_chan_resources(struct dma_chan *chan)
11171ec1e82fSSascha Hauer {
11181ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11191ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11201ec1e82fSSascha Hauer 
11217b350ab0SMaxime Ripard 	sdma_disable_channel(chan);
11221ec1e82fSSascha Hauer 
11231ec1e82fSSascha Hauer 	if (sdmac->event_id0)
11241ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id0);
11251ec1e82fSSascha Hauer 	if (sdmac->event_id1)
11261ec1e82fSSascha Hauer 		sdma_event_disable(sdmac, sdmac->event_id1);
11271ec1e82fSSascha Hauer 
11281ec1e82fSSascha Hauer 	sdmac->event_id0 = 0;
11291ec1e82fSSascha Hauer 	sdmac->event_id1 = 0;
11301ec1e82fSSascha Hauer 
11311ec1e82fSSascha Hauer 	sdma_set_channel_priority(sdmac, 0);
11321ec1e82fSSascha Hauer 
11331ec1e82fSSascha Hauer 	dma_free_coherent(NULL, PAGE_SIZE, sdmac->bd, sdmac->bd_phys);
11341ec1e82fSSascha Hauer 
11357560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
11367560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
11371ec1e82fSSascha Hauer }
11381ec1e82fSSascha Hauer 
11391ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_slave_sg(
11401ec1e82fSSascha Hauer 		struct dma_chan *chan, struct scatterlist *sgl,
1141db8196dfSVinod Koul 		unsigned int sg_len, enum dma_transfer_direction direction,
1142185ecb5fSAlexandre Bounine 		unsigned long flags, void *context)
11431ec1e82fSSascha Hauer {
11441ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
11451ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
11461ec1e82fSSascha Hauer 	int ret, i, count;
114723889c63SSascha Hauer 	int channel = sdmac->channel;
11481ec1e82fSSascha Hauer 	struct scatterlist *sg;
11491ec1e82fSSascha Hauer 
11501ec1e82fSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
11511ec1e82fSSascha Hauer 		return NULL;
11521ec1e82fSSascha Hauer 	sdmac->status = DMA_IN_PROGRESS;
11531ec1e82fSSascha Hauer 
11541ec1e82fSSascha Hauer 	sdmac->flags = 0;
11551ec1e82fSSascha Hauer 
11568e2e27c7SRichard Zhao 	sdmac->buf_tail = 0;
11578e2e27c7SRichard Zhao 
11581ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "setting up %d entries for channel %d.\n",
11591ec1e82fSSascha Hauer 			sg_len, channel);
11601ec1e82fSSascha Hauer 
11611ec1e82fSSascha Hauer 	sdmac->direction = direction;
11621ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
11631ec1e82fSSascha Hauer 	if (ret)
11641ec1e82fSSascha Hauer 		goto err_out;
11651ec1e82fSSascha Hauer 
11661ec1e82fSSascha Hauer 	if (sg_len > NUM_BD) {
11671ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
11681ec1e82fSSascha Hauer 				channel, sg_len, NUM_BD);
11691ec1e82fSSascha Hauer 		ret = -EINVAL;
11701ec1e82fSSascha Hauer 		goto err_out;
11711ec1e82fSSascha Hauer 	}
11721ec1e82fSSascha Hauer 
1173ab59a510SHuang Shijie 	sdmac->chn_count = 0;
11741ec1e82fSSascha Hauer 	for_each_sg(sgl, sg, sg_len, i) {
11751ec1e82fSSascha Hauer 		struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
11761ec1e82fSSascha Hauer 		int param;
11771ec1e82fSSascha Hauer 
1178d2f5c276SAnatolij Gustschin 		bd->buffer_addr = sg->dma_address;
11791ec1e82fSSascha Hauer 
1180fdaf9c4bSLars-Peter Clausen 		count = sg_dma_len(sg);
11811ec1e82fSSascha Hauer 
11821ec1e82fSSascha Hauer 		if (count > 0xffff) {
11831ec1e82fSSascha Hauer 			dev_err(sdma->dev, "SDMA channel %d: maximum bytes for sg entry exceeded: %d > %d\n",
11841ec1e82fSSascha Hauer 					channel, count, 0xffff);
11851ec1e82fSSascha Hauer 			ret = -EINVAL;
11861ec1e82fSSascha Hauer 			goto err_out;
11871ec1e82fSSascha Hauer 		}
11881ec1e82fSSascha Hauer 
11891ec1e82fSSascha Hauer 		bd->mode.count = count;
1190ab59a510SHuang Shijie 		sdmac->chn_count += count;
11911ec1e82fSSascha Hauer 
11921ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES) {
11931ec1e82fSSascha Hauer 			ret =  -EINVAL;
11941ec1e82fSSascha Hauer 			goto err_out;
11951ec1e82fSSascha Hauer 		}
11961fa81c27SSascha Hauer 
11971fa81c27SSascha Hauer 		switch (sdmac->word_size) {
11981fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
11991ec1e82fSSascha Hauer 			bd->mode.command = 0;
12001fa81c27SSascha Hauer 			if (count & 3 || sg->dma_address & 3)
12011fa81c27SSascha Hauer 				return NULL;
12021fa81c27SSascha Hauer 			break;
12031fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
12041fa81c27SSascha Hauer 			bd->mode.command = 2;
12051fa81c27SSascha Hauer 			if (count & 1 || sg->dma_address & 1)
12061fa81c27SSascha Hauer 				return NULL;
12071fa81c27SSascha Hauer 			break;
12081fa81c27SSascha Hauer 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
12091fa81c27SSascha Hauer 			bd->mode.command = 1;
12101fa81c27SSascha Hauer 			break;
12111fa81c27SSascha Hauer 		default:
12121fa81c27SSascha Hauer 			return NULL;
12131fa81c27SSascha Hauer 		}
12141ec1e82fSSascha Hauer 
12151ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT;
12161ec1e82fSSascha Hauer 
1217341b9419SShawn Guo 		if (i + 1 == sg_len) {
12181ec1e82fSSascha Hauer 			param |= BD_INTR;
1219341b9419SShawn Guo 			param |= BD_LAST;
1220341b9419SShawn Guo 			param &= ~BD_CONT;
12211ec1e82fSSascha Hauer 		}
12221ec1e82fSSascha Hauer 
1223c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1224c3cc74b2SOlof Johansson 				i, count, (u64)sg->dma_address,
12251ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
12261ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
12271ec1e82fSSascha Hauer 
12281ec1e82fSSascha Hauer 		bd->mode.status = param;
12291ec1e82fSSascha Hauer 	}
12301ec1e82fSSascha Hauer 
12311ec1e82fSSascha Hauer 	sdmac->num_bd = sg_len;
12321ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
12331ec1e82fSSascha Hauer 
12341ec1e82fSSascha Hauer 	return &sdmac->desc;
12351ec1e82fSSascha Hauer err_out:
12364b2ce9ddSShawn Guo 	sdmac->status = DMA_ERROR;
12371ec1e82fSSascha Hauer 	return NULL;
12381ec1e82fSSascha Hauer }
12391ec1e82fSSascha Hauer 
12401ec1e82fSSascha Hauer static struct dma_async_tx_descriptor *sdma_prep_dma_cyclic(
12411ec1e82fSSascha Hauer 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
1242185ecb5fSAlexandre Bounine 		size_t period_len, enum dma_transfer_direction direction,
124331c1e5a1SLaurent Pinchart 		unsigned long flags)
12441ec1e82fSSascha Hauer {
12451ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
12461ec1e82fSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
12471ec1e82fSSascha Hauer 	int num_periods = buf_len / period_len;
124823889c63SSascha Hauer 	int channel = sdmac->channel;
12491ec1e82fSSascha Hauer 	int ret, i = 0, buf = 0;
12501ec1e82fSSascha Hauer 
12511ec1e82fSSascha Hauer 	dev_dbg(sdma->dev, "%s channel: %d\n", __func__, channel);
12521ec1e82fSSascha Hauer 
12531ec1e82fSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
12541ec1e82fSSascha Hauer 		return NULL;
12551ec1e82fSSascha Hauer 
12561ec1e82fSSascha Hauer 	sdmac->status = DMA_IN_PROGRESS;
12571ec1e82fSSascha Hauer 
12588e2e27c7SRichard Zhao 	sdmac->buf_tail = 0;
1259d1a792f3SRussell King - ARM Linux 	sdmac->period_len = period_len;
12608e2e27c7SRichard Zhao 
12611ec1e82fSSascha Hauer 	sdmac->flags |= IMX_DMA_SG_LOOP;
12621ec1e82fSSascha Hauer 	sdmac->direction = direction;
12631ec1e82fSSascha Hauer 	ret = sdma_load_context(sdmac);
12641ec1e82fSSascha Hauer 	if (ret)
12651ec1e82fSSascha Hauer 		goto err_out;
12661ec1e82fSSascha Hauer 
12671ec1e82fSSascha Hauer 	if (num_periods > NUM_BD) {
12681ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum number of sg exceeded: %d > %d\n",
12691ec1e82fSSascha Hauer 				channel, num_periods, NUM_BD);
12701ec1e82fSSascha Hauer 		goto err_out;
12711ec1e82fSSascha Hauer 	}
12721ec1e82fSSascha Hauer 
12731ec1e82fSSascha Hauer 	if (period_len > 0xffff) {
12741ec1e82fSSascha Hauer 		dev_err(sdma->dev, "SDMA channel %d: maximum period size exceeded: %d > %d\n",
12751ec1e82fSSascha Hauer 				channel, period_len, 0xffff);
12761ec1e82fSSascha Hauer 		goto err_out;
12771ec1e82fSSascha Hauer 	}
12781ec1e82fSSascha Hauer 
12791ec1e82fSSascha Hauer 	while (buf < buf_len) {
12801ec1e82fSSascha Hauer 		struct sdma_buffer_descriptor *bd = &sdmac->bd[i];
12811ec1e82fSSascha Hauer 		int param;
12821ec1e82fSSascha Hauer 
12831ec1e82fSSascha Hauer 		bd->buffer_addr = dma_addr;
12841ec1e82fSSascha Hauer 
12851ec1e82fSSascha Hauer 		bd->mode.count = period_len;
12861ec1e82fSSascha Hauer 
12871ec1e82fSSascha Hauer 		if (sdmac->word_size > DMA_SLAVE_BUSWIDTH_4_BYTES)
12881ec1e82fSSascha Hauer 			goto err_out;
12891ec1e82fSSascha Hauer 		if (sdmac->word_size == DMA_SLAVE_BUSWIDTH_4_BYTES)
12901ec1e82fSSascha Hauer 			bd->mode.command = 0;
12911ec1e82fSSascha Hauer 		else
12921ec1e82fSSascha Hauer 			bd->mode.command = sdmac->word_size;
12931ec1e82fSSascha Hauer 
12941ec1e82fSSascha Hauer 		param = BD_DONE | BD_EXTD | BD_CONT | BD_INTR;
12951ec1e82fSSascha Hauer 		if (i + 1 == num_periods)
12961ec1e82fSSascha Hauer 			param |= BD_WRAP;
12971ec1e82fSSascha Hauer 
1298c3cc74b2SOlof Johansson 		dev_dbg(sdma->dev, "entry %d: count: %d dma: %#llx %s%s\n",
1299c3cc74b2SOlof Johansson 				i, period_len, (u64)dma_addr,
13001ec1e82fSSascha Hauer 				param & BD_WRAP ? "wrap" : "",
13011ec1e82fSSascha Hauer 				param & BD_INTR ? " intr" : "");
13021ec1e82fSSascha Hauer 
13031ec1e82fSSascha Hauer 		bd->mode.status = param;
13041ec1e82fSSascha Hauer 
13051ec1e82fSSascha Hauer 		dma_addr += period_len;
13061ec1e82fSSascha Hauer 		buf += period_len;
13071ec1e82fSSascha Hauer 
13081ec1e82fSSascha Hauer 		i++;
13091ec1e82fSSascha Hauer 	}
13101ec1e82fSSascha Hauer 
13111ec1e82fSSascha Hauer 	sdmac->num_bd = num_periods;
13121ec1e82fSSascha Hauer 	sdma->channel_control[channel].current_bd_ptr = sdmac->bd_phys;
13131ec1e82fSSascha Hauer 
13141ec1e82fSSascha Hauer 	return &sdmac->desc;
13151ec1e82fSSascha Hauer err_out:
13161ec1e82fSSascha Hauer 	sdmac->status = DMA_ERROR;
13171ec1e82fSSascha Hauer 	return NULL;
13181ec1e82fSSascha Hauer }
13191ec1e82fSSascha Hauer 
13207b350ab0SMaxime Ripard static int sdma_config(struct dma_chan *chan,
13217b350ab0SMaxime Ripard 		       struct dma_slave_config *dmaengine_cfg)
13221ec1e82fSSascha Hauer {
13231ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13241ec1e82fSSascha Hauer 
1325db8196dfSVinod Koul 	if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
13261ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->src_addr;
132794ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->src_maxburst *
132894ac27a5SPhilippe Rétornaz 			dmaengine_cfg->src_addr_width;
13291ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->src_addr_width;
13308391ecf4SShengjiu Wang 	} else if (dmaengine_cfg->direction == DMA_DEV_TO_DEV) {
13318391ecf4SShengjiu Wang 		sdmac->per_address2 = dmaengine_cfg->src_addr;
13328391ecf4SShengjiu Wang 		sdmac->per_address = dmaengine_cfg->dst_addr;
13338391ecf4SShengjiu Wang 		sdmac->watermark_level = dmaengine_cfg->src_maxburst &
13348391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_LWML;
13358391ecf4SShengjiu Wang 		sdmac->watermark_level |= (dmaengine_cfg->dst_maxburst << 16) &
13368391ecf4SShengjiu Wang 			SDMA_WATERMARK_LEVEL_HWML;
13378391ecf4SShengjiu Wang 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
13381ec1e82fSSascha Hauer 	} else {
13391ec1e82fSSascha Hauer 		sdmac->per_address = dmaengine_cfg->dst_addr;
134094ac27a5SPhilippe Rétornaz 		sdmac->watermark_level = dmaengine_cfg->dst_maxburst *
134194ac27a5SPhilippe Rétornaz 			dmaengine_cfg->dst_addr_width;
13421ec1e82fSSascha Hauer 		sdmac->word_size = dmaengine_cfg->dst_addr_width;
13431ec1e82fSSascha Hauer 	}
1344e6966433SHuang Shijie 	sdmac->direction = dmaengine_cfg->direction;
13457b350ab0SMaxime Ripard 	return sdma_config_channel(chan);
13461ec1e82fSSascha Hauer }
13471ec1e82fSSascha Hauer 
13481ec1e82fSSascha Hauer static enum dma_status sdma_tx_status(struct dma_chan *chan,
13491ec1e82fSSascha Hauer 				      dma_cookie_t cookie,
13501ec1e82fSSascha Hauer 				      struct dma_tx_state *txstate)
13511ec1e82fSSascha Hauer {
13521ec1e82fSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
1353d1a792f3SRussell King - ARM Linux 	u32 residue;
1354d1a792f3SRussell King - ARM Linux 
1355d1a792f3SRussell King - ARM Linux 	if (sdmac->flags & IMX_DMA_SG_LOOP)
1356d1a792f3SRussell King - ARM Linux 		residue = (sdmac->num_bd - sdmac->buf_tail) * sdmac->period_len;
1357d1a792f3SRussell King - ARM Linux 	else
1358d1a792f3SRussell King - ARM Linux 		residue = sdmac->chn_count - sdmac->chn_real_count;
13591ec1e82fSSascha Hauer 
1360e8e3a790SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
1361d1a792f3SRussell King - ARM Linux 			 residue);
13621ec1e82fSSascha Hauer 
13638a965911SShawn Guo 	return sdmac->status;
13641ec1e82fSSascha Hauer }
13651ec1e82fSSascha Hauer 
13661ec1e82fSSascha Hauer static void sdma_issue_pending(struct dma_chan *chan)
13671ec1e82fSSascha Hauer {
13682b4f130eSSascha Hauer 	struct sdma_channel *sdmac = to_sdma_chan(chan);
13692b4f130eSSascha Hauer 	struct sdma_engine *sdma = sdmac->sdma;
13702b4f130eSSascha Hauer 
13712b4f130eSSascha Hauer 	if (sdmac->status == DMA_IN_PROGRESS)
13722b4f130eSSascha Hauer 		sdma_enable_channel(sdma, sdmac->channel);
13731ec1e82fSSascha Hauer }
13741ec1e82fSSascha Hauer 
13755b28aa31SSascha Hauer #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1	34
1376cd72b846SNicolin Chen #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2	38
1377a572460bSFabio Estevam #define SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3	41
13785b28aa31SSascha Hauer 
13795b28aa31SSascha Hauer static void sdma_add_scripts(struct sdma_engine *sdma,
13805b28aa31SSascha Hauer 		const struct sdma_script_start_addrs *addr)
13815b28aa31SSascha Hauer {
13825b28aa31SSascha Hauer 	s32 *addr_arr = (u32 *)addr;
13835b28aa31SSascha Hauer 	s32 *saddr_arr = (u32 *)sdma->script_addrs;
13845b28aa31SSascha Hauer 	int i;
13855b28aa31SSascha Hauer 
138670dabaedSNicolin Chen 	/* use the default firmware in ROM if missing external firmware */
138770dabaedSNicolin Chen 	if (!sdma->script_number)
138870dabaedSNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
138970dabaedSNicolin Chen 
1390cd72b846SNicolin Chen 	for (i = 0; i < sdma->script_number; i++)
13915b28aa31SSascha Hauer 		if (addr_arr[i] > 0)
13925b28aa31SSascha Hauer 			saddr_arr[i] = addr_arr[i];
13935b28aa31SSascha Hauer }
13945b28aa31SSascha Hauer 
13957b4b88e0SSascha Hauer static void sdma_load_firmware(const struct firmware *fw, void *context)
13965b28aa31SSascha Hauer {
13977b4b88e0SSascha Hauer 	struct sdma_engine *sdma = context;
13985b28aa31SSascha Hauer 	const struct sdma_firmware_header *header;
13995b28aa31SSascha Hauer 	const struct sdma_script_start_addrs *addr;
14005b28aa31SSascha Hauer 	unsigned short *ram_code;
14015b28aa31SSascha Hauer 
14027b4b88e0SSascha Hauer 	if (!fw) {
14030f927a11SSascha Hauer 		dev_info(sdma->dev, "external firmware not found, using ROM firmware\n");
14040f927a11SSascha Hauer 		/* In this case we just use the ROM firmware. */
14057b4b88e0SSascha Hauer 		return;
14067b4b88e0SSascha Hauer 	}
14075b28aa31SSascha Hauer 
14085b28aa31SSascha Hauer 	if (fw->size < sizeof(*header))
14095b28aa31SSascha Hauer 		goto err_firmware;
14105b28aa31SSascha Hauer 
14115b28aa31SSascha Hauer 	header = (struct sdma_firmware_header *)fw->data;
14125b28aa31SSascha Hauer 
14135b28aa31SSascha Hauer 	if (header->magic != SDMA_FIRMWARE_MAGIC)
14145b28aa31SSascha Hauer 		goto err_firmware;
14155b28aa31SSascha Hauer 	if (header->ram_code_start + header->ram_code_size > fw->size)
14165b28aa31SSascha Hauer 		goto err_firmware;
1417cd72b846SNicolin Chen 	switch (header->version_major) {
1418cd72b846SNicolin Chen 	case 1:
1419cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1;
1420cd72b846SNicolin Chen 		break;
1421cd72b846SNicolin Chen 	case 2:
1422cd72b846SNicolin Chen 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V2;
1423cd72b846SNicolin Chen 		break;
1424a572460bSFabio Estevam 	case 3:
1425a572460bSFabio Estevam 		sdma->script_number = SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V3;
1426a572460bSFabio Estevam 		break;
1427cd72b846SNicolin Chen 	default:
1428cd72b846SNicolin Chen 		dev_err(sdma->dev, "unknown firmware version\n");
1429cd72b846SNicolin Chen 		goto err_firmware;
1430cd72b846SNicolin Chen 	}
14315b28aa31SSascha Hauer 
14325b28aa31SSascha Hauer 	addr = (void *)header + header->script_addrs_start;
14335b28aa31SSascha Hauer 	ram_code = (void *)header + header->ram_code_start;
14345b28aa31SSascha Hauer 
14357560e3f3SSascha Hauer 	clk_enable(sdma->clk_ipg);
14367560e3f3SSascha Hauer 	clk_enable(sdma->clk_ahb);
14375b28aa31SSascha Hauer 	/* download the RAM image for SDMA */
14385b28aa31SSascha Hauer 	sdma_load_script(sdma, ram_code,
14395b28aa31SSascha Hauer 			header->ram_code_size,
14406866fd3bSSascha Hauer 			addr->ram_code_start_addr);
14417560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
14427560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
14435b28aa31SSascha Hauer 
14445b28aa31SSascha Hauer 	sdma_add_scripts(sdma, addr);
14455b28aa31SSascha Hauer 
14465b28aa31SSascha Hauer 	dev_info(sdma->dev, "loaded firmware %d.%d\n",
14475b28aa31SSascha Hauer 			header->version_major,
14485b28aa31SSascha Hauer 			header->version_minor);
14495b28aa31SSascha Hauer 
14505b28aa31SSascha Hauer err_firmware:
14515b28aa31SSascha Hauer 	release_firmware(fw);
14527b4b88e0SSascha Hauer }
14537b4b88e0SSascha Hauer 
1454d078cd1bSZidan Wang #define EVENT_REMAP_CELLS 3
1455d078cd1bSZidan Wang 
145629f493daSJason Liu static int sdma_event_remap(struct sdma_engine *sdma)
1457d078cd1bSZidan Wang {
1458d078cd1bSZidan Wang 	struct device_node *np = sdma->dev->of_node;
1459d078cd1bSZidan Wang 	struct device_node *gpr_np = of_parse_phandle(np, "gpr", 0);
1460d078cd1bSZidan Wang 	struct property *event_remap;
1461d078cd1bSZidan Wang 	struct regmap *gpr;
1462d078cd1bSZidan Wang 	char propname[] = "fsl,sdma-event-remap";
1463d078cd1bSZidan Wang 	u32 reg, val, shift, num_map, i;
1464d078cd1bSZidan Wang 	int ret = 0;
1465d078cd1bSZidan Wang 
1466d078cd1bSZidan Wang 	if (IS_ERR(np) || IS_ERR(gpr_np))
1467d078cd1bSZidan Wang 		goto out;
1468d078cd1bSZidan Wang 
1469d078cd1bSZidan Wang 	event_remap = of_find_property(np, propname, NULL);
1470d078cd1bSZidan Wang 	num_map = event_remap ? (event_remap->length / sizeof(u32)) : 0;
1471d078cd1bSZidan Wang 	if (!num_map) {
1472ce078af7SFabio Estevam 		dev_dbg(sdma->dev, "no event needs to be remapped\n");
1473d078cd1bSZidan Wang 		goto out;
1474d078cd1bSZidan Wang 	} else if (num_map % EVENT_REMAP_CELLS) {
1475d078cd1bSZidan Wang 		dev_err(sdma->dev, "the property %s must modulo %d\n",
1476d078cd1bSZidan Wang 				propname, EVENT_REMAP_CELLS);
1477d078cd1bSZidan Wang 		ret = -EINVAL;
1478d078cd1bSZidan Wang 		goto out;
1479d078cd1bSZidan Wang 	}
1480d078cd1bSZidan Wang 
1481d078cd1bSZidan Wang 	gpr = syscon_node_to_regmap(gpr_np);
1482d078cd1bSZidan Wang 	if (IS_ERR(gpr)) {
1483d078cd1bSZidan Wang 		dev_err(sdma->dev, "failed to get gpr regmap\n");
1484d078cd1bSZidan Wang 		ret = PTR_ERR(gpr);
1485d078cd1bSZidan Wang 		goto out;
1486d078cd1bSZidan Wang 	}
1487d078cd1bSZidan Wang 
1488d078cd1bSZidan Wang 	for (i = 0; i < num_map; i += EVENT_REMAP_CELLS) {
1489d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i, &reg);
1490d078cd1bSZidan Wang 		if (ret) {
1491d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1492d078cd1bSZidan Wang 					propname, i);
1493d078cd1bSZidan Wang 			goto out;
1494d078cd1bSZidan Wang 		}
1495d078cd1bSZidan Wang 
1496d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 1, &shift);
1497d078cd1bSZidan Wang 		if (ret) {
1498d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1499d078cd1bSZidan Wang 					propname, i + 1);
1500d078cd1bSZidan Wang 			goto out;
1501d078cd1bSZidan Wang 		}
1502d078cd1bSZidan Wang 
1503d078cd1bSZidan Wang 		ret = of_property_read_u32_index(np, propname, i + 2, &val);
1504d078cd1bSZidan Wang 		if (ret) {
1505d078cd1bSZidan Wang 			dev_err(sdma->dev, "failed to read property %s index %d\n",
1506d078cd1bSZidan Wang 					propname, i + 2);
1507d078cd1bSZidan Wang 			goto out;
1508d078cd1bSZidan Wang 		}
1509d078cd1bSZidan Wang 
1510d078cd1bSZidan Wang 		regmap_update_bits(gpr, reg, BIT(shift), val << shift);
1511d078cd1bSZidan Wang 	}
1512d078cd1bSZidan Wang 
1513d078cd1bSZidan Wang out:
1514d078cd1bSZidan Wang 	if (!IS_ERR(gpr_np))
1515d078cd1bSZidan Wang 		of_node_put(gpr_np);
1516d078cd1bSZidan Wang 
1517d078cd1bSZidan Wang 	return ret;
1518d078cd1bSZidan Wang }
1519d078cd1bSZidan Wang 
1520fe6cf289SArnd Bergmann static int sdma_get_firmware(struct sdma_engine *sdma,
15217b4b88e0SSascha Hauer 		const char *fw_name)
15227b4b88e0SSascha Hauer {
15237b4b88e0SSascha Hauer 	int ret;
15247b4b88e0SSascha Hauer 
15257b4b88e0SSascha Hauer 	ret = request_firmware_nowait(THIS_MODULE,
15267b4b88e0SSascha Hauer 			FW_ACTION_HOTPLUG, fw_name, sdma->dev,
15277b4b88e0SSascha Hauer 			GFP_KERNEL, sdma, sdma_load_firmware);
15285b28aa31SSascha Hauer 
15295b28aa31SSascha Hauer 	return ret;
15305b28aa31SSascha Hauer }
15315b28aa31SSascha Hauer 
153219bfc772SJingoo Han static int sdma_init(struct sdma_engine *sdma)
15331ec1e82fSSascha Hauer {
15341ec1e82fSSascha Hauer 	int i, ret;
15351ec1e82fSSascha Hauer 	dma_addr_t ccb_phys;
15361ec1e82fSSascha Hauer 
1537b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ipg);
1538b93edcddSFabio Estevam 	if (ret)
1539b93edcddSFabio Estevam 		return ret;
1540b93edcddSFabio Estevam 	ret = clk_enable(sdma->clk_ahb);
1541b93edcddSFabio Estevam 	if (ret)
1542b93edcddSFabio Estevam 		goto disable_clk_ipg;
15431ec1e82fSSascha Hauer 
15441ec1e82fSSascha Hauer 	/* Be sure SDMA has not started yet */
1545c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_C0PTR);
15461ec1e82fSSascha Hauer 
15471ec1e82fSSascha Hauer 	sdma->channel_control = dma_alloc_coherent(NULL,
15481ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control) +
15491ec1e82fSSascha Hauer 			sizeof(struct sdma_context_data),
15501ec1e82fSSascha Hauer 			&ccb_phys, GFP_KERNEL);
15511ec1e82fSSascha Hauer 
15521ec1e82fSSascha Hauer 	if (!sdma->channel_control) {
15531ec1e82fSSascha Hauer 		ret = -ENOMEM;
15541ec1e82fSSascha Hauer 		goto err_dma_alloc;
15551ec1e82fSSascha Hauer 	}
15561ec1e82fSSascha Hauer 
15571ec1e82fSSascha Hauer 	sdma->context = (void *)sdma->channel_control +
15581ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
15591ec1e82fSSascha Hauer 	sdma->context_phys = ccb_phys +
15601ec1e82fSSascha Hauer 		MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control);
15611ec1e82fSSascha Hauer 
15621ec1e82fSSascha Hauer 	/* Zero-out the CCB structures array just allocated */
15631ec1e82fSSascha Hauer 	memset(sdma->channel_control, 0,
15641ec1e82fSSascha Hauer 			MAX_DMA_CHANNELS * sizeof (struct sdma_channel_control));
15651ec1e82fSSascha Hauer 
15661ec1e82fSSascha Hauer 	/* disable all channels */
156717bba72fSSascha Hauer 	for (i = 0; i < sdma->drvdata->num_events; i++)
1568c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + chnenbl_ofs(sdma, i));
15691ec1e82fSSascha Hauer 
15701ec1e82fSSascha Hauer 	/* All channels have priority 0 */
15711ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++)
1572c4b56857SRichard Zhao 		writel_relaxed(0, sdma->regs + SDMA_CHNPRI_0 + i * 4);
15731ec1e82fSSascha Hauer 
15741ec1e82fSSascha Hauer 	ret = sdma_request_channel(&sdma->channel[0]);
15751ec1e82fSSascha Hauer 	if (ret)
15761ec1e82fSSascha Hauer 		goto err_dma_alloc;
15771ec1e82fSSascha Hauer 
15781ec1e82fSSascha Hauer 	sdma_config_ownership(&sdma->channel[0], false, true, false);
15791ec1e82fSSascha Hauer 
15801ec1e82fSSascha Hauer 	/* Set Command Channel (Channel Zero) */
1581c4b56857SRichard Zhao 	writel_relaxed(0x4050, sdma->regs + SDMA_CHN0ADDR);
15821ec1e82fSSascha Hauer 
15831ec1e82fSSascha Hauer 	/* Set bits of CONFIG register but with static context switching */
15841ec1e82fSSascha Hauer 	/* FIXME: Check whether to set ACR bit depending on clock ratios */
1585c4b56857SRichard Zhao 	writel_relaxed(0, sdma->regs + SDMA_H_CONFIG);
15861ec1e82fSSascha Hauer 
1587c4b56857SRichard Zhao 	writel_relaxed(ccb_phys, sdma->regs + SDMA_H_C0PTR);
15881ec1e82fSSascha Hauer 
15891ec1e82fSSascha Hauer 	/* Initializes channel's priorities */
15901ec1e82fSSascha Hauer 	sdma_set_channel_priority(&sdma->channel[0], 7);
15911ec1e82fSSascha Hauer 
15927560e3f3SSascha Hauer 	clk_disable(sdma->clk_ipg);
15937560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
15941ec1e82fSSascha Hauer 
15951ec1e82fSSascha Hauer 	return 0;
15961ec1e82fSSascha Hauer 
15971ec1e82fSSascha Hauer err_dma_alloc:
15987560e3f3SSascha Hauer 	clk_disable(sdma->clk_ahb);
1599b93edcddSFabio Estevam disable_clk_ipg:
1600b93edcddSFabio Estevam 	clk_disable(sdma->clk_ipg);
16011ec1e82fSSascha Hauer 	dev_err(sdma->dev, "initialisation failed with %d\n", ret);
16021ec1e82fSSascha Hauer 	return ret;
16031ec1e82fSSascha Hauer }
16041ec1e82fSSascha Hauer 
16059479e17cSShawn Guo static bool sdma_filter_fn(struct dma_chan *chan, void *fn_param)
16069479e17cSShawn Guo {
16070b351865SNicolin Chen 	struct sdma_channel *sdmac = to_sdma_chan(chan);
16089479e17cSShawn Guo 	struct imx_dma_data *data = fn_param;
16099479e17cSShawn Guo 
16109479e17cSShawn Guo 	if (!imx_dma_is_general_purpose(chan))
16119479e17cSShawn Guo 		return false;
16129479e17cSShawn Guo 
16130b351865SNicolin Chen 	sdmac->data = *data;
16140b351865SNicolin Chen 	chan->private = &sdmac->data;
16159479e17cSShawn Guo 
16169479e17cSShawn Guo 	return true;
16179479e17cSShawn Guo }
16189479e17cSShawn Guo 
16199479e17cSShawn Guo static struct dma_chan *sdma_xlate(struct of_phandle_args *dma_spec,
16209479e17cSShawn Guo 				   struct of_dma *ofdma)
16219479e17cSShawn Guo {
16229479e17cSShawn Guo 	struct sdma_engine *sdma = ofdma->of_dma_data;
16239479e17cSShawn Guo 	dma_cap_mask_t mask = sdma->dma_device.cap_mask;
16249479e17cSShawn Guo 	struct imx_dma_data data;
16259479e17cSShawn Guo 
16269479e17cSShawn Guo 	if (dma_spec->args_count != 3)
16279479e17cSShawn Guo 		return NULL;
16289479e17cSShawn Guo 
16299479e17cSShawn Guo 	data.dma_request = dma_spec->args[0];
16309479e17cSShawn Guo 	data.peripheral_type = dma_spec->args[1];
16319479e17cSShawn Guo 	data.priority = dma_spec->args[2];
16328391ecf4SShengjiu Wang 	/*
16338391ecf4SShengjiu Wang 	 * init dma_request2 to zero, which is not used by the dts.
16348391ecf4SShengjiu Wang 	 * For P2P, dma_request2 is init from dma_request_channel(),
16358391ecf4SShengjiu Wang 	 * chan->private will point to the imx_dma_data, and in
16368391ecf4SShengjiu Wang 	 * device_alloc_chan_resources(), imx_dma_data.dma_request2 will
16378391ecf4SShengjiu Wang 	 * be set to sdmac->event_id1.
16388391ecf4SShengjiu Wang 	 */
16398391ecf4SShengjiu Wang 	data.dma_request2 = 0;
16409479e17cSShawn Guo 
16419479e17cSShawn Guo 	return dma_request_channel(mask, sdma_filter_fn, &data);
16429479e17cSShawn Guo }
16439479e17cSShawn Guo 
1644e34b731fSMark Brown static int sdma_probe(struct platform_device *pdev)
16451ec1e82fSSascha Hauer {
1646580975d7SShawn Guo 	const struct of_device_id *of_id =
1647580975d7SShawn Guo 			of_match_device(sdma_dt_ids, &pdev->dev);
1648580975d7SShawn Guo 	struct device_node *np = pdev->dev.of_node;
16498391ecf4SShengjiu Wang 	struct device_node *spba_bus;
1650580975d7SShawn Guo 	const char *fw_name;
16511ec1e82fSSascha Hauer 	int ret;
16521ec1e82fSSascha Hauer 	int irq;
16531ec1e82fSSascha Hauer 	struct resource *iores;
16548391ecf4SShengjiu Wang 	struct resource spba_res;
1655d4adcc01SJingoo Han 	struct sdma_platform_data *pdata = dev_get_platdata(&pdev->dev);
16561ec1e82fSSascha Hauer 	int i;
16571ec1e82fSSascha Hauer 	struct sdma_engine *sdma;
165836e2f21aSSascha Hauer 	s32 *saddr_arr;
165917bba72fSSascha Hauer 	const struct sdma_driver_data *drvdata = NULL;
166017bba72fSSascha Hauer 
166117bba72fSSascha Hauer 	if (of_id)
166217bba72fSSascha Hauer 		drvdata = of_id->data;
166317bba72fSSascha Hauer 	else if (pdev->id_entry)
166417bba72fSSascha Hauer 		drvdata = (void *)pdev->id_entry->driver_data;
166517bba72fSSascha Hauer 
166617bba72fSSascha Hauer 	if (!drvdata) {
166717bba72fSSascha Hauer 		dev_err(&pdev->dev, "unable to find driver data\n");
166817bba72fSSascha Hauer 		return -EINVAL;
166917bba72fSSascha Hauer 	}
16701ec1e82fSSascha Hauer 
167142536b9fSPhilippe Retornaz 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
167242536b9fSPhilippe Retornaz 	if (ret)
167342536b9fSPhilippe Retornaz 		return ret;
167442536b9fSPhilippe Retornaz 
16757f24e0eeSFabio Estevam 	sdma = devm_kzalloc(&pdev->dev, sizeof(*sdma), GFP_KERNEL);
16761ec1e82fSSascha Hauer 	if (!sdma)
16771ec1e82fSSascha Hauer 		return -ENOMEM;
16781ec1e82fSSascha Hauer 
16792ccaef05SRichard Zhao 	spin_lock_init(&sdma->channel_0_lock);
168073eab978SSascha Hauer 
16811ec1e82fSSascha Hauer 	sdma->dev = &pdev->dev;
168217bba72fSSascha Hauer 	sdma->drvdata = drvdata;
16831ec1e82fSSascha Hauer 
16841ec1e82fSSascha Hauer 	irq = platform_get_irq(pdev, 0);
16857f24e0eeSFabio Estevam 	if (irq < 0)
168663c72e02SFabio Estevam 		return irq;
16871ec1e82fSSascha Hauer 
16887f24e0eeSFabio Estevam 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16897f24e0eeSFabio Estevam 	sdma->regs = devm_ioremap_resource(&pdev->dev, iores);
16907f24e0eeSFabio Estevam 	if (IS_ERR(sdma->regs))
16917f24e0eeSFabio Estevam 		return PTR_ERR(sdma->regs);
16921ec1e82fSSascha Hauer 
16937560e3f3SSascha Hauer 	sdma->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
16947f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ipg))
16957f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ipg);
16961ec1e82fSSascha Hauer 
16977560e3f3SSascha Hauer 	sdma->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
16987f24e0eeSFabio Estevam 	if (IS_ERR(sdma->clk_ahb))
16997f24e0eeSFabio Estevam 		return PTR_ERR(sdma->clk_ahb);
17007560e3f3SSascha Hauer 
17017560e3f3SSascha Hauer 	clk_prepare(sdma->clk_ipg);
17027560e3f3SSascha Hauer 	clk_prepare(sdma->clk_ahb);
17037560e3f3SSascha Hauer 
17047f24e0eeSFabio Estevam 	ret = devm_request_irq(&pdev->dev, irq, sdma_int_handler, 0, "sdma",
17057f24e0eeSFabio Estevam 			       sdma);
17061ec1e82fSSascha Hauer 	if (ret)
17077f24e0eeSFabio Estevam 		return ret;
17081ec1e82fSSascha Hauer 
17095bb9dbb5SVinod Koul 	sdma->irq = irq;
17105bb9dbb5SVinod Koul 
17115b28aa31SSascha Hauer 	sdma->script_addrs = kzalloc(sizeof(*sdma->script_addrs), GFP_KERNEL);
17127f24e0eeSFabio Estevam 	if (!sdma->script_addrs)
17137f24e0eeSFabio Estevam 		return -ENOMEM;
17141ec1e82fSSascha Hauer 
171536e2f21aSSascha Hauer 	/* initially no scripts available */
171636e2f21aSSascha Hauer 	saddr_arr = (s32 *)sdma->script_addrs;
171736e2f21aSSascha Hauer 	for (i = 0; i < SDMA_SCRIPT_ADDRS_ARRAY_SIZE_V1; i++)
171836e2f21aSSascha Hauer 		saddr_arr[i] = -EINVAL;
171936e2f21aSSascha Hauer 
17207214a8b1SSascha Hauer 	dma_cap_set(DMA_SLAVE, sdma->dma_device.cap_mask);
17217214a8b1SSascha Hauer 	dma_cap_set(DMA_CYCLIC, sdma->dma_device.cap_mask);
17227214a8b1SSascha Hauer 
17231ec1e82fSSascha Hauer 	INIT_LIST_HEAD(&sdma->dma_device.channels);
17241ec1e82fSSascha Hauer 	/* Initialize channel parameters */
17251ec1e82fSSascha Hauer 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
17261ec1e82fSSascha Hauer 		struct sdma_channel *sdmac = &sdma->channel[i];
17271ec1e82fSSascha Hauer 
17281ec1e82fSSascha Hauer 		sdmac->sdma = sdma;
17291ec1e82fSSascha Hauer 		spin_lock_init(&sdmac->lock);
17301ec1e82fSSascha Hauer 
17311ec1e82fSSascha Hauer 		sdmac->chan.device = &sdma->dma_device;
17328ac69546SRussell King - ARM Linux 		dma_cookie_init(&sdmac->chan);
17331ec1e82fSSascha Hauer 		sdmac->channel = i;
17341ec1e82fSSascha Hauer 
1735abd9ccc8SHuang Shijie 		tasklet_init(&sdmac->tasklet, sdma_tasklet,
1736abd9ccc8SHuang Shijie 			     (unsigned long) sdmac);
173723889c63SSascha Hauer 		/*
173823889c63SSascha Hauer 		 * Add the channel to the DMAC list. Do not add channel 0 though
173923889c63SSascha Hauer 		 * because we need it internally in the SDMA driver. This also means
174023889c63SSascha Hauer 		 * that channel 0 in dmaengine counting matches sdma channel 1.
174123889c63SSascha Hauer 		 */
174223889c63SSascha Hauer 		if (i)
174323889c63SSascha Hauer 			list_add_tail(&sdmac->chan.device_node,
174423889c63SSascha Hauer 					&sdma->dma_device.channels);
17451ec1e82fSSascha Hauer 	}
17461ec1e82fSSascha Hauer 
17475b28aa31SSascha Hauer 	ret = sdma_init(sdma);
17481ec1e82fSSascha Hauer 	if (ret)
17491ec1e82fSSascha Hauer 		goto err_init;
17501ec1e82fSSascha Hauer 
1751d078cd1bSZidan Wang 	ret = sdma_event_remap(sdma);
1752d078cd1bSZidan Wang 	if (ret)
1753d078cd1bSZidan Wang 		goto err_init;
1754d078cd1bSZidan Wang 
1755dcfec3c0SSascha Hauer 	if (sdma->drvdata->script_addrs)
1756dcfec3c0SSascha Hauer 		sdma_add_scripts(sdma, sdma->drvdata->script_addrs);
1757580975d7SShawn Guo 	if (pdata && pdata->script_addrs)
17585b28aa31SSascha Hauer 		sdma_add_scripts(sdma, pdata->script_addrs);
17595b28aa31SSascha Hauer 
1760580975d7SShawn Guo 	if (pdata) {
17616d0d7e2dSFabio Estevam 		ret = sdma_get_firmware(sdma, pdata->fw_name);
17626d0d7e2dSFabio Estevam 		if (ret)
1763ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware from platform data\n");
1764580975d7SShawn Guo 	} else {
1765580975d7SShawn Guo 		/*
1766580975d7SShawn Guo 		 * Because that device tree does not encode ROM script address,
1767580975d7SShawn Guo 		 * the RAM script in firmware is mandatory for device tree
1768580975d7SShawn Guo 		 * probe, otherwise it fails.
1769580975d7SShawn Guo 		 */
1770580975d7SShawn Guo 		ret = of_property_read_string(np, "fsl,sdma-ram-script-name",
1771580975d7SShawn Guo 					      &fw_name);
17726602b0ddSFabio Estevam 		if (ret)
1773ad1122e5SFabio Estevam 			dev_warn(&pdev->dev, "failed to get firmware name\n");
17746602b0ddSFabio Estevam 		else {
1775580975d7SShawn Guo 			ret = sdma_get_firmware(sdma, fw_name);
17766602b0ddSFabio Estevam 			if (ret)
1777ad1122e5SFabio Estevam 				dev_warn(&pdev->dev, "failed to get firmware from device tree\n");
1778580975d7SShawn Guo 		}
1779580975d7SShawn Guo 	}
17805b28aa31SSascha Hauer 
17811ec1e82fSSascha Hauer 	sdma->dma_device.dev = &pdev->dev;
17821ec1e82fSSascha Hauer 
17831ec1e82fSSascha Hauer 	sdma->dma_device.device_alloc_chan_resources = sdma_alloc_chan_resources;
17841ec1e82fSSascha Hauer 	sdma->dma_device.device_free_chan_resources = sdma_free_chan_resources;
17851ec1e82fSSascha Hauer 	sdma->dma_device.device_tx_status = sdma_tx_status;
17861ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_slave_sg = sdma_prep_slave_sg;
17871ec1e82fSSascha Hauer 	sdma->dma_device.device_prep_dma_cyclic = sdma_prep_dma_cyclic;
17887b350ab0SMaxime Ripard 	sdma->dma_device.device_config = sdma_config;
17897b350ab0SMaxime Ripard 	sdma->dma_device.device_terminate_all = sdma_disable_channel;
17901e4a4f50SFabio Estevam 	sdma->dma_device.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
17911e4a4f50SFabio Estevam 	sdma->dma_device.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
17921e4a4f50SFabio Estevam 	sdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
17931e4a4f50SFabio Estevam 	sdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
17941ec1e82fSSascha Hauer 	sdma->dma_device.device_issue_pending = sdma_issue_pending;
1795b9b3f82fSSascha Hauer 	sdma->dma_device.dev->dma_parms = &sdma->dma_parms;
1796b9b3f82fSSascha Hauer 	dma_set_max_seg_size(sdma->dma_device.dev, 65535);
17971ec1e82fSSascha Hauer 
179823e11811SVignesh Raman 	platform_set_drvdata(pdev, sdma);
179923e11811SVignesh Raman 
18001ec1e82fSSascha Hauer 	ret = dma_async_device_register(&sdma->dma_device);
18011ec1e82fSSascha Hauer 	if (ret) {
18021ec1e82fSSascha Hauer 		dev_err(&pdev->dev, "unable to register\n");
18031ec1e82fSSascha Hauer 		goto err_init;
18041ec1e82fSSascha Hauer 	}
18051ec1e82fSSascha Hauer 
18069479e17cSShawn Guo 	if (np) {
18079479e17cSShawn Guo 		ret = of_dma_controller_register(np, sdma_xlate, sdma);
18089479e17cSShawn Guo 		if (ret) {
18099479e17cSShawn Guo 			dev_err(&pdev->dev, "failed to register controller\n");
18109479e17cSShawn Guo 			goto err_register;
18119479e17cSShawn Guo 		}
18128391ecf4SShengjiu Wang 
18138391ecf4SShengjiu Wang 		spba_bus = of_find_compatible_node(NULL, NULL, "fsl,spba-bus");
18148391ecf4SShengjiu Wang 		ret = of_address_to_resource(spba_bus, 0, &spba_res);
18158391ecf4SShengjiu Wang 		if (!ret) {
18168391ecf4SShengjiu Wang 			sdma->spba_start_addr = spba_res.start;
18178391ecf4SShengjiu Wang 			sdma->spba_end_addr = spba_res.end;
18188391ecf4SShengjiu Wang 		}
18198391ecf4SShengjiu Wang 		of_node_put(spba_bus);
18209479e17cSShawn Guo 	}
18219479e17cSShawn Guo 
18221ec1e82fSSascha Hauer 	return 0;
18231ec1e82fSSascha Hauer 
18249479e17cSShawn Guo err_register:
18259479e17cSShawn Guo 	dma_async_device_unregister(&sdma->dma_device);
18261ec1e82fSSascha Hauer err_init:
18271ec1e82fSSascha Hauer 	kfree(sdma->script_addrs);
1828939fd4f0SShawn Guo 	return ret;
18291ec1e82fSSascha Hauer }
18301ec1e82fSSascha Hauer 
18311d1bbd30SMaxin B. John static int sdma_remove(struct platform_device *pdev)
18321ec1e82fSSascha Hauer {
183323e11811SVignesh Raman 	struct sdma_engine *sdma = platform_get_drvdata(pdev);
1834c12fe497SVignesh Raman 	int i;
183523e11811SVignesh Raman 
18365bb9dbb5SVinod Koul 	devm_free_irq(&pdev->dev, sdma->irq, sdma);
183723e11811SVignesh Raman 	dma_async_device_unregister(&sdma->dma_device);
183823e11811SVignesh Raman 	kfree(sdma->script_addrs);
1839c12fe497SVignesh Raman 	/* Kill the tasklet */
1840c12fe497SVignesh Raman 	for (i = 0; i < MAX_DMA_CHANNELS; i++) {
1841c12fe497SVignesh Raman 		struct sdma_channel *sdmac = &sdma->channel[i];
1842c12fe497SVignesh Raman 
1843c12fe497SVignesh Raman 		tasklet_kill(&sdmac->tasklet);
1844c12fe497SVignesh Raman 	}
184523e11811SVignesh Raman 
184623e11811SVignesh Raman 	platform_set_drvdata(pdev, NULL);
184723e11811SVignesh Raman 	return 0;
18481ec1e82fSSascha Hauer }
18491ec1e82fSSascha Hauer 
18501ec1e82fSSascha Hauer static struct platform_driver sdma_driver = {
18511ec1e82fSSascha Hauer 	.driver		= {
18521ec1e82fSSascha Hauer 		.name	= "imx-sdma",
1853580975d7SShawn Guo 		.of_match_table = sdma_dt_ids,
18541ec1e82fSSascha Hauer 	},
185562550cd7SShawn Guo 	.id_table	= sdma_devtypes,
18561d1bbd30SMaxin B. John 	.remove		= sdma_remove,
185723e11811SVignesh Raman 	.probe		= sdma_probe,
18581ec1e82fSSascha Hauer };
18591ec1e82fSSascha Hauer 
186023e11811SVignesh Raman module_platform_driver(sdma_driver);
18611ec1e82fSSascha Hauer 
18621ec1e82fSSascha Hauer MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
18631ec1e82fSSascha Hauer MODULE_DESCRIPTION("i.MX SDMA driver");
18641ec1e82fSSascha Hauer MODULE_LICENSE("GPL");
1865