xref: /linux/drivers/dma/ste_dma40.c (revision fdaf9c4b22247a6cc6cda9459be3e52764c14d95)
18d318a50SLinus Walleij /*
2d49278e3SPer Forlin  * Copyright (C) Ericsson AB 2007-2008
3d49278e3SPer Forlin  * Copyright (C) ST-Ericsson SA 2008-2010
4661385f9SPer Forlin  * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5767a9675SJonas Aaberg  * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
68d318a50SLinus Walleij  * License terms: GNU General Public License (GPL) version 2
78d318a50SLinus Walleij  */
88d318a50SLinus Walleij 
9b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h>
108d318a50SLinus Walleij #include <linux/kernel.h>
118d318a50SLinus Walleij #include <linux/slab.h>
12f492b210SPaul Gortmaker #include <linux/export.h>
138d318a50SLinus Walleij #include <linux/dmaengine.h>
148d318a50SLinus Walleij #include <linux/platform_device.h>
158d318a50SLinus Walleij #include <linux/clk.h>
168d318a50SLinus Walleij #include <linux/delay.h>
177fb3e75eSNarayanan G #include <linux/pm.h>
187fb3e75eSNarayanan G #include <linux/pm_runtime.h>
19698e4732SJonas Aaberg #include <linux/err.h>
20f4b89764SLinus Walleij #include <linux/amba/bus.h>
2115e4b78dSLinus Walleij #include <linux/regulator/consumer.h>
228d318a50SLinus Walleij 
238d318a50SLinus Walleij #include <plat/ste_dma40.h>
248d318a50SLinus Walleij 
25d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
268d318a50SLinus Walleij #include "ste_dma40_ll.h"
278d318a50SLinus Walleij 
288d318a50SLinus Walleij #define D40_NAME "dma40"
298d318a50SLinus Walleij 
308d318a50SLinus Walleij #define D40_PHY_CHAN -1
318d318a50SLinus Walleij 
328d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */
338d318a50SLinus Walleij #define D40_CHAN_POS(chan)  (2 * (chan / 2))
348d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
358d318a50SLinus Walleij 
368d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */
378d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500
388d318a50SLinus Walleij 
397fb3e75eSNarayanan G /* Milliseconds */
407fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY	100
417fb3e75eSNarayanan G 
42508849adSLinus Walleij /* Hardware requirement on LCLA alignment */
43508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000
44698e4732SJonas Aaberg 
45698e4732SJonas Aaberg /* Max number of links per event group */
46698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128
47698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
48698e4732SJonas Aaberg 
49508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */
50508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256
51508849adSLinus Walleij 
52508849adSLinus Walleij /* Bit markings for allocation map */
538d318a50SLinus Walleij #define D40_ALLOC_FREE		(1 << 31)
548d318a50SLinus Walleij #define D40_ALLOC_PHY		(1 << 30)
558d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE	0
568d318a50SLinus Walleij 
578d318a50SLinus Walleij /**
588d318a50SLinus Walleij  * enum 40_command - The different commands and/or statuses.
598d318a50SLinus Walleij  *
608d318a50SLinus Walleij  * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
618d318a50SLinus Walleij  * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
628d318a50SLinus Walleij  * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
638d318a50SLinus Walleij  * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
648d318a50SLinus Walleij  */
658d318a50SLinus Walleij enum d40_command {
668d318a50SLinus Walleij 	D40_DMA_STOP		= 0,
678d318a50SLinus Walleij 	D40_DMA_RUN		= 1,
688d318a50SLinus Walleij 	D40_DMA_SUSPEND_REQ	= 2,
698d318a50SLinus Walleij 	D40_DMA_SUSPENDED	= 3
708d318a50SLinus Walleij };
718d318a50SLinus Walleij 
727fb3e75eSNarayanan G /*
731bdae6f4SNarayanan G  * enum d40_events - The different Event Enables for the event lines.
741bdae6f4SNarayanan G  *
751bdae6f4SNarayanan G  * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
761bdae6f4SNarayanan G  * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
771bdae6f4SNarayanan G  * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
781bdae6f4SNarayanan G  * @D40_ROUND_EVENTLINE: Status check for event line.
791bdae6f4SNarayanan G  */
801bdae6f4SNarayanan G 
811bdae6f4SNarayanan G enum d40_events {
821bdae6f4SNarayanan G 	D40_DEACTIVATE_EVENTLINE	= 0,
831bdae6f4SNarayanan G 	D40_ACTIVATE_EVENTLINE		= 1,
841bdae6f4SNarayanan G 	D40_SUSPEND_REQ_EVENTLINE	= 2,
851bdae6f4SNarayanan G 	D40_ROUND_EVENTLINE		= 3
861bdae6f4SNarayanan G };
871bdae6f4SNarayanan G 
881bdae6f4SNarayanan G /*
897fb3e75eSNarayanan G  * These are the registers that has to be saved and later restored
907fb3e75eSNarayanan G  * when the DMA hw is powered off.
917fb3e75eSNarayanan G  * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
927fb3e75eSNarayanan G  */
937fb3e75eSNarayanan G static u32 d40_backup_regs[] = {
947fb3e75eSNarayanan G 	D40_DREG_LCPA,
957fb3e75eSNarayanan G 	D40_DREG_LCLA,
967fb3e75eSNarayanan G 	D40_DREG_PRMSE,
977fb3e75eSNarayanan G 	D40_DREG_PRMSO,
987fb3e75eSNarayanan G 	D40_DREG_PRMOE,
997fb3e75eSNarayanan G 	D40_DREG_PRMOO,
1007fb3e75eSNarayanan G };
1017fb3e75eSNarayanan G 
1027fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
1037fb3e75eSNarayanan G 
1047fb3e75eSNarayanan G /* TODO: Check if all these registers have to be saved/restored on dma40 v3 */
1057fb3e75eSNarayanan G static u32 d40_backup_regs_v3[] = {
1067fb3e75eSNarayanan G 	D40_DREG_PSEG1,
1077fb3e75eSNarayanan G 	D40_DREG_PSEG2,
1087fb3e75eSNarayanan G 	D40_DREG_PSEG3,
1097fb3e75eSNarayanan G 	D40_DREG_PSEG4,
1107fb3e75eSNarayanan G 	D40_DREG_PCEG1,
1117fb3e75eSNarayanan G 	D40_DREG_PCEG2,
1127fb3e75eSNarayanan G 	D40_DREG_PCEG3,
1137fb3e75eSNarayanan G 	D40_DREG_PCEG4,
1147fb3e75eSNarayanan G 	D40_DREG_RSEG1,
1157fb3e75eSNarayanan G 	D40_DREG_RSEG2,
1167fb3e75eSNarayanan G 	D40_DREG_RSEG3,
1177fb3e75eSNarayanan G 	D40_DREG_RSEG4,
1187fb3e75eSNarayanan G 	D40_DREG_RCEG1,
1197fb3e75eSNarayanan G 	D40_DREG_RCEG2,
1207fb3e75eSNarayanan G 	D40_DREG_RCEG3,
1217fb3e75eSNarayanan G 	D40_DREG_RCEG4,
1227fb3e75eSNarayanan G };
1237fb3e75eSNarayanan G 
1247fb3e75eSNarayanan G #define BACKUP_REGS_SZ_V3 ARRAY_SIZE(d40_backup_regs_v3)
1257fb3e75eSNarayanan G 
1267fb3e75eSNarayanan G static u32 d40_backup_regs_chan[] = {
1277fb3e75eSNarayanan G 	D40_CHAN_REG_SSCFG,
1287fb3e75eSNarayanan G 	D40_CHAN_REG_SSELT,
1297fb3e75eSNarayanan G 	D40_CHAN_REG_SSPTR,
1307fb3e75eSNarayanan G 	D40_CHAN_REG_SSLNK,
1317fb3e75eSNarayanan G 	D40_CHAN_REG_SDCFG,
1327fb3e75eSNarayanan G 	D40_CHAN_REG_SDELT,
1337fb3e75eSNarayanan G 	D40_CHAN_REG_SDPTR,
1347fb3e75eSNarayanan G 	D40_CHAN_REG_SDLNK,
1357fb3e75eSNarayanan G };
1367fb3e75eSNarayanan G 
1378d318a50SLinus Walleij /**
1388d318a50SLinus Walleij  * struct d40_lli_pool - Structure for keeping LLIs in memory
1398d318a50SLinus Walleij  *
1408d318a50SLinus Walleij  * @base: Pointer to memory area when the pre_alloc_lli's are not large
1418d318a50SLinus Walleij  * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
1428d318a50SLinus Walleij  * pre_alloc_lli is used.
143b00f938cSRabin Vincent  * @dma_addr: DMA address, if mapped
1448d318a50SLinus Walleij  * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
1458d318a50SLinus Walleij  * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
1468d318a50SLinus Walleij  * one buffer to one buffer.
1478d318a50SLinus Walleij  */
1488d318a50SLinus Walleij struct d40_lli_pool {
1498d318a50SLinus Walleij 	void	*base;
1508d318a50SLinus Walleij 	int	 size;
151b00f938cSRabin Vincent 	dma_addr_t	dma_addr;
1528d318a50SLinus Walleij 	/* Space for dst and src, plus an extra for padding */
1538d318a50SLinus Walleij 	u8	 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
1548d318a50SLinus Walleij };
1558d318a50SLinus Walleij 
1568d318a50SLinus Walleij /**
1578d318a50SLinus Walleij  * struct d40_desc - A descriptor is one DMA job.
1588d318a50SLinus Walleij  *
1598d318a50SLinus Walleij  * @lli_phy: LLI settings for physical channel. Both src and dst=
1608d318a50SLinus Walleij  * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
1618d318a50SLinus Walleij  * lli_len equals one.
1628d318a50SLinus Walleij  * @lli_log: Same as above but for logical channels.
1638d318a50SLinus Walleij  * @lli_pool: The pool with two entries pre-allocated.
164941b77a3SPer Friden  * @lli_len: Number of llis of current descriptor.
16525985edcSLucas De Marchi  * @lli_current: Number of transferred llis.
166698e4732SJonas Aaberg  * @lcla_alloc: Number of LCLA entries allocated.
1678d318a50SLinus Walleij  * @txd: DMA engine struct. Used for among other things for communication
1688d318a50SLinus Walleij  * during a transfer.
1698d318a50SLinus Walleij  * @node: List entry.
1708d318a50SLinus Walleij  * @is_in_client_list: true if the client owns this descriptor.
1717fb3e75eSNarayanan G  * @cyclic: true if this is a cyclic job
1728d318a50SLinus Walleij  *
1738d318a50SLinus Walleij  * This descriptor is used for both logical and physical transfers.
1748d318a50SLinus Walleij  */
1758d318a50SLinus Walleij struct d40_desc {
1768d318a50SLinus Walleij 	/* LLI physical */
1778d318a50SLinus Walleij 	struct d40_phy_lli_bidir	 lli_phy;
1788d318a50SLinus Walleij 	/* LLI logical */
1798d318a50SLinus Walleij 	struct d40_log_lli_bidir	 lli_log;
1808d318a50SLinus Walleij 
1818d318a50SLinus Walleij 	struct d40_lli_pool		 lli_pool;
182941b77a3SPer Friden 	int				 lli_len;
183698e4732SJonas Aaberg 	int				 lli_current;
184698e4732SJonas Aaberg 	int				 lcla_alloc;
1858d318a50SLinus Walleij 
1868d318a50SLinus Walleij 	struct dma_async_tx_descriptor	 txd;
1878d318a50SLinus Walleij 	struct list_head		 node;
1888d318a50SLinus Walleij 
1898d318a50SLinus Walleij 	bool				 is_in_client_list;
1900c842b55SRabin Vincent 	bool				 cyclic;
1918d318a50SLinus Walleij };
1928d318a50SLinus Walleij 
1938d318a50SLinus Walleij /**
1948d318a50SLinus Walleij  * struct d40_lcla_pool - LCLA pool settings and data.
1958d318a50SLinus Walleij  *
196508849adSLinus Walleij  * @base: The virtual address of LCLA. 18 bit aligned.
197508849adSLinus Walleij  * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
198508849adSLinus Walleij  * This pointer is only there for clean-up on error.
199508849adSLinus Walleij  * @pages: The number of pages needed for all physical channels.
200508849adSLinus Walleij  * Only used later for clean-up on error
2018d318a50SLinus Walleij  * @lock: Lock to protect the content in this struct.
202698e4732SJonas Aaberg  * @alloc_map: big map over which LCLA entry is own by which job.
2038d318a50SLinus Walleij  */
2048d318a50SLinus Walleij struct d40_lcla_pool {
2058d318a50SLinus Walleij 	void		*base;
206026cbc42SRabin Vincent 	dma_addr_t	dma_addr;
207508849adSLinus Walleij 	void		*base_unaligned;
208508849adSLinus Walleij 	int		 pages;
2098d318a50SLinus Walleij 	spinlock_t	 lock;
210698e4732SJonas Aaberg 	struct d40_desc	**alloc_map;
2118d318a50SLinus Walleij };
2128d318a50SLinus Walleij 
2138d318a50SLinus Walleij /**
2148d318a50SLinus Walleij  * struct d40_phy_res - struct for handling eventlines mapped to physical
2158d318a50SLinus Walleij  * channels.
2168d318a50SLinus Walleij  *
2178d318a50SLinus Walleij  * @lock: A lock protection this entity.
2187fb3e75eSNarayanan G  * @reserved: True if used by secure world or otherwise.
2198d318a50SLinus Walleij  * @num: The physical channel number of this entity.
2208d318a50SLinus Walleij  * @allocated_src: Bit mapped to show which src event line's are mapped to
2218d318a50SLinus Walleij  * this physical channel. Can also be free or physically allocated.
2228d318a50SLinus Walleij  * @allocated_dst: Same as for src but is dst.
2238d318a50SLinus Walleij  * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
224767a9675SJonas Aaberg  * event line number.
2258d318a50SLinus Walleij  */
2268d318a50SLinus Walleij struct d40_phy_res {
2278d318a50SLinus Walleij 	spinlock_t lock;
2287fb3e75eSNarayanan G 	bool	   reserved;
2298d318a50SLinus Walleij 	int	   num;
2308d318a50SLinus Walleij 	u32	   allocated_src;
2318d318a50SLinus Walleij 	u32	   allocated_dst;
2328d318a50SLinus Walleij };
2338d318a50SLinus Walleij 
2348d318a50SLinus Walleij struct d40_base;
2358d318a50SLinus Walleij 
2368d318a50SLinus Walleij /**
2378d318a50SLinus Walleij  * struct d40_chan - Struct that describes a channel.
2388d318a50SLinus Walleij  *
2398d318a50SLinus Walleij  * @lock: A spinlock to protect this struct.
2408d318a50SLinus Walleij  * @log_num: The logical number, if any of this channel.
2418d318a50SLinus Walleij  * @pending_tx: The number of pending transfers. Used between interrupt handler
2428d318a50SLinus Walleij  * and tasklet.
2438d318a50SLinus Walleij  * @busy: Set to true when transfer is ongoing on this channel.
2442a614340SJonas Aaberg  * @phy_chan: Pointer to physical channel which this instance runs on. If this
2452a614340SJonas Aaberg  * point is NULL, then the channel is not allocated.
2468d318a50SLinus Walleij  * @chan: DMA engine handle.
2478d318a50SLinus Walleij  * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
2488d318a50SLinus Walleij  * transfer and call client callback.
2498d318a50SLinus Walleij  * @client: Cliented owned descriptor list.
250da063d26SPer Forlin  * @pending_queue: Submitted jobs, to be issued by issue_pending()
2518d318a50SLinus Walleij  * @active: Active descriptor.
2528d318a50SLinus Walleij  * @queue: Queued jobs.
25382babbb3SPer Forlin  * @prepare_queue: Prepared jobs.
2548d318a50SLinus Walleij  * @dma_cfg: The client configuration of this dma channel.
255ce2ca125SRabin Vincent  * @configured: whether the dma_cfg configuration is valid
2568d318a50SLinus Walleij  * @base: Pointer to the device instance struct.
2578d318a50SLinus Walleij  * @src_def_cfg: Default cfg register setting for src.
2588d318a50SLinus Walleij  * @dst_def_cfg: Default cfg register setting for dst.
2598d318a50SLinus Walleij  * @log_def: Default logical channel settings.
2608d318a50SLinus Walleij  * @lcpa: Pointer to dst and src lcpa settings.
261ae752bf4Som prakash  * @runtime_addr: runtime configured address.
262ae752bf4Som prakash  * @runtime_direction: runtime configured direction.
2638d318a50SLinus Walleij  *
2648d318a50SLinus Walleij  * This struct can either "be" a logical or a physical channel.
2658d318a50SLinus Walleij  */
2668d318a50SLinus Walleij struct d40_chan {
2678d318a50SLinus Walleij 	spinlock_t			 lock;
2688d318a50SLinus Walleij 	int				 log_num;
2698d318a50SLinus Walleij 	int				 pending_tx;
2708d318a50SLinus Walleij 	bool				 busy;
2718d318a50SLinus Walleij 	struct d40_phy_res		*phy_chan;
2728d318a50SLinus Walleij 	struct dma_chan			 chan;
2738d318a50SLinus Walleij 	struct tasklet_struct		 tasklet;
2748d318a50SLinus Walleij 	struct list_head		 client;
275a8f3067bSPer Forlin 	struct list_head		 pending_queue;
2768d318a50SLinus Walleij 	struct list_head		 active;
2778d318a50SLinus Walleij 	struct list_head		 queue;
27882babbb3SPer Forlin 	struct list_head		 prepare_queue;
2798d318a50SLinus Walleij 	struct stedma40_chan_cfg	 dma_cfg;
280ce2ca125SRabin Vincent 	bool				 configured;
2818d318a50SLinus Walleij 	struct d40_base			*base;
2828d318a50SLinus Walleij 	/* Default register configurations */
2838d318a50SLinus Walleij 	u32				 src_def_cfg;
2848d318a50SLinus Walleij 	u32				 dst_def_cfg;
2858d318a50SLinus Walleij 	struct d40_def_lcsp		 log_def;
2868d318a50SLinus Walleij 	struct d40_log_lli_full		*lcpa;
28795e1400fSLinus Walleij 	/* Runtime reconfiguration */
28895e1400fSLinus Walleij 	dma_addr_t			runtime_addr;
289db8196dfSVinod Koul 	enum dma_transfer_direction	runtime_direction;
2908d318a50SLinus Walleij };
2918d318a50SLinus Walleij 
2928d318a50SLinus Walleij /**
2938d318a50SLinus Walleij  * struct d40_base - The big global struct, one for each probe'd instance.
2948d318a50SLinus Walleij  *
2958d318a50SLinus Walleij  * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
2968d318a50SLinus Walleij  * @execmd_lock: Lock for execute command usage since several channels share
2978d318a50SLinus Walleij  * the same physical register.
2988d318a50SLinus Walleij  * @dev: The device structure.
2998d318a50SLinus Walleij  * @virtbase: The virtual base address of the DMA's register.
300f4185592SLinus Walleij  * @rev: silicon revision detected.
3018d318a50SLinus Walleij  * @clk: Pointer to the DMA clock structure.
3028d318a50SLinus Walleij  * @phy_start: Physical memory start of the DMA registers.
3038d318a50SLinus Walleij  * @phy_size: Size of the DMA register map.
3048d318a50SLinus Walleij  * @irq: The IRQ number.
3058d318a50SLinus Walleij  * @num_phy_chans: The number of physical channels. Read from HW. This
3068d318a50SLinus Walleij  * is the number of available channels for this driver, not counting "Secure
3078d318a50SLinus Walleij  * mode" allocated physical channels.
3088d318a50SLinus Walleij  * @num_log_chans: The number of logical channels. Calculated from
3098d318a50SLinus Walleij  * num_phy_chans.
3108d318a50SLinus Walleij  * @dma_both: dma_device channels that can do both memcpy and slave transfers.
3118d318a50SLinus Walleij  * @dma_slave: dma_device channels that can do only do slave transfers.
3128d318a50SLinus Walleij  * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
3137fb3e75eSNarayanan G  * @phy_chans: Room for all possible physical channels in system.
3148d318a50SLinus Walleij  * @log_chans: Room for all possible logical channels in system.
3158d318a50SLinus Walleij  * @lookup_log_chans: Used to map interrupt number to logical channel. Points
3168d318a50SLinus Walleij  * to log_chans entries.
3178d318a50SLinus Walleij  * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
3188d318a50SLinus Walleij  * to phy_chans entries.
3198d318a50SLinus Walleij  * @plat_data: Pointer to provided platform_data which is the driver
3208d318a50SLinus Walleij  * configuration.
32128c7a19dSNarayanan G  * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
3228d318a50SLinus Walleij  * @phy_res: Vector containing all physical channels.
3238d318a50SLinus Walleij  * @lcla_pool: lcla pool settings and data.
3248d318a50SLinus Walleij  * @lcpa_base: The virtual mapped address of LCPA.
3258d318a50SLinus Walleij  * @phy_lcpa: The physical address of the LCPA.
3268d318a50SLinus Walleij  * @lcpa_size: The size of the LCPA area.
327c675b1b4SJonas Aaberg  * @desc_slab: cache for descriptors.
3287fb3e75eSNarayanan G  * @reg_val_backup: Here the values of some hardware registers are stored
3297fb3e75eSNarayanan G  * before the DMA is powered off. They are restored when the power is back on.
3307fb3e75eSNarayanan G  * @reg_val_backup_v3: Backup of registers that only exits on dma40 v3 and
3317fb3e75eSNarayanan G  * later.
3327fb3e75eSNarayanan G  * @reg_val_backup_chan: Backup data for standard channel parameter registers.
3337fb3e75eSNarayanan G  * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
3347fb3e75eSNarayanan G  * @initialized: true if the dma has been initialized
3358d318a50SLinus Walleij  */
3368d318a50SLinus Walleij struct d40_base {
3378d318a50SLinus Walleij 	spinlock_t			 interrupt_lock;
3388d318a50SLinus Walleij 	spinlock_t			 execmd_lock;
3398d318a50SLinus Walleij 	struct device			 *dev;
3408d318a50SLinus Walleij 	void __iomem			 *virtbase;
341f4185592SLinus Walleij 	u8				  rev:4;
3428d318a50SLinus Walleij 	struct clk			 *clk;
3438d318a50SLinus Walleij 	phys_addr_t			  phy_start;
3448d318a50SLinus Walleij 	resource_size_t			  phy_size;
3458d318a50SLinus Walleij 	int				  irq;
3468d318a50SLinus Walleij 	int				  num_phy_chans;
3478d318a50SLinus Walleij 	int				  num_log_chans;
3488d318a50SLinus Walleij 	struct dma_device		  dma_both;
3498d318a50SLinus Walleij 	struct dma_device		  dma_slave;
3508d318a50SLinus Walleij 	struct dma_device		  dma_memcpy;
3518d318a50SLinus Walleij 	struct d40_chan			 *phy_chans;
3528d318a50SLinus Walleij 	struct d40_chan			 *log_chans;
3538d318a50SLinus Walleij 	struct d40_chan			**lookup_log_chans;
3548d318a50SLinus Walleij 	struct d40_chan			**lookup_phy_chans;
3558d318a50SLinus Walleij 	struct stedma40_platform_data	 *plat_data;
35628c7a19dSNarayanan G 	struct regulator		 *lcpa_regulator;
3578d318a50SLinus Walleij 	/* Physical half channels */
3588d318a50SLinus Walleij 	struct d40_phy_res		 *phy_res;
3598d318a50SLinus Walleij 	struct d40_lcla_pool		  lcla_pool;
3608d318a50SLinus Walleij 	void				 *lcpa_base;
3618d318a50SLinus Walleij 	dma_addr_t			  phy_lcpa;
3628d318a50SLinus Walleij 	resource_size_t			  lcpa_size;
363c675b1b4SJonas Aaberg 	struct kmem_cache		 *desc_slab;
3647fb3e75eSNarayanan G 	u32				  reg_val_backup[BACKUP_REGS_SZ];
3657fb3e75eSNarayanan G 	u32				  reg_val_backup_v3[BACKUP_REGS_SZ_V3];
3667fb3e75eSNarayanan G 	u32				 *reg_val_backup_chan;
3677fb3e75eSNarayanan G 	u16				  gcc_pwr_off_mask;
3687fb3e75eSNarayanan G 	bool				  initialized;
3698d318a50SLinus Walleij };
3708d318a50SLinus Walleij 
3718d318a50SLinus Walleij /**
3728d318a50SLinus Walleij  * struct d40_interrupt_lookup - lookup table for interrupt handler
3738d318a50SLinus Walleij  *
3748d318a50SLinus Walleij  * @src: Interrupt mask register.
3758d318a50SLinus Walleij  * @clr: Interrupt clear register.
3768d318a50SLinus Walleij  * @is_error: true if this is an error interrupt.
3778d318a50SLinus Walleij  * @offset: start delta in the lookup_log_chans in d40_base. If equals to
3788d318a50SLinus Walleij  * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
3798d318a50SLinus Walleij  */
3808d318a50SLinus Walleij struct d40_interrupt_lookup {
3818d318a50SLinus Walleij 	u32 src;
3828d318a50SLinus Walleij 	u32 clr;
3838d318a50SLinus Walleij 	bool is_error;
3848d318a50SLinus Walleij 	int offset;
3858d318a50SLinus Walleij };
3868d318a50SLinus Walleij 
3878d318a50SLinus Walleij /**
3888d318a50SLinus Walleij  * struct d40_reg_val - simple lookup struct
3898d318a50SLinus Walleij  *
3908d318a50SLinus Walleij  * @reg: The register.
3918d318a50SLinus Walleij  * @val: The value that belongs to the register in reg.
3928d318a50SLinus Walleij  */
3938d318a50SLinus Walleij struct d40_reg_val {
3948d318a50SLinus Walleij 	unsigned int reg;
3958d318a50SLinus Walleij 	unsigned int val;
3968d318a50SLinus Walleij };
3978d318a50SLinus Walleij 
398262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c)
399262d2915SRabin Vincent {
400262d2915SRabin Vincent 	return &d40c->chan.dev->device;
401262d2915SRabin Vincent }
402262d2915SRabin Vincent 
403724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan)
404724a8577SRabin Vincent {
405724a8577SRabin Vincent 	return chan->log_num == D40_PHY_CHAN;
406724a8577SRabin Vincent }
407724a8577SRabin Vincent 
408724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan)
409724a8577SRabin Vincent {
410724a8577SRabin Vincent 	return !chan_is_physical(chan);
411724a8577SRabin Vincent }
412724a8577SRabin Vincent 
4138ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan)
4148ca84687SRabin Vincent {
4158ca84687SRabin Vincent 	return chan->base->virtbase + D40_DREG_PCBASE +
4168ca84687SRabin Vincent 	       chan->phy_chan->num * D40_DREG_PCDELTA;
4178ca84687SRabin Vincent }
4188ca84687SRabin Vincent 
4196db5a8baSRabin Vincent #define d40_err(dev, format, arg...)		\
4206db5a8baSRabin Vincent 	dev_err(dev, "[%s] " format, __func__, ## arg)
4216db5a8baSRabin Vincent 
4226db5a8baSRabin Vincent #define chan_err(d40c, format, arg...)		\
4236db5a8baSRabin Vincent 	d40_err(chan2dev(d40c), format, ## arg)
4246db5a8baSRabin Vincent 
425b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
426dbd88788SRabin Vincent 			      int lli_len)
4278d318a50SLinus Walleij {
428dbd88788SRabin Vincent 	bool is_log = chan_is_logical(d40c);
4298d318a50SLinus Walleij 	u32 align;
4308d318a50SLinus Walleij 	void *base;
4318d318a50SLinus Walleij 
4328d318a50SLinus Walleij 	if (is_log)
4338d318a50SLinus Walleij 		align = sizeof(struct d40_log_lli);
4348d318a50SLinus Walleij 	else
4358d318a50SLinus Walleij 		align = sizeof(struct d40_phy_lli);
4368d318a50SLinus Walleij 
4378d318a50SLinus Walleij 	if (lli_len == 1) {
4388d318a50SLinus Walleij 		base = d40d->lli_pool.pre_alloc_lli;
4398d318a50SLinus Walleij 		d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
4408d318a50SLinus Walleij 		d40d->lli_pool.base = NULL;
4418d318a50SLinus Walleij 	} else {
442594ece4dSRabin Vincent 		d40d->lli_pool.size = lli_len * 2 * align;
4438d318a50SLinus Walleij 
4448d318a50SLinus Walleij 		base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
4458d318a50SLinus Walleij 		d40d->lli_pool.base = base;
4468d318a50SLinus Walleij 
4478d318a50SLinus Walleij 		if (d40d->lli_pool.base == NULL)
4488d318a50SLinus Walleij 			return -ENOMEM;
4498d318a50SLinus Walleij 	}
4508d318a50SLinus Walleij 
4518d318a50SLinus Walleij 	if (is_log) {
452d924abadSRabin Vincent 		d40d->lli_log.src = PTR_ALIGN(base, align);
453594ece4dSRabin Vincent 		d40d->lli_log.dst = d40d->lli_log.src + lli_len;
454b00f938cSRabin Vincent 
455b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = 0;
4568d318a50SLinus Walleij 	} else {
457d924abadSRabin Vincent 		d40d->lli_phy.src = PTR_ALIGN(base, align);
458594ece4dSRabin Vincent 		d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
459b00f938cSRabin Vincent 
460b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
461b00f938cSRabin Vincent 							 d40d->lli_phy.src,
462b00f938cSRabin Vincent 							 d40d->lli_pool.size,
463b00f938cSRabin Vincent 							 DMA_TO_DEVICE);
464b00f938cSRabin Vincent 
465b00f938cSRabin Vincent 		if (dma_mapping_error(d40c->base->dev,
466b00f938cSRabin Vincent 				      d40d->lli_pool.dma_addr)) {
467b00f938cSRabin Vincent 			kfree(d40d->lli_pool.base);
468b00f938cSRabin Vincent 			d40d->lli_pool.base = NULL;
469b00f938cSRabin Vincent 			d40d->lli_pool.dma_addr = 0;
470b00f938cSRabin Vincent 			return -ENOMEM;
471b00f938cSRabin Vincent 		}
4728d318a50SLinus Walleij 	}
4738d318a50SLinus Walleij 
4748d318a50SLinus Walleij 	return 0;
4758d318a50SLinus Walleij }
4768d318a50SLinus Walleij 
477b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
4788d318a50SLinus Walleij {
479b00f938cSRabin Vincent 	if (d40d->lli_pool.dma_addr)
480b00f938cSRabin Vincent 		dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
481b00f938cSRabin Vincent 				 d40d->lli_pool.size, DMA_TO_DEVICE);
482b00f938cSRabin Vincent 
4838d318a50SLinus Walleij 	kfree(d40d->lli_pool.base);
4848d318a50SLinus Walleij 	d40d->lli_pool.base = NULL;
4858d318a50SLinus Walleij 	d40d->lli_pool.size = 0;
4868d318a50SLinus Walleij 	d40d->lli_log.src = NULL;
4878d318a50SLinus Walleij 	d40d->lli_log.dst = NULL;
4888d318a50SLinus Walleij 	d40d->lli_phy.src = NULL;
4898d318a50SLinus Walleij 	d40d->lli_phy.dst = NULL;
4908d318a50SLinus Walleij }
4918d318a50SLinus Walleij 
492698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c,
493698e4732SJonas Aaberg 			      struct d40_desc *d40d)
494698e4732SJonas Aaberg {
495698e4732SJonas Aaberg 	unsigned long flags;
496698e4732SJonas Aaberg 	int i;
497698e4732SJonas Aaberg 	int ret = -EINVAL;
498698e4732SJonas Aaberg 	int p;
499698e4732SJonas Aaberg 
500698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
501698e4732SJonas Aaberg 
502698e4732SJonas Aaberg 	p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP;
503698e4732SJonas Aaberg 
504698e4732SJonas Aaberg 	/*
505698e4732SJonas Aaberg 	 * Allocate both src and dst at the same time, therefore the half
506698e4732SJonas Aaberg 	 * start on 1 since 0 can't be used since zero is used as end marker.
507698e4732SJonas Aaberg 	 */
508698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
509698e4732SJonas Aaberg 		if (!d40c->base->lcla_pool.alloc_map[p + i]) {
510698e4732SJonas Aaberg 			d40c->base->lcla_pool.alloc_map[p + i] = d40d;
511698e4732SJonas Aaberg 			d40d->lcla_alloc++;
512698e4732SJonas Aaberg 			ret = i;
513698e4732SJonas Aaberg 			break;
514698e4732SJonas Aaberg 		}
515698e4732SJonas Aaberg 	}
516698e4732SJonas Aaberg 
517698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
518698e4732SJonas Aaberg 
519698e4732SJonas Aaberg 	return ret;
520698e4732SJonas Aaberg }
521698e4732SJonas Aaberg 
522698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c,
523698e4732SJonas Aaberg 			     struct d40_desc *d40d)
524698e4732SJonas Aaberg {
525698e4732SJonas Aaberg 	unsigned long flags;
526698e4732SJonas Aaberg 	int i;
527698e4732SJonas Aaberg 	int ret = -EINVAL;
528698e4732SJonas Aaberg 
529724a8577SRabin Vincent 	if (chan_is_physical(d40c))
530698e4732SJonas Aaberg 		return 0;
531698e4732SJonas Aaberg 
532698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
533698e4732SJonas Aaberg 
534698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
535698e4732SJonas Aaberg 		if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
536698e4732SJonas Aaberg 						    D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) {
537698e4732SJonas Aaberg 			d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num *
538698e4732SJonas Aaberg 							D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL;
539698e4732SJonas Aaberg 			d40d->lcla_alloc--;
540698e4732SJonas Aaberg 			if (d40d->lcla_alloc == 0) {
541698e4732SJonas Aaberg 				ret = 0;
542698e4732SJonas Aaberg 				break;
543698e4732SJonas Aaberg 			}
544698e4732SJonas Aaberg 		}
545698e4732SJonas Aaberg 	}
546698e4732SJonas Aaberg 
547698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
548698e4732SJonas Aaberg 
549698e4732SJonas Aaberg 	return ret;
550698e4732SJonas Aaberg 
551698e4732SJonas Aaberg }
552698e4732SJonas Aaberg 
5538d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d)
5548d318a50SLinus Walleij {
5558d318a50SLinus Walleij 	list_del(&d40d->node);
5568d318a50SLinus Walleij }
5578d318a50SLinus Walleij 
5588d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
5598d318a50SLinus Walleij {
560a2c15fa4SRabin Vincent 	struct d40_desc *desc = NULL;
561a2c15fa4SRabin Vincent 
562a2c15fa4SRabin Vincent 	if (!list_empty(&d40c->client)) {
5638d318a50SLinus Walleij 		struct d40_desc *d;
5648d318a50SLinus Walleij 		struct d40_desc *_d;
5658d318a50SLinus Walleij 
5667fb3e75eSNarayanan G 		list_for_each_entry_safe(d, _d, &d40c->client, node) {
5678d318a50SLinus Walleij 			if (async_tx_test_ack(&d->txd)) {
5688d318a50SLinus Walleij 				d40_desc_remove(d);
569a2c15fa4SRabin Vincent 				desc = d;
570a2c15fa4SRabin Vincent 				memset(desc, 0, sizeof(*desc));
571c675b1b4SJonas Aaberg 				break;
5728d318a50SLinus Walleij 			}
5738d318a50SLinus Walleij 		}
5747fb3e75eSNarayanan G 	}
575a2c15fa4SRabin Vincent 
576a2c15fa4SRabin Vincent 	if (!desc)
577a2c15fa4SRabin Vincent 		desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
578a2c15fa4SRabin Vincent 
579a2c15fa4SRabin Vincent 	if (desc)
580a2c15fa4SRabin Vincent 		INIT_LIST_HEAD(&desc->node);
581a2c15fa4SRabin Vincent 
582a2c15fa4SRabin Vincent 	return desc;
5838d318a50SLinus Walleij }
5848d318a50SLinus Walleij 
5858d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
5868d318a50SLinus Walleij {
587698e4732SJonas Aaberg 
588b00f938cSRabin Vincent 	d40_pool_lli_free(d40c, d40d);
589698e4732SJonas Aaberg 	d40_lcla_free_all(d40c, d40d);
590c675b1b4SJonas Aaberg 	kmem_cache_free(d40c->base->desc_slab, d40d);
5918d318a50SLinus Walleij }
5928d318a50SLinus Walleij 
5938d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
5948d318a50SLinus Walleij {
5958d318a50SLinus Walleij 	list_add_tail(&desc->node, &d40c->active);
5968d318a50SLinus Walleij }
5978d318a50SLinus Walleij 
5981c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
5991c4b0927SRabin Vincent {
6001c4b0927SRabin Vincent 	struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
6011c4b0927SRabin Vincent 	struct d40_phy_lli *lli_src = desc->lli_phy.src;
6021c4b0927SRabin Vincent 	void __iomem *base = chan_base(chan);
6031c4b0927SRabin Vincent 
6041c4b0927SRabin Vincent 	writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
6051c4b0927SRabin Vincent 	writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
6061c4b0927SRabin Vincent 	writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
6071c4b0927SRabin Vincent 	writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
6081c4b0927SRabin Vincent 
6091c4b0927SRabin Vincent 	writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
6101c4b0927SRabin Vincent 	writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
6111c4b0927SRabin Vincent 	writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
6121c4b0927SRabin Vincent 	writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
6131c4b0927SRabin Vincent }
6141c4b0927SRabin Vincent 
615e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
616698e4732SJonas Aaberg {
617e65889c7SRabin Vincent 	struct d40_lcla_pool *pool = &chan->base->lcla_pool;
618e65889c7SRabin Vincent 	struct d40_log_lli_bidir *lli = &desc->lli_log;
619e65889c7SRabin Vincent 	int lli_current = desc->lli_current;
620e65889c7SRabin Vincent 	int lli_len = desc->lli_len;
6210c842b55SRabin Vincent 	bool cyclic = desc->cyclic;
622e65889c7SRabin Vincent 	int curr_lcla = -EINVAL;
6230c842b55SRabin Vincent 	int first_lcla = 0;
62428c7a19dSNarayanan G 	bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
6250c842b55SRabin Vincent 	bool linkback;
626698e4732SJonas Aaberg 
6270c842b55SRabin Vincent 	/*
6280c842b55SRabin Vincent 	 * We may have partially running cyclic transfers, in case we did't get
6290c842b55SRabin Vincent 	 * enough LCLA entries.
6300c842b55SRabin Vincent 	 */
6310c842b55SRabin Vincent 	linkback = cyclic && lli_current == 0;
6320c842b55SRabin Vincent 
6330c842b55SRabin Vincent 	/*
6340c842b55SRabin Vincent 	 * For linkback, we need one LCLA even with only one link, because we
6350c842b55SRabin Vincent 	 * can't link back to the one in LCPA space
6360c842b55SRabin Vincent 	 */
6370c842b55SRabin Vincent 	if (linkback || (lli_len - lli_current > 1)) {
638e65889c7SRabin Vincent 		curr_lcla = d40_lcla_alloc_one(chan, desc);
6390c842b55SRabin Vincent 		first_lcla = curr_lcla;
6400c842b55SRabin Vincent 	}
6410c842b55SRabin Vincent 
6420c842b55SRabin Vincent 	/*
6430c842b55SRabin Vincent 	 * For linkback, we normally load the LCPA in the loop since we need to
6440c842b55SRabin Vincent 	 * link it to the second LCLA and not the first.  However, if we
6450c842b55SRabin Vincent 	 * couldn't even get a first LCLA, then we have to run in LCPA and
6460c842b55SRabin Vincent 	 * reload manually.
6470c842b55SRabin Vincent 	 */
6480c842b55SRabin Vincent 	if (!linkback || curr_lcla == -EINVAL) {
6490c842b55SRabin Vincent 		unsigned int flags = 0;
6500c842b55SRabin Vincent 
6510c842b55SRabin Vincent 		if (curr_lcla == -EINVAL)
6520c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
653698e4732SJonas Aaberg 
654e65889c7SRabin Vincent 		d40_log_lli_lcpa_write(chan->lcpa,
655e65889c7SRabin Vincent 				       &lli->dst[lli_current],
656e65889c7SRabin Vincent 				       &lli->src[lli_current],
6570c842b55SRabin Vincent 				       curr_lcla,
6580c842b55SRabin Vincent 				       flags);
659e65889c7SRabin Vincent 		lli_current++;
6600c842b55SRabin Vincent 	}
6616045f0bbSRabin Vincent 
6626045f0bbSRabin Vincent 	if (curr_lcla < 0)
6636045f0bbSRabin Vincent 		goto out;
6646045f0bbSRabin Vincent 
665e65889c7SRabin Vincent 	for (; lli_current < lli_len; lli_current++) {
666e65889c7SRabin Vincent 		unsigned int lcla_offset = chan->phy_chan->num * 1024 +
667026cbc42SRabin Vincent 					   8 * curr_lcla * 2;
668026cbc42SRabin Vincent 		struct d40_log_lli *lcla = pool->base + lcla_offset;
6690c842b55SRabin Vincent 		unsigned int flags = 0;
670e65889c7SRabin Vincent 		int next_lcla;
671698e4732SJonas Aaberg 
672e65889c7SRabin Vincent 		if (lli_current + 1 < lli_len)
673e65889c7SRabin Vincent 			next_lcla = d40_lcla_alloc_one(chan, desc);
674698e4732SJonas Aaberg 		else
6750c842b55SRabin Vincent 			next_lcla = linkback ? first_lcla : -EINVAL;
676698e4732SJonas Aaberg 
6770c842b55SRabin Vincent 		if (cyclic || next_lcla == -EINVAL)
6780c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
6790c842b55SRabin Vincent 
6800c842b55SRabin Vincent 		if (linkback && curr_lcla == first_lcla) {
6810c842b55SRabin Vincent 			/* First link goes in both LCPA and LCLA */
6820c842b55SRabin Vincent 			d40_log_lli_lcpa_write(chan->lcpa,
6830c842b55SRabin Vincent 					       &lli->dst[lli_current],
6840c842b55SRabin Vincent 					       &lli->src[lli_current],
6850c842b55SRabin Vincent 					       next_lcla, flags);
6860c842b55SRabin Vincent 		}
6870c842b55SRabin Vincent 
6880c842b55SRabin Vincent 		/*
6890c842b55SRabin Vincent 		 * One unused LCLA in the cyclic case if the very first
6900c842b55SRabin Vincent 		 * next_lcla fails...
6910c842b55SRabin Vincent 		 */
692698e4732SJonas Aaberg 		d40_log_lli_lcla_write(lcla,
693e65889c7SRabin Vincent 				       &lli->dst[lli_current],
694e65889c7SRabin Vincent 				       &lli->src[lli_current],
6950c842b55SRabin Vincent 				       next_lcla, flags);
696698e4732SJonas Aaberg 
69728c7a19dSNarayanan G 		/*
69828c7a19dSNarayanan G 		 * Cache maintenance is not needed if lcla is
69928c7a19dSNarayanan G 		 * mapped in esram
70028c7a19dSNarayanan G 		 */
70128c7a19dSNarayanan G 		if (!use_esram_lcla) {
702e65889c7SRabin Vincent 			dma_sync_single_range_for_device(chan->base->dev,
703026cbc42SRabin Vincent 						pool->dma_addr, lcla_offset,
704698e4732SJonas Aaberg 						2 * sizeof(struct d40_log_lli),
705698e4732SJonas Aaberg 						DMA_TO_DEVICE);
70628c7a19dSNarayanan G 		}
707698e4732SJonas Aaberg 		curr_lcla = next_lcla;
708698e4732SJonas Aaberg 
7090c842b55SRabin Vincent 		if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
710e65889c7SRabin Vincent 			lli_current++;
711698e4732SJonas Aaberg 			break;
712698e4732SJonas Aaberg 		}
713e65889c7SRabin Vincent 	}
714698e4732SJonas Aaberg 
7156045f0bbSRabin Vincent out:
716e65889c7SRabin Vincent 	desc->lli_current = lli_current;
717698e4732SJonas Aaberg }
718e65889c7SRabin Vincent 
719e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
720e65889c7SRabin Vincent {
721e65889c7SRabin Vincent 	if (chan_is_physical(d40c)) {
722e65889c7SRabin Vincent 		d40_phy_lli_load(d40c, d40d);
723e65889c7SRabin Vincent 		d40d->lli_current = d40d->lli_len;
724e65889c7SRabin Vincent 	} else
725e65889c7SRabin Vincent 		d40_log_lli_to_lcxa(d40c, d40d);
726698e4732SJonas Aaberg }
727698e4732SJonas Aaberg 
7288d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
7298d318a50SLinus Walleij {
7308d318a50SLinus Walleij 	struct d40_desc *d;
7318d318a50SLinus Walleij 
7328d318a50SLinus Walleij 	if (list_empty(&d40c->active))
7338d318a50SLinus Walleij 		return NULL;
7348d318a50SLinus Walleij 
7358d318a50SLinus Walleij 	d = list_first_entry(&d40c->active,
7368d318a50SLinus Walleij 			     struct d40_desc,
7378d318a50SLinus Walleij 			     node);
7388d318a50SLinus Walleij 	return d;
7398d318a50SLinus Walleij }
7408d318a50SLinus Walleij 
7417404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */
7428d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
7438d318a50SLinus Walleij {
7447404368cSPer Forlin 	d40_desc_remove(desc);
7457404368cSPer Forlin 	desc->is_in_client_list = false;
746a8f3067bSPer Forlin 	list_add_tail(&desc->node, &d40c->pending_queue);
747a8f3067bSPer Forlin }
748a8f3067bSPer Forlin 
749a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
750a8f3067bSPer Forlin {
751a8f3067bSPer Forlin 	struct d40_desc *d;
752a8f3067bSPer Forlin 
753a8f3067bSPer Forlin 	if (list_empty(&d40c->pending_queue))
754a8f3067bSPer Forlin 		return NULL;
755a8f3067bSPer Forlin 
756a8f3067bSPer Forlin 	d = list_first_entry(&d40c->pending_queue,
757a8f3067bSPer Forlin 			     struct d40_desc,
758a8f3067bSPer Forlin 			     node);
759a8f3067bSPer Forlin 	return d;
7608d318a50SLinus Walleij }
7618d318a50SLinus Walleij 
7628d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
7638d318a50SLinus Walleij {
7648d318a50SLinus Walleij 	struct d40_desc *d;
7658d318a50SLinus Walleij 
7668d318a50SLinus Walleij 	if (list_empty(&d40c->queue))
7678d318a50SLinus Walleij 		return NULL;
7688d318a50SLinus Walleij 
7698d318a50SLinus Walleij 	d = list_first_entry(&d40c->queue,
7708d318a50SLinus Walleij 			     struct d40_desc,
7718d318a50SLinus Walleij 			     node);
7728d318a50SLinus Walleij 	return d;
7738d318a50SLinus Walleij }
7748d318a50SLinus Walleij 
775d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize)
776d49278e3SPer Forlin {
777d49278e3SPer Forlin 	if (is_log) {
778d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_LOG_1)
779d49278e3SPer Forlin 			return 1;
780d49278e3SPer Forlin 	} else {
781d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_PHY_1)
782d49278e3SPer Forlin 			return 1;
783d49278e3SPer Forlin 	}
7848d318a50SLinus Walleij 
785d49278e3SPer Forlin 	return 2 << psize;
786d49278e3SPer Forlin }
787d49278e3SPer Forlin 
788d49278e3SPer Forlin /*
789d49278e3SPer Forlin  * The dma only supports transmitting packages up to
790d49278e3SPer Forlin  * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of
791d49278e3SPer Forlin  * dma elements required to send the entire sg list
792d49278e3SPer Forlin  */
793d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
794d49278e3SPer Forlin {
795d49278e3SPer Forlin 	int dmalen;
796d49278e3SPer Forlin 	u32 max_w = max(data_width1, data_width2);
797d49278e3SPer Forlin 	u32 min_w = min(data_width1, data_width2);
798d49278e3SPer Forlin 	u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w);
799d49278e3SPer Forlin 
800d49278e3SPer Forlin 	if (seg_max > STEDMA40_MAX_SEG_SIZE)
801d49278e3SPer Forlin 		seg_max -= (1 << max_w);
802d49278e3SPer Forlin 
803d49278e3SPer Forlin 	if (!IS_ALIGNED(size, 1 << max_w))
804d49278e3SPer Forlin 		return -EINVAL;
805d49278e3SPer Forlin 
806d49278e3SPer Forlin 	if (size <= seg_max)
807d49278e3SPer Forlin 		dmalen = 1;
808d49278e3SPer Forlin 	else {
809d49278e3SPer Forlin 		dmalen = size / seg_max;
810d49278e3SPer Forlin 		if (dmalen * seg_max < size)
811d49278e3SPer Forlin 			dmalen++;
812d49278e3SPer Forlin 	}
813d49278e3SPer Forlin 	return dmalen;
814d49278e3SPer Forlin }
815d49278e3SPer Forlin 
816d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
817d49278e3SPer Forlin 			   u32 data_width1, u32 data_width2)
818d49278e3SPer Forlin {
819d49278e3SPer Forlin 	struct scatterlist *sg;
820d49278e3SPer Forlin 	int i;
821d49278e3SPer Forlin 	int len = 0;
822d49278e3SPer Forlin 	int ret;
823d49278e3SPer Forlin 
824d49278e3SPer Forlin 	for_each_sg(sgl, sg, sg_len, i) {
825d49278e3SPer Forlin 		ret = d40_size_2_dmalen(sg_dma_len(sg),
826d49278e3SPer Forlin 					data_width1, data_width2);
827d49278e3SPer Forlin 		if (ret < 0)
828d49278e3SPer Forlin 			return ret;
829d49278e3SPer Forlin 		len += ret;
830d49278e3SPer Forlin 	}
831d49278e3SPer Forlin 	return len;
832d49278e3SPer Forlin }
833d49278e3SPer Forlin 
8347fb3e75eSNarayanan G 
8357fb3e75eSNarayanan G #ifdef CONFIG_PM
8367fb3e75eSNarayanan G static void dma40_backup(void __iomem *baseaddr, u32 *backup,
8377fb3e75eSNarayanan G 			 u32 *regaddr, int num, bool save)
8387fb3e75eSNarayanan G {
8397fb3e75eSNarayanan G 	int i;
8407fb3e75eSNarayanan G 
8417fb3e75eSNarayanan G 	for (i = 0; i < num; i++) {
8427fb3e75eSNarayanan G 		void __iomem *addr = baseaddr + regaddr[i];
8437fb3e75eSNarayanan G 
8447fb3e75eSNarayanan G 		if (save)
8457fb3e75eSNarayanan G 			backup[i] = readl_relaxed(addr);
8467fb3e75eSNarayanan G 		else
8477fb3e75eSNarayanan G 			writel_relaxed(backup[i], addr);
8487fb3e75eSNarayanan G 	}
8497fb3e75eSNarayanan G }
8507fb3e75eSNarayanan G 
8517fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save)
8527fb3e75eSNarayanan G {
8537fb3e75eSNarayanan G 	int i;
8547fb3e75eSNarayanan G 
8557fb3e75eSNarayanan G 	/* Save/Restore channel specific registers */
8567fb3e75eSNarayanan G 	for (i = 0; i < base->num_phy_chans; i++) {
8577fb3e75eSNarayanan G 		void __iomem *addr;
8587fb3e75eSNarayanan G 		int idx;
8597fb3e75eSNarayanan G 
8607fb3e75eSNarayanan G 		if (base->phy_res[i].reserved)
8617fb3e75eSNarayanan G 			continue;
8627fb3e75eSNarayanan G 
8637fb3e75eSNarayanan G 		addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
8647fb3e75eSNarayanan G 		idx = i * ARRAY_SIZE(d40_backup_regs_chan);
8657fb3e75eSNarayanan G 
8667fb3e75eSNarayanan G 		dma40_backup(addr, &base->reg_val_backup_chan[idx],
8677fb3e75eSNarayanan G 			     d40_backup_regs_chan,
8687fb3e75eSNarayanan G 			     ARRAY_SIZE(d40_backup_regs_chan),
8697fb3e75eSNarayanan G 			     save);
8707fb3e75eSNarayanan G 	}
8717fb3e75eSNarayanan G 
8727fb3e75eSNarayanan G 	/* Save/Restore global registers */
8737fb3e75eSNarayanan G 	dma40_backup(base->virtbase, base->reg_val_backup,
8747fb3e75eSNarayanan G 		     d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
8757fb3e75eSNarayanan G 		     save);
8767fb3e75eSNarayanan G 
8777fb3e75eSNarayanan G 	/* Save/Restore registers only existing on dma40 v3 and later */
8787fb3e75eSNarayanan G 	if (base->rev >= 3)
8797fb3e75eSNarayanan G 		dma40_backup(base->virtbase, base->reg_val_backup_v3,
8807fb3e75eSNarayanan G 			     d40_backup_regs_v3,
8817fb3e75eSNarayanan G 			     ARRAY_SIZE(d40_backup_regs_v3),
8827fb3e75eSNarayanan G 			     save);
8837fb3e75eSNarayanan G }
8847fb3e75eSNarayanan G #else
8857fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save)
8867fb3e75eSNarayanan G {
8877fb3e75eSNarayanan G }
8887fb3e75eSNarayanan G #endif
8898d318a50SLinus Walleij 
8901bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c,
8918d318a50SLinus Walleij 				     enum d40_command command)
8928d318a50SLinus Walleij {
893767a9675SJonas Aaberg 	u32 status;
894767a9675SJonas Aaberg 	int i;
8958d318a50SLinus Walleij 	void __iomem *active_reg;
8968d318a50SLinus Walleij 	int ret = 0;
8978d318a50SLinus Walleij 	unsigned long flags;
8981d392a7bSJonas Aaberg 	u32 wmask;
8998d318a50SLinus Walleij 
9001bdae6f4SNarayanan G 	if (command == D40_DMA_STOP) {
9011bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
9021bdae6f4SNarayanan G 		if (ret)
9031bdae6f4SNarayanan G 			return ret;
9041bdae6f4SNarayanan G 	}
9051bdae6f4SNarayanan G 
9068d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->base->execmd_lock, flags);
9078d318a50SLinus Walleij 
9088d318a50SLinus Walleij 	if (d40c->phy_chan->num % 2 == 0)
9098d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
9108d318a50SLinus Walleij 	else
9118d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
9128d318a50SLinus Walleij 
9138d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
9148d318a50SLinus Walleij 		status = (readl(active_reg) &
9158d318a50SLinus Walleij 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
9168d318a50SLinus Walleij 			D40_CHAN_POS(d40c->phy_chan->num);
9178d318a50SLinus Walleij 
9188d318a50SLinus Walleij 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
9198d318a50SLinus Walleij 			goto done;
9208d318a50SLinus Walleij 	}
9218d318a50SLinus Walleij 
9221d392a7bSJonas Aaberg 	wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
9231d392a7bSJonas Aaberg 	writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
9241d392a7bSJonas Aaberg 	       active_reg);
9258d318a50SLinus Walleij 
9268d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
9278d318a50SLinus Walleij 
9288d318a50SLinus Walleij 		for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
9298d318a50SLinus Walleij 			status = (readl(active_reg) &
9308d318a50SLinus Walleij 				  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
9318d318a50SLinus Walleij 				D40_CHAN_POS(d40c->phy_chan->num);
9328d318a50SLinus Walleij 
9338d318a50SLinus Walleij 			cpu_relax();
9348d318a50SLinus Walleij 			/*
9358d318a50SLinus Walleij 			 * Reduce the number of bus accesses while
9368d318a50SLinus Walleij 			 * waiting for the DMA to suspend.
9378d318a50SLinus Walleij 			 */
9388d318a50SLinus Walleij 			udelay(3);
9398d318a50SLinus Walleij 
9408d318a50SLinus Walleij 			if (status == D40_DMA_STOP ||
9418d318a50SLinus Walleij 			    status == D40_DMA_SUSPENDED)
9428d318a50SLinus Walleij 				break;
9438d318a50SLinus Walleij 		}
9448d318a50SLinus Walleij 
9458d318a50SLinus Walleij 		if (i == D40_SUSPEND_MAX_IT) {
9466db5a8baSRabin Vincent 			chan_err(d40c,
9476db5a8baSRabin Vincent 				"unable to suspend the chl %d (log: %d) status %x\n",
9486db5a8baSRabin Vincent 				d40c->phy_chan->num, d40c->log_num,
9498d318a50SLinus Walleij 				status);
9508d318a50SLinus Walleij 			dump_stack();
9518d318a50SLinus Walleij 			ret = -EBUSY;
9528d318a50SLinus Walleij 		}
9538d318a50SLinus Walleij 
9548d318a50SLinus Walleij 	}
9558d318a50SLinus Walleij done:
9568d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
9578d318a50SLinus Walleij 	return ret;
9588d318a50SLinus Walleij }
9598d318a50SLinus Walleij 
9608d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c)
9618d318a50SLinus Walleij {
9628d318a50SLinus Walleij 	struct d40_desc *d40d;
9637404368cSPer Forlin 	struct d40_desc *_d;
9648d318a50SLinus Walleij 
9658d318a50SLinus Walleij 	/* Release active descriptors */
9668d318a50SLinus Walleij 	while ((d40d = d40_first_active_get(d40c))) {
9678d318a50SLinus Walleij 		d40_desc_remove(d40d);
9688d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
9698d318a50SLinus Walleij 	}
9708d318a50SLinus Walleij 
9718d318a50SLinus Walleij 	/* Release queued descriptors waiting for transfer */
9728d318a50SLinus Walleij 	while ((d40d = d40_first_queued(d40c))) {
9738d318a50SLinus Walleij 		d40_desc_remove(d40d);
9748d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
9758d318a50SLinus Walleij 	}
9768d318a50SLinus Walleij 
977a8f3067bSPer Forlin 	/* Release pending descriptors */
978a8f3067bSPer Forlin 	while ((d40d = d40_first_pending(d40c))) {
979a8f3067bSPer Forlin 		d40_desc_remove(d40d);
980a8f3067bSPer Forlin 		d40_desc_free(d40c, d40d);
981a8f3067bSPer Forlin 	}
9828d318a50SLinus Walleij 
9837404368cSPer Forlin 	/* Release client owned descriptors */
9847404368cSPer Forlin 	if (!list_empty(&d40c->client))
9857404368cSPer Forlin 		list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
9867404368cSPer Forlin 			d40_desc_remove(d40d);
9877404368cSPer Forlin 			d40_desc_free(d40c, d40d);
9887404368cSPer Forlin 		}
9897404368cSPer Forlin 
99082babbb3SPer Forlin 	/* Release descriptors in prepare queue */
99182babbb3SPer Forlin 	if (!list_empty(&d40c->prepare_queue))
99282babbb3SPer Forlin 		list_for_each_entry_safe(d40d, _d,
99382babbb3SPer Forlin 					 &d40c->prepare_queue, node) {
99482babbb3SPer Forlin 			d40_desc_remove(d40d);
99582babbb3SPer Forlin 			d40_desc_free(d40c, d40d);
99682babbb3SPer Forlin 		}
9977404368cSPer Forlin 
9988d318a50SLinus Walleij 	d40c->pending_tx = 0;
9998d318a50SLinus Walleij }
10008d318a50SLinus Walleij 
10011bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c,
10021bdae6f4SNarayanan G 				   enum d40_events event_type, u32 event,
10031bdae6f4SNarayanan G 				   int reg)
1004262d2915SRabin Vincent {
10058ca84687SRabin Vincent 	void __iomem *addr = chan_base(d40c) + reg;
1006262d2915SRabin Vincent 	int tries;
10071bdae6f4SNarayanan G 	u32 status;
1008262d2915SRabin Vincent 
10091bdae6f4SNarayanan G 	switch (event_type) {
10101bdae6f4SNarayanan G 
10111bdae6f4SNarayanan G 	case D40_DEACTIVATE_EVENTLINE:
10121bdae6f4SNarayanan G 
1013262d2915SRabin Vincent 		writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1014262d2915SRabin Vincent 		       | ~D40_EVENTLINE_MASK(event), addr);
10151bdae6f4SNarayanan G 		break;
10161bdae6f4SNarayanan G 
10171bdae6f4SNarayanan G 	case D40_SUSPEND_REQ_EVENTLINE:
10181bdae6f4SNarayanan G 		status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
10191bdae6f4SNarayanan G 			  D40_EVENTLINE_POS(event);
10201bdae6f4SNarayanan G 
10211bdae6f4SNarayanan G 		if (status == D40_DEACTIVATE_EVENTLINE ||
10221bdae6f4SNarayanan G 		    status == D40_SUSPEND_REQ_EVENTLINE)
10231bdae6f4SNarayanan G 			break;
10241bdae6f4SNarayanan G 
10251bdae6f4SNarayanan G 		writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
10261bdae6f4SNarayanan G 		       | ~D40_EVENTLINE_MASK(event), addr);
10271bdae6f4SNarayanan G 
10281bdae6f4SNarayanan G 		for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
10291bdae6f4SNarayanan G 
10301bdae6f4SNarayanan G 			status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
10311bdae6f4SNarayanan G 				  D40_EVENTLINE_POS(event);
10321bdae6f4SNarayanan G 
10331bdae6f4SNarayanan G 			cpu_relax();
10341bdae6f4SNarayanan G 			/*
10351bdae6f4SNarayanan G 			 * Reduce the number of bus accesses while
10361bdae6f4SNarayanan G 			 * waiting for the DMA to suspend.
10371bdae6f4SNarayanan G 			 */
10381bdae6f4SNarayanan G 			udelay(3);
10391bdae6f4SNarayanan G 
10401bdae6f4SNarayanan G 			if (status == D40_DEACTIVATE_EVENTLINE)
10411bdae6f4SNarayanan G 				break;
1042262d2915SRabin Vincent 		}
1043262d2915SRabin Vincent 
10441bdae6f4SNarayanan G 		if (tries == D40_SUSPEND_MAX_IT) {
10451bdae6f4SNarayanan G 			chan_err(d40c,
10461bdae6f4SNarayanan G 				"unable to stop the event_line chl %d (log: %d)"
10471bdae6f4SNarayanan G 				"status %x\n", d40c->phy_chan->num,
10481bdae6f4SNarayanan G 				 d40c->log_num, status);
10491bdae6f4SNarayanan G 		}
10501bdae6f4SNarayanan G 		break;
10511bdae6f4SNarayanan G 
10521bdae6f4SNarayanan G 	case D40_ACTIVATE_EVENTLINE:
1053262d2915SRabin Vincent 	/*
1054262d2915SRabin Vincent 	 * The hardware sometimes doesn't register the enable when src and dst
1055262d2915SRabin Vincent 	 * event lines are active on the same logical channel.  Retry to ensure
1056262d2915SRabin Vincent 	 * it does.  Usually only one retry is sufficient.
1057262d2915SRabin Vincent 	 */
1058262d2915SRabin Vincent 		tries = 100;
1059262d2915SRabin Vincent 		while (--tries) {
10601bdae6f4SNarayanan G 			writel((D40_ACTIVATE_EVENTLINE <<
10611bdae6f4SNarayanan G 				D40_EVENTLINE_POS(event)) |
10621bdae6f4SNarayanan G 				~D40_EVENTLINE_MASK(event), addr);
1063262d2915SRabin Vincent 
1064262d2915SRabin Vincent 			if (readl(addr) & D40_EVENTLINE_MASK(event))
1065262d2915SRabin Vincent 				break;
1066262d2915SRabin Vincent 		}
1067262d2915SRabin Vincent 
1068262d2915SRabin Vincent 		if (tries != 99)
1069262d2915SRabin Vincent 			dev_dbg(chan2dev(d40c),
1070262d2915SRabin Vincent 				"[%s] workaround enable S%cLNK (%d tries)\n",
1071262d2915SRabin Vincent 				__func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1072262d2915SRabin Vincent 				100 - tries);
1073262d2915SRabin Vincent 
1074262d2915SRabin Vincent 		WARN_ON(!tries);
10751bdae6f4SNarayanan G 		break;
10761bdae6f4SNarayanan G 
10771bdae6f4SNarayanan G 	case D40_ROUND_EVENTLINE:
10781bdae6f4SNarayanan G 		BUG();
10791bdae6f4SNarayanan G 		break;
10801bdae6f4SNarayanan G 
10811bdae6f4SNarayanan G 	}
1082262d2915SRabin Vincent }
1083262d2915SRabin Vincent 
10841bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c,
10851bdae6f4SNarayanan G 				 enum d40_events event_type)
10868d318a50SLinus Walleij {
10878d318a50SLinus Walleij 	/* Enable event line connected to device (or memcpy) */
10888d318a50SLinus Walleij 	if ((d40c->dma_cfg.dir ==  STEDMA40_PERIPH_TO_MEM) ||
10898d318a50SLinus Walleij 	    (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) {
10908d318a50SLinus Walleij 		u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
10918d318a50SLinus Walleij 
10921bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
10938d318a50SLinus Walleij 				       D40_CHAN_REG_SSLNK);
10948d318a50SLinus Walleij 	}
1095262d2915SRabin Vincent 
10968d318a50SLinus Walleij 	if (d40c->dma_cfg.dir !=  STEDMA40_PERIPH_TO_MEM) {
10978d318a50SLinus Walleij 		u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
10988d318a50SLinus Walleij 
10991bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
11008d318a50SLinus Walleij 				       D40_CHAN_REG_SDLNK);
11018d318a50SLinus Walleij 	}
11028d318a50SLinus Walleij }
11038d318a50SLinus Walleij 
1104a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c)
11058d318a50SLinus Walleij {
11068ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
1107be8cb7dfSJonas Aaberg 	u32 val;
11088d318a50SLinus Walleij 
11098ca84687SRabin Vincent 	val = readl(chanbase + D40_CHAN_REG_SSLNK);
11108ca84687SRabin Vincent 	val |= readl(chanbase + D40_CHAN_REG_SDLNK);
11118d318a50SLinus Walleij 
1112a5ebca47SJonas Aaberg 	return val;
11138d318a50SLinus Walleij }
11148d318a50SLinus Walleij 
11151bdae6f4SNarayanan G static int
11161bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
11171bdae6f4SNarayanan G {
11181bdae6f4SNarayanan G 	unsigned long flags;
11191bdae6f4SNarayanan G 	int ret = 0;
11201bdae6f4SNarayanan G 	u32 active_status;
11211bdae6f4SNarayanan G 	void __iomem *active_reg;
11221bdae6f4SNarayanan G 
11231bdae6f4SNarayanan G 	if (d40c->phy_chan->num % 2 == 0)
11241bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
11251bdae6f4SNarayanan G 	else
11261bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
11271bdae6f4SNarayanan G 
11281bdae6f4SNarayanan G 
11291bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->phy_chan->lock, flags);
11301bdae6f4SNarayanan G 
11311bdae6f4SNarayanan G 	switch (command) {
11321bdae6f4SNarayanan G 	case D40_DMA_STOP:
11331bdae6f4SNarayanan G 	case D40_DMA_SUSPEND_REQ:
11341bdae6f4SNarayanan G 
11351bdae6f4SNarayanan G 		active_status = (readl(active_reg) &
11361bdae6f4SNarayanan G 				 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
11371bdae6f4SNarayanan G 				 D40_CHAN_POS(d40c->phy_chan->num);
11381bdae6f4SNarayanan G 
11391bdae6f4SNarayanan G 		if (active_status == D40_DMA_RUN)
11401bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
11411bdae6f4SNarayanan G 		else
11421bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
11431bdae6f4SNarayanan G 
11441bdae6f4SNarayanan G 		if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
11451bdae6f4SNarayanan G 			ret = __d40_execute_command_phy(d40c, command);
11461bdae6f4SNarayanan G 
11471bdae6f4SNarayanan G 		break;
11481bdae6f4SNarayanan G 
11491bdae6f4SNarayanan G 	case D40_DMA_RUN:
11501bdae6f4SNarayanan G 
11511bdae6f4SNarayanan G 		d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
11521bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, command);
11531bdae6f4SNarayanan G 		break;
11541bdae6f4SNarayanan G 
11551bdae6f4SNarayanan G 	case D40_DMA_SUSPENDED:
11561bdae6f4SNarayanan G 		BUG();
11571bdae6f4SNarayanan G 		break;
11581bdae6f4SNarayanan G 	}
11591bdae6f4SNarayanan G 
11601bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
11611bdae6f4SNarayanan G 	return ret;
11621bdae6f4SNarayanan G }
11631bdae6f4SNarayanan G 
11641bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c,
11651bdae6f4SNarayanan G 				       enum d40_command command)
11661bdae6f4SNarayanan G {
11671bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
11681bdae6f4SNarayanan G 		return __d40_execute_command_log(d40c, command);
11691bdae6f4SNarayanan G 	else
11701bdae6f4SNarayanan G 		return __d40_execute_command_phy(d40c, command);
11711bdae6f4SNarayanan G }
11721bdae6f4SNarayanan G 
117320a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c)
117420a5b6d0SRabin Vincent {
117520a5b6d0SRabin Vincent 	static const unsigned int phy_map[] = {
117620a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_BASIC_MODE]
117720a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_BASIC,
117820a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_MODULO_MODE]
117920a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_MODULO,
118020a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_DOUBLE_DST_MODE]
118120a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_DOUBLE_DST,
118220a5b6d0SRabin Vincent 	};
118320a5b6d0SRabin Vincent 	static const unsigned int log_map[] = {
118420a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_PHY_DST_LOG]
118520a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
118620a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_PHY]
118720a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
118820a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_LOG]
118920a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
119020a5b6d0SRabin Vincent 	};
119120a5b6d0SRabin Vincent 
1192724a8577SRabin Vincent 	if (chan_is_physical(d40c))
119320a5b6d0SRabin Vincent 		return phy_map[d40c->dma_cfg.mode_opt];
119420a5b6d0SRabin Vincent 	else
119520a5b6d0SRabin Vincent 		return log_map[d40c->dma_cfg.mode_opt];
119620a5b6d0SRabin Vincent }
119720a5b6d0SRabin Vincent 
1198b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c)
11998d318a50SLinus Walleij {
12008d318a50SLinus Walleij 	u32 addr_base;
12018d318a50SLinus Walleij 	u32 var;
12028d318a50SLinus Walleij 
12038d318a50SLinus Walleij 	/* Odd addresses are even addresses + 4 */
12048d318a50SLinus Walleij 	addr_base = (d40c->phy_chan->num % 2) * 4;
12058d318a50SLinus Walleij 	/* Setup channel mode to logical or physical */
1206724a8577SRabin Vincent 	var = ((u32)(chan_is_logical(d40c)) + 1) <<
12078d318a50SLinus Walleij 		D40_CHAN_POS(d40c->phy_chan->num);
12088d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
12098d318a50SLinus Walleij 
12108d318a50SLinus Walleij 	/* Setup operational mode option register */
121120a5b6d0SRabin Vincent 	var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
12128d318a50SLinus Walleij 
12138d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
12148d318a50SLinus Walleij 
1215724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
12168ca84687SRabin Vincent 		int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
12178ca84687SRabin Vincent 			   & D40_SREG_ELEM_LOG_LIDX_MASK;
12188ca84687SRabin Vincent 		void __iomem *chanbase = chan_base(d40c);
12198ca84687SRabin Vincent 
12208d318a50SLinus Walleij 		/* Set default config for CFG reg */
12218ca84687SRabin Vincent 		writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
12228ca84687SRabin Vincent 		writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
12238d318a50SLinus Walleij 
1224b55912c6SJonas Aaberg 		/* Set LIDX for lcla */
12258ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SSELT);
12268ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1227e9f3a49cSRabin Vincent 
1228e9f3a49cSRabin Vincent 		/* Clear LNK which will be used by d40_chan_has_events() */
1229e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SSLNK);
1230e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SDLNK);
12318d318a50SLinus Walleij 	}
12328d318a50SLinus Walleij }
12338d318a50SLinus Walleij 
1234aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c)
1235aa182ae2SJonas Aaberg {
1236aa182ae2SJonas Aaberg 	u32 num_elt;
1237aa182ae2SJonas Aaberg 
1238724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1239aa182ae2SJonas Aaberg 		num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1240aa182ae2SJonas Aaberg 			>> D40_MEM_LCSP2_ECNT_POS;
12418ca84687SRabin Vincent 	else {
12428ca84687SRabin Vincent 		u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
12438ca84687SRabin Vincent 		num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
12448ca84687SRabin Vincent 			  >> D40_SREG_ELEM_PHY_ECNT_POS;
12458ca84687SRabin Vincent 	}
12468ca84687SRabin Vincent 
1247aa182ae2SJonas Aaberg 	return num_elt * (1 << d40c->dma_cfg.dst_info.data_width);
1248aa182ae2SJonas Aaberg }
1249aa182ae2SJonas Aaberg 
1250aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c)
1251aa182ae2SJonas Aaberg {
1252aa182ae2SJonas Aaberg 	bool is_link;
1253aa182ae2SJonas Aaberg 
1254724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1255aa182ae2SJonas Aaberg 		is_link = readl(&d40c->lcpa->lcsp3) &  D40_MEM_LCSP3_DLOS_MASK;
1256aa182ae2SJonas Aaberg 	else
12578ca84687SRabin Vincent 		is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
12588ca84687SRabin Vincent 			  & D40_SREG_LNK_PHYS_LNK_MASK;
12598ca84687SRabin Vincent 
1260aa182ae2SJonas Aaberg 	return is_link;
1261aa182ae2SJonas Aaberg }
1262aa182ae2SJonas Aaberg 
126386eb5fb6SRabin Vincent static int d40_pause(struct d40_chan *d40c)
1264aa182ae2SJonas Aaberg {
1265aa182ae2SJonas Aaberg 	int res = 0;
1266aa182ae2SJonas Aaberg 	unsigned long flags;
1267aa182ae2SJonas Aaberg 
12683ac012afSJonas Aaberg 	if (!d40c->busy)
12693ac012afSJonas Aaberg 		return 0;
12703ac012afSJonas Aaberg 
12717fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
1272aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
1273aa182ae2SJonas Aaberg 
1274aa182ae2SJonas Aaberg 	res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
12751bdae6f4SNarayanan G 
12767fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
12777fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1278aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1279aa182ae2SJonas Aaberg 	return res;
1280aa182ae2SJonas Aaberg }
1281aa182ae2SJonas Aaberg 
128286eb5fb6SRabin Vincent static int d40_resume(struct d40_chan *d40c)
1283aa182ae2SJonas Aaberg {
1284aa182ae2SJonas Aaberg 	int res = 0;
1285aa182ae2SJonas Aaberg 	unsigned long flags;
1286aa182ae2SJonas Aaberg 
12873ac012afSJonas Aaberg 	if (!d40c->busy)
12883ac012afSJonas Aaberg 		return 0;
12893ac012afSJonas Aaberg 
1290aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
12917fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
1292aa182ae2SJonas Aaberg 
1293aa182ae2SJonas Aaberg 	/* If bytes left to transfer or linked tx resume job */
12941bdae6f4SNarayanan G 	if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1295aa182ae2SJonas Aaberg 		res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1296aa182ae2SJonas Aaberg 
12977fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
12987fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1299aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1300aa182ae2SJonas Aaberg 	return res;
1301aa182ae2SJonas Aaberg }
1302aa182ae2SJonas Aaberg 
13038d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
13048d318a50SLinus Walleij {
13058d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(tx->chan,
13068d318a50SLinus Walleij 					     struct d40_chan,
13078d318a50SLinus Walleij 					     chan);
13088d318a50SLinus Walleij 	struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
13098d318a50SLinus Walleij 	unsigned long flags;
1310884485e1SRussell King - ARM Linux 	dma_cookie_t cookie;
13118d318a50SLinus Walleij 
13128d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
1313884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
13148d318a50SLinus Walleij 	d40_desc_queue(d40c, d40d);
13158d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
13168d318a50SLinus Walleij 
1317884485e1SRussell King - ARM Linux 	return cookie;
13188d318a50SLinus Walleij }
13198d318a50SLinus Walleij 
13208d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c)
13218d318a50SLinus Walleij {
13220c32269dSJonas Aaberg 	return d40_channel_execute_command(d40c, D40_DMA_RUN);
13238d318a50SLinus Walleij }
13248d318a50SLinus Walleij 
13258d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
13268d318a50SLinus Walleij {
13278d318a50SLinus Walleij 	struct d40_desc *d40d;
13288d318a50SLinus Walleij 	int err;
13298d318a50SLinus Walleij 
13308d318a50SLinus Walleij 	/* Start queued jobs, if any */
13318d318a50SLinus Walleij 	d40d = d40_first_queued(d40c);
13328d318a50SLinus Walleij 
13338d318a50SLinus Walleij 	if (d40d != NULL) {
13341bdae6f4SNarayanan G 		if (!d40c->busy) {
13358d318a50SLinus Walleij 			d40c->busy = true;
13367fb3e75eSNarayanan G 			pm_runtime_get_sync(d40c->base->dev);
13371bdae6f4SNarayanan G 		}
13387fb3e75eSNarayanan G 
13398d318a50SLinus Walleij 		/* Remove from queue */
13408d318a50SLinus Walleij 		d40_desc_remove(d40d);
13418d318a50SLinus Walleij 
13428d318a50SLinus Walleij 		/* Add to active queue */
13438d318a50SLinus Walleij 		d40_desc_submit(d40c, d40d);
13448d318a50SLinus Walleij 
13458d318a50SLinus Walleij 		/* Initiate DMA job */
13468d318a50SLinus Walleij 		d40_desc_load(d40c, d40d);
13478d318a50SLinus Walleij 
13488d318a50SLinus Walleij 		/* Start dma job */
13498d318a50SLinus Walleij 		err = d40_start(d40c);
13508d318a50SLinus Walleij 
13518d318a50SLinus Walleij 		if (err)
13528d318a50SLinus Walleij 			return NULL;
13538d318a50SLinus Walleij 	}
13548d318a50SLinus Walleij 
13558d318a50SLinus Walleij 	return d40d;
13568d318a50SLinus Walleij }
13578d318a50SLinus Walleij 
13588d318a50SLinus Walleij /* called from interrupt context */
13598d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c)
13608d318a50SLinus Walleij {
13618d318a50SLinus Walleij 	struct d40_desc *d40d;
13628d318a50SLinus Walleij 
13638d318a50SLinus Walleij 	/* Get first active entry from list */
13648d318a50SLinus Walleij 	d40d = d40_first_active_get(d40c);
13658d318a50SLinus Walleij 
13668d318a50SLinus Walleij 	if (d40d == NULL)
13678d318a50SLinus Walleij 		return;
13688d318a50SLinus Walleij 
13690c842b55SRabin Vincent 	if (d40d->cyclic) {
13700c842b55SRabin Vincent 		/*
13710c842b55SRabin Vincent 		 * If this was a paritially loaded list, we need to reloaded
13720c842b55SRabin Vincent 		 * it, and only when the list is completed.  We need to check
13730c842b55SRabin Vincent 		 * for done because the interrupt will hit for every link, and
13740c842b55SRabin Vincent 		 * not just the last one.
13750c842b55SRabin Vincent 		 */
13760c842b55SRabin Vincent 		if (d40d->lli_current < d40d->lli_len
13770c842b55SRabin Vincent 		    && !d40_tx_is_linked(d40c)
13780c842b55SRabin Vincent 		    && !d40_residue(d40c)) {
13790c842b55SRabin Vincent 			d40_lcla_free_all(d40c, d40d);
13800c842b55SRabin Vincent 			d40_desc_load(d40c, d40d);
13810c842b55SRabin Vincent 			(void) d40_start(d40c);
13820c842b55SRabin Vincent 
13830c842b55SRabin Vincent 			if (d40d->lli_current == d40d->lli_len)
13840c842b55SRabin Vincent 				d40d->lli_current = 0;
13850c842b55SRabin Vincent 		}
13860c842b55SRabin Vincent 	} else {
1387698e4732SJonas Aaberg 		d40_lcla_free_all(d40c, d40d);
13888d318a50SLinus Walleij 
1389698e4732SJonas Aaberg 		if (d40d->lli_current < d40d->lli_len) {
13908d318a50SLinus Walleij 			d40_desc_load(d40c, d40d);
13918d318a50SLinus Walleij 			/* Start dma job */
13928d318a50SLinus Walleij 			(void) d40_start(d40c);
13938d318a50SLinus Walleij 			return;
13948d318a50SLinus Walleij 		}
13958d318a50SLinus Walleij 
13968d318a50SLinus Walleij 		if (d40_queue_start(d40c) == NULL)
13978d318a50SLinus Walleij 			d40c->busy = false;
13987fb3e75eSNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
13997fb3e75eSNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
14000c842b55SRabin Vincent 	}
14018d318a50SLinus Walleij 
14028d318a50SLinus Walleij 	d40c->pending_tx++;
14038d318a50SLinus Walleij 	tasklet_schedule(&d40c->tasklet);
14048d318a50SLinus Walleij 
14058d318a50SLinus Walleij }
14068d318a50SLinus Walleij 
14078d318a50SLinus Walleij static void dma_tasklet(unsigned long data)
14088d318a50SLinus Walleij {
14098d318a50SLinus Walleij 	struct d40_chan *d40c = (struct d40_chan *) data;
1410767a9675SJonas Aaberg 	struct d40_desc *d40d;
14118d318a50SLinus Walleij 	unsigned long flags;
14128d318a50SLinus Walleij 	dma_async_tx_callback callback;
14138d318a50SLinus Walleij 	void *callback_param;
14148d318a50SLinus Walleij 
14158d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
14168d318a50SLinus Walleij 
14178d318a50SLinus Walleij 	/* Get first active entry from list */
1418767a9675SJonas Aaberg 	d40d = d40_first_active_get(d40c);
1419767a9675SJonas Aaberg 	if (d40d == NULL)
14208d318a50SLinus Walleij 		goto err;
14218d318a50SLinus Walleij 
14220c842b55SRabin Vincent 	if (!d40d->cyclic)
1423f7fbce07SRussell King - ARM Linux 		dma_cookie_complete(&d40d->txd);
14248d318a50SLinus Walleij 
14258d318a50SLinus Walleij 	/*
14268d318a50SLinus Walleij 	 * If terminating a channel pending_tx is set to zero.
14278d318a50SLinus Walleij 	 * This prevents any finished active jobs to return to the client.
14288d318a50SLinus Walleij 	 */
14298d318a50SLinus Walleij 	if (d40c->pending_tx == 0) {
14308d318a50SLinus Walleij 		spin_unlock_irqrestore(&d40c->lock, flags);
14318d318a50SLinus Walleij 		return;
14328d318a50SLinus Walleij 	}
14338d318a50SLinus Walleij 
14348d318a50SLinus Walleij 	/* Callback to client */
1435767a9675SJonas Aaberg 	callback = d40d->txd.callback;
1436767a9675SJonas Aaberg 	callback_param = d40d->txd.callback_param;
14378d318a50SLinus Walleij 
14380c842b55SRabin Vincent 	if (!d40d->cyclic) {
1439767a9675SJonas Aaberg 		if (async_tx_test_ack(&d40d->txd)) {
1440767a9675SJonas Aaberg 			d40_desc_remove(d40d);
1441767a9675SJonas Aaberg 			d40_desc_free(d40c, d40d);
14428d318a50SLinus Walleij 		} else {
1443767a9675SJonas Aaberg 			if (!d40d->is_in_client_list) {
1444767a9675SJonas Aaberg 				d40_desc_remove(d40d);
1445698e4732SJonas Aaberg 				d40_lcla_free_all(d40c, d40d);
1446767a9675SJonas Aaberg 				list_add_tail(&d40d->node, &d40c->client);
1447767a9675SJonas Aaberg 				d40d->is_in_client_list = true;
14488d318a50SLinus Walleij 			}
14498d318a50SLinus Walleij 		}
14500c842b55SRabin Vincent 	}
14518d318a50SLinus Walleij 
14528d318a50SLinus Walleij 	d40c->pending_tx--;
14538d318a50SLinus Walleij 
14548d318a50SLinus Walleij 	if (d40c->pending_tx)
14558d318a50SLinus Walleij 		tasklet_schedule(&d40c->tasklet);
14568d318a50SLinus Walleij 
14578d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
14588d318a50SLinus Walleij 
1459767a9675SJonas Aaberg 	if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT))
14608d318a50SLinus Walleij 		callback(callback_param);
14618d318a50SLinus Walleij 
14628d318a50SLinus Walleij 	return;
14638d318a50SLinus Walleij 
14648d318a50SLinus Walleij err:
14651bdae6f4SNarayanan G 	/* Rescue manouver if receiving double interrupts */
14668d318a50SLinus Walleij 	if (d40c->pending_tx > 0)
14678d318a50SLinus Walleij 		d40c->pending_tx--;
14688d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
14698d318a50SLinus Walleij }
14708d318a50SLinus Walleij 
14718d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data)
14728d318a50SLinus Walleij {
14738d318a50SLinus Walleij 	static const struct d40_interrupt_lookup il[] = {
14748d318a50SLinus Walleij 		{D40_DREG_LCTIS0, D40_DREG_LCICR0, false,  0},
14758d318a50SLinus Walleij 		{D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
14768d318a50SLinus Walleij 		{D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
14778d318a50SLinus Walleij 		{D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
14788d318a50SLinus Walleij 		{D40_DREG_LCEIS0, D40_DREG_LCICR0, true,   0},
14798d318a50SLinus Walleij 		{D40_DREG_LCEIS1, D40_DREG_LCICR1, true,  32},
14808d318a50SLinus Walleij 		{D40_DREG_LCEIS2, D40_DREG_LCICR2, true,  64},
14818d318a50SLinus Walleij 		{D40_DREG_LCEIS3, D40_DREG_LCICR3, true,  96},
14828d318a50SLinus Walleij 		{D40_DREG_PCTIS,  D40_DREG_PCICR,  false, D40_PHY_CHAN},
14838d318a50SLinus Walleij 		{D40_DREG_PCEIS,  D40_DREG_PCICR,  true,  D40_PHY_CHAN},
14848d318a50SLinus Walleij 	};
14858d318a50SLinus Walleij 
14868d318a50SLinus Walleij 	int i;
14878d318a50SLinus Walleij 	u32 regs[ARRAY_SIZE(il)];
14888d318a50SLinus Walleij 	u32 idx;
14898d318a50SLinus Walleij 	u32 row;
14908d318a50SLinus Walleij 	long chan = -1;
14918d318a50SLinus Walleij 	struct d40_chan *d40c;
14928d318a50SLinus Walleij 	unsigned long flags;
14938d318a50SLinus Walleij 	struct d40_base *base = data;
14948d318a50SLinus Walleij 
14958d318a50SLinus Walleij 	spin_lock_irqsave(&base->interrupt_lock, flags);
14968d318a50SLinus Walleij 
14978d318a50SLinus Walleij 	/* Read interrupt status of both logical and physical channels */
14988d318a50SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(il); i++)
14998d318a50SLinus Walleij 		regs[i] = readl(base->virtbase + il[i].src);
15008d318a50SLinus Walleij 
15018d318a50SLinus Walleij 	for (;;) {
15028d318a50SLinus Walleij 
15038d318a50SLinus Walleij 		chan = find_next_bit((unsigned long *)regs,
15048d318a50SLinus Walleij 				     BITS_PER_LONG * ARRAY_SIZE(il), chan + 1);
15058d318a50SLinus Walleij 
15068d318a50SLinus Walleij 		/* No more set bits found? */
15078d318a50SLinus Walleij 		if (chan == BITS_PER_LONG * ARRAY_SIZE(il))
15088d318a50SLinus Walleij 			break;
15098d318a50SLinus Walleij 
15108d318a50SLinus Walleij 		row = chan / BITS_PER_LONG;
15118d318a50SLinus Walleij 		idx = chan & (BITS_PER_LONG - 1);
15128d318a50SLinus Walleij 
15138d318a50SLinus Walleij 		/* ACK interrupt */
15141b00348dSJonas Aaberg 		writel(1 << idx, base->virtbase + il[row].clr);
15158d318a50SLinus Walleij 
15168d318a50SLinus Walleij 		if (il[row].offset == D40_PHY_CHAN)
15178d318a50SLinus Walleij 			d40c = base->lookup_phy_chans[idx];
15188d318a50SLinus Walleij 		else
15198d318a50SLinus Walleij 			d40c = base->lookup_log_chans[il[row].offset + idx];
15208d318a50SLinus Walleij 		spin_lock(&d40c->lock);
15218d318a50SLinus Walleij 
15228d318a50SLinus Walleij 		if (!il[row].is_error)
15238d318a50SLinus Walleij 			dma_tc_handle(d40c);
15248d318a50SLinus Walleij 		else
15256db5a8baSRabin Vincent 			d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
15266db5a8baSRabin Vincent 				chan, il[row].offset, idx);
15278d318a50SLinus Walleij 
15288d318a50SLinus Walleij 		spin_unlock(&d40c->lock);
15298d318a50SLinus Walleij 	}
15308d318a50SLinus Walleij 
15318d318a50SLinus Walleij 	spin_unlock_irqrestore(&base->interrupt_lock, flags);
15328d318a50SLinus Walleij 
15338d318a50SLinus Walleij 	return IRQ_HANDLED;
15348d318a50SLinus Walleij }
15358d318a50SLinus Walleij 
15368d318a50SLinus Walleij static int d40_validate_conf(struct d40_chan *d40c,
15378d318a50SLinus Walleij 			     struct stedma40_chan_cfg *conf)
15388d318a50SLinus Walleij {
15398d318a50SLinus Walleij 	int res = 0;
15408d318a50SLinus Walleij 	u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type);
15418d318a50SLinus Walleij 	u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type);
154238bdbf02SRabin Vincent 	bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
15438d318a50SLinus Walleij 
15440747c7baSLinus Walleij 	if (!conf->dir) {
15456db5a8baSRabin Vincent 		chan_err(d40c, "Invalid direction.\n");
15460747c7baSLinus Walleij 		res = -EINVAL;
15470747c7baSLinus Walleij 	}
15480747c7baSLinus Walleij 
15490747c7baSLinus Walleij 	if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY &&
15500747c7baSLinus Walleij 	    d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
15510747c7baSLinus Walleij 	    d40c->runtime_addr == 0) {
15520747c7baSLinus Walleij 
15536db5a8baSRabin Vincent 		chan_err(d40c, "Invalid TX channel address (%d)\n",
15546db5a8baSRabin Vincent 			 conf->dst_dev_type);
15550747c7baSLinus Walleij 		res = -EINVAL;
15560747c7baSLinus Walleij 	}
15570747c7baSLinus Walleij 
15580747c7baSLinus Walleij 	if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
15590747c7baSLinus Walleij 	    d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
15600747c7baSLinus Walleij 	    d40c->runtime_addr == 0) {
15616db5a8baSRabin Vincent 		chan_err(d40c, "Invalid RX channel address (%d)\n",
15626db5a8baSRabin Vincent 			conf->src_dev_type);
15630747c7baSLinus Walleij 		res = -EINVAL;
15640747c7baSLinus Walleij 	}
15650747c7baSLinus Walleij 
15660747c7baSLinus Walleij 	if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
15678d318a50SLinus Walleij 	    dst_event_group == STEDMA40_DEV_DST_MEMORY) {
15686db5a8baSRabin Vincent 		chan_err(d40c, "Invalid dst\n");
15698d318a50SLinus Walleij 		res = -EINVAL;
15708d318a50SLinus Walleij 	}
15718d318a50SLinus Walleij 
15720747c7baSLinus Walleij 	if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
15738d318a50SLinus Walleij 	    src_event_group == STEDMA40_DEV_SRC_MEMORY) {
15746db5a8baSRabin Vincent 		chan_err(d40c, "Invalid src\n");
15758d318a50SLinus Walleij 		res = -EINVAL;
15768d318a50SLinus Walleij 	}
15778d318a50SLinus Walleij 
15788d318a50SLinus Walleij 	if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
15798d318a50SLinus Walleij 	    dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
15806db5a8baSRabin Vincent 		chan_err(d40c, "No event line\n");
15818d318a50SLinus Walleij 		res = -EINVAL;
15828d318a50SLinus Walleij 	}
15838d318a50SLinus Walleij 
15848d318a50SLinus Walleij 	if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
15858d318a50SLinus Walleij 	    (src_event_group != dst_event_group)) {
15866db5a8baSRabin Vincent 		chan_err(d40c, "Invalid event group\n");
15878d318a50SLinus Walleij 		res = -EINVAL;
15888d318a50SLinus Walleij 	}
15898d318a50SLinus Walleij 
15908d318a50SLinus Walleij 	if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) {
15918d318a50SLinus Walleij 		/*
15928d318a50SLinus Walleij 		 * DMAC HW supports it. Will be added to this driver,
15938d318a50SLinus Walleij 		 * in case any dma client requires it.
15948d318a50SLinus Walleij 		 */
15956db5a8baSRabin Vincent 		chan_err(d40c, "periph to periph not supported\n");
15968d318a50SLinus Walleij 		res = -EINVAL;
15978d318a50SLinus Walleij 	}
15988d318a50SLinus Walleij 
1599d49278e3SPer Forlin 	if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
1600d49278e3SPer Forlin 	    (1 << conf->src_info.data_width) !=
1601d49278e3SPer Forlin 	    d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
1602d49278e3SPer Forlin 	    (1 << conf->dst_info.data_width)) {
1603d49278e3SPer Forlin 		/*
1604d49278e3SPer Forlin 		 * The DMAC hardware only supports
1605d49278e3SPer Forlin 		 * src (burst x width) == dst (burst x width)
1606d49278e3SPer Forlin 		 */
1607d49278e3SPer Forlin 
16086db5a8baSRabin Vincent 		chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1609d49278e3SPer Forlin 		res = -EINVAL;
1610d49278e3SPer Forlin 	}
1611d49278e3SPer Forlin 
16128d318a50SLinus Walleij 	return res;
16138d318a50SLinus Walleij }
16148d318a50SLinus Walleij 
16155cd326fdSNarayanan G static bool d40_alloc_mask_set(struct d40_phy_res *phy,
16165cd326fdSNarayanan G 			       bool is_src, int log_event_line, bool is_log,
16175cd326fdSNarayanan G 			       bool *first_user)
16188d318a50SLinus Walleij {
16198d318a50SLinus Walleij 	unsigned long flags;
16208d318a50SLinus Walleij 	spin_lock_irqsave(&phy->lock, flags);
16215cd326fdSNarayanan G 
16225cd326fdSNarayanan G 	*first_user = ((phy->allocated_src | phy->allocated_dst)
16235cd326fdSNarayanan G 			== D40_ALLOC_FREE);
16245cd326fdSNarayanan G 
16254aed79b2SMarcin Mielczarczyk 	if (!is_log) {
16268d318a50SLinus Walleij 		/* Physical interrupts are masked per physical full channel */
16278d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE &&
16288d318a50SLinus Walleij 		    phy->allocated_dst == D40_ALLOC_FREE) {
16298d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_PHY;
16308d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_PHY;
16318d318a50SLinus Walleij 			goto found;
16328d318a50SLinus Walleij 		} else
16338d318a50SLinus Walleij 			goto not_found;
16348d318a50SLinus Walleij 	}
16358d318a50SLinus Walleij 
16368d318a50SLinus Walleij 	/* Logical channel */
16378d318a50SLinus Walleij 	if (is_src) {
16388d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_PHY)
16398d318a50SLinus Walleij 			goto not_found;
16408d318a50SLinus Walleij 
16418d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE)
16428d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_LOG_FREE;
16438d318a50SLinus Walleij 
16448d318a50SLinus Walleij 		if (!(phy->allocated_src & (1 << log_event_line))) {
16458d318a50SLinus Walleij 			phy->allocated_src |= 1 << log_event_line;
16468d318a50SLinus Walleij 			goto found;
16478d318a50SLinus Walleij 		} else
16488d318a50SLinus Walleij 			goto not_found;
16498d318a50SLinus Walleij 	} else {
16508d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_PHY)
16518d318a50SLinus Walleij 			goto not_found;
16528d318a50SLinus Walleij 
16538d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_FREE)
16548d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_LOG_FREE;
16558d318a50SLinus Walleij 
16568d318a50SLinus Walleij 		if (!(phy->allocated_dst & (1 << log_event_line))) {
16578d318a50SLinus Walleij 			phy->allocated_dst |= 1 << log_event_line;
16588d318a50SLinus Walleij 			goto found;
16598d318a50SLinus Walleij 		} else
16608d318a50SLinus Walleij 			goto not_found;
16618d318a50SLinus Walleij 	}
16628d318a50SLinus Walleij 
16638d318a50SLinus Walleij not_found:
16648d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
16658d318a50SLinus Walleij 	return false;
16668d318a50SLinus Walleij found:
16678d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
16688d318a50SLinus Walleij 	return true;
16698d318a50SLinus Walleij }
16708d318a50SLinus Walleij 
16718d318a50SLinus Walleij static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
16728d318a50SLinus Walleij 			       int log_event_line)
16738d318a50SLinus Walleij {
16748d318a50SLinus Walleij 	unsigned long flags;
16758d318a50SLinus Walleij 	bool is_free = false;
16768d318a50SLinus Walleij 
16778d318a50SLinus Walleij 	spin_lock_irqsave(&phy->lock, flags);
16788d318a50SLinus Walleij 	if (!log_event_line) {
16798d318a50SLinus Walleij 		phy->allocated_dst = D40_ALLOC_FREE;
16808d318a50SLinus Walleij 		phy->allocated_src = D40_ALLOC_FREE;
16818d318a50SLinus Walleij 		is_free = true;
16828d318a50SLinus Walleij 		goto out;
16838d318a50SLinus Walleij 	}
16848d318a50SLinus Walleij 
16858d318a50SLinus Walleij 	/* Logical channel */
16868d318a50SLinus Walleij 	if (is_src) {
16878d318a50SLinus Walleij 		phy->allocated_src &= ~(1 << log_event_line);
16888d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_LOG_FREE)
16898d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_FREE;
16908d318a50SLinus Walleij 	} else {
16918d318a50SLinus Walleij 		phy->allocated_dst &= ~(1 << log_event_line);
16928d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
16938d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_FREE;
16948d318a50SLinus Walleij 	}
16958d318a50SLinus Walleij 
16968d318a50SLinus Walleij 	is_free = ((phy->allocated_src | phy->allocated_dst) ==
16978d318a50SLinus Walleij 		   D40_ALLOC_FREE);
16988d318a50SLinus Walleij 
16998d318a50SLinus Walleij out:
17008d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
17018d318a50SLinus Walleij 
17028d318a50SLinus Walleij 	return is_free;
17038d318a50SLinus Walleij }
17048d318a50SLinus Walleij 
17055cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
17068d318a50SLinus Walleij {
17078d318a50SLinus Walleij 	int dev_type;
17088d318a50SLinus Walleij 	int event_group;
17098d318a50SLinus Walleij 	int event_line;
17108d318a50SLinus Walleij 	struct d40_phy_res *phys;
17118d318a50SLinus Walleij 	int i;
17128d318a50SLinus Walleij 	int j;
17138d318a50SLinus Walleij 	int log_num;
17148d318a50SLinus Walleij 	bool is_src;
171538bdbf02SRabin Vincent 	bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
17168d318a50SLinus Walleij 
17178d318a50SLinus Walleij 	phys = d40c->base->phy_res;
17188d318a50SLinus Walleij 
17198d318a50SLinus Walleij 	if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
17208d318a50SLinus Walleij 		dev_type = d40c->dma_cfg.src_dev_type;
17218d318a50SLinus Walleij 		log_num = 2 * dev_type;
17228d318a50SLinus Walleij 		is_src = true;
17238d318a50SLinus Walleij 	} else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
17248d318a50SLinus Walleij 		   d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
17258d318a50SLinus Walleij 		/* dst event lines are used for logical memcpy */
17268d318a50SLinus Walleij 		dev_type = d40c->dma_cfg.dst_dev_type;
17278d318a50SLinus Walleij 		log_num = 2 * dev_type + 1;
17288d318a50SLinus Walleij 		is_src = false;
17298d318a50SLinus Walleij 	} else
17308d318a50SLinus Walleij 		return -EINVAL;
17318d318a50SLinus Walleij 
17328d318a50SLinus Walleij 	event_group = D40_TYPE_TO_GROUP(dev_type);
17338d318a50SLinus Walleij 	event_line = D40_TYPE_TO_EVENT(dev_type);
17348d318a50SLinus Walleij 
17358d318a50SLinus Walleij 	if (!is_log) {
17368d318a50SLinus Walleij 		if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
17378d318a50SLinus Walleij 			/* Find physical half channel */
17388d318a50SLinus Walleij 			for (i = 0; i < d40c->base->num_phy_chans; i++) {
17398d318a50SLinus Walleij 
17404aed79b2SMarcin Mielczarczyk 				if (d40_alloc_mask_set(&phys[i], is_src,
17415cd326fdSNarayanan G 						       0, is_log,
17425cd326fdSNarayanan G 						       first_phy_user))
17438d318a50SLinus Walleij 					goto found_phy;
17448d318a50SLinus Walleij 			}
17458d318a50SLinus Walleij 		} else
17468d318a50SLinus Walleij 			for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
17478d318a50SLinus Walleij 				int phy_num = j  + event_group * 2;
17488d318a50SLinus Walleij 				for (i = phy_num; i < phy_num + 2; i++) {
1749508849adSLinus Walleij 					if (d40_alloc_mask_set(&phys[i],
1750508849adSLinus Walleij 							       is_src,
1751508849adSLinus Walleij 							       0,
17525cd326fdSNarayanan G 							       is_log,
17535cd326fdSNarayanan G 							       first_phy_user))
17548d318a50SLinus Walleij 						goto found_phy;
17558d318a50SLinus Walleij 				}
17568d318a50SLinus Walleij 			}
17578d318a50SLinus Walleij 		return -EINVAL;
17588d318a50SLinus Walleij found_phy:
17598d318a50SLinus Walleij 		d40c->phy_chan = &phys[i];
17608d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
17618d318a50SLinus Walleij 		goto out;
17628d318a50SLinus Walleij 	}
17638d318a50SLinus Walleij 	if (dev_type == -1)
17648d318a50SLinus Walleij 		return -EINVAL;
17658d318a50SLinus Walleij 
17668d318a50SLinus Walleij 	/* Find logical channel */
17678d318a50SLinus Walleij 	for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
17688d318a50SLinus Walleij 		int phy_num = j + event_group * 2;
17695cd326fdSNarayanan G 
17705cd326fdSNarayanan G 		if (d40c->dma_cfg.use_fixed_channel) {
17715cd326fdSNarayanan G 			i = d40c->dma_cfg.phy_channel;
17725cd326fdSNarayanan G 
17735cd326fdSNarayanan G 			if ((i != phy_num) && (i != phy_num + 1)) {
17745cd326fdSNarayanan G 				dev_err(chan2dev(d40c),
17755cd326fdSNarayanan G 					"invalid fixed phy channel %d\n", i);
17765cd326fdSNarayanan G 				return -EINVAL;
17775cd326fdSNarayanan G 			}
17785cd326fdSNarayanan G 
17795cd326fdSNarayanan G 			if (d40_alloc_mask_set(&phys[i], is_src, event_line,
17805cd326fdSNarayanan G 					       is_log, first_phy_user))
17815cd326fdSNarayanan G 				goto found_log;
17825cd326fdSNarayanan G 
17835cd326fdSNarayanan G 			dev_err(chan2dev(d40c),
17845cd326fdSNarayanan G 				"could not allocate fixed phy channel %d\n", i);
17855cd326fdSNarayanan G 			return -EINVAL;
17865cd326fdSNarayanan G 		}
17875cd326fdSNarayanan G 
17888d318a50SLinus Walleij 		/*
17898d318a50SLinus Walleij 		 * Spread logical channels across all available physical rather
17908d318a50SLinus Walleij 		 * than pack every logical channel at the first available phy
17918d318a50SLinus Walleij 		 * channels.
17928d318a50SLinus Walleij 		 */
17938d318a50SLinus Walleij 		if (is_src) {
17948d318a50SLinus Walleij 			for (i = phy_num; i < phy_num + 2; i++) {
17958d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
17965cd326fdSNarayanan G 						       event_line, is_log,
17975cd326fdSNarayanan G 						       first_phy_user))
17988d318a50SLinus Walleij 					goto found_log;
17998d318a50SLinus Walleij 			}
18008d318a50SLinus Walleij 		} else {
18018d318a50SLinus Walleij 			for (i = phy_num + 1; i >= phy_num; i--) {
18028d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
18035cd326fdSNarayanan G 						       event_line, is_log,
18045cd326fdSNarayanan G 						       first_phy_user))
18058d318a50SLinus Walleij 					goto found_log;
18068d318a50SLinus Walleij 			}
18078d318a50SLinus Walleij 		}
18088d318a50SLinus Walleij 	}
18098d318a50SLinus Walleij 	return -EINVAL;
18108d318a50SLinus Walleij 
18118d318a50SLinus Walleij found_log:
18128d318a50SLinus Walleij 	d40c->phy_chan = &phys[i];
18138d318a50SLinus Walleij 	d40c->log_num = log_num;
18148d318a50SLinus Walleij out:
18158d318a50SLinus Walleij 
18168d318a50SLinus Walleij 	if (is_log)
18178d318a50SLinus Walleij 		d40c->base->lookup_log_chans[d40c->log_num] = d40c;
18188d318a50SLinus Walleij 	else
18198d318a50SLinus Walleij 		d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
18208d318a50SLinus Walleij 
18218d318a50SLinus Walleij 	return 0;
18228d318a50SLinus Walleij 
18238d318a50SLinus Walleij }
18248d318a50SLinus Walleij 
18258d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c)
18268d318a50SLinus Walleij {
18278d318a50SLinus Walleij 	dma_cap_mask_t cap = d40c->chan.device->cap_mask;
18288d318a50SLinus Walleij 
18298d318a50SLinus Walleij 	if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
18308d318a50SLinus Walleij 		d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log;
18318d318a50SLinus Walleij 		d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY;
18328d318a50SLinus Walleij 		d40c->dma_cfg.dst_dev_type = d40c->base->plat_data->
18338d318a50SLinus Walleij 			memcpy[d40c->chan.chan_id];
18348d318a50SLinus Walleij 
18358d318a50SLinus Walleij 	} else if (dma_has_cap(DMA_MEMCPY, cap) &&
18368d318a50SLinus Walleij 		   dma_has_cap(DMA_SLAVE, cap)) {
18378d318a50SLinus Walleij 		d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
18388d318a50SLinus Walleij 	} else {
18396db5a8baSRabin Vincent 		chan_err(d40c, "No memcpy\n");
18408d318a50SLinus Walleij 		return -EINVAL;
18418d318a50SLinus Walleij 	}
18428d318a50SLinus Walleij 
18438d318a50SLinus Walleij 	return 0;
18448d318a50SLinus Walleij }
18458d318a50SLinus Walleij 
18468d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c)
18478d318a50SLinus Walleij {
18488d318a50SLinus Walleij 
18498d318a50SLinus Walleij 	int res = 0;
1850d181b3a8SJonas Aaberg 	u32 event;
18518d318a50SLinus Walleij 	struct d40_phy_res *phy = d40c->phy_chan;
18528d318a50SLinus Walleij 	bool is_src;
18538d318a50SLinus Walleij 
18548d318a50SLinus Walleij 	/* Terminate all queued and active transfers */
18558d318a50SLinus Walleij 	d40_term_all(d40c);
18568d318a50SLinus Walleij 
18578d318a50SLinus Walleij 	if (phy == NULL) {
18586db5a8baSRabin Vincent 		chan_err(d40c, "phy == null\n");
18598d318a50SLinus Walleij 		return -EINVAL;
18608d318a50SLinus Walleij 	}
18618d318a50SLinus Walleij 
18628d318a50SLinus Walleij 	if (phy->allocated_src == D40_ALLOC_FREE &&
18638d318a50SLinus Walleij 	    phy->allocated_dst == D40_ALLOC_FREE) {
18646db5a8baSRabin Vincent 		chan_err(d40c, "channel already free\n");
18658d318a50SLinus Walleij 		return -EINVAL;
18668d318a50SLinus Walleij 	}
18678d318a50SLinus Walleij 
18688d318a50SLinus Walleij 	if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
18698d318a50SLinus Walleij 	    d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
18708d318a50SLinus Walleij 		event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
18718d318a50SLinus Walleij 		is_src = false;
18728d318a50SLinus Walleij 	} else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
18738d318a50SLinus Walleij 		event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
18748d318a50SLinus Walleij 		is_src = true;
18758d318a50SLinus Walleij 	} else {
18766db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
18778d318a50SLinus Walleij 		return -EINVAL;
18788d318a50SLinus Walleij 	}
18798d318a50SLinus Walleij 
18807fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
18818d318a50SLinus Walleij 	res = d40_channel_execute_command(d40c, D40_DMA_STOP);
18828d318a50SLinus Walleij 	if (res) {
18831bdae6f4SNarayanan G 		chan_err(d40c, "stop failed\n");
18847fb3e75eSNarayanan G 		goto out;
18858d318a50SLinus Walleij 	}
18867fb3e75eSNarayanan G 
18871bdae6f4SNarayanan G 	d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
18881bdae6f4SNarayanan G 
18891bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
18901bdae6f4SNarayanan G 		d40c->base->lookup_log_chans[d40c->log_num] = NULL;
18911bdae6f4SNarayanan G 	else
18921bdae6f4SNarayanan G 		d40c->base->lookup_phy_chans[phy->num] = NULL;
18931bdae6f4SNarayanan G 
18947fb3e75eSNarayanan G 	if (d40c->busy) {
18957fb3e75eSNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
18967fb3e75eSNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
18977fb3e75eSNarayanan G 	}
18987fb3e75eSNarayanan G 
18997fb3e75eSNarayanan G 	d40c->busy = false;
19008d318a50SLinus Walleij 	d40c->phy_chan = NULL;
1901ce2ca125SRabin Vincent 	d40c->configured = false;
19027fb3e75eSNarayanan G out:
19038d318a50SLinus Walleij 
19047fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
19057fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
19067fb3e75eSNarayanan G 	return res;
19078d318a50SLinus Walleij }
19088d318a50SLinus Walleij 
1909a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c)
1910a5ebca47SJonas Aaberg {
19118ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
1912a5ebca47SJonas Aaberg 	bool is_paused = false;
1913a5ebca47SJonas Aaberg 	unsigned long flags;
1914a5ebca47SJonas Aaberg 	void __iomem *active_reg;
1915a5ebca47SJonas Aaberg 	u32 status;
1916a5ebca47SJonas Aaberg 	u32 event;
1917a5ebca47SJonas Aaberg 
1918a5ebca47SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
1919a5ebca47SJonas Aaberg 
1920724a8577SRabin Vincent 	if (chan_is_physical(d40c)) {
1921a5ebca47SJonas Aaberg 		if (d40c->phy_chan->num % 2 == 0)
1922a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
1923a5ebca47SJonas Aaberg 		else
1924a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
1925a5ebca47SJonas Aaberg 
1926a5ebca47SJonas Aaberg 		status = (readl(active_reg) &
1927a5ebca47SJonas Aaberg 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
1928a5ebca47SJonas Aaberg 			D40_CHAN_POS(d40c->phy_chan->num);
1929a5ebca47SJonas Aaberg 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1930a5ebca47SJonas Aaberg 			is_paused = true;
1931a5ebca47SJonas Aaberg 
1932a5ebca47SJonas Aaberg 		goto _exit;
1933a5ebca47SJonas Aaberg 	}
1934a5ebca47SJonas Aaberg 
1935a5ebca47SJonas Aaberg 	if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH ||
19369dbfbd35SJonas Aaberg 	    d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) {
1937a5ebca47SJonas Aaberg 		event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type);
19388ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SDLNK);
19399dbfbd35SJonas Aaberg 	} else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) {
1940a5ebca47SJonas Aaberg 		event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
19418ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SSLNK);
19429dbfbd35SJonas Aaberg 	} else {
19436db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
1944a5ebca47SJonas Aaberg 		goto _exit;
1945a5ebca47SJonas Aaberg 	}
19469dbfbd35SJonas Aaberg 
1947a5ebca47SJonas Aaberg 	status = (status & D40_EVENTLINE_MASK(event)) >>
1948a5ebca47SJonas Aaberg 		D40_EVENTLINE_POS(event);
1949a5ebca47SJonas Aaberg 
1950a5ebca47SJonas Aaberg 	if (status != D40_DMA_RUN)
1951a5ebca47SJonas Aaberg 		is_paused = true;
1952a5ebca47SJonas Aaberg _exit:
1953a5ebca47SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1954a5ebca47SJonas Aaberg 	return is_paused;
1955a5ebca47SJonas Aaberg 
1956a5ebca47SJonas Aaberg }
1957a5ebca47SJonas Aaberg 
1958a5ebca47SJonas Aaberg 
19598d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan)
19608d318a50SLinus Walleij {
19618d318a50SLinus Walleij 	struct d40_chan *d40c =
19628d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
19638d318a50SLinus Walleij 	u32 bytes_left;
19648d318a50SLinus Walleij 	unsigned long flags;
19658d318a50SLinus Walleij 
19668d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
19678d318a50SLinus Walleij 	bytes_left = d40_residue(d40c);
19688d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
19698d318a50SLinus Walleij 
19708d318a50SLinus Walleij 	return bytes_left;
19718d318a50SLinus Walleij }
19728d318a50SLinus Walleij 
19733e3a0763SRabin Vincent static int
19743e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
19753e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
1976822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
1977822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
19783e3a0763SRabin Vincent {
19793e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
19803e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
19813e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
19825ed04b85SRabin Vincent 	int ret;
19833e3a0763SRabin Vincent 
19845ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_src, sg_len,
19855ed04b85SRabin Vincent 				src_dev_addr,
19863e3a0763SRabin Vincent 				desc->lli_log.src,
19873e3a0763SRabin Vincent 				chan->log_def.lcsp1,
19883e3a0763SRabin Vincent 				src_info->data_width,
19893e3a0763SRabin Vincent 				dst_info->data_width);
19903e3a0763SRabin Vincent 
19915ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_dst, sg_len,
19925ed04b85SRabin Vincent 				dst_dev_addr,
19933e3a0763SRabin Vincent 				desc->lli_log.dst,
19943e3a0763SRabin Vincent 				chan->log_def.lcsp3,
19953e3a0763SRabin Vincent 				dst_info->data_width,
19963e3a0763SRabin Vincent 				src_info->data_width);
19973e3a0763SRabin Vincent 
19985ed04b85SRabin Vincent 	return ret < 0 ? ret : 0;
19993e3a0763SRabin Vincent }
20003e3a0763SRabin Vincent 
20013e3a0763SRabin Vincent static int
20023e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
20033e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
2004822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
2005822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
20063e3a0763SRabin Vincent {
20073e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
20083e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
20093e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
20100c842b55SRabin Vincent 	unsigned long flags = 0;
20113e3a0763SRabin Vincent 	int ret;
20123e3a0763SRabin Vincent 
20130c842b55SRabin Vincent 	if (desc->cyclic)
20140c842b55SRabin Vincent 		flags |= LLI_CYCLIC | LLI_TERM_INT;
20150c842b55SRabin Vincent 
20163e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
20173e3a0763SRabin Vincent 				desc->lli_phy.src,
20183e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.src),
20193e3a0763SRabin Vincent 				chan->src_def_cfg,
20200c842b55SRabin Vincent 				src_info, dst_info, flags);
20213e3a0763SRabin Vincent 
20223e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
20233e3a0763SRabin Vincent 				desc->lli_phy.dst,
20243e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.dst),
20253e3a0763SRabin Vincent 				chan->dst_def_cfg,
20260c842b55SRabin Vincent 				dst_info, src_info, flags);
20273e3a0763SRabin Vincent 
20283e3a0763SRabin Vincent 	dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
20293e3a0763SRabin Vincent 				   desc->lli_pool.size, DMA_TO_DEVICE);
20303e3a0763SRabin Vincent 
20313e3a0763SRabin Vincent 	return ret < 0 ? ret : 0;
20323e3a0763SRabin Vincent }
20333e3a0763SRabin Vincent 
20343e3a0763SRabin Vincent 
20355f81158fSRabin Vincent static struct d40_desc *
20365f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
20375f81158fSRabin Vincent 	      unsigned int sg_len, unsigned long dma_flags)
20385f81158fSRabin Vincent {
20395f81158fSRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
20405f81158fSRabin Vincent 	struct d40_desc *desc;
2041dbd88788SRabin Vincent 	int ret;
20425f81158fSRabin Vincent 
20435f81158fSRabin Vincent 	desc = d40_desc_get(chan);
20445f81158fSRabin Vincent 	if (!desc)
20455f81158fSRabin Vincent 		return NULL;
20465f81158fSRabin Vincent 
20475f81158fSRabin Vincent 	desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
20485f81158fSRabin Vincent 					cfg->dst_info.data_width);
20495f81158fSRabin Vincent 	if (desc->lli_len < 0) {
20505f81158fSRabin Vincent 		chan_err(chan, "Unaligned size\n");
2051dbd88788SRabin Vincent 		goto err;
20525f81158fSRabin Vincent 	}
20535f81158fSRabin Vincent 
2054dbd88788SRabin Vincent 	ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2055dbd88788SRabin Vincent 	if (ret < 0) {
2056dbd88788SRabin Vincent 		chan_err(chan, "Could not allocate lli\n");
2057dbd88788SRabin Vincent 		goto err;
2058dbd88788SRabin Vincent 	}
2059dbd88788SRabin Vincent 
2060dbd88788SRabin Vincent 
20615f81158fSRabin Vincent 	desc->lli_current = 0;
20625f81158fSRabin Vincent 	desc->txd.flags = dma_flags;
20635f81158fSRabin Vincent 	desc->txd.tx_submit = d40_tx_submit;
20645f81158fSRabin Vincent 
20655f81158fSRabin Vincent 	dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
20665f81158fSRabin Vincent 
20675f81158fSRabin Vincent 	return desc;
2068dbd88788SRabin Vincent 
2069dbd88788SRabin Vincent err:
2070dbd88788SRabin Vincent 	d40_desc_free(chan, desc);
2071dbd88788SRabin Vincent 	return NULL;
20725f81158fSRabin Vincent }
20735f81158fSRabin Vincent 
2074cade1d30SRabin Vincent static dma_addr_t
2075db8196dfSVinod Koul d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction)
20768d318a50SLinus Walleij {
2077cade1d30SRabin Vincent 	struct stedma40_platform_data *plat = chan->base->plat_data;
2078cade1d30SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
2079711b9ceaSPhilippe Langlais 	dma_addr_t addr = 0;
20808d318a50SLinus Walleij 
2081cade1d30SRabin Vincent 	if (chan->runtime_addr)
2082cade1d30SRabin Vincent 		return chan->runtime_addr;
2083cade1d30SRabin Vincent 
2084db8196dfSVinod Koul 	if (direction == DMA_DEV_TO_MEM)
2085cade1d30SRabin Vincent 		addr = plat->dev_rx[cfg->src_dev_type];
2086db8196dfSVinod Koul 	else if (direction == DMA_MEM_TO_DEV)
2087cade1d30SRabin Vincent 		addr = plat->dev_tx[cfg->dst_dev_type];
2088cade1d30SRabin Vincent 
2089cade1d30SRabin Vincent 	return addr;
20900d0f6b8bSJonas Aaberg }
20910d0f6b8bSJonas Aaberg 
2092cade1d30SRabin Vincent static struct dma_async_tx_descriptor *
2093cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2094cade1d30SRabin Vincent 	    struct scatterlist *sg_dst, unsigned int sg_len,
2095db8196dfSVinod Koul 	    enum dma_transfer_direction direction, unsigned long dma_flags)
2096cade1d30SRabin Vincent {
2097cade1d30SRabin Vincent 	struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2098822c5676SRabin Vincent 	dma_addr_t src_dev_addr = 0;
2099822c5676SRabin Vincent 	dma_addr_t dst_dev_addr = 0;
2100cade1d30SRabin Vincent 	struct d40_desc *desc;
2101cade1d30SRabin Vincent 	unsigned long flags;
2102cade1d30SRabin Vincent 	int ret;
21038d318a50SLinus Walleij 
2104cade1d30SRabin Vincent 	if (!chan->phy_chan) {
2105cade1d30SRabin Vincent 		chan_err(chan, "Cannot prepare unallocated channel\n");
2106cade1d30SRabin Vincent 		return NULL;
2107cade1d30SRabin Vincent 	}
2108cade1d30SRabin Vincent 
21090c842b55SRabin Vincent 
2110cade1d30SRabin Vincent 	spin_lock_irqsave(&chan->lock, flags);
2111cade1d30SRabin Vincent 
2112cade1d30SRabin Vincent 	desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2113cade1d30SRabin Vincent 	if (desc == NULL)
21148d318a50SLinus Walleij 		goto err;
21158d318a50SLinus Walleij 
21160c842b55SRabin Vincent 	if (sg_next(&sg_src[sg_len - 1]) == sg_src)
21170c842b55SRabin Vincent 		desc->cyclic = true;
21180c842b55SRabin Vincent 
21197e426da8SLinus Walleij 	if (direction != DMA_TRANS_NONE) {
2120822c5676SRabin Vincent 		dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
2121822c5676SRabin Vincent 
2122db8196dfSVinod Koul 		if (direction == DMA_DEV_TO_MEM)
2123822c5676SRabin Vincent 			src_dev_addr = dev_addr;
2124db8196dfSVinod Koul 		else if (direction == DMA_MEM_TO_DEV)
2125822c5676SRabin Vincent 			dst_dev_addr = dev_addr;
2126822c5676SRabin Vincent 	}
2127cade1d30SRabin Vincent 
2128cade1d30SRabin Vincent 	if (chan_is_logical(chan))
2129cade1d30SRabin Vincent 		ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2130822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2131cade1d30SRabin Vincent 	else
2132cade1d30SRabin Vincent 		ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2133822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2134cade1d30SRabin Vincent 
2135cade1d30SRabin Vincent 	if (ret) {
2136cade1d30SRabin Vincent 		chan_err(chan, "Failed to prepare %s sg job: %d\n",
2137cade1d30SRabin Vincent 			 chan_is_logical(chan) ? "log" : "phy", ret);
2138cade1d30SRabin Vincent 		goto err;
21398d318a50SLinus Walleij 	}
21408d318a50SLinus Walleij 
214182babbb3SPer Forlin 	/*
214282babbb3SPer Forlin 	 * add descriptor to the prepare queue in order to be able
214382babbb3SPer Forlin 	 * to free them later in terminate_all
214482babbb3SPer Forlin 	 */
214582babbb3SPer Forlin 	list_add_tail(&desc->node, &chan->prepare_queue);
214682babbb3SPer Forlin 
2147cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
21488d318a50SLinus Walleij 
2149cade1d30SRabin Vincent 	return &desc->txd;
2150cade1d30SRabin Vincent 
21518d318a50SLinus Walleij err:
2152cade1d30SRabin Vincent 	if (desc)
2153cade1d30SRabin Vincent 		d40_desc_free(chan, desc);
2154cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
21558d318a50SLinus Walleij 	return NULL;
21568d318a50SLinus Walleij }
21578d318a50SLinus Walleij 
21588d318a50SLinus Walleij bool stedma40_filter(struct dma_chan *chan, void *data)
21598d318a50SLinus Walleij {
21608d318a50SLinus Walleij 	struct stedma40_chan_cfg *info = data;
21618d318a50SLinus Walleij 	struct d40_chan *d40c =
21628d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
21638d318a50SLinus Walleij 	int err;
21648d318a50SLinus Walleij 
21658d318a50SLinus Walleij 	if (data) {
21668d318a50SLinus Walleij 		err = d40_validate_conf(d40c, info);
21678d318a50SLinus Walleij 		if (!err)
21688d318a50SLinus Walleij 			d40c->dma_cfg = *info;
21698d318a50SLinus Walleij 	} else
21708d318a50SLinus Walleij 		err = d40_config_memcpy(d40c);
21718d318a50SLinus Walleij 
2172ce2ca125SRabin Vincent 	if (!err)
2173ce2ca125SRabin Vincent 		d40c->configured = true;
2174ce2ca125SRabin Vincent 
21758d318a50SLinus Walleij 	return err == 0;
21768d318a50SLinus Walleij }
21778d318a50SLinus Walleij EXPORT_SYMBOL(stedma40_filter);
21788d318a50SLinus Walleij 
2179ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2180ac2c0a38SRabin Vincent {
2181ac2c0a38SRabin Vincent 	bool realtime = d40c->dma_cfg.realtime;
2182ac2c0a38SRabin Vincent 	bool highprio = d40c->dma_cfg.high_priority;
2183ac2c0a38SRabin Vincent 	u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
2184ac2c0a38SRabin Vincent 	u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
2185ac2c0a38SRabin Vincent 	u32 event = D40_TYPE_TO_EVENT(dev_type);
2186ac2c0a38SRabin Vincent 	u32 group = D40_TYPE_TO_GROUP(dev_type);
2187ac2c0a38SRabin Vincent 	u32 bit = 1 << event;
2188ac2c0a38SRabin Vincent 
2189ac2c0a38SRabin Vincent 	/* Destination event lines are stored in the upper halfword */
2190ac2c0a38SRabin Vincent 	if (!src)
2191ac2c0a38SRabin Vincent 		bit <<= 16;
2192ac2c0a38SRabin Vincent 
2193ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + prioreg + group * 4);
2194ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + rtreg + group * 4);
2195ac2c0a38SRabin Vincent }
2196ac2c0a38SRabin Vincent 
2197ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c)
2198ac2c0a38SRabin Vincent {
2199ac2c0a38SRabin Vincent 	if (d40c->base->rev < 3)
2200ac2c0a38SRabin Vincent 		return;
2201ac2c0a38SRabin Vincent 
2202ac2c0a38SRabin Vincent 	if ((d40c->dma_cfg.dir ==  STEDMA40_PERIPH_TO_MEM) ||
2203ac2c0a38SRabin Vincent 	    (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2204ac2c0a38SRabin Vincent 		__d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
2205ac2c0a38SRabin Vincent 
2206ac2c0a38SRabin Vincent 	if ((d40c->dma_cfg.dir ==  STEDMA40_MEM_TO_PERIPH) ||
2207ac2c0a38SRabin Vincent 	    (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
2208ac2c0a38SRabin Vincent 		__d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
2209ac2c0a38SRabin Vincent }
2210ac2c0a38SRabin Vincent 
22118d318a50SLinus Walleij /* DMA ENGINE functions */
22128d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan)
22138d318a50SLinus Walleij {
22148d318a50SLinus Walleij 	int err;
22158d318a50SLinus Walleij 	unsigned long flags;
22168d318a50SLinus Walleij 	struct d40_chan *d40c =
22178d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
2218ef1872ecSLinus Walleij 	bool is_free_phy;
22198d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
22208d318a50SLinus Walleij 
2221d3ee98cdSRussell King - ARM Linux 	dma_cookie_init(chan);
22228d318a50SLinus Walleij 
2223ce2ca125SRabin Vincent 	/* If no dma configuration is set use default configuration (memcpy) */
2224ce2ca125SRabin Vincent 	if (!d40c->configured) {
22258d318a50SLinus Walleij 		err = d40_config_memcpy(d40c);
2226ff0b12baSJonas Aaberg 		if (err) {
22276db5a8baSRabin Vincent 			chan_err(d40c, "Failed to configure memcpy channel\n");
2228ff0b12baSJonas Aaberg 			goto fail;
2229ff0b12baSJonas Aaberg 		}
22308d318a50SLinus Walleij 	}
22318d318a50SLinus Walleij 
22325cd326fdSNarayanan G 	err = d40_allocate_channel(d40c, &is_free_phy);
22338d318a50SLinus Walleij 	if (err) {
22346db5a8baSRabin Vincent 		chan_err(d40c, "Failed to allocate channel\n");
22357fb3e75eSNarayanan G 		d40c->configured = false;
2236ff0b12baSJonas Aaberg 		goto fail;
22378d318a50SLinus Walleij 	}
22388d318a50SLinus Walleij 
22397fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
2240ef1872ecSLinus Walleij 	/* Fill in basic CFG register values */
2241ef1872ecSLinus Walleij 	d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
2242724a8577SRabin Vincent 		    &d40c->dst_def_cfg, chan_is_logical(d40c));
2243ef1872ecSLinus Walleij 
2244ac2c0a38SRabin Vincent 	d40_set_prio_realtime(d40c);
2245ac2c0a38SRabin Vincent 
2246724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
2247ef1872ecSLinus Walleij 		d40_log_cfg(&d40c->dma_cfg,
2248ef1872ecSLinus Walleij 			    &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2249ef1872ecSLinus Walleij 
2250ef1872ecSLinus Walleij 		if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM)
2251ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
2252ef1872ecSLinus Walleij 			  d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE;
2253ef1872ecSLinus Walleij 		else
2254ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
2255ef1872ecSLinus Walleij 			  d40c->dma_cfg.dst_dev_type *
2256ef1872ecSLinus Walleij 			  D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
2257ef1872ecSLinus Walleij 	}
2258ef1872ecSLinus Walleij 
22595cd326fdSNarayanan G 	dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
22605cd326fdSNarayanan G 		 chan_is_logical(d40c) ? "logical" : "physical",
22615cd326fdSNarayanan G 		 d40c->phy_chan->num,
22625cd326fdSNarayanan G 		 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
22635cd326fdSNarayanan G 
22645cd326fdSNarayanan G 
2265ef1872ecSLinus Walleij 	/*
2266ef1872ecSLinus Walleij 	 * Only write channel configuration to the DMA if the physical
2267ef1872ecSLinus Walleij 	 * resource is free. In case of multiple logical channels
2268ef1872ecSLinus Walleij 	 * on the same physical resource, only the first write is necessary.
2269ef1872ecSLinus Walleij 	 */
2270b55912c6SJonas Aaberg 	if (is_free_phy)
2271b55912c6SJonas Aaberg 		d40_config_write(d40c);
2272ff0b12baSJonas Aaberg fail:
22737fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
22747fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
22758d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
2276ff0b12baSJonas Aaberg 	return err;
22778d318a50SLinus Walleij }
22788d318a50SLinus Walleij 
22798d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan)
22808d318a50SLinus Walleij {
22818d318a50SLinus Walleij 	struct d40_chan *d40c =
22828d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
22838d318a50SLinus Walleij 	int err;
22848d318a50SLinus Walleij 	unsigned long flags;
22858d318a50SLinus Walleij 
22860d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
22876db5a8baSRabin Vincent 		chan_err(d40c, "Cannot free unallocated channel\n");
22880d0f6b8bSJonas Aaberg 		return;
22890d0f6b8bSJonas Aaberg 	}
22900d0f6b8bSJonas Aaberg 
22910d0f6b8bSJonas Aaberg 
22928d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
22938d318a50SLinus Walleij 
22948d318a50SLinus Walleij 	err = d40_free_dma(d40c);
22958d318a50SLinus Walleij 
22968d318a50SLinus Walleij 	if (err)
22976db5a8baSRabin Vincent 		chan_err(d40c, "Failed to free channel\n");
22988d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
22998d318a50SLinus Walleij }
23008d318a50SLinus Walleij 
23018d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
23028d318a50SLinus Walleij 						       dma_addr_t dst,
23038d318a50SLinus Walleij 						       dma_addr_t src,
23048d318a50SLinus Walleij 						       size_t size,
23052a614340SJonas Aaberg 						       unsigned long dma_flags)
23068d318a50SLinus Walleij {
230795944c6eSRabin Vincent 	struct scatterlist dst_sg;
230895944c6eSRabin Vincent 	struct scatterlist src_sg;
23098d318a50SLinus Walleij 
231095944c6eSRabin Vincent 	sg_init_table(&dst_sg, 1);
231195944c6eSRabin Vincent 	sg_init_table(&src_sg, 1);
23120d0f6b8bSJonas Aaberg 
231395944c6eSRabin Vincent 	sg_dma_address(&dst_sg) = dst;
231495944c6eSRabin Vincent 	sg_dma_address(&src_sg) = src;
23158d318a50SLinus Walleij 
231695944c6eSRabin Vincent 	sg_dma_len(&dst_sg) = size;
231795944c6eSRabin Vincent 	sg_dma_len(&src_sg) = size;
23188d318a50SLinus Walleij 
2319cade1d30SRabin Vincent 	return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
23208d318a50SLinus Walleij }
23218d318a50SLinus Walleij 
23220d688662SIra Snyder static struct dma_async_tx_descriptor *
2323cade1d30SRabin Vincent d40_prep_memcpy_sg(struct dma_chan *chan,
23240d688662SIra Snyder 		   struct scatterlist *dst_sg, unsigned int dst_nents,
23250d688662SIra Snyder 		   struct scatterlist *src_sg, unsigned int src_nents,
23260d688662SIra Snyder 		   unsigned long dma_flags)
23270d688662SIra Snyder {
23280d688662SIra Snyder 	if (dst_nents != src_nents)
23290d688662SIra Snyder 		return NULL;
23300d688662SIra Snyder 
2331cade1d30SRabin Vincent 	return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
233200ac0341SRabin Vincent }
233300ac0341SRabin Vincent 
23348d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
23358d318a50SLinus Walleij 							 struct scatterlist *sgl,
23368d318a50SLinus Walleij 							 unsigned int sg_len,
2337db8196dfSVinod Koul 							 enum dma_transfer_direction direction,
2338185ecb5fSAlexandre Bounine 							 unsigned long dma_flags,
2339185ecb5fSAlexandre Bounine 							 void *context)
23408d318a50SLinus Walleij {
2341db8196dfSVinod Koul 	if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV)
234200ac0341SRabin Vincent 		return NULL;
234300ac0341SRabin Vincent 
2344cade1d30SRabin Vincent 	return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
23458d318a50SLinus Walleij }
23468d318a50SLinus Walleij 
23470c842b55SRabin Vincent static struct dma_async_tx_descriptor *
23480c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
23490c842b55SRabin Vincent 		     size_t buf_len, size_t period_len,
2350185ecb5fSAlexandre Bounine 		     enum dma_transfer_direction direction, void *context)
23510c842b55SRabin Vincent {
23520c842b55SRabin Vincent 	unsigned int periods = buf_len / period_len;
23530c842b55SRabin Vincent 	struct dma_async_tx_descriptor *txd;
23540c842b55SRabin Vincent 	struct scatterlist *sg;
23550c842b55SRabin Vincent 	int i;
23560c842b55SRabin Vincent 
235779ca7ec3SRobert Marklund 	sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
23580c842b55SRabin Vincent 	for (i = 0; i < periods; i++) {
23590c842b55SRabin Vincent 		sg_dma_address(&sg[i]) = dma_addr;
23600c842b55SRabin Vincent 		sg_dma_len(&sg[i]) = period_len;
23610c842b55SRabin Vincent 		dma_addr += period_len;
23620c842b55SRabin Vincent 	}
23630c842b55SRabin Vincent 
23640c842b55SRabin Vincent 	sg[periods].offset = 0;
2365*fdaf9c4bSLars-Peter Clausen 	sg_dma_len(&sg[periods]) = 0;
23660c842b55SRabin Vincent 	sg[periods].page_link =
23670c842b55SRabin Vincent 		((unsigned long)sg | 0x01) & ~0x02;
23680c842b55SRabin Vincent 
23690c842b55SRabin Vincent 	txd = d40_prep_sg(chan, sg, sg, periods, direction,
23700c842b55SRabin Vincent 			  DMA_PREP_INTERRUPT);
23710c842b55SRabin Vincent 
23720c842b55SRabin Vincent 	kfree(sg);
23730c842b55SRabin Vincent 
23740c842b55SRabin Vincent 	return txd;
23750c842b55SRabin Vincent }
23760c842b55SRabin Vincent 
23778d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan,
23788d318a50SLinus Walleij 				     dma_cookie_t cookie,
23798d318a50SLinus Walleij 				     struct dma_tx_state *txstate)
23808d318a50SLinus Walleij {
23818d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
238296a2af41SRussell King - ARM Linux 	enum dma_status ret;
23838d318a50SLinus Walleij 
23840d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
23856db5a8baSRabin Vincent 		chan_err(d40c, "Cannot read status of unallocated channel\n");
23860d0f6b8bSJonas Aaberg 		return -EINVAL;
23870d0f6b8bSJonas Aaberg 	}
23880d0f6b8bSJonas Aaberg 
238996a2af41SRussell King - ARM Linux 	ret = dma_cookie_status(chan, cookie, txstate);
239096a2af41SRussell King - ARM Linux 	if (ret != DMA_SUCCESS)
239196a2af41SRussell King - ARM Linux 		dma_set_residue(txstate, stedma40_residue(chan));
23928d318a50SLinus Walleij 
2393a5ebca47SJonas Aaberg 	if (d40_is_paused(d40c))
2394a5ebca47SJonas Aaberg 		ret = DMA_PAUSED;
23958d318a50SLinus Walleij 
23968d318a50SLinus Walleij 	return ret;
23978d318a50SLinus Walleij }
23988d318a50SLinus Walleij 
23998d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan)
24008d318a50SLinus Walleij {
24018d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
24028d318a50SLinus Walleij 	unsigned long flags;
24038d318a50SLinus Walleij 
24040d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
24056db5a8baSRabin Vincent 		chan_err(d40c, "Channel is not allocated!\n");
24060d0f6b8bSJonas Aaberg 		return;
24070d0f6b8bSJonas Aaberg 	}
24080d0f6b8bSJonas Aaberg 
24098d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
24108d318a50SLinus Walleij 
2411a8f3067bSPer Forlin 	list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2412a8f3067bSPer Forlin 
2413a8f3067bSPer Forlin 	/* Busy means that queued jobs are already being processed */
24148d318a50SLinus Walleij 	if (!d40c->busy)
24158d318a50SLinus Walleij 		(void) d40_queue_start(d40c);
24168d318a50SLinus Walleij 
24178d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
24188d318a50SLinus Walleij }
24198d318a50SLinus Walleij 
24201bdae6f4SNarayanan G static void d40_terminate_all(struct dma_chan *chan)
24211bdae6f4SNarayanan G {
24221bdae6f4SNarayanan G 	unsigned long flags;
24231bdae6f4SNarayanan G 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
24241bdae6f4SNarayanan G 	int ret;
24251bdae6f4SNarayanan G 
24261bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->lock, flags);
24271bdae6f4SNarayanan G 
24281bdae6f4SNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
24291bdae6f4SNarayanan G 	ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
24301bdae6f4SNarayanan G 	if (ret)
24311bdae6f4SNarayanan G 		chan_err(d40c, "Failed to stop channel\n");
24321bdae6f4SNarayanan G 
24331bdae6f4SNarayanan G 	d40_term_all(d40c);
24341bdae6f4SNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
24351bdae6f4SNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
24361bdae6f4SNarayanan G 	if (d40c->busy) {
24371bdae6f4SNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
24381bdae6f4SNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
24391bdae6f4SNarayanan G 	}
24401bdae6f4SNarayanan G 	d40c->busy = false;
24411bdae6f4SNarayanan G 
24421bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->lock, flags);
24431bdae6f4SNarayanan G }
24441bdae6f4SNarayanan G 
244598ca5289SRabin Vincent static int
244698ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c,
244798ca5289SRabin Vincent 			    struct stedma40_half_channel_info *info,
244898ca5289SRabin Vincent 			    enum dma_slave_buswidth width,
244998ca5289SRabin Vincent 			    u32 maxburst)
245098ca5289SRabin Vincent {
245198ca5289SRabin Vincent 	enum stedma40_periph_data_width addr_width;
245298ca5289SRabin Vincent 	int psize;
245398ca5289SRabin Vincent 
245498ca5289SRabin Vincent 	switch (width) {
245598ca5289SRabin Vincent 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
245698ca5289SRabin Vincent 		addr_width = STEDMA40_BYTE_WIDTH;
245798ca5289SRabin Vincent 		break;
245898ca5289SRabin Vincent 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
245998ca5289SRabin Vincent 		addr_width = STEDMA40_HALFWORD_WIDTH;
246098ca5289SRabin Vincent 		break;
246198ca5289SRabin Vincent 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
246298ca5289SRabin Vincent 		addr_width = STEDMA40_WORD_WIDTH;
246398ca5289SRabin Vincent 		break;
246498ca5289SRabin Vincent 	case DMA_SLAVE_BUSWIDTH_8_BYTES:
246598ca5289SRabin Vincent 		addr_width = STEDMA40_DOUBLEWORD_WIDTH;
246698ca5289SRabin Vincent 		break;
246798ca5289SRabin Vincent 	default:
246898ca5289SRabin Vincent 		dev_err(d40c->base->dev,
246998ca5289SRabin Vincent 			"illegal peripheral address width "
247098ca5289SRabin Vincent 			"requested (%d)\n",
247198ca5289SRabin Vincent 			width);
247298ca5289SRabin Vincent 		return -EINVAL;
247398ca5289SRabin Vincent 	}
247498ca5289SRabin Vincent 
247598ca5289SRabin Vincent 	if (chan_is_logical(d40c)) {
247698ca5289SRabin Vincent 		if (maxburst >= 16)
247798ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_16;
247898ca5289SRabin Vincent 		else if (maxburst >= 8)
247998ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_8;
248098ca5289SRabin Vincent 		else if (maxburst >= 4)
248198ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_4;
248298ca5289SRabin Vincent 		else
248398ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_1;
248498ca5289SRabin Vincent 	} else {
248598ca5289SRabin Vincent 		if (maxburst >= 16)
248698ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_16;
248798ca5289SRabin Vincent 		else if (maxburst >= 8)
248898ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_8;
248998ca5289SRabin Vincent 		else if (maxburst >= 4)
249098ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_4;
249198ca5289SRabin Vincent 		else
249298ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_1;
249398ca5289SRabin Vincent 	}
249498ca5289SRabin Vincent 
249598ca5289SRabin Vincent 	info->data_width = addr_width;
249698ca5289SRabin Vincent 	info->psize = psize;
249798ca5289SRabin Vincent 	info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
249898ca5289SRabin Vincent 
249998ca5289SRabin Vincent 	return 0;
250098ca5289SRabin Vincent }
250198ca5289SRabin Vincent 
250295e1400fSLinus Walleij /* Runtime reconfiguration extension */
250398ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan,
250495e1400fSLinus Walleij 				  struct dma_slave_config *config)
250595e1400fSLinus Walleij {
250695e1400fSLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
250795e1400fSLinus Walleij 	struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
250898ca5289SRabin Vincent 	enum dma_slave_buswidth src_addr_width, dst_addr_width;
250995e1400fSLinus Walleij 	dma_addr_t config_addr;
251098ca5289SRabin Vincent 	u32 src_maxburst, dst_maxburst;
251198ca5289SRabin Vincent 	int ret;
251298ca5289SRabin Vincent 
251398ca5289SRabin Vincent 	src_addr_width = config->src_addr_width;
251498ca5289SRabin Vincent 	src_maxburst = config->src_maxburst;
251598ca5289SRabin Vincent 	dst_addr_width = config->dst_addr_width;
251698ca5289SRabin Vincent 	dst_maxburst = config->dst_maxburst;
251795e1400fSLinus Walleij 
2518db8196dfSVinod Koul 	if (config->direction == DMA_DEV_TO_MEM) {
251995e1400fSLinus Walleij 		dma_addr_t dev_addr_rx =
252095e1400fSLinus Walleij 			d40c->base->plat_data->dev_rx[cfg->src_dev_type];
252195e1400fSLinus Walleij 
252295e1400fSLinus Walleij 		config_addr = config->src_addr;
252395e1400fSLinus Walleij 		if (dev_addr_rx)
252495e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
252595e1400fSLinus Walleij 				"channel has a pre-wired RX address %08x "
252695e1400fSLinus Walleij 				"overriding with %08x\n",
252795e1400fSLinus Walleij 				dev_addr_rx, config_addr);
252895e1400fSLinus Walleij 		if (cfg->dir != STEDMA40_PERIPH_TO_MEM)
252995e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
253095e1400fSLinus Walleij 				"channel was not configured for peripheral "
253195e1400fSLinus Walleij 				"to memory transfer (%d) overriding\n",
253295e1400fSLinus Walleij 				cfg->dir);
253395e1400fSLinus Walleij 		cfg->dir = STEDMA40_PERIPH_TO_MEM;
253495e1400fSLinus Walleij 
253598ca5289SRabin Vincent 		/* Configure the memory side */
253698ca5289SRabin Vincent 		if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
253798ca5289SRabin Vincent 			dst_addr_width = src_addr_width;
253898ca5289SRabin Vincent 		if (dst_maxburst == 0)
253998ca5289SRabin Vincent 			dst_maxburst = src_maxburst;
254095e1400fSLinus Walleij 
2541db8196dfSVinod Koul 	} else if (config->direction == DMA_MEM_TO_DEV) {
254295e1400fSLinus Walleij 		dma_addr_t dev_addr_tx =
254395e1400fSLinus Walleij 			d40c->base->plat_data->dev_tx[cfg->dst_dev_type];
254495e1400fSLinus Walleij 
254595e1400fSLinus Walleij 		config_addr = config->dst_addr;
254695e1400fSLinus Walleij 		if (dev_addr_tx)
254795e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
254895e1400fSLinus Walleij 				"channel has a pre-wired TX address %08x "
254995e1400fSLinus Walleij 				"overriding with %08x\n",
255095e1400fSLinus Walleij 				dev_addr_tx, config_addr);
255195e1400fSLinus Walleij 		if (cfg->dir != STEDMA40_MEM_TO_PERIPH)
255295e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
255395e1400fSLinus Walleij 				"channel was not configured for memory "
255495e1400fSLinus Walleij 				"to peripheral transfer (%d) overriding\n",
255595e1400fSLinus Walleij 				cfg->dir);
255695e1400fSLinus Walleij 		cfg->dir = STEDMA40_MEM_TO_PERIPH;
255795e1400fSLinus Walleij 
255898ca5289SRabin Vincent 		/* Configure the memory side */
255998ca5289SRabin Vincent 		if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
256098ca5289SRabin Vincent 			src_addr_width = dst_addr_width;
256198ca5289SRabin Vincent 		if (src_maxburst == 0)
256298ca5289SRabin Vincent 			src_maxburst = dst_maxburst;
256395e1400fSLinus Walleij 	} else {
256495e1400fSLinus Walleij 		dev_err(d40c->base->dev,
256595e1400fSLinus Walleij 			"unrecognized channel direction %d\n",
256695e1400fSLinus Walleij 			config->direction);
256798ca5289SRabin Vincent 		return -EINVAL;
256895e1400fSLinus Walleij 	}
256995e1400fSLinus Walleij 
257098ca5289SRabin Vincent 	if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
257195e1400fSLinus Walleij 		dev_err(d40c->base->dev,
257298ca5289SRabin Vincent 			"src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
257398ca5289SRabin Vincent 			src_maxburst,
257498ca5289SRabin Vincent 			src_addr_width,
257598ca5289SRabin Vincent 			dst_maxburst,
257698ca5289SRabin Vincent 			dst_addr_width);
257798ca5289SRabin Vincent 		return -EINVAL;
257895e1400fSLinus Walleij 	}
257995e1400fSLinus Walleij 
258098ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
258198ca5289SRabin Vincent 					  src_addr_width,
258298ca5289SRabin Vincent 					  src_maxburst);
258398ca5289SRabin Vincent 	if (ret)
258498ca5289SRabin Vincent 		return ret;
258595e1400fSLinus Walleij 
258698ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
258798ca5289SRabin Vincent 					  dst_addr_width,
258898ca5289SRabin Vincent 					  dst_maxburst);
258998ca5289SRabin Vincent 	if (ret)
259098ca5289SRabin Vincent 		return ret;
259195e1400fSLinus Walleij 
2592a59670a4SPer Forlin 	/* Fill in register values */
2593724a8577SRabin Vincent 	if (chan_is_logical(d40c))
2594a59670a4SPer Forlin 		d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2595a59670a4SPer Forlin 	else
2596a59670a4SPer Forlin 		d40_phy_cfg(cfg, &d40c->src_def_cfg,
2597a59670a4SPer Forlin 			    &d40c->dst_def_cfg, false);
2598a59670a4SPer Forlin 
259995e1400fSLinus Walleij 	/* These settings will take precedence later */
260095e1400fSLinus Walleij 	d40c->runtime_addr = config_addr;
260195e1400fSLinus Walleij 	d40c->runtime_direction = config->direction;
260295e1400fSLinus Walleij 	dev_dbg(d40c->base->dev,
260398ca5289SRabin Vincent 		"configured channel %s for %s, data width %d/%d, "
260498ca5289SRabin Vincent 		"maxburst %d/%d elements, LE, no flow control\n",
260595e1400fSLinus Walleij 		dma_chan_name(chan),
2606db8196dfSVinod Koul 		(config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
260798ca5289SRabin Vincent 		src_addr_width, dst_addr_width,
260898ca5289SRabin Vincent 		src_maxburst, dst_maxburst);
260998ca5289SRabin Vincent 
261098ca5289SRabin Vincent 	return 0;
261195e1400fSLinus Walleij }
261295e1400fSLinus Walleij 
261305827630SLinus Walleij static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
261405827630SLinus Walleij 		       unsigned long arg)
26158d318a50SLinus Walleij {
26168d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26178d318a50SLinus Walleij 
26180d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
26196db5a8baSRabin Vincent 		chan_err(d40c, "Channel is not allocated!\n");
26200d0f6b8bSJonas Aaberg 		return -EINVAL;
26210d0f6b8bSJonas Aaberg 	}
26220d0f6b8bSJonas Aaberg 
26238d318a50SLinus Walleij 	switch (cmd) {
26248d318a50SLinus Walleij 	case DMA_TERMINATE_ALL:
26251bdae6f4SNarayanan G 		d40_terminate_all(chan);
26261bdae6f4SNarayanan G 		return 0;
26278d318a50SLinus Walleij 	case DMA_PAUSE:
262886eb5fb6SRabin Vincent 		return d40_pause(d40c);
26298d318a50SLinus Walleij 	case DMA_RESUME:
263086eb5fb6SRabin Vincent 		return d40_resume(d40c);
263195e1400fSLinus Walleij 	case DMA_SLAVE_CONFIG:
263298ca5289SRabin Vincent 		return d40_set_runtime_config(chan,
263395e1400fSLinus Walleij 			(struct dma_slave_config *) arg);
263495e1400fSLinus Walleij 	default:
263595e1400fSLinus Walleij 		break;
26368d318a50SLinus Walleij 	}
26378d318a50SLinus Walleij 
26388d318a50SLinus Walleij 	/* Other commands are unimplemented */
26398d318a50SLinus Walleij 	return -ENXIO;
26408d318a50SLinus Walleij }
26418d318a50SLinus Walleij 
26428d318a50SLinus Walleij /* Initialization functions */
26438d318a50SLinus Walleij 
26448d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
26458d318a50SLinus Walleij 				 struct d40_chan *chans, int offset,
26468d318a50SLinus Walleij 				 int num_chans)
26478d318a50SLinus Walleij {
26488d318a50SLinus Walleij 	int i = 0;
26498d318a50SLinus Walleij 	struct d40_chan *d40c;
26508d318a50SLinus Walleij 
26518d318a50SLinus Walleij 	INIT_LIST_HEAD(&dma->channels);
26528d318a50SLinus Walleij 
26538d318a50SLinus Walleij 	for (i = offset; i < offset + num_chans; i++) {
26548d318a50SLinus Walleij 		d40c = &chans[i];
26558d318a50SLinus Walleij 		d40c->base = base;
26568d318a50SLinus Walleij 		d40c->chan.device = dma;
26578d318a50SLinus Walleij 
26588d318a50SLinus Walleij 		spin_lock_init(&d40c->lock);
26598d318a50SLinus Walleij 
26608d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
26618d318a50SLinus Walleij 
26628d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->active);
26638d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->queue);
2664a8f3067bSPer Forlin 		INIT_LIST_HEAD(&d40c->pending_queue);
26658d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->client);
266682babbb3SPer Forlin 		INIT_LIST_HEAD(&d40c->prepare_queue);
26678d318a50SLinus Walleij 
26688d318a50SLinus Walleij 		tasklet_init(&d40c->tasklet, dma_tasklet,
26698d318a50SLinus Walleij 			     (unsigned long) d40c);
26708d318a50SLinus Walleij 
26718d318a50SLinus Walleij 		list_add_tail(&d40c->chan.device_node,
26728d318a50SLinus Walleij 			      &dma->channels);
26738d318a50SLinus Walleij 	}
26748d318a50SLinus Walleij }
26758d318a50SLinus Walleij 
26767ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
26777ad74a7cSRabin Vincent {
26787ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
26797ad74a7cSRabin Vincent 		dev->device_prep_slave_sg = d40_prep_slave_sg;
26807ad74a7cSRabin Vincent 
26817ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
26827ad74a7cSRabin Vincent 		dev->device_prep_dma_memcpy = d40_prep_memcpy;
26837ad74a7cSRabin Vincent 
26847ad74a7cSRabin Vincent 		/*
26857ad74a7cSRabin Vincent 		 * This controller can only access address at even
26867ad74a7cSRabin Vincent 		 * 32bit boundaries, i.e. 2^2
26877ad74a7cSRabin Vincent 		 */
26887ad74a7cSRabin Vincent 		dev->copy_align = 2;
26897ad74a7cSRabin Vincent 	}
26907ad74a7cSRabin Vincent 
26917ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_SG, dev->cap_mask))
26927ad74a7cSRabin Vincent 		dev->device_prep_dma_sg = d40_prep_memcpy_sg;
26937ad74a7cSRabin Vincent 
26940c842b55SRabin Vincent 	if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
26950c842b55SRabin Vincent 		dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
26960c842b55SRabin Vincent 
26977ad74a7cSRabin Vincent 	dev->device_alloc_chan_resources = d40_alloc_chan_resources;
26987ad74a7cSRabin Vincent 	dev->device_free_chan_resources = d40_free_chan_resources;
26997ad74a7cSRabin Vincent 	dev->device_issue_pending = d40_issue_pending;
27007ad74a7cSRabin Vincent 	dev->device_tx_status = d40_tx_status;
27017ad74a7cSRabin Vincent 	dev->device_control = d40_control;
27027ad74a7cSRabin Vincent 	dev->dev = base->dev;
27037ad74a7cSRabin Vincent }
27047ad74a7cSRabin Vincent 
27058d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base,
27068d318a50SLinus Walleij 				     int num_reserved_chans)
27078d318a50SLinus Walleij {
27088d318a50SLinus Walleij 	int err ;
27098d318a50SLinus Walleij 
27108d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_slave, base->log_chans,
27118d318a50SLinus Walleij 		      0, base->num_log_chans);
27128d318a50SLinus Walleij 
27138d318a50SLinus Walleij 	dma_cap_zero(base->dma_slave.cap_mask);
27148d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
27150c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
27168d318a50SLinus Walleij 
27177ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_slave);
27188d318a50SLinus Walleij 
27198d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_slave);
27208d318a50SLinus Walleij 
27218d318a50SLinus Walleij 	if (err) {
27226db5a8baSRabin Vincent 		d40_err(base->dev, "Failed to register slave channels\n");
27238d318a50SLinus Walleij 		goto failure1;
27248d318a50SLinus Walleij 	}
27258d318a50SLinus Walleij 
27268d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_memcpy, base->log_chans,
27278d318a50SLinus Walleij 		      base->num_log_chans, base->plat_data->memcpy_len);
27288d318a50SLinus Walleij 
27298d318a50SLinus Walleij 	dma_cap_zero(base->dma_memcpy.cap_mask);
27308d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
27317ad74a7cSRabin Vincent 	dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
27328d318a50SLinus Walleij 
27337ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_memcpy);
27348d318a50SLinus Walleij 
27358d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_memcpy);
27368d318a50SLinus Walleij 
27378d318a50SLinus Walleij 	if (err) {
27386db5a8baSRabin Vincent 		d40_err(base->dev,
27396db5a8baSRabin Vincent 			"Failed to regsiter memcpy only channels\n");
27408d318a50SLinus Walleij 		goto failure2;
27418d318a50SLinus Walleij 	}
27428d318a50SLinus Walleij 
27438d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_both, base->phy_chans,
27448d318a50SLinus Walleij 		      0, num_reserved_chans);
27458d318a50SLinus Walleij 
27468d318a50SLinus Walleij 	dma_cap_zero(base->dma_both.cap_mask);
27478d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
27488d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
27497ad74a7cSRabin Vincent 	dma_cap_set(DMA_SG, base->dma_both.cap_mask);
27500c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
27518d318a50SLinus Walleij 
27527ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_both);
27538d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_both);
27548d318a50SLinus Walleij 
27558d318a50SLinus Walleij 	if (err) {
27566db5a8baSRabin Vincent 		d40_err(base->dev,
27576db5a8baSRabin Vincent 			"Failed to register logical and physical capable channels\n");
27588d318a50SLinus Walleij 		goto failure3;
27598d318a50SLinus Walleij 	}
27608d318a50SLinus Walleij 	return 0;
27618d318a50SLinus Walleij failure3:
27628d318a50SLinus Walleij 	dma_async_device_unregister(&base->dma_memcpy);
27638d318a50SLinus Walleij failure2:
27648d318a50SLinus Walleij 	dma_async_device_unregister(&base->dma_slave);
27658d318a50SLinus Walleij failure1:
27668d318a50SLinus Walleij 	return err;
27678d318a50SLinus Walleij }
27688d318a50SLinus Walleij 
27697fb3e75eSNarayanan G /* Suspend resume functionality */
27707fb3e75eSNarayanan G #ifdef CONFIG_PM
27717fb3e75eSNarayanan G static int dma40_pm_suspend(struct device *dev)
27727fb3e75eSNarayanan G {
277328c7a19dSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
277428c7a19dSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
277528c7a19dSNarayanan G 	int ret = 0;
27767fb3e75eSNarayanan G 	if (!pm_runtime_suspended(dev))
27777fb3e75eSNarayanan G 		return -EBUSY;
27787fb3e75eSNarayanan G 
277928c7a19dSNarayanan G 	if (base->lcpa_regulator)
278028c7a19dSNarayanan G 		ret = regulator_disable(base->lcpa_regulator);
278128c7a19dSNarayanan G 	return ret;
27827fb3e75eSNarayanan G }
27837fb3e75eSNarayanan G 
27847fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev)
27857fb3e75eSNarayanan G {
27867fb3e75eSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
27877fb3e75eSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
27887fb3e75eSNarayanan G 
27897fb3e75eSNarayanan G 	d40_save_restore_registers(base, true);
27907fb3e75eSNarayanan G 
27917fb3e75eSNarayanan G 	/* Don't disable/enable clocks for v1 due to HW bugs */
27927fb3e75eSNarayanan G 	if (base->rev != 1)
27937fb3e75eSNarayanan G 		writel_relaxed(base->gcc_pwr_off_mask,
27947fb3e75eSNarayanan G 			       base->virtbase + D40_DREG_GCC);
27957fb3e75eSNarayanan G 
27967fb3e75eSNarayanan G 	return 0;
27977fb3e75eSNarayanan G }
27987fb3e75eSNarayanan G 
27997fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev)
28007fb3e75eSNarayanan G {
28017fb3e75eSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
28027fb3e75eSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
28037fb3e75eSNarayanan G 
28047fb3e75eSNarayanan G 	if (base->initialized)
28057fb3e75eSNarayanan G 		d40_save_restore_registers(base, false);
28067fb3e75eSNarayanan G 
28077fb3e75eSNarayanan G 	writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
28087fb3e75eSNarayanan G 		       base->virtbase + D40_DREG_GCC);
28097fb3e75eSNarayanan G 	return 0;
28107fb3e75eSNarayanan G }
28117fb3e75eSNarayanan G 
281228c7a19dSNarayanan G static int dma40_resume(struct device *dev)
281328c7a19dSNarayanan G {
281428c7a19dSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
281528c7a19dSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
281628c7a19dSNarayanan G 	int ret = 0;
281728c7a19dSNarayanan G 
281828c7a19dSNarayanan G 	if (base->lcpa_regulator)
281928c7a19dSNarayanan G 		ret = regulator_enable(base->lcpa_regulator);
282028c7a19dSNarayanan G 
282128c7a19dSNarayanan G 	return ret;
282228c7a19dSNarayanan G }
28237fb3e75eSNarayanan G 
28247fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = {
28257fb3e75eSNarayanan G 	.suspend		= dma40_pm_suspend,
28267fb3e75eSNarayanan G 	.runtime_suspend	= dma40_runtime_suspend,
28277fb3e75eSNarayanan G 	.runtime_resume		= dma40_runtime_resume,
282828c7a19dSNarayanan G 	.resume			= dma40_resume,
28297fb3e75eSNarayanan G };
28307fb3e75eSNarayanan G #define DMA40_PM_OPS	(&dma40_pm_ops)
28317fb3e75eSNarayanan G #else
28327fb3e75eSNarayanan G #define DMA40_PM_OPS	NULL
28337fb3e75eSNarayanan G #endif
28347fb3e75eSNarayanan G 
28358d318a50SLinus Walleij /* Initialization functions. */
28368d318a50SLinus Walleij 
28378d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base)
28388d318a50SLinus Walleij {
28398d318a50SLinus Walleij 	int i;
28408d318a50SLinus Walleij 	int num_phy_chans_avail = 0;
28418d318a50SLinus Walleij 	u32 val[2];
28428d318a50SLinus Walleij 	int odd_even_bit = -2;
28437fb3e75eSNarayanan G 	int gcc = D40_DREG_GCC_ENA;
28448d318a50SLinus Walleij 
28458d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRSME);
28468d318a50SLinus Walleij 	val[1] = readl(base->virtbase + D40_DREG_PRSMO);
28478d318a50SLinus Walleij 
28488d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
28498d318a50SLinus Walleij 		base->phy_res[i].num = i;
28508d318a50SLinus Walleij 		odd_even_bit += 2 * ((i % 2) == 0);
28518d318a50SLinus Walleij 		if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
28528d318a50SLinus Walleij 			/* Mark security only channels as occupied */
28538d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_PHY;
28548d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
28557fb3e75eSNarayanan G 			base->phy_res[i].reserved = true;
28567fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
28577fb3e75eSNarayanan G 						       D40_DREG_GCC_SRC);
28587fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
28597fb3e75eSNarayanan G 						       D40_DREG_GCC_DST);
28607fb3e75eSNarayanan G 
28617fb3e75eSNarayanan G 
28628d318a50SLinus Walleij 		} else {
28638d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_FREE;
28648d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
28657fb3e75eSNarayanan G 			base->phy_res[i].reserved = false;
28668d318a50SLinus Walleij 			num_phy_chans_avail++;
28678d318a50SLinus Walleij 		}
28688d318a50SLinus Walleij 		spin_lock_init(&base->phy_res[i].lock);
28698d318a50SLinus Walleij 	}
28706b7acd84SJonas Aaberg 
28716b7acd84SJonas Aaberg 	/* Mark disabled channels as occupied */
28726b7acd84SJonas Aaberg 	for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
2873f57b407cSRabin Vincent 		int chan = base->plat_data->disabled_channels[i];
2874f57b407cSRabin Vincent 
2875f57b407cSRabin Vincent 		base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
2876f57b407cSRabin Vincent 		base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
28777fb3e75eSNarayanan G 		base->phy_res[chan].reserved = true;
28787fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
28797fb3e75eSNarayanan G 					       D40_DREG_GCC_SRC);
28807fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
28817fb3e75eSNarayanan G 					       D40_DREG_GCC_DST);
28826b7acd84SJonas Aaberg 		num_phy_chans_avail--;
28836b7acd84SJonas Aaberg 	}
28846b7acd84SJonas Aaberg 
28858d318a50SLinus Walleij 	dev_info(base->dev, "%d of %d physical DMA channels available\n",
28868d318a50SLinus Walleij 		 num_phy_chans_avail, base->num_phy_chans);
28878d318a50SLinus Walleij 
28888d318a50SLinus Walleij 	/* Verify settings extended vs standard */
28898d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRTYP);
28908d318a50SLinus Walleij 
28918d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
28928d318a50SLinus Walleij 
28938d318a50SLinus Walleij 		if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
28948d318a50SLinus Walleij 		    (val[0] & 0x3) != 1)
28958d318a50SLinus Walleij 			dev_info(base->dev,
28968d318a50SLinus Walleij 				 "[%s] INFO: channel %d is misconfigured (%d)\n",
28978d318a50SLinus Walleij 				 __func__, i, val[0] & 0x3);
28988d318a50SLinus Walleij 
28998d318a50SLinus Walleij 		val[0] = val[0] >> 2;
29008d318a50SLinus Walleij 	}
29018d318a50SLinus Walleij 
29027fb3e75eSNarayanan G 	/*
29037fb3e75eSNarayanan G 	 * To keep things simple, Enable all clocks initially.
29047fb3e75eSNarayanan G 	 * The clocks will get managed later post channel allocation.
29057fb3e75eSNarayanan G 	 * The clocks for the event lines on which reserved channels exists
29067fb3e75eSNarayanan G 	 * are not managed here.
29077fb3e75eSNarayanan G 	 */
29087fb3e75eSNarayanan G 	writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
29097fb3e75eSNarayanan G 	base->gcc_pwr_off_mask = gcc;
29107fb3e75eSNarayanan G 
29118d318a50SLinus Walleij 	return num_phy_chans_avail;
29128d318a50SLinus Walleij }
29138d318a50SLinus Walleij 
29148d318a50SLinus Walleij static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
29158d318a50SLinus Walleij {
29168d318a50SLinus Walleij 	struct stedma40_platform_data *plat_data;
29178d318a50SLinus Walleij 	struct clk *clk = NULL;
29188d318a50SLinus Walleij 	void __iomem *virtbase = NULL;
29198d318a50SLinus Walleij 	struct resource *res = NULL;
29208d318a50SLinus Walleij 	struct d40_base *base = NULL;
29218d318a50SLinus Walleij 	int num_log_chans = 0;
29228d318a50SLinus Walleij 	int num_phy_chans;
29238d318a50SLinus Walleij 	int i;
2924f4b89764SLinus Walleij 	u32 pid;
2925f4b89764SLinus Walleij 	u32 cid;
2926f4b89764SLinus Walleij 	u8 rev;
29278d318a50SLinus Walleij 
29288d318a50SLinus Walleij 	clk = clk_get(&pdev->dev, NULL);
29298d318a50SLinus Walleij 
29308d318a50SLinus Walleij 	if (IS_ERR(clk)) {
29316db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No matching clock found\n");
29328d318a50SLinus Walleij 		goto failure;
29338d318a50SLinus Walleij 	}
29348d318a50SLinus Walleij 
29358d318a50SLinus Walleij 	clk_enable(clk);
29368d318a50SLinus Walleij 
29378d318a50SLinus Walleij 	/* Get IO for DMAC base address */
29388d318a50SLinus Walleij 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
29398d318a50SLinus Walleij 	if (!res)
29408d318a50SLinus Walleij 		goto failure;
29418d318a50SLinus Walleij 
29428d318a50SLinus Walleij 	if (request_mem_region(res->start, resource_size(res),
29438d318a50SLinus Walleij 			       D40_NAME " I/O base") == NULL)
29448d318a50SLinus Walleij 		goto failure;
29458d318a50SLinus Walleij 
29468d318a50SLinus Walleij 	virtbase = ioremap(res->start, resource_size(res));
29478d318a50SLinus Walleij 	if (!virtbase)
29488d318a50SLinus Walleij 		goto failure;
29498d318a50SLinus Walleij 
2950f4b89764SLinus Walleij 	/* This is just a regular AMBA PrimeCell ID actually */
2951f4b89764SLinus Walleij 	for (pid = 0, i = 0; i < 4; i++)
2952f4b89764SLinus Walleij 		pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
2953f4b89764SLinus Walleij 			& 255) << (i * 8);
2954f4b89764SLinus Walleij 	for (cid = 0, i = 0; i < 4; i++)
2955f4b89764SLinus Walleij 		cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
2956f4b89764SLinus Walleij 			& 255) << (i * 8);
2957f4b89764SLinus Walleij 
2958f4b89764SLinus Walleij 	if (cid != AMBA_CID) {
2959f4b89764SLinus Walleij 		d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
29608d318a50SLinus Walleij 		goto failure;
29618d318a50SLinus Walleij 	}
2962f4b89764SLinus Walleij 	if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
29636db5a8baSRabin Vincent 		d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
2964f4b89764SLinus Walleij 			AMBA_MANF_BITS(pid),
2965f4b89764SLinus Walleij 			AMBA_VENDOR_ST);
29668d318a50SLinus Walleij 		goto failure;
29678d318a50SLinus Walleij 	}
2968f4b89764SLinus Walleij 	/*
2969f4b89764SLinus Walleij 	 * HW revision:
2970f4b89764SLinus Walleij 	 * DB8500ed has revision 0
2971f4b89764SLinus Walleij 	 * ? has revision 1
2972f4b89764SLinus Walleij 	 * DB8500v1 has revision 2
2973f4b89764SLinus Walleij 	 * DB8500v2 has revision 3
2974f4b89764SLinus Walleij 	 */
2975f4b89764SLinus Walleij 	rev = AMBA_REV_BITS(pid);
29763ae0267fSJonas Aaberg 
29778d318a50SLinus Walleij 	/* The number of physical channels on this HW */
29788d318a50SLinus Walleij 	num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
29798d318a50SLinus Walleij 
29808d318a50SLinus Walleij 	dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n",
29813ae0267fSJonas Aaberg 		 rev, res->start);
29828d318a50SLinus Walleij 
29831bdae6f4SNarayanan G 	if (rev < 2) {
29841bdae6f4SNarayanan G 		d40_err(&pdev->dev, "hardware revision: %d is not supported",
29851bdae6f4SNarayanan G 			rev);
29861bdae6f4SNarayanan G 		goto failure;
29871bdae6f4SNarayanan G 	}
29881bdae6f4SNarayanan G 
29898d318a50SLinus Walleij 	plat_data = pdev->dev.platform_data;
29908d318a50SLinus Walleij 
29918d318a50SLinus Walleij 	/* Count the number of logical channels in use */
29928d318a50SLinus Walleij 	for (i = 0; i < plat_data->dev_len; i++)
29938d318a50SLinus Walleij 		if (plat_data->dev_rx[i] != 0)
29948d318a50SLinus Walleij 			num_log_chans++;
29958d318a50SLinus Walleij 
29968d318a50SLinus Walleij 	for (i = 0; i < plat_data->dev_len; i++)
29978d318a50SLinus Walleij 		if (plat_data->dev_tx[i] != 0)
29988d318a50SLinus Walleij 			num_log_chans++;
29998d318a50SLinus Walleij 
30008d318a50SLinus Walleij 	base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
30018d318a50SLinus Walleij 		       (num_phy_chans + num_log_chans + plat_data->memcpy_len) *
30028d318a50SLinus Walleij 		       sizeof(struct d40_chan), GFP_KERNEL);
30038d318a50SLinus Walleij 
30048d318a50SLinus Walleij 	if (base == NULL) {
30056db5a8baSRabin Vincent 		d40_err(&pdev->dev, "Out of memory\n");
30068d318a50SLinus Walleij 		goto failure;
30078d318a50SLinus Walleij 	}
30088d318a50SLinus Walleij 
30093ae0267fSJonas Aaberg 	base->rev = rev;
30108d318a50SLinus Walleij 	base->clk = clk;
30118d318a50SLinus Walleij 	base->num_phy_chans = num_phy_chans;
30128d318a50SLinus Walleij 	base->num_log_chans = num_log_chans;
30138d318a50SLinus Walleij 	base->phy_start = res->start;
30148d318a50SLinus Walleij 	base->phy_size = resource_size(res);
30158d318a50SLinus Walleij 	base->virtbase = virtbase;
30168d318a50SLinus Walleij 	base->plat_data = plat_data;
30178d318a50SLinus Walleij 	base->dev = &pdev->dev;
30188d318a50SLinus Walleij 	base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
30198d318a50SLinus Walleij 	base->log_chans = &base->phy_chans[num_phy_chans];
30208d318a50SLinus Walleij 
30218d318a50SLinus Walleij 	base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
30228d318a50SLinus Walleij 				GFP_KERNEL);
30238d318a50SLinus Walleij 	if (!base->phy_res)
30248d318a50SLinus Walleij 		goto failure;
30258d318a50SLinus Walleij 
30268d318a50SLinus Walleij 	base->lookup_phy_chans = kzalloc(num_phy_chans *
30278d318a50SLinus Walleij 					 sizeof(struct d40_chan *),
30288d318a50SLinus Walleij 					 GFP_KERNEL);
30298d318a50SLinus Walleij 	if (!base->lookup_phy_chans)
30308d318a50SLinus Walleij 		goto failure;
30318d318a50SLinus Walleij 
30328d318a50SLinus Walleij 	if (num_log_chans + plat_data->memcpy_len) {
30338d318a50SLinus Walleij 		/*
30348d318a50SLinus Walleij 		 * The max number of logical channels are event lines for all
30358d318a50SLinus Walleij 		 * src devices and dst devices
30368d318a50SLinus Walleij 		 */
30378d318a50SLinus Walleij 		base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 *
30388d318a50SLinus Walleij 						 sizeof(struct d40_chan *),
30398d318a50SLinus Walleij 						 GFP_KERNEL);
30408d318a50SLinus Walleij 		if (!base->lookup_log_chans)
30418d318a50SLinus Walleij 			goto failure;
30428d318a50SLinus Walleij 	}
3043698e4732SJonas Aaberg 
30447fb3e75eSNarayanan G 	base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
30457fb3e75eSNarayanan G 					    sizeof(d40_backup_regs_chan),
30468d318a50SLinus Walleij 					    GFP_KERNEL);
30477fb3e75eSNarayanan G 	if (!base->reg_val_backup_chan)
30487fb3e75eSNarayanan G 		goto failure;
30497fb3e75eSNarayanan G 
30507fb3e75eSNarayanan G 	base->lcla_pool.alloc_map =
30517fb3e75eSNarayanan G 		kzalloc(num_phy_chans * sizeof(struct d40_desc *)
30527fb3e75eSNarayanan G 			* D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
30538d318a50SLinus Walleij 	if (!base->lcla_pool.alloc_map)
30548d318a50SLinus Walleij 		goto failure;
30558d318a50SLinus Walleij 
3056c675b1b4SJonas Aaberg 	base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3057c675b1b4SJonas Aaberg 					    0, SLAB_HWCACHE_ALIGN,
3058c675b1b4SJonas Aaberg 					    NULL);
3059c675b1b4SJonas Aaberg 	if (base->desc_slab == NULL)
3060c675b1b4SJonas Aaberg 		goto failure;
3061c675b1b4SJonas Aaberg 
30628d318a50SLinus Walleij 	return base;
30638d318a50SLinus Walleij 
30648d318a50SLinus Walleij failure:
3065c6134c96SRabin Vincent 	if (!IS_ERR(clk)) {
30668d318a50SLinus Walleij 		clk_disable(clk);
30678d318a50SLinus Walleij 		clk_put(clk);
30688d318a50SLinus Walleij 	}
30698d318a50SLinus Walleij 	if (virtbase)
30708d318a50SLinus Walleij 		iounmap(virtbase);
30718d318a50SLinus Walleij 	if (res)
30728d318a50SLinus Walleij 		release_mem_region(res->start,
30738d318a50SLinus Walleij 				   resource_size(res));
30748d318a50SLinus Walleij 	if (virtbase)
30758d318a50SLinus Walleij 		iounmap(virtbase);
30768d318a50SLinus Walleij 
30778d318a50SLinus Walleij 	if (base) {
30788d318a50SLinus Walleij 		kfree(base->lcla_pool.alloc_map);
30791bdae6f4SNarayanan G 		kfree(base->reg_val_backup_chan);
30808d318a50SLinus Walleij 		kfree(base->lookup_log_chans);
30818d318a50SLinus Walleij 		kfree(base->lookup_phy_chans);
30828d318a50SLinus Walleij 		kfree(base->phy_res);
30838d318a50SLinus Walleij 		kfree(base);
30848d318a50SLinus Walleij 	}
30858d318a50SLinus Walleij 
30868d318a50SLinus Walleij 	return NULL;
30878d318a50SLinus Walleij }
30888d318a50SLinus Walleij 
30898d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base)
30908d318a50SLinus Walleij {
30918d318a50SLinus Walleij 
30927fb3e75eSNarayanan G 	static struct d40_reg_val dma_init_reg[] = {
30938d318a50SLinus Walleij 		/* Clock every part of the DMA block from start */
30947fb3e75eSNarayanan G 		{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},
30958d318a50SLinus Walleij 
30968d318a50SLinus Walleij 		/* Interrupts on all logical channels */
30978d318a50SLinus Walleij 		{ .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
30988d318a50SLinus Walleij 		{ .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
30998d318a50SLinus Walleij 		{ .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
31008d318a50SLinus Walleij 		{ .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
31018d318a50SLinus Walleij 		{ .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
31028d318a50SLinus Walleij 		{ .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
31038d318a50SLinus Walleij 		{ .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
31048d318a50SLinus Walleij 		{ .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
31058d318a50SLinus Walleij 		{ .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
31068d318a50SLinus Walleij 		{ .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
31078d318a50SLinus Walleij 		{ .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
31088d318a50SLinus Walleij 		{ .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
31098d318a50SLinus Walleij 	};
31108d318a50SLinus Walleij 	int i;
31118d318a50SLinus Walleij 	u32 prmseo[2] = {0, 0};
31128d318a50SLinus Walleij 	u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
31138d318a50SLinus Walleij 	u32 pcmis = 0;
31148d318a50SLinus Walleij 	u32 pcicr = 0;
31158d318a50SLinus Walleij 
31168d318a50SLinus Walleij 	for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++)
31178d318a50SLinus Walleij 		writel(dma_init_reg[i].val,
31188d318a50SLinus Walleij 		       base->virtbase + dma_init_reg[i].reg);
31198d318a50SLinus Walleij 
31208d318a50SLinus Walleij 	/* Configure all our dma channels to default settings */
31218d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
31228d318a50SLinus Walleij 
31238d318a50SLinus Walleij 		activeo[i % 2] = activeo[i % 2] << 2;
31248d318a50SLinus Walleij 
31258d318a50SLinus Walleij 		if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
31268d318a50SLinus Walleij 		    == D40_ALLOC_PHY) {
31278d318a50SLinus Walleij 			activeo[i % 2] |= 3;
31288d318a50SLinus Walleij 			continue;
31298d318a50SLinus Walleij 		}
31308d318a50SLinus Walleij 
31318d318a50SLinus Walleij 		/* Enable interrupt # */
31328d318a50SLinus Walleij 		pcmis = (pcmis << 1) | 1;
31338d318a50SLinus Walleij 
31348d318a50SLinus Walleij 		/* Clear interrupt # */
31358d318a50SLinus Walleij 		pcicr = (pcicr << 1) | 1;
31368d318a50SLinus Walleij 
31378d318a50SLinus Walleij 		/* Set channel to physical mode */
31388d318a50SLinus Walleij 		prmseo[i % 2] = prmseo[i % 2] << 2;
31398d318a50SLinus Walleij 		prmseo[i % 2] |= 1;
31408d318a50SLinus Walleij 
31418d318a50SLinus Walleij 	}
31428d318a50SLinus Walleij 
31438d318a50SLinus Walleij 	writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
31448d318a50SLinus Walleij 	writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
31458d318a50SLinus Walleij 	writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
31468d318a50SLinus Walleij 	writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
31478d318a50SLinus Walleij 
31488d318a50SLinus Walleij 	/* Write which interrupt to enable */
31498d318a50SLinus Walleij 	writel(pcmis, base->virtbase + D40_DREG_PCMIS);
31508d318a50SLinus Walleij 
31518d318a50SLinus Walleij 	/* Write which interrupt to clear */
31528d318a50SLinus Walleij 	writel(pcicr, base->virtbase + D40_DREG_PCICR);
31538d318a50SLinus Walleij 
31548d318a50SLinus Walleij }
31558d318a50SLinus Walleij 
3156508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base)
3157508849adSLinus Walleij {
3158026cbc42SRabin Vincent 	struct d40_lcla_pool *pool = &base->lcla_pool;
3159508849adSLinus Walleij 	unsigned long *page_list;
3160508849adSLinus Walleij 	int i, j;
3161508849adSLinus Walleij 	int ret = 0;
3162508849adSLinus Walleij 
3163508849adSLinus Walleij 	/*
3164508849adSLinus Walleij 	 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3165508849adSLinus Walleij 	 * To full fill this hardware requirement without wasting 256 kb
3166508849adSLinus Walleij 	 * we allocate pages until we get an aligned one.
3167508849adSLinus Walleij 	 */
3168508849adSLinus Walleij 	page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
3169508849adSLinus Walleij 			    GFP_KERNEL);
3170508849adSLinus Walleij 
3171508849adSLinus Walleij 	if (!page_list) {
3172508849adSLinus Walleij 		ret = -ENOMEM;
3173508849adSLinus Walleij 		goto failure;
3174508849adSLinus Walleij 	}
3175508849adSLinus Walleij 
3176508849adSLinus Walleij 	/* Calculating how many pages that are required */
3177508849adSLinus Walleij 	base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3178508849adSLinus Walleij 
3179508849adSLinus Walleij 	for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3180508849adSLinus Walleij 		page_list[i] = __get_free_pages(GFP_KERNEL,
3181508849adSLinus Walleij 						base->lcla_pool.pages);
3182508849adSLinus Walleij 		if (!page_list[i]) {
3183508849adSLinus Walleij 
31846db5a8baSRabin Vincent 			d40_err(base->dev, "Failed to allocate %d pages.\n",
31856db5a8baSRabin Vincent 				base->lcla_pool.pages);
3186508849adSLinus Walleij 
3187508849adSLinus Walleij 			for (j = 0; j < i; j++)
3188508849adSLinus Walleij 				free_pages(page_list[j], base->lcla_pool.pages);
3189508849adSLinus Walleij 			goto failure;
3190508849adSLinus Walleij 		}
3191508849adSLinus Walleij 
3192508849adSLinus Walleij 		if ((virt_to_phys((void *)page_list[i]) &
3193508849adSLinus Walleij 		     (LCLA_ALIGNMENT - 1)) == 0)
3194508849adSLinus Walleij 			break;
3195508849adSLinus Walleij 	}
3196508849adSLinus Walleij 
3197508849adSLinus Walleij 	for (j = 0; j < i; j++)
3198508849adSLinus Walleij 		free_pages(page_list[j], base->lcla_pool.pages);
3199508849adSLinus Walleij 
3200508849adSLinus Walleij 	if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3201508849adSLinus Walleij 		base->lcla_pool.base = (void *)page_list[i];
3202508849adSLinus Walleij 	} else {
3203767a9675SJonas Aaberg 		/*
3204767a9675SJonas Aaberg 		 * After many attempts and no succees with finding the correct
3205767a9675SJonas Aaberg 		 * alignment, try with allocating a big buffer.
3206767a9675SJonas Aaberg 		 */
3207508849adSLinus Walleij 		dev_warn(base->dev,
3208508849adSLinus Walleij 			 "[%s] Failed to get %d pages @ 18 bit align.\n",
3209508849adSLinus Walleij 			 __func__, base->lcla_pool.pages);
3210508849adSLinus Walleij 		base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3211508849adSLinus Walleij 							 base->num_phy_chans +
3212508849adSLinus Walleij 							 LCLA_ALIGNMENT,
3213508849adSLinus Walleij 							 GFP_KERNEL);
3214508849adSLinus Walleij 		if (!base->lcla_pool.base_unaligned) {
3215508849adSLinus Walleij 			ret = -ENOMEM;
3216508849adSLinus Walleij 			goto failure;
3217508849adSLinus Walleij 		}
3218508849adSLinus Walleij 
3219508849adSLinus Walleij 		base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3220508849adSLinus Walleij 						 LCLA_ALIGNMENT);
3221508849adSLinus Walleij 	}
3222508849adSLinus Walleij 
3223026cbc42SRabin Vincent 	pool->dma_addr = dma_map_single(base->dev, pool->base,
3224026cbc42SRabin Vincent 					SZ_1K * base->num_phy_chans,
3225026cbc42SRabin Vincent 					DMA_TO_DEVICE);
3226026cbc42SRabin Vincent 	if (dma_mapping_error(base->dev, pool->dma_addr)) {
3227026cbc42SRabin Vincent 		pool->dma_addr = 0;
3228026cbc42SRabin Vincent 		ret = -ENOMEM;
3229026cbc42SRabin Vincent 		goto failure;
3230026cbc42SRabin Vincent 	}
3231026cbc42SRabin Vincent 
3232508849adSLinus Walleij 	writel(virt_to_phys(base->lcla_pool.base),
3233508849adSLinus Walleij 	       base->virtbase + D40_DREG_LCLA);
3234508849adSLinus Walleij failure:
3235508849adSLinus Walleij 	kfree(page_list);
3236508849adSLinus Walleij 	return ret;
3237508849adSLinus Walleij }
3238508849adSLinus Walleij 
32398d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev)
32408d318a50SLinus Walleij {
32418d318a50SLinus Walleij 	int err;
32428d318a50SLinus Walleij 	int ret = -ENOENT;
32438d318a50SLinus Walleij 	struct d40_base *base;
32448d318a50SLinus Walleij 	struct resource *res = NULL;
32458d318a50SLinus Walleij 	int num_reserved_chans;
32468d318a50SLinus Walleij 	u32 val;
32478d318a50SLinus Walleij 
32488d318a50SLinus Walleij 	base = d40_hw_detect_init(pdev);
32498d318a50SLinus Walleij 
32508d318a50SLinus Walleij 	if (!base)
32518d318a50SLinus Walleij 		goto failure;
32528d318a50SLinus Walleij 
32538d318a50SLinus Walleij 	num_reserved_chans = d40_phy_res_init(base);
32548d318a50SLinus Walleij 
32558d318a50SLinus Walleij 	platform_set_drvdata(pdev, base);
32568d318a50SLinus Walleij 
32578d318a50SLinus Walleij 	spin_lock_init(&base->interrupt_lock);
32588d318a50SLinus Walleij 	spin_lock_init(&base->execmd_lock);
32598d318a50SLinus Walleij 
32608d318a50SLinus Walleij 	/* Get IO for logical channel parameter address */
32618d318a50SLinus Walleij 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
32628d318a50SLinus Walleij 	if (!res) {
32638d318a50SLinus Walleij 		ret = -ENOENT;
32646db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
32658d318a50SLinus Walleij 		goto failure;
32668d318a50SLinus Walleij 	}
32678d318a50SLinus Walleij 	base->lcpa_size = resource_size(res);
32688d318a50SLinus Walleij 	base->phy_lcpa = res->start;
32698d318a50SLinus Walleij 
32708d318a50SLinus Walleij 	if (request_mem_region(res->start, resource_size(res),
32718d318a50SLinus Walleij 			       D40_NAME " I/O lcpa") == NULL) {
32728d318a50SLinus Walleij 		ret = -EBUSY;
32736db5a8baSRabin Vincent 		d40_err(&pdev->dev,
32746db5a8baSRabin Vincent 			"Failed to request LCPA region 0x%x-0x%x\n",
32756db5a8baSRabin Vincent 			res->start, res->end);
32768d318a50SLinus Walleij 		goto failure;
32778d318a50SLinus Walleij 	}
32788d318a50SLinus Walleij 
32798d318a50SLinus Walleij 	/* We make use of ESRAM memory for this. */
32808d318a50SLinus Walleij 	val = readl(base->virtbase + D40_DREG_LCPA);
32818d318a50SLinus Walleij 	if (res->start != val && val != 0) {
32828d318a50SLinus Walleij 		dev_warn(&pdev->dev,
32838d318a50SLinus Walleij 			 "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n",
32848d318a50SLinus Walleij 			 __func__, val, res->start);
32858d318a50SLinus Walleij 	} else
32868d318a50SLinus Walleij 		writel(res->start, base->virtbase + D40_DREG_LCPA);
32878d318a50SLinus Walleij 
32888d318a50SLinus Walleij 	base->lcpa_base = ioremap(res->start, resource_size(res));
32898d318a50SLinus Walleij 	if (!base->lcpa_base) {
32908d318a50SLinus Walleij 		ret = -ENOMEM;
32916db5a8baSRabin Vincent 		d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
32928d318a50SLinus Walleij 		goto failure;
32938d318a50SLinus Walleij 	}
329428c7a19dSNarayanan G 	/* If lcla has to be located in ESRAM we don't need to allocate */
329528c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
329628c7a19dSNarayanan G 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
329728c7a19dSNarayanan G 							"lcla_esram");
329828c7a19dSNarayanan G 		if (!res) {
329928c7a19dSNarayanan G 			ret = -ENOENT;
330028c7a19dSNarayanan G 			d40_err(&pdev->dev,
330128c7a19dSNarayanan G 				"No \"lcla_esram\" memory resource\n");
330228c7a19dSNarayanan G 			goto failure;
330328c7a19dSNarayanan G 		}
330428c7a19dSNarayanan G 		base->lcla_pool.base = ioremap(res->start,
330528c7a19dSNarayanan G 						resource_size(res));
330628c7a19dSNarayanan G 		if (!base->lcla_pool.base) {
330728c7a19dSNarayanan G 			ret = -ENOMEM;
330828c7a19dSNarayanan G 			d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
330928c7a19dSNarayanan G 			goto failure;
331028c7a19dSNarayanan G 		}
331128c7a19dSNarayanan G 		writel(res->start, base->virtbase + D40_DREG_LCLA);
3312508849adSLinus Walleij 
331328c7a19dSNarayanan G 	} else {
3314508849adSLinus Walleij 		ret = d40_lcla_allocate(base);
3315508849adSLinus Walleij 		if (ret) {
33166db5a8baSRabin Vincent 			d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
33178d318a50SLinus Walleij 			goto failure;
33188d318a50SLinus Walleij 		}
331928c7a19dSNarayanan G 	}
33208d318a50SLinus Walleij 
33218d318a50SLinus Walleij 	spin_lock_init(&base->lcla_pool.lock);
33228d318a50SLinus Walleij 
33238d318a50SLinus Walleij 	base->irq = platform_get_irq(pdev, 0);
33248d318a50SLinus Walleij 
33258d318a50SLinus Walleij 	ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
33268d318a50SLinus Walleij 	if (ret) {
33276db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No IRQ defined\n");
33288d318a50SLinus Walleij 		goto failure;
33298d318a50SLinus Walleij 	}
33308d318a50SLinus Walleij 
33317fb3e75eSNarayanan G 	pm_runtime_irq_safe(base->dev);
33327fb3e75eSNarayanan G 	pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
33337fb3e75eSNarayanan G 	pm_runtime_use_autosuspend(base->dev);
33347fb3e75eSNarayanan G 	pm_runtime_enable(base->dev);
33357fb3e75eSNarayanan G 	pm_runtime_resume(base->dev);
333628c7a19dSNarayanan G 
333728c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
333828c7a19dSNarayanan G 
333928c7a19dSNarayanan G 		base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
334028c7a19dSNarayanan G 		if (IS_ERR(base->lcpa_regulator)) {
334128c7a19dSNarayanan G 			d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
334228c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
334328c7a19dSNarayanan G 			goto failure;
334428c7a19dSNarayanan G 		}
334528c7a19dSNarayanan G 
334628c7a19dSNarayanan G 		ret = regulator_enable(base->lcpa_regulator);
334728c7a19dSNarayanan G 		if (ret) {
334828c7a19dSNarayanan G 			d40_err(&pdev->dev,
334928c7a19dSNarayanan G 				"Failed to enable lcpa_regulator\n");
335028c7a19dSNarayanan G 			regulator_put(base->lcpa_regulator);
335128c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
335228c7a19dSNarayanan G 			goto failure;
335328c7a19dSNarayanan G 		}
335428c7a19dSNarayanan G 	}
335528c7a19dSNarayanan G 
33567fb3e75eSNarayanan G 	base->initialized = true;
33578d318a50SLinus Walleij 	err = d40_dmaengine_init(base, num_reserved_chans);
33588d318a50SLinus Walleij 	if (err)
33598d318a50SLinus Walleij 		goto failure;
33608d318a50SLinus Walleij 
33618d318a50SLinus Walleij 	d40_hw_init(base);
33628d318a50SLinus Walleij 
33638d318a50SLinus Walleij 	dev_info(base->dev, "initialized\n");
33648d318a50SLinus Walleij 	return 0;
33658d318a50SLinus Walleij 
33668d318a50SLinus Walleij failure:
33678d318a50SLinus Walleij 	if (base) {
3368c675b1b4SJonas Aaberg 		if (base->desc_slab)
3369c675b1b4SJonas Aaberg 			kmem_cache_destroy(base->desc_slab);
33708d318a50SLinus Walleij 		if (base->virtbase)
33718d318a50SLinus Walleij 			iounmap(base->virtbase);
3372026cbc42SRabin Vincent 
337328c7a19dSNarayanan G 		if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
337428c7a19dSNarayanan G 			iounmap(base->lcla_pool.base);
337528c7a19dSNarayanan G 			base->lcla_pool.base = NULL;
337628c7a19dSNarayanan G 		}
337728c7a19dSNarayanan G 
3378026cbc42SRabin Vincent 		if (base->lcla_pool.dma_addr)
3379026cbc42SRabin Vincent 			dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3380026cbc42SRabin Vincent 					 SZ_1K * base->num_phy_chans,
3381026cbc42SRabin Vincent 					 DMA_TO_DEVICE);
3382026cbc42SRabin Vincent 
3383508849adSLinus Walleij 		if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3384508849adSLinus Walleij 			free_pages((unsigned long)base->lcla_pool.base,
3385508849adSLinus Walleij 				   base->lcla_pool.pages);
3386767a9675SJonas Aaberg 
3387508849adSLinus Walleij 		kfree(base->lcla_pool.base_unaligned);
3388767a9675SJonas Aaberg 
33898d318a50SLinus Walleij 		if (base->phy_lcpa)
33908d318a50SLinus Walleij 			release_mem_region(base->phy_lcpa,
33918d318a50SLinus Walleij 					   base->lcpa_size);
33928d318a50SLinus Walleij 		if (base->phy_start)
33938d318a50SLinus Walleij 			release_mem_region(base->phy_start,
33948d318a50SLinus Walleij 					   base->phy_size);
33958d318a50SLinus Walleij 		if (base->clk) {
33968d318a50SLinus Walleij 			clk_disable(base->clk);
33978d318a50SLinus Walleij 			clk_put(base->clk);
33988d318a50SLinus Walleij 		}
33998d318a50SLinus Walleij 
340028c7a19dSNarayanan G 		if (base->lcpa_regulator) {
340128c7a19dSNarayanan G 			regulator_disable(base->lcpa_regulator);
340228c7a19dSNarayanan G 			regulator_put(base->lcpa_regulator);
340328c7a19dSNarayanan G 		}
340428c7a19dSNarayanan G 
34058d318a50SLinus Walleij 		kfree(base->lcla_pool.alloc_map);
34068d318a50SLinus Walleij 		kfree(base->lookup_log_chans);
34078d318a50SLinus Walleij 		kfree(base->lookup_phy_chans);
34088d318a50SLinus Walleij 		kfree(base->phy_res);
34098d318a50SLinus Walleij 		kfree(base);
34108d318a50SLinus Walleij 	}
34118d318a50SLinus Walleij 
34126db5a8baSRabin Vincent 	d40_err(&pdev->dev, "probe failed\n");
34138d318a50SLinus Walleij 	return ret;
34148d318a50SLinus Walleij }
34158d318a50SLinus Walleij 
34168d318a50SLinus Walleij static struct platform_driver d40_driver = {
34178d318a50SLinus Walleij 	.driver = {
34188d318a50SLinus Walleij 		.owner = THIS_MODULE,
34198d318a50SLinus Walleij 		.name  = D40_NAME,
34207fb3e75eSNarayanan G 		.pm = DMA40_PM_OPS,
34218d318a50SLinus Walleij 	},
34228d318a50SLinus Walleij };
34238d318a50SLinus Walleij 
3424cb9ab2d8SRabin Vincent static int __init stedma40_init(void)
34258d318a50SLinus Walleij {
34268d318a50SLinus Walleij 	return platform_driver_probe(&d40_driver, d40_probe);
34278d318a50SLinus Walleij }
3428a0eb221aSLinus Walleij subsys_initcall(stedma40_init);
3429