18d318a50SLinus Walleij /* 2d49278e3SPer Forlin * Copyright (C) Ericsson AB 2007-2008 3d49278e3SPer Forlin * Copyright (C) ST-Ericsson SA 2008-2010 4661385f9SPer Forlin * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson 5767a9675SJonas Aaberg * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson 68d318a50SLinus Walleij * License terms: GNU General Public License (GPL) version 2 78d318a50SLinus Walleij */ 88d318a50SLinus Walleij 9b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h> 108d318a50SLinus Walleij #include <linux/kernel.h> 118d318a50SLinus Walleij #include <linux/slab.h> 12f492b210SPaul Gortmaker #include <linux/export.h> 138d318a50SLinus Walleij #include <linux/dmaengine.h> 148d318a50SLinus Walleij #include <linux/platform_device.h> 158d318a50SLinus Walleij #include <linux/clk.h> 168d318a50SLinus Walleij #include <linux/delay.h> 177fb3e75eSNarayanan G #include <linux/pm.h> 187fb3e75eSNarayanan G #include <linux/pm_runtime.h> 19698e4732SJonas Aaberg #include <linux/err.h> 201814a170SLee Jones #include <linux/of.h> 21fa332de5SLee Jones #include <linux/of_dma.h> 22f4b89764SLinus Walleij #include <linux/amba/bus.h> 2315e4b78dSLinus Walleij #include <linux/regulator/consumer.h> 24865fab60SLinus Walleij #include <linux/platform_data/dma-ste-dma40.h> 258d318a50SLinus Walleij 26d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 278d318a50SLinus Walleij #include "ste_dma40_ll.h" 288d318a50SLinus Walleij 298d318a50SLinus Walleij #define D40_NAME "dma40" 308d318a50SLinus Walleij 318d318a50SLinus Walleij #define D40_PHY_CHAN -1 328d318a50SLinus Walleij 338d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */ 348d318a50SLinus Walleij #define D40_CHAN_POS(chan) (2 * (chan / 2)) 358d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan)) 368d318a50SLinus Walleij 378d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */ 388d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500 398d318a50SLinus Walleij 407fb3e75eSNarayanan G /* Milliseconds */ 417fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY 100 427fb3e75eSNarayanan G 43508849adSLinus Walleij /* Hardware requirement on LCLA alignment */ 44508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000 45698e4732SJonas Aaberg 46698e4732SJonas Aaberg /* Max number of links per event group */ 47698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128 48698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP 49698e4732SJonas Aaberg 50db72da92SLee Jones /* Max number of logical channels per physical channel */ 51db72da92SLee Jones #define D40_MAX_LOG_CHAN_PER_PHY 32 52db72da92SLee Jones 53508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */ 54508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256 55508849adSLinus Walleij 56508849adSLinus Walleij /* Bit markings for allocation map */ 578a3b6e14SLee Jones #define D40_ALLOC_FREE BIT(31) 588a3b6e14SLee Jones #define D40_ALLOC_PHY BIT(30) 598d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE 0 608d318a50SLinus Walleij 61a7dacb68SLee Jones #define D40_MEMCPY_MAX_CHANS 8 62a7dacb68SLee Jones 63664a57ecSLee Jones /* Reserved event lines for memcpy only. */ 64a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_0 51 65a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_1 56 66a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_2 57 67a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_3 58 68a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_4 59 69a2acaa21SLinus Walleij #define DB8500_DMA_MEMCPY_EV_5 60 70a2acaa21SLinus Walleij 71a2acaa21SLinus Walleij static int dma40_memcpy_channels[] = { 72a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_0, 73a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_1, 74a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_2, 75a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_3, 76a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_4, 77a2acaa21SLinus Walleij DB8500_DMA_MEMCPY_EV_5, 78a2acaa21SLinus Walleij }; 79664a57ecSLee Jones 8029027a1eSLee Jones /* Default configuration for physcial memcpy */ 81*b4a1ccdfSFabio Baltieri static struct stedma40_chan_cfg dma40_memcpy_conf_phy = { 8229027a1eSLee Jones .mode = STEDMA40_MODE_PHYSICAL, 832c2b62d5SLee Jones .dir = DMA_MEM_TO_MEM, 8429027a1eSLee Jones 8543f2e1a3SLee Jones .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 8629027a1eSLee Jones .src_info.psize = STEDMA40_PSIZE_PHY_1, 8729027a1eSLee Jones .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL, 8829027a1eSLee Jones 8943f2e1a3SLee Jones .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 9029027a1eSLee Jones .dst_info.psize = STEDMA40_PSIZE_PHY_1, 9129027a1eSLee Jones .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL, 9229027a1eSLee Jones }; 9329027a1eSLee Jones 9429027a1eSLee Jones /* Default configuration for logical memcpy */ 95*b4a1ccdfSFabio Baltieri static struct stedma40_chan_cfg dma40_memcpy_conf_log = { 9629027a1eSLee Jones .mode = STEDMA40_MODE_LOGICAL, 972c2b62d5SLee Jones .dir = DMA_MEM_TO_MEM, 9829027a1eSLee Jones 9943f2e1a3SLee Jones .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 10029027a1eSLee Jones .src_info.psize = STEDMA40_PSIZE_LOG_1, 10129027a1eSLee Jones .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL, 10229027a1eSLee Jones 10343f2e1a3SLee Jones .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE, 10429027a1eSLee Jones .dst_info.psize = STEDMA40_PSIZE_LOG_1, 10529027a1eSLee Jones .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL, 10629027a1eSLee Jones }; 10729027a1eSLee Jones 1088d318a50SLinus Walleij /** 1098d318a50SLinus Walleij * enum 40_command - The different commands and/or statuses. 1108d318a50SLinus Walleij * 1118d318a50SLinus Walleij * @D40_DMA_STOP: DMA channel command STOP or status STOPPED, 1128d318a50SLinus Walleij * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN. 1138d318a50SLinus Walleij * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible. 1148d318a50SLinus Walleij * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED. 1158d318a50SLinus Walleij */ 1168d318a50SLinus Walleij enum d40_command { 1178d318a50SLinus Walleij D40_DMA_STOP = 0, 1188d318a50SLinus Walleij D40_DMA_RUN = 1, 1198d318a50SLinus Walleij D40_DMA_SUSPEND_REQ = 2, 1208d318a50SLinus Walleij D40_DMA_SUSPENDED = 3 1218d318a50SLinus Walleij }; 1228d318a50SLinus Walleij 1237fb3e75eSNarayanan G /* 1241bdae6f4SNarayanan G * enum d40_events - The different Event Enables for the event lines. 1251bdae6f4SNarayanan G * 1261bdae6f4SNarayanan G * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan. 1271bdae6f4SNarayanan G * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan. 1281bdae6f4SNarayanan G * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line. 1291bdae6f4SNarayanan G * @D40_ROUND_EVENTLINE: Status check for event line. 1301bdae6f4SNarayanan G */ 1311bdae6f4SNarayanan G 1321bdae6f4SNarayanan G enum d40_events { 1331bdae6f4SNarayanan G D40_DEACTIVATE_EVENTLINE = 0, 1341bdae6f4SNarayanan G D40_ACTIVATE_EVENTLINE = 1, 1351bdae6f4SNarayanan G D40_SUSPEND_REQ_EVENTLINE = 2, 1361bdae6f4SNarayanan G D40_ROUND_EVENTLINE = 3 1371bdae6f4SNarayanan G }; 1381bdae6f4SNarayanan G 1391bdae6f4SNarayanan G /* 1407fb3e75eSNarayanan G * These are the registers that has to be saved and later restored 1417fb3e75eSNarayanan G * when the DMA hw is powered off. 1427fb3e75eSNarayanan G * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works. 1437fb3e75eSNarayanan G */ 1447fb3e75eSNarayanan G static u32 d40_backup_regs[] = { 1457fb3e75eSNarayanan G D40_DREG_LCPA, 1467fb3e75eSNarayanan G D40_DREG_LCLA, 1477fb3e75eSNarayanan G D40_DREG_PRMSE, 1487fb3e75eSNarayanan G D40_DREG_PRMSO, 1497fb3e75eSNarayanan G D40_DREG_PRMOE, 1507fb3e75eSNarayanan G D40_DREG_PRMOO, 1517fb3e75eSNarayanan G }; 1527fb3e75eSNarayanan G 1537fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs) 1547fb3e75eSNarayanan G 1553cb645dcSTong Liu /* 1563cb645dcSTong Liu * since 9540 and 8540 has the same HW revision 1573cb645dcSTong Liu * use v4a for 9540 or ealier 1583cb645dcSTong Liu * use v4b for 8540 or later 1593cb645dcSTong Liu * HW revision: 1603cb645dcSTong Liu * DB8500ed has revision 0 1613cb645dcSTong Liu * DB8500v1 has revision 2 1623cb645dcSTong Liu * DB8500v2 has revision 3 1633cb645dcSTong Liu * AP9540v1 has revision 4 1643cb645dcSTong Liu * DB8540v1 has revision 4 1653cb645dcSTong Liu * TODO: Check if all these registers have to be saved/restored on dma40 v4a 1663cb645dcSTong Liu */ 1673cb645dcSTong Liu static u32 d40_backup_regs_v4a[] = { 1687fb3e75eSNarayanan G D40_DREG_PSEG1, 1697fb3e75eSNarayanan G D40_DREG_PSEG2, 1707fb3e75eSNarayanan G D40_DREG_PSEG3, 1717fb3e75eSNarayanan G D40_DREG_PSEG4, 1727fb3e75eSNarayanan G D40_DREG_PCEG1, 1737fb3e75eSNarayanan G D40_DREG_PCEG2, 1747fb3e75eSNarayanan G D40_DREG_PCEG3, 1757fb3e75eSNarayanan G D40_DREG_PCEG4, 1767fb3e75eSNarayanan G D40_DREG_RSEG1, 1777fb3e75eSNarayanan G D40_DREG_RSEG2, 1787fb3e75eSNarayanan G D40_DREG_RSEG3, 1797fb3e75eSNarayanan G D40_DREG_RSEG4, 1807fb3e75eSNarayanan G D40_DREG_RCEG1, 1817fb3e75eSNarayanan G D40_DREG_RCEG2, 1827fb3e75eSNarayanan G D40_DREG_RCEG3, 1837fb3e75eSNarayanan G D40_DREG_RCEG4, 1847fb3e75eSNarayanan G }; 1857fb3e75eSNarayanan G 1863cb645dcSTong Liu #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a) 1873cb645dcSTong Liu 1883cb645dcSTong Liu static u32 d40_backup_regs_v4b[] = { 1893cb645dcSTong Liu D40_DREG_CPSEG1, 1903cb645dcSTong Liu D40_DREG_CPSEG2, 1913cb645dcSTong Liu D40_DREG_CPSEG3, 1923cb645dcSTong Liu D40_DREG_CPSEG4, 1933cb645dcSTong Liu D40_DREG_CPSEG5, 1943cb645dcSTong Liu D40_DREG_CPCEG1, 1953cb645dcSTong Liu D40_DREG_CPCEG2, 1963cb645dcSTong Liu D40_DREG_CPCEG3, 1973cb645dcSTong Liu D40_DREG_CPCEG4, 1983cb645dcSTong Liu D40_DREG_CPCEG5, 1993cb645dcSTong Liu D40_DREG_CRSEG1, 2003cb645dcSTong Liu D40_DREG_CRSEG2, 2013cb645dcSTong Liu D40_DREG_CRSEG3, 2023cb645dcSTong Liu D40_DREG_CRSEG4, 2033cb645dcSTong Liu D40_DREG_CRSEG5, 2043cb645dcSTong Liu D40_DREG_CRCEG1, 2053cb645dcSTong Liu D40_DREG_CRCEG2, 2063cb645dcSTong Liu D40_DREG_CRCEG3, 2073cb645dcSTong Liu D40_DREG_CRCEG4, 2083cb645dcSTong Liu D40_DREG_CRCEG5, 2093cb645dcSTong Liu }; 2103cb645dcSTong Liu 2113cb645dcSTong Liu #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b) 2127fb3e75eSNarayanan G 2137fb3e75eSNarayanan G static u32 d40_backup_regs_chan[] = { 2147fb3e75eSNarayanan G D40_CHAN_REG_SSCFG, 2157fb3e75eSNarayanan G D40_CHAN_REG_SSELT, 2167fb3e75eSNarayanan G D40_CHAN_REG_SSPTR, 2177fb3e75eSNarayanan G D40_CHAN_REG_SSLNK, 2187fb3e75eSNarayanan G D40_CHAN_REG_SDCFG, 2197fb3e75eSNarayanan G D40_CHAN_REG_SDELT, 2207fb3e75eSNarayanan G D40_CHAN_REG_SDPTR, 2217fb3e75eSNarayanan G D40_CHAN_REG_SDLNK, 2227fb3e75eSNarayanan G }; 2237fb3e75eSNarayanan G 22484b3da14SLee Jones #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \ 22584b3da14SLee Jones BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B) 22684b3da14SLee Jones 2278d318a50SLinus Walleij /** 2283cb645dcSTong Liu * struct d40_interrupt_lookup - lookup table for interrupt handler 2293cb645dcSTong Liu * 2303cb645dcSTong Liu * @src: Interrupt mask register. 2313cb645dcSTong Liu * @clr: Interrupt clear register. 2323cb645dcSTong Liu * @is_error: true if this is an error interrupt. 2333cb645dcSTong Liu * @offset: start delta in the lookup_log_chans in d40_base. If equals to 2343cb645dcSTong Liu * D40_PHY_CHAN, the lookup_phy_chans shall be used instead. 2353cb645dcSTong Liu */ 2363cb645dcSTong Liu struct d40_interrupt_lookup { 2373cb645dcSTong Liu u32 src; 2383cb645dcSTong Liu u32 clr; 2393cb645dcSTong Liu bool is_error; 2403cb645dcSTong Liu int offset; 2413cb645dcSTong Liu }; 2423cb645dcSTong Liu 2433cb645dcSTong Liu 2443cb645dcSTong Liu static struct d40_interrupt_lookup il_v4a[] = { 2453cb645dcSTong Liu {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0}, 2463cb645dcSTong Liu {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32}, 2473cb645dcSTong Liu {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64}, 2483cb645dcSTong Liu {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96}, 2493cb645dcSTong Liu {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0}, 2503cb645dcSTong Liu {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32}, 2513cb645dcSTong Liu {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64}, 2523cb645dcSTong Liu {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96}, 2533cb645dcSTong Liu {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN}, 2543cb645dcSTong Liu {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN}, 2553cb645dcSTong Liu }; 2563cb645dcSTong Liu 2573cb645dcSTong Liu static struct d40_interrupt_lookup il_v4b[] = { 2583cb645dcSTong Liu {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0}, 2593cb645dcSTong Liu {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32}, 2603cb645dcSTong Liu {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64}, 2613cb645dcSTong Liu {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96}, 2623cb645dcSTong Liu {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128}, 2633cb645dcSTong Liu {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0}, 2643cb645dcSTong Liu {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32}, 2653cb645dcSTong Liu {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64}, 2663cb645dcSTong Liu {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96}, 2673cb645dcSTong Liu {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128}, 2683cb645dcSTong Liu {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN}, 2693cb645dcSTong Liu {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN}, 2703cb645dcSTong Liu }; 2713cb645dcSTong Liu 2723cb645dcSTong Liu /** 2733cb645dcSTong Liu * struct d40_reg_val - simple lookup struct 2743cb645dcSTong Liu * 2753cb645dcSTong Liu * @reg: The register. 2763cb645dcSTong Liu * @val: The value that belongs to the register in reg. 2773cb645dcSTong Liu */ 2783cb645dcSTong Liu struct d40_reg_val { 2793cb645dcSTong Liu unsigned int reg; 2803cb645dcSTong Liu unsigned int val; 2813cb645dcSTong Liu }; 2823cb645dcSTong Liu 2833cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4a[] = { 2843cb645dcSTong Liu /* Clock every part of the DMA block from start */ 2853cb645dcSTong Liu { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL}, 2863cb645dcSTong Liu 2873cb645dcSTong Liu /* Interrupts on all logical channels */ 2883cb645dcSTong Liu { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF}, 2893cb645dcSTong Liu { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF}, 2903cb645dcSTong Liu { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF}, 2913cb645dcSTong Liu { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF}, 2923cb645dcSTong Liu { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF}, 2933cb645dcSTong Liu { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF}, 2943cb645dcSTong Liu { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF}, 2953cb645dcSTong Liu { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF}, 2963cb645dcSTong Liu { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF}, 2973cb645dcSTong Liu { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF}, 2983cb645dcSTong Liu { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF}, 2993cb645dcSTong Liu { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF} 3003cb645dcSTong Liu }; 3013cb645dcSTong Liu static __initdata struct d40_reg_val dma_init_reg_v4b[] = { 3023cb645dcSTong Liu /* Clock every part of the DMA block from start */ 3033cb645dcSTong Liu { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL}, 3043cb645dcSTong Liu 3053cb645dcSTong Liu /* Interrupts on all logical channels */ 3063cb645dcSTong Liu { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF}, 3073cb645dcSTong Liu { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF}, 3083cb645dcSTong Liu { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF}, 3093cb645dcSTong Liu { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF}, 3103cb645dcSTong Liu { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF}, 3113cb645dcSTong Liu { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF}, 3123cb645dcSTong Liu { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF}, 3133cb645dcSTong Liu { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF}, 3143cb645dcSTong Liu { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF}, 3153cb645dcSTong Liu { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF}, 3163cb645dcSTong Liu { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF}, 3173cb645dcSTong Liu { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF}, 3183cb645dcSTong Liu { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF}, 3193cb645dcSTong Liu { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF}, 3203cb645dcSTong Liu { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF} 3213cb645dcSTong Liu }; 3223cb645dcSTong Liu 3233cb645dcSTong Liu /** 3248d318a50SLinus Walleij * struct d40_lli_pool - Structure for keeping LLIs in memory 3258d318a50SLinus Walleij * 3268d318a50SLinus Walleij * @base: Pointer to memory area when the pre_alloc_lli's are not large 3278d318a50SLinus Walleij * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if 3288d318a50SLinus Walleij * pre_alloc_lli is used. 329b00f938cSRabin Vincent * @dma_addr: DMA address, if mapped 3308d318a50SLinus Walleij * @size: The size in bytes of the memory at base or the size of pre_alloc_lli. 3318d318a50SLinus Walleij * @pre_alloc_lli: Pre allocated area for the most common case of transfers, 3328d318a50SLinus Walleij * one buffer to one buffer. 3338d318a50SLinus Walleij */ 3348d318a50SLinus Walleij struct d40_lli_pool { 3358d318a50SLinus Walleij void *base; 3368d318a50SLinus Walleij int size; 337b00f938cSRabin Vincent dma_addr_t dma_addr; 3388d318a50SLinus Walleij /* Space for dst and src, plus an extra for padding */ 3398d318a50SLinus Walleij u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)]; 3408d318a50SLinus Walleij }; 3418d318a50SLinus Walleij 3428d318a50SLinus Walleij /** 3438d318a50SLinus Walleij * struct d40_desc - A descriptor is one DMA job. 3448d318a50SLinus Walleij * 3458d318a50SLinus Walleij * @lli_phy: LLI settings for physical channel. Both src and dst= 3468d318a50SLinus Walleij * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if 3478d318a50SLinus Walleij * lli_len equals one. 3488d318a50SLinus Walleij * @lli_log: Same as above but for logical channels. 3498d318a50SLinus Walleij * @lli_pool: The pool with two entries pre-allocated. 350941b77a3SPer Friden * @lli_len: Number of llis of current descriptor. 35125985edcSLucas De Marchi * @lli_current: Number of transferred llis. 352698e4732SJonas Aaberg * @lcla_alloc: Number of LCLA entries allocated. 3538d318a50SLinus Walleij * @txd: DMA engine struct. Used for among other things for communication 3548d318a50SLinus Walleij * during a transfer. 3558d318a50SLinus Walleij * @node: List entry. 3568d318a50SLinus Walleij * @is_in_client_list: true if the client owns this descriptor. 3577fb3e75eSNarayanan G * @cyclic: true if this is a cyclic job 3588d318a50SLinus Walleij * 3598d318a50SLinus Walleij * This descriptor is used for both logical and physical transfers. 3608d318a50SLinus Walleij */ 3618d318a50SLinus Walleij struct d40_desc { 3628d318a50SLinus Walleij /* LLI physical */ 3638d318a50SLinus Walleij struct d40_phy_lli_bidir lli_phy; 3648d318a50SLinus Walleij /* LLI logical */ 3658d318a50SLinus Walleij struct d40_log_lli_bidir lli_log; 3668d318a50SLinus Walleij 3678d318a50SLinus Walleij struct d40_lli_pool lli_pool; 368941b77a3SPer Friden int lli_len; 369698e4732SJonas Aaberg int lli_current; 370698e4732SJonas Aaberg int lcla_alloc; 3718d318a50SLinus Walleij 3728d318a50SLinus Walleij struct dma_async_tx_descriptor txd; 3738d318a50SLinus Walleij struct list_head node; 3748d318a50SLinus Walleij 3758d318a50SLinus Walleij bool is_in_client_list; 3760c842b55SRabin Vincent bool cyclic; 3778d318a50SLinus Walleij }; 3788d318a50SLinus Walleij 3798d318a50SLinus Walleij /** 3808d318a50SLinus Walleij * struct d40_lcla_pool - LCLA pool settings and data. 3818d318a50SLinus Walleij * 382508849adSLinus Walleij * @base: The virtual address of LCLA. 18 bit aligned. 383508849adSLinus Walleij * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used. 384508849adSLinus Walleij * This pointer is only there for clean-up on error. 385508849adSLinus Walleij * @pages: The number of pages needed for all physical channels. 386508849adSLinus Walleij * Only used later for clean-up on error 3878d318a50SLinus Walleij * @lock: Lock to protect the content in this struct. 388698e4732SJonas Aaberg * @alloc_map: big map over which LCLA entry is own by which job. 3898d318a50SLinus Walleij */ 3908d318a50SLinus Walleij struct d40_lcla_pool { 3918d318a50SLinus Walleij void *base; 392026cbc42SRabin Vincent dma_addr_t dma_addr; 393508849adSLinus Walleij void *base_unaligned; 394508849adSLinus Walleij int pages; 3958d318a50SLinus Walleij spinlock_t lock; 396698e4732SJonas Aaberg struct d40_desc **alloc_map; 3978d318a50SLinus Walleij }; 3988d318a50SLinus Walleij 3998d318a50SLinus Walleij /** 4008d318a50SLinus Walleij * struct d40_phy_res - struct for handling eventlines mapped to physical 4018d318a50SLinus Walleij * channels. 4028d318a50SLinus Walleij * 4038d318a50SLinus Walleij * @lock: A lock protection this entity. 4047fb3e75eSNarayanan G * @reserved: True if used by secure world or otherwise. 4058d318a50SLinus Walleij * @num: The physical channel number of this entity. 4068d318a50SLinus Walleij * @allocated_src: Bit mapped to show which src event line's are mapped to 4078d318a50SLinus Walleij * this physical channel. Can also be free or physically allocated. 4088d318a50SLinus Walleij * @allocated_dst: Same as for src but is dst. 4098d318a50SLinus Walleij * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as 410767a9675SJonas Aaberg * event line number. 4117407048bSFabio Baltieri * @use_soft_lli: To mark if the linked lists of channel are managed by SW. 4128d318a50SLinus Walleij */ 4138d318a50SLinus Walleij struct d40_phy_res { 4148d318a50SLinus Walleij spinlock_t lock; 4157fb3e75eSNarayanan G bool reserved; 4168d318a50SLinus Walleij int num; 4178d318a50SLinus Walleij u32 allocated_src; 4188d318a50SLinus Walleij u32 allocated_dst; 4197407048bSFabio Baltieri bool use_soft_lli; 4208d318a50SLinus Walleij }; 4218d318a50SLinus Walleij 4228d318a50SLinus Walleij struct d40_base; 4238d318a50SLinus Walleij 4248d318a50SLinus Walleij /** 4258d318a50SLinus Walleij * struct d40_chan - Struct that describes a channel. 4268d318a50SLinus Walleij * 4278d318a50SLinus Walleij * @lock: A spinlock to protect this struct. 4288d318a50SLinus Walleij * @log_num: The logical number, if any of this channel. 4298d318a50SLinus Walleij * @pending_tx: The number of pending transfers. Used between interrupt handler 4308d318a50SLinus Walleij * and tasklet. 4318d318a50SLinus Walleij * @busy: Set to true when transfer is ongoing on this channel. 4322a614340SJonas Aaberg * @phy_chan: Pointer to physical channel which this instance runs on. If this 4332a614340SJonas Aaberg * point is NULL, then the channel is not allocated. 4348d318a50SLinus Walleij * @chan: DMA engine handle. 4358d318a50SLinus Walleij * @tasklet: Tasklet that gets scheduled from interrupt context to complete a 4368d318a50SLinus Walleij * transfer and call client callback. 4378d318a50SLinus Walleij * @client: Cliented owned descriptor list. 438da063d26SPer Forlin * @pending_queue: Submitted jobs, to be issued by issue_pending() 4398d318a50SLinus Walleij * @active: Active descriptor. 4404226dd86SFabio Baltieri * @done: Completed jobs 4418d318a50SLinus Walleij * @queue: Queued jobs. 44282babbb3SPer Forlin * @prepare_queue: Prepared jobs. 4438d318a50SLinus Walleij * @dma_cfg: The client configuration of this dma channel. 444ce2ca125SRabin Vincent * @configured: whether the dma_cfg configuration is valid 4458d318a50SLinus Walleij * @base: Pointer to the device instance struct. 4468d318a50SLinus Walleij * @src_def_cfg: Default cfg register setting for src. 4478d318a50SLinus Walleij * @dst_def_cfg: Default cfg register setting for dst. 4488d318a50SLinus Walleij * @log_def: Default logical channel settings. 4498d318a50SLinus Walleij * @lcpa: Pointer to dst and src lcpa settings. 450ae752bf4Som prakash * @runtime_addr: runtime configured address. 451ae752bf4Som prakash * @runtime_direction: runtime configured direction. 4528d318a50SLinus Walleij * 4538d318a50SLinus Walleij * This struct can either "be" a logical or a physical channel. 4548d318a50SLinus Walleij */ 4558d318a50SLinus Walleij struct d40_chan { 4568d318a50SLinus Walleij spinlock_t lock; 4578d318a50SLinus Walleij int log_num; 4588d318a50SLinus Walleij int pending_tx; 4598d318a50SLinus Walleij bool busy; 4608d318a50SLinus Walleij struct d40_phy_res *phy_chan; 4618d318a50SLinus Walleij struct dma_chan chan; 4628d318a50SLinus Walleij struct tasklet_struct tasklet; 4638d318a50SLinus Walleij struct list_head client; 464a8f3067bSPer Forlin struct list_head pending_queue; 4658d318a50SLinus Walleij struct list_head active; 4664226dd86SFabio Baltieri struct list_head done; 4678d318a50SLinus Walleij struct list_head queue; 46882babbb3SPer Forlin struct list_head prepare_queue; 4698d318a50SLinus Walleij struct stedma40_chan_cfg dma_cfg; 470ce2ca125SRabin Vincent bool configured; 4718d318a50SLinus Walleij struct d40_base *base; 4728d318a50SLinus Walleij /* Default register configurations */ 4738d318a50SLinus Walleij u32 src_def_cfg; 4748d318a50SLinus Walleij u32 dst_def_cfg; 4758d318a50SLinus Walleij struct d40_def_lcsp log_def; 4768d318a50SLinus Walleij struct d40_log_lli_full *lcpa; 47795e1400fSLinus Walleij /* Runtime reconfiguration */ 47895e1400fSLinus Walleij dma_addr_t runtime_addr; 479db8196dfSVinod Koul enum dma_transfer_direction runtime_direction; 4808d318a50SLinus Walleij }; 4818d318a50SLinus Walleij 4828d318a50SLinus Walleij /** 4833cb645dcSTong Liu * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA 4843cb645dcSTong Liu * controller 4853cb645dcSTong Liu * 4863cb645dcSTong Liu * @backup: the pointer to the registers address array for backup 4873cb645dcSTong Liu * @backup_size: the size of the registers address array for backup 4883cb645dcSTong Liu * @realtime_en: the realtime enable register 4893cb645dcSTong Liu * @realtime_clear: the realtime clear register 4903cb645dcSTong Liu * @high_prio_en: the high priority enable register 4913cb645dcSTong Liu * @high_prio_clear: the high priority clear register 4923cb645dcSTong Liu * @interrupt_en: the interrupt enable register 4933cb645dcSTong Liu * @interrupt_clear: the interrupt clear register 4943cb645dcSTong Liu * @il: the pointer to struct d40_interrupt_lookup 4953cb645dcSTong Liu * @il_size: the size of d40_interrupt_lookup array 4963cb645dcSTong Liu * @init_reg: the pointer to the struct d40_reg_val 4973cb645dcSTong Liu * @init_reg_size: the size of d40_reg_val array 4983cb645dcSTong Liu */ 4993cb645dcSTong Liu struct d40_gen_dmac { 5003cb645dcSTong Liu u32 *backup; 5013cb645dcSTong Liu u32 backup_size; 5023cb645dcSTong Liu u32 realtime_en; 5033cb645dcSTong Liu u32 realtime_clear; 5043cb645dcSTong Liu u32 high_prio_en; 5053cb645dcSTong Liu u32 high_prio_clear; 5063cb645dcSTong Liu u32 interrupt_en; 5073cb645dcSTong Liu u32 interrupt_clear; 5083cb645dcSTong Liu struct d40_interrupt_lookup *il; 5093cb645dcSTong Liu u32 il_size; 5103cb645dcSTong Liu struct d40_reg_val *init_reg; 5113cb645dcSTong Liu u32 init_reg_size; 5123cb645dcSTong Liu }; 5133cb645dcSTong Liu 5143cb645dcSTong Liu /** 5158d318a50SLinus Walleij * struct d40_base - The big global struct, one for each probe'd instance. 5168d318a50SLinus Walleij * 5178d318a50SLinus Walleij * @interrupt_lock: Lock used to make sure one interrupt is handle a time. 5188d318a50SLinus Walleij * @execmd_lock: Lock for execute command usage since several channels share 5198d318a50SLinus Walleij * the same physical register. 5208d318a50SLinus Walleij * @dev: The device structure. 5218d318a50SLinus Walleij * @virtbase: The virtual base address of the DMA's register. 522f4185592SLinus Walleij * @rev: silicon revision detected. 5238d318a50SLinus Walleij * @clk: Pointer to the DMA clock structure. 5248d318a50SLinus Walleij * @phy_start: Physical memory start of the DMA registers. 5258d318a50SLinus Walleij * @phy_size: Size of the DMA register map. 5268d318a50SLinus Walleij * @irq: The IRQ number. 527a7dacb68SLee Jones * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem 528a7dacb68SLee Jones * transfers). 5298d318a50SLinus Walleij * @num_phy_chans: The number of physical channels. Read from HW. This 5308d318a50SLinus Walleij * is the number of available channels for this driver, not counting "Secure 5318d318a50SLinus Walleij * mode" allocated physical channels. 5328d318a50SLinus Walleij * @num_log_chans: The number of logical channels. Calculated from 5338d318a50SLinus Walleij * num_phy_chans. 5348d318a50SLinus Walleij * @dma_both: dma_device channels that can do both memcpy and slave transfers. 5358d318a50SLinus Walleij * @dma_slave: dma_device channels that can do only do slave transfers. 5368d318a50SLinus Walleij * @dma_memcpy: dma_device channels that can do only do memcpy transfers. 5377fb3e75eSNarayanan G * @phy_chans: Room for all possible physical channels in system. 5388d318a50SLinus Walleij * @log_chans: Room for all possible logical channels in system. 5398d318a50SLinus Walleij * @lookup_log_chans: Used to map interrupt number to logical channel. Points 5408d318a50SLinus Walleij * to log_chans entries. 5418d318a50SLinus Walleij * @lookup_phy_chans: Used to map interrupt number to physical channel. Points 5428d318a50SLinus Walleij * to phy_chans entries. 5438d318a50SLinus Walleij * @plat_data: Pointer to provided platform_data which is the driver 5448d318a50SLinus Walleij * configuration. 54528c7a19dSNarayanan G * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla. 5468d318a50SLinus Walleij * @phy_res: Vector containing all physical channels. 5478d318a50SLinus Walleij * @lcla_pool: lcla pool settings and data. 5488d318a50SLinus Walleij * @lcpa_base: The virtual mapped address of LCPA. 5498d318a50SLinus Walleij * @phy_lcpa: The physical address of the LCPA. 5508d318a50SLinus Walleij * @lcpa_size: The size of the LCPA area. 551c675b1b4SJonas Aaberg * @desc_slab: cache for descriptors. 5527fb3e75eSNarayanan G * @reg_val_backup: Here the values of some hardware registers are stored 5537fb3e75eSNarayanan G * before the DMA is powered off. They are restored when the power is back on. 5543cb645dcSTong Liu * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and 5553cb645dcSTong Liu * later 5567fb3e75eSNarayanan G * @reg_val_backup_chan: Backup data for standard channel parameter registers. 5577fb3e75eSNarayanan G * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off. 5587fb3e75eSNarayanan G * @initialized: true if the dma has been initialized 5593cb645dcSTong Liu * @gen_dmac: the struct for generic registers values to represent u8500/8540 5603cb645dcSTong Liu * DMA controller 5618d318a50SLinus Walleij */ 5628d318a50SLinus Walleij struct d40_base { 5638d318a50SLinus Walleij spinlock_t interrupt_lock; 5648d318a50SLinus Walleij spinlock_t execmd_lock; 5658d318a50SLinus Walleij struct device *dev; 5668d318a50SLinus Walleij void __iomem *virtbase; 567f4185592SLinus Walleij u8 rev:4; 5688d318a50SLinus Walleij struct clk *clk; 5698d318a50SLinus Walleij phys_addr_t phy_start; 5708d318a50SLinus Walleij resource_size_t phy_size; 5718d318a50SLinus Walleij int irq; 572a7dacb68SLee Jones int num_memcpy_chans; 5738d318a50SLinus Walleij int num_phy_chans; 5748d318a50SLinus Walleij int num_log_chans; 575b96710e5SPer Forlin struct device_dma_parameters dma_parms; 5768d318a50SLinus Walleij struct dma_device dma_both; 5778d318a50SLinus Walleij struct dma_device dma_slave; 5788d318a50SLinus Walleij struct dma_device dma_memcpy; 5798d318a50SLinus Walleij struct d40_chan *phy_chans; 5808d318a50SLinus Walleij struct d40_chan *log_chans; 5818d318a50SLinus Walleij struct d40_chan **lookup_log_chans; 5828d318a50SLinus Walleij struct d40_chan **lookup_phy_chans; 5838d318a50SLinus Walleij struct stedma40_platform_data *plat_data; 58428c7a19dSNarayanan G struct regulator *lcpa_regulator; 5858d318a50SLinus Walleij /* Physical half channels */ 5868d318a50SLinus Walleij struct d40_phy_res *phy_res; 5878d318a50SLinus Walleij struct d40_lcla_pool lcla_pool; 5888d318a50SLinus Walleij void *lcpa_base; 5898d318a50SLinus Walleij dma_addr_t phy_lcpa; 5908d318a50SLinus Walleij resource_size_t lcpa_size; 591c675b1b4SJonas Aaberg struct kmem_cache *desc_slab; 5927fb3e75eSNarayanan G u32 reg_val_backup[BACKUP_REGS_SZ]; 59384b3da14SLee Jones u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX]; 5947fb3e75eSNarayanan G u32 *reg_val_backup_chan; 5957fb3e75eSNarayanan G u16 gcc_pwr_off_mask; 5967fb3e75eSNarayanan G bool initialized; 5973cb645dcSTong Liu struct d40_gen_dmac gen_dmac; 5988d318a50SLinus Walleij }; 5998d318a50SLinus Walleij 600262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c) 601262d2915SRabin Vincent { 602262d2915SRabin Vincent return &d40c->chan.dev->device; 603262d2915SRabin Vincent } 604262d2915SRabin Vincent 605724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan) 606724a8577SRabin Vincent { 607724a8577SRabin Vincent return chan->log_num == D40_PHY_CHAN; 608724a8577SRabin Vincent } 609724a8577SRabin Vincent 610724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan) 611724a8577SRabin Vincent { 612724a8577SRabin Vincent return !chan_is_physical(chan); 613724a8577SRabin Vincent } 614724a8577SRabin Vincent 6158ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan) 6168ca84687SRabin Vincent { 6178ca84687SRabin Vincent return chan->base->virtbase + D40_DREG_PCBASE + 6188ca84687SRabin Vincent chan->phy_chan->num * D40_DREG_PCDELTA; 6198ca84687SRabin Vincent } 6208ca84687SRabin Vincent 6216db5a8baSRabin Vincent #define d40_err(dev, format, arg...) \ 6226db5a8baSRabin Vincent dev_err(dev, "[%s] " format, __func__, ## arg) 6236db5a8baSRabin Vincent 6246db5a8baSRabin Vincent #define chan_err(d40c, format, arg...) \ 6256db5a8baSRabin Vincent d40_err(chan2dev(d40c), format, ## arg) 6266db5a8baSRabin Vincent 627b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d, 628dbd88788SRabin Vincent int lli_len) 6298d318a50SLinus Walleij { 630dbd88788SRabin Vincent bool is_log = chan_is_logical(d40c); 6318d318a50SLinus Walleij u32 align; 6328d318a50SLinus Walleij void *base; 6338d318a50SLinus Walleij 6348d318a50SLinus Walleij if (is_log) 6358d318a50SLinus Walleij align = sizeof(struct d40_log_lli); 6368d318a50SLinus Walleij else 6378d318a50SLinus Walleij align = sizeof(struct d40_phy_lli); 6388d318a50SLinus Walleij 6398d318a50SLinus Walleij if (lli_len == 1) { 6408d318a50SLinus Walleij base = d40d->lli_pool.pre_alloc_lli; 6418d318a50SLinus Walleij d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli); 6428d318a50SLinus Walleij d40d->lli_pool.base = NULL; 6438d318a50SLinus Walleij } else { 644594ece4dSRabin Vincent d40d->lli_pool.size = lli_len * 2 * align; 6458d318a50SLinus Walleij 6468d318a50SLinus Walleij base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT); 6478d318a50SLinus Walleij d40d->lli_pool.base = base; 6488d318a50SLinus Walleij 6498d318a50SLinus Walleij if (d40d->lli_pool.base == NULL) 6508d318a50SLinus Walleij return -ENOMEM; 6518d318a50SLinus Walleij } 6528d318a50SLinus Walleij 6538d318a50SLinus Walleij if (is_log) { 654d924abadSRabin Vincent d40d->lli_log.src = PTR_ALIGN(base, align); 655594ece4dSRabin Vincent d40d->lli_log.dst = d40d->lli_log.src + lli_len; 656b00f938cSRabin Vincent 657b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0; 6588d318a50SLinus Walleij } else { 659d924abadSRabin Vincent d40d->lli_phy.src = PTR_ALIGN(base, align); 660594ece4dSRabin Vincent d40d->lli_phy.dst = d40d->lli_phy.src + lli_len; 661b00f938cSRabin Vincent 662b00f938cSRabin Vincent d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev, 663b00f938cSRabin Vincent d40d->lli_phy.src, 664b00f938cSRabin Vincent d40d->lli_pool.size, 665b00f938cSRabin Vincent DMA_TO_DEVICE); 666b00f938cSRabin Vincent 667b00f938cSRabin Vincent if (dma_mapping_error(d40c->base->dev, 668b00f938cSRabin Vincent d40d->lli_pool.dma_addr)) { 669b00f938cSRabin Vincent kfree(d40d->lli_pool.base); 670b00f938cSRabin Vincent d40d->lli_pool.base = NULL; 671b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0; 672b00f938cSRabin Vincent return -ENOMEM; 673b00f938cSRabin Vincent } 6748d318a50SLinus Walleij } 6758d318a50SLinus Walleij 6768d318a50SLinus Walleij return 0; 6778d318a50SLinus Walleij } 6788d318a50SLinus Walleij 679b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d) 6808d318a50SLinus Walleij { 681b00f938cSRabin Vincent if (d40d->lli_pool.dma_addr) 682b00f938cSRabin Vincent dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr, 683b00f938cSRabin Vincent d40d->lli_pool.size, DMA_TO_DEVICE); 684b00f938cSRabin Vincent 6858d318a50SLinus Walleij kfree(d40d->lli_pool.base); 6868d318a50SLinus Walleij d40d->lli_pool.base = NULL; 6878d318a50SLinus Walleij d40d->lli_pool.size = 0; 6888d318a50SLinus Walleij d40d->lli_log.src = NULL; 6898d318a50SLinus Walleij d40d->lli_log.dst = NULL; 6908d318a50SLinus Walleij d40d->lli_phy.src = NULL; 6918d318a50SLinus Walleij d40d->lli_phy.dst = NULL; 6928d318a50SLinus Walleij } 6938d318a50SLinus Walleij 694698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c, 695698e4732SJonas Aaberg struct d40_desc *d40d) 696698e4732SJonas Aaberg { 697698e4732SJonas Aaberg unsigned long flags; 698698e4732SJonas Aaberg int i; 699698e4732SJonas Aaberg int ret = -EINVAL; 700698e4732SJonas Aaberg 701698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); 702698e4732SJonas Aaberg 703698e4732SJonas Aaberg /* 704698e4732SJonas Aaberg * Allocate both src and dst at the same time, therefore the half 705698e4732SJonas Aaberg * start on 1 since 0 can't be used since zero is used as end marker. 706698e4732SJonas Aaberg */ 707698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { 7087ce529efSFabio Baltieri int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i; 7097ce529efSFabio Baltieri 7107ce529efSFabio Baltieri if (!d40c->base->lcla_pool.alloc_map[idx]) { 7117ce529efSFabio Baltieri d40c->base->lcla_pool.alloc_map[idx] = d40d; 712698e4732SJonas Aaberg d40d->lcla_alloc++; 713698e4732SJonas Aaberg ret = i; 714698e4732SJonas Aaberg break; 715698e4732SJonas Aaberg } 716698e4732SJonas Aaberg } 717698e4732SJonas Aaberg 718698e4732SJonas Aaberg spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); 719698e4732SJonas Aaberg 720698e4732SJonas Aaberg return ret; 721698e4732SJonas Aaberg } 722698e4732SJonas Aaberg 723698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c, 724698e4732SJonas Aaberg struct d40_desc *d40d) 725698e4732SJonas Aaberg { 726698e4732SJonas Aaberg unsigned long flags; 727698e4732SJonas Aaberg int i; 728698e4732SJonas Aaberg int ret = -EINVAL; 729698e4732SJonas Aaberg 730724a8577SRabin Vincent if (chan_is_physical(d40c)) 731698e4732SJonas Aaberg return 0; 732698e4732SJonas Aaberg 733698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); 734698e4732SJonas Aaberg 735698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { 7367ce529efSFabio Baltieri int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i; 7377ce529efSFabio Baltieri 7387ce529efSFabio Baltieri if (d40c->base->lcla_pool.alloc_map[idx] == d40d) { 7397ce529efSFabio Baltieri d40c->base->lcla_pool.alloc_map[idx] = NULL; 740698e4732SJonas Aaberg d40d->lcla_alloc--; 741698e4732SJonas Aaberg if (d40d->lcla_alloc == 0) { 742698e4732SJonas Aaberg ret = 0; 743698e4732SJonas Aaberg break; 744698e4732SJonas Aaberg } 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 } 753698e4732SJonas Aaberg 7548d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d) 7558d318a50SLinus Walleij { 7568d318a50SLinus Walleij list_del(&d40d->node); 7578d318a50SLinus Walleij } 7588d318a50SLinus Walleij 7598d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c) 7608d318a50SLinus Walleij { 761a2c15fa4SRabin Vincent struct d40_desc *desc = NULL; 762a2c15fa4SRabin Vincent 763a2c15fa4SRabin Vincent if (!list_empty(&d40c->client)) { 7648d318a50SLinus Walleij struct d40_desc *d; 7658d318a50SLinus Walleij struct d40_desc *_d; 7668d318a50SLinus Walleij 7677fb3e75eSNarayanan G list_for_each_entry_safe(d, _d, &d40c->client, node) { 7688d318a50SLinus Walleij if (async_tx_test_ack(&d->txd)) { 7698d318a50SLinus Walleij d40_desc_remove(d); 770a2c15fa4SRabin Vincent desc = d; 771a2c15fa4SRabin Vincent memset(desc, 0, sizeof(*desc)); 772c675b1b4SJonas Aaberg break; 7738d318a50SLinus Walleij } 7748d318a50SLinus Walleij } 7757fb3e75eSNarayanan G } 776a2c15fa4SRabin Vincent 777a2c15fa4SRabin Vincent if (!desc) 778a2c15fa4SRabin Vincent desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT); 779a2c15fa4SRabin Vincent 780a2c15fa4SRabin Vincent if (desc) 781a2c15fa4SRabin Vincent INIT_LIST_HEAD(&desc->node); 782a2c15fa4SRabin Vincent 783a2c15fa4SRabin Vincent return desc; 7848d318a50SLinus Walleij } 7858d318a50SLinus Walleij 7868d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d) 7878d318a50SLinus Walleij { 788698e4732SJonas Aaberg 789b00f938cSRabin Vincent d40_pool_lli_free(d40c, d40d); 790698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 791c675b1b4SJonas Aaberg kmem_cache_free(d40c->base->desc_slab, d40d); 7928d318a50SLinus Walleij } 7938d318a50SLinus Walleij 7948d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc) 7958d318a50SLinus Walleij { 7968d318a50SLinus Walleij list_add_tail(&desc->node, &d40c->active); 7978d318a50SLinus Walleij } 7988d318a50SLinus Walleij 7991c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc) 8001c4b0927SRabin Vincent { 8011c4b0927SRabin Vincent struct d40_phy_lli *lli_dst = desc->lli_phy.dst; 8021c4b0927SRabin Vincent struct d40_phy_lli *lli_src = desc->lli_phy.src; 8031c4b0927SRabin Vincent void __iomem *base = chan_base(chan); 8041c4b0927SRabin Vincent 8051c4b0927SRabin Vincent writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG); 8061c4b0927SRabin Vincent writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT); 8071c4b0927SRabin Vincent writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR); 8081c4b0927SRabin Vincent writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK); 8091c4b0927SRabin Vincent 8101c4b0927SRabin Vincent writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG); 8111c4b0927SRabin Vincent writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT); 8121c4b0927SRabin Vincent writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR); 8131c4b0927SRabin Vincent writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK); 8141c4b0927SRabin Vincent } 8151c4b0927SRabin Vincent 8164226dd86SFabio Baltieri static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc) 8174226dd86SFabio Baltieri { 8184226dd86SFabio Baltieri list_add_tail(&desc->node, &d40c->done); 8194226dd86SFabio Baltieri } 8204226dd86SFabio Baltieri 821e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc) 822698e4732SJonas Aaberg { 823e65889c7SRabin Vincent struct d40_lcla_pool *pool = &chan->base->lcla_pool; 824e65889c7SRabin Vincent struct d40_log_lli_bidir *lli = &desc->lli_log; 825e65889c7SRabin Vincent int lli_current = desc->lli_current; 826e65889c7SRabin Vincent int lli_len = desc->lli_len; 8270c842b55SRabin Vincent bool cyclic = desc->cyclic; 828e65889c7SRabin Vincent int curr_lcla = -EINVAL; 8290c842b55SRabin Vincent int first_lcla = 0; 83028c7a19dSNarayanan G bool use_esram_lcla = chan->base->plat_data->use_esram_lcla; 8310c842b55SRabin Vincent bool linkback; 832698e4732SJonas Aaberg 8330c842b55SRabin Vincent /* 8340c842b55SRabin Vincent * We may have partially running cyclic transfers, in case we did't get 8350c842b55SRabin Vincent * enough LCLA entries. 8360c842b55SRabin Vincent */ 8370c842b55SRabin Vincent linkback = cyclic && lli_current == 0; 8380c842b55SRabin Vincent 8390c842b55SRabin Vincent /* 8400c842b55SRabin Vincent * For linkback, we need one LCLA even with only one link, because we 8410c842b55SRabin Vincent * can't link back to the one in LCPA space 8420c842b55SRabin Vincent */ 8430c842b55SRabin Vincent if (linkback || (lli_len - lli_current > 1)) { 8447407048bSFabio Baltieri /* 8457407048bSFabio Baltieri * If the channel is expected to use only soft_lli don't 8467407048bSFabio Baltieri * allocate a lcla. This is to avoid a HW issue that exists 8477407048bSFabio Baltieri * in some controller during a peripheral to memory transfer 8487407048bSFabio Baltieri * that uses linked lists. 8497407048bSFabio Baltieri */ 8507407048bSFabio Baltieri if (!(chan->phy_chan->use_soft_lli && 8512c2b62d5SLee Jones chan->dma_cfg.dir == DMA_DEV_TO_MEM)) 852e65889c7SRabin Vincent curr_lcla = d40_lcla_alloc_one(chan, desc); 8537407048bSFabio Baltieri 8540c842b55SRabin Vincent first_lcla = curr_lcla; 8550c842b55SRabin Vincent } 8560c842b55SRabin Vincent 8570c842b55SRabin Vincent /* 8580c842b55SRabin Vincent * For linkback, we normally load the LCPA in the loop since we need to 8590c842b55SRabin Vincent * link it to the second LCLA and not the first. However, if we 8600c842b55SRabin Vincent * couldn't even get a first LCLA, then we have to run in LCPA and 8610c842b55SRabin Vincent * reload manually. 8620c842b55SRabin Vincent */ 8630c842b55SRabin Vincent if (!linkback || curr_lcla == -EINVAL) { 8640c842b55SRabin Vincent unsigned int flags = 0; 8650c842b55SRabin Vincent 8660c842b55SRabin Vincent if (curr_lcla == -EINVAL) 8670c842b55SRabin Vincent flags |= LLI_TERM_INT; 868698e4732SJonas Aaberg 869e65889c7SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa, 870e65889c7SRabin Vincent &lli->dst[lli_current], 871e65889c7SRabin Vincent &lli->src[lli_current], 8720c842b55SRabin Vincent curr_lcla, 8730c842b55SRabin Vincent flags); 874e65889c7SRabin Vincent lli_current++; 8750c842b55SRabin Vincent } 8766045f0bbSRabin Vincent 8776045f0bbSRabin Vincent if (curr_lcla < 0) 8786045f0bbSRabin Vincent goto out; 8796045f0bbSRabin Vincent 880e65889c7SRabin Vincent for (; lli_current < lli_len; lli_current++) { 881e65889c7SRabin Vincent unsigned int lcla_offset = chan->phy_chan->num * 1024 + 882026cbc42SRabin Vincent 8 * curr_lcla * 2; 883026cbc42SRabin Vincent struct d40_log_lli *lcla = pool->base + lcla_offset; 8840c842b55SRabin Vincent unsigned int flags = 0; 885e65889c7SRabin Vincent int next_lcla; 886698e4732SJonas Aaberg 887e65889c7SRabin Vincent if (lli_current + 1 < lli_len) 888e65889c7SRabin Vincent next_lcla = d40_lcla_alloc_one(chan, desc); 889698e4732SJonas Aaberg else 8900c842b55SRabin Vincent next_lcla = linkback ? first_lcla : -EINVAL; 891698e4732SJonas Aaberg 8920c842b55SRabin Vincent if (cyclic || next_lcla == -EINVAL) 8930c842b55SRabin Vincent flags |= LLI_TERM_INT; 8940c842b55SRabin Vincent 8950c842b55SRabin Vincent if (linkback && curr_lcla == first_lcla) { 8960c842b55SRabin Vincent /* First link goes in both LCPA and LCLA */ 8970c842b55SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa, 8980c842b55SRabin Vincent &lli->dst[lli_current], 8990c842b55SRabin Vincent &lli->src[lli_current], 9000c842b55SRabin Vincent next_lcla, flags); 9010c842b55SRabin Vincent } 9020c842b55SRabin Vincent 9030c842b55SRabin Vincent /* 9040c842b55SRabin Vincent * One unused LCLA in the cyclic case if the very first 9050c842b55SRabin Vincent * next_lcla fails... 9060c842b55SRabin Vincent */ 907698e4732SJonas Aaberg d40_log_lli_lcla_write(lcla, 908e65889c7SRabin Vincent &lli->dst[lli_current], 909e65889c7SRabin Vincent &lli->src[lli_current], 9100c842b55SRabin Vincent next_lcla, flags); 911698e4732SJonas Aaberg 91228c7a19dSNarayanan G /* 91328c7a19dSNarayanan G * Cache maintenance is not needed if lcla is 91428c7a19dSNarayanan G * mapped in esram 91528c7a19dSNarayanan G */ 91628c7a19dSNarayanan G if (!use_esram_lcla) { 917e65889c7SRabin Vincent dma_sync_single_range_for_device(chan->base->dev, 918026cbc42SRabin Vincent pool->dma_addr, lcla_offset, 919698e4732SJonas Aaberg 2 * sizeof(struct d40_log_lli), 920698e4732SJonas Aaberg DMA_TO_DEVICE); 92128c7a19dSNarayanan G } 922698e4732SJonas Aaberg curr_lcla = next_lcla; 923698e4732SJonas Aaberg 9240c842b55SRabin Vincent if (curr_lcla == -EINVAL || curr_lcla == first_lcla) { 925e65889c7SRabin Vincent lli_current++; 926698e4732SJonas Aaberg break; 927698e4732SJonas Aaberg } 928e65889c7SRabin Vincent } 929698e4732SJonas Aaberg 9306045f0bbSRabin Vincent out: 931e65889c7SRabin Vincent desc->lli_current = lli_current; 932698e4732SJonas Aaberg } 933e65889c7SRabin Vincent 934e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d) 935e65889c7SRabin Vincent { 936e65889c7SRabin Vincent if (chan_is_physical(d40c)) { 937e65889c7SRabin Vincent d40_phy_lli_load(d40c, d40d); 938e65889c7SRabin Vincent d40d->lli_current = d40d->lli_len; 939e65889c7SRabin Vincent } else 940e65889c7SRabin Vincent d40_log_lli_to_lcxa(d40c, d40d); 941698e4732SJonas Aaberg } 942698e4732SJonas Aaberg 9438d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c) 9448d318a50SLinus Walleij { 9458d318a50SLinus Walleij struct d40_desc *d; 9468d318a50SLinus Walleij 9478d318a50SLinus Walleij if (list_empty(&d40c->active)) 9488d318a50SLinus Walleij return NULL; 9498d318a50SLinus Walleij 9508d318a50SLinus Walleij d = list_first_entry(&d40c->active, 9518d318a50SLinus Walleij struct d40_desc, 9528d318a50SLinus Walleij node); 9538d318a50SLinus Walleij return d; 9548d318a50SLinus Walleij } 9558d318a50SLinus Walleij 9567404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */ 9578d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc) 9588d318a50SLinus Walleij { 9597404368cSPer Forlin d40_desc_remove(desc); 9607404368cSPer Forlin desc->is_in_client_list = false; 961a8f3067bSPer Forlin list_add_tail(&desc->node, &d40c->pending_queue); 962a8f3067bSPer Forlin } 963a8f3067bSPer Forlin 964a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c) 965a8f3067bSPer Forlin { 966a8f3067bSPer Forlin struct d40_desc *d; 967a8f3067bSPer Forlin 968a8f3067bSPer Forlin if (list_empty(&d40c->pending_queue)) 969a8f3067bSPer Forlin return NULL; 970a8f3067bSPer Forlin 971a8f3067bSPer Forlin d = list_first_entry(&d40c->pending_queue, 972a8f3067bSPer Forlin struct d40_desc, 973a8f3067bSPer Forlin node); 974a8f3067bSPer Forlin return d; 9758d318a50SLinus Walleij } 9768d318a50SLinus Walleij 9778d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c) 9788d318a50SLinus Walleij { 9798d318a50SLinus Walleij struct d40_desc *d; 9808d318a50SLinus Walleij 9818d318a50SLinus Walleij if (list_empty(&d40c->queue)) 9828d318a50SLinus Walleij return NULL; 9838d318a50SLinus Walleij 9848d318a50SLinus Walleij d = list_first_entry(&d40c->queue, 9858d318a50SLinus Walleij struct d40_desc, 9868d318a50SLinus Walleij node); 9878d318a50SLinus Walleij return d; 9888d318a50SLinus Walleij } 9898d318a50SLinus Walleij 9904226dd86SFabio Baltieri static struct d40_desc *d40_first_done(struct d40_chan *d40c) 9914226dd86SFabio Baltieri { 9924226dd86SFabio Baltieri if (list_empty(&d40c->done)) 9934226dd86SFabio Baltieri return NULL; 9944226dd86SFabio Baltieri 9954226dd86SFabio Baltieri return list_first_entry(&d40c->done, struct d40_desc, node); 9964226dd86SFabio Baltieri } 9974226dd86SFabio Baltieri 998d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize) 999d49278e3SPer Forlin { 1000d49278e3SPer Forlin if (is_log) { 1001d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_LOG_1) 1002d49278e3SPer Forlin return 1; 1003d49278e3SPer Forlin } else { 1004d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_PHY_1) 1005d49278e3SPer Forlin return 1; 1006d49278e3SPer Forlin } 10078d318a50SLinus Walleij 1008d49278e3SPer Forlin return 2 << psize; 1009d49278e3SPer Forlin } 1010d49278e3SPer Forlin 1011d49278e3SPer Forlin /* 1012d49278e3SPer Forlin * The dma only supports transmitting packages up to 101343f2e1a3SLee Jones * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes. 101443f2e1a3SLee Jones * 101543f2e1a3SLee Jones * Calculate the total number of dma elements required to send the entire sg list. 1016d49278e3SPer Forlin */ 1017d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2) 1018d49278e3SPer Forlin { 1019d49278e3SPer Forlin int dmalen; 1020d49278e3SPer Forlin u32 max_w = max(data_width1, data_width2); 1021d49278e3SPer Forlin u32 min_w = min(data_width1, data_width2); 102243f2e1a3SLee Jones u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w); 1023d49278e3SPer Forlin 1024d49278e3SPer Forlin if (seg_max > STEDMA40_MAX_SEG_SIZE) 102543f2e1a3SLee Jones seg_max -= max_w; 1026d49278e3SPer Forlin 102743f2e1a3SLee Jones if (!IS_ALIGNED(size, max_w)) 1028d49278e3SPer Forlin return -EINVAL; 1029d49278e3SPer Forlin 1030d49278e3SPer Forlin if (size <= seg_max) 1031d49278e3SPer Forlin dmalen = 1; 1032d49278e3SPer Forlin else { 1033d49278e3SPer Forlin dmalen = size / seg_max; 1034d49278e3SPer Forlin if (dmalen * seg_max < size) 1035d49278e3SPer Forlin dmalen++; 1036d49278e3SPer Forlin } 1037d49278e3SPer Forlin return dmalen; 1038d49278e3SPer Forlin } 1039d49278e3SPer Forlin 1040d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len, 1041d49278e3SPer Forlin u32 data_width1, u32 data_width2) 1042d49278e3SPer Forlin { 1043d49278e3SPer Forlin struct scatterlist *sg; 1044d49278e3SPer Forlin int i; 1045d49278e3SPer Forlin int len = 0; 1046d49278e3SPer Forlin int ret; 1047d49278e3SPer Forlin 1048d49278e3SPer Forlin for_each_sg(sgl, sg, sg_len, i) { 1049d49278e3SPer Forlin ret = d40_size_2_dmalen(sg_dma_len(sg), 1050d49278e3SPer Forlin data_width1, data_width2); 1051d49278e3SPer Forlin if (ret < 0) 1052d49278e3SPer Forlin return ret; 1053d49278e3SPer Forlin len += ret; 1054d49278e3SPer Forlin } 1055d49278e3SPer Forlin return len; 1056d49278e3SPer Forlin } 1057d49278e3SPer Forlin 10587fb3e75eSNarayanan G 10597fb3e75eSNarayanan G #ifdef CONFIG_PM 10607fb3e75eSNarayanan G static void dma40_backup(void __iomem *baseaddr, u32 *backup, 10617fb3e75eSNarayanan G u32 *regaddr, int num, bool save) 10627fb3e75eSNarayanan G { 10637fb3e75eSNarayanan G int i; 10647fb3e75eSNarayanan G 10657fb3e75eSNarayanan G for (i = 0; i < num; i++) { 10667fb3e75eSNarayanan G void __iomem *addr = baseaddr + regaddr[i]; 10677fb3e75eSNarayanan G 10687fb3e75eSNarayanan G if (save) 10697fb3e75eSNarayanan G backup[i] = readl_relaxed(addr); 10707fb3e75eSNarayanan G else 10717fb3e75eSNarayanan G writel_relaxed(backup[i], addr); 10727fb3e75eSNarayanan G } 10737fb3e75eSNarayanan G } 10747fb3e75eSNarayanan G 10757fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save) 10767fb3e75eSNarayanan G { 10777fb3e75eSNarayanan G int i; 10787fb3e75eSNarayanan G 10797fb3e75eSNarayanan G /* Save/Restore channel specific registers */ 10807fb3e75eSNarayanan G for (i = 0; i < base->num_phy_chans; i++) { 10817fb3e75eSNarayanan G void __iomem *addr; 10827fb3e75eSNarayanan G int idx; 10837fb3e75eSNarayanan G 10847fb3e75eSNarayanan G if (base->phy_res[i].reserved) 10857fb3e75eSNarayanan G continue; 10867fb3e75eSNarayanan G 10877fb3e75eSNarayanan G addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA; 10887fb3e75eSNarayanan G idx = i * ARRAY_SIZE(d40_backup_regs_chan); 10897fb3e75eSNarayanan G 10907fb3e75eSNarayanan G dma40_backup(addr, &base->reg_val_backup_chan[idx], 10917fb3e75eSNarayanan G d40_backup_regs_chan, 10927fb3e75eSNarayanan G ARRAY_SIZE(d40_backup_regs_chan), 10937fb3e75eSNarayanan G save); 10947fb3e75eSNarayanan G } 10957fb3e75eSNarayanan G 10967fb3e75eSNarayanan G /* Save/Restore global registers */ 10977fb3e75eSNarayanan G dma40_backup(base->virtbase, base->reg_val_backup, 10987fb3e75eSNarayanan G d40_backup_regs, ARRAY_SIZE(d40_backup_regs), 10997fb3e75eSNarayanan G save); 11007fb3e75eSNarayanan G 11017fb3e75eSNarayanan G /* Save/Restore registers only existing on dma40 v3 and later */ 11023cb645dcSTong Liu if (base->gen_dmac.backup) 11033cb645dcSTong Liu dma40_backup(base->virtbase, base->reg_val_backup_v4, 11043cb645dcSTong Liu base->gen_dmac.backup, 11053cb645dcSTong Liu base->gen_dmac.backup_size, 11067fb3e75eSNarayanan G save); 11077fb3e75eSNarayanan G } 11087fb3e75eSNarayanan G #else 11097fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save) 11107fb3e75eSNarayanan G { 11117fb3e75eSNarayanan G } 11127fb3e75eSNarayanan G #endif 11138d318a50SLinus Walleij 11141bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c, 11158d318a50SLinus Walleij enum d40_command command) 11168d318a50SLinus Walleij { 1117767a9675SJonas Aaberg u32 status; 1118767a9675SJonas Aaberg int i; 11198d318a50SLinus Walleij void __iomem *active_reg; 11208d318a50SLinus Walleij int ret = 0; 11218d318a50SLinus Walleij unsigned long flags; 11221d392a7bSJonas Aaberg u32 wmask; 11238d318a50SLinus Walleij 11241bdae6f4SNarayanan G if (command == D40_DMA_STOP) { 11251bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ); 11261bdae6f4SNarayanan G if (ret) 11271bdae6f4SNarayanan G return ret; 11281bdae6f4SNarayanan G } 11291bdae6f4SNarayanan G 11308d318a50SLinus Walleij spin_lock_irqsave(&d40c->base->execmd_lock, flags); 11318d318a50SLinus Walleij 11328d318a50SLinus Walleij if (d40c->phy_chan->num % 2 == 0) 11338d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 11348d318a50SLinus Walleij else 11358d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 11368d318a50SLinus Walleij 11378d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) { 11388d318a50SLinus Walleij status = (readl(active_reg) & 11398d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 11408d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 11418d318a50SLinus Walleij 11428d318a50SLinus Walleij if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) 11438d318a50SLinus Walleij goto done; 11448d318a50SLinus Walleij } 11458d318a50SLinus Walleij 11461d392a7bSJonas Aaberg wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num)); 11471d392a7bSJonas Aaberg writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)), 11481d392a7bSJonas Aaberg active_reg); 11498d318a50SLinus Walleij 11508d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) { 11518d318a50SLinus Walleij 11528d318a50SLinus Walleij for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) { 11538d318a50SLinus Walleij status = (readl(active_reg) & 11548d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 11558d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 11568d318a50SLinus Walleij 11578d318a50SLinus Walleij cpu_relax(); 11588d318a50SLinus Walleij /* 11598d318a50SLinus Walleij * Reduce the number of bus accesses while 11608d318a50SLinus Walleij * waiting for the DMA to suspend. 11618d318a50SLinus Walleij */ 11628d318a50SLinus Walleij udelay(3); 11638d318a50SLinus Walleij 11648d318a50SLinus Walleij if (status == D40_DMA_STOP || 11658d318a50SLinus Walleij status == D40_DMA_SUSPENDED) 11668d318a50SLinus Walleij break; 11678d318a50SLinus Walleij } 11688d318a50SLinus Walleij 11698d318a50SLinus Walleij if (i == D40_SUSPEND_MAX_IT) { 11706db5a8baSRabin Vincent chan_err(d40c, 11716db5a8baSRabin Vincent "unable to suspend the chl %d (log: %d) status %x\n", 11726db5a8baSRabin Vincent d40c->phy_chan->num, d40c->log_num, 11738d318a50SLinus Walleij status); 11748d318a50SLinus Walleij dump_stack(); 11758d318a50SLinus Walleij ret = -EBUSY; 11768d318a50SLinus Walleij } 11778d318a50SLinus Walleij 11788d318a50SLinus Walleij } 11798d318a50SLinus Walleij done: 11808d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->base->execmd_lock, flags); 11818d318a50SLinus Walleij return ret; 11828d318a50SLinus Walleij } 11838d318a50SLinus Walleij 11848d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c) 11858d318a50SLinus Walleij { 11868d318a50SLinus Walleij struct d40_desc *d40d; 11877404368cSPer Forlin struct d40_desc *_d; 11888d318a50SLinus Walleij 11894226dd86SFabio Baltieri /* Release completed descriptors */ 11904226dd86SFabio Baltieri while ((d40d = d40_first_done(d40c))) { 11914226dd86SFabio Baltieri d40_desc_remove(d40d); 11924226dd86SFabio Baltieri d40_desc_free(d40c, d40d); 11934226dd86SFabio Baltieri } 11944226dd86SFabio Baltieri 11958d318a50SLinus Walleij /* Release active descriptors */ 11968d318a50SLinus Walleij while ((d40d = d40_first_active_get(d40c))) { 11978d318a50SLinus Walleij d40_desc_remove(d40d); 11988d318a50SLinus Walleij d40_desc_free(d40c, d40d); 11998d318a50SLinus Walleij } 12008d318a50SLinus Walleij 12018d318a50SLinus Walleij /* Release queued descriptors waiting for transfer */ 12028d318a50SLinus Walleij while ((d40d = d40_first_queued(d40c))) { 12038d318a50SLinus Walleij d40_desc_remove(d40d); 12048d318a50SLinus Walleij d40_desc_free(d40c, d40d); 12058d318a50SLinus Walleij } 12068d318a50SLinus Walleij 1207a8f3067bSPer Forlin /* Release pending descriptors */ 1208a8f3067bSPer Forlin while ((d40d = d40_first_pending(d40c))) { 1209a8f3067bSPer Forlin d40_desc_remove(d40d); 1210a8f3067bSPer Forlin d40_desc_free(d40c, d40d); 1211a8f3067bSPer Forlin } 12128d318a50SLinus Walleij 12137404368cSPer Forlin /* Release client owned descriptors */ 12147404368cSPer Forlin if (!list_empty(&d40c->client)) 12157404368cSPer Forlin list_for_each_entry_safe(d40d, _d, &d40c->client, node) { 12167404368cSPer Forlin d40_desc_remove(d40d); 12177404368cSPer Forlin d40_desc_free(d40c, d40d); 12187404368cSPer Forlin } 12197404368cSPer Forlin 122082babbb3SPer Forlin /* Release descriptors in prepare queue */ 122182babbb3SPer Forlin if (!list_empty(&d40c->prepare_queue)) 122282babbb3SPer Forlin list_for_each_entry_safe(d40d, _d, 122382babbb3SPer Forlin &d40c->prepare_queue, node) { 122482babbb3SPer Forlin d40_desc_remove(d40d); 122582babbb3SPer Forlin d40_desc_free(d40c, d40d); 122682babbb3SPer Forlin } 12277404368cSPer Forlin 12288d318a50SLinus Walleij d40c->pending_tx = 0; 12298d318a50SLinus Walleij } 12308d318a50SLinus Walleij 12311bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c, 12321bdae6f4SNarayanan G enum d40_events event_type, u32 event, 12331bdae6f4SNarayanan G int reg) 1234262d2915SRabin Vincent { 12358ca84687SRabin Vincent void __iomem *addr = chan_base(d40c) + reg; 1236262d2915SRabin Vincent int tries; 12371bdae6f4SNarayanan G u32 status; 1238262d2915SRabin Vincent 12391bdae6f4SNarayanan G switch (event_type) { 12401bdae6f4SNarayanan G 12411bdae6f4SNarayanan G case D40_DEACTIVATE_EVENTLINE: 12421bdae6f4SNarayanan G 1243262d2915SRabin Vincent writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) 1244262d2915SRabin Vincent | ~D40_EVENTLINE_MASK(event), addr); 12451bdae6f4SNarayanan G break; 12461bdae6f4SNarayanan G 12471bdae6f4SNarayanan G case D40_SUSPEND_REQ_EVENTLINE: 12481bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >> 12491bdae6f4SNarayanan G D40_EVENTLINE_POS(event); 12501bdae6f4SNarayanan G 12511bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE || 12521bdae6f4SNarayanan G status == D40_SUSPEND_REQ_EVENTLINE) 12531bdae6f4SNarayanan G break; 12541bdae6f4SNarayanan G 12551bdae6f4SNarayanan G writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event)) 12561bdae6f4SNarayanan G | ~D40_EVENTLINE_MASK(event), addr); 12571bdae6f4SNarayanan G 12581bdae6f4SNarayanan G for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) { 12591bdae6f4SNarayanan G 12601bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >> 12611bdae6f4SNarayanan G D40_EVENTLINE_POS(event); 12621bdae6f4SNarayanan G 12631bdae6f4SNarayanan G cpu_relax(); 12641bdae6f4SNarayanan G /* 12651bdae6f4SNarayanan G * Reduce the number of bus accesses while 12661bdae6f4SNarayanan G * waiting for the DMA to suspend. 12671bdae6f4SNarayanan G */ 12681bdae6f4SNarayanan G udelay(3); 12691bdae6f4SNarayanan G 12701bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE) 12711bdae6f4SNarayanan G break; 1272262d2915SRabin Vincent } 1273262d2915SRabin Vincent 12741bdae6f4SNarayanan G if (tries == D40_SUSPEND_MAX_IT) { 12751bdae6f4SNarayanan G chan_err(d40c, 12761bdae6f4SNarayanan G "unable to stop the event_line chl %d (log: %d)" 12771bdae6f4SNarayanan G "status %x\n", d40c->phy_chan->num, 12781bdae6f4SNarayanan G d40c->log_num, status); 12791bdae6f4SNarayanan G } 12801bdae6f4SNarayanan G break; 12811bdae6f4SNarayanan G 12821bdae6f4SNarayanan G case D40_ACTIVATE_EVENTLINE: 1283262d2915SRabin Vincent /* 1284262d2915SRabin Vincent * The hardware sometimes doesn't register the enable when src and dst 1285262d2915SRabin Vincent * event lines are active on the same logical channel. Retry to ensure 1286262d2915SRabin Vincent * it does. Usually only one retry is sufficient. 1287262d2915SRabin Vincent */ 1288262d2915SRabin Vincent tries = 100; 1289262d2915SRabin Vincent while (--tries) { 12901bdae6f4SNarayanan G writel((D40_ACTIVATE_EVENTLINE << 12911bdae6f4SNarayanan G D40_EVENTLINE_POS(event)) | 12921bdae6f4SNarayanan G ~D40_EVENTLINE_MASK(event), addr); 1293262d2915SRabin Vincent 1294262d2915SRabin Vincent if (readl(addr) & D40_EVENTLINE_MASK(event)) 1295262d2915SRabin Vincent break; 1296262d2915SRabin Vincent } 1297262d2915SRabin Vincent 1298262d2915SRabin Vincent if (tries != 99) 1299262d2915SRabin Vincent dev_dbg(chan2dev(d40c), 1300262d2915SRabin Vincent "[%s] workaround enable S%cLNK (%d tries)\n", 1301262d2915SRabin Vincent __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D', 1302262d2915SRabin Vincent 100 - tries); 1303262d2915SRabin Vincent 1304262d2915SRabin Vincent WARN_ON(!tries); 13051bdae6f4SNarayanan G break; 13061bdae6f4SNarayanan G 13071bdae6f4SNarayanan G case D40_ROUND_EVENTLINE: 13081bdae6f4SNarayanan G BUG(); 13091bdae6f4SNarayanan G break; 13101bdae6f4SNarayanan G 13111bdae6f4SNarayanan G } 1312262d2915SRabin Vincent } 1313262d2915SRabin Vincent 13141bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c, 13151bdae6f4SNarayanan G enum d40_events event_type) 13168d318a50SLinus Walleij { 131726955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type); 131826955c07SLee Jones 13198d318a50SLinus Walleij /* Enable event line connected to device (or memcpy) */ 13202c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) || 13212c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV)) 13221bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event, 13238d318a50SLinus Walleij D40_CHAN_REG_SSLNK); 1324262d2915SRabin Vincent 13252c2b62d5SLee Jones if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM) 13261bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event, 13278d318a50SLinus Walleij D40_CHAN_REG_SDLNK); 13288d318a50SLinus Walleij } 13298d318a50SLinus Walleij 1330a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c) 13318d318a50SLinus Walleij { 13328ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 1333be8cb7dfSJonas Aaberg u32 val; 13348d318a50SLinus Walleij 13358ca84687SRabin Vincent val = readl(chanbase + D40_CHAN_REG_SSLNK); 13368ca84687SRabin Vincent val |= readl(chanbase + D40_CHAN_REG_SDLNK); 13378d318a50SLinus Walleij 1338a5ebca47SJonas Aaberg return val; 13398d318a50SLinus Walleij } 13408d318a50SLinus Walleij 13411bdae6f4SNarayanan G static int 13421bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command) 13431bdae6f4SNarayanan G { 13441bdae6f4SNarayanan G unsigned long flags; 13451bdae6f4SNarayanan G int ret = 0; 13461bdae6f4SNarayanan G u32 active_status; 13471bdae6f4SNarayanan G void __iomem *active_reg; 13481bdae6f4SNarayanan G 13491bdae6f4SNarayanan G if (d40c->phy_chan->num % 2 == 0) 13501bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 13511bdae6f4SNarayanan G else 13521bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 13531bdae6f4SNarayanan G 13541bdae6f4SNarayanan G 13551bdae6f4SNarayanan G spin_lock_irqsave(&d40c->phy_chan->lock, flags); 13561bdae6f4SNarayanan G 13571bdae6f4SNarayanan G switch (command) { 13581bdae6f4SNarayanan G case D40_DMA_STOP: 13591bdae6f4SNarayanan G case D40_DMA_SUSPEND_REQ: 13601bdae6f4SNarayanan G 13611bdae6f4SNarayanan G active_status = (readl(active_reg) & 13621bdae6f4SNarayanan G D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 13631bdae6f4SNarayanan G D40_CHAN_POS(d40c->phy_chan->num); 13641bdae6f4SNarayanan G 13651bdae6f4SNarayanan G if (active_status == D40_DMA_RUN) 13661bdae6f4SNarayanan G d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE); 13671bdae6f4SNarayanan G else 13681bdae6f4SNarayanan G d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE); 13691bdae6f4SNarayanan G 13701bdae6f4SNarayanan G if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP)) 13711bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command); 13721bdae6f4SNarayanan G 13731bdae6f4SNarayanan G break; 13741bdae6f4SNarayanan G 13751bdae6f4SNarayanan G case D40_DMA_RUN: 13761bdae6f4SNarayanan G 13771bdae6f4SNarayanan G d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE); 13781bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command); 13791bdae6f4SNarayanan G break; 13801bdae6f4SNarayanan G 13811bdae6f4SNarayanan G case D40_DMA_SUSPENDED: 13821bdae6f4SNarayanan G BUG(); 13831bdae6f4SNarayanan G break; 13841bdae6f4SNarayanan G } 13851bdae6f4SNarayanan G 13861bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->phy_chan->lock, flags); 13871bdae6f4SNarayanan G return ret; 13881bdae6f4SNarayanan G } 13891bdae6f4SNarayanan G 13901bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c, 13911bdae6f4SNarayanan G enum d40_command command) 13921bdae6f4SNarayanan G { 13931bdae6f4SNarayanan G if (chan_is_logical(d40c)) 13941bdae6f4SNarayanan G return __d40_execute_command_log(d40c, command); 13951bdae6f4SNarayanan G else 13961bdae6f4SNarayanan G return __d40_execute_command_phy(d40c, command); 13971bdae6f4SNarayanan G } 13981bdae6f4SNarayanan G 139920a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c) 140020a5b6d0SRabin Vincent { 140120a5b6d0SRabin Vincent static const unsigned int phy_map[] = { 140220a5b6d0SRabin Vincent [STEDMA40_PCHAN_BASIC_MODE] 140320a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_BASIC, 140420a5b6d0SRabin Vincent [STEDMA40_PCHAN_MODULO_MODE] 140520a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_MODULO, 140620a5b6d0SRabin Vincent [STEDMA40_PCHAN_DOUBLE_DST_MODE] 140720a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_DOUBLE_DST, 140820a5b6d0SRabin Vincent }; 140920a5b6d0SRabin Vincent static const unsigned int log_map[] = { 141020a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_PHY_DST_LOG] 141120a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG, 141220a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_PHY] 141320a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY, 141420a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_LOG] 141520a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG, 141620a5b6d0SRabin Vincent }; 141720a5b6d0SRabin Vincent 1418724a8577SRabin Vincent if (chan_is_physical(d40c)) 141920a5b6d0SRabin Vincent return phy_map[d40c->dma_cfg.mode_opt]; 142020a5b6d0SRabin Vincent else 142120a5b6d0SRabin Vincent return log_map[d40c->dma_cfg.mode_opt]; 142220a5b6d0SRabin Vincent } 142320a5b6d0SRabin Vincent 1424b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c) 14258d318a50SLinus Walleij { 14268d318a50SLinus Walleij u32 addr_base; 14278d318a50SLinus Walleij u32 var; 14288d318a50SLinus Walleij 14298d318a50SLinus Walleij /* Odd addresses are even addresses + 4 */ 14308d318a50SLinus Walleij addr_base = (d40c->phy_chan->num % 2) * 4; 14318d318a50SLinus Walleij /* Setup channel mode to logical or physical */ 1432724a8577SRabin Vincent var = ((u32)(chan_is_logical(d40c)) + 1) << 14338d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 14348d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base); 14358d318a50SLinus Walleij 14368d318a50SLinus Walleij /* Setup operational mode option register */ 143720a5b6d0SRabin Vincent var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num); 14388d318a50SLinus Walleij 14398d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base); 14408d318a50SLinus Walleij 1441724a8577SRabin Vincent if (chan_is_logical(d40c)) { 14428ca84687SRabin Vincent int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) 14438ca84687SRabin Vincent & D40_SREG_ELEM_LOG_LIDX_MASK; 14448ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 14458ca84687SRabin Vincent 14468d318a50SLinus Walleij /* Set default config for CFG reg */ 14478ca84687SRabin Vincent writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG); 14488ca84687SRabin Vincent writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG); 14498d318a50SLinus Walleij 1450b55912c6SJonas Aaberg /* Set LIDX for lcla */ 14518ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SSELT); 14528ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SDELT); 1453e9f3a49cSRabin Vincent 1454e9f3a49cSRabin Vincent /* Clear LNK which will be used by d40_chan_has_events() */ 1455e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SSLNK); 1456e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SDLNK); 14578d318a50SLinus Walleij } 14588d318a50SLinus Walleij } 14598d318a50SLinus Walleij 1460aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c) 1461aa182ae2SJonas Aaberg { 1462aa182ae2SJonas Aaberg u32 num_elt; 1463aa182ae2SJonas Aaberg 1464724a8577SRabin Vincent if (chan_is_logical(d40c)) 1465aa182ae2SJonas Aaberg num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK) 1466aa182ae2SJonas Aaberg >> D40_MEM_LCSP2_ECNT_POS; 14678ca84687SRabin Vincent else { 14688ca84687SRabin Vincent u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT); 14698ca84687SRabin Vincent num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK) 14708ca84687SRabin Vincent >> D40_SREG_ELEM_PHY_ECNT_POS; 14718ca84687SRabin Vincent } 14728ca84687SRabin Vincent 147343f2e1a3SLee Jones return num_elt * d40c->dma_cfg.dst_info.data_width; 1474aa182ae2SJonas Aaberg } 1475aa182ae2SJonas Aaberg 1476aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c) 1477aa182ae2SJonas Aaberg { 1478aa182ae2SJonas Aaberg bool is_link; 1479aa182ae2SJonas Aaberg 1480724a8577SRabin Vincent if (chan_is_logical(d40c)) 1481aa182ae2SJonas Aaberg is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK; 1482aa182ae2SJonas Aaberg else 14838ca84687SRabin Vincent is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK) 14848ca84687SRabin Vincent & D40_SREG_LNK_PHYS_LNK_MASK; 14858ca84687SRabin Vincent 1486aa182ae2SJonas Aaberg return is_link; 1487aa182ae2SJonas Aaberg } 1488aa182ae2SJonas Aaberg 148986eb5fb6SRabin Vincent static int d40_pause(struct d40_chan *d40c) 1490aa182ae2SJonas Aaberg { 1491aa182ae2SJonas Aaberg int res = 0; 1492aa182ae2SJonas Aaberg unsigned long flags; 1493aa182ae2SJonas Aaberg 14943ac012afSJonas Aaberg if (!d40c->busy) 14953ac012afSJonas Aaberg return 0; 14963ac012afSJonas Aaberg 14977fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 1498aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 1499aa182ae2SJonas Aaberg 1500aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); 15011bdae6f4SNarayanan G 15027fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 15037fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 1504aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 1505aa182ae2SJonas Aaberg return res; 1506aa182ae2SJonas Aaberg } 1507aa182ae2SJonas Aaberg 150886eb5fb6SRabin Vincent static int d40_resume(struct d40_chan *d40c) 1509aa182ae2SJonas Aaberg { 1510aa182ae2SJonas Aaberg int res = 0; 1511aa182ae2SJonas Aaberg unsigned long flags; 1512aa182ae2SJonas Aaberg 15133ac012afSJonas Aaberg if (!d40c->busy) 15143ac012afSJonas Aaberg return 0; 15153ac012afSJonas Aaberg 1516aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 15177fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 1518aa182ae2SJonas Aaberg 1519aa182ae2SJonas Aaberg /* If bytes left to transfer or linked tx resume job */ 15201bdae6f4SNarayanan G if (d40_residue(d40c) || d40_tx_is_linked(d40c)) 1521aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_RUN); 1522aa182ae2SJonas Aaberg 15237fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 15247fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 1525aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 1526aa182ae2SJonas Aaberg return res; 1527aa182ae2SJonas Aaberg } 1528aa182ae2SJonas Aaberg 15298d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx) 15308d318a50SLinus Walleij { 15318d318a50SLinus Walleij struct d40_chan *d40c = container_of(tx->chan, 15328d318a50SLinus Walleij struct d40_chan, 15338d318a50SLinus Walleij chan); 15348d318a50SLinus Walleij struct d40_desc *d40d = container_of(tx, struct d40_desc, txd); 15358d318a50SLinus Walleij unsigned long flags; 1536884485e1SRussell King - ARM Linux dma_cookie_t cookie; 15378d318a50SLinus Walleij 15388d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 1539884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 15408d318a50SLinus Walleij d40_desc_queue(d40c, d40d); 15418d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 15428d318a50SLinus Walleij 1543884485e1SRussell King - ARM Linux return cookie; 15448d318a50SLinus Walleij } 15458d318a50SLinus Walleij 15468d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c) 15478d318a50SLinus Walleij { 15480c32269dSJonas Aaberg return d40_channel_execute_command(d40c, D40_DMA_RUN); 15498d318a50SLinus Walleij } 15508d318a50SLinus Walleij 15518d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c) 15528d318a50SLinus Walleij { 15538d318a50SLinus Walleij struct d40_desc *d40d; 15548d318a50SLinus Walleij int err; 15558d318a50SLinus Walleij 15568d318a50SLinus Walleij /* Start queued jobs, if any */ 15578d318a50SLinus Walleij d40d = d40_first_queued(d40c); 15588d318a50SLinus Walleij 15598d318a50SLinus Walleij if (d40d != NULL) { 15601bdae6f4SNarayanan G if (!d40c->busy) { 15618d318a50SLinus Walleij d40c->busy = true; 15627fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 15631bdae6f4SNarayanan G } 15647fb3e75eSNarayanan G 15658d318a50SLinus Walleij /* Remove from queue */ 15668d318a50SLinus Walleij d40_desc_remove(d40d); 15678d318a50SLinus Walleij 15688d318a50SLinus Walleij /* Add to active queue */ 15698d318a50SLinus Walleij d40_desc_submit(d40c, d40d); 15708d318a50SLinus Walleij 15718d318a50SLinus Walleij /* Initiate DMA job */ 15728d318a50SLinus Walleij d40_desc_load(d40c, d40d); 15738d318a50SLinus Walleij 15748d318a50SLinus Walleij /* Start dma job */ 15758d318a50SLinus Walleij err = d40_start(d40c); 15768d318a50SLinus Walleij 15778d318a50SLinus Walleij if (err) 15788d318a50SLinus Walleij return NULL; 15798d318a50SLinus Walleij } 15808d318a50SLinus Walleij 15818d318a50SLinus Walleij return d40d; 15828d318a50SLinus Walleij } 15838d318a50SLinus Walleij 15848d318a50SLinus Walleij /* called from interrupt context */ 15858d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c) 15868d318a50SLinus Walleij { 15878d318a50SLinus Walleij struct d40_desc *d40d; 15888d318a50SLinus Walleij 15898d318a50SLinus Walleij /* Get first active entry from list */ 15908d318a50SLinus Walleij d40d = d40_first_active_get(d40c); 15918d318a50SLinus Walleij 15928d318a50SLinus Walleij if (d40d == NULL) 15938d318a50SLinus Walleij return; 15948d318a50SLinus Walleij 15950c842b55SRabin Vincent if (d40d->cyclic) { 15960c842b55SRabin Vincent /* 15970c842b55SRabin Vincent * If this was a paritially loaded list, we need to reloaded 15980c842b55SRabin Vincent * it, and only when the list is completed. We need to check 15990c842b55SRabin Vincent * for done because the interrupt will hit for every link, and 16000c842b55SRabin Vincent * not just the last one. 16010c842b55SRabin Vincent */ 16020c842b55SRabin Vincent if (d40d->lli_current < d40d->lli_len 16030c842b55SRabin Vincent && !d40_tx_is_linked(d40c) 16040c842b55SRabin Vincent && !d40_residue(d40c)) { 16050c842b55SRabin Vincent d40_lcla_free_all(d40c, d40d); 16060c842b55SRabin Vincent d40_desc_load(d40c, d40d); 16070c842b55SRabin Vincent (void) d40_start(d40c); 16080c842b55SRabin Vincent 16090c842b55SRabin Vincent if (d40d->lli_current == d40d->lli_len) 16100c842b55SRabin Vincent d40d->lli_current = 0; 16110c842b55SRabin Vincent } 16120c842b55SRabin Vincent } else { 1613698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 16148d318a50SLinus Walleij 1615698e4732SJonas Aaberg if (d40d->lli_current < d40d->lli_len) { 16168d318a50SLinus Walleij d40_desc_load(d40c, d40d); 16178d318a50SLinus Walleij /* Start dma job */ 16188d318a50SLinus Walleij (void) d40_start(d40c); 16198d318a50SLinus Walleij return; 16208d318a50SLinus Walleij } 16218d318a50SLinus Walleij 16228d318a50SLinus Walleij if (d40_queue_start(d40c) == NULL) 16238d318a50SLinus Walleij d40c->busy = false; 16247fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 16257fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 16268d318a50SLinus Walleij 16274226dd86SFabio Baltieri d40_desc_remove(d40d); 16284226dd86SFabio Baltieri d40_desc_done(d40c, d40d); 16297dd14525SFabio Baltieri } 16304226dd86SFabio Baltieri 16318d318a50SLinus Walleij d40c->pending_tx++; 16328d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet); 16338d318a50SLinus Walleij 16348d318a50SLinus Walleij } 16358d318a50SLinus Walleij 16368d318a50SLinus Walleij static void dma_tasklet(unsigned long data) 16378d318a50SLinus Walleij { 16388d318a50SLinus Walleij struct d40_chan *d40c = (struct d40_chan *) data; 1639767a9675SJonas Aaberg struct d40_desc *d40d; 16408d318a50SLinus Walleij unsigned long flags; 16418d318a50SLinus Walleij dma_async_tx_callback callback; 16428d318a50SLinus Walleij void *callback_param; 16438d318a50SLinus Walleij 16448d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 16458d318a50SLinus Walleij 16464226dd86SFabio Baltieri /* Get first entry from the done list */ 16474226dd86SFabio Baltieri d40d = d40_first_done(d40c); 16484226dd86SFabio Baltieri if (d40d == NULL) { 16494226dd86SFabio Baltieri /* Check if we have reached here for cyclic job */ 1650767a9675SJonas Aaberg d40d = d40_first_active_get(d40c); 16514226dd86SFabio Baltieri if (d40d == NULL || !d40d->cyclic) 16528d318a50SLinus Walleij goto err; 16534226dd86SFabio Baltieri } 16548d318a50SLinus Walleij 16550c842b55SRabin Vincent if (!d40d->cyclic) 1656f7fbce07SRussell King - ARM Linux dma_cookie_complete(&d40d->txd); 16578d318a50SLinus Walleij 16588d318a50SLinus Walleij /* 16598d318a50SLinus Walleij * If terminating a channel pending_tx is set to zero. 16608d318a50SLinus Walleij * This prevents any finished active jobs to return to the client. 16618d318a50SLinus Walleij */ 16628d318a50SLinus Walleij if (d40c->pending_tx == 0) { 16638d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 16648d318a50SLinus Walleij return; 16658d318a50SLinus Walleij } 16668d318a50SLinus Walleij 16678d318a50SLinus Walleij /* Callback to client */ 1668767a9675SJonas Aaberg callback = d40d->txd.callback; 1669767a9675SJonas Aaberg callback_param = d40d->txd.callback_param; 16708d318a50SLinus Walleij 16710c842b55SRabin Vincent if (!d40d->cyclic) { 1672767a9675SJonas Aaberg if (async_tx_test_ack(&d40d->txd)) { 1673767a9675SJonas Aaberg d40_desc_remove(d40d); 1674767a9675SJonas Aaberg d40_desc_free(d40c, d40d); 1675f26e03adSFabio Baltieri } else if (!d40d->is_in_client_list) { 1676767a9675SJonas Aaberg d40_desc_remove(d40d); 1677698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 1678767a9675SJonas Aaberg list_add_tail(&d40d->node, &d40c->client); 1679767a9675SJonas Aaberg d40d->is_in_client_list = true; 16808d318a50SLinus Walleij } 16818d318a50SLinus Walleij } 16828d318a50SLinus Walleij 16838d318a50SLinus Walleij d40c->pending_tx--; 16848d318a50SLinus Walleij 16858d318a50SLinus Walleij if (d40c->pending_tx) 16868d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet); 16878d318a50SLinus Walleij 16888d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 16898d318a50SLinus Walleij 1690767a9675SJonas Aaberg if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT)) 16918d318a50SLinus Walleij callback(callback_param); 16928d318a50SLinus Walleij 16938d318a50SLinus Walleij return; 16948d318a50SLinus Walleij 16958d318a50SLinus Walleij err: 16961bdae6f4SNarayanan G /* Rescue manouver if receiving double interrupts */ 16978d318a50SLinus Walleij if (d40c->pending_tx > 0) 16988d318a50SLinus Walleij d40c->pending_tx--; 16998d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 17008d318a50SLinus Walleij } 17018d318a50SLinus Walleij 17028d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data) 17038d318a50SLinus Walleij { 17048d318a50SLinus Walleij int i; 17058d318a50SLinus Walleij u32 idx; 17068d318a50SLinus Walleij u32 row; 17078d318a50SLinus Walleij long chan = -1; 17088d318a50SLinus Walleij struct d40_chan *d40c; 17098d318a50SLinus Walleij unsigned long flags; 17108d318a50SLinus Walleij struct d40_base *base = data; 17113cb645dcSTong Liu u32 regs[base->gen_dmac.il_size]; 17123cb645dcSTong Liu struct d40_interrupt_lookup *il = base->gen_dmac.il; 17133cb645dcSTong Liu u32 il_size = base->gen_dmac.il_size; 17148d318a50SLinus Walleij 17158d318a50SLinus Walleij spin_lock_irqsave(&base->interrupt_lock, flags); 17168d318a50SLinus Walleij 17178d318a50SLinus Walleij /* Read interrupt status of both logical and physical channels */ 17183cb645dcSTong Liu for (i = 0; i < il_size; i++) 17198d318a50SLinus Walleij regs[i] = readl(base->virtbase + il[i].src); 17208d318a50SLinus Walleij 17218d318a50SLinus Walleij for (;;) { 17228d318a50SLinus Walleij 17238d318a50SLinus Walleij chan = find_next_bit((unsigned long *)regs, 17243cb645dcSTong Liu BITS_PER_LONG * il_size, chan + 1); 17258d318a50SLinus Walleij 17268d318a50SLinus Walleij /* No more set bits found? */ 17273cb645dcSTong Liu if (chan == BITS_PER_LONG * il_size) 17288d318a50SLinus Walleij break; 17298d318a50SLinus Walleij 17308d318a50SLinus Walleij row = chan / BITS_PER_LONG; 17318d318a50SLinus Walleij idx = chan & (BITS_PER_LONG - 1); 17328d318a50SLinus Walleij 17338d318a50SLinus Walleij if (il[row].offset == D40_PHY_CHAN) 17348d318a50SLinus Walleij d40c = base->lookup_phy_chans[idx]; 17358d318a50SLinus Walleij else 17368d318a50SLinus Walleij d40c = base->lookup_log_chans[il[row].offset + idx]; 173753d6d68fSFabio Baltieri 173853d6d68fSFabio Baltieri if (!d40c) { 173953d6d68fSFabio Baltieri /* 174053d6d68fSFabio Baltieri * No error because this can happen if something else 174153d6d68fSFabio Baltieri * in the system is using the channel. 174253d6d68fSFabio Baltieri */ 174353d6d68fSFabio Baltieri continue; 174453d6d68fSFabio Baltieri } 174553d6d68fSFabio Baltieri 174653d6d68fSFabio Baltieri /* ACK interrupt */ 17478a3b6e14SLee Jones writel(BIT(idx), base->virtbase + il[row].clr); 174853d6d68fSFabio Baltieri 17498d318a50SLinus Walleij spin_lock(&d40c->lock); 17508d318a50SLinus Walleij 17518d318a50SLinus Walleij if (!il[row].is_error) 17528d318a50SLinus Walleij dma_tc_handle(d40c); 17538d318a50SLinus Walleij else 17546db5a8baSRabin Vincent d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n", 17556db5a8baSRabin Vincent chan, il[row].offset, idx); 17568d318a50SLinus Walleij 17578d318a50SLinus Walleij spin_unlock(&d40c->lock); 17588d318a50SLinus Walleij } 17598d318a50SLinus Walleij 17608d318a50SLinus Walleij spin_unlock_irqrestore(&base->interrupt_lock, flags); 17618d318a50SLinus Walleij 17628d318a50SLinus Walleij return IRQ_HANDLED; 17638d318a50SLinus Walleij } 17648d318a50SLinus Walleij 17658d318a50SLinus Walleij static int d40_validate_conf(struct d40_chan *d40c, 17668d318a50SLinus Walleij struct stedma40_chan_cfg *conf) 17678d318a50SLinus Walleij { 17688d318a50SLinus Walleij int res = 0; 176938bdbf02SRabin Vincent bool is_log = conf->mode == STEDMA40_MODE_LOGICAL; 17708d318a50SLinus Walleij 17710747c7baSLinus Walleij if (!conf->dir) { 17726db5a8baSRabin Vincent chan_err(d40c, "Invalid direction.\n"); 17730747c7baSLinus Walleij res = -EINVAL; 17740747c7baSLinus Walleij } 17750747c7baSLinus Walleij 177626955c07SLee Jones if ((is_log && conf->dev_type > d40c->base->num_log_chans) || 177726955c07SLee Jones (!is_log && conf->dev_type > d40c->base->num_phy_chans) || 177826955c07SLee Jones (conf->dev_type < 0)) { 177926955c07SLee Jones chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type); 17800747c7baSLinus Walleij res = -EINVAL; 17810747c7baSLinus Walleij } 17820747c7baSLinus Walleij 17832c2b62d5SLee Jones if (conf->dir == DMA_DEV_TO_DEV) { 17848d318a50SLinus Walleij /* 17858d318a50SLinus Walleij * DMAC HW supports it. Will be added to this driver, 17868d318a50SLinus Walleij * in case any dma client requires it. 17878d318a50SLinus Walleij */ 17886db5a8baSRabin Vincent chan_err(d40c, "periph to periph not supported\n"); 17898d318a50SLinus Walleij res = -EINVAL; 17908d318a50SLinus Walleij } 17918d318a50SLinus Walleij 1792d49278e3SPer Forlin if (d40_psize_2_burst_size(is_log, conf->src_info.psize) * 179343f2e1a3SLee Jones conf->src_info.data_width != 1794d49278e3SPer Forlin d40_psize_2_burst_size(is_log, conf->dst_info.psize) * 179543f2e1a3SLee Jones conf->dst_info.data_width) { 1796d49278e3SPer Forlin /* 1797d49278e3SPer Forlin * The DMAC hardware only supports 1798d49278e3SPer Forlin * src (burst x width) == dst (burst x width) 1799d49278e3SPer Forlin */ 1800d49278e3SPer Forlin 18016db5a8baSRabin Vincent chan_err(d40c, "src (burst x width) != dst (burst x width)\n"); 1802d49278e3SPer Forlin res = -EINVAL; 1803d49278e3SPer Forlin } 1804d49278e3SPer Forlin 18058d318a50SLinus Walleij return res; 18068d318a50SLinus Walleij } 18078d318a50SLinus Walleij 18085cd326fdSNarayanan G static bool d40_alloc_mask_set(struct d40_phy_res *phy, 18095cd326fdSNarayanan G bool is_src, int log_event_line, bool is_log, 18105cd326fdSNarayanan G bool *first_user) 18118d318a50SLinus Walleij { 18128d318a50SLinus Walleij unsigned long flags; 18138d318a50SLinus Walleij spin_lock_irqsave(&phy->lock, flags); 18145cd326fdSNarayanan G 18155cd326fdSNarayanan G *first_user = ((phy->allocated_src | phy->allocated_dst) 18165cd326fdSNarayanan G == D40_ALLOC_FREE); 18175cd326fdSNarayanan G 18184aed79b2SMarcin Mielczarczyk if (!is_log) { 18198d318a50SLinus Walleij /* Physical interrupts are masked per physical full channel */ 18208d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE && 18218d318a50SLinus Walleij phy->allocated_dst == D40_ALLOC_FREE) { 18228d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_PHY; 18238d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_PHY; 18248d318a50SLinus Walleij goto found; 18258d318a50SLinus Walleij } else 18268d318a50SLinus Walleij goto not_found; 18278d318a50SLinus Walleij } 18288d318a50SLinus Walleij 18298d318a50SLinus Walleij /* Logical channel */ 18308d318a50SLinus Walleij if (is_src) { 18318d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_PHY) 18328d318a50SLinus Walleij goto not_found; 18338d318a50SLinus Walleij 18348d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE) 18358d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_LOG_FREE; 18368d318a50SLinus Walleij 18378a3b6e14SLee Jones if (!(phy->allocated_src & BIT(log_event_line))) { 18388a3b6e14SLee Jones phy->allocated_src |= BIT(log_event_line); 18398d318a50SLinus Walleij goto found; 18408d318a50SLinus Walleij } else 18418d318a50SLinus Walleij goto not_found; 18428d318a50SLinus Walleij } else { 18438d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_PHY) 18448d318a50SLinus Walleij goto not_found; 18458d318a50SLinus Walleij 18468d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_FREE) 18478d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_LOG_FREE; 18488d318a50SLinus Walleij 18498a3b6e14SLee Jones if (!(phy->allocated_dst & BIT(log_event_line))) { 18508a3b6e14SLee Jones phy->allocated_dst |= BIT(log_event_line); 18518d318a50SLinus Walleij goto found; 18528d318a50SLinus Walleij } else 18538d318a50SLinus Walleij goto not_found; 18548d318a50SLinus Walleij } 18558d318a50SLinus Walleij 18568d318a50SLinus Walleij not_found: 18578d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 18588d318a50SLinus Walleij return false; 18598d318a50SLinus Walleij found: 18608d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 18618d318a50SLinus Walleij return true; 18628d318a50SLinus Walleij } 18638d318a50SLinus Walleij 18648d318a50SLinus Walleij static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src, 18658d318a50SLinus Walleij int log_event_line) 18668d318a50SLinus Walleij { 18678d318a50SLinus Walleij unsigned long flags; 18688d318a50SLinus Walleij bool is_free = false; 18698d318a50SLinus Walleij 18708d318a50SLinus Walleij spin_lock_irqsave(&phy->lock, flags); 18718d318a50SLinus Walleij if (!log_event_line) { 18728d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE; 18738d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE; 18748d318a50SLinus Walleij is_free = true; 18758d318a50SLinus Walleij goto out; 18768d318a50SLinus Walleij } 18778d318a50SLinus Walleij 18788d318a50SLinus Walleij /* Logical channel */ 18798d318a50SLinus Walleij if (is_src) { 18808a3b6e14SLee Jones phy->allocated_src &= ~BIT(log_event_line); 18818d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_LOG_FREE) 18828d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE; 18838d318a50SLinus Walleij } else { 18848a3b6e14SLee Jones phy->allocated_dst &= ~BIT(log_event_line); 18858d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_LOG_FREE) 18868d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE; 18878d318a50SLinus Walleij } 18888d318a50SLinus Walleij 18898d318a50SLinus Walleij is_free = ((phy->allocated_src | phy->allocated_dst) == 18908d318a50SLinus Walleij D40_ALLOC_FREE); 18918d318a50SLinus Walleij 18928d318a50SLinus Walleij out: 18938d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 18948d318a50SLinus Walleij 18958d318a50SLinus Walleij return is_free; 18968d318a50SLinus Walleij } 18978d318a50SLinus Walleij 18985cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user) 18998d318a50SLinus Walleij { 190026955c07SLee Jones int dev_type = d40c->dma_cfg.dev_type; 19018d318a50SLinus Walleij int event_group; 19028d318a50SLinus Walleij int event_line; 19038d318a50SLinus Walleij struct d40_phy_res *phys; 19048d318a50SLinus Walleij int i; 19058d318a50SLinus Walleij int j; 19068d318a50SLinus Walleij int log_num; 1907f000df8cSGerald Baeza int num_phy_chans; 19088d318a50SLinus Walleij bool is_src; 190938bdbf02SRabin Vincent bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL; 19108d318a50SLinus Walleij 19118d318a50SLinus Walleij phys = d40c->base->phy_res; 1912f000df8cSGerald Baeza num_phy_chans = d40c->base->num_phy_chans; 19138d318a50SLinus Walleij 19142c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) { 19158d318a50SLinus Walleij log_num = 2 * dev_type; 19168d318a50SLinus Walleij is_src = true; 19172c2b62d5SLee Jones } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV || 19182c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM) { 19198d318a50SLinus Walleij /* dst event lines are used for logical memcpy */ 19208d318a50SLinus Walleij log_num = 2 * dev_type + 1; 19218d318a50SLinus Walleij is_src = false; 19228d318a50SLinus Walleij } else 19238d318a50SLinus Walleij return -EINVAL; 19248d318a50SLinus Walleij 19258d318a50SLinus Walleij event_group = D40_TYPE_TO_GROUP(dev_type); 19268d318a50SLinus Walleij event_line = D40_TYPE_TO_EVENT(dev_type); 19278d318a50SLinus Walleij 19288d318a50SLinus Walleij if (!is_log) { 19292c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) { 19308d318a50SLinus Walleij /* Find physical half channel */ 1931f000df8cSGerald Baeza if (d40c->dma_cfg.use_fixed_channel) { 1932f000df8cSGerald Baeza i = d40c->dma_cfg.phy_channel; 19334aed79b2SMarcin Mielczarczyk if (d40_alloc_mask_set(&phys[i], is_src, 19345cd326fdSNarayanan G 0, is_log, 19355cd326fdSNarayanan G first_phy_user)) 19368d318a50SLinus Walleij goto found_phy; 1937f000df8cSGerald Baeza } else { 1938f000df8cSGerald Baeza for (i = 0; i < num_phy_chans; i++) { 1939f000df8cSGerald Baeza if (d40_alloc_mask_set(&phys[i], is_src, 1940f000df8cSGerald Baeza 0, is_log, 1941f000df8cSGerald Baeza first_phy_user)) 1942f000df8cSGerald Baeza goto found_phy; 1943f000df8cSGerald Baeza } 19448d318a50SLinus Walleij } 19458d318a50SLinus Walleij } else 19468d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) { 19478d318a50SLinus Walleij int phy_num = j + event_group * 2; 19488d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) { 1949508849adSLinus Walleij if (d40_alloc_mask_set(&phys[i], 1950508849adSLinus Walleij is_src, 1951508849adSLinus Walleij 0, 19525cd326fdSNarayanan G is_log, 19535cd326fdSNarayanan G first_phy_user)) 19548d318a50SLinus Walleij goto found_phy; 19558d318a50SLinus Walleij } 19568d318a50SLinus Walleij } 19578d318a50SLinus Walleij return -EINVAL; 19588d318a50SLinus Walleij found_phy: 19598d318a50SLinus Walleij d40c->phy_chan = &phys[i]; 19608d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN; 19618d318a50SLinus Walleij goto out; 19628d318a50SLinus Walleij } 19638d318a50SLinus Walleij if (dev_type == -1) 19648d318a50SLinus Walleij return -EINVAL; 19658d318a50SLinus Walleij 19668d318a50SLinus Walleij /* Find logical channel */ 19678d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) { 19688d318a50SLinus Walleij int phy_num = j + event_group * 2; 19695cd326fdSNarayanan G 19705cd326fdSNarayanan G if (d40c->dma_cfg.use_fixed_channel) { 19715cd326fdSNarayanan G i = d40c->dma_cfg.phy_channel; 19725cd326fdSNarayanan G 19735cd326fdSNarayanan G if ((i != phy_num) && (i != phy_num + 1)) { 19745cd326fdSNarayanan G dev_err(chan2dev(d40c), 19755cd326fdSNarayanan G "invalid fixed phy channel %d\n", i); 19765cd326fdSNarayanan G return -EINVAL; 19775cd326fdSNarayanan G } 19785cd326fdSNarayanan G 19795cd326fdSNarayanan G if (d40_alloc_mask_set(&phys[i], is_src, event_line, 19805cd326fdSNarayanan G is_log, first_phy_user)) 19815cd326fdSNarayanan G goto found_log; 19825cd326fdSNarayanan G 19835cd326fdSNarayanan G dev_err(chan2dev(d40c), 19845cd326fdSNarayanan G "could not allocate fixed phy channel %d\n", i); 19855cd326fdSNarayanan G return -EINVAL; 19865cd326fdSNarayanan G } 19875cd326fdSNarayanan G 19888d318a50SLinus Walleij /* 19898d318a50SLinus Walleij * Spread logical channels across all available physical rather 19908d318a50SLinus Walleij * than pack every logical channel at the first available phy 19918d318a50SLinus Walleij * channels. 19928d318a50SLinus Walleij */ 19938d318a50SLinus Walleij if (is_src) { 19948d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) { 19958d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src, 19965cd326fdSNarayanan G event_line, is_log, 19975cd326fdSNarayanan G first_phy_user)) 19988d318a50SLinus Walleij goto found_log; 19998d318a50SLinus Walleij } 20008d318a50SLinus Walleij } else { 20018d318a50SLinus Walleij for (i = phy_num + 1; i >= phy_num; i--) { 20028d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src, 20035cd326fdSNarayanan G event_line, is_log, 20045cd326fdSNarayanan G first_phy_user)) 20058d318a50SLinus Walleij goto found_log; 20068d318a50SLinus Walleij } 20078d318a50SLinus Walleij } 20088d318a50SLinus Walleij } 20098d318a50SLinus Walleij return -EINVAL; 20108d318a50SLinus Walleij 20118d318a50SLinus Walleij found_log: 20128d318a50SLinus Walleij d40c->phy_chan = &phys[i]; 20138d318a50SLinus Walleij d40c->log_num = log_num; 20148d318a50SLinus Walleij out: 20158d318a50SLinus Walleij 20168d318a50SLinus Walleij if (is_log) 20178d318a50SLinus Walleij d40c->base->lookup_log_chans[d40c->log_num] = d40c; 20188d318a50SLinus Walleij else 20198d318a50SLinus Walleij d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c; 20208d318a50SLinus Walleij 20218d318a50SLinus Walleij return 0; 20228d318a50SLinus Walleij 20238d318a50SLinus Walleij } 20248d318a50SLinus Walleij 20258d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c) 20268d318a50SLinus Walleij { 20278d318a50SLinus Walleij dma_cap_mask_t cap = d40c->chan.device->cap_mask; 20288d318a50SLinus Walleij 20298d318a50SLinus Walleij if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) { 203029027a1eSLee Jones d40c->dma_cfg = dma40_memcpy_conf_log; 203126955c07SLee Jones d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id]; 20328d318a50SLinus Walleij 20339b233f9bSLee Jones d40_log_cfg(&d40c->dma_cfg, 20349b233f9bSLee Jones &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); 20359b233f9bSLee Jones 20368d318a50SLinus Walleij } else if (dma_has_cap(DMA_MEMCPY, cap) && 20378d318a50SLinus Walleij dma_has_cap(DMA_SLAVE, cap)) { 203829027a1eSLee Jones d40c->dma_cfg = dma40_memcpy_conf_phy; 203957e65ad7SLee Jones 204057e65ad7SLee Jones /* Generate interrrupt at end of transfer or relink. */ 204157e65ad7SLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS); 204257e65ad7SLee Jones 204357e65ad7SLee Jones /* Generate interrupt on error. */ 204457e65ad7SLee Jones d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS); 204557e65ad7SLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS); 204657e65ad7SLee Jones 20478d318a50SLinus Walleij } else { 20486db5a8baSRabin Vincent chan_err(d40c, "No memcpy\n"); 20498d318a50SLinus Walleij return -EINVAL; 20508d318a50SLinus Walleij } 20518d318a50SLinus Walleij 20528d318a50SLinus Walleij return 0; 20538d318a50SLinus Walleij } 20548d318a50SLinus Walleij 20558d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c) 20568d318a50SLinus Walleij { 20578d318a50SLinus Walleij 20588d318a50SLinus Walleij int res = 0; 205926955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type); 20608d318a50SLinus Walleij struct d40_phy_res *phy = d40c->phy_chan; 20618d318a50SLinus Walleij bool is_src; 20628d318a50SLinus Walleij 20638d318a50SLinus Walleij /* Terminate all queued and active transfers */ 20648d318a50SLinus Walleij d40_term_all(d40c); 20658d318a50SLinus Walleij 20668d318a50SLinus Walleij if (phy == NULL) { 20676db5a8baSRabin Vincent chan_err(d40c, "phy == null\n"); 20688d318a50SLinus Walleij return -EINVAL; 20698d318a50SLinus Walleij } 20708d318a50SLinus Walleij 20718d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE && 20728d318a50SLinus Walleij phy->allocated_dst == D40_ALLOC_FREE) { 20736db5a8baSRabin Vincent chan_err(d40c, "channel already free\n"); 20748d318a50SLinus Walleij return -EINVAL; 20758d318a50SLinus Walleij } 20768d318a50SLinus Walleij 20772c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV || 20782c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM) 20798d318a50SLinus Walleij is_src = false; 20802c2b62d5SLee Jones else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) 20818d318a50SLinus Walleij is_src = true; 208226955c07SLee Jones else { 20836db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n"); 20848d318a50SLinus Walleij return -EINVAL; 20858d318a50SLinus Walleij } 20868d318a50SLinus Walleij 20877fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 20888d318a50SLinus Walleij res = d40_channel_execute_command(d40c, D40_DMA_STOP); 20898d318a50SLinus Walleij if (res) { 20901bdae6f4SNarayanan G chan_err(d40c, "stop failed\n"); 20917fb3e75eSNarayanan G goto out; 20928d318a50SLinus Walleij } 20937fb3e75eSNarayanan G 20941bdae6f4SNarayanan G d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0); 20951bdae6f4SNarayanan G 20961bdae6f4SNarayanan G if (chan_is_logical(d40c)) 20971bdae6f4SNarayanan G d40c->base->lookup_log_chans[d40c->log_num] = NULL; 20981bdae6f4SNarayanan G else 20991bdae6f4SNarayanan G d40c->base->lookup_phy_chans[phy->num] = NULL; 21001bdae6f4SNarayanan G 21017fb3e75eSNarayanan G if (d40c->busy) { 21027fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 21037fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 21047fb3e75eSNarayanan G } 21057fb3e75eSNarayanan G 21067fb3e75eSNarayanan G d40c->busy = false; 21078d318a50SLinus Walleij d40c->phy_chan = NULL; 2108ce2ca125SRabin Vincent d40c->configured = false; 21097fb3e75eSNarayanan G out: 21108d318a50SLinus Walleij 21117fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 21127fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 21137fb3e75eSNarayanan G return res; 21148d318a50SLinus Walleij } 21158d318a50SLinus Walleij 2116a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c) 2117a5ebca47SJonas Aaberg { 21188ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 2119a5ebca47SJonas Aaberg bool is_paused = false; 2120a5ebca47SJonas Aaberg unsigned long flags; 2121a5ebca47SJonas Aaberg void __iomem *active_reg; 2122a5ebca47SJonas Aaberg u32 status; 212326955c07SLee Jones u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type); 2124a5ebca47SJonas Aaberg 2125a5ebca47SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 2126a5ebca47SJonas Aaberg 2127724a8577SRabin Vincent if (chan_is_physical(d40c)) { 2128a5ebca47SJonas Aaberg if (d40c->phy_chan->num % 2 == 0) 2129a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 2130a5ebca47SJonas Aaberg else 2131a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 2132a5ebca47SJonas Aaberg 2133a5ebca47SJonas Aaberg status = (readl(active_reg) & 2134a5ebca47SJonas Aaberg D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 2135a5ebca47SJonas Aaberg D40_CHAN_POS(d40c->phy_chan->num); 2136a5ebca47SJonas Aaberg if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) 2137a5ebca47SJonas Aaberg is_paused = true; 2138a5ebca47SJonas Aaberg 2139a5ebca47SJonas Aaberg goto _exit; 2140a5ebca47SJonas Aaberg } 2141a5ebca47SJonas Aaberg 21422c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV || 21432c2b62d5SLee Jones d40c->dma_cfg.dir == DMA_MEM_TO_MEM) { 21448ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SDLNK); 21452c2b62d5SLee Jones } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) { 21468ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SSLNK); 21479dbfbd35SJonas Aaberg } else { 21486db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n"); 2149a5ebca47SJonas Aaberg goto _exit; 2150a5ebca47SJonas Aaberg } 21519dbfbd35SJonas Aaberg 2152a5ebca47SJonas Aaberg status = (status & D40_EVENTLINE_MASK(event)) >> 2153a5ebca47SJonas Aaberg D40_EVENTLINE_POS(event); 2154a5ebca47SJonas Aaberg 2155a5ebca47SJonas Aaberg if (status != D40_DMA_RUN) 2156a5ebca47SJonas Aaberg is_paused = true; 2157a5ebca47SJonas Aaberg _exit: 2158a5ebca47SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 2159a5ebca47SJonas Aaberg return is_paused; 2160a5ebca47SJonas Aaberg 2161a5ebca47SJonas Aaberg } 2162a5ebca47SJonas Aaberg 21638d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan) 21648d318a50SLinus Walleij { 21658d318a50SLinus Walleij struct d40_chan *d40c = 21668d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 21678d318a50SLinus Walleij u32 bytes_left; 21688d318a50SLinus Walleij unsigned long flags; 21698d318a50SLinus Walleij 21708d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 21718d318a50SLinus Walleij bytes_left = d40_residue(d40c); 21728d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 21738d318a50SLinus Walleij 21748d318a50SLinus Walleij return bytes_left; 21758d318a50SLinus Walleij } 21768d318a50SLinus Walleij 21773e3a0763SRabin Vincent static int 21783e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc, 21793e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst, 2180822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr, 2181822c5676SRabin Vincent dma_addr_t dst_dev_addr) 21823e3a0763SRabin Vincent { 21833e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 21843e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info; 21853e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info; 21865ed04b85SRabin Vincent int ret; 21873e3a0763SRabin Vincent 21885ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_src, sg_len, 21895ed04b85SRabin Vincent src_dev_addr, 21903e3a0763SRabin Vincent desc->lli_log.src, 21913e3a0763SRabin Vincent chan->log_def.lcsp1, 21923e3a0763SRabin Vincent src_info->data_width, 21933e3a0763SRabin Vincent dst_info->data_width); 21943e3a0763SRabin Vincent 21955ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_dst, sg_len, 21965ed04b85SRabin Vincent dst_dev_addr, 21973e3a0763SRabin Vincent desc->lli_log.dst, 21983e3a0763SRabin Vincent chan->log_def.lcsp3, 21993e3a0763SRabin Vincent dst_info->data_width, 22003e3a0763SRabin Vincent src_info->data_width); 22013e3a0763SRabin Vincent 22025ed04b85SRabin Vincent return ret < 0 ? ret : 0; 22033e3a0763SRabin Vincent } 22043e3a0763SRabin Vincent 22053e3a0763SRabin Vincent static int 22063e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc, 22073e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst, 2208822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr, 2209822c5676SRabin Vincent dma_addr_t dst_dev_addr) 22103e3a0763SRabin Vincent { 22113e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 22123e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info; 22133e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info; 22140c842b55SRabin Vincent unsigned long flags = 0; 22153e3a0763SRabin Vincent int ret; 22163e3a0763SRabin Vincent 22170c842b55SRabin Vincent if (desc->cyclic) 22180c842b55SRabin Vincent flags |= LLI_CYCLIC | LLI_TERM_INT; 22190c842b55SRabin Vincent 22203e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr, 22213e3a0763SRabin Vincent desc->lli_phy.src, 22223e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.src), 22233e3a0763SRabin Vincent chan->src_def_cfg, 22240c842b55SRabin Vincent src_info, dst_info, flags); 22253e3a0763SRabin Vincent 22263e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr, 22273e3a0763SRabin Vincent desc->lli_phy.dst, 22283e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.dst), 22293e3a0763SRabin Vincent chan->dst_def_cfg, 22300c842b55SRabin Vincent dst_info, src_info, flags); 22313e3a0763SRabin Vincent 22323e3a0763SRabin Vincent dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr, 22333e3a0763SRabin Vincent desc->lli_pool.size, DMA_TO_DEVICE); 22343e3a0763SRabin Vincent 22353e3a0763SRabin Vincent return ret < 0 ? ret : 0; 22363e3a0763SRabin Vincent } 22373e3a0763SRabin Vincent 22385f81158fSRabin Vincent static struct d40_desc * 22395f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg, 22405f81158fSRabin Vincent unsigned int sg_len, unsigned long dma_flags) 22415f81158fSRabin Vincent { 22425f81158fSRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 22435f81158fSRabin Vincent struct d40_desc *desc; 2244dbd88788SRabin Vincent int ret; 22455f81158fSRabin Vincent 22465f81158fSRabin Vincent desc = d40_desc_get(chan); 22475f81158fSRabin Vincent if (!desc) 22485f81158fSRabin Vincent return NULL; 22495f81158fSRabin Vincent 22505f81158fSRabin Vincent desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width, 22515f81158fSRabin Vincent cfg->dst_info.data_width); 22525f81158fSRabin Vincent if (desc->lli_len < 0) { 22535f81158fSRabin Vincent chan_err(chan, "Unaligned size\n"); 2254dbd88788SRabin Vincent goto err; 22555f81158fSRabin Vincent } 22565f81158fSRabin Vincent 2257dbd88788SRabin Vincent ret = d40_pool_lli_alloc(chan, desc, desc->lli_len); 2258dbd88788SRabin Vincent if (ret < 0) { 2259dbd88788SRabin Vincent chan_err(chan, "Could not allocate lli\n"); 2260dbd88788SRabin Vincent goto err; 2261dbd88788SRabin Vincent } 2262dbd88788SRabin Vincent 22635f81158fSRabin Vincent desc->lli_current = 0; 22645f81158fSRabin Vincent desc->txd.flags = dma_flags; 22655f81158fSRabin Vincent desc->txd.tx_submit = d40_tx_submit; 22665f81158fSRabin Vincent 22675f81158fSRabin Vincent dma_async_tx_descriptor_init(&desc->txd, &chan->chan); 22685f81158fSRabin Vincent 22695f81158fSRabin Vincent return desc; 2270dbd88788SRabin Vincent 2271dbd88788SRabin Vincent err: 2272dbd88788SRabin Vincent d40_desc_free(chan, desc); 2273dbd88788SRabin Vincent return NULL; 22745f81158fSRabin Vincent } 22755f81158fSRabin Vincent 2276cade1d30SRabin Vincent static struct dma_async_tx_descriptor * 2277cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src, 2278cade1d30SRabin Vincent struct scatterlist *sg_dst, unsigned int sg_len, 2279db8196dfSVinod Koul enum dma_transfer_direction direction, unsigned long dma_flags) 2280cade1d30SRabin Vincent { 2281cade1d30SRabin Vincent struct d40_chan *chan = container_of(dchan, struct d40_chan, chan); 2282822c5676SRabin Vincent dma_addr_t src_dev_addr = 0; 2283822c5676SRabin Vincent dma_addr_t dst_dev_addr = 0; 2284cade1d30SRabin Vincent struct d40_desc *desc; 2285cade1d30SRabin Vincent unsigned long flags; 2286cade1d30SRabin Vincent int ret; 22878d318a50SLinus Walleij 2288cade1d30SRabin Vincent if (!chan->phy_chan) { 2289cade1d30SRabin Vincent chan_err(chan, "Cannot prepare unallocated channel\n"); 2290cade1d30SRabin Vincent return NULL; 2291cade1d30SRabin Vincent } 2292cade1d30SRabin Vincent 2293cade1d30SRabin Vincent spin_lock_irqsave(&chan->lock, flags); 2294cade1d30SRabin Vincent 2295cade1d30SRabin Vincent desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags); 2296cade1d30SRabin Vincent if (desc == NULL) 22978d318a50SLinus Walleij goto err; 22988d318a50SLinus Walleij 22990c842b55SRabin Vincent if (sg_next(&sg_src[sg_len - 1]) == sg_src) 23000c842b55SRabin Vincent desc->cyclic = true; 23010c842b55SRabin Vincent 2302db8196dfSVinod Koul if (direction == DMA_DEV_TO_MEM) 2303ef9c89b3SLee Jones src_dev_addr = chan->runtime_addr; 2304db8196dfSVinod Koul else if (direction == DMA_MEM_TO_DEV) 2305ef9c89b3SLee Jones dst_dev_addr = chan->runtime_addr; 2306cade1d30SRabin Vincent 2307cade1d30SRabin Vincent if (chan_is_logical(chan)) 2308cade1d30SRabin Vincent ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst, 2309822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr); 2310cade1d30SRabin Vincent else 2311cade1d30SRabin Vincent ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst, 2312822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr); 2313cade1d30SRabin Vincent 2314cade1d30SRabin Vincent if (ret) { 2315cade1d30SRabin Vincent chan_err(chan, "Failed to prepare %s sg job: %d\n", 2316cade1d30SRabin Vincent chan_is_logical(chan) ? "log" : "phy", ret); 2317cade1d30SRabin Vincent goto err; 23188d318a50SLinus Walleij } 23198d318a50SLinus Walleij 232082babbb3SPer Forlin /* 232182babbb3SPer Forlin * add descriptor to the prepare queue in order to be able 232282babbb3SPer Forlin * to free them later in terminate_all 232382babbb3SPer Forlin */ 232482babbb3SPer Forlin list_add_tail(&desc->node, &chan->prepare_queue); 232582babbb3SPer Forlin 2326cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags); 23278d318a50SLinus Walleij 2328cade1d30SRabin Vincent return &desc->txd; 2329cade1d30SRabin Vincent 23308d318a50SLinus Walleij err: 2331cade1d30SRabin Vincent if (desc) 2332cade1d30SRabin Vincent d40_desc_free(chan, desc); 2333cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags); 23348d318a50SLinus Walleij return NULL; 23358d318a50SLinus Walleij } 23368d318a50SLinus Walleij 23378d318a50SLinus Walleij bool stedma40_filter(struct dma_chan *chan, void *data) 23388d318a50SLinus Walleij { 23398d318a50SLinus Walleij struct stedma40_chan_cfg *info = data; 23408d318a50SLinus Walleij struct d40_chan *d40c = 23418d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 23428d318a50SLinus Walleij int err; 23438d318a50SLinus Walleij 23448d318a50SLinus Walleij if (data) { 23458d318a50SLinus Walleij err = d40_validate_conf(d40c, info); 23468d318a50SLinus Walleij if (!err) 23478d318a50SLinus Walleij d40c->dma_cfg = *info; 23488d318a50SLinus Walleij } else 23498d318a50SLinus Walleij err = d40_config_memcpy(d40c); 23508d318a50SLinus Walleij 2351ce2ca125SRabin Vincent if (!err) 2352ce2ca125SRabin Vincent d40c->configured = true; 2353ce2ca125SRabin Vincent 23548d318a50SLinus Walleij return err == 0; 23558d318a50SLinus Walleij } 23568d318a50SLinus Walleij EXPORT_SYMBOL(stedma40_filter); 23578d318a50SLinus Walleij 2358ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src) 2359ac2c0a38SRabin Vincent { 2360ac2c0a38SRabin Vincent bool realtime = d40c->dma_cfg.realtime; 2361ac2c0a38SRabin Vincent bool highprio = d40c->dma_cfg.high_priority; 23623cb645dcSTong Liu u32 rtreg; 2363ac2c0a38SRabin Vincent u32 event = D40_TYPE_TO_EVENT(dev_type); 2364ac2c0a38SRabin Vincent u32 group = D40_TYPE_TO_GROUP(dev_type); 23658a3b6e14SLee Jones u32 bit = BIT(event); 2366ccc3d697SRabin Vincent u32 prioreg; 23673cb645dcSTong Liu struct d40_gen_dmac *dmac = &d40c->base->gen_dmac; 2368ccc3d697SRabin Vincent 23693cb645dcSTong Liu rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear; 2370ccc3d697SRabin Vincent /* 2371ccc3d697SRabin Vincent * Due to a hardware bug, in some cases a logical channel triggered by 2372ccc3d697SRabin Vincent * a high priority destination event line can generate extra packet 2373ccc3d697SRabin Vincent * transactions. 2374ccc3d697SRabin Vincent * 2375ccc3d697SRabin Vincent * The workaround is to not set the high priority level for the 2376ccc3d697SRabin Vincent * destination event lines that trigger logical channels. 2377ccc3d697SRabin Vincent */ 2378ccc3d697SRabin Vincent if (!src && chan_is_logical(d40c)) 2379ccc3d697SRabin Vincent highprio = false; 2380ccc3d697SRabin Vincent 23813cb645dcSTong Liu prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear; 2382ac2c0a38SRabin Vincent 2383ac2c0a38SRabin Vincent /* Destination event lines are stored in the upper halfword */ 2384ac2c0a38SRabin Vincent if (!src) 2385ac2c0a38SRabin Vincent bit <<= 16; 2386ac2c0a38SRabin Vincent 2387ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + prioreg + group * 4); 2388ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + rtreg + group * 4); 2389ac2c0a38SRabin Vincent } 2390ac2c0a38SRabin Vincent 2391ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c) 2392ac2c0a38SRabin Vincent { 2393ac2c0a38SRabin Vincent if (d40c->base->rev < 3) 2394ac2c0a38SRabin Vincent return; 2395ac2c0a38SRabin Vincent 23962c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) || 23972c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV)) 239826955c07SLee Jones __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true); 2399ac2c0a38SRabin Vincent 24002c2b62d5SLee Jones if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) || 24012c2b62d5SLee Jones (d40c->dma_cfg.dir == DMA_DEV_TO_DEV)) 240226955c07SLee Jones __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false); 2403ac2c0a38SRabin Vincent } 2404ac2c0a38SRabin Vincent 2405fa332de5SLee Jones #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1) 2406fa332de5SLee Jones #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1) 2407fa332de5SLee Jones #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1) 2408fa332de5SLee Jones #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1) 2409fa332de5SLee Jones 2410fa332de5SLee Jones static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec, 2411fa332de5SLee Jones struct of_dma *ofdma) 2412fa332de5SLee Jones { 2413fa332de5SLee Jones struct stedma40_chan_cfg cfg; 2414fa332de5SLee Jones dma_cap_mask_t cap; 2415fa332de5SLee Jones u32 flags; 2416fa332de5SLee Jones 2417fa332de5SLee Jones memset(&cfg, 0, sizeof(struct stedma40_chan_cfg)); 2418fa332de5SLee Jones 2419fa332de5SLee Jones dma_cap_zero(cap); 2420fa332de5SLee Jones dma_cap_set(DMA_SLAVE, cap); 2421fa332de5SLee Jones 2422fa332de5SLee Jones cfg.dev_type = dma_spec->args[0]; 2423fa332de5SLee Jones flags = dma_spec->args[2]; 2424fa332de5SLee Jones 2425fa332de5SLee Jones switch (D40_DT_FLAGS_MODE(flags)) { 2426fa332de5SLee Jones case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break; 2427fa332de5SLee Jones case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break; 2428fa332de5SLee Jones } 2429fa332de5SLee Jones 2430fa332de5SLee Jones switch (D40_DT_FLAGS_DIR(flags)) { 2431fa332de5SLee Jones case 0: 24322c2b62d5SLee Jones cfg.dir = DMA_MEM_TO_DEV; 2433fa332de5SLee Jones cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags); 2434fa332de5SLee Jones break; 2435fa332de5SLee Jones case 1: 24362c2b62d5SLee Jones cfg.dir = DMA_DEV_TO_MEM; 2437fa332de5SLee Jones cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags); 2438fa332de5SLee Jones break; 2439fa332de5SLee Jones } 2440fa332de5SLee Jones 2441fa332de5SLee Jones if (D40_DT_FLAGS_FIXED_CHAN(flags)) { 2442fa332de5SLee Jones cfg.phy_channel = dma_spec->args[1]; 2443fa332de5SLee Jones cfg.use_fixed_channel = true; 2444fa332de5SLee Jones } 2445fa332de5SLee Jones 2446fa332de5SLee Jones return dma_request_channel(cap, stedma40_filter, &cfg); 2447fa332de5SLee Jones } 2448fa332de5SLee Jones 24498d318a50SLinus Walleij /* DMA ENGINE functions */ 24508d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan) 24518d318a50SLinus Walleij { 24528d318a50SLinus Walleij int err; 24538d318a50SLinus Walleij unsigned long flags; 24548d318a50SLinus Walleij struct d40_chan *d40c = 24558d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 2456ef1872ecSLinus Walleij bool is_free_phy; 24578d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 24588d318a50SLinus Walleij 2459d3ee98cdSRussell King - ARM Linux dma_cookie_init(chan); 24608d318a50SLinus Walleij 2461ce2ca125SRabin Vincent /* If no dma configuration is set use default configuration (memcpy) */ 2462ce2ca125SRabin Vincent if (!d40c->configured) { 24638d318a50SLinus Walleij err = d40_config_memcpy(d40c); 2464ff0b12baSJonas Aaberg if (err) { 24656db5a8baSRabin Vincent chan_err(d40c, "Failed to configure memcpy channel\n"); 2466ff0b12baSJonas Aaberg goto fail; 2467ff0b12baSJonas Aaberg } 24688d318a50SLinus Walleij } 24698d318a50SLinus Walleij 24705cd326fdSNarayanan G err = d40_allocate_channel(d40c, &is_free_phy); 24718d318a50SLinus Walleij if (err) { 24726db5a8baSRabin Vincent chan_err(d40c, "Failed to allocate channel\n"); 24737fb3e75eSNarayanan G d40c->configured = false; 2474ff0b12baSJonas Aaberg goto fail; 24758d318a50SLinus Walleij } 24768d318a50SLinus Walleij 24777fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 2478ef1872ecSLinus Walleij 2479ac2c0a38SRabin Vincent d40_set_prio_realtime(d40c); 2480ac2c0a38SRabin Vincent 2481724a8577SRabin Vincent if (chan_is_logical(d40c)) { 24822c2b62d5SLee Jones if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) 2483ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base + 248426955c07SLee Jones d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE; 2485ef1872ecSLinus Walleij else 2486ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base + 248726955c07SLee Jones d40c->dma_cfg.dev_type * 2488ef1872ecSLinus Walleij D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA; 24899778256bSLee Jones 24909778256bSLee Jones /* Unmask the Global Interrupt Mask. */ 24919778256bSLee Jones d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS); 24929778256bSLee Jones d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS); 2493ef1872ecSLinus Walleij } 2494ef1872ecSLinus Walleij 24955cd326fdSNarayanan G dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n", 24965cd326fdSNarayanan G chan_is_logical(d40c) ? "logical" : "physical", 24975cd326fdSNarayanan G d40c->phy_chan->num, 24985cd326fdSNarayanan G d40c->dma_cfg.use_fixed_channel ? ", fixed" : ""); 24995cd326fdSNarayanan G 25005cd326fdSNarayanan G 2501ef1872ecSLinus Walleij /* 2502ef1872ecSLinus Walleij * Only write channel configuration to the DMA if the physical 2503ef1872ecSLinus Walleij * resource is free. In case of multiple logical channels 2504ef1872ecSLinus Walleij * on the same physical resource, only the first write is necessary. 2505ef1872ecSLinus Walleij */ 2506b55912c6SJonas Aaberg if (is_free_phy) 2507b55912c6SJonas Aaberg d40_config_write(d40c); 2508ff0b12baSJonas Aaberg fail: 25097fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 25107fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 25118d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 2512ff0b12baSJonas Aaberg return err; 25138d318a50SLinus Walleij } 25148d318a50SLinus Walleij 25158d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan) 25168d318a50SLinus Walleij { 25178d318a50SLinus Walleij struct d40_chan *d40c = 25188d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 25198d318a50SLinus Walleij int err; 25208d318a50SLinus Walleij unsigned long flags; 25218d318a50SLinus Walleij 25220d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 25236db5a8baSRabin Vincent chan_err(d40c, "Cannot free unallocated channel\n"); 25240d0f6b8bSJonas Aaberg return; 25250d0f6b8bSJonas Aaberg } 25260d0f6b8bSJonas Aaberg 25278d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 25288d318a50SLinus Walleij 25298d318a50SLinus Walleij err = d40_free_dma(d40c); 25308d318a50SLinus Walleij 25318d318a50SLinus Walleij if (err) 25326db5a8baSRabin Vincent chan_err(d40c, "Failed to free channel\n"); 25338d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 25348d318a50SLinus Walleij } 25358d318a50SLinus Walleij 25368d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan, 25378d318a50SLinus Walleij dma_addr_t dst, 25388d318a50SLinus Walleij dma_addr_t src, 25398d318a50SLinus Walleij size_t size, 25402a614340SJonas Aaberg unsigned long dma_flags) 25418d318a50SLinus Walleij { 254295944c6eSRabin Vincent struct scatterlist dst_sg; 254395944c6eSRabin Vincent struct scatterlist src_sg; 25448d318a50SLinus Walleij 254595944c6eSRabin Vincent sg_init_table(&dst_sg, 1); 254695944c6eSRabin Vincent sg_init_table(&src_sg, 1); 25470d0f6b8bSJonas Aaberg 254895944c6eSRabin Vincent sg_dma_address(&dst_sg) = dst; 254995944c6eSRabin Vincent sg_dma_address(&src_sg) = src; 25508d318a50SLinus Walleij 255195944c6eSRabin Vincent sg_dma_len(&dst_sg) = size; 255295944c6eSRabin Vincent sg_dma_len(&src_sg) = size; 25538d318a50SLinus Walleij 2554cade1d30SRabin Vincent return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags); 25558d318a50SLinus Walleij } 25568d318a50SLinus Walleij 25570d688662SIra Snyder static struct dma_async_tx_descriptor * 2558cade1d30SRabin Vincent d40_prep_memcpy_sg(struct dma_chan *chan, 25590d688662SIra Snyder struct scatterlist *dst_sg, unsigned int dst_nents, 25600d688662SIra Snyder struct scatterlist *src_sg, unsigned int src_nents, 25610d688662SIra Snyder unsigned long dma_flags) 25620d688662SIra Snyder { 25630d688662SIra Snyder if (dst_nents != src_nents) 25640d688662SIra Snyder return NULL; 25650d688662SIra Snyder 2566cade1d30SRabin Vincent return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags); 256700ac0341SRabin Vincent } 256800ac0341SRabin Vincent 2569f26e03adSFabio Baltieri static struct dma_async_tx_descriptor * 2570f26e03adSFabio Baltieri d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, 2571f26e03adSFabio Baltieri unsigned int sg_len, enum dma_transfer_direction direction, 2572f26e03adSFabio Baltieri unsigned long dma_flags, void *context) 25738d318a50SLinus Walleij { 2574a725dcc0SAndy Shevchenko if (!is_slave_direction(direction)) 257500ac0341SRabin Vincent return NULL; 257600ac0341SRabin Vincent 2577cade1d30SRabin Vincent return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags); 25788d318a50SLinus Walleij } 25798d318a50SLinus Walleij 25800c842b55SRabin Vincent static struct dma_async_tx_descriptor * 25810c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr, 25820c842b55SRabin Vincent size_t buf_len, size_t period_len, 2583ec8b5e48SPeter Ujfalusi enum dma_transfer_direction direction, unsigned long flags, 2584ec8b5e48SPeter Ujfalusi void *context) 25850c842b55SRabin Vincent { 25860c842b55SRabin Vincent unsigned int periods = buf_len / period_len; 25870c842b55SRabin Vincent struct dma_async_tx_descriptor *txd; 25880c842b55SRabin Vincent struct scatterlist *sg; 25890c842b55SRabin Vincent int i; 25900c842b55SRabin Vincent 259179ca7ec3SRobert Marklund sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT); 25920c842b55SRabin Vincent for (i = 0; i < periods; i++) { 25930c842b55SRabin Vincent sg_dma_address(&sg[i]) = dma_addr; 25940c842b55SRabin Vincent sg_dma_len(&sg[i]) = period_len; 25950c842b55SRabin Vincent dma_addr += period_len; 25960c842b55SRabin Vincent } 25970c842b55SRabin Vincent 25980c842b55SRabin Vincent sg[periods].offset = 0; 2599fdaf9c4bSLars-Peter Clausen sg_dma_len(&sg[periods]) = 0; 26000c842b55SRabin Vincent sg[periods].page_link = 26010c842b55SRabin Vincent ((unsigned long)sg | 0x01) & ~0x02; 26020c842b55SRabin Vincent 26030c842b55SRabin Vincent txd = d40_prep_sg(chan, sg, sg, periods, direction, 26040c842b55SRabin Vincent DMA_PREP_INTERRUPT); 26050c842b55SRabin Vincent 26060c842b55SRabin Vincent kfree(sg); 26070c842b55SRabin Vincent 26080c842b55SRabin Vincent return txd; 26090c842b55SRabin Vincent } 26100c842b55SRabin Vincent 26118d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan, 26128d318a50SLinus Walleij dma_cookie_t cookie, 26138d318a50SLinus Walleij struct dma_tx_state *txstate) 26148d318a50SLinus Walleij { 26158d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 261696a2af41SRussell King - ARM Linux enum dma_status ret; 26178d318a50SLinus Walleij 26180d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 26196db5a8baSRabin Vincent chan_err(d40c, "Cannot read status of unallocated channel\n"); 26200d0f6b8bSJonas Aaberg return -EINVAL; 26210d0f6b8bSJonas Aaberg } 26220d0f6b8bSJonas Aaberg 262396a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 262496a2af41SRussell King - ARM Linux if (ret != DMA_SUCCESS) 262596a2af41SRussell King - ARM Linux dma_set_residue(txstate, stedma40_residue(chan)); 26268d318a50SLinus Walleij 2627a5ebca47SJonas Aaberg if (d40_is_paused(d40c)) 2628a5ebca47SJonas Aaberg ret = DMA_PAUSED; 26298d318a50SLinus Walleij 26308d318a50SLinus Walleij return ret; 26318d318a50SLinus Walleij } 26328d318a50SLinus Walleij 26338d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan) 26348d318a50SLinus Walleij { 26358d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 26368d318a50SLinus Walleij unsigned long flags; 26378d318a50SLinus Walleij 26380d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 26396db5a8baSRabin Vincent chan_err(d40c, "Channel is not allocated!\n"); 26400d0f6b8bSJonas Aaberg return; 26410d0f6b8bSJonas Aaberg } 26420d0f6b8bSJonas Aaberg 26438d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 26448d318a50SLinus Walleij 2645a8f3067bSPer Forlin list_splice_tail_init(&d40c->pending_queue, &d40c->queue); 2646a8f3067bSPer Forlin 2647a8f3067bSPer Forlin /* Busy means that queued jobs are already being processed */ 26488d318a50SLinus Walleij if (!d40c->busy) 26498d318a50SLinus Walleij (void) d40_queue_start(d40c); 26508d318a50SLinus Walleij 26518d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 26528d318a50SLinus Walleij } 26538d318a50SLinus Walleij 26541bdae6f4SNarayanan G static void d40_terminate_all(struct dma_chan *chan) 26551bdae6f4SNarayanan G { 26561bdae6f4SNarayanan G unsigned long flags; 26571bdae6f4SNarayanan G struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 26581bdae6f4SNarayanan G int ret; 26591bdae6f4SNarayanan G 26601bdae6f4SNarayanan G spin_lock_irqsave(&d40c->lock, flags); 26611bdae6f4SNarayanan G 26621bdae6f4SNarayanan G pm_runtime_get_sync(d40c->base->dev); 26631bdae6f4SNarayanan G ret = d40_channel_execute_command(d40c, D40_DMA_STOP); 26641bdae6f4SNarayanan G if (ret) 26651bdae6f4SNarayanan G chan_err(d40c, "Failed to stop channel\n"); 26661bdae6f4SNarayanan G 26671bdae6f4SNarayanan G d40_term_all(d40c); 26681bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 26691bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 26701bdae6f4SNarayanan G if (d40c->busy) { 26711bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 26721bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 26731bdae6f4SNarayanan G } 26741bdae6f4SNarayanan G d40c->busy = false; 26751bdae6f4SNarayanan G 26761bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->lock, flags); 26771bdae6f4SNarayanan G } 26781bdae6f4SNarayanan G 267998ca5289SRabin Vincent static int 268098ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c, 268198ca5289SRabin Vincent struct stedma40_half_channel_info *info, 268298ca5289SRabin Vincent u32 maxburst) 268398ca5289SRabin Vincent { 268498ca5289SRabin Vincent int psize; 268598ca5289SRabin Vincent 268698ca5289SRabin Vincent if (chan_is_logical(d40c)) { 268798ca5289SRabin Vincent if (maxburst >= 16) 268898ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_16; 268998ca5289SRabin Vincent else if (maxburst >= 8) 269098ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_8; 269198ca5289SRabin Vincent else if (maxburst >= 4) 269298ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_4; 269398ca5289SRabin Vincent else 269498ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_1; 269598ca5289SRabin Vincent } else { 269698ca5289SRabin Vincent if (maxburst >= 16) 269798ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_16; 269898ca5289SRabin Vincent else if (maxburst >= 8) 269998ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_8; 270098ca5289SRabin Vincent else if (maxburst >= 4) 270198ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_4; 270298ca5289SRabin Vincent else 270398ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_1; 270498ca5289SRabin Vincent } 270598ca5289SRabin Vincent 270698ca5289SRabin Vincent info->psize = psize; 270798ca5289SRabin Vincent info->flow_ctrl = STEDMA40_NO_FLOW_CTRL; 270898ca5289SRabin Vincent 270998ca5289SRabin Vincent return 0; 271098ca5289SRabin Vincent } 271198ca5289SRabin Vincent 271295e1400fSLinus Walleij /* Runtime reconfiguration extension */ 271398ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan, 271495e1400fSLinus Walleij struct dma_slave_config *config) 271595e1400fSLinus Walleij { 271695e1400fSLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 271795e1400fSLinus Walleij struct stedma40_chan_cfg *cfg = &d40c->dma_cfg; 271898ca5289SRabin Vincent enum dma_slave_buswidth src_addr_width, dst_addr_width; 271995e1400fSLinus Walleij dma_addr_t config_addr; 272098ca5289SRabin Vincent u32 src_maxburst, dst_maxburst; 272198ca5289SRabin Vincent int ret; 272298ca5289SRabin Vincent 272398ca5289SRabin Vincent src_addr_width = config->src_addr_width; 272498ca5289SRabin Vincent src_maxburst = config->src_maxburst; 272598ca5289SRabin Vincent dst_addr_width = config->dst_addr_width; 272698ca5289SRabin Vincent dst_maxburst = config->dst_maxburst; 272795e1400fSLinus Walleij 2728db8196dfSVinod Koul if (config->direction == DMA_DEV_TO_MEM) { 272995e1400fSLinus Walleij config_addr = config->src_addr; 2730ef9c89b3SLee Jones 27312c2b62d5SLee Jones if (cfg->dir != DMA_DEV_TO_MEM) 273295e1400fSLinus Walleij dev_dbg(d40c->base->dev, 273395e1400fSLinus Walleij "channel was not configured for peripheral " 273495e1400fSLinus Walleij "to memory transfer (%d) overriding\n", 273595e1400fSLinus Walleij cfg->dir); 27362c2b62d5SLee Jones cfg->dir = DMA_DEV_TO_MEM; 273795e1400fSLinus Walleij 273898ca5289SRabin Vincent /* Configure the memory side */ 273998ca5289SRabin Vincent if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 274098ca5289SRabin Vincent dst_addr_width = src_addr_width; 274198ca5289SRabin Vincent if (dst_maxburst == 0) 274298ca5289SRabin Vincent dst_maxburst = src_maxburst; 274395e1400fSLinus Walleij 2744db8196dfSVinod Koul } else if (config->direction == DMA_MEM_TO_DEV) { 274595e1400fSLinus Walleij config_addr = config->dst_addr; 2746ef9c89b3SLee Jones 27472c2b62d5SLee Jones if (cfg->dir != DMA_MEM_TO_DEV) 274895e1400fSLinus Walleij dev_dbg(d40c->base->dev, 274995e1400fSLinus Walleij "channel was not configured for memory " 275095e1400fSLinus Walleij "to peripheral transfer (%d) overriding\n", 275195e1400fSLinus Walleij cfg->dir); 27522c2b62d5SLee Jones cfg->dir = DMA_MEM_TO_DEV; 275395e1400fSLinus Walleij 275498ca5289SRabin Vincent /* Configure the memory side */ 275598ca5289SRabin Vincent if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 275698ca5289SRabin Vincent src_addr_width = dst_addr_width; 275798ca5289SRabin Vincent if (src_maxburst == 0) 275898ca5289SRabin Vincent src_maxburst = dst_maxburst; 275995e1400fSLinus Walleij } else { 276095e1400fSLinus Walleij dev_err(d40c->base->dev, 276195e1400fSLinus Walleij "unrecognized channel direction %d\n", 276295e1400fSLinus Walleij config->direction); 276398ca5289SRabin Vincent return -EINVAL; 276495e1400fSLinus Walleij } 276595e1400fSLinus Walleij 2766ef9c89b3SLee Jones if (config_addr <= 0) { 2767ef9c89b3SLee Jones dev_err(d40c->base->dev, "no address supplied\n"); 2768ef9c89b3SLee Jones return -EINVAL; 2769ef9c89b3SLee Jones } 2770ef9c89b3SLee Jones 277198ca5289SRabin Vincent if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) { 277295e1400fSLinus Walleij dev_err(d40c->base->dev, 277398ca5289SRabin Vincent "src/dst width/maxburst mismatch: %d*%d != %d*%d\n", 277498ca5289SRabin Vincent src_maxburst, 277598ca5289SRabin Vincent src_addr_width, 277698ca5289SRabin Vincent dst_maxburst, 277798ca5289SRabin Vincent dst_addr_width); 277898ca5289SRabin Vincent return -EINVAL; 277995e1400fSLinus Walleij } 278095e1400fSLinus Walleij 278192bb6cdbSPer Forlin if (src_maxburst > 16) { 278292bb6cdbSPer Forlin src_maxburst = 16; 278392bb6cdbSPer Forlin dst_maxburst = src_maxburst * src_addr_width / dst_addr_width; 278492bb6cdbSPer Forlin } else if (dst_maxburst > 16) { 278592bb6cdbSPer Forlin dst_maxburst = 16; 278692bb6cdbSPer Forlin src_maxburst = dst_maxburst * dst_addr_width / src_addr_width; 278792bb6cdbSPer Forlin } 278892bb6cdbSPer Forlin 278943f2e1a3SLee Jones /* Only valid widths are; 1, 2, 4 and 8. */ 279043f2e1a3SLee Jones if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED || 279143f2e1a3SLee Jones src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES || 279243f2e1a3SLee Jones dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED || 279343f2e1a3SLee Jones dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES || 279443f2e1a3SLee Jones ((src_addr_width > 1) && (src_addr_width & 1)) || 279543f2e1a3SLee Jones ((dst_addr_width > 1) && (dst_addr_width & 1))) 279643f2e1a3SLee Jones return -EINVAL; 279743f2e1a3SLee Jones 279843f2e1a3SLee Jones cfg->src_info.data_width = src_addr_width; 279943f2e1a3SLee Jones cfg->dst_info.data_width = dst_addr_width; 280043f2e1a3SLee Jones 280198ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->src_info, 280298ca5289SRabin Vincent src_maxburst); 280398ca5289SRabin Vincent if (ret) 280498ca5289SRabin Vincent return ret; 280595e1400fSLinus Walleij 280698ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info, 280798ca5289SRabin Vincent dst_maxburst); 280898ca5289SRabin Vincent if (ret) 280998ca5289SRabin Vincent return ret; 281095e1400fSLinus Walleij 2811a59670a4SPer Forlin /* Fill in register values */ 2812724a8577SRabin Vincent if (chan_is_logical(d40c)) 2813a59670a4SPer Forlin d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); 2814a59670a4SPer Forlin else 281557e65ad7SLee Jones d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg); 2816a59670a4SPer Forlin 281795e1400fSLinus Walleij /* These settings will take precedence later */ 281895e1400fSLinus Walleij d40c->runtime_addr = config_addr; 281995e1400fSLinus Walleij d40c->runtime_direction = config->direction; 282095e1400fSLinus Walleij dev_dbg(d40c->base->dev, 282198ca5289SRabin Vincent "configured channel %s for %s, data width %d/%d, " 282298ca5289SRabin Vincent "maxburst %d/%d elements, LE, no flow control\n", 282395e1400fSLinus Walleij dma_chan_name(chan), 2824db8196dfSVinod Koul (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX", 282598ca5289SRabin Vincent src_addr_width, dst_addr_width, 282698ca5289SRabin Vincent src_maxburst, dst_maxburst); 282798ca5289SRabin Vincent 282898ca5289SRabin Vincent return 0; 282995e1400fSLinus Walleij } 283095e1400fSLinus Walleij 283105827630SLinus Walleij static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 283205827630SLinus Walleij unsigned long arg) 28338d318a50SLinus Walleij { 28348d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 28358d318a50SLinus Walleij 28360d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 28376db5a8baSRabin Vincent chan_err(d40c, "Channel is not allocated!\n"); 28380d0f6b8bSJonas Aaberg return -EINVAL; 28390d0f6b8bSJonas Aaberg } 28400d0f6b8bSJonas Aaberg 28418d318a50SLinus Walleij switch (cmd) { 28428d318a50SLinus Walleij case DMA_TERMINATE_ALL: 28431bdae6f4SNarayanan G d40_terminate_all(chan); 28441bdae6f4SNarayanan G return 0; 28458d318a50SLinus Walleij case DMA_PAUSE: 284686eb5fb6SRabin Vincent return d40_pause(d40c); 28478d318a50SLinus Walleij case DMA_RESUME: 284886eb5fb6SRabin Vincent return d40_resume(d40c); 284995e1400fSLinus Walleij case DMA_SLAVE_CONFIG: 285098ca5289SRabin Vincent return d40_set_runtime_config(chan, 285195e1400fSLinus Walleij (struct dma_slave_config *) arg); 285295e1400fSLinus Walleij default: 285395e1400fSLinus Walleij break; 28548d318a50SLinus Walleij } 28558d318a50SLinus Walleij 28568d318a50SLinus Walleij /* Other commands are unimplemented */ 28578d318a50SLinus Walleij return -ENXIO; 28588d318a50SLinus Walleij } 28598d318a50SLinus Walleij 28608d318a50SLinus Walleij /* Initialization functions */ 28618d318a50SLinus Walleij 28628d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma, 28638d318a50SLinus Walleij struct d40_chan *chans, int offset, 28648d318a50SLinus Walleij int num_chans) 28658d318a50SLinus Walleij { 28668d318a50SLinus Walleij int i = 0; 28678d318a50SLinus Walleij struct d40_chan *d40c; 28688d318a50SLinus Walleij 28698d318a50SLinus Walleij INIT_LIST_HEAD(&dma->channels); 28708d318a50SLinus Walleij 28718d318a50SLinus Walleij for (i = offset; i < offset + num_chans; i++) { 28728d318a50SLinus Walleij d40c = &chans[i]; 28738d318a50SLinus Walleij d40c->base = base; 28748d318a50SLinus Walleij d40c->chan.device = dma; 28758d318a50SLinus Walleij 28768d318a50SLinus Walleij spin_lock_init(&d40c->lock); 28778d318a50SLinus Walleij 28788d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN; 28798d318a50SLinus Walleij 28804226dd86SFabio Baltieri INIT_LIST_HEAD(&d40c->done); 28818d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->active); 28828d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->queue); 2883a8f3067bSPer Forlin INIT_LIST_HEAD(&d40c->pending_queue); 28848d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->client); 288582babbb3SPer Forlin INIT_LIST_HEAD(&d40c->prepare_queue); 28868d318a50SLinus Walleij 28878d318a50SLinus Walleij tasklet_init(&d40c->tasklet, dma_tasklet, 28888d318a50SLinus Walleij (unsigned long) d40c); 28898d318a50SLinus Walleij 28908d318a50SLinus Walleij list_add_tail(&d40c->chan.device_node, 28918d318a50SLinus Walleij &dma->channels); 28928d318a50SLinus Walleij } 28938d318a50SLinus Walleij } 28948d318a50SLinus Walleij 28957ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev) 28967ad74a7cSRabin Vincent { 28977ad74a7cSRabin Vincent if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) 28987ad74a7cSRabin Vincent dev->device_prep_slave_sg = d40_prep_slave_sg; 28997ad74a7cSRabin Vincent 29007ad74a7cSRabin Vincent if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) { 29017ad74a7cSRabin Vincent dev->device_prep_dma_memcpy = d40_prep_memcpy; 29027ad74a7cSRabin Vincent 29037ad74a7cSRabin Vincent /* 29047ad74a7cSRabin Vincent * This controller can only access address at even 29057ad74a7cSRabin Vincent * 32bit boundaries, i.e. 2^2 29067ad74a7cSRabin Vincent */ 29077ad74a7cSRabin Vincent dev->copy_align = 2; 29087ad74a7cSRabin Vincent } 29097ad74a7cSRabin Vincent 29107ad74a7cSRabin Vincent if (dma_has_cap(DMA_SG, dev->cap_mask)) 29117ad74a7cSRabin Vincent dev->device_prep_dma_sg = d40_prep_memcpy_sg; 29127ad74a7cSRabin Vincent 29130c842b55SRabin Vincent if (dma_has_cap(DMA_CYCLIC, dev->cap_mask)) 29140c842b55SRabin Vincent dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic; 29150c842b55SRabin Vincent 29167ad74a7cSRabin Vincent dev->device_alloc_chan_resources = d40_alloc_chan_resources; 29177ad74a7cSRabin Vincent dev->device_free_chan_resources = d40_free_chan_resources; 29187ad74a7cSRabin Vincent dev->device_issue_pending = d40_issue_pending; 29197ad74a7cSRabin Vincent dev->device_tx_status = d40_tx_status; 29207ad74a7cSRabin Vincent dev->device_control = d40_control; 29217ad74a7cSRabin Vincent dev->dev = base->dev; 29227ad74a7cSRabin Vincent } 29237ad74a7cSRabin Vincent 29248d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base, 29258d318a50SLinus Walleij int num_reserved_chans) 29268d318a50SLinus Walleij { 29278d318a50SLinus Walleij int err ; 29288d318a50SLinus Walleij 29298d318a50SLinus Walleij d40_chan_init(base, &base->dma_slave, base->log_chans, 29308d318a50SLinus Walleij 0, base->num_log_chans); 29318d318a50SLinus Walleij 29328d318a50SLinus Walleij dma_cap_zero(base->dma_slave.cap_mask); 29338d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask); 29340c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); 29358d318a50SLinus Walleij 29367ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_slave); 29378d318a50SLinus Walleij 29388d318a50SLinus Walleij err = dma_async_device_register(&base->dma_slave); 29398d318a50SLinus Walleij 29408d318a50SLinus Walleij if (err) { 29416db5a8baSRabin Vincent d40_err(base->dev, "Failed to register slave channels\n"); 29428d318a50SLinus Walleij goto failure1; 29438d318a50SLinus Walleij } 29448d318a50SLinus Walleij 29458d318a50SLinus Walleij d40_chan_init(base, &base->dma_memcpy, base->log_chans, 2946a7dacb68SLee Jones base->num_log_chans, base->num_memcpy_chans); 29478d318a50SLinus Walleij 29488d318a50SLinus Walleij dma_cap_zero(base->dma_memcpy.cap_mask); 29498d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask); 29507ad74a7cSRabin Vincent dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask); 29518d318a50SLinus Walleij 29527ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_memcpy); 29538d318a50SLinus Walleij 29548d318a50SLinus Walleij err = dma_async_device_register(&base->dma_memcpy); 29558d318a50SLinus Walleij 29568d318a50SLinus Walleij if (err) { 29576db5a8baSRabin Vincent d40_err(base->dev, 29586db5a8baSRabin Vincent "Failed to regsiter memcpy only channels\n"); 29598d318a50SLinus Walleij goto failure2; 29608d318a50SLinus Walleij } 29618d318a50SLinus Walleij 29628d318a50SLinus Walleij d40_chan_init(base, &base->dma_both, base->phy_chans, 29638d318a50SLinus Walleij 0, num_reserved_chans); 29648d318a50SLinus Walleij 29658d318a50SLinus Walleij dma_cap_zero(base->dma_both.cap_mask); 29668d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask); 29678d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask); 29687ad74a7cSRabin Vincent dma_cap_set(DMA_SG, base->dma_both.cap_mask); 29690c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); 29708d318a50SLinus Walleij 29717ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_both); 29728d318a50SLinus Walleij err = dma_async_device_register(&base->dma_both); 29738d318a50SLinus Walleij 29748d318a50SLinus Walleij if (err) { 29756db5a8baSRabin Vincent d40_err(base->dev, 29766db5a8baSRabin Vincent "Failed to register logical and physical capable channels\n"); 29778d318a50SLinus Walleij goto failure3; 29788d318a50SLinus Walleij } 29798d318a50SLinus Walleij return 0; 29808d318a50SLinus Walleij failure3: 29818d318a50SLinus Walleij dma_async_device_unregister(&base->dma_memcpy); 29828d318a50SLinus Walleij failure2: 29838d318a50SLinus Walleij dma_async_device_unregister(&base->dma_slave); 29848d318a50SLinus Walleij failure1: 29858d318a50SLinus Walleij return err; 29868d318a50SLinus Walleij } 29878d318a50SLinus Walleij 29887fb3e75eSNarayanan G /* Suspend resume functionality */ 29897fb3e75eSNarayanan G #ifdef CONFIG_PM 29907fb3e75eSNarayanan G static int dma40_pm_suspend(struct device *dev) 29917fb3e75eSNarayanan G { 299228c7a19dSNarayanan G struct platform_device *pdev = to_platform_device(dev); 299328c7a19dSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 299428c7a19dSNarayanan G int ret = 0; 29957fb3e75eSNarayanan G 299628c7a19dSNarayanan G if (base->lcpa_regulator) 299728c7a19dSNarayanan G ret = regulator_disable(base->lcpa_regulator); 299828c7a19dSNarayanan G return ret; 29997fb3e75eSNarayanan G } 30007fb3e75eSNarayanan G 30017fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev) 30027fb3e75eSNarayanan G { 30037fb3e75eSNarayanan G struct platform_device *pdev = to_platform_device(dev); 30047fb3e75eSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 30057fb3e75eSNarayanan G 30067fb3e75eSNarayanan G d40_save_restore_registers(base, true); 30077fb3e75eSNarayanan G 30087fb3e75eSNarayanan G /* Don't disable/enable clocks for v1 due to HW bugs */ 30097fb3e75eSNarayanan G if (base->rev != 1) 30107fb3e75eSNarayanan G writel_relaxed(base->gcc_pwr_off_mask, 30117fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC); 30127fb3e75eSNarayanan G 30137fb3e75eSNarayanan G return 0; 30147fb3e75eSNarayanan G } 30157fb3e75eSNarayanan G 30167fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev) 30177fb3e75eSNarayanan G { 30187fb3e75eSNarayanan G struct platform_device *pdev = to_platform_device(dev); 30197fb3e75eSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 30207fb3e75eSNarayanan G 30217fb3e75eSNarayanan G if (base->initialized) 30227fb3e75eSNarayanan G d40_save_restore_registers(base, false); 30237fb3e75eSNarayanan G 30247fb3e75eSNarayanan G writel_relaxed(D40_DREG_GCC_ENABLE_ALL, 30257fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC); 30267fb3e75eSNarayanan G return 0; 30277fb3e75eSNarayanan G } 30287fb3e75eSNarayanan G 302928c7a19dSNarayanan G static int dma40_resume(struct device *dev) 303028c7a19dSNarayanan G { 303128c7a19dSNarayanan G struct platform_device *pdev = to_platform_device(dev); 303228c7a19dSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 303328c7a19dSNarayanan G int ret = 0; 303428c7a19dSNarayanan G 303528c7a19dSNarayanan G if (base->lcpa_regulator) 303628c7a19dSNarayanan G ret = regulator_enable(base->lcpa_regulator); 303728c7a19dSNarayanan G 303828c7a19dSNarayanan G return ret; 303928c7a19dSNarayanan G } 30407fb3e75eSNarayanan G 30417fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = { 30427fb3e75eSNarayanan G .suspend = dma40_pm_suspend, 30437fb3e75eSNarayanan G .runtime_suspend = dma40_runtime_suspend, 30447fb3e75eSNarayanan G .runtime_resume = dma40_runtime_resume, 304528c7a19dSNarayanan G .resume = dma40_resume, 30467fb3e75eSNarayanan G }; 30477fb3e75eSNarayanan G #define DMA40_PM_OPS (&dma40_pm_ops) 30487fb3e75eSNarayanan G #else 30497fb3e75eSNarayanan G #define DMA40_PM_OPS NULL 30507fb3e75eSNarayanan G #endif 30517fb3e75eSNarayanan G 30528d318a50SLinus Walleij /* Initialization functions. */ 30538d318a50SLinus Walleij 30548d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base) 30558d318a50SLinus Walleij { 30568d318a50SLinus Walleij int i; 30578d318a50SLinus Walleij int num_phy_chans_avail = 0; 30588d318a50SLinus Walleij u32 val[2]; 30598d318a50SLinus Walleij int odd_even_bit = -2; 30607fb3e75eSNarayanan G int gcc = D40_DREG_GCC_ENA; 30618d318a50SLinus Walleij 30628d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRSME); 30638d318a50SLinus Walleij val[1] = readl(base->virtbase + D40_DREG_PRSMO); 30648d318a50SLinus Walleij 30658d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 30668d318a50SLinus Walleij base->phy_res[i].num = i; 30678d318a50SLinus Walleij odd_even_bit += 2 * ((i % 2) == 0); 30688d318a50SLinus Walleij if (((val[i % 2] >> odd_even_bit) & 3) == 1) { 30698d318a50SLinus Walleij /* Mark security only channels as occupied */ 30708d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_PHY; 30718d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_PHY; 30727fb3e75eSNarayanan G base->phy_res[i].reserved = true; 30737fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), 30747fb3e75eSNarayanan G D40_DREG_GCC_SRC); 30757fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), 30767fb3e75eSNarayanan G D40_DREG_GCC_DST); 30777fb3e75eSNarayanan G 30787fb3e75eSNarayanan G 30798d318a50SLinus Walleij } else { 30808d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_FREE; 30818d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_FREE; 30827fb3e75eSNarayanan G base->phy_res[i].reserved = false; 30838d318a50SLinus Walleij num_phy_chans_avail++; 30848d318a50SLinus Walleij } 30858d318a50SLinus Walleij spin_lock_init(&base->phy_res[i].lock); 30868d318a50SLinus Walleij } 30876b7acd84SJonas Aaberg 30886b7acd84SJonas Aaberg /* Mark disabled channels as occupied */ 30896b7acd84SJonas Aaberg for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) { 3090f57b407cSRabin Vincent int chan = base->plat_data->disabled_channels[i]; 3091f57b407cSRabin Vincent 3092f57b407cSRabin Vincent base->phy_res[chan].allocated_src = D40_ALLOC_PHY; 3093f57b407cSRabin Vincent base->phy_res[chan].allocated_dst = D40_ALLOC_PHY; 30947fb3e75eSNarayanan G base->phy_res[chan].reserved = true; 30957fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), 30967fb3e75eSNarayanan G D40_DREG_GCC_SRC); 30977fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), 30987fb3e75eSNarayanan G D40_DREG_GCC_DST); 30996b7acd84SJonas Aaberg num_phy_chans_avail--; 31006b7acd84SJonas Aaberg } 31016b7acd84SJonas Aaberg 31027407048bSFabio Baltieri /* Mark soft_lli channels */ 31037407048bSFabio Baltieri for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) { 31047407048bSFabio Baltieri int chan = base->plat_data->soft_lli_chans[i]; 31057407048bSFabio Baltieri 31067407048bSFabio Baltieri base->phy_res[chan].use_soft_lli = true; 31077407048bSFabio Baltieri } 31087407048bSFabio Baltieri 31098d318a50SLinus Walleij dev_info(base->dev, "%d of %d physical DMA channels available\n", 31108d318a50SLinus Walleij num_phy_chans_avail, base->num_phy_chans); 31118d318a50SLinus Walleij 31128d318a50SLinus Walleij /* Verify settings extended vs standard */ 31138d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRTYP); 31148d318a50SLinus Walleij 31158d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 31168d318a50SLinus Walleij 31178d318a50SLinus Walleij if (base->phy_res[i].allocated_src == D40_ALLOC_FREE && 31188d318a50SLinus Walleij (val[0] & 0x3) != 1) 31198d318a50SLinus Walleij dev_info(base->dev, 31208d318a50SLinus Walleij "[%s] INFO: channel %d is misconfigured (%d)\n", 31218d318a50SLinus Walleij __func__, i, val[0] & 0x3); 31228d318a50SLinus Walleij 31238d318a50SLinus Walleij val[0] = val[0] >> 2; 31248d318a50SLinus Walleij } 31258d318a50SLinus Walleij 31267fb3e75eSNarayanan G /* 31277fb3e75eSNarayanan G * To keep things simple, Enable all clocks initially. 31287fb3e75eSNarayanan G * The clocks will get managed later post channel allocation. 31297fb3e75eSNarayanan G * The clocks for the event lines on which reserved channels exists 31307fb3e75eSNarayanan G * are not managed here. 31317fb3e75eSNarayanan G */ 31327fb3e75eSNarayanan G writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC); 31337fb3e75eSNarayanan G base->gcc_pwr_off_mask = gcc; 31347fb3e75eSNarayanan G 31358d318a50SLinus Walleij return num_phy_chans_avail; 31368d318a50SLinus Walleij } 31378d318a50SLinus Walleij 31388d318a50SLinus Walleij static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev) 31398d318a50SLinus Walleij { 3140bb75d93bSLee Jones struct stedma40_platform_data *plat_data = pdev->dev.platform_data; 31418d318a50SLinus Walleij struct clk *clk = NULL; 31428d318a50SLinus Walleij void __iomem *virtbase = NULL; 31438d318a50SLinus Walleij struct resource *res = NULL; 31448d318a50SLinus Walleij struct d40_base *base = NULL; 31458d318a50SLinus Walleij int num_log_chans = 0; 31468d318a50SLinus Walleij int num_phy_chans; 3147a7dacb68SLee Jones int num_memcpy_chans; 3148b707c658SUlf Hansson int clk_ret = -EINVAL; 31498d318a50SLinus Walleij int i; 3150f4b89764SLinus Walleij u32 pid; 3151f4b89764SLinus Walleij u32 cid; 3152f4b89764SLinus Walleij u8 rev; 31538d318a50SLinus Walleij 31548d318a50SLinus Walleij clk = clk_get(&pdev->dev, NULL); 31558d318a50SLinus Walleij if (IS_ERR(clk)) { 31566db5a8baSRabin Vincent d40_err(&pdev->dev, "No matching clock found\n"); 31578d318a50SLinus Walleij goto failure; 31588d318a50SLinus Walleij } 31598d318a50SLinus Walleij 3160b707c658SUlf Hansson clk_ret = clk_prepare_enable(clk); 3161b707c658SUlf Hansson if (clk_ret) { 3162b707c658SUlf Hansson d40_err(&pdev->dev, "Failed to prepare/enable clock\n"); 3163b707c658SUlf Hansson goto failure; 3164b707c658SUlf Hansson } 31658d318a50SLinus Walleij 31668d318a50SLinus Walleij /* Get IO for DMAC base address */ 31678d318a50SLinus Walleij res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base"); 31688d318a50SLinus Walleij if (!res) 31698d318a50SLinus Walleij goto failure; 31708d318a50SLinus Walleij 31718d318a50SLinus Walleij if (request_mem_region(res->start, resource_size(res), 31728d318a50SLinus Walleij D40_NAME " I/O base") == NULL) 31738d318a50SLinus Walleij goto failure; 31748d318a50SLinus Walleij 31758d318a50SLinus Walleij virtbase = ioremap(res->start, resource_size(res)); 31768d318a50SLinus Walleij if (!virtbase) 31778d318a50SLinus Walleij goto failure; 31788d318a50SLinus Walleij 3179f4b89764SLinus Walleij /* This is just a regular AMBA PrimeCell ID actually */ 3180f4b89764SLinus Walleij for (pid = 0, i = 0; i < 4; i++) 3181f4b89764SLinus Walleij pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i) 3182f4b89764SLinus Walleij & 255) << (i * 8); 3183f4b89764SLinus Walleij for (cid = 0, i = 0; i < 4; i++) 3184f4b89764SLinus Walleij cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i) 3185f4b89764SLinus Walleij & 255) << (i * 8); 3186f4b89764SLinus Walleij 3187f4b89764SLinus Walleij if (cid != AMBA_CID) { 3188f4b89764SLinus Walleij d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n"); 31898d318a50SLinus Walleij goto failure; 31908d318a50SLinus Walleij } 3191f4b89764SLinus Walleij if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) { 31926db5a8baSRabin Vincent d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n", 3193f4b89764SLinus Walleij AMBA_MANF_BITS(pid), 3194f4b89764SLinus Walleij AMBA_VENDOR_ST); 31958d318a50SLinus Walleij goto failure; 31968d318a50SLinus Walleij } 3197f4b89764SLinus Walleij /* 3198f4b89764SLinus Walleij * HW revision: 3199f4b89764SLinus Walleij * DB8500ed has revision 0 3200f4b89764SLinus Walleij * ? has revision 1 3201f4b89764SLinus Walleij * DB8500v1 has revision 2 3202f4b89764SLinus Walleij * DB8500v2 has revision 3 320347db92f4SGerald Baeza * AP9540v1 has revision 4 320447db92f4SGerald Baeza * DB8540v1 has revision 4 3205f4b89764SLinus Walleij */ 3206f4b89764SLinus Walleij rev = AMBA_REV_BITS(pid); 32078b2fe9b6SLee Jones if (rev < 2) { 32088b2fe9b6SLee Jones d40_err(&pdev->dev, "hardware revision: %d is not supported", rev); 32098b2fe9b6SLee Jones goto failure; 32108b2fe9b6SLee Jones } 32113ae0267fSJonas Aaberg 32128d318a50SLinus Walleij /* The number of physical channels on this HW */ 321347db92f4SGerald Baeza if (plat_data->num_of_phy_chans) 321447db92f4SGerald Baeza num_phy_chans = plat_data->num_of_phy_chans; 321547db92f4SGerald Baeza else 32168d318a50SLinus Walleij num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4; 32178d318a50SLinus Walleij 3218a7dacb68SLee Jones /* The number of channels used for memcpy */ 3219a7dacb68SLee Jones if (plat_data->num_of_memcpy_chans) 3220a7dacb68SLee Jones num_memcpy_chans = plat_data->num_of_memcpy_chans; 3221a7dacb68SLee Jones else 3222a7dacb68SLee Jones num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels); 3223a7dacb68SLee Jones 3224db72da92SLee Jones num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY; 3225db72da92SLee Jones 3226b2abb249SLee Jones dev_info(&pdev->dev, 3227b2abb249SLee Jones "hardware rev: %d @ 0x%x with %d physical and %d logical channels\n", 3228b2abb249SLee Jones rev, res->start, num_phy_chans, num_log_chans); 32298d318a50SLinus Walleij 32308d318a50SLinus Walleij base = kzalloc(ALIGN(sizeof(struct d40_base), 4) + 3231a7dacb68SLee Jones (num_phy_chans + num_log_chans + num_memcpy_chans) * 32328d318a50SLinus Walleij sizeof(struct d40_chan), GFP_KERNEL); 32338d318a50SLinus Walleij 32348d318a50SLinus Walleij if (base == NULL) { 32356db5a8baSRabin Vincent d40_err(&pdev->dev, "Out of memory\n"); 32368d318a50SLinus Walleij goto failure; 32378d318a50SLinus Walleij } 32388d318a50SLinus Walleij 32393ae0267fSJonas Aaberg base->rev = rev; 32408d318a50SLinus Walleij base->clk = clk; 3241a7dacb68SLee Jones base->num_memcpy_chans = num_memcpy_chans; 32428d318a50SLinus Walleij base->num_phy_chans = num_phy_chans; 32438d318a50SLinus Walleij base->num_log_chans = num_log_chans; 32448d318a50SLinus Walleij base->phy_start = res->start; 32458d318a50SLinus Walleij base->phy_size = resource_size(res); 32468d318a50SLinus Walleij base->virtbase = virtbase; 32478d318a50SLinus Walleij base->plat_data = plat_data; 32488d318a50SLinus Walleij base->dev = &pdev->dev; 32498d318a50SLinus Walleij base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4); 32508d318a50SLinus Walleij base->log_chans = &base->phy_chans[num_phy_chans]; 32518d318a50SLinus Walleij 32523cb645dcSTong Liu if (base->plat_data->num_of_phy_chans == 14) { 32533cb645dcSTong Liu base->gen_dmac.backup = d40_backup_regs_v4b; 32543cb645dcSTong Liu base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B; 32553cb645dcSTong Liu base->gen_dmac.interrupt_en = D40_DREG_CPCMIS; 32563cb645dcSTong Liu base->gen_dmac.interrupt_clear = D40_DREG_CPCICR; 32573cb645dcSTong Liu base->gen_dmac.realtime_en = D40_DREG_CRSEG1; 32583cb645dcSTong Liu base->gen_dmac.realtime_clear = D40_DREG_CRCEG1; 32593cb645dcSTong Liu base->gen_dmac.high_prio_en = D40_DREG_CPSEG1; 32603cb645dcSTong Liu base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1; 32613cb645dcSTong Liu base->gen_dmac.il = il_v4b; 32623cb645dcSTong Liu base->gen_dmac.il_size = ARRAY_SIZE(il_v4b); 32633cb645dcSTong Liu base->gen_dmac.init_reg = dma_init_reg_v4b; 32643cb645dcSTong Liu base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b); 32653cb645dcSTong Liu } else { 32663cb645dcSTong Liu if (base->rev >= 3) { 32673cb645dcSTong Liu base->gen_dmac.backup = d40_backup_regs_v4a; 32683cb645dcSTong Liu base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A; 32693cb645dcSTong Liu } 32703cb645dcSTong Liu base->gen_dmac.interrupt_en = D40_DREG_PCMIS; 32713cb645dcSTong Liu base->gen_dmac.interrupt_clear = D40_DREG_PCICR; 32723cb645dcSTong Liu base->gen_dmac.realtime_en = D40_DREG_RSEG1; 32733cb645dcSTong Liu base->gen_dmac.realtime_clear = D40_DREG_RCEG1; 32743cb645dcSTong Liu base->gen_dmac.high_prio_en = D40_DREG_PSEG1; 32753cb645dcSTong Liu base->gen_dmac.high_prio_clear = D40_DREG_PCEG1; 32763cb645dcSTong Liu base->gen_dmac.il = il_v4a; 32773cb645dcSTong Liu base->gen_dmac.il_size = ARRAY_SIZE(il_v4a); 32783cb645dcSTong Liu base->gen_dmac.init_reg = dma_init_reg_v4a; 32793cb645dcSTong Liu base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a); 32803cb645dcSTong Liu } 32813cb645dcSTong Liu 32828d318a50SLinus Walleij base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res), 32838d318a50SLinus Walleij GFP_KERNEL); 32848d318a50SLinus Walleij if (!base->phy_res) 32858d318a50SLinus Walleij goto failure; 32868d318a50SLinus Walleij 32878d318a50SLinus Walleij base->lookup_phy_chans = kzalloc(num_phy_chans * 32888d318a50SLinus Walleij sizeof(struct d40_chan *), 32898d318a50SLinus Walleij GFP_KERNEL); 32908d318a50SLinus Walleij if (!base->lookup_phy_chans) 32918d318a50SLinus Walleij goto failure; 32928d318a50SLinus Walleij 3293db72da92SLee Jones base->lookup_log_chans = kzalloc(num_log_chans * 32948d318a50SLinus Walleij sizeof(struct d40_chan *), 32958d318a50SLinus Walleij GFP_KERNEL); 32968d318a50SLinus Walleij if (!base->lookup_log_chans) 32978d318a50SLinus Walleij goto failure; 3298698e4732SJonas Aaberg 32997fb3e75eSNarayanan G base->reg_val_backup_chan = kmalloc(base->num_phy_chans * 33007fb3e75eSNarayanan G sizeof(d40_backup_regs_chan), 33018d318a50SLinus Walleij GFP_KERNEL); 33027fb3e75eSNarayanan G if (!base->reg_val_backup_chan) 33037fb3e75eSNarayanan G goto failure; 33047fb3e75eSNarayanan G 33057fb3e75eSNarayanan G base->lcla_pool.alloc_map = 33067fb3e75eSNarayanan G kzalloc(num_phy_chans * sizeof(struct d40_desc *) 33077fb3e75eSNarayanan G * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL); 33088d318a50SLinus Walleij if (!base->lcla_pool.alloc_map) 33098d318a50SLinus Walleij goto failure; 33108d318a50SLinus Walleij 3311c675b1b4SJonas Aaberg base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc), 3312c675b1b4SJonas Aaberg 0, SLAB_HWCACHE_ALIGN, 3313c675b1b4SJonas Aaberg NULL); 3314c675b1b4SJonas Aaberg if (base->desc_slab == NULL) 3315c675b1b4SJonas Aaberg goto failure; 3316c675b1b4SJonas Aaberg 33178d318a50SLinus Walleij return base; 33188d318a50SLinus Walleij 33198d318a50SLinus Walleij failure: 3320b707c658SUlf Hansson if (!clk_ret) 3321b707c658SUlf Hansson clk_disable_unprepare(clk); 3322b707c658SUlf Hansson if (!IS_ERR(clk)) 33238d318a50SLinus Walleij clk_put(clk); 33248d318a50SLinus Walleij if (virtbase) 33258d318a50SLinus Walleij iounmap(virtbase); 33268d318a50SLinus Walleij if (res) 33278d318a50SLinus Walleij release_mem_region(res->start, 33288d318a50SLinus Walleij resource_size(res)); 33298d318a50SLinus Walleij if (virtbase) 33308d318a50SLinus Walleij iounmap(virtbase); 33318d318a50SLinus Walleij 33328d318a50SLinus Walleij if (base) { 33338d318a50SLinus Walleij kfree(base->lcla_pool.alloc_map); 33341bdae6f4SNarayanan G kfree(base->reg_val_backup_chan); 33358d318a50SLinus Walleij kfree(base->lookup_log_chans); 33368d318a50SLinus Walleij kfree(base->lookup_phy_chans); 33378d318a50SLinus Walleij kfree(base->phy_res); 33388d318a50SLinus Walleij kfree(base); 33398d318a50SLinus Walleij } 33408d318a50SLinus Walleij 33418d318a50SLinus Walleij return NULL; 33428d318a50SLinus Walleij } 33438d318a50SLinus Walleij 33448d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base) 33458d318a50SLinus Walleij { 33468d318a50SLinus Walleij 33478d318a50SLinus Walleij int i; 33488d318a50SLinus Walleij u32 prmseo[2] = {0, 0}; 33498d318a50SLinus Walleij u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF}; 33508d318a50SLinus Walleij u32 pcmis = 0; 33518d318a50SLinus Walleij u32 pcicr = 0; 33523cb645dcSTong Liu struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg; 33533cb645dcSTong Liu u32 reg_size = base->gen_dmac.init_reg_size; 33548d318a50SLinus Walleij 33553cb645dcSTong Liu for (i = 0; i < reg_size; i++) 33568d318a50SLinus Walleij writel(dma_init_reg[i].val, 33578d318a50SLinus Walleij base->virtbase + dma_init_reg[i].reg); 33588d318a50SLinus Walleij 33598d318a50SLinus Walleij /* Configure all our dma channels to default settings */ 33608d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 33618d318a50SLinus Walleij 33628d318a50SLinus Walleij activeo[i % 2] = activeo[i % 2] << 2; 33638d318a50SLinus Walleij 33648d318a50SLinus Walleij if (base->phy_res[base->num_phy_chans - i - 1].allocated_src 33658d318a50SLinus Walleij == D40_ALLOC_PHY) { 33668d318a50SLinus Walleij activeo[i % 2] |= 3; 33678d318a50SLinus Walleij continue; 33688d318a50SLinus Walleij } 33698d318a50SLinus Walleij 33708d318a50SLinus Walleij /* Enable interrupt # */ 33718d318a50SLinus Walleij pcmis = (pcmis << 1) | 1; 33728d318a50SLinus Walleij 33738d318a50SLinus Walleij /* Clear interrupt # */ 33748d318a50SLinus Walleij pcicr = (pcicr << 1) | 1; 33758d318a50SLinus Walleij 33768d318a50SLinus Walleij /* Set channel to physical mode */ 33778d318a50SLinus Walleij prmseo[i % 2] = prmseo[i % 2] << 2; 33788d318a50SLinus Walleij prmseo[i % 2] |= 1; 33798d318a50SLinus Walleij 33808d318a50SLinus Walleij } 33818d318a50SLinus Walleij 33828d318a50SLinus Walleij writel(prmseo[1], base->virtbase + D40_DREG_PRMSE); 33838d318a50SLinus Walleij writel(prmseo[0], base->virtbase + D40_DREG_PRMSO); 33848d318a50SLinus Walleij writel(activeo[1], base->virtbase + D40_DREG_ACTIVE); 33858d318a50SLinus Walleij writel(activeo[0], base->virtbase + D40_DREG_ACTIVO); 33868d318a50SLinus Walleij 33878d318a50SLinus Walleij /* Write which interrupt to enable */ 33883cb645dcSTong Liu writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en); 33898d318a50SLinus Walleij 33908d318a50SLinus Walleij /* Write which interrupt to clear */ 33913cb645dcSTong Liu writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear); 33928d318a50SLinus Walleij 33933cb645dcSTong Liu /* These are __initdata and cannot be accessed after init */ 33943cb645dcSTong Liu base->gen_dmac.init_reg = NULL; 33953cb645dcSTong Liu base->gen_dmac.init_reg_size = 0; 33968d318a50SLinus Walleij } 33978d318a50SLinus Walleij 3398508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base) 3399508849adSLinus Walleij { 3400026cbc42SRabin Vincent struct d40_lcla_pool *pool = &base->lcla_pool; 3401508849adSLinus Walleij unsigned long *page_list; 3402508849adSLinus Walleij int i, j; 3403508849adSLinus Walleij int ret = 0; 3404508849adSLinus Walleij 3405508849adSLinus Walleij /* 3406508849adSLinus Walleij * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned, 3407508849adSLinus Walleij * To full fill this hardware requirement without wasting 256 kb 3408508849adSLinus Walleij * we allocate pages until we get an aligned one. 3409508849adSLinus Walleij */ 3410508849adSLinus Walleij page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS, 3411508849adSLinus Walleij GFP_KERNEL); 3412508849adSLinus Walleij 3413508849adSLinus Walleij if (!page_list) { 3414508849adSLinus Walleij ret = -ENOMEM; 3415508849adSLinus Walleij goto failure; 3416508849adSLinus Walleij } 3417508849adSLinus Walleij 3418508849adSLinus Walleij /* Calculating how many pages that are required */ 3419508849adSLinus Walleij base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE; 3420508849adSLinus Walleij 3421508849adSLinus Walleij for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) { 3422508849adSLinus Walleij page_list[i] = __get_free_pages(GFP_KERNEL, 3423508849adSLinus Walleij base->lcla_pool.pages); 3424508849adSLinus Walleij if (!page_list[i]) { 3425508849adSLinus Walleij 34266db5a8baSRabin Vincent d40_err(base->dev, "Failed to allocate %d pages.\n", 34276db5a8baSRabin Vincent base->lcla_pool.pages); 3428508849adSLinus Walleij 3429508849adSLinus Walleij for (j = 0; j < i; j++) 3430508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages); 3431508849adSLinus Walleij goto failure; 3432508849adSLinus Walleij } 3433508849adSLinus Walleij 3434508849adSLinus Walleij if ((virt_to_phys((void *)page_list[i]) & 3435508849adSLinus Walleij (LCLA_ALIGNMENT - 1)) == 0) 3436508849adSLinus Walleij break; 3437508849adSLinus Walleij } 3438508849adSLinus Walleij 3439508849adSLinus Walleij for (j = 0; j < i; j++) 3440508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages); 3441508849adSLinus Walleij 3442508849adSLinus Walleij if (i < MAX_LCLA_ALLOC_ATTEMPTS) { 3443508849adSLinus Walleij base->lcla_pool.base = (void *)page_list[i]; 3444508849adSLinus Walleij } else { 3445767a9675SJonas Aaberg /* 3446767a9675SJonas Aaberg * After many attempts and no succees with finding the correct 3447767a9675SJonas Aaberg * alignment, try with allocating a big buffer. 3448767a9675SJonas Aaberg */ 3449508849adSLinus Walleij dev_warn(base->dev, 3450508849adSLinus Walleij "[%s] Failed to get %d pages @ 18 bit align.\n", 3451508849adSLinus Walleij __func__, base->lcla_pool.pages); 3452508849adSLinus Walleij base->lcla_pool.base_unaligned = kmalloc(SZ_1K * 3453508849adSLinus Walleij base->num_phy_chans + 3454508849adSLinus Walleij LCLA_ALIGNMENT, 3455508849adSLinus Walleij GFP_KERNEL); 3456508849adSLinus Walleij if (!base->lcla_pool.base_unaligned) { 3457508849adSLinus Walleij ret = -ENOMEM; 3458508849adSLinus Walleij goto failure; 3459508849adSLinus Walleij } 3460508849adSLinus Walleij 3461508849adSLinus Walleij base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned, 3462508849adSLinus Walleij LCLA_ALIGNMENT); 3463508849adSLinus Walleij } 3464508849adSLinus Walleij 3465026cbc42SRabin Vincent pool->dma_addr = dma_map_single(base->dev, pool->base, 3466026cbc42SRabin Vincent SZ_1K * base->num_phy_chans, 3467026cbc42SRabin Vincent DMA_TO_DEVICE); 3468026cbc42SRabin Vincent if (dma_mapping_error(base->dev, pool->dma_addr)) { 3469026cbc42SRabin Vincent pool->dma_addr = 0; 3470026cbc42SRabin Vincent ret = -ENOMEM; 3471026cbc42SRabin Vincent goto failure; 3472026cbc42SRabin Vincent } 3473026cbc42SRabin Vincent 3474508849adSLinus Walleij writel(virt_to_phys(base->lcla_pool.base), 3475508849adSLinus Walleij base->virtbase + D40_DREG_LCLA); 3476508849adSLinus Walleij failure: 3477508849adSLinus Walleij kfree(page_list); 3478508849adSLinus Walleij return ret; 3479508849adSLinus Walleij } 3480508849adSLinus Walleij 34811814a170SLee Jones static int __init d40_of_probe(struct platform_device *pdev, 34821814a170SLee Jones struct device_node *np) 34831814a170SLee Jones { 34841814a170SLee Jones struct stedma40_platform_data *pdata; 3485499c2bc3SLee Jones int num_phy = 0, num_memcpy = 0, num_disabled = 0; 3486a7dacb68SLee Jones const const __be32 *list; 34871814a170SLee Jones 34881814a170SLee Jones pdata = devm_kzalloc(&pdev->dev, 34891814a170SLee Jones sizeof(struct stedma40_platform_data), 34901814a170SLee Jones GFP_KERNEL); 34911814a170SLee Jones if (!pdata) 34921814a170SLee Jones return -ENOMEM; 34931814a170SLee Jones 3494fd59f9e6SLee Jones /* If absent this value will be obtained from h/w. */ 3495fd59f9e6SLee Jones of_property_read_u32(np, "dma-channels", &num_phy); 3496fd59f9e6SLee Jones if (num_phy > 0) 3497fd59f9e6SLee Jones pdata->num_of_phy_chans = num_phy; 3498fd59f9e6SLee Jones 3499a7dacb68SLee Jones list = of_get_property(np, "memcpy-channels", &num_memcpy); 3500a7dacb68SLee Jones num_memcpy /= sizeof(*list); 3501a7dacb68SLee Jones 3502a7dacb68SLee Jones if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) { 3503a7dacb68SLee Jones d40_err(&pdev->dev, 3504a7dacb68SLee Jones "Invalid number of memcpy channels specified (%d)\n", 3505a7dacb68SLee Jones num_memcpy); 3506a7dacb68SLee Jones return -EINVAL; 3507a7dacb68SLee Jones } 3508a7dacb68SLee Jones pdata->num_of_memcpy_chans = num_memcpy; 3509a7dacb68SLee Jones 3510a7dacb68SLee Jones of_property_read_u32_array(np, "memcpy-channels", 3511a7dacb68SLee Jones dma40_memcpy_channels, 3512a7dacb68SLee Jones num_memcpy); 3513a7dacb68SLee Jones 3514499c2bc3SLee Jones list = of_get_property(np, "disabled-channels", &num_disabled); 3515499c2bc3SLee Jones num_disabled /= sizeof(*list); 3516499c2bc3SLee Jones 3517499c2bc3SLee Jones if (num_disabled > STEDMA40_MAX_PHYS || num_disabled < 0) { 3518499c2bc3SLee Jones d40_err(&pdev->dev, 3519499c2bc3SLee Jones "Invalid number of disabled channels specified (%d)\n", 3520499c2bc3SLee Jones num_disabled); 3521499c2bc3SLee Jones return -EINVAL; 3522499c2bc3SLee Jones } 3523499c2bc3SLee Jones 3524499c2bc3SLee Jones of_property_read_u32_array(np, "disabled-channels", 3525499c2bc3SLee Jones pdata->disabled_channels, 3526499c2bc3SLee Jones num_disabled); 3527499c2bc3SLee Jones pdata->disabled_channels[num_disabled] = -1; 3528499c2bc3SLee Jones 35291814a170SLee Jones pdev->dev.platform_data = pdata; 35301814a170SLee Jones 35311814a170SLee Jones return 0; 35321814a170SLee Jones } 35331814a170SLee Jones 35348d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev) 35358d318a50SLinus Walleij { 35361814a170SLee Jones struct stedma40_platform_data *plat_data = pdev->dev.platform_data; 35371814a170SLee Jones struct device_node *np = pdev->dev.of_node; 35388d318a50SLinus Walleij int ret = -ENOENT; 35391814a170SLee Jones struct d40_base *base = NULL; 35408d318a50SLinus Walleij struct resource *res = NULL; 35418d318a50SLinus Walleij int num_reserved_chans; 35428d318a50SLinus Walleij u32 val; 35438d318a50SLinus Walleij 35441814a170SLee Jones if (!plat_data) { 35451814a170SLee Jones if (np) { 35461814a170SLee Jones if(d40_of_probe(pdev, np)) { 35471814a170SLee Jones ret = -ENOMEM; 35481814a170SLee Jones goto failure; 35491814a170SLee Jones } 35501814a170SLee Jones } else { 35511814a170SLee Jones d40_err(&pdev->dev, "No pdata or Device Tree provided\n"); 35521814a170SLee Jones goto failure; 35531814a170SLee Jones } 35541814a170SLee Jones } 35558d318a50SLinus Walleij 35561814a170SLee Jones base = d40_hw_detect_init(pdev); 35578d318a50SLinus Walleij if (!base) 35588d318a50SLinus Walleij goto failure; 35598d318a50SLinus Walleij 35608d318a50SLinus Walleij num_reserved_chans = d40_phy_res_init(base); 35618d318a50SLinus Walleij 35628d318a50SLinus Walleij platform_set_drvdata(pdev, base); 35638d318a50SLinus Walleij 35648d318a50SLinus Walleij spin_lock_init(&base->interrupt_lock); 35658d318a50SLinus Walleij spin_lock_init(&base->execmd_lock); 35668d318a50SLinus Walleij 35678d318a50SLinus Walleij /* Get IO for logical channel parameter address */ 35688d318a50SLinus Walleij res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa"); 35698d318a50SLinus Walleij if (!res) { 35708d318a50SLinus Walleij ret = -ENOENT; 35716db5a8baSRabin Vincent d40_err(&pdev->dev, "No \"lcpa\" memory resource\n"); 35728d318a50SLinus Walleij goto failure; 35738d318a50SLinus Walleij } 35748d318a50SLinus Walleij base->lcpa_size = resource_size(res); 35758d318a50SLinus Walleij base->phy_lcpa = res->start; 35768d318a50SLinus Walleij 35778d318a50SLinus Walleij if (request_mem_region(res->start, resource_size(res), 35788d318a50SLinus Walleij D40_NAME " I/O lcpa") == NULL) { 35798d318a50SLinus Walleij ret = -EBUSY; 35806db5a8baSRabin Vincent d40_err(&pdev->dev, 35816db5a8baSRabin Vincent "Failed to request LCPA region 0x%x-0x%x\n", 35826db5a8baSRabin Vincent res->start, res->end); 35838d318a50SLinus Walleij goto failure; 35848d318a50SLinus Walleij } 35858d318a50SLinus Walleij 35868d318a50SLinus Walleij /* We make use of ESRAM memory for this. */ 35878d318a50SLinus Walleij val = readl(base->virtbase + D40_DREG_LCPA); 35888d318a50SLinus Walleij if (res->start != val && val != 0) { 35898d318a50SLinus Walleij dev_warn(&pdev->dev, 35908d318a50SLinus Walleij "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n", 35918d318a50SLinus Walleij __func__, val, res->start); 35928d318a50SLinus Walleij } else 35938d318a50SLinus Walleij writel(res->start, base->virtbase + D40_DREG_LCPA); 35948d318a50SLinus Walleij 35958d318a50SLinus Walleij base->lcpa_base = ioremap(res->start, resource_size(res)); 35968d318a50SLinus Walleij if (!base->lcpa_base) { 35978d318a50SLinus Walleij ret = -ENOMEM; 35986db5a8baSRabin Vincent d40_err(&pdev->dev, "Failed to ioremap LCPA region\n"); 35998d318a50SLinus Walleij goto failure; 36008d318a50SLinus Walleij } 360128c7a19dSNarayanan G /* If lcla has to be located in ESRAM we don't need to allocate */ 360228c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) { 360328c7a19dSNarayanan G res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 360428c7a19dSNarayanan G "lcla_esram"); 360528c7a19dSNarayanan G if (!res) { 360628c7a19dSNarayanan G ret = -ENOENT; 360728c7a19dSNarayanan G d40_err(&pdev->dev, 360828c7a19dSNarayanan G "No \"lcla_esram\" memory resource\n"); 360928c7a19dSNarayanan G goto failure; 361028c7a19dSNarayanan G } 361128c7a19dSNarayanan G base->lcla_pool.base = ioremap(res->start, 361228c7a19dSNarayanan G resource_size(res)); 361328c7a19dSNarayanan G if (!base->lcla_pool.base) { 361428c7a19dSNarayanan G ret = -ENOMEM; 361528c7a19dSNarayanan G d40_err(&pdev->dev, "Failed to ioremap LCLA region\n"); 361628c7a19dSNarayanan G goto failure; 361728c7a19dSNarayanan G } 361828c7a19dSNarayanan G writel(res->start, base->virtbase + D40_DREG_LCLA); 3619508849adSLinus Walleij 362028c7a19dSNarayanan G } else { 3621508849adSLinus Walleij ret = d40_lcla_allocate(base); 3622508849adSLinus Walleij if (ret) { 36236db5a8baSRabin Vincent d40_err(&pdev->dev, "Failed to allocate LCLA area\n"); 36248d318a50SLinus Walleij goto failure; 36258d318a50SLinus Walleij } 362628c7a19dSNarayanan G } 36278d318a50SLinus Walleij 36288d318a50SLinus Walleij spin_lock_init(&base->lcla_pool.lock); 36298d318a50SLinus Walleij 36308d318a50SLinus Walleij base->irq = platform_get_irq(pdev, 0); 36318d318a50SLinus Walleij 36328d318a50SLinus Walleij ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base); 36338d318a50SLinus Walleij if (ret) { 36346db5a8baSRabin Vincent d40_err(&pdev->dev, "No IRQ defined\n"); 36358d318a50SLinus Walleij goto failure; 36368d318a50SLinus Walleij } 36378d318a50SLinus Walleij 36387fb3e75eSNarayanan G pm_runtime_irq_safe(base->dev); 36397fb3e75eSNarayanan G pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY); 36407fb3e75eSNarayanan G pm_runtime_use_autosuspend(base->dev); 36417fb3e75eSNarayanan G pm_runtime_enable(base->dev); 36427fb3e75eSNarayanan G pm_runtime_resume(base->dev); 364328c7a19dSNarayanan G 364428c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) { 364528c7a19dSNarayanan G 364628c7a19dSNarayanan G base->lcpa_regulator = regulator_get(base->dev, "lcla_esram"); 364728c7a19dSNarayanan G if (IS_ERR(base->lcpa_regulator)) { 364828c7a19dSNarayanan G d40_err(&pdev->dev, "Failed to get lcpa_regulator\n"); 36498581bbcdSWei Yongjun ret = PTR_ERR(base->lcpa_regulator); 365028c7a19dSNarayanan G base->lcpa_regulator = NULL; 365128c7a19dSNarayanan G goto failure; 365228c7a19dSNarayanan G } 365328c7a19dSNarayanan G 365428c7a19dSNarayanan G ret = regulator_enable(base->lcpa_regulator); 365528c7a19dSNarayanan G if (ret) { 365628c7a19dSNarayanan G d40_err(&pdev->dev, 365728c7a19dSNarayanan G "Failed to enable lcpa_regulator\n"); 365828c7a19dSNarayanan G regulator_put(base->lcpa_regulator); 365928c7a19dSNarayanan G base->lcpa_regulator = NULL; 366028c7a19dSNarayanan G goto failure; 366128c7a19dSNarayanan G } 366228c7a19dSNarayanan G } 366328c7a19dSNarayanan G 36647fb3e75eSNarayanan G base->initialized = true; 36658581bbcdSWei Yongjun ret = d40_dmaengine_init(base, num_reserved_chans); 36668581bbcdSWei Yongjun if (ret) 36678d318a50SLinus Walleij goto failure; 36688d318a50SLinus Walleij 3669b96710e5SPer Forlin base->dev->dma_parms = &base->dma_parms; 36708581bbcdSWei Yongjun ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE); 36718581bbcdSWei Yongjun if (ret) { 3672b96710e5SPer Forlin d40_err(&pdev->dev, "Failed to set dma max seg size\n"); 3673b96710e5SPer Forlin goto failure; 3674b96710e5SPer Forlin } 3675b96710e5SPer Forlin 36768d318a50SLinus Walleij d40_hw_init(base); 36778d318a50SLinus Walleij 3678fa332de5SLee Jones if (np) { 36798581bbcdSWei Yongjun ret = of_dma_controller_register(np, d40_xlate, NULL); 36808581bbcdSWei Yongjun if (ret) 3681fa332de5SLee Jones dev_err(&pdev->dev, 3682fa332de5SLee Jones "could not register of_dma_controller\n"); 3683fa332de5SLee Jones } 3684fa332de5SLee Jones 36858d318a50SLinus Walleij dev_info(base->dev, "initialized\n"); 36868d318a50SLinus Walleij return 0; 36878d318a50SLinus Walleij 36888d318a50SLinus Walleij failure: 36898d318a50SLinus Walleij if (base) { 3690c675b1b4SJonas Aaberg if (base->desc_slab) 3691c675b1b4SJonas Aaberg kmem_cache_destroy(base->desc_slab); 36928d318a50SLinus Walleij if (base->virtbase) 36938d318a50SLinus Walleij iounmap(base->virtbase); 3694026cbc42SRabin Vincent 369528c7a19dSNarayanan G if (base->lcla_pool.base && base->plat_data->use_esram_lcla) { 369628c7a19dSNarayanan G iounmap(base->lcla_pool.base); 369728c7a19dSNarayanan G base->lcla_pool.base = NULL; 369828c7a19dSNarayanan G } 369928c7a19dSNarayanan G 3700026cbc42SRabin Vincent if (base->lcla_pool.dma_addr) 3701026cbc42SRabin Vincent dma_unmap_single(base->dev, base->lcla_pool.dma_addr, 3702026cbc42SRabin Vincent SZ_1K * base->num_phy_chans, 3703026cbc42SRabin Vincent DMA_TO_DEVICE); 3704026cbc42SRabin Vincent 3705508849adSLinus Walleij if (!base->lcla_pool.base_unaligned && base->lcla_pool.base) 3706508849adSLinus Walleij free_pages((unsigned long)base->lcla_pool.base, 3707508849adSLinus Walleij base->lcla_pool.pages); 3708767a9675SJonas Aaberg 3709508849adSLinus Walleij kfree(base->lcla_pool.base_unaligned); 3710767a9675SJonas Aaberg 37118d318a50SLinus Walleij if (base->phy_lcpa) 37128d318a50SLinus Walleij release_mem_region(base->phy_lcpa, 37138d318a50SLinus Walleij base->lcpa_size); 37148d318a50SLinus Walleij if (base->phy_start) 37158d318a50SLinus Walleij release_mem_region(base->phy_start, 37168d318a50SLinus Walleij base->phy_size); 37178d318a50SLinus Walleij if (base->clk) { 3718da2ac56aSFabio Baltieri clk_disable_unprepare(base->clk); 37198d318a50SLinus Walleij clk_put(base->clk); 37208d318a50SLinus Walleij } 37218d318a50SLinus Walleij 372228c7a19dSNarayanan G if (base->lcpa_regulator) { 372328c7a19dSNarayanan G regulator_disable(base->lcpa_regulator); 372428c7a19dSNarayanan G regulator_put(base->lcpa_regulator); 372528c7a19dSNarayanan G } 372628c7a19dSNarayanan G 37278d318a50SLinus Walleij kfree(base->lcla_pool.alloc_map); 37288d318a50SLinus Walleij kfree(base->lookup_log_chans); 37298d318a50SLinus Walleij kfree(base->lookup_phy_chans); 37308d318a50SLinus Walleij kfree(base->phy_res); 37318d318a50SLinus Walleij kfree(base); 37328d318a50SLinus Walleij } 37338d318a50SLinus Walleij 37346db5a8baSRabin Vincent d40_err(&pdev->dev, "probe failed\n"); 37358d318a50SLinus Walleij return ret; 37368d318a50SLinus Walleij } 37378d318a50SLinus Walleij 37381814a170SLee Jones static const struct of_device_id d40_match[] = { 37391814a170SLee Jones { .compatible = "stericsson,dma40", }, 37401814a170SLee Jones {} 37411814a170SLee Jones }; 37421814a170SLee Jones 37438d318a50SLinus Walleij static struct platform_driver d40_driver = { 37448d318a50SLinus Walleij .driver = { 37458d318a50SLinus Walleij .owner = THIS_MODULE, 37468d318a50SLinus Walleij .name = D40_NAME, 37477fb3e75eSNarayanan G .pm = DMA40_PM_OPS, 37481814a170SLee Jones .of_match_table = d40_match, 37498d318a50SLinus Walleij }, 37508d318a50SLinus Walleij }; 37518d318a50SLinus Walleij 3752cb9ab2d8SRabin Vincent static int __init stedma40_init(void) 37538d318a50SLinus Walleij { 37548d318a50SLinus Walleij return platform_driver_probe(&d40_driver, d40_probe); 37558d318a50SLinus Walleij } 3756a0eb221aSLinus Walleij subsys_initcall(stedma40_init); 3757