xref: /linux/drivers/dma/ste_dma40.c (revision 41be14c71bae05c8daa3de5ae7fd7c2a7bc0d057)
1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28d318a50SLinus Walleij /*
3d49278e3SPer Forlin  * Copyright (C) Ericsson AB 2007-2008
4d49278e3SPer Forlin  * Copyright (C) ST-Ericsson SA 2008-2010
5661385f9SPer Forlin  * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
6767a9675SJonas Aaberg  * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
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>
17c95905a6SGuennadi Liakhovetski #include <linux/log2.h>
187fb3e75eSNarayanan G #include <linux/pm.h>
197fb3e75eSNarayanan G #include <linux/pm_runtime.h>
20698e4732SJonas Aaberg #include <linux/err.h>
211814a170SLee Jones #include <linux/of.h>
225a1a3b9cSLinus Walleij #include <linux/of_address.h>
23fa332de5SLee Jones #include <linux/of_dma.h>
24f4b89764SLinus Walleij #include <linux/amba/bus.h>
2515e4b78dSLinus Walleij #include <linux/regulator/consumer.h>
268d318a50SLinus Walleij 
27d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
2842ae6f16SLinus Walleij #include "ste_dma40.h"
298d318a50SLinus Walleij #include "ste_dma40_ll.h"
308d318a50SLinus Walleij 
3142ae6f16SLinus Walleij /**
3242ae6f16SLinus Walleij  * struct stedma40_platform_data - Configuration struct for the dma device.
3342ae6f16SLinus Walleij  *
3442ae6f16SLinus Walleij  * @dev_tx: mapping between destination event line and io address
3542ae6f16SLinus Walleij  * @dev_rx: mapping between source event line and io address
3642ae6f16SLinus Walleij  * @disabled_channels: A vector, ending with -1, that marks physical channels
3742ae6f16SLinus Walleij  * that are for different reasons not available for the driver.
3842ae6f16SLinus Walleij  * @soft_lli_chans: A vector, that marks physical channels will use LLI by SW
3942ae6f16SLinus Walleij  * which avoids HW bug that exists in some versions of the controller.
4042ae6f16SLinus Walleij  * SoftLLI introduces relink overhead that could impact performace for
4142ae6f16SLinus Walleij  * certain use cases.
4242ae6f16SLinus Walleij  * @num_of_soft_lli_chans: The number of channels that needs to be configured
4342ae6f16SLinus Walleij  * to use SoftLLI.
4442ae6f16SLinus Walleij  * @use_esram_lcla: flag for mapping the lcla into esram region
4542ae6f16SLinus Walleij  * @num_of_memcpy_chans: The number of channels reserved for memcpy.
4642ae6f16SLinus Walleij  * @num_of_phy_chans: The number of physical channels implemented in HW.
4742ae6f16SLinus Walleij  * 0 means reading the number of channels from DMA HW but this is only valid
4842ae6f16SLinus Walleij  * for 'multiple of 4' channels, like 8.
4942ae6f16SLinus Walleij  */
5042ae6f16SLinus Walleij struct stedma40_platform_data {
5142ae6f16SLinus Walleij 	int				 disabled_channels[STEDMA40_MAX_PHYS];
5242ae6f16SLinus Walleij 	int				*soft_lli_chans;
5342ae6f16SLinus Walleij 	int				 num_of_soft_lli_chans;
5442ae6f16SLinus Walleij 	bool				 use_esram_lcla;
5542ae6f16SLinus Walleij 	int				 num_of_memcpy_chans;
5642ae6f16SLinus Walleij 	int				 num_of_phy_chans;
5742ae6f16SLinus Walleij };
5842ae6f16SLinus Walleij 
598d318a50SLinus Walleij #define D40_NAME "dma40"
608d318a50SLinus Walleij 
618d318a50SLinus Walleij #define D40_PHY_CHAN -1
628d318a50SLinus Walleij 
638d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */
648d318a50SLinus Walleij #define D40_CHAN_POS(chan)  (2 * (chan / 2))
658d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
668d318a50SLinus Walleij 
678d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */
688d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500
698d318a50SLinus Walleij 
707fb3e75eSNarayanan G /* Milliseconds */
717fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY	100
727fb3e75eSNarayanan G 
73508849adSLinus Walleij /* Hardware requirement on LCLA alignment */
74508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000
75698e4732SJonas Aaberg 
76698e4732SJonas Aaberg /* Max number of links per event group */
77698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128
78698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
79698e4732SJonas Aaberg 
80db72da92SLee Jones /* Max number of logical channels per physical channel */
81db72da92SLee Jones #define D40_MAX_LOG_CHAN_PER_PHY 32
82db72da92SLee Jones 
83508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */
84508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256
85508849adSLinus Walleij 
86508849adSLinus Walleij /* Bit markings for allocation map */
878a3b6e14SLee Jones #define D40_ALLOC_FREE		BIT(31)
888a3b6e14SLee Jones #define D40_ALLOC_PHY		BIT(30)
898d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE	0
908d318a50SLinus Walleij 
91a7dacb68SLee Jones #define D40_MEMCPY_MAX_CHANS	8
92a7dacb68SLee Jones 
93664a57ecSLee Jones /* Reserved event lines for memcpy only. */
94a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_0	51
95a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_1	56
96a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_2	57
97a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_3	58
98a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_4	59
99a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_5	60
100a2acaa21SLinus Walleij 
101a2acaa21SLinus Walleij static int dma40_memcpy_channels[] = {
102a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_0,
103a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_1,
104a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_2,
105a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_3,
106a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_4,
107a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_5,
108a2acaa21SLinus Walleij };
109664a57ecSLee Jones 
110c281cde7SBjorn Helgaas /* Default configuration for physical memcpy */
111e43341caSBhumika Goyal static const struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
11229027a1eSLee Jones 	.mode = STEDMA40_MODE_PHYSICAL,
1132c2b62d5SLee Jones 	.dir = DMA_MEM_TO_MEM,
11429027a1eSLee Jones 
11543f2e1a3SLee Jones 	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
11629027a1eSLee Jones 	.src_info.psize = STEDMA40_PSIZE_PHY_1,
11729027a1eSLee Jones 	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
11829027a1eSLee Jones 
11943f2e1a3SLee Jones 	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
12029027a1eSLee Jones 	.dst_info.psize = STEDMA40_PSIZE_PHY_1,
12129027a1eSLee Jones 	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
12229027a1eSLee Jones };
12329027a1eSLee Jones 
12429027a1eSLee Jones /* Default configuration for logical memcpy */
125e43341caSBhumika Goyal static const struct stedma40_chan_cfg dma40_memcpy_conf_log = {
12629027a1eSLee Jones 	.mode = STEDMA40_MODE_LOGICAL,
1272c2b62d5SLee Jones 	.dir = DMA_MEM_TO_MEM,
12829027a1eSLee Jones 
12943f2e1a3SLee Jones 	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
13029027a1eSLee Jones 	.src_info.psize = STEDMA40_PSIZE_LOG_1,
13129027a1eSLee Jones 	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
13229027a1eSLee Jones 
13343f2e1a3SLee Jones 	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
13429027a1eSLee Jones 	.dst_info.psize = STEDMA40_PSIZE_LOG_1,
13529027a1eSLee Jones 	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
13629027a1eSLee Jones };
13729027a1eSLee Jones 
1388d318a50SLinus Walleij /**
139401f022cSVinod Koul  * enum d40_command - The different commands and/or statuses.
1408d318a50SLinus Walleij  *
1418d318a50SLinus Walleij  * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
1428d318a50SLinus Walleij  * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
1438d318a50SLinus Walleij  * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
1448d318a50SLinus Walleij  * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
1458d318a50SLinus Walleij  */
1468d318a50SLinus Walleij enum d40_command {
1478d318a50SLinus Walleij 	D40_DMA_STOP		= 0,
1488d318a50SLinus Walleij 	D40_DMA_RUN		= 1,
1498d318a50SLinus Walleij 	D40_DMA_SUSPEND_REQ	= 2,
1508d318a50SLinus Walleij 	D40_DMA_SUSPENDED	= 3
1518d318a50SLinus Walleij };
1528d318a50SLinus Walleij 
1537fb3e75eSNarayanan G /*
1541bdae6f4SNarayanan G  * enum d40_events - The different Event Enables for the event lines.
1551bdae6f4SNarayanan G  *
1561bdae6f4SNarayanan G  * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
1571bdae6f4SNarayanan G  * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
1581bdae6f4SNarayanan G  * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
1591bdae6f4SNarayanan G  * @D40_ROUND_EVENTLINE: Status check for event line.
1601bdae6f4SNarayanan G  */
1611bdae6f4SNarayanan G 
1621bdae6f4SNarayanan G enum d40_events {
1631bdae6f4SNarayanan G 	D40_DEACTIVATE_EVENTLINE	= 0,
1641bdae6f4SNarayanan G 	D40_ACTIVATE_EVENTLINE		= 1,
1651bdae6f4SNarayanan G 	D40_SUSPEND_REQ_EVENTLINE	= 2,
1661bdae6f4SNarayanan G 	D40_ROUND_EVENTLINE		= 3
1671bdae6f4SNarayanan G };
1681bdae6f4SNarayanan G 
1691bdae6f4SNarayanan G /*
1707fb3e75eSNarayanan G  * These are the registers that has to be saved and later restored
1717fb3e75eSNarayanan G  * when the DMA hw is powered off.
1727fb3e75eSNarayanan G  * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
1737fb3e75eSNarayanan G  */
1745d6fb560SArnd Bergmann static __maybe_unused u32 d40_backup_regs[] = {
1757fb3e75eSNarayanan G 	D40_DREG_LCPA,
1767fb3e75eSNarayanan G 	D40_DREG_LCLA,
1777fb3e75eSNarayanan G 	D40_DREG_PRMSE,
1787fb3e75eSNarayanan G 	D40_DREG_PRMSO,
1797fb3e75eSNarayanan G 	D40_DREG_PRMOE,
1807fb3e75eSNarayanan G 	D40_DREG_PRMOO,
1817fb3e75eSNarayanan G };
1827fb3e75eSNarayanan G 
1837fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
1847fb3e75eSNarayanan G 
1853cb645dcSTong Liu /*
1863cb645dcSTong Liu  * since 9540 and 8540 has the same HW revision
1873cb645dcSTong Liu  * use v4a for 9540 or ealier
1883cb645dcSTong Liu  * use v4b for 8540 or later
1893cb645dcSTong Liu  * HW revision:
1903cb645dcSTong Liu  * DB8500ed has revision 0
1913cb645dcSTong Liu  * DB8500v1 has revision 2
1923cb645dcSTong Liu  * DB8500v2 has revision 3
1933cb645dcSTong Liu  * AP9540v1 has revision 4
1943cb645dcSTong Liu  * DB8540v1 has revision 4
1953cb645dcSTong Liu  * TODO: Check if all these registers have to be saved/restored on dma40 v4a
1963cb645dcSTong Liu  */
1973cb645dcSTong Liu static u32 d40_backup_regs_v4a[] = {
1987fb3e75eSNarayanan G 	D40_DREG_PSEG1,
1997fb3e75eSNarayanan G 	D40_DREG_PSEG2,
2007fb3e75eSNarayanan G 	D40_DREG_PSEG3,
2017fb3e75eSNarayanan G 	D40_DREG_PSEG4,
2027fb3e75eSNarayanan G 	D40_DREG_PCEG1,
2037fb3e75eSNarayanan G 	D40_DREG_PCEG2,
2047fb3e75eSNarayanan G 	D40_DREG_PCEG3,
2057fb3e75eSNarayanan G 	D40_DREG_PCEG4,
2067fb3e75eSNarayanan G 	D40_DREG_RSEG1,
2077fb3e75eSNarayanan G 	D40_DREG_RSEG2,
2087fb3e75eSNarayanan G 	D40_DREG_RSEG3,
2097fb3e75eSNarayanan G 	D40_DREG_RSEG4,
2107fb3e75eSNarayanan G 	D40_DREG_RCEG1,
2117fb3e75eSNarayanan G 	D40_DREG_RCEG2,
2127fb3e75eSNarayanan G 	D40_DREG_RCEG3,
2137fb3e75eSNarayanan G 	D40_DREG_RCEG4,
2147fb3e75eSNarayanan G };
2157fb3e75eSNarayanan G 
2163cb645dcSTong Liu #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
2173cb645dcSTong Liu 
2183cb645dcSTong Liu static u32 d40_backup_regs_v4b[] = {
2193cb645dcSTong Liu 	D40_DREG_CPSEG1,
2203cb645dcSTong Liu 	D40_DREG_CPSEG2,
2213cb645dcSTong Liu 	D40_DREG_CPSEG3,
2223cb645dcSTong Liu 	D40_DREG_CPSEG4,
2233cb645dcSTong Liu 	D40_DREG_CPSEG5,
2243cb645dcSTong Liu 	D40_DREG_CPCEG1,
2253cb645dcSTong Liu 	D40_DREG_CPCEG2,
2263cb645dcSTong Liu 	D40_DREG_CPCEG3,
2273cb645dcSTong Liu 	D40_DREG_CPCEG4,
2283cb645dcSTong Liu 	D40_DREG_CPCEG5,
2293cb645dcSTong Liu 	D40_DREG_CRSEG1,
2303cb645dcSTong Liu 	D40_DREG_CRSEG2,
2313cb645dcSTong Liu 	D40_DREG_CRSEG3,
2323cb645dcSTong Liu 	D40_DREG_CRSEG4,
2333cb645dcSTong Liu 	D40_DREG_CRSEG5,
2343cb645dcSTong Liu 	D40_DREG_CRCEG1,
2353cb645dcSTong Liu 	D40_DREG_CRCEG2,
2363cb645dcSTong Liu 	D40_DREG_CRCEG3,
2373cb645dcSTong Liu 	D40_DREG_CRCEG4,
2383cb645dcSTong Liu 	D40_DREG_CRCEG5,
2393cb645dcSTong Liu };
2403cb645dcSTong Liu 
2413cb645dcSTong Liu #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
2427fb3e75eSNarayanan G 
2435d6fb560SArnd Bergmann static __maybe_unused u32 d40_backup_regs_chan[] = {
2447fb3e75eSNarayanan G 	D40_CHAN_REG_SSCFG,
2457fb3e75eSNarayanan G 	D40_CHAN_REG_SSELT,
2467fb3e75eSNarayanan G 	D40_CHAN_REG_SSPTR,
2477fb3e75eSNarayanan G 	D40_CHAN_REG_SSLNK,
2487fb3e75eSNarayanan G 	D40_CHAN_REG_SDCFG,
2497fb3e75eSNarayanan G 	D40_CHAN_REG_SDELT,
2507fb3e75eSNarayanan G 	D40_CHAN_REG_SDPTR,
2517fb3e75eSNarayanan G 	D40_CHAN_REG_SDLNK,
2527fb3e75eSNarayanan G };
2537fb3e75eSNarayanan G 
25484b3da14SLee Jones #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
25584b3da14SLee Jones 			     BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
25684b3da14SLee Jones 
2578d318a50SLinus Walleij /**
2583cb645dcSTong Liu  * struct d40_interrupt_lookup - lookup table for interrupt handler
2593cb645dcSTong Liu  *
2603cb645dcSTong Liu  * @src: Interrupt mask register.
2613cb645dcSTong Liu  * @clr: Interrupt clear register.
2623cb645dcSTong Liu  * @is_error: true if this is an error interrupt.
2633cb645dcSTong Liu  * @offset: start delta in the lookup_log_chans in d40_base. If equals to
2643cb645dcSTong Liu  * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
2653cb645dcSTong Liu  */
2663cb645dcSTong Liu struct d40_interrupt_lookup {
2673cb645dcSTong Liu 	u32 src;
2683cb645dcSTong Liu 	u32 clr;
2693cb645dcSTong Liu 	bool is_error;
2703cb645dcSTong Liu 	int offset;
2713cb645dcSTong Liu };
2723cb645dcSTong Liu 
2733cb645dcSTong Liu 
2743cb645dcSTong Liu static struct d40_interrupt_lookup il_v4a[] = {
2753cb645dcSTong Liu 	{D40_DREG_LCTIS0, D40_DREG_LCICR0, false,  0},
2763cb645dcSTong Liu 	{D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
2773cb645dcSTong Liu 	{D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
2783cb645dcSTong Liu 	{D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
2793cb645dcSTong Liu 	{D40_DREG_LCEIS0, D40_DREG_LCICR0, true,   0},
2803cb645dcSTong Liu 	{D40_DREG_LCEIS1, D40_DREG_LCICR1, true,  32},
2813cb645dcSTong Liu 	{D40_DREG_LCEIS2, D40_DREG_LCICR2, true,  64},
2823cb645dcSTong Liu 	{D40_DREG_LCEIS3, D40_DREG_LCICR3, true,  96},
2833cb645dcSTong Liu 	{D40_DREG_PCTIS,  D40_DREG_PCICR,  false, D40_PHY_CHAN},
2843cb645dcSTong Liu 	{D40_DREG_PCEIS,  D40_DREG_PCICR,  true,  D40_PHY_CHAN},
2853cb645dcSTong Liu };
2863cb645dcSTong Liu 
2873cb645dcSTong Liu static struct d40_interrupt_lookup il_v4b[] = {
2883cb645dcSTong Liu 	{D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false,  0},
2893cb645dcSTong Liu 	{D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
2903cb645dcSTong Liu 	{D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
2913cb645dcSTong Liu 	{D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
2923cb645dcSTong Liu 	{D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
2933cb645dcSTong Liu 	{D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true,   0},
2943cb645dcSTong Liu 	{D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true,  32},
2953cb645dcSTong Liu 	{D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true,  64},
2963cb645dcSTong Liu 	{D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true,  96},
2973cb645dcSTong Liu 	{D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true,  128},
2983cb645dcSTong Liu 	{D40_DREG_CPCTIS,  D40_DREG_CPCICR,  false, D40_PHY_CHAN},
2993cb645dcSTong Liu 	{D40_DREG_CPCEIS,  D40_DREG_CPCICR,  true,  D40_PHY_CHAN},
3003cb645dcSTong Liu };
3013cb645dcSTong Liu 
3023cb645dcSTong Liu /**
3033cb645dcSTong Liu  * struct d40_reg_val - simple lookup struct
3043cb645dcSTong Liu  *
3053cb645dcSTong Liu  * @reg: The register.
3063cb645dcSTong Liu  * @val: The value that belongs to the register in reg.
3073cb645dcSTong Liu  */
3083cb645dcSTong Liu struct d40_reg_val {
3093cb645dcSTong Liu 	unsigned int reg;
3103cb645dcSTong Liu 	unsigned int val;
3113cb645dcSTong Liu };
3123cb645dcSTong Liu 
3133cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
3143cb645dcSTong Liu 	/* Clock every part of the DMA block from start */
3153cb645dcSTong Liu 	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},
3163cb645dcSTong Liu 
3173cb645dcSTong Liu 	/* Interrupts on all logical channels */
3183cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
3193cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
3203cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
3213cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
3223cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
3233cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
3243cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
3253cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
3263cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
3273cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
3283cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
3293cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
3303cb645dcSTong Liu };
3313cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
3323cb645dcSTong Liu 	/* Clock every part of the DMA block from start */
3333cb645dcSTong Liu 	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},
3343cb645dcSTong Liu 
3353cb645dcSTong Liu 	/* Interrupts on all logical channels */
3363cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
3373cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
3383cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
3393cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
3403cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
3413cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
3423cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
3433cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
3443cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
3453cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
3463cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
3473cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
3483cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
3493cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
3503cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
3513cb645dcSTong Liu };
3523cb645dcSTong Liu 
3533cb645dcSTong Liu /**
3548d318a50SLinus Walleij  * struct d40_lli_pool - Structure for keeping LLIs in memory
3558d318a50SLinus Walleij  *
3568d318a50SLinus Walleij  * @base: Pointer to memory area when the pre_alloc_lli's are not large
3578d318a50SLinus Walleij  * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
3588d318a50SLinus Walleij  * pre_alloc_lli is used.
359b00f938cSRabin Vincent  * @dma_addr: DMA address, if mapped
3608d318a50SLinus Walleij  * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
3618d318a50SLinus Walleij  * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
3628d318a50SLinus Walleij  * one buffer to one buffer.
3638d318a50SLinus Walleij  */
3648d318a50SLinus Walleij struct d40_lli_pool {
3658d318a50SLinus Walleij 	void	*base;
3668d318a50SLinus Walleij 	int	 size;
367b00f938cSRabin Vincent 	dma_addr_t	dma_addr;
3688d318a50SLinus Walleij 	/* Space for dst and src, plus an extra for padding */
3698d318a50SLinus Walleij 	u8	 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
3708d318a50SLinus Walleij };
3718d318a50SLinus Walleij 
3728d318a50SLinus Walleij /**
3738d318a50SLinus Walleij  * struct d40_desc - A descriptor is one DMA job.
3748d318a50SLinus Walleij  *
3758d318a50SLinus Walleij  * @lli_phy: LLI settings for physical channel. Both src and dst=
3768d318a50SLinus Walleij  * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
3778d318a50SLinus Walleij  * lli_len equals one.
3788d318a50SLinus Walleij  * @lli_log: Same as above but for logical channels.
3798d318a50SLinus Walleij  * @lli_pool: The pool with two entries pre-allocated.
380941b77a3SPer Friden  * @lli_len: Number of llis of current descriptor.
38125985edcSLucas De Marchi  * @lli_current: Number of transferred llis.
382698e4732SJonas Aaberg  * @lcla_alloc: Number of LCLA entries allocated.
3838d318a50SLinus Walleij  * @txd: DMA engine struct. Used for among other things for communication
3848d318a50SLinus Walleij  * during a transfer.
3858d318a50SLinus Walleij  * @node: List entry.
3868d318a50SLinus Walleij  * @is_in_client_list: true if the client owns this descriptor.
3877fb3e75eSNarayanan G  * @cyclic: true if this is a cyclic job
3888d318a50SLinus Walleij  *
3898d318a50SLinus Walleij  * This descriptor is used for both logical and physical transfers.
3908d318a50SLinus Walleij  */
3918d318a50SLinus Walleij struct d40_desc {
3928d318a50SLinus Walleij 	/* LLI physical */
3938d318a50SLinus Walleij 	struct d40_phy_lli_bidir	 lli_phy;
3948d318a50SLinus Walleij 	/* LLI logical */
3958d318a50SLinus Walleij 	struct d40_log_lli_bidir	 lli_log;
3968d318a50SLinus Walleij 
3978d318a50SLinus Walleij 	struct d40_lli_pool		 lli_pool;
398941b77a3SPer Friden 	int				 lli_len;
399698e4732SJonas Aaberg 	int				 lli_current;
400698e4732SJonas Aaberg 	int				 lcla_alloc;
4018d318a50SLinus Walleij 
4028d318a50SLinus Walleij 	struct dma_async_tx_descriptor	 txd;
4038d318a50SLinus Walleij 	struct list_head		 node;
4048d318a50SLinus Walleij 
4058d318a50SLinus Walleij 	bool				 is_in_client_list;
4060c842b55SRabin Vincent 	bool				 cyclic;
4078d318a50SLinus Walleij };
4088d318a50SLinus Walleij 
4098d318a50SLinus Walleij /**
4108d318a50SLinus Walleij  * struct d40_lcla_pool - LCLA pool settings and data.
4118d318a50SLinus Walleij  *
412508849adSLinus Walleij  * @base: The virtual address of LCLA. 18 bit aligned.
4130b851134SLee Jones  * @dma_addr: DMA address, if mapped
414508849adSLinus Walleij  * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
415508849adSLinus Walleij  * This pointer is only there for clean-up on error.
416508849adSLinus Walleij  * @pages: The number of pages needed for all physical channels.
417508849adSLinus Walleij  * Only used later for clean-up on error
4188d318a50SLinus Walleij  * @lock: Lock to protect the content in this struct.
419698e4732SJonas Aaberg  * @alloc_map: big map over which LCLA entry is own by which job.
4208d318a50SLinus Walleij  */
4218d318a50SLinus Walleij struct d40_lcla_pool {
4228d318a50SLinus Walleij 	void		*base;
423026cbc42SRabin Vincent 	dma_addr_t	dma_addr;
424508849adSLinus Walleij 	void		*base_unaligned;
425508849adSLinus Walleij 	int		 pages;
4268d318a50SLinus Walleij 	spinlock_t	 lock;
427698e4732SJonas Aaberg 	struct d40_desc	**alloc_map;
4288d318a50SLinus Walleij };
4298d318a50SLinus Walleij 
4308d318a50SLinus Walleij /**
4318d318a50SLinus Walleij  * struct d40_phy_res - struct for handling eventlines mapped to physical
4328d318a50SLinus Walleij  * channels.
4338d318a50SLinus Walleij  *
4348d318a50SLinus Walleij  * @lock: A lock protection this entity.
4357fb3e75eSNarayanan G  * @reserved: True if used by secure world or otherwise.
4368d318a50SLinus Walleij  * @num: The physical channel number of this entity.
4378d318a50SLinus Walleij  * @allocated_src: Bit mapped to show which src event line's are mapped to
4388d318a50SLinus Walleij  * this physical channel. Can also be free or physically allocated.
4398d318a50SLinus Walleij  * @allocated_dst: Same as for src but is dst.
4408d318a50SLinus Walleij  * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
441767a9675SJonas Aaberg  * event line number.
4427407048bSFabio Baltieri  * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
4438d318a50SLinus Walleij  */
4448d318a50SLinus Walleij struct d40_phy_res {
4458d318a50SLinus Walleij 	spinlock_t lock;
4467fb3e75eSNarayanan G 	bool	   reserved;
4478d318a50SLinus Walleij 	int	   num;
4488d318a50SLinus Walleij 	u32	   allocated_src;
4498d318a50SLinus Walleij 	u32	   allocated_dst;
4507407048bSFabio Baltieri 	bool	   use_soft_lli;
4518d318a50SLinus Walleij };
4528d318a50SLinus Walleij 
4538d318a50SLinus Walleij struct d40_base;
4548d318a50SLinus Walleij 
4558d318a50SLinus Walleij /**
4568d318a50SLinus Walleij  * struct d40_chan - Struct that describes a channel.
4578d318a50SLinus Walleij  *
4588d318a50SLinus Walleij  * @lock: A spinlock to protect this struct.
4598d318a50SLinus Walleij  * @log_num: The logical number, if any of this channel.
4608d318a50SLinus Walleij  * @pending_tx: The number of pending transfers. Used between interrupt handler
4618d318a50SLinus Walleij  * and tasklet.
4628d318a50SLinus Walleij  * @busy: Set to true when transfer is ongoing on this channel.
4632a614340SJonas Aaberg  * @phy_chan: Pointer to physical channel which this instance runs on. If this
4642a614340SJonas Aaberg  * point is NULL, then the channel is not allocated.
4658d318a50SLinus Walleij  * @chan: DMA engine handle.
4668d318a50SLinus Walleij  * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
4678d318a50SLinus Walleij  * transfer and call client callback.
4688d318a50SLinus Walleij  * @client: Cliented owned descriptor list.
469da063d26SPer Forlin  * @pending_queue: Submitted jobs, to be issued by issue_pending()
4708d318a50SLinus Walleij  * @active: Active descriptor.
4714226dd86SFabio Baltieri  * @done: Completed jobs
4728d318a50SLinus Walleij  * @queue: Queued jobs.
47382babbb3SPer Forlin  * @prepare_queue: Prepared jobs.
4748d318a50SLinus Walleij  * @dma_cfg: The client configuration of this dma channel.
4759e314ef3SVinod Koul  * @slave_config: DMA slave configuration.
476ce2ca125SRabin Vincent  * @configured: whether the dma_cfg configuration is valid
4778d318a50SLinus Walleij  * @base: Pointer to the device instance struct.
4788d318a50SLinus Walleij  * @src_def_cfg: Default cfg register setting for src.
4798d318a50SLinus Walleij  * @dst_def_cfg: Default cfg register setting for dst.
4808d318a50SLinus Walleij  * @log_def: Default logical channel settings.
4818d318a50SLinus Walleij  * @lcpa: Pointer to dst and src lcpa settings.
482ae752bf4Som prakash  * @runtime_addr: runtime configured address.
483ae752bf4Som prakash  * @runtime_direction: runtime configured direction.
4848d318a50SLinus Walleij  *
4858d318a50SLinus Walleij  * This struct can either "be" a logical or a physical channel.
4868d318a50SLinus Walleij  */
4878d318a50SLinus Walleij struct d40_chan {
4888d318a50SLinus Walleij 	spinlock_t			 lock;
4898d318a50SLinus Walleij 	int				 log_num;
4908d318a50SLinus Walleij 	int				 pending_tx;
4918d318a50SLinus Walleij 	bool				 busy;
4928d318a50SLinus Walleij 	struct d40_phy_res		*phy_chan;
4938d318a50SLinus Walleij 	struct dma_chan			 chan;
4948d318a50SLinus Walleij 	struct tasklet_struct		 tasklet;
4958d318a50SLinus Walleij 	struct list_head		 client;
496a8f3067bSPer Forlin 	struct list_head		 pending_queue;
4978d318a50SLinus Walleij 	struct list_head		 active;
4984226dd86SFabio Baltieri 	struct list_head		 done;
4998d318a50SLinus Walleij 	struct list_head		 queue;
50082babbb3SPer Forlin 	struct list_head		 prepare_queue;
5018d318a50SLinus Walleij 	struct stedma40_chan_cfg	 dma_cfg;
5029e314ef3SVinod Koul 	struct dma_slave_config		 slave_config;
503ce2ca125SRabin Vincent 	bool				 configured;
5048d318a50SLinus Walleij 	struct d40_base			*base;
5058d318a50SLinus Walleij 	/* Default register configurations */
5068d318a50SLinus Walleij 	u32				 src_def_cfg;
5078d318a50SLinus Walleij 	u32				 dst_def_cfg;
5088d318a50SLinus Walleij 	struct d40_def_lcsp		 log_def;
5098d318a50SLinus Walleij 	struct d40_log_lli_full		*lcpa;
51095e1400fSLinus Walleij 	/* Runtime reconfiguration */
51195e1400fSLinus Walleij 	dma_addr_t			runtime_addr;
512db8196dfSVinod Koul 	enum dma_transfer_direction	runtime_direction;
5138d318a50SLinus Walleij };
5148d318a50SLinus Walleij 
5158d318a50SLinus Walleij /**
5163cb645dcSTong Liu  * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
5173cb645dcSTong Liu  * controller
5183cb645dcSTong Liu  *
5193cb645dcSTong Liu  * @backup: the pointer to the registers address array for backup
5203cb645dcSTong Liu  * @backup_size: the size of the registers address array for backup
5213cb645dcSTong Liu  * @realtime_en: the realtime enable register
5223cb645dcSTong Liu  * @realtime_clear: the realtime clear register
5233cb645dcSTong Liu  * @high_prio_en: the high priority enable register
5243cb645dcSTong Liu  * @high_prio_clear: the high priority clear register
5253cb645dcSTong Liu  * @interrupt_en: the interrupt enable register
5263cb645dcSTong Liu  * @interrupt_clear: the interrupt clear register
5273cb645dcSTong Liu  * @il: the pointer to struct d40_interrupt_lookup
5283cb645dcSTong Liu  * @il_size: the size of d40_interrupt_lookup array
5293cb645dcSTong Liu  * @init_reg: the pointer to the struct d40_reg_val
5303cb645dcSTong Liu  * @init_reg_size: the size of d40_reg_val array
5313cb645dcSTong Liu  */
5323cb645dcSTong Liu struct d40_gen_dmac {
5333cb645dcSTong Liu 	u32				*backup;
5343cb645dcSTong Liu 	u32				 backup_size;
5353cb645dcSTong Liu 	u32				 realtime_en;
5363cb645dcSTong Liu 	u32				 realtime_clear;
5373cb645dcSTong Liu 	u32				 high_prio_en;
5383cb645dcSTong Liu 	u32				 high_prio_clear;
5393cb645dcSTong Liu 	u32				 interrupt_en;
5403cb645dcSTong Liu 	u32				 interrupt_clear;
5413cb645dcSTong Liu 	struct d40_interrupt_lookup	*il;
5423cb645dcSTong Liu 	u32				 il_size;
5433cb645dcSTong Liu 	struct d40_reg_val		*init_reg;
5443cb645dcSTong Liu 	u32				 init_reg_size;
5453cb645dcSTong Liu };
5463cb645dcSTong Liu 
5473cb645dcSTong Liu /**
5488d318a50SLinus Walleij  * struct d40_base - The big global struct, one for each probe'd instance.
5498d318a50SLinus Walleij  *
5508d318a50SLinus Walleij  * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
5518d318a50SLinus Walleij  * @execmd_lock: Lock for execute command usage since several channels share
5528d318a50SLinus Walleij  * the same physical register.
5538d318a50SLinus Walleij  * @dev: The device structure.
5548d318a50SLinus Walleij  * @virtbase: The virtual base address of the DMA's register.
555f4185592SLinus Walleij  * @rev: silicon revision detected.
5568d318a50SLinus Walleij  * @clk: Pointer to the DMA clock structure.
5578d318a50SLinus Walleij  * @irq: The IRQ number.
558a7dacb68SLee Jones  * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
559a7dacb68SLee Jones  * transfers).
5608d318a50SLinus Walleij  * @num_phy_chans: The number of physical channels. Read from HW. This
5618d318a50SLinus Walleij  * is the number of available channels for this driver, not counting "Secure
5628d318a50SLinus Walleij  * mode" allocated physical channels.
5638d318a50SLinus Walleij  * @num_log_chans: The number of logical channels. Calculated from
5648d318a50SLinus Walleij  * num_phy_chans.
5658d318a50SLinus Walleij  * @dma_both: dma_device channels that can do both memcpy and slave transfers.
5668d318a50SLinus Walleij  * @dma_slave: dma_device channels that can do only do slave transfers.
5678d318a50SLinus Walleij  * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
5687fb3e75eSNarayanan G  * @phy_chans: Room for all possible physical channels in system.
5698d318a50SLinus Walleij  * @log_chans: Room for all possible logical channels in system.
5708d318a50SLinus Walleij  * @lookup_log_chans: Used to map interrupt number to logical channel. Points
5718d318a50SLinus Walleij  * to log_chans entries.
5728d318a50SLinus Walleij  * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
5738d318a50SLinus Walleij  * to phy_chans entries.
5748d318a50SLinus Walleij  * @plat_data: Pointer to provided platform_data which is the driver
5758d318a50SLinus Walleij  * configuration.
57628c7a19dSNarayanan G  * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
5778d318a50SLinus Walleij  * @phy_res: Vector containing all physical channels.
5788d318a50SLinus Walleij  * @lcla_pool: lcla pool settings and data.
5798d318a50SLinus Walleij  * @lcpa_base: The virtual mapped address of LCPA.
5808d318a50SLinus Walleij  * @phy_lcpa: The physical address of the LCPA.
5818d318a50SLinus Walleij  * @lcpa_size: The size of the LCPA area.
582c675b1b4SJonas Aaberg  * @desc_slab: cache for descriptors.
5837fb3e75eSNarayanan G  * @reg_val_backup: Here the values of some hardware registers are stored
5847fb3e75eSNarayanan G  * before the DMA is powered off. They are restored when the power is back on.
5853cb645dcSTong Liu  * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
5863cb645dcSTong Liu  * later
5877fb3e75eSNarayanan G  * @reg_val_backup_chan: Backup data for standard channel parameter registers.
588e6a78511SKees Cook  * @regs_interrupt: Scratch space for registers during interrupt.
5897fb3e75eSNarayanan G  * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
5903cb645dcSTong Liu  * @gen_dmac: the struct for generic registers values to represent u8500/8540
5913cb645dcSTong Liu  * DMA controller
5928d318a50SLinus Walleij  */
5938d318a50SLinus Walleij struct d40_base {
5948d318a50SLinus Walleij 	spinlock_t			 interrupt_lock;
5958d318a50SLinus Walleij 	spinlock_t			 execmd_lock;
5968d318a50SLinus Walleij 	struct device			 *dev;
5978d318a50SLinus Walleij 	void __iomem			 *virtbase;
598f4185592SLinus Walleij 	u8				  rev:4;
5998d318a50SLinus Walleij 	struct clk			 *clk;
6008d318a50SLinus Walleij 	int				  irq;
601a7dacb68SLee Jones 	int				  num_memcpy_chans;
6028d318a50SLinus Walleij 	int				  num_phy_chans;
6038d318a50SLinus Walleij 	int				  num_log_chans;
6048d318a50SLinus Walleij 	struct dma_device		  dma_both;
6058d318a50SLinus Walleij 	struct dma_device		  dma_slave;
6068d318a50SLinus Walleij 	struct dma_device		  dma_memcpy;
6078d318a50SLinus Walleij 	struct d40_chan			 *phy_chans;
6088d318a50SLinus Walleij 	struct d40_chan			 *log_chans;
6098d318a50SLinus Walleij 	struct d40_chan			**lookup_log_chans;
6108d318a50SLinus Walleij 	struct d40_chan			**lookup_phy_chans;
6118d318a50SLinus Walleij 	struct stedma40_platform_data	 *plat_data;
61228c7a19dSNarayanan G 	struct regulator		 *lcpa_regulator;
6138d318a50SLinus Walleij 	/* Physical half channels */
6148d318a50SLinus Walleij 	struct d40_phy_res		 *phy_res;
6158d318a50SLinus Walleij 	struct d40_lcla_pool		  lcla_pool;
6168d318a50SLinus Walleij 	void				 *lcpa_base;
6178d318a50SLinus Walleij 	dma_addr_t			  phy_lcpa;
6188d318a50SLinus Walleij 	resource_size_t			  lcpa_size;
619c675b1b4SJonas Aaberg 	struct kmem_cache		 *desc_slab;
6207fb3e75eSNarayanan G 	u32				  reg_val_backup[BACKUP_REGS_SZ];
62184b3da14SLee Jones 	u32				  reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
6227fb3e75eSNarayanan G 	u32				 *reg_val_backup_chan;
623e6a78511SKees Cook 	u32				 *regs_interrupt;
6247fb3e75eSNarayanan G 	u16				  gcc_pwr_off_mask;
6253cb645dcSTong Liu 	struct d40_gen_dmac		  gen_dmac;
6268d318a50SLinus Walleij };
6278d318a50SLinus Walleij 
628262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c)
629262d2915SRabin Vincent {
630262d2915SRabin Vincent 	return &d40c->chan.dev->device;
631262d2915SRabin Vincent }
632262d2915SRabin Vincent 
633724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan)
634724a8577SRabin Vincent {
635724a8577SRabin Vincent 	return chan->log_num == D40_PHY_CHAN;
636724a8577SRabin Vincent }
637724a8577SRabin Vincent 
638724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan)
639724a8577SRabin Vincent {
640724a8577SRabin Vincent 	return !chan_is_physical(chan);
641724a8577SRabin Vincent }
642724a8577SRabin Vincent 
6438ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan)
6448ca84687SRabin Vincent {
6458ca84687SRabin Vincent 	return chan->base->virtbase + D40_DREG_PCBASE +
6468ca84687SRabin Vincent 	       chan->phy_chan->num * D40_DREG_PCDELTA;
6478ca84687SRabin Vincent }
6488ca84687SRabin Vincent 
6496db5a8baSRabin Vincent #define d40_err(dev, format, arg...)		\
6506db5a8baSRabin Vincent 	dev_err(dev, "[%s] " format, __func__, ## arg)
6516db5a8baSRabin Vincent 
6526db5a8baSRabin Vincent #define chan_err(d40c, format, arg...)		\
6536db5a8baSRabin Vincent 	d40_err(chan2dev(d40c), format, ## arg)
6546db5a8baSRabin Vincent 
6559e314ef3SVinod Koul static int d40_set_runtime_config_write(struct dma_chan *chan,
6569e314ef3SVinod Koul 				  struct dma_slave_config *config,
6579e314ef3SVinod Koul 				  enum dma_transfer_direction direction);
6589e314ef3SVinod Koul 
659b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
660dbd88788SRabin Vincent 			      int lli_len)
6618d318a50SLinus Walleij {
662dbd88788SRabin Vincent 	bool is_log = chan_is_logical(d40c);
6638d318a50SLinus Walleij 	u32 align;
6648d318a50SLinus Walleij 	void *base;
6658d318a50SLinus Walleij 
6668d318a50SLinus Walleij 	if (is_log)
6678d318a50SLinus Walleij 		align = sizeof(struct d40_log_lli);
6688d318a50SLinus Walleij 	else
6698d318a50SLinus Walleij 		align = sizeof(struct d40_phy_lli);
6708d318a50SLinus Walleij 
6718d318a50SLinus Walleij 	if (lli_len == 1) {
6728d318a50SLinus Walleij 		base = d40d->lli_pool.pre_alloc_lli;
6738d318a50SLinus Walleij 		d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
6748d318a50SLinus Walleij 		d40d->lli_pool.base = NULL;
6758d318a50SLinus Walleij 	} else {
676594ece4dSRabin Vincent 		d40d->lli_pool.size = lli_len * 2 * align;
6778d318a50SLinus Walleij 
6788d318a50SLinus Walleij 		base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
6798d318a50SLinus Walleij 		d40d->lli_pool.base = base;
6808d318a50SLinus Walleij 
6818d318a50SLinus Walleij 		if (d40d->lli_pool.base == NULL)
6828d318a50SLinus Walleij 			return -ENOMEM;
6838d318a50SLinus Walleij 	}
6848d318a50SLinus Walleij 
6858d318a50SLinus Walleij 	if (is_log) {
686d924abadSRabin Vincent 		d40d->lli_log.src = PTR_ALIGN(base, align);
687594ece4dSRabin Vincent 		d40d->lli_log.dst = d40d->lli_log.src + lli_len;
688b00f938cSRabin Vincent 
689b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = 0;
6908d318a50SLinus Walleij 	} else {
691d924abadSRabin Vincent 		d40d->lli_phy.src = PTR_ALIGN(base, align);
692594ece4dSRabin Vincent 		d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
693b00f938cSRabin Vincent 
694b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
695b00f938cSRabin Vincent 							 d40d->lli_phy.src,
696b00f938cSRabin Vincent 							 d40d->lli_pool.size,
697b00f938cSRabin Vincent 							 DMA_TO_DEVICE);
698b00f938cSRabin Vincent 
699b00f938cSRabin Vincent 		if (dma_mapping_error(d40c->base->dev,
700b00f938cSRabin Vincent 				      d40d->lli_pool.dma_addr)) {
701b00f938cSRabin Vincent 			kfree(d40d->lli_pool.base);
702b00f938cSRabin Vincent 			d40d->lli_pool.base = NULL;
703b00f938cSRabin Vincent 			d40d->lli_pool.dma_addr = 0;
704b00f938cSRabin Vincent 			return -ENOMEM;
705b00f938cSRabin Vincent 		}
7068d318a50SLinus Walleij 	}
7078d318a50SLinus Walleij 
7088d318a50SLinus Walleij 	return 0;
7098d318a50SLinus Walleij }
7108d318a50SLinus Walleij 
711b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
7128d318a50SLinus Walleij {
713b00f938cSRabin Vincent 	if (d40d->lli_pool.dma_addr)
714b00f938cSRabin Vincent 		dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
715b00f938cSRabin Vincent 				 d40d->lli_pool.size, DMA_TO_DEVICE);
716b00f938cSRabin Vincent 
7178d318a50SLinus Walleij 	kfree(d40d->lli_pool.base);
7188d318a50SLinus Walleij 	d40d->lli_pool.base = NULL;
7198d318a50SLinus Walleij 	d40d->lli_pool.size = 0;
7208d318a50SLinus Walleij 	d40d->lli_log.src = NULL;
7218d318a50SLinus Walleij 	d40d->lli_log.dst = NULL;
7228d318a50SLinus Walleij 	d40d->lli_phy.src = NULL;
7238d318a50SLinus Walleij 	d40d->lli_phy.dst = NULL;
7248d318a50SLinus Walleij }
7258d318a50SLinus Walleij 
726698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c,
727698e4732SJonas Aaberg 			      struct d40_desc *d40d)
728698e4732SJonas Aaberg {
729698e4732SJonas Aaberg 	unsigned long flags;
730698e4732SJonas Aaberg 	int i;
731698e4732SJonas Aaberg 	int ret = -EINVAL;
732698e4732SJonas Aaberg 
733698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
734698e4732SJonas Aaberg 
735698e4732SJonas Aaberg 	/*
736698e4732SJonas Aaberg 	 * Allocate both src and dst at the same time, therefore the half
737698e4732SJonas Aaberg 	 * start on 1 since 0 can't be used since zero is used as end marker.
738698e4732SJonas Aaberg 	 */
739698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7407ce529efSFabio Baltieri 		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7417ce529efSFabio Baltieri 
7427ce529efSFabio Baltieri 		if (!d40c->base->lcla_pool.alloc_map[idx]) {
7437ce529efSFabio Baltieri 			d40c->base->lcla_pool.alloc_map[idx] = d40d;
744698e4732SJonas Aaberg 			d40d->lcla_alloc++;
745698e4732SJonas Aaberg 			ret = i;
746698e4732SJonas Aaberg 			break;
747698e4732SJonas Aaberg 		}
748698e4732SJonas Aaberg 	}
749698e4732SJonas Aaberg 
750698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
751698e4732SJonas Aaberg 
752698e4732SJonas Aaberg 	return ret;
753698e4732SJonas Aaberg }
754698e4732SJonas Aaberg 
755698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c,
756698e4732SJonas Aaberg 			     struct d40_desc *d40d)
757698e4732SJonas Aaberg {
758698e4732SJonas Aaberg 	unsigned long flags;
759698e4732SJonas Aaberg 	int i;
760698e4732SJonas Aaberg 	int ret = -EINVAL;
761698e4732SJonas Aaberg 
762724a8577SRabin Vincent 	if (chan_is_physical(d40c))
763698e4732SJonas Aaberg 		return 0;
764698e4732SJonas Aaberg 
765698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
766698e4732SJonas Aaberg 
767698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7687ce529efSFabio Baltieri 		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7697ce529efSFabio Baltieri 
7707ce529efSFabio Baltieri 		if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
7717ce529efSFabio Baltieri 			d40c->base->lcla_pool.alloc_map[idx] = NULL;
772698e4732SJonas Aaberg 			d40d->lcla_alloc--;
773698e4732SJonas Aaberg 			if (d40d->lcla_alloc == 0) {
774698e4732SJonas Aaberg 				ret = 0;
775698e4732SJonas Aaberg 				break;
776698e4732SJonas Aaberg 			}
777698e4732SJonas Aaberg 		}
778698e4732SJonas Aaberg 	}
779698e4732SJonas Aaberg 
780698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
781698e4732SJonas Aaberg 
782698e4732SJonas Aaberg 	return ret;
783698e4732SJonas Aaberg 
784698e4732SJonas Aaberg }
785698e4732SJonas Aaberg 
7868d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d)
7878d318a50SLinus Walleij {
7888d318a50SLinus Walleij 	list_del(&d40d->node);
7898d318a50SLinus Walleij }
7908d318a50SLinus Walleij 
7918d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
7928d318a50SLinus Walleij {
793a2c15fa4SRabin Vincent 	struct d40_desc *desc = NULL;
794a2c15fa4SRabin Vincent 
795a2c15fa4SRabin Vincent 	if (!list_empty(&d40c->client)) {
7968d318a50SLinus Walleij 		struct d40_desc *d;
7978d318a50SLinus Walleij 		struct d40_desc *_d;
7988d318a50SLinus Walleij 
7997fb3e75eSNarayanan G 		list_for_each_entry_safe(d, _d, &d40c->client, node) {
8008d318a50SLinus Walleij 			if (async_tx_test_ack(&d->txd)) {
8018d318a50SLinus Walleij 				d40_desc_remove(d);
802a2c15fa4SRabin Vincent 				desc = d;
803a2c15fa4SRabin Vincent 				memset(desc, 0, sizeof(*desc));
804c675b1b4SJonas Aaberg 				break;
8058d318a50SLinus Walleij 			}
8068d318a50SLinus Walleij 		}
8077fb3e75eSNarayanan G 	}
808a2c15fa4SRabin Vincent 
809a2c15fa4SRabin Vincent 	if (!desc)
810a2c15fa4SRabin Vincent 		desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
811a2c15fa4SRabin Vincent 
812a2c15fa4SRabin Vincent 	if (desc)
813a2c15fa4SRabin Vincent 		INIT_LIST_HEAD(&desc->node);
814a2c15fa4SRabin Vincent 
815a2c15fa4SRabin Vincent 	return desc;
8168d318a50SLinus Walleij }
8178d318a50SLinus Walleij 
8188d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
8198d318a50SLinus Walleij {
820698e4732SJonas Aaberg 
821b00f938cSRabin Vincent 	d40_pool_lli_free(d40c, d40d);
822698e4732SJonas Aaberg 	d40_lcla_free_all(d40c, d40d);
823c675b1b4SJonas Aaberg 	kmem_cache_free(d40c->base->desc_slab, d40d);
8248d318a50SLinus Walleij }
8258d318a50SLinus Walleij 
8268d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
8278d318a50SLinus Walleij {
8288d318a50SLinus Walleij 	list_add_tail(&desc->node, &d40c->active);
8298d318a50SLinus Walleij }
8308d318a50SLinus Walleij 
8311c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
8321c4b0927SRabin Vincent {
8331c4b0927SRabin Vincent 	struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
8341c4b0927SRabin Vincent 	struct d40_phy_lli *lli_src = desc->lli_phy.src;
8351c4b0927SRabin Vincent 	void __iomem *base = chan_base(chan);
8361c4b0927SRabin Vincent 
8371c4b0927SRabin Vincent 	writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
8381c4b0927SRabin Vincent 	writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
8391c4b0927SRabin Vincent 	writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
8401c4b0927SRabin Vincent 	writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
8411c4b0927SRabin Vincent 
8421c4b0927SRabin Vincent 	writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
8431c4b0927SRabin Vincent 	writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
8441c4b0927SRabin Vincent 	writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
8451c4b0927SRabin Vincent 	writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
8461c4b0927SRabin Vincent }
8471c4b0927SRabin Vincent 
8484226dd86SFabio Baltieri static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
8494226dd86SFabio Baltieri {
8504226dd86SFabio Baltieri 	list_add_tail(&desc->node, &d40c->done);
8514226dd86SFabio Baltieri }
8524226dd86SFabio Baltieri 
853e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
854698e4732SJonas Aaberg {
855e65889c7SRabin Vincent 	struct d40_lcla_pool *pool = &chan->base->lcla_pool;
856e65889c7SRabin Vincent 	struct d40_log_lli_bidir *lli = &desc->lli_log;
857e65889c7SRabin Vincent 	int lli_current = desc->lli_current;
858e65889c7SRabin Vincent 	int lli_len = desc->lli_len;
8590c842b55SRabin Vincent 	bool cyclic = desc->cyclic;
860e65889c7SRabin Vincent 	int curr_lcla = -EINVAL;
8610c842b55SRabin Vincent 	int first_lcla = 0;
86228c7a19dSNarayanan G 	bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
8630c842b55SRabin Vincent 	bool linkback;
864698e4732SJonas Aaberg 
8650c842b55SRabin Vincent 	/*
8660c842b55SRabin Vincent 	 * We may have partially running cyclic transfers, in case we did't get
8670c842b55SRabin Vincent 	 * enough LCLA entries.
8680c842b55SRabin Vincent 	 */
8690c842b55SRabin Vincent 	linkback = cyclic && lli_current == 0;
8700c842b55SRabin Vincent 
8710c842b55SRabin Vincent 	/*
8720c842b55SRabin Vincent 	 * For linkback, we need one LCLA even with only one link, because we
8730c842b55SRabin Vincent 	 * can't link back to the one in LCPA space
8740c842b55SRabin Vincent 	 */
8750c842b55SRabin Vincent 	if (linkback || (lli_len - lli_current > 1)) {
8767407048bSFabio Baltieri 		/*
8777407048bSFabio Baltieri 		 * If the channel is expected to use only soft_lli don't
8787407048bSFabio Baltieri 		 * allocate a lcla. This is to avoid a HW issue that exists
8797407048bSFabio Baltieri 		 * in some controller during a peripheral to memory transfer
8807407048bSFabio Baltieri 		 * that uses linked lists.
8817407048bSFabio Baltieri 		 */
8827407048bSFabio Baltieri 		if (!(chan->phy_chan->use_soft_lli &&
8832c2b62d5SLee Jones 			chan->dma_cfg.dir == DMA_DEV_TO_MEM))
884e65889c7SRabin Vincent 			curr_lcla = d40_lcla_alloc_one(chan, desc);
8857407048bSFabio Baltieri 
8860c842b55SRabin Vincent 		first_lcla = curr_lcla;
8870c842b55SRabin Vincent 	}
8880c842b55SRabin Vincent 
8890c842b55SRabin Vincent 	/*
8900c842b55SRabin Vincent 	 * For linkback, we normally load the LCPA in the loop since we need to
8910c842b55SRabin Vincent 	 * link it to the second LCLA and not the first.  However, if we
8920c842b55SRabin Vincent 	 * couldn't even get a first LCLA, then we have to run in LCPA and
8930c842b55SRabin Vincent 	 * reload manually.
8940c842b55SRabin Vincent 	 */
8950c842b55SRabin Vincent 	if (!linkback || curr_lcla == -EINVAL) {
8960c842b55SRabin Vincent 		unsigned int flags = 0;
8970c842b55SRabin Vincent 
8980c842b55SRabin Vincent 		if (curr_lcla == -EINVAL)
8990c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
900698e4732SJonas Aaberg 
901e65889c7SRabin Vincent 		d40_log_lli_lcpa_write(chan->lcpa,
902e65889c7SRabin Vincent 				       &lli->dst[lli_current],
903e65889c7SRabin Vincent 				       &lli->src[lli_current],
9040c842b55SRabin Vincent 				       curr_lcla,
9050c842b55SRabin Vincent 				       flags);
906e65889c7SRabin Vincent 		lli_current++;
9070c842b55SRabin Vincent 	}
9086045f0bbSRabin Vincent 
9096045f0bbSRabin Vincent 	if (curr_lcla < 0)
9104d8673a0SMarkus Elfring 		goto set_current;
9116045f0bbSRabin Vincent 
912e65889c7SRabin Vincent 	for (; lli_current < lli_len; lli_current++) {
913e65889c7SRabin Vincent 		unsigned int lcla_offset = chan->phy_chan->num * 1024 +
914026cbc42SRabin Vincent 					   8 * curr_lcla * 2;
915026cbc42SRabin Vincent 		struct d40_log_lli *lcla = pool->base + lcla_offset;
9160c842b55SRabin Vincent 		unsigned int flags = 0;
917e65889c7SRabin Vincent 		int next_lcla;
918698e4732SJonas Aaberg 
919e65889c7SRabin Vincent 		if (lli_current + 1 < lli_len)
920e65889c7SRabin Vincent 			next_lcla = d40_lcla_alloc_one(chan, desc);
921698e4732SJonas Aaberg 		else
9220c842b55SRabin Vincent 			next_lcla = linkback ? first_lcla : -EINVAL;
923698e4732SJonas Aaberg 
9240c842b55SRabin Vincent 		if (cyclic || next_lcla == -EINVAL)
9250c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
9260c842b55SRabin Vincent 
9270c842b55SRabin Vincent 		if (linkback && curr_lcla == first_lcla) {
9280c842b55SRabin Vincent 			/* First link goes in both LCPA and LCLA */
9290c842b55SRabin Vincent 			d40_log_lli_lcpa_write(chan->lcpa,
9300c842b55SRabin Vincent 					       &lli->dst[lli_current],
9310c842b55SRabin Vincent 					       &lli->src[lli_current],
9320c842b55SRabin Vincent 					       next_lcla, flags);
9330c842b55SRabin Vincent 		}
9340c842b55SRabin Vincent 
9350c842b55SRabin Vincent 		/*
9360c842b55SRabin Vincent 		 * One unused LCLA in the cyclic case if the very first
9370c842b55SRabin Vincent 		 * next_lcla fails...
9380c842b55SRabin Vincent 		 */
939698e4732SJonas Aaberg 		d40_log_lli_lcla_write(lcla,
940e65889c7SRabin Vincent 				       &lli->dst[lli_current],
941e65889c7SRabin Vincent 				       &lli->src[lli_current],
9420c842b55SRabin Vincent 				       next_lcla, flags);
943698e4732SJonas Aaberg 
94428c7a19dSNarayanan G 		/*
94528c7a19dSNarayanan G 		 * Cache maintenance is not needed if lcla is
94628c7a19dSNarayanan G 		 * mapped in esram
94728c7a19dSNarayanan G 		 */
94828c7a19dSNarayanan G 		if (!use_esram_lcla) {
949e65889c7SRabin Vincent 			dma_sync_single_range_for_device(chan->base->dev,
950026cbc42SRabin Vincent 						pool->dma_addr, lcla_offset,
951698e4732SJonas Aaberg 						2 * sizeof(struct d40_log_lli),
952698e4732SJonas Aaberg 						DMA_TO_DEVICE);
95328c7a19dSNarayanan G 		}
954698e4732SJonas Aaberg 		curr_lcla = next_lcla;
955698e4732SJonas Aaberg 
9560c842b55SRabin Vincent 		if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
957e65889c7SRabin Vincent 			lli_current++;
958698e4732SJonas Aaberg 			break;
959698e4732SJonas Aaberg 		}
960e65889c7SRabin Vincent 	}
9614d8673a0SMarkus Elfring  set_current:
962e65889c7SRabin Vincent 	desc->lli_current = lli_current;
963698e4732SJonas Aaberg }
964e65889c7SRabin Vincent 
965e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
966e65889c7SRabin Vincent {
967e65889c7SRabin Vincent 	if (chan_is_physical(d40c)) {
968e65889c7SRabin Vincent 		d40_phy_lli_load(d40c, d40d);
969e65889c7SRabin Vincent 		d40d->lli_current = d40d->lli_len;
970e65889c7SRabin Vincent 	} else
971e65889c7SRabin Vincent 		d40_log_lli_to_lcxa(d40c, d40d);
972698e4732SJonas Aaberg }
973698e4732SJonas Aaberg 
9748d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
9758d318a50SLinus Walleij {
976360af35bSMasahiro Yamada 	return list_first_entry_or_null(&d40c->active, struct d40_desc, node);
9778d318a50SLinus Walleij }
9788d318a50SLinus Walleij 
9797404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */
9808d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
9818d318a50SLinus Walleij {
9827404368cSPer Forlin 	d40_desc_remove(desc);
9837404368cSPer Forlin 	desc->is_in_client_list = false;
984a8f3067bSPer Forlin 	list_add_tail(&desc->node, &d40c->pending_queue);
985a8f3067bSPer Forlin }
986a8f3067bSPer Forlin 
987a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
988a8f3067bSPer Forlin {
989360af35bSMasahiro Yamada 	return list_first_entry_or_null(&d40c->pending_queue, struct d40_desc,
990a8f3067bSPer Forlin 					node);
9918d318a50SLinus Walleij }
9928d318a50SLinus Walleij 
9938d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
9948d318a50SLinus Walleij {
995360af35bSMasahiro Yamada 	return list_first_entry_or_null(&d40c->queue, struct d40_desc, node);
9968d318a50SLinus Walleij }
9978d318a50SLinus Walleij 
9984226dd86SFabio Baltieri static struct d40_desc *d40_first_done(struct d40_chan *d40c)
9994226dd86SFabio Baltieri {
1000360af35bSMasahiro Yamada 	return list_first_entry_or_null(&d40c->done, struct d40_desc, node);
10014226dd86SFabio Baltieri }
10024226dd86SFabio Baltieri 
1003d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize)
1004d49278e3SPer Forlin {
1005d49278e3SPer Forlin 	if (is_log) {
1006d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_LOG_1)
1007d49278e3SPer Forlin 			return 1;
1008d49278e3SPer Forlin 	} else {
1009d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_PHY_1)
1010d49278e3SPer Forlin 			return 1;
1011d49278e3SPer Forlin 	}
10128d318a50SLinus Walleij 
1013d49278e3SPer Forlin 	return 2 << psize;
1014d49278e3SPer Forlin }
1015d49278e3SPer Forlin 
1016d49278e3SPer Forlin /*
1017d49278e3SPer Forlin  * The dma only supports transmitting packages up to
101843f2e1a3SLee Jones  * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
101943f2e1a3SLee Jones  *
102043f2e1a3SLee Jones  * Calculate the total number of dma elements required to send the entire sg list.
1021d49278e3SPer Forlin  */
1022d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1023d49278e3SPer Forlin {
1024d49278e3SPer Forlin 	int dmalen;
1025d49278e3SPer Forlin 	u32 max_w = max(data_width1, data_width2);
1026d49278e3SPer Forlin 	u32 min_w = min(data_width1, data_width2);
102743f2e1a3SLee Jones 	u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1028d49278e3SPer Forlin 
1029d49278e3SPer Forlin 	if (seg_max > STEDMA40_MAX_SEG_SIZE)
103043f2e1a3SLee Jones 		seg_max -= max_w;
1031d49278e3SPer Forlin 
103243f2e1a3SLee Jones 	if (!IS_ALIGNED(size, max_w))
1033d49278e3SPer Forlin 		return -EINVAL;
1034d49278e3SPer Forlin 
1035d49278e3SPer Forlin 	if (size <= seg_max)
1036d49278e3SPer Forlin 		dmalen = 1;
1037d49278e3SPer Forlin 	else {
1038d49278e3SPer Forlin 		dmalen = size / seg_max;
1039d49278e3SPer Forlin 		if (dmalen * seg_max < size)
1040d49278e3SPer Forlin 			dmalen++;
1041d49278e3SPer Forlin 	}
1042d49278e3SPer Forlin 	return dmalen;
1043d49278e3SPer Forlin }
1044d49278e3SPer Forlin 
1045d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1046d49278e3SPer Forlin 			   u32 data_width1, u32 data_width2)
1047d49278e3SPer Forlin {
1048d49278e3SPer Forlin 	struct scatterlist *sg;
1049d49278e3SPer Forlin 	int i;
1050d49278e3SPer Forlin 	int len = 0;
1051d49278e3SPer Forlin 	int ret;
1052d49278e3SPer Forlin 
1053d49278e3SPer Forlin 	for_each_sg(sgl, sg, sg_len, i) {
1054d49278e3SPer Forlin 		ret = d40_size_2_dmalen(sg_dma_len(sg),
1055d49278e3SPer Forlin 					data_width1, data_width2);
1056d49278e3SPer Forlin 		if (ret < 0)
1057d49278e3SPer Forlin 			return ret;
1058d49278e3SPer Forlin 		len += ret;
1059d49278e3SPer Forlin 	}
1060d49278e3SPer Forlin 	return len;
1061d49278e3SPer Forlin }
1062d49278e3SPer Forlin 
10631bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c,
10648d318a50SLinus Walleij 				     enum d40_command command)
10658d318a50SLinus Walleij {
1066767a9675SJonas Aaberg 	u32 status;
1067767a9675SJonas Aaberg 	int i;
10688d318a50SLinus Walleij 	void __iomem *active_reg;
10698d318a50SLinus Walleij 	int ret = 0;
10708d318a50SLinus Walleij 	unsigned long flags;
10711d392a7bSJonas Aaberg 	u32 wmask;
10728d318a50SLinus Walleij 
10731bdae6f4SNarayanan G 	if (command == D40_DMA_STOP) {
10741bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
10751bdae6f4SNarayanan G 		if (ret)
10761bdae6f4SNarayanan G 			return ret;
10771bdae6f4SNarayanan G 	}
10781bdae6f4SNarayanan G 
10798d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->base->execmd_lock, flags);
10808d318a50SLinus Walleij 
10818d318a50SLinus Walleij 	if (d40c->phy_chan->num % 2 == 0)
10828d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
10838d318a50SLinus Walleij 	else
10848d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
10858d318a50SLinus Walleij 
10868d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
10878d318a50SLinus Walleij 		status = (readl(active_reg) &
10888d318a50SLinus Walleij 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
10898d318a50SLinus Walleij 			D40_CHAN_POS(d40c->phy_chan->num);
10908d318a50SLinus Walleij 
10918d318a50SLinus Walleij 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1092b140ea0fSMarkus Elfring 			goto unlock;
10938d318a50SLinus Walleij 	}
10948d318a50SLinus Walleij 
10951d392a7bSJonas Aaberg 	wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
10961d392a7bSJonas Aaberg 	writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
10971d392a7bSJonas Aaberg 	       active_reg);
10988d318a50SLinus Walleij 
10998d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
11008d318a50SLinus Walleij 
11018d318a50SLinus Walleij 		for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
11028d318a50SLinus Walleij 			status = (readl(active_reg) &
11038d318a50SLinus Walleij 				  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
11048d318a50SLinus Walleij 				D40_CHAN_POS(d40c->phy_chan->num);
11058d318a50SLinus Walleij 
11068d318a50SLinus Walleij 			cpu_relax();
11078d318a50SLinus Walleij 			/*
11088d318a50SLinus Walleij 			 * Reduce the number of bus accesses while
11098d318a50SLinus Walleij 			 * waiting for the DMA to suspend.
11108d318a50SLinus Walleij 			 */
11118d318a50SLinus Walleij 			udelay(3);
11128d318a50SLinus Walleij 
11138d318a50SLinus Walleij 			if (status == D40_DMA_STOP ||
11148d318a50SLinus Walleij 			    status == D40_DMA_SUSPENDED)
11158d318a50SLinus Walleij 				break;
11168d318a50SLinus Walleij 		}
11178d318a50SLinus Walleij 
11188d318a50SLinus Walleij 		if (i == D40_SUSPEND_MAX_IT) {
11196db5a8baSRabin Vincent 			chan_err(d40c,
11206db5a8baSRabin Vincent 				"unable to suspend the chl %d (log: %d) status %x\n",
11216db5a8baSRabin Vincent 				d40c->phy_chan->num, d40c->log_num,
11228d318a50SLinus Walleij 				status);
11238d318a50SLinus Walleij 			dump_stack();
11248d318a50SLinus Walleij 			ret = -EBUSY;
11258d318a50SLinus Walleij 		}
11268d318a50SLinus Walleij 
11278d318a50SLinus Walleij 	}
1128b140ea0fSMarkus Elfring  unlock:
11298d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
11308d318a50SLinus Walleij 	return ret;
11318d318a50SLinus Walleij }
11328d318a50SLinus Walleij 
11338d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c)
11348d318a50SLinus Walleij {
11358d318a50SLinus Walleij 	struct d40_desc *d40d;
11367404368cSPer Forlin 	struct d40_desc *_d;
11378d318a50SLinus Walleij 
11384226dd86SFabio Baltieri 	/* Release completed descriptors */
11394226dd86SFabio Baltieri 	while ((d40d = d40_first_done(d40c))) {
11404226dd86SFabio Baltieri 		d40_desc_remove(d40d);
11414226dd86SFabio Baltieri 		d40_desc_free(d40c, d40d);
11424226dd86SFabio Baltieri 	}
11434226dd86SFabio Baltieri 
11448d318a50SLinus Walleij 	/* Release active descriptors */
11458d318a50SLinus Walleij 	while ((d40d = d40_first_active_get(d40c))) {
11468d318a50SLinus Walleij 		d40_desc_remove(d40d);
11478d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
11488d318a50SLinus Walleij 	}
11498d318a50SLinus Walleij 
11508d318a50SLinus Walleij 	/* Release queued descriptors waiting for transfer */
11518d318a50SLinus Walleij 	while ((d40d = d40_first_queued(d40c))) {
11528d318a50SLinus Walleij 		d40_desc_remove(d40d);
11538d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
11548d318a50SLinus Walleij 	}
11558d318a50SLinus Walleij 
1156a8f3067bSPer Forlin 	/* Release pending descriptors */
1157a8f3067bSPer Forlin 	while ((d40d = d40_first_pending(d40c))) {
1158a8f3067bSPer Forlin 		d40_desc_remove(d40d);
1159a8f3067bSPer Forlin 		d40_desc_free(d40c, d40d);
1160a8f3067bSPer Forlin 	}
11618d318a50SLinus Walleij 
11627404368cSPer Forlin 	/* Release client owned descriptors */
11637404368cSPer Forlin 	if (!list_empty(&d40c->client))
11647404368cSPer Forlin 		list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
11657404368cSPer Forlin 			d40_desc_remove(d40d);
11667404368cSPer Forlin 			d40_desc_free(d40c, d40d);
11677404368cSPer Forlin 		}
11687404368cSPer Forlin 
116982babbb3SPer Forlin 	/* Release descriptors in prepare queue */
117082babbb3SPer Forlin 	if (!list_empty(&d40c->prepare_queue))
117182babbb3SPer Forlin 		list_for_each_entry_safe(d40d, _d,
117282babbb3SPer Forlin 					 &d40c->prepare_queue, node) {
117382babbb3SPer Forlin 			d40_desc_remove(d40d);
117482babbb3SPer Forlin 			d40_desc_free(d40c, d40d);
117582babbb3SPer Forlin 		}
11767404368cSPer Forlin 
11778d318a50SLinus Walleij 	d40c->pending_tx = 0;
11788d318a50SLinus Walleij }
11798d318a50SLinus Walleij 
11801bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c,
11811bdae6f4SNarayanan G 				   enum d40_events event_type, u32 event,
11821bdae6f4SNarayanan G 				   int reg)
1183262d2915SRabin Vincent {
11848ca84687SRabin Vincent 	void __iomem *addr = chan_base(d40c) + reg;
1185262d2915SRabin Vincent 	int tries;
11861bdae6f4SNarayanan G 	u32 status;
1187262d2915SRabin Vincent 
11881bdae6f4SNarayanan G 	switch (event_type) {
11891bdae6f4SNarayanan G 
11901bdae6f4SNarayanan G 	case D40_DEACTIVATE_EVENTLINE:
11911bdae6f4SNarayanan G 
1192262d2915SRabin Vincent 		writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1193262d2915SRabin Vincent 		       | ~D40_EVENTLINE_MASK(event), addr);
11941bdae6f4SNarayanan G 		break;
11951bdae6f4SNarayanan G 
11961bdae6f4SNarayanan G 	case D40_SUSPEND_REQ_EVENTLINE:
11971bdae6f4SNarayanan G 		status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
11981bdae6f4SNarayanan G 			  D40_EVENTLINE_POS(event);
11991bdae6f4SNarayanan G 
12001bdae6f4SNarayanan G 		if (status == D40_DEACTIVATE_EVENTLINE ||
12011bdae6f4SNarayanan G 		    status == D40_SUSPEND_REQ_EVENTLINE)
12021bdae6f4SNarayanan G 			break;
12031bdae6f4SNarayanan G 
12041bdae6f4SNarayanan G 		writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
12051bdae6f4SNarayanan G 		       | ~D40_EVENTLINE_MASK(event), addr);
12061bdae6f4SNarayanan G 
12071bdae6f4SNarayanan G 		for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
12081bdae6f4SNarayanan G 
12091bdae6f4SNarayanan G 			status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
12101bdae6f4SNarayanan G 				  D40_EVENTLINE_POS(event);
12111bdae6f4SNarayanan G 
12121bdae6f4SNarayanan G 			cpu_relax();
12131bdae6f4SNarayanan G 			/*
12141bdae6f4SNarayanan G 			 * Reduce the number of bus accesses while
12151bdae6f4SNarayanan G 			 * waiting for the DMA to suspend.
12161bdae6f4SNarayanan G 			 */
12171bdae6f4SNarayanan G 			udelay(3);
12181bdae6f4SNarayanan G 
12191bdae6f4SNarayanan G 			if (status == D40_DEACTIVATE_EVENTLINE)
12201bdae6f4SNarayanan G 				break;
1221262d2915SRabin Vincent 		}
1222262d2915SRabin Vincent 
12231bdae6f4SNarayanan G 		if (tries == D40_SUSPEND_MAX_IT) {
12241bdae6f4SNarayanan G 			chan_err(d40c,
12251bdae6f4SNarayanan G 				"unable to stop the event_line chl %d (log: %d)"
12261bdae6f4SNarayanan G 				"status %x\n", d40c->phy_chan->num,
12271bdae6f4SNarayanan G 				 d40c->log_num, status);
12281bdae6f4SNarayanan G 		}
12291bdae6f4SNarayanan G 		break;
12301bdae6f4SNarayanan G 
12311bdae6f4SNarayanan G 	case D40_ACTIVATE_EVENTLINE:
1232262d2915SRabin Vincent 	/*
1233262d2915SRabin Vincent 	 * The hardware sometimes doesn't register the enable when src and dst
1234262d2915SRabin Vincent 	 * event lines are active on the same logical channel.  Retry to ensure
1235262d2915SRabin Vincent 	 * it does.  Usually only one retry is sufficient.
1236262d2915SRabin Vincent 	 */
1237262d2915SRabin Vincent 		tries = 100;
1238262d2915SRabin Vincent 		while (--tries) {
12391bdae6f4SNarayanan G 			writel((D40_ACTIVATE_EVENTLINE <<
12401bdae6f4SNarayanan G 				D40_EVENTLINE_POS(event)) |
12411bdae6f4SNarayanan G 				~D40_EVENTLINE_MASK(event), addr);
1242262d2915SRabin Vincent 
1243262d2915SRabin Vincent 			if (readl(addr) & D40_EVENTLINE_MASK(event))
1244262d2915SRabin Vincent 				break;
1245262d2915SRabin Vincent 		}
1246262d2915SRabin Vincent 
1247262d2915SRabin Vincent 		if (tries != 99)
1248262d2915SRabin Vincent 			dev_dbg(chan2dev(d40c),
1249262d2915SRabin Vincent 				"[%s] workaround enable S%cLNK (%d tries)\n",
1250262d2915SRabin Vincent 				__func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1251262d2915SRabin Vincent 				100 - tries);
1252262d2915SRabin Vincent 
1253262d2915SRabin Vincent 		WARN_ON(!tries);
12541bdae6f4SNarayanan G 		break;
12551bdae6f4SNarayanan G 
12561bdae6f4SNarayanan G 	case D40_ROUND_EVENTLINE:
12571bdae6f4SNarayanan G 		BUG();
12581bdae6f4SNarayanan G 		break;
12591bdae6f4SNarayanan G 
12601bdae6f4SNarayanan G 	}
1261262d2915SRabin Vincent }
1262262d2915SRabin Vincent 
12631bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c,
12641bdae6f4SNarayanan G 				 enum d40_events event_type)
12658d318a50SLinus Walleij {
126626955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
126726955c07SLee Jones 
12688d318a50SLinus Walleij 	/* Enable event line connected to device (or memcpy) */
12692c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
12702c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
12711bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
12728d318a50SLinus Walleij 				       D40_CHAN_REG_SSLNK);
1273262d2915SRabin Vincent 
12742c2b62d5SLee Jones 	if (d40c->dma_cfg.dir !=  DMA_DEV_TO_MEM)
12751bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
12768d318a50SLinus Walleij 				       D40_CHAN_REG_SDLNK);
12778d318a50SLinus Walleij }
12788d318a50SLinus Walleij 
1279a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c)
12808d318a50SLinus Walleij {
12818ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
1282be8cb7dfSJonas Aaberg 	u32 val;
12838d318a50SLinus Walleij 
12848ca84687SRabin Vincent 	val = readl(chanbase + D40_CHAN_REG_SSLNK);
12858ca84687SRabin Vincent 	val |= readl(chanbase + D40_CHAN_REG_SDLNK);
12868d318a50SLinus Walleij 
1287a5ebca47SJonas Aaberg 	return val;
12888d318a50SLinus Walleij }
12898d318a50SLinus Walleij 
12901bdae6f4SNarayanan G static int
12911bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
12921bdae6f4SNarayanan G {
12931bdae6f4SNarayanan G 	unsigned long flags;
12941bdae6f4SNarayanan G 	int ret = 0;
12951bdae6f4SNarayanan G 	u32 active_status;
12961bdae6f4SNarayanan G 	void __iomem *active_reg;
12971bdae6f4SNarayanan G 
12981bdae6f4SNarayanan G 	if (d40c->phy_chan->num % 2 == 0)
12991bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
13001bdae6f4SNarayanan G 	else
13011bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
13021bdae6f4SNarayanan G 
13031bdae6f4SNarayanan G 
13041bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->phy_chan->lock, flags);
13051bdae6f4SNarayanan G 
13061bdae6f4SNarayanan G 	switch (command) {
13071bdae6f4SNarayanan G 	case D40_DMA_STOP:
13081bdae6f4SNarayanan G 	case D40_DMA_SUSPEND_REQ:
13091bdae6f4SNarayanan G 
13101bdae6f4SNarayanan G 		active_status = (readl(active_reg) &
13111bdae6f4SNarayanan G 				 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
13121bdae6f4SNarayanan G 				 D40_CHAN_POS(d40c->phy_chan->num);
13131bdae6f4SNarayanan G 
13141bdae6f4SNarayanan G 		if (active_status == D40_DMA_RUN)
13151bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
13161bdae6f4SNarayanan G 		else
13171bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
13181bdae6f4SNarayanan G 
13191bdae6f4SNarayanan G 		if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
13201bdae6f4SNarayanan G 			ret = __d40_execute_command_phy(d40c, command);
13211bdae6f4SNarayanan G 
13221bdae6f4SNarayanan G 		break;
13231bdae6f4SNarayanan G 
13241bdae6f4SNarayanan G 	case D40_DMA_RUN:
13251bdae6f4SNarayanan G 
13261bdae6f4SNarayanan G 		d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
13271bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, command);
13281bdae6f4SNarayanan G 		break;
13291bdae6f4SNarayanan G 
13301bdae6f4SNarayanan G 	case D40_DMA_SUSPENDED:
13311bdae6f4SNarayanan G 		BUG();
13321bdae6f4SNarayanan G 		break;
13331bdae6f4SNarayanan G 	}
13341bdae6f4SNarayanan G 
13351bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
13361bdae6f4SNarayanan G 	return ret;
13371bdae6f4SNarayanan G }
13381bdae6f4SNarayanan G 
13391bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c,
13401bdae6f4SNarayanan G 				       enum d40_command command)
13411bdae6f4SNarayanan G {
13421bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
13431bdae6f4SNarayanan G 		return __d40_execute_command_log(d40c, command);
13441bdae6f4SNarayanan G 	else
13451bdae6f4SNarayanan G 		return __d40_execute_command_phy(d40c, command);
13461bdae6f4SNarayanan G }
13471bdae6f4SNarayanan G 
134820a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c)
134920a5b6d0SRabin Vincent {
135020a5b6d0SRabin Vincent 	static const unsigned int phy_map[] = {
135120a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_BASIC_MODE]
135220a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_BASIC,
135320a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_MODULO_MODE]
135420a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_MODULO,
135520a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_DOUBLE_DST_MODE]
135620a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_DOUBLE_DST,
135720a5b6d0SRabin Vincent 	};
135820a5b6d0SRabin Vincent 	static const unsigned int log_map[] = {
135920a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_PHY_DST_LOG]
136020a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
136120a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_PHY]
136220a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
136320a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_LOG]
136420a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
136520a5b6d0SRabin Vincent 	};
136620a5b6d0SRabin Vincent 
1367724a8577SRabin Vincent 	if (chan_is_physical(d40c))
136820a5b6d0SRabin Vincent 		return phy_map[d40c->dma_cfg.mode_opt];
136920a5b6d0SRabin Vincent 	else
137020a5b6d0SRabin Vincent 		return log_map[d40c->dma_cfg.mode_opt];
137120a5b6d0SRabin Vincent }
137220a5b6d0SRabin Vincent 
1373b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c)
13748d318a50SLinus Walleij {
13758d318a50SLinus Walleij 	u32 addr_base;
13768d318a50SLinus Walleij 	u32 var;
13778d318a50SLinus Walleij 
13788d318a50SLinus Walleij 	/* Odd addresses are even addresses + 4 */
13798d318a50SLinus Walleij 	addr_base = (d40c->phy_chan->num % 2) * 4;
13808d318a50SLinus Walleij 	/* Setup channel mode to logical or physical */
1381724a8577SRabin Vincent 	var = ((u32)(chan_is_logical(d40c)) + 1) <<
13828d318a50SLinus Walleij 		D40_CHAN_POS(d40c->phy_chan->num);
13838d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
13848d318a50SLinus Walleij 
13858d318a50SLinus Walleij 	/* Setup operational mode option register */
138620a5b6d0SRabin Vincent 	var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
13878d318a50SLinus Walleij 
13888d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
13898d318a50SLinus Walleij 
1390724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
13918ca84687SRabin Vincent 		int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
13928ca84687SRabin Vincent 			   & D40_SREG_ELEM_LOG_LIDX_MASK;
13938ca84687SRabin Vincent 		void __iomem *chanbase = chan_base(d40c);
13948ca84687SRabin Vincent 
13958d318a50SLinus Walleij 		/* Set default config for CFG reg */
13968ca84687SRabin Vincent 		writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
13978ca84687SRabin Vincent 		writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
13988d318a50SLinus Walleij 
1399b55912c6SJonas Aaberg 		/* Set LIDX for lcla */
14008ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SSELT);
14018ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1402e9f3a49cSRabin Vincent 
1403e9f3a49cSRabin Vincent 		/* Clear LNK which will be used by d40_chan_has_events() */
1404e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SSLNK);
1405e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SDLNK);
14068d318a50SLinus Walleij 	}
14078d318a50SLinus Walleij }
14088d318a50SLinus Walleij 
1409aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c)
1410aa182ae2SJonas Aaberg {
1411aa182ae2SJonas Aaberg 	u32 num_elt;
1412aa182ae2SJonas Aaberg 
1413724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1414aa182ae2SJonas Aaberg 		num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1415aa182ae2SJonas Aaberg 			>> D40_MEM_LCSP2_ECNT_POS;
14168ca84687SRabin Vincent 	else {
14178ca84687SRabin Vincent 		u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
14188ca84687SRabin Vincent 		num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
14198ca84687SRabin Vincent 			  >> D40_SREG_ELEM_PHY_ECNT_POS;
14208ca84687SRabin Vincent 	}
14218ca84687SRabin Vincent 
142243f2e1a3SLee Jones 	return num_elt * d40c->dma_cfg.dst_info.data_width;
1423aa182ae2SJonas Aaberg }
1424aa182ae2SJonas Aaberg 
1425aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c)
1426aa182ae2SJonas Aaberg {
1427aa182ae2SJonas Aaberg 	bool is_link;
1428aa182ae2SJonas Aaberg 
1429724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1430aa182ae2SJonas Aaberg 		is_link = readl(&d40c->lcpa->lcsp3) &  D40_MEM_LCSP3_DLOS_MASK;
1431aa182ae2SJonas Aaberg 	else
14328ca84687SRabin Vincent 		is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
14338ca84687SRabin Vincent 			  & D40_SREG_LNK_PHYS_LNK_MASK;
14348ca84687SRabin Vincent 
1435aa182ae2SJonas Aaberg 	return is_link;
1436aa182ae2SJonas Aaberg }
1437aa182ae2SJonas Aaberg 
14386f5bad03SMaxime Ripard static int d40_pause(struct dma_chan *chan)
1439aa182ae2SJonas Aaberg {
14406f5bad03SMaxime Ripard 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1441aa182ae2SJonas Aaberg 	int res = 0;
1442aa182ae2SJonas Aaberg 	unsigned long flags;
1443aa182ae2SJonas Aaberg 
14446f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
14456f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
14466f5bad03SMaxime Ripard 		return -EINVAL;
14476f5bad03SMaxime Ripard 	}
14486f5bad03SMaxime Ripard 
14493ac012afSJonas Aaberg 	if (!d40c->busy)
14503ac012afSJonas Aaberg 		return 0;
14513ac012afSJonas Aaberg 
1452aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
145380245216SUlf Hansson 	pm_runtime_get_sync(d40c->base->dev);
1454aa182ae2SJonas Aaberg 
1455aa182ae2SJonas Aaberg 	res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
14561bdae6f4SNarayanan G 
14577fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
14587fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1459aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1460aa182ae2SJonas Aaberg 	return res;
1461aa182ae2SJonas Aaberg }
1462aa182ae2SJonas Aaberg 
14636f5bad03SMaxime Ripard static int d40_resume(struct dma_chan *chan)
1464aa182ae2SJonas Aaberg {
14656f5bad03SMaxime Ripard 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1466aa182ae2SJonas Aaberg 	int res = 0;
1467aa182ae2SJonas Aaberg 	unsigned long flags;
1468aa182ae2SJonas Aaberg 
14696f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
14706f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
14716f5bad03SMaxime Ripard 		return -EINVAL;
14726f5bad03SMaxime Ripard 	}
14736f5bad03SMaxime Ripard 
14743ac012afSJonas Aaberg 	if (!d40c->busy)
14753ac012afSJonas Aaberg 		return 0;
14763ac012afSJonas Aaberg 
1477aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
14787fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
1479aa182ae2SJonas Aaberg 
1480aa182ae2SJonas Aaberg 	/* If bytes left to transfer or linked tx resume job */
14811bdae6f4SNarayanan G 	if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1482aa182ae2SJonas Aaberg 		res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1483aa182ae2SJonas Aaberg 
14847fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
14857fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1486aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1487aa182ae2SJonas Aaberg 	return res;
1488aa182ae2SJonas Aaberg }
1489aa182ae2SJonas Aaberg 
14908d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
14918d318a50SLinus Walleij {
14928d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(tx->chan,
14938d318a50SLinus Walleij 					     struct d40_chan,
14948d318a50SLinus Walleij 					     chan);
14958d318a50SLinus Walleij 	struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
14968d318a50SLinus Walleij 	unsigned long flags;
1497884485e1SRussell King - ARM Linux 	dma_cookie_t cookie;
14988d318a50SLinus Walleij 
14998d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
1500884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
15018d318a50SLinus Walleij 	d40_desc_queue(d40c, d40d);
15028d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
15038d318a50SLinus Walleij 
1504884485e1SRussell King - ARM Linux 	return cookie;
15058d318a50SLinus Walleij }
15068d318a50SLinus Walleij 
15078d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c)
15088d318a50SLinus Walleij {
15090c32269dSJonas Aaberg 	return d40_channel_execute_command(d40c, D40_DMA_RUN);
15108d318a50SLinus Walleij }
15118d318a50SLinus Walleij 
15128d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
15138d318a50SLinus Walleij {
15148d318a50SLinus Walleij 	struct d40_desc *d40d;
15158d318a50SLinus Walleij 	int err;
15168d318a50SLinus Walleij 
15178d318a50SLinus Walleij 	/* Start queued jobs, if any */
15188d318a50SLinus Walleij 	d40d = d40_first_queued(d40c);
15198d318a50SLinus Walleij 
15208d318a50SLinus Walleij 	if (d40d != NULL) {
15211bdae6f4SNarayanan G 		if (!d40c->busy) {
15228d318a50SLinus Walleij 			d40c->busy = true;
15237fb3e75eSNarayanan G 			pm_runtime_get_sync(d40c->base->dev);
15241bdae6f4SNarayanan G 		}
15257fb3e75eSNarayanan G 
15268d318a50SLinus Walleij 		/* Remove from queue */
15278d318a50SLinus Walleij 		d40_desc_remove(d40d);
15288d318a50SLinus Walleij 
15298d318a50SLinus Walleij 		/* Add to active queue */
15308d318a50SLinus Walleij 		d40_desc_submit(d40c, d40d);
15318d318a50SLinus Walleij 
15328d318a50SLinus Walleij 		/* Initiate DMA job */
15338d318a50SLinus Walleij 		d40_desc_load(d40c, d40d);
15348d318a50SLinus Walleij 
15358d318a50SLinus Walleij 		/* Start dma job */
15368d318a50SLinus Walleij 		err = d40_start(d40c);
15378d318a50SLinus Walleij 
15388d318a50SLinus Walleij 		if (err)
15398d318a50SLinus Walleij 			return NULL;
15408d318a50SLinus Walleij 	}
15418d318a50SLinus Walleij 
15428d318a50SLinus Walleij 	return d40d;
15438d318a50SLinus Walleij }
15448d318a50SLinus Walleij 
15458d318a50SLinus Walleij /* called from interrupt context */
15468d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c)
15478d318a50SLinus Walleij {
15488d318a50SLinus Walleij 	struct d40_desc *d40d;
15498d318a50SLinus Walleij 
15508d318a50SLinus Walleij 	/* Get first active entry from list */
15518d318a50SLinus Walleij 	d40d = d40_first_active_get(d40c);
15528d318a50SLinus Walleij 
15538d318a50SLinus Walleij 	if (d40d == NULL)
15548d318a50SLinus Walleij 		return;
15558d318a50SLinus Walleij 
15560c842b55SRabin Vincent 	if (d40d->cyclic) {
15570c842b55SRabin Vincent 		/*
15580c842b55SRabin Vincent 		 * If this was a paritially loaded list, we need to reloaded
15590c842b55SRabin Vincent 		 * it, and only when the list is completed.  We need to check
15600c842b55SRabin Vincent 		 * for done because the interrupt will hit for every link, and
15610c842b55SRabin Vincent 		 * not just the last one.
15620c842b55SRabin Vincent 		 */
15630c842b55SRabin Vincent 		if (d40d->lli_current < d40d->lli_len
15640c842b55SRabin Vincent 		    && !d40_tx_is_linked(d40c)
15650c842b55SRabin Vincent 		    && !d40_residue(d40c)) {
15660c842b55SRabin Vincent 			d40_lcla_free_all(d40c, d40d);
15670c842b55SRabin Vincent 			d40_desc_load(d40c, d40d);
15680c842b55SRabin Vincent 			(void) d40_start(d40c);
15690c842b55SRabin Vincent 
15700c842b55SRabin Vincent 			if (d40d->lli_current == d40d->lli_len)
15710c842b55SRabin Vincent 				d40d->lli_current = 0;
15720c842b55SRabin Vincent 		}
15730c842b55SRabin Vincent 	} else {
1574698e4732SJonas Aaberg 		d40_lcla_free_all(d40c, d40d);
15758d318a50SLinus Walleij 
1576698e4732SJonas Aaberg 		if (d40d->lli_current < d40d->lli_len) {
15778d318a50SLinus Walleij 			d40_desc_load(d40c, d40d);
15788d318a50SLinus Walleij 			/* Start dma job */
15798d318a50SLinus Walleij 			(void) d40_start(d40c);
15808d318a50SLinus Walleij 			return;
15818d318a50SLinus Walleij 		}
15828d318a50SLinus Walleij 
15839ecb41bdSRabin Vincent 		if (d40_queue_start(d40c) == NULL) {
15848d318a50SLinus Walleij 			d40c->busy = false;
15859ecb41bdSRabin Vincent 
15867fb3e75eSNarayanan G 			pm_runtime_mark_last_busy(d40c->base->dev);
15877fb3e75eSNarayanan G 			pm_runtime_put_autosuspend(d40c->base->dev);
15889ecb41bdSRabin Vincent 		}
15898d318a50SLinus Walleij 
15904226dd86SFabio Baltieri 		d40_desc_remove(d40d);
15914226dd86SFabio Baltieri 		d40_desc_done(d40c, d40d);
15927dd14525SFabio Baltieri 	}
15934226dd86SFabio Baltieri 
15948d318a50SLinus Walleij 	d40c->pending_tx++;
15958d318a50SLinus Walleij 	tasklet_schedule(&d40c->tasklet);
15968d318a50SLinus Walleij 
15978d318a50SLinus Walleij }
15988d318a50SLinus Walleij 
1599b1880c90SAllen Pais static void dma_tasklet(struct tasklet_struct *t)
16008d318a50SLinus Walleij {
1601b1880c90SAllen Pais 	struct d40_chan *d40c = from_tasklet(d40c, t, tasklet);
1602767a9675SJonas Aaberg 	struct d40_desc *d40d;
16038d318a50SLinus Walleij 	unsigned long flags;
1604e9baa9d9SLinus Walleij 	bool callback_active;
16053a315d5dSDave Jiang 	struct dmaengine_desc_callback cb;
16068d318a50SLinus Walleij 
16078d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
16088d318a50SLinus Walleij 
16094226dd86SFabio Baltieri 	/* Get first entry from the done list */
16104226dd86SFabio Baltieri 	d40d = d40_first_done(d40c);
16114226dd86SFabio Baltieri 	if (d40d == NULL) {
16124226dd86SFabio Baltieri 		/* Check if we have reached here for cyclic job */
1613767a9675SJonas Aaberg 		d40d = d40_first_active_get(d40c);
16144226dd86SFabio Baltieri 		if (d40d == NULL || !d40d->cyclic)
1615d4cd217aSMarkus Elfring 			goto check_pending_tx;
16164226dd86SFabio Baltieri 	}
16178d318a50SLinus Walleij 
16180c842b55SRabin Vincent 	if (!d40d->cyclic)
1619f7fbce07SRussell King - ARM Linux 		dma_cookie_complete(&d40d->txd);
16208d318a50SLinus Walleij 
16218d318a50SLinus Walleij 	/*
16228d318a50SLinus Walleij 	 * If terminating a channel pending_tx is set to zero.
16238d318a50SLinus Walleij 	 * This prevents any finished active jobs to return to the client.
16248d318a50SLinus Walleij 	 */
16258d318a50SLinus Walleij 	if (d40c->pending_tx == 0) {
16268d318a50SLinus Walleij 		spin_unlock_irqrestore(&d40c->lock, flags);
16278d318a50SLinus Walleij 		return;
16288d318a50SLinus Walleij 	}
16298d318a50SLinus Walleij 
16308d318a50SLinus Walleij 	/* Callback to client */
1631e9baa9d9SLinus Walleij 	callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
16323a315d5dSDave Jiang 	dmaengine_desc_get_callback(&d40d->txd, &cb);
16338d318a50SLinus Walleij 
16340c842b55SRabin Vincent 	if (!d40d->cyclic) {
1635767a9675SJonas Aaberg 		if (async_tx_test_ack(&d40d->txd)) {
1636767a9675SJonas Aaberg 			d40_desc_remove(d40d);
1637767a9675SJonas Aaberg 			d40_desc_free(d40c, d40d);
1638f26e03adSFabio Baltieri 		} else if (!d40d->is_in_client_list) {
1639767a9675SJonas Aaberg 			d40_desc_remove(d40d);
1640698e4732SJonas Aaberg 			d40_lcla_free_all(d40c, d40d);
1641767a9675SJonas Aaberg 			list_add_tail(&d40d->node, &d40c->client);
1642767a9675SJonas Aaberg 			d40d->is_in_client_list = true;
16438d318a50SLinus Walleij 		}
16448d318a50SLinus Walleij 	}
16458d318a50SLinus Walleij 
16468d318a50SLinus Walleij 	d40c->pending_tx--;
16478d318a50SLinus Walleij 
16488d318a50SLinus Walleij 	if (d40c->pending_tx)
16498d318a50SLinus Walleij 		tasklet_schedule(&d40c->tasklet);
16508d318a50SLinus Walleij 
16518d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
16528d318a50SLinus Walleij 
16533a315d5dSDave Jiang 	if (callback_active)
16543a315d5dSDave Jiang 		dmaengine_desc_callback_invoke(&cb, NULL);
16558d318a50SLinus Walleij 
16568d318a50SLinus Walleij 	return;
1657d4cd217aSMarkus Elfring  check_pending_tx:
16581bdae6f4SNarayanan G 	/* Rescue manouver if receiving double interrupts */
16598d318a50SLinus Walleij 	if (d40c->pending_tx > 0)
16608d318a50SLinus Walleij 		d40c->pending_tx--;
16618d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
16628d318a50SLinus Walleij }
16638d318a50SLinus Walleij 
16648d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data)
16658d318a50SLinus Walleij {
16668d318a50SLinus Walleij 	int i;
16678d318a50SLinus Walleij 	u32 idx;
16688d318a50SLinus Walleij 	u32 row;
16698d318a50SLinus Walleij 	long chan = -1;
16708d318a50SLinus Walleij 	struct d40_chan *d40c;
16718d318a50SLinus Walleij 	struct d40_base *base = data;
1672e6a78511SKees Cook 	u32 *regs = base->regs_interrupt;
16733cb645dcSTong Liu 	struct d40_interrupt_lookup *il = base->gen_dmac.il;
16743cb645dcSTong Liu 	u32 il_size = base->gen_dmac.il_size;
16758d318a50SLinus Walleij 
1676618a8e38SBarry Song 	spin_lock(&base->interrupt_lock);
16778d318a50SLinus Walleij 
16788d318a50SLinus Walleij 	/* Read interrupt status of both logical and physical channels */
16793cb645dcSTong Liu 	for (i = 0; i < il_size; i++)
16808d318a50SLinus Walleij 		regs[i] = readl(base->virtbase + il[i].src);
16818d318a50SLinus Walleij 
16828d318a50SLinus Walleij 	for (;;) {
16838d318a50SLinus Walleij 
16848d318a50SLinus Walleij 		chan = find_next_bit((unsigned long *)regs,
16853cb645dcSTong Liu 				     BITS_PER_LONG * il_size, chan + 1);
16868d318a50SLinus Walleij 
16878d318a50SLinus Walleij 		/* No more set bits found? */
16883cb645dcSTong Liu 		if (chan == BITS_PER_LONG * il_size)
16898d318a50SLinus Walleij 			break;
16908d318a50SLinus Walleij 
16918d318a50SLinus Walleij 		row = chan / BITS_PER_LONG;
16928d318a50SLinus Walleij 		idx = chan & (BITS_PER_LONG - 1);
16938d318a50SLinus Walleij 
16948d318a50SLinus Walleij 		if (il[row].offset == D40_PHY_CHAN)
16958d318a50SLinus Walleij 			d40c = base->lookup_phy_chans[idx];
16968d318a50SLinus Walleij 		else
16978d318a50SLinus Walleij 			d40c = base->lookup_log_chans[il[row].offset + idx];
169853d6d68fSFabio Baltieri 
169953d6d68fSFabio Baltieri 		if (!d40c) {
170053d6d68fSFabio Baltieri 			/*
170153d6d68fSFabio Baltieri 			 * No error because this can happen if something else
170253d6d68fSFabio Baltieri 			 * in the system is using the channel.
170353d6d68fSFabio Baltieri 			 */
170453d6d68fSFabio Baltieri 			continue;
170553d6d68fSFabio Baltieri 		}
170653d6d68fSFabio Baltieri 
170753d6d68fSFabio Baltieri 		/* ACK interrupt */
17088a3b6e14SLee Jones 		writel(BIT(idx), base->virtbase + il[row].clr);
170953d6d68fSFabio Baltieri 
17108d318a50SLinus Walleij 		spin_lock(&d40c->lock);
17118d318a50SLinus Walleij 
17128d318a50SLinus Walleij 		if (!il[row].is_error)
17138d318a50SLinus Walleij 			dma_tc_handle(d40c);
17148d318a50SLinus Walleij 		else
17156db5a8baSRabin Vincent 			d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
17166db5a8baSRabin Vincent 				chan, il[row].offset, idx);
17178d318a50SLinus Walleij 
17188d318a50SLinus Walleij 		spin_unlock(&d40c->lock);
17198d318a50SLinus Walleij 	}
17208d318a50SLinus Walleij 
1721618a8e38SBarry Song 	spin_unlock(&base->interrupt_lock);
17228d318a50SLinus Walleij 
17238d318a50SLinus Walleij 	return IRQ_HANDLED;
17248d318a50SLinus Walleij }
17258d318a50SLinus Walleij 
17268d318a50SLinus Walleij static int d40_validate_conf(struct d40_chan *d40c,
17278d318a50SLinus Walleij 			     struct stedma40_chan_cfg *conf)
17288d318a50SLinus Walleij {
17298d318a50SLinus Walleij 	int res = 0;
173038bdbf02SRabin Vincent 	bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
17318d318a50SLinus Walleij 
17320747c7baSLinus Walleij 	if (!conf->dir) {
17336db5a8baSRabin Vincent 		chan_err(d40c, "Invalid direction.\n");
17340747c7baSLinus Walleij 		res = -EINVAL;
17350747c7baSLinus Walleij 	}
17360747c7baSLinus Walleij 
173726955c07SLee Jones 	if ((is_log && conf->dev_type > d40c->base->num_log_chans)  ||
173826955c07SLee Jones 	    (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
173926955c07SLee Jones 	    (conf->dev_type < 0)) {
174026955c07SLee Jones 		chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
17410747c7baSLinus Walleij 		res = -EINVAL;
17420747c7baSLinus Walleij 	}
17430747c7baSLinus Walleij 
17442c2b62d5SLee Jones 	if (conf->dir == DMA_DEV_TO_DEV) {
17458d318a50SLinus Walleij 		/*
17468d318a50SLinus Walleij 		 * DMAC HW supports it. Will be added to this driver,
17478d318a50SLinus Walleij 		 * in case any dma client requires it.
17488d318a50SLinus Walleij 		 */
17496db5a8baSRabin Vincent 		chan_err(d40c, "periph to periph not supported\n");
17508d318a50SLinus Walleij 		res = -EINVAL;
17518d318a50SLinus Walleij 	}
17528d318a50SLinus Walleij 
1753d49278e3SPer Forlin 	if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
175443f2e1a3SLee Jones 	    conf->src_info.data_width !=
1755d49278e3SPer Forlin 	    d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
175643f2e1a3SLee Jones 	    conf->dst_info.data_width) {
1757d49278e3SPer Forlin 		/*
1758d49278e3SPer Forlin 		 * The DMAC hardware only supports
1759d49278e3SPer Forlin 		 * src (burst x width) == dst (burst x width)
1760d49278e3SPer Forlin 		 */
1761d49278e3SPer Forlin 
17626db5a8baSRabin Vincent 		chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1763d49278e3SPer Forlin 		res = -EINVAL;
1764d49278e3SPer Forlin 	}
1765d49278e3SPer Forlin 
17668d318a50SLinus Walleij 	return res;
17678d318a50SLinus Walleij }
17688d318a50SLinus Walleij 
17695cd326fdSNarayanan G static bool d40_alloc_mask_set(struct d40_phy_res *phy,
17705cd326fdSNarayanan G 			       bool is_src, int log_event_line, bool is_log,
17715cd326fdSNarayanan G 			       bool *first_user)
17728d318a50SLinus Walleij {
17738d318a50SLinus Walleij 	unsigned long flags;
17748d318a50SLinus Walleij 	spin_lock_irqsave(&phy->lock, flags);
17755cd326fdSNarayanan G 
17765cd326fdSNarayanan G 	*first_user = ((phy->allocated_src | phy->allocated_dst)
17775cd326fdSNarayanan G 			== D40_ALLOC_FREE);
17785cd326fdSNarayanan G 
17794aed79b2SMarcin Mielczarczyk 	if (!is_log) {
17808d318a50SLinus Walleij 		/* Physical interrupts are masked per physical full channel */
17818d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE &&
17828d318a50SLinus Walleij 		    phy->allocated_dst == D40_ALLOC_FREE) {
17838d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_PHY;
17848d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_PHY;
17858eff80e4SMarkus Elfring 			goto found_unlock;
17868d318a50SLinus Walleij 		} else
17878eff80e4SMarkus Elfring 			goto not_found_unlock;
17888d318a50SLinus Walleij 	}
17898d318a50SLinus Walleij 
17908d318a50SLinus Walleij 	/* Logical channel */
17918d318a50SLinus Walleij 	if (is_src) {
17928d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_PHY)
17938eff80e4SMarkus Elfring 			goto not_found_unlock;
17948d318a50SLinus Walleij 
17958d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE)
17968d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_LOG_FREE;
17978d318a50SLinus Walleij 
17988a3b6e14SLee Jones 		if (!(phy->allocated_src & BIT(log_event_line))) {
17998a3b6e14SLee Jones 			phy->allocated_src |= BIT(log_event_line);
18008eff80e4SMarkus Elfring 			goto found_unlock;
18018d318a50SLinus Walleij 		} else
18028eff80e4SMarkus Elfring 			goto not_found_unlock;
18038d318a50SLinus Walleij 	} else {
18048d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_PHY)
18058eff80e4SMarkus Elfring 			goto not_found_unlock;
18068d318a50SLinus Walleij 
18078d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_FREE)
18088d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_LOG_FREE;
18098d318a50SLinus Walleij 
18108a3b6e14SLee Jones 		if (!(phy->allocated_dst & BIT(log_event_line))) {
18118a3b6e14SLee Jones 			phy->allocated_dst |= BIT(log_event_line);
18128eff80e4SMarkus Elfring 			goto found_unlock;
18138d318a50SLinus Walleij 		}
18148eff80e4SMarkus Elfring 	}
18158eff80e4SMarkus Elfring  not_found_unlock:
18168d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
18178d318a50SLinus Walleij 	return false;
18188eff80e4SMarkus Elfring  found_unlock:
18198d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
18208d318a50SLinus Walleij 	return true;
18218d318a50SLinus Walleij }
18228d318a50SLinus Walleij 
18238d318a50SLinus Walleij static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
18248d318a50SLinus Walleij 			       int log_event_line)
18258d318a50SLinus Walleij {
18268d318a50SLinus Walleij 	unsigned long flags;
18278d318a50SLinus Walleij 	bool is_free = false;
18288d318a50SLinus Walleij 
18298d318a50SLinus Walleij 	spin_lock_irqsave(&phy->lock, flags);
18308d318a50SLinus Walleij 	if (!log_event_line) {
18318d318a50SLinus Walleij 		phy->allocated_dst = D40_ALLOC_FREE;
18328d318a50SLinus Walleij 		phy->allocated_src = D40_ALLOC_FREE;
18338d318a50SLinus Walleij 		is_free = true;
1834f19b8ee8SMarkus Elfring 		goto unlock;
18358d318a50SLinus Walleij 	}
18368d318a50SLinus Walleij 
18378d318a50SLinus Walleij 	/* Logical channel */
18388d318a50SLinus Walleij 	if (is_src) {
18398a3b6e14SLee Jones 		phy->allocated_src &= ~BIT(log_event_line);
18408d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_LOG_FREE)
18418d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_FREE;
18428d318a50SLinus Walleij 	} else {
18438a3b6e14SLee Jones 		phy->allocated_dst &= ~BIT(log_event_line);
18448d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
18458d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_FREE;
18468d318a50SLinus Walleij 	}
18478d318a50SLinus Walleij 
18488d318a50SLinus Walleij 	is_free = ((phy->allocated_src | phy->allocated_dst) ==
18498d318a50SLinus Walleij 		   D40_ALLOC_FREE);
1850f19b8ee8SMarkus Elfring  unlock:
18518d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
18528d318a50SLinus Walleij 
18538d318a50SLinus Walleij 	return is_free;
18548d318a50SLinus Walleij }
18558d318a50SLinus Walleij 
18565cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
18578d318a50SLinus Walleij {
185826955c07SLee Jones 	int dev_type = d40c->dma_cfg.dev_type;
18598d318a50SLinus Walleij 	int event_group;
18608d318a50SLinus Walleij 	int event_line;
18618d318a50SLinus Walleij 	struct d40_phy_res *phys;
18628d318a50SLinus Walleij 	int i;
18638d318a50SLinus Walleij 	int j;
18648d318a50SLinus Walleij 	int log_num;
1865f000df8cSGerald Baeza 	int num_phy_chans;
18668d318a50SLinus Walleij 	bool is_src;
186738bdbf02SRabin Vincent 	bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
18688d318a50SLinus Walleij 
18698d318a50SLinus Walleij 	phys = d40c->base->phy_res;
1870f000df8cSGerald Baeza 	num_phy_chans = d40c->base->num_phy_chans;
18718d318a50SLinus Walleij 
18722c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
18738d318a50SLinus Walleij 		log_num = 2 * dev_type;
18748d318a50SLinus Walleij 		is_src = true;
18752c2b62d5SLee Jones 	} else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
18762c2b62d5SLee Jones 		   d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18778d318a50SLinus Walleij 		/* dst event lines are used for logical memcpy */
18788d318a50SLinus Walleij 		log_num = 2 * dev_type + 1;
18798d318a50SLinus Walleij 		is_src = false;
18808d318a50SLinus Walleij 	} else
18818d318a50SLinus Walleij 		return -EINVAL;
18828d318a50SLinus Walleij 
18838d318a50SLinus Walleij 	event_group = D40_TYPE_TO_GROUP(dev_type);
18848d318a50SLinus Walleij 	event_line = D40_TYPE_TO_EVENT(dev_type);
18858d318a50SLinus Walleij 
18868d318a50SLinus Walleij 	if (!is_log) {
18872c2b62d5SLee Jones 		if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18888d318a50SLinus Walleij 			/* Find physical half channel */
1889f000df8cSGerald Baeza 			if (d40c->dma_cfg.use_fixed_channel) {
1890f000df8cSGerald Baeza 				i = d40c->dma_cfg.phy_channel;
18914aed79b2SMarcin Mielczarczyk 				if (d40_alloc_mask_set(&phys[i], is_src,
18925cd326fdSNarayanan G 						       0, is_log,
18935cd326fdSNarayanan G 						       first_phy_user))
18948d318a50SLinus Walleij 					goto found_phy;
1895f000df8cSGerald Baeza 			} else {
1896f000df8cSGerald Baeza 				for (i = 0; i < num_phy_chans; i++) {
1897f000df8cSGerald Baeza 					if (d40_alloc_mask_set(&phys[i], is_src,
1898f000df8cSGerald Baeza 						       0, is_log,
1899f000df8cSGerald Baeza 						       first_phy_user))
1900f000df8cSGerald Baeza 						goto found_phy;
1901f000df8cSGerald Baeza 				}
19028d318a50SLinus Walleij 			}
19038d318a50SLinus Walleij 		} else
19048d318a50SLinus Walleij 			for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19058d318a50SLinus Walleij 				int phy_num = j  + event_group * 2;
19068d318a50SLinus Walleij 				for (i = phy_num; i < phy_num + 2; i++) {
1907508849adSLinus Walleij 					if (d40_alloc_mask_set(&phys[i],
1908508849adSLinus Walleij 							       is_src,
1909508849adSLinus Walleij 							       0,
19105cd326fdSNarayanan G 							       is_log,
19115cd326fdSNarayanan G 							       first_phy_user))
19128d318a50SLinus Walleij 						goto found_phy;
19138d318a50SLinus Walleij 				}
19148d318a50SLinus Walleij 			}
19158d318a50SLinus Walleij 		return -EINVAL;
19168d318a50SLinus Walleij found_phy:
19178d318a50SLinus Walleij 		d40c->phy_chan = &phys[i];
19188d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
19198d318a50SLinus Walleij 		goto out;
19208d318a50SLinus Walleij 	}
19218d318a50SLinus Walleij 	if (dev_type == -1)
19228d318a50SLinus Walleij 		return -EINVAL;
19238d318a50SLinus Walleij 
19248d318a50SLinus Walleij 	/* Find logical channel */
19258d318a50SLinus Walleij 	for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19268d318a50SLinus Walleij 		int phy_num = j + event_group * 2;
19275cd326fdSNarayanan G 
19285cd326fdSNarayanan G 		if (d40c->dma_cfg.use_fixed_channel) {
19295cd326fdSNarayanan G 			i = d40c->dma_cfg.phy_channel;
19305cd326fdSNarayanan G 
19315cd326fdSNarayanan G 			if ((i != phy_num) && (i != phy_num + 1)) {
19325cd326fdSNarayanan G 				dev_err(chan2dev(d40c),
19335cd326fdSNarayanan G 					"invalid fixed phy channel %d\n", i);
19345cd326fdSNarayanan G 				return -EINVAL;
19355cd326fdSNarayanan G 			}
19365cd326fdSNarayanan G 
19375cd326fdSNarayanan G 			if (d40_alloc_mask_set(&phys[i], is_src, event_line,
19385cd326fdSNarayanan G 					       is_log, first_phy_user))
19395cd326fdSNarayanan G 				goto found_log;
19405cd326fdSNarayanan G 
19415cd326fdSNarayanan G 			dev_err(chan2dev(d40c),
19425cd326fdSNarayanan G 				"could not allocate fixed phy channel %d\n", i);
19435cd326fdSNarayanan G 			return -EINVAL;
19445cd326fdSNarayanan G 		}
19455cd326fdSNarayanan G 
19468d318a50SLinus Walleij 		/*
19478d318a50SLinus Walleij 		 * Spread logical channels across all available physical rather
19488d318a50SLinus Walleij 		 * than pack every logical channel at the first available phy
19498d318a50SLinus Walleij 		 * channels.
19508d318a50SLinus Walleij 		 */
19518d318a50SLinus Walleij 		if (is_src) {
19528d318a50SLinus Walleij 			for (i = phy_num; i < phy_num + 2; i++) {
19538d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
19545cd326fdSNarayanan G 						       event_line, is_log,
19555cd326fdSNarayanan G 						       first_phy_user))
19568d318a50SLinus Walleij 					goto found_log;
19578d318a50SLinus Walleij 			}
19588d318a50SLinus Walleij 		} else {
19598d318a50SLinus Walleij 			for (i = phy_num + 1; i >= phy_num; i--) {
19608d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
19615cd326fdSNarayanan G 						       event_line, is_log,
19625cd326fdSNarayanan G 						       first_phy_user))
19638d318a50SLinus Walleij 					goto found_log;
19648d318a50SLinus Walleij 			}
19658d318a50SLinus Walleij 		}
19668d318a50SLinus Walleij 	}
19678d318a50SLinus Walleij 	return -EINVAL;
19688d318a50SLinus Walleij 
19698d318a50SLinus Walleij found_log:
19708d318a50SLinus Walleij 	d40c->phy_chan = &phys[i];
19718d318a50SLinus Walleij 	d40c->log_num = log_num;
19728d318a50SLinus Walleij out:
19738d318a50SLinus Walleij 
19748d318a50SLinus Walleij 	if (is_log)
19758d318a50SLinus Walleij 		d40c->base->lookup_log_chans[d40c->log_num] = d40c;
19768d318a50SLinus Walleij 	else
19778d318a50SLinus Walleij 		d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
19788d318a50SLinus Walleij 
19798d318a50SLinus Walleij 	return 0;
19808d318a50SLinus Walleij 
19818d318a50SLinus Walleij }
19828d318a50SLinus Walleij 
19838d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c)
19848d318a50SLinus Walleij {
19858d318a50SLinus Walleij 	dma_cap_mask_t cap = d40c->chan.device->cap_mask;
19868d318a50SLinus Walleij 
19878d318a50SLinus Walleij 	if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
198829027a1eSLee Jones 		d40c->dma_cfg = dma40_memcpy_conf_log;
198926955c07SLee Jones 		d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
19908d318a50SLinus Walleij 
19919b233f9bSLee Jones 		d40_log_cfg(&d40c->dma_cfg,
19929b233f9bSLee Jones 			    &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
19939b233f9bSLee Jones 
19948d318a50SLinus Walleij 	} else if (dma_has_cap(DMA_MEMCPY, cap) &&
19958d318a50SLinus Walleij 		   dma_has_cap(DMA_SLAVE, cap)) {
199629027a1eSLee Jones 		d40c->dma_cfg = dma40_memcpy_conf_phy;
199757e65ad7SLee Jones 
1998a71da24cSJulia Lawall 		/* Generate interrupt at end of transfer or relink. */
199957e65ad7SLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
200057e65ad7SLee Jones 
200157e65ad7SLee Jones 		/* Generate interrupt on error. */
200257e65ad7SLee Jones 		d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200357e65ad7SLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200457e65ad7SLee Jones 
20058d318a50SLinus Walleij 	} else {
20066db5a8baSRabin Vincent 		chan_err(d40c, "No memcpy\n");
20078d318a50SLinus Walleij 		return -EINVAL;
20088d318a50SLinus Walleij 	}
20098d318a50SLinus Walleij 
20108d318a50SLinus Walleij 	return 0;
20118d318a50SLinus Walleij }
20128d318a50SLinus Walleij 
20138d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c)
20148d318a50SLinus Walleij {
20158d318a50SLinus Walleij 
20168d318a50SLinus Walleij 	int res = 0;
201726955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
20188d318a50SLinus Walleij 	struct d40_phy_res *phy = d40c->phy_chan;
20198d318a50SLinus Walleij 	bool is_src;
20208d318a50SLinus Walleij 
20218d318a50SLinus Walleij 	/* Terminate all queued and active transfers */
20228d318a50SLinus Walleij 	d40_term_all(d40c);
20238d318a50SLinus Walleij 
20248d318a50SLinus Walleij 	if (phy == NULL) {
20256db5a8baSRabin Vincent 		chan_err(d40c, "phy == null\n");
20268d318a50SLinus Walleij 		return -EINVAL;
20278d318a50SLinus Walleij 	}
20288d318a50SLinus Walleij 
20298d318a50SLinus Walleij 	if (phy->allocated_src == D40_ALLOC_FREE &&
20308d318a50SLinus Walleij 	    phy->allocated_dst == D40_ALLOC_FREE) {
20316db5a8baSRabin Vincent 		chan_err(d40c, "channel already free\n");
20328d318a50SLinus Walleij 		return -EINVAL;
20338d318a50SLinus Walleij 	}
20348d318a50SLinus Walleij 
20352c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
20362c2b62d5SLee Jones 	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
20378d318a50SLinus Walleij 		is_src = false;
20382c2b62d5SLee Jones 	else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
20398d318a50SLinus Walleij 		is_src = true;
204026955c07SLee Jones 	else {
20416db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
20428d318a50SLinus Walleij 		return -EINVAL;
20438d318a50SLinus Walleij 	}
20448d318a50SLinus Walleij 
20457fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
20468d318a50SLinus Walleij 	res = d40_channel_execute_command(d40c, D40_DMA_STOP);
20478d318a50SLinus Walleij 	if (res) {
20481bdae6f4SNarayanan G 		chan_err(d40c, "stop failed\n");
2049e714b470SMarkus Elfring 		goto mark_last_busy;
20508d318a50SLinus Walleij 	}
20517fb3e75eSNarayanan G 
20521bdae6f4SNarayanan G 	d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
20531bdae6f4SNarayanan G 
20541bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
20551bdae6f4SNarayanan G 		d40c->base->lookup_log_chans[d40c->log_num] = NULL;
20561bdae6f4SNarayanan G 	else
20571bdae6f4SNarayanan G 		d40c->base->lookup_phy_chans[phy->num] = NULL;
20581bdae6f4SNarayanan G 
20597fb3e75eSNarayanan G 	if (d40c->busy) {
20607fb3e75eSNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
20617fb3e75eSNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
20627fb3e75eSNarayanan G 	}
20637fb3e75eSNarayanan G 
20647fb3e75eSNarayanan G 	d40c->busy = false;
20658d318a50SLinus Walleij 	d40c->phy_chan = NULL;
2066ce2ca125SRabin Vincent 	d40c->configured = false;
2067e714b470SMarkus Elfring  mark_last_busy:
20687fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
20697fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
20707fb3e75eSNarayanan G 	return res;
20718d318a50SLinus Walleij }
20728d318a50SLinus Walleij 
2073a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c)
2074a5ebca47SJonas Aaberg {
20758ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
2076a5ebca47SJonas Aaberg 	bool is_paused = false;
2077a5ebca47SJonas Aaberg 	unsigned long flags;
2078a5ebca47SJonas Aaberg 	void __iomem *active_reg;
2079a5ebca47SJonas Aaberg 	u32 status;
208026955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2081a5ebca47SJonas Aaberg 
2082a5ebca47SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
2083a5ebca47SJonas Aaberg 
2084724a8577SRabin Vincent 	if (chan_is_physical(d40c)) {
2085a5ebca47SJonas Aaberg 		if (d40c->phy_chan->num % 2 == 0)
2086a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2087a5ebca47SJonas Aaberg 		else
2088a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2089a5ebca47SJonas Aaberg 
2090a5ebca47SJonas Aaberg 		status = (readl(active_reg) &
2091a5ebca47SJonas Aaberg 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2092a5ebca47SJonas Aaberg 			D40_CHAN_POS(d40c->phy_chan->num);
2093a5ebca47SJonas Aaberg 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2094a5ebca47SJonas Aaberg 			is_paused = true;
20955a5eecb3SMarkus Elfring 		goto unlock;
2096a5ebca47SJonas Aaberg 	}
2097a5ebca47SJonas Aaberg 
20982c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
20992c2b62d5SLee Jones 	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
21008ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SDLNK);
21012c2b62d5SLee Jones 	} else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
21028ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SSLNK);
21039dbfbd35SJonas Aaberg 	} else {
21046db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
21055a5eecb3SMarkus Elfring 		goto unlock;
2106a5ebca47SJonas Aaberg 	}
21079dbfbd35SJonas Aaberg 
2108a5ebca47SJonas Aaberg 	status = (status & D40_EVENTLINE_MASK(event)) >>
2109a5ebca47SJonas Aaberg 		D40_EVENTLINE_POS(event);
2110a5ebca47SJonas Aaberg 
2111a5ebca47SJonas Aaberg 	if (status != D40_DMA_RUN)
2112a5ebca47SJonas Aaberg 		is_paused = true;
21135a5eecb3SMarkus Elfring  unlock:
2114a5ebca47SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
2115a5ebca47SJonas Aaberg 	return is_paused;
2116a5ebca47SJonas Aaberg 
2117a5ebca47SJonas Aaberg }
2118a5ebca47SJonas Aaberg 
21198d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan)
21208d318a50SLinus Walleij {
21218d318a50SLinus Walleij 	struct d40_chan *d40c =
21228d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
21238d318a50SLinus Walleij 	u32 bytes_left;
21248d318a50SLinus Walleij 	unsigned long flags;
21258d318a50SLinus Walleij 
21268d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
21278d318a50SLinus Walleij 	bytes_left = d40_residue(d40c);
21288d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
21298d318a50SLinus Walleij 
21308d318a50SLinus Walleij 	return bytes_left;
21318d318a50SLinus Walleij }
21328d318a50SLinus Walleij 
21333e3a0763SRabin Vincent static int
21343e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
21353e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
2136822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
2137822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
21383e3a0763SRabin Vincent {
21393e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21403e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
21413e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21425ed04b85SRabin Vincent 	int ret;
21433e3a0763SRabin Vincent 
21445ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_src, sg_len,
21455ed04b85SRabin Vincent 				src_dev_addr,
21463e3a0763SRabin Vincent 				desc->lli_log.src,
21473e3a0763SRabin Vincent 				chan->log_def.lcsp1,
21483e3a0763SRabin Vincent 				src_info->data_width,
21493e3a0763SRabin Vincent 				dst_info->data_width);
21503e3a0763SRabin Vincent 
21515ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_dst, sg_len,
21525ed04b85SRabin Vincent 				dst_dev_addr,
21533e3a0763SRabin Vincent 				desc->lli_log.dst,
21543e3a0763SRabin Vincent 				chan->log_def.lcsp3,
21553e3a0763SRabin Vincent 				dst_info->data_width,
21563e3a0763SRabin Vincent 				src_info->data_width);
21573e3a0763SRabin Vincent 
21585ed04b85SRabin Vincent 	return ret < 0 ? ret : 0;
21593e3a0763SRabin Vincent }
21603e3a0763SRabin Vincent 
21613e3a0763SRabin Vincent static int
21623e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
21633e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
2164822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
2165822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
21663e3a0763SRabin Vincent {
21673e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21683e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
21693e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21700c842b55SRabin Vincent 	unsigned long flags = 0;
21713e3a0763SRabin Vincent 	int ret;
21723e3a0763SRabin Vincent 
21730c842b55SRabin Vincent 	if (desc->cyclic)
21740c842b55SRabin Vincent 		flags |= LLI_CYCLIC | LLI_TERM_INT;
21750c842b55SRabin Vincent 
21763e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
21773e3a0763SRabin Vincent 				desc->lli_phy.src,
21783e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.src),
21793e3a0763SRabin Vincent 				chan->src_def_cfg,
21800c842b55SRabin Vincent 				src_info, dst_info, flags);
21813e3a0763SRabin Vincent 
21823e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
21833e3a0763SRabin Vincent 				desc->lli_phy.dst,
21843e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.dst),
21853e3a0763SRabin Vincent 				chan->dst_def_cfg,
21860c842b55SRabin Vincent 				dst_info, src_info, flags);
21873e3a0763SRabin Vincent 
21883e3a0763SRabin Vincent 	dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
21893e3a0763SRabin Vincent 				   desc->lli_pool.size, DMA_TO_DEVICE);
21903e3a0763SRabin Vincent 
21913e3a0763SRabin Vincent 	return ret < 0 ? ret : 0;
21923e3a0763SRabin Vincent }
21933e3a0763SRabin Vincent 
21945f81158fSRabin Vincent static struct d40_desc *
21955f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
21965f81158fSRabin Vincent 	      unsigned int sg_len, unsigned long dma_flags)
21975f81158fSRabin Vincent {
219886145910SMarkus Elfring 	struct stedma40_chan_cfg *cfg;
21995f81158fSRabin Vincent 	struct d40_desc *desc;
2200dbd88788SRabin Vincent 	int ret;
22015f81158fSRabin Vincent 
22025f81158fSRabin Vincent 	desc = d40_desc_get(chan);
22035f81158fSRabin Vincent 	if (!desc)
22045f81158fSRabin Vincent 		return NULL;
22055f81158fSRabin Vincent 
220686145910SMarkus Elfring 	cfg = &chan->dma_cfg;
22075f81158fSRabin Vincent 	desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
22085f81158fSRabin Vincent 					cfg->dst_info.data_width);
22095f81158fSRabin Vincent 	if (desc->lli_len < 0) {
22105f81158fSRabin Vincent 		chan_err(chan, "Unaligned size\n");
2211254e1254SMarkus Elfring 		goto free_desc;
22125f81158fSRabin Vincent 	}
22135f81158fSRabin Vincent 
2214dbd88788SRabin Vincent 	ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2215dbd88788SRabin Vincent 	if (ret < 0) {
2216dbd88788SRabin Vincent 		chan_err(chan, "Could not allocate lli\n");
2217254e1254SMarkus Elfring 		goto free_desc;
2218dbd88788SRabin Vincent 	}
2219dbd88788SRabin Vincent 
22205f81158fSRabin Vincent 	desc->lli_current = 0;
22215f81158fSRabin Vincent 	desc->txd.flags = dma_flags;
22225f81158fSRabin Vincent 	desc->txd.tx_submit = d40_tx_submit;
22235f81158fSRabin Vincent 
22245f81158fSRabin Vincent 	dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
22255f81158fSRabin Vincent 
22265f81158fSRabin Vincent 	return desc;
2227254e1254SMarkus Elfring  free_desc:
2228dbd88788SRabin Vincent 	d40_desc_free(chan, desc);
2229dbd88788SRabin Vincent 	return NULL;
22305f81158fSRabin Vincent }
22315f81158fSRabin Vincent 
2232cade1d30SRabin Vincent static struct dma_async_tx_descriptor *
2233cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2234cade1d30SRabin Vincent 	    struct scatterlist *sg_dst, unsigned int sg_len,
2235db8196dfSVinod Koul 	    enum dma_transfer_direction direction, unsigned long dma_flags)
2236cade1d30SRabin Vincent {
2237cade1d30SRabin Vincent 	struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2238444fa147SMarkus Elfring 	dma_addr_t src_dev_addr;
2239444fa147SMarkus Elfring 	dma_addr_t dst_dev_addr;
2240cade1d30SRabin Vincent 	struct d40_desc *desc;
2241cade1d30SRabin Vincent 	unsigned long flags;
2242cade1d30SRabin Vincent 	int ret;
22438d318a50SLinus Walleij 
2244cade1d30SRabin Vincent 	if (!chan->phy_chan) {
2245cade1d30SRabin Vincent 		chan_err(chan, "Cannot prepare unallocated channel\n");
2246cade1d30SRabin Vincent 		return NULL;
2247cade1d30SRabin Vincent 	}
2248cade1d30SRabin Vincent 
22499e314ef3SVinod Koul 	d40_set_runtime_config_write(dchan, &chan->slave_config, direction);
22509e314ef3SVinod Koul 
2251cade1d30SRabin Vincent 	spin_lock_irqsave(&chan->lock, flags);
2252cade1d30SRabin Vincent 
2253cade1d30SRabin Vincent 	desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2254cade1d30SRabin Vincent 	if (desc == NULL)
225578c6e1a5SMarkus Elfring 		goto unlock;
22568d318a50SLinus Walleij 
22570c842b55SRabin Vincent 	if (sg_next(&sg_src[sg_len - 1]) == sg_src)
22580c842b55SRabin Vincent 		desc->cyclic = true;
22590c842b55SRabin Vincent 
2260444fa147SMarkus Elfring 	src_dev_addr = 0;
2261444fa147SMarkus Elfring 	dst_dev_addr = 0;
2262db8196dfSVinod Koul 	if (direction == DMA_DEV_TO_MEM)
2263ef9c89b3SLee Jones 		src_dev_addr = chan->runtime_addr;
2264db8196dfSVinod Koul 	else if (direction == DMA_MEM_TO_DEV)
2265ef9c89b3SLee Jones 		dst_dev_addr = chan->runtime_addr;
2266cade1d30SRabin Vincent 
2267cade1d30SRabin Vincent 	if (chan_is_logical(chan))
2268cade1d30SRabin Vincent 		ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2269822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2270cade1d30SRabin Vincent 	else
2271cade1d30SRabin Vincent 		ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2272822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2273cade1d30SRabin Vincent 
2274cade1d30SRabin Vincent 	if (ret) {
2275cade1d30SRabin Vincent 		chan_err(chan, "Failed to prepare %s sg job: %d\n",
2276cade1d30SRabin Vincent 			 chan_is_logical(chan) ? "log" : "phy", ret);
227778c6e1a5SMarkus Elfring 		goto free_desc;
22788d318a50SLinus Walleij 	}
22798d318a50SLinus Walleij 
228082babbb3SPer Forlin 	/*
228182babbb3SPer Forlin 	 * add descriptor to the prepare queue in order to be able
228282babbb3SPer Forlin 	 * to free them later in terminate_all
228382babbb3SPer Forlin 	 */
228482babbb3SPer Forlin 	list_add_tail(&desc->node, &chan->prepare_queue);
228582babbb3SPer Forlin 
2286cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
22878d318a50SLinus Walleij 
2288cade1d30SRabin Vincent 	return &desc->txd;
228978c6e1a5SMarkus Elfring  free_desc:
2290cade1d30SRabin Vincent 	d40_desc_free(chan, desc);
229178c6e1a5SMarkus Elfring  unlock:
2292cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
22938d318a50SLinus Walleij 	return NULL;
22948d318a50SLinus Walleij }
22958d318a50SLinus Walleij 
229642ae6f16SLinus Walleij static bool stedma40_filter(struct dma_chan *chan, void *data)
22978d318a50SLinus Walleij {
22988d318a50SLinus Walleij 	struct stedma40_chan_cfg *info = data;
22998d318a50SLinus Walleij 	struct d40_chan *d40c =
23008d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
23018d318a50SLinus Walleij 	int err;
23028d318a50SLinus Walleij 
23038d318a50SLinus Walleij 	if (data) {
23048d318a50SLinus Walleij 		err = d40_validate_conf(d40c, info);
23058d318a50SLinus Walleij 		if (!err)
23068d318a50SLinus Walleij 			d40c->dma_cfg = *info;
23078d318a50SLinus Walleij 	} else
23088d318a50SLinus Walleij 		err = d40_config_memcpy(d40c);
23098d318a50SLinus Walleij 
2310ce2ca125SRabin Vincent 	if (!err)
2311ce2ca125SRabin Vincent 		d40c->configured = true;
2312ce2ca125SRabin Vincent 
23138d318a50SLinus Walleij 	return err == 0;
23148d318a50SLinus Walleij }
23158d318a50SLinus Walleij 
2316ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2317ac2c0a38SRabin Vincent {
2318ac2c0a38SRabin Vincent 	bool realtime = d40c->dma_cfg.realtime;
2319ac2c0a38SRabin Vincent 	bool highprio = d40c->dma_cfg.high_priority;
23203cb645dcSTong Liu 	u32 rtreg;
2321ac2c0a38SRabin Vincent 	u32 event = D40_TYPE_TO_EVENT(dev_type);
2322ac2c0a38SRabin Vincent 	u32 group = D40_TYPE_TO_GROUP(dev_type);
23238a3b6e14SLee Jones 	u32 bit = BIT(event);
2324ccc3d697SRabin Vincent 	u32 prioreg;
23253cb645dcSTong Liu 	struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2326ccc3d697SRabin Vincent 
23273cb645dcSTong Liu 	rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2328ccc3d697SRabin Vincent 	/*
2329ccc3d697SRabin Vincent 	 * Due to a hardware bug, in some cases a logical channel triggered by
2330ccc3d697SRabin Vincent 	 * a high priority destination event line can generate extra packet
2331ccc3d697SRabin Vincent 	 * transactions.
2332ccc3d697SRabin Vincent 	 *
2333ccc3d697SRabin Vincent 	 * The workaround is to not set the high priority level for the
2334ccc3d697SRabin Vincent 	 * destination event lines that trigger logical channels.
2335ccc3d697SRabin Vincent 	 */
2336ccc3d697SRabin Vincent 	if (!src && chan_is_logical(d40c))
2337ccc3d697SRabin Vincent 		highprio = false;
2338ccc3d697SRabin Vincent 
23393cb645dcSTong Liu 	prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2340ac2c0a38SRabin Vincent 
2341ac2c0a38SRabin Vincent 	/* Destination event lines are stored in the upper halfword */
2342ac2c0a38SRabin Vincent 	if (!src)
2343ac2c0a38SRabin Vincent 		bit <<= 16;
2344ac2c0a38SRabin Vincent 
2345ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + prioreg + group * 4);
2346ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + rtreg + group * 4);
2347ac2c0a38SRabin Vincent }
2348ac2c0a38SRabin Vincent 
2349ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c)
2350ac2c0a38SRabin Vincent {
2351ac2c0a38SRabin Vincent 	if (d40c->base->rev < 3)
2352ac2c0a38SRabin Vincent 		return;
2353ac2c0a38SRabin Vincent 
23542c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir ==  DMA_DEV_TO_MEM) ||
23552c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
235626955c07SLee Jones 		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2357ac2c0a38SRabin Vincent 
23582c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir ==  DMA_MEM_TO_DEV) ||
23592c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
236026955c07SLee Jones 		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2361ac2c0a38SRabin Vincent }
2362ac2c0a38SRabin Vincent 
2363fa332de5SLee Jones #define D40_DT_FLAGS_MODE(flags)       ((flags >> 0) & 0x1)
2364fa332de5SLee Jones #define D40_DT_FLAGS_DIR(flags)        ((flags >> 1) & 0x1)
2365fa332de5SLee Jones #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2366fa332de5SLee Jones #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2367bddd5a2bSLee Jones #define D40_DT_FLAGS_HIGH_PRIO(flags)  ((flags >> 4) & 0x1)
2368fa332de5SLee Jones 
2369fa332de5SLee Jones static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2370fa332de5SLee Jones 				  struct of_dma *ofdma)
2371fa332de5SLee Jones {
2372fa332de5SLee Jones 	struct stedma40_chan_cfg cfg;
2373fa332de5SLee Jones 	dma_cap_mask_t cap;
2374fa332de5SLee Jones 	u32 flags;
2375fa332de5SLee Jones 
2376fa332de5SLee Jones 	memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2377fa332de5SLee Jones 
2378fa332de5SLee Jones 	dma_cap_zero(cap);
2379fa332de5SLee Jones 	dma_cap_set(DMA_SLAVE, cap);
2380fa332de5SLee Jones 
2381fa332de5SLee Jones 	cfg.dev_type = dma_spec->args[0];
2382fa332de5SLee Jones 	flags = dma_spec->args[2];
2383fa332de5SLee Jones 
2384fa332de5SLee Jones 	switch (D40_DT_FLAGS_MODE(flags)) {
2385fa332de5SLee Jones 	case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2386fa332de5SLee Jones 	case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2387fa332de5SLee Jones 	}
2388fa332de5SLee Jones 
2389fa332de5SLee Jones 	switch (D40_DT_FLAGS_DIR(flags)) {
2390fa332de5SLee Jones 	case 0:
23912c2b62d5SLee Jones 		cfg.dir = DMA_MEM_TO_DEV;
2392fa332de5SLee Jones 		cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2393fa332de5SLee Jones 		break;
2394fa332de5SLee Jones 	case 1:
23952c2b62d5SLee Jones 		cfg.dir = DMA_DEV_TO_MEM;
2396fa332de5SLee Jones 		cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2397fa332de5SLee Jones 		break;
2398fa332de5SLee Jones 	}
2399fa332de5SLee Jones 
2400fa332de5SLee Jones 	if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2401fa332de5SLee Jones 		cfg.phy_channel = dma_spec->args[1];
2402fa332de5SLee Jones 		cfg.use_fixed_channel = true;
2403fa332de5SLee Jones 	}
2404fa332de5SLee Jones 
2405bddd5a2bSLee Jones 	if (D40_DT_FLAGS_HIGH_PRIO(flags))
2406bddd5a2bSLee Jones 		cfg.high_priority = true;
2407bddd5a2bSLee Jones 
2408fa332de5SLee Jones 	return dma_request_channel(cap, stedma40_filter, &cfg);
2409fa332de5SLee Jones }
2410fa332de5SLee Jones 
24118d318a50SLinus Walleij /* DMA ENGINE functions */
24128d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan)
24138d318a50SLinus Walleij {
24148d318a50SLinus Walleij 	int err;
24158d318a50SLinus Walleij 	unsigned long flags;
24168d318a50SLinus Walleij 	struct d40_chan *d40c =
24178d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
2418ef1872ecSLinus Walleij 	bool is_free_phy;
24198d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
24208d318a50SLinus Walleij 
2421d3ee98cdSRussell King - ARM Linux 	dma_cookie_init(chan);
24228d318a50SLinus Walleij 
2423ce2ca125SRabin Vincent 	/* If no dma configuration is set use default configuration (memcpy) */
2424ce2ca125SRabin Vincent 	if (!d40c->configured) {
24258d318a50SLinus Walleij 		err = d40_config_memcpy(d40c);
2426ff0b12baSJonas Aaberg 		if (err) {
24276db5a8baSRabin Vincent 			chan_err(d40c, "Failed to configure memcpy channel\n");
24288452b859SMarkus Elfring 			goto mark_last_busy;
2429ff0b12baSJonas Aaberg 		}
24308d318a50SLinus Walleij 	}
24318d318a50SLinus Walleij 
24325cd326fdSNarayanan G 	err = d40_allocate_channel(d40c, &is_free_phy);
24338d318a50SLinus Walleij 	if (err) {
24346db5a8baSRabin Vincent 		chan_err(d40c, "Failed to allocate channel\n");
24357fb3e75eSNarayanan G 		d40c->configured = false;
24368452b859SMarkus Elfring 		goto mark_last_busy;
24378d318a50SLinus Walleij 	}
24388d318a50SLinus Walleij 
24397fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
2440ef1872ecSLinus Walleij 
2441ac2c0a38SRabin Vincent 	d40_set_prio_realtime(d40c);
2442ac2c0a38SRabin Vincent 
2443724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
24442c2b62d5SLee Jones 		if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2445ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
244626955c07SLee Jones 				d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2447ef1872ecSLinus Walleij 		else
2448ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
244926955c07SLee Jones 				d40c->dma_cfg.dev_type *
2450ef1872ecSLinus Walleij 				D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
24519778256bSLee Jones 
24529778256bSLee Jones 		/* Unmask the Global Interrupt Mask. */
24539778256bSLee Jones 		d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
24549778256bSLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2455ef1872ecSLinus Walleij 	}
2456ef1872ecSLinus Walleij 
24575cd326fdSNarayanan G 	dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
24585cd326fdSNarayanan G 		 chan_is_logical(d40c) ? "logical" : "physical",
24595cd326fdSNarayanan G 		 d40c->phy_chan->num,
24605cd326fdSNarayanan G 		 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
24615cd326fdSNarayanan G 
24625cd326fdSNarayanan G 
2463ef1872ecSLinus Walleij 	/*
2464ef1872ecSLinus Walleij 	 * Only write channel configuration to the DMA if the physical
2465ef1872ecSLinus Walleij 	 * resource is free. In case of multiple logical channels
2466ef1872ecSLinus Walleij 	 * on the same physical resource, only the first write is necessary.
2467ef1872ecSLinus Walleij 	 */
2468b55912c6SJonas Aaberg 	if (is_free_phy)
2469b55912c6SJonas Aaberg 		d40_config_write(d40c);
24708452b859SMarkus Elfring  mark_last_busy:
24717fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
24727fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
24738d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
2474ff0b12baSJonas Aaberg 	return err;
24758d318a50SLinus Walleij }
24768d318a50SLinus Walleij 
24778d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan)
24788d318a50SLinus Walleij {
24798d318a50SLinus Walleij 	struct d40_chan *d40c =
24808d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
24818d318a50SLinus Walleij 	int err;
24828d318a50SLinus Walleij 	unsigned long flags;
24838d318a50SLinus Walleij 
24840d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
24856db5a8baSRabin Vincent 		chan_err(d40c, "Cannot free unallocated channel\n");
24860d0f6b8bSJonas Aaberg 		return;
24870d0f6b8bSJonas Aaberg 	}
24880d0f6b8bSJonas Aaberg 
24898d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
24908d318a50SLinus Walleij 
24918d318a50SLinus Walleij 	err = d40_free_dma(d40c);
24928d318a50SLinus Walleij 
24938d318a50SLinus Walleij 	if (err)
24946db5a8baSRabin Vincent 		chan_err(d40c, "Failed to free channel\n");
24958d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
24968d318a50SLinus Walleij }
24978d318a50SLinus Walleij 
24988d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
24998d318a50SLinus Walleij 						       dma_addr_t dst,
25008d318a50SLinus Walleij 						       dma_addr_t src,
25018d318a50SLinus Walleij 						       size_t size,
25022a614340SJonas Aaberg 						       unsigned long dma_flags)
25038d318a50SLinus Walleij {
250495944c6eSRabin Vincent 	struct scatterlist dst_sg;
250595944c6eSRabin Vincent 	struct scatterlist src_sg;
25068d318a50SLinus Walleij 
250795944c6eSRabin Vincent 	sg_init_table(&dst_sg, 1);
250895944c6eSRabin Vincent 	sg_init_table(&src_sg, 1);
25090d0f6b8bSJonas Aaberg 
251095944c6eSRabin Vincent 	sg_dma_address(&dst_sg) = dst;
251195944c6eSRabin Vincent 	sg_dma_address(&src_sg) = src;
25128d318a50SLinus Walleij 
251395944c6eSRabin Vincent 	sg_dma_len(&dst_sg) = size;
251495944c6eSRabin Vincent 	sg_dma_len(&src_sg) = size;
25158d318a50SLinus Walleij 
2516de6b641eSStefan Agner 	return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
2517de6b641eSStefan Agner 			   DMA_MEM_TO_MEM, dma_flags);
25188d318a50SLinus Walleij }
25198d318a50SLinus Walleij 
25200d688662SIra Snyder static struct dma_async_tx_descriptor *
2521f26e03adSFabio Baltieri d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2522f26e03adSFabio Baltieri 		  unsigned int sg_len, enum dma_transfer_direction direction,
2523f26e03adSFabio Baltieri 		  unsigned long dma_flags, void *context)
25248d318a50SLinus Walleij {
2525a725dcc0SAndy Shevchenko 	if (!is_slave_direction(direction))
252600ac0341SRabin Vincent 		return NULL;
252700ac0341SRabin Vincent 
2528cade1d30SRabin Vincent 	return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
25298d318a50SLinus Walleij }
25308d318a50SLinus Walleij 
25310c842b55SRabin Vincent static struct dma_async_tx_descriptor *
25320c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
25330c842b55SRabin Vincent 		     size_t buf_len, size_t period_len,
253431c1e5a1SLaurent Pinchart 		     enum dma_transfer_direction direction, unsigned long flags)
25350c842b55SRabin Vincent {
25360c842b55SRabin Vincent 	unsigned int periods = buf_len / period_len;
25370c842b55SRabin Vincent 	struct dma_async_tx_descriptor *txd;
25380c842b55SRabin Vincent 	struct scatterlist *sg;
25390c842b55SRabin Vincent 	int i;
25400c842b55SRabin Vincent 
254179ca7ec3SRobert Marklund 	sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
25422ec7e2e7SSachin Kamat 	if (!sg)
25432ec7e2e7SSachin Kamat 		return NULL;
25442ec7e2e7SSachin Kamat 
25450c842b55SRabin Vincent 	for (i = 0; i < periods; i++) {
25460c842b55SRabin Vincent 		sg_dma_address(&sg[i]) = dma_addr;
25470c842b55SRabin Vincent 		sg_dma_len(&sg[i]) = period_len;
25480c842b55SRabin Vincent 		dma_addr += period_len;
25490c842b55SRabin Vincent 	}
25500c842b55SRabin Vincent 
2551838b56adSLogan Gunthorpe 	sg_chain(sg, periods + 1, sg);
25520c842b55SRabin Vincent 
25530c842b55SRabin Vincent 	txd = d40_prep_sg(chan, sg, sg, periods, direction,
25540c842b55SRabin Vincent 			  DMA_PREP_INTERRUPT);
25550c842b55SRabin Vincent 
25560c842b55SRabin Vincent 	kfree(sg);
25570c842b55SRabin Vincent 
25580c842b55SRabin Vincent 	return txd;
25590c842b55SRabin Vincent }
25600c842b55SRabin Vincent 
25618d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan,
25628d318a50SLinus Walleij 				     dma_cookie_t cookie,
25638d318a50SLinus Walleij 				     struct dma_tx_state *txstate)
25648d318a50SLinus Walleij {
25658d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
256696a2af41SRussell King - ARM Linux 	enum dma_status ret;
25678d318a50SLinus Walleij 
25680d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
25696db5a8baSRabin Vincent 		chan_err(d40c, "Cannot read status of unallocated channel\n");
25700d0f6b8bSJonas Aaberg 		return -EINVAL;
25710d0f6b8bSJonas Aaberg 	}
25720d0f6b8bSJonas Aaberg 
257396a2af41SRussell King - ARM Linux 	ret = dma_cookie_status(chan, cookie, txstate);
2574a90e56e5SPeter Griffin 	if (ret != DMA_COMPLETE && txstate)
257596a2af41SRussell King - ARM Linux 		dma_set_residue(txstate, stedma40_residue(chan));
25768d318a50SLinus Walleij 
2577a5ebca47SJonas Aaberg 	if (d40_is_paused(d40c))
2578a5ebca47SJonas Aaberg 		ret = DMA_PAUSED;
25798d318a50SLinus Walleij 
25808d318a50SLinus Walleij 	return ret;
25818d318a50SLinus Walleij }
25828d318a50SLinus Walleij 
25838d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan)
25848d318a50SLinus Walleij {
25858d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
25868d318a50SLinus Walleij 	unsigned long flags;
25878d318a50SLinus Walleij 
25880d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
25896db5a8baSRabin Vincent 		chan_err(d40c, "Channel is not allocated!\n");
25900d0f6b8bSJonas Aaberg 		return;
25910d0f6b8bSJonas Aaberg 	}
25920d0f6b8bSJonas Aaberg 
25938d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
25948d318a50SLinus Walleij 
2595a8f3067bSPer Forlin 	list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2596a8f3067bSPer Forlin 
2597a8f3067bSPer Forlin 	/* Busy means that queued jobs are already being processed */
25988d318a50SLinus Walleij 	if (!d40c->busy)
25998d318a50SLinus Walleij 		(void) d40_queue_start(d40c);
26008d318a50SLinus Walleij 
26018d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
26028d318a50SLinus Walleij }
26038d318a50SLinus Walleij 
260435e639d1SVinod Koul static int d40_terminate_all(struct dma_chan *chan)
26051bdae6f4SNarayanan G {
26061bdae6f4SNarayanan G 	unsigned long flags;
26071bdae6f4SNarayanan G 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26081bdae6f4SNarayanan G 	int ret;
26091bdae6f4SNarayanan G 
26106f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
26116f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
26126f5bad03SMaxime Ripard 		return -EINVAL;
26136f5bad03SMaxime Ripard 	}
26146f5bad03SMaxime Ripard 
26151bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->lock, flags);
26161bdae6f4SNarayanan G 
26171bdae6f4SNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
26181bdae6f4SNarayanan G 	ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
26191bdae6f4SNarayanan G 	if (ret)
26201bdae6f4SNarayanan G 		chan_err(d40c, "Failed to stop channel\n");
26211bdae6f4SNarayanan G 
26221bdae6f4SNarayanan G 	d40_term_all(d40c);
26231bdae6f4SNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
26241bdae6f4SNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
26251bdae6f4SNarayanan G 	if (d40c->busy) {
26261bdae6f4SNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
26271bdae6f4SNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
26281bdae6f4SNarayanan G 	}
26291bdae6f4SNarayanan G 	d40c->busy = false;
26301bdae6f4SNarayanan G 
26311bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->lock, flags);
263235e639d1SVinod Koul 	return 0;
26331bdae6f4SNarayanan G }
26341bdae6f4SNarayanan G 
263598ca5289SRabin Vincent static int
263698ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c,
263798ca5289SRabin Vincent 			    struct stedma40_half_channel_info *info,
263898ca5289SRabin Vincent 			    u32 maxburst)
263998ca5289SRabin Vincent {
264098ca5289SRabin Vincent 	int psize;
264198ca5289SRabin Vincent 
264298ca5289SRabin Vincent 	if (chan_is_logical(d40c)) {
264398ca5289SRabin Vincent 		if (maxburst >= 16)
264498ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_16;
264598ca5289SRabin Vincent 		else if (maxburst >= 8)
264698ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_8;
264798ca5289SRabin Vincent 		else if (maxburst >= 4)
264898ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_4;
264998ca5289SRabin Vincent 		else
265098ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_1;
265198ca5289SRabin Vincent 	} else {
265298ca5289SRabin Vincent 		if (maxburst >= 16)
265398ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_16;
265498ca5289SRabin Vincent 		else if (maxburst >= 8)
265598ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_8;
265698ca5289SRabin Vincent 		else if (maxburst >= 4)
265798ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_4;
265898ca5289SRabin Vincent 		else
265998ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_1;
266098ca5289SRabin Vincent 	}
266198ca5289SRabin Vincent 
266298ca5289SRabin Vincent 	info->psize = psize;
266398ca5289SRabin Vincent 	info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
266498ca5289SRabin Vincent 
266598ca5289SRabin Vincent 	return 0;
266698ca5289SRabin Vincent }
266798ca5289SRabin Vincent 
266898ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan,
266995e1400fSLinus Walleij 				  struct dma_slave_config *config)
267095e1400fSLinus Walleij {
267195e1400fSLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26729e314ef3SVinod Koul 
26739e314ef3SVinod Koul 	memcpy(&d40c->slave_config, config, sizeof(*config));
26749e314ef3SVinod Koul 
26759e314ef3SVinod Koul 	return 0;
26769e314ef3SVinod Koul }
26779e314ef3SVinod Koul 
26789e314ef3SVinod Koul /* Runtime reconfiguration extension */
26799e314ef3SVinod Koul static int d40_set_runtime_config_write(struct dma_chan *chan,
26809e314ef3SVinod Koul 				  struct dma_slave_config *config,
26819e314ef3SVinod Koul 				  enum dma_transfer_direction direction)
26829e314ef3SVinod Koul {
26839e314ef3SVinod Koul 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
268495e1400fSLinus Walleij 	struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
268598ca5289SRabin Vincent 	enum dma_slave_buswidth src_addr_width, dst_addr_width;
268695e1400fSLinus Walleij 	dma_addr_t config_addr;
268798ca5289SRabin Vincent 	u32 src_maxburst, dst_maxburst;
268898ca5289SRabin Vincent 	int ret;
268998ca5289SRabin Vincent 
26906f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
26916f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
26926f5bad03SMaxime Ripard 		return -EINVAL;
26936f5bad03SMaxime Ripard 	}
26946f5bad03SMaxime Ripard 
269598ca5289SRabin Vincent 	src_addr_width = config->src_addr_width;
269698ca5289SRabin Vincent 	src_maxburst = config->src_maxburst;
269798ca5289SRabin Vincent 	dst_addr_width = config->dst_addr_width;
269898ca5289SRabin Vincent 	dst_maxburst = config->dst_maxburst;
269995e1400fSLinus Walleij 
27009e314ef3SVinod Koul 	if (direction == DMA_DEV_TO_MEM) {
270195e1400fSLinus Walleij 		config_addr = config->src_addr;
2702ef9c89b3SLee Jones 
27032c2b62d5SLee Jones 		if (cfg->dir != DMA_DEV_TO_MEM)
270495e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
270595e1400fSLinus Walleij 				"channel was not configured for peripheral "
270695e1400fSLinus Walleij 				"to memory transfer (%d) overriding\n",
270795e1400fSLinus Walleij 				cfg->dir);
27082c2b62d5SLee Jones 		cfg->dir = DMA_DEV_TO_MEM;
270995e1400fSLinus Walleij 
271098ca5289SRabin Vincent 		/* Configure the memory side */
271198ca5289SRabin Vincent 		if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
271298ca5289SRabin Vincent 			dst_addr_width = src_addr_width;
271398ca5289SRabin Vincent 		if (dst_maxburst == 0)
271498ca5289SRabin Vincent 			dst_maxburst = src_maxburst;
271595e1400fSLinus Walleij 
27169e314ef3SVinod Koul 	} else if (direction == DMA_MEM_TO_DEV) {
271795e1400fSLinus Walleij 		config_addr = config->dst_addr;
2718ef9c89b3SLee Jones 
27192c2b62d5SLee Jones 		if (cfg->dir != DMA_MEM_TO_DEV)
272095e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
272195e1400fSLinus Walleij 				"channel was not configured for memory "
272295e1400fSLinus Walleij 				"to peripheral transfer (%d) overriding\n",
272395e1400fSLinus Walleij 				cfg->dir);
27242c2b62d5SLee Jones 		cfg->dir = DMA_MEM_TO_DEV;
272595e1400fSLinus Walleij 
272698ca5289SRabin Vincent 		/* Configure the memory side */
272798ca5289SRabin Vincent 		if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
272898ca5289SRabin Vincent 			src_addr_width = dst_addr_width;
272998ca5289SRabin Vincent 		if (src_maxburst == 0)
273098ca5289SRabin Vincent 			src_maxburst = dst_maxburst;
273195e1400fSLinus Walleij 	} else {
273295e1400fSLinus Walleij 		dev_err(d40c->base->dev,
273395e1400fSLinus Walleij 			"unrecognized channel direction %d\n",
27349e314ef3SVinod Koul 			direction);
273598ca5289SRabin Vincent 		return -EINVAL;
273695e1400fSLinus Walleij 	}
273795e1400fSLinus Walleij 
2738ef9c89b3SLee Jones 	if (config_addr <= 0) {
2739ef9c89b3SLee Jones 		dev_err(d40c->base->dev, "no address supplied\n");
2740ef9c89b3SLee Jones 		return -EINVAL;
2741ef9c89b3SLee Jones 	}
2742ef9c89b3SLee Jones 
274398ca5289SRabin Vincent 	if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
274495e1400fSLinus Walleij 		dev_err(d40c->base->dev,
274598ca5289SRabin Vincent 			"src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
274698ca5289SRabin Vincent 			src_maxburst,
274798ca5289SRabin Vincent 			src_addr_width,
274898ca5289SRabin Vincent 			dst_maxburst,
274998ca5289SRabin Vincent 			dst_addr_width);
275098ca5289SRabin Vincent 		return -EINVAL;
275195e1400fSLinus Walleij 	}
275295e1400fSLinus Walleij 
275392bb6cdbSPer Forlin 	if (src_maxburst > 16) {
275492bb6cdbSPer Forlin 		src_maxburst = 16;
275592bb6cdbSPer Forlin 		dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
275692bb6cdbSPer Forlin 	} else if (dst_maxburst > 16) {
275792bb6cdbSPer Forlin 		dst_maxburst = 16;
275892bb6cdbSPer Forlin 		src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
275992bb6cdbSPer Forlin 	}
276092bb6cdbSPer Forlin 
276143f2e1a3SLee Jones 	/* Only valid widths are; 1, 2, 4 and 8. */
276243f2e1a3SLee Jones 	if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
276343f2e1a3SLee Jones 	    src_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
276443f2e1a3SLee Jones 	    dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
276543f2e1a3SLee Jones 	    dst_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
2766c95905a6SGuennadi Liakhovetski 	    !is_power_of_2(src_addr_width) ||
2767c95905a6SGuennadi Liakhovetski 	    !is_power_of_2(dst_addr_width))
276843f2e1a3SLee Jones 		return -EINVAL;
276943f2e1a3SLee Jones 
277043f2e1a3SLee Jones 	cfg->src_info.data_width = src_addr_width;
277143f2e1a3SLee Jones 	cfg->dst_info.data_width = dst_addr_width;
277243f2e1a3SLee Jones 
277398ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
277498ca5289SRabin Vincent 					  src_maxburst);
277598ca5289SRabin Vincent 	if (ret)
277698ca5289SRabin Vincent 		return ret;
277795e1400fSLinus Walleij 
277898ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
277998ca5289SRabin Vincent 					  dst_maxburst);
278098ca5289SRabin Vincent 	if (ret)
278198ca5289SRabin Vincent 		return ret;
278295e1400fSLinus Walleij 
2783a59670a4SPer Forlin 	/* Fill in register values */
2784724a8577SRabin Vincent 	if (chan_is_logical(d40c))
2785a59670a4SPer Forlin 		d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2786a59670a4SPer Forlin 	else
278757e65ad7SLee Jones 		d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2788a59670a4SPer Forlin 
278995e1400fSLinus Walleij 	/* These settings will take precedence later */
279095e1400fSLinus Walleij 	d40c->runtime_addr = config_addr;
27919e314ef3SVinod Koul 	d40c->runtime_direction = direction;
279295e1400fSLinus Walleij 	dev_dbg(d40c->base->dev,
279398ca5289SRabin Vincent 		"configured channel %s for %s, data width %d/%d, "
279498ca5289SRabin Vincent 		"maxburst %d/%d elements, LE, no flow control\n",
279595e1400fSLinus Walleij 		dma_chan_name(chan),
27969e314ef3SVinod Koul 		(direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
279798ca5289SRabin Vincent 		src_addr_width, dst_addr_width,
279898ca5289SRabin Vincent 		src_maxburst, dst_maxburst);
279998ca5289SRabin Vincent 
280098ca5289SRabin Vincent 	return 0;
280195e1400fSLinus Walleij }
280295e1400fSLinus Walleij 
28038d318a50SLinus Walleij /* Initialization functions */
28048d318a50SLinus Walleij 
28058d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
28068d318a50SLinus Walleij 				 struct d40_chan *chans, int offset,
28078d318a50SLinus Walleij 				 int num_chans)
28088d318a50SLinus Walleij {
28098d318a50SLinus Walleij 	int i = 0;
28108d318a50SLinus Walleij 	struct d40_chan *d40c;
28118d318a50SLinus Walleij 
28128d318a50SLinus Walleij 	INIT_LIST_HEAD(&dma->channels);
28138d318a50SLinus Walleij 
28148d318a50SLinus Walleij 	for (i = offset; i < offset + num_chans; i++) {
28158d318a50SLinus Walleij 		d40c = &chans[i];
28168d318a50SLinus Walleij 		d40c->base = base;
28178d318a50SLinus Walleij 		d40c->chan.device = dma;
28188d318a50SLinus Walleij 
28198d318a50SLinus Walleij 		spin_lock_init(&d40c->lock);
28208d318a50SLinus Walleij 
28218d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
28228d318a50SLinus Walleij 
28234226dd86SFabio Baltieri 		INIT_LIST_HEAD(&d40c->done);
28248d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->active);
28258d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->queue);
2826a8f3067bSPer Forlin 		INIT_LIST_HEAD(&d40c->pending_queue);
28278d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->client);
282882babbb3SPer Forlin 		INIT_LIST_HEAD(&d40c->prepare_queue);
28298d318a50SLinus Walleij 
2830b1880c90SAllen Pais 		tasklet_setup(&d40c->tasklet, dma_tasklet);
28318d318a50SLinus Walleij 
28328d318a50SLinus Walleij 		list_add_tail(&d40c->chan.device_node,
28338d318a50SLinus Walleij 			      &dma->channels);
28348d318a50SLinus Walleij 	}
28358d318a50SLinus Walleij }
28368d318a50SLinus Walleij 
28377ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
28387ad74a7cSRabin Vincent {
283949873e99SLinus Walleij 	if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) {
28407ad74a7cSRabin Vincent 		dev->device_prep_slave_sg = d40_prep_slave_sg;
284149873e99SLinus Walleij 		dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
284249873e99SLinus Walleij 	}
28437ad74a7cSRabin Vincent 
28447ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
28457ad74a7cSRabin Vincent 		dev->device_prep_dma_memcpy = d40_prep_memcpy;
284649873e99SLinus Walleij 		dev->directions = BIT(DMA_MEM_TO_MEM);
28477ad74a7cSRabin Vincent 		/*
28487ad74a7cSRabin Vincent 		 * This controller can only access address at even
28497ad74a7cSRabin Vincent 		 * 32bit boundaries, i.e. 2^2
28507ad74a7cSRabin Vincent 		 */
285177a68e56SMaxime Ripard 		dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
28527ad74a7cSRabin Vincent 	}
28537ad74a7cSRabin Vincent 
28540c842b55SRabin Vincent 	if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
28550c842b55SRabin Vincent 		dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
28560c842b55SRabin Vincent 
28577ad74a7cSRabin Vincent 	dev->device_alloc_chan_resources = d40_alloc_chan_resources;
28587ad74a7cSRabin Vincent 	dev->device_free_chan_resources = d40_free_chan_resources;
28597ad74a7cSRabin Vincent 	dev->device_issue_pending = d40_issue_pending;
28607ad74a7cSRabin Vincent 	dev->device_tx_status = d40_tx_status;
28616f5bad03SMaxime Ripard 	dev->device_config = d40_set_runtime_config;
28626f5bad03SMaxime Ripard 	dev->device_pause = d40_pause;
28636f5bad03SMaxime Ripard 	dev->device_resume = d40_resume;
28646f5bad03SMaxime Ripard 	dev->device_terminate_all = d40_terminate_all;
286515c60668SLinus Walleij 	dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
28667ad74a7cSRabin Vincent 	dev->dev = base->dev;
28677ad74a7cSRabin Vincent }
28687ad74a7cSRabin Vincent 
28698d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base,
28708d318a50SLinus Walleij 				     int num_reserved_chans)
28718d318a50SLinus Walleij {
28728d318a50SLinus Walleij 	int err ;
28738d318a50SLinus Walleij 
28748d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_slave, base->log_chans,
28758d318a50SLinus Walleij 		      0, base->num_log_chans);
28768d318a50SLinus Walleij 
28778d318a50SLinus Walleij 	dma_cap_zero(base->dma_slave.cap_mask);
28788d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
28790c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
28808d318a50SLinus Walleij 
28817ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_slave);
28828d318a50SLinus Walleij 
2883fc9826deSHuang Shijie 	err = dmaenginem_async_device_register(&base->dma_slave);
28848d318a50SLinus Walleij 
28858d318a50SLinus Walleij 	if (err) {
28866db5a8baSRabin Vincent 		d40_err(base->dev, "Failed to register slave channels\n");
2887c9909935SMarkus Elfring 		goto exit;
28888d318a50SLinus Walleij 	}
28898d318a50SLinus Walleij 
28908d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2891a7dacb68SLee Jones 		      base->num_log_chans, base->num_memcpy_chans);
28928d318a50SLinus Walleij 
28938d318a50SLinus Walleij 	dma_cap_zero(base->dma_memcpy.cap_mask);
28948d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
28958d318a50SLinus Walleij 
28967ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_memcpy);
28978d318a50SLinus Walleij 
2898fc9826deSHuang Shijie 	err = dmaenginem_async_device_register(&base->dma_memcpy);
28998d318a50SLinus Walleij 
29008d318a50SLinus Walleij 	if (err) {
29016db5a8baSRabin Vincent 		d40_err(base->dev,
290252984aabSGeliang Tang 			"Failed to register memcpy only channels\n");
2903fc9826deSHuang Shijie 		goto exit;
29048d318a50SLinus Walleij 	}
29058d318a50SLinus Walleij 
29068d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_both, base->phy_chans,
29078d318a50SLinus Walleij 		      0, num_reserved_chans);
29088d318a50SLinus Walleij 
29098d318a50SLinus Walleij 	dma_cap_zero(base->dma_both.cap_mask);
29108d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
29118d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
29120c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
29138d318a50SLinus Walleij 
29147ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_both);
2915fc9826deSHuang Shijie 	err = dmaenginem_async_device_register(&base->dma_both);
29168d318a50SLinus Walleij 
29178d318a50SLinus Walleij 	if (err) {
29186db5a8baSRabin Vincent 		d40_err(base->dev,
29196db5a8baSRabin Vincent 			"Failed to register logical and physical capable channels\n");
2920fc9826deSHuang Shijie 		goto exit;
29218d318a50SLinus Walleij 	}
29228d318a50SLinus Walleij 	return 0;
2923c9909935SMarkus Elfring  exit:
29248d318a50SLinus Walleij 	return err;
29258d318a50SLinus Walleij }
29268d318a50SLinus Walleij 
29277fb3e75eSNarayanan G /* Suspend resume functionality */
2928123e4ca1SUlf Hansson #ifdef CONFIG_PM_SLEEP
2929123e4ca1SUlf Hansson static int dma40_suspend(struct device *dev)
29307fb3e75eSNarayanan G {
2931be34c218SWolfram Sang 	struct d40_base *base = dev_get_drvdata(dev);
2932c906a3ecSUlf Hansson 	int ret;
2933c906a3ecSUlf Hansson 
2934c906a3ecSUlf Hansson 	ret = pm_runtime_force_suspend(dev);
2935c906a3ecSUlf Hansson 	if (ret)
2936c906a3ecSUlf Hansson 		return ret;
29377fb3e75eSNarayanan G 
293828c7a19dSNarayanan G 	if (base->lcpa_regulator)
293928c7a19dSNarayanan G 		ret = regulator_disable(base->lcpa_regulator);
294028c7a19dSNarayanan G 	return ret;
29417fb3e75eSNarayanan G }
29427fb3e75eSNarayanan G 
2943123e4ca1SUlf Hansson static int dma40_resume(struct device *dev)
2944123e4ca1SUlf Hansson {
2945be34c218SWolfram Sang 	struct d40_base *base = dev_get_drvdata(dev);
2946123e4ca1SUlf Hansson 	int ret = 0;
2947123e4ca1SUlf Hansson 
2948c906a3ecSUlf Hansson 	if (base->lcpa_regulator) {
2949123e4ca1SUlf Hansson 		ret = regulator_enable(base->lcpa_regulator);
2950c906a3ecSUlf Hansson 		if (ret)
2951123e4ca1SUlf Hansson 			return ret;
2952123e4ca1SUlf Hansson 	}
2953c906a3ecSUlf Hansson 
2954c906a3ecSUlf Hansson 	return pm_runtime_force_resume(dev);
2955c906a3ecSUlf Hansson }
2956123e4ca1SUlf Hansson #endif
2957123e4ca1SUlf Hansson 
2958123e4ca1SUlf Hansson #ifdef CONFIG_PM
2959123e4ca1SUlf Hansson static void dma40_backup(void __iomem *baseaddr, u32 *backup,
2960123e4ca1SUlf Hansson 			 u32 *regaddr, int num, bool save)
2961123e4ca1SUlf Hansson {
2962123e4ca1SUlf Hansson 	int i;
2963123e4ca1SUlf Hansson 
2964123e4ca1SUlf Hansson 	for (i = 0; i < num; i++) {
2965123e4ca1SUlf Hansson 		void __iomem *addr = baseaddr + regaddr[i];
2966123e4ca1SUlf Hansson 
2967123e4ca1SUlf Hansson 		if (save)
2968123e4ca1SUlf Hansson 			backup[i] = readl_relaxed(addr);
2969123e4ca1SUlf Hansson 		else
2970123e4ca1SUlf Hansson 			writel_relaxed(backup[i], addr);
2971123e4ca1SUlf Hansson 	}
2972123e4ca1SUlf Hansson }
2973123e4ca1SUlf Hansson 
2974123e4ca1SUlf Hansson static void d40_save_restore_registers(struct d40_base *base, bool save)
2975123e4ca1SUlf Hansson {
2976123e4ca1SUlf Hansson 	int i;
2977123e4ca1SUlf Hansson 
2978123e4ca1SUlf Hansson 	/* Save/Restore channel specific registers */
2979123e4ca1SUlf Hansson 	for (i = 0; i < base->num_phy_chans; i++) {
2980123e4ca1SUlf Hansson 		void __iomem *addr;
2981123e4ca1SUlf Hansson 		int idx;
2982123e4ca1SUlf Hansson 
2983123e4ca1SUlf Hansson 		if (base->phy_res[i].reserved)
2984123e4ca1SUlf Hansson 			continue;
2985123e4ca1SUlf Hansson 
2986123e4ca1SUlf Hansson 		addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
2987123e4ca1SUlf Hansson 		idx = i * ARRAY_SIZE(d40_backup_regs_chan);
2988123e4ca1SUlf Hansson 
2989123e4ca1SUlf Hansson 		dma40_backup(addr, &base->reg_val_backup_chan[idx],
2990123e4ca1SUlf Hansson 			     d40_backup_regs_chan,
2991123e4ca1SUlf Hansson 			     ARRAY_SIZE(d40_backup_regs_chan),
2992123e4ca1SUlf Hansson 			     save);
2993123e4ca1SUlf Hansson 	}
2994123e4ca1SUlf Hansson 
2995123e4ca1SUlf Hansson 	/* Save/Restore global registers */
2996123e4ca1SUlf Hansson 	dma40_backup(base->virtbase, base->reg_val_backup,
2997123e4ca1SUlf Hansson 		     d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
2998123e4ca1SUlf Hansson 		     save);
2999123e4ca1SUlf Hansson 
3000123e4ca1SUlf Hansson 	/* Save/Restore registers only existing on dma40 v3 and later */
3001123e4ca1SUlf Hansson 	if (base->gen_dmac.backup)
3002123e4ca1SUlf Hansson 		dma40_backup(base->virtbase, base->reg_val_backup_v4,
3003123e4ca1SUlf Hansson 			     base->gen_dmac.backup,
3004123e4ca1SUlf Hansson 			base->gen_dmac.backup_size,
3005123e4ca1SUlf Hansson 			save);
3006123e4ca1SUlf Hansson }
3007123e4ca1SUlf Hansson 
30087fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev)
30097fb3e75eSNarayanan G {
3010be34c218SWolfram Sang 	struct d40_base *base = dev_get_drvdata(dev);
30117fb3e75eSNarayanan G 
30127fb3e75eSNarayanan G 	d40_save_restore_registers(base, true);
30137fb3e75eSNarayanan G 
30147fb3e75eSNarayanan G 	/* Don't disable/enable clocks for v1 due to HW bugs */
30157fb3e75eSNarayanan G 	if (base->rev != 1)
30167fb3e75eSNarayanan G 		writel_relaxed(base->gcc_pwr_off_mask,
30177fb3e75eSNarayanan G 			       base->virtbase + D40_DREG_GCC);
30187fb3e75eSNarayanan G 
30197fb3e75eSNarayanan G 	return 0;
30207fb3e75eSNarayanan G }
30217fb3e75eSNarayanan G 
30227fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev)
30237fb3e75eSNarayanan G {
3024be34c218SWolfram Sang 	struct d40_base *base = dev_get_drvdata(dev);
30257fb3e75eSNarayanan G 
30267fb3e75eSNarayanan G 	d40_save_restore_registers(base, false);
30277fb3e75eSNarayanan G 
30287fb3e75eSNarayanan G 	writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
30297fb3e75eSNarayanan G 		       base->virtbase + D40_DREG_GCC);
30307fb3e75eSNarayanan G 	return 0;
30317fb3e75eSNarayanan G }
3032123e4ca1SUlf Hansson #endif
30337fb3e75eSNarayanan G 
30347fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = {
3035673d3773SUlf Hansson 	SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
30366ed23b80SRafael J. Wysocki 	SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
3037123e4ca1SUlf Hansson 				dma40_runtime_resume,
3038123e4ca1SUlf Hansson 				NULL)
30397fb3e75eSNarayanan G };
30407fb3e75eSNarayanan G 
30418d318a50SLinus Walleij /* Initialization functions. */
30428d318a50SLinus Walleij 
30438d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base)
30448d318a50SLinus Walleij {
30458d318a50SLinus Walleij 	int i;
30468d318a50SLinus Walleij 	int num_phy_chans_avail = 0;
30478d318a50SLinus Walleij 	u32 val[2];
30488d318a50SLinus Walleij 	int odd_even_bit = -2;
30497fb3e75eSNarayanan G 	int gcc = D40_DREG_GCC_ENA;
30508d318a50SLinus Walleij 
30518d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRSME);
30528d318a50SLinus Walleij 	val[1] = readl(base->virtbase + D40_DREG_PRSMO);
30538d318a50SLinus Walleij 
30548d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
30558d318a50SLinus Walleij 		base->phy_res[i].num = i;
30568d318a50SLinus Walleij 		odd_even_bit += 2 * ((i % 2) == 0);
30578d318a50SLinus Walleij 		if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
30588d318a50SLinus Walleij 			/* Mark security only channels as occupied */
30598d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_PHY;
30608d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
30617fb3e75eSNarayanan G 			base->phy_res[i].reserved = true;
30627fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30637fb3e75eSNarayanan G 						       D40_DREG_GCC_SRC);
30647fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30657fb3e75eSNarayanan G 						       D40_DREG_GCC_DST);
30667fb3e75eSNarayanan G 
30677fb3e75eSNarayanan G 
30688d318a50SLinus Walleij 		} else {
30698d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_FREE;
30708d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
30717fb3e75eSNarayanan G 			base->phy_res[i].reserved = false;
30728d318a50SLinus Walleij 			num_phy_chans_avail++;
30738d318a50SLinus Walleij 		}
30748d318a50SLinus Walleij 		spin_lock_init(&base->phy_res[i].lock);
30758d318a50SLinus Walleij 	}
30766b7acd84SJonas Aaberg 
30776b7acd84SJonas Aaberg 	/* Mark disabled channels as occupied */
30786b7acd84SJonas Aaberg 	for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3079f57b407cSRabin Vincent 		int chan = base->plat_data->disabled_channels[i];
3080f57b407cSRabin Vincent 
3081f57b407cSRabin Vincent 		base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3082f57b407cSRabin Vincent 		base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
30837fb3e75eSNarayanan G 		base->phy_res[chan].reserved = true;
30847fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
30857fb3e75eSNarayanan G 					       D40_DREG_GCC_SRC);
30867fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
30877fb3e75eSNarayanan G 					       D40_DREG_GCC_DST);
30886b7acd84SJonas Aaberg 		num_phy_chans_avail--;
30896b7acd84SJonas Aaberg 	}
30906b7acd84SJonas Aaberg 
30917407048bSFabio Baltieri 	/* Mark soft_lli channels */
30927407048bSFabio Baltieri 	for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
30937407048bSFabio Baltieri 		int chan = base->plat_data->soft_lli_chans[i];
30947407048bSFabio Baltieri 
30957407048bSFabio Baltieri 		base->phy_res[chan].use_soft_lli = true;
30967407048bSFabio Baltieri 	}
30977407048bSFabio Baltieri 
30988d318a50SLinus Walleij 	dev_info(base->dev, "%d of %d physical DMA channels available\n",
30998d318a50SLinus Walleij 		 num_phy_chans_avail, base->num_phy_chans);
31008d318a50SLinus Walleij 
31018d318a50SLinus Walleij 	/* Verify settings extended vs standard */
31028d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRTYP);
31038d318a50SLinus Walleij 
31048d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
31058d318a50SLinus Walleij 
31068d318a50SLinus Walleij 		if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
31078d318a50SLinus Walleij 		    (val[0] & 0x3) != 1)
31088d318a50SLinus Walleij 			dev_info(base->dev,
31098d318a50SLinus Walleij 				 "[%s] INFO: channel %d is misconfigured (%d)\n",
31108d318a50SLinus Walleij 				 __func__, i, val[0] & 0x3);
31118d318a50SLinus Walleij 
31128d318a50SLinus Walleij 		val[0] = val[0] >> 2;
31138d318a50SLinus Walleij 	}
31148d318a50SLinus Walleij 
31157fb3e75eSNarayanan G 	/*
31167fb3e75eSNarayanan G 	 * To keep things simple, Enable all clocks initially.
31177fb3e75eSNarayanan G 	 * The clocks will get managed later post channel allocation.
31187fb3e75eSNarayanan G 	 * The clocks for the event lines on which reserved channels exists
31197fb3e75eSNarayanan G 	 * are not managed here.
31207fb3e75eSNarayanan G 	 */
31217fb3e75eSNarayanan G 	writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
31227fb3e75eSNarayanan G 	base->gcc_pwr_off_mask = gcc;
31237fb3e75eSNarayanan G 
31248d318a50SLinus Walleij 	return num_phy_chans_avail;
31258d318a50SLinus Walleij }
31268d318a50SLinus Walleij 
3127339f5041SLinus Walleij /* Called from the registered devm action */
3128339f5041SLinus Walleij static void d40_drop_kmem_cache_action(void *d)
3129339f5041SLinus Walleij {
3130339f5041SLinus Walleij 	struct kmem_cache *desc_slab = d;
3131339f5041SLinus Walleij 
3132339f5041SLinus Walleij 	kmem_cache_destroy(desc_slab);
3133339f5041SLinus Walleij }
3134339f5041SLinus Walleij 
31352893f6bcSLinus Walleij static int __init d40_hw_detect_init(struct platform_device *pdev,
31362893f6bcSLinus Walleij 				     struct d40_base **retbase)
31378d318a50SLinus Walleij {
3138d4adcc01SJingoo Han 	struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
3139fb85a8c5SLinus Walleij 	struct device *dev = &pdev->dev;
314011f7a8d1SMarkus Elfring 	struct clk *clk;
314111f7a8d1SMarkus Elfring 	void __iomem *virtbase;
314211f7a8d1SMarkus Elfring 	struct d40_base *base;
314311f7a8d1SMarkus Elfring 	int num_log_chans;
31448d318a50SLinus Walleij 	int num_phy_chans;
3145a7dacb68SLee Jones 	int num_memcpy_chans;
31468d318a50SLinus Walleij 	int i;
3147f4b89764SLinus Walleij 	u32 pid;
3148f4b89764SLinus Walleij 	u32 cid;
3149f4b89764SLinus Walleij 	u8 rev;
3150339f5041SLinus Walleij 	int ret;
31518d318a50SLinus Walleij 
3152339f5041SLinus Walleij 	clk = devm_clk_get_enabled(dev, NULL);
3153339f5041SLinus Walleij 	if (IS_ERR(clk))
31542893f6bcSLinus Walleij 		return PTR_ERR(clk);
31558d318a50SLinus Walleij 
31568d318a50SLinus Walleij 	/* Get IO for DMAC base address */
3157339f5041SLinus Walleij 	virtbase = devm_platform_ioremap_resource_byname(pdev, "base");
31582893f6bcSLinus Walleij 	if (IS_ERR(virtbase))
31592893f6bcSLinus Walleij 		return PTR_ERR(virtbase);
31608d318a50SLinus Walleij 
3161f4b89764SLinus Walleij 	/* This is just a regular AMBA PrimeCell ID actually */
3162f4b89764SLinus Walleij 	for (pid = 0, i = 0; i < 4; i++)
3163339f5041SLinus Walleij 		pid |= (readl(virtbase + SZ_4K - 0x20 + 4 * i)
3164f4b89764SLinus Walleij 			& 255) << (i * 8);
3165f4b89764SLinus Walleij 	for (cid = 0, i = 0; i < 4; i++)
3166339f5041SLinus Walleij 		cid |= (readl(virtbase + SZ_4K - 0x10 + 4 * i)
3167f4b89764SLinus Walleij 			& 255) << (i * 8);
3168f4b89764SLinus Walleij 
3169f4b89764SLinus Walleij 	if (cid != AMBA_CID) {
3170fb85a8c5SLinus Walleij 		d40_err(dev, "Unknown hardware! No PrimeCell ID\n");
31712893f6bcSLinus Walleij 		return -EINVAL;
31728d318a50SLinus Walleij 	}
3173f4b89764SLinus Walleij 	if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3174fb85a8c5SLinus Walleij 		d40_err(dev, "Unknown designer! Got %x wanted %x\n",
3175f4b89764SLinus Walleij 			AMBA_MANF_BITS(pid),
3176f4b89764SLinus Walleij 			AMBA_VENDOR_ST);
31772893f6bcSLinus Walleij 		return -EINVAL;
31788d318a50SLinus Walleij 	}
3179f4b89764SLinus Walleij 	/*
3180f4b89764SLinus Walleij 	 * HW revision:
3181f4b89764SLinus Walleij 	 * DB8500ed has revision 0
3182f4b89764SLinus Walleij 	 * ? has revision 1
3183f4b89764SLinus Walleij 	 * DB8500v1 has revision 2
3184f4b89764SLinus Walleij 	 * DB8500v2 has revision 3
318547db92f4SGerald Baeza 	 * AP9540v1 has revision 4
318647db92f4SGerald Baeza 	 * DB8540v1 has revision 4
3187f4b89764SLinus Walleij 	 */
3188f4b89764SLinus Walleij 	rev = AMBA_REV_BITS(pid);
31898b2fe9b6SLee Jones 	if (rev < 2) {
3190fb85a8c5SLinus Walleij 		d40_err(dev, "hardware revision: %d is not supported", rev);
31912893f6bcSLinus Walleij 		return -EINVAL;
31928b2fe9b6SLee Jones 	}
31933ae0267fSJonas Aaberg 
31948d318a50SLinus Walleij 	/* The number of physical channels on this HW */
319547db92f4SGerald Baeza 	if (plat_data->num_of_phy_chans)
319647db92f4SGerald Baeza 		num_phy_chans = plat_data->num_of_phy_chans;
319747db92f4SGerald Baeza 	else
31988d318a50SLinus Walleij 		num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
31998d318a50SLinus Walleij 
3200a7dacb68SLee Jones 	/* The number of channels used for memcpy */
3201a7dacb68SLee Jones 	if (plat_data->num_of_memcpy_chans)
3202a7dacb68SLee Jones 		num_memcpy_chans = plat_data->num_of_memcpy_chans;
3203a7dacb68SLee Jones 	else
3204a7dacb68SLee Jones 		num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
3205a7dacb68SLee Jones 
3206db72da92SLee Jones 	num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3207db72da92SLee Jones 
3208fb85a8c5SLinus Walleij 	dev_info(dev,
3209339f5041SLinus Walleij 		 "hardware rev: %d with %d physical and %d logical channels\n",
3210339f5041SLinus Walleij 		 rev, num_phy_chans, num_log_chans);
32118d318a50SLinus Walleij 
3212339f5041SLinus Walleij 	base = devm_kzalloc(dev,
3213339f5041SLinus Walleij 		ALIGN(sizeof(struct d40_base), 4) +
3214a7dacb68SLee Jones 		(num_phy_chans + num_log_chans + num_memcpy_chans) *
32158d318a50SLinus Walleij 		sizeof(struct d40_chan), GFP_KERNEL);
32168d318a50SLinus Walleij 
3217339f5041SLinus Walleij 	if (!base)
32182893f6bcSLinus Walleij 		return -ENOMEM;
32198d318a50SLinus Walleij 
32203ae0267fSJonas Aaberg 	base->rev = rev;
32218d318a50SLinus Walleij 	base->clk = clk;
3222a7dacb68SLee Jones 	base->num_memcpy_chans = num_memcpy_chans;
32238d318a50SLinus Walleij 	base->num_phy_chans = num_phy_chans;
32248d318a50SLinus Walleij 	base->num_log_chans = num_log_chans;
32258d318a50SLinus Walleij 	base->virtbase = virtbase;
32268d318a50SLinus Walleij 	base->plat_data = plat_data;
3227fb85a8c5SLinus Walleij 	base->dev = dev;
32288d318a50SLinus Walleij 	base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
32298d318a50SLinus Walleij 	base->log_chans = &base->phy_chans[num_phy_chans];
32308d318a50SLinus Walleij 
32313cb645dcSTong Liu 	if (base->plat_data->num_of_phy_chans == 14) {
32323cb645dcSTong Liu 		base->gen_dmac.backup = d40_backup_regs_v4b;
32333cb645dcSTong Liu 		base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
32343cb645dcSTong Liu 		base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
32353cb645dcSTong Liu 		base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
32363cb645dcSTong Liu 		base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
32373cb645dcSTong Liu 		base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
32383cb645dcSTong Liu 		base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
32393cb645dcSTong Liu 		base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
32403cb645dcSTong Liu 		base->gen_dmac.il = il_v4b;
32413cb645dcSTong Liu 		base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
32423cb645dcSTong Liu 		base->gen_dmac.init_reg = dma_init_reg_v4b;
32433cb645dcSTong Liu 		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
32443cb645dcSTong Liu 	} else {
32453cb645dcSTong Liu 		if (base->rev >= 3) {
32463cb645dcSTong Liu 			base->gen_dmac.backup = d40_backup_regs_v4a;
32473cb645dcSTong Liu 			base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
32483cb645dcSTong Liu 		}
32493cb645dcSTong Liu 		base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
32503cb645dcSTong Liu 		base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
32513cb645dcSTong Liu 		base->gen_dmac.realtime_en = D40_DREG_RSEG1;
32523cb645dcSTong Liu 		base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
32533cb645dcSTong Liu 		base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
32543cb645dcSTong Liu 		base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
32553cb645dcSTong Liu 		base->gen_dmac.il = il_v4a;
32563cb645dcSTong Liu 		base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
32573cb645dcSTong Liu 		base->gen_dmac.init_reg = dma_init_reg_v4a;
32583cb645dcSTong Liu 		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
32593cb645dcSTong Liu 	}
32603cb645dcSTong Liu 
3261339f5041SLinus Walleij 	base->phy_res = devm_kcalloc(dev, num_phy_chans,
3262e349d4b7SMarkus Elfring 				     sizeof(*base->phy_res),
32638d318a50SLinus Walleij 				     GFP_KERNEL);
32648d318a50SLinus Walleij 	if (!base->phy_res)
32652893f6bcSLinus Walleij 		return -ENOMEM;
32668d318a50SLinus Walleij 
3267339f5041SLinus Walleij 	base->lookup_phy_chans = devm_kcalloc(dev, num_phy_chans,
3268e349d4b7SMarkus Elfring 					      sizeof(*base->lookup_phy_chans),
32698d318a50SLinus Walleij 					      GFP_KERNEL);
32708d318a50SLinus Walleij 	if (!base->lookup_phy_chans)
32712893f6bcSLinus Walleij 		return -ENOMEM;
32728d318a50SLinus Walleij 
3273339f5041SLinus Walleij 	base->lookup_log_chans = devm_kcalloc(dev, num_log_chans,
3274e349d4b7SMarkus Elfring 					      sizeof(*base->lookup_log_chans),
32758d318a50SLinus Walleij 					      GFP_KERNEL);
32768d318a50SLinus Walleij 	if (!base->lookup_log_chans)
32772893f6bcSLinus Walleij 		return -ENOMEM;
3278698e4732SJonas Aaberg 
3279339f5041SLinus Walleij 	base->reg_val_backup_chan = devm_kmalloc_array(dev, base->num_phy_chans,
32807fb3e75eSNarayanan G 						  sizeof(d40_backup_regs_chan),
32818d318a50SLinus Walleij 						  GFP_KERNEL);
32827fb3e75eSNarayanan G 	if (!base->reg_val_backup_chan)
32832893f6bcSLinus Walleij 		return -ENOMEM;
32847fb3e75eSNarayanan G 
3285339f5041SLinus Walleij 	base->lcla_pool.alloc_map = devm_kcalloc(dev, num_phy_chans
3286e349d4b7SMarkus Elfring 					    * D40_LCLA_LINK_PER_EVENT_GRP,
3287e349d4b7SMarkus Elfring 					    sizeof(*base->lcla_pool.alloc_map),
3288e349d4b7SMarkus Elfring 					    GFP_KERNEL);
32898d318a50SLinus Walleij 	if (!base->lcla_pool.alloc_map)
32902893f6bcSLinus Walleij 		return -ENOMEM;
32918d318a50SLinus Walleij 
3292339f5041SLinus Walleij 	base->regs_interrupt = devm_kmalloc_array(dev, base->gen_dmac.il_size,
3293e6a78511SKees Cook 					     sizeof(*base->regs_interrupt),
3294e6a78511SKees Cook 					     GFP_KERNEL);
3295e6a78511SKees Cook 	if (!base->regs_interrupt)
32962893f6bcSLinus Walleij 		return -ENOMEM;
3297e6a78511SKees Cook 
3298c675b1b4SJonas Aaberg 	base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3299c675b1b4SJonas Aaberg 					    0, SLAB_HWCACHE_ALIGN,
3300c675b1b4SJonas Aaberg 					    NULL);
3301339f5041SLinus Walleij 	if (!base->desc_slab)
33022893f6bcSLinus Walleij 		return -ENOMEM;
3303e6a78511SKees Cook 
3304339f5041SLinus Walleij 	ret = devm_add_action_or_reset(dev, d40_drop_kmem_cache_action,
3305339f5041SLinus Walleij 				       base->desc_slab);
3306339f5041SLinus Walleij 	if (ret)
33072893f6bcSLinus Walleij 		return ret;
3308c675b1b4SJonas Aaberg 
33092893f6bcSLinus Walleij 	*retbase = base;
33102893f6bcSLinus Walleij 
33112893f6bcSLinus Walleij 	return 0;
33128d318a50SLinus Walleij }
33138d318a50SLinus Walleij 
33148d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base)
33158d318a50SLinus Walleij {
33168d318a50SLinus Walleij 
33178d318a50SLinus Walleij 	int i;
33188d318a50SLinus Walleij 	u32 prmseo[2] = {0, 0};
33198d318a50SLinus Walleij 	u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
33208d318a50SLinus Walleij 	u32 pcmis = 0;
33218d318a50SLinus Walleij 	u32 pcicr = 0;
33223cb645dcSTong Liu 	struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
33233cb645dcSTong Liu 	u32 reg_size = base->gen_dmac.init_reg_size;
33248d318a50SLinus Walleij 
33253cb645dcSTong Liu 	for (i = 0; i < reg_size; i++)
33268d318a50SLinus Walleij 		writel(dma_init_reg[i].val,
33278d318a50SLinus Walleij 		       base->virtbase + dma_init_reg[i].reg);
33288d318a50SLinus Walleij 
33298d318a50SLinus Walleij 	/* Configure all our dma channels to default settings */
33308d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
33318d318a50SLinus Walleij 
33328d318a50SLinus Walleij 		activeo[i % 2] = activeo[i % 2] << 2;
33338d318a50SLinus Walleij 
33348d318a50SLinus Walleij 		if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
33358d318a50SLinus Walleij 		    == D40_ALLOC_PHY) {
33368d318a50SLinus Walleij 			activeo[i % 2] |= 3;
33378d318a50SLinus Walleij 			continue;
33388d318a50SLinus Walleij 		}
33398d318a50SLinus Walleij 
33408d318a50SLinus Walleij 		/* Enable interrupt # */
33418d318a50SLinus Walleij 		pcmis = (pcmis << 1) | 1;
33428d318a50SLinus Walleij 
33438d318a50SLinus Walleij 		/* Clear interrupt # */
33448d318a50SLinus Walleij 		pcicr = (pcicr << 1) | 1;
33458d318a50SLinus Walleij 
33468d318a50SLinus Walleij 		/* Set channel to physical mode */
33478d318a50SLinus Walleij 		prmseo[i % 2] = prmseo[i % 2] << 2;
33488d318a50SLinus Walleij 		prmseo[i % 2] |= 1;
33498d318a50SLinus Walleij 
33508d318a50SLinus Walleij 	}
33518d318a50SLinus Walleij 
33528d318a50SLinus Walleij 	writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
33538d318a50SLinus Walleij 	writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
33548d318a50SLinus Walleij 	writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
33558d318a50SLinus Walleij 	writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
33568d318a50SLinus Walleij 
33578d318a50SLinus Walleij 	/* Write which interrupt to enable */
33583cb645dcSTong Liu 	writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
33598d318a50SLinus Walleij 
33608d318a50SLinus Walleij 	/* Write which interrupt to clear */
33613cb645dcSTong Liu 	writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
33628d318a50SLinus Walleij 
33633cb645dcSTong Liu 	/* These are __initdata and cannot be accessed after init */
33643cb645dcSTong Liu 	base->gen_dmac.init_reg = NULL;
33653cb645dcSTong Liu 	base->gen_dmac.init_reg_size = 0;
33668d318a50SLinus Walleij }
33678d318a50SLinus Walleij 
3368508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base)
3369508849adSLinus Walleij {
3370026cbc42SRabin Vincent 	struct d40_lcla_pool *pool = &base->lcla_pool;
3371508849adSLinus Walleij 	unsigned long *page_list;
3372508849adSLinus Walleij 	int i, j;
3373abac5bacSMarkus Elfring 	int ret;
3374508849adSLinus Walleij 
3375508849adSLinus Walleij 	/*
3376508849adSLinus Walleij 	 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3377508849adSLinus Walleij 	 * To full fill this hardware requirement without wasting 256 kb
3378508849adSLinus Walleij 	 * we allocate pages until we get an aligned one.
3379508849adSLinus Walleij 	 */
3380cf80ecf7SMarkus Elfring 	page_list = kmalloc_array(MAX_LCLA_ALLOC_ATTEMPTS,
3381cf80ecf7SMarkus Elfring 				  sizeof(*page_list),
3382508849adSLinus Walleij 				  GFP_KERNEL);
33832c7f2f20SMarkus Elfring 	if (!page_list)
33842c7f2f20SMarkus Elfring 		return -ENOMEM;
3385508849adSLinus Walleij 
3386508849adSLinus Walleij 	/* Calculating how many pages that are required */
3387508849adSLinus Walleij 	base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3388508849adSLinus Walleij 
3389508849adSLinus Walleij 	for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3390508849adSLinus Walleij 		page_list[i] = __get_free_pages(GFP_KERNEL,
3391508849adSLinus Walleij 						base->lcla_pool.pages);
3392508849adSLinus Walleij 		if (!page_list[i]) {
3393508849adSLinus Walleij 
33946db5a8baSRabin Vincent 			d40_err(base->dev, "Failed to allocate %d pages.\n",
33956db5a8baSRabin Vincent 				base->lcla_pool.pages);
339639375334SJulia Lawall 			ret = -ENOMEM;
3397508849adSLinus Walleij 
3398508849adSLinus Walleij 			for (j = 0; j < i; j++)
3399508849adSLinus Walleij 				free_pages(page_list[j], base->lcla_pool.pages);
3400aae32ec6SMarkus Elfring 			goto free_page_list;
3401508849adSLinus Walleij 		}
3402508849adSLinus Walleij 
3403508849adSLinus Walleij 		if ((virt_to_phys((void *)page_list[i]) &
3404508849adSLinus Walleij 		     (LCLA_ALIGNMENT - 1)) == 0)
3405508849adSLinus Walleij 			break;
3406508849adSLinus Walleij 	}
3407508849adSLinus Walleij 
3408508849adSLinus Walleij 	for (j = 0; j < i; j++)
3409508849adSLinus Walleij 		free_pages(page_list[j], base->lcla_pool.pages);
3410508849adSLinus Walleij 
3411508849adSLinus Walleij 	if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3412508849adSLinus Walleij 		base->lcla_pool.base = (void *)page_list[i];
3413508849adSLinus Walleij 	} else {
3414767a9675SJonas Aaberg 		/*
3415767a9675SJonas Aaberg 		 * After many attempts and no succees with finding the correct
3416767a9675SJonas Aaberg 		 * alignment, try with allocating a big buffer.
3417767a9675SJonas Aaberg 		 */
3418508849adSLinus Walleij 		dev_warn(base->dev,
3419508849adSLinus Walleij 			 "[%s] Failed to get %d pages @ 18 bit align.\n",
3420508849adSLinus Walleij 			 __func__, base->lcla_pool.pages);
3421508849adSLinus Walleij 		base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3422508849adSLinus Walleij 							 base->num_phy_chans +
3423508849adSLinus Walleij 							 LCLA_ALIGNMENT,
3424508849adSLinus Walleij 							 GFP_KERNEL);
3425508849adSLinus Walleij 		if (!base->lcla_pool.base_unaligned) {
3426508849adSLinus Walleij 			ret = -ENOMEM;
3427aae32ec6SMarkus Elfring 			goto free_page_list;
3428508849adSLinus Walleij 		}
3429508849adSLinus Walleij 
3430508849adSLinus Walleij 		base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3431508849adSLinus Walleij 						 LCLA_ALIGNMENT);
3432508849adSLinus Walleij 	}
3433508849adSLinus Walleij 
3434026cbc42SRabin Vincent 	pool->dma_addr = dma_map_single(base->dev, pool->base,
3435026cbc42SRabin Vincent 					SZ_1K * base->num_phy_chans,
3436026cbc42SRabin Vincent 					DMA_TO_DEVICE);
3437026cbc42SRabin Vincent 	if (dma_mapping_error(base->dev, pool->dma_addr)) {
3438026cbc42SRabin Vincent 		pool->dma_addr = 0;
3439026cbc42SRabin Vincent 		ret = -ENOMEM;
3440aae32ec6SMarkus Elfring 		goto free_page_list;
3441026cbc42SRabin Vincent 	}
3442026cbc42SRabin Vincent 
3443508849adSLinus Walleij 	writel(virt_to_phys(base->lcla_pool.base),
3444508849adSLinus Walleij 	       base->virtbase + D40_DREG_LCLA);
3445abac5bacSMarkus Elfring 	ret = 0;
3446aae32ec6SMarkus Elfring  free_page_list:
3447508849adSLinus Walleij 	kfree(page_list);
3448508849adSLinus Walleij 	return ret;
3449508849adSLinus Walleij }
3450508849adSLinus Walleij 
3451e59d81e9SLinus Walleij static int __init d40_of_probe(struct device *dev,
34521814a170SLee Jones 			       struct device_node *np)
34531814a170SLee Jones {
34541814a170SLee Jones 	struct stedma40_platform_data *pdata;
3455499c2bc3SLee Jones 	int num_phy = 0, num_memcpy = 0, num_disabled = 0;
3456cbbe13eaSSachin Kamat 	const __be32 *list;
34571814a170SLee Jones 
3458e59d81e9SLinus Walleij 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
34591814a170SLee Jones 	if (!pdata)
34601814a170SLee Jones 		return -ENOMEM;
34611814a170SLee Jones 
3462fd59f9e6SLee Jones 	/* If absent this value will be obtained from h/w. */
3463fd59f9e6SLee Jones 	of_property_read_u32(np, "dma-channels", &num_phy);
3464fd59f9e6SLee Jones 	if (num_phy > 0)
3465fd59f9e6SLee Jones 		pdata->num_of_phy_chans = num_phy;
3466fd59f9e6SLee Jones 
3467a7dacb68SLee Jones 	list = of_get_property(np, "memcpy-channels", &num_memcpy);
3468a7dacb68SLee Jones 	num_memcpy /= sizeof(*list);
3469a7dacb68SLee Jones 
3470a7dacb68SLee Jones 	if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
3471e59d81e9SLinus Walleij 		d40_err(dev,
3472a7dacb68SLee Jones 			"Invalid number of memcpy channels specified (%d)\n",
3473a7dacb68SLee Jones 			num_memcpy);
3474a7dacb68SLee Jones 		return -EINVAL;
3475a7dacb68SLee Jones 	}
3476a7dacb68SLee Jones 	pdata->num_of_memcpy_chans = num_memcpy;
3477a7dacb68SLee Jones 
3478a7dacb68SLee Jones 	of_property_read_u32_array(np, "memcpy-channels",
3479a7dacb68SLee Jones 				   dma40_memcpy_channels,
3480a7dacb68SLee Jones 				   num_memcpy);
3481a7dacb68SLee Jones 
3482499c2bc3SLee Jones 	list = of_get_property(np, "disabled-channels", &num_disabled);
3483499c2bc3SLee Jones 	num_disabled /= sizeof(*list);
3484499c2bc3SLee Jones 
34855be2190aSDan Carpenter 	if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
3486e59d81e9SLinus Walleij 		d40_err(dev,
3487499c2bc3SLee Jones 			"Invalid number of disabled channels specified (%d)\n",
3488499c2bc3SLee Jones 			num_disabled);
3489499c2bc3SLee Jones 		return -EINVAL;
3490499c2bc3SLee Jones 	}
3491499c2bc3SLee Jones 
3492499c2bc3SLee Jones 	of_property_read_u32_array(np, "disabled-channels",
3493499c2bc3SLee Jones 				   pdata->disabled_channels,
3494499c2bc3SLee Jones 				   num_disabled);
3495499c2bc3SLee Jones 	pdata->disabled_channels[num_disabled] = -1;
3496499c2bc3SLee Jones 
3497e59d81e9SLinus Walleij 	dev->platform_data = pdata;
34981814a170SLee Jones 
34991814a170SLee Jones 	return 0;
35001814a170SLee Jones }
35011814a170SLee Jones 
35028d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev)
35038d318a50SLinus Walleij {
3504fb85a8c5SLinus Walleij 	struct device *dev = &pdev->dev;
35051814a170SLee Jones 	struct device_node *np = pdev->dev.of_node;
35065a1a3b9cSLinus Walleij 	struct device_node *np_lcpa;
3507a9bae06dSMarkus Elfring 	struct d40_base *base;
3508aeb8974aSMarkus Elfring 	struct resource *res;
35095a1a3b9cSLinus Walleij 	struct resource res_lcpa;
35108d318a50SLinus Walleij 	int num_reserved_chans;
35118d318a50SLinus Walleij 	u32 val;
35122893f6bcSLinus Walleij 	int ret;
35138d318a50SLinus Walleij 
3514e59d81e9SLinus Walleij 	if (d40_of_probe(dev, np)) {
35151814a170SLee Jones 		ret = -ENOMEM;
3516a9bae06dSMarkus Elfring 		goto report_failure;
35171814a170SLee Jones 	}
35188d318a50SLinus Walleij 
35192893f6bcSLinus Walleij 	ret = d40_hw_detect_init(pdev, &base);
35202893f6bcSLinus Walleij 	if (ret)
3521a9bae06dSMarkus Elfring 		goto report_failure;
35228d318a50SLinus Walleij 
35238d318a50SLinus Walleij 	num_reserved_chans = d40_phy_res_init(base);
35248d318a50SLinus Walleij 
35258d318a50SLinus Walleij 	platform_set_drvdata(pdev, base);
35268d318a50SLinus Walleij 
35278d318a50SLinus Walleij 	spin_lock_init(&base->interrupt_lock);
35288d318a50SLinus Walleij 	spin_lock_init(&base->execmd_lock);
35298d318a50SLinus Walleij 
35305a1a3b9cSLinus Walleij 	/* Get IO for logical channel parameter address (LCPA) */
35315a1a3b9cSLinus Walleij 	np_lcpa = of_parse_phandle(np, "sram", 0);
35325a1a3b9cSLinus Walleij 	if (!np_lcpa) {
3533fb85a8c5SLinus Walleij 		dev_err(dev, "no LCPA SRAM node\n");
35342893f6bcSLinus Walleij 		ret = -EINVAL;
35355a1a3b9cSLinus Walleij 		goto report_failure;
35368d318a50SLinus Walleij 	}
35375a1a3b9cSLinus Walleij 	/* This is no device so read the address directly from the node */
35385a1a3b9cSLinus Walleij 	ret = of_address_to_resource(np_lcpa, 0, &res_lcpa);
35395a1a3b9cSLinus Walleij 	if (ret) {
3540fb85a8c5SLinus Walleij 		dev_err(dev, "no LCPA SRAM resource\n");
35415a1a3b9cSLinus Walleij 		goto report_failure;
35428d318a50SLinus Walleij 	}
35435a1a3b9cSLinus Walleij 	base->lcpa_size = resource_size(&res_lcpa);
35445a1a3b9cSLinus Walleij 	base->phy_lcpa = res_lcpa.start;
3545*41be14c7SArnd Bergmann 	dev_info(dev, "found LCPA SRAM at %pad, size %pa\n",
3546*41be14c7SArnd Bergmann 		 &base->phy_lcpa, &base->lcpa_size);
35478d318a50SLinus Walleij 
35488d318a50SLinus Walleij 	/* We make use of ESRAM memory for this. */
35498d318a50SLinus Walleij 	val = readl(base->virtbase + D40_DREG_LCPA);
35505a1a3b9cSLinus Walleij 	if (base->phy_lcpa != val && val != 0) {
3551fb85a8c5SLinus Walleij 		dev_warn(dev,
35525a1a3b9cSLinus Walleij 			 "[%s] Mismatch LCPA dma 0x%x, def %08x\n",
35535a1a3b9cSLinus Walleij 			 __func__, val, (u32)base->phy_lcpa);
35548d318a50SLinus Walleij 	} else
35555a1a3b9cSLinus Walleij 		writel(base->phy_lcpa, base->virtbase + D40_DREG_LCPA);
35568d318a50SLinus Walleij 
3557339f5041SLinus Walleij 	base->lcpa_base = devm_ioremap(dev, base->phy_lcpa, base->lcpa_size);
35588d318a50SLinus Walleij 	if (!base->lcpa_base) {
35598d318a50SLinus Walleij 		ret = -ENOMEM;
3560fb85a8c5SLinus Walleij 		d40_err(dev, "Failed to ioremap LCPA region\n");
3561339f5041SLinus Walleij 		goto report_failure;
35628d318a50SLinus Walleij 	}
356328c7a19dSNarayanan G 	/* If lcla has to be located in ESRAM we don't need to allocate */
356428c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
356528c7a19dSNarayanan G 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
356628c7a19dSNarayanan G 							"lcla_esram");
356728c7a19dSNarayanan G 		if (!res) {
356828c7a19dSNarayanan G 			ret = -ENOENT;
3569fb85a8c5SLinus Walleij 			d40_err(dev,
357028c7a19dSNarayanan G 				"No \"lcla_esram\" memory resource\n");
3571339f5041SLinus Walleij 			goto report_failure;
357228c7a19dSNarayanan G 		}
3573339f5041SLinus Walleij 		base->lcla_pool.base = devm_ioremap(dev, res->start,
357428c7a19dSNarayanan G 						    resource_size(res));
357528c7a19dSNarayanan G 		if (!base->lcla_pool.base) {
357628c7a19dSNarayanan G 			ret = -ENOMEM;
3577fb85a8c5SLinus Walleij 			d40_err(dev, "Failed to ioremap LCLA region\n");
3578339f5041SLinus Walleij 			goto report_failure;
357928c7a19dSNarayanan G 		}
358028c7a19dSNarayanan G 		writel(res->start, base->virtbase + D40_DREG_LCLA);
3581508849adSLinus Walleij 
358228c7a19dSNarayanan G 	} else {
3583508849adSLinus Walleij 		ret = d40_lcla_allocate(base);
3584508849adSLinus Walleij 		if (ret) {
3585fb85a8c5SLinus Walleij 			d40_err(dev, "Failed to allocate LCLA area\n");
3586d7b7ecceSMarkus Elfring 			goto destroy_cache;
35878d318a50SLinus Walleij 		}
358828c7a19dSNarayanan G 	}
35898d318a50SLinus Walleij 
35908d318a50SLinus Walleij 	spin_lock_init(&base->lcla_pool.lock);
35918d318a50SLinus Walleij 
35928d318a50SLinus Walleij 	base->irq = platform_get_irq(pdev, 0);
35938d318a50SLinus Walleij 
35948d318a50SLinus Walleij 	ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
35958d318a50SLinus Walleij 	if (ret) {
3596fb85a8c5SLinus Walleij 		d40_err(dev, "No IRQ defined\n");
3597d7b7ecceSMarkus Elfring 		goto destroy_cache;
35988d318a50SLinus Walleij 	}
35998d318a50SLinus Walleij 
360028c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
360128c7a19dSNarayanan G 
360228c7a19dSNarayanan G 		base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
360328c7a19dSNarayanan G 		if (IS_ERR(base->lcpa_regulator)) {
3604fb85a8c5SLinus Walleij 			d40_err(dev, "Failed to get lcpa_regulator\n");
36058581bbcdSWei Yongjun 			ret = PTR_ERR(base->lcpa_regulator);
360628c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
3607d7b7ecceSMarkus Elfring 			goto destroy_cache;
360828c7a19dSNarayanan G 		}
360928c7a19dSNarayanan G 
361028c7a19dSNarayanan G 		ret = regulator_enable(base->lcpa_regulator);
361128c7a19dSNarayanan G 		if (ret) {
3612fb85a8c5SLinus Walleij 			d40_err(dev,
361328c7a19dSNarayanan G 				"Failed to enable lcpa_regulator\n");
361428c7a19dSNarayanan G 			regulator_put(base->lcpa_regulator);
361528c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
3616d7b7ecceSMarkus Elfring 			goto destroy_cache;
361728c7a19dSNarayanan G 		}
361828c7a19dSNarayanan G 	}
361928c7a19dSNarayanan G 
36202dafca17SUlf Hansson 	writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
36212dafca17SUlf Hansson 
36222dafca17SUlf Hansson 	pm_runtime_irq_safe(base->dev);
36232dafca17SUlf Hansson 	pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
36242dafca17SUlf Hansson 	pm_runtime_use_autosuspend(base->dev);
36252dafca17SUlf Hansson 	pm_runtime_mark_last_busy(base->dev);
36262dafca17SUlf Hansson 	pm_runtime_set_active(base->dev);
36272dafca17SUlf Hansson 	pm_runtime_enable(base->dev);
36282dafca17SUlf Hansson 
36298581bbcdSWei Yongjun 	ret = d40_dmaengine_init(base, num_reserved_chans);
36308581bbcdSWei Yongjun 	if (ret)
3631d7b7ecceSMarkus Elfring 		goto destroy_cache;
36328d318a50SLinus Walleij 
36338581bbcdSWei Yongjun 	ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
36348581bbcdSWei Yongjun 	if (ret) {
3635fb85a8c5SLinus Walleij 		d40_err(dev, "Failed to set dma max seg size\n");
3636d7b7ecceSMarkus Elfring 		goto destroy_cache;
3637b96710e5SPer Forlin 	}
3638b96710e5SPer Forlin 
36398d318a50SLinus Walleij 	d40_hw_init(base);
36408d318a50SLinus Walleij 
36418581bbcdSWei Yongjun 	ret = of_dma_controller_register(np, d40_xlate, NULL);
364242ae6f16SLinus Walleij 	if (ret) {
3643fb85a8c5SLinus Walleij 		dev_err(dev,
3644fa332de5SLee Jones 			"could not register of_dma_controller\n");
364542ae6f16SLinus Walleij 		goto destroy_cache;
3646fa332de5SLee Jones 	}
3647fa332de5SLee Jones 
36488d318a50SLinus Walleij 	dev_info(base->dev, "initialized\n");
36498d318a50SLinus Walleij 	return 0;
3650339f5041SLinus Walleij 
3651d7b7ecceSMarkus Elfring  destroy_cache:
3652026cbc42SRabin Vincent 	if (base->lcla_pool.dma_addr)
3653026cbc42SRabin Vincent 		dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3654026cbc42SRabin Vincent 				 SZ_1K * base->num_phy_chans,
3655026cbc42SRabin Vincent 				 DMA_TO_DEVICE);
3656026cbc42SRabin Vincent 
3657508849adSLinus Walleij 	if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3658508849adSLinus Walleij 		free_pages((unsigned long)base->lcla_pool.base,
3659508849adSLinus Walleij 			   base->lcla_pool.pages);
3660767a9675SJonas Aaberg 
3661508849adSLinus Walleij 	kfree(base->lcla_pool.base_unaligned);
3662767a9675SJonas Aaberg 
366328c7a19dSNarayanan G 	if (base->lcpa_regulator) {
366428c7a19dSNarayanan G 		regulator_disable(base->lcpa_regulator);
366528c7a19dSNarayanan G 		regulator_put(base->lcpa_regulator);
366628c7a19dSNarayanan G 	}
366728c7a19dSNarayanan G 
3668a9bae06dSMarkus Elfring  report_failure:
3669fb85a8c5SLinus Walleij 	d40_err(dev, "probe failed\n");
36708d318a50SLinus Walleij 	return ret;
36718d318a50SLinus Walleij }
36728d318a50SLinus Walleij 
36731814a170SLee Jones static const struct of_device_id d40_match[] = {
36741814a170SLee Jones         { .compatible = "stericsson,dma40", },
36751814a170SLee Jones         {}
36761814a170SLee Jones };
36771814a170SLee Jones 
36788d318a50SLinus Walleij static struct platform_driver d40_driver = {
36798d318a50SLinus Walleij 	.driver = {
36808d318a50SLinus Walleij 		.name  = D40_NAME,
3681123e4ca1SUlf Hansson 		.pm = &dma40_pm_ops,
36821814a170SLee Jones 		.of_match_table = d40_match,
36838d318a50SLinus Walleij 	},
36848d318a50SLinus Walleij };
36858d318a50SLinus Walleij 
3686cb9ab2d8SRabin Vincent static int __init stedma40_init(void)
36878d318a50SLinus Walleij {
36888d318a50SLinus Walleij 	return platform_driver_probe(&d40_driver, d40_probe);
36898d318a50SLinus Walleij }
3690a0eb221aSLinus Walleij subsys_initcall(stedma40_init);
3691