xref: /linux/drivers/dma/ste_dma40.c (revision abac5bac829cc9d8cf178344b4f34b2264927672)
18d318a50SLinus Walleij /*
2d49278e3SPer Forlin  * Copyright (C) Ericsson AB 2007-2008
3d49278e3SPer Forlin  * Copyright (C) ST-Ericsson SA 2008-2010
4661385f9SPer Forlin  * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
5767a9675SJonas Aaberg  * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
68d318a50SLinus Walleij  * License terms: GNU General Public License (GPL) version 2
78d318a50SLinus Walleij  */
88d318a50SLinus Walleij 
9b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h>
108d318a50SLinus Walleij #include <linux/kernel.h>
118d318a50SLinus Walleij #include <linux/slab.h>
12f492b210SPaul Gortmaker #include <linux/export.h>
138d318a50SLinus Walleij #include <linux/dmaengine.h>
148d318a50SLinus Walleij #include <linux/platform_device.h>
158d318a50SLinus Walleij #include <linux/clk.h>
168d318a50SLinus Walleij #include <linux/delay.h>
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>
22fa332de5SLee Jones #include <linux/of_dma.h>
23f4b89764SLinus Walleij #include <linux/amba/bus.h>
2415e4b78dSLinus Walleij #include <linux/regulator/consumer.h>
25865fab60SLinus Walleij #include <linux/platform_data/dma-ste-dma40.h>
268d318a50SLinus Walleij 
27d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
288d318a50SLinus Walleij #include "ste_dma40_ll.h"
298d318a50SLinus Walleij 
308d318a50SLinus Walleij #define D40_NAME "dma40"
318d318a50SLinus Walleij 
328d318a50SLinus Walleij #define D40_PHY_CHAN -1
338d318a50SLinus Walleij 
348d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */
358d318a50SLinus Walleij #define D40_CHAN_POS(chan)  (2 * (chan / 2))
368d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
378d318a50SLinus Walleij 
388d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */
398d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500
408d318a50SLinus Walleij 
417fb3e75eSNarayanan G /* Milliseconds */
427fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY	100
437fb3e75eSNarayanan G 
44508849adSLinus Walleij /* Hardware requirement on LCLA alignment */
45508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000
46698e4732SJonas Aaberg 
47698e4732SJonas Aaberg /* Max number of links per event group */
48698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128
49698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
50698e4732SJonas Aaberg 
51db72da92SLee Jones /* Max number of logical channels per physical channel */
52db72da92SLee Jones #define D40_MAX_LOG_CHAN_PER_PHY 32
53db72da92SLee Jones 
54508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */
55508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256
56508849adSLinus Walleij 
57508849adSLinus Walleij /* Bit markings for allocation map */
588a3b6e14SLee Jones #define D40_ALLOC_FREE		BIT(31)
598a3b6e14SLee Jones #define D40_ALLOC_PHY		BIT(30)
608d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE	0
618d318a50SLinus Walleij 
62a7dacb68SLee Jones #define D40_MEMCPY_MAX_CHANS	8
63a7dacb68SLee Jones 
64664a57ecSLee Jones /* Reserved event lines for memcpy only. */
65a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_0	51
66a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_1	56
67a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_2	57
68a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_3	58
69a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_4	59
70a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_5	60
71a2acaa21SLinus Walleij 
72a2acaa21SLinus Walleij static int dma40_memcpy_channels[] = {
73a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_0,
74a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_1,
75a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_2,
76a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_3,
77a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_4,
78a2acaa21SLinus Walleij 	DB8500_DMA_MEMCPY_EV_5,
79a2acaa21SLinus Walleij };
80664a57ecSLee Jones 
8129027a1eSLee Jones /* Default configuration for physcial memcpy */
82b4a1ccdfSFabio Baltieri static struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
8329027a1eSLee Jones 	.mode = STEDMA40_MODE_PHYSICAL,
842c2b62d5SLee Jones 	.dir = DMA_MEM_TO_MEM,
8529027a1eSLee Jones 
8643f2e1a3SLee Jones 	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
8729027a1eSLee Jones 	.src_info.psize = STEDMA40_PSIZE_PHY_1,
8829027a1eSLee Jones 	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
8929027a1eSLee Jones 
9043f2e1a3SLee Jones 	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
9129027a1eSLee Jones 	.dst_info.psize = STEDMA40_PSIZE_PHY_1,
9229027a1eSLee Jones 	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
9329027a1eSLee Jones };
9429027a1eSLee Jones 
9529027a1eSLee Jones /* Default configuration for logical memcpy */
96b4a1ccdfSFabio Baltieri static struct stedma40_chan_cfg dma40_memcpy_conf_log = {
9729027a1eSLee Jones 	.mode = STEDMA40_MODE_LOGICAL,
982c2b62d5SLee Jones 	.dir = DMA_MEM_TO_MEM,
9929027a1eSLee Jones 
10043f2e1a3SLee Jones 	.src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
10129027a1eSLee Jones 	.src_info.psize = STEDMA40_PSIZE_LOG_1,
10229027a1eSLee Jones 	.src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
10329027a1eSLee Jones 
10443f2e1a3SLee Jones 	.dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
10529027a1eSLee Jones 	.dst_info.psize = STEDMA40_PSIZE_LOG_1,
10629027a1eSLee Jones 	.dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
10729027a1eSLee Jones };
10829027a1eSLee Jones 
1098d318a50SLinus Walleij /**
1108d318a50SLinus Walleij  * enum 40_command - The different commands and/or statuses.
1118d318a50SLinus Walleij  *
1128d318a50SLinus Walleij  * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
1138d318a50SLinus Walleij  * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
1148d318a50SLinus Walleij  * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
1158d318a50SLinus Walleij  * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
1168d318a50SLinus Walleij  */
1178d318a50SLinus Walleij enum d40_command {
1188d318a50SLinus Walleij 	D40_DMA_STOP		= 0,
1198d318a50SLinus Walleij 	D40_DMA_RUN		= 1,
1208d318a50SLinus Walleij 	D40_DMA_SUSPEND_REQ	= 2,
1218d318a50SLinus Walleij 	D40_DMA_SUSPENDED	= 3
1228d318a50SLinus Walleij };
1238d318a50SLinus Walleij 
1247fb3e75eSNarayanan G /*
1251bdae6f4SNarayanan G  * enum d40_events - The different Event Enables for the event lines.
1261bdae6f4SNarayanan G  *
1271bdae6f4SNarayanan G  * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
1281bdae6f4SNarayanan G  * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
1291bdae6f4SNarayanan G  * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
1301bdae6f4SNarayanan G  * @D40_ROUND_EVENTLINE: Status check for event line.
1311bdae6f4SNarayanan G  */
1321bdae6f4SNarayanan G 
1331bdae6f4SNarayanan G enum d40_events {
1341bdae6f4SNarayanan G 	D40_DEACTIVATE_EVENTLINE	= 0,
1351bdae6f4SNarayanan G 	D40_ACTIVATE_EVENTLINE		= 1,
1361bdae6f4SNarayanan G 	D40_SUSPEND_REQ_EVENTLINE	= 2,
1371bdae6f4SNarayanan G 	D40_ROUND_EVENTLINE		= 3
1381bdae6f4SNarayanan G };
1391bdae6f4SNarayanan G 
1401bdae6f4SNarayanan G /*
1417fb3e75eSNarayanan G  * These are the registers that has to be saved and later restored
1427fb3e75eSNarayanan G  * when the DMA hw is powered off.
1437fb3e75eSNarayanan G  * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
1447fb3e75eSNarayanan G  */
1457fb3e75eSNarayanan G static u32 d40_backup_regs[] = {
1467fb3e75eSNarayanan G 	D40_DREG_LCPA,
1477fb3e75eSNarayanan G 	D40_DREG_LCLA,
1487fb3e75eSNarayanan G 	D40_DREG_PRMSE,
1497fb3e75eSNarayanan G 	D40_DREG_PRMSO,
1507fb3e75eSNarayanan G 	D40_DREG_PRMOE,
1517fb3e75eSNarayanan G 	D40_DREG_PRMOO,
1527fb3e75eSNarayanan G };
1537fb3e75eSNarayanan G 
1547fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
1557fb3e75eSNarayanan G 
1563cb645dcSTong Liu /*
1573cb645dcSTong Liu  * since 9540 and 8540 has the same HW revision
1583cb645dcSTong Liu  * use v4a for 9540 or ealier
1593cb645dcSTong Liu  * use v4b for 8540 or later
1603cb645dcSTong Liu  * HW revision:
1613cb645dcSTong Liu  * DB8500ed has revision 0
1623cb645dcSTong Liu  * DB8500v1 has revision 2
1633cb645dcSTong Liu  * DB8500v2 has revision 3
1643cb645dcSTong Liu  * AP9540v1 has revision 4
1653cb645dcSTong Liu  * DB8540v1 has revision 4
1663cb645dcSTong Liu  * TODO: Check if all these registers have to be saved/restored on dma40 v4a
1673cb645dcSTong Liu  */
1683cb645dcSTong Liu static u32 d40_backup_regs_v4a[] = {
1697fb3e75eSNarayanan G 	D40_DREG_PSEG1,
1707fb3e75eSNarayanan G 	D40_DREG_PSEG2,
1717fb3e75eSNarayanan G 	D40_DREG_PSEG3,
1727fb3e75eSNarayanan G 	D40_DREG_PSEG4,
1737fb3e75eSNarayanan G 	D40_DREG_PCEG1,
1747fb3e75eSNarayanan G 	D40_DREG_PCEG2,
1757fb3e75eSNarayanan G 	D40_DREG_PCEG3,
1767fb3e75eSNarayanan G 	D40_DREG_PCEG4,
1777fb3e75eSNarayanan G 	D40_DREG_RSEG1,
1787fb3e75eSNarayanan G 	D40_DREG_RSEG2,
1797fb3e75eSNarayanan G 	D40_DREG_RSEG3,
1807fb3e75eSNarayanan G 	D40_DREG_RSEG4,
1817fb3e75eSNarayanan G 	D40_DREG_RCEG1,
1827fb3e75eSNarayanan G 	D40_DREG_RCEG2,
1837fb3e75eSNarayanan G 	D40_DREG_RCEG3,
1847fb3e75eSNarayanan G 	D40_DREG_RCEG4,
1857fb3e75eSNarayanan G };
1867fb3e75eSNarayanan G 
1873cb645dcSTong Liu #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
1883cb645dcSTong Liu 
1893cb645dcSTong Liu static u32 d40_backup_regs_v4b[] = {
1903cb645dcSTong Liu 	D40_DREG_CPSEG1,
1913cb645dcSTong Liu 	D40_DREG_CPSEG2,
1923cb645dcSTong Liu 	D40_DREG_CPSEG3,
1933cb645dcSTong Liu 	D40_DREG_CPSEG4,
1943cb645dcSTong Liu 	D40_DREG_CPSEG5,
1953cb645dcSTong Liu 	D40_DREG_CPCEG1,
1963cb645dcSTong Liu 	D40_DREG_CPCEG2,
1973cb645dcSTong Liu 	D40_DREG_CPCEG3,
1983cb645dcSTong Liu 	D40_DREG_CPCEG4,
1993cb645dcSTong Liu 	D40_DREG_CPCEG5,
2003cb645dcSTong Liu 	D40_DREG_CRSEG1,
2013cb645dcSTong Liu 	D40_DREG_CRSEG2,
2023cb645dcSTong Liu 	D40_DREG_CRSEG3,
2033cb645dcSTong Liu 	D40_DREG_CRSEG4,
2043cb645dcSTong Liu 	D40_DREG_CRSEG5,
2053cb645dcSTong Liu 	D40_DREG_CRCEG1,
2063cb645dcSTong Liu 	D40_DREG_CRCEG2,
2073cb645dcSTong Liu 	D40_DREG_CRCEG3,
2083cb645dcSTong Liu 	D40_DREG_CRCEG4,
2093cb645dcSTong Liu 	D40_DREG_CRCEG5,
2103cb645dcSTong Liu };
2113cb645dcSTong Liu 
2123cb645dcSTong Liu #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
2137fb3e75eSNarayanan G 
2147fb3e75eSNarayanan G static u32 d40_backup_regs_chan[] = {
2157fb3e75eSNarayanan G 	D40_CHAN_REG_SSCFG,
2167fb3e75eSNarayanan G 	D40_CHAN_REG_SSELT,
2177fb3e75eSNarayanan G 	D40_CHAN_REG_SSPTR,
2187fb3e75eSNarayanan G 	D40_CHAN_REG_SSLNK,
2197fb3e75eSNarayanan G 	D40_CHAN_REG_SDCFG,
2207fb3e75eSNarayanan G 	D40_CHAN_REG_SDELT,
2217fb3e75eSNarayanan G 	D40_CHAN_REG_SDPTR,
2227fb3e75eSNarayanan G 	D40_CHAN_REG_SDLNK,
2237fb3e75eSNarayanan G };
2247fb3e75eSNarayanan G 
22584b3da14SLee Jones #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
22684b3da14SLee Jones 			     BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
22784b3da14SLee Jones 
2288d318a50SLinus Walleij /**
2293cb645dcSTong Liu  * struct d40_interrupt_lookup - lookup table for interrupt handler
2303cb645dcSTong Liu  *
2313cb645dcSTong Liu  * @src: Interrupt mask register.
2323cb645dcSTong Liu  * @clr: Interrupt clear register.
2333cb645dcSTong Liu  * @is_error: true if this is an error interrupt.
2343cb645dcSTong Liu  * @offset: start delta in the lookup_log_chans in d40_base. If equals to
2353cb645dcSTong Liu  * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
2363cb645dcSTong Liu  */
2373cb645dcSTong Liu struct d40_interrupt_lookup {
2383cb645dcSTong Liu 	u32 src;
2393cb645dcSTong Liu 	u32 clr;
2403cb645dcSTong Liu 	bool is_error;
2413cb645dcSTong Liu 	int offset;
2423cb645dcSTong Liu };
2433cb645dcSTong Liu 
2443cb645dcSTong Liu 
2453cb645dcSTong Liu static struct d40_interrupt_lookup il_v4a[] = {
2463cb645dcSTong Liu 	{D40_DREG_LCTIS0, D40_DREG_LCICR0, false,  0},
2473cb645dcSTong Liu 	{D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
2483cb645dcSTong Liu 	{D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
2493cb645dcSTong Liu 	{D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
2503cb645dcSTong Liu 	{D40_DREG_LCEIS0, D40_DREG_LCICR0, true,   0},
2513cb645dcSTong Liu 	{D40_DREG_LCEIS1, D40_DREG_LCICR1, true,  32},
2523cb645dcSTong Liu 	{D40_DREG_LCEIS2, D40_DREG_LCICR2, true,  64},
2533cb645dcSTong Liu 	{D40_DREG_LCEIS3, D40_DREG_LCICR3, true,  96},
2543cb645dcSTong Liu 	{D40_DREG_PCTIS,  D40_DREG_PCICR,  false, D40_PHY_CHAN},
2553cb645dcSTong Liu 	{D40_DREG_PCEIS,  D40_DREG_PCICR,  true,  D40_PHY_CHAN},
2563cb645dcSTong Liu };
2573cb645dcSTong Liu 
2583cb645dcSTong Liu static struct d40_interrupt_lookup il_v4b[] = {
2593cb645dcSTong Liu 	{D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false,  0},
2603cb645dcSTong Liu 	{D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
2613cb645dcSTong Liu 	{D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
2623cb645dcSTong Liu 	{D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
2633cb645dcSTong Liu 	{D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
2643cb645dcSTong Liu 	{D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true,   0},
2653cb645dcSTong Liu 	{D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true,  32},
2663cb645dcSTong Liu 	{D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true,  64},
2673cb645dcSTong Liu 	{D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true,  96},
2683cb645dcSTong Liu 	{D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true,  128},
2693cb645dcSTong Liu 	{D40_DREG_CPCTIS,  D40_DREG_CPCICR,  false, D40_PHY_CHAN},
2703cb645dcSTong Liu 	{D40_DREG_CPCEIS,  D40_DREG_CPCICR,  true,  D40_PHY_CHAN},
2713cb645dcSTong Liu };
2723cb645dcSTong Liu 
2733cb645dcSTong Liu /**
2743cb645dcSTong Liu  * struct d40_reg_val - simple lookup struct
2753cb645dcSTong Liu  *
2763cb645dcSTong Liu  * @reg: The register.
2773cb645dcSTong Liu  * @val: The value that belongs to the register in reg.
2783cb645dcSTong Liu  */
2793cb645dcSTong Liu struct d40_reg_val {
2803cb645dcSTong Liu 	unsigned int reg;
2813cb645dcSTong Liu 	unsigned int val;
2823cb645dcSTong Liu };
2833cb645dcSTong Liu 
2843cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
2853cb645dcSTong Liu 	/* Clock every part of the DMA block from start */
2863cb645dcSTong Liu 	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},
2873cb645dcSTong Liu 
2883cb645dcSTong Liu 	/* Interrupts on all logical channels */
2893cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
2903cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
2913cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
2923cb645dcSTong Liu 	{ .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
2933cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
2943cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
2953cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
2963cb645dcSTong Liu 	{ .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
2973cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
2983cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
2993cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
3003cb645dcSTong Liu 	{ .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
3013cb645dcSTong Liu };
3023cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
3033cb645dcSTong Liu 	/* Clock every part of the DMA block from start */
3043cb645dcSTong Liu 	{ .reg = D40_DREG_GCC,    .val = D40_DREG_GCC_ENABLE_ALL},
3053cb645dcSTong Liu 
3063cb645dcSTong Liu 	/* Interrupts on all logical channels */
3073cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
3083cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
3093cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
3103cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
3113cb645dcSTong Liu 	{ .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
3123cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
3133cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
3143cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
3153cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
3163cb645dcSTong Liu 	{ .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
3173cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
3183cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
3193cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
3203cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
3213cb645dcSTong Liu 	{ .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
3223cb645dcSTong Liu };
3233cb645dcSTong Liu 
3243cb645dcSTong Liu /**
3258d318a50SLinus Walleij  * struct d40_lli_pool - Structure for keeping LLIs in memory
3268d318a50SLinus Walleij  *
3278d318a50SLinus Walleij  * @base: Pointer to memory area when the pre_alloc_lli's are not large
3288d318a50SLinus Walleij  * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
3298d318a50SLinus Walleij  * pre_alloc_lli is used.
330b00f938cSRabin Vincent  * @dma_addr: DMA address, if mapped
3318d318a50SLinus Walleij  * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
3328d318a50SLinus Walleij  * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
3338d318a50SLinus Walleij  * one buffer to one buffer.
3348d318a50SLinus Walleij  */
3358d318a50SLinus Walleij struct d40_lli_pool {
3368d318a50SLinus Walleij 	void	*base;
3378d318a50SLinus Walleij 	int	 size;
338b00f938cSRabin Vincent 	dma_addr_t	dma_addr;
3398d318a50SLinus Walleij 	/* Space for dst and src, plus an extra for padding */
3408d318a50SLinus Walleij 	u8	 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
3418d318a50SLinus Walleij };
3428d318a50SLinus Walleij 
3438d318a50SLinus Walleij /**
3448d318a50SLinus Walleij  * struct d40_desc - A descriptor is one DMA job.
3458d318a50SLinus Walleij  *
3468d318a50SLinus Walleij  * @lli_phy: LLI settings for physical channel. Both src and dst=
3478d318a50SLinus Walleij  * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
3488d318a50SLinus Walleij  * lli_len equals one.
3498d318a50SLinus Walleij  * @lli_log: Same as above but for logical channels.
3508d318a50SLinus Walleij  * @lli_pool: The pool with two entries pre-allocated.
351941b77a3SPer Friden  * @lli_len: Number of llis of current descriptor.
35225985edcSLucas De Marchi  * @lli_current: Number of transferred llis.
353698e4732SJonas Aaberg  * @lcla_alloc: Number of LCLA entries allocated.
3548d318a50SLinus Walleij  * @txd: DMA engine struct. Used for among other things for communication
3558d318a50SLinus Walleij  * during a transfer.
3568d318a50SLinus Walleij  * @node: List entry.
3578d318a50SLinus Walleij  * @is_in_client_list: true if the client owns this descriptor.
3587fb3e75eSNarayanan G  * @cyclic: true if this is a cyclic job
3598d318a50SLinus Walleij  *
3608d318a50SLinus Walleij  * This descriptor is used for both logical and physical transfers.
3618d318a50SLinus Walleij  */
3628d318a50SLinus Walleij struct d40_desc {
3638d318a50SLinus Walleij 	/* LLI physical */
3648d318a50SLinus Walleij 	struct d40_phy_lli_bidir	 lli_phy;
3658d318a50SLinus Walleij 	/* LLI logical */
3668d318a50SLinus Walleij 	struct d40_log_lli_bidir	 lli_log;
3678d318a50SLinus Walleij 
3688d318a50SLinus Walleij 	struct d40_lli_pool		 lli_pool;
369941b77a3SPer Friden 	int				 lli_len;
370698e4732SJonas Aaberg 	int				 lli_current;
371698e4732SJonas Aaberg 	int				 lcla_alloc;
3728d318a50SLinus Walleij 
3738d318a50SLinus Walleij 	struct dma_async_tx_descriptor	 txd;
3748d318a50SLinus Walleij 	struct list_head		 node;
3758d318a50SLinus Walleij 
3768d318a50SLinus Walleij 	bool				 is_in_client_list;
3770c842b55SRabin Vincent 	bool				 cyclic;
3788d318a50SLinus Walleij };
3798d318a50SLinus Walleij 
3808d318a50SLinus Walleij /**
3818d318a50SLinus Walleij  * struct d40_lcla_pool - LCLA pool settings and data.
3828d318a50SLinus Walleij  *
383508849adSLinus Walleij  * @base: The virtual address of LCLA. 18 bit aligned.
384508849adSLinus Walleij  * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
385508849adSLinus Walleij  * This pointer is only there for clean-up on error.
386508849adSLinus Walleij  * @pages: The number of pages needed for all physical channels.
387508849adSLinus Walleij  * Only used later for clean-up on error
3888d318a50SLinus Walleij  * @lock: Lock to protect the content in this struct.
389698e4732SJonas Aaberg  * @alloc_map: big map over which LCLA entry is own by which job.
3908d318a50SLinus Walleij  */
3918d318a50SLinus Walleij struct d40_lcla_pool {
3928d318a50SLinus Walleij 	void		*base;
393026cbc42SRabin Vincent 	dma_addr_t	dma_addr;
394508849adSLinus Walleij 	void		*base_unaligned;
395508849adSLinus Walleij 	int		 pages;
3968d318a50SLinus Walleij 	spinlock_t	 lock;
397698e4732SJonas Aaberg 	struct d40_desc	**alloc_map;
3988d318a50SLinus Walleij };
3998d318a50SLinus Walleij 
4008d318a50SLinus Walleij /**
4018d318a50SLinus Walleij  * struct d40_phy_res - struct for handling eventlines mapped to physical
4028d318a50SLinus Walleij  * channels.
4038d318a50SLinus Walleij  *
4048d318a50SLinus Walleij  * @lock: A lock protection this entity.
4057fb3e75eSNarayanan G  * @reserved: True if used by secure world or otherwise.
4068d318a50SLinus Walleij  * @num: The physical channel number of this entity.
4078d318a50SLinus Walleij  * @allocated_src: Bit mapped to show which src event line's are mapped to
4088d318a50SLinus Walleij  * this physical channel. Can also be free or physically allocated.
4098d318a50SLinus Walleij  * @allocated_dst: Same as for src but is dst.
4108d318a50SLinus Walleij  * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
411767a9675SJonas Aaberg  * event line number.
4127407048bSFabio Baltieri  * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
4138d318a50SLinus Walleij  */
4148d318a50SLinus Walleij struct d40_phy_res {
4158d318a50SLinus Walleij 	spinlock_t lock;
4167fb3e75eSNarayanan G 	bool	   reserved;
4178d318a50SLinus Walleij 	int	   num;
4188d318a50SLinus Walleij 	u32	   allocated_src;
4198d318a50SLinus Walleij 	u32	   allocated_dst;
4207407048bSFabio Baltieri 	bool	   use_soft_lli;
4218d318a50SLinus Walleij };
4228d318a50SLinus Walleij 
4238d318a50SLinus Walleij struct d40_base;
4248d318a50SLinus Walleij 
4258d318a50SLinus Walleij /**
4268d318a50SLinus Walleij  * struct d40_chan - Struct that describes a channel.
4278d318a50SLinus Walleij  *
4288d318a50SLinus Walleij  * @lock: A spinlock to protect this struct.
4298d318a50SLinus Walleij  * @log_num: The logical number, if any of this channel.
4308d318a50SLinus Walleij  * @pending_tx: The number of pending transfers. Used between interrupt handler
4318d318a50SLinus Walleij  * and tasklet.
4328d318a50SLinus Walleij  * @busy: Set to true when transfer is ongoing on this channel.
4332a614340SJonas Aaberg  * @phy_chan: Pointer to physical channel which this instance runs on. If this
4342a614340SJonas Aaberg  * point is NULL, then the channel is not allocated.
4358d318a50SLinus Walleij  * @chan: DMA engine handle.
4368d318a50SLinus Walleij  * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
4378d318a50SLinus Walleij  * transfer and call client callback.
4388d318a50SLinus Walleij  * @client: Cliented owned descriptor list.
439da063d26SPer Forlin  * @pending_queue: Submitted jobs, to be issued by issue_pending()
4408d318a50SLinus Walleij  * @active: Active descriptor.
4414226dd86SFabio Baltieri  * @done: Completed jobs
4428d318a50SLinus Walleij  * @queue: Queued jobs.
44382babbb3SPer Forlin  * @prepare_queue: Prepared jobs.
4448d318a50SLinus Walleij  * @dma_cfg: The client configuration of this dma channel.
445ce2ca125SRabin Vincent  * @configured: whether the dma_cfg configuration is valid
4468d318a50SLinus Walleij  * @base: Pointer to the device instance struct.
4478d318a50SLinus Walleij  * @src_def_cfg: Default cfg register setting for src.
4488d318a50SLinus Walleij  * @dst_def_cfg: Default cfg register setting for dst.
4498d318a50SLinus Walleij  * @log_def: Default logical channel settings.
4508d318a50SLinus Walleij  * @lcpa: Pointer to dst and src lcpa settings.
451ae752bf4Som prakash  * @runtime_addr: runtime configured address.
452ae752bf4Som prakash  * @runtime_direction: runtime configured direction.
4538d318a50SLinus Walleij  *
4548d318a50SLinus Walleij  * This struct can either "be" a logical or a physical channel.
4558d318a50SLinus Walleij  */
4568d318a50SLinus Walleij struct d40_chan {
4578d318a50SLinus Walleij 	spinlock_t			 lock;
4588d318a50SLinus Walleij 	int				 log_num;
4598d318a50SLinus Walleij 	int				 pending_tx;
4608d318a50SLinus Walleij 	bool				 busy;
4618d318a50SLinus Walleij 	struct d40_phy_res		*phy_chan;
4628d318a50SLinus Walleij 	struct dma_chan			 chan;
4638d318a50SLinus Walleij 	struct tasklet_struct		 tasklet;
4648d318a50SLinus Walleij 	struct list_head		 client;
465a8f3067bSPer Forlin 	struct list_head		 pending_queue;
4668d318a50SLinus Walleij 	struct list_head		 active;
4674226dd86SFabio Baltieri 	struct list_head		 done;
4688d318a50SLinus Walleij 	struct list_head		 queue;
46982babbb3SPer Forlin 	struct list_head		 prepare_queue;
4708d318a50SLinus Walleij 	struct stedma40_chan_cfg	 dma_cfg;
471ce2ca125SRabin Vincent 	bool				 configured;
4728d318a50SLinus Walleij 	struct d40_base			*base;
4738d318a50SLinus Walleij 	/* Default register configurations */
4748d318a50SLinus Walleij 	u32				 src_def_cfg;
4758d318a50SLinus Walleij 	u32				 dst_def_cfg;
4768d318a50SLinus Walleij 	struct d40_def_lcsp		 log_def;
4778d318a50SLinus Walleij 	struct d40_log_lli_full		*lcpa;
47895e1400fSLinus Walleij 	/* Runtime reconfiguration */
47995e1400fSLinus Walleij 	dma_addr_t			runtime_addr;
480db8196dfSVinod Koul 	enum dma_transfer_direction	runtime_direction;
4818d318a50SLinus Walleij };
4828d318a50SLinus Walleij 
4838d318a50SLinus Walleij /**
4843cb645dcSTong Liu  * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
4853cb645dcSTong Liu  * controller
4863cb645dcSTong Liu  *
4873cb645dcSTong Liu  * @backup: the pointer to the registers address array for backup
4883cb645dcSTong Liu  * @backup_size: the size of the registers address array for backup
4893cb645dcSTong Liu  * @realtime_en: the realtime enable register
4903cb645dcSTong Liu  * @realtime_clear: the realtime clear register
4913cb645dcSTong Liu  * @high_prio_en: the high priority enable register
4923cb645dcSTong Liu  * @high_prio_clear: the high priority clear register
4933cb645dcSTong Liu  * @interrupt_en: the interrupt enable register
4943cb645dcSTong Liu  * @interrupt_clear: the interrupt clear register
4953cb645dcSTong Liu  * @il: the pointer to struct d40_interrupt_lookup
4963cb645dcSTong Liu  * @il_size: the size of d40_interrupt_lookup array
4973cb645dcSTong Liu  * @init_reg: the pointer to the struct d40_reg_val
4983cb645dcSTong Liu  * @init_reg_size: the size of d40_reg_val array
4993cb645dcSTong Liu  */
5003cb645dcSTong Liu struct d40_gen_dmac {
5013cb645dcSTong Liu 	u32				*backup;
5023cb645dcSTong Liu 	u32				 backup_size;
5033cb645dcSTong Liu 	u32				 realtime_en;
5043cb645dcSTong Liu 	u32				 realtime_clear;
5053cb645dcSTong Liu 	u32				 high_prio_en;
5063cb645dcSTong Liu 	u32				 high_prio_clear;
5073cb645dcSTong Liu 	u32				 interrupt_en;
5083cb645dcSTong Liu 	u32				 interrupt_clear;
5093cb645dcSTong Liu 	struct d40_interrupt_lookup	*il;
5103cb645dcSTong Liu 	u32				 il_size;
5113cb645dcSTong Liu 	struct d40_reg_val		*init_reg;
5123cb645dcSTong Liu 	u32				 init_reg_size;
5133cb645dcSTong Liu };
5143cb645dcSTong Liu 
5153cb645dcSTong Liu /**
5168d318a50SLinus Walleij  * struct d40_base - The big global struct, one for each probe'd instance.
5178d318a50SLinus Walleij  *
5188d318a50SLinus Walleij  * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
5198d318a50SLinus Walleij  * @execmd_lock: Lock for execute command usage since several channels share
5208d318a50SLinus Walleij  * the same physical register.
5218d318a50SLinus Walleij  * @dev: The device structure.
5228d318a50SLinus Walleij  * @virtbase: The virtual base address of the DMA's register.
523f4185592SLinus Walleij  * @rev: silicon revision detected.
5248d318a50SLinus Walleij  * @clk: Pointer to the DMA clock structure.
5258d318a50SLinus Walleij  * @phy_start: Physical memory start of the DMA registers.
5268d318a50SLinus Walleij  * @phy_size: Size of the DMA register map.
5278d318a50SLinus Walleij  * @irq: The IRQ number.
528a7dacb68SLee Jones  * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
529a7dacb68SLee Jones  * transfers).
5308d318a50SLinus Walleij  * @num_phy_chans: The number of physical channels. Read from HW. This
5318d318a50SLinus Walleij  * is the number of available channels for this driver, not counting "Secure
5328d318a50SLinus Walleij  * mode" allocated physical channels.
5338d318a50SLinus Walleij  * @num_log_chans: The number of logical channels. Calculated from
5348d318a50SLinus Walleij  * num_phy_chans.
5358d318a50SLinus Walleij  * @dma_both: dma_device channels that can do both memcpy and slave transfers.
5368d318a50SLinus Walleij  * @dma_slave: dma_device channels that can do only do slave transfers.
5378d318a50SLinus Walleij  * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
5387fb3e75eSNarayanan G  * @phy_chans: Room for all possible physical channels in system.
5398d318a50SLinus Walleij  * @log_chans: Room for all possible logical channels in system.
5408d318a50SLinus Walleij  * @lookup_log_chans: Used to map interrupt number to logical channel. Points
5418d318a50SLinus Walleij  * to log_chans entries.
5428d318a50SLinus Walleij  * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
5438d318a50SLinus Walleij  * to phy_chans entries.
5448d318a50SLinus Walleij  * @plat_data: Pointer to provided platform_data which is the driver
5458d318a50SLinus Walleij  * configuration.
54628c7a19dSNarayanan G  * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
5478d318a50SLinus Walleij  * @phy_res: Vector containing all physical channels.
5488d318a50SLinus Walleij  * @lcla_pool: lcla pool settings and data.
5498d318a50SLinus Walleij  * @lcpa_base: The virtual mapped address of LCPA.
5508d318a50SLinus Walleij  * @phy_lcpa: The physical address of the LCPA.
5518d318a50SLinus Walleij  * @lcpa_size: The size of the LCPA area.
552c675b1b4SJonas Aaberg  * @desc_slab: cache for descriptors.
5537fb3e75eSNarayanan G  * @reg_val_backup: Here the values of some hardware registers are stored
5547fb3e75eSNarayanan G  * before the DMA is powered off. They are restored when the power is back on.
5553cb645dcSTong Liu  * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
5563cb645dcSTong Liu  * later
5577fb3e75eSNarayanan G  * @reg_val_backup_chan: Backup data for standard channel parameter registers.
5587fb3e75eSNarayanan G  * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
5593cb645dcSTong Liu  * @gen_dmac: the struct for generic registers values to represent u8500/8540
5603cb645dcSTong Liu  * DMA controller
5618d318a50SLinus Walleij  */
5628d318a50SLinus Walleij struct d40_base {
5638d318a50SLinus Walleij 	spinlock_t			 interrupt_lock;
5648d318a50SLinus Walleij 	spinlock_t			 execmd_lock;
5658d318a50SLinus Walleij 	struct device			 *dev;
5668d318a50SLinus Walleij 	void __iomem			 *virtbase;
567f4185592SLinus Walleij 	u8				  rev:4;
5688d318a50SLinus Walleij 	struct clk			 *clk;
5698d318a50SLinus Walleij 	phys_addr_t			  phy_start;
5708d318a50SLinus Walleij 	resource_size_t			  phy_size;
5718d318a50SLinus Walleij 	int				  irq;
572a7dacb68SLee Jones 	int				  num_memcpy_chans;
5738d318a50SLinus Walleij 	int				  num_phy_chans;
5748d318a50SLinus Walleij 	int				  num_log_chans;
575b96710e5SPer Forlin 	struct device_dma_parameters	  dma_parms;
5768d318a50SLinus Walleij 	struct dma_device		  dma_both;
5778d318a50SLinus Walleij 	struct dma_device		  dma_slave;
5788d318a50SLinus Walleij 	struct dma_device		  dma_memcpy;
5798d318a50SLinus Walleij 	struct d40_chan			 *phy_chans;
5808d318a50SLinus Walleij 	struct d40_chan			 *log_chans;
5818d318a50SLinus Walleij 	struct d40_chan			**lookup_log_chans;
5828d318a50SLinus Walleij 	struct d40_chan			**lookup_phy_chans;
5838d318a50SLinus Walleij 	struct stedma40_platform_data	 *plat_data;
58428c7a19dSNarayanan G 	struct regulator		 *lcpa_regulator;
5858d318a50SLinus Walleij 	/* Physical half channels */
5868d318a50SLinus Walleij 	struct d40_phy_res		 *phy_res;
5878d318a50SLinus Walleij 	struct d40_lcla_pool		  lcla_pool;
5888d318a50SLinus Walleij 	void				 *lcpa_base;
5898d318a50SLinus Walleij 	dma_addr_t			  phy_lcpa;
5908d318a50SLinus Walleij 	resource_size_t			  lcpa_size;
591c675b1b4SJonas Aaberg 	struct kmem_cache		 *desc_slab;
5927fb3e75eSNarayanan G 	u32				  reg_val_backup[BACKUP_REGS_SZ];
59384b3da14SLee Jones 	u32				  reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
5947fb3e75eSNarayanan G 	u32				 *reg_val_backup_chan;
5957fb3e75eSNarayanan G 	u16				  gcc_pwr_off_mask;
5963cb645dcSTong Liu 	struct d40_gen_dmac		  gen_dmac;
5978d318a50SLinus Walleij };
5988d318a50SLinus Walleij 
599262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c)
600262d2915SRabin Vincent {
601262d2915SRabin Vincent 	return &d40c->chan.dev->device;
602262d2915SRabin Vincent }
603262d2915SRabin Vincent 
604724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan)
605724a8577SRabin Vincent {
606724a8577SRabin Vincent 	return chan->log_num == D40_PHY_CHAN;
607724a8577SRabin Vincent }
608724a8577SRabin Vincent 
609724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan)
610724a8577SRabin Vincent {
611724a8577SRabin Vincent 	return !chan_is_physical(chan);
612724a8577SRabin Vincent }
613724a8577SRabin Vincent 
6148ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan)
6158ca84687SRabin Vincent {
6168ca84687SRabin Vincent 	return chan->base->virtbase + D40_DREG_PCBASE +
6178ca84687SRabin Vincent 	       chan->phy_chan->num * D40_DREG_PCDELTA;
6188ca84687SRabin Vincent }
6198ca84687SRabin Vincent 
6206db5a8baSRabin Vincent #define d40_err(dev, format, arg...)		\
6216db5a8baSRabin Vincent 	dev_err(dev, "[%s] " format, __func__, ## arg)
6226db5a8baSRabin Vincent 
6236db5a8baSRabin Vincent #define chan_err(d40c, format, arg...)		\
6246db5a8baSRabin Vincent 	d40_err(chan2dev(d40c), format, ## arg)
6256db5a8baSRabin Vincent 
626b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
627dbd88788SRabin Vincent 			      int lli_len)
6288d318a50SLinus Walleij {
629dbd88788SRabin Vincent 	bool is_log = chan_is_logical(d40c);
6308d318a50SLinus Walleij 	u32 align;
6318d318a50SLinus Walleij 	void *base;
6328d318a50SLinus Walleij 
6338d318a50SLinus Walleij 	if (is_log)
6348d318a50SLinus Walleij 		align = sizeof(struct d40_log_lli);
6358d318a50SLinus Walleij 	else
6368d318a50SLinus Walleij 		align = sizeof(struct d40_phy_lli);
6378d318a50SLinus Walleij 
6388d318a50SLinus Walleij 	if (lli_len == 1) {
6398d318a50SLinus Walleij 		base = d40d->lli_pool.pre_alloc_lli;
6408d318a50SLinus Walleij 		d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
6418d318a50SLinus Walleij 		d40d->lli_pool.base = NULL;
6428d318a50SLinus Walleij 	} else {
643594ece4dSRabin Vincent 		d40d->lli_pool.size = lli_len * 2 * align;
6448d318a50SLinus Walleij 
6458d318a50SLinus Walleij 		base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
6468d318a50SLinus Walleij 		d40d->lli_pool.base = base;
6478d318a50SLinus Walleij 
6488d318a50SLinus Walleij 		if (d40d->lli_pool.base == NULL)
6498d318a50SLinus Walleij 			return -ENOMEM;
6508d318a50SLinus Walleij 	}
6518d318a50SLinus Walleij 
6528d318a50SLinus Walleij 	if (is_log) {
653d924abadSRabin Vincent 		d40d->lli_log.src = PTR_ALIGN(base, align);
654594ece4dSRabin Vincent 		d40d->lli_log.dst = d40d->lli_log.src + lli_len;
655b00f938cSRabin Vincent 
656b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = 0;
6578d318a50SLinus Walleij 	} else {
658d924abadSRabin Vincent 		d40d->lli_phy.src = PTR_ALIGN(base, align);
659594ece4dSRabin Vincent 		d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
660b00f938cSRabin Vincent 
661b00f938cSRabin Vincent 		d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
662b00f938cSRabin Vincent 							 d40d->lli_phy.src,
663b00f938cSRabin Vincent 							 d40d->lli_pool.size,
664b00f938cSRabin Vincent 							 DMA_TO_DEVICE);
665b00f938cSRabin Vincent 
666b00f938cSRabin Vincent 		if (dma_mapping_error(d40c->base->dev,
667b00f938cSRabin Vincent 				      d40d->lli_pool.dma_addr)) {
668b00f938cSRabin Vincent 			kfree(d40d->lli_pool.base);
669b00f938cSRabin Vincent 			d40d->lli_pool.base = NULL;
670b00f938cSRabin Vincent 			d40d->lli_pool.dma_addr = 0;
671b00f938cSRabin Vincent 			return -ENOMEM;
672b00f938cSRabin Vincent 		}
6738d318a50SLinus Walleij 	}
6748d318a50SLinus Walleij 
6758d318a50SLinus Walleij 	return 0;
6768d318a50SLinus Walleij }
6778d318a50SLinus Walleij 
678b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
6798d318a50SLinus Walleij {
680b00f938cSRabin Vincent 	if (d40d->lli_pool.dma_addr)
681b00f938cSRabin Vincent 		dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
682b00f938cSRabin Vincent 				 d40d->lli_pool.size, DMA_TO_DEVICE);
683b00f938cSRabin Vincent 
6848d318a50SLinus Walleij 	kfree(d40d->lli_pool.base);
6858d318a50SLinus Walleij 	d40d->lli_pool.base = NULL;
6868d318a50SLinus Walleij 	d40d->lli_pool.size = 0;
6878d318a50SLinus Walleij 	d40d->lli_log.src = NULL;
6888d318a50SLinus Walleij 	d40d->lli_log.dst = NULL;
6898d318a50SLinus Walleij 	d40d->lli_phy.src = NULL;
6908d318a50SLinus Walleij 	d40d->lli_phy.dst = NULL;
6918d318a50SLinus Walleij }
6928d318a50SLinus Walleij 
693698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c,
694698e4732SJonas Aaberg 			      struct d40_desc *d40d)
695698e4732SJonas Aaberg {
696698e4732SJonas Aaberg 	unsigned long flags;
697698e4732SJonas Aaberg 	int i;
698698e4732SJonas Aaberg 	int ret = -EINVAL;
699698e4732SJonas Aaberg 
700698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
701698e4732SJonas Aaberg 
702698e4732SJonas Aaberg 	/*
703698e4732SJonas Aaberg 	 * Allocate both src and dst at the same time, therefore the half
704698e4732SJonas Aaberg 	 * start on 1 since 0 can't be used since zero is used as end marker.
705698e4732SJonas Aaberg 	 */
706698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7077ce529efSFabio Baltieri 		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7087ce529efSFabio Baltieri 
7097ce529efSFabio Baltieri 		if (!d40c->base->lcla_pool.alloc_map[idx]) {
7107ce529efSFabio Baltieri 			d40c->base->lcla_pool.alloc_map[idx] = d40d;
711698e4732SJonas Aaberg 			d40d->lcla_alloc++;
712698e4732SJonas Aaberg 			ret = i;
713698e4732SJonas Aaberg 			break;
714698e4732SJonas Aaberg 		}
715698e4732SJonas Aaberg 	}
716698e4732SJonas Aaberg 
717698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
718698e4732SJonas Aaberg 
719698e4732SJonas Aaberg 	return ret;
720698e4732SJonas Aaberg }
721698e4732SJonas Aaberg 
722698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c,
723698e4732SJonas Aaberg 			     struct d40_desc *d40d)
724698e4732SJonas Aaberg {
725698e4732SJonas Aaberg 	unsigned long flags;
726698e4732SJonas Aaberg 	int i;
727698e4732SJonas Aaberg 	int ret = -EINVAL;
728698e4732SJonas Aaberg 
729724a8577SRabin Vincent 	if (chan_is_physical(d40c))
730698e4732SJonas Aaberg 		return 0;
731698e4732SJonas Aaberg 
732698e4732SJonas Aaberg 	spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
733698e4732SJonas Aaberg 
734698e4732SJonas Aaberg 	for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7357ce529efSFabio Baltieri 		int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7367ce529efSFabio Baltieri 
7377ce529efSFabio Baltieri 		if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
7387ce529efSFabio Baltieri 			d40c->base->lcla_pool.alloc_map[idx] = NULL;
739698e4732SJonas Aaberg 			d40d->lcla_alloc--;
740698e4732SJonas Aaberg 			if (d40d->lcla_alloc == 0) {
741698e4732SJonas Aaberg 				ret = 0;
742698e4732SJonas Aaberg 				break;
743698e4732SJonas Aaberg 			}
744698e4732SJonas Aaberg 		}
745698e4732SJonas Aaberg 	}
746698e4732SJonas Aaberg 
747698e4732SJonas Aaberg 	spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
748698e4732SJonas Aaberg 
749698e4732SJonas Aaberg 	return ret;
750698e4732SJonas Aaberg 
751698e4732SJonas Aaberg }
752698e4732SJonas Aaberg 
7538d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d)
7548d318a50SLinus Walleij {
7558d318a50SLinus Walleij 	list_del(&d40d->node);
7568d318a50SLinus Walleij }
7578d318a50SLinus Walleij 
7588d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
7598d318a50SLinus Walleij {
760a2c15fa4SRabin Vincent 	struct d40_desc *desc = NULL;
761a2c15fa4SRabin Vincent 
762a2c15fa4SRabin Vincent 	if (!list_empty(&d40c->client)) {
7638d318a50SLinus Walleij 		struct d40_desc *d;
7648d318a50SLinus Walleij 		struct d40_desc *_d;
7658d318a50SLinus Walleij 
7667fb3e75eSNarayanan G 		list_for_each_entry_safe(d, _d, &d40c->client, node) {
7678d318a50SLinus Walleij 			if (async_tx_test_ack(&d->txd)) {
7688d318a50SLinus Walleij 				d40_desc_remove(d);
769a2c15fa4SRabin Vincent 				desc = d;
770a2c15fa4SRabin Vincent 				memset(desc, 0, sizeof(*desc));
771c675b1b4SJonas Aaberg 				break;
7728d318a50SLinus Walleij 			}
7738d318a50SLinus Walleij 		}
7747fb3e75eSNarayanan G 	}
775a2c15fa4SRabin Vincent 
776a2c15fa4SRabin Vincent 	if (!desc)
777a2c15fa4SRabin Vincent 		desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
778a2c15fa4SRabin Vincent 
779a2c15fa4SRabin Vincent 	if (desc)
780a2c15fa4SRabin Vincent 		INIT_LIST_HEAD(&desc->node);
781a2c15fa4SRabin Vincent 
782a2c15fa4SRabin Vincent 	return desc;
7838d318a50SLinus Walleij }
7848d318a50SLinus Walleij 
7858d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
7868d318a50SLinus Walleij {
787698e4732SJonas Aaberg 
788b00f938cSRabin Vincent 	d40_pool_lli_free(d40c, d40d);
789698e4732SJonas Aaberg 	d40_lcla_free_all(d40c, d40d);
790c675b1b4SJonas Aaberg 	kmem_cache_free(d40c->base->desc_slab, d40d);
7918d318a50SLinus Walleij }
7928d318a50SLinus Walleij 
7938d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
7948d318a50SLinus Walleij {
7958d318a50SLinus Walleij 	list_add_tail(&desc->node, &d40c->active);
7968d318a50SLinus Walleij }
7978d318a50SLinus Walleij 
7981c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
7991c4b0927SRabin Vincent {
8001c4b0927SRabin Vincent 	struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
8011c4b0927SRabin Vincent 	struct d40_phy_lli *lli_src = desc->lli_phy.src;
8021c4b0927SRabin Vincent 	void __iomem *base = chan_base(chan);
8031c4b0927SRabin Vincent 
8041c4b0927SRabin Vincent 	writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
8051c4b0927SRabin Vincent 	writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
8061c4b0927SRabin Vincent 	writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
8071c4b0927SRabin Vincent 	writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
8081c4b0927SRabin Vincent 
8091c4b0927SRabin Vincent 	writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
8101c4b0927SRabin Vincent 	writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
8111c4b0927SRabin Vincent 	writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
8121c4b0927SRabin Vincent 	writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
8131c4b0927SRabin Vincent }
8141c4b0927SRabin Vincent 
8154226dd86SFabio Baltieri static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
8164226dd86SFabio Baltieri {
8174226dd86SFabio Baltieri 	list_add_tail(&desc->node, &d40c->done);
8184226dd86SFabio Baltieri }
8194226dd86SFabio Baltieri 
820e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
821698e4732SJonas Aaberg {
822e65889c7SRabin Vincent 	struct d40_lcla_pool *pool = &chan->base->lcla_pool;
823e65889c7SRabin Vincent 	struct d40_log_lli_bidir *lli = &desc->lli_log;
824e65889c7SRabin Vincent 	int lli_current = desc->lli_current;
825e65889c7SRabin Vincent 	int lli_len = desc->lli_len;
8260c842b55SRabin Vincent 	bool cyclic = desc->cyclic;
827e65889c7SRabin Vincent 	int curr_lcla = -EINVAL;
8280c842b55SRabin Vincent 	int first_lcla = 0;
82928c7a19dSNarayanan G 	bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
8300c842b55SRabin Vincent 	bool linkback;
831698e4732SJonas Aaberg 
8320c842b55SRabin Vincent 	/*
8330c842b55SRabin Vincent 	 * We may have partially running cyclic transfers, in case we did't get
8340c842b55SRabin Vincent 	 * enough LCLA entries.
8350c842b55SRabin Vincent 	 */
8360c842b55SRabin Vincent 	linkback = cyclic && lli_current == 0;
8370c842b55SRabin Vincent 
8380c842b55SRabin Vincent 	/*
8390c842b55SRabin Vincent 	 * For linkback, we need one LCLA even with only one link, because we
8400c842b55SRabin Vincent 	 * can't link back to the one in LCPA space
8410c842b55SRabin Vincent 	 */
8420c842b55SRabin Vincent 	if (linkback || (lli_len - lli_current > 1)) {
8437407048bSFabio Baltieri 		/*
8447407048bSFabio Baltieri 		 * If the channel is expected to use only soft_lli don't
8457407048bSFabio Baltieri 		 * allocate a lcla. This is to avoid a HW issue that exists
8467407048bSFabio Baltieri 		 * in some controller during a peripheral to memory transfer
8477407048bSFabio Baltieri 		 * that uses linked lists.
8487407048bSFabio Baltieri 		 */
8497407048bSFabio Baltieri 		if (!(chan->phy_chan->use_soft_lli &&
8502c2b62d5SLee Jones 			chan->dma_cfg.dir == DMA_DEV_TO_MEM))
851e65889c7SRabin Vincent 			curr_lcla = d40_lcla_alloc_one(chan, desc);
8527407048bSFabio Baltieri 
8530c842b55SRabin Vincent 		first_lcla = curr_lcla;
8540c842b55SRabin Vincent 	}
8550c842b55SRabin Vincent 
8560c842b55SRabin Vincent 	/*
8570c842b55SRabin Vincent 	 * For linkback, we normally load the LCPA in the loop since we need to
8580c842b55SRabin Vincent 	 * link it to the second LCLA and not the first.  However, if we
8590c842b55SRabin Vincent 	 * couldn't even get a first LCLA, then we have to run in LCPA and
8600c842b55SRabin Vincent 	 * reload manually.
8610c842b55SRabin Vincent 	 */
8620c842b55SRabin Vincent 	if (!linkback || curr_lcla == -EINVAL) {
8630c842b55SRabin Vincent 		unsigned int flags = 0;
8640c842b55SRabin Vincent 
8650c842b55SRabin Vincent 		if (curr_lcla == -EINVAL)
8660c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
867698e4732SJonas Aaberg 
868e65889c7SRabin Vincent 		d40_log_lli_lcpa_write(chan->lcpa,
869e65889c7SRabin Vincent 				       &lli->dst[lli_current],
870e65889c7SRabin Vincent 				       &lli->src[lli_current],
8710c842b55SRabin Vincent 				       curr_lcla,
8720c842b55SRabin Vincent 				       flags);
873e65889c7SRabin Vincent 		lli_current++;
8740c842b55SRabin Vincent 	}
8756045f0bbSRabin Vincent 
8766045f0bbSRabin Vincent 	if (curr_lcla < 0)
8776045f0bbSRabin Vincent 		goto out;
8786045f0bbSRabin Vincent 
879e65889c7SRabin Vincent 	for (; lli_current < lli_len; lli_current++) {
880e65889c7SRabin Vincent 		unsigned int lcla_offset = chan->phy_chan->num * 1024 +
881026cbc42SRabin Vincent 					   8 * curr_lcla * 2;
882026cbc42SRabin Vincent 		struct d40_log_lli *lcla = pool->base + lcla_offset;
8830c842b55SRabin Vincent 		unsigned int flags = 0;
884e65889c7SRabin Vincent 		int next_lcla;
885698e4732SJonas Aaberg 
886e65889c7SRabin Vincent 		if (lli_current + 1 < lli_len)
887e65889c7SRabin Vincent 			next_lcla = d40_lcla_alloc_one(chan, desc);
888698e4732SJonas Aaberg 		else
8890c842b55SRabin Vincent 			next_lcla = linkback ? first_lcla : -EINVAL;
890698e4732SJonas Aaberg 
8910c842b55SRabin Vincent 		if (cyclic || next_lcla == -EINVAL)
8920c842b55SRabin Vincent 			flags |= LLI_TERM_INT;
8930c842b55SRabin Vincent 
8940c842b55SRabin Vincent 		if (linkback && curr_lcla == first_lcla) {
8950c842b55SRabin Vincent 			/* First link goes in both LCPA and LCLA */
8960c842b55SRabin Vincent 			d40_log_lli_lcpa_write(chan->lcpa,
8970c842b55SRabin Vincent 					       &lli->dst[lli_current],
8980c842b55SRabin Vincent 					       &lli->src[lli_current],
8990c842b55SRabin Vincent 					       next_lcla, flags);
9000c842b55SRabin Vincent 		}
9010c842b55SRabin Vincent 
9020c842b55SRabin Vincent 		/*
9030c842b55SRabin Vincent 		 * One unused LCLA in the cyclic case if the very first
9040c842b55SRabin Vincent 		 * next_lcla fails...
9050c842b55SRabin Vincent 		 */
906698e4732SJonas Aaberg 		d40_log_lli_lcla_write(lcla,
907e65889c7SRabin Vincent 				       &lli->dst[lli_current],
908e65889c7SRabin Vincent 				       &lli->src[lli_current],
9090c842b55SRabin Vincent 				       next_lcla, flags);
910698e4732SJonas Aaberg 
91128c7a19dSNarayanan G 		/*
91228c7a19dSNarayanan G 		 * Cache maintenance is not needed if lcla is
91328c7a19dSNarayanan G 		 * mapped in esram
91428c7a19dSNarayanan G 		 */
91528c7a19dSNarayanan G 		if (!use_esram_lcla) {
916e65889c7SRabin Vincent 			dma_sync_single_range_for_device(chan->base->dev,
917026cbc42SRabin Vincent 						pool->dma_addr, lcla_offset,
918698e4732SJonas Aaberg 						2 * sizeof(struct d40_log_lli),
919698e4732SJonas Aaberg 						DMA_TO_DEVICE);
92028c7a19dSNarayanan G 		}
921698e4732SJonas Aaberg 		curr_lcla = next_lcla;
922698e4732SJonas Aaberg 
9230c842b55SRabin Vincent 		if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
924e65889c7SRabin Vincent 			lli_current++;
925698e4732SJonas Aaberg 			break;
926698e4732SJonas Aaberg 		}
927e65889c7SRabin Vincent 	}
928698e4732SJonas Aaberg 
9296045f0bbSRabin Vincent out:
930e65889c7SRabin Vincent 	desc->lli_current = lli_current;
931698e4732SJonas Aaberg }
932e65889c7SRabin Vincent 
933e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
934e65889c7SRabin Vincent {
935e65889c7SRabin Vincent 	if (chan_is_physical(d40c)) {
936e65889c7SRabin Vincent 		d40_phy_lli_load(d40c, d40d);
937e65889c7SRabin Vincent 		d40d->lli_current = d40d->lli_len;
938e65889c7SRabin Vincent 	} else
939e65889c7SRabin Vincent 		d40_log_lli_to_lcxa(d40c, d40d);
940698e4732SJonas Aaberg }
941698e4732SJonas Aaberg 
9428d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
9438d318a50SLinus Walleij {
9448d318a50SLinus Walleij 	struct d40_desc *d;
9458d318a50SLinus Walleij 
9468d318a50SLinus Walleij 	if (list_empty(&d40c->active))
9478d318a50SLinus Walleij 		return NULL;
9488d318a50SLinus Walleij 
9498d318a50SLinus Walleij 	d = list_first_entry(&d40c->active,
9508d318a50SLinus Walleij 			     struct d40_desc,
9518d318a50SLinus Walleij 			     node);
9528d318a50SLinus Walleij 	return d;
9538d318a50SLinus Walleij }
9548d318a50SLinus Walleij 
9557404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */
9568d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
9578d318a50SLinus Walleij {
9587404368cSPer Forlin 	d40_desc_remove(desc);
9597404368cSPer Forlin 	desc->is_in_client_list = false;
960a8f3067bSPer Forlin 	list_add_tail(&desc->node, &d40c->pending_queue);
961a8f3067bSPer Forlin }
962a8f3067bSPer Forlin 
963a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
964a8f3067bSPer Forlin {
965a8f3067bSPer Forlin 	struct d40_desc *d;
966a8f3067bSPer Forlin 
967a8f3067bSPer Forlin 	if (list_empty(&d40c->pending_queue))
968a8f3067bSPer Forlin 		return NULL;
969a8f3067bSPer Forlin 
970a8f3067bSPer Forlin 	d = list_first_entry(&d40c->pending_queue,
971a8f3067bSPer Forlin 			     struct d40_desc,
972a8f3067bSPer Forlin 			     node);
973a8f3067bSPer Forlin 	return d;
9748d318a50SLinus Walleij }
9758d318a50SLinus Walleij 
9768d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
9778d318a50SLinus Walleij {
9788d318a50SLinus Walleij 	struct d40_desc *d;
9798d318a50SLinus Walleij 
9808d318a50SLinus Walleij 	if (list_empty(&d40c->queue))
9818d318a50SLinus Walleij 		return NULL;
9828d318a50SLinus Walleij 
9838d318a50SLinus Walleij 	d = list_first_entry(&d40c->queue,
9848d318a50SLinus Walleij 			     struct d40_desc,
9858d318a50SLinus Walleij 			     node);
9868d318a50SLinus Walleij 	return d;
9878d318a50SLinus Walleij }
9888d318a50SLinus Walleij 
9894226dd86SFabio Baltieri static struct d40_desc *d40_first_done(struct d40_chan *d40c)
9904226dd86SFabio Baltieri {
9914226dd86SFabio Baltieri 	if (list_empty(&d40c->done))
9924226dd86SFabio Baltieri 		return NULL;
9934226dd86SFabio Baltieri 
9944226dd86SFabio Baltieri 	return list_first_entry(&d40c->done, struct d40_desc, node);
9954226dd86SFabio Baltieri }
9964226dd86SFabio Baltieri 
997d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize)
998d49278e3SPer Forlin {
999d49278e3SPer Forlin 	if (is_log) {
1000d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_LOG_1)
1001d49278e3SPer Forlin 			return 1;
1002d49278e3SPer Forlin 	} else {
1003d49278e3SPer Forlin 		if (psize == STEDMA40_PSIZE_PHY_1)
1004d49278e3SPer Forlin 			return 1;
1005d49278e3SPer Forlin 	}
10068d318a50SLinus Walleij 
1007d49278e3SPer Forlin 	return 2 << psize;
1008d49278e3SPer Forlin }
1009d49278e3SPer Forlin 
1010d49278e3SPer Forlin /*
1011d49278e3SPer Forlin  * The dma only supports transmitting packages up to
101243f2e1a3SLee Jones  * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
101343f2e1a3SLee Jones  *
101443f2e1a3SLee Jones  * Calculate the total number of dma elements required to send the entire sg list.
1015d49278e3SPer Forlin  */
1016d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1017d49278e3SPer Forlin {
1018d49278e3SPer Forlin 	int dmalen;
1019d49278e3SPer Forlin 	u32 max_w = max(data_width1, data_width2);
1020d49278e3SPer Forlin 	u32 min_w = min(data_width1, data_width2);
102143f2e1a3SLee Jones 	u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1022d49278e3SPer Forlin 
1023d49278e3SPer Forlin 	if (seg_max > STEDMA40_MAX_SEG_SIZE)
102443f2e1a3SLee Jones 		seg_max -= max_w;
1025d49278e3SPer Forlin 
102643f2e1a3SLee Jones 	if (!IS_ALIGNED(size, max_w))
1027d49278e3SPer Forlin 		return -EINVAL;
1028d49278e3SPer Forlin 
1029d49278e3SPer Forlin 	if (size <= seg_max)
1030d49278e3SPer Forlin 		dmalen = 1;
1031d49278e3SPer Forlin 	else {
1032d49278e3SPer Forlin 		dmalen = size / seg_max;
1033d49278e3SPer Forlin 		if (dmalen * seg_max < size)
1034d49278e3SPer Forlin 			dmalen++;
1035d49278e3SPer Forlin 	}
1036d49278e3SPer Forlin 	return dmalen;
1037d49278e3SPer Forlin }
1038d49278e3SPer Forlin 
1039d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1040d49278e3SPer Forlin 			   u32 data_width1, u32 data_width2)
1041d49278e3SPer Forlin {
1042d49278e3SPer Forlin 	struct scatterlist *sg;
1043d49278e3SPer Forlin 	int i;
1044d49278e3SPer Forlin 	int len = 0;
1045d49278e3SPer Forlin 	int ret;
1046d49278e3SPer Forlin 
1047d49278e3SPer Forlin 	for_each_sg(sgl, sg, sg_len, i) {
1048d49278e3SPer Forlin 		ret = d40_size_2_dmalen(sg_dma_len(sg),
1049d49278e3SPer Forlin 					data_width1, data_width2);
1050d49278e3SPer Forlin 		if (ret < 0)
1051d49278e3SPer Forlin 			return ret;
1052d49278e3SPer Forlin 		len += ret;
1053d49278e3SPer Forlin 	}
1054d49278e3SPer Forlin 	return len;
1055d49278e3SPer Forlin }
1056d49278e3SPer Forlin 
10571bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c,
10588d318a50SLinus Walleij 				     enum d40_command command)
10598d318a50SLinus Walleij {
1060767a9675SJonas Aaberg 	u32 status;
1061767a9675SJonas Aaberg 	int i;
10628d318a50SLinus Walleij 	void __iomem *active_reg;
10638d318a50SLinus Walleij 	int ret = 0;
10648d318a50SLinus Walleij 	unsigned long flags;
10651d392a7bSJonas Aaberg 	u32 wmask;
10668d318a50SLinus Walleij 
10671bdae6f4SNarayanan G 	if (command == D40_DMA_STOP) {
10681bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
10691bdae6f4SNarayanan G 		if (ret)
10701bdae6f4SNarayanan G 			return ret;
10711bdae6f4SNarayanan G 	}
10721bdae6f4SNarayanan G 
10738d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->base->execmd_lock, flags);
10748d318a50SLinus Walleij 
10758d318a50SLinus Walleij 	if (d40c->phy_chan->num % 2 == 0)
10768d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
10778d318a50SLinus Walleij 	else
10788d318a50SLinus Walleij 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
10798d318a50SLinus Walleij 
10808d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
10818d318a50SLinus Walleij 		status = (readl(active_reg) &
10828d318a50SLinus Walleij 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
10838d318a50SLinus Walleij 			D40_CHAN_POS(d40c->phy_chan->num);
10848d318a50SLinus Walleij 
10858d318a50SLinus Walleij 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
10868d318a50SLinus Walleij 			goto done;
10878d318a50SLinus Walleij 	}
10888d318a50SLinus Walleij 
10891d392a7bSJonas Aaberg 	wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
10901d392a7bSJonas Aaberg 	writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
10911d392a7bSJonas Aaberg 	       active_reg);
10928d318a50SLinus Walleij 
10938d318a50SLinus Walleij 	if (command == D40_DMA_SUSPEND_REQ) {
10948d318a50SLinus Walleij 
10958d318a50SLinus Walleij 		for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
10968d318a50SLinus Walleij 			status = (readl(active_reg) &
10978d318a50SLinus Walleij 				  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
10988d318a50SLinus Walleij 				D40_CHAN_POS(d40c->phy_chan->num);
10998d318a50SLinus Walleij 
11008d318a50SLinus Walleij 			cpu_relax();
11018d318a50SLinus Walleij 			/*
11028d318a50SLinus Walleij 			 * Reduce the number of bus accesses while
11038d318a50SLinus Walleij 			 * waiting for the DMA to suspend.
11048d318a50SLinus Walleij 			 */
11058d318a50SLinus Walleij 			udelay(3);
11068d318a50SLinus Walleij 
11078d318a50SLinus Walleij 			if (status == D40_DMA_STOP ||
11088d318a50SLinus Walleij 			    status == D40_DMA_SUSPENDED)
11098d318a50SLinus Walleij 				break;
11108d318a50SLinus Walleij 		}
11118d318a50SLinus Walleij 
11128d318a50SLinus Walleij 		if (i == D40_SUSPEND_MAX_IT) {
11136db5a8baSRabin Vincent 			chan_err(d40c,
11146db5a8baSRabin Vincent 				"unable to suspend the chl %d (log: %d) status %x\n",
11156db5a8baSRabin Vincent 				d40c->phy_chan->num, d40c->log_num,
11168d318a50SLinus Walleij 				status);
11178d318a50SLinus Walleij 			dump_stack();
11188d318a50SLinus Walleij 			ret = -EBUSY;
11198d318a50SLinus Walleij 		}
11208d318a50SLinus Walleij 
11218d318a50SLinus Walleij 	}
11228d318a50SLinus Walleij done:
11238d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
11248d318a50SLinus Walleij 	return ret;
11258d318a50SLinus Walleij }
11268d318a50SLinus Walleij 
11278d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c)
11288d318a50SLinus Walleij {
11298d318a50SLinus Walleij 	struct d40_desc *d40d;
11307404368cSPer Forlin 	struct d40_desc *_d;
11318d318a50SLinus Walleij 
11324226dd86SFabio Baltieri 	/* Release completed descriptors */
11334226dd86SFabio Baltieri 	while ((d40d = d40_first_done(d40c))) {
11344226dd86SFabio Baltieri 		d40_desc_remove(d40d);
11354226dd86SFabio Baltieri 		d40_desc_free(d40c, d40d);
11364226dd86SFabio Baltieri 	}
11374226dd86SFabio Baltieri 
11388d318a50SLinus Walleij 	/* Release active descriptors */
11398d318a50SLinus Walleij 	while ((d40d = d40_first_active_get(d40c))) {
11408d318a50SLinus Walleij 		d40_desc_remove(d40d);
11418d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
11428d318a50SLinus Walleij 	}
11438d318a50SLinus Walleij 
11448d318a50SLinus Walleij 	/* Release queued descriptors waiting for transfer */
11458d318a50SLinus Walleij 	while ((d40d = d40_first_queued(d40c))) {
11468d318a50SLinus Walleij 		d40_desc_remove(d40d);
11478d318a50SLinus Walleij 		d40_desc_free(d40c, d40d);
11488d318a50SLinus Walleij 	}
11498d318a50SLinus Walleij 
1150a8f3067bSPer Forlin 	/* Release pending descriptors */
1151a8f3067bSPer Forlin 	while ((d40d = d40_first_pending(d40c))) {
1152a8f3067bSPer Forlin 		d40_desc_remove(d40d);
1153a8f3067bSPer Forlin 		d40_desc_free(d40c, d40d);
1154a8f3067bSPer Forlin 	}
11558d318a50SLinus Walleij 
11567404368cSPer Forlin 	/* Release client owned descriptors */
11577404368cSPer Forlin 	if (!list_empty(&d40c->client))
11587404368cSPer Forlin 		list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
11597404368cSPer Forlin 			d40_desc_remove(d40d);
11607404368cSPer Forlin 			d40_desc_free(d40c, d40d);
11617404368cSPer Forlin 		}
11627404368cSPer Forlin 
116382babbb3SPer Forlin 	/* Release descriptors in prepare queue */
116482babbb3SPer Forlin 	if (!list_empty(&d40c->prepare_queue))
116582babbb3SPer Forlin 		list_for_each_entry_safe(d40d, _d,
116682babbb3SPer Forlin 					 &d40c->prepare_queue, node) {
116782babbb3SPer Forlin 			d40_desc_remove(d40d);
116882babbb3SPer Forlin 			d40_desc_free(d40c, d40d);
116982babbb3SPer Forlin 		}
11707404368cSPer Forlin 
11718d318a50SLinus Walleij 	d40c->pending_tx = 0;
11728d318a50SLinus Walleij }
11738d318a50SLinus Walleij 
11741bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c,
11751bdae6f4SNarayanan G 				   enum d40_events event_type, u32 event,
11761bdae6f4SNarayanan G 				   int reg)
1177262d2915SRabin Vincent {
11788ca84687SRabin Vincent 	void __iomem *addr = chan_base(d40c) + reg;
1179262d2915SRabin Vincent 	int tries;
11801bdae6f4SNarayanan G 	u32 status;
1181262d2915SRabin Vincent 
11821bdae6f4SNarayanan G 	switch (event_type) {
11831bdae6f4SNarayanan G 
11841bdae6f4SNarayanan G 	case D40_DEACTIVATE_EVENTLINE:
11851bdae6f4SNarayanan G 
1186262d2915SRabin Vincent 		writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1187262d2915SRabin Vincent 		       | ~D40_EVENTLINE_MASK(event), addr);
11881bdae6f4SNarayanan G 		break;
11891bdae6f4SNarayanan G 
11901bdae6f4SNarayanan G 	case D40_SUSPEND_REQ_EVENTLINE:
11911bdae6f4SNarayanan G 		status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
11921bdae6f4SNarayanan G 			  D40_EVENTLINE_POS(event);
11931bdae6f4SNarayanan G 
11941bdae6f4SNarayanan G 		if (status == D40_DEACTIVATE_EVENTLINE ||
11951bdae6f4SNarayanan G 		    status == D40_SUSPEND_REQ_EVENTLINE)
11961bdae6f4SNarayanan G 			break;
11971bdae6f4SNarayanan G 
11981bdae6f4SNarayanan G 		writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
11991bdae6f4SNarayanan G 		       | ~D40_EVENTLINE_MASK(event), addr);
12001bdae6f4SNarayanan G 
12011bdae6f4SNarayanan G 		for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
12021bdae6f4SNarayanan G 
12031bdae6f4SNarayanan G 			status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
12041bdae6f4SNarayanan G 				  D40_EVENTLINE_POS(event);
12051bdae6f4SNarayanan G 
12061bdae6f4SNarayanan G 			cpu_relax();
12071bdae6f4SNarayanan G 			/*
12081bdae6f4SNarayanan G 			 * Reduce the number of bus accesses while
12091bdae6f4SNarayanan G 			 * waiting for the DMA to suspend.
12101bdae6f4SNarayanan G 			 */
12111bdae6f4SNarayanan G 			udelay(3);
12121bdae6f4SNarayanan G 
12131bdae6f4SNarayanan G 			if (status == D40_DEACTIVATE_EVENTLINE)
12141bdae6f4SNarayanan G 				break;
1215262d2915SRabin Vincent 		}
1216262d2915SRabin Vincent 
12171bdae6f4SNarayanan G 		if (tries == D40_SUSPEND_MAX_IT) {
12181bdae6f4SNarayanan G 			chan_err(d40c,
12191bdae6f4SNarayanan G 				"unable to stop the event_line chl %d (log: %d)"
12201bdae6f4SNarayanan G 				"status %x\n", d40c->phy_chan->num,
12211bdae6f4SNarayanan G 				 d40c->log_num, status);
12221bdae6f4SNarayanan G 		}
12231bdae6f4SNarayanan G 		break;
12241bdae6f4SNarayanan G 
12251bdae6f4SNarayanan G 	case D40_ACTIVATE_EVENTLINE:
1226262d2915SRabin Vincent 	/*
1227262d2915SRabin Vincent 	 * The hardware sometimes doesn't register the enable when src and dst
1228262d2915SRabin Vincent 	 * event lines are active on the same logical channel.  Retry to ensure
1229262d2915SRabin Vincent 	 * it does.  Usually only one retry is sufficient.
1230262d2915SRabin Vincent 	 */
1231262d2915SRabin Vincent 		tries = 100;
1232262d2915SRabin Vincent 		while (--tries) {
12331bdae6f4SNarayanan G 			writel((D40_ACTIVATE_EVENTLINE <<
12341bdae6f4SNarayanan G 				D40_EVENTLINE_POS(event)) |
12351bdae6f4SNarayanan G 				~D40_EVENTLINE_MASK(event), addr);
1236262d2915SRabin Vincent 
1237262d2915SRabin Vincent 			if (readl(addr) & D40_EVENTLINE_MASK(event))
1238262d2915SRabin Vincent 				break;
1239262d2915SRabin Vincent 		}
1240262d2915SRabin Vincent 
1241262d2915SRabin Vincent 		if (tries != 99)
1242262d2915SRabin Vincent 			dev_dbg(chan2dev(d40c),
1243262d2915SRabin Vincent 				"[%s] workaround enable S%cLNK (%d tries)\n",
1244262d2915SRabin Vincent 				__func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1245262d2915SRabin Vincent 				100 - tries);
1246262d2915SRabin Vincent 
1247262d2915SRabin Vincent 		WARN_ON(!tries);
12481bdae6f4SNarayanan G 		break;
12491bdae6f4SNarayanan G 
12501bdae6f4SNarayanan G 	case D40_ROUND_EVENTLINE:
12511bdae6f4SNarayanan G 		BUG();
12521bdae6f4SNarayanan G 		break;
12531bdae6f4SNarayanan G 
12541bdae6f4SNarayanan G 	}
1255262d2915SRabin Vincent }
1256262d2915SRabin Vincent 
12571bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c,
12581bdae6f4SNarayanan G 				 enum d40_events event_type)
12598d318a50SLinus Walleij {
126026955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
126126955c07SLee Jones 
12628d318a50SLinus Walleij 	/* Enable event line connected to device (or memcpy) */
12632c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
12642c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
12651bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
12668d318a50SLinus Walleij 				       D40_CHAN_REG_SSLNK);
1267262d2915SRabin Vincent 
12682c2b62d5SLee Jones 	if (d40c->dma_cfg.dir !=  DMA_DEV_TO_MEM)
12691bdae6f4SNarayanan G 		__d40_config_set_event(d40c, event_type, event,
12708d318a50SLinus Walleij 				       D40_CHAN_REG_SDLNK);
12718d318a50SLinus Walleij }
12728d318a50SLinus Walleij 
1273a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c)
12748d318a50SLinus Walleij {
12758ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
1276be8cb7dfSJonas Aaberg 	u32 val;
12778d318a50SLinus Walleij 
12788ca84687SRabin Vincent 	val = readl(chanbase + D40_CHAN_REG_SSLNK);
12798ca84687SRabin Vincent 	val |= readl(chanbase + D40_CHAN_REG_SDLNK);
12808d318a50SLinus Walleij 
1281a5ebca47SJonas Aaberg 	return val;
12828d318a50SLinus Walleij }
12838d318a50SLinus Walleij 
12841bdae6f4SNarayanan G static int
12851bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
12861bdae6f4SNarayanan G {
12871bdae6f4SNarayanan G 	unsigned long flags;
12881bdae6f4SNarayanan G 	int ret = 0;
12891bdae6f4SNarayanan G 	u32 active_status;
12901bdae6f4SNarayanan G 	void __iomem *active_reg;
12911bdae6f4SNarayanan G 
12921bdae6f4SNarayanan G 	if (d40c->phy_chan->num % 2 == 0)
12931bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
12941bdae6f4SNarayanan G 	else
12951bdae6f4SNarayanan G 		active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
12961bdae6f4SNarayanan G 
12971bdae6f4SNarayanan G 
12981bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->phy_chan->lock, flags);
12991bdae6f4SNarayanan G 
13001bdae6f4SNarayanan G 	switch (command) {
13011bdae6f4SNarayanan G 	case D40_DMA_STOP:
13021bdae6f4SNarayanan G 	case D40_DMA_SUSPEND_REQ:
13031bdae6f4SNarayanan G 
13041bdae6f4SNarayanan G 		active_status = (readl(active_reg) &
13051bdae6f4SNarayanan G 				 D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
13061bdae6f4SNarayanan G 				 D40_CHAN_POS(d40c->phy_chan->num);
13071bdae6f4SNarayanan G 
13081bdae6f4SNarayanan G 		if (active_status == D40_DMA_RUN)
13091bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
13101bdae6f4SNarayanan G 		else
13111bdae6f4SNarayanan G 			d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
13121bdae6f4SNarayanan G 
13131bdae6f4SNarayanan G 		if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
13141bdae6f4SNarayanan G 			ret = __d40_execute_command_phy(d40c, command);
13151bdae6f4SNarayanan G 
13161bdae6f4SNarayanan G 		break;
13171bdae6f4SNarayanan G 
13181bdae6f4SNarayanan G 	case D40_DMA_RUN:
13191bdae6f4SNarayanan G 
13201bdae6f4SNarayanan G 		d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
13211bdae6f4SNarayanan G 		ret = __d40_execute_command_phy(d40c, command);
13221bdae6f4SNarayanan G 		break;
13231bdae6f4SNarayanan G 
13241bdae6f4SNarayanan G 	case D40_DMA_SUSPENDED:
13251bdae6f4SNarayanan G 		BUG();
13261bdae6f4SNarayanan G 		break;
13271bdae6f4SNarayanan G 	}
13281bdae6f4SNarayanan G 
13291bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
13301bdae6f4SNarayanan G 	return ret;
13311bdae6f4SNarayanan G }
13321bdae6f4SNarayanan G 
13331bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c,
13341bdae6f4SNarayanan G 				       enum d40_command command)
13351bdae6f4SNarayanan G {
13361bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
13371bdae6f4SNarayanan G 		return __d40_execute_command_log(d40c, command);
13381bdae6f4SNarayanan G 	else
13391bdae6f4SNarayanan G 		return __d40_execute_command_phy(d40c, command);
13401bdae6f4SNarayanan G }
13411bdae6f4SNarayanan G 
134220a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c)
134320a5b6d0SRabin Vincent {
134420a5b6d0SRabin Vincent 	static const unsigned int phy_map[] = {
134520a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_BASIC_MODE]
134620a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_BASIC,
134720a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_MODULO_MODE]
134820a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_MODULO,
134920a5b6d0SRabin Vincent 		[STEDMA40_PCHAN_DOUBLE_DST_MODE]
135020a5b6d0SRabin Vincent 			= D40_DREG_PRMO_PCHAN_DOUBLE_DST,
135120a5b6d0SRabin Vincent 	};
135220a5b6d0SRabin Vincent 	static const unsigned int log_map[] = {
135320a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_PHY_DST_LOG]
135420a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
135520a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_PHY]
135620a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
135720a5b6d0SRabin Vincent 		[STEDMA40_LCHAN_SRC_LOG_DST_LOG]
135820a5b6d0SRabin Vincent 			= D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
135920a5b6d0SRabin Vincent 	};
136020a5b6d0SRabin Vincent 
1361724a8577SRabin Vincent 	if (chan_is_physical(d40c))
136220a5b6d0SRabin Vincent 		return phy_map[d40c->dma_cfg.mode_opt];
136320a5b6d0SRabin Vincent 	else
136420a5b6d0SRabin Vincent 		return log_map[d40c->dma_cfg.mode_opt];
136520a5b6d0SRabin Vincent }
136620a5b6d0SRabin Vincent 
1367b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c)
13688d318a50SLinus Walleij {
13698d318a50SLinus Walleij 	u32 addr_base;
13708d318a50SLinus Walleij 	u32 var;
13718d318a50SLinus Walleij 
13728d318a50SLinus Walleij 	/* Odd addresses are even addresses + 4 */
13738d318a50SLinus Walleij 	addr_base = (d40c->phy_chan->num % 2) * 4;
13748d318a50SLinus Walleij 	/* Setup channel mode to logical or physical */
1375724a8577SRabin Vincent 	var = ((u32)(chan_is_logical(d40c)) + 1) <<
13768d318a50SLinus Walleij 		D40_CHAN_POS(d40c->phy_chan->num);
13778d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
13788d318a50SLinus Walleij 
13798d318a50SLinus Walleij 	/* Setup operational mode option register */
138020a5b6d0SRabin Vincent 	var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
13818d318a50SLinus Walleij 
13828d318a50SLinus Walleij 	writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
13838d318a50SLinus Walleij 
1384724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
13858ca84687SRabin Vincent 		int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
13868ca84687SRabin Vincent 			   & D40_SREG_ELEM_LOG_LIDX_MASK;
13878ca84687SRabin Vincent 		void __iomem *chanbase = chan_base(d40c);
13888ca84687SRabin Vincent 
13898d318a50SLinus Walleij 		/* Set default config for CFG reg */
13908ca84687SRabin Vincent 		writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
13918ca84687SRabin Vincent 		writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
13928d318a50SLinus Walleij 
1393b55912c6SJonas Aaberg 		/* Set LIDX for lcla */
13948ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SSELT);
13958ca84687SRabin Vincent 		writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1396e9f3a49cSRabin Vincent 
1397e9f3a49cSRabin Vincent 		/* Clear LNK which will be used by d40_chan_has_events() */
1398e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SSLNK);
1399e9f3a49cSRabin Vincent 		writel(0, chanbase + D40_CHAN_REG_SDLNK);
14008d318a50SLinus Walleij 	}
14018d318a50SLinus Walleij }
14028d318a50SLinus Walleij 
1403aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c)
1404aa182ae2SJonas Aaberg {
1405aa182ae2SJonas Aaberg 	u32 num_elt;
1406aa182ae2SJonas Aaberg 
1407724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1408aa182ae2SJonas Aaberg 		num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1409aa182ae2SJonas Aaberg 			>> D40_MEM_LCSP2_ECNT_POS;
14108ca84687SRabin Vincent 	else {
14118ca84687SRabin Vincent 		u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
14128ca84687SRabin Vincent 		num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
14138ca84687SRabin Vincent 			  >> D40_SREG_ELEM_PHY_ECNT_POS;
14148ca84687SRabin Vincent 	}
14158ca84687SRabin Vincent 
141643f2e1a3SLee Jones 	return num_elt * d40c->dma_cfg.dst_info.data_width;
1417aa182ae2SJonas Aaberg }
1418aa182ae2SJonas Aaberg 
1419aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c)
1420aa182ae2SJonas Aaberg {
1421aa182ae2SJonas Aaberg 	bool is_link;
1422aa182ae2SJonas Aaberg 
1423724a8577SRabin Vincent 	if (chan_is_logical(d40c))
1424aa182ae2SJonas Aaberg 		is_link = readl(&d40c->lcpa->lcsp3) &  D40_MEM_LCSP3_DLOS_MASK;
1425aa182ae2SJonas Aaberg 	else
14268ca84687SRabin Vincent 		is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
14278ca84687SRabin Vincent 			  & D40_SREG_LNK_PHYS_LNK_MASK;
14288ca84687SRabin Vincent 
1429aa182ae2SJonas Aaberg 	return is_link;
1430aa182ae2SJonas Aaberg }
1431aa182ae2SJonas Aaberg 
14326f5bad03SMaxime Ripard static int d40_pause(struct dma_chan *chan)
1433aa182ae2SJonas Aaberg {
14346f5bad03SMaxime Ripard 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1435aa182ae2SJonas Aaberg 	int res = 0;
1436aa182ae2SJonas Aaberg 	unsigned long flags;
1437aa182ae2SJonas Aaberg 
14386f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
14396f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
14406f5bad03SMaxime Ripard 		return -EINVAL;
14416f5bad03SMaxime Ripard 	}
14426f5bad03SMaxime Ripard 
14433ac012afSJonas Aaberg 	if (!d40c->busy)
14443ac012afSJonas Aaberg 		return 0;
14453ac012afSJonas Aaberg 
1446aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
144780245216SUlf Hansson 	pm_runtime_get_sync(d40c->base->dev);
1448aa182ae2SJonas Aaberg 
1449aa182ae2SJonas Aaberg 	res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
14501bdae6f4SNarayanan G 
14517fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
14527fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1453aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1454aa182ae2SJonas Aaberg 	return res;
1455aa182ae2SJonas Aaberg }
1456aa182ae2SJonas Aaberg 
14576f5bad03SMaxime Ripard static int d40_resume(struct dma_chan *chan)
1458aa182ae2SJonas Aaberg {
14596f5bad03SMaxime Ripard 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1460aa182ae2SJonas Aaberg 	int res = 0;
1461aa182ae2SJonas Aaberg 	unsigned long flags;
1462aa182ae2SJonas Aaberg 
14636f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
14646f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
14656f5bad03SMaxime Ripard 		return -EINVAL;
14666f5bad03SMaxime Ripard 	}
14676f5bad03SMaxime Ripard 
14683ac012afSJonas Aaberg 	if (!d40c->busy)
14693ac012afSJonas Aaberg 		return 0;
14703ac012afSJonas Aaberg 
1471aa182ae2SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
14727fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
1473aa182ae2SJonas Aaberg 
1474aa182ae2SJonas Aaberg 	/* If bytes left to transfer or linked tx resume job */
14751bdae6f4SNarayanan G 	if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1476aa182ae2SJonas Aaberg 		res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1477aa182ae2SJonas Aaberg 
14787fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
14797fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
1480aa182ae2SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
1481aa182ae2SJonas Aaberg 	return res;
1482aa182ae2SJonas Aaberg }
1483aa182ae2SJonas Aaberg 
14848d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
14858d318a50SLinus Walleij {
14868d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(tx->chan,
14878d318a50SLinus Walleij 					     struct d40_chan,
14888d318a50SLinus Walleij 					     chan);
14898d318a50SLinus Walleij 	struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
14908d318a50SLinus Walleij 	unsigned long flags;
1491884485e1SRussell King - ARM Linux 	dma_cookie_t cookie;
14928d318a50SLinus Walleij 
14938d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
1494884485e1SRussell King - ARM Linux 	cookie = dma_cookie_assign(tx);
14958d318a50SLinus Walleij 	d40_desc_queue(d40c, d40d);
14968d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
14978d318a50SLinus Walleij 
1498884485e1SRussell King - ARM Linux 	return cookie;
14998d318a50SLinus Walleij }
15008d318a50SLinus Walleij 
15018d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c)
15028d318a50SLinus Walleij {
15030c32269dSJonas Aaberg 	return d40_channel_execute_command(d40c, D40_DMA_RUN);
15048d318a50SLinus Walleij }
15058d318a50SLinus Walleij 
15068d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
15078d318a50SLinus Walleij {
15088d318a50SLinus Walleij 	struct d40_desc *d40d;
15098d318a50SLinus Walleij 	int err;
15108d318a50SLinus Walleij 
15118d318a50SLinus Walleij 	/* Start queued jobs, if any */
15128d318a50SLinus Walleij 	d40d = d40_first_queued(d40c);
15138d318a50SLinus Walleij 
15148d318a50SLinus Walleij 	if (d40d != NULL) {
15151bdae6f4SNarayanan G 		if (!d40c->busy) {
15168d318a50SLinus Walleij 			d40c->busy = true;
15177fb3e75eSNarayanan G 			pm_runtime_get_sync(d40c->base->dev);
15181bdae6f4SNarayanan G 		}
15197fb3e75eSNarayanan G 
15208d318a50SLinus Walleij 		/* Remove from queue */
15218d318a50SLinus Walleij 		d40_desc_remove(d40d);
15228d318a50SLinus Walleij 
15238d318a50SLinus Walleij 		/* Add to active queue */
15248d318a50SLinus Walleij 		d40_desc_submit(d40c, d40d);
15258d318a50SLinus Walleij 
15268d318a50SLinus Walleij 		/* Initiate DMA job */
15278d318a50SLinus Walleij 		d40_desc_load(d40c, d40d);
15288d318a50SLinus Walleij 
15298d318a50SLinus Walleij 		/* Start dma job */
15308d318a50SLinus Walleij 		err = d40_start(d40c);
15318d318a50SLinus Walleij 
15328d318a50SLinus Walleij 		if (err)
15338d318a50SLinus Walleij 			return NULL;
15348d318a50SLinus Walleij 	}
15358d318a50SLinus Walleij 
15368d318a50SLinus Walleij 	return d40d;
15378d318a50SLinus Walleij }
15388d318a50SLinus Walleij 
15398d318a50SLinus Walleij /* called from interrupt context */
15408d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c)
15418d318a50SLinus Walleij {
15428d318a50SLinus Walleij 	struct d40_desc *d40d;
15438d318a50SLinus Walleij 
15448d318a50SLinus Walleij 	/* Get first active entry from list */
15458d318a50SLinus Walleij 	d40d = d40_first_active_get(d40c);
15468d318a50SLinus Walleij 
15478d318a50SLinus Walleij 	if (d40d == NULL)
15488d318a50SLinus Walleij 		return;
15498d318a50SLinus Walleij 
15500c842b55SRabin Vincent 	if (d40d->cyclic) {
15510c842b55SRabin Vincent 		/*
15520c842b55SRabin Vincent 		 * If this was a paritially loaded list, we need to reloaded
15530c842b55SRabin Vincent 		 * it, and only when the list is completed.  We need to check
15540c842b55SRabin Vincent 		 * for done because the interrupt will hit for every link, and
15550c842b55SRabin Vincent 		 * not just the last one.
15560c842b55SRabin Vincent 		 */
15570c842b55SRabin Vincent 		if (d40d->lli_current < d40d->lli_len
15580c842b55SRabin Vincent 		    && !d40_tx_is_linked(d40c)
15590c842b55SRabin Vincent 		    && !d40_residue(d40c)) {
15600c842b55SRabin Vincent 			d40_lcla_free_all(d40c, d40d);
15610c842b55SRabin Vincent 			d40_desc_load(d40c, d40d);
15620c842b55SRabin Vincent 			(void) d40_start(d40c);
15630c842b55SRabin Vincent 
15640c842b55SRabin Vincent 			if (d40d->lli_current == d40d->lli_len)
15650c842b55SRabin Vincent 				d40d->lli_current = 0;
15660c842b55SRabin Vincent 		}
15670c842b55SRabin Vincent 	} else {
1568698e4732SJonas Aaberg 		d40_lcla_free_all(d40c, d40d);
15698d318a50SLinus Walleij 
1570698e4732SJonas Aaberg 		if (d40d->lli_current < d40d->lli_len) {
15718d318a50SLinus Walleij 			d40_desc_load(d40c, d40d);
15728d318a50SLinus Walleij 			/* Start dma job */
15738d318a50SLinus Walleij 			(void) d40_start(d40c);
15748d318a50SLinus Walleij 			return;
15758d318a50SLinus Walleij 		}
15768d318a50SLinus Walleij 
15779ecb41bdSRabin Vincent 		if (d40_queue_start(d40c) == NULL) {
15788d318a50SLinus Walleij 			d40c->busy = false;
15799ecb41bdSRabin Vincent 
15807fb3e75eSNarayanan G 			pm_runtime_mark_last_busy(d40c->base->dev);
15817fb3e75eSNarayanan G 			pm_runtime_put_autosuspend(d40c->base->dev);
15829ecb41bdSRabin Vincent 		}
15838d318a50SLinus Walleij 
15844226dd86SFabio Baltieri 		d40_desc_remove(d40d);
15854226dd86SFabio Baltieri 		d40_desc_done(d40c, d40d);
15867dd14525SFabio Baltieri 	}
15874226dd86SFabio Baltieri 
15888d318a50SLinus Walleij 	d40c->pending_tx++;
15898d318a50SLinus Walleij 	tasklet_schedule(&d40c->tasklet);
15908d318a50SLinus Walleij 
15918d318a50SLinus Walleij }
15928d318a50SLinus Walleij 
15938d318a50SLinus Walleij static void dma_tasklet(unsigned long data)
15948d318a50SLinus Walleij {
15958d318a50SLinus Walleij 	struct d40_chan *d40c = (struct d40_chan *) data;
1596767a9675SJonas Aaberg 	struct d40_desc *d40d;
15978d318a50SLinus Walleij 	unsigned long flags;
1598e9baa9d9SLinus Walleij 	bool callback_active;
15998d318a50SLinus Walleij 	dma_async_tx_callback callback;
16008d318a50SLinus Walleij 	void *callback_param;
16018d318a50SLinus Walleij 
16028d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
16038d318a50SLinus Walleij 
16044226dd86SFabio Baltieri 	/* Get first entry from the done list */
16054226dd86SFabio Baltieri 	d40d = d40_first_done(d40c);
16064226dd86SFabio Baltieri 	if (d40d == NULL) {
16074226dd86SFabio Baltieri 		/* Check if we have reached here for cyclic job */
1608767a9675SJonas Aaberg 		d40d = d40_first_active_get(d40c);
16094226dd86SFabio Baltieri 		if (d40d == NULL || !d40d->cyclic)
16108d318a50SLinus Walleij 			goto err;
16114226dd86SFabio Baltieri 	}
16128d318a50SLinus Walleij 
16130c842b55SRabin Vincent 	if (!d40d->cyclic)
1614f7fbce07SRussell King - ARM Linux 		dma_cookie_complete(&d40d->txd);
16158d318a50SLinus Walleij 
16168d318a50SLinus Walleij 	/*
16178d318a50SLinus Walleij 	 * If terminating a channel pending_tx is set to zero.
16188d318a50SLinus Walleij 	 * This prevents any finished active jobs to return to the client.
16198d318a50SLinus Walleij 	 */
16208d318a50SLinus Walleij 	if (d40c->pending_tx == 0) {
16218d318a50SLinus Walleij 		spin_unlock_irqrestore(&d40c->lock, flags);
16228d318a50SLinus Walleij 		return;
16238d318a50SLinus Walleij 	}
16248d318a50SLinus Walleij 
16258d318a50SLinus Walleij 	/* Callback to client */
1626e9baa9d9SLinus Walleij 	callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
1627767a9675SJonas Aaberg 	callback = d40d->txd.callback;
1628767a9675SJonas Aaberg 	callback_param = d40d->txd.callback_param;
16298d318a50SLinus Walleij 
16300c842b55SRabin Vincent 	if (!d40d->cyclic) {
1631767a9675SJonas Aaberg 		if (async_tx_test_ack(&d40d->txd)) {
1632767a9675SJonas Aaberg 			d40_desc_remove(d40d);
1633767a9675SJonas Aaberg 			d40_desc_free(d40c, d40d);
1634f26e03adSFabio Baltieri 		} else if (!d40d->is_in_client_list) {
1635767a9675SJonas Aaberg 			d40_desc_remove(d40d);
1636698e4732SJonas Aaberg 			d40_lcla_free_all(d40c, d40d);
1637767a9675SJonas Aaberg 			list_add_tail(&d40d->node, &d40c->client);
1638767a9675SJonas Aaberg 			d40d->is_in_client_list = true;
16398d318a50SLinus Walleij 		}
16408d318a50SLinus Walleij 	}
16418d318a50SLinus Walleij 
16428d318a50SLinus Walleij 	d40c->pending_tx--;
16438d318a50SLinus Walleij 
16448d318a50SLinus Walleij 	if (d40c->pending_tx)
16458d318a50SLinus Walleij 		tasklet_schedule(&d40c->tasklet);
16468d318a50SLinus Walleij 
16478d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
16488d318a50SLinus Walleij 
1649e9baa9d9SLinus Walleij 	if (callback_active && callback)
16508d318a50SLinus Walleij 		callback(callback_param);
16518d318a50SLinus Walleij 
16528d318a50SLinus Walleij 	return;
16538d318a50SLinus Walleij 
16548d318a50SLinus Walleij err:
16551bdae6f4SNarayanan G 	/* Rescue manouver if receiving double interrupts */
16568d318a50SLinus Walleij 	if (d40c->pending_tx > 0)
16578d318a50SLinus Walleij 		d40c->pending_tx--;
16588d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
16598d318a50SLinus Walleij }
16608d318a50SLinus Walleij 
16618d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data)
16628d318a50SLinus Walleij {
16638d318a50SLinus Walleij 	int i;
16648d318a50SLinus Walleij 	u32 idx;
16658d318a50SLinus Walleij 	u32 row;
16668d318a50SLinus Walleij 	long chan = -1;
16678d318a50SLinus Walleij 	struct d40_chan *d40c;
16688d318a50SLinus Walleij 	unsigned long flags;
16698d318a50SLinus Walleij 	struct d40_base *base = data;
16703cb645dcSTong Liu 	u32 regs[base->gen_dmac.il_size];
16713cb645dcSTong Liu 	struct d40_interrupt_lookup *il = base->gen_dmac.il;
16723cb645dcSTong Liu 	u32 il_size = base->gen_dmac.il_size;
16738d318a50SLinus Walleij 
16748d318a50SLinus Walleij 	spin_lock_irqsave(&base->interrupt_lock, flags);
16758d318a50SLinus Walleij 
16768d318a50SLinus Walleij 	/* Read interrupt status of both logical and physical channels */
16773cb645dcSTong Liu 	for (i = 0; i < il_size; i++)
16788d318a50SLinus Walleij 		regs[i] = readl(base->virtbase + il[i].src);
16798d318a50SLinus Walleij 
16808d318a50SLinus Walleij 	for (;;) {
16818d318a50SLinus Walleij 
16828d318a50SLinus Walleij 		chan = find_next_bit((unsigned long *)regs,
16833cb645dcSTong Liu 				     BITS_PER_LONG * il_size, chan + 1);
16848d318a50SLinus Walleij 
16858d318a50SLinus Walleij 		/* No more set bits found? */
16863cb645dcSTong Liu 		if (chan == BITS_PER_LONG * il_size)
16878d318a50SLinus Walleij 			break;
16888d318a50SLinus Walleij 
16898d318a50SLinus Walleij 		row = chan / BITS_PER_LONG;
16908d318a50SLinus Walleij 		idx = chan & (BITS_PER_LONG - 1);
16918d318a50SLinus Walleij 
16928d318a50SLinus Walleij 		if (il[row].offset == D40_PHY_CHAN)
16938d318a50SLinus Walleij 			d40c = base->lookup_phy_chans[idx];
16948d318a50SLinus Walleij 		else
16958d318a50SLinus Walleij 			d40c = base->lookup_log_chans[il[row].offset + idx];
169653d6d68fSFabio Baltieri 
169753d6d68fSFabio Baltieri 		if (!d40c) {
169853d6d68fSFabio Baltieri 			/*
169953d6d68fSFabio Baltieri 			 * No error because this can happen if something else
170053d6d68fSFabio Baltieri 			 * in the system is using the channel.
170153d6d68fSFabio Baltieri 			 */
170253d6d68fSFabio Baltieri 			continue;
170353d6d68fSFabio Baltieri 		}
170453d6d68fSFabio Baltieri 
170553d6d68fSFabio Baltieri 		/* ACK interrupt */
17068a3b6e14SLee Jones 		writel(BIT(idx), base->virtbase + il[row].clr);
170753d6d68fSFabio Baltieri 
17088d318a50SLinus Walleij 		spin_lock(&d40c->lock);
17098d318a50SLinus Walleij 
17108d318a50SLinus Walleij 		if (!il[row].is_error)
17118d318a50SLinus Walleij 			dma_tc_handle(d40c);
17128d318a50SLinus Walleij 		else
17136db5a8baSRabin Vincent 			d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
17146db5a8baSRabin Vincent 				chan, il[row].offset, idx);
17158d318a50SLinus Walleij 
17168d318a50SLinus Walleij 		spin_unlock(&d40c->lock);
17178d318a50SLinus Walleij 	}
17188d318a50SLinus Walleij 
17198d318a50SLinus Walleij 	spin_unlock_irqrestore(&base->interrupt_lock, flags);
17208d318a50SLinus Walleij 
17218d318a50SLinus Walleij 	return IRQ_HANDLED;
17228d318a50SLinus Walleij }
17238d318a50SLinus Walleij 
17248d318a50SLinus Walleij static int d40_validate_conf(struct d40_chan *d40c,
17258d318a50SLinus Walleij 			     struct stedma40_chan_cfg *conf)
17268d318a50SLinus Walleij {
17278d318a50SLinus Walleij 	int res = 0;
172838bdbf02SRabin Vincent 	bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
17298d318a50SLinus Walleij 
17300747c7baSLinus Walleij 	if (!conf->dir) {
17316db5a8baSRabin Vincent 		chan_err(d40c, "Invalid direction.\n");
17320747c7baSLinus Walleij 		res = -EINVAL;
17330747c7baSLinus Walleij 	}
17340747c7baSLinus Walleij 
173526955c07SLee Jones 	if ((is_log && conf->dev_type > d40c->base->num_log_chans)  ||
173626955c07SLee Jones 	    (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
173726955c07SLee Jones 	    (conf->dev_type < 0)) {
173826955c07SLee Jones 		chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
17390747c7baSLinus Walleij 		res = -EINVAL;
17400747c7baSLinus Walleij 	}
17410747c7baSLinus Walleij 
17422c2b62d5SLee Jones 	if (conf->dir == DMA_DEV_TO_DEV) {
17438d318a50SLinus Walleij 		/*
17448d318a50SLinus Walleij 		 * DMAC HW supports it. Will be added to this driver,
17458d318a50SLinus Walleij 		 * in case any dma client requires it.
17468d318a50SLinus Walleij 		 */
17476db5a8baSRabin Vincent 		chan_err(d40c, "periph to periph not supported\n");
17488d318a50SLinus Walleij 		res = -EINVAL;
17498d318a50SLinus Walleij 	}
17508d318a50SLinus Walleij 
1751d49278e3SPer Forlin 	if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
175243f2e1a3SLee Jones 	    conf->src_info.data_width !=
1753d49278e3SPer Forlin 	    d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
175443f2e1a3SLee Jones 	    conf->dst_info.data_width) {
1755d49278e3SPer Forlin 		/*
1756d49278e3SPer Forlin 		 * The DMAC hardware only supports
1757d49278e3SPer Forlin 		 * src (burst x width) == dst (burst x width)
1758d49278e3SPer Forlin 		 */
1759d49278e3SPer Forlin 
17606db5a8baSRabin Vincent 		chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
1761d49278e3SPer Forlin 		res = -EINVAL;
1762d49278e3SPer Forlin 	}
1763d49278e3SPer Forlin 
17648d318a50SLinus Walleij 	return res;
17658d318a50SLinus Walleij }
17668d318a50SLinus Walleij 
17675cd326fdSNarayanan G static bool d40_alloc_mask_set(struct d40_phy_res *phy,
17685cd326fdSNarayanan G 			       bool is_src, int log_event_line, bool is_log,
17695cd326fdSNarayanan G 			       bool *first_user)
17708d318a50SLinus Walleij {
17718d318a50SLinus Walleij 	unsigned long flags;
17728d318a50SLinus Walleij 	spin_lock_irqsave(&phy->lock, flags);
17735cd326fdSNarayanan G 
17745cd326fdSNarayanan G 	*first_user = ((phy->allocated_src | phy->allocated_dst)
17755cd326fdSNarayanan G 			== D40_ALLOC_FREE);
17765cd326fdSNarayanan G 
17774aed79b2SMarcin Mielczarczyk 	if (!is_log) {
17788d318a50SLinus Walleij 		/* Physical interrupts are masked per physical full channel */
17798d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE &&
17808d318a50SLinus Walleij 		    phy->allocated_dst == D40_ALLOC_FREE) {
17818d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_PHY;
17828d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_PHY;
17838d318a50SLinus Walleij 			goto found;
17848d318a50SLinus Walleij 		} else
17858d318a50SLinus Walleij 			goto not_found;
17868d318a50SLinus Walleij 	}
17878d318a50SLinus Walleij 
17888d318a50SLinus Walleij 	/* Logical channel */
17898d318a50SLinus Walleij 	if (is_src) {
17908d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_PHY)
17918d318a50SLinus Walleij 			goto not_found;
17928d318a50SLinus Walleij 
17938d318a50SLinus Walleij 		if (phy->allocated_src == D40_ALLOC_FREE)
17948d318a50SLinus Walleij 			phy->allocated_src = D40_ALLOC_LOG_FREE;
17958d318a50SLinus Walleij 
17968a3b6e14SLee Jones 		if (!(phy->allocated_src & BIT(log_event_line))) {
17978a3b6e14SLee Jones 			phy->allocated_src |= BIT(log_event_line);
17988d318a50SLinus Walleij 			goto found;
17998d318a50SLinus Walleij 		} else
18008d318a50SLinus Walleij 			goto not_found;
18018d318a50SLinus Walleij 	} else {
18028d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_PHY)
18038d318a50SLinus Walleij 			goto not_found;
18048d318a50SLinus Walleij 
18058d318a50SLinus Walleij 		if (phy->allocated_dst == D40_ALLOC_FREE)
18068d318a50SLinus Walleij 			phy->allocated_dst = D40_ALLOC_LOG_FREE;
18078d318a50SLinus Walleij 
18088a3b6e14SLee Jones 		if (!(phy->allocated_dst & BIT(log_event_line))) {
18098a3b6e14SLee Jones 			phy->allocated_dst |= BIT(log_event_line);
18108d318a50SLinus Walleij 			goto found;
18118d318a50SLinus Walleij 		} else
18128d318a50SLinus Walleij 			goto not_found;
18138d318a50SLinus Walleij 	}
18148d318a50SLinus Walleij 
18158d318a50SLinus Walleij not_found:
18168d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
18178d318a50SLinus Walleij 	return false;
18188d318a50SLinus Walleij found:
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;
18348d318a50SLinus Walleij 		goto out;
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);
18508d318a50SLinus Walleij 
18518d318a50SLinus Walleij out:
18528d318a50SLinus Walleij 	spin_unlock_irqrestore(&phy->lock, flags);
18538d318a50SLinus Walleij 
18548d318a50SLinus Walleij 	return is_free;
18558d318a50SLinus Walleij }
18568d318a50SLinus Walleij 
18575cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
18588d318a50SLinus Walleij {
185926955c07SLee Jones 	int dev_type = d40c->dma_cfg.dev_type;
18608d318a50SLinus Walleij 	int event_group;
18618d318a50SLinus Walleij 	int event_line;
18628d318a50SLinus Walleij 	struct d40_phy_res *phys;
18638d318a50SLinus Walleij 	int i;
18648d318a50SLinus Walleij 	int j;
18658d318a50SLinus Walleij 	int log_num;
1866f000df8cSGerald Baeza 	int num_phy_chans;
18678d318a50SLinus Walleij 	bool is_src;
186838bdbf02SRabin Vincent 	bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
18698d318a50SLinus Walleij 
18708d318a50SLinus Walleij 	phys = d40c->base->phy_res;
1871f000df8cSGerald Baeza 	num_phy_chans = d40c->base->num_phy_chans;
18728d318a50SLinus Walleij 
18732c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
18748d318a50SLinus Walleij 		log_num = 2 * dev_type;
18758d318a50SLinus Walleij 		is_src = true;
18762c2b62d5SLee Jones 	} else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
18772c2b62d5SLee Jones 		   d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18788d318a50SLinus Walleij 		/* dst event lines are used for logical memcpy */
18798d318a50SLinus Walleij 		log_num = 2 * dev_type + 1;
18808d318a50SLinus Walleij 		is_src = false;
18818d318a50SLinus Walleij 	} else
18828d318a50SLinus Walleij 		return -EINVAL;
18838d318a50SLinus Walleij 
18848d318a50SLinus Walleij 	event_group = D40_TYPE_TO_GROUP(dev_type);
18858d318a50SLinus Walleij 	event_line = D40_TYPE_TO_EVENT(dev_type);
18868d318a50SLinus Walleij 
18878d318a50SLinus Walleij 	if (!is_log) {
18882c2b62d5SLee Jones 		if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18898d318a50SLinus Walleij 			/* Find physical half channel */
1890f000df8cSGerald Baeza 			if (d40c->dma_cfg.use_fixed_channel) {
1891f000df8cSGerald Baeza 				i = d40c->dma_cfg.phy_channel;
18924aed79b2SMarcin Mielczarczyk 				if (d40_alloc_mask_set(&phys[i], is_src,
18935cd326fdSNarayanan G 						       0, is_log,
18945cd326fdSNarayanan G 						       first_phy_user))
18958d318a50SLinus Walleij 					goto found_phy;
1896f000df8cSGerald Baeza 			} else {
1897f000df8cSGerald Baeza 				for (i = 0; i < num_phy_chans; i++) {
1898f000df8cSGerald Baeza 					if (d40_alloc_mask_set(&phys[i], is_src,
1899f000df8cSGerald Baeza 						       0, is_log,
1900f000df8cSGerald Baeza 						       first_phy_user))
1901f000df8cSGerald Baeza 						goto found_phy;
1902f000df8cSGerald Baeza 				}
19038d318a50SLinus Walleij 			}
19048d318a50SLinus Walleij 		} else
19058d318a50SLinus Walleij 			for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19068d318a50SLinus Walleij 				int phy_num = j  + event_group * 2;
19078d318a50SLinus Walleij 				for (i = phy_num; i < phy_num + 2; i++) {
1908508849adSLinus Walleij 					if (d40_alloc_mask_set(&phys[i],
1909508849adSLinus Walleij 							       is_src,
1910508849adSLinus Walleij 							       0,
19115cd326fdSNarayanan G 							       is_log,
19125cd326fdSNarayanan G 							       first_phy_user))
19138d318a50SLinus Walleij 						goto found_phy;
19148d318a50SLinus Walleij 				}
19158d318a50SLinus Walleij 			}
19168d318a50SLinus Walleij 		return -EINVAL;
19178d318a50SLinus Walleij found_phy:
19188d318a50SLinus Walleij 		d40c->phy_chan = &phys[i];
19198d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
19208d318a50SLinus Walleij 		goto out;
19218d318a50SLinus Walleij 	}
19228d318a50SLinus Walleij 	if (dev_type == -1)
19238d318a50SLinus Walleij 		return -EINVAL;
19248d318a50SLinus Walleij 
19258d318a50SLinus Walleij 	/* Find logical channel */
19268d318a50SLinus Walleij 	for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19278d318a50SLinus Walleij 		int phy_num = j + event_group * 2;
19285cd326fdSNarayanan G 
19295cd326fdSNarayanan G 		if (d40c->dma_cfg.use_fixed_channel) {
19305cd326fdSNarayanan G 			i = d40c->dma_cfg.phy_channel;
19315cd326fdSNarayanan G 
19325cd326fdSNarayanan G 			if ((i != phy_num) && (i != phy_num + 1)) {
19335cd326fdSNarayanan G 				dev_err(chan2dev(d40c),
19345cd326fdSNarayanan G 					"invalid fixed phy channel %d\n", i);
19355cd326fdSNarayanan G 				return -EINVAL;
19365cd326fdSNarayanan G 			}
19375cd326fdSNarayanan G 
19385cd326fdSNarayanan G 			if (d40_alloc_mask_set(&phys[i], is_src, event_line,
19395cd326fdSNarayanan G 					       is_log, first_phy_user))
19405cd326fdSNarayanan G 				goto found_log;
19415cd326fdSNarayanan G 
19425cd326fdSNarayanan G 			dev_err(chan2dev(d40c),
19435cd326fdSNarayanan G 				"could not allocate fixed phy channel %d\n", i);
19445cd326fdSNarayanan G 			return -EINVAL;
19455cd326fdSNarayanan G 		}
19465cd326fdSNarayanan G 
19478d318a50SLinus Walleij 		/*
19488d318a50SLinus Walleij 		 * Spread logical channels across all available physical rather
19498d318a50SLinus Walleij 		 * than pack every logical channel at the first available phy
19508d318a50SLinus Walleij 		 * channels.
19518d318a50SLinus Walleij 		 */
19528d318a50SLinus Walleij 		if (is_src) {
19538d318a50SLinus Walleij 			for (i = phy_num; i < phy_num + 2; i++) {
19548d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
19555cd326fdSNarayanan G 						       event_line, is_log,
19565cd326fdSNarayanan G 						       first_phy_user))
19578d318a50SLinus Walleij 					goto found_log;
19588d318a50SLinus Walleij 			}
19598d318a50SLinus Walleij 		} else {
19608d318a50SLinus Walleij 			for (i = phy_num + 1; i >= phy_num; i--) {
19618d318a50SLinus Walleij 				if (d40_alloc_mask_set(&phys[i], is_src,
19625cd326fdSNarayanan G 						       event_line, is_log,
19635cd326fdSNarayanan G 						       first_phy_user))
19648d318a50SLinus Walleij 					goto found_log;
19658d318a50SLinus Walleij 			}
19668d318a50SLinus Walleij 		}
19678d318a50SLinus Walleij 	}
19688d318a50SLinus Walleij 	return -EINVAL;
19698d318a50SLinus Walleij 
19708d318a50SLinus Walleij found_log:
19718d318a50SLinus Walleij 	d40c->phy_chan = &phys[i];
19728d318a50SLinus Walleij 	d40c->log_num = log_num;
19738d318a50SLinus Walleij out:
19748d318a50SLinus Walleij 
19758d318a50SLinus Walleij 	if (is_log)
19768d318a50SLinus Walleij 		d40c->base->lookup_log_chans[d40c->log_num] = d40c;
19778d318a50SLinus Walleij 	else
19788d318a50SLinus Walleij 		d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
19798d318a50SLinus Walleij 
19808d318a50SLinus Walleij 	return 0;
19818d318a50SLinus Walleij 
19828d318a50SLinus Walleij }
19838d318a50SLinus Walleij 
19848d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c)
19858d318a50SLinus Walleij {
19868d318a50SLinus Walleij 	dma_cap_mask_t cap = d40c->chan.device->cap_mask;
19878d318a50SLinus Walleij 
19888d318a50SLinus Walleij 	if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
198929027a1eSLee Jones 		d40c->dma_cfg = dma40_memcpy_conf_log;
199026955c07SLee Jones 		d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
19918d318a50SLinus Walleij 
19929b233f9bSLee Jones 		d40_log_cfg(&d40c->dma_cfg,
19939b233f9bSLee Jones 			    &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
19949b233f9bSLee Jones 
19958d318a50SLinus Walleij 	} else if (dma_has_cap(DMA_MEMCPY, cap) &&
19968d318a50SLinus Walleij 		   dma_has_cap(DMA_SLAVE, cap)) {
199729027a1eSLee Jones 		d40c->dma_cfg = dma40_memcpy_conf_phy;
199857e65ad7SLee Jones 
199957e65ad7SLee Jones 		/* Generate interrrupt at end of transfer or relink. */
200057e65ad7SLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
200157e65ad7SLee Jones 
200257e65ad7SLee Jones 		/* Generate interrupt on error. */
200357e65ad7SLee Jones 		d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200457e65ad7SLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200557e65ad7SLee Jones 
20068d318a50SLinus Walleij 	} else {
20076db5a8baSRabin Vincent 		chan_err(d40c, "No memcpy\n");
20088d318a50SLinus Walleij 		return -EINVAL;
20098d318a50SLinus Walleij 	}
20108d318a50SLinus Walleij 
20118d318a50SLinus Walleij 	return 0;
20128d318a50SLinus Walleij }
20138d318a50SLinus Walleij 
20148d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c)
20158d318a50SLinus Walleij {
20168d318a50SLinus Walleij 
20178d318a50SLinus Walleij 	int res = 0;
201826955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
20198d318a50SLinus Walleij 	struct d40_phy_res *phy = d40c->phy_chan;
20208d318a50SLinus Walleij 	bool is_src;
20218d318a50SLinus Walleij 
20228d318a50SLinus Walleij 	/* Terminate all queued and active transfers */
20238d318a50SLinus Walleij 	d40_term_all(d40c);
20248d318a50SLinus Walleij 
20258d318a50SLinus Walleij 	if (phy == NULL) {
20266db5a8baSRabin Vincent 		chan_err(d40c, "phy == null\n");
20278d318a50SLinus Walleij 		return -EINVAL;
20288d318a50SLinus Walleij 	}
20298d318a50SLinus Walleij 
20308d318a50SLinus Walleij 	if (phy->allocated_src == D40_ALLOC_FREE &&
20318d318a50SLinus Walleij 	    phy->allocated_dst == D40_ALLOC_FREE) {
20326db5a8baSRabin Vincent 		chan_err(d40c, "channel already free\n");
20338d318a50SLinus Walleij 		return -EINVAL;
20348d318a50SLinus Walleij 	}
20358d318a50SLinus Walleij 
20362c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
20372c2b62d5SLee Jones 	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
20388d318a50SLinus Walleij 		is_src = false;
20392c2b62d5SLee Jones 	else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
20408d318a50SLinus Walleij 		is_src = true;
204126955c07SLee Jones 	else {
20426db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
20438d318a50SLinus Walleij 		return -EINVAL;
20448d318a50SLinus Walleij 	}
20458d318a50SLinus Walleij 
20467fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
20478d318a50SLinus Walleij 	res = d40_channel_execute_command(d40c, D40_DMA_STOP);
20488d318a50SLinus Walleij 	if (res) {
20491bdae6f4SNarayanan G 		chan_err(d40c, "stop failed\n");
20507fb3e75eSNarayanan G 		goto out;
20518d318a50SLinus Walleij 	}
20527fb3e75eSNarayanan G 
20531bdae6f4SNarayanan G 	d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
20541bdae6f4SNarayanan G 
20551bdae6f4SNarayanan G 	if (chan_is_logical(d40c))
20561bdae6f4SNarayanan G 		d40c->base->lookup_log_chans[d40c->log_num] = NULL;
20571bdae6f4SNarayanan G 	else
20581bdae6f4SNarayanan G 		d40c->base->lookup_phy_chans[phy->num] = NULL;
20591bdae6f4SNarayanan G 
20607fb3e75eSNarayanan G 	if (d40c->busy) {
20617fb3e75eSNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
20627fb3e75eSNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
20637fb3e75eSNarayanan G 	}
20647fb3e75eSNarayanan G 
20657fb3e75eSNarayanan G 	d40c->busy = false;
20668d318a50SLinus Walleij 	d40c->phy_chan = NULL;
2067ce2ca125SRabin Vincent 	d40c->configured = false;
20687fb3e75eSNarayanan G out:
20698d318a50SLinus Walleij 
20707fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
20717fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
20727fb3e75eSNarayanan G 	return res;
20738d318a50SLinus Walleij }
20748d318a50SLinus Walleij 
2075a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c)
2076a5ebca47SJonas Aaberg {
20778ca84687SRabin Vincent 	void __iomem *chanbase = chan_base(d40c);
2078a5ebca47SJonas Aaberg 	bool is_paused = false;
2079a5ebca47SJonas Aaberg 	unsigned long flags;
2080a5ebca47SJonas Aaberg 	void __iomem *active_reg;
2081a5ebca47SJonas Aaberg 	u32 status;
208226955c07SLee Jones 	u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2083a5ebca47SJonas Aaberg 
2084a5ebca47SJonas Aaberg 	spin_lock_irqsave(&d40c->lock, flags);
2085a5ebca47SJonas Aaberg 
2086724a8577SRabin Vincent 	if (chan_is_physical(d40c)) {
2087a5ebca47SJonas Aaberg 		if (d40c->phy_chan->num % 2 == 0)
2088a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2089a5ebca47SJonas Aaberg 		else
2090a5ebca47SJonas Aaberg 			active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2091a5ebca47SJonas Aaberg 
2092a5ebca47SJonas Aaberg 		status = (readl(active_reg) &
2093a5ebca47SJonas Aaberg 			  D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2094a5ebca47SJonas Aaberg 			D40_CHAN_POS(d40c->phy_chan->num);
2095a5ebca47SJonas Aaberg 		if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2096a5ebca47SJonas Aaberg 			is_paused = true;
2097a5ebca47SJonas Aaberg 
2098a5ebca47SJonas Aaberg 		goto _exit;
2099a5ebca47SJonas Aaberg 	}
2100a5ebca47SJonas Aaberg 
21012c2b62d5SLee Jones 	if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
21022c2b62d5SLee Jones 	    d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
21038ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SDLNK);
21042c2b62d5SLee Jones 	} else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
21058ca84687SRabin Vincent 		status = readl(chanbase + D40_CHAN_REG_SSLNK);
21069dbfbd35SJonas Aaberg 	} else {
21076db5a8baSRabin Vincent 		chan_err(d40c, "Unknown direction\n");
2108a5ebca47SJonas Aaberg 		goto _exit;
2109a5ebca47SJonas Aaberg 	}
21109dbfbd35SJonas Aaberg 
2111a5ebca47SJonas Aaberg 	status = (status & D40_EVENTLINE_MASK(event)) >>
2112a5ebca47SJonas Aaberg 		D40_EVENTLINE_POS(event);
2113a5ebca47SJonas Aaberg 
2114a5ebca47SJonas Aaberg 	if (status != D40_DMA_RUN)
2115a5ebca47SJonas Aaberg 		is_paused = true;
2116a5ebca47SJonas Aaberg _exit:
2117a5ebca47SJonas Aaberg 	spin_unlock_irqrestore(&d40c->lock, flags);
2118a5ebca47SJonas Aaberg 	return is_paused;
2119a5ebca47SJonas Aaberg 
2120a5ebca47SJonas Aaberg }
2121a5ebca47SJonas Aaberg 
21228d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan)
21238d318a50SLinus Walleij {
21248d318a50SLinus Walleij 	struct d40_chan *d40c =
21258d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
21268d318a50SLinus Walleij 	u32 bytes_left;
21278d318a50SLinus Walleij 	unsigned long flags;
21288d318a50SLinus Walleij 
21298d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
21308d318a50SLinus Walleij 	bytes_left = d40_residue(d40c);
21318d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
21328d318a50SLinus Walleij 
21338d318a50SLinus Walleij 	return bytes_left;
21348d318a50SLinus Walleij }
21358d318a50SLinus Walleij 
21363e3a0763SRabin Vincent static int
21373e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
21383e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
2139822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
2140822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
21413e3a0763SRabin Vincent {
21423e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21433e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
21443e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21455ed04b85SRabin Vincent 	int ret;
21463e3a0763SRabin Vincent 
21475ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_src, sg_len,
21485ed04b85SRabin Vincent 				src_dev_addr,
21493e3a0763SRabin Vincent 				desc->lli_log.src,
21503e3a0763SRabin Vincent 				chan->log_def.lcsp1,
21513e3a0763SRabin Vincent 				src_info->data_width,
21523e3a0763SRabin Vincent 				dst_info->data_width);
21533e3a0763SRabin Vincent 
21545ed04b85SRabin Vincent 	ret = d40_log_sg_to_lli(sg_dst, sg_len,
21555ed04b85SRabin Vincent 				dst_dev_addr,
21563e3a0763SRabin Vincent 				desc->lli_log.dst,
21573e3a0763SRabin Vincent 				chan->log_def.lcsp3,
21583e3a0763SRabin Vincent 				dst_info->data_width,
21593e3a0763SRabin Vincent 				src_info->data_width);
21603e3a0763SRabin Vincent 
21615ed04b85SRabin Vincent 	return ret < 0 ? ret : 0;
21623e3a0763SRabin Vincent }
21633e3a0763SRabin Vincent 
21643e3a0763SRabin Vincent static int
21653e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
21663e3a0763SRabin Vincent 		struct scatterlist *sg_src, struct scatterlist *sg_dst,
2167822c5676SRabin Vincent 		unsigned int sg_len, dma_addr_t src_dev_addr,
2168822c5676SRabin Vincent 		dma_addr_t dst_dev_addr)
21693e3a0763SRabin Vincent {
21703e3a0763SRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21713e3a0763SRabin Vincent 	struct stedma40_half_channel_info *src_info = &cfg->src_info;
21723e3a0763SRabin Vincent 	struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21730c842b55SRabin Vincent 	unsigned long flags = 0;
21743e3a0763SRabin Vincent 	int ret;
21753e3a0763SRabin Vincent 
21760c842b55SRabin Vincent 	if (desc->cyclic)
21770c842b55SRabin Vincent 		flags |= LLI_CYCLIC | LLI_TERM_INT;
21780c842b55SRabin Vincent 
21793e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
21803e3a0763SRabin Vincent 				desc->lli_phy.src,
21813e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.src),
21823e3a0763SRabin Vincent 				chan->src_def_cfg,
21830c842b55SRabin Vincent 				src_info, dst_info, flags);
21843e3a0763SRabin Vincent 
21853e3a0763SRabin Vincent 	ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
21863e3a0763SRabin Vincent 				desc->lli_phy.dst,
21873e3a0763SRabin Vincent 				virt_to_phys(desc->lli_phy.dst),
21883e3a0763SRabin Vincent 				chan->dst_def_cfg,
21890c842b55SRabin Vincent 				dst_info, src_info, flags);
21903e3a0763SRabin Vincent 
21913e3a0763SRabin Vincent 	dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
21923e3a0763SRabin Vincent 				   desc->lli_pool.size, DMA_TO_DEVICE);
21933e3a0763SRabin Vincent 
21943e3a0763SRabin Vincent 	return ret < 0 ? ret : 0;
21953e3a0763SRabin Vincent }
21963e3a0763SRabin Vincent 
21975f81158fSRabin Vincent static struct d40_desc *
21985f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
21995f81158fSRabin Vincent 	      unsigned int sg_len, unsigned long dma_flags)
22005f81158fSRabin Vincent {
22015f81158fSRabin Vincent 	struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
22025f81158fSRabin Vincent 	struct d40_desc *desc;
2203dbd88788SRabin Vincent 	int ret;
22045f81158fSRabin Vincent 
22055f81158fSRabin Vincent 	desc = d40_desc_get(chan);
22065f81158fSRabin Vincent 	if (!desc)
22075f81158fSRabin Vincent 		return NULL;
22085f81158fSRabin Vincent 
22095f81158fSRabin Vincent 	desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
22105f81158fSRabin Vincent 					cfg->dst_info.data_width);
22115f81158fSRabin Vincent 	if (desc->lli_len < 0) {
22125f81158fSRabin Vincent 		chan_err(chan, "Unaligned size\n");
2213dbd88788SRabin Vincent 		goto err;
22145f81158fSRabin Vincent 	}
22155f81158fSRabin Vincent 
2216dbd88788SRabin Vincent 	ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2217dbd88788SRabin Vincent 	if (ret < 0) {
2218dbd88788SRabin Vincent 		chan_err(chan, "Could not allocate lli\n");
2219dbd88788SRabin Vincent 		goto err;
2220dbd88788SRabin Vincent 	}
2221dbd88788SRabin Vincent 
22225f81158fSRabin Vincent 	desc->lli_current = 0;
22235f81158fSRabin Vincent 	desc->txd.flags = dma_flags;
22245f81158fSRabin Vincent 	desc->txd.tx_submit = d40_tx_submit;
22255f81158fSRabin Vincent 
22265f81158fSRabin Vincent 	dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
22275f81158fSRabin Vincent 
22285f81158fSRabin Vincent 	return desc;
2229dbd88788SRabin Vincent 
2230dbd88788SRabin Vincent err:
2231dbd88788SRabin Vincent 	d40_desc_free(chan, desc);
2232dbd88788SRabin Vincent 	return NULL;
22335f81158fSRabin Vincent }
22345f81158fSRabin Vincent 
2235cade1d30SRabin Vincent static struct dma_async_tx_descriptor *
2236cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2237cade1d30SRabin Vincent 	    struct scatterlist *sg_dst, unsigned int sg_len,
2238db8196dfSVinod Koul 	    enum dma_transfer_direction direction, unsigned long dma_flags)
2239cade1d30SRabin Vincent {
2240cade1d30SRabin Vincent 	struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2241822c5676SRabin Vincent 	dma_addr_t src_dev_addr = 0;
2242822c5676SRabin Vincent 	dma_addr_t dst_dev_addr = 0;
2243cade1d30SRabin Vincent 	struct d40_desc *desc;
2244cade1d30SRabin Vincent 	unsigned long flags;
2245cade1d30SRabin Vincent 	int ret;
22468d318a50SLinus Walleij 
2247cade1d30SRabin Vincent 	if (!chan->phy_chan) {
2248cade1d30SRabin Vincent 		chan_err(chan, "Cannot prepare unallocated channel\n");
2249cade1d30SRabin Vincent 		return NULL;
2250cade1d30SRabin Vincent 	}
2251cade1d30SRabin Vincent 
2252cade1d30SRabin Vincent 	spin_lock_irqsave(&chan->lock, flags);
2253cade1d30SRabin Vincent 
2254cade1d30SRabin Vincent 	desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2255cade1d30SRabin Vincent 	if (desc == NULL)
22568d318a50SLinus Walleij 		goto err;
22578d318a50SLinus Walleij 
22580c842b55SRabin Vincent 	if (sg_next(&sg_src[sg_len - 1]) == sg_src)
22590c842b55SRabin Vincent 		desc->cyclic = true;
22600c842b55SRabin Vincent 
2261db8196dfSVinod Koul 	if (direction == DMA_DEV_TO_MEM)
2262ef9c89b3SLee Jones 		src_dev_addr = chan->runtime_addr;
2263db8196dfSVinod Koul 	else if (direction == DMA_MEM_TO_DEV)
2264ef9c89b3SLee Jones 		dst_dev_addr = chan->runtime_addr;
2265cade1d30SRabin Vincent 
2266cade1d30SRabin Vincent 	if (chan_is_logical(chan))
2267cade1d30SRabin Vincent 		ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2268822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2269cade1d30SRabin Vincent 	else
2270cade1d30SRabin Vincent 		ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2271822c5676SRabin Vincent 				      sg_len, src_dev_addr, dst_dev_addr);
2272cade1d30SRabin Vincent 
2273cade1d30SRabin Vincent 	if (ret) {
2274cade1d30SRabin Vincent 		chan_err(chan, "Failed to prepare %s sg job: %d\n",
2275cade1d30SRabin Vincent 			 chan_is_logical(chan) ? "log" : "phy", ret);
2276cade1d30SRabin Vincent 		goto err;
22778d318a50SLinus Walleij 	}
22788d318a50SLinus Walleij 
227982babbb3SPer Forlin 	/*
228082babbb3SPer Forlin 	 * add descriptor to the prepare queue in order to be able
228182babbb3SPer Forlin 	 * to free them later in terminate_all
228282babbb3SPer Forlin 	 */
228382babbb3SPer Forlin 	list_add_tail(&desc->node, &chan->prepare_queue);
228482babbb3SPer Forlin 
2285cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
22868d318a50SLinus Walleij 
2287cade1d30SRabin Vincent 	return &desc->txd;
2288cade1d30SRabin Vincent 
22898d318a50SLinus Walleij err:
2290cade1d30SRabin Vincent 	if (desc)
2291cade1d30SRabin Vincent 		d40_desc_free(chan, desc);
2292cade1d30SRabin Vincent 	spin_unlock_irqrestore(&chan->lock, flags);
22938d318a50SLinus Walleij 	return NULL;
22948d318a50SLinus Walleij }
22958d318a50SLinus Walleij 
22968d318a50SLinus Walleij 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 EXPORT_SYMBOL(stedma40_filter);
23168d318a50SLinus Walleij 
2317ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2318ac2c0a38SRabin Vincent {
2319ac2c0a38SRabin Vincent 	bool realtime = d40c->dma_cfg.realtime;
2320ac2c0a38SRabin Vincent 	bool highprio = d40c->dma_cfg.high_priority;
23213cb645dcSTong Liu 	u32 rtreg;
2322ac2c0a38SRabin Vincent 	u32 event = D40_TYPE_TO_EVENT(dev_type);
2323ac2c0a38SRabin Vincent 	u32 group = D40_TYPE_TO_GROUP(dev_type);
23248a3b6e14SLee Jones 	u32 bit = BIT(event);
2325ccc3d697SRabin Vincent 	u32 prioreg;
23263cb645dcSTong Liu 	struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2327ccc3d697SRabin Vincent 
23283cb645dcSTong Liu 	rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2329ccc3d697SRabin Vincent 	/*
2330ccc3d697SRabin Vincent 	 * Due to a hardware bug, in some cases a logical channel triggered by
2331ccc3d697SRabin Vincent 	 * a high priority destination event line can generate extra packet
2332ccc3d697SRabin Vincent 	 * transactions.
2333ccc3d697SRabin Vincent 	 *
2334ccc3d697SRabin Vincent 	 * The workaround is to not set the high priority level for the
2335ccc3d697SRabin Vincent 	 * destination event lines that trigger logical channels.
2336ccc3d697SRabin Vincent 	 */
2337ccc3d697SRabin Vincent 	if (!src && chan_is_logical(d40c))
2338ccc3d697SRabin Vincent 		highprio = false;
2339ccc3d697SRabin Vincent 
23403cb645dcSTong Liu 	prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2341ac2c0a38SRabin Vincent 
2342ac2c0a38SRabin Vincent 	/* Destination event lines are stored in the upper halfword */
2343ac2c0a38SRabin Vincent 	if (!src)
2344ac2c0a38SRabin Vincent 		bit <<= 16;
2345ac2c0a38SRabin Vincent 
2346ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + prioreg + group * 4);
2347ac2c0a38SRabin Vincent 	writel(bit, d40c->base->virtbase + rtreg + group * 4);
2348ac2c0a38SRabin Vincent }
2349ac2c0a38SRabin Vincent 
2350ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c)
2351ac2c0a38SRabin Vincent {
2352ac2c0a38SRabin Vincent 	if (d40c->base->rev < 3)
2353ac2c0a38SRabin Vincent 		return;
2354ac2c0a38SRabin Vincent 
23552c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir ==  DMA_DEV_TO_MEM) ||
23562c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
235726955c07SLee Jones 		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2358ac2c0a38SRabin Vincent 
23592c2b62d5SLee Jones 	if ((d40c->dma_cfg.dir ==  DMA_MEM_TO_DEV) ||
23602c2b62d5SLee Jones 	    (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
236126955c07SLee Jones 		__d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2362ac2c0a38SRabin Vincent }
2363ac2c0a38SRabin Vincent 
2364fa332de5SLee Jones #define D40_DT_FLAGS_MODE(flags)       ((flags >> 0) & 0x1)
2365fa332de5SLee Jones #define D40_DT_FLAGS_DIR(flags)        ((flags >> 1) & 0x1)
2366fa332de5SLee Jones #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2367fa332de5SLee Jones #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2368bddd5a2bSLee Jones #define D40_DT_FLAGS_HIGH_PRIO(flags)  ((flags >> 4) & 0x1)
2369fa332de5SLee Jones 
2370fa332de5SLee Jones static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2371fa332de5SLee Jones 				  struct of_dma *ofdma)
2372fa332de5SLee Jones {
2373fa332de5SLee Jones 	struct stedma40_chan_cfg cfg;
2374fa332de5SLee Jones 	dma_cap_mask_t cap;
2375fa332de5SLee Jones 	u32 flags;
2376fa332de5SLee Jones 
2377fa332de5SLee Jones 	memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2378fa332de5SLee Jones 
2379fa332de5SLee Jones 	dma_cap_zero(cap);
2380fa332de5SLee Jones 	dma_cap_set(DMA_SLAVE, cap);
2381fa332de5SLee Jones 
2382fa332de5SLee Jones 	cfg.dev_type = dma_spec->args[0];
2383fa332de5SLee Jones 	flags = dma_spec->args[2];
2384fa332de5SLee Jones 
2385fa332de5SLee Jones 	switch (D40_DT_FLAGS_MODE(flags)) {
2386fa332de5SLee Jones 	case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2387fa332de5SLee Jones 	case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2388fa332de5SLee Jones 	}
2389fa332de5SLee Jones 
2390fa332de5SLee Jones 	switch (D40_DT_FLAGS_DIR(flags)) {
2391fa332de5SLee Jones 	case 0:
23922c2b62d5SLee Jones 		cfg.dir = DMA_MEM_TO_DEV;
2393fa332de5SLee Jones 		cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2394fa332de5SLee Jones 		break;
2395fa332de5SLee Jones 	case 1:
23962c2b62d5SLee Jones 		cfg.dir = DMA_DEV_TO_MEM;
2397fa332de5SLee Jones 		cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2398fa332de5SLee Jones 		break;
2399fa332de5SLee Jones 	}
2400fa332de5SLee Jones 
2401fa332de5SLee Jones 	if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2402fa332de5SLee Jones 		cfg.phy_channel = dma_spec->args[1];
2403fa332de5SLee Jones 		cfg.use_fixed_channel = true;
2404fa332de5SLee Jones 	}
2405fa332de5SLee Jones 
2406bddd5a2bSLee Jones 	if (D40_DT_FLAGS_HIGH_PRIO(flags))
2407bddd5a2bSLee Jones 		cfg.high_priority = true;
2408bddd5a2bSLee Jones 
2409fa332de5SLee Jones 	return dma_request_channel(cap, stedma40_filter, &cfg);
2410fa332de5SLee Jones }
2411fa332de5SLee Jones 
24128d318a50SLinus Walleij /* DMA ENGINE functions */
24138d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan)
24148d318a50SLinus Walleij {
24158d318a50SLinus Walleij 	int err;
24168d318a50SLinus Walleij 	unsigned long flags;
24178d318a50SLinus Walleij 	struct d40_chan *d40c =
24188d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
2419ef1872ecSLinus Walleij 	bool is_free_phy;
24208d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
24218d318a50SLinus Walleij 
2422d3ee98cdSRussell King - ARM Linux 	dma_cookie_init(chan);
24238d318a50SLinus Walleij 
2424ce2ca125SRabin Vincent 	/* If no dma configuration is set use default configuration (memcpy) */
2425ce2ca125SRabin Vincent 	if (!d40c->configured) {
24268d318a50SLinus Walleij 		err = d40_config_memcpy(d40c);
2427ff0b12baSJonas Aaberg 		if (err) {
24286db5a8baSRabin Vincent 			chan_err(d40c, "Failed to configure memcpy channel\n");
2429ff0b12baSJonas Aaberg 			goto fail;
2430ff0b12baSJonas Aaberg 		}
24318d318a50SLinus Walleij 	}
24328d318a50SLinus Walleij 
24335cd326fdSNarayanan G 	err = d40_allocate_channel(d40c, &is_free_phy);
24348d318a50SLinus Walleij 	if (err) {
24356db5a8baSRabin Vincent 		chan_err(d40c, "Failed to allocate channel\n");
24367fb3e75eSNarayanan G 		d40c->configured = false;
2437ff0b12baSJonas Aaberg 		goto fail;
24388d318a50SLinus Walleij 	}
24398d318a50SLinus Walleij 
24407fb3e75eSNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
2441ef1872ecSLinus Walleij 
2442ac2c0a38SRabin Vincent 	d40_set_prio_realtime(d40c);
2443ac2c0a38SRabin Vincent 
2444724a8577SRabin Vincent 	if (chan_is_logical(d40c)) {
24452c2b62d5SLee Jones 		if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2446ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
244726955c07SLee Jones 				d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2448ef1872ecSLinus Walleij 		else
2449ef1872ecSLinus Walleij 			d40c->lcpa = d40c->base->lcpa_base +
245026955c07SLee Jones 				d40c->dma_cfg.dev_type *
2451ef1872ecSLinus Walleij 				D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
24529778256bSLee Jones 
24539778256bSLee Jones 		/* Unmask the Global Interrupt Mask. */
24549778256bSLee Jones 		d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
24559778256bSLee Jones 		d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2456ef1872ecSLinus Walleij 	}
2457ef1872ecSLinus Walleij 
24585cd326fdSNarayanan G 	dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
24595cd326fdSNarayanan G 		 chan_is_logical(d40c) ? "logical" : "physical",
24605cd326fdSNarayanan G 		 d40c->phy_chan->num,
24615cd326fdSNarayanan G 		 d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
24625cd326fdSNarayanan G 
24635cd326fdSNarayanan G 
2464ef1872ecSLinus Walleij 	/*
2465ef1872ecSLinus Walleij 	 * Only write channel configuration to the DMA if the physical
2466ef1872ecSLinus Walleij 	 * resource is free. In case of multiple logical channels
2467ef1872ecSLinus Walleij 	 * on the same physical resource, only the first write is necessary.
2468ef1872ecSLinus Walleij 	 */
2469b55912c6SJonas Aaberg 	if (is_free_phy)
2470b55912c6SJonas Aaberg 		d40_config_write(d40c);
2471ff0b12baSJonas Aaberg fail:
24727fb3e75eSNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
24737fb3e75eSNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
24748d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
2475ff0b12baSJonas Aaberg 	return err;
24768d318a50SLinus Walleij }
24778d318a50SLinus Walleij 
24788d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan)
24798d318a50SLinus Walleij {
24808d318a50SLinus Walleij 	struct d40_chan *d40c =
24818d318a50SLinus Walleij 		container_of(chan, struct d40_chan, chan);
24828d318a50SLinus Walleij 	int err;
24838d318a50SLinus Walleij 	unsigned long flags;
24848d318a50SLinus Walleij 
24850d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
24866db5a8baSRabin Vincent 		chan_err(d40c, "Cannot free unallocated channel\n");
24870d0f6b8bSJonas Aaberg 		return;
24880d0f6b8bSJonas Aaberg 	}
24890d0f6b8bSJonas Aaberg 
24908d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
24918d318a50SLinus Walleij 
24928d318a50SLinus Walleij 	err = d40_free_dma(d40c);
24938d318a50SLinus Walleij 
24948d318a50SLinus Walleij 	if (err)
24956db5a8baSRabin Vincent 		chan_err(d40c, "Failed to free channel\n");
24968d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
24978d318a50SLinus Walleij }
24988d318a50SLinus Walleij 
24998d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
25008d318a50SLinus Walleij 						       dma_addr_t dst,
25018d318a50SLinus Walleij 						       dma_addr_t src,
25028d318a50SLinus Walleij 						       size_t size,
25032a614340SJonas Aaberg 						       unsigned long dma_flags)
25048d318a50SLinus Walleij {
250595944c6eSRabin Vincent 	struct scatterlist dst_sg;
250695944c6eSRabin Vincent 	struct scatterlist src_sg;
25078d318a50SLinus Walleij 
250895944c6eSRabin Vincent 	sg_init_table(&dst_sg, 1);
250995944c6eSRabin Vincent 	sg_init_table(&src_sg, 1);
25100d0f6b8bSJonas Aaberg 
251195944c6eSRabin Vincent 	sg_dma_address(&dst_sg) = dst;
251295944c6eSRabin Vincent 	sg_dma_address(&src_sg) = src;
25138d318a50SLinus Walleij 
251495944c6eSRabin Vincent 	sg_dma_len(&dst_sg) = size;
251595944c6eSRabin Vincent 	sg_dma_len(&src_sg) = size;
25168d318a50SLinus Walleij 
2517de6b641eSStefan Agner 	return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
2518de6b641eSStefan Agner 			   DMA_MEM_TO_MEM, dma_flags);
25198d318a50SLinus Walleij }
25208d318a50SLinus Walleij 
25210d688662SIra Snyder static struct dma_async_tx_descriptor *
2522cade1d30SRabin Vincent d40_prep_memcpy_sg(struct dma_chan *chan,
25230d688662SIra Snyder 		   struct scatterlist *dst_sg, unsigned int dst_nents,
25240d688662SIra Snyder 		   struct scatterlist *src_sg, unsigned int src_nents,
25250d688662SIra Snyder 		   unsigned long dma_flags)
25260d688662SIra Snyder {
25270d688662SIra Snyder 	if (dst_nents != src_nents)
25280d688662SIra Snyder 		return NULL;
25290d688662SIra Snyder 
2530de6b641eSStefan Agner 	return d40_prep_sg(chan, src_sg, dst_sg, src_nents,
2531de6b641eSStefan Agner 			   DMA_MEM_TO_MEM, dma_flags);
253200ac0341SRabin Vincent }
253300ac0341SRabin Vincent 
2534f26e03adSFabio Baltieri static struct dma_async_tx_descriptor *
2535f26e03adSFabio Baltieri d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2536f26e03adSFabio Baltieri 		  unsigned int sg_len, enum dma_transfer_direction direction,
2537f26e03adSFabio Baltieri 		  unsigned long dma_flags, void *context)
25388d318a50SLinus Walleij {
2539a725dcc0SAndy Shevchenko 	if (!is_slave_direction(direction))
254000ac0341SRabin Vincent 		return NULL;
254100ac0341SRabin Vincent 
2542cade1d30SRabin Vincent 	return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
25438d318a50SLinus Walleij }
25448d318a50SLinus Walleij 
25450c842b55SRabin Vincent static struct dma_async_tx_descriptor *
25460c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
25470c842b55SRabin Vincent 		     size_t buf_len, size_t period_len,
254831c1e5a1SLaurent Pinchart 		     enum dma_transfer_direction direction, unsigned long flags)
25490c842b55SRabin Vincent {
25500c842b55SRabin Vincent 	unsigned int periods = buf_len / period_len;
25510c842b55SRabin Vincent 	struct dma_async_tx_descriptor *txd;
25520c842b55SRabin Vincent 	struct scatterlist *sg;
25530c842b55SRabin Vincent 	int i;
25540c842b55SRabin Vincent 
255579ca7ec3SRobert Marklund 	sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
25562ec7e2e7SSachin Kamat 	if (!sg)
25572ec7e2e7SSachin Kamat 		return NULL;
25582ec7e2e7SSachin Kamat 
25590c842b55SRabin Vincent 	for (i = 0; i < periods; i++) {
25600c842b55SRabin Vincent 		sg_dma_address(&sg[i]) = dma_addr;
25610c842b55SRabin Vincent 		sg_dma_len(&sg[i]) = period_len;
25620c842b55SRabin Vincent 		dma_addr += period_len;
25630c842b55SRabin Vincent 	}
25640c842b55SRabin Vincent 
25650c842b55SRabin Vincent 	sg[periods].offset = 0;
2566fdaf9c4bSLars-Peter Clausen 	sg_dma_len(&sg[periods]) = 0;
25670c842b55SRabin Vincent 	sg[periods].page_link =
25680c842b55SRabin Vincent 		((unsigned long)sg | 0x01) & ~0x02;
25690c842b55SRabin Vincent 
25700c842b55SRabin Vincent 	txd = d40_prep_sg(chan, sg, sg, periods, direction,
25710c842b55SRabin Vincent 			  DMA_PREP_INTERRUPT);
25720c842b55SRabin Vincent 
25730c842b55SRabin Vincent 	kfree(sg);
25740c842b55SRabin Vincent 
25750c842b55SRabin Vincent 	return txd;
25760c842b55SRabin Vincent }
25770c842b55SRabin Vincent 
25788d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan,
25798d318a50SLinus Walleij 				     dma_cookie_t cookie,
25808d318a50SLinus Walleij 				     struct dma_tx_state *txstate)
25818d318a50SLinus Walleij {
25828d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
258396a2af41SRussell King - ARM Linux 	enum dma_status ret;
25848d318a50SLinus Walleij 
25850d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
25866db5a8baSRabin Vincent 		chan_err(d40c, "Cannot read status of unallocated channel\n");
25870d0f6b8bSJonas Aaberg 		return -EINVAL;
25880d0f6b8bSJonas Aaberg 	}
25890d0f6b8bSJonas Aaberg 
259096a2af41SRussell King - ARM Linux 	ret = dma_cookie_status(chan, cookie, txstate);
2591a90e56e5SPeter Griffin 	if (ret != DMA_COMPLETE && txstate)
259296a2af41SRussell King - ARM Linux 		dma_set_residue(txstate, stedma40_residue(chan));
25938d318a50SLinus Walleij 
2594a5ebca47SJonas Aaberg 	if (d40_is_paused(d40c))
2595a5ebca47SJonas Aaberg 		ret = DMA_PAUSED;
25968d318a50SLinus Walleij 
25978d318a50SLinus Walleij 	return ret;
25988d318a50SLinus Walleij }
25998d318a50SLinus Walleij 
26008d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan)
26018d318a50SLinus Walleij {
26028d318a50SLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26038d318a50SLinus Walleij 	unsigned long flags;
26048d318a50SLinus Walleij 
26050d0f6b8bSJonas Aaberg 	if (d40c->phy_chan == NULL) {
26066db5a8baSRabin Vincent 		chan_err(d40c, "Channel is not allocated!\n");
26070d0f6b8bSJonas Aaberg 		return;
26080d0f6b8bSJonas Aaberg 	}
26090d0f6b8bSJonas Aaberg 
26108d318a50SLinus Walleij 	spin_lock_irqsave(&d40c->lock, flags);
26118d318a50SLinus Walleij 
2612a8f3067bSPer Forlin 	list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2613a8f3067bSPer Forlin 
2614a8f3067bSPer Forlin 	/* Busy means that queued jobs are already being processed */
26158d318a50SLinus Walleij 	if (!d40c->busy)
26168d318a50SLinus Walleij 		(void) d40_queue_start(d40c);
26178d318a50SLinus Walleij 
26188d318a50SLinus Walleij 	spin_unlock_irqrestore(&d40c->lock, flags);
26198d318a50SLinus Walleij }
26208d318a50SLinus Walleij 
262135e639d1SVinod Koul static int d40_terminate_all(struct dma_chan *chan)
26221bdae6f4SNarayanan G {
26231bdae6f4SNarayanan G 	unsigned long flags;
26241bdae6f4SNarayanan G 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26251bdae6f4SNarayanan G 	int ret;
26261bdae6f4SNarayanan G 
26276f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
26286f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
26296f5bad03SMaxime Ripard 		return -EINVAL;
26306f5bad03SMaxime Ripard 	}
26316f5bad03SMaxime Ripard 
26321bdae6f4SNarayanan G 	spin_lock_irqsave(&d40c->lock, flags);
26331bdae6f4SNarayanan G 
26341bdae6f4SNarayanan G 	pm_runtime_get_sync(d40c->base->dev);
26351bdae6f4SNarayanan G 	ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
26361bdae6f4SNarayanan G 	if (ret)
26371bdae6f4SNarayanan G 		chan_err(d40c, "Failed to stop channel\n");
26381bdae6f4SNarayanan G 
26391bdae6f4SNarayanan G 	d40_term_all(d40c);
26401bdae6f4SNarayanan G 	pm_runtime_mark_last_busy(d40c->base->dev);
26411bdae6f4SNarayanan G 	pm_runtime_put_autosuspend(d40c->base->dev);
26421bdae6f4SNarayanan G 	if (d40c->busy) {
26431bdae6f4SNarayanan G 		pm_runtime_mark_last_busy(d40c->base->dev);
26441bdae6f4SNarayanan G 		pm_runtime_put_autosuspend(d40c->base->dev);
26451bdae6f4SNarayanan G 	}
26461bdae6f4SNarayanan G 	d40c->busy = false;
26471bdae6f4SNarayanan G 
26481bdae6f4SNarayanan G 	spin_unlock_irqrestore(&d40c->lock, flags);
264935e639d1SVinod Koul 	return 0;
26501bdae6f4SNarayanan G }
26511bdae6f4SNarayanan G 
265298ca5289SRabin Vincent static int
265398ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c,
265498ca5289SRabin Vincent 			    struct stedma40_half_channel_info *info,
265598ca5289SRabin Vincent 			    u32 maxburst)
265698ca5289SRabin Vincent {
265798ca5289SRabin Vincent 	int psize;
265898ca5289SRabin Vincent 
265998ca5289SRabin Vincent 	if (chan_is_logical(d40c)) {
266098ca5289SRabin Vincent 		if (maxburst >= 16)
266198ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_16;
266298ca5289SRabin Vincent 		else if (maxburst >= 8)
266398ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_8;
266498ca5289SRabin Vincent 		else if (maxburst >= 4)
266598ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_4;
266698ca5289SRabin Vincent 		else
266798ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_LOG_1;
266898ca5289SRabin Vincent 	} else {
266998ca5289SRabin Vincent 		if (maxburst >= 16)
267098ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_16;
267198ca5289SRabin Vincent 		else if (maxburst >= 8)
267298ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_8;
267398ca5289SRabin Vincent 		else if (maxburst >= 4)
267498ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_4;
267598ca5289SRabin Vincent 		else
267698ca5289SRabin Vincent 			psize = STEDMA40_PSIZE_PHY_1;
267798ca5289SRabin Vincent 	}
267898ca5289SRabin Vincent 
267998ca5289SRabin Vincent 	info->psize = psize;
268098ca5289SRabin Vincent 	info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
268198ca5289SRabin Vincent 
268298ca5289SRabin Vincent 	return 0;
268398ca5289SRabin Vincent }
268498ca5289SRabin Vincent 
268595e1400fSLinus Walleij /* Runtime reconfiguration extension */
268698ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan,
268795e1400fSLinus Walleij 				  struct dma_slave_config *config)
268895e1400fSLinus Walleij {
268995e1400fSLinus Walleij 	struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
269095e1400fSLinus Walleij 	struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
269198ca5289SRabin Vincent 	enum dma_slave_buswidth src_addr_width, dst_addr_width;
269295e1400fSLinus Walleij 	dma_addr_t config_addr;
269398ca5289SRabin Vincent 	u32 src_maxburst, dst_maxburst;
269498ca5289SRabin Vincent 	int ret;
269598ca5289SRabin Vincent 
26966f5bad03SMaxime Ripard 	if (d40c->phy_chan == NULL) {
26976f5bad03SMaxime Ripard 		chan_err(d40c, "Channel is not allocated!\n");
26986f5bad03SMaxime Ripard 		return -EINVAL;
26996f5bad03SMaxime Ripard 	}
27006f5bad03SMaxime Ripard 
270198ca5289SRabin Vincent 	src_addr_width = config->src_addr_width;
270298ca5289SRabin Vincent 	src_maxburst = config->src_maxburst;
270398ca5289SRabin Vincent 	dst_addr_width = config->dst_addr_width;
270498ca5289SRabin Vincent 	dst_maxburst = config->dst_maxburst;
270595e1400fSLinus Walleij 
2706db8196dfSVinod Koul 	if (config->direction == DMA_DEV_TO_MEM) {
270795e1400fSLinus Walleij 		config_addr = config->src_addr;
2708ef9c89b3SLee Jones 
27092c2b62d5SLee Jones 		if (cfg->dir != DMA_DEV_TO_MEM)
271095e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
271195e1400fSLinus Walleij 				"channel was not configured for peripheral "
271295e1400fSLinus Walleij 				"to memory transfer (%d) overriding\n",
271395e1400fSLinus Walleij 				cfg->dir);
27142c2b62d5SLee Jones 		cfg->dir = DMA_DEV_TO_MEM;
271595e1400fSLinus Walleij 
271698ca5289SRabin Vincent 		/* Configure the memory side */
271798ca5289SRabin Vincent 		if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
271898ca5289SRabin Vincent 			dst_addr_width = src_addr_width;
271998ca5289SRabin Vincent 		if (dst_maxburst == 0)
272098ca5289SRabin Vincent 			dst_maxburst = src_maxburst;
272195e1400fSLinus Walleij 
2722db8196dfSVinod Koul 	} else if (config->direction == DMA_MEM_TO_DEV) {
272395e1400fSLinus Walleij 		config_addr = config->dst_addr;
2724ef9c89b3SLee Jones 
27252c2b62d5SLee Jones 		if (cfg->dir != DMA_MEM_TO_DEV)
272695e1400fSLinus Walleij 			dev_dbg(d40c->base->dev,
272795e1400fSLinus Walleij 				"channel was not configured for memory "
272895e1400fSLinus Walleij 				"to peripheral transfer (%d) overriding\n",
272995e1400fSLinus Walleij 				cfg->dir);
27302c2b62d5SLee Jones 		cfg->dir = DMA_MEM_TO_DEV;
273195e1400fSLinus Walleij 
273298ca5289SRabin Vincent 		/* Configure the memory side */
273398ca5289SRabin Vincent 		if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
273498ca5289SRabin Vincent 			src_addr_width = dst_addr_width;
273598ca5289SRabin Vincent 		if (src_maxburst == 0)
273698ca5289SRabin Vincent 			src_maxburst = dst_maxburst;
273795e1400fSLinus Walleij 	} else {
273895e1400fSLinus Walleij 		dev_err(d40c->base->dev,
273995e1400fSLinus Walleij 			"unrecognized channel direction %d\n",
274095e1400fSLinus Walleij 			config->direction);
274198ca5289SRabin Vincent 		return -EINVAL;
274295e1400fSLinus Walleij 	}
274395e1400fSLinus Walleij 
2744ef9c89b3SLee Jones 	if (config_addr <= 0) {
2745ef9c89b3SLee Jones 		dev_err(d40c->base->dev, "no address supplied\n");
2746ef9c89b3SLee Jones 		return -EINVAL;
2747ef9c89b3SLee Jones 	}
2748ef9c89b3SLee Jones 
274998ca5289SRabin Vincent 	if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
275095e1400fSLinus Walleij 		dev_err(d40c->base->dev,
275198ca5289SRabin Vincent 			"src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
275298ca5289SRabin Vincent 			src_maxburst,
275398ca5289SRabin Vincent 			src_addr_width,
275498ca5289SRabin Vincent 			dst_maxburst,
275598ca5289SRabin Vincent 			dst_addr_width);
275698ca5289SRabin Vincent 		return -EINVAL;
275795e1400fSLinus Walleij 	}
275895e1400fSLinus Walleij 
275992bb6cdbSPer Forlin 	if (src_maxburst > 16) {
276092bb6cdbSPer Forlin 		src_maxburst = 16;
276192bb6cdbSPer Forlin 		dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
276292bb6cdbSPer Forlin 	} else if (dst_maxburst > 16) {
276392bb6cdbSPer Forlin 		dst_maxburst = 16;
276492bb6cdbSPer Forlin 		src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
276592bb6cdbSPer Forlin 	}
276692bb6cdbSPer Forlin 
276743f2e1a3SLee Jones 	/* Only valid widths are; 1, 2, 4 and 8. */
276843f2e1a3SLee Jones 	if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
276943f2e1a3SLee Jones 	    src_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
277043f2e1a3SLee Jones 	    dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
277143f2e1a3SLee Jones 	    dst_addr_width >  DMA_SLAVE_BUSWIDTH_8_BYTES   ||
2772c95905a6SGuennadi Liakhovetski 	    !is_power_of_2(src_addr_width) ||
2773c95905a6SGuennadi Liakhovetski 	    !is_power_of_2(dst_addr_width))
277443f2e1a3SLee Jones 		return -EINVAL;
277543f2e1a3SLee Jones 
277643f2e1a3SLee Jones 	cfg->src_info.data_width = src_addr_width;
277743f2e1a3SLee Jones 	cfg->dst_info.data_width = dst_addr_width;
277843f2e1a3SLee Jones 
277998ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
278098ca5289SRabin Vincent 					  src_maxburst);
278198ca5289SRabin Vincent 	if (ret)
278298ca5289SRabin Vincent 		return ret;
278395e1400fSLinus Walleij 
278498ca5289SRabin Vincent 	ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
278598ca5289SRabin Vincent 					  dst_maxburst);
278698ca5289SRabin Vincent 	if (ret)
278798ca5289SRabin Vincent 		return ret;
278895e1400fSLinus Walleij 
2789a59670a4SPer Forlin 	/* Fill in register values */
2790724a8577SRabin Vincent 	if (chan_is_logical(d40c))
2791a59670a4SPer Forlin 		d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2792a59670a4SPer Forlin 	else
279357e65ad7SLee Jones 		d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2794a59670a4SPer Forlin 
279595e1400fSLinus Walleij 	/* These settings will take precedence later */
279695e1400fSLinus Walleij 	d40c->runtime_addr = config_addr;
279795e1400fSLinus Walleij 	d40c->runtime_direction = config->direction;
279895e1400fSLinus Walleij 	dev_dbg(d40c->base->dev,
279998ca5289SRabin Vincent 		"configured channel %s for %s, data width %d/%d, "
280098ca5289SRabin Vincent 		"maxburst %d/%d elements, LE, no flow control\n",
280195e1400fSLinus Walleij 		dma_chan_name(chan),
2802db8196dfSVinod Koul 		(config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
280398ca5289SRabin Vincent 		src_addr_width, dst_addr_width,
280498ca5289SRabin Vincent 		src_maxburst, dst_maxburst);
280598ca5289SRabin Vincent 
280698ca5289SRabin Vincent 	return 0;
280795e1400fSLinus Walleij }
280895e1400fSLinus Walleij 
28098d318a50SLinus Walleij /* Initialization functions */
28108d318a50SLinus Walleij 
28118d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
28128d318a50SLinus Walleij 				 struct d40_chan *chans, int offset,
28138d318a50SLinus Walleij 				 int num_chans)
28148d318a50SLinus Walleij {
28158d318a50SLinus Walleij 	int i = 0;
28168d318a50SLinus Walleij 	struct d40_chan *d40c;
28178d318a50SLinus Walleij 
28188d318a50SLinus Walleij 	INIT_LIST_HEAD(&dma->channels);
28198d318a50SLinus Walleij 
28208d318a50SLinus Walleij 	for (i = offset; i < offset + num_chans; i++) {
28218d318a50SLinus Walleij 		d40c = &chans[i];
28228d318a50SLinus Walleij 		d40c->base = base;
28238d318a50SLinus Walleij 		d40c->chan.device = dma;
28248d318a50SLinus Walleij 
28258d318a50SLinus Walleij 		spin_lock_init(&d40c->lock);
28268d318a50SLinus Walleij 
28278d318a50SLinus Walleij 		d40c->log_num = D40_PHY_CHAN;
28288d318a50SLinus Walleij 
28294226dd86SFabio Baltieri 		INIT_LIST_HEAD(&d40c->done);
28308d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->active);
28318d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->queue);
2832a8f3067bSPer Forlin 		INIT_LIST_HEAD(&d40c->pending_queue);
28338d318a50SLinus Walleij 		INIT_LIST_HEAD(&d40c->client);
283482babbb3SPer Forlin 		INIT_LIST_HEAD(&d40c->prepare_queue);
28358d318a50SLinus Walleij 
28368d318a50SLinus Walleij 		tasklet_init(&d40c->tasklet, dma_tasklet,
28378d318a50SLinus Walleij 			     (unsigned long) d40c);
28388d318a50SLinus Walleij 
28398d318a50SLinus Walleij 		list_add_tail(&d40c->chan.device_node,
28408d318a50SLinus Walleij 			      &dma->channels);
28418d318a50SLinus Walleij 	}
28428d318a50SLinus Walleij }
28438d318a50SLinus Walleij 
28447ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
28457ad74a7cSRabin Vincent {
28467ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
28477ad74a7cSRabin Vincent 		dev->device_prep_slave_sg = d40_prep_slave_sg;
28487ad74a7cSRabin Vincent 
28497ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
28507ad74a7cSRabin Vincent 		dev->device_prep_dma_memcpy = d40_prep_memcpy;
28517ad74a7cSRabin Vincent 
28527ad74a7cSRabin Vincent 		/*
28537ad74a7cSRabin Vincent 		 * This controller can only access address at even
28547ad74a7cSRabin Vincent 		 * 32bit boundaries, i.e. 2^2
28557ad74a7cSRabin Vincent 		 */
285677a68e56SMaxime Ripard 		dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
28577ad74a7cSRabin Vincent 	}
28587ad74a7cSRabin Vincent 
28597ad74a7cSRabin Vincent 	if (dma_has_cap(DMA_SG, dev->cap_mask))
28607ad74a7cSRabin Vincent 		dev->device_prep_dma_sg = d40_prep_memcpy_sg;
28617ad74a7cSRabin Vincent 
28620c842b55SRabin Vincent 	if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
28630c842b55SRabin Vincent 		dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
28640c842b55SRabin Vincent 
28657ad74a7cSRabin Vincent 	dev->device_alloc_chan_resources = d40_alloc_chan_resources;
28667ad74a7cSRabin Vincent 	dev->device_free_chan_resources = d40_free_chan_resources;
28677ad74a7cSRabin Vincent 	dev->device_issue_pending = d40_issue_pending;
28687ad74a7cSRabin Vincent 	dev->device_tx_status = d40_tx_status;
28696f5bad03SMaxime Ripard 	dev->device_config = d40_set_runtime_config;
28706f5bad03SMaxime Ripard 	dev->device_pause = d40_pause;
28716f5bad03SMaxime Ripard 	dev->device_resume = d40_resume;
28726f5bad03SMaxime Ripard 	dev->device_terminate_all = d40_terminate_all;
28737ad74a7cSRabin Vincent 	dev->dev = base->dev;
28747ad74a7cSRabin Vincent }
28757ad74a7cSRabin Vincent 
28768d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base,
28778d318a50SLinus Walleij 				     int num_reserved_chans)
28788d318a50SLinus Walleij {
28798d318a50SLinus Walleij 	int err ;
28808d318a50SLinus Walleij 
28818d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_slave, base->log_chans,
28828d318a50SLinus Walleij 		      0, base->num_log_chans);
28838d318a50SLinus Walleij 
28848d318a50SLinus Walleij 	dma_cap_zero(base->dma_slave.cap_mask);
28858d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
28860c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
28878d318a50SLinus Walleij 
28887ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_slave);
28898d318a50SLinus Walleij 
28908d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_slave);
28918d318a50SLinus Walleij 
28928d318a50SLinus Walleij 	if (err) {
28936db5a8baSRabin Vincent 		d40_err(base->dev, "Failed to register slave channels\n");
28948d318a50SLinus Walleij 		goto failure1;
28958d318a50SLinus Walleij 	}
28968d318a50SLinus Walleij 
28978d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2898a7dacb68SLee Jones 		      base->num_log_chans, base->num_memcpy_chans);
28998d318a50SLinus Walleij 
29008d318a50SLinus Walleij 	dma_cap_zero(base->dma_memcpy.cap_mask);
29018d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
29027ad74a7cSRabin Vincent 	dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
29038d318a50SLinus Walleij 
29047ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_memcpy);
29058d318a50SLinus Walleij 
29068d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_memcpy);
29078d318a50SLinus Walleij 
29088d318a50SLinus Walleij 	if (err) {
29096db5a8baSRabin Vincent 		d40_err(base->dev,
291052984aabSGeliang Tang 			"Failed to register memcpy only channels\n");
29118d318a50SLinus Walleij 		goto failure2;
29128d318a50SLinus Walleij 	}
29138d318a50SLinus Walleij 
29148d318a50SLinus Walleij 	d40_chan_init(base, &base->dma_both, base->phy_chans,
29158d318a50SLinus Walleij 		      0, num_reserved_chans);
29168d318a50SLinus Walleij 
29178d318a50SLinus Walleij 	dma_cap_zero(base->dma_both.cap_mask);
29188d318a50SLinus Walleij 	dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
29198d318a50SLinus Walleij 	dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
29207ad74a7cSRabin Vincent 	dma_cap_set(DMA_SG, base->dma_both.cap_mask);
29210c842b55SRabin Vincent 	dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
29228d318a50SLinus Walleij 
29237ad74a7cSRabin Vincent 	d40_ops_init(base, &base->dma_both);
29248d318a50SLinus Walleij 	err = dma_async_device_register(&base->dma_both);
29258d318a50SLinus Walleij 
29268d318a50SLinus Walleij 	if (err) {
29276db5a8baSRabin Vincent 		d40_err(base->dev,
29286db5a8baSRabin Vincent 			"Failed to register logical and physical capable channels\n");
29298d318a50SLinus Walleij 		goto failure3;
29308d318a50SLinus Walleij 	}
29318d318a50SLinus Walleij 	return 0;
29328d318a50SLinus Walleij failure3:
29338d318a50SLinus Walleij 	dma_async_device_unregister(&base->dma_memcpy);
29348d318a50SLinus Walleij failure2:
29358d318a50SLinus Walleij 	dma_async_device_unregister(&base->dma_slave);
29368d318a50SLinus Walleij failure1:
29378d318a50SLinus Walleij 	return err;
29388d318a50SLinus Walleij }
29398d318a50SLinus Walleij 
29407fb3e75eSNarayanan G /* Suspend resume functionality */
2941123e4ca1SUlf Hansson #ifdef CONFIG_PM_SLEEP
2942123e4ca1SUlf Hansson static int dma40_suspend(struct device *dev)
29437fb3e75eSNarayanan G {
294428c7a19dSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
294528c7a19dSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
2946c906a3ecSUlf Hansson 	int ret;
2947c906a3ecSUlf Hansson 
2948c906a3ecSUlf Hansson 	ret = pm_runtime_force_suspend(dev);
2949c906a3ecSUlf Hansson 	if (ret)
2950c906a3ecSUlf Hansson 		return ret;
29517fb3e75eSNarayanan G 
295228c7a19dSNarayanan G 	if (base->lcpa_regulator)
295328c7a19dSNarayanan G 		ret = regulator_disable(base->lcpa_regulator);
295428c7a19dSNarayanan G 	return ret;
29557fb3e75eSNarayanan G }
29567fb3e75eSNarayanan G 
2957123e4ca1SUlf Hansson static int dma40_resume(struct device *dev)
2958123e4ca1SUlf Hansson {
2959123e4ca1SUlf Hansson 	struct platform_device *pdev = to_platform_device(dev);
2960123e4ca1SUlf Hansson 	struct d40_base *base = platform_get_drvdata(pdev);
2961123e4ca1SUlf Hansson 	int ret = 0;
2962123e4ca1SUlf Hansson 
2963c906a3ecSUlf Hansson 	if (base->lcpa_regulator) {
2964123e4ca1SUlf Hansson 		ret = regulator_enable(base->lcpa_regulator);
2965c906a3ecSUlf Hansson 		if (ret)
2966123e4ca1SUlf Hansson 			return ret;
2967123e4ca1SUlf Hansson 	}
2968c906a3ecSUlf Hansson 
2969c906a3ecSUlf Hansson 	return pm_runtime_force_resume(dev);
2970c906a3ecSUlf Hansson }
2971123e4ca1SUlf Hansson #endif
2972123e4ca1SUlf Hansson 
2973123e4ca1SUlf Hansson #ifdef CONFIG_PM
2974123e4ca1SUlf Hansson static void dma40_backup(void __iomem *baseaddr, u32 *backup,
2975123e4ca1SUlf Hansson 			 u32 *regaddr, int num, bool save)
2976123e4ca1SUlf Hansson {
2977123e4ca1SUlf Hansson 	int i;
2978123e4ca1SUlf Hansson 
2979123e4ca1SUlf Hansson 	for (i = 0; i < num; i++) {
2980123e4ca1SUlf Hansson 		void __iomem *addr = baseaddr + regaddr[i];
2981123e4ca1SUlf Hansson 
2982123e4ca1SUlf Hansson 		if (save)
2983123e4ca1SUlf Hansson 			backup[i] = readl_relaxed(addr);
2984123e4ca1SUlf Hansson 		else
2985123e4ca1SUlf Hansson 			writel_relaxed(backup[i], addr);
2986123e4ca1SUlf Hansson 	}
2987123e4ca1SUlf Hansson }
2988123e4ca1SUlf Hansson 
2989123e4ca1SUlf Hansson static void d40_save_restore_registers(struct d40_base *base, bool save)
2990123e4ca1SUlf Hansson {
2991123e4ca1SUlf Hansson 	int i;
2992123e4ca1SUlf Hansson 
2993123e4ca1SUlf Hansson 	/* Save/Restore channel specific registers */
2994123e4ca1SUlf Hansson 	for (i = 0; i < base->num_phy_chans; i++) {
2995123e4ca1SUlf Hansson 		void __iomem *addr;
2996123e4ca1SUlf Hansson 		int idx;
2997123e4ca1SUlf Hansson 
2998123e4ca1SUlf Hansson 		if (base->phy_res[i].reserved)
2999123e4ca1SUlf Hansson 			continue;
3000123e4ca1SUlf Hansson 
3001123e4ca1SUlf Hansson 		addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
3002123e4ca1SUlf Hansson 		idx = i * ARRAY_SIZE(d40_backup_regs_chan);
3003123e4ca1SUlf Hansson 
3004123e4ca1SUlf Hansson 		dma40_backup(addr, &base->reg_val_backup_chan[idx],
3005123e4ca1SUlf Hansson 			     d40_backup_regs_chan,
3006123e4ca1SUlf Hansson 			     ARRAY_SIZE(d40_backup_regs_chan),
3007123e4ca1SUlf Hansson 			     save);
3008123e4ca1SUlf Hansson 	}
3009123e4ca1SUlf Hansson 
3010123e4ca1SUlf Hansson 	/* Save/Restore global registers */
3011123e4ca1SUlf Hansson 	dma40_backup(base->virtbase, base->reg_val_backup,
3012123e4ca1SUlf Hansson 		     d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
3013123e4ca1SUlf Hansson 		     save);
3014123e4ca1SUlf Hansson 
3015123e4ca1SUlf Hansson 	/* Save/Restore registers only existing on dma40 v3 and later */
3016123e4ca1SUlf Hansson 	if (base->gen_dmac.backup)
3017123e4ca1SUlf Hansson 		dma40_backup(base->virtbase, base->reg_val_backup_v4,
3018123e4ca1SUlf Hansson 			     base->gen_dmac.backup,
3019123e4ca1SUlf Hansson 			base->gen_dmac.backup_size,
3020123e4ca1SUlf Hansson 			save);
3021123e4ca1SUlf Hansson }
3022123e4ca1SUlf Hansson 
30237fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev)
30247fb3e75eSNarayanan G {
30257fb3e75eSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
30267fb3e75eSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
30277fb3e75eSNarayanan G 
30287fb3e75eSNarayanan G 	d40_save_restore_registers(base, true);
30297fb3e75eSNarayanan G 
30307fb3e75eSNarayanan G 	/* Don't disable/enable clocks for v1 due to HW bugs */
30317fb3e75eSNarayanan G 	if (base->rev != 1)
30327fb3e75eSNarayanan G 		writel_relaxed(base->gcc_pwr_off_mask,
30337fb3e75eSNarayanan G 			       base->virtbase + D40_DREG_GCC);
30347fb3e75eSNarayanan G 
30357fb3e75eSNarayanan G 	return 0;
30367fb3e75eSNarayanan G }
30377fb3e75eSNarayanan G 
30387fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev)
30397fb3e75eSNarayanan G {
30407fb3e75eSNarayanan G 	struct platform_device *pdev = to_platform_device(dev);
30417fb3e75eSNarayanan G 	struct d40_base *base = platform_get_drvdata(pdev);
30427fb3e75eSNarayanan G 
30437fb3e75eSNarayanan G 	d40_save_restore_registers(base, false);
30447fb3e75eSNarayanan G 
30457fb3e75eSNarayanan G 	writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
30467fb3e75eSNarayanan G 		       base->virtbase + D40_DREG_GCC);
30477fb3e75eSNarayanan G 	return 0;
30487fb3e75eSNarayanan G }
3049123e4ca1SUlf Hansson #endif
30507fb3e75eSNarayanan G 
30517fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = {
3052673d3773SUlf Hansson 	SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
30536ed23b80SRafael J. Wysocki 	SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
3054123e4ca1SUlf Hansson 				dma40_runtime_resume,
3055123e4ca1SUlf Hansson 				NULL)
30567fb3e75eSNarayanan G };
30577fb3e75eSNarayanan G 
30588d318a50SLinus Walleij /* Initialization functions. */
30598d318a50SLinus Walleij 
30608d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base)
30618d318a50SLinus Walleij {
30628d318a50SLinus Walleij 	int i;
30638d318a50SLinus Walleij 	int num_phy_chans_avail = 0;
30648d318a50SLinus Walleij 	u32 val[2];
30658d318a50SLinus Walleij 	int odd_even_bit = -2;
30667fb3e75eSNarayanan G 	int gcc = D40_DREG_GCC_ENA;
30678d318a50SLinus Walleij 
30688d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRSME);
30698d318a50SLinus Walleij 	val[1] = readl(base->virtbase + D40_DREG_PRSMO);
30708d318a50SLinus Walleij 
30718d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
30728d318a50SLinus Walleij 		base->phy_res[i].num = i;
30738d318a50SLinus Walleij 		odd_even_bit += 2 * ((i % 2) == 0);
30748d318a50SLinus Walleij 		if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
30758d318a50SLinus Walleij 			/* Mark security only channels as occupied */
30768d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_PHY;
30778d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
30787fb3e75eSNarayanan G 			base->phy_res[i].reserved = true;
30797fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30807fb3e75eSNarayanan G 						       D40_DREG_GCC_SRC);
30817fb3e75eSNarayanan G 			gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30827fb3e75eSNarayanan G 						       D40_DREG_GCC_DST);
30837fb3e75eSNarayanan G 
30847fb3e75eSNarayanan G 
30858d318a50SLinus Walleij 		} else {
30868d318a50SLinus Walleij 			base->phy_res[i].allocated_src = D40_ALLOC_FREE;
30878d318a50SLinus Walleij 			base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
30887fb3e75eSNarayanan G 			base->phy_res[i].reserved = false;
30898d318a50SLinus Walleij 			num_phy_chans_avail++;
30908d318a50SLinus Walleij 		}
30918d318a50SLinus Walleij 		spin_lock_init(&base->phy_res[i].lock);
30928d318a50SLinus Walleij 	}
30936b7acd84SJonas Aaberg 
30946b7acd84SJonas Aaberg 	/* Mark disabled channels as occupied */
30956b7acd84SJonas Aaberg 	for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3096f57b407cSRabin Vincent 		int chan = base->plat_data->disabled_channels[i];
3097f57b407cSRabin Vincent 
3098f57b407cSRabin Vincent 		base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3099f57b407cSRabin Vincent 		base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
31007fb3e75eSNarayanan G 		base->phy_res[chan].reserved = true;
31017fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
31027fb3e75eSNarayanan G 					       D40_DREG_GCC_SRC);
31037fb3e75eSNarayanan G 		gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
31047fb3e75eSNarayanan G 					       D40_DREG_GCC_DST);
31056b7acd84SJonas Aaberg 		num_phy_chans_avail--;
31066b7acd84SJonas Aaberg 	}
31076b7acd84SJonas Aaberg 
31087407048bSFabio Baltieri 	/* Mark soft_lli channels */
31097407048bSFabio Baltieri 	for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
31107407048bSFabio Baltieri 		int chan = base->plat_data->soft_lli_chans[i];
31117407048bSFabio Baltieri 
31127407048bSFabio Baltieri 		base->phy_res[chan].use_soft_lli = true;
31137407048bSFabio Baltieri 	}
31147407048bSFabio Baltieri 
31158d318a50SLinus Walleij 	dev_info(base->dev, "%d of %d physical DMA channels available\n",
31168d318a50SLinus Walleij 		 num_phy_chans_avail, base->num_phy_chans);
31178d318a50SLinus Walleij 
31188d318a50SLinus Walleij 	/* Verify settings extended vs standard */
31198d318a50SLinus Walleij 	val[0] = readl(base->virtbase + D40_DREG_PRTYP);
31208d318a50SLinus Walleij 
31218d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
31228d318a50SLinus Walleij 
31238d318a50SLinus Walleij 		if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
31248d318a50SLinus Walleij 		    (val[0] & 0x3) != 1)
31258d318a50SLinus Walleij 			dev_info(base->dev,
31268d318a50SLinus Walleij 				 "[%s] INFO: channel %d is misconfigured (%d)\n",
31278d318a50SLinus Walleij 				 __func__, i, val[0] & 0x3);
31288d318a50SLinus Walleij 
31298d318a50SLinus Walleij 		val[0] = val[0] >> 2;
31308d318a50SLinus Walleij 	}
31318d318a50SLinus Walleij 
31327fb3e75eSNarayanan G 	/*
31337fb3e75eSNarayanan G 	 * To keep things simple, Enable all clocks initially.
31347fb3e75eSNarayanan G 	 * The clocks will get managed later post channel allocation.
31357fb3e75eSNarayanan G 	 * The clocks for the event lines on which reserved channels exists
31367fb3e75eSNarayanan G 	 * are not managed here.
31377fb3e75eSNarayanan G 	 */
31387fb3e75eSNarayanan G 	writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
31397fb3e75eSNarayanan G 	base->gcc_pwr_off_mask = gcc;
31407fb3e75eSNarayanan G 
31418d318a50SLinus Walleij 	return num_phy_chans_avail;
31428d318a50SLinus Walleij }
31438d318a50SLinus Walleij 
31448d318a50SLinus Walleij static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
31458d318a50SLinus Walleij {
3146d4adcc01SJingoo Han 	struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
31478d318a50SLinus Walleij 	struct clk *clk = NULL;
31488d318a50SLinus Walleij 	void __iomem *virtbase = NULL;
31498d318a50SLinus Walleij 	struct resource *res = NULL;
31508d318a50SLinus Walleij 	struct d40_base *base = NULL;
31518d318a50SLinus Walleij 	int num_log_chans = 0;
31528d318a50SLinus Walleij 	int num_phy_chans;
3153a7dacb68SLee Jones 	int num_memcpy_chans;
3154b707c658SUlf Hansson 	int clk_ret = -EINVAL;
31558d318a50SLinus Walleij 	int i;
3156f4b89764SLinus Walleij 	u32 pid;
3157f4b89764SLinus Walleij 	u32 cid;
3158f4b89764SLinus Walleij 	u8 rev;
31598d318a50SLinus Walleij 
31608d318a50SLinus Walleij 	clk = clk_get(&pdev->dev, NULL);
31618d318a50SLinus Walleij 	if (IS_ERR(clk)) {
31626db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No matching clock found\n");
31638d318a50SLinus Walleij 		goto failure;
31648d318a50SLinus Walleij 	}
31658d318a50SLinus Walleij 
3166b707c658SUlf Hansson 	clk_ret = clk_prepare_enable(clk);
3167b707c658SUlf Hansson 	if (clk_ret) {
3168b707c658SUlf Hansson 		d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
3169b707c658SUlf Hansson 		goto failure;
3170b707c658SUlf Hansson 	}
31718d318a50SLinus Walleij 
31728d318a50SLinus Walleij 	/* Get IO for DMAC base address */
31738d318a50SLinus Walleij 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
31748d318a50SLinus Walleij 	if (!res)
31758d318a50SLinus Walleij 		goto failure;
31768d318a50SLinus Walleij 
31778d318a50SLinus Walleij 	if (request_mem_region(res->start, resource_size(res),
31788d318a50SLinus Walleij 			       D40_NAME " I/O base") == NULL)
31798d318a50SLinus Walleij 		goto failure;
31808d318a50SLinus Walleij 
31818d318a50SLinus Walleij 	virtbase = ioremap(res->start, resource_size(res));
31828d318a50SLinus Walleij 	if (!virtbase)
31838d318a50SLinus Walleij 		goto failure;
31848d318a50SLinus Walleij 
3185f4b89764SLinus Walleij 	/* This is just a regular AMBA PrimeCell ID actually */
3186f4b89764SLinus Walleij 	for (pid = 0, i = 0; i < 4; i++)
3187f4b89764SLinus Walleij 		pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
3188f4b89764SLinus Walleij 			& 255) << (i * 8);
3189f4b89764SLinus Walleij 	for (cid = 0, i = 0; i < 4; i++)
3190f4b89764SLinus Walleij 		cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
3191f4b89764SLinus Walleij 			& 255) << (i * 8);
3192f4b89764SLinus Walleij 
3193f4b89764SLinus Walleij 	if (cid != AMBA_CID) {
3194f4b89764SLinus Walleij 		d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
31958d318a50SLinus Walleij 		goto failure;
31968d318a50SLinus Walleij 	}
3197f4b89764SLinus Walleij 	if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
31986db5a8baSRabin Vincent 		d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
3199f4b89764SLinus Walleij 			AMBA_MANF_BITS(pid),
3200f4b89764SLinus Walleij 			AMBA_VENDOR_ST);
32018d318a50SLinus Walleij 		goto failure;
32028d318a50SLinus Walleij 	}
3203f4b89764SLinus Walleij 	/*
3204f4b89764SLinus Walleij 	 * HW revision:
3205f4b89764SLinus Walleij 	 * DB8500ed has revision 0
3206f4b89764SLinus Walleij 	 * ? has revision 1
3207f4b89764SLinus Walleij 	 * DB8500v1 has revision 2
3208f4b89764SLinus Walleij 	 * DB8500v2 has revision 3
320947db92f4SGerald Baeza 	 * AP9540v1 has revision 4
321047db92f4SGerald Baeza 	 * DB8540v1 has revision 4
3211f4b89764SLinus Walleij 	 */
3212f4b89764SLinus Walleij 	rev = AMBA_REV_BITS(pid);
32138b2fe9b6SLee Jones 	if (rev < 2) {
32148b2fe9b6SLee Jones 		d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
32158b2fe9b6SLee Jones 		goto failure;
32168b2fe9b6SLee Jones 	}
32173ae0267fSJonas Aaberg 
32188d318a50SLinus Walleij 	/* The number of physical channels on this HW */
321947db92f4SGerald Baeza 	if (plat_data->num_of_phy_chans)
322047db92f4SGerald Baeza 		num_phy_chans = plat_data->num_of_phy_chans;
322147db92f4SGerald Baeza 	else
32228d318a50SLinus Walleij 		num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
32238d318a50SLinus Walleij 
3224a7dacb68SLee Jones 	/* The number of channels used for memcpy */
3225a7dacb68SLee Jones 	if (plat_data->num_of_memcpy_chans)
3226a7dacb68SLee Jones 		num_memcpy_chans = plat_data->num_of_memcpy_chans;
3227a7dacb68SLee Jones 	else
3228a7dacb68SLee Jones 		num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
3229a7dacb68SLee Jones 
3230db72da92SLee Jones 	num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3231db72da92SLee Jones 
3232b2abb249SLee Jones 	dev_info(&pdev->dev,
32333a919d5bSFabio Estevam 		 "hardware rev: %d @ %pa with %d physical and %d logical channels\n",
32343a919d5bSFabio Estevam 		 rev, &res->start, num_phy_chans, num_log_chans);
32358d318a50SLinus Walleij 
32368d318a50SLinus Walleij 	base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
3237a7dacb68SLee Jones 		       (num_phy_chans + num_log_chans + num_memcpy_chans) *
32388d318a50SLinus Walleij 		       sizeof(struct d40_chan), GFP_KERNEL);
32398d318a50SLinus Walleij 
3240aef94feaSPeter Griffin 	if (base == NULL)
32418d318a50SLinus Walleij 		goto failure;
32428d318a50SLinus Walleij 
32433ae0267fSJonas Aaberg 	base->rev = rev;
32448d318a50SLinus Walleij 	base->clk = clk;
3245a7dacb68SLee Jones 	base->num_memcpy_chans = num_memcpy_chans;
32468d318a50SLinus Walleij 	base->num_phy_chans = num_phy_chans;
32478d318a50SLinus Walleij 	base->num_log_chans = num_log_chans;
32488d318a50SLinus Walleij 	base->phy_start = res->start;
32498d318a50SLinus Walleij 	base->phy_size = resource_size(res);
32508d318a50SLinus Walleij 	base->virtbase = virtbase;
32518d318a50SLinus Walleij 	base->plat_data = plat_data;
32528d318a50SLinus Walleij 	base->dev = &pdev->dev;
32538d318a50SLinus Walleij 	base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
32548d318a50SLinus Walleij 	base->log_chans = &base->phy_chans[num_phy_chans];
32558d318a50SLinus Walleij 
32563cb645dcSTong Liu 	if (base->plat_data->num_of_phy_chans == 14) {
32573cb645dcSTong Liu 		base->gen_dmac.backup = d40_backup_regs_v4b;
32583cb645dcSTong Liu 		base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
32593cb645dcSTong Liu 		base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
32603cb645dcSTong Liu 		base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
32613cb645dcSTong Liu 		base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
32623cb645dcSTong Liu 		base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
32633cb645dcSTong Liu 		base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
32643cb645dcSTong Liu 		base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
32653cb645dcSTong Liu 		base->gen_dmac.il = il_v4b;
32663cb645dcSTong Liu 		base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
32673cb645dcSTong Liu 		base->gen_dmac.init_reg = dma_init_reg_v4b;
32683cb645dcSTong Liu 		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
32693cb645dcSTong Liu 	} else {
32703cb645dcSTong Liu 		if (base->rev >= 3) {
32713cb645dcSTong Liu 			base->gen_dmac.backup = d40_backup_regs_v4a;
32723cb645dcSTong Liu 			base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
32733cb645dcSTong Liu 		}
32743cb645dcSTong Liu 		base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
32753cb645dcSTong Liu 		base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
32763cb645dcSTong Liu 		base->gen_dmac.realtime_en = D40_DREG_RSEG1;
32773cb645dcSTong Liu 		base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
32783cb645dcSTong Liu 		base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
32793cb645dcSTong Liu 		base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
32803cb645dcSTong Liu 		base->gen_dmac.il = il_v4a;
32813cb645dcSTong Liu 		base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
32823cb645dcSTong Liu 		base->gen_dmac.init_reg = dma_init_reg_v4a;
32833cb645dcSTong Liu 		base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
32843cb645dcSTong Liu 	}
32853cb645dcSTong Liu 
32868d318a50SLinus Walleij 	base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
32878d318a50SLinus Walleij 				GFP_KERNEL);
32888d318a50SLinus Walleij 	if (!base->phy_res)
32898d318a50SLinus Walleij 		goto failure;
32908d318a50SLinus Walleij 
32918d318a50SLinus Walleij 	base->lookup_phy_chans = kzalloc(num_phy_chans *
32928d318a50SLinus Walleij 					 sizeof(struct d40_chan *),
32938d318a50SLinus Walleij 					 GFP_KERNEL);
32948d318a50SLinus Walleij 	if (!base->lookup_phy_chans)
32958d318a50SLinus Walleij 		goto failure;
32968d318a50SLinus Walleij 
3297db72da92SLee Jones 	base->lookup_log_chans = kzalloc(num_log_chans *
32988d318a50SLinus Walleij 					 sizeof(struct d40_chan *),
32998d318a50SLinus Walleij 					 GFP_KERNEL);
33008d318a50SLinus Walleij 	if (!base->lookup_log_chans)
33018d318a50SLinus Walleij 		goto failure;
3302698e4732SJonas Aaberg 
33037fb3e75eSNarayanan G 	base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
33047fb3e75eSNarayanan G 					    sizeof(d40_backup_regs_chan),
33058d318a50SLinus Walleij 					    GFP_KERNEL);
33067fb3e75eSNarayanan G 	if (!base->reg_val_backup_chan)
33077fb3e75eSNarayanan G 		goto failure;
33087fb3e75eSNarayanan G 
33097fb3e75eSNarayanan G 	base->lcla_pool.alloc_map =
33107fb3e75eSNarayanan G 		kzalloc(num_phy_chans * sizeof(struct d40_desc *)
33117fb3e75eSNarayanan G 			* D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
33128d318a50SLinus Walleij 	if (!base->lcla_pool.alloc_map)
33138d318a50SLinus Walleij 		goto failure;
33148d318a50SLinus Walleij 
3315c675b1b4SJonas Aaberg 	base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3316c675b1b4SJonas Aaberg 					    0, SLAB_HWCACHE_ALIGN,
3317c675b1b4SJonas Aaberg 					    NULL);
3318c675b1b4SJonas Aaberg 	if (base->desc_slab == NULL)
3319c675b1b4SJonas Aaberg 		goto failure;
3320c675b1b4SJonas Aaberg 
33218d318a50SLinus Walleij 	return base;
33228d318a50SLinus Walleij 
33238d318a50SLinus Walleij failure:
3324b707c658SUlf Hansson 	if (!clk_ret)
3325b707c658SUlf Hansson 		clk_disable_unprepare(clk);
3326b707c658SUlf Hansson 	if (!IS_ERR(clk))
33278d318a50SLinus Walleij 		clk_put(clk);
33288d318a50SLinus Walleij 	if (virtbase)
33298d318a50SLinus Walleij 		iounmap(virtbase);
33308d318a50SLinus Walleij 	if (res)
33318d318a50SLinus Walleij 		release_mem_region(res->start,
33328d318a50SLinus Walleij 				   resource_size(res));
33338d318a50SLinus Walleij 	if (virtbase)
33348d318a50SLinus Walleij 		iounmap(virtbase);
33358d318a50SLinus Walleij 
33368d318a50SLinus Walleij 	if (base) {
33378d318a50SLinus Walleij 		kfree(base->lcla_pool.alloc_map);
33381bdae6f4SNarayanan G 		kfree(base->reg_val_backup_chan);
33398d318a50SLinus Walleij 		kfree(base->lookup_log_chans);
33408d318a50SLinus Walleij 		kfree(base->lookup_phy_chans);
33418d318a50SLinus Walleij 		kfree(base->phy_res);
33428d318a50SLinus Walleij 		kfree(base);
33438d318a50SLinus Walleij 	}
33448d318a50SLinus Walleij 
33458d318a50SLinus Walleij 	return NULL;
33468d318a50SLinus Walleij }
33478d318a50SLinus Walleij 
33488d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base)
33498d318a50SLinus Walleij {
33508d318a50SLinus Walleij 
33518d318a50SLinus Walleij 	int i;
33528d318a50SLinus Walleij 	u32 prmseo[2] = {0, 0};
33538d318a50SLinus Walleij 	u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
33548d318a50SLinus Walleij 	u32 pcmis = 0;
33558d318a50SLinus Walleij 	u32 pcicr = 0;
33563cb645dcSTong Liu 	struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
33573cb645dcSTong Liu 	u32 reg_size = base->gen_dmac.init_reg_size;
33588d318a50SLinus Walleij 
33593cb645dcSTong Liu 	for (i = 0; i < reg_size; i++)
33608d318a50SLinus Walleij 		writel(dma_init_reg[i].val,
33618d318a50SLinus Walleij 		       base->virtbase + dma_init_reg[i].reg);
33628d318a50SLinus Walleij 
33638d318a50SLinus Walleij 	/* Configure all our dma channels to default settings */
33648d318a50SLinus Walleij 	for (i = 0; i < base->num_phy_chans; i++) {
33658d318a50SLinus Walleij 
33668d318a50SLinus Walleij 		activeo[i % 2] = activeo[i % 2] << 2;
33678d318a50SLinus Walleij 
33688d318a50SLinus Walleij 		if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
33698d318a50SLinus Walleij 		    == D40_ALLOC_PHY) {
33708d318a50SLinus Walleij 			activeo[i % 2] |= 3;
33718d318a50SLinus Walleij 			continue;
33728d318a50SLinus Walleij 		}
33738d318a50SLinus Walleij 
33748d318a50SLinus Walleij 		/* Enable interrupt # */
33758d318a50SLinus Walleij 		pcmis = (pcmis << 1) | 1;
33768d318a50SLinus Walleij 
33778d318a50SLinus Walleij 		/* Clear interrupt # */
33788d318a50SLinus Walleij 		pcicr = (pcicr << 1) | 1;
33798d318a50SLinus Walleij 
33808d318a50SLinus Walleij 		/* Set channel to physical mode */
33818d318a50SLinus Walleij 		prmseo[i % 2] = prmseo[i % 2] << 2;
33828d318a50SLinus Walleij 		prmseo[i % 2] |= 1;
33838d318a50SLinus Walleij 
33848d318a50SLinus Walleij 	}
33858d318a50SLinus Walleij 
33868d318a50SLinus Walleij 	writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
33878d318a50SLinus Walleij 	writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
33888d318a50SLinus Walleij 	writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
33898d318a50SLinus Walleij 	writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
33908d318a50SLinus Walleij 
33918d318a50SLinus Walleij 	/* Write which interrupt to enable */
33923cb645dcSTong Liu 	writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
33938d318a50SLinus Walleij 
33948d318a50SLinus Walleij 	/* Write which interrupt to clear */
33953cb645dcSTong Liu 	writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
33968d318a50SLinus Walleij 
33973cb645dcSTong Liu 	/* These are __initdata and cannot be accessed after init */
33983cb645dcSTong Liu 	base->gen_dmac.init_reg = NULL;
33993cb645dcSTong Liu 	base->gen_dmac.init_reg_size = 0;
34008d318a50SLinus Walleij }
34018d318a50SLinus Walleij 
3402508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base)
3403508849adSLinus Walleij {
3404026cbc42SRabin Vincent 	struct d40_lcla_pool *pool = &base->lcla_pool;
3405508849adSLinus Walleij 	unsigned long *page_list;
3406508849adSLinus Walleij 	int i, j;
3407*abac5bacSMarkus Elfring 	int ret;
3408508849adSLinus Walleij 
3409508849adSLinus Walleij 	/*
3410508849adSLinus Walleij 	 * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3411508849adSLinus Walleij 	 * To full fill this hardware requirement without wasting 256 kb
3412508849adSLinus Walleij 	 * we allocate pages until we get an aligned one.
3413508849adSLinus Walleij 	 */
3414cf80ecf7SMarkus Elfring 	page_list = kmalloc_array(MAX_LCLA_ALLOC_ATTEMPTS,
3415cf80ecf7SMarkus Elfring 				  sizeof(*page_list),
3416508849adSLinus Walleij 				  GFP_KERNEL);
34172c7f2f20SMarkus Elfring 	if (!page_list)
34182c7f2f20SMarkus Elfring 		return -ENOMEM;
3419508849adSLinus Walleij 
3420508849adSLinus Walleij 	/* Calculating how many pages that are required */
3421508849adSLinus Walleij 	base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3422508849adSLinus Walleij 
3423508849adSLinus Walleij 	for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3424508849adSLinus Walleij 		page_list[i] = __get_free_pages(GFP_KERNEL,
3425508849adSLinus Walleij 						base->lcla_pool.pages);
3426508849adSLinus Walleij 		if (!page_list[i]) {
3427508849adSLinus Walleij 
34286db5a8baSRabin Vincent 			d40_err(base->dev, "Failed to allocate %d pages.\n",
34296db5a8baSRabin Vincent 				base->lcla_pool.pages);
343039375334SJulia Lawall 			ret = -ENOMEM;
3431508849adSLinus Walleij 
3432508849adSLinus Walleij 			for (j = 0; j < i; j++)
3433508849adSLinus Walleij 				free_pages(page_list[j], base->lcla_pool.pages);
3434aae32ec6SMarkus Elfring 			goto free_page_list;
3435508849adSLinus Walleij 		}
3436508849adSLinus Walleij 
3437508849adSLinus Walleij 		if ((virt_to_phys((void *)page_list[i]) &
3438508849adSLinus Walleij 		     (LCLA_ALIGNMENT - 1)) == 0)
3439508849adSLinus Walleij 			break;
3440508849adSLinus Walleij 	}
3441508849adSLinus Walleij 
3442508849adSLinus Walleij 	for (j = 0; j < i; j++)
3443508849adSLinus Walleij 		free_pages(page_list[j], base->lcla_pool.pages);
3444508849adSLinus Walleij 
3445508849adSLinus Walleij 	if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3446508849adSLinus Walleij 		base->lcla_pool.base = (void *)page_list[i];
3447508849adSLinus Walleij 	} else {
3448767a9675SJonas Aaberg 		/*
3449767a9675SJonas Aaberg 		 * After many attempts and no succees with finding the correct
3450767a9675SJonas Aaberg 		 * alignment, try with allocating a big buffer.
3451767a9675SJonas Aaberg 		 */
3452508849adSLinus Walleij 		dev_warn(base->dev,
3453508849adSLinus Walleij 			 "[%s] Failed to get %d pages @ 18 bit align.\n",
3454508849adSLinus Walleij 			 __func__, base->lcla_pool.pages);
3455508849adSLinus Walleij 		base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3456508849adSLinus Walleij 							 base->num_phy_chans +
3457508849adSLinus Walleij 							 LCLA_ALIGNMENT,
3458508849adSLinus Walleij 							 GFP_KERNEL);
3459508849adSLinus Walleij 		if (!base->lcla_pool.base_unaligned) {
3460508849adSLinus Walleij 			ret = -ENOMEM;
3461aae32ec6SMarkus Elfring 			goto free_page_list;
3462508849adSLinus Walleij 		}
3463508849adSLinus Walleij 
3464508849adSLinus Walleij 		base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3465508849adSLinus Walleij 						 LCLA_ALIGNMENT);
3466508849adSLinus Walleij 	}
3467508849adSLinus Walleij 
3468026cbc42SRabin Vincent 	pool->dma_addr = dma_map_single(base->dev, pool->base,
3469026cbc42SRabin Vincent 					SZ_1K * base->num_phy_chans,
3470026cbc42SRabin Vincent 					DMA_TO_DEVICE);
3471026cbc42SRabin Vincent 	if (dma_mapping_error(base->dev, pool->dma_addr)) {
3472026cbc42SRabin Vincent 		pool->dma_addr = 0;
3473026cbc42SRabin Vincent 		ret = -ENOMEM;
3474aae32ec6SMarkus Elfring 		goto free_page_list;
3475026cbc42SRabin Vincent 	}
3476026cbc42SRabin Vincent 
3477508849adSLinus Walleij 	writel(virt_to_phys(base->lcla_pool.base),
3478508849adSLinus Walleij 	       base->virtbase + D40_DREG_LCLA);
3479*abac5bacSMarkus Elfring 	ret = 0;
3480aae32ec6SMarkus Elfring  free_page_list:
3481508849adSLinus Walleij 	kfree(page_list);
3482508849adSLinus Walleij 	return ret;
3483508849adSLinus Walleij }
3484508849adSLinus Walleij 
34851814a170SLee Jones static int __init d40_of_probe(struct platform_device *pdev,
34861814a170SLee Jones 			       struct device_node *np)
34871814a170SLee Jones {
34881814a170SLee Jones 	struct stedma40_platform_data *pdata;
3489499c2bc3SLee Jones 	int num_phy = 0, num_memcpy = 0, num_disabled = 0;
3490cbbe13eaSSachin Kamat 	const __be32 *list;
34911814a170SLee Jones 
34921814a170SLee Jones 	pdata = devm_kzalloc(&pdev->dev,
34931814a170SLee Jones 			     sizeof(struct stedma40_platform_data),
34941814a170SLee Jones 			     GFP_KERNEL);
34951814a170SLee Jones 	if (!pdata)
34961814a170SLee Jones 		return -ENOMEM;
34971814a170SLee Jones 
3498fd59f9e6SLee Jones 	/* If absent this value will be obtained from h/w. */
3499fd59f9e6SLee Jones 	of_property_read_u32(np, "dma-channels", &num_phy);
3500fd59f9e6SLee Jones 	if (num_phy > 0)
3501fd59f9e6SLee Jones 		pdata->num_of_phy_chans = num_phy;
3502fd59f9e6SLee Jones 
3503a7dacb68SLee Jones 	list = of_get_property(np, "memcpy-channels", &num_memcpy);
3504a7dacb68SLee Jones 	num_memcpy /= sizeof(*list);
3505a7dacb68SLee Jones 
3506a7dacb68SLee Jones 	if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
3507a7dacb68SLee Jones 		d40_err(&pdev->dev,
3508a7dacb68SLee Jones 			"Invalid number of memcpy channels specified (%d)\n",
3509a7dacb68SLee Jones 			num_memcpy);
3510a7dacb68SLee Jones 		return -EINVAL;
3511a7dacb68SLee Jones 	}
3512a7dacb68SLee Jones 	pdata->num_of_memcpy_chans = num_memcpy;
3513a7dacb68SLee Jones 
3514a7dacb68SLee Jones 	of_property_read_u32_array(np, "memcpy-channels",
3515a7dacb68SLee Jones 				   dma40_memcpy_channels,
3516a7dacb68SLee Jones 				   num_memcpy);
3517a7dacb68SLee Jones 
3518499c2bc3SLee Jones 	list = of_get_property(np, "disabled-channels", &num_disabled);
3519499c2bc3SLee Jones 	num_disabled /= sizeof(*list);
3520499c2bc3SLee Jones 
35215be2190aSDan Carpenter 	if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
3522499c2bc3SLee Jones 		d40_err(&pdev->dev,
3523499c2bc3SLee Jones 			"Invalid number of disabled channels specified (%d)\n",
3524499c2bc3SLee Jones 			num_disabled);
3525499c2bc3SLee Jones 		return -EINVAL;
3526499c2bc3SLee Jones 	}
3527499c2bc3SLee Jones 
3528499c2bc3SLee Jones 	of_property_read_u32_array(np, "disabled-channels",
3529499c2bc3SLee Jones 				   pdata->disabled_channels,
3530499c2bc3SLee Jones 				   num_disabled);
3531499c2bc3SLee Jones 	pdata->disabled_channels[num_disabled] = -1;
3532499c2bc3SLee Jones 
35331814a170SLee Jones 	pdev->dev.platform_data = pdata;
35341814a170SLee Jones 
35351814a170SLee Jones 	return 0;
35361814a170SLee Jones }
35371814a170SLee Jones 
35388d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev)
35398d318a50SLinus Walleij {
3540d4adcc01SJingoo Han 	struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
35411814a170SLee Jones 	struct device_node *np = pdev->dev.of_node;
35428d318a50SLinus Walleij 	int ret = -ENOENT;
3543a9bae06dSMarkus Elfring 	struct d40_base *base;
3544aeb8974aSMarkus Elfring 	struct resource *res;
35458d318a50SLinus Walleij 	int num_reserved_chans;
35468d318a50SLinus Walleij 	u32 val;
35478d318a50SLinus Walleij 
35481814a170SLee Jones 	if (!plat_data) {
35491814a170SLee Jones 		if (np) {
35501814a170SLee Jones 			if (d40_of_probe(pdev, np)) {
35511814a170SLee Jones 				ret = -ENOMEM;
3552a9bae06dSMarkus Elfring 				goto report_failure;
35531814a170SLee Jones 			}
35541814a170SLee Jones 		} else {
35551814a170SLee Jones 			d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
3556a9bae06dSMarkus Elfring 			goto report_failure;
35571814a170SLee Jones 		}
35581814a170SLee Jones 	}
35598d318a50SLinus Walleij 
35601814a170SLee Jones 	base = d40_hw_detect_init(pdev);
35618d318a50SLinus Walleij 	if (!base)
3562a9bae06dSMarkus Elfring 		goto report_failure;
35638d318a50SLinus Walleij 
35648d318a50SLinus Walleij 	num_reserved_chans = d40_phy_res_init(base);
35658d318a50SLinus Walleij 
35668d318a50SLinus Walleij 	platform_set_drvdata(pdev, base);
35678d318a50SLinus Walleij 
35688d318a50SLinus Walleij 	spin_lock_init(&base->interrupt_lock);
35698d318a50SLinus Walleij 	spin_lock_init(&base->execmd_lock);
35708d318a50SLinus Walleij 
35718d318a50SLinus Walleij 	/* Get IO for logical channel parameter address */
35728d318a50SLinus Walleij 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
35738d318a50SLinus Walleij 	if (!res) {
35748d318a50SLinus Walleij 		ret = -ENOENT;
35756db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
35768d318a50SLinus Walleij 		goto failure;
35778d318a50SLinus Walleij 	}
35788d318a50SLinus Walleij 	base->lcpa_size = resource_size(res);
35798d318a50SLinus Walleij 	base->phy_lcpa = res->start;
35808d318a50SLinus Walleij 
35818d318a50SLinus Walleij 	if (request_mem_region(res->start, resource_size(res),
35828d318a50SLinus Walleij 			       D40_NAME " I/O lcpa") == NULL) {
35838d318a50SLinus Walleij 		ret = -EBUSY;
35843a919d5bSFabio Estevam 		d40_err(&pdev->dev, "Failed to request LCPA region %pR\n", res);
35858d318a50SLinus Walleij 		goto failure;
35868d318a50SLinus Walleij 	}
35878d318a50SLinus Walleij 
35888d318a50SLinus Walleij 	/* We make use of ESRAM memory for this. */
35898d318a50SLinus Walleij 	val = readl(base->virtbase + D40_DREG_LCPA);
35908d318a50SLinus Walleij 	if (res->start != val && val != 0) {
35918d318a50SLinus Walleij 		dev_warn(&pdev->dev,
35923a919d5bSFabio Estevam 			 "[%s] Mismatch LCPA dma 0x%x, def %pa\n",
35933a919d5bSFabio Estevam 			 __func__, val, &res->start);
35948d318a50SLinus Walleij 	} else
35958d318a50SLinus Walleij 		writel(res->start, base->virtbase + D40_DREG_LCPA);
35968d318a50SLinus Walleij 
35978d318a50SLinus Walleij 	base->lcpa_base = ioremap(res->start, resource_size(res));
35988d318a50SLinus Walleij 	if (!base->lcpa_base) {
35998d318a50SLinus Walleij 		ret = -ENOMEM;
36006db5a8baSRabin Vincent 		d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
36018d318a50SLinus Walleij 		goto failure;
36028d318a50SLinus Walleij 	}
360328c7a19dSNarayanan G 	/* If lcla has to be located in ESRAM we don't need to allocate */
360428c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
360528c7a19dSNarayanan G 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
360628c7a19dSNarayanan G 							"lcla_esram");
360728c7a19dSNarayanan G 		if (!res) {
360828c7a19dSNarayanan G 			ret = -ENOENT;
360928c7a19dSNarayanan G 			d40_err(&pdev->dev,
361028c7a19dSNarayanan G 				"No \"lcla_esram\" memory resource\n");
361128c7a19dSNarayanan G 			goto failure;
361228c7a19dSNarayanan G 		}
361328c7a19dSNarayanan G 		base->lcla_pool.base = ioremap(res->start,
361428c7a19dSNarayanan G 						resource_size(res));
361528c7a19dSNarayanan G 		if (!base->lcla_pool.base) {
361628c7a19dSNarayanan G 			ret = -ENOMEM;
361728c7a19dSNarayanan G 			d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
361828c7a19dSNarayanan G 			goto failure;
361928c7a19dSNarayanan G 		}
362028c7a19dSNarayanan G 		writel(res->start, base->virtbase + D40_DREG_LCLA);
3621508849adSLinus Walleij 
362228c7a19dSNarayanan G 	} else {
3623508849adSLinus Walleij 		ret = d40_lcla_allocate(base);
3624508849adSLinus Walleij 		if (ret) {
36256db5a8baSRabin Vincent 			d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
36268d318a50SLinus Walleij 			goto failure;
36278d318a50SLinus Walleij 		}
362828c7a19dSNarayanan G 	}
36298d318a50SLinus Walleij 
36308d318a50SLinus Walleij 	spin_lock_init(&base->lcla_pool.lock);
36318d318a50SLinus Walleij 
36328d318a50SLinus Walleij 	base->irq = platform_get_irq(pdev, 0);
36338d318a50SLinus Walleij 
36348d318a50SLinus Walleij 	ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
36358d318a50SLinus Walleij 	if (ret) {
36366db5a8baSRabin Vincent 		d40_err(&pdev->dev, "No IRQ defined\n");
36378d318a50SLinus Walleij 		goto failure;
36388d318a50SLinus Walleij 	}
36398d318a50SLinus Walleij 
364028c7a19dSNarayanan G 	if (base->plat_data->use_esram_lcla) {
364128c7a19dSNarayanan G 
364228c7a19dSNarayanan G 		base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
364328c7a19dSNarayanan G 		if (IS_ERR(base->lcpa_regulator)) {
364428c7a19dSNarayanan G 			d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
36458581bbcdSWei Yongjun 			ret = PTR_ERR(base->lcpa_regulator);
364628c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
364728c7a19dSNarayanan G 			goto failure;
364828c7a19dSNarayanan G 		}
364928c7a19dSNarayanan G 
365028c7a19dSNarayanan G 		ret = regulator_enable(base->lcpa_regulator);
365128c7a19dSNarayanan G 		if (ret) {
365228c7a19dSNarayanan G 			d40_err(&pdev->dev,
365328c7a19dSNarayanan G 				"Failed to enable lcpa_regulator\n");
365428c7a19dSNarayanan G 			regulator_put(base->lcpa_regulator);
365528c7a19dSNarayanan G 			base->lcpa_regulator = NULL;
365628c7a19dSNarayanan G 			goto failure;
365728c7a19dSNarayanan G 		}
365828c7a19dSNarayanan G 	}
365928c7a19dSNarayanan G 
36602dafca17SUlf Hansson 	writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
36612dafca17SUlf Hansson 
36622dafca17SUlf Hansson 	pm_runtime_irq_safe(base->dev);
36632dafca17SUlf Hansson 	pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
36642dafca17SUlf Hansson 	pm_runtime_use_autosuspend(base->dev);
36652dafca17SUlf Hansson 	pm_runtime_mark_last_busy(base->dev);
36662dafca17SUlf Hansson 	pm_runtime_set_active(base->dev);
36672dafca17SUlf Hansson 	pm_runtime_enable(base->dev);
36682dafca17SUlf Hansson 
36698581bbcdSWei Yongjun 	ret = d40_dmaengine_init(base, num_reserved_chans);
36708581bbcdSWei Yongjun 	if (ret)
36718d318a50SLinus Walleij 		goto failure;
36728d318a50SLinus Walleij 
3673b96710e5SPer Forlin 	base->dev->dma_parms = &base->dma_parms;
36748581bbcdSWei Yongjun 	ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
36758581bbcdSWei Yongjun 	if (ret) {
3676b96710e5SPer Forlin 		d40_err(&pdev->dev, "Failed to set dma max seg size\n");
3677b96710e5SPer Forlin 		goto failure;
3678b96710e5SPer Forlin 	}
3679b96710e5SPer Forlin 
36808d318a50SLinus Walleij 	d40_hw_init(base);
36818d318a50SLinus Walleij 
3682fa332de5SLee Jones 	if (np) {
36838581bbcdSWei Yongjun 		ret = of_dma_controller_register(np, d40_xlate, NULL);
36848581bbcdSWei Yongjun 		if (ret)
3685fa332de5SLee Jones 			dev_err(&pdev->dev,
3686fa332de5SLee Jones 				"could not register of_dma_controller\n");
3687fa332de5SLee Jones 	}
3688fa332de5SLee Jones 
36898d318a50SLinus Walleij 	dev_info(base->dev, "initialized\n");
36908d318a50SLinus Walleij 	return 0;
36918d318a50SLinus Walleij 
36928d318a50SLinus Walleij failure:
3693c675b1b4SJonas Aaberg 	kmem_cache_destroy(base->desc_slab);
36948d318a50SLinus Walleij 	if (base->virtbase)
36958d318a50SLinus Walleij 		iounmap(base->virtbase);
3696026cbc42SRabin Vincent 
369728c7a19dSNarayanan G 	if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
369828c7a19dSNarayanan G 		iounmap(base->lcla_pool.base);
369928c7a19dSNarayanan G 		base->lcla_pool.base = NULL;
370028c7a19dSNarayanan G 	}
370128c7a19dSNarayanan G 
3702026cbc42SRabin Vincent 	if (base->lcla_pool.dma_addr)
3703026cbc42SRabin Vincent 		dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3704026cbc42SRabin Vincent 				 SZ_1K * base->num_phy_chans,
3705026cbc42SRabin Vincent 				 DMA_TO_DEVICE);
3706026cbc42SRabin Vincent 
3707508849adSLinus Walleij 	if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3708508849adSLinus Walleij 		free_pages((unsigned long)base->lcla_pool.base,
3709508849adSLinus Walleij 			   base->lcla_pool.pages);
3710767a9675SJonas Aaberg 
3711508849adSLinus Walleij 	kfree(base->lcla_pool.base_unaligned);
3712767a9675SJonas Aaberg 
37138d318a50SLinus Walleij 	if (base->phy_lcpa)
37148d318a50SLinus Walleij 		release_mem_region(base->phy_lcpa,
37158d318a50SLinus Walleij 				   base->lcpa_size);
37168d318a50SLinus Walleij 	if (base->phy_start)
37178d318a50SLinus Walleij 		release_mem_region(base->phy_start,
37188d318a50SLinus Walleij 				   base->phy_size);
37198d318a50SLinus Walleij 	if (base->clk) {
3720da2ac56aSFabio Baltieri 		clk_disable_unprepare(base->clk);
37218d318a50SLinus Walleij 		clk_put(base->clk);
37228d318a50SLinus Walleij 	}
37238d318a50SLinus Walleij 
372428c7a19dSNarayanan G 	if (base->lcpa_regulator) {
372528c7a19dSNarayanan G 		regulator_disable(base->lcpa_regulator);
372628c7a19dSNarayanan G 		regulator_put(base->lcpa_regulator);
372728c7a19dSNarayanan G 	}
372828c7a19dSNarayanan G 
37298d318a50SLinus Walleij 	kfree(base->lcla_pool.alloc_map);
37308d318a50SLinus Walleij 	kfree(base->lookup_log_chans);
37318d318a50SLinus Walleij 	kfree(base->lookup_phy_chans);
37328d318a50SLinus Walleij 	kfree(base->phy_res);
37338d318a50SLinus Walleij 	kfree(base);
3734a9bae06dSMarkus Elfring report_failure:
37356db5a8baSRabin Vincent 	d40_err(&pdev->dev, "probe failed\n");
37368d318a50SLinus Walleij 	return ret;
37378d318a50SLinus Walleij }
37388d318a50SLinus Walleij 
37391814a170SLee Jones static const struct of_device_id d40_match[] = {
37401814a170SLee Jones         { .compatible = "stericsson,dma40", },
37411814a170SLee Jones         {}
37421814a170SLee Jones };
37431814a170SLee Jones 
37448d318a50SLinus Walleij static struct platform_driver d40_driver = {
37458d318a50SLinus Walleij 	.driver = {
37468d318a50SLinus Walleij 		.name  = D40_NAME,
3747123e4ca1SUlf Hansson 		.pm = &dma40_pm_ops,
37481814a170SLee Jones 		.of_match_table = d40_match,
37498d318a50SLinus Walleij 	},
37508d318a50SLinus Walleij };
37518d318a50SLinus Walleij 
3752cb9ab2d8SRabin Vincent static int __init stedma40_init(void)
37538d318a50SLinus Walleij {
37548d318a50SLinus Walleij 	return platform_driver_probe(&d40_driver, d40_probe);
37558d318a50SLinus Walleij }
3756a0eb221aSLinus Walleij subsys_initcall(stedma40_init);
3757