1af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28d318a50SLinus Walleij /*
3d49278e3SPer Forlin * Copyright (C) Ericsson AB 2007-2008
4d49278e3SPer Forlin * Copyright (C) ST-Ericsson SA 2008-2010
5661385f9SPer Forlin * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
6767a9675SJonas Aaberg * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
78d318a50SLinus Walleij */
88d318a50SLinus Walleij
9b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h>
108d318a50SLinus Walleij #include <linux/kernel.h>
118d318a50SLinus Walleij #include <linux/slab.h>
12f492b210SPaul Gortmaker #include <linux/export.h>
138d318a50SLinus Walleij #include <linux/dmaengine.h>
148d318a50SLinus Walleij #include <linux/platform_device.h>
158d318a50SLinus Walleij #include <linux/clk.h>
168d318a50SLinus Walleij #include <linux/delay.h>
17c95905a6SGuennadi Liakhovetski #include <linux/log2.h>
187fb3e75eSNarayanan G #include <linux/pm.h>
197fb3e75eSNarayanan G #include <linux/pm_runtime.h>
20698e4732SJonas Aaberg #include <linux/err.h>
211814a170SLee Jones #include <linux/of.h>
225a1a3b9cSLinus Walleij #include <linux/of_address.h>
23fa332de5SLee Jones #include <linux/of_dma.h>
24f4b89764SLinus Walleij #include <linux/amba/bus.h>
2515e4b78dSLinus Walleij #include <linux/regulator/consumer.h>
268d318a50SLinus Walleij
27d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
2842ae6f16SLinus Walleij #include "ste_dma40.h"
298d318a50SLinus Walleij #include "ste_dma40_ll.h"
308d318a50SLinus Walleij
3142ae6f16SLinus Walleij /**
3242ae6f16SLinus Walleij * struct stedma40_platform_data - Configuration struct for the dma device.
3342ae6f16SLinus Walleij *
3442ae6f16SLinus Walleij * @disabled_channels: A vector, ending with -1, that marks physical channels
3542ae6f16SLinus Walleij * that are for different reasons not available for the driver.
3642ae6f16SLinus Walleij * @soft_lli_chans: A vector, that marks physical channels will use LLI by SW
3742ae6f16SLinus Walleij * which avoids HW bug that exists in some versions of the controller.
3871a5197eSRandy Dunlap * SoftLLI introduces relink overhead that could impact performance for
3942ae6f16SLinus Walleij * certain use cases.
4042ae6f16SLinus Walleij * @num_of_soft_lli_chans: The number of channels that needs to be configured
4142ae6f16SLinus Walleij * to use SoftLLI.
4242ae6f16SLinus Walleij * @use_esram_lcla: flag for mapping the lcla into esram region
4342ae6f16SLinus Walleij * @num_of_memcpy_chans: The number of channels reserved for memcpy.
4442ae6f16SLinus Walleij * @num_of_phy_chans: The number of physical channels implemented in HW.
4542ae6f16SLinus Walleij * 0 means reading the number of channels from DMA HW but this is only valid
4642ae6f16SLinus Walleij * for 'multiple of 4' channels, like 8.
4742ae6f16SLinus Walleij */
4842ae6f16SLinus Walleij struct stedma40_platform_data {
4942ae6f16SLinus Walleij int disabled_channels[STEDMA40_MAX_PHYS];
5042ae6f16SLinus Walleij int *soft_lli_chans;
5142ae6f16SLinus Walleij int num_of_soft_lli_chans;
5242ae6f16SLinus Walleij bool use_esram_lcla;
5342ae6f16SLinus Walleij int num_of_memcpy_chans;
5442ae6f16SLinus Walleij int num_of_phy_chans;
5542ae6f16SLinus Walleij };
5642ae6f16SLinus Walleij
578d318a50SLinus Walleij #define D40_NAME "dma40"
588d318a50SLinus Walleij
598d318a50SLinus Walleij #define D40_PHY_CHAN -1
608d318a50SLinus Walleij
618d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */
628d318a50SLinus Walleij #define D40_CHAN_POS(chan) (2 * (chan / 2))
638d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
648d318a50SLinus Walleij
658d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */
668d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500
678d318a50SLinus Walleij
687fb3e75eSNarayanan G /* Milliseconds */
697fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY 100
707fb3e75eSNarayanan G
71508849adSLinus Walleij /* Hardware requirement on LCLA alignment */
72508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000
73698e4732SJonas Aaberg
74698e4732SJonas Aaberg /* Max number of links per event group */
75698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128
76698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
77698e4732SJonas Aaberg
78db72da92SLee Jones /* Max number of logical channels per physical channel */
79db72da92SLee Jones #define D40_MAX_LOG_CHAN_PER_PHY 32
80db72da92SLee Jones
81508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */
82508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256
83508849adSLinus Walleij
84508849adSLinus Walleij /* Bit markings for allocation map */
858a3b6e14SLee Jones #define D40_ALLOC_FREE BIT(31)
868a3b6e14SLee Jones #define D40_ALLOC_PHY BIT(30)
878d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE 0
888d318a50SLinus Walleij
89a7dacb68SLee Jones #define D40_MEMCPY_MAX_CHANS 8
90a7dacb68SLee Jones
91664a57ecSLee Jones /* Reserved event lines for memcpy only. */
92a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_0 51
93a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_1 56
94a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_2 57
95a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_3 58
96a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_4 59
97a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_5 60
98a2acaa21SLinus Walleij
99a2acaa21SLinus Walleij static int dma40_memcpy_channels[] = {
100a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_0,
101a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_1,
102a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_2,
103a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_3,
104a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_4,
105a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_5,
106a2acaa21SLinus Walleij };
107664a57ecSLee Jones
108c281cde7SBjorn Helgaas /* Default configuration for physical memcpy */
109e43341caSBhumika Goyal static const struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
11029027a1eSLee Jones .mode = STEDMA40_MODE_PHYSICAL,
1112c2b62d5SLee Jones .dir = DMA_MEM_TO_MEM,
11229027a1eSLee Jones
11343f2e1a3SLee Jones .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
11429027a1eSLee Jones .src_info.psize = STEDMA40_PSIZE_PHY_1,
11529027a1eSLee Jones .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
11629027a1eSLee Jones
11743f2e1a3SLee Jones .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
11829027a1eSLee Jones .dst_info.psize = STEDMA40_PSIZE_PHY_1,
11929027a1eSLee Jones .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
12029027a1eSLee Jones };
12129027a1eSLee Jones
12229027a1eSLee Jones /* Default configuration for logical memcpy */
123e43341caSBhumika Goyal static const struct stedma40_chan_cfg dma40_memcpy_conf_log = {
12429027a1eSLee Jones .mode = STEDMA40_MODE_LOGICAL,
1252c2b62d5SLee Jones .dir = DMA_MEM_TO_MEM,
12629027a1eSLee Jones
12743f2e1a3SLee Jones .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
12829027a1eSLee Jones .src_info.psize = STEDMA40_PSIZE_LOG_1,
12929027a1eSLee Jones .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
13029027a1eSLee Jones
13143f2e1a3SLee Jones .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
13229027a1eSLee Jones .dst_info.psize = STEDMA40_PSIZE_LOG_1,
13329027a1eSLee Jones .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
13429027a1eSLee Jones };
13529027a1eSLee Jones
1368d318a50SLinus Walleij /**
137401f022cSVinod Koul * enum d40_command - The different commands and/or statuses.
1388d318a50SLinus Walleij *
1398d318a50SLinus Walleij * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
1408d318a50SLinus Walleij * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
1418d318a50SLinus Walleij * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
1428d318a50SLinus Walleij * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
1438d318a50SLinus Walleij */
1448d318a50SLinus Walleij enum d40_command {
1458d318a50SLinus Walleij D40_DMA_STOP = 0,
1468d318a50SLinus Walleij D40_DMA_RUN = 1,
1478d318a50SLinus Walleij D40_DMA_SUSPEND_REQ = 2,
1488d318a50SLinus Walleij D40_DMA_SUSPENDED = 3
1498d318a50SLinus Walleij };
1508d318a50SLinus Walleij
1517fb3e75eSNarayanan G /*
1521bdae6f4SNarayanan G * enum d40_events - The different Event Enables for the event lines.
1531bdae6f4SNarayanan G *
1541bdae6f4SNarayanan G * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
1551bdae6f4SNarayanan G * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
1561bdae6f4SNarayanan G * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
1571bdae6f4SNarayanan G * @D40_ROUND_EVENTLINE: Status check for event line.
1581bdae6f4SNarayanan G */
1591bdae6f4SNarayanan G
1601bdae6f4SNarayanan G enum d40_events {
1611bdae6f4SNarayanan G D40_DEACTIVATE_EVENTLINE = 0,
1621bdae6f4SNarayanan G D40_ACTIVATE_EVENTLINE = 1,
1631bdae6f4SNarayanan G D40_SUSPEND_REQ_EVENTLINE = 2,
1641bdae6f4SNarayanan G D40_ROUND_EVENTLINE = 3
1651bdae6f4SNarayanan G };
1661bdae6f4SNarayanan G
1671bdae6f4SNarayanan G /*
1687fb3e75eSNarayanan G * These are the registers that has to be saved and later restored
1697fb3e75eSNarayanan G * when the DMA hw is powered off.
1707fb3e75eSNarayanan G * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
1717fb3e75eSNarayanan G */
1725d6fb560SArnd Bergmann static __maybe_unused u32 d40_backup_regs[] = {
1737fb3e75eSNarayanan G D40_DREG_LCPA,
1747fb3e75eSNarayanan G D40_DREG_LCLA,
1757fb3e75eSNarayanan G D40_DREG_PRMSE,
1767fb3e75eSNarayanan G D40_DREG_PRMSO,
1777fb3e75eSNarayanan G D40_DREG_PRMOE,
1787fb3e75eSNarayanan G D40_DREG_PRMOO,
1797fb3e75eSNarayanan G };
1807fb3e75eSNarayanan G
1817fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
1827fb3e75eSNarayanan G
1833cb645dcSTong Liu /*
1843cb645dcSTong Liu * since 9540 and 8540 has the same HW revision
18571a5197eSRandy Dunlap * use v4a for 9540 or earlier
1863cb645dcSTong Liu * use v4b for 8540 or later
1873cb645dcSTong Liu * HW revision:
1883cb645dcSTong Liu * DB8500ed has revision 0
1893cb645dcSTong Liu * DB8500v1 has revision 2
1903cb645dcSTong Liu * DB8500v2 has revision 3
1913cb645dcSTong Liu * AP9540v1 has revision 4
1923cb645dcSTong Liu * DB8540v1 has revision 4
1933cb645dcSTong Liu * TODO: Check if all these registers have to be saved/restored on dma40 v4a
1943cb645dcSTong Liu */
1953cb645dcSTong Liu static u32 d40_backup_regs_v4a[] = {
1967fb3e75eSNarayanan G D40_DREG_PSEG1,
1977fb3e75eSNarayanan G D40_DREG_PSEG2,
1987fb3e75eSNarayanan G D40_DREG_PSEG3,
1997fb3e75eSNarayanan G D40_DREG_PSEG4,
2007fb3e75eSNarayanan G D40_DREG_PCEG1,
2017fb3e75eSNarayanan G D40_DREG_PCEG2,
2027fb3e75eSNarayanan G D40_DREG_PCEG3,
2037fb3e75eSNarayanan G D40_DREG_PCEG4,
2047fb3e75eSNarayanan G D40_DREG_RSEG1,
2057fb3e75eSNarayanan G D40_DREG_RSEG2,
2067fb3e75eSNarayanan G D40_DREG_RSEG3,
2077fb3e75eSNarayanan G D40_DREG_RSEG4,
2087fb3e75eSNarayanan G D40_DREG_RCEG1,
2097fb3e75eSNarayanan G D40_DREG_RCEG2,
2107fb3e75eSNarayanan G D40_DREG_RCEG3,
2117fb3e75eSNarayanan G D40_DREG_RCEG4,
2127fb3e75eSNarayanan G };
2137fb3e75eSNarayanan G
2143cb645dcSTong Liu #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
2153cb645dcSTong Liu
2163cb645dcSTong Liu static u32 d40_backup_regs_v4b[] = {
2173cb645dcSTong Liu D40_DREG_CPSEG1,
2183cb645dcSTong Liu D40_DREG_CPSEG2,
2193cb645dcSTong Liu D40_DREG_CPSEG3,
2203cb645dcSTong Liu D40_DREG_CPSEG4,
2213cb645dcSTong Liu D40_DREG_CPSEG5,
2223cb645dcSTong Liu D40_DREG_CPCEG1,
2233cb645dcSTong Liu D40_DREG_CPCEG2,
2243cb645dcSTong Liu D40_DREG_CPCEG3,
2253cb645dcSTong Liu D40_DREG_CPCEG4,
2263cb645dcSTong Liu D40_DREG_CPCEG5,
2273cb645dcSTong Liu D40_DREG_CRSEG1,
2283cb645dcSTong Liu D40_DREG_CRSEG2,
2293cb645dcSTong Liu D40_DREG_CRSEG3,
2303cb645dcSTong Liu D40_DREG_CRSEG4,
2313cb645dcSTong Liu D40_DREG_CRSEG5,
2323cb645dcSTong Liu D40_DREG_CRCEG1,
2333cb645dcSTong Liu D40_DREG_CRCEG2,
2343cb645dcSTong Liu D40_DREG_CRCEG3,
2353cb645dcSTong Liu D40_DREG_CRCEG4,
2363cb645dcSTong Liu D40_DREG_CRCEG5,
2373cb645dcSTong Liu };
2383cb645dcSTong Liu
2393cb645dcSTong Liu #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
2407fb3e75eSNarayanan G
2415d6fb560SArnd Bergmann static __maybe_unused u32 d40_backup_regs_chan[] = {
2427fb3e75eSNarayanan G D40_CHAN_REG_SSCFG,
2437fb3e75eSNarayanan G D40_CHAN_REG_SSELT,
2447fb3e75eSNarayanan G D40_CHAN_REG_SSPTR,
2457fb3e75eSNarayanan G D40_CHAN_REG_SSLNK,
2467fb3e75eSNarayanan G D40_CHAN_REG_SDCFG,
2477fb3e75eSNarayanan G D40_CHAN_REG_SDELT,
2487fb3e75eSNarayanan G D40_CHAN_REG_SDPTR,
2497fb3e75eSNarayanan G D40_CHAN_REG_SDLNK,
2507fb3e75eSNarayanan G };
2517fb3e75eSNarayanan G
25284b3da14SLee Jones #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
25384b3da14SLee Jones BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
25484b3da14SLee Jones
2558d318a50SLinus Walleij /**
2563cb645dcSTong Liu * struct d40_interrupt_lookup - lookup table for interrupt handler
2573cb645dcSTong Liu *
2583cb645dcSTong Liu * @src: Interrupt mask register.
2593cb645dcSTong Liu * @clr: Interrupt clear register.
2603cb645dcSTong Liu * @is_error: true if this is an error interrupt.
2613cb645dcSTong Liu * @offset: start delta in the lookup_log_chans in d40_base. If equals to
2623cb645dcSTong Liu * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
2633cb645dcSTong Liu */
2643cb645dcSTong Liu struct d40_interrupt_lookup {
2653cb645dcSTong Liu u32 src;
2663cb645dcSTong Liu u32 clr;
2673cb645dcSTong Liu bool is_error;
2683cb645dcSTong Liu int offset;
2693cb645dcSTong Liu };
2703cb645dcSTong Liu
2713cb645dcSTong Liu
2723cb645dcSTong Liu static struct d40_interrupt_lookup il_v4a[] = {
2733cb645dcSTong Liu {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
2743cb645dcSTong Liu {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
2753cb645dcSTong Liu {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
2763cb645dcSTong Liu {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
2773cb645dcSTong Liu {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
2783cb645dcSTong Liu {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
2793cb645dcSTong Liu {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
2803cb645dcSTong Liu {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
2813cb645dcSTong Liu {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
2823cb645dcSTong Liu {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
2833cb645dcSTong Liu };
2843cb645dcSTong Liu
2853cb645dcSTong Liu static struct d40_interrupt_lookup il_v4b[] = {
2863cb645dcSTong Liu {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
2873cb645dcSTong Liu {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
2883cb645dcSTong Liu {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
2893cb645dcSTong Liu {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
2903cb645dcSTong Liu {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
2913cb645dcSTong Liu {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
2923cb645dcSTong Liu {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
2933cb645dcSTong Liu {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
2943cb645dcSTong Liu {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
2953cb645dcSTong Liu {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
2963cb645dcSTong Liu {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
2973cb645dcSTong Liu {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
2983cb645dcSTong Liu };
2993cb645dcSTong Liu
3003cb645dcSTong Liu /**
3013cb645dcSTong Liu * struct d40_reg_val - simple lookup struct
3023cb645dcSTong Liu *
3033cb645dcSTong Liu * @reg: The register.
3043cb645dcSTong Liu * @val: The value that belongs to the register in reg.
3053cb645dcSTong Liu */
3063cb645dcSTong Liu struct d40_reg_val {
3073cb645dcSTong Liu unsigned int reg;
3083cb645dcSTong Liu unsigned int val;
3093cb645dcSTong Liu };
3103cb645dcSTong Liu
3113cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
3123cb645dcSTong Liu /* Clock every part of the DMA block from start */
3133cb645dcSTong Liu { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
3143cb645dcSTong Liu
3153cb645dcSTong Liu /* Interrupts on all logical channels */
3163cb645dcSTong Liu { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
3173cb645dcSTong Liu { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
3183cb645dcSTong Liu { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
3193cb645dcSTong Liu { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
3203cb645dcSTong Liu { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
3213cb645dcSTong Liu { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
3223cb645dcSTong Liu { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
3233cb645dcSTong Liu { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
3243cb645dcSTong Liu { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
3253cb645dcSTong Liu { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
3263cb645dcSTong Liu { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
3273cb645dcSTong Liu { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
3283cb645dcSTong Liu };
3293cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
3303cb645dcSTong Liu /* Clock every part of the DMA block from start */
3313cb645dcSTong Liu { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
3323cb645dcSTong Liu
3333cb645dcSTong Liu /* Interrupts on all logical channels */
3343cb645dcSTong Liu { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
3353cb645dcSTong Liu { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
3363cb645dcSTong Liu { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
3373cb645dcSTong Liu { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
3383cb645dcSTong Liu { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
3393cb645dcSTong Liu { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
3403cb645dcSTong Liu { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
3413cb645dcSTong Liu { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
3423cb645dcSTong Liu { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
3433cb645dcSTong Liu { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
3443cb645dcSTong Liu { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
3453cb645dcSTong Liu { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
3463cb645dcSTong Liu { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
3473cb645dcSTong Liu { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
3483cb645dcSTong Liu { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
3493cb645dcSTong Liu };
3503cb645dcSTong Liu
3513cb645dcSTong Liu /**
3528d318a50SLinus Walleij * struct d40_lli_pool - Structure for keeping LLIs in memory
3538d318a50SLinus Walleij *
3548d318a50SLinus Walleij * @base: Pointer to memory area when the pre_alloc_lli's are not large
3558d318a50SLinus Walleij * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
3568d318a50SLinus Walleij * pre_alloc_lli is used.
357b00f938cSRabin Vincent * @dma_addr: DMA address, if mapped
3588d318a50SLinus Walleij * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
3598d318a50SLinus Walleij * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
3608d318a50SLinus Walleij * one buffer to one buffer.
3618d318a50SLinus Walleij */
3628d318a50SLinus Walleij struct d40_lli_pool {
3638d318a50SLinus Walleij void *base;
3648d318a50SLinus Walleij int size;
365b00f938cSRabin Vincent dma_addr_t dma_addr;
3668d318a50SLinus Walleij /* Space for dst and src, plus an extra for padding */
3678d318a50SLinus Walleij u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
3688d318a50SLinus Walleij };
3698d318a50SLinus Walleij
3708d318a50SLinus Walleij /**
3718d318a50SLinus Walleij * struct d40_desc - A descriptor is one DMA job.
3728d318a50SLinus Walleij *
3738d318a50SLinus Walleij * @lli_phy: LLI settings for physical channel. Both src and dst=
3748d318a50SLinus Walleij * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
3758d318a50SLinus Walleij * lli_len equals one.
3768d318a50SLinus Walleij * @lli_log: Same as above but for logical channels.
3778d318a50SLinus Walleij * @lli_pool: The pool with two entries pre-allocated.
378941b77a3SPer Friden * @lli_len: Number of llis of current descriptor.
37925985edcSLucas De Marchi * @lli_current: Number of transferred llis.
380698e4732SJonas Aaberg * @lcla_alloc: Number of LCLA entries allocated.
3818d318a50SLinus Walleij * @txd: DMA engine struct. Used for among other things for communication
3828d318a50SLinus Walleij * during a transfer.
3838d318a50SLinus Walleij * @node: List entry.
3848d318a50SLinus Walleij * @is_in_client_list: true if the client owns this descriptor.
3857fb3e75eSNarayanan G * @cyclic: true if this is a cyclic job
3868d318a50SLinus Walleij *
3878d318a50SLinus Walleij * This descriptor is used for both logical and physical transfers.
3888d318a50SLinus Walleij */
3898d318a50SLinus Walleij struct d40_desc {
3908d318a50SLinus Walleij /* LLI physical */
3918d318a50SLinus Walleij struct d40_phy_lli_bidir lli_phy;
3928d318a50SLinus Walleij /* LLI logical */
3938d318a50SLinus Walleij struct d40_log_lli_bidir lli_log;
3948d318a50SLinus Walleij
3958d318a50SLinus Walleij struct d40_lli_pool lli_pool;
396941b77a3SPer Friden int lli_len;
397698e4732SJonas Aaberg int lli_current;
398698e4732SJonas Aaberg int lcla_alloc;
3998d318a50SLinus Walleij
4008d318a50SLinus Walleij struct dma_async_tx_descriptor txd;
4018d318a50SLinus Walleij struct list_head node;
4028d318a50SLinus Walleij
4038d318a50SLinus Walleij bool is_in_client_list;
4040c842b55SRabin Vincent bool cyclic;
4058d318a50SLinus Walleij };
4068d318a50SLinus Walleij
4078d318a50SLinus Walleij /**
4088d318a50SLinus Walleij * struct d40_lcla_pool - LCLA pool settings and data.
4098d318a50SLinus Walleij *
410508849adSLinus Walleij * @base: The virtual address of LCLA. 18 bit aligned.
4110b851134SLee Jones * @dma_addr: DMA address, if mapped
41271a5197eSRandy Dunlap * @base_unaligned: The original kmalloc pointer, if kmalloc is used.
413508849adSLinus Walleij * This pointer is only there for clean-up on error.
414508849adSLinus Walleij * @pages: The number of pages needed for all physical channels.
415508849adSLinus Walleij * Only used later for clean-up on error
4168d318a50SLinus Walleij * @lock: Lock to protect the content in this struct.
417698e4732SJonas Aaberg * @alloc_map: big map over which LCLA entry is own by which job.
4188d318a50SLinus Walleij */
4198d318a50SLinus Walleij struct d40_lcla_pool {
4208d318a50SLinus Walleij void *base;
421026cbc42SRabin Vincent dma_addr_t dma_addr;
422508849adSLinus Walleij void *base_unaligned;
423508849adSLinus Walleij int pages;
4248d318a50SLinus Walleij spinlock_t lock;
425698e4732SJonas Aaberg struct d40_desc **alloc_map;
4268d318a50SLinus Walleij };
4278d318a50SLinus Walleij
4288d318a50SLinus Walleij /**
4298d318a50SLinus Walleij * struct d40_phy_res - struct for handling eventlines mapped to physical
4308d318a50SLinus Walleij * channels.
4318d318a50SLinus Walleij *
4328d318a50SLinus Walleij * @lock: A lock protection this entity.
4337fb3e75eSNarayanan G * @reserved: True if used by secure world or otherwise.
4348d318a50SLinus Walleij * @num: The physical channel number of this entity.
4358d318a50SLinus Walleij * @allocated_src: Bit mapped to show which src event line's are mapped to
4368d318a50SLinus Walleij * this physical channel. Can also be free or physically allocated.
4378d318a50SLinus Walleij * @allocated_dst: Same as for src but is dst.
4388d318a50SLinus Walleij * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
439767a9675SJonas Aaberg * event line number.
4407407048bSFabio Baltieri * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
4418d318a50SLinus Walleij */
4428d318a50SLinus Walleij struct d40_phy_res {
4438d318a50SLinus Walleij spinlock_t lock;
4447fb3e75eSNarayanan G bool reserved;
4458d318a50SLinus Walleij int num;
4468d318a50SLinus Walleij u32 allocated_src;
4478d318a50SLinus Walleij u32 allocated_dst;
4487407048bSFabio Baltieri bool use_soft_lli;
4498d318a50SLinus Walleij };
4508d318a50SLinus Walleij
4518d318a50SLinus Walleij struct d40_base;
4528d318a50SLinus Walleij
4538d318a50SLinus Walleij /**
4548d318a50SLinus Walleij * struct d40_chan - Struct that describes a channel.
4558d318a50SLinus Walleij *
4568d318a50SLinus Walleij * @lock: A spinlock to protect this struct.
4578d318a50SLinus Walleij * @log_num: The logical number, if any of this channel.
4588d318a50SLinus Walleij * @pending_tx: The number of pending transfers. Used between interrupt handler
4598d318a50SLinus Walleij * and tasklet.
4608d318a50SLinus Walleij * @busy: Set to true when transfer is ongoing on this channel.
4612a614340SJonas Aaberg * @phy_chan: Pointer to physical channel which this instance runs on. If this
4622a614340SJonas Aaberg * point is NULL, then the channel is not allocated.
4638d318a50SLinus Walleij * @chan: DMA engine handle.
4648d318a50SLinus Walleij * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
4658d318a50SLinus Walleij * transfer and call client callback.
4668d318a50SLinus Walleij * @client: Cliented owned descriptor list.
467da063d26SPer Forlin * @pending_queue: Submitted jobs, to be issued by issue_pending()
4688d318a50SLinus Walleij * @active: Active descriptor.
4694226dd86SFabio Baltieri * @done: Completed jobs
4708d318a50SLinus Walleij * @queue: Queued jobs.
47182babbb3SPer Forlin * @prepare_queue: Prepared jobs.
4728d318a50SLinus Walleij * @dma_cfg: The client configuration of this dma channel.
4739e314ef3SVinod Koul * @slave_config: DMA slave configuration.
474ce2ca125SRabin Vincent * @configured: whether the dma_cfg configuration is valid
4758d318a50SLinus Walleij * @base: Pointer to the device instance struct.
4768d318a50SLinus Walleij * @src_def_cfg: Default cfg register setting for src.
4778d318a50SLinus Walleij * @dst_def_cfg: Default cfg register setting for dst.
4788d318a50SLinus Walleij * @log_def: Default logical channel settings.
4798d318a50SLinus Walleij * @lcpa: Pointer to dst and src lcpa settings.
480ae752bf4Som prakash * @runtime_addr: runtime configured address.
481ae752bf4Som prakash * @runtime_direction: runtime configured direction.
4828d318a50SLinus Walleij *
4838d318a50SLinus Walleij * This struct can either "be" a logical or a physical channel.
4848d318a50SLinus Walleij */
4858d318a50SLinus Walleij struct d40_chan {
4868d318a50SLinus Walleij spinlock_t lock;
4878d318a50SLinus Walleij int log_num;
4888d318a50SLinus Walleij int pending_tx;
4898d318a50SLinus Walleij bool busy;
4908d318a50SLinus Walleij struct d40_phy_res *phy_chan;
4918d318a50SLinus Walleij struct dma_chan chan;
4928d318a50SLinus Walleij struct tasklet_struct tasklet;
4938d318a50SLinus Walleij struct list_head client;
494a8f3067bSPer Forlin struct list_head pending_queue;
4958d318a50SLinus Walleij struct list_head active;
4964226dd86SFabio Baltieri struct list_head done;
4978d318a50SLinus Walleij struct list_head queue;
49882babbb3SPer Forlin struct list_head prepare_queue;
4998d318a50SLinus Walleij struct stedma40_chan_cfg dma_cfg;
5009e314ef3SVinod Koul struct dma_slave_config slave_config;
501ce2ca125SRabin Vincent bool configured;
5028d318a50SLinus Walleij struct d40_base *base;
5038d318a50SLinus Walleij /* Default register configurations */
5048d318a50SLinus Walleij u32 src_def_cfg;
5058d318a50SLinus Walleij u32 dst_def_cfg;
5068d318a50SLinus Walleij struct d40_def_lcsp log_def;
5078d318a50SLinus Walleij struct d40_log_lli_full *lcpa;
50895e1400fSLinus Walleij /* Runtime reconfiguration */
50995e1400fSLinus Walleij dma_addr_t runtime_addr;
510db8196dfSVinod Koul enum dma_transfer_direction runtime_direction;
5118d318a50SLinus Walleij };
5128d318a50SLinus Walleij
5138d318a50SLinus Walleij /**
5143cb645dcSTong Liu * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
5153cb645dcSTong Liu * controller
5163cb645dcSTong Liu *
5173cb645dcSTong Liu * @backup: the pointer to the registers address array for backup
5183cb645dcSTong Liu * @backup_size: the size of the registers address array for backup
5193cb645dcSTong Liu * @realtime_en: the realtime enable register
5203cb645dcSTong Liu * @realtime_clear: the realtime clear register
5213cb645dcSTong Liu * @high_prio_en: the high priority enable register
5223cb645dcSTong Liu * @high_prio_clear: the high priority clear register
5233cb645dcSTong Liu * @interrupt_en: the interrupt enable register
5243cb645dcSTong Liu * @interrupt_clear: the interrupt clear register
5253cb645dcSTong Liu * @il: the pointer to struct d40_interrupt_lookup
5263cb645dcSTong Liu * @il_size: the size of d40_interrupt_lookup array
5273cb645dcSTong Liu * @init_reg: the pointer to the struct d40_reg_val
5283cb645dcSTong Liu * @init_reg_size: the size of d40_reg_val array
5293cb645dcSTong Liu */
5303cb645dcSTong Liu struct d40_gen_dmac {
5313cb645dcSTong Liu u32 *backup;
5323cb645dcSTong Liu u32 backup_size;
5333cb645dcSTong Liu u32 realtime_en;
5343cb645dcSTong Liu u32 realtime_clear;
5353cb645dcSTong Liu u32 high_prio_en;
5363cb645dcSTong Liu u32 high_prio_clear;
5373cb645dcSTong Liu u32 interrupt_en;
5383cb645dcSTong Liu u32 interrupt_clear;
5393cb645dcSTong Liu struct d40_interrupt_lookup *il;
5403cb645dcSTong Liu u32 il_size;
5413cb645dcSTong Liu struct d40_reg_val *init_reg;
5423cb645dcSTong Liu u32 init_reg_size;
5433cb645dcSTong Liu };
5443cb645dcSTong Liu
5453cb645dcSTong Liu /**
5468d318a50SLinus Walleij * struct d40_base - The big global struct, one for each probe'd instance.
5478d318a50SLinus Walleij *
5488d318a50SLinus Walleij * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
5498d318a50SLinus Walleij * @execmd_lock: Lock for execute command usage since several channels share
5508d318a50SLinus Walleij * the same physical register.
5518d318a50SLinus Walleij * @dev: The device structure.
5528d318a50SLinus Walleij * @virtbase: The virtual base address of the DMA's register.
553f4185592SLinus Walleij * @rev: silicon revision detected.
5548d318a50SLinus Walleij * @clk: Pointer to the DMA clock structure.
5558d318a50SLinus Walleij * @irq: The IRQ number.
556a7dacb68SLee Jones * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
557a7dacb68SLee Jones * transfers).
5588d318a50SLinus Walleij * @num_phy_chans: The number of physical channels. Read from HW. This
5598d318a50SLinus Walleij * is the number of available channels for this driver, not counting "Secure
5608d318a50SLinus Walleij * mode" allocated physical channels.
5618d318a50SLinus Walleij * @num_log_chans: The number of logical channels. Calculated from
5628d318a50SLinus Walleij * num_phy_chans.
5638d318a50SLinus Walleij * @dma_both: dma_device channels that can do both memcpy and slave transfers.
5648d318a50SLinus Walleij * @dma_slave: dma_device channels that can do only do slave transfers.
5658d318a50SLinus Walleij * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
5667fb3e75eSNarayanan G * @phy_chans: Room for all possible physical channels in system.
5678d318a50SLinus Walleij * @log_chans: Room for all possible logical channels in system.
5688d318a50SLinus Walleij * @lookup_log_chans: Used to map interrupt number to logical channel. Points
5698d318a50SLinus Walleij * to log_chans entries.
5708d318a50SLinus Walleij * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
5718d318a50SLinus Walleij * to phy_chans entries.
5728d318a50SLinus Walleij * @plat_data: Pointer to provided platform_data which is the driver
5738d318a50SLinus Walleij * configuration.
57428c7a19dSNarayanan G * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
5758d318a50SLinus Walleij * @phy_res: Vector containing all physical channels.
5768d318a50SLinus Walleij * @lcla_pool: lcla pool settings and data.
5778d318a50SLinus Walleij * @lcpa_base: The virtual mapped address of LCPA.
5788d318a50SLinus Walleij * @phy_lcpa: The physical address of the LCPA.
5798d318a50SLinus Walleij * @lcpa_size: The size of the LCPA area.
580c675b1b4SJonas Aaberg * @desc_slab: cache for descriptors.
5817fb3e75eSNarayanan G * @reg_val_backup: Here the values of some hardware registers are stored
5827fb3e75eSNarayanan G * before the DMA is powered off. They are restored when the power is back on.
5833cb645dcSTong Liu * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
5843cb645dcSTong Liu * later
5857fb3e75eSNarayanan G * @reg_val_backup_chan: Backup data for standard channel parameter registers.
586e6a78511SKees Cook * @regs_interrupt: Scratch space for registers during interrupt.
5877fb3e75eSNarayanan G * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
5883cb645dcSTong Liu * @gen_dmac: the struct for generic registers values to represent u8500/8540
5893cb645dcSTong Liu * DMA controller
5908d318a50SLinus Walleij */
5918d318a50SLinus Walleij struct d40_base {
5928d318a50SLinus Walleij spinlock_t interrupt_lock;
5938d318a50SLinus Walleij spinlock_t execmd_lock;
5948d318a50SLinus Walleij struct device *dev;
5958d318a50SLinus Walleij void __iomem *virtbase;
596f4185592SLinus Walleij u8 rev:4;
5978d318a50SLinus Walleij struct clk *clk;
5988d318a50SLinus Walleij int irq;
599a7dacb68SLee Jones int num_memcpy_chans;
6008d318a50SLinus Walleij int num_phy_chans;
6018d318a50SLinus Walleij int num_log_chans;
6028d318a50SLinus Walleij struct dma_device dma_both;
6038d318a50SLinus Walleij struct dma_device dma_slave;
6048d318a50SLinus Walleij struct dma_device dma_memcpy;
6058d318a50SLinus Walleij struct d40_chan *phy_chans;
6068d318a50SLinus Walleij struct d40_chan *log_chans;
6078d318a50SLinus Walleij struct d40_chan **lookup_log_chans;
6088d318a50SLinus Walleij struct d40_chan **lookup_phy_chans;
6098d318a50SLinus Walleij struct stedma40_platform_data *plat_data;
61028c7a19dSNarayanan G struct regulator *lcpa_regulator;
6118d318a50SLinus Walleij /* Physical half channels */
6128d318a50SLinus Walleij struct d40_phy_res *phy_res;
6138d318a50SLinus Walleij struct d40_lcla_pool lcla_pool;
6148d318a50SLinus Walleij void *lcpa_base;
6158d318a50SLinus Walleij dma_addr_t phy_lcpa;
6168d318a50SLinus Walleij resource_size_t lcpa_size;
617c675b1b4SJonas Aaberg struct kmem_cache *desc_slab;
6187fb3e75eSNarayanan G u32 reg_val_backup[BACKUP_REGS_SZ];
61984b3da14SLee Jones u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
6207fb3e75eSNarayanan G u32 *reg_val_backup_chan;
621e6a78511SKees Cook u32 *regs_interrupt;
6227fb3e75eSNarayanan G u16 gcc_pwr_off_mask;
6233cb645dcSTong Liu struct d40_gen_dmac gen_dmac;
6248d318a50SLinus Walleij };
6258d318a50SLinus Walleij
chan2dev(struct d40_chan * d40c)626262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c)
627262d2915SRabin Vincent {
628262d2915SRabin Vincent return &d40c->chan.dev->device;
629262d2915SRabin Vincent }
630262d2915SRabin Vincent
chan_is_physical(struct d40_chan * chan)631724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan)
632724a8577SRabin Vincent {
633724a8577SRabin Vincent return chan->log_num == D40_PHY_CHAN;
634724a8577SRabin Vincent }
635724a8577SRabin Vincent
chan_is_logical(struct d40_chan * chan)636724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan)
637724a8577SRabin Vincent {
638724a8577SRabin Vincent return !chan_is_physical(chan);
639724a8577SRabin Vincent }
640724a8577SRabin Vincent
chan_base(struct d40_chan * chan)6418ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan)
6428ca84687SRabin Vincent {
6438ca84687SRabin Vincent return chan->base->virtbase + D40_DREG_PCBASE +
6448ca84687SRabin Vincent chan->phy_chan->num * D40_DREG_PCDELTA;
6458ca84687SRabin Vincent }
6468ca84687SRabin Vincent
6476db5a8baSRabin Vincent #define d40_err(dev, format, arg...) \
6486db5a8baSRabin Vincent dev_err(dev, "[%s] " format, __func__, ## arg)
6496db5a8baSRabin Vincent
6506db5a8baSRabin Vincent #define chan_err(d40c, format, arg...) \
6516db5a8baSRabin Vincent d40_err(chan2dev(d40c), format, ## arg)
6526db5a8baSRabin Vincent
6539e314ef3SVinod Koul static int d40_set_runtime_config_write(struct dma_chan *chan,
6549e314ef3SVinod Koul struct dma_slave_config *config,
6559e314ef3SVinod Koul enum dma_transfer_direction direction);
6569e314ef3SVinod Koul
d40_pool_lli_alloc(struct d40_chan * d40c,struct d40_desc * d40d,int lli_len)657b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
658dbd88788SRabin Vincent int lli_len)
6598d318a50SLinus Walleij {
660dbd88788SRabin Vincent bool is_log = chan_is_logical(d40c);
6618d318a50SLinus Walleij u32 align;
6628d318a50SLinus Walleij void *base;
6638d318a50SLinus Walleij
6648d318a50SLinus Walleij if (is_log)
6658d318a50SLinus Walleij align = sizeof(struct d40_log_lli);
6668d318a50SLinus Walleij else
6678d318a50SLinus Walleij align = sizeof(struct d40_phy_lli);
6688d318a50SLinus Walleij
6698d318a50SLinus Walleij if (lli_len == 1) {
6708d318a50SLinus Walleij base = d40d->lli_pool.pre_alloc_lli;
6718d318a50SLinus Walleij d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
6728d318a50SLinus Walleij d40d->lli_pool.base = NULL;
6738d318a50SLinus Walleij } else {
674594ece4dSRabin Vincent d40d->lli_pool.size = lli_len * 2 * align;
6758d318a50SLinus Walleij
6768d318a50SLinus Walleij base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
6778d318a50SLinus Walleij d40d->lli_pool.base = base;
6788d318a50SLinus Walleij
6798d318a50SLinus Walleij if (d40d->lli_pool.base == NULL)
6808d318a50SLinus Walleij return -ENOMEM;
6818d318a50SLinus Walleij }
6828d318a50SLinus Walleij
6838d318a50SLinus Walleij if (is_log) {
684d924abadSRabin Vincent d40d->lli_log.src = PTR_ALIGN(base, align);
685594ece4dSRabin Vincent d40d->lli_log.dst = d40d->lli_log.src + lli_len;
686b00f938cSRabin Vincent
687b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0;
6888d318a50SLinus Walleij } else {
689d924abadSRabin Vincent d40d->lli_phy.src = PTR_ALIGN(base, align);
690594ece4dSRabin Vincent d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
691b00f938cSRabin Vincent
692b00f938cSRabin Vincent d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
693b00f938cSRabin Vincent d40d->lli_phy.src,
694b00f938cSRabin Vincent d40d->lli_pool.size,
695b00f938cSRabin Vincent DMA_TO_DEVICE);
696b00f938cSRabin Vincent
697b00f938cSRabin Vincent if (dma_mapping_error(d40c->base->dev,
698b00f938cSRabin Vincent d40d->lli_pool.dma_addr)) {
699b00f938cSRabin Vincent kfree(d40d->lli_pool.base);
700b00f938cSRabin Vincent d40d->lli_pool.base = NULL;
701b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0;
702b00f938cSRabin Vincent return -ENOMEM;
703b00f938cSRabin Vincent }
7048d318a50SLinus Walleij }
7058d318a50SLinus Walleij
7068d318a50SLinus Walleij return 0;
7078d318a50SLinus Walleij }
7088d318a50SLinus Walleij
d40_pool_lli_free(struct d40_chan * d40c,struct d40_desc * d40d)709b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
7108d318a50SLinus Walleij {
711b00f938cSRabin Vincent if (d40d->lli_pool.dma_addr)
712b00f938cSRabin Vincent dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
713b00f938cSRabin Vincent d40d->lli_pool.size, DMA_TO_DEVICE);
714b00f938cSRabin Vincent
7158d318a50SLinus Walleij kfree(d40d->lli_pool.base);
7168d318a50SLinus Walleij d40d->lli_pool.base = NULL;
7178d318a50SLinus Walleij d40d->lli_pool.size = 0;
7188d318a50SLinus Walleij d40d->lli_log.src = NULL;
7198d318a50SLinus Walleij d40d->lli_log.dst = NULL;
7208d318a50SLinus Walleij d40d->lli_phy.src = NULL;
7218d318a50SLinus Walleij d40d->lli_phy.dst = NULL;
7228d318a50SLinus Walleij }
7238d318a50SLinus Walleij
d40_lcla_alloc_one(struct d40_chan * d40c,struct d40_desc * d40d)724698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c,
725698e4732SJonas Aaberg struct d40_desc *d40d)
726698e4732SJonas Aaberg {
727698e4732SJonas Aaberg unsigned long flags;
728698e4732SJonas Aaberg int i;
729698e4732SJonas Aaberg int ret = -EINVAL;
730698e4732SJonas Aaberg
731698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
732698e4732SJonas Aaberg
733698e4732SJonas Aaberg /*
734698e4732SJonas Aaberg * Allocate both src and dst at the same time, therefore the half
735698e4732SJonas Aaberg * start on 1 since 0 can't be used since zero is used as end marker.
736698e4732SJonas Aaberg */
737698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7387ce529efSFabio Baltieri int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7397ce529efSFabio Baltieri
7407ce529efSFabio Baltieri if (!d40c->base->lcla_pool.alloc_map[idx]) {
7417ce529efSFabio Baltieri d40c->base->lcla_pool.alloc_map[idx] = d40d;
742698e4732SJonas Aaberg d40d->lcla_alloc++;
743698e4732SJonas Aaberg ret = i;
744698e4732SJonas Aaberg break;
745698e4732SJonas Aaberg }
746698e4732SJonas Aaberg }
747698e4732SJonas Aaberg
748698e4732SJonas Aaberg spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
749698e4732SJonas Aaberg
750698e4732SJonas Aaberg return ret;
751698e4732SJonas Aaberg }
752698e4732SJonas Aaberg
d40_lcla_free_all(struct d40_chan * d40c,struct d40_desc * d40d)753698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c,
754698e4732SJonas Aaberg struct d40_desc *d40d)
755698e4732SJonas Aaberg {
756698e4732SJonas Aaberg unsigned long flags;
757698e4732SJonas Aaberg int i;
758698e4732SJonas Aaberg int ret = -EINVAL;
759698e4732SJonas Aaberg
760724a8577SRabin Vincent if (chan_is_physical(d40c))
761698e4732SJonas Aaberg return 0;
762698e4732SJonas Aaberg
763698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
764698e4732SJonas Aaberg
765698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
7667ce529efSFabio Baltieri int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
7677ce529efSFabio Baltieri
7687ce529efSFabio Baltieri if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
7697ce529efSFabio Baltieri d40c->base->lcla_pool.alloc_map[idx] = NULL;
770698e4732SJonas Aaberg d40d->lcla_alloc--;
771698e4732SJonas Aaberg if (d40d->lcla_alloc == 0) {
772698e4732SJonas Aaberg ret = 0;
773698e4732SJonas Aaberg break;
774698e4732SJonas Aaberg }
775698e4732SJonas Aaberg }
776698e4732SJonas Aaberg }
777698e4732SJonas Aaberg
778698e4732SJonas Aaberg spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
779698e4732SJonas Aaberg
780698e4732SJonas Aaberg return ret;
781698e4732SJonas Aaberg
782698e4732SJonas Aaberg }
783698e4732SJonas Aaberg
d40_desc_remove(struct d40_desc * d40d)7848d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d)
7858d318a50SLinus Walleij {
7868d318a50SLinus Walleij list_del(&d40d->node);
7878d318a50SLinus Walleij }
7888d318a50SLinus Walleij
d40_desc_get(struct d40_chan * d40c)7898d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
7908d318a50SLinus Walleij {
791a2c15fa4SRabin Vincent struct d40_desc *desc = NULL;
792a2c15fa4SRabin Vincent
793a2c15fa4SRabin Vincent if (!list_empty(&d40c->client)) {
7948d318a50SLinus Walleij struct d40_desc *d;
7958d318a50SLinus Walleij struct d40_desc *_d;
7968d318a50SLinus Walleij
7977fb3e75eSNarayanan G list_for_each_entry_safe(d, _d, &d40c->client, node) {
7988d318a50SLinus Walleij if (async_tx_test_ack(&d->txd)) {
7998d318a50SLinus Walleij d40_desc_remove(d);
800a2c15fa4SRabin Vincent desc = d;
801a2c15fa4SRabin Vincent memset(desc, 0, sizeof(*desc));
802c675b1b4SJonas Aaberg break;
8038d318a50SLinus Walleij }
8048d318a50SLinus Walleij }
8057fb3e75eSNarayanan G }
806a2c15fa4SRabin Vincent
807a2c15fa4SRabin Vincent if (!desc)
808a2c15fa4SRabin Vincent desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
809a2c15fa4SRabin Vincent
810a2c15fa4SRabin Vincent if (desc)
811a2c15fa4SRabin Vincent INIT_LIST_HEAD(&desc->node);
812a2c15fa4SRabin Vincent
813a2c15fa4SRabin Vincent return desc;
8148d318a50SLinus Walleij }
8158d318a50SLinus Walleij
d40_desc_free(struct d40_chan * d40c,struct d40_desc * d40d)8168d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
8178d318a50SLinus Walleij {
818698e4732SJonas Aaberg
819b00f938cSRabin Vincent d40_pool_lli_free(d40c, d40d);
820698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d);
821c675b1b4SJonas Aaberg kmem_cache_free(d40c->base->desc_slab, d40d);
8228d318a50SLinus Walleij }
8238d318a50SLinus Walleij
d40_desc_submit(struct d40_chan * d40c,struct d40_desc * desc)8248d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
8258d318a50SLinus Walleij {
8268d318a50SLinus Walleij list_add_tail(&desc->node, &d40c->active);
8278d318a50SLinus Walleij }
8288d318a50SLinus Walleij
d40_phy_lli_load(struct d40_chan * chan,struct d40_desc * desc)8291c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
8301c4b0927SRabin Vincent {
8311c4b0927SRabin Vincent struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
8321c4b0927SRabin Vincent struct d40_phy_lli *lli_src = desc->lli_phy.src;
8331c4b0927SRabin Vincent void __iomem *base = chan_base(chan);
8341c4b0927SRabin Vincent
8351c4b0927SRabin Vincent writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
8361c4b0927SRabin Vincent writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
8371c4b0927SRabin Vincent writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
8381c4b0927SRabin Vincent writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
8391c4b0927SRabin Vincent
8401c4b0927SRabin Vincent writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
8411c4b0927SRabin Vincent writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
8421c4b0927SRabin Vincent writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
8431c4b0927SRabin Vincent writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
8441c4b0927SRabin Vincent }
8451c4b0927SRabin Vincent
d40_desc_done(struct d40_chan * d40c,struct d40_desc * desc)8464226dd86SFabio Baltieri static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
8474226dd86SFabio Baltieri {
8484226dd86SFabio Baltieri list_add_tail(&desc->node, &d40c->done);
8494226dd86SFabio Baltieri }
8504226dd86SFabio Baltieri
d40_log_lli_to_lcxa(struct d40_chan * chan,struct d40_desc * desc)851e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
852698e4732SJonas Aaberg {
853e65889c7SRabin Vincent struct d40_lcla_pool *pool = &chan->base->lcla_pool;
854e65889c7SRabin Vincent struct d40_log_lli_bidir *lli = &desc->lli_log;
855e65889c7SRabin Vincent int lli_current = desc->lli_current;
856e65889c7SRabin Vincent int lli_len = desc->lli_len;
8570c842b55SRabin Vincent bool cyclic = desc->cyclic;
858e65889c7SRabin Vincent int curr_lcla = -EINVAL;
8590c842b55SRabin Vincent int first_lcla = 0;
86028c7a19dSNarayanan G bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
8610c842b55SRabin Vincent bool linkback;
862698e4732SJonas Aaberg
8630c842b55SRabin Vincent /*
8640c842b55SRabin Vincent * We may have partially running cyclic transfers, in case we did't get
8650c842b55SRabin Vincent * enough LCLA entries.
8660c842b55SRabin Vincent */
8670c842b55SRabin Vincent linkback = cyclic && lli_current == 0;
8680c842b55SRabin Vincent
8690c842b55SRabin Vincent /*
8700c842b55SRabin Vincent * For linkback, we need one LCLA even with only one link, because we
8710c842b55SRabin Vincent * can't link back to the one in LCPA space
8720c842b55SRabin Vincent */
8730c842b55SRabin Vincent if (linkback || (lli_len - lli_current > 1)) {
8747407048bSFabio Baltieri /*
8757407048bSFabio Baltieri * If the channel is expected to use only soft_lli don't
8767407048bSFabio Baltieri * allocate a lcla. This is to avoid a HW issue that exists
8777407048bSFabio Baltieri * in some controller during a peripheral to memory transfer
8787407048bSFabio Baltieri * that uses linked lists.
8797407048bSFabio Baltieri */
8807407048bSFabio Baltieri if (!(chan->phy_chan->use_soft_lli &&
8812c2b62d5SLee Jones chan->dma_cfg.dir == DMA_DEV_TO_MEM))
882e65889c7SRabin Vincent curr_lcla = d40_lcla_alloc_one(chan, desc);
8837407048bSFabio Baltieri
8840c842b55SRabin Vincent first_lcla = curr_lcla;
8850c842b55SRabin Vincent }
8860c842b55SRabin Vincent
8870c842b55SRabin Vincent /*
8880c842b55SRabin Vincent * For linkback, we normally load the LCPA in the loop since we need to
8890c842b55SRabin Vincent * link it to the second LCLA and not the first. However, if we
8900c842b55SRabin Vincent * couldn't even get a first LCLA, then we have to run in LCPA and
8910c842b55SRabin Vincent * reload manually.
8920c842b55SRabin Vincent */
8930c842b55SRabin Vincent if (!linkback || curr_lcla == -EINVAL) {
8940c842b55SRabin Vincent unsigned int flags = 0;
8950c842b55SRabin Vincent
8960c842b55SRabin Vincent if (curr_lcla == -EINVAL)
8970c842b55SRabin Vincent flags |= LLI_TERM_INT;
898698e4732SJonas Aaberg
899e65889c7SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa,
900e65889c7SRabin Vincent &lli->dst[lli_current],
901e65889c7SRabin Vincent &lli->src[lli_current],
9020c842b55SRabin Vincent curr_lcla,
9030c842b55SRabin Vincent flags);
904e65889c7SRabin Vincent lli_current++;
9050c842b55SRabin Vincent }
9066045f0bbSRabin Vincent
9076045f0bbSRabin Vincent if (curr_lcla < 0)
9084d8673a0SMarkus Elfring goto set_current;
9096045f0bbSRabin Vincent
910e65889c7SRabin Vincent for (; lli_current < lli_len; lli_current++) {
911e65889c7SRabin Vincent unsigned int lcla_offset = chan->phy_chan->num * 1024 +
912026cbc42SRabin Vincent 8 * curr_lcla * 2;
913026cbc42SRabin Vincent struct d40_log_lli *lcla = pool->base + lcla_offset;
9140c842b55SRabin Vincent unsigned int flags = 0;
915e65889c7SRabin Vincent int next_lcla;
916698e4732SJonas Aaberg
917e65889c7SRabin Vincent if (lli_current + 1 < lli_len)
918e65889c7SRabin Vincent next_lcla = d40_lcla_alloc_one(chan, desc);
919698e4732SJonas Aaberg else
9200c842b55SRabin Vincent next_lcla = linkback ? first_lcla : -EINVAL;
921698e4732SJonas Aaberg
9220c842b55SRabin Vincent if (cyclic || next_lcla == -EINVAL)
9230c842b55SRabin Vincent flags |= LLI_TERM_INT;
9240c842b55SRabin Vincent
9250c842b55SRabin Vincent if (linkback && curr_lcla == first_lcla) {
9260c842b55SRabin Vincent /* First link goes in both LCPA and LCLA */
9270c842b55SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa,
9280c842b55SRabin Vincent &lli->dst[lli_current],
9290c842b55SRabin Vincent &lli->src[lli_current],
9300c842b55SRabin Vincent next_lcla, flags);
9310c842b55SRabin Vincent }
9320c842b55SRabin Vincent
9330c842b55SRabin Vincent /*
9340c842b55SRabin Vincent * One unused LCLA in the cyclic case if the very first
9350c842b55SRabin Vincent * next_lcla fails...
9360c842b55SRabin Vincent */
937698e4732SJonas Aaberg d40_log_lli_lcla_write(lcla,
938e65889c7SRabin Vincent &lli->dst[lli_current],
939e65889c7SRabin Vincent &lli->src[lli_current],
9400c842b55SRabin Vincent next_lcla, flags);
941698e4732SJonas Aaberg
94228c7a19dSNarayanan G /*
94328c7a19dSNarayanan G * Cache maintenance is not needed if lcla is
94428c7a19dSNarayanan G * mapped in esram
94528c7a19dSNarayanan G */
94628c7a19dSNarayanan G if (!use_esram_lcla) {
947e65889c7SRabin Vincent dma_sync_single_range_for_device(chan->base->dev,
948026cbc42SRabin Vincent pool->dma_addr, lcla_offset,
949698e4732SJonas Aaberg 2 * sizeof(struct d40_log_lli),
950698e4732SJonas Aaberg DMA_TO_DEVICE);
95128c7a19dSNarayanan G }
952698e4732SJonas Aaberg curr_lcla = next_lcla;
953698e4732SJonas Aaberg
9540c842b55SRabin Vincent if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
955e65889c7SRabin Vincent lli_current++;
956698e4732SJonas Aaberg break;
957698e4732SJonas Aaberg }
958e65889c7SRabin Vincent }
9594d8673a0SMarkus Elfring set_current:
960e65889c7SRabin Vincent desc->lli_current = lli_current;
961698e4732SJonas Aaberg }
962e65889c7SRabin Vincent
d40_desc_load(struct d40_chan * d40c,struct d40_desc * d40d)963e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
964e65889c7SRabin Vincent {
965e65889c7SRabin Vincent if (chan_is_physical(d40c)) {
966e65889c7SRabin Vincent d40_phy_lli_load(d40c, d40d);
967e65889c7SRabin Vincent d40d->lli_current = d40d->lli_len;
968e65889c7SRabin Vincent } else
969e65889c7SRabin Vincent d40_log_lli_to_lcxa(d40c, d40d);
970698e4732SJonas Aaberg }
971698e4732SJonas Aaberg
d40_first_active_get(struct d40_chan * d40c)9728d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
9738d318a50SLinus Walleij {
974360af35bSMasahiro Yamada return list_first_entry_or_null(&d40c->active, struct d40_desc, node);
9758d318a50SLinus Walleij }
9768d318a50SLinus Walleij
9777404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */
d40_desc_queue(struct d40_chan * d40c,struct d40_desc * desc)9788d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
9798d318a50SLinus Walleij {
9807404368cSPer Forlin d40_desc_remove(desc);
9817404368cSPer Forlin desc->is_in_client_list = false;
982a8f3067bSPer Forlin list_add_tail(&desc->node, &d40c->pending_queue);
983a8f3067bSPer Forlin }
984a8f3067bSPer Forlin
d40_first_pending(struct d40_chan * d40c)985a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
986a8f3067bSPer Forlin {
987360af35bSMasahiro Yamada return list_first_entry_or_null(&d40c->pending_queue, struct d40_desc,
988a8f3067bSPer Forlin node);
9898d318a50SLinus Walleij }
9908d318a50SLinus Walleij
d40_first_queued(struct d40_chan * d40c)9918d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
9928d318a50SLinus Walleij {
993360af35bSMasahiro Yamada return list_first_entry_or_null(&d40c->queue, struct d40_desc, node);
9948d318a50SLinus Walleij }
9958d318a50SLinus Walleij
d40_first_done(struct d40_chan * d40c)9964226dd86SFabio Baltieri static struct d40_desc *d40_first_done(struct d40_chan *d40c)
9974226dd86SFabio Baltieri {
998360af35bSMasahiro Yamada return list_first_entry_or_null(&d40c->done, struct d40_desc, node);
9994226dd86SFabio Baltieri }
10004226dd86SFabio Baltieri
d40_psize_2_burst_size(bool is_log,int psize)1001d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize)
1002d49278e3SPer Forlin {
1003d49278e3SPer Forlin if (is_log) {
1004d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_LOG_1)
1005d49278e3SPer Forlin return 1;
1006d49278e3SPer Forlin } else {
1007d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_PHY_1)
1008d49278e3SPer Forlin return 1;
1009d49278e3SPer Forlin }
10108d318a50SLinus Walleij
1011d49278e3SPer Forlin return 2 << psize;
1012d49278e3SPer Forlin }
1013d49278e3SPer Forlin
1014d49278e3SPer Forlin /*
1015d49278e3SPer Forlin * The dma only supports transmitting packages up to
101643f2e1a3SLee Jones * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
101743f2e1a3SLee Jones *
101843f2e1a3SLee Jones * Calculate the total number of dma elements required to send the entire sg list.
1019d49278e3SPer Forlin */
d40_size_2_dmalen(int size,u32 data_width1,u32 data_width2)1020d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
1021d49278e3SPer Forlin {
1022d49278e3SPer Forlin int dmalen;
1023d49278e3SPer Forlin u32 max_w = max(data_width1, data_width2);
1024d49278e3SPer Forlin u32 min_w = min(data_width1, data_width2);
102543f2e1a3SLee Jones u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
1026d49278e3SPer Forlin
1027d49278e3SPer Forlin if (seg_max > STEDMA40_MAX_SEG_SIZE)
102843f2e1a3SLee Jones seg_max -= max_w;
1029d49278e3SPer Forlin
103043f2e1a3SLee Jones if (!IS_ALIGNED(size, max_w))
1031d49278e3SPer Forlin return -EINVAL;
1032d49278e3SPer Forlin
1033d49278e3SPer Forlin if (size <= seg_max)
1034d49278e3SPer Forlin dmalen = 1;
1035d49278e3SPer Forlin else {
1036d49278e3SPer Forlin dmalen = size / seg_max;
1037d49278e3SPer Forlin if (dmalen * seg_max < size)
1038d49278e3SPer Forlin dmalen++;
1039d49278e3SPer Forlin }
1040d49278e3SPer Forlin return dmalen;
1041d49278e3SPer Forlin }
1042d49278e3SPer Forlin
d40_sg_2_dmalen(struct scatterlist * sgl,int sg_len,u32 data_width1,u32 data_width2)1043d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
1044d49278e3SPer Forlin u32 data_width1, u32 data_width2)
1045d49278e3SPer Forlin {
1046d49278e3SPer Forlin struct scatterlist *sg;
1047d49278e3SPer Forlin int i;
1048d49278e3SPer Forlin int len = 0;
1049d49278e3SPer Forlin int ret;
1050d49278e3SPer Forlin
1051d49278e3SPer Forlin for_each_sg(sgl, sg, sg_len, i) {
1052d49278e3SPer Forlin ret = d40_size_2_dmalen(sg_dma_len(sg),
1053d49278e3SPer Forlin data_width1, data_width2);
1054d49278e3SPer Forlin if (ret < 0)
1055d49278e3SPer Forlin return ret;
1056d49278e3SPer Forlin len += ret;
1057d49278e3SPer Forlin }
1058d49278e3SPer Forlin return len;
1059d49278e3SPer Forlin }
1060d49278e3SPer Forlin
__d40_execute_command_phy(struct d40_chan * d40c,enum d40_command command)10611bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c,
10628d318a50SLinus Walleij enum d40_command command)
10638d318a50SLinus Walleij {
1064767a9675SJonas Aaberg u32 status;
1065767a9675SJonas Aaberg int i;
10668d318a50SLinus Walleij void __iomem *active_reg;
10678d318a50SLinus Walleij int ret = 0;
10688d318a50SLinus Walleij unsigned long flags;
10691d392a7bSJonas Aaberg u32 wmask;
10708d318a50SLinus Walleij
10711bdae6f4SNarayanan G if (command == D40_DMA_STOP) {
10721bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
10731bdae6f4SNarayanan G if (ret)
10741bdae6f4SNarayanan G return ret;
10751bdae6f4SNarayanan G }
10761bdae6f4SNarayanan G
10778d318a50SLinus Walleij spin_lock_irqsave(&d40c->base->execmd_lock, flags);
10788d318a50SLinus Walleij
10798d318a50SLinus Walleij if (d40c->phy_chan->num % 2 == 0)
10808d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
10818d318a50SLinus Walleij else
10828d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
10838d318a50SLinus Walleij
10848d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) {
10858d318a50SLinus Walleij status = (readl(active_reg) &
10868d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
10878d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num);
10888d318a50SLinus Walleij
10898d318a50SLinus Walleij if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
1090b140ea0fSMarkus Elfring goto unlock;
10918d318a50SLinus Walleij }
10928d318a50SLinus Walleij
10931d392a7bSJonas Aaberg wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
10941d392a7bSJonas Aaberg writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
10951d392a7bSJonas Aaberg active_reg);
10968d318a50SLinus Walleij
10978d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) {
10988d318a50SLinus Walleij
10998d318a50SLinus Walleij for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
11008d318a50SLinus Walleij status = (readl(active_reg) &
11018d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
11028d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num);
11038d318a50SLinus Walleij
11048d318a50SLinus Walleij cpu_relax();
11058d318a50SLinus Walleij /*
11068d318a50SLinus Walleij * Reduce the number of bus accesses while
11078d318a50SLinus Walleij * waiting for the DMA to suspend.
11088d318a50SLinus Walleij */
11098d318a50SLinus Walleij udelay(3);
11108d318a50SLinus Walleij
11118d318a50SLinus Walleij if (status == D40_DMA_STOP ||
11128d318a50SLinus Walleij status == D40_DMA_SUSPENDED)
11138d318a50SLinus Walleij break;
11148d318a50SLinus Walleij }
11158d318a50SLinus Walleij
11168d318a50SLinus Walleij if (i == D40_SUSPEND_MAX_IT) {
11176db5a8baSRabin Vincent chan_err(d40c,
11186db5a8baSRabin Vincent "unable to suspend the chl %d (log: %d) status %x\n",
11196db5a8baSRabin Vincent d40c->phy_chan->num, d40c->log_num,
11208d318a50SLinus Walleij status);
11218d318a50SLinus Walleij dump_stack();
11228d318a50SLinus Walleij ret = -EBUSY;
11238d318a50SLinus Walleij }
11248d318a50SLinus Walleij
11258d318a50SLinus Walleij }
1126b140ea0fSMarkus Elfring unlock:
11278d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
11288d318a50SLinus Walleij return ret;
11298d318a50SLinus Walleij }
11308d318a50SLinus Walleij
d40_term_all(struct d40_chan * d40c)11318d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c)
11328d318a50SLinus Walleij {
11338d318a50SLinus Walleij struct d40_desc *d40d;
11347404368cSPer Forlin struct d40_desc *_d;
11358d318a50SLinus Walleij
11364226dd86SFabio Baltieri /* Release completed descriptors */
11374226dd86SFabio Baltieri while ((d40d = d40_first_done(d40c))) {
11384226dd86SFabio Baltieri d40_desc_remove(d40d);
11394226dd86SFabio Baltieri d40_desc_free(d40c, d40d);
11404226dd86SFabio Baltieri }
11414226dd86SFabio Baltieri
11428d318a50SLinus Walleij /* Release active descriptors */
11438d318a50SLinus Walleij while ((d40d = d40_first_active_get(d40c))) {
11448d318a50SLinus Walleij d40_desc_remove(d40d);
11458d318a50SLinus Walleij d40_desc_free(d40c, d40d);
11468d318a50SLinus Walleij }
11478d318a50SLinus Walleij
11488d318a50SLinus Walleij /* Release queued descriptors waiting for transfer */
11498d318a50SLinus Walleij while ((d40d = d40_first_queued(d40c))) {
11508d318a50SLinus Walleij d40_desc_remove(d40d);
11518d318a50SLinus Walleij d40_desc_free(d40c, d40d);
11528d318a50SLinus Walleij }
11538d318a50SLinus Walleij
1154a8f3067bSPer Forlin /* Release pending descriptors */
1155a8f3067bSPer Forlin while ((d40d = d40_first_pending(d40c))) {
1156a8f3067bSPer Forlin d40_desc_remove(d40d);
1157a8f3067bSPer Forlin d40_desc_free(d40c, d40d);
1158a8f3067bSPer Forlin }
11598d318a50SLinus Walleij
11607404368cSPer Forlin /* Release client owned descriptors */
11617404368cSPer Forlin if (!list_empty(&d40c->client))
11627404368cSPer Forlin list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
11637404368cSPer Forlin d40_desc_remove(d40d);
11647404368cSPer Forlin d40_desc_free(d40c, d40d);
11657404368cSPer Forlin }
11667404368cSPer Forlin
116782babbb3SPer Forlin /* Release descriptors in prepare queue */
116882babbb3SPer Forlin if (!list_empty(&d40c->prepare_queue))
116982babbb3SPer Forlin list_for_each_entry_safe(d40d, _d,
117082babbb3SPer Forlin &d40c->prepare_queue, node) {
117182babbb3SPer Forlin d40_desc_remove(d40d);
117282babbb3SPer Forlin d40_desc_free(d40c, d40d);
117382babbb3SPer Forlin }
11747404368cSPer Forlin
11758d318a50SLinus Walleij d40c->pending_tx = 0;
11768d318a50SLinus Walleij }
11778d318a50SLinus Walleij
__d40_config_set_event(struct d40_chan * d40c,enum d40_events event_type,u32 event,int reg)11781bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c,
11791bdae6f4SNarayanan G enum d40_events event_type, u32 event,
11801bdae6f4SNarayanan G int reg)
1181262d2915SRabin Vincent {
11828ca84687SRabin Vincent void __iomem *addr = chan_base(d40c) + reg;
1183262d2915SRabin Vincent int tries;
11841bdae6f4SNarayanan G u32 status;
1185262d2915SRabin Vincent
11861bdae6f4SNarayanan G switch (event_type) {
11871bdae6f4SNarayanan G
11881bdae6f4SNarayanan G case D40_DEACTIVATE_EVENTLINE:
11891bdae6f4SNarayanan G
1190262d2915SRabin Vincent writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
1191262d2915SRabin Vincent | ~D40_EVENTLINE_MASK(event), addr);
11921bdae6f4SNarayanan G break;
11931bdae6f4SNarayanan G
11941bdae6f4SNarayanan G case D40_SUSPEND_REQ_EVENTLINE:
11951bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
11961bdae6f4SNarayanan G D40_EVENTLINE_POS(event);
11971bdae6f4SNarayanan G
11981bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE ||
11991bdae6f4SNarayanan G status == D40_SUSPEND_REQ_EVENTLINE)
12001bdae6f4SNarayanan G break;
12011bdae6f4SNarayanan G
12021bdae6f4SNarayanan G writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
12031bdae6f4SNarayanan G | ~D40_EVENTLINE_MASK(event), addr);
12041bdae6f4SNarayanan G
12051bdae6f4SNarayanan G for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
12061bdae6f4SNarayanan G
12071bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
12081bdae6f4SNarayanan G D40_EVENTLINE_POS(event);
12091bdae6f4SNarayanan G
12101bdae6f4SNarayanan G cpu_relax();
12111bdae6f4SNarayanan G /*
12121bdae6f4SNarayanan G * Reduce the number of bus accesses while
12131bdae6f4SNarayanan G * waiting for the DMA to suspend.
12141bdae6f4SNarayanan G */
12151bdae6f4SNarayanan G udelay(3);
12161bdae6f4SNarayanan G
12171bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE)
12181bdae6f4SNarayanan G break;
1219262d2915SRabin Vincent }
1220262d2915SRabin Vincent
12211bdae6f4SNarayanan G if (tries == D40_SUSPEND_MAX_IT) {
12221bdae6f4SNarayanan G chan_err(d40c,
12231bdae6f4SNarayanan G "unable to stop the event_line chl %d (log: %d)"
12241bdae6f4SNarayanan G "status %x\n", d40c->phy_chan->num,
12251bdae6f4SNarayanan G d40c->log_num, status);
12261bdae6f4SNarayanan G }
12271bdae6f4SNarayanan G break;
12281bdae6f4SNarayanan G
12291bdae6f4SNarayanan G case D40_ACTIVATE_EVENTLINE:
1230262d2915SRabin Vincent /*
1231262d2915SRabin Vincent * The hardware sometimes doesn't register the enable when src and dst
1232262d2915SRabin Vincent * event lines are active on the same logical channel. Retry to ensure
1233262d2915SRabin Vincent * it does. Usually only one retry is sufficient.
1234262d2915SRabin Vincent */
1235262d2915SRabin Vincent tries = 100;
1236262d2915SRabin Vincent while (--tries) {
12371bdae6f4SNarayanan G writel((D40_ACTIVATE_EVENTLINE <<
12381bdae6f4SNarayanan G D40_EVENTLINE_POS(event)) |
12391bdae6f4SNarayanan G ~D40_EVENTLINE_MASK(event), addr);
1240262d2915SRabin Vincent
1241262d2915SRabin Vincent if (readl(addr) & D40_EVENTLINE_MASK(event))
1242262d2915SRabin Vincent break;
1243262d2915SRabin Vincent }
1244262d2915SRabin Vincent
1245262d2915SRabin Vincent if (tries != 99)
1246262d2915SRabin Vincent dev_dbg(chan2dev(d40c),
1247262d2915SRabin Vincent "[%s] workaround enable S%cLNK (%d tries)\n",
1248262d2915SRabin Vincent __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
1249262d2915SRabin Vincent 100 - tries);
1250262d2915SRabin Vincent
1251262d2915SRabin Vincent WARN_ON(!tries);
12521bdae6f4SNarayanan G break;
12531bdae6f4SNarayanan G
12541bdae6f4SNarayanan G case D40_ROUND_EVENTLINE:
12551bdae6f4SNarayanan G BUG();
12561bdae6f4SNarayanan G break;
12571bdae6f4SNarayanan G
12581bdae6f4SNarayanan G }
1259262d2915SRabin Vincent }
1260262d2915SRabin Vincent
d40_config_set_event(struct d40_chan * d40c,enum d40_events event_type)12611bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c,
12621bdae6f4SNarayanan G enum d40_events event_type)
12638d318a50SLinus Walleij {
126426955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
126526955c07SLee Jones
12668d318a50SLinus Walleij /* Enable event line connected to device (or memcpy) */
12672c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
12682c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
12691bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event,
12708d318a50SLinus Walleij D40_CHAN_REG_SSLNK);
1271262d2915SRabin Vincent
12722c2b62d5SLee Jones if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
12731bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event,
12748d318a50SLinus Walleij D40_CHAN_REG_SDLNK);
12758d318a50SLinus Walleij }
12768d318a50SLinus Walleij
d40_chan_has_events(struct d40_chan * d40c)1277a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c)
12788d318a50SLinus Walleij {
12798ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c);
1280be8cb7dfSJonas Aaberg u32 val;
12818d318a50SLinus Walleij
12828ca84687SRabin Vincent val = readl(chanbase + D40_CHAN_REG_SSLNK);
12838ca84687SRabin Vincent val |= readl(chanbase + D40_CHAN_REG_SDLNK);
12848d318a50SLinus Walleij
1285a5ebca47SJonas Aaberg return val;
12868d318a50SLinus Walleij }
12878d318a50SLinus Walleij
12881bdae6f4SNarayanan G static int
__d40_execute_command_log(struct d40_chan * d40c,enum d40_command command)12891bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
12901bdae6f4SNarayanan G {
12911bdae6f4SNarayanan G unsigned long flags;
12921bdae6f4SNarayanan G int ret = 0;
12931bdae6f4SNarayanan G u32 active_status;
12941bdae6f4SNarayanan G void __iomem *active_reg;
12951bdae6f4SNarayanan G
12961bdae6f4SNarayanan G if (d40c->phy_chan->num % 2 == 0)
12971bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
12981bdae6f4SNarayanan G else
12991bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
13001bdae6f4SNarayanan G
13011bdae6f4SNarayanan G
13021bdae6f4SNarayanan G spin_lock_irqsave(&d40c->phy_chan->lock, flags);
13031bdae6f4SNarayanan G
13041bdae6f4SNarayanan G switch (command) {
13051bdae6f4SNarayanan G case D40_DMA_STOP:
13061bdae6f4SNarayanan G case D40_DMA_SUSPEND_REQ:
13071bdae6f4SNarayanan G
13081bdae6f4SNarayanan G active_status = (readl(active_reg) &
13091bdae6f4SNarayanan G D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
13101bdae6f4SNarayanan G D40_CHAN_POS(d40c->phy_chan->num);
13111bdae6f4SNarayanan G
13121bdae6f4SNarayanan G if (active_status == D40_DMA_RUN)
13131bdae6f4SNarayanan G d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
13141bdae6f4SNarayanan G else
13151bdae6f4SNarayanan G d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
13161bdae6f4SNarayanan G
13171bdae6f4SNarayanan G if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
13181bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command);
13191bdae6f4SNarayanan G
13201bdae6f4SNarayanan G break;
13211bdae6f4SNarayanan G
13221bdae6f4SNarayanan G case D40_DMA_RUN:
13231bdae6f4SNarayanan G
13241bdae6f4SNarayanan G d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
13251bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command);
13261bdae6f4SNarayanan G break;
13271bdae6f4SNarayanan G
13281bdae6f4SNarayanan G case D40_DMA_SUSPENDED:
13291bdae6f4SNarayanan G BUG();
13301bdae6f4SNarayanan G break;
13311bdae6f4SNarayanan G }
13321bdae6f4SNarayanan G
13331bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
13341bdae6f4SNarayanan G return ret;
13351bdae6f4SNarayanan G }
13361bdae6f4SNarayanan G
d40_channel_execute_command(struct d40_chan * d40c,enum d40_command command)13371bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c,
13381bdae6f4SNarayanan G enum d40_command command)
13391bdae6f4SNarayanan G {
13401bdae6f4SNarayanan G if (chan_is_logical(d40c))
13411bdae6f4SNarayanan G return __d40_execute_command_log(d40c, command);
13421bdae6f4SNarayanan G else
13431bdae6f4SNarayanan G return __d40_execute_command_phy(d40c, command);
13441bdae6f4SNarayanan G }
13451bdae6f4SNarayanan G
d40_get_prmo(struct d40_chan * d40c)134620a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c)
134720a5b6d0SRabin Vincent {
134820a5b6d0SRabin Vincent static const unsigned int phy_map[] = {
134920a5b6d0SRabin Vincent [STEDMA40_PCHAN_BASIC_MODE]
135020a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_BASIC,
135120a5b6d0SRabin Vincent [STEDMA40_PCHAN_MODULO_MODE]
135220a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_MODULO,
135320a5b6d0SRabin Vincent [STEDMA40_PCHAN_DOUBLE_DST_MODE]
135420a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
135520a5b6d0SRabin Vincent };
135620a5b6d0SRabin Vincent static const unsigned int log_map[] = {
135720a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
135820a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
135920a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
136020a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
136120a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
136220a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
136320a5b6d0SRabin Vincent };
136420a5b6d0SRabin Vincent
1365724a8577SRabin Vincent if (chan_is_physical(d40c))
136620a5b6d0SRabin Vincent return phy_map[d40c->dma_cfg.mode_opt];
136720a5b6d0SRabin Vincent else
136820a5b6d0SRabin Vincent return log_map[d40c->dma_cfg.mode_opt];
136920a5b6d0SRabin Vincent }
137020a5b6d0SRabin Vincent
d40_config_write(struct d40_chan * d40c)1371b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c)
13728d318a50SLinus Walleij {
13738d318a50SLinus Walleij u32 addr_base;
13748d318a50SLinus Walleij u32 var;
13758d318a50SLinus Walleij
13768d318a50SLinus Walleij /* Odd addresses are even addresses + 4 */
13778d318a50SLinus Walleij addr_base = (d40c->phy_chan->num % 2) * 4;
13788d318a50SLinus Walleij /* Setup channel mode to logical or physical */
1379724a8577SRabin Vincent var = ((u32)(chan_is_logical(d40c)) + 1) <<
13808d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num);
13818d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
13828d318a50SLinus Walleij
13838d318a50SLinus Walleij /* Setup operational mode option register */
138420a5b6d0SRabin Vincent var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
13858d318a50SLinus Walleij
13868d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
13878d318a50SLinus Walleij
1388724a8577SRabin Vincent if (chan_is_logical(d40c)) {
13898ca84687SRabin Vincent int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
13908ca84687SRabin Vincent & D40_SREG_ELEM_LOG_LIDX_MASK;
13918ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c);
13928ca84687SRabin Vincent
13938d318a50SLinus Walleij /* Set default config for CFG reg */
13948ca84687SRabin Vincent writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
13958ca84687SRabin Vincent writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
13968d318a50SLinus Walleij
1397b55912c6SJonas Aaberg /* Set LIDX for lcla */
13988ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SSELT);
13998ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SDELT);
1400e9f3a49cSRabin Vincent
1401e9f3a49cSRabin Vincent /* Clear LNK which will be used by d40_chan_has_events() */
1402e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SSLNK);
1403e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SDLNK);
14048d318a50SLinus Walleij }
14058d318a50SLinus Walleij }
14068d318a50SLinus Walleij
d40_residue(struct d40_chan * d40c)1407aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c)
1408aa182ae2SJonas Aaberg {
1409aa182ae2SJonas Aaberg u32 num_elt;
1410aa182ae2SJonas Aaberg
1411724a8577SRabin Vincent if (chan_is_logical(d40c))
1412aa182ae2SJonas Aaberg num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
1413aa182ae2SJonas Aaberg >> D40_MEM_LCSP2_ECNT_POS;
14148ca84687SRabin Vincent else {
14158ca84687SRabin Vincent u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
14168ca84687SRabin Vincent num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
14178ca84687SRabin Vincent >> D40_SREG_ELEM_PHY_ECNT_POS;
14188ca84687SRabin Vincent }
14198ca84687SRabin Vincent
142043f2e1a3SLee Jones return num_elt * d40c->dma_cfg.dst_info.data_width;
1421aa182ae2SJonas Aaberg }
1422aa182ae2SJonas Aaberg
d40_tx_is_linked(struct d40_chan * d40c)1423aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c)
1424aa182ae2SJonas Aaberg {
1425aa182ae2SJonas Aaberg bool is_link;
1426aa182ae2SJonas Aaberg
1427724a8577SRabin Vincent if (chan_is_logical(d40c))
1428aa182ae2SJonas Aaberg is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
1429aa182ae2SJonas Aaberg else
14308ca84687SRabin Vincent is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
14318ca84687SRabin Vincent & D40_SREG_LNK_PHYS_LNK_MASK;
14328ca84687SRabin Vincent
1433aa182ae2SJonas Aaberg return is_link;
1434aa182ae2SJonas Aaberg }
1435aa182ae2SJonas Aaberg
d40_pause(struct dma_chan * chan)14366f5bad03SMaxime Ripard static int d40_pause(struct dma_chan *chan)
1437aa182ae2SJonas Aaberg {
14386f5bad03SMaxime Ripard struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1439aa182ae2SJonas Aaberg int res = 0;
1440aa182ae2SJonas Aaberg unsigned long flags;
1441aa182ae2SJonas Aaberg
14426f5bad03SMaxime Ripard if (d40c->phy_chan == NULL) {
14436f5bad03SMaxime Ripard chan_err(d40c, "Channel is not allocated!\n");
14446f5bad03SMaxime Ripard return -EINVAL;
14456f5bad03SMaxime Ripard }
14466f5bad03SMaxime Ripard
14473ac012afSJonas Aaberg if (!d40c->busy)
14483ac012afSJonas Aaberg return 0;
14493ac012afSJonas Aaberg
1450aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags);
145180245216SUlf Hansson pm_runtime_get_sync(d40c->base->dev);
1452aa182ae2SJonas Aaberg
1453aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
14541bdae6f4SNarayanan G
14557fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
14567fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
1457aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags);
1458aa182ae2SJonas Aaberg return res;
1459aa182ae2SJonas Aaberg }
1460aa182ae2SJonas Aaberg
d40_resume(struct dma_chan * chan)14616f5bad03SMaxime Ripard static int d40_resume(struct dma_chan *chan)
1462aa182ae2SJonas Aaberg {
14636f5bad03SMaxime Ripard struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
1464aa182ae2SJonas Aaberg int res = 0;
1465aa182ae2SJonas Aaberg unsigned long flags;
1466aa182ae2SJonas Aaberg
14676f5bad03SMaxime Ripard if (d40c->phy_chan == NULL) {
14686f5bad03SMaxime Ripard chan_err(d40c, "Channel is not allocated!\n");
14696f5bad03SMaxime Ripard return -EINVAL;
14706f5bad03SMaxime Ripard }
14716f5bad03SMaxime Ripard
14723ac012afSJonas Aaberg if (!d40c->busy)
14733ac012afSJonas Aaberg return 0;
14743ac012afSJonas Aaberg
1475aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags);
14767fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev);
1477aa182ae2SJonas Aaberg
1478aa182ae2SJonas Aaberg /* If bytes left to transfer or linked tx resume job */
14791bdae6f4SNarayanan G if (d40_residue(d40c) || d40_tx_is_linked(d40c))
1480aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_RUN);
1481aa182ae2SJonas Aaberg
14827fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
14837fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
1484aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags);
1485aa182ae2SJonas Aaberg return res;
1486aa182ae2SJonas Aaberg }
1487aa182ae2SJonas Aaberg
d40_tx_submit(struct dma_async_tx_descriptor * tx)14888d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
14898d318a50SLinus Walleij {
14908d318a50SLinus Walleij struct d40_chan *d40c = container_of(tx->chan,
14918d318a50SLinus Walleij struct d40_chan,
14928d318a50SLinus Walleij chan);
14938d318a50SLinus Walleij struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
14948d318a50SLinus Walleij unsigned long flags;
1495884485e1SRussell King - ARM Linux dma_cookie_t cookie;
14968d318a50SLinus Walleij
14978d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
1498884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx);
14998d318a50SLinus Walleij d40_desc_queue(d40c, d40d);
15008d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
15018d318a50SLinus Walleij
1502884485e1SRussell King - ARM Linux return cookie;
15038d318a50SLinus Walleij }
15048d318a50SLinus Walleij
d40_start(struct d40_chan * d40c)15058d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c)
15068d318a50SLinus Walleij {
15070c32269dSJonas Aaberg return d40_channel_execute_command(d40c, D40_DMA_RUN);
15088d318a50SLinus Walleij }
15098d318a50SLinus Walleij
d40_queue_start(struct d40_chan * d40c)15108d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
15118d318a50SLinus Walleij {
15128d318a50SLinus Walleij struct d40_desc *d40d;
15138d318a50SLinus Walleij int err;
15148d318a50SLinus Walleij
15158d318a50SLinus Walleij /* Start queued jobs, if any */
15168d318a50SLinus Walleij d40d = d40_first_queued(d40c);
15178d318a50SLinus Walleij
15188d318a50SLinus Walleij if (d40d != NULL) {
15191bdae6f4SNarayanan G if (!d40c->busy) {
15208d318a50SLinus Walleij d40c->busy = true;
15217fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev);
15221bdae6f4SNarayanan G }
15237fb3e75eSNarayanan G
15248d318a50SLinus Walleij /* Remove from queue */
15258d318a50SLinus Walleij d40_desc_remove(d40d);
15268d318a50SLinus Walleij
15278d318a50SLinus Walleij /* Add to active queue */
15288d318a50SLinus Walleij d40_desc_submit(d40c, d40d);
15298d318a50SLinus Walleij
15308d318a50SLinus Walleij /* Initiate DMA job */
15318d318a50SLinus Walleij d40_desc_load(d40c, d40d);
15328d318a50SLinus Walleij
15338d318a50SLinus Walleij /* Start dma job */
15348d318a50SLinus Walleij err = d40_start(d40c);
15358d318a50SLinus Walleij
15368d318a50SLinus Walleij if (err)
15378d318a50SLinus Walleij return NULL;
15388d318a50SLinus Walleij }
15398d318a50SLinus Walleij
15408d318a50SLinus Walleij return d40d;
15418d318a50SLinus Walleij }
15428d318a50SLinus Walleij
15438d318a50SLinus Walleij /* called from interrupt context */
dma_tc_handle(struct d40_chan * d40c)15448d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c)
15458d318a50SLinus Walleij {
15468d318a50SLinus Walleij struct d40_desc *d40d;
15478d318a50SLinus Walleij
15488d318a50SLinus Walleij /* Get first active entry from list */
15498d318a50SLinus Walleij d40d = d40_first_active_get(d40c);
15508d318a50SLinus Walleij
15518d318a50SLinus Walleij if (d40d == NULL)
15528d318a50SLinus Walleij return;
15538d318a50SLinus Walleij
15540c842b55SRabin Vincent if (d40d->cyclic) {
15550c842b55SRabin Vincent /*
15560c842b55SRabin Vincent * If this was a paritially loaded list, we need to reloaded
15570c842b55SRabin Vincent * it, and only when the list is completed. We need to check
15580c842b55SRabin Vincent * for done because the interrupt will hit for every link, and
15590c842b55SRabin Vincent * not just the last one.
15600c842b55SRabin Vincent */
15610c842b55SRabin Vincent if (d40d->lli_current < d40d->lli_len
15620c842b55SRabin Vincent && !d40_tx_is_linked(d40c)
15630c842b55SRabin Vincent && !d40_residue(d40c)) {
15640c842b55SRabin Vincent d40_lcla_free_all(d40c, d40d);
15650c842b55SRabin Vincent d40_desc_load(d40c, d40d);
15660c842b55SRabin Vincent (void) d40_start(d40c);
15670c842b55SRabin Vincent
15680c842b55SRabin Vincent if (d40d->lli_current == d40d->lli_len)
15690c842b55SRabin Vincent d40d->lli_current = 0;
15700c842b55SRabin Vincent }
15710c842b55SRabin Vincent } else {
1572698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d);
15738d318a50SLinus Walleij
1574698e4732SJonas Aaberg if (d40d->lli_current < d40d->lli_len) {
15758d318a50SLinus Walleij d40_desc_load(d40c, d40d);
15768d318a50SLinus Walleij /* Start dma job */
15778d318a50SLinus Walleij (void) d40_start(d40c);
15788d318a50SLinus Walleij return;
15798d318a50SLinus Walleij }
15808d318a50SLinus Walleij
15819ecb41bdSRabin Vincent if (d40_queue_start(d40c) == NULL) {
15828d318a50SLinus Walleij d40c->busy = false;
15839ecb41bdSRabin Vincent
15847fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
15857fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
15869ecb41bdSRabin Vincent }
15878d318a50SLinus Walleij
15884226dd86SFabio Baltieri d40_desc_remove(d40d);
15894226dd86SFabio Baltieri d40_desc_done(d40c, d40d);
15907dd14525SFabio Baltieri }
15914226dd86SFabio Baltieri
15928d318a50SLinus Walleij d40c->pending_tx++;
15938d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet);
15948d318a50SLinus Walleij
15958d318a50SLinus Walleij }
15968d318a50SLinus Walleij
dma_tasklet(struct tasklet_struct * t)1597b1880c90SAllen Pais static void dma_tasklet(struct tasklet_struct *t)
15988d318a50SLinus Walleij {
1599b1880c90SAllen Pais struct d40_chan *d40c = from_tasklet(d40c, t, tasklet);
1600767a9675SJonas Aaberg struct d40_desc *d40d;
16018d318a50SLinus Walleij unsigned long flags;
1602e9baa9d9SLinus Walleij bool callback_active;
16033a315d5dSDave Jiang struct dmaengine_desc_callback cb;
16048d318a50SLinus Walleij
16058d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
16068d318a50SLinus Walleij
16074226dd86SFabio Baltieri /* Get first entry from the done list */
16084226dd86SFabio Baltieri d40d = d40_first_done(d40c);
16094226dd86SFabio Baltieri if (d40d == NULL) {
16104226dd86SFabio Baltieri /* Check if we have reached here for cyclic job */
1611767a9675SJonas Aaberg d40d = d40_first_active_get(d40c);
16124226dd86SFabio Baltieri if (d40d == NULL || !d40d->cyclic)
1613d4cd217aSMarkus Elfring goto check_pending_tx;
16144226dd86SFabio Baltieri }
16158d318a50SLinus Walleij
16160c842b55SRabin Vincent if (!d40d->cyclic)
1617f7fbce07SRussell King - ARM Linux dma_cookie_complete(&d40d->txd);
16188d318a50SLinus Walleij
16198d318a50SLinus Walleij /*
16208d318a50SLinus Walleij * If terminating a channel pending_tx is set to zero.
16218d318a50SLinus Walleij * This prevents any finished active jobs to return to the client.
16228d318a50SLinus Walleij */
16238d318a50SLinus Walleij if (d40c->pending_tx == 0) {
16248d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
16258d318a50SLinus Walleij return;
16268d318a50SLinus Walleij }
16278d318a50SLinus Walleij
16288d318a50SLinus Walleij /* Callback to client */
1629e9baa9d9SLinus Walleij callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
16303a315d5dSDave Jiang dmaengine_desc_get_callback(&d40d->txd, &cb);
16318d318a50SLinus Walleij
16320c842b55SRabin Vincent if (!d40d->cyclic) {
1633767a9675SJonas Aaberg if (async_tx_test_ack(&d40d->txd)) {
1634767a9675SJonas Aaberg d40_desc_remove(d40d);
1635767a9675SJonas Aaberg d40_desc_free(d40c, d40d);
1636f26e03adSFabio Baltieri } else if (!d40d->is_in_client_list) {
1637767a9675SJonas Aaberg d40_desc_remove(d40d);
1638698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d);
1639767a9675SJonas Aaberg list_add_tail(&d40d->node, &d40c->client);
1640767a9675SJonas Aaberg d40d->is_in_client_list = true;
16418d318a50SLinus Walleij }
16428d318a50SLinus Walleij }
16438d318a50SLinus Walleij
16448d318a50SLinus Walleij d40c->pending_tx--;
16458d318a50SLinus Walleij
16468d318a50SLinus Walleij if (d40c->pending_tx)
16478d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet);
16488d318a50SLinus Walleij
16498d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
16508d318a50SLinus Walleij
16513a315d5dSDave Jiang if (callback_active)
16523a315d5dSDave Jiang dmaengine_desc_callback_invoke(&cb, NULL);
16538d318a50SLinus Walleij
16548d318a50SLinus Walleij return;
1655d4cd217aSMarkus Elfring check_pending_tx:
165671a5197eSRandy Dunlap /* Rescue maneuver if receiving double interrupts */
16578d318a50SLinus Walleij if (d40c->pending_tx > 0)
16588d318a50SLinus Walleij d40c->pending_tx--;
16598d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
16608d318a50SLinus Walleij }
16618d318a50SLinus Walleij
d40_handle_interrupt(int irq,void * data)16628d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data)
16638d318a50SLinus Walleij {
16648d318a50SLinus Walleij int i;
16658d318a50SLinus Walleij u32 idx;
16668d318a50SLinus Walleij u32 row;
16678d318a50SLinus Walleij long chan = -1;
16688d318a50SLinus Walleij struct d40_chan *d40c;
16698d318a50SLinus Walleij struct d40_base *base = data;
1670e6a78511SKees Cook u32 *regs = base->regs_interrupt;
16713cb645dcSTong Liu struct d40_interrupt_lookup *il = base->gen_dmac.il;
16723cb645dcSTong Liu u32 il_size = base->gen_dmac.il_size;
16738d318a50SLinus Walleij
1674618a8e38SBarry Song spin_lock(&base->interrupt_lock);
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
1719618a8e38SBarry Song spin_unlock(&base->interrupt_lock);
17208d318a50SLinus Walleij
17218d318a50SLinus Walleij return IRQ_HANDLED;
17228d318a50SLinus Walleij }
17238d318a50SLinus Walleij
d40_validate_conf(struct d40_chan * d40c,struct stedma40_chan_cfg * conf)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
d40_alloc_mask_set(struct d40_phy_res * phy,bool is_src,int log_event_line,bool is_log,bool * first_user)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;
17838eff80e4SMarkus Elfring goto found_unlock;
17848d318a50SLinus Walleij } else
17858eff80e4SMarkus Elfring goto not_found_unlock;
17868d318a50SLinus Walleij }
17878d318a50SLinus Walleij
17888d318a50SLinus Walleij /* Logical channel */
17898d318a50SLinus Walleij if (is_src) {
17908d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_PHY)
17918eff80e4SMarkus Elfring goto not_found_unlock;
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);
17988eff80e4SMarkus Elfring goto found_unlock;
17998d318a50SLinus Walleij } else
18008eff80e4SMarkus Elfring goto not_found_unlock;
18018d318a50SLinus Walleij } else {
18028d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_PHY)
18038eff80e4SMarkus Elfring goto not_found_unlock;
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);
18108eff80e4SMarkus Elfring goto found_unlock;
18118d318a50SLinus Walleij }
18128eff80e4SMarkus Elfring }
18138eff80e4SMarkus Elfring not_found_unlock:
18148d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags);
18158d318a50SLinus Walleij return false;
18168eff80e4SMarkus Elfring found_unlock:
18178d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags);
18188d318a50SLinus Walleij return true;
18198d318a50SLinus Walleij }
18208d318a50SLinus Walleij
d40_alloc_mask_free(struct d40_phy_res * phy,bool is_src,int log_event_line)18218d318a50SLinus Walleij static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
18228d318a50SLinus Walleij int log_event_line)
18238d318a50SLinus Walleij {
18248d318a50SLinus Walleij unsigned long flags;
18258d318a50SLinus Walleij bool is_free = false;
18268d318a50SLinus Walleij
18278d318a50SLinus Walleij spin_lock_irqsave(&phy->lock, flags);
18288d318a50SLinus Walleij if (!log_event_line) {
18298d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE;
18308d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE;
18318d318a50SLinus Walleij is_free = true;
1832f19b8ee8SMarkus Elfring goto unlock;
18338d318a50SLinus Walleij }
18348d318a50SLinus Walleij
18358d318a50SLinus Walleij /* Logical channel */
18368d318a50SLinus Walleij if (is_src) {
18378a3b6e14SLee Jones phy->allocated_src &= ~BIT(log_event_line);
18388d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_LOG_FREE)
18398d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE;
18408d318a50SLinus Walleij } else {
18418a3b6e14SLee Jones phy->allocated_dst &= ~BIT(log_event_line);
18428d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
18438d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE;
18448d318a50SLinus Walleij }
18458d318a50SLinus Walleij
18468d318a50SLinus Walleij is_free = ((phy->allocated_src | phy->allocated_dst) ==
18478d318a50SLinus Walleij D40_ALLOC_FREE);
1848f19b8ee8SMarkus Elfring unlock:
18498d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags);
18508d318a50SLinus Walleij
18518d318a50SLinus Walleij return is_free;
18528d318a50SLinus Walleij }
18538d318a50SLinus Walleij
d40_allocate_channel(struct d40_chan * d40c,bool * first_phy_user)18545cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
18558d318a50SLinus Walleij {
185626955c07SLee Jones int dev_type = d40c->dma_cfg.dev_type;
18578d318a50SLinus Walleij int event_group;
18588d318a50SLinus Walleij int event_line;
18598d318a50SLinus Walleij struct d40_phy_res *phys;
18608d318a50SLinus Walleij int i;
18618d318a50SLinus Walleij int j;
18628d318a50SLinus Walleij int log_num;
1863f000df8cSGerald Baeza int num_phy_chans;
18648d318a50SLinus Walleij bool is_src;
186538bdbf02SRabin Vincent bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
18668d318a50SLinus Walleij
18678d318a50SLinus Walleij phys = d40c->base->phy_res;
1868f000df8cSGerald Baeza num_phy_chans = d40c->base->num_phy_chans;
18698d318a50SLinus Walleij
18702c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
18718d318a50SLinus Walleij log_num = 2 * dev_type;
18728d318a50SLinus Walleij is_src = true;
18732c2b62d5SLee Jones } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
18742c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18758d318a50SLinus Walleij /* dst event lines are used for logical memcpy */
18768d318a50SLinus Walleij log_num = 2 * dev_type + 1;
18778d318a50SLinus Walleij is_src = false;
18788d318a50SLinus Walleij } else
18798d318a50SLinus Walleij return -EINVAL;
18808d318a50SLinus Walleij
18818d318a50SLinus Walleij event_group = D40_TYPE_TO_GROUP(dev_type);
18828d318a50SLinus Walleij event_line = D40_TYPE_TO_EVENT(dev_type);
18838d318a50SLinus Walleij
18848d318a50SLinus Walleij if (!is_log) {
18852c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
18868d318a50SLinus Walleij /* Find physical half channel */
1887f000df8cSGerald Baeza if (d40c->dma_cfg.use_fixed_channel) {
1888f000df8cSGerald Baeza i = d40c->dma_cfg.phy_channel;
18894aed79b2SMarcin Mielczarczyk if (d40_alloc_mask_set(&phys[i], is_src,
18905cd326fdSNarayanan G 0, is_log,
18915cd326fdSNarayanan G first_phy_user))
18928d318a50SLinus Walleij goto found_phy;
1893f000df8cSGerald Baeza } else {
1894f000df8cSGerald Baeza for (i = 0; i < num_phy_chans; i++) {
1895f000df8cSGerald Baeza if (d40_alloc_mask_set(&phys[i], is_src,
1896f000df8cSGerald Baeza 0, is_log,
1897f000df8cSGerald Baeza first_phy_user))
1898f000df8cSGerald Baeza goto found_phy;
1899f000df8cSGerald Baeza }
19008d318a50SLinus Walleij }
19018d318a50SLinus Walleij } else
19028d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19038d318a50SLinus Walleij int phy_num = j + event_group * 2;
19048d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) {
1905508849adSLinus Walleij if (d40_alloc_mask_set(&phys[i],
1906508849adSLinus Walleij is_src,
1907508849adSLinus Walleij 0,
19085cd326fdSNarayanan G is_log,
19095cd326fdSNarayanan G first_phy_user))
19108d318a50SLinus Walleij goto found_phy;
19118d318a50SLinus Walleij }
19128d318a50SLinus Walleij }
19138d318a50SLinus Walleij return -EINVAL;
19148d318a50SLinus Walleij found_phy:
19158d318a50SLinus Walleij d40c->phy_chan = &phys[i];
19168d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN;
19178d318a50SLinus Walleij goto out;
19188d318a50SLinus Walleij }
19198d318a50SLinus Walleij if (dev_type == -1)
19208d318a50SLinus Walleij return -EINVAL;
19218d318a50SLinus Walleij
19228d318a50SLinus Walleij /* Find logical channel */
19238d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
19248d318a50SLinus Walleij int phy_num = j + event_group * 2;
19255cd326fdSNarayanan G
19265cd326fdSNarayanan G if (d40c->dma_cfg.use_fixed_channel) {
19275cd326fdSNarayanan G i = d40c->dma_cfg.phy_channel;
19285cd326fdSNarayanan G
19295cd326fdSNarayanan G if ((i != phy_num) && (i != phy_num + 1)) {
19305cd326fdSNarayanan G dev_err(chan2dev(d40c),
19315cd326fdSNarayanan G "invalid fixed phy channel %d\n", i);
19325cd326fdSNarayanan G return -EINVAL;
19335cd326fdSNarayanan G }
19345cd326fdSNarayanan G
19355cd326fdSNarayanan G if (d40_alloc_mask_set(&phys[i], is_src, event_line,
19365cd326fdSNarayanan G is_log, first_phy_user))
19375cd326fdSNarayanan G goto found_log;
19385cd326fdSNarayanan G
19395cd326fdSNarayanan G dev_err(chan2dev(d40c),
19405cd326fdSNarayanan G "could not allocate fixed phy channel %d\n", i);
19415cd326fdSNarayanan G return -EINVAL;
19425cd326fdSNarayanan G }
19435cd326fdSNarayanan G
19448d318a50SLinus Walleij /*
19458d318a50SLinus Walleij * Spread logical channels across all available physical rather
19468d318a50SLinus Walleij * than pack every logical channel at the first available phy
19478d318a50SLinus Walleij * channels.
19488d318a50SLinus Walleij */
19498d318a50SLinus Walleij if (is_src) {
19508d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) {
19518d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src,
19525cd326fdSNarayanan G event_line, is_log,
19535cd326fdSNarayanan G first_phy_user))
19548d318a50SLinus Walleij goto found_log;
19558d318a50SLinus Walleij }
19568d318a50SLinus Walleij } else {
19578d318a50SLinus Walleij for (i = phy_num + 1; i >= phy_num; i--) {
19588d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src,
19595cd326fdSNarayanan G event_line, is_log,
19605cd326fdSNarayanan G first_phy_user))
19618d318a50SLinus Walleij goto found_log;
19628d318a50SLinus Walleij }
19638d318a50SLinus Walleij }
19648d318a50SLinus Walleij }
19658d318a50SLinus Walleij return -EINVAL;
19668d318a50SLinus Walleij
19678d318a50SLinus Walleij found_log:
19688d318a50SLinus Walleij d40c->phy_chan = &phys[i];
19698d318a50SLinus Walleij d40c->log_num = log_num;
19708d318a50SLinus Walleij out:
19718d318a50SLinus Walleij
19728d318a50SLinus Walleij if (is_log)
19738d318a50SLinus Walleij d40c->base->lookup_log_chans[d40c->log_num] = d40c;
19748d318a50SLinus Walleij else
19758d318a50SLinus Walleij d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
19768d318a50SLinus Walleij
19778d318a50SLinus Walleij return 0;
19788d318a50SLinus Walleij
19798d318a50SLinus Walleij }
19808d318a50SLinus Walleij
d40_config_memcpy(struct d40_chan * d40c)19818d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c)
19828d318a50SLinus Walleij {
19838d318a50SLinus Walleij dma_cap_mask_t cap = d40c->chan.device->cap_mask;
19848d318a50SLinus Walleij
19858d318a50SLinus Walleij if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
198629027a1eSLee Jones d40c->dma_cfg = dma40_memcpy_conf_log;
198726955c07SLee Jones d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
19888d318a50SLinus Walleij
19899b233f9bSLee Jones d40_log_cfg(&d40c->dma_cfg,
19909b233f9bSLee Jones &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
19919b233f9bSLee Jones
19928d318a50SLinus Walleij } else if (dma_has_cap(DMA_MEMCPY, cap) &&
19938d318a50SLinus Walleij dma_has_cap(DMA_SLAVE, cap)) {
199429027a1eSLee Jones d40c->dma_cfg = dma40_memcpy_conf_phy;
199557e65ad7SLee Jones
1996a71da24cSJulia Lawall /* Generate interrupt at end of transfer or relink. */
199757e65ad7SLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
199857e65ad7SLee Jones
199957e65ad7SLee Jones /* Generate interrupt on error. */
200057e65ad7SLee Jones d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200157e65ad7SLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
200257e65ad7SLee Jones
20038d318a50SLinus Walleij } else {
20046db5a8baSRabin Vincent chan_err(d40c, "No memcpy\n");
20058d318a50SLinus Walleij return -EINVAL;
20068d318a50SLinus Walleij }
20078d318a50SLinus Walleij
20088d318a50SLinus Walleij return 0;
20098d318a50SLinus Walleij }
20108d318a50SLinus Walleij
d40_free_dma(struct d40_chan * d40c)20118d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c)
20128d318a50SLinus Walleij {
20138d318a50SLinus Walleij
20148d318a50SLinus Walleij int res = 0;
201526955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
20168d318a50SLinus Walleij struct d40_phy_res *phy = d40c->phy_chan;
20178d318a50SLinus Walleij bool is_src;
20188d318a50SLinus Walleij
20198d318a50SLinus Walleij /* Terminate all queued and active transfers */
20208d318a50SLinus Walleij d40_term_all(d40c);
20218d318a50SLinus Walleij
20228d318a50SLinus Walleij if (phy == NULL) {
20236db5a8baSRabin Vincent chan_err(d40c, "phy == null\n");
20248d318a50SLinus Walleij return -EINVAL;
20258d318a50SLinus Walleij }
20268d318a50SLinus Walleij
20278d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE &&
20288d318a50SLinus Walleij phy->allocated_dst == D40_ALLOC_FREE) {
20296db5a8baSRabin Vincent chan_err(d40c, "channel already free\n");
20308d318a50SLinus Walleij return -EINVAL;
20318d318a50SLinus Walleij }
20328d318a50SLinus Walleij
20332c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
20342c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
20358d318a50SLinus Walleij is_src = false;
20362c2b62d5SLee Jones else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
20378d318a50SLinus Walleij is_src = true;
203826955c07SLee Jones else {
20396db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n");
20408d318a50SLinus Walleij return -EINVAL;
20418d318a50SLinus Walleij }
20428d318a50SLinus Walleij
20437fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev);
20448d318a50SLinus Walleij res = d40_channel_execute_command(d40c, D40_DMA_STOP);
20458d318a50SLinus Walleij if (res) {
20461bdae6f4SNarayanan G chan_err(d40c, "stop failed\n");
2047e714b470SMarkus Elfring goto mark_last_busy;
20488d318a50SLinus Walleij }
20497fb3e75eSNarayanan G
20501bdae6f4SNarayanan G d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
20511bdae6f4SNarayanan G
20521bdae6f4SNarayanan G if (chan_is_logical(d40c))
20531bdae6f4SNarayanan G d40c->base->lookup_log_chans[d40c->log_num] = NULL;
20541bdae6f4SNarayanan G else
20551bdae6f4SNarayanan G d40c->base->lookup_phy_chans[phy->num] = NULL;
20561bdae6f4SNarayanan G
20577fb3e75eSNarayanan G if (d40c->busy) {
20587fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
20597fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
20607fb3e75eSNarayanan G }
20617fb3e75eSNarayanan G
20627fb3e75eSNarayanan G d40c->busy = false;
20638d318a50SLinus Walleij d40c->phy_chan = NULL;
2064ce2ca125SRabin Vincent d40c->configured = false;
2065e714b470SMarkus Elfring mark_last_busy:
20667fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
20677fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
20687fb3e75eSNarayanan G return res;
20698d318a50SLinus Walleij }
20708d318a50SLinus Walleij
d40_is_paused(struct d40_chan * d40c)2071a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c)
2072a5ebca47SJonas Aaberg {
20738ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c);
2074a5ebca47SJonas Aaberg bool is_paused = false;
2075a5ebca47SJonas Aaberg unsigned long flags;
2076a5ebca47SJonas Aaberg void __iomem *active_reg;
2077a5ebca47SJonas Aaberg u32 status;
207826955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
2079a5ebca47SJonas Aaberg
2080a5ebca47SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags);
2081a5ebca47SJonas Aaberg
2082724a8577SRabin Vincent if (chan_is_physical(d40c)) {
2083a5ebca47SJonas Aaberg if (d40c->phy_chan->num % 2 == 0)
2084a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
2085a5ebca47SJonas Aaberg else
2086a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
2087a5ebca47SJonas Aaberg
2088a5ebca47SJonas Aaberg status = (readl(active_reg) &
2089a5ebca47SJonas Aaberg D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
2090a5ebca47SJonas Aaberg D40_CHAN_POS(d40c->phy_chan->num);
2091a5ebca47SJonas Aaberg if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
2092a5ebca47SJonas Aaberg is_paused = true;
20935a5eecb3SMarkus Elfring goto unlock;
2094a5ebca47SJonas Aaberg }
2095a5ebca47SJonas Aaberg
20962c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
20972c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
20988ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SDLNK);
20992c2b62d5SLee Jones } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
21008ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SSLNK);
21019dbfbd35SJonas Aaberg } else {
21026db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n");
21035a5eecb3SMarkus Elfring goto unlock;
2104a5ebca47SJonas Aaberg }
21059dbfbd35SJonas Aaberg
2106a5ebca47SJonas Aaberg status = (status & D40_EVENTLINE_MASK(event)) >>
2107a5ebca47SJonas Aaberg D40_EVENTLINE_POS(event);
2108a5ebca47SJonas Aaberg
2109a5ebca47SJonas Aaberg if (status != D40_DMA_RUN)
2110a5ebca47SJonas Aaberg is_paused = true;
21115a5eecb3SMarkus Elfring unlock:
2112a5ebca47SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags);
2113a5ebca47SJonas Aaberg return is_paused;
2114a5ebca47SJonas Aaberg
2115a5ebca47SJonas Aaberg }
2116a5ebca47SJonas Aaberg
stedma40_residue(struct dma_chan * chan)21178d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan)
21188d318a50SLinus Walleij {
21198d318a50SLinus Walleij struct d40_chan *d40c =
21208d318a50SLinus Walleij container_of(chan, struct d40_chan, chan);
21218d318a50SLinus Walleij u32 bytes_left;
21228d318a50SLinus Walleij unsigned long flags;
21238d318a50SLinus Walleij
21248d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
21258d318a50SLinus Walleij bytes_left = d40_residue(d40c);
21268d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
21278d318a50SLinus Walleij
21288d318a50SLinus Walleij return bytes_left;
21298d318a50SLinus Walleij }
21308d318a50SLinus Walleij
21313e3a0763SRabin Vincent static int
d40_prep_sg_log(struct d40_chan * chan,struct d40_desc * desc,struct scatterlist * sg_src,struct scatterlist * sg_dst,unsigned int sg_len,dma_addr_t src_dev_addr,dma_addr_t dst_dev_addr)21323e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
21333e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst,
2134822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr,
2135822c5676SRabin Vincent dma_addr_t dst_dev_addr)
21363e3a0763SRabin Vincent {
21373e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21383e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info;
21393e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21405ed04b85SRabin Vincent int ret;
21413e3a0763SRabin Vincent
21425ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_src, sg_len,
21435ed04b85SRabin Vincent src_dev_addr,
21443e3a0763SRabin Vincent desc->lli_log.src,
21453e3a0763SRabin Vincent chan->log_def.lcsp1,
21463e3a0763SRabin Vincent src_info->data_width,
21473e3a0763SRabin Vincent dst_info->data_width);
21483e3a0763SRabin Vincent
21495ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_dst, sg_len,
21505ed04b85SRabin Vincent dst_dev_addr,
21513e3a0763SRabin Vincent desc->lli_log.dst,
21523e3a0763SRabin Vincent chan->log_def.lcsp3,
21533e3a0763SRabin Vincent dst_info->data_width,
21543e3a0763SRabin Vincent src_info->data_width);
21553e3a0763SRabin Vincent
21565ed04b85SRabin Vincent return ret < 0 ? ret : 0;
21573e3a0763SRabin Vincent }
21583e3a0763SRabin Vincent
21593e3a0763SRabin Vincent static int
d40_prep_sg_phy(struct d40_chan * chan,struct d40_desc * desc,struct scatterlist * sg_src,struct scatterlist * sg_dst,unsigned int sg_len,dma_addr_t src_dev_addr,dma_addr_t dst_dev_addr)21603e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
21613e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst,
2162822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr,
2163822c5676SRabin Vincent dma_addr_t dst_dev_addr)
21643e3a0763SRabin Vincent {
21653e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
21663e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info;
21673e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
21680c842b55SRabin Vincent unsigned long flags = 0;
21693e3a0763SRabin Vincent int ret;
21703e3a0763SRabin Vincent
21710c842b55SRabin Vincent if (desc->cyclic)
21720c842b55SRabin Vincent flags |= LLI_CYCLIC | LLI_TERM_INT;
21730c842b55SRabin Vincent
21743e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
21753e3a0763SRabin Vincent desc->lli_phy.src,
21763e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.src),
21773e3a0763SRabin Vincent chan->src_def_cfg,
21780c842b55SRabin Vincent src_info, dst_info, flags);
21793e3a0763SRabin Vincent
21803e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
21813e3a0763SRabin Vincent desc->lli_phy.dst,
21823e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.dst),
21833e3a0763SRabin Vincent chan->dst_def_cfg,
21840c842b55SRabin Vincent dst_info, src_info, flags);
21853e3a0763SRabin Vincent
21863e3a0763SRabin Vincent dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
21873e3a0763SRabin Vincent desc->lli_pool.size, DMA_TO_DEVICE);
21883e3a0763SRabin Vincent
21893e3a0763SRabin Vincent return ret < 0 ? ret : 0;
21903e3a0763SRabin Vincent }
21913e3a0763SRabin Vincent
21925f81158fSRabin Vincent static struct d40_desc *
d40_prep_desc(struct d40_chan * chan,struct scatterlist * sg,unsigned int sg_len,unsigned long dma_flags)21935f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
21945f81158fSRabin Vincent unsigned int sg_len, unsigned long dma_flags)
21955f81158fSRabin Vincent {
219686145910SMarkus Elfring struct stedma40_chan_cfg *cfg;
21975f81158fSRabin Vincent struct d40_desc *desc;
2198dbd88788SRabin Vincent int ret;
21995f81158fSRabin Vincent
22005f81158fSRabin Vincent desc = d40_desc_get(chan);
22015f81158fSRabin Vincent if (!desc)
22025f81158fSRabin Vincent return NULL;
22035f81158fSRabin Vincent
220486145910SMarkus Elfring cfg = &chan->dma_cfg;
22055f81158fSRabin Vincent desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
22065f81158fSRabin Vincent cfg->dst_info.data_width);
22075f81158fSRabin Vincent if (desc->lli_len < 0) {
22085f81158fSRabin Vincent chan_err(chan, "Unaligned size\n");
2209254e1254SMarkus Elfring goto free_desc;
22105f81158fSRabin Vincent }
22115f81158fSRabin Vincent
2212dbd88788SRabin Vincent ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
2213dbd88788SRabin Vincent if (ret < 0) {
2214dbd88788SRabin Vincent chan_err(chan, "Could not allocate lli\n");
2215254e1254SMarkus Elfring goto free_desc;
2216dbd88788SRabin Vincent }
2217dbd88788SRabin Vincent
22185f81158fSRabin Vincent desc->lli_current = 0;
22195f81158fSRabin Vincent desc->txd.flags = dma_flags;
22205f81158fSRabin Vincent desc->txd.tx_submit = d40_tx_submit;
22215f81158fSRabin Vincent
22225f81158fSRabin Vincent dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
22235f81158fSRabin Vincent
22245f81158fSRabin Vincent return desc;
2225254e1254SMarkus Elfring free_desc:
2226dbd88788SRabin Vincent d40_desc_free(chan, desc);
2227dbd88788SRabin Vincent return NULL;
22285f81158fSRabin Vincent }
22295f81158fSRabin Vincent
2230cade1d30SRabin Vincent static struct dma_async_tx_descriptor *
d40_prep_sg(struct dma_chan * dchan,struct scatterlist * sg_src,struct scatterlist * sg_dst,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long dma_flags)2231cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
2232cade1d30SRabin Vincent struct scatterlist *sg_dst, unsigned int sg_len,
2233db8196dfSVinod Koul enum dma_transfer_direction direction, unsigned long dma_flags)
2234cade1d30SRabin Vincent {
2235cade1d30SRabin Vincent struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
2236444fa147SMarkus Elfring dma_addr_t src_dev_addr;
2237444fa147SMarkus Elfring dma_addr_t dst_dev_addr;
2238cade1d30SRabin Vincent struct d40_desc *desc;
2239cade1d30SRabin Vincent unsigned long flags;
2240cade1d30SRabin Vincent int ret;
22418d318a50SLinus Walleij
2242cade1d30SRabin Vincent if (!chan->phy_chan) {
2243cade1d30SRabin Vincent chan_err(chan, "Cannot prepare unallocated channel\n");
2244cade1d30SRabin Vincent return NULL;
2245cade1d30SRabin Vincent }
2246cade1d30SRabin Vincent
22479e314ef3SVinod Koul d40_set_runtime_config_write(dchan, &chan->slave_config, direction);
22489e314ef3SVinod Koul
2249cade1d30SRabin Vincent spin_lock_irqsave(&chan->lock, flags);
2250cade1d30SRabin Vincent
2251cade1d30SRabin Vincent desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
2252cade1d30SRabin Vincent if (desc == NULL)
225378c6e1a5SMarkus Elfring goto unlock;
22548d318a50SLinus Walleij
22550c842b55SRabin Vincent if (sg_next(&sg_src[sg_len - 1]) == sg_src)
22560c842b55SRabin Vincent desc->cyclic = true;
22570c842b55SRabin Vincent
2258444fa147SMarkus Elfring src_dev_addr = 0;
2259444fa147SMarkus Elfring dst_dev_addr = 0;
2260db8196dfSVinod Koul if (direction == DMA_DEV_TO_MEM)
2261ef9c89b3SLee Jones src_dev_addr = chan->runtime_addr;
2262db8196dfSVinod Koul else if (direction == DMA_MEM_TO_DEV)
2263ef9c89b3SLee Jones dst_dev_addr = chan->runtime_addr;
2264cade1d30SRabin Vincent
2265cade1d30SRabin Vincent if (chan_is_logical(chan))
2266cade1d30SRabin Vincent ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
2267822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr);
2268cade1d30SRabin Vincent else
2269cade1d30SRabin Vincent ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
2270822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr);
2271cade1d30SRabin Vincent
2272cade1d30SRabin Vincent if (ret) {
2273cade1d30SRabin Vincent chan_err(chan, "Failed to prepare %s sg job: %d\n",
2274cade1d30SRabin Vincent chan_is_logical(chan) ? "log" : "phy", ret);
227578c6e1a5SMarkus Elfring goto free_desc;
22768d318a50SLinus Walleij }
22778d318a50SLinus Walleij
227882babbb3SPer Forlin /*
227982babbb3SPer Forlin * add descriptor to the prepare queue in order to be able
228082babbb3SPer Forlin * to free them later in terminate_all
228182babbb3SPer Forlin */
228282babbb3SPer Forlin list_add_tail(&desc->node, &chan->prepare_queue);
228382babbb3SPer Forlin
2284cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags);
22858d318a50SLinus Walleij
2286cade1d30SRabin Vincent return &desc->txd;
228778c6e1a5SMarkus Elfring free_desc:
2288cade1d30SRabin Vincent d40_desc_free(chan, desc);
228978c6e1a5SMarkus Elfring unlock:
2290cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags);
22918d318a50SLinus Walleij return NULL;
22928d318a50SLinus Walleij }
22938d318a50SLinus Walleij
stedma40_filter(struct dma_chan * chan,void * data)229442ae6f16SLinus Walleij static bool stedma40_filter(struct dma_chan *chan, void *data)
22958d318a50SLinus Walleij {
22968d318a50SLinus Walleij struct stedma40_chan_cfg *info = data;
22978d318a50SLinus Walleij struct d40_chan *d40c =
22988d318a50SLinus Walleij container_of(chan, struct d40_chan, chan);
22998d318a50SLinus Walleij int err;
23008d318a50SLinus Walleij
23018d318a50SLinus Walleij if (data) {
23028d318a50SLinus Walleij err = d40_validate_conf(d40c, info);
23038d318a50SLinus Walleij if (!err)
23048d318a50SLinus Walleij d40c->dma_cfg = *info;
23058d318a50SLinus Walleij } else
23068d318a50SLinus Walleij err = d40_config_memcpy(d40c);
23078d318a50SLinus Walleij
2308ce2ca125SRabin Vincent if (!err)
2309ce2ca125SRabin Vincent d40c->configured = true;
2310ce2ca125SRabin Vincent
23118d318a50SLinus Walleij return err == 0;
23128d318a50SLinus Walleij }
23138d318a50SLinus Walleij
__d40_set_prio_rt(struct d40_chan * d40c,int dev_type,bool src)2314ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
2315ac2c0a38SRabin Vincent {
2316ac2c0a38SRabin Vincent bool realtime = d40c->dma_cfg.realtime;
2317ac2c0a38SRabin Vincent bool highprio = d40c->dma_cfg.high_priority;
23183cb645dcSTong Liu u32 rtreg;
2319ac2c0a38SRabin Vincent u32 event = D40_TYPE_TO_EVENT(dev_type);
2320ac2c0a38SRabin Vincent u32 group = D40_TYPE_TO_GROUP(dev_type);
23218a3b6e14SLee Jones u32 bit = BIT(event);
2322ccc3d697SRabin Vincent u32 prioreg;
23233cb645dcSTong Liu struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
2324ccc3d697SRabin Vincent
23253cb645dcSTong Liu rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
2326ccc3d697SRabin Vincent /*
2327ccc3d697SRabin Vincent * Due to a hardware bug, in some cases a logical channel triggered by
2328ccc3d697SRabin Vincent * a high priority destination event line can generate extra packet
2329ccc3d697SRabin Vincent * transactions.
2330ccc3d697SRabin Vincent *
2331ccc3d697SRabin Vincent * The workaround is to not set the high priority level for the
2332ccc3d697SRabin Vincent * destination event lines that trigger logical channels.
2333ccc3d697SRabin Vincent */
2334ccc3d697SRabin Vincent if (!src && chan_is_logical(d40c))
2335ccc3d697SRabin Vincent highprio = false;
2336ccc3d697SRabin Vincent
23373cb645dcSTong Liu prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
2338ac2c0a38SRabin Vincent
2339ac2c0a38SRabin Vincent /* Destination event lines are stored in the upper halfword */
2340ac2c0a38SRabin Vincent if (!src)
2341ac2c0a38SRabin Vincent bit <<= 16;
2342ac2c0a38SRabin Vincent
2343ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + prioreg + group * 4);
2344ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + rtreg + group * 4);
2345ac2c0a38SRabin Vincent }
2346ac2c0a38SRabin Vincent
d40_set_prio_realtime(struct d40_chan * d40c)2347ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c)
2348ac2c0a38SRabin Vincent {
2349ac2c0a38SRabin Vincent if (d40c->base->rev < 3)
2350ac2c0a38SRabin Vincent return;
2351ac2c0a38SRabin Vincent
23522c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
23532c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
235426955c07SLee Jones __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
2355ac2c0a38SRabin Vincent
23562c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
23572c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
235826955c07SLee Jones __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
2359ac2c0a38SRabin Vincent }
2360ac2c0a38SRabin Vincent
2361fa332de5SLee Jones #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
2362fa332de5SLee Jones #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
2363fa332de5SLee Jones #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
2364fa332de5SLee Jones #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
2365bddd5a2bSLee Jones #define D40_DT_FLAGS_HIGH_PRIO(flags) ((flags >> 4) & 0x1)
2366fa332de5SLee Jones
d40_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)2367fa332de5SLee Jones static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
2368fa332de5SLee Jones struct of_dma *ofdma)
2369fa332de5SLee Jones {
2370fa332de5SLee Jones struct stedma40_chan_cfg cfg;
2371fa332de5SLee Jones dma_cap_mask_t cap;
2372fa332de5SLee Jones u32 flags;
2373fa332de5SLee Jones
2374fa332de5SLee Jones memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
2375fa332de5SLee Jones
2376fa332de5SLee Jones dma_cap_zero(cap);
2377fa332de5SLee Jones dma_cap_set(DMA_SLAVE, cap);
2378fa332de5SLee Jones
2379fa332de5SLee Jones cfg.dev_type = dma_spec->args[0];
2380fa332de5SLee Jones flags = dma_spec->args[2];
2381fa332de5SLee Jones
2382fa332de5SLee Jones switch (D40_DT_FLAGS_MODE(flags)) {
2383fa332de5SLee Jones case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
2384fa332de5SLee Jones case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
2385fa332de5SLee Jones }
2386fa332de5SLee Jones
2387fa332de5SLee Jones switch (D40_DT_FLAGS_DIR(flags)) {
2388fa332de5SLee Jones case 0:
23892c2b62d5SLee Jones cfg.dir = DMA_MEM_TO_DEV;
2390fa332de5SLee Jones cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2391fa332de5SLee Jones break;
2392fa332de5SLee Jones case 1:
23932c2b62d5SLee Jones cfg.dir = DMA_DEV_TO_MEM;
2394fa332de5SLee Jones cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
2395fa332de5SLee Jones break;
2396fa332de5SLee Jones }
2397fa332de5SLee Jones
2398fa332de5SLee Jones if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
2399fa332de5SLee Jones cfg.phy_channel = dma_spec->args[1];
2400fa332de5SLee Jones cfg.use_fixed_channel = true;
2401fa332de5SLee Jones }
2402fa332de5SLee Jones
2403bddd5a2bSLee Jones if (D40_DT_FLAGS_HIGH_PRIO(flags))
2404bddd5a2bSLee Jones cfg.high_priority = true;
2405bddd5a2bSLee Jones
2406fa332de5SLee Jones return dma_request_channel(cap, stedma40_filter, &cfg);
2407fa332de5SLee Jones }
2408fa332de5SLee Jones
24098d318a50SLinus Walleij /* DMA ENGINE functions */
d40_alloc_chan_resources(struct dma_chan * chan)24108d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan)
24118d318a50SLinus Walleij {
24128d318a50SLinus Walleij int err;
24138d318a50SLinus Walleij unsigned long flags;
24148d318a50SLinus Walleij struct d40_chan *d40c =
24158d318a50SLinus Walleij container_of(chan, struct d40_chan, chan);
2416ef1872ecSLinus Walleij bool is_free_phy;
24178d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
24188d318a50SLinus Walleij
2419d3ee98cdSRussell King - ARM Linux dma_cookie_init(chan);
24208d318a50SLinus Walleij
2421ce2ca125SRabin Vincent /* If no dma configuration is set use default configuration (memcpy) */
2422ce2ca125SRabin Vincent if (!d40c->configured) {
24238d318a50SLinus Walleij err = d40_config_memcpy(d40c);
2424ff0b12baSJonas Aaberg if (err) {
24256db5a8baSRabin Vincent chan_err(d40c, "Failed to configure memcpy channel\n");
24268452b859SMarkus Elfring goto mark_last_busy;
2427ff0b12baSJonas Aaberg }
24288d318a50SLinus Walleij }
24298d318a50SLinus Walleij
24305cd326fdSNarayanan G err = d40_allocate_channel(d40c, &is_free_phy);
24318d318a50SLinus Walleij if (err) {
24326db5a8baSRabin Vincent chan_err(d40c, "Failed to allocate channel\n");
24337fb3e75eSNarayanan G d40c->configured = false;
24348452b859SMarkus Elfring goto mark_last_busy;
24358d318a50SLinus Walleij }
24368d318a50SLinus Walleij
24377fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev);
2438ef1872ecSLinus Walleij
2439ac2c0a38SRabin Vincent d40_set_prio_realtime(d40c);
2440ac2c0a38SRabin Vincent
2441724a8577SRabin Vincent if (chan_is_logical(d40c)) {
24422c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
2443ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base +
244426955c07SLee Jones d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
2445ef1872ecSLinus Walleij else
2446ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base +
244726955c07SLee Jones d40c->dma_cfg.dev_type *
2448ef1872ecSLinus Walleij D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
24499778256bSLee Jones
24509778256bSLee Jones /* Unmask the Global Interrupt Mask. */
24519778256bSLee Jones d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
24529778256bSLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
2453ef1872ecSLinus Walleij }
2454ef1872ecSLinus Walleij
24555cd326fdSNarayanan G dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
24565cd326fdSNarayanan G chan_is_logical(d40c) ? "logical" : "physical",
24575cd326fdSNarayanan G d40c->phy_chan->num,
24585cd326fdSNarayanan G d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
24595cd326fdSNarayanan G
24605cd326fdSNarayanan G
2461ef1872ecSLinus Walleij /*
2462ef1872ecSLinus Walleij * Only write channel configuration to the DMA if the physical
2463ef1872ecSLinus Walleij * resource is free. In case of multiple logical channels
2464ef1872ecSLinus Walleij * on the same physical resource, only the first write is necessary.
2465ef1872ecSLinus Walleij */
2466b55912c6SJonas Aaberg if (is_free_phy)
2467b55912c6SJonas Aaberg d40_config_write(d40c);
24688452b859SMarkus Elfring mark_last_busy:
24697fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
24707fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
24718d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
2472ff0b12baSJonas Aaberg return err;
24738d318a50SLinus Walleij }
24748d318a50SLinus Walleij
d40_free_chan_resources(struct dma_chan * chan)24758d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan)
24768d318a50SLinus Walleij {
24778d318a50SLinus Walleij struct d40_chan *d40c =
24788d318a50SLinus Walleij container_of(chan, struct d40_chan, chan);
24798d318a50SLinus Walleij int err;
24808d318a50SLinus Walleij unsigned long flags;
24818d318a50SLinus Walleij
24820d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) {
24836db5a8baSRabin Vincent chan_err(d40c, "Cannot free unallocated channel\n");
24840d0f6b8bSJonas Aaberg return;
24850d0f6b8bSJonas Aaberg }
24860d0f6b8bSJonas Aaberg
24878d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
24888d318a50SLinus Walleij
24898d318a50SLinus Walleij err = d40_free_dma(d40c);
24908d318a50SLinus Walleij
24918d318a50SLinus Walleij if (err)
24926db5a8baSRabin Vincent chan_err(d40c, "Failed to free channel\n");
24938d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
24948d318a50SLinus Walleij }
24958d318a50SLinus Walleij
d40_prep_memcpy(struct dma_chan * chan,dma_addr_t dst,dma_addr_t src,size_t size,unsigned long dma_flags)24968d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
24978d318a50SLinus Walleij dma_addr_t dst,
24988d318a50SLinus Walleij dma_addr_t src,
24998d318a50SLinus Walleij size_t size,
25002a614340SJonas Aaberg unsigned long dma_flags)
25018d318a50SLinus Walleij {
250295944c6eSRabin Vincent struct scatterlist dst_sg;
250395944c6eSRabin Vincent struct scatterlist src_sg;
25048d318a50SLinus Walleij
250595944c6eSRabin Vincent sg_init_table(&dst_sg, 1);
250695944c6eSRabin Vincent sg_init_table(&src_sg, 1);
25070d0f6b8bSJonas Aaberg
250895944c6eSRabin Vincent sg_dma_address(&dst_sg) = dst;
250995944c6eSRabin Vincent sg_dma_address(&src_sg) = src;
25108d318a50SLinus Walleij
251195944c6eSRabin Vincent sg_dma_len(&dst_sg) = size;
251295944c6eSRabin Vincent sg_dma_len(&src_sg) = size;
25138d318a50SLinus Walleij
2514de6b641eSStefan Agner return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
2515de6b641eSStefan Agner DMA_MEM_TO_MEM, dma_flags);
25168d318a50SLinus Walleij }
25178d318a50SLinus Walleij
25180d688662SIra Snyder static struct dma_async_tx_descriptor *
d40_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long dma_flags,void * context)2519f26e03adSFabio Baltieri d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
2520f26e03adSFabio Baltieri unsigned int sg_len, enum dma_transfer_direction direction,
2521f26e03adSFabio Baltieri unsigned long dma_flags, void *context)
25228d318a50SLinus Walleij {
2523a725dcc0SAndy Shevchenko if (!is_slave_direction(direction))
252400ac0341SRabin Vincent return NULL;
252500ac0341SRabin Vincent
2526cade1d30SRabin Vincent return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
25278d318a50SLinus Walleij }
25288d318a50SLinus Walleij
25290c842b55SRabin Vincent static struct dma_async_tx_descriptor *
dma40_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t dma_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)25300c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
25310c842b55SRabin Vincent size_t buf_len, size_t period_len,
253231c1e5a1SLaurent Pinchart enum dma_transfer_direction direction, unsigned long flags)
25330c842b55SRabin Vincent {
25340c842b55SRabin Vincent unsigned int periods = buf_len / period_len;
25350c842b55SRabin Vincent struct dma_async_tx_descriptor *txd;
25360c842b55SRabin Vincent struct scatterlist *sg;
25370c842b55SRabin Vincent int i;
25380c842b55SRabin Vincent
253979ca7ec3SRobert Marklund sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
25402ec7e2e7SSachin Kamat if (!sg)
25412ec7e2e7SSachin Kamat return NULL;
25422ec7e2e7SSachin Kamat
25430c842b55SRabin Vincent for (i = 0; i < periods; i++) {
25440c842b55SRabin Vincent sg_dma_address(&sg[i]) = dma_addr;
25450c842b55SRabin Vincent sg_dma_len(&sg[i]) = period_len;
25460c842b55SRabin Vincent dma_addr += period_len;
25470c842b55SRabin Vincent }
25480c842b55SRabin Vincent
2549838b56adSLogan Gunthorpe sg_chain(sg, periods + 1, sg);
25500c842b55SRabin Vincent
25510c842b55SRabin Vincent txd = d40_prep_sg(chan, sg, sg, periods, direction,
25520c842b55SRabin Vincent DMA_PREP_INTERRUPT);
25530c842b55SRabin Vincent
25540c842b55SRabin Vincent kfree(sg);
25550c842b55SRabin Vincent
25560c842b55SRabin Vincent return txd;
25570c842b55SRabin Vincent }
25580c842b55SRabin Vincent
d40_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)25598d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan,
25608d318a50SLinus Walleij dma_cookie_t cookie,
25618d318a50SLinus Walleij struct dma_tx_state *txstate)
25628d318a50SLinus Walleij {
25638d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
256496a2af41SRussell King - ARM Linux enum dma_status ret;
25658d318a50SLinus Walleij
25660d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) {
25676db5a8baSRabin Vincent chan_err(d40c, "Cannot read status of unallocated channel\n");
25680d0f6b8bSJonas Aaberg return -EINVAL;
25690d0f6b8bSJonas Aaberg }
25700d0f6b8bSJonas Aaberg
257196a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate);
2572a90e56e5SPeter Griffin if (ret != DMA_COMPLETE && txstate)
257396a2af41SRussell King - ARM Linux dma_set_residue(txstate, stedma40_residue(chan));
25748d318a50SLinus Walleij
2575a5ebca47SJonas Aaberg if (d40_is_paused(d40c))
2576a5ebca47SJonas Aaberg ret = DMA_PAUSED;
25778d318a50SLinus Walleij
25788d318a50SLinus Walleij return ret;
25798d318a50SLinus Walleij }
25808d318a50SLinus Walleij
d40_issue_pending(struct dma_chan * chan)25818d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan)
25828d318a50SLinus Walleij {
25838d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
25848d318a50SLinus Walleij unsigned long flags;
25858d318a50SLinus Walleij
25860d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) {
25876db5a8baSRabin Vincent chan_err(d40c, "Channel is not allocated!\n");
25880d0f6b8bSJonas Aaberg return;
25890d0f6b8bSJonas Aaberg }
25900d0f6b8bSJonas Aaberg
25918d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags);
25928d318a50SLinus Walleij
2593a8f3067bSPer Forlin list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
2594a8f3067bSPer Forlin
2595a8f3067bSPer Forlin /* Busy means that queued jobs are already being processed */
25968d318a50SLinus Walleij if (!d40c->busy)
25978d318a50SLinus Walleij (void) d40_queue_start(d40c);
25988d318a50SLinus Walleij
25998d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags);
26008d318a50SLinus Walleij }
26018d318a50SLinus Walleij
d40_terminate_all(struct dma_chan * chan)260235e639d1SVinod Koul static int d40_terminate_all(struct dma_chan *chan)
26031bdae6f4SNarayanan G {
26041bdae6f4SNarayanan G unsigned long flags;
26051bdae6f4SNarayanan G struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26061bdae6f4SNarayanan G int ret;
26071bdae6f4SNarayanan G
26086f5bad03SMaxime Ripard if (d40c->phy_chan == NULL) {
26096f5bad03SMaxime Ripard chan_err(d40c, "Channel is not allocated!\n");
26106f5bad03SMaxime Ripard return -EINVAL;
26116f5bad03SMaxime Ripard }
26126f5bad03SMaxime Ripard
26131bdae6f4SNarayanan G spin_lock_irqsave(&d40c->lock, flags);
26141bdae6f4SNarayanan G
26151bdae6f4SNarayanan G pm_runtime_get_sync(d40c->base->dev);
26161bdae6f4SNarayanan G ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
26171bdae6f4SNarayanan G if (ret)
26181bdae6f4SNarayanan G chan_err(d40c, "Failed to stop channel\n");
26191bdae6f4SNarayanan G
26201bdae6f4SNarayanan G d40_term_all(d40c);
26211bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
26221bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
26231bdae6f4SNarayanan G if (d40c->busy) {
26241bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev);
26251bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev);
26261bdae6f4SNarayanan G }
26271bdae6f4SNarayanan G d40c->busy = false;
26281bdae6f4SNarayanan G
26291bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->lock, flags);
263035e639d1SVinod Koul return 0;
26311bdae6f4SNarayanan G }
26321bdae6f4SNarayanan G
263398ca5289SRabin Vincent static int
dma40_config_to_halfchannel(struct d40_chan * d40c,struct stedma40_half_channel_info * info,u32 maxburst)263498ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c,
263598ca5289SRabin Vincent struct stedma40_half_channel_info *info,
263698ca5289SRabin Vincent u32 maxburst)
263798ca5289SRabin Vincent {
263898ca5289SRabin Vincent int psize;
263998ca5289SRabin Vincent
264098ca5289SRabin Vincent if (chan_is_logical(d40c)) {
264198ca5289SRabin Vincent if (maxburst >= 16)
264298ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_16;
264398ca5289SRabin Vincent else if (maxburst >= 8)
264498ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_8;
264598ca5289SRabin Vincent else if (maxburst >= 4)
264698ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_4;
264798ca5289SRabin Vincent else
264898ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_1;
264998ca5289SRabin Vincent } else {
265098ca5289SRabin Vincent if (maxburst >= 16)
265198ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_16;
265298ca5289SRabin Vincent else if (maxburst >= 8)
265398ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_8;
265498ca5289SRabin Vincent else if (maxburst >= 4)
265598ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_4;
265698ca5289SRabin Vincent else
265798ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_1;
265898ca5289SRabin Vincent }
265998ca5289SRabin Vincent
266098ca5289SRabin Vincent info->psize = psize;
266198ca5289SRabin Vincent info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
266298ca5289SRabin Vincent
266398ca5289SRabin Vincent return 0;
266498ca5289SRabin Vincent }
266598ca5289SRabin Vincent
d40_set_runtime_config(struct dma_chan * chan,struct dma_slave_config * config)266698ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan,
266795e1400fSLinus Walleij struct dma_slave_config *config)
266895e1400fSLinus Walleij {
266995e1400fSLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
26709e314ef3SVinod Koul
26719e314ef3SVinod Koul memcpy(&d40c->slave_config, config, sizeof(*config));
26729e314ef3SVinod Koul
26739e314ef3SVinod Koul return 0;
26749e314ef3SVinod Koul }
26759e314ef3SVinod Koul
26769e314ef3SVinod Koul /* Runtime reconfiguration extension */
d40_set_runtime_config_write(struct dma_chan * chan,struct dma_slave_config * config,enum dma_transfer_direction direction)26779e314ef3SVinod Koul static int d40_set_runtime_config_write(struct dma_chan *chan,
26789e314ef3SVinod Koul struct dma_slave_config *config,
26799e314ef3SVinod Koul enum dma_transfer_direction direction)
26809e314ef3SVinod Koul {
26819e314ef3SVinod Koul struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
268295e1400fSLinus Walleij struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
268398ca5289SRabin Vincent enum dma_slave_buswidth src_addr_width, dst_addr_width;
268495e1400fSLinus Walleij dma_addr_t config_addr;
268598ca5289SRabin Vincent u32 src_maxburst, dst_maxburst;
268698ca5289SRabin Vincent int ret;
268798ca5289SRabin Vincent
26886f5bad03SMaxime Ripard if (d40c->phy_chan == NULL) {
26896f5bad03SMaxime Ripard chan_err(d40c, "Channel is not allocated!\n");
26906f5bad03SMaxime Ripard return -EINVAL;
26916f5bad03SMaxime Ripard }
26926f5bad03SMaxime Ripard
269398ca5289SRabin Vincent src_addr_width = config->src_addr_width;
269498ca5289SRabin Vincent src_maxburst = config->src_maxburst;
269598ca5289SRabin Vincent dst_addr_width = config->dst_addr_width;
269698ca5289SRabin Vincent dst_maxburst = config->dst_maxburst;
269795e1400fSLinus Walleij
26989e314ef3SVinod Koul if (direction == DMA_DEV_TO_MEM) {
269995e1400fSLinus Walleij config_addr = config->src_addr;
2700ef9c89b3SLee Jones
27012c2b62d5SLee Jones if (cfg->dir != DMA_DEV_TO_MEM)
270295e1400fSLinus Walleij dev_dbg(d40c->base->dev,
270395e1400fSLinus Walleij "channel was not configured for peripheral "
270495e1400fSLinus Walleij "to memory transfer (%d) overriding\n",
270595e1400fSLinus Walleij cfg->dir);
27062c2b62d5SLee Jones cfg->dir = DMA_DEV_TO_MEM;
270795e1400fSLinus Walleij
270898ca5289SRabin Vincent /* Configure the memory side */
270998ca5289SRabin Vincent if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
271098ca5289SRabin Vincent dst_addr_width = src_addr_width;
271198ca5289SRabin Vincent if (dst_maxburst == 0)
271298ca5289SRabin Vincent dst_maxburst = src_maxburst;
271395e1400fSLinus Walleij
27149e314ef3SVinod Koul } else if (direction == DMA_MEM_TO_DEV) {
271595e1400fSLinus Walleij config_addr = config->dst_addr;
2716ef9c89b3SLee Jones
27172c2b62d5SLee Jones if (cfg->dir != DMA_MEM_TO_DEV)
271895e1400fSLinus Walleij dev_dbg(d40c->base->dev,
271995e1400fSLinus Walleij "channel was not configured for memory "
272095e1400fSLinus Walleij "to peripheral transfer (%d) overriding\n",
272195e1400fSLinus Walleij cfg->dir);
27222c2b62d5SLee Jones cfg->dir = DMA_MEM_TO_DEV;
272395e1400fSLinus Walleij
272498ca5289SRabin Vincent /* Configure the memory side */
272598ca5289SRabin Vincent if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
272698ca5289SRabin Vincent src_addr_width = dst_addr_width;
272798ca5289SRabin Vincent if (src_maxburst == 0)
272898ca5289SRabin Vincent src_maxburst = dst_maxburst;
272995e1400fSLinus Walleij } else {
273095e1400fSLinus Walleij dev_err(d40c->base->dev,
273195e1400fSLinus Walleij "unrecognized channel direction %d\n",
27329e314ef3SVinod Koul direction);
273398ca5289SRabin Vincent return -EINVAL;
273495e1400fSLinus Walleij }
273595e1400fSLinus Walleij
2736ef9c89b3SLee Jones if (config_addr <= 0) {
2737ef9c89b3SLee Jones dev_err(d40c->base->dev, "no address supplied\n");
2738ef9c89b3SLee Jones return -EINVAL;
2739ef9c89b3SLee Jones }
2740ef9c89b3SLee Jones
274198ca5289SRabin Vincent if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
274295e1400fSLinus Walleij dev_err(d40c->base->dev,
274398ca5289SRabin Vincent "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
274498ca5289SRabin Vincent src_maxburst,
274598ca5289SRabin Vincent src_addr_width,
274698ca5289SRabin Vincent dst_maxburst,
274798ca5289SRabin Vincent dst_addr_width);
274898ca5289SRabin Vincent return -EINVAL;
274995e1400fSLinus Walleij }
275095e1400fSLinus Walleij
275192bb6cdbSPer Forlin if (src_maxburst > 16) {
275292bb6cdbSPer Forlin src_maxburst = 16;
275392bb6cdbSPer Forlin dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
275492bb6cdbSPer Forlin } else if (dst_maxburst > 16) {
275592bb6cdbSPer Forlin dst_maxburst = 16;
275692bb6cdbSPer Forlin src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
275792bb6cdbSPer Forlin }
275892bb6cdbSPer Forlin
275943f2e1a3SLee Jones /* Only valid widths are; 1, 2, 4 and 8. */
276043f2e1a3SLee Jones if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
276143f2e1a3SLee Jones src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
276243f2e1a3SLee Jones dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
276343f2e1a3SLee Jones dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
2764c95905a6SGuennadi Liakhovetski !is_power_of_2(src_addr_width) ||
2765c95905a6SGuennadi Liakhovetski !is_power_of_2(dst_addr_width))
276643f2e1a3SLee Jones return -EINVAL;
276743f2e1a3SLee Jones
276843f2e1a3SLee Jones cfg->src_info.data_width = src_addr_width;
276943f2e1a3SLee Jones cfg->dst_info.data_width = dst_addr_width;
277043f2e1a3SLee Jones
277198ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
277298ca5289SRabin Vincent src_maxburst);
277398ca5289SRabin Vincent if (ret)
277498ca5289SRabin Vincent return ret;
277595e1400fSLinus Walleij
277698ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
277798ca5289SRabin Vincent dst_maxburst);
277898ca5289SRabin Vincent if (ret)
277998ca5289SRabin Vincent return ret;
278095e1400fSLinus Walleij
2781a59670a4SPer Forlin /* Fill in register values */
2782724a8577SRabin Vincent if (chan_is_logical(d40c))
2783a59670a4SPer Forlin d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
2784a59670a4SPer Forlin else
278557e65ad7SLee Jones d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
2786a59670a4SPer Forlin
278795e1400fSLinus Walleij /* These settings will take precedence later */
278895e1400fSLinus Walleij d40c->runtime_addr = config_addr;
27899e314ef3SVinod Koul d40c->runtime_direction = direction;
279095e1400fSLinus Walleij dev_dbg(d40c->base->dev,
279198ca5289SRabin Vincent "configured channel %s for %s, data width %d/%d, "
279298ca5289SRabin Vincent "maxburst %d/%d elements, LE, no flow control\n",
279395e1400fSLinus Walleij dma_chan_name(chan),
27949e314ef3SVinod Koul (direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
279598ca5289SRabin Vincent src_addr_width, dst_addr_width,
279698ca5289SRabin Vincent src_maxburst, dst_maxburst);
279798ca5289SRabin Vincent
279898ca5289SRabin Vincent return 0;
279995e1400fSLinus Walleij }
280095e1400fSLinus Walleij
28018d318a50SLinus Walleij /* Initialization functions */
28028d318a50SLinus Walleij
d40_chan_init(struct d40_base * base,struct dma_device * dma,struct d40_chan * chans,int offset,int num_chans)28038d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
28048d318a50SLinus Walleij struct d40_chan *chans, int offset,
28058d318a50SLinus Walleij int num_chans)
28068d318a50SLinus Walleij {
28078d318a50SLinus Walleij int i = 0;
28088d318a50SLinus Walleij struct d40_chan *d40c;
28098d318a50SLinus Walleij
28108d318a50SLinus Walleij INIT_LIST_HEAD(&dma->channels);
28118d318a50SLinus Walleij
28128d318a50SLinus Walleij for (i = offset; i < offset + num_chans; i++) {
28138d318a50SLinus Walleij d40c = &chans[i];
28148d318a50SLinus Walleij d40c->base = base;
28158d318a50SLinus Walleij d40c->chan.device = dma;
28168d318a50SLinus Walleij
28178d318a50SLinus Walleij spin_lock_init(&d40c->lock);
28188d318a50SLinus Walleij
28198d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN;
28208d318a50SLinus Walleij
28214226dd86SFabio Baltieri INIT_LIST_HEAD(&d40c->done);
28228d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->active);
28238d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->queue);
2824a8f3067bSPer Forlin INIT_LIST_HEAD(&d40c->pending_queue);
28258d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->client);
282682babbb3SPer Forlin INIT_LIST_HEAD(&d40c->prepare_queue);
28278d318a50SLinus Walleij
2828b1880c90SAllen Pais tasklet_setup(&d40c->tasklet, dma_tasklet);
28298d318a50SLinus Walleij
28308d318a50SLinus Walleij list_add_tail(&d40c->chan.device_node,
28318d318a50SLinus Walleij &dma->channels);
28328d318a50SLinus Walleij }
28338d318a50SLinus Walleij }
28348d318a50SLinus Walleij
d40_ops_init(struct d40_base * base,struct dma_device * dev)28357ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
28367ad74a7cSRabin Vincent {
283749873e99SLinus Walleij if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) {
28387ad74a7cSRabin Vincent dev->device_prep_slave_sg = d40_prep_slave_sg;
283949873e99SLinus Walleij dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
284049873e99SLinus Walleij }
28417ad74a7cSRabin Vincent
28427ad74a7cSRabin Vincent if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
28437ad74a7cSRabin Vincent dev->device_prep_dma_memcpy = d40_prep_memcpy;
284449873e99SLinus Walleij dev->directions = BIT(DMA_MEM_TO_MEM);
28457ad74a7cSRabin Vincent /*
28467ad74a7cSRabin Vincent * This controller can only access address at even
28477ad74a7cSRabin Vincent * 32bit boundaries, i.e. 2^2
28487ad74a7cSRabin Vincent */
284977a68e56SMaxime Ripard dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
28507ad74a7cSRabin Vincent }
28517ad74a7cSRabin Vincent
28520c842b55SRabin Vincent if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
28530c842b55SRabin Vincent dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
28540c842b55SRabin Vincent
28557ad74a7cSRabin Vincent dev->device_alloc_chan_resources = d40_alloc_chan_resources;
28567ad74a7cSRabin Vincent dev->device_free_chan_resources = d40_free_chan_resources;
28577ad74a7cSRabin Vincent dev->device_issue_pending = d40_issue_pending;
28587ad74a7cSRabin Vincent dev->device_tx_status = d40_tx_status;
28596f5bad03SMaxime Ripard dev->device_config = d40_set_runtime_config;
28606f5bad03SMaxime Ripard dev->device_pause = d40_pause;
28616f5bad03SMaxime Ripard dev->device_resume = d40_resume;
28626f5bad03SMaxime Ripard dev->device_terminate_all = d40_terminate_all;
286315c60668SLinus Walleij dev->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
28647ad74a7cSRabin Vincent dev->dev = base->dev;
28657ad74a7cSRabin Vincent }
28667ad74a7cSRabin Vincent
d40_dmaengine_init(struct d40_base * base,int num_reserved_chans)28678d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base,
28688d318a50SLinus Walleij int num_reserved_chans)
28698d318a50SLinus Walleij {
28708d318a50SLinus Walleij int err ;
28718d318a50SLinus Walleij
28728d318a50SLinus Walleij d40_chan_init(base, &base->dma_slave, base->log_chans,
28738d318a50SLinus Walleij 0, base->num_log_chans);
28748d318a50SLinus Walleij
28758d318a50SLinus Walleij dma_cap_zero(base->dma_slave.cap_mask);
28768d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
28770c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
28788d318a50SLinus Walleij
28797ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_slave);
28808d318a50SLinus Walleij
2881fc9826deSHuang Shijie err = dmaenginem_async_device_register(&base->dma_slave);
28828d318a50SLinus Walleij
28838d318a50SLinus Walleij if (err) {
28846db5a8baSRabin Vincent d40_err(base->dev, "Failed to register slave channels\n");
2885c9909935SMarkus Elfring goto exit;
28868d318a50SLinus Walleij }
28878d318a50SLinus Walleij
28888d318a50SLinus Walleij d40_chan_init(base, &base->dma_memcpy, base->log_chans,
2889a7dacb68SLee Jones base->num_log_chans, base->num_memcpy_chans);
28908d318a50SLinus Walleij
28918d318a50SLinus Walleij dma_cap_zero(base->dma_memcpy.cap_mask);
28928d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
28938d318a50SLinus Walleij
28947ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_memcpy);
28958d318a50SLinus Walleij
2896fc9826deSHuang Shijie err = dmaenginem_async_device_register(&base->dma_memcpy);
28978d318a50SLinus Walleij
28988d318a50SLinus Walleij if (err) {
28996db5a8baSRabin Vincent d40_err(base->dev,
290052984aabSGeliang Tang "Failed to register memcpy only channels\n");
2901fc9826deSHuang Shijie goto exit;
29028d318a50SLinus Walleij }
29038d318a50SLinus Walleij
29048d318a50SLinus Walleij d40_chan_init(base, &base->dma_both, base->phy_chans,
29058d318a50SLinus Walleij 0, num_reserved_chans);
29068d318a50SLinus Walleij
29078d318a50SLinus Walleij dma_cap_zero(base->dma_both.cap_mask);
29088d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
29098d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
29100c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
29118d318a50SLinus Walleij
29127ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_both);
2913fc9826deSHuang Shijie err = dmaenginem_async_device_register(&base->dma_both);
29148d318a50SLinus Walleij
29158d318a50SLinus Walleij if (err) {
29166db5a8baSRabin Vincent d40_err(base->dev,
29176db5a8baSRabin Vincent "Failed to register logical and physical capable channels\n");
2918fc9826deSHuang Shijie goto exit;
29198d318a50SLinus Walleij }
29208d318a50SLinus Walleij return 0;
2921c9909935SMarkus Elfring exit:
29228d318a50SLinus Walleij return err;
29238d318a50SLinus Walleij }
29248d318a50SLinus Walleij
29257fb3e75eSNarayanan G /* Suspend resume functionality */
2926123e4ca1SUlf Hansson #ifdef CONFIG_PM_SLEEP
dma40_suspend(struct device * dev)2927123e4ca1SUlf Hansson static int dma40_suspend(struct device *dev)
29287fb3e75eSNarayanan G {
2929be34c218SWolfram Sang struct d40_base *base = dev_get_drvdata(dev);
2930c906a3ecSUlf Hansson int ret;
2931c906a3ecSUlf Hansson
2932c906a3ecSUlf Hansson ret = pm_runtime_force_suspend(dev);
2933c906a3ecSUlf Hansson if (ret)
2934c906a3ecSUlf Hansson return ret;
29357fb3e75eSNarayanan G
293628c7a19dSNarayanan G if (base->lcpa_regulator)
293728c7a19dSNarayanan G ret = regulator_disable(base->lcpa_regulator);
293828c7a19dSNarayanan G return ret;
29397fb3e75eSNarayanan G }
29407fb3e75eSNarayanan G
dma40_resume(struct device * dev)2941123e4ca1SUlf Hansson static int dma40_resume(struct device *dev)
2942123e4ca1SUlf Hansson {
2943be34c218SWolfram Sang struct d40_base *base = dev_get_drvdata(dev);
2944123e4ca1SUlf Hansson int ret = 0;
2945123e4ca1SUlf Hansson
2946c906a3ecSUlf Hansson if (base->lcpa_regulator) {
2947123e4ca1SUlf Hansson ret = regulator_enable(base->lcpa_regulator);
2948c906a3ecSUlf Hansson if (ret)
2949123e4ca1SUlf Hansson return ret;
2950123e4ca1SUlf Hansson }
2951c906a3ecSUlf Hansson
2952c906a3ecSUlf Hansson return pm_runtime_force_resume(dev);
2953c906a3ecSUlf Hansson }
2954123e4ca1SUlf Hansson #endif
2955123e4ca1SUlf Hansson
2956123e4ca1SUlf Hansson #ifdef CONFIG_PM
dma40_backup(void __iomem * baseaddr,u32 * backup,u32 * regaddr,int num,bool save)2957123e4ca1SUlf Hansson static void dma40_backup(void __iomem *baseaddr, u32 *backup,
2958123e4ca1SUlf Hansson u32 *regaddr, int num, bool save)
2959123e4ca1SUlf Hansson {
2960123e4ca1SUlf Hansson int i;
2961123e4ca1SUlf Hansson
2962123e4ca1SUlf Hansson for (i = 0; i < num; i++) {
2963123e4ca1SUlf Hansson void __iomem *addr = baseaddr + regaddr[i];
2964123e4ca1SUlf Hansson
2965123e4ca1SUlf Hansson if (save)
2966123e4ca1SUlf Hansson backup[i] = readl_relaxed(addr);
2967123e4ca1SUlf Hansson else
2968123e4ca1SUlf Hansson writel_relaxed(backup[i], addr);
2969123e4ca1SUlf Hansson }
2970123e4ca1SUlf Hansson }
2971123e4ca1SUlf Hansson
d40_save_restore_registers(struct d40_base * base,bool save)2972123e4ca1SUlf Hansson static void d40_save_restore_registers(struct d40_base *base, bool save)
2973123e4ca1SUlf Hansson {
2974123e4ca1SUlf Hansson int i;
2975123e4ca1SUlf Hansson
2976123e4ca1SUlf Hansson /* Save/Restore channel specific registers */
2977123e4ca1SUlf Hansson for (i = 0; i < base->num_phy_chans; i++) {
2978123e4ca1SUlf Hansson void __iomem *addr;
2979123e4ca1SUlf Hansson int idx;
2980123e4ca1SUlf Hansson
2981123e4ca1SUlf Hansson if (base->phy_res[i].reserved)
2982123e4ca1SUlf Hansson continue;
2983123e4ca1SUlf Hansson
2984123e4ca1SUlf Hansson addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
2985123e4ca1SUlf Hansson idx = i * ARRAY_SIZE(d40_backup_regs_chan);
2986123e4ca1SUlf Hansson
2987123e4ca1SUlf Hansson dma40_backup(addr, &base->reg_val_backup_chan[idx],
2988123e4ca1SUlf Hansson d40_backup_regs_chan,
2989123e4ca1SUlf Hansson ARRAY_SIZE(d40_backup_regs_chan),
2990123e4ca1SUlf Hansson save);
2991123e4ca1SUlf Hansson }
2992123e4ca1SUlf Hansson
2993123e4ca1SUlf Hansson /* Save/Restore global registers */
2994123e4ca1SUlf Hansson dma40_backup(base->virtbase, base->reg_val_backup,
2995123e4ca1SUlf Hansson d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
2996123e4ca1SUlf Hansson save);
2997123e4ca1SUlf Hansson
2998123e4ca1SUlf Hansson /* Save/Restore registers only existing on dma40 v3 and later */
2999123e4ca1SUlf Hansson if (base->gen_dmac.backup)
3000123e4ca1SUlf Hansson dma40_backup(base->virtbase, base->reg_val_backup_v4,
3001123e4ca1SUlf Hansson base->gen_dmac.backup,
3002123e4ca1SUlf Hansson base->gen_dmac.backup_size,
3003123e4ca1SUlf Hansson save);
3004123e4ca1SUlf Hansson }
3005123e4ca1SUlf Hansson
dma40_runtime_suspend(struct device * dev)30067fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev)
30077fb3e75eSNarayanan G {
3008be34c218SWolfram Sang struct d40_base *base = dev_get_drvdata(dev);
30097fb3e75eSNarayanan G
30107fb3e75eSNarayanan G d40_save_restore_registers(base, true);
30117fb3e75eSNarayanan G
30127fb3e75eSNarayanan G /* Don't disable/enable clocks for v1 due to HW bugs */
30137fb3e75eSNarayanan G if (base->rev != 1)
30147fb3e75eSNarayanan G writel_relaxed(base->gcc_pwr_off_mask,
30157fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC);
30167fb3e75eSNarayanan G
30177fb3e75eSNarayanan G return 0;
30187fb3e75eSNarayanan G }
30197fb3e75eSNarayanan G
dma40_runtime_resume(struct device * dev)30207fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev)
30217fb3e75eSNarayanan G {
3022be34c218SWolfram Sang struct d40_base *base = dev_get_drvdata(dev);
30237fb3e75eSNarayanan G
30247fb3e75eSNarayanan G d40_save_restore_registers(base, false);
30257fb3e75eSNarayanan G
30267fb3e75eSNarayanan G writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
30277fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC);
30287fb3e75eSNarayanan G return 0;
30297fb3e75eSNarayanan G }
3030123e4ca1SUlf Hansson #endif
30317fb3e75eSNarayanan G
30327fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = {
3033673d3773SUlf Hansson SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
30346ed23b80SRafael J. Wysocki SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
3035123e4ca1SUlf Hansson dma40_runtime_resume,
3036123e4ca1SUlf Hansson NULL)
30377fb3e75eSNarayanan G };
30387fb3e75eSNarayanan G
30398d318a50SLinus Walleij /* Initialization functions. */
30408d318a50SLinus Walleij
d40_phy_res_init(struct d40_base * base)30418d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base)
30428d318a50SLinus Walleij {
30438d318a50SLinus Walleij int i;
30448d318a50SLinus Walleij int num_phy_chans_avail = 0;
30458d318a50SLinus Walleij u32 val[2];
30468d318a50SLinus Walleij int odd_even_bit = -2;
30477fb3e75eSNarayanan G int gcc = D40_DREG_GCC_ENA;
30488d318a50SLinus Walleij
30498d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRSME);
30508d318a50SLinus Walleij val[1] = readl(base->virtbase + D40_DREG_PRSMO);
30518d318a50SLinus Walleij
30528d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) {
30538d318a50SLinus Walleij base->phy_res[i].num = i;
30548d318a50SLinus Walleij odd_even_bit += 2 * ((i % 2) == 0);
30558d318a50SLinus Walleij if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
30568d318a50SLinus Walleij /* Mark security only channels as occupied */
30578d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_PHY;
30588d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
30597fb3e75eSNarayanan G base->phy_res[i].reserved = true;
30607fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30617fb3e75eSNarayanan G D40_DREG_GCC_SRC);
30627fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
30637fb3e75eSNarayanan G D40_DREG_GCC_DST);
30647fb3e75eSNarayanan G
30657fb3e75eSNarayanan G
30668d318a50SLinus Walleij } else {
30678d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_FREE;
30688d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
30697fb3e75eSNarayanan G base->phy_res[i].reserved = false;
30708d318a50SLinus Walleij num_phy_chans_avail++;
30718d318a50SLinus Walleij }
30728d318a50SLinus Walleij spin_lock_init(&base->phy_res[i].lock);
30738d318a50SLinus Walleij }
30746b7acd84SJonas Aaberg
30756b7acd84SJonas Aaberg /* Mark disabled channels as occupied */
30766b7acd84SJonas Aaberg for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
3077f57b407cSRabin Vincent int chan = base->plat_data->disabled_channels[i];
3078f57b407cSRabin Vincent
3079f57b407cSRabin Vincent base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
3080f57b407cSRabin Vincent base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
30817fb3e75eSNarayanan G base->phy_res[chan].reserved = true;
30827fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
30837fb3e75eSNarayanan G D40_DREG_GCC_SRC);
30847fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
30857fb3e75eSNarayanan G D40_DREG_GCC_DST);
30866b7acd84SJonas Aaberg num_phy_chans_avail--;
30876b7acd84SJonas Aaberg }
30886b7acd84SJonas Aaberg
30897407048bSFabio Baltieri /* Mark soft_lli channels */
30907407048bSFabio Baltieri for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
30917407048bSFabio Baltieri int chan = base->plat_data->soft_lli_chans[i];
30927407048bSFabio Baltieri
30937407048bSFabio Baltieri base->phy_res[chan].use_soft_lli = true;
30947407048bSFabio Baltieri }
30957407048bSFabio Baltieri
30968d318a50SLinus Walleij dev_info(base->dev, "%d of %d physical DMA channels available\n",
30978d318a50SLinus Walleij num_phy_chans_avail, base->num_phy_chans);
30988d318a50SLinus Walleij
30998d318a50SLinus Walleij /* Verify settings extended vs standard */
31008d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRTYP);
31018d318a50SLinus Walleij
31028d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) {
31038d318a50SLinus Walleij
31048d318a50SLinus Walleij if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
31058d318a50SLinus Walleij (val[0] & 0x3) != 1)
31068d318a50SLinus Walleij dev_info(base->dev,
31078d318a50SLinus Walleij "[%s] INFO: channel %d is misconfigured (%d)\n",
31088d318a50SLinus Walleij __func__, i, val[0] & 0x3);
31098d318a50SLinus Walleij
31108d318a50SLinus Walleij val[0] = val[0] >> 2;
31118d318a50SLinus Walleij }
31128d318a50SLinus Walleij
31137fb3e75eSNarayanan G /*
31147fb3e75eSNarayanan G * To keep things simple, Enable all clocks initially.
31157fb3e75eSNarayanan G * The clocks will get managed later post channel allocation.
31167fb3e75eSNarayanan G * The clocks for the event lines on which reserved channels exists
31177fb3e75eSNarayanan G * are not managed here.
31187fb3e75eSNarayanan G */
31197fb3e75eSNarayanan G writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
31207fb3e75eSNarayanan G base->gcc_pwr_off_mask = gcc;
31217fb3e75eSNarayanan G
31228d318a50SLinus Walleij return num_phy_chans_avail;
31238d318a50SLinus Walleij }
31248d318a50SLinus Walleij
3125339f5041SLinus Walleij /* Called from the registered devm action */
d40_drop_kmem_cache_action(void * d)3126339f5041SLinus Walleij static void d40_drop_kmem_cache_action(void *d)
3127339f5041SLinus Walleij {
3128339f5041SLinus Walleij struct kmem_cache *desc_slab = d;
3129339f5041SLinus Walleij
3130339f5041SLinus Walleij kmem_cache_destroy(desc_slab);
3131339f5041SLinus Walleij }
3132339f5041SLinus Walleij
d40_hw_detect_init(struct platform_device * pdev,struct d40_base ** retbase)31332893f6bcSLinus Walleij static int __init d40_hw_detect_init(struct platform_device *pdev,
31342893f6bcSLinus Walleij struct d40_base **retbase)
31358d318a50SLinus Walleij {
3136d4adcc01SJingoo Han struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
3137fb85a8c5SLinus Walleij struct device *dev = &pdev->dev;
313811f7a8d1SMarkus Elfring struct clk *clk;
313911f7a8d1SMarkus Elfring void __iomem *virtbase;
314011f7a8d1SMarkus Elfring struct d40_base *base;
314111f7a8d1SMarkus Elfring int num_log_chans;
31428d318a50SLinus Walleij int num_phy_chans;
3143a7dacb68SLee Jones int num_memcpy_chans;
31448d318a50SLinus Walleij int i;
3145f4b89764SLinus Walleij u32 pid;
3146f4b89764SLinus Walleij u32 cid;
3147f4b89764SLinus Walleij u8 rev;
3148339f5041SLinus Walleij int ret;
31498d318a50SLinus Walleij
3150339f5041SLinus Walleij clk = devm_clk_get_enabled(dev, NULL);
3151339f5041SLinus Walleij if (IS_ERR(clk))
31522893f6bcSLinus Walleij return PTR_ERR(clk);
31538d318a50SLinus Walleij
31548d318a50SLinus Walleij /* Get IO for DMAC base address */
3155339f5041SLinus Walleij virtbase = devm_platform_ioremap_resource_byname(pdev, "base");
31562893f6bcSLinus Walleij if (IS_ERR(virtbase))
31572893f6bcSLinus Walleij return PTR_ERR(virtbase);
31588d318a50SLinus Walleij
3159f4b89764SLinus Walleij /* This is just a regular AMBA PrimeCell ID actually */
3160f4b89764SLinus Walleij for (pid = 0, i = 0; i < 4; i++)
3161339f5041SLinus Walleij pid |= (readl(virtbase + SZ_4K - 0x20 + 4 * i)
3162f4b89764SLinus Walleij & 255) << (i * 8);
3163f4b89764SLinus Walleij for (cid = 0, i = 0; i < 4; i++)
3164339f5041SLinus Walleij cid |= (readl(virtbase + SZ_4K - 0x10 + 4 * i)
3165f4b89764SLinus Walleij & 255) << (i * 8);
3166f4b89764SLinus Walleij
3167f4b89764SLinus Walleij if (cid != AMBA_CID) {
3168fb85a8c5SLinus Walleij d40_err(dev, "Unknown hardware! No PrimeCell ID\n");
31692893f6bcSLinus Walleij return -EINVAL;
31708d318a50SLinus Walleij }
3171f4b89764SLinus Walleij if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
3172fb85a8c5SLinus Walleij d40_err(dev, "Unknown designer! Got %x wanted %x\n",
3173f4b89764SLinus Walleij AMBA_MANF_BITS(pid),
3174f4b89764SLinus Walleij AMBA_VENDOR_ST);
31752893f6bcSLinus Walleij return -EINVAL;
31768d318a50SLinus Walleij }
3177f4b89764SLinus Walleij /*
3178f4b89764SLinus Walleij * HW revision:
3179f4b89764SLinus Walleij * DB8500ed has revision 0
3180f4b89764SLinus Walleij * ? has revision 1
3181f4b89764SLinus Walleij * DB8500v1 has revision 2
3182f4b89764SLinus Walleij * DB8500v2 has revision 3
318347db92f4SGerald Baeza * AP9540v1 has revision 4
318447db92f4SGerald Baeza * DB8540v1 has revision 4
3185f4b89764SLinus Walleij */
3186f4b89764SLinus Walleij rev = AMBA_REV_BITS(pid);
31878b2fe9b6SLee Jones if (rev < 2) {
3188fb85a8c5SLinus Walleij d40_err(dev, "hardware revision: %d is not supported", rev);
31892893f6bcSLinus Walleij return -EINVAL;
31908b2fe9b6SLee Jones }
31913ae0267fSJonas Aaberg
31928d318a50SLinus Walleij /* The number of physical channels on this HW */
319347db92f4SGerald Baeza if (plat_data->num_of_phy_chans)
319447db92f4SGerald Baeza num_phy_chans = plat_data->num_of_phy_chans;
319547db92f4SGerald Baeza else
31968d318a50SLinus Walleij num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
31978d318a50SLinus Walleij
3198a7dacb68SLee Jones /* The number of channels used for memcpy */
3199a7dacb68SLee Jones if (plat_data->num_of_memcpy_chans)
3200a7dacb68SLee Jones num_memcpy_chans = plat_data->num_of_memcpy_chans;
3201a7dacb68SLee Jones else
3202a7dacb68SLee Jones num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
3203a7dacb68SLee Jones
3204db72da92SLee Jones num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
3205db72da92SLee Jones
3206fb85a8c5SLinus Walleij dev_info(dev,
3207339f5041SLinus Walleij "hardware rev: %d with %d physical and %d logical channels\n",
3208339f5041SLinus Walleij rev, num_phy_chans, num_log_chans);
32098d318a50SLinus Walleij
3210339f5041SLinus Walleij base = devm_kzalloc(dev,
3211339f5041SLinus Walleij ALIGN(sizeof(struct d40_base), 4) +
3212a7dacb68SLee Jones (num_phy_chans + num_log_chans + num_memcpy_chans) *
32138d318a50SLinus Walleij sizeof(struct d40_chan), GFP_KERNEL);
32148d318a50SLinus Walleij
3215339f5041SLinus Walleij if (!base)
32162893f6bcSLinus Walleij return -ENOMEM;
32178d318a50SLinus Walleij
32183ae0267fSJonas Aaberg base->rev = rev;
32198d318a50SLinus Walleij base->clk = clk;
3220a7dacb68SLee Jones base->num_memcpy_chans = num_memcpy_chans;
32218d318a50SLinus Walleij base->num_phy_chans = num_phy_chans;
32228d318a50SLinus Walleij base->num_log_chans = num_log_chans;
32238d318a50SLinus Walleij base->virtbase = virtbase;
32248d318a50SLinus Walleij base->plat_data = plat_data;
3225fb85a8c5SLinus Walleij base->dev = dev;
32268d318a50SLinus Walleij base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
32278d318a50SLinus Walleij base->log_chans = &base->phy_chans[num_phy_chans];
32288d318a50SLinus Walleij
32293cb645dcSTong Liu if (base->plat_data->num_of_phy_chans == 14) {
32303cb645dcSTong Liu base->gen_dmac.backup = d40_backup_regs_v4b;
32313cb645dcSTong Liu base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
32323cb645dcSTong Liu base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
32333cb645dcSTong Liu base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
32343cb645dcSTong Liu base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
32353cb645dcSTong Liu base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
32363cb645dcSTong Liu base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
32373cb645dcSTong Liu base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
32383cb645dcSTong Liu base->gen_dmac.il = il_v4b;
32393cb645dcSTong Liu base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
32403cb645dcSTong Liu base->gen_dmac.init_reg = dma_init_reg_v4b;
32413cb645dcSTong Liu base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
32423cb645dcSTong Liu } else {
32433cb645dcSTong Liu if (base->rev >= 3) {
32443cb645dcSTong Liu base->gen_dmac.backup = d40_backup_regs_v4a;
32453cb645dcSTong Liu base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
32463cb645dcSTong Liu }
32473cb645dcSTong Liu base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
32483cb645dcSTong Liu base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
32493cb645dcSTong Liu base->gen_dmac.realtime_en = D40_DREG_RSEG1;
32503cb645dcSTong Liu base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
32513cb645dcSTong Liu base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
32523cb645dcSTong Liu base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
32533cb645dcSTong Liu base->gen_dmac.il = il_v4a;
32543cb645dcSTong Liu base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
32553cb645dcSTong Liu base->gen_dmac.init_reg = dma_init_reg_v4a;
32563cb645dcSTong Liu base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
32573cb645dcSTong Liu }
32583cb645dcSTong Liu
3259339f5041SLinus Walleij base->phy_res = devm_kcalloc(dev, num_phy_chans,
3260e349d4b7SMarkus Elfring sizeof(*base->phy_res),
32618d318a50SLinus Walleij GFP_KERNEL);
32628d318a50SLinus Walleij if (!base->phy_res)
32632893f6bcSLinus Walleij return -ENOMEM;
32648d318a50SLinus Walleij
3265339f5041SLinus Walleij base->lookup_phy_chans = devm_kcalloc(dev, num_phy_chans,
3266e349d4b7SMarkus Elfring sizeof(*base->lookup_phy_chans),
32678d318a50SLinus Walleij GFP_KERNEL);
32688d318a50SLinus Walleij if (!base->lookup_phy_chans)
32692893f6bcSLinus Walleij return -ENOMEM;
32708d318a50SLinus Walleij
3271339f5041SLinus Walleij base->lookup_log_chans = devm_kcalloc(dev, num_log_chans,
3272e349d4b7SMarkus Elfring sizeof(*base->lookup_log_chans),
32738d318a50SLinus Walleij GFP_KERNEL);
32748d318a50SLinus Walleij if (!base->lookup_log_chans)
32752893f6bcSLinus Walleij return -ENOMEM;
3276698e4732SJonas Aaberg
3277339f5041SLinus Walleij base->reg_val_backup_chan = devm_kmalloc_array(dev, base->num_phy_chans,
32787fb3e75eSNarayanan G sizeof(d40_backup_regs_chan),
32798d318a50SLinus Walleij GFP_KERNEL);
32807fb3e75eSNarayanan G if (!base->reg_val_backup_chan)
32812893f6bcSLinus Walleij return -ENOMEM;
32827fb3e75eSNarayanan G
3283339f5041SLinus Walleij base->lcla_pool.alloc_map = devm_kcalloc(dev, num_phy_chans
3284e349d4b7SMarkus Elfring * D40_LCLA_LINK_PER_EVENT_GRP,
3285e349d4b7SMarkus Elfring sizeof(*base->lcla_pool.alloc_map),
3286e349d4b7SMarkus Elfring GFP_KERNEL);
32878d318a50SLinus Walleij if (!base->lcla_pool.alloc_map)
32882893f6bcSLinus Walleij return -ENOMEM;
32898d318a50SLinus Walleij
3290339f5041SLinus Walleij base->regs_interrupt = devm_kmalloc_array(dev, base->gen_dmac.il_size,
3291e6a78511SKees Cook sizeof(*base->regs_interrupt),
3292e6a78511SKees Cook GFP_KERNEL);
3293e6a78511SKees Cook if (!base->regs_interrupt)
32942893f6bcSLinus Walleij return -ENOMEM;
3295e6a78511SKees Cook
3296c675b1b4SJonas Aaberg base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
3297c675b1b4SJonas Aaberg 0, SLAB_HWCACHE_ALIGN,
3298c675b1b4SJonas Aaberg NULL);
3299339f5041SLinus Walleij if (!base->desc_slab)
33002893f6bcSLinus Walleij return -ENOMEM;
3301e6a78511SKees Cook
3302339f5041SLinus Walleij ret = devm_add_action_or_reset(dev, d40_drop_kmem_cache_action,
3303339f5041SLinus Walleij base->desc_slab);
3304339f5041SLinus Walleij if (ret)
33052893f6bcSLinus Walleij return ret;
3306c675b1b4SJonas Aaberg
33072893f6bcSLinus Walleij *retbase = base;
33082893f6bcSLinus Walleij
33092893f6bcSLinus Walleij return 0;
33108d318a50SLinus Walleij }
33118d318a50SLinus Walleij
d40_hw_init(struct d40_base * base)33128d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base)
33138d318a50SLinus Walleij {
33148d318a50SLinus Walleij
33158d318a50SLinus Walleij int i;
33168d318a50SLinus Walleij u32 prmseo[2] = {0, 0};
33178d318a50SLinus Walleij u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
33188d318a50SLinus Walleij u32 pcmis = 0;
33198d318a50SLinus Walleij u32 pcicr = 0;
33203cb645dcSTong Liu struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
33213cb645dcSTong Liu u32 reg_size = base->gen_dmac.init_reg_size;
33228d318a50SLinus Walleij
33233cb645dcSTong Liu for (i = 0; i < reg_size; i++)
33248d318a50SLinus Walleij writel(dma_init_reg[i].val,
33258d318a50SLinus Walleij base->virtbase + dma_init_reg[i].reg);
33268d318a50SLinus Walleij
33278d318a50SLinus Walleij /* Configure all our dma channels to default settings */
33288d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) {
33298d318a50SLinus Walleij
33308d318a50SLinus Walleij activeo[i % 2] = activeo[i % 2] << 2;
33318d318a50SLinus Walleij
33328d318a50SLinus Walleij if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
33338d318a50SLinus Walleij == D40_ALLOC_PHY) {
33348d318a50SLinus Walleij activeo[i % 2] |= 3;
33358d318a50SLinus Walleij continue;
33368d318a50SLinus Walleij }
33378d318a50SLinus Walleij
33388d318a50SLinus Walleij /* Enable interrupt # */
33398d318a50SLinus Walleij pcmis = (pcmis << 1) | 1;
33408d318a50SLinus Walleij
33418d318a50SLinus Walleij /* Clear interrupt # */
33428d318a50SLinus Walleij pcicr = (pcicr << 1) | 1;
33438d318a50SLinus Walleij
33448d318a50SLinus Walleij /* Set channel to physical mode */
33458d318a50SLinus Walleij prmseo[i % 2] = prmseo[i % 2] << 2;
33468d318a50SLinus Walleij prmseo[i % 2] |= 1;
33478d318a50SLinus Walleij
33488d318a50SLinus Walleij }
33498d318a50SLinus Walleij
33508d318a50SLinus Walleij writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
33518d318a50SLinus Walleij writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
33528d318a50SLinus Walleij writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
33538d318a50SLinus Walleij writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
33548d318a50SLinus Walleij
33558d318a50SLinus Walleij /* Write which interrupt to enable */
33563cb645dcSTong Liu writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
33578d318a50SLinus Walleij
33588d318a50SLinus Walleij /* Write which interrupt to clear */
33593cb645dcSTong Liu writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
33608d318a50SLinus Walleij
33613cb645dcSTong Liu /* These are __initdata and cannot be accessed after init */
33623cb645dcSTong Liu base->gen_dmac.init_reg = NULL;
33633cb645dcSTong Liu base->gen_dmac.init_reg_size = 0;
33648d318a50SLinus Walleij }
33658d318a50SLinus Walleij
d40_lcla_allocate(struct d40_base * base)3366508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base)
3367508849adSLinus Walleij {
3368026cbc42SRabin Vincent struct d40_lcla_pool *pool = &base->lcla_pool;
3369508849adSLinus Walleij unsigned long *page_list;
3370508849adSLinus Walleij int i, j;
3371abac5bacSMarkus Elfring int ret;
3372508849adSLinus Walleij
3373508849adSLinus Walleij /*
3374508849adSLinus Walleij * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
3375508849adSLinus Walleij * To full fill this hardware requirement without wasting 256 kb
3376508849adSLinus Walleij * we allocate pages until we get an aligned one.
3377508849adSLinus Walleij */
3378cf80ecf7SMarkus Elfring page_list = kmalloc_array(MAX_LCLA_ALLOC_ATTEMPTS,
3379cf80ecf7SMarkus Elfring sizeof(*page_list),
3380508849adSLinus Walleij GFP_KERNEL);
33812c7f2f20SMarkus Elfring if (!page_list)
33822c7f2f20SMarkus Elfring return -ENOMEM;
3383508849adSLinus Walleij
3384508849adSLinus Walleij /* Calculating how many pages that are required */
3385508849adSLinus Walleij base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
3386508849adSLinus Walleij
3387508849adSLinus Walleij for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
3388508849adSLinus Walleij page_list[i] = __get_free_pages(GFP_KERNEL,
3389508849adSLinus Walleij base->lcla_pool.pages);
3390508849adSLinus Walleij if (!page_list[i]) {
3391508849adSLinus Walleij
33926db5a8baSRabin Vincent d40_err(base->dev, "Failed to allocate %d pages.\n",
33936db5a8baSRabin Vincent base->lcla_pool.pages);
339439375334SJulia Lawall ret = -ENOMEM;
3395508849adSLinus Walleij
3396508849adSLinus Walleij for (j = 0; j < i; j++)
3397508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages);
3398aae32ec6SMarkus Elfring goto free_page_list;
3399508849adSLinus Walleij }
3400508849adSLinus Walleij
3401508849adSLinus Walleij if ((virt_to_phys((void *)page_list[i]) &
3402508849adSLinus Walleij (LCLA_ALIGNMENT - 1)) == 0)
3403508849adSLinus Walleij break;
3404508849adSLinus Walleij }
3405508849adSLinus Walleij
3406508849adSLinus Walleij for (j = 0; j < i; j++)
3407508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages);
3408508849adSLinus Walleij
3409508849adSLinus Walleij if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
3410508849adSLinus Walleij base->lcla_pool.base = (void *)page_list[i];
3411508849adSLinus Walleij } else {
3412767a9675SJonas Aaberg /*
341371a5197eSRandy Dunlap * After many attempts and no success with finding the correct
3414767a9675SJonas Aaberg * alignment, try with allocating a big buffer.
3415767a9675SJonas Aaberg */
3416508849adSLinus Walleij dev_warn(base->dev,
3417508849adSLinus Walleij "[%s] Failed to get %d pages @ 18 bit align.\n",
3418508849adSLinus Walleij __func__, base->lcla_pool.pages);
3419508849adSLinus Walleij base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
3420508849adSLinus Walleij base->num_phy_chans +
3421508849adSLinus Walleij LCLA_ALIGNMENT,
3422508849adSLinus Walleij GFP_KERNEL);
3423508849adSLinus Walleij if (!base->lcla_pool.base_unaligned) {
3424508849adSLinus Walleij ret = -ENOMEM;
3425aae32ec6SMarkus Elfring goto free_page_list;
3426508849adSLinus Walleij }
3427508849adSLinus Walleij
3428508849adSLinus Walleij base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
3429508849adSLinus Walleij LCLA_ALIGNMENT);
3430508849adSLinus Walleij }
3431508849adSLinus Walleij
3432026cbc42SRabin Vincent pool->dma_addr = dma_map_single(base->dev, pool->base,
3433026cbc42SRabin Vincent SZ_1K * base->num_phy_chans,
3434026cbc42SRabin Vincent DMA_TO_DEVICE);
3435026cbc42SRabin Vincent if (dma_mapping_error(base->dev, pool->dma_addr)) {
3436026cbc42SRabin Vincent pool->dma_addr = 0;
3437026cbc42SRabin Vincent ret = -ENOMEM;
3438aae32ec6SMarkus Elfring goto free_page_list;
3439026cbc42SRabin Vincent }
3440026cbc42SRabin Vincent
3441508849adSLinus Walleij writel(virt_to_phys(base->lcla_pool.base),
3442508849adSLinus Walleij base->virtbase + D40_DREG_LCLA);
3443abac5bacSMarkus Elfring ret = 0;
3444aae32ec6SMarkus Elfring free_page_list:
3445508849adSLinus Walleij kfree(page_list);
3446508849adSLinus Walleij return ret;
3447508849adSLinus Walleij }
3448508849adSLinus Walleij
d40_of_probe(struct device * dev,struct device_node * np)3449e59d81e9SLinus Walleij static int __init d40_of_probe(struct device *dev,
34501814a170SLee Jones struct device_node *np)
34511814a170SLee Jones {
34521814a170SLee Jones struct stedma40_platform_data *pdata;
3453499c2bc3SLee Jones int num_phy = 0, num_memcpy = 0, num_disabled = 0;
3454cbbe13eaSSachin Kamat const __be32 *list;
34551814a170SLee Jones
3456e59d81e9SLinus Walleij pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
34571814a170SLee Jones if (!pdata)
34581814a170SLee Jones return -ENOMEM;
34591814a170SLee Jones
3460fd59f9e6SLee Jones /* If absent this value will be obtained from h/w. */
3461fd59f9e6SLee Jones of_property_read_u32(np, "dma-channels", &num_phy);
3462fd59f9e6SLee Jones if (num_phy > 0)
3463fd59f9e6SLee Jones pdata->num_of_phy_chans = num_phy;
3464fd59f9e6SLee Jones
3465a7dacb68SLee Jones list = of_get_property(np, "memcpy-channels", &num_memcpy);
3466a7dacb68SLee Jones num_memcpy /= sizeof(*list);
3467a7dacb68SLee Jones
3468a7dacb68SLee Jones if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
3469e59d81e9SLinus Walleij d40_err(dev,
3470a7dacb68SLee Jones "Invalid number of memcpy channels specified (%d)\n",
3471a7dacb68SLee Jones num_memcpy);
3472a7dacb68SLee Jones return -EINVAL;
3473a7dacb68SLee Jones }
3474a7dacb68SLee Jones pdata->num_of_memcpy_chans = num_memcpy;
3475a7dacb68SLee Jones
3476a7dacb68SLee Jones of_property_read_u32_array(np, "memcpy-channels",
3477a7dacb68SLee Jones dma40_memcpy_channels,
3478a7dacb68SLee Jones num_memcpy);
3479a7dacb68SLee Jones
3480499c2bc3SLee Jones list = of_get_property(np, "disabled-channels", &num_disabled);
3481499c2bc3SLee Jones num_disabled /= sizeof(*list);
3482499c2bc3SLee Jones
34835be2190aSDan Carpenter if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
3484e59d81e9SLinus Walleij d40_err(dev,
3485499c2bc3SLee Jones "Invalid number of disabled channels specified (%d)\n",
3486499c2bc3SLee Jones num_disabled);
3487499c2bc3SLee Jones return -EINVAL;
3488499c2bc3SLee Jones }
3489499c2bc3SLee Jones
3490499c2bc3SLee Jones of_property_read_u32_array(np, "disabled-channels",
3491499c2bc3SLee Jones pdata->disabled_channels,
3492499c2bc3SLee Jones num_disabled);
3493499c2bc3SLee Jones pdata->disabled_channels[num_disabled] = -1;
3494499c2bc3SLee Jones
3495e59d81e9SLinus Walleij dev->platform_data = pdata;
34961814a170SLee Jones
34971814a170SLee Jones return 0;
34981814a170SLee Jones }
34991814a170SLee Jones
d40_probe(struct platform_device * pdev)35008d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev)
35018d318a50SLinus Walleij {
3502fb85a8c5SLinus Walleij struct device *dev = &pdev->dev;
35031814a170SLee Jones struct device_node *np = pdev->dev.of_node;
35045a1a3b9cSLinus Walleij struct device_node *np_lcpa;
3505a9bae06dSMarkus Elfring struct d40_base *base;
3506aeb8974aSMarkus Elfring struct resource *res;
35075a1a3b9cSLinus Walleij struct resource res_lcpa;
35088d318a50SLinus Walleij int num_reserved_chans;
35098d318a50SLinus Walleij u32 val;
35102893f6bcSLinus Walleij int ret;
35118d318a50SLinus Walleij
3512e59d81e9SLinus Walleij if (d40_of_probe(dev, np)) {
35131814a170SLee Jones ret = -ENOMEM;
3514a9bae06dSMarkus Elfring goto report_failure;
35151814a170SLee Jones }
35168d318a50SLinus Walleij
35172893f6bcSLinus Walleij ret = d40_hw_detect_init(pdev, &base);
35182893f6bcSLinus Walleij if (ret)
3519a9bae06dSMarkus Elfring goto report_failure;
35208d318a50SLinus Walleij
35218d318a50SLinus Walleij num_reserved_chans = d40_phy_res_init(base);
35228d318a50SLinus Walleij
35238d318a50SLinus Walleij platform_set_drvdata(pdev, base);
35248d318a50SLinus Walleij
35258d318a50SLinus Walleij spin_lock_init(&base->interrupt_lock);
35268d318a50SLinus Walleij spin_lock_init(&base->execmd_lock);
35278d318a50SLinus Walleij
35285a1a3b9cSLinus Walleij /* Get IO for logical channel parameter address (LCPA) */
35295a1a3b9cSLinus Walleij np_lcpa = of_parse_phandle(np, "sram", 0);
35305a1a3b9cSLinus Walleij if (!np_lcpa) {
3531fb85a8c5SLinus Walleij dev_err(dev, "no LCPA SRAM node\n");
35322893f6bcSLinus Walleij ret = -EINVAL;
35335a1a3b9cSLinus Walleij goto report_failure;
35348d318a50SLinus Walleij }
35355a1a3b9cSLinus Walleij /* This is no device so read the address directly from the node */
35365a1a3b9cSLinus Walleij ret = of_address_to_resource(np_lcpa, 0, &res_lcpa);
35375a1a3b9cSLinus Walleij if (ret) {
3538fb85a8c5SLinus Walleij dev_err(dev, "no LCPA SRAM resource\n");
35395a1a3b9cSLinus Walleij goto report_failure;
35408d318a50SLinus Walleij }
35415a1a3b9cSLinus Walleij base->lcpa_size = resource_size(&res_lcpa);
35425a1a3b9cSLinus Walleij base->phy_lcpa = res_lcpa.start;
354341be14c7SArnd Bergmann dev_info(dev, "found LCPA SRAM at %pad, size %pa\n",
354441be14c7SArnd Bergmann &base->phy_lcpa, &base->lcpa_size);
35458d318a50SLinus Walleij
35468d318a50SLinus Walleij /* We make use of ESRAM memory for this. */
35478d318a50SLinus Walleij val = readl(base->virtbase + D40_DREG_LCPA);
35485a1a3b9cSLinus Walleij if (base->phy_lcpa != val && val != 0) {
3549fb85a8c5SLinus Walleij dev_warn(dev,
35505a1a3b9cSLinus Walleij "[%s] Mismatch LCPA dma 0x%x, def %08x\n",
35515a1a3b9cSLinus Walleij __func__, val, (u32)base->phy_lcpa);
35528d318a50SLinus Walleij } else
35535a1a3b9cSLinus Walleij writel(base->phy_lcpa, base->virtbase + D40_DREG_LCPA);
35548d318a50SLinus Walleij
3555339f5041SLinus Walleij base->lcpa_base = devm_ioremap(dev, base->phy_lcpa, base->lcpa_size);
35568d318a50SLinus Walleij if (!base->lcpa_base) {
35578d318a50SLinus Walleij ret = -ENOMEM;
3558fb85a8c5SLinus Walleij d40_err(dev, "Failed to ioremap LCPA region\n");
3559339f5041SLinus Walleij goto report_failure;
35608d318a50SLinus Walleij }
356128c7a19dSNarayanan G /* If lcla has to be located in ESRAM we don't need to allocate */
356228c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) {
356328c7a19dSNarayanan G res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
356428c7a19dSNarayanan G "lcla_esram");
356528c7a19dSNarayanan G if (!res) {
356628c7a19dSNarayanan G ret = -ENOENT;
3567fb85a8c5SLinus Walleij d40_err(dev,
356828c7a19dSNarayanan G "No \"lcla_esram\" memory resource\n");
3569339f5041SLinus Walleij goto report_failure;
357028c7a19dSNarayanan G }
3571339f5041SLinus Walleij base->lcla_pool.base = devm_ioremap(dev, res->start,
357228c7a19dSNarayanan G resource_size(res));
357328c7a19dSNarayanan G if (!base->lcla_pool.base) {
357428c7a19dSNarayanan G ret = -ENOMEM;
3575fb85a8c5SLinus Walleij d40_err(dev, "Failed to ioremap LCLA region\n");
3576339f5041SLinus Walleij goto report_failure;
357728c7a19dSNarayanan G }
357828c7a19dSNarayanan G writel(res->start, base->virtbase + D40_DREG_LCLA);
3579508849adSLinus Walleij
358028c7a19dSNarayanan G } else {
3581508849adSLinus Walleij ret = d40_lcla_allocate(base);
3582508849adSLinus Walleij if (ret) {
3583fb85a8c5SLinus Walleij d40_err(dev, "Failed to allocate LCLA area\n");
3584d7b7ecceSMarkus Elfring goto destroy_cache;
35858d318a50SLinus Walleij }
358628c7a19dSNarayanan G }
35878d318a50SLinus Walleij
35888d318a50SLinus Walleij spin_lock_init(&base->lcla_pool.lock);
35898d318a50SLinus Walleij
35908d318a50SLinus Walleij base->irq = platform_get_irq(pdev, 0);
3591c05ce690Sruanjinjie if (base->irq < 0) {
3592c05ce690Sruanjinjie ret = base->irq;
3593c05ce690Sruanjinjie goto destroy_cache;
3594c05ce690Sruanjinjie }
35958d318a50SLinus Walleij
35968d318a50SLinus Walleij ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
35978d318a50SLinus Walleij if (ret) {
3598fb85a8c5SLinus Walleij d40_err(dev, "No IRQ defined\n");
3599d7b7ecceSMarkus Elfring goto destroy_cache;
36008d318a50SLinus Walleij }
36018d318a50SLinus Walleij
360228c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) {
360328c7a19dSNarayanan G
360428c7a19dSNarayanan G base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
360528c7a19dSNarayanan G if (IS_ERR(base->lcpa_regulator)) {
3606fb85a8c5SLinus Walleij d40_err(dev, "Failed to get lcpa_regulator\n");
36078581bbcdSWei Yongjun ret = PTR_ERR(base->lcpa_regulator);
360828c7a19dSNarayanan G base->lcpa_regulator = NULL;
3609d7b7ecceSMarkus Elfring goto destroy_cache;
361028c7a19dSNarayanan G }
361128c7a19dSNarayanan G
361228c7a19dSNarayanan G ret = regulator_enable(base->lcpa_regulator);
361328c7a19dSNarayanan G if (ret) {
3614fb85a8c5SLinus Walleij d40_err(dev,
361528c7a19dSNarayanan G "Failed to enable lcpa_regulator\n");
361628c7a19dSNarayanan G regulator_put(base->lcpa_regulator);
361728c7a19dSNarayanan G base->lcpa_regulator = NULL;
3618d7b7ecceSMarkus Elfring goto destroy_cache;
361928c7a19dSNarayanan G }
362028c7a19dSNarayanan G }
362128c7a19dSNarayanan G
36222dafca17SUlf Hansson writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
36232dafca17SUlf Hansson
36242dafca17SUlf Hansson pm_runtime_irq_safe(base->dev);
36252dafca17SUlf Hansson pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
36262dafca17SUlf Hansson pm_runtime_use_autosuspend(base->dev);
36272dafca17SUlf Hansson pm_runtime_mark_last_busy(base->dev);
36282dafca17SUlf Hansson pm_runtime_set_active(base->dev);
36292dafca17SUlf Hansson pm_runtime_enable(base->dev);
36302dafca17SUlf Hansson
36318581bbcdSWei Yongjun ret = d40_dmaengine_init(base, num_reserved_chans);
36328581bbcdSWei Yongjun if (ret)
3633d7b7ecceSMarkus Elfring goto destroy_cache;
36348d318a50SLinus Walleij
3635*334304acSChristoph Hellwig dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
3636b96710e5SPer Forlin
36378d318a50SLinus Walleij d40_hw_init(base);
36388d318a50SLinus Walleij
36398581bbcdSWei Yongjun ret = of_dma_controller_register(np, d40_xlate, NULL);
364042ae6f16SLinus Walleij if (ret) {
3641fb85a8c5SLinus Walleij dev_err(dev,
3642fa332de5SLee Jones "could not register of_dma_controller\n");
364342ae6f16SLinus Walleij goto destroy_cache;
3644fa332de5SLee Jones }
3645fa332de5SLee Jones
36468d318a50SLinus Walleij dev_info(base->dev, "initialized\n");
36478d318a50SLinus Walleij return 0;
3648339f5041SLinus Walleij
3649d7b7ecceSMarkus Elfring destroy_cache:
3650026cbc42SRabin Vincent if (base->lcla_pool.dma_addr)
3651026cbc42SRabin Vincent dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
3652026cbc42SRabin Vincent SZ_1K * base->num_phy_chans,
3653026cbc42SRabin Vincent DMA_TO_DEVICE);
3654026cbc42SRabin Vincent
3655508849adSLinus Walleij if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
3656508849adSLinus Walleij free_pages((unsigned long)base->lcla_pool.base,
3657508849adSLinus Walleij base->lcla_pool.pages);
3658767a9675SJonas Aaberg
3659508849adSLinus Walleij kfree(base->lcla_pool.base_unaligned);
3660767a9675SJonas Aaberg
366128c7a19dSNarayanan G if (base->lcpa_regulator) {
366228c7a19dSNarayanan G regulator_disable(base->lcpa_regulator);
366328c7a19dSNarayanan G regulator_put(base->lcpa_regulator);
366428c7a19dSNarayanan G }
36650618c077SZhang Shurong pm_runtime_disable(base->dev);
366628c7a19dSNarayanan G
3667a9bae06dSMarkus Elfring report_failure:
3668fb85a8c5SLinus Walleij d40_err(dev, "probe failed\n");
36698d318a50SLinus Walleij return ret;
36708d318a50SLinus Walleij }
36718d318a50SLinus Walleij
36721814a170SLee Jones static const struct of_device_id d40_match[] = {
36731814a170SLee Jones { .compatible = "stericsson,dma40", },
36741814a170SLee Jones {}
36751814a170SLee Jones };
36761814a170SLee Jones
36778d318a50SLinus Walleij static struct platform_driver d40_driver = {
36788d318a50SLinus Walleij .driver = {
36798d318a50SLinus Walleij .name = D40_NAME,
3680123e4ca1SUlf Hansson .pm = &dma40_pm_ops,
36811814a170SLee Jones .of_match_table = d40_match,
36828d318a50SLinus Walleij },
36838d318a50SLinus Walleij };
36848d318a50SLinus Walleij
stedma40_init(void)3685cb9ab2d8SRabin Vincent static int __init stedma40_init(void)
36868d318a50SLinus Walleij {
36878d318a50SLinus Walleij return platform_driver_probe(&d40_driver, d40_probe);
36888d318a50SLinus Walleij }
3689a0eb221aSLinus Walleij subsys_initcall(stedma40_init);
3690