18d318a50SLinus Walleij /* 2d49278e3SPer Forlin * Copyright (C) Ericsson AB 2007-2008 3d49278e3SPer Forlin * Copyright (C) ST-Ericsson SA 2008-2010 4661385f9SPer Forlin * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson 5767a9675SJonas Aaberg * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson 68d318a50SLinus Walleij * License terms: GNU General Public License (GPL) version 2 78d318a50SLinus Walleij */ 88d318a50SLinus Walleij 9b7f080cfSAlexey Dobriyan #include <linux/dma-mapping.h> 108d318a50SLinus Walleij #include <linux/kernel.h> 118d318a50SLinus Walleij #include <linux/slab.h> 12f492b210SPaul Gortmaker #include <linux/export.h> 138d318a50SLinus Walleij #include <linux/dmaengine.h> 148d318a50SLinus Walleij #include <linux/platform_device.h> 158d318a50SLinus Walleij #include <linux/clk.h> 168d318a50SLinus Walleij #include <linux/delay.h> 177fb3e75eSNarayanan G #include <linux/pm.h> 187fb3e75eSNarayanan G #include <linux/pm_runtime.h> 19698e4732SJonas Aaberg #include <linux/err.h> 20f4b89764SLinus Walleij #include <linux/amba/bus.h> 2115e4b78dSLinus Walleij #include <linux/regulator/consumer.h> 22*865fab60SLinus Walleij #include <linux/platform_data/dma-ste-dma40.h> 238d318a50SLinus Walleij 24d2ebfb33SRussell King - ARM Linux #include "dmaengine.h" 258d318a50SLinus Walleij #include "ste_dma40_ll.h" 268d318a50SLinus Walleij 278d318a50SLinus Walleij #define D40_NAME "dma40" 288d318a50SLinus Walleij 298d318a50SLinus Walleij #define D40_PHY_CHAN -1 308d318a50SLinus Walleij 318d318a50SLinus Walleij /* For masking out/in 2 bit channel positions */ 328d318a50SLinus Walleij #define D40_CHAN_POS(chan) (2 * (chan / 2)) 338d318a50SLinus Walleij #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan)) 348d318a50SLinus Walleij 358d318a50SLinus Walleij /* Maximum iterations taken before giving up suspending a channel */ 368d318a50SLinus Walleij #define D40_SUSPEND_MAX_IT 500 378d318a50SLinus Walleij 387fb3e75eSNarayanan G /* Milliseconds */ 397fb3e75eSNarayanan G #define DMA40_AUTOSUSPEND_DELAY 100 407fb3e75eSNarayanan G 41508849adSLinus Walleij /* Hardware requirement on LCLA alignment */ 42508849adSLinus Walleij #define LCLA_ALIGNMENT 0x40000 43698e4732SJonas Aaberg 44698e4732SJonas Aaberg /* Max number of links per event group */ 45698e4732SJonas Aaberg #define D40_LCLA_LINK_PER_EVENT_GRP 128 46698e4732SJonas Aaberg #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP 47698e4732SJonas Aaberg 48508849adSLinus Walleij /* Attempts before giving up to trying to get pages that are aligned */ 49508849adSLinus Walleij #define MAX_LCLA_ALLOC_ATTEMPTS 256 50508849adSLinus Walleij 51508849adSLinus Walleij /* Bit markings for allocation map */ 528d318a50SLinus Walleij #define D40_ALLOC_FREE (1 << 31) 538d318a50SLinus Walleij #define D40_ALLOC_PHY (1 << 30) 548d318a50SLinus Walleij #define D40_ALLOC_LOG_FREE 0 558d318a50SLinus Walleij 568d318a50SLinus Walleij /** 578d318a50SLinus Walleij * enum 40_command - The different commands and/or statuses. 588d318a50SLinus Walleij * 598d318a50SLinus Walleij * @D40_DMA_STOP: DMA channel command STOP or status STOPPED, 608d318a50SLinus Walleij * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN. 618d318a50SLinus Walleij * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible. 628d318a50SLinus Walleij * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED. 638d318a50SLinus Walleij */ 648d318a50SLinus Walleij enum d40_command { 658d318a50SLinus Walleij D40_DMA_STOP = 0, 668d318a50SLinus Walleij D40_DMA_RUN = 1, 678d318a50SLinus Walleij D40_DMA_SUSPEND_REQ = 2, 688d318a50SLinus Walleij D40_DMA_SUSPENDED = 3 698d318a50SLinus Walleij }; 708d318a50SLinus Walleij 717fb3e75eSNarayanan G /* 721bdae6f4SNarayanan G * enum d40_events - The different Event Enables for the event lines. 731bdae6f4SNarayanan G * 741bdae6f4SNarayanan G * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan. 751bdae6f4SNarayanan G * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan. 761bdae6f4SNarayanan G * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line. 771bdae6f4SNarayanan G * @D40_ROUND_EVENTLINE: Status check for event line. 781bdae6f4SNarayanan G */ 791bdae6f4SNarayanan G 801bdae6f4SNarayanan G enum d40_events { 811bdae6f4SNarayanan G D40_DEACTIVATE_EVENTLINE = 0, 821bdae6f4SNarayanan G D40_ACTIVATE_EVENTLINE = 1, 831bdae6f4SNarayanan G D40_SUSPEND_REQ_EVENTLINE = 2, 841bdae6f4SNarayanan G D40_ROUND_EVENTLINE = 3 851bdae6f4SNarayanan G }; 861bdae6f4SNarayanan G 871bdae6f4SNarayanan G /* 887fb3e75eSNarayanan G * These are the registers that has to be saved and later restored 897fb3e75eSNarayanan G * when the DMA hw is powered off. 907fb3e75eSNarayanan G * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works. 917fb3e75eSNarayanan G */ 927fb3e75eSNarayanan G static u32 d40_backup_regs[] = { 937fb3e75eSNarayanan G D40_DREG_LCPA, 947fb3e75eSNarayanan G D40_DREG_LCLA, 957fb3e75eSNarayanan G D40_DREG_PRMSE, 967fb3e75eSNarayanan G D40_DREG_PRMSO, 977fb3e75eSNarayanan G D40_DREG_PRMOE, 987fb3e75eSNarayanan G D40_DREG_PRMOO, 997fb3e75eSNarayanan G }; 1007fb3e75eSNarayanan G 1017fb3e75eSNarayanan G #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs) 1027fb3e75eSNarayanan G 1037fb3e75eSNarayanan G /* TODO: Check if all these registers have to be saved/restored on dma40 v3 */ 1047fb3e75eSNarayanan G static u32 d40_backup_regs_v3[] = { 1057fb3e75eSNarayanan G D40_DREG_PSEG1, 1067fb3e75eSNarayanan G D40_DREG_PSEG2, 1077fb3e75eSNarayanan G D40_DREG_PSEG3, 1087fb3e75eSNarayanan G D40_DREG_PSEG4, 1097fb3e75eSNarayanan G D40_DREG_PCEG1, 1107fb3e75eSNarayanan G D40_DREG_PCEG2, 1117fb3e75eSNarayanan G D40_DREG_PCEG3, 1127fb3e75eSNarayanan G D40_DREG_PCEG4, 1137fb3e75eSNarayanan G D40_DREG_RSEG1, 1147fb3e75eSNarayanan G D40_DREG_RSEG2, 1157fb3e75eSNarayanan G D40_DREG_RSEG3, 1167fb3e75eSNarayanan G D40_DREG_RSEG4, 1177fb3e75eSNarayanan G D40_DREG_RCEG1, 1187fb3e75eSNarayanan G D40_DREG_RCEG2, 1197fb3e75eSNarayanan G D40_DREG_RCEG3, 1207fb3e75eSNarayanan G D40_DREG_RCEG4, 1217fb3e75eSNarayanan G }; 1227fb3e75eSNarayanan G 1237fb3e75eSNarayanan G #define BACKUP_REGS_SZ_V3 ARRAY_SIZE(d40_backup_regs_v3) 1247fb3e75eSNarayanan G 1257fb3e75eSNarayanan G static u32 d40_backup_regs_chan[] = { 1267fb3e75eSNarayanan G D40_CHAN_REG_SSCFG, 1277fb3e75eSNarayanan G D40_CHAN_REG_SSELT, 1287fb3e75eSNarayanan G D40_CHAN_REG_SSPTR, 1297fb3e75eSNarayanan G D40_CHAN_REG_SSLNK, 1307fb3e75eSNarayanan G D40_CHAN_REG_SDCFG, 1317fb3e75eSNarayanan G D40_CHAN_REG_SDELT, 1327fb3e75eSNarayanan G D40_CHAN_REG_SDPTR, 1337fb3e75eSNarayanan G D40_CHAN_REG_SDLNK, 1347fb3e75eSNarayanan G }; 1357fb3e75eSNarayanan G 1368d318a50SLinus Walleij /** 1378d318a50SLinus Walleij * struct d40_lli_pool - Structure for keeping LLIs in memory 1388d318a50SLinus Walleij * 1398d318a50SLinus Walleij * @base: Pointer to memory area when the pre_alloc_lli's are not large 1408d318a50SLinus Walleij * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if 1418d318a50SLinus Walleij * pre_alloc_lli is used. 142b00f938cSRabin Vincent * @dma_addr: DMA address, if mapped 1438d318a50SLinus Walleij * @size: The size in bytes of the memory at base or the size of pre_alloc_lli. 1448d318a50SLinus Walleij * @pre_alloc_lli: Pre allocated area for the most common case of transfers, 1458d318a50SLinus Walleij * one buffer to one buffer. 1468d318a50SLinus Walleij */ 1478d318a50SLinus Walleij struct d40_lli_pool { 1488d318a50SLinus Walleij void *base; 1498d318a50SLinus Walleij int size; 150b00f938cSRabin Vincent dma_addr_t dma_addr; 1518d318a50SLinus Walleij /* Space for dst and src, plus an extra for padding */ 1528d318a50SLinus Walleij u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)]; 1538d318a50SLinus Walleij }; 1548d318a50SLinus Walleij 1558d318a50SLinus Walleij /** 1568d318a50SLinus Walleij * struct d40_desc - A descriptor is one DMA job. 1578d318a50SLinus Walleij * 1588d318a50SLinus Walleij * @lli_phy: LLI settings for physical channel. Both src and dst= 1598d318a50SLinus Walleij * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if 1608d318a50SLinus Walleij * lli_len equals one. 1618d318a50SLinus Walleij * @lli_log: Same as above but for logical channels. 1628d318a50SLinus Walleij * @lli_pool: The pool with two entries pre-allocated. 163941b77a3SPer Friden * @lli_len: Number of llis of current descriptor. 16425985edcSLucas De Marchi * @lli_current: Number of transferred llis. 165698e4732SJonas Aaberg * @lcla_alloc: Number of LCLA entries allocated. 1668d318a50SLinus Walleij * @txd: DMA engine struct. Used for among other things for communication 1678d318a50SLinus Walleij * during a transfer. 1688d318a50SLinus Walleij * @node: List entry. 1698d318a50SLinus Walleij * @is_in_client_list: true if the client owns this descriptor. 1707fb3e75eSNarayanan G * @cyclic: true if this is a cyclic job 1718d318a50SLinus Walleij * 1728d318a50SLinus Walleij * This descriptor is used for both logical and physical transfers. 1738d318a50SLinus Walleij */ 1748d318a50SLinus Walleij struct d40_desc { 1758d318a50SLinus Walleij /* LLI physical */ 1768d318a50SLinus Walleij struct d40_phy_lli_bidir lli_phy; 1778d318a50SLinus Walleij /* LLI logical */ 1788d318a50SLinus Walleij struct d40_log_lli_bidir lli_log; 1798d318a50SLinus Walleij 1808d318a50SLinus Walleij struct d40_lli_pool lli_pool; 181941b77a3SPer Friden int lli_len; 182698e4732SJonas Aaberg int lli_current; 183698e4732SJonas Aaberg int lcla_alloc; 1848d318a50SLinus Walleij 1858d318a50SLinus Walleij struct dma_async_tx_descriptor txd; 1868d318a50SLinus Walleij struct list_head node; 1878d318a50SLinus Walleij 1888d318a50SLinus Walleij bool is_in_client_list; 1890c842b55SRabin Vincent bool cyclic; 1908d318a50SLinus Walleij }; 1918d318a50SLinus Walleij 1928d318a50SLinus Walleij /** 1938d318a50SLinus Walleij * struct d40_lcla_pool - LCLA pool settings and data. 1948d318a50SLinus Walleij * 195508849adSLinus Walleij * @base: The virtual address of LCLA. 18 bit aligned. 196508849adSLinus Walleij * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used. 197508849adSLinus Walleij * This pointer is only there for clean-up on error. 198508849adSLinus Walleij * @pages: The number of pages needed for all physical channels. 199508849adSLinus Walleij * Only used later for clean-up on error 2008d318a50SLinus Walleij * @lock: Lock to protect the content in this struct. 201698e4732SJonas Aaberg * @alloc_map: big map over which LCLA entry is own by which job. 2028d318a50SLinus Walleij */ 2038d318a50SLinus Walleij struct d40_lcla_pool { 2048d318a50SLinus Walleij void *base; 205026cbc42SRabin Vincent dma_addr_t dma_addr; 206508849adSLinus Walleij void *base_unaligned; 207508849adSLinus Walleij int pages; 2088d318a50SLinus Walleij spinlock_t lock; 209698e4732SJonas Aaberg struct d40_desc **alloc_map; 2108d318a50SLinus Walleij }; 2118d318a50SLinus Walleij 2128d318a50SLinus Walleij /** 2138d318a50SLinus Walleij * struct d40_phy_res - struct for handling eventlines mapped to physical 2148d318a50SLinus Walleij * channels. 2158d318a50SLinus Walleij * 2168d318a50SLinus Walleij * @lock: A lock protection this entity. 2177fb3e75eSNarayanan G * @reserved: True if used by secure world or otherwise. 2188d318a50SLinus Walleij * @num: The physical channel number of this entity. 2198d318a50SLinus Walleij * @allocated_src: Bit mapped to show which src event line's are mapped to 2208d318a50SLinus Walleij * this physical channel. Can also be free or physically allocated. 2218d318a50SLinus Walleij * @allocated_dst: Same as for src but is dst. 2228d318a50SLinus Walleij * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as 223767a9675SJonas Aaberg * event line number. 2248d318a50SLinus Walleij */ 2258d318a50SLinus Walleij struct d40_phy_res { 2268d318a50SLinus Walleij spinlock_t lock; 2277fb3e75eSNarayanan G bool reserved; 2288d318a50SLinus Walleij int num; 2298d318a50SLinus Walleij u32 allocated_src; 2308d318a50SLinus Walleij u32 allocated_dst; 2318d318a50SLinus Walleij }; 2328d318a50SLinus Walleij 2338d318a50SLinus Walleij struct d40_base; 2348d318a50SLinus Walleij 2358d318a50SLinus Walleij /** 2368d318a50SLinus Walleij * struct d40_chan - Struct that describes a channel. 2378d318a50SLinus Walleij * 2388d318a50SLinus Walleij * @lock: A spinlock to protect this struct. 2398d318a50SLinus Walleij * @log_num: The logical number, if any of this channel. 2408d318a50SLinus Walleij * @pending_tx: The number of pending transfers. Used between interrupt handler 2418d318a50SLinus Walleij * and tasklet. 2428d318a50SLinus Walleij * @busy: Set to true when transfer is ongoing on this channel. 2432a614340SJonas Aaberg * @phy_chan: Pointer to physical channel which this instance runs on. If this 2442a614340SJonas Aaberg * point is NULL, then the channel is not allocated. 2458d318a50SLinus Walleij * @chan: DMA engine handle. 2468d318a50SLinus Walleij * @tasklet: Tasklet that gets scheduled from interrupt context to complete a 2478d318a50SLinus Walleij * transfer and call client callback. 2488d318a50SLinus Walleij * @client: Cliented owned descriptor list. 249da063d26SPer Forlin * @pending_queue: Submitted jobs, to be issued by issue_pending() 2508d318a50SLinus Walleij * @active: Active descriptor. 2518d318a50SLinus Walleij * @queue: Queued jobs. 25282babbb3SPer Forlin * @prepare_queue: Prepared jobs. 2538d318a50SLinus Walleij * @dma_cfg: The client configuration of this dma channel. 254ce2ca125SRabin Vincent * @configured: whether the dma_cfg configuration is valid 2558d318a50SLinus Walleij * @base: Pointer to the device instance struct. 2568d318a50SLinus Walleij * @src_def_cfg: Default cfg register setting for src. 2578d318a50SLinus Walleij * @dst_def_cfg: Default cfg register setting for dst. 2588d318a50SLinus Walleij * @log_def: Default logical channel settings. 2598d318a50SLinus Walleij * @lcpa: Pointer to dst and src lcpa settings. 260ae752bf4Som prakash * @runtime_addr: runtime configured address. 261ae752bf4Som prakash * @runtime_direction: runtime configured direction. 2628d318a50SLinus Walleij * 2638d318a50SLinus Walleij * This struct can either "be" a logical or a physical channel. 2648d318a50SLinus Walleij */ 2658d318a50SLinus Walleij struct d40_chan { 2668d318a50SLinus Walleij spinlock_t lock; 2678d318a50SLinus Walleij int log_num; 2688d318a50SLinus Walleij int pending_tx; 2698d318a50SLinus Walleij bool busy; 2708d318a50SLinus Walleij struct d40_phy_res *phy_chan; 2718d318a50SLinus Walleij struct dma_chan chan; 2728d318a50SLinus Walleij struct tasklet_struct tasklet; 2738d318a50SLinus Walleij struct list_head client; 274a8f3067bSPer Forlin struct list_head pending_queue; 2758d318a50SLinus Walleij struct list_head active; 2768d318a50SLinus Walleij struct list_head queue; 27782babbb3SPer Forlin struct list_head prepare_queue; 2788d318a50SLinus Walleij struct stedma40_chan_cfg dma_cfg; 279ce2ca125SRabin Vincent bool configured; 2808d318a50SLinus Walleij struct d40_base *base; 2818d318a50SLinus Walleij /* Default register configurations */ 2828d318a50SLinus Walleij u32 src_def_cfg; 2838d318a50SLinus Walleij u32 dst_def_cfg; 2848d318a50SLinus Walleij struct d40_def_lcsp log_def; 2858d318a50SLinus Walleij struct d40_log_lli_full *lcpa; 28695e1400fSLinus Walleij /* Runtime reconfiguration */ 28795e1400fSLinus Walleij dma_addr_t runtime_addr; 288db8196dfSVinod Koul enum dma_transfer_direction runtime_direction; 2898d318a50SLinus Walleij }; 2908d318a50SLinus Walleij 2918d318a50SLinus Walleij /** 2928d318a50SLinus Walleij * struct d40_base - The big global struct, one for each probe'd instance. 2938d318a50SLinus Walleij * 2948d318a50SLinus Walleij * @interrupt_lock: Lock used to make sure one interrupt is handle a time. 2958d318a50SLinus Walleij * @execmd_lock: Lock for execute command usage since several channels share 2968d318a50SLinus Walleij * the same physical register. 2978d318a50SLinus Walleij * @dev: The device structure. 2988d318a50SLinus Walleij * @virtbase: The virtual base address of the DMA's register. 299f4185592SLinus Walleij * @rev: silicon revision detected. 3008d318a50SLinus Walleij * @clk: Pointer to the DMA clock structure. 3018d318a50SLinus Walleij * @phy_start: Physical memory start of the DMA registers. 3028d318a50SLinus Walleij * @phy_size: Size of the DMA register map. 3038d318a50SLinus Walleij * @irq: The IRQ number. 3048d318a50SLinus Walleij * @num_phy_chans: The number of physical channels. Read from HW. This 3058d318a50SLinus Walleij * is the number of available channels for this driver, not counting "Secure 3068d318a50SLinus Walleij * mode" allocated physical channels. 3078d318a50SLinus Walleij * @num_log_chans: The number of logical channels. Calculated from 3088d318a50SLinus Walleij * num_phy_chans. 3098d318a50SLinus Walleij * @dma_both: dma_device channels that can do both memcpy and slave transfers. 3108d318a50SLinus Walleij * @dma_slave: dma_device channels that can do only do slave transfers. 3118d318a50SLinus Walleij * @dma_memcpy: dma_device channels that can do only do memcpy transfers. 3127fb3e75eSNarayanan G * @phy_chans: Room for all possible physical channels in system. 3138d318a50SLinus Walleij * @log_chans: Room for all possible logical channels in system. 3148d318a50SLinus Walleij * @lookup_log_chans: Used to map interrupt number to logical channel. Points 3158d318a50SLinus Walleij * to log_chans entries. 3168d318a50SLinus Walleij * @lookup_phy_chans: Used to map interrupt number to physical channel. Points 3178d318a50SLinus Walleij * to phy_chans entries. 3188d318a50SLinus Walleij * @plat_data: Pointer to provided platform_data which is the driver 3198d318a50SLinus Walleij * configuration. 32028c7a19dSNarayanan G * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla. 3218d318a50SLinus Walleij * @phy_res: Vector containing all physical channels. 3228d318a50SLinus Walleij * @lcla_pool: lcla pool settings and data. 3238d318a50SLinus Walleij * @lcpa_base: The virtual mapped address of LCPA. 3248d318a50SLinus Walleij * @phy_lcpa: The physical address of the LCPA. 3258d318a50SLinus Walleij * @lcpa_size: The size of the LCPA area. 326c675b1b4SJonas Aaberg * @desc_slab: cache for descriptors. 3277fb3e75eSNarayanan G * @reg_val_backup: Here the values of some hardware registers are stored 3287fb3e75eSNarayanan G * before the DMA is powered off. They are restored when the power is back on. 3297fb3e75eSNarayanan G * @reg_val_backup_v3: Backup of registers that only exits on dma40 v3 and 3307fb3e75eSNarayanan G * later. 3317fb3e75eSNarayanan G * @reg_val_backup_chan: Backup data for standard channel parameter registers. 3327fb3e75eSNarayanan G * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off. 3337fb3e75eSNarayanan G * @initialized: true if the dma has been initialized 3348d318a50SLinus Walleij */ 3358d318a50SLinus Walleij struct d40_base { 3368d318a50SLinus Walleij spinlock_t interrupt_lock; 3378d318a50SLinus Walleij spinlock_t execmd_lock; 3388d318a50SLinus Walleij struct device *dev; 3398d318a50SLinus Walleij void __iomem *virtbase; 340f4185592SLinus Walleij u8 rev:4; 3418d318a50SLinus Walleij struct clk *clk; 3428d318a50SLinus Walleij phys_addr_t phy_start; 3438d318a50SLinus Walleij resource_size_t phy_size; 3448d318a50SLinus Walleij int irq; 3458d318a50SLinus Walleij int num_phy_chans; 3468d318a50SLinus Walleij int num_log_chans; 3478d318a50SLinus Walleij struct dma_device dma_both; 3488d318a50SLinus Walleij struct dma_device dma_slave; 3498d318a50SLinus Walleij struct dma_device dma_memcpy; 3508d318a50SLinus Walleij struct d40_chan *phy_chans; 3518d318a50SLinus Walleij struct d40_chan *log_chans; 3528d318a50SLinus Walleij struct d40_chan **lookup_log_chans; 3538d318a50SLinus Walleij struct d40_chan **lookup_phy_chans; 3548d318a50SLinus Walleij struct stedma40_platform_data *plat_data; 35528c7a19dSNarayanan G struct regulator *lcpa_regulator; 3568d318a50SLinus Walleij /* Physical half channels */ 3578d318a50SLinus Walleij struct d40_phy_res *phy_res; 3588d318a50SLinus Walleij struct d40_lcla_pool lcla_pool; 3598d318a50SLinus Walleij void *lcpa_base; 3608d318a50SLinus Walleij dma_addr_t phy_lcpa; 3618d318a50SLinus Walleij resource_size_t lcpa_size; 362c675b1b4SJonas Aaberg struct kmem_cache *desc_slab; 3637fb3e75eSNarayanan G u32 reg_val_backup[BACKUP_REGS_SZ]; 3647fb3e75eSNarayanan G u32 reg_val_backup_v3[BACKUP_REGS_SZ_V3]; 3657fb3e75eSNarayanan G u32 *reg_val_backup_chan; 3667fb3e75eSNarayanan G u16 gcc_pwr_off_mask; 3677fb3e75eSNarayanan G bool initialized; 3688d318a50SLinus Walleij }; 3698d318a50SLinus Walleij 3708d318a50SLinus Walleij /** 3718d318a50SLinus Walleij * struct d40_interrupt_lookup - lookup table for interrupt handler 3728d318a50SLinus Walleij * 3738d318a50SLinus Walleij * @src: Interrupt mask register. 3748d318a50SLinus Walleij * @clr: Interrupt clear register. 3758d318a50SLinus Walleij * @is_error: true if this is an error interrupt. 3768d318a50SLinus Walleij * @offset: start delta in the lookup_log_chans in d40_base. If equals to 3778d318a50SLinus Walleij * D40_PHY_CHAN, the lookup_phy_chans shall be used instead. 3788d318a50SLinus Walleij */ 3798d318a50SLinus Walleij struct d40_interrupt_lookup { 3808d318a50SLinus Walleij u32 src; 3818d318a50SLinus Walleij u32 clr; 3828d318a50SLinus Walleij bool is_error; 3838d318a50SLinus Walleij int offset; 3848d318a50SLinus Walleij }; 3858d318a50SLinus Walleij 3868d318a50SLinus Walleij /** 3878d318a50SLinus Walleij * struct d40_reg_val - simple lookup struct 3888d318a50SLinus Walleij * 3898d318a50SLinus Walleij * @reg: The register. 3908d318a50SLinus Walleij * @val: The value that belongs to the register in reg. 3918d318a50SLinus Walleij */ 3928d318a50SLinus Walleij struct d40_reg_val { 3938d318a50SLinus Walleij unsigned int reg; 3948d318a50SLinus Walleij unsigned int val; 3958d318a50SLinus Walleij }; 3968d318a50SLinus Walleij 397262d2915SRabin Vincent static struct device *chan2dev(struct d40_chan *d40c) 398262d2915SRabin Vincent { 399262d2915SRabin Vincent return &d40c->chan.dev->device; 400262d2915SRabin Vincent } 401262d2915SRabin Vincent 402724a8577SRabin Vincent static bool chan_is_physical(struct d40_chan *chan) 403724a8577SRabin Vincent { 404724a8577SRabin Vincent return chan->log_num == D40_PHY_CHAN; 405724a8577SRabin Vincent } 406724a8577SRabin Vincent 407724a8577SRabin Vincent static bool chan_is_logical(struct d40_chan *chan) 408724a8577SRabin Vincent { 409724a8577SRabin Vincent return !chan_is_physical(chan); 410724a8577SRabin Vincent } 411724a8577SRabin Vincent 4128ca84687SRabin Vincent static void __iomem *chan_base(struct d40_chan *chan) 4138ca84687SRabin Vincent { 4148ca84687SRabin Vincent return chan->base->virtbase + D40_DREG_PCBASE + 4158ca84687SRabin Vincent chan->phy_chan->num * D40_DREG_PCDELTA; 4168ca84687SRabin Vincent } 4178ca84687SRabin Vincent 4186db5a8baSRabin Vincent #define d40_err(dev, format, arg...) \ 4196db5a8baSRabin Vincent dev_err(dev, "[%s] " format, __func__, ## arg) 4206db5a8baSRabin Vincent 4216db5a8baSRabin Vincent #define chan_err(d40c, format, arg...) \ 4226db5a8baSRabin Vincent d40_err(chan2dev(d40c), format, ## arg) 4236db5a8baSRabin Vincent 424b00f938cSRabin Vincent static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d, 425dbd88788SRabin Vincent int lli_len) 4268d318a50SLinus Walleij { 427dbd88788SRabin Vincent bool is_log = chan_is_logical(d40c); 4288d318a50SLinus Walleij u32 align; 4298d318a50SLinus Walleij void *base; 4308d318a50SLinus Walleij 4318d318a50SLinus Walleij if (is_log) 4328d318a50SLinus Walleij align = sizeof(struct d40_log_lli); 4338d318a50SLinus Walleij else 4348d318a50SLinus Walleij align = sizeof(struct d40_phy_lli); 4358d318a50SLinus Walleij 4368d318a50SLinus Walleij if (lli_len == 1) { 4378d318a50SLinus Walleij base = d40d->lli_pool.pre_alloc_lli; 4388d318a50SLinus Walleij d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli); 4398d318a50SLinus Walleij d40d->lli_pool.base = NULL; 4408d318a50SLinus Walleij } else { 441594ece4dSRabin Vincent d40d->lli_pool.size = lli_len * 2 * align; 4428d318a50SLinus Walleij 4438d318a50SLinus Walleij base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT); 4448d318a50SLinus Walleij d40d->lli_pool.base = base; 4458d318a50SLinus Walleij 4468d318a50SLinus Walleij if (d40d->lli_pool.base == NULL) 4478d318a50SLinus Walleij return -ENOMEM; 4488d318a50SLinus Walleij } 4498d318a50SLinus Walleij 4508d318a50SLinus Walleij if (is_log) { 451d924abadSRabin Vincent d40d->lli_log.src = PTR_ALIGN(base, align); 452594ece4dSRabin Vincent d40d->lli_log.dst = d40d->lli_log.src + lli_len; 453b00f938cSRabin Vincent 454b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0; 4558d318a50SLinus Walleij } else { 456d924abadSRabin Vincent d40d->lli_phy.src = PTR_ALIGN(base, align); 457594ece4dSRabin Vincent d40d->lli_phy.dst = d40d->lli_phy.src + lli_len; 458b00f938cSRabin Vincent 459b00f938cSRabin Vincent d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev, 460b00f938cSRabin Vincent d40d->lli_phy.src, 461b00f938cSRabin Vincent d40d->lli_pool.size, 462b00f938cSRabin Vincent DMA_TO_DEVICE); 463b00f938cSRabin Vincent 464b00f938cSRabin Vincent if (dma_mapping_error(d40c->base->dev, 465b00f938cSRabin Vincent d40d->lli_pool.dma_addr)) { 466b00f938cSRabin Vincent kfree(d40d->lli_pool.base); 467b00f938cSRabin Vincent d40d->lli_pool.base = NULL; 468b00f938cSRabin Vincent d40d->lli_pool.dma_addr = 0; 469b00f938cSRabin Vincent return -ENOMEM; 470b00f938cSRabin Vincent } 4718d318a50SLinus Walleij } 4728d318a50SLinus Walleij 4738d318a50SLinus Walleij return 0; 4748d318a50SLinus Walleij } 4758d318a50SLinus Walleij 476b00f938cSRabin Vincent static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d) 4778d318a50SLinus Walleij { 478b00f938cSRabin Vincent if (d40d->lli_pool.dma_addr) 479b00f938cSRabin Vincent dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr, 480b00f938cSRabin Vincent d40d->lli_pool.size, DMA_TO_DEVICE); 481b00f938cSRabin Vincent 4828d318a50SLinus Walleij kfree(d40d->lli_pool.base); 4838d318a50SLinus Walleij d40d->lli_pool.base = NULL; 4848d318a50SLinus Walleij d40d->lli_pool.size = 0; 4858d318a50SLinus Walleij d40d->lli_log.src = NULL; 4868d318a50SLinus Walleij d40d->lli_log.dst = NULL; 4878d318a50SLinus Walleij d40d->lli_phy.src = NULL; 4888d318a50SLinus Walleij d40d->lli_phy.dst = NULL; 4898d318a50SLinus Walleij } 4908d318a50SLinus Walleij 491698e4732SJonas Aaberg static int d40_lcla_alloc_one(struct d40_chan *d40c, 492698e4732SJonas Aaberg struct d40_desc *d40d) 493698e4732SJonas Aaberg { 494698e4732SJonas Aaberg unsigned long flags; 495698e4732SJonas Aaberg int i; 496698e4732SJonas Aaberg int ret = -EINVAL; 497698e4732SJonas Aaberg int p; 498698e4732SJonas Aaberg 499698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); 500698e4732SJonas Aaberg 501698e4732SJonas Aaberg p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP; 502698e4732SJonas Aaberg 503698e4732SJonas Aaberg /* 504698e4732SJonas Aaberg * Allocate both src and dst at the same time, therefore the half 505698e4732SJonas Aaberg * start on 1 since 0 can't be used since zero is used as end marker. 506698e4732SJonas Aaberg */ 507698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { 508698e4732SJonas Aaberg if (!d40c->base->lcla_pool.alloc_map[p + i]) { 509698e4732SJonas Aaberg d40c->base->lcla_pool.alloc_map[p + i] = d40d; 510698e4732SJonas Aaberg d40d->lcla_alloc++; 511698e4732SJonas Aaberg ret = i; 512698e4732SJonas Aaberg break; 513698e4732SJonas Aaberg } 514698e4732SJonas Aaberg } 515698e4732SJonas Aaberg 516698e4732SJonas Aaberg spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); 517698e4732SJonas Aaberg 518698e4732SJonas Aaberg return ret; 519698e4732SJonas Aaberg } 520698e4732SJonas Aaberg 521698e4732SJonas Aaberg static int d40_lcla_free_all(struct d40_chan *d40c, 522698e4732SJonas Aaberg struct d40_desc *d40d) 523698e4732SJonas Aaberg { 524698e4732SJonas Aaberg unsigned long flags; 525698e4732SJonas Aaberg int i; 526698e4732SJonas Aaberg int ret = -EINVAL; 527698e4732SJonas Aaberg 528724a8577SRabin Vincent if (chan_is_physical(d40c)) 529698e4732SJonas Aaberg return 0; 530698e4732SJonas Aaberg 531698e4732SJonas Aaberg spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); 532698e4732SJonas Aaberg 533698e4732SJonas Aaberg for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { 534698e4732SJonas Aaberg if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * 535698e4732SJonas Aaberg D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) { 536698e4732SJonas Aaberg d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * 537698e4732SJonas Aaberg D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL; 538698e4732SJonas Aaberg d40d->lcla_alloc--; 539698e4732SJonas Aaberg if (d40d->lcla_alloc == 0) { 540698e4732SJonas Aaberg ret = 0; 541698e4732SJonas Aaberg break; 542698e4732SJonas Aaberg } 543698e4732SJonas Aaberg } 544698e4732SJonas Aaberg } 545698e4732SJonas Aaberg 546698e4732SJonas Aaberg spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); 547698e4732SJonas Aaberg 548698e4732SJonas Aaberg return ret; 549698e4732SJonas Aaberg 550698e4732SJonas Aaberg } 551698e4732SJonas Aaberg 5528d318a50SLinus Walleij static void d40_desc_remove(struct d40_desc *d40d) 5538d318a50SLinus Walleij { 5548d318a50SLinus Walleij list_del(&d40d->node); 5558d318a50SLinus Walleij } 5568d318a50SLinus Walleij 5578d318a50SLinus Walleij static struct d40_desc *d40_desc_get(struct d40_chan *d40c) 5588d318a50SLinus Walleij { 559a2c15fa4SRabin Vincent struct d40_desc *desc = NULL; 560a2c15fa4SRabin Vincent 561a2c15fa4SRabin Vincent if (!list_empty(&d40c->client)) { 5628d318a50SLinus Walleij struct d40_desc *d; 5638d318a50SLinus Walleij struct d40_desc *_d; 5648d318a50SLinus Walleij 5657fb3e75eSNarayanan G list_for_each_entry_safe(d, _d, &d40c->client, node) { 5668d318a50SLinus Walleij if (async_tx_test_ack(&d->txd)) { 5678d318a50SLinus Walleij d40_desc_remove(d); 568a2c15fa4SRabin Vincent desc = d; 569a2c15fa4SRabin Vincent memset(desc, 0, sizeof(*desc)); 570c675b1b4SJonas Aaberg break; 5718d318a50SLinus Walleij } 5728d318a50SLinus Walleij } 5737fb3e75eSNarayanan G } 574a2c15fa4SRabin Vincent 575a2c15fa4SRabin Vincent if (!desc) 576a2c15fa4SRabin Vincent desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT); 577a2c15fa4SRabin Vincent 578a2c15fa4SRabin Vincent if (desc) 579a2c15fa4SRabin Vincent INIT_LIST_HEAD(&desc->node); 580a2c15fa4SRabin Vincent 581a2c15fa4SRabin Vincent return desc; 5828d318a50SLinus Walleij } 5838d318a50SLinus Walleij 5848d318a50SLinus Walleij static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d) 5858d318a50SLinus Walleij { 586698e4732SJonas Aaberg 587b00f938cSRabin Vincent d40_pool_lli_free(d40c, d40d); 588698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 589c675b1b4SJonas Aaberg kmem_cache_free(d40c->base->desc_slab, d40d); 5908d318a50SLinus Walleij } 5918d318a50SLinus Walleij 5928d318a50SLinus Walleij static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc) 5938d318a50SLinus Walleij { 5948d318a50SLinus Walleij list_add_tail(&desc->node, &d40c->active); 5958d318a50SLinus Walleij } 5968d318a50SLinus Walleij 5971c4b0927SRabin Vincent static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc) 5981c4b0927SRabin Vincent { 5991c4b0927SRabin Vincent struct d40_phy_lli *lli_dst = desc->lli_phy.dst; 6001c4b0927SRabin Vincent struct d40_phy_lli *lli_src = desc->lli_phy.src; 6011c4b0927SRabin Vincent void __iomem *base = chan_base(chan); 6021c4b0927SRabin Vincent 6031c4b0927SRabin Vincent writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG); 6041c4b0927SRabin Vincent writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT); 6051c4b0927SRabin Vincent writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR); 6061c4b0927SRabin Vincent writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK); 6071c4b0927SRabin Vincent 6081c4b0927SRabin Vincent writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG); 6091c4b0927SRabin Vincent writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT); 6101c4b0927SRabin Vincent writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR); 6111c4b0927SRabin Vincent writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK); 6121c4b0927SRabin Vincent } 6131c4b0927SRabin Vincent 614e65889c7SRabin Vincent static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc) 615698e4732SJonas Aaberg { 616e65889c7SRabin Vincent struct d40_lcla_pool *pool = &chan->base->lcla_pool; 617e65889c7SRabin Vincent struct d40_log_lli_bidir *lli = &desc->lli_log; 618e65889c7SRabin Vincent int lli_current = desc->lli_current; 619e65889c7SRabin Vincent int lli_len = desc->lli_len; 6200c842b55SRabin Vincent bool cyclic = desc->cyclic; 621e65889c7SRabin Vincent int curr_lcla = -EINVAL; 6220c842b55SRabin Vincent int first_lcla = 0; 62328c7a19dSNarayanan G bool use_esram_lcla = chan->base->plat_data->use_esram_lcla; 6240c842b55SRabin Vincent bool linkback; 625698e4732SJonas Aaberg 6260c842b55SRabin Vincent /* 6270c842b55SRabin Vincent * We may have partially running cyclic transfers, in case we did't get 6280c842b55SRabin Vincent * enough LCLA entries. 6290c842b55SRabin Vincent */ 6300c842b55SRabin Vincent linkback = cyclic && lli_current == 0; 6310c842b55SRabin Vincent 6320c842b55SRabin Vincent /* 6330c842b55SRabin Vincent * For linkback, we need one LCLA even with only one link, because we 6340c842b55SRabin Vincent * can't link back to the one in LCPA space 6350c842b55SRabin Vincent */ 6360c842b55SRabin Vincent if (linkback || (lli_len - lli_current > 1)) { 637e65889c7SRabin Vincent curr_lcla = d40_lcla_alloc_one(chan, desc); 6380c842b55SRabin Vincent first_lcla = curr_lcla; 6390c842b55SRabin Vincent } 6400c842b55SRabin Vincent 6410c842b55SRabin Vincent /* 6420c842b55SRabin Vincent * For linkback, we normally load the LCPA in the loop since we need to 6430c842b55SRabin Vincent * link it to the second LCLA and not the first. However, if we 6440c842b55SRabin Vincent * couldn't even get a first LCLA, then we have to run in LCPA and 6450c842b55SRabin Vincent * reload manually. 6460c842b55SRabin Vincent */ 6470c842b55SRabin Vincent if (!linkback || curr_lcla == -EINVAL) { 6480c842b55SRabin Vincent unsigned int flags = 0; 6490c842b55SRabin Vincent 6500c842b55SRabin Vincent if (curr_lcla == -EINVAL) 6510c842b55SRabin Vincent flags |= LLI_TERM_INT; 652698e4732SJonas Aaberg 653e65889c7SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa, 654e65889c7SRabin Vincent &lli->dst[lli_current], 655e65889c7SRabin Vincent &lli->src[lli_current], 6560c842b55SRabin Vincent curr_lcla, 6570c842b55SRabin Vincent flags); 658e65889c7SRabin Vincent lli_current++; 6590c842b55SRabin Vincent } 6606045f0bbSRabin Vincent 6616045f0bbSRabin Vincent if (curr_lcla < 0) 6626045f0bbSRabin Vincent goto out; 6636045f0bbSRabin Vincent 664e65889c7SRabin Vincent for (; lli_current < lli_len; lli_current++) { 665e65889c7SRabin Vincent unsigned int lcla_offset = chan->phy_chan->num * 1024 + 666026cbc42SRabin Vincent 8 * curr_lcla * 2; 667026cbc42SRabin Vincent struct d40_log_lli *lcla = pool->base + lcla_offset; 6680c842b55SRabin Vincent unsigned int flags = 0; 669e65889c7SRabin Vincent int next_lcla; 670698e4732SJonas Aaberg 671e65889c7SRabin Vincent if (lli_current + 1 < lli_len) 672e65889c7SRabin Vincent next_lcla = d40_lcla_alloc_one(chan, desc); 673698e4732SJonas Aaberg else 6740c842b55SRabin Vincent next_lcla = linkback ? first_lcla : -EINVAL; 675698e4732SJonas Aaberg 6760c842b55SRabin Vincent if (cyclic || next_lcla == -EINVAL) 6770c842b55SRabin Vincent flags |= LLI_TERM_INT; 6780c842b55SRabin Vincent 6790c842b55SRabin Vincent if (linkback && curr_lcla == first_lcla) { 6800c842b55SRabin Vincent /* First link goes in both LCPA and LCLA */ 6810c842b55SRabin Vincent d40_log_lli_lcpa_write(chan->lcpa, 6820c842b55SRabin Vincent &lli->dst[lli_current], 6830c842b55SRabin Vincent &lli->src[lli_current], 6840c842b55SRabin Vincent next_lcla, flags); 6850c842b55SRabin Vincent } 6860c842b55SRabin Vincent 6870c842b55SRabin Vincent /* 6880c842b55SRabin Vincent * One unused LCLA in the cyclic case if the very first 6890c842b55SRabin Vincent * next_lcla fails... 6900c842b55SRabin Vincent */ 691698e4732SJonas Aaberg d40_log_lli_lcla_write(lcla, 692e65889c7SRabin Vincent &lli->dst[lli_current], 693e65889c7SRabin Vincent &lli->src[lli_current], 6940c842b55SRabin Vincent next_lcla, flags); 695698e4732SJonas Aaberg 69628c7a19dSNarayanan G /* 69728c7a19dSNarayanan G * Cache maintenance is not needed if lcla is 69828c7a19dSNarayanan G * mapped in esram 69928c7a19dSNarayanan G */ 70028c7a19dSNarayanan G if (!use_esram_lcla) { 701e65889c7SRabin Vincent dma_sync_single_range_for_device(chan->base->dev, 702026cbc42SRabin Vincent pool->dma_addr, lcla_offset, 703698e4732SJonas Aaberg 2 * sizeof(struct d40_log_lli), 704698e4732SJonas Aaberg DMA_TO_DEVICE); 70528c7a19dSNarayanan G } 706698e4732SJonas Aaberg curr_lcla = next_lcla; 707698e4732SJonas Aaberg 7080c842b55SRabin Vincent if (curr_lcla == -EINVAL || curr_lcla == first_lcla) { 709e65889c7SRabin Vincent lli_current++; 710698e4732SJonas Aaberg break; 711698e4732SJonas Aaberg } 712e65889c7SRabin Vincent } 713698e4732SJonas Aaberg 7146045f0bbSRabin Vincent out: 715e65889c7SRabin Vincent desc->lli_current = lli_current; 716698e4732SJonas Aaberg } 717e65889c7SRabin Vincent 718e65889c7SRabin Vincent static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d) 719e65889c7SRabin Vincent { 720e65889c7SRabin Vincent if (chan_is_physical(d40c)) { 721e65889c7SRabin Vincent d40_phy_lli_load(d40c, d40d); 722e65889c7SRabin Vincent d40d->lli_current = d40d->lli_len; 723e65889c7SRabin Vincent } else 724e65889c7SRabin Vincent d40_log_lli_to_lcxa(d40c, d40d); 725698e4732SJonas Aaberg } 726698e4732SJonas Aaberg 7278d318a50SLinus Walleij static struct d40_desc *d40_first_active_get(struct d40_chan *d40c) 7288d318a50SLinus Walleij { 7298d318a50SLinus Walleij struct d40_desc *d; 7308d318a50SLinus Walleij 7318d318a50SLinus Walleij if (list_empty(&d40c->active)) 7328d318a50SLinus Walleij return NULL; 7338d318a50SLinus Walleij 7348d318a50SLinus Walleij d = list_first_entry(&d40c->active, 7358d318a50SLinus Walleij struct d40_desc, 7368d318a50SLinus Walleij node); 7378d318a50SLinus Walleij return d; 7388d318a50SLinus Walleij } 7398d318a50SLinus Walleij 7407404368cSPer Forlin /* remove desc from current queue and add it to the pending_queue */ 7418d318a50SLinus Walleij static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc) 7428d318a50SLinus Walleij { 7437404368cSPer Forlin d40_desc_remove(desc); 7447404368cSPer Forlin desc->is_in_client_list = false; 745a8f3067bSPer Forlin list_add_tail(&desc->node, &d40c->pending_queue); 746a8f3067bSPer Forlin } 747a8f3067bSPer Forlin 748a8f3067bSPer Forlin static struct d40_desc *d40_first_pending(struct d40_chan *d40c) 749a8f3067bSPer Forlin { 750a8f3067bSPer Forlin struct d40_desc *d; 751a8f3067bSPer Forlin 752a8f3067bSPer Forlin if (list_empty(&d40c->pending_queue)) 753a8f3067bSPer Forlin return NULL; 754a8f3067bSPer Forlin 755a8f3067bSPer Forlin d = list_first_entry(&d40c->pending_queue, 756a8f3067bSPer Forlin struct d40_desc, 757a8f3067bSPer Forlin node); 758a8f3067bSPer Forlin return d; 7598d318a50SLinus Walleij } 7608d318a50SLinus Walleij 7618d318a50SLinus Walleij static struct d40_desc *d40_first_queued(struct d40_chan *d40c) 7628d318a50SLinus Walleij { 7638d318a50SLinus Walleij struct d40_desc *d; 7648d318a50SLinus Walleij 7658d318a50SLinus Walleij if (list_empty(&d40c->queue)) 7668d318a50SLinus Walleij return NULL; 7678d318a50SLinus Walleij 7688d318a50SLinus Walleij d = list_first_entry(&d40c->queue, 7698d318a50SLinus Walleij struct d40_desc, 7708d318a50SLinus Walleij node); 7718d318a50SLinus Walleij return d; 7728d318a50SLinus Walleij } 7738d318a50SLinus Walleij 774d49278e3SPer Forlin static int d40_psize_2_burst_size(bool is_log, int psize) 775d49278e3SPer Forlin { 776d49278e3SPer Forlin if (is_log) { 777d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_LOG_1) 778d49278e3SPer Forlin return 1; 779d49278e3SPer Forlin } else { 780d49278e3SPer Forlin if (psize == STEDMA40_PSIZE_PHY_1) 781d49278e3SPer Forlin return 1; 782d49278e3SPer Forlin } 7838d318a50SLinus Walleij 784d49278e3SPer Forlin return 2 << psize; 785d49278e3SPer Forlin } 786d49278e3SPer Forlin 787d49278e3SPer Forlin /* 788d49278e3SPer Forlin * The dma only supports transmitting packages up to 789d49278e3SPer Forlin * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of 790d49278e3SPer Forlin * dma elements required to send the entire sg list 791d49278e3SPer Forlin */ 792d49278e3SPer Forlin static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2) 793d49278e3SPer Forlin { 794d49278e3SPer Forlin int dmalen; 795d49278e3SPer Forlin u32 max_w = max(data_width1, data_width2); 796d49278e3SPer Forlin u32 min_w = min(data_width1, data_width2); 797d49278e3SPer Forlin u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w); 798d49278e3SPer Forlin 799d49278e3SPer Forlin if (seg_max > STEDMA40_MAX_SEG_SIZE) 800d49278e3SPer Forlin seg_max -= (1 << max_w); 801d49278e3SPer Forlin 802d49278e3SPer Forlin if (!IS_ALIGNED(size, 1 << max_w)) 803d49278e3SPer Forlin return -EINVAL; 804d49278e3SPer Forlin 805d49278e3SPer Forlin if (size <= seg_max) 806d49278e3SPer Forlin dmalen = 1; 807d49278e3SPer Forlin else { 808d49278e3SPer Forlin dmalen = size / seg_max; 809d49278e3SPer Forlin if (dmalen * seg_max < size) 810d49278e3SPer Forlin dmalen++; 811d49278e3SPer Forlin } 812d49278e3SPer Forlin return dmalen; 813d49278e3SPer Forlin } 814d49278e3SPer Forlin 815d49278e3SPer Forlin static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len, 816d49278e3SPer Forlin u32 data_width1, u32 data_width2) 817d49278e3SPer Forlin { 818d49278e3SPer Forlin struct scatterlist *sg; 819d49278e3SPer Forlin int i; 820d49278e3SPer Forlin int len = 0; 821d49278e3SPer Forlin int ret; 822d49278e3SPer Forlin 823d49278e3SPer Forlin for_each_sg(sgl, sg, sg_len, i) { 824d49278e3SPer Forlin ret = d40_size_2_dmalen(sg_dma_len(sg), 825d49278e3SPer Forlin data_width1, data_width2); 826d49278e3SPer Forlin if (ret < 0) 827d49278e3SPer Forlin return ret; 828d49278e3SPer Forlin len += ret; 829d49278e3SPer Forlin } 830d49278e3SPer Forlin return len; 831d49278e3SPer Forlin } 832d49278e3SPer Forlin 8337fb3e75eSNarayanan G 8347fb3e75eSNarayanan G #ifdef CONFIG_PM 8357fb3e75eSNarayanan G static void dma40_backup(void __iomem *baseaddr, u32 *backup, 8367fb3e75eSNarayanan G u32 *regaddr, int num, bool save) 8377fb3e75eSNarayanan G { 8387fb3e75eSNarayanan G int i; 8397fb3e75eSNarayanan G 8407fb3e75eSNarayanan G for (i = 0; i < num; i++) { 8417fb3e75eSNarayanan G void __iomem *addr = baseaddr + regaddr[i]; 8427fb3e75eSNarayanan G 8437fb3e75eSNarayanan G if (save) 8447fb3e75eSNarayanan G backup[i] = readl_relaxed(addr); 8457fb3e75eSNarayanan G else 8467fb3e75eSNarayanan G writel_relaxed(backup[i], addr); 8477fb3e75eSNarayanan G } 8487fb3e75eSNarayanan G } 8497fb3e75eSNarayanan G 8507fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save) 8517fb3e75eSNarayanan G { 8527fb3e75eSNarayanan G int i; 8537fb3e75eSNarayanan G 8547fb3e75eSNarayanan G /* Save/Restore channel specific registers */ 8557fb3e75eSNarayanan G for (i = 0; i < base->num_phy_chans; i++) { 8567fb3e75eSNarayanan G void __iomem *addr; 8577fb3e75eSNarayanan G int idx; 8587fb3e75eSNarayanan G 8597fb3e75eSNarayanan G if (base->phy_res[i].reserved) 8607fb3e75eSNarayanan G continue; 8617fb3e75eSNarayanan G 8627fb3e75eSNarayanan G addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA; 8637fb3e75eSNarayanan G idx = i * ARRAY_SIZE(d40_backup_regs_chan); 8647fb3e75eSNarayanan G 8657fb3e75eSNarayanan G dma40_backup(addr, &base->reg_val_backup_chan[idx], 8667fb3e75eSNarayanan G d40_backup_regs_chan, 8677fb3e75eSNarayanan G ARRAY_SIZE(d40_backup_regs_chan), 8687fb3e75eSNarayanan G save); 8697fb3e75eSNarayanan G } 8707fb3e75eSNarayanan G 8717fb3e75eSNarayanan G /* Save/Restore global registers */ 8727fb3e75eSNarayanan G dma40_backup(base->virtbase, base->reg_val_backup, 8737fb3e75eSNarayanan G d40_backup_regs, ARRAY_SIZE(d40_backup_regs), 8747fb3e75eSNarayanan G save); 8757fb3e75eSNarayanan G 8767fb3e75eSNarayanan G /* Save/Restore registers only existing on dma40 v3 and later */ 8777fb3e75eSNarayanan G if (base->rev >= 3) 8787fb3e75eSNarayanan G dma40_backup(base->virtbase, base->reg_val_backup_v3, 8797fb3e75eSNarayanan G d40_backup_regs_v3, 8807fb3e75eSNarayanan G ARRAY_SIZE(d40_backup_regs_v3), 8817fb3e75eSNarayanan G save); 8827fb3e75eSNarayanan G } 8837fb3e75eSNarayanan G #else 8847fb3e75eSNarayanan G static void d40_save_restore_registers(struct d40_base *base, bool save) 8857fb3e75eSNarayanan G { 8867fb3e75eSNarayanan G } 8877fb3e75eSNarayanan G #endif 8888d318a50SLinus Walleij 8891bdae6f4SNarayanan G static int __d40_execute_command_phy(struct d40_chan *d40c, 8908d318a50SLinus Walleij enum d40_command command) 8918d318a50SLinus Walleij { 892767a9675SJonas Aaberg u32 status; 893767a9675SJonas Aaberg int i; 8948d318a50SLinus Walleij void __iomem *active_reg; 8958d318a50SLinus Walleij int ret = 0; 8968d318a50SLinus Walleij unsigned long flags; 8971d392a7bSJonas Aaberg u32 wmask; 8988d318a50SLinus Walleij 8991bdae6f4SNarayanan G if (command == D40_DMA_STOP) { 9001bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ); 9011bdae6f4SNarayanan G if (ret) 9021bdae6f4SNarayanan G return ret; 9031bdae6f4SNarayanan G } 9041bdae6f4SNarayanan G 9058d318a50SLinus Walleij spin_lock_irqsave(&d40c->base->execmd_lock, flags); 9068d318a50SLinus Walleij 9078d318a50SLinus Walleij if (d40c->phy_chan->num % 2 == 0) 9088d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 9098d318a50SLinus Walleij else 9108d318a50SLinus Walleij active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 9118d318a50SLinus Walleij 9128d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) { 9138d318a50SLinus Walleij status = (readl(active_reg) & 9148d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 9158d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 9168d318a50SLinus Walleij 9178d318a50SLinus Walleij if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) 9188d318a50SLinus Walleij goto done; 9198d318a50SLinus Walleij } 9208d318a50SLinus Walleij 9211d392a7bSJonas Aaberg wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num)); 9221d392a7bSJonas Aaberg writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)), 9231d392a7bSJonas Aaberg active_reg); 9248d318a50SLinus Walleij 9258d318a50SLinus Walleij if (command == D40_DMA_SUSPEND_REQ) { 9268d318a50SLinus Walleij 9278d318a50SLinus Walleij for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) { 9288d318a50SLinus Walleij status = (readl(active_reg) & 9298d318a50SLinus Walleij D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 9308d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 9318d318a50SLinus Walleij 9328d318a50SLinus Walleij cpu_relax(); 9338d318a50SLinus Walleij /* 9348d318a50SLinus Walleij * Reduce the number of bus accesses while 9358d318a50SLinus Walleij * waiting for the DMA to suspend. 9368d318a50SLinus Walleij */ 9378d318a50SLinus Walleij udelay(3); 9388d318a50SLinus Walleij 9398d318a50SLinus Walleij if (status == D40_DMA_STOP || 9408d318a50SLinus Walleij status == D40_DMA_SUSPENDED) 9418d318a50SLinus Walleij break; 9428d318a50SLinus Walleij } 9438d318a50SLinus Walleij 9448d318a50SLinus Walleij if (i == D40_SUSPEND_MAX_IT) { 9456db5a8baSRabin Vincent chan_err(d40c, 9466db5a8baSRabin Vincent "unable to suspend the chl %d (log: %d) status %x\n", 9476db5a8baSRabin Vincent d40c->phy_chan->num, d40c->log_num, 9488d318a50SLinus Walleij status); 9498d318a50SLinus Walleij dump_stack(); 9508d318a50SLinus Walleij ret = -EBUSY; 9518d318a50SLinus Walleij } 9528d318a50SLinus Walleij 9538d318a50SLinus Walleij } 9548d318a50SLinus Walleij done: 9558d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->base->execmd_lock, flags); 9568d318a50SLinus Walleij return ret; 9578d318a50SLinus Walleij } 9588d318a50SLinus Walleij 9598d318a50SLinus Walleij static void d40_term_all(struct d40_chan *d40c) 9608d318a50SLinus Walleij { 9618d318a50SLinus Walleij struct d40_desc *d40d; 9627404368cSPer Forlin struct d40_desc *_d; 9638d318a50SLinus Walleij 9648d318a50SLinus Walleij /* Release active descriptors */ 9658d318a50SLinus Walleij while ((d40d = d40_first_active_get(d40c))) { 9668d318a50SLinus Walleij d40_desc_remove(d40d); 9678d318a50SLinus Walleij d40_desc_free(d40c, d40d); 9688d318a50SLinus Walleij } 9698d318a50SLinus Walleij 9708d318a50SLinus Walleij /* Release queued descriptors waiting for transfer */ 9718d318a50SLinus Walleij while ((d40d = d40_first_queued(d40c))) { 9728d318a50SLinus Walleij d40_desc_remove(d40d); 9738d318a50SLinus Walleij d40_desc_free(d40c, d40d); 9748d318a50SLinus Walleij } 9758d318a50SLinus Walleij 976a8f3067bSPer Forlin /* Release pending descriptors */ 977a8f3067bSPer Forlin while ((d40d = d40_first_pending(d40c))) { 978a8f3067bSPer Forlin d40_desc_remove(d40d); 979a8f3067bSPer Forlin d40_desc_free(d40c, d40d); 980a8f3067bSPer Forlin } 9818d318a50SLinus Walleij 9827404368cSPer Forlin /* Release client owned descriptors */ 9837404368cSPer Forlin if (!list_empty(&d40c->client)) 9847404368cSPer Forlin list_for_each_entry_safe(d40d, _d, &d40c->client, node) { 9857404368cSPer Forlin d40_desc_remove(d40d); 9867404368cSPer Forlin d40_desc_free(d40c, d40d); 9877404368cSPer Forlin } 9887404368cSPer Forlin 98982babbb3SPer Forlin /* Release descriptors in prepare queue */ 99082babbb3SPer Forlin if (!list_empty(&d40c->prepare_queue)) 99182babbb3SPer Forlin list_for_each_entry_safe(d40d, _d, 99282babbb3SPer Forlin &d40c->prepare_queue, node) { 99382babbb3SPer Forlin d40_desc_remove(d40d); 99482babbb3SPer Forlin d40_desc_free(d40c, d40d); 99582babbb3SPer Forlin } 9967404368cSPer Forlin 9978d318a50SLinus Walleij d40c->pending_tx = 0; 9988d318a50SLinus Walleij } 9998d318a50SLinus Walleij 10001bdae6f4SNarayanan G static void __d40_config_set_event(struct d40_chan *d40c, 10011bdae6f4SNarayanan G enum d40_events event_type, u32 event, 10021bdae6f4SNarayanan G int reg) 1003262d2915SRabin Vincent { 10048ca84687SRabin Vincent void __iomem *addr = chan_base(d40c) + reg; 1005262d2915SRabin Vincent int tries; 10061bdae6f4SNarayanan G u32 status; 1007262d2915SRabin Vincent 10081bdae6f4SNarayanan G switch (event_type) { 10091bdae6f4SNarayanan G 10101bdae6f4SNarayanan G case D40_DEACTIVATE_EVENTLINE: 10111bdae6f4SNarayanan G 1012262d2915SRabin Vincent writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) 1013262d2915SRabin Vincent | ~D40_EVENTLINE_MASK(event), addr); 10141bdae6f4SNarayanan G break; 10151bdae6f4SNarayanan G 10161bdae6f4SNarayanan G case D40_SUSPEND_REQ_EVENTLINE: 10171bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >> 10181bdae6f4SNarayanan G D40_EVENTLINE_POS(event); 10191bdae6f4SNarayanan G 10201bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE || 10211bdae6f4SNarayanan G status == D40_SUSPEND_REQ_EVENTLINE) 10221bdae6f4SNarayanan G break; 10231bdae6f4SNarayanan G 10241bdae6f4SNarayanan G writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event)) 10251bdae6f4SNarayanan G | ~D40_EVENTLINE_MASK(event), addr); 10261bdae6f4SNarayanan G 10271bdae6f4SNarayanan G for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) { 10281bdae6f4SNarayanan G 10291bdae6f4SNarayanan G status = (readl(addr) & D40_EVENTLINE_MASK(event)) >> 10301bdae6f4SNarayanan G D40_EVENTLINE_POS(event); 10311bdae6f4SNarayanan G 10321bdae6f4SNarayanan G cpu_relax(); 10331bdae6f4SNarayanan G /* 10341bdae6f4SNarayanan G * Reduce the number of bus accesses while 10351bdae6f4SNarayanan G * waiting for the DMA to suspend. 10361bdae6f4SNarayanan G */ 10371bdae6f4SNarayanan G udelay(3); 10381bdae6f4SNarayanan G 10391bdae6f4SNarayanan G if (status == D40_DEACTIVATE_EVENTLINE) 10401bdae6f4SNarayanan G break; 1041262d2915SRabin Vincent } 1042262d2915SRabin Vincent 10431bdae6f4SNarayanan G if (tries == D40_SUSPEND_MAX_IT) { 10441bdae6f4SNarayanan G chan_err(d40c, 10451bdae6f4SNarayanan G "unable to stop the event_line chl %d (log: %d)" 10461bdae6f4SNarayanan G "status %x\n", d40c->phy_chan->num, 10471bdae6f4SNarayanan G d40c->log_num, status); 10481bdae6f4SNarayanan G } 10491bdae6f4SNarayanan G break; 10501bdae6f4SNarayanan G 10511bdae6f4SNarayanan G case D40_ACTIVATE_EVENTLINE: 1052262d2915SRabin Vincent /* 1053262d2915SRabin Vincent * The hardware sometimes doesn't register the enable when src and dst 1054262d2915SRabin Vincent * event lines are active on the same logical channel. Retry to ensure 1055262d2915SRabin Vincent * it does. Usually only one retry is sufficient. 1056262d2915SRabin Vincent */ 1057262d2915SRabin Vincent tries = 100; 1058262d2915SRabin Vincent while (--tries) { 10591bdae6f4SNarayanan G writel((D40_ACTIVATE_EVENTLINE << 10601bdae6f4SNarayanan G D40_EVENTLINE_POS(event)) | 10611bdae6f4SNarayanan G ~D40_EVENTLINE_MASK(event), addr); 1062262d2915SRabin Vincent 1063262d2915SRabin Vincent if (readl(addr) & D40_EVENTLINE_MASK(event)) 1064262d2915SRabin Vincent break; 1065262d2915SRabin Vincent } 1066262d2915SRabin Vincent 1067262d2915SRabin Vincent if (tries != 99) 1068262d2915SRabin Vincent dev_dbg(chan2dev(d40c), 1069262d2915SRabin Vincent "[%s] workaround enable S%cLNK (%d tries)\n", 1070262d2915SRabin Vincent __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D', 1071262d2915SRabin Vincent 100 - tries); 1072262d2915SRabin Vincent 1073262d2915SRabin Vincent WARN_ON(!tries); 10741bdae6f4SNarayanan G break; 10751bdae6f4SNarayanan G 10761bdae6f4SNarayanan G case D40_ROUND_EVENTLINE: 10771bdae6f4SNarayanan G BUG(); 10781bdae6f4SNarayanan G break; 10791bdae6f4SNarayanan G 10801bdae6f4SNarayanan G } 1081262d2915SRabin Vincent } 1082262d2915SRabin Vincent 10831bdae6f4SNarayanan G static void d40_config_set_event(struct d40_chan *d40c, 10841bdae6f4SNarayanan G enum d40_events event_type) 10858d318a50SLinus Walleij { 10868d318a50SLinus Walleij /* Enable event line connected to device (or memcpy) */ 10878d318a50SLinus Walleij if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || 10888d318a50SLinus Walleij (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) { 10898d318a50SLinus Walleij u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); 10908d318a50SLinus Walleij 10911bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event, 10928d318a50SLinus Walleij D40_CHAN_REG_SSLNK); 10938d318a50SLinus Walleij } 1094262d2915SRabin Vincent 10958d318a50SLinus Walleij if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) { 10968d318a50SLinus Walleij u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); 10978d318a50SLinus Walleij 10981bdae6f4SNarayanan G __d40_config_set_event(d40c, event_type, event, 10998d318a50SLinus Walleij D40_CHAN_REG_SDLNK); 11008d318a50SLinus Walleij } 11018d318a50SLinus Walleij } 11028d318a50SLinus Walleij 1103a5ebca47SJonas Aaberg static u32 d40_chan_has_events(struct d40_chan *d40c) 11048d318a50SLinus Walleij { 11058ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 1106be8cb7dfSJonas Aaberg u32 val; 11078d318a50SLinus Walleij 11088ca84687SRabin Vincent val = readl(chanbase + D40_CHAN_REG_SSLNK); 11098ca84687SRabin Vincent val |= readl(chanbase + D40_CHAN_REG_SDLNK); 11108d318a50SLinus Walleij 1111a5ebca47SJonas Aaberg return val; 11128d318a50SLinus Walleij } 11138d318a50SLinus Walleij 11141bdae6f4SNarayanan G static int 11151bdae6f4SNarayanan G __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command) 11161bdae6f4SNarayanan G { 11171bdae6f4SNarayanan G unsigned long flags; 11181bdae6f4SNarayanan G int ret = 0; 11191bdae6f4SNarayanan G u32 active_status; 11201bdae6f4SNarayanan G void __iomem *active_reg; 11211bdae6f4SNarayanan G 11221bdae6f4SNarayanan G if (d40c->phy_chan->num % 2 == 0) 11231bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 11241bdae6f4SNarayanan G else 11251bdae6f4SNarayanan G active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 11261bdae6f4SNarayanan G 11271bdae6f4SNarayanan G 11281bdae6f4SNarayanan G spin_lock_irqsave(&d40c->phy_chan->lock, flags); 11291bdae6f4SNarayanan G 11301bdae6f4SNarayanan G switch (command) { 11311bdae6f4SNarayanan G case D40_DMA_STOP: 11321bdae6f4SNarayanan G case D40_DMA_SUSPEND_REQ: 11331bdae6f4SNarayanan G 11341bdae6f4SNarayanan G active_status = (readl(active_reg) & 11351bdae6f4SNarayanan G D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 11361bdae6f4SNarayanan G D40_CHAN_POS(d40c->phy_chan->num); 11371bdae6f4SNarayanan G 11381bdae6f4SNarayanan G if (active_status == D40_DMA_RUN) 11391bdae6f4SNarayanan G d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE); 11401bdae6f4SNarayanan G else 11411bdae6f4SNarayanan G d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE); 11421bdae6f4SNarayanan G 11431bdae6f4SNarayanan G if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP)) 11441bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command); 11451bdae6f4SNarayanan G 11461bdae6f4SNarayanan G break; 11471bdae6f4SNarayanan G 11481bdae6f4SNarayanan G case D40_DMA_RUN: 11491bdae6f4SNarayanan G 11501bdae6f4SNarayanan G d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE); 11511bdae6f4SNarayanan G ret = __d40_execute_command_phy(d40c, command); 11521bdae6f4SNarayanan G break; 11531bdae6f4SNarayanan G 11541bdae6f4SNarayanan G case D40_DMA_SUSPENDED: 11551bdae6f4SNarayanan G BUG(); 11561bdae6f4SNarayanan G break; 11571bdae6f4SNarayanan G } 11581bdae6f4SNarayanan G 11591bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->phy_chan->lock, flags); 11601bdae6f4SNarayanan G return ret; 11611bdae6f4SNarayanan G } 11621bdae6f4SNarayanan G 11631bdae6f4SNarayanan G static int d40_channel_execute_command(struct d40_chan *d40c, 11641bdae6f4SNarayanan G enum d40_command command) 11651bdae6f4SNarayanan G { 11661bdae6f4SNarayanan G if (chan_is_logical(d40c)) 11671bdae6f4SNarayanan G return __d40_execute_command_log(d40c, command); 11681bdae6f4SNarayanan G else 11691bdae6f4SNarayanan G return __d40_execute_command_phy(d40c, command); 11701bdae6f4SNarayanan G } 11711bdae6f4SNarayanan G 117220a5b6d0SRabin Vincent static u32 d40_get_prmo(struct d40_chan *d40c) 117320a5b6d0SRabin Vincent { 117420a5b6d0SRabin Vincent static const unsigned int phy_map[] = { 117520a5b6d0SRabin Vincent [STEDMA40_PCHAN_BASIC_MODE] 117620a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_BASIC, 117720a5b6d0SRabin Vincent [STEDMA40_PCHAN_MODULO_MODE] 117820a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_MODULO, 117920a5b6d0SRabin Vincent [STEDMA40_PCHAN_DOUBLE_DST_MODE] 118020a5b6d0SRabin Vincent = D40_DREG_PRMO_PCHAN_DOUBLE_DST, 118120a5b6d0SRabin Vincent }; 118220a5b6d0SRabin Vincent static const unsigned int log_map[] = { 118320a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_PHY_DST_LOG] 118420a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG, 118520a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_PHY] 118620a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY, 118720a5b6d0SRabin Vincent [STEDMA40_LCHAN_SRC_LOG_DST_LOG] 118820a5b6d0SRabin Vincent = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG, 118920a5b6d0SRabin Vincent }; 119020a5b6d0SRabin Vincent 1191724a8577SRabin Vincent if (chan_is_physical(d40c)) 119220a5b6d0SRabin Vincent return phy_map[d40c->dma_cfg.mode_opt]; 119320a5b6d0SRabin Vincent else 119420a5b6d0SRabin Vincent return log_map[d40c->dma_cfg.mode_opt]; 119520a5b6d0SRabin Vincent } 119620a5b6d0SRabin Vincent 1197b55912c6SJonas Aaberg static void d40_config_write(struct d40_chan *d40c) 11988d318a50SLinus Walleij { 11998d318a50SLinus Walleij u32 addr_base; 12008d318a50SLinus Walleij u32 var; 12018d318a50SLinus Walleij 12028d318a50SLinus Walleij /* Odd addresses are even addresses + 4 */ 12038d318a50SLinus Walleij addr_base = (d40c->phy_chan->num % 2) * 4; 12048d318a50SLinus Walleij /* Setup channel mode to logical or physical */ 1205724a8577SRabin Vincent var = ((u32)(chan_is_logical(d40c)) + 1) << 12068d318a50SLinus Walleij D40_CHAN_POS(d40c->phy_chan->num); 12078d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base); 12088d318a50SLinus Walleij 12098d318a50SLinus Walleij /* Setup operational mode option register */ 121020a5b6d0SRabin Vincent var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num); 12118d318a50SLinus Walleij 12128d318a50SLinus Walleij writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base); 12138d318a50SLinus Walleij 1214724a8577SRabin Vincent if (chan_is_logical(d40c)) { 12158ca84687SRabin Vincent int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) 12168ca84687SRabin Vincent & D40_SREG_ELEM_LOG_LIDX_MASK; 12178ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 12188ca84687SRabin Vincent 12198d318a50SLinus Walleij /* Set default config for CFG reg */ 12208ca84687SRabin Vincent writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG); 12218ca84687SRabin Vincent writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG); 12228d318a50SLinus Walleij 1223b55912c6SJonas Aaberg /* Set LIDX for lcla */ 12248ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SSELT); 12258ca84687SRabin Vincent writel(lidx, chanbase + D40_CHAN_REG_SDELT); 1226e9f3a49cSRabin Vincent 1227e9f3a49cSRabin Vincent /* Clear LNK which will be used by d40_chan_has_events() */ 1228e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SSLNK); 1229e9f3a49cSRabin Vincent writel(0, chanbase + D40_CHAN_REG_SDLNK); 12308d318a50SLinus Walleij } 12318d318a50SLinus Walleij } 12328d318a50SLinus Walleij 1233aa182ae2SJonas Aaberg static u32 d40_residue(struct d40_chan *d40c) 1234aa182ae2SJonas Aaberg { 1235aa182ae2SJonas Aaberg u32 num_elt; 1236aa182ae2SJonas Aaberg 1237724a8577SRabin Vincent if (chan_is_logical(d40c)) 1238aa182ae2SJonas Aaberg num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK) 1239aa182ae2SJonas Aaberg >> D40_MEM_LCSP2_ECNT_POS; 12408ca84687SRabin Vincent else { 12418ca84687SRabin Vincent u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT); 12428ca84687SRabin Vincent num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK) 12438ca84687SRabin Vincent >> D40_SREG_ELEM_PHY_ECNT_POS; 12448ca84687SRabin Vincent } 12458ca84687SRabin Vincent 1246aa182ae2SJonas Aaberg return num_elt * (1 << d40c->dma_cfg.dst_info.data_width); 1247aa182ae2SJonas Aaberg } 1248aa182ae2SJonas Aaberg 1249aa182ae2SJonas Aaberg static bool d40_tx_is_linked(struct d40_chan *d40c) 1250aa182ae2SJonas Aaberg { 1251aa182ae2SJonas Aaberg bool is_link; 1252aa182ae2SJonas Aaberg 1253724a8577SRabin Vincent if (chan_is_logical(d40c)) 1254aa182ae2SJonas Aaberg is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK; 1255aa182ae2SJonas Aaberg else 12568ca84687SRabin Vincent is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK) 12578ca84687SRabin Vincent & D40_SREG_LNK_PHYS_LNK_MASK; 12588ca84687SRabin Vincent 1259aa182ae2SJonas Aaberg return is_link; 1260aa182ae2SJonas Aaberg } 1261aa182ae2SJonas Aaberg 126286eb5fb6SRabin Vincent static int d40_pause(struct d40_chan *d40c) 1263aa182ae2SJonas Aaberg { 1264aa182ae2SJonas Aaberg int res = 0; 1265aa182ae2SJonas Aaberg unsigned long flags; 1266aa182ae2SJonas Aaberg 12673ac012afSJonas Aaberg if (!d40c->busy) 12683ac012afSJonas Aaberg return 0; 12693ac012afSJonas Aaberg 12707fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 1271aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 1272aa182ae2SJonas Aaberg 1273aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); 12741bdae6f4SNarayanan G 12757fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 12767fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 1277aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 1278aa182ae2SJonas Aaberg return res; 1279aa182ae2SJonas Aaberg } 1280aa182ae2SJonas Aaberg 128186eb5fb6SRabin Vincent static int d40_resume(struct d40_chan *d40c) 1282aa182ae2SJonas Aaberg { 1283aa182ae2SJonas Aaberg int res = 0; 1284aa182ae2SJonas Aaberg unsigned long flags; 1285aa182ae2SJonas Aaberg 12863ac012afSJonas Aaberg if (!d40c->busy) 12873ac012afSJonas Aaberg return 0; 12883ac012afSJonas Aaberg 1289aa182ae2SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 12907fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 1291aa182ae2SJonas Aaberg 1292aa182ae2SJonas Aaberg /* If bytes left to transfer or linked tx resume job */ 12931bdae6f4SNarayanan G if (d40_residue(d40c) || d40_tx_is_linked(d40c)) 1294aa182ae2SJonas Aaberg res = d40_channel_execute_command(d40c, D40_DMA_RUN); 1295aa182ae2SJonas Aaberg 12967fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 12977fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 1298aa182ae2SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 1299aa182ae2SJonas Aaberg return res; 1300aa182ae2SJonas Aaberg } 1301aa182ae2SJonas Aaberg 13028d318a50SLinus Walleij static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx) 13038d318a50SLinus Walleij { 13048d318a50SLinus Walleij struct d40_chan *d40c = container_of(tx->chan, 13058d318a50SLinus Walleij struct d40_chan, 13068d318a50SLinus Walleij chan); 13078d318a50SLinus Walleij struct d40_desc *d40d = container_of(tx, struct d40_desc, txd); 13088d318a50SLinus Walleij unsigned long flags; 1309884485e1SRussell King - ARM Linux dma_cookie_t cookie; 13108d318a50SLinus Walleij 13118d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 1312884485e1SRussell King - ARM Linux cookie = dma_cookie_assign(tx); 13138d318a50SLinus Walleij d40_desc_queue(d40c, d40d); 13148d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 13158d318a50SLinus Walleij 1316884485e1SRussell King - ARM Linux return cookie; 13178d318a50SLinus Walleij } 13188d318a50SLinus Walleij 13198d318a50SLinus Walleij static int d40_start(struct d40_chan *d40c) 13208d318a50SLinus Walleij { 13210c32269dSJonas Aaberg return d40_channel_execute_command(d40c, D40_DMA_RUN); 13228d318a50SLinus Walleij } 13238d318a50SLinus Walleij 13248d318a50SLinus Walleij static struct d40_desc *d40_queue_start(struct d40_chan *d40c) 13258d318a50SLinus Walleij { 13268d318a50SLinus Walleij struct d40_desc *d40d; 13278d318a50SLinus Walleij int err; 13288d318a50SLinus Walleij 13298d318a50SLinus Walleij /* Start queued jobs, if any */ 13308d318a50SLinus Walleij d40d = d40_first_queued(d40c); 13318d318a50SLinus Walleij 13328d318a50SLinus Walleij if (d40d != NULL) { 13331bdae6f4SNarayanan G if (!d40c->busy) { 13348d318a50SLinus Walleij d40c->busy = true; 13357fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 13361bdae6f4SNarayanan G } 13377fb3e75eSNarayanan G 13388d318a50SLinus Walleij /* Remove from queue */ 13398d318a50SLinus Walleij d40_desc_remove(d40d); 13408d318a50SLinus Walleij 13418d318a50SLinus Walleij /* Add to active queue */ 13428d318a50SLinus Walleij d40_desc_submit(d40c, d40d); 13438d318a50SLinus Walleij 13448d318a50SLinus Walleij /* Initiate DMA job */ 13458d318a50SLinus Walleij d40_desc_load(d40c, d40d); 13468d318a50SLinus Walleij 13478d318a50SLinus Walleij /* Start dma job */ 13488d318a50SLinus Walleij err = d40_start(d40c); 13498d318a50SLinus Walleij 13508d318a50SLinus Walleij if (err) 13518d318a50SLinus Walleij return NULL; 13528d318a50SLinus Walleij } 13538d318a50SLinus Walleij 13548d318a50SLinus Walleij return d40d; 13558d318a50SLinus Walleij } 13568d318a50SLinus Walleij 13578d318a50SLinus Walleij /* called from interrupt context */ 13588d318a50SLinus Walleij static void dma_tc_handle(struct d40_chan *d40c) 13598d318a50SLinus Walleij { 13608d318a50SLinus Walleij struct d40_desc *d40d; 13618d318a50SLinus Walleij 13628d318a50SLinus Walleij /* Get first active entry from list */ 13638d318a50SLinus Walleij d40d = d40_first_active_get(d40c); 13648d318a50SLinus Walleij 13658d318a50SLinus Walleij if (d40d == NULL) 13668d318a50SLinus Walleij return; 13678d318a50SLinus Walleij 13680c842b55SRabin Vincent if (d40d->cyclic) { 13690c842b55SRabin Vincent /* 13700c842b55SRabin Vincent * If this was a paritially loaded list, we need to reloaded 13710c842b55SRabin Vincent * it, and only when the list is completed. We need to check 13720c842b55SRabin Vincent * for done because the interrupt will hit for every link, and 13730c842b55SRabin Vincent * not just the last one. 13740c842b55SRabin Vincent */ 13750c842b55SRabin Vincent if (d40d->lli_current < d40d->lli_len 13760c842b55SRabin Vincent && !d40_tx_is_linked(d40c) 13770c842b55SRabin Vincent && !d40_residue(d40c)) { 13780c842b55SRabin Vincent d40_lcla_free_all(d40c, d40d); 13790c842b55SRabin Vincent d40_desc_load(d40c, d40d); 13800c842b55SRabin Vincent (void) d40_start(d40c); 13810c842b55SRabin Vincent 13820c842b55SRabin Vincent if (d40d->lli_current == d40d->lli_len) 13830c842b55SRabin Vincent d40d->lli_current = 0; 13840c842b55SRabin Vincent } 13850c842b55SRabin Vincent } else { 1386698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 13878d318a50SLinus Walleij 1388698e4732SJonas Aaberg if (d40d->lli_current < d40d->lli_len) { 13898d318a50SLinus Walleij d40_desc_load(d40c, d40d); 13908d318a50SLinus Walleij /* Start dma job */ 13918d318a50SLinus Walleij (void) d40_start(d40c); 13928d318a50SLinus Walleij return; 13938d318a50SLinus Walleij } 13948d318a50SLinus Walleij 13958d318a50SLinus Walleij if (d40_queue_start(d40c) == NULL) 13968d318a50SLinus Walleij d40c->busy = false; 13977fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 13987fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 13990c842b55SRabin Vincent } 14008d318a50SLinus Walleij 14018d318a50SLinus Walleij d40c->pending_tx++; 14028d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet); 14038d318a50SLinus Walleij 14048d318a50SLinus Walleij } 14058d318a50SLinus Walleij 14068d318a50SLinus Walleij static void dma_tasklet(unsigned long data) 14078d318a50SLinus Walleij { 14088d318a50SLinus Walleij struct d40_chan *d40c = (struct d40_chan *) data; 1409767a9675SJonas Aaberg struct d40_desc *d40d; 14108d318a50SLinus Walleij unsigned long flags; 14118d318a50SLinus Walleij dma_async_tx_callback callback; 14128d318a50SLinus Walleij void *callback_param; 14138d318a50SLinus Walleij 14148d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 14158d318a50SLinus Walleij 14168d318a50SLinus Walleij /* Get first active entry from list */ 1417767a9675SJonas Aaberg d40d = d40_first_active_get(d40c); 1418767a9675SJonas Aaberg if (d40d == NULL) 14198d318a50SLinus Walleij goto err; 14208d318a50SLinus Walleij 14210c842b55SRabin Vincent if (!d40d->cyclic) 1422f7fbce07SRussell King - ARM Linux dma_cookie_complete(&d40d->txd); 14238d318a50SLinus Walleij 14248d318a50SLinus Walleij /* 14258d318a50SLinus Walleij * If terminating a channel pending_tx is set to zero. 14268d318a50SLinus Walleij * This prevents any finished active jobs to return to the client. 14278d318a50SLinus Walleij */ 14288d318a50SLinus Walleij if (d40c->pending_tx == 0) { 14298d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 14308d318a50SLinus Walleij return; 14318d318a50SLinus Walleij } 14328d318a50SLinus Walleij 14338d318a50SLinus Walleij /* Callback to client */ 1434767a9675SJonas Aaberg callback = d40d->txd.callback; 1435767a9675SJonas Aaberg callback_param = d40d->txd.callback_param; 14368d318a50SLinus Walleij 14370c842b55SRabin Vincent if (!d40d->cyclic) { 1438767a9675SJonas Aaberg if (async_tx_test_ack(&d40d->txd)) { 1439767a9675SJonas Aaberg d40_desc_remove(d40d); 1440767a9675SJonas Aaberg d40_desc_free(d40c, d40d); 14418d318a50SLinus Walleij } else { 1442767a9675SJonas Aaberg if (!d40d->is_in_client_list) { 1443767a9675SJonas Aaberg d40_desc_remove(d40d); 1444698e4732SJonas Aaberg d40_lcla_free_all(d40c, d40d); 1445767a9675SJonas Aaberg list_add_tail(&d40d->node, &d40c->client); 1446767a9675SJonas Aaberg d40d->is_in_client_list = true; 14478d318a50SLinus Walleij } 14488d318a50SLinus Walleij } 14490c842b55SRabin Vincent } 14508d318a50SLinus Walleij 14518d318a50SLinus Walleij d40c->pending_tx--; 14528d318a50SLinus Walleij 14538d318a50SLinus Walleij if (d40c->pending_tx) 14548d318a50SLinus Walleij tasklet_schedule(&d40c->tasklet); 14558d318a50SLinus Walleij 14568d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 14578d318a50SLinus Walleij 1458767a9675SJonas Aaberg if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT)) 14598d318a50SLinus Walleij callback(callback_param); 14608d318a50SLinus Walleij 14618d318a50SLinus Walleij return; 14628d318a50SLinus Walleij 14638d318a50SLinus Walleij err: 14641bdae6f4SNarayanan G /* Rescue manouver if receiving double interrupts */ 14658d318a50SLinus Walleij if (d40c->pending_tx > 0) 14668d318a50SLinus Walleij d40c->pending_tx--; 14678d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 14688d318a50SLinus Walleij } 14698d318a50SLinus Walleij 14708d318a50SLinus Walleij static irqreturn_t d40_handle_interrupt(int irq, void *data) 14718d318a50SLinus Walleij { 14728d318a50SLinus Walleij static const struct d40_interrupt_lookup il[] = { 14738d318a50SLinus Walleij {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0}, 14748d318a50SLinus Walleij {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32}, 14758d318a50SLinus Walleij {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64}, 14768d318a50SLinus Walleij {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96}, 14778d318a50SLinus Walleij {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0}, 14788d318a50SLinus Walleij {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32}, 14798d318a50SLinus Walleij {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64}, 14808d318a50SLinus Walleij {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96}, 14818d318a50SLinus Walleij {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN}, 14828d318a50SLinus Walleij {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN}, 14838d318a50SLinus Walleij }; 14848d318a50SLinus Walleij 14858d318a50SLinus Walleij int i; 14868d318a50SLinus Walleij u32 regs[ARRAY_SIZE(il)]; 14878d318a50SLinus Walleij u32 idx; 14888d318a50SLinus Walleij u32 row; 14898d318a50SLinus Walleij long chan = -1; 14908d318a50SLinus Walleij struct d40_chan *d40c; 14918d318a50SLinus Walleij unsigned long flags; 14928d318a50SLinus Walleij struct d40_base *base = data; 14938d318a50SLinus Walleij 14948d318a50SLinus Walleij spin_lock_irqsave(&base->interrupt_lock, flags); 14958d318a50SLinus Walleij 14968d318a50SLinus Walleij /* Read interrupt status of both logical and physical channels */ 14978d318a50SLinus Walleij for (i = 0; i < ARRAY_SIZE(il); i++) 14988d318a50SLinus Walleij regs[i] = readl(base->virtbase + il[i].src); 14998d318a50SLinus Walleij 15008d318a50SLinus Walleij for (;;) { 15018d318a50SLinus Walleij 15028d318a50SLinus Walleij chan = find_next_bit((unsigned long *)regs, 15038d318a50SLinus Walleij BITS_PER_LONG * ARRAY_SIZE(il), chan + 1); 15048d318a50SLinus Walleij 15058d318a50SLinus Walleij /* No more set bits found? */ 15068d318a50SLinus Walleij if (chan == BITS_PER_LONG * ARRAY_SIZE(il)) 15078d318a50SLinus Walleij break; 15088d318a50SLinus Walleij 15098d318a50SLinus Walleij row = chan / BITS_PER_LONG; 15108d318a50SLinus Walleij idx = chan & (BITS_PER_LONG - 1); 15118d318a50SLinus Walleij 15128d318a50SLinus Walleij /* ACK interrupt */ 15131b00348dSJonas Aaberg writel(1 << idx, base->virtbase + il[row].clr); 15148d318a50SLinus Walleij 15158d318a50SLinus Walleij if (il[row].offset == D40_PHY_CHAN) 15168d318a50SLinus Walleij d40c = base->lookup_phy_chans[idx]; 15178d318a50SLinus Walleij else 15188d318a50SLinus Walleij d40c = base->lookup_log_chans[il[row].offset + idx]; 15198d318a50SLinus Walleij spin_lock(&d40c->lock); 15208d318a50SLinus Walleij 15218d318a50SLinus Walleij if (!il[row].is_error) 15228d318a50SLinus Walleij dma_tc_handle(d40c); 15238d318a50SLinus Walleij else 15246db5a8baSRabin Vincent d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n", 15256db5a8baSRabin Vincent chan, il[row].offset, idx); 15268d318a50SLinus Walleij 15278d318a50SLinus Walleij spin_unlock(&d40c->lock); 15288d318a50SLinus Walleij } 15298d318a50SLinus Walleij 15308d318a50SLinus Walleij spin_unlock_irqrestore(&base->interrupt_lock, flags); 15318d318a50SLinus Walleij 15328d318a50SLinus Walleij return IRQ_HANDLED; 15338d318a50SLinus Walleij } 15348d318a50SLinus Walleij 15358d318a50SLinus Walleij static int d40_validate_conf(struct d40_chan *d40c, 15368d318a50SLinus Walleij struct stedma40_chan_cfg *conf) 15378d318a50SLinus Walleij { 15388d318a50SLinus Walleij int res = 0; 15398d318a50SLinus Walleij u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type); 15408d318a50SLinus Walleij u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type); 154138bdbf02SRabin Vincent bool is_log = conf->mode == STEDMA40_MODE_LOGICAL; 15428d318a50SLinus Walleij 15430747c7baSLinus Walleij if (!conf->dir) { 15446db5a8baSRabin Vincent chan_err(d40c, "Invalid direction.\n"); 15450747c7baSLinus Walleij res = -EINVAL; 15460747c7baSLinus Walleij } 15470747c7baSLinus Walleij 15480747c7baSLinus Walleij if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY && 15490747c7baSLinus Walleij d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 && 15500747c7baSLinus Walleij d40c->runtime_addr == 0) { 15510747c7baSLinus Walleij 15526db5a8baSRabin Vincent chan_err(d40c, "Invalid TX channel address (%d)\n", 15536db5a8baSRabin Vincent conf->dst_dev_type); 15540747c7baSLinus Walleij res = -EINVAL; 15550747c7baSLinus Walleij } 15560747c7baSLinus Walleij 15570747c7baSLinus Walleij if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY && 15580747c7baSLinus Walleij d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 && 15590747c7baSLinus Walleij d40c->runtime_addr == 0) { 15606db5a8baSRabin Vincent chan_err(d40c, "Invalid RX channel address (%d)\n", 15616db5a8baSRabin Vincent conf->src_dev_type); 15620747c7baSLinus Walleij res = -EINVAL; 15630747c7baSLinus Walleij } 15640747c7baSLinus Walleij 15650747c7baSLinus Walleij if (conf->dir == STEDMA40_MEM_TO_PERIPH && 15668d318a50SLinus Walleij dst_event_group == STEDMA40_DEV_DST_MEMORY) { 15676db5a8baSRabin Vincent chan_err(d40c, "Invalid dst\n"); 15688d318a50SLinus Walleij res = -EINVAL; 15698d318a50SLinus Walleij } 15708d318a50SLinus Walleij 15710747c7baSLinus Walleij if (conf->dir == STEDMA40_PERIPH_TO_MEM && 15728d318a50SLinus Walleij src_event_group == STEDMA40_DEV_SRC_MEMORY) { 15736db5a8baSRabin Vincent chan_err(d40c, "Invalid src\n"); 15748d318a50SLinus Walleij res = -EINVAL; 15758d318a50SLinus Walleij } 15768d318a50SLinus Walleij 15778d318a50SLinus Walleij if (src_event_group == STEDMA40_DEV_SRC_MEMORY && 15788d318a50SLinus Walleij dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) { 15796db5a8baSRabin Vincent chan_err(d40c, "No event line\n"); 15808d318a50SLinus Walleij res = -EINVAL; 15818d318a50SLinus Walleij } 15828d318a50SLinus Walleij 15838d318a50SLinus Walleij if (conf->dir == STEDMA40_PERIPH_TO_PERIPH && 15848d318a50SLinus Walleij (src_event_group != dst_event_group)) { 15856db5a8baSRabin Vincent chan_err(d40c, "Invalid event group\n"); 15868d318a50SLinus Walleij res = -EINVAL; 15878d318a50SLinus Walleij } 15888d318a50SLinus Walleij 15898d318a50SLinus Walleij if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) { 15908d318a50SLinus Walleij /* 15918d318a50SLinus Walleij * DMAC HW supports it. Will be added to this driver, 15928d318a50SLinus Walleij * in case any dma client requires it. 15938d318a50SLinus Walleij */ 15946db5a8baSRabin Vincent chan_err(d40c, "periph to periph not supported\n"); 15958d318a50SLinus Walleij res = -EINVAL; 15968d318a50SLinus Walleij } 15978d318a50SLinus Walleij 1598d49278e3SPer Forlin if (d40_psize_2_burst_size(is_log, conf->src_info.psize) * 1599d49278e3SPer Forlin (1 << conf->src_info.data_width) != 1600d49278e3SPer Forlin d40_psize_2_burst_size(is_log, conf->dst_info.psize) * 1601d49278e3SPer Forlin (1 << conf->dst_info.data_width)) { 1602d49278e3SPer Forlin /* 1603d49278e3SPer Forlin * The DMAC hardware only supports 1604d49278e3SPer Forlin * src (burst x width) == dst (burst x width) 1605d49278e3SPer Forlin */ 1606d49278e3SPer Forlin 16076db5a8baSRabin Vincent chan_err(d40c, "src (burst x width) != dst (burst x width)\n"); 1608d49278e3SPer Forlin res = -EINVAL; 1609d49278e3SPer Forlin } 1610d49278e3SPer Forlin 16118d318a50SLinus Walleij return res; 16128d318a50SLinus Walleij } 16138d318a50SLinus Walleij 16145cd326fdSNarayanan G static bool d40_alloc_mask_set(struct d40_phy_res *phy, 16155cd326fdSNarayanan G bool is_src, int log_event_line, bool is_log, 16165cd326fdSNarayanan G bool *first_user) 16178d318a50SLinus Walleij { 16188d318a50SLinus Walleij unsigned long flags; 16198d318a50SLinus Walleij spin_lock_irqsave(&phy->lock, flags); 16205cd326fdSNarayanan G 16215cd326fdSNarayanan G *first_user = ((phy->allocated_src | phy->allocated_dst) 16225cd326fdSNarayanan G == D40_ALLOC_FREE); 16235cd326fdSNarayanan G 16244aed79b2SMarcin Mielczarczyk if (!is_log) { 16258d318a50SLinus Walleij /* Physical interrupts are masked per physical full channel */ 16268d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE && 16278d318a50SLinus Walleij phy->allocated_dst == D40_ALLOC_FREE) { 16288d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_PHY; 16298d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_PHY; 16308d318a50SLinus Walleij goto found; 16318d318a50SLinus Walleij } else 16328d318a50SLinus Walleij goto not_found; 16338d318a50SLinus Walleij } 16348d318a50SLinus Walleij 16358d318a50SLinus Walleij /* Logical channel */ 16368d318a50SLinus Walleij if (is_src) { 16378d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_PHY) 16388d318a50SLinus Walleij goto not_found; 16398d318a50SLinus Walleij 16408d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE) 16418d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_LOG_FREE; 16428d318a50SLinus Walleij 16438d318a50SLinus Walleij if (!(phy->allocated_src & (1 << log_event_line))) { 16448d318a50SLinus Walleij phy->allocated_src |= 1 << log_event_line; 16458d318a50SLinus Walleij goto found; 16468d318a50SLinus Walleij } else 16478d318a50SLinus Walleij goto not_found; 16488d318a50SLinus Walleij } else { 16498d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_PHY) 16508d318a50SLinus Walleij goto not_found; 16518d318a50SLinus Walleij 16528d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_FREE) 16538d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_LOG_FREE; 16548d318a50SLinus Walleij 16558d318a50SLinus Walleij if (!(phy->allocated_dst & (1 << log_event_line))) { 16568d318a50SLinus Walleij phy->allocated_dst |= 1 << log_event_line; 16578d318a50SLinus Walleij goto found; 16588d318a50SLinus Walleij } else 16598d318a50SLinus Walleij goto not_found; 16608d318a50SLinus Walleij } 16618d318a50SLinus Walleij 16628d318a50SLinus Walleij not_found: 16638d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 16648d318a50SLinus Walleij return false; 16658d318a50SLinus Walleij found: 16668d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 16678d318a50SLinus Walleij return true; 16688d318a50SLinus Walleij } 16698d318a50SLinus Walleij 16708d318a50SLinus Walleij static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src, 16718d318a50SLinus Walleij int log_event_line) 16728d318a50SLinus Walleij { 16738d318a50SLinus Walleij unsigned long flags; 16748d318a50SLinus Walleij bool is_free = false; 16758d318a50SLinus Walleij 16768d318a50SLinus Walleij spin_lock_irqsave(&phy->lock, flags); 16778d318a50SLinus Walleij if (!log_event_line) { 16788d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE; 16798d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE; 16808d318a50SLinus Walleij is_free = true; 16818d318a50SLinus Walleij goto out; 16828d318a50SLinus Walleij } 16838d318a50SLinus Walleij 16848d318a50SLinus Walleij /* Logical channel */ 16858d318a50SLinus Walleij if (is_src) { 16868d318a50SLinus Walleij phy->allocated_src &= ~(1 << log_event_line); 16878d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_LOG_FREE) 16888d318a50SLinus Walleij phy->allocated_src = D40_ALLOC_FREE; 16898d318a50SLinus Walleij } else { 16908d318a50SLinus Walleij phy->allocated_dst &= ~(1 << log_event_line); 16918d318a50SLinus Walleij if (phy->allocated_dst == D40_ALLOC_LOG_FREE) 16928d318a50SLinus Walleij phy->allocated_dst = D40_ALLOC_FREE; 16938d318a50SLinus Walleij } 16948d318a50SLinus Walleij 16958d318a50SLinus Walleij is_free = ((phy->allocated_src | phy->allocated_dst) == 16968d318a50SLinus Walleij D40_ALLOC_FREE); 16978d318a50SLinus Walleij 16988d318a50SLinus Walleij out: 16998d318a50SLinus Walleij spin_unlock_irqrestore(&phy->lock, flags); 17008d318a50SLinus Walleij 17018d318a50SLinus Walleij return is_free; 17028d318a50SLinus Walleij } 17038d318a50SLinus Walleij 17045cd326fdSNarayanan G static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user) 17058d318a50SLinus Walleij { 17068d318a50SLinus Walleij int dev_type; 17078d318a50SLinus Walleij int event_group; 17088d318a50SLinus Walleij int event_line; 17098d318a50SLinus Walleij struct d40_phy_res *phys; 17108d318a50SLinus Walleij int i; 17118d318a50SLinus Walleij int j; 17128d318a50SLinus Walleij int log_num; 17138d318a50SLinus Walleij bool is_src; 171438bdbf02SRabin Vincent bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL; 17158d318a50SLinus Walleij 17168d318a50SLinus Walleij phys = d40c->base->phy_res; 17178d318a50SLinus Walleij 17188d318a50SLinus Walleij if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { 17198d318a50SLinus Walleij dev_type = d40c->dma_cfg.src_dev_type; 17208d318a50SLinus Walleij log_num = 2 * dev_type; 17218d318a50SLinus Walleij is_src = true; 17228d318a50SLinus Walleij } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || 17238d318a50SLinus Walleij d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { 17248d318a50SLinus Walleij /* dst event lines are used for logical memcpy */ 17258d318a50SLinus Walleij dev_type = d40c->dma_cfg.dst_dev_type; 17268d318a50SLinus Walleij log_num = 2 * dev_type + 1; 17278d318a50SLinus Walleij is_src = false; 17288d318a50SLinus Walleij } else 17298d318a50SLinus Walleij return -EINVAL; 17308d318a50SLinus Walleij 17318d318a50SLinus Walleij event_group = D40_TYPE_TO_GROUP(dev_type); 17328d318a50SLinus Walleij event_line = D40_TYPE_TO_EVENT(dev_type); 17338d318a50SLinus Walleij 17348d318a50SLinus Walleij if (!is_log) { 17358d318a50SLinus Walleij if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { 17368d318a50SLinus Walleij /* Find physical half channel */ 17378d318a50SLinus Walleij for (i = 0; i < d40c->base->num_phy_chans; i++) { 17388d318a50SLinus Walleij 17394aed79b2SMarcin Mielczarczyk if (d40_alloc_mask_set(&phys[i], is_src, 17405cd326fdSNarayanan G 0, is_log, 17415cd326fdSNarayanan G first_phy_user)) 17428d318a50SLinus Walleij goto found_phy; 17438d318a50SLinus Walleij } 17448d318a50SLinus Walleij } else 17458d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) { 17468d318a50SLinus Walleij int phy_num = j + event_group * 2; 17478d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) { 1748508849adSLinus Walleij if (d40_alloc_mask_set(&phys[i], 1749508849adSLinus Walleij is_src, 1750508849adSLinus Walleij 0, 17515cd326fdSNarayanan G is_log, 17525cd326fdSNarayanan G first_phy_user)) 17538d318a50SLinus Walleij goto found_phy; 17548d318a50SLinus Walleij } 17558d318a50SLinus Walleij } 17568d318a50SLinus Walleij return -EINVAL; 17578d318a50SLinus Walleij found_phy: 17588d318a50SLinus Walleij d40c->phy_chan = &phys[i]; 17598d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN; 17608d318a50SLinus Walleij goto out; 17618d318a50SLinus Walleij } 17628d318a50SLinus Walleij if (dev_type == -1) 17638d318a50SLinus Walleij return -EINVAL; 17648d318a50SLinus Walleij 17658d318a50SLinus Walleij /* Find logical channel */ 17668d318a50SLinus Walleij for (j = 0; j < d40c->base->num_phy_chans; j += 8) { 17678d318a50SLinus Walleij int phy_num = j + event_group * 2; 17685cd326fdSNarayanan G 17695cd326fdSNarayanan G if (d40c->dma_cfg.use_fixed_channel) { 17705cd326fdSNarayanan G i = d40c->dma_cfg.phy_channel; 17715cd326fdSNarayanan G 17725cd326fdSNarayanan G if ((i != phy_num) && (i != phy_num + 1)) { 17735cd326fdSNarayanan G dev_err(chan2dev(d40c), 17745cd326fdSNarayanan G "invalid fixed phy channel %d\n", i); 17755cd326fdSNarayanan G return -EINVAL; 17765cd326fdSNarayanan G } 17775cd326fdSNarayanan G 17785cd326fdSNarayanan G if (d40_alloc_mask_set(&phys[i], is_src, event_line, 17795cd326fdSNarayanan G is_log, first_phy_user)) 17805cd326fdSNarayanan G goto found_log; 17815cd326fdSNarayanan G 17825cd326fdSNarayanan G dev_err(chan2dev(d40c), 17835cd326fdSNarayanan G "could not allocate fixed phy channel %d\n", i); 17845cd326fdSNarayanan G return -EINVAL; 17855cd326fdSNarayanan G } 17865cd326fdSNarayanan G 17878d318a50SLinus Walleij /* 17888d318a50SLinus Walleij * Spread logical channels across all available physical rather 17898d318a50SLinus Walleij * than pack every logical channel at the first available phy 17908d318a50SLinus Walleij * channels. 17918d318a50SLinus Walleij */ 17928d318a50SLinus Walleij if (is_src) { 17938d318a50SLinus Walleij for (i = phy_num; i < phy_num + 2; i++) { 17948d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src, 17955cd326fdSNarayanan G event_line, is_log, 17965cd326fdSNarayanan G first_phy_user)) 17978d318a50SLinus Walleij goto found_log; 17988d318a50SLinus Walleij } 17998d318a50SLinus Walleij } else { 18008d318a50SLinus Walleij for (i = phy_num + 1; i >= phy_num; i--) { 18018d318a50SLinus Walleij if (d40_alloc_mask_set(&phys[i], is_src, 18025cd326fdSNarayanan G event_line, is_log, 18035cd326fdSNarayanan G first_phy_user)) 18048d318a50SLinus Walleij goto found_log; 18058d318a50SLinus Walleij } 18068d318a50SLinus Walleij } 18078d318a50SLinus Walleij } 18088d318a50SLinus Walleij return -EINVAL; 18098d318a50SLinus Walleij 18108d318a50SLinus Walleij found_log: 18118d318a50SLinus Walleij d40c->phy_chan = &phys[i]; 18128d318a50SLinus Walleij d40c->log_num = log_num; 18138d318a50SLinus Walleij out: 18148d318a50SLinus Walleij 18158d318a50SLinus Walleij if (is_log) 18168d318a50SLinus Walleij d40c->base->lookup_log_chans[d40c->log_num] = d40c; 18178d318a50SLinus Walleij else 18188d318a50SLinus Walleij d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c; 18198d318a50SLinus Walleij 18208d318a50SLinus Walleij return 0; 18218d318a50SLinus Walleij 18228d318a50SLinus Walleij } 18238d318a50SLinus Walleij 18248d318a50SLinus Walleij static int d40_config_memcpy(struct d40_chan *d40c) 18258d318a50SLinus Walleij { 18268d318a50SLinus Walleij dma_cap_mask_t cap = d40c->chan.device->cap_mask; 18278d318a50SLinus Walleij 18288d318a50SLinus Walleij if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) { 18298d318a50SLinus Walleij d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log; 18308d318a50SLinus Walleij d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY; 18318d318a50SLinus Walleij d40c->dma_cfg.dst_dev_type = d40c->base->plat_data-> 18328d318a50SLinus Walleij memcpy[d40c->chan.chan_id]; 18338d318a50SLinus Walleij 18348d318a50SLinus Walleij } else if (dma_has_cap(DMA_MEMCPY, cap) && 18358d318a50SLinus Walleij dma_has_cap(DMA_SLAVE, cap)) { 18368d318a50SLinus Walleij d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy; 18378d318a50SLinus Walleij } else { 18386db5a8baSRabin Vincent chan_err(d40c, "No memcpy\n"); 18398d318a50SLinus Walleij return -EINVAL; 18408d318a50SLinus Walleij } 18418d318a50SLinus Walleij 18428d318a50SLinus Walleij return 0; 18438d318a50SLinus Walleij } 18448d318a50SLinus Walleij 18458d318a50SLinus Walleij static int d40_free_dma(struct d40_chan *d40c) 18468d318a50SLinus Walleij { 18478d318a50SLinus Walleij 18488d318a50SLinus Walleij int res = 0; 1849d181b3a8SJonas Aaberg u32 event; 18508d318a50SLinus Walleij struct d40_phy_res *phy = d40c->phy_chan; 18518d318a50SLinus Walleij bool is_src; 18528d318a50SLinus Walleij 18538d318a50SLinus Walleij /* Terminate all queued and active transfers */ 18548d318a50SLinus Walleij d40_term_all(d40c); 18558d318a50SLinus Walleij 18568d318a50SLinus Walleij if (phy == NULL) { 18576db5a8baSRabin Vincent chan_err(d40c, "phy == null\n"); 18588d318a50SLinus Walleij return -EINVAL; 18598d318a50SLinus Walleij } 18608d318a50SLinus Walleij 18618d318a50SLinus Walleij if (phy->allocated_src == D40_ALLOC_FREE && 18628d318a50SLinus Walleij phy->allocated_dst == D40_ALLOC_FREE) { 18636db5a8baSRabin Vincent chan_err(d40c, "channel already free\n"); 18648d318a50SLinus Walleij return -EINVAL; 18658d318a50SLinus Walleij } 18668d318a50SLinus Walleij 18678d318a50SLinus Walleij if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || 18688d318a50SLinus Walleij d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { 18698d318a50SLinus Walleij event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); 18708d318a50SLinus Walleij is_src = false; 18718d318a50SLinus Walleij } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { 18728d318a50SLinus Walleij event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); 18738d318a50SLinus Walleij is_src = true; 18748d318a50SLinus Walleij } else { 18756db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n"); 18768d318a50SLinus Walleij return -EINVAL; 18778d318a50SLinus Walleij } 18788d318a50SLinus Walleij 18797fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 18808d318a50SLinus Walleij res = d40_channel_execute_command(d40c, D40_DMA_STOP); 18818d318a50SLinus Walleij if (res) { 18821bdae6f4SNarayanan G chan_err(d40c, "stop failed\n"); 18837fb3e75eSNarayanan G goto out; 18848d318a50SLinus Walleij } 18857fb3e75eSNarayanan G 18861bdae6f4SNarayanan G d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0); 18871bdae6f4SNarayanan G 18881bdae6f4SNarayanan G if (chan_is_logical(d40c)) 18891bdae6f4SNarayanan G d40c->base->lookup_log_chans[d40c->log_num] = NULL; 18901bdae6f4SNarayanan G else 18911bdae6f4SNarayanan G d40c->base->lookup_phy_chans[phy->num] = NULL; 18921bdae6f4SNarayanan G 18937fb3e75eSNarayanan G if (d40c->busy) { 18947fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 18957fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 18967fb3e75eSNarayanan G } 18977fb3e75eSNarayanan G 18987fb3e75eSNarayanan G d40c->busy = false; 18998d318a50SLinus Walleij d40c->phy_chan = NULL; 1900ce2ca125SRabin Vincent d40c->configured = false; 19017fb3e75eSNarayanan G out: 19028d318a50SLinus Walleij 19037fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 19047fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 19057fb3e75eSNarayanan G return res; 19068d318a50SLinus Walleij } 19078d318a50SLinus Walleij 1908a5ebca47SJonas Aaberg static bool d40_is_paused(struct d40_chan *d40c) 1909a5ebca47SJonas Aaberg { 19108ca84687SRabin Vincent void __iomem *chanbase = chan_base(d40c); 1911a5ebca47SJonas Aaberg bool is_paused = false; 1912a5ebca47SJonas Aaberg unsigned long flags; 1913a5ebca47SJonas Aaberg void __iomem *active_reg; 1914a5ebca47SJonas Aaberg u32 status; 1915a5ebca47SJonas Aaberg u32 event; 1916a5ebca47SJonas Aaberg 1917a5ebca47SJonas Aaberg spin_lock_irqsave(&d40c->lock, flags); 1918a5ebca47SJonas Aaberg 1919724a8577SRabin Vincent if (chan_is_physical(d40c)) { 1920a5ebca47SJonas Aaberg if (d40c->phy_chan->num % 2 == 0) 1921a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; 1922a5ebca47SJonas Aaberg else 1923a5ebca47SJonas Aaberg active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; 1924a5ebca47SJonas Aaberg 1925a5ebca47SJonas Aaberg status = (readl(active_reg) & 1926a5ebca47SJonas Aaberg D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> 1927a5ebca47SJonas Aaberg D40_CHAN_POS(d40c->phy_chan->num); 1928a5ebca47SJonas Aaberg if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) 1929a5ebca47SJonas Aaberg is_paused = true; 1930a5ebca47SJonas Aaberg 1931a5ebca47SJonas Aaberg goto _exit; 1932a5ebca47SJonas Aaberg } 1933a5ebca47SJonas Aaberg 1934a5ebca47SJonas Aaberg if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || 19359dbfbd35SJonas Aaberg d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { 1936a5ebca47SJonas Aaberg event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); 19378ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SDLNK); 19389dbfbd35SJonas Aaberg } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { 1939a5ebca47SJonas Aaberg event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); 19408ca84687SRabin Vincent status = readl(chanbase + D40_CHAN_REG_SSLNK); 19419dbfbd35SJonas Aaberg } else { 19426db5a8baSRabin Vincent chan_err(d40c, "Unknown direction\n"); 1943a5ebca47SJonas Aaberg goto _exit; 1944a5ebca47SJonas Aaberg } 19459dbfbd35SJonas Aaberg 1946a5ebca47SJonas Aaberg status = (status & D40_EVENTLINE_MASK(event)) >> 1947a5ebca47SJonas Aaberg D40_EVENTLINE_POS(event); 1948a5ebca47SJonas Aaberg 1949a5ebca47SJonas Aaberg if (status != D40_DMA_RUN) 1950a5ebca47SJonas Aaberg is_paused = true; 1951a5ebca47SJonas Aaberg _exit: 1952a5ebca47SJonas Aaberg spin_unlock_irqrestore(&d40c->lock, flags); 1953a5ebca47SJonas Aaberg return is_paused; 1954a5ebca47SJonas Aaberg 1955a5ebca47SJonas Aaberg } 1956a5ebca47SJonas Aaberg 1957a5ebca47SJonas Aaberg 19588d318a50SLinus Walleij static u32 stedma40_residue(struct dma_chan *chan) 19598d318a50SLinus Walleij { 19608d318a50SLinus Walleij struct d40_chan *d40c = 19618d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 19628d318a50SLinus Walleij u32 bytes_left; 19638d318a50SLinus Walleij unsigned long flags; 19648d318a50SLinus Walleij 19658d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 19668d318a50SLinus Walleij bytes_left = d40_residue(d40c); 19678d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 19688d318a50SLinus Walleij 19698d318a50SLinus Walleij return bytes_left; 19708d318a50SLinus Walleij } 19718d318a50SLinus Walleij 19723e3a0763SRabin Vincent static int 19733e3a0763SRabin Vincent d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc, 19743e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst, 1975822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr, 1976822c5676SRabin Vincent dma_addr_t dst_dev_addr) 19773e3a0763SRabin Vincent { 19783e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 19793e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info; 19803e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info; 19815ed04b85SRabin Vincent int ret; 19823e3a0763SRabin Vincent 19835ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_src, sg_len, 19845ed04b85SRabin Vincent src_dev_addr, 19853e3a0763SRabin Vincent desc->lli_log.src, 19863e3a0763SRabin Vincent chan->log_def.lcsp1, 19873e3a0763SRabin Vincent src_info->data_width, 19883e3a0763SRabin Vincent dst_info->data_width); 19893e3a0763SRabin Vincent 19905ed04b85SRabin Vincent ret = d40_log_sg_to_lli(sg_dst, sg_len, 19915ed04b85SRabin Vincent dst_dev_addr, 19923e3a0763SRabin Vincent desc->lli_log.dst, 19933e3a0763SRabin Vincent chan->log_def.lcsp3, 19943e3a0763SRabin Vincent dst_info->data_width, 19953e3a0763SRabin Vincent src_info->data_width); 19963e3a0763SRabin Vincent 19975ed04b85SRabin Vincent return ret < 0 ? ret : 0; 19983e3a0763SRabin Vincent } 19993e3a0763SRabin Vincent 20003e3a0763SRabin Vincent static int 20013e3a0763SRabin Vincent d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc, 20023e3a0763SRabin Vincent struct scatterlist *sg_src, struct scatterlist *sg_dst, 2003822c5676SRabin Vincent unsigned int sg_len, dma_addr_t src_dev_addr, 2004822c5676SRabin Vincent dma_addr_t dst_dev_addr) 20053e3a0763SRabin Vincent { 20063e3a0763SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 20073e3a0763SRabin Vincent struct stedma40_half_channel_info *src_info = &cfg->src_info; 20083e3a0763SRabin Vincent struct stedma40_half_channel_info *dst_info = &cfg->dst_info; 20090c842b55SRabin Vincent unsigned long flags = 0; 20103e3a0763SRabin Vincent int ret; 20113e3a0763SRabin Vincent 20120c842b55SRabin Vincent if (desc->cyclic) 20130c842b55SRabin Vincent flags |= LLI_CYCLIC | LLI_TERM_INT; 20140c842b55SRabin Vincent 20153e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr, 20163e3a0763SRabin Vincent desc->lli_phy.src, 20173e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.src), 20183e3a0763SRabin Vincent chan->src_def_cfg, 20190c842b55SRabin Vincent src_info, dst_info, flags); 20203e3a0763SRabin Vincent 20213e3a0763SRabin Vincent ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr, 20223e3a0763SRabin Vincent desc->lli_phy.dst, 20233e3a0763SRabin Vincent virt_to_phys(desc->lli_phy.dst), 20243e3a0763SRabin Vincent chan->dst_def_cfg, 20250c842b55SRabin Vincent dst_info, src_info, flags); 20263e3a0763SRabin Vincent 20273e3a0763SRabin Vincent dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr, 20283e3a0763SRabin Vincent desc->lli_pool.size, DMA_TO_DEVICE); 20293e3a0763SRabin Vincent 20303e3a0763SRabin Vincent return ret < 0 ? ret : 0; 20313e3a0763SRabin Vincent } 20323e3a0763SRabin Vincent 20333e3a0763SRabin Vincent 20345f81158fSRabin Vincent static struct d40_desc * 20355f81158fSRabin Vincent d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg, 20365f81158fSRabin Vincent unsigned int sg_len, unsigned long dma_flags) 20375f81158fSRabin Vincent { 20385f81158fSRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 20395f81158fSRabin Vincent struct d40_desc *desc; 2040dbd88788SRabin Vincent int ret; 20415f81158fSRabin Vincent 20425f81158fSRabin Vincent desc = d40_desc_get(chan); 20435f81158fSRabin Vincent if (!desc) 20445f81158fSRabin Vincent return NULL; 20455f81158fSRabin Vincent 20465f81158fSRabin Vincent desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width, 20475f81158fSRabin Vincent cfg->dst_info.data_width); 20485f81158fSRabin Vincent if (desc->lli_len < 0) { 20495f81158fSRabin Vincent chan_err(chan, "Unaligned size\n"); 2050dbd88788SRabin Vincent goto err; 20515f81158fSRabin Vincent } 20525f81158fSRabin Vincent 2053dbd88788SRabin Vincent ret = d40_pool_lli_alloc(chan, desc, desc->lli_len); 2054dbd88788SRabin Vincent if (ret < 0) { 2055dbd88788SRabin Vincent chan_err(chan, "Could not allocate lli\n"); 2056dbd88788SRabin Vincent goto err; 2057dbd88788SRabin Vincent } 2058dbd88788SRabin Vincent 2059dbd88788SRabin Vincent 20605f81158fSRabin Vincent desc->lli_current = 0; 20615f81158fSRabin Vincent desc->txd.flags = dma_flags; 20625f81158fSRabin Vincent desc->txd.tx_submit = d40_tx_submit; 20635f81158fSRabin Vincent 20645f81158fSRabin Vincent dma_async_tx_descriptor_init(&desc->txd, &chan->chan); 20655f81158fSRabin Vincent 20665f81158fSRabin Vincent return desc; 2067dbd88788SRabin Vincent 2068dbd88788SRabin Vincent err: 2069dbd88788SRabin Vincent d40_desc_free(chan, desc); 2070dbd88788SRabin Vincent return NULL; 20715f81158fSRabin Vincent } 20725f81158fSRabin Vincent 2073cade1d30SRabin Vincent static dma_addr_t 2074db8196dfSVinod Koul d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction) 20758d318a50SLinus Walleij { 2076cade1d30SRabin Vincent struct stedma40_platform_data *plat = chan->base->plat_data; 2077cade1d30SRabin Vincent struct stedma40_chan_cfg *cfg = &chan->dma_cfg; 2078711b9ceaSPhilippe Langlais dma_addr_t addr = 0; 20798d318a50SLinus Walleij 2080cade1d30SRabin Vincent if (chan->runtime_addr) 2081cade1d30SRabin Vincent return chan->runtime_addr; 2082cade1d30SRabin Vincent 2083db8196dfSVinod Koul if (direction == DMA_DEV_TO_MEM) 2084cade1d30SRabin Vincent addr = plat->dev_rx[cfg->src_dev_type]; 2085db8196dfSVinod Koul else if (direction == DMA_MEM_TO_DEV) 2086cade1d30SRabin Vincent addr = plat->dev_tx[cfg->dst_dev_type]; 2087cade1d30SRabin Vincent 2088cade1d30SRabin Vincent return addr; 20890d0f6b8bSJonas Aaberg } 20900d0f6b8bSJonas Aaberg 2091cade1d30SRabin Vincent static struct dma_async_tx_descriptor * 2092cade1d30SRabin Vincent d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src, 2093cade1d30SRabin Vincent struct scatterlist *sg_dst, unsigned int sg_len, 2094db8196dfSVinod Koul enum dma_transfer_direction direction, unsigned long dma_flags) 2095cade1d30SRabin Vincent { 2096cade1d30SRabin Vincent struct d40_chan *chan = container_of(dchan, struct d40_chan, chan); 2097822c5676SRabin Vincent dma_addr_t src_dev_addr = 0; 2098822c5676SRabin Vincent dma_addr_t dst_dev_addr = 0; 2099cade1d30SRabin Vincent struct d40_desc *desc; 2100cade1d30SRabin Vincent unsigned long flags; 2101cade1d30SRabin Vincent int ret; 21028d318a50SLinus Walleij 2103cade1d30SRabin Vincent if (!chan->phy_chan) { 2104cade1d30SRabin Vincent chan_err(chan, "Cannot prepare unallocated channel\n"); 2105cade1d30SRabin Vincent return NULL; 2106cade1d30SRabin Vincent } 2107cade1d30SRabin Vincent 21080c842b55SRabin Vincent 2109cade1d30SRabin Vincent spin_lock_irqsave(&chan->lock, flags); 2110cade1d30SRabin Vincent 2111cade1d30SRabin Vincent desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags); 2112cade1d30SRabin Vincent if (desc == NULL) 21138d318a50SLinus Walleij goto err; 21148d318a50SLinus Walleij 21150c842b55SRabin Vincent if (sg_next(&sg_src[sg_len - 1]) == sg_src) 21160c842b55SRabin Vincent desc->cyclic = true; 21170c842b55SRabin Vincent 21187e426da8SLinus Walleij if (direction != DMA_TRANS_NONE) { 2119822c5676SRabin Vincent dma_addr_t dev_addr = d40_get_dev_addr(chan, direction); 2120822c5676SRabin Vincent 2121db8196dfSVinod Koul if (direction == DMA_DEV_TO_MEM) 2122822c5676SRabin Vincent src_dev_addr = dev_addr; 2123db8196dfSVinod Koul else if (direction == DMA_MEM_TO_DEV) 2124822c5676SRabin Vincent dst_dev_addr = dev_addr; 2125822c5676SRabin Vincent } 2126cade1d30SRabin Vincent 2127cade1d30SRabin Vincent if (chan_is_logical(chan)) 2128cade1d30SRabin Vincent ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst, 2129822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr); 2130cade1d30SRabin Vincent else 2131cade1d30SRabin Vincent ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst, 2132822c5676SRabin Vincent sg_len, src_dev_addr, dst_dev_addr); 2133cade1d30SRabin Vincent 2134cade1d30SRabin Vincent if (ret) { 2135cade1d30SRabin Vincent chan_err(chan, "Failed to prepare %s sg job: %d\n", 2136cade1d30SRabin Vincent chan_is_logical(chan) ? "log" : "phy", ret); 2137cade1d30SRabin Vincent goto err; 21388d318a50SLinus Walleij } 21398d318a50SLinus Walleij 214082babbb3SPer Forlin /* 214182babbb3SPer Forlin * add descriptor to the prepare queue in order to be able 214282babbb3SPer Forlin * to free them later in terminate_all 214382babbb3SPer Forlin */ 214482babbb3SPer Forlin list_add_tail(&desc->node, &chan->prepare_queue); 214582babbb3SPer Forlin 2146cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags); 21478d318a50SLinus Walleij 2148cade1d30SRabin Vincent return &desc->txd; 2149cade1d30SRabin Vincent 21508d318a50SLinus Walleij err: 2151cade1d30SRabin Vincent if (desc) 2152cade1d30SRabin Vincent d40_desc_free(chan, desc); 2153cade1d30SRabin Vincent spin_unlock_irqrestore(&chan->lock, flags); 21548d318a50SLinus Walleij return NULL; 21558d318a50SLinus Walleij } 21568d318a50SLinus Walleij 21578d318a50SLinus Walleij bool stedma40_filter(struct dma_chan *chan, void *data) 21588d318a50SLinus Walleij { 21598d318a50SLinus Walleij struct stedma40_chan_cfg *info = data; 21608d318a50SLinus Walleij struct d40_chan *d40c = 21618d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 21628d318a50SLinus Walleij int err; 21638d318a50SLinus Walleij 21648d318a50SLinus Walleij if (data) { 21658d318a50SLinus Walleij err = d40_validate_conf(d40c, info); 21668d318a50SLinus Walleij if (!err) 21678d318a50SLinus Walleij d40c->dma_cfg = *info; 21688d318a50SLinus Walleij } else 21698d318a50SLinus Walleij err = d40_config_memcpy(d40c); 21708d318a50SLinus Walleij 2171ce2ca125SRabin Vincent if (!err) 2172ce2ca125SRabin Vincent d40c->configured = true; 2173ce2ca125SRabin Vincent 21748d318a50SLinus Walleij return err == 0; 21758d318a50SLinus Walleij } 21768d318a50SLinus Walleij EXPORT_SYMBOL(stedma40_filter); 21778d318a50SLinus Walleij 2178ac2c0a38SRabin Vincent static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src) 2179ac2c0a38SRabin Vincent { 2180ac2c0a38SRabin Vincent bool realtime = d40c->dma_cfg.realtime; 2181ac2c0a38SRabin Vincent bool highprio = d40c->dma_cfg.high_priority; 2182ac2c0a38SRabin Vincent u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1; 2183ac2c0a38SRabin Vincent u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1; 2184ac2c0a38SRabin Vincent u32 event = D40_TYPE_TO_EVENT(dev_type); 2185ac2c0a38SRabin Vincent u32 group = D40_TYPE_TO_GROUP(dev_type); 2186ac2c0a38SRabin Vincent u32 bit = 1 << event; 2187ac2c0a38SRabin Vincent 2188ac2c0a38SRabin Vincent /* Destination event lines are stored in the upper halfword */ 2189ac2c0a38SRabin Vincent if (!src) 2190ac2c0a38SRabin Vincent bit <<= 16; 2191ac2c0a38SRabin Vincent 2192ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + prioreg + group * 4); 2193ac2c0a38SRabin Vincent writel(bit, d40c->base->virtbase + rtreg + group * 4); 2194ac2c0a38SRabin Vincent } 2195ac2c0a38SRabin Vincent 2196ac2c0a38SRabin Vincent static void d40_set_prio_realtime(struct d40_chan *d40c) 2197ac2c0a38SRabin Vincent { 2198ac2c0a38SRabin Vincent if (d40c->base->rev < 3) 2199ac2c0a38SRabin Vincent return; 2200ac2c0a38SRabin Vincent 2201ac2c0a38SRabin Vincent if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || 2202ac2c0a38SRabin Vincent (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) 2203ac2c0a38SRabin Vincent __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true); 2204ac2c0a38SRabin Vincent 2205ac2c0a38SRabin Vincent if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) || 2206ac2c0a38SRabin Vincent (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) 2207ac2c0a38SRabin Vincent __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false); 2208ac2c0a38SRabin Vincent } 2209ac2c0a38SRabin Vincent 22108d318a50SLinus Walleij /* DMA ENGINE functions */ 22118d318a50SLinus Walleij static int d40_alloc_chan_resources(struct dma_chan *chan) 22128d318a50SLinus Walleij { 22138d318a50SLinus Walleij int err; 22148d318a50SLinus Walleij unsigned long flags; 22158d318a50SLinus Walleij struct d40_chan *d40c = 22168d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 2217ef1872ecSLinus Walleij bool is_free_phy; 22188d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 22198d318a50SLinus Walleij 2220d3ee98cdSRussell King - ARM Linux dma_cookie_init(chan); 22218d318a50SLinus Walleij 2222ce2ca125SRabin Vincent /* If no dma configuration is set use default configuration (memcpy) */ 2223ce2ca125SRabin Vincent if (!d40c->configured) { 22248d318a50SLinus Walleij err = d40_config_memcpy(d40c); 2225ff0b12baSJonas Aaberg if (err) { 22266db5a8baSRabin Vincent chan_err(d40c, "Failed to configure memcpy channel\n"); 2227ff0b12baSJonas Aaberg goto fail; 2228ff0b12baSJonas Aaberg } 22298d318a50SLinus Walleij } 22308d318a50SLinus Walleij 22315cd326fdSNarayanan G err = d40_allocate_channel(d40c, &is_free_phy); 22328d318a50SLinus Walleij if (err) { 22336db5a8baSRabin Vincent chan_err(d40c, "Failed to allocate channel\n"); 22347fb3e75eSNarayanan G d40c->configured = false; 2235ff0b12baSJonas Aaberg goto fail; 22368d318a50SLinus Walleij } 22378d318a50SLinus Walleij 22387fb3e75eSNarayanan G pm_runtime_get_sync(d40c->base->dev); 2239ef1872ecSLinus Walleij /* Fill in basic CFG register values */ 2240ef1872ecSLinus Walleij d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg, 2241724a8577SRabin Vincent &d40c->dst_def_cfg, chan_is_logical(d40c)); 2242ef1872ecSLinus Walleij 2243ac2c0a38SRabin Vincent d40_set_prio_realtime(d40c); 2244ac2c0a38SRabin Vincent 2245724a8577SRabin Vincent if (chan_is_logical(d40c)) { 2246ef1872ecSLinus Walleij d40_log_cfg(&d40c->dma_cfg, 2247ef1872ecSLinus Walleij &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); 2248ef1872ecSLinus Walleij 2249ef1872ecSLinus Walleij if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) 2250ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base + 2251ef1872ecSLinus Walleij d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE; 2252ef1872ecSLinus Walleij else 2253ef1872ecSLinus Walleij d40c->lcpa = d40c->base->lcpa_base + 2254ef1872ecSLinus Walleij d40c->dma_cfg.dst_dev_type * 2255ef1872ecSLinus Walleij D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA; 2256ef1872ecSLinus Walleij } 2257ef1872ecSLinus Walleij 22585cd326fdSNarayanan G dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n", 22595cd326fdSNarayanan G chan_is_logical(d40c) ? "logical" : "physical", 22605cd326fdSNarayanan G d40c->phy_chan->num, 22615cd326fdSNarayanan G d40c->dma_cfg.use_fixed_channel ? ", fixed" : ""); 22625cd326fdSNarayanan G 22635cd326fdSNarayanan G 2264ef1872ecSLinus Walleij /* 2265ef1872ecSLinus Walleij * Only write channel configuration to the DMA if the physical 2266ef1872ecSLinus Walleij * resource is free. In case of multiple logical channels 2267ef1872ecSLinus Walleij * on the same physical resource, only the first write is necessary. 2268ef1872ecSLinus Walleij */ 2269b55912c6SJonas Aaberg if (is_free_phy) 2270b55912c6SJonas Aaberg d40_config_write(d40c); 2271ff0b12baSJonas Aaberg fail: 22727fb3e75eSNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 22737fb3e75eSNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 22748d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 2275ff0b12baSJonas Aaberg return err; 22768d318a50SLinus Walleij } 22778d318a50SLinus Walleij 22788d318a50SLinus Walleij static void d40_free_chan_resources(struct dma_chan *chan) 22798d318a50SLinus Walleij { 22808d318a50SLinus Walleij struct d40_chan *d40c = 22818d318a50SLinus Walleij container_of(chan, struct d40_chan, chan); 22828d318a50SLinus Walleij int err; 22838d318a50SLinus Walleij unsigned long flags; 22848d318a50SLinus Walleij 22850d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 22866db5a8baSRabin Vincent chan_err(d40c, "Cannot free unallocated channel\n"); 22870d0f6b8bSJonas Aaberg return; 22880d0f6b8bSJonas Aaberg } 22890d0f6b8bSJonas Aaberg 22900d0f6b8bSJonas Aaberg 22918d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 22928d318a50SLinus Walleij 22938d318a50SLinus Walleij err = d40_free_dma(d40c); 22948d318a50SLinus Walleij 22958d318a50SLinus Walleij if (err) 22966db5a8baSRabin Vincent chan_err(d40c, "Failed to free channel\n"); 22978d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 22988d318a50SLinus Walleij } 22998d318a50SLinus Walleij 23008d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan, 23018d318a50SLinus Walleij dma_addr_t dst, 23028d318a50SLinus Walleij dma_addr_t src, 23038d318a50SLinus Walleij size_t size, 23042a614340SJonas Aaberg unsigned long dma_flags) 23058d318a50SLinus Walleij { 230695944c6eSRabin Vincent struct scatterlist dst_sg; 230795944c6eSRabin Vincent struct scatterlist src_sg; 23088d318a50SLinus Walleij 230995944c6eSRabin Vincent sg_init_table(&dst_sg, 1); 231095944c6eSRabin Vincent sg_init_table(&src_sg, 1); 23110d0f6b8bSJonas Aaberg 231295944c6eSRabin Vincent sg_dma_address(&dst_sg) = dst; 231395944c6eSRabin Vincent sg_dma_address(&src_sg) = src; 23148d318a50SLinus Walleij 231595944c6eSRabin Vincent sg_dma_len(&dst_sg) = size; 231695944c6eSRabin Vincent sg_dma_len(&src_sg) = size; 23178d318a50SLinus Walleij 2318cade1d30SRabin Vincent return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags); 23198d318a50SLinus Walleij } 23208d318a50SLinus Walleij 23210d688662SIra Snyder static struct dma_async_tx_descriptor * 2322cade1d30SRabin Vincent d40_prep_memcpy_sg(struct dma_chan *chan, 23230d688662SIra Snyder struct scatterlist *dst_sg, unsigned int dst_nents, 23240d688662SIra Snyder struct scatterlist *src_sg, unsigned int src_nents, 23250d688662SIra Snyder unsigned long dma_flags) 23260d688662SIra Snyder { 23270d688662SIra Snyder if (dst_nents != src_nents) 23280d688662SIra Snyder return NULL; 23290d688662SIra Snyder 2330cade1d30SRabin Vincent return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags); 233100ac0341SRabin Vincent } 233200ac0341SRabin Vincent 23338d318a50SLinus Walleij static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan, 23348d318a50SLinus Walleij struct scatterlist *sgl, 23358d318a50SLinus Walleij unsigned int sg_len, 2336db8196dfSVinod Koul enum dma_transfer_direction direction, 2337185ecb5fSAlexandre Bounine unsigned long dma_flags, 2338185ecb5fSAlexandre Bounine void *context) 23398d318a50SLinus Walleij { 2340db8196dfSVinod Koul if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV) 234100ac0341SRabin Vincent return NULL; 234200ac0341SRabin Vincent 2343cade1d30SRabin Vincent return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags); 23448d318a50SLinus Walleij } 23458d318a50SLinus Walleij 23460c842b55SRabin Vincent static struct dma_async_tx_descriptor * 23470c842b55SRabin Vincent dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr, 23480c842b55SRabin Vincent size_t buf_len, size_t period_len, 2349ec8b5e48SPeter Ujfalusi enum dma_transfer_direction direction, unsigned long flags, 2350ec8b5e48SPeter Ujfalusi void *context) 23510c842b55SRabin Vincent { 23520c842b55SRabin Vincent unsigned int periods = buf_len / period_len; 23530c842b55SRabin Vincent struct dma_async_tx_descriptor *txd; 23540c842b55SRabin Vincent struct scatterlist *sg; 23550c842b55SRabin Vincent int i; 23560c842b55SRabin Vincent 235779ca7ec3SRobert Marklund sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT); 23580c842b55SRabin Vincent for (i = 0; i < periods; i++) { 23590c842b55SRabin Vincent sg_dma_address(&sg[i]) = dma_addr; 23600c842b55SRabin Vincent sg_dma_len(&sg[i]) = period_len; 23610c842b55SRabin Vincent dma_addr += period_len; 23620c842b55SRabin Vincent } 23630c842b55SRabin Vincent 23640c842b55SRabin Vincent sg[periods].offset = 0; 2365fdaf9c4bSLars-Peter Clausen sg_dma_len(&sg[periods]) = 0; 23660c842b55SRabin Vincent sg[periods].page_link = 23670c842b55SRabin Vincent ((unsigned long)sg | 0x01) & ~0x02; 23680c842b55SRabin Vincent 23690c842b55SRabin Vincent txd = d40_prep_sg(chan, sg, sg, periods, direction, 23700c842b55SRabin Vincent DMA_PREP_INTERRUPT); 23710c842b55SRabin Vincent 23720c842b55SRabin Vincent kfree(sg); 23730c842b55SRabin Vincent 23740c842b55SRabin Vincent return txd; 23750c842b55SRabin Vincent } 23760c842b55SRabin Vincent 23778d318a50SLinus Walleij static enum dma_status d40_tx_status(struct dma_chan *chan, 23788d318a50SLinus Walleij dma_cookie_t cookie, 23798d318a50SLinus Walleij struct dma_tx_state *txstate) 23808d318a50SLinus Walleij { 23818d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 238296a2af41SRussell King - ARM Linux enum dma_status ret; 23838d318a50SLinus Walleij 23840d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 23856db5a8baSRabin Vincent chan_err(d40c, "Cannot read status of unallocated channel\n"); 23860d0f6b8bSJonas Aaberg return -EINVAL; 23870d0f6b8bSJonas Aaberg } 23880d0f6b8bSJonas Aaberg 238996a2af41SRussell King - ARM Linux ret = dma_cookie_status(chan, cookie, txstate); 239096a2af41SRussell King - ARM Linux if (ret != DMA_SUCCESS) 239196a2af41SRussell King - ARM Linux dma_set_residue(txstate, stedma40_residue(chan)); 23928d318a50SLinus Walleij 2393a5ebca47SJonas Aaberg if (d40_is_paused(d40c)) 2394a5ebca47SJonas Aaberg ret = DMA_PAUSED; 23958d318a50SLinus Walleij 23968d318a50SLinus Walleij return ret; 23978d318a50SLinus Walleij } 23988d318a50SLinus Walleij 23998d318a50SLinus Walleij static void d40_issue_pending(struct dma_chan *chan) 24008d318a50SLinus Walleij { 24018d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 24028d318a50SLinus Walleij unsigned long flags; 24038d318a50SLinus Walleij 24040d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 24056db5a8baSRabin Vincent chan_err(d40c, "Channel is not allocated!\n"); 24060d0f6b8bSJonas Aaberg return; 24070d0f6b8bSJonas Aaberg } 24080d0f6b8bSJonas Aaberg 24098d318a50SLinus Walleij spin_lock_irqsave(&d40c->lock, flags); 24108d318a50SLinus Walleij 2411a8f3067bSPer Forlin list_splice_tail_init(&d40c->pending_queue, &d40c->queue); 2412a8f3067bSPer Forlin 2413a8f3067bSPer Forlin /* Busy means that queued jobs are already being processed */ 24148d318a50SLinus Walleij if (!d40c->busy) 24158d318a50SLinus Walleij (void) d40_queue_start(d40c); 24168d318a50SLinus Walleij 24178d318a50SLinus Walleij spin_unlock_irqrestore(&d40c->lock, flags); 24188d318a50SLinus Walleij } 24198d318a50SLinus Walleij 24201bdae6f4SNarayanan G static void d40_terminate_all(struct dma_chan *chan) 24211bdae6f4SNarayanan G { 24221bdae6f4SNarayanan G unsigned long flags; 24231bdae6f4SNarayanan G struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 24241bdae6f4SNarayanan G int ret; 24251bdae6f4SNarayanan G 24261bdae6f4SNarayanan G spin_lock_irqsave(&d40c->lock, flags); 24271bdae6f4SNarayanan G 24281bdae6f4SNarayanan G pm_runtime_get_sync(d40c->base->dev); 24291bdae6f4SNarayanan G ret = d40_channel_execute_command(d40c, D40_DMA_STOP); 24301bdae6f4SNarayanan G if (ret) 24311bdae6f4SNarayanan G chan_err(d40c, "Failed to stop channel\n"); 24321bdae6f4SNarayanan G 24331bdae6f4SNarayanan G d40_term_all(d40c); 24341bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 24351bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 24361bdae6f4SNarayanan G if (d40c->busy) { 24371bdae6f4SNarayanan G pm_runtime_mark_last_busy(d40c->base->dev); 24381bdae6f4SNarayanan G pm_runtime_put_autosuspend(d40c->base->dev); 24391bdae6f4SNarayanan G } 24401bdae6f4SNarayanan G d40c->busy = false; 24411bdae6f4SNarayanan G 24421bdae6f4SNarayanan G spin_unlock_irqrestore(&d40c->lock, flags); 24431bdae6f4SNarayanan G } 24441bdae6f4SNarayanan G 244598ca5289SRabin Vincent static int 244698ca5289SRabin Vincent dma40_config_to_halfchannel(struct d40_chan *d40c, 244798ca5289SRabin Vincent struct stedma40_half_channel_info *info, 244898ca5289SRabin Vincent enum dma_slave_buswidth width, 244998ca5289SRabin Vincent u32 maxburst) 245098ca5289SRabin Vincent { 245198ca5289SRabin Vincent enum stedma40_periph_data_width addr_width; 245298ca5289SRabin Vincent int psize; 245398ca5289SRabin Vincent 245498ca5289SRabin Vincent switch (width) { 245598ca5289SRabin Vincent case DMA_SLAVE_BUSWIDTH_1_BYTE: 245698ca5289SRabin Vincent addr_width = STEDMA40_BYTE_WIDTH; 245798ca5289SRabin Vincent break; 245898ca5289SRabin Vincent case DMA_SLAVE_BUSWIDTH_2_BYTES: 245998ca5289SRabin Vincent addr_width = STEDMA40_HALFWORD_WIDTH; 246098ca5289SRabin Vincent break; 246198ca5289SRabin Vincent case DMA_SLAVE_BUSWIDTH_4_BYTES: 246298ca5289SRabin Vincent addr_width = STEDMA40_WORD_WIDTH; 246398ca5289SRabin Vincent break; 246498ca5289SRabin Vincent case DMA_SLAVE_BUSWIDTH_8_BYTES: 246598ca5289SRabin Vincent addr_width = STEDMA40_DOUBLEWORD_WIDTH; 246698ca5289SRabin Vincent break; 246798ca5289SRabin Vincent default: 246898ca5289SRabin Vincent dev_err(d40c->base->dev, 246998ca5289SRabin Vincent "illegal peripheral address width " 247098ca5289SRabin Vincent "requested (%d)\n", 247198ca5289SRabin Vincent width); 247298ca5289SRabin Vincent return -EINVAL; 247398ca5289SRabin Vincent } 247498ca5289SRabin Vincent 247598ca5289SRabin Vincent if (chan_is_logical(d40c)) { 247698ca5289SRabin Vincent if (maxburst >= 16) 247798ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_16; 247898ca5289SRabin Vincent else if (maxburst >= 8) 247998ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_8; 248098ca5289SRabin Vincent else if (maxburst >= 4) 248198ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_4; 248298ca5289SRabin Vincent else 248398ca5289SRabin Vincent psize = STEDMA40_PSIZE_LOG_1; 248498ca5289SRabin Vincent } else { 248598ca5289SRabin Vincent if (maxburst >= 16) 248698ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_16; 248798ca5289SRabin Vincent else if (maxburst >= 8) 248898ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_8; 248998ca5289SRabin Vincent else if (maxburst >= 4) 249098ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_4; 249198ca5289SRabin Vincent else 249298ca5289SRabin Vincent psize = STEDMA40_PSIZE_PHY_1; 249398ca5289SRabin Vincent } 249498ca5289SRabin Vincent 249598ca5289SRabin Vincent info->data_width = addr_width; 249698ca5289SRabin Vincent info->psize = psize; 249798ca5289SRabin Vincent info->flow_ctrl = STEDMA40_NO_FLOW_CTRL; 249898ca5289SRabin Vincent 249998ca5289SRabin Vincent return 0; 250098ca5289SRabin Vincent } 250198ca5289SRabin Vincent 250295e1400fSLinus Walleij /* Runtime reconfiguration extension */ 250398ca5289SRabin Vincent static int d40_set_runtime_config(struct dma_chan *chan, 250495e1400fSLinus Walleij struct dma_slave_config *config) 250595e1400fSLinus Walleij { 250695e1400fSLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 250795e1400fSLinus Walleij struct stedma40_chan_cfg *cfg = &d40c->dma_cfg; 250898ca5289SRabin Vincent enum dma_slave_buswidth src_addr_width, dst_addr_width; 250995e1400fSLinus Walleij dma_addr_t config_addr; 251098ca5289SRabin Vincent u32 src_maxburst, dst_maxburst; 251198ca5289SRabin Vincent int ret; 251298ca5289SRabin Vincent 251398ca5289SRabin Vincent src_addr_width = config->src_addr_width; 251498ca5289SRabin Vincent src_maxburst = config->src_maxburst; 251598ca5289SRabin Vincent dst_addr_width = config->dst_addr_width; 251698ca5289SRabin Vincent dst_maxburst = config->dst_maxburst; 251795e1400fSLinus Walleij 2518db8196dfSVinod Koul if (config->direction == DMA_DEV_TO_MEM) { 251995e1400fSLinus Walleij dma_addr_t dev_addr_rx = 252095e1400fSLinus Walleij d40c->base->plat_data->dev_rx[cfg->src_dev_type]; 252195e1400fSLinus Walleij 252295e1400fSLinus Walleij config_addr = config->src_addr; 252395e1400fSLinus Walleij if (dev_addr_rx) 252495e1400fSLinus Walleij dev_dbg(d40c->base->dev, 252595e1400fSLinus Walleij "channel has a pre-wired RX address %08x " 252695e1400fSLinus Walleij "overriding with %08x\n", 252795e1400fSLinus Walleij dev_addr_rx, config_addr); 252895e1400fSLinus Walleij if (cfg->dir != STEDMA40_PERIPH_TO_MEM) 252995e1400fSLinus Walleij dev_dbg(d40c->base->dev, 253095e1400fSLinus Walleij "channel was not configured for peripheral " 253195e1400fSLinus Walleij "to memory transfer (%d) overriding\n", 253295e1400fSLinus Walleij cfg->dir); 253395e1400fSLinus Walleij cfg->dir = STEDMA40_PERIPH_TO_MEM; 253495e1400fSLinus Walleij 253598ca5289SRabin Vincent /* Configure the memory side */ 253698ca5289SRabin Vincent if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 253798ca5289SRabin Vincent dst_addr_width = src_addr_width; 253898ca5289SRabin Vincent if (dst_maxburst == 0) 253998ca5289SRabin Vincent dst_maxburst = src_maxburst; 254095e1400fSLinus Walleij 2541db8196dfSVinod Koul } else if (config->direction == DMA_MEM_TO_DEV) { 254295e1400fSLinus Walleij dma_addr_t dev_addr_tx = 254395e1400fSLinus Walleij d40c->base->plat_data->dev_tx[cfg->dst_dev_type]; 254495e1400fSLinus Walleij 254595e1400fSLinus Walleij config_addr = config->dst_addr; 254695e1400fSLinus Walleij if (dev_addr_tx) 254795e1400fSLinus Walleij dev_dbg(d40c->base->dev, 254895e1400fSLinus Walleij "channel has a pre-wired TX address %08x " 254995e1400fSLinus Walleij "overriding with %08x\n", 255095e1400fSLinus Walleij dev_addr_tx, config_addr); 255195e1400fSLinus Walleij if (cfg->dir != STEDMA40_MEM_TO_PERIPH) 255295e1400fSLinus Walleij dev_dbg(d40c->base->dev, 255395e1400fSLinus Walleij "channel was not configured for memory " 255495e1400fSLinus Walleij "to peripheral transfer (%d) overriding\n", 255595e1400fSLinus Walleij cfg->dir); 255695e1400fSLinus Walleij cfg->dir = STEDMA40_MEM_TO_PERIPH; 255795e1400fSLinus Walleij 255898ca5289SRabin Vincent /* Configure the memory side */ 255998ca5289SRabin Vincent if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) 256098ca5289SRabin Vincent src_addr_width = dst_addr_width; 256198ca5289SRabin Vincent if (src_maxburst == 0) 256298ca5289SRabin Vincent src_maxburst = dst_maxburst; 256395e1400fSLinus Walleij } else { 256495e1400fSLinus Walleij dev_err(d40c->base->dev, 256595e1400fSLinus Walleij "unrecognized channel direction %d\n", 256695e1400fSLinus Walleij config->direction); 256798ca5289SRabin Vincent return -EINVAL; 256895e1400fSLinus Walleij } 256995e1400fSLinus Walleij 257098ca5289SRabin Vincent if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) { 257195e1400fSLinus Walleij dev_err(d40c->base->dev, 257298ca5289SRabin Vincent "src/dst width/maxburst mismatch: %d*%d != %d*%d\n", 257398ca5289SRabin Vincent src_maxburst, 257498ca5289SRabin Vincent src_addr_width, 257598ca5289SRabin Vincent dst_maxburst, 257698ca5289SRabin Vincent dst_addr_width); 257798ca5289SRabin Vincent return -EINVAL; 257895e1400fSLinus Walleij } 257995e1400fSLinus Walleij 258098ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->src_info, 258198ca5289SRabin Vincent src_addr_width, 258298ca5289SRabin Vincent src_maxburst); 258398ca5289SRabin Vincent if (ret) 258498ca5289SRabin Vincent return ret; 258595e1400fSLinus Walleij 258698ca5289SRabin Vincent ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info, 258798ca5289SRabin Vincent dst_addr_width, 258898ca5289SRabin Vincent dst_maxburst); 258998ca5289SRabin Vincent if (ret) 259098ca5289SRabin Vincent return ret; 259195e1400fSLinus Walleij 2592a59670a4SPer Forlin /* Fill in register values */ 2593724a8577SRabin Vincent if (chan_is_logical(d40c)) 2594a59670a4SPer Forlin d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); 2595a59670a4SPer Forlin else 2596a59670a4SPer Forlin d40_phy_cfg(cfg, &d40c->src_def_cfg, 2597a59670a4SPer Forlin &d40c->dst_def_cfg, false); 2598a59670a4SPer Forlin 259995e1400fSLinus Walleij /* These settings will take precedence later */ 260095e1400fSLinus Walleij d40c->runtime_addr = config_addr; 260195e1400fSLinus Walleij d40c->runtime_direction = config->direction; 260295e1400fSLinus Walleij dev_dbg(d40c->base->dev, 260398ca5289SRabin Vincent "configured channel %s for %s, data width %d/%d, " 260498ca5289SRabin Vincent "maxburst %d/%d elements, LE, no flow control\n", 260595e1400fSLinus Walleij dma_chan_name(chan), 2606db8196dfSVinod Koul (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX", 260798ca5289SRabin Vincent src_addr_width, dst_addr_width, 260898ca5289SRabin Vincent src_maxburst, dst_maxburst); 260998ca5289SRabin Vincent 261098ca5289SRabin Vincent return 0; 261195e1400fSLinus Walleij } 261295e1400fSLinus Walleij 261305827630SLinus Walleij static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, 261405827630SLinus Walleij unsigned long arg) 26158d318a50SLinus Walleij { 26168d318a50SLinus Walleij struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); 26178d318a50SLinus Walleij 26180d0f6b8bSJonas Aaberg if (d40c->phy_chan == NULL) { 26196db5a8baSRabin Vincent chan_err(d40c, "Channel is not allocated!\n"); 26200d0f6b8bSJonas Aaberg return -EINVAL; 26210d0f6b8bSJonas Aaberg } 26220d0f6b8bSJonas Aaberg 26238d318a50SLinus Walleij switch (cmd) { 26248d318a50SLinus Walleij case DMA_TERMINATE_ALL: 26251bdae6f4SNarayanan G d40_terminate_all(chan); 26261bdae6f4SNarayanan G return 0; 26278d318a50SLinus Walleij case DMA_PAUSE: 262886eb5fb6SRabin Vincent return d40_pause(d40c); 26298d318a50SLinus Walleij case DMA_RESUME: 263086eb5fb6SRabin Vincent return d40_resume(d40c); 263195e1400fSLinus Walleij case DMA_SLAVE_CONFIG: 263298ca5289SRabin Vincent return d40_set_runtime_config(chan, 263395e1400fSLinus Walleij (struct dma_slave_config *) arg); 263495e1400fSLinus Walleij default: 263595e1400fSLinus Walleij break; 26368d318a50SLinus Walleij } 26378d318a50SLinus Walleij 26388d318a50SLinus Walleij /* Other commands are unimplemented */ 26398d318a50SLinus Walleij return -ENXIO; 26408d318a50SLinus Walleij } 26418d318a50SLinus Walleij 26428d318a50SLinus Walleij /* Initialization functions */ 26438d318a50SLinus Walleij 26448d318a50SLinus Walleij static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma, 26458d318a50SLinus Walleij struct d40_chan *chans, int offset, 26468d318a50SLinus Walleij int num_chans) 26478d318a50SLinus Walleij { 26488d318a50SLinus Walleij int i = 0; 26498d318a50SLinus Walleij struct d40_chan *d40c; 26508d318a50SLinus Walleij 26518d318a50SLinus Walleij INIT_LIST_HEAD(&dma->channels); 26528d318a50SLinus Walleij 26538d318a50SLinus Walleij for (i = offset; i < offset + num_chans; i++) { 26548d318a50SLinus Walleij d40c = &chans[i]; 26558d318a50SLinus Walleij d40c->base = base; 26568d318a50SLinus Walleij d40c->chan.device = dma; 26578d318a50SLinus Walleij 26588d318a50SLinus Walleij spin_lock_init(&d40c->lock); 26598d318a50SLinus Walleij 26608d318a50SLinus Walleij d40c->log_num = D40_PHY_CHAN; 26618d318a50SLinus Walleij 26628d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->active); 26638d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->queue); 2664a8f3067bSPer Forlin INIT_LIST_HEAD(&d40c->pending_queue); 26658d318a50SLinus Walleij INIT_LIST_HEAD(&d40c->client); 266682babbb3SPer Forlin INIT_LIST_HEAD(&d40c->prepare_queue); 26678d318a50SLinus Walleij 26688d318a50SLinus Walleij tasklet_init(&d40c->tasklet, dma_tasklet, 26698d318a50SLinus Walleij (unsigned long) d40c); 26708d318a50SLinus Walleij 26718d318a50SLinus Walleij list_add_tail(&d40c->chan.device_node, 26728d318a50SLinus Walleij &dma->channels); 26738d318a50SLinus Walleij } 26748d318a50SLinus Walleij } 26758d318a50SLinus Walleij 26767ad74a7cSRabin Vincent static void d40_ops_init(struct d40_base *base, struct dma_device *dev) 26777ad74a7cSRabin Vincent { 26787ad74a7cSRabin Vincent if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) 26797ad74a7cSRabin Vincent dev->device_prep_slave_sg = d40_prep_slave_sg; 26807ad74a7cSRabin Vincent 26817ad74a7cSRabin Vincent if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) { 26827ad74a7cSRabin Vincent dev->device_prep_dma_memcpy = d40_prep_memcpy; 26837ad74a7cSRabin Vincent 26847ad74a7cSRabin Vincent /* 26857ad74a7cSRabin Vincent * This controller can only access address at even 26867ad74a7cSRabin Vincent * 32bit boundaries, i.e. 2^2 26877ad74a7cSRabin Vincent */ 26887ad74a7cSRabin Vincent dev->copy_align = 2; 26897ad74a7cSRabin Vincent } 26907ad74a7cSRabin Vincent 26917ad74a7cSRabin Vincent if (dma_has_cap(DMA_SG, dev->cap_mask)) 26927ad74a7cSRabin Vincent dev->device_prep_dma_sg = d40_prep_memcpy_sg; 26937ad74a7cSRabin Vincent 26940c842b55SRabin Vincent if (dma_has_cap(DMA_CYCLIC, dev->cap_mask)) 26950c842b55SRabin Vincent dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic; 26960c842b55SRabin Vincent 26977ad74a7cSRabin Vincent dev->device_alloc_chan_resources = d40_alloc_chan_resources; 26987ad74a7cSRabin Vincent dev->device_free_chan_resources = d40_free_chan_resources; 26997ad74a7cSRabin Vincent dev->device_issue_pending = d40_issue_pending; 27007ad74a7cSRabin Vincent dev->device_tx_status = d40_tx_status; 27017ad74a7cSRabin Vincent dev->device_control = d40_control; 27027ad74a7cSRabin Vincent dev->dev = base->dev; 27037ad74a7cSRabin Vincent } 27047ad74a7cSRabin Vincent 27058d318a50SLinus Walleij static int __init d40_dmaengine_init(struct d40_base *base, 27068d318a50SLinus Walleij int num_reserved_chans) 27078d318a50SLinus Walleij { 27088d318a50SLinus Walleij int err ; 27098d318a50SLinus Walleij 27108d318a50SLinus Walleij d40_chan_init(base, &base->dma_slave, base->log_chans, 27118d318a50SLinus Walleij 0, base->num_log_chans); 27128d318a50SLinus Walleij 27138d318a50SLinus Walleij dma_cap_zero(base->dma_slave.cap_mask); 27148d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask); 27150c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); 27168d318a50SLinus Walleij 27177ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_slave); 27188d318a50SLinus Walleij 27198d318a50SLinus Walleij err = dma_async_device_register(&base->dma_slave); 27208d318a50SLinus Walleij 27218d318a50SLinus Walleij if (err) { 27226db5a8baSRabin Vincent d40_err(base->dev, "Failed to register slave channels\n"); 27238d318a50SLinus Walleij goto failure1; 27248d318a50SLinus Walleij } 27258d318a50SLinus Walleij 27268d318a50SLinus Walleij d40_chan_init(base, &base->dma_memcpy, base->log_chans, 27278d318a50SLinus Walleij base->num_log_chans, base->plat_data->memcpy_len); 27288d318a50SLinus Walleij 27298d318a50SLinus Walleij dma_cap_zero(base->dma_memcpy.cap_mask); 27308d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask); 27317ad74a7cSRabin Vincent dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask); 27328d318a50SLinus Walleij 27337ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_memcpy); 27348d318a50SLinus Walleij 27358d318a50SLinus Walleij err = dma_async_device_register(&base->dma_memcpy); 27368d318a50SLinus Walleij 27378d318a50SLinus Walleij if (err) { 27386db5a8baSRabin Vincent d40_err(base->dev, 27396db5a8baSRabin Vincent "Failed to regsiter memcpy only channels\n"); 27408d318a50SLinus Walleij goto failure2; 27418d318a50SLinus Walleij } 27428d318a50SLinus Walleij 27438d318a50SLinus Walleij d40_chan_init(base, &base->dma_both, base->phy_chans, 27448d318a50SLinus Walleij 0, num_reserved_chans); 27458d318a50SLinus Walleij 27468d318a50SLinus Walleij dma_cap_zero(base->dma_both.cap_mask); 27478d318a50SLinus Walleij dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask); 27488d318a50SLinus Walleij dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask); 27497ad74a7cSRabin Vincent dma_cap_set(DMA_SG, base->dma_both.cap_mask); 27500c842b55SRabin Vincent dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); 27518d318a50SLinus Walleij 27527ad74a7cSRabin Vincent d40_ops_init(base, &base->dma_both); 27538d318a50SLinus Walleij err = dma_async_device_register(&base->dma_both); 27548d318a50SLinus Walleij 27558d318a50SLinus Walleij if (err) { 27566db5a8baSRabin Vincent d40_err(base->dev, 27576db5a8baSRabin Vincent "Failed to register logical and physical capable channels\n"); 27588d318a50SLinus Walleij goto failure3; 27598d318a50SLinus Walleij } 27608d318a50SLinus Walleij return 0; 27618d318a50SLinus Walleij failure3: 27628d318a50SLinus Walleij dma_async_device_unregister(&base->dma_memcpy); 27638d318a50SLinus Walleij failure2: 27648d318a50SLinus Walleij dma_async_device_unregister(&base->dma_slave); 27658d318a50SLinus Walleij failure1: 27668d318a50SLinus Walleij return err; 27678d318a50SLinus Walleij } 27688d318a50SLinus Walleij 27697fb3e75eSNarayanan G /* Suspend resume functionality */ 27707fb3e75eSNarayanan G #ifdef CONFIG_PM 27717fb3e75eSNarayanan G static int dma40_pm_suspend(struct device *dev) 27727fb3e75eSNarayanan G { 277328c7a19dSNarayanan G struct platform_device *pdev = to_platform_device(dev); 277428c7a19dSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 277528c7a19dSNarayanan G int ret = 0; 27767fb3e75eSNarayanan G if (!pm_runtime_suspended(dev)) 27777fb3e75eSNarayanan G return -EBUSY; 27787fb3e75eSNarayanan G 277928c7a19dSNarayanan G if (base->lcpa_regulator) 278028c7a19dSNarayanan G ret = regulator_disable(base->lcpa_regulator); 278128c7a19dSNarayanan G return ret; 27827fb3e75eSNarayanan G } 27837fb3e75eSNarayanan G 27847fb3e75eSNarayanan G static int dma40_runtime_suspend(struct device *dev) 27857fb3e75eSNarayanan G { 27867fb3e75eSNarayanan G struct platform_device *pdev = to_platform_device(dev); 27877fb3e75eSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 27887fb3e75eSNarayanan G 27897fb3e75eSNarayanan G d40_save_restore_registers(base, true); 27907fb3e75eSNarayanan G 27917fb3e75eSNarayanan G /* Don't disable/enable clocks for v1 due to HW bugs */ 27927fb3e75eSNarayanan G if (base->rev != 1) 27937fb3e75eSNarayanan G writel_relaxed(base->gcc_pwr_off_mask, 27947fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC); 27957fb3e75eSNarayanan G 27967fb3e75eSNarayanan G return 0; 27977fb3e75eSNarayanan G } 27987fb3e75eSNarayanan G 27997fb3e75eSNarayanan G static int dma40_runtime_resume(struct device *dev) 28007fb3e75eSNarayanan G { 28017fb3e75eSNarayanan G struct platform_device *pdev = to_platform_device(dev); 28027fb3e75eSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 28037fb3e75eSNarayanan G 28047fb3e75eSNarayanan G if (base->initialized) 28057fb3e75eSNarayanan G d40_save_restore_registers(base, false); 28067fb3e75eSNarayanan G 28077fb3e75eSNarayanan G writel_relaxed(D40_DREG_GCC_ENABLE_ALL, 28087fb3e75eSNarayanan G base->virtbase + D40_DREG_GCC); 28097fb3e75eSNarayanan G return 0; 28107fb3e75eSNarayanan G } 28117fb3e75eSNarayanan G 281228c7a19dSNarayanan G static int dma40_resume(struct device *dev) 281328c7a19dSNarayanan G { 281428c7a19dSNarayanan G struct platform_device *pdev = to_platform_device(dev); 281528c7a19dSNarayanan G struct d40_base *base = platform_get_drvdata(pdev); 281628c7a19dSNarayanan G int ret = 0; 281728c7a19dSNarayanan G 281828c7a19dSNarayanan G if (base->lcpa_regulator) 281928c7a19dSNarayanan G ret = regulator_enable(base->lcpa_regulator); 282028c7a19dSNarayanan G 282128c7a19dSNarayanan G return ret; 282228c7a19dSNarayanan G } 28237fb3e75eSNarayanan G 28247fb3e75eSNarayanan G static const struct dev_pm_ops dma40_pm_ops = { 28257fb3e75eSNarayanan G .suspend = dma40_pm_suspend, 28267fb3e75eSNarayanan G .runtime_suspend = dma40_runtime_suspend, 28277fb3e75eSNarayanan G .runtime_resume = dma40_runtime_resume, 282828c7a19dSNarayanan G .resume = dma40_resume, 28297fb3e75eSNarayanan G }; 28307fb3e75eSNarayanan G #define DMA40_PM_OPS (&dma40_pm_ops) 28317fb3e75eSNarayanan G #else 28327fb3e75eSNarayanan G #define DMA40_PM_OPS NULL 28337fb3e75eSNarayanan G #endif 28347fb3e75eSNarayanan G 28358d318a50SLinus Walleij /* Initialization functions. */ 28368d318a50SLinus Walleij 28378d318a50SLinus Walleij static int __init d40_phy_res_init(struct d40_base *base) 28388d318a50SLinus Walleij { 28398d318a50SLinus Walleij int i; 28408d318a50SLinus Walleij int num_phy_chans_avail = 0; 28418d318a50SLinus Walleij u32 val[2]; 28428d318a50SLinus Walleij int odd_even_bit = -2; 28437fb3e75eSNarayanan G int gcc = D40_DREG_GCC_ENA; 28448d318a50SLinus Walleij 28458d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRSME); 28468d318a50SLinus Walleij val[1] = readl(base->virtbase + D40_DREG_PRSMO); 28478d318a50SLinus Walleij 28488d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 28498d318a50SLinus Walleij base->phy_res[i].num = i; 28508d318a50SLinus Walleij odd_even_bit += 2 * ((i % 2) == 0); 28518d318a50SLinus Walleij if (((val[i % 2] >> odd_even_bit) & 3) == 1) { 28528d318a50SLinus Walleij /* Mark security only channels as occupied */ 28538d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_PHY; 28548d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_PHY; 28557fb3e75eSNarayanan G base->phy_res[i].reserved = true; 28567fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), 28577fb3e75eSNarayanan G D40_DREG_GCC_SRC); 28587fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), 28597fb3e75eSNarayanan G D40_DREG_GCC_DST); 28607fb3e75eSNarayanan G 28617fb3e75eSNarayanan G 28628d318a50SLinus Walleij } else { 28638d318a50SLinus Walleij base->phy_res[i].allocated_src = D40_ALLOC_FREE; 28648d318a50SLinus Walleij base->phy_res[i].allocated_dst = D40_ALLOC_FREE; 28657fb3e75eSNarayanan G base->phy_res[i].reserved = false; 28668d318a50SLinus Walleij num_phy_chans_avail++; 28678d318a50SLinus Walleij } 28688d318a50SLinus Walleij spin_lock_init(&base->phy_res[i].lock); 28698d318a50SLinus Walleij } 28706b7acd84SJonas Aaberg 28716b7acd84SJonas Aaberg /* Mark disabled channels as occupied */ 28726b7acd84SJonas Aaberg for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) { 2873f57b407cSRabin Vincent int chan = base->plat_data->disabled_channels[i]; 2874f57b407cSRabin Vincent 2875f57b407cSRabin Vincent base->phy_res[chan].allocated_src = D40_ALLOC_PHY; 2876f57b407cSRabin Vincent base->phy_res[chan].allocated_dst = D40_ALLOC_PHY; 28777fb3e75eSNarayanan G base->phy_res[chan].reserved = true; 28787fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), 28797fb3e75eSNarayanan G D40_DREG_GCC_SRC); 28807fb3e75eSNarayanan G gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), 28817fb3e75eSNarayanan G D40_DREG_GCC_DST); 28826b7acd84SJonas Aaberg num_phy_chans_avail--; 28836b7acd84SJonas Aaberg } 28846b7acd84SJonas Aaberg 28858d318a50SLinus Walleij dev_info(base->dev, "%d of %d physical DMA channels available\n", 28868d318a50SLinus Walleij num_phy_chans_avail, base->num_phy_chans); 28878d318a50SLinus Walleij 28888d318a50SLinus Walleij /* Verify settings extended vs standard */ 28898d318a50SLinus Walleij val[0] = readl(base->virtbase + D40_DREG_PRTYP); 28908d318a50SLinus Walleij 28918d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 28928d318a50SLinus Walleij 28938d318a50SLinus Walleij if (base->phy_res[i].allocated_src == D40_ALLOC_FREE && 28948d318a50SLinus Walleij (val[0] & 0x3) != 1) 28958d318a50SLinus Walleij dev_info(base->dev, 28968d318a50SLinus Walleij "[%s] INFO: channel %d is misconfigured (%d)\n", 28978d318a50SLinus Walleij __func__, i, val[0] & 0x3); 28988d318a50SLinus Walleij 28998d318a50SLinus Walleij val[0] = val[0] >> 2; 29008d318a50SLinus Walleij } 29018d318a50SLinus Walleij 29027fb3e75eSNarayanan G /* 29037fb3e75eSNarayanan G * To keep things simple, Enable all clocks initially. 29047fb3e75eSNarayanan G * The clocks will get managed later post channel allocation. 29057fb3e75eSNarayanan G * The clocks for the event lines on which reserved channels exists 29067fb3e75eSNarayanan G * are not managed here. 29077fb3e75eSNarayanan G */ 29087fb3e75eSNarayanan G writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC); 29097fb3e75eSNarayanan G base->gcc_pwr_off_mask = gcc; 29107fb3e75eSNarayanan G 29118d318a50SLinus Walleij return num_phy_chans_avail; 29128d318a50SLinus Walleij } 29138d318a50SLinus Walleij 29148d318a50SLinus Walleij static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev) 29158d318a50SLinus Walleij { 29168d318a50SLinus Walleij struct stedma40_platform_data *plat_data; 29178d318a50SLinus Walleij struct clk *clk = NULL; 29188d318a50SLinus Walleij void __iomem *virtbase = NULL; 29198d318a50SLinus Walleij struct resource *res = NULL; 29208d318a50SLinus Walleij struct d40_base *base = NULL; 29218d318a50SLinus Walleij int num_log_chans = 0; 29228d318a50SLinus Walleij int num_phy_chans; 2923b707c658SUlf Hansson int clk_ret = -EINVAL; 29248d318a50SLinus Walleij int i; 2925f4b89764SLinus Walleij u32 pid; 2926f4b89764SLinus Walleij u32 cid; 2927f4b89764SLinus Walleij u8 rev; 29288d318a50SLinus Walleij 29298d318a50SLinus Walleij clk = clk_get(&pdev->dev, NULL); 29308d318a50SLinus Walleij if (IS_ERR(clk)) { 29316db5a8baSRabin Vincent d40_err(&pdev->dev, "No matching clock found\n"); 29328d318a50SLinus Walleij goto failure; 29338d318a50SLinus Walleij } 29348d318a50SLinus Walleij 2935b707c658SUlf Hansson clk_ret = clk_prepare_enable(clk); 2936b707c658SUlf Hansson if (clk_ret) { 2937b707c658SUlf Hansson d40_err(&pdev->dev, "Failed to prepare/enable clock\n"); 2938b707c658SUlf Hansson goto failure; 2939b707c658SUlf Hansson } 29408d318a50SLinus Walleij 29418d318a50SLinus Walleij /* Get IO for DMAC base address */ 29428d318a50SLinus Walleij res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base"); 29438d318a50SLinus Walleij if (!res) 29448d318a50SLinus Walleij goto failure; 29458d318a50SLinus Walleij 29468d318a50SLinus Walleij if (request_mem_region(res->start, resource_size(res), 29478d318a50SLinus Walleij D40_NAME " I/O base") == NULL) 29488d318a50SLinus Walleij goto failure; 29498d318a50SLinus Walleij 29508d318a50SLinus Walleij virtbase = ioremap(res->start, resource_size(res)); 29518d318a50SLinus Walleij if (!virtbase) 29528d318a50SLinus Walleij goto failure; 29538d318a50SLinus Walleij 2954f4b89764SLinus Walleij /* This is just a regular AMBA PrimeCell ID actually */ 2955f4b89764SLinus Walleij for (pid = 0, i = 0; i < 4; i++) 2956f4b89764SLinus Walleij pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i) 2957f4b89764SLinus Walleij & 255) << (i * 8); 2958f4b89764SLinus Walleij for (cid = 0, i = 0; i < 4; i++) 2959f4b89764SLinus Walleij cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i) 2960f4b89764SLinus Walleij & 255) << (i * 8); 2961f4b89764SLinus Walleij 2962f4b89764SLinus Walleij if (cid != AMBA_CID) { 2963f4b89764SLinus Walleij d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n"); 29648d318a50SLinus Walleij goto failure; 29658d318a50SLinus Walleij } 2966f4b89764SLinus Walleij if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) { 29676db5a8baSRabin Vincent d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n", 2968f4b89764SLinus Walleij AMBA_MANF_BITS(pid), 2969f4b89764SLinus Walleij AMBA_VENDOR_ST); 29708d318a50SLinus Walleij goto failure; 29718d318a50SLinus Walleij } 2972f4b89764SLinus Walleij /* 2973f4b89764SLinus Walleij * HW revision: 2974f4b89764SLinus Walleij * DB8500ed has revision 0 2975f4b89764SLinus Walleij * ? has revision 1 2976f4b89764SLinus Walleij * DB8500v1 has revision 2 2977f4b89764SLinus Walleij * DB8500v2 has revision 3 2978f4b89764SLinus Walleij */ 2979f4b89764SLinus Walleij rev = AMBA_REV_BITS(pid); 29803ae0267fSJonas Aaberg 29818d318a50SLinus Walleij /* The number of physical channels on this HW */ 29828d318a50SLinus Walleij num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4; 29838d318a50SLinus Walleij 29848d318a50SLinus Walleij dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n", 29853ae0267fSJonas Aaberg rev, res->start); 29868d318a50SLinus Walleij 29871bdae6f4SNarayanan G if (rev < 2) { 29881bdae6f4SNarayanan G d40_err(&pdev->dev, "hardware revision: %d is not supported", 29891bdae6f4SNarayanan G rev); 29901bdae6f4SNarayanan G goto failure; 29911bdae6f4SNarayanan G } 29921bdae6f4SNarayanan G 29938d318a50SLinus Walleij plat_data = pdev->dev.platform_data; 29948d318a50SLinus Walleij 29958d318a50SLinus Walleij /* Count the number of logical channels in use */ 29968d318a50SLinus Walleij for (i = 0; i < plat_data->dev_len; i++) 29978d318a50SLinus Walleij if (plat_data->dev_rx[i] != 0) 29988d318a50SLinus Walleij num_log_chans++; 29998d318a50SLinus Walleij 30008d318a50SLinus Walleij for (i = 0; i < plat_data->dev_len; i++) 30018d318a50SLinus Walleij if (plat_data->dev_tx[i] != 0) 30028d318a50SLinus Walleij num_log_chans++; 30038d318a50SLinus Walleij 30048d318a50SLinus Walleij base = kzalloc(ALIGN(sizeof(struct d40_base), 4) + 30058d318a50SLinus Walleij (num_phy_chans + num_log_chans + plat_data->memcpy_len) * 30068d318a50SLinus Walleij sizeof(struct d40_chan), GFP_KERNEL); 30078d318a50SLinus Walleij 30088d318a50SLinus Walleij if (base == NULL) { 30096db5a8baSRabin Vincent d40_err(&pdev->dev, "Out of memory\n"); 30108d318a50SLinus Walleij goto failure; 30118d318a50SLinus Walleij } 30128d318a50SLinus Walleij 30133ae0267fSJonas Aaberg base->rev = rev; 30148d318a50SLinus Walleij base->clk = clk; 30158d318a50SLinus Walleij base->num_phy_chans = num_phy_chans; 30168d318a50SLinus Walleij base->num_log_chans = num_log_chans; 30178d318a50SLinus Walleij base->phy_start = res->start; 30188d318a50SLinus Walleij base->phy_size = resource_size(res); 30198d318a50SLinus Walleij base->virtbase = virtbase; 30208d318a50SLinus Walleij base->plat_data = plat_data; 30218d318a50SLinus Walleij base->dev = &pdev->dev; 30228d318a50SLinus Walleij base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4); 30238d318a50SLinus Walleij base->log_chans = &base->phy_chans[num_phy_chans]; 30248d318a50SLinus Walleij 30258d318a50SLinus Walleij base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res), 30268d318a50SLinus Walleij GFP_KERNEL); 30278d318a50SLinus Walleij if (!base->phy_res) 30288d318a50SLinus Walleij goto failure; 30298d318a50SLinus Walleij 30308d318a50SLinus Walleij base->lookup_phy_chans = kzalloc(num_phy_chans * 30318d318a50SLinus Walleij sizeof(struct d40_chan *), 30328d318a50SLinus Walleij GFP_KERNEL); 30338d318a50SLinus Walleij if (!base->lookup_phy_chans) 30348d318a50SLinus Walleij goto failure; 30358d318a50SLinus Walleij 30368d318a50SLinus Walleij if (num_log_chans + plat_data->memcpy_len) { 30378d318a50SLinus Walleij /* 30388d318a50SLinus Walleij * The max number of logical channels are event lines for all 30398d318a50SLinus Walleij * src devices and dst devices 30408d318a50SLinus Walleij */ 30418d318a50SLinus Walleij base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 * 30428d318a50SLinus Walleij sizeof(struct d40_chan *), 30438d318a50SLinus Walleij GFP_KERNEL); 30448d318a50SLinus Walleij if (!base->lookup_log_chans) 30458d318a50SLinus Walleij goto failure; 30468d318a50SLinus Walleij } 3047698e4732SJonas Aaberg 30487fb3e75eSNarayanan G base->reg_val_backup_chan = kmalloc(base->num_phy_chans * 30497fb3e75eSNarayanan G sizeof(d40_backup_regs_chan), 30508d318a50SLinus Walleij GFP_KERNEL); 30517fb3e75eSNarayanan G if (!base->reg_val_backup_chan) 30527fb3e75eSNarayanan G goto failure; 30537fb3e75eSNarayanan G 30547fb3e75eSNarayanan G base->lcla_pool.alloc_map = 30557fb3e75eSNarayanan G kzalloc(num_phy_chans * sizeof(struct d40_desc *) 30567fb3e75eSNarayanan G * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL); 30578d318a50SLinus Walleij if (!base->lcla_pool.alloc_map) 30588d318a50SLinus Walleij goto failure; 30598d318a50SLinus Walleij 3060c675b1b4SJonas Aaberg base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc), 3061c675b1b4SJonas Aaberg 0, SLAB_HWCACHE_ALIGN, 3062c675b1b4SJonas Aaberg NULL); 3063c675b1b4SJonas Aaberg if (base->desc_slab == NULL) 3064c675b1b4SJonas Aaberg goto failure; 3065c675b1b4SJonas Aaberg 30668d318a50SLinus Walleij return base; 30678d318a50SLinus Walleij 30688d318a50SLinus Walleij failure: 3069b707c658SUlf Hansson if (!clk_ret) 3070b707c658SUlf Hansson clk_disable_unprepare(clk); 3071b707c658SUlf Hansson if (!IS_ERR(clk)) 30728d318a50SLinus Walleij clk_put(clk); 30738d318a50SLinus Walleij if (virtbase) 30748d318a50SLinus Walleij iounmap(virtbase); 30758d318a50SLinus Walleij if (res) 30768d318a50SLinus Walleij release_mem_region(res->start, 30778d318a50SLinus Walleij resource_size(res)); 30788d318a50SLinus Walleij if (virtbase) 30798d318a50SLinus Walleij iounmap(virtbase); 30808d318a50SLinus Walleij 30818d318a50SLinus Walleij if (base) { 30828d318a50SLinus Walleij kfree(base->lcla_pool.alloc_map); 30831bdae6f4SNarayanan G kfree(base->reg_val_backup_chan); 30848d318a50SLinus Walleij kfree(base->lookup_log_chans); 30858d318a50SLinus Walleij kfree(base->lookup_phy_chans); 30868d318a50SLinus Walleij kfree(base->phy_res); 30878d318a50SLinus Walleij kfree(base); 30888d318a50SLinus Walleij } 30898d318a50SLinus Walleij 30908d318a50SLinus Walleij return NULL; 30918d318a50SLinus Walleij } 30928d318a50SLinus Walleij 30938d318a50SLinus Walleij static void __init d40_hw_init(struct d40_base *base) 30948d318a50SLinus Walleij { 30958d318a50SLinus Walleij 30967fb3e75eSNarayanan G static struct d40_reg_val dma_init_reg[] = { 30978d318a50SLinus Walleij /* Clock every part of the DMA block from start */ 30987fb3e75eSNarayanan G { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL}, 30998d318a50SLinus Walleij 31008d318a50SLinus Walleij /* Interrupts on all logical channels */ 31018d318a50SLinus Walleij { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF}, 31028d318a50SLinus Walleij { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF}, 31038d318a50SLinus Walleij { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF}, 31048d318a50SLinus Walleij { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF}, 31058d318a50SLinus Walleij { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF}, 31068d318a50SLinus Walleij { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF}, 31078d318a50SLinus Walleij { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF}, 31088d318a50SLinus Walleij { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF}, 31098d318a50SLinus Walleij { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF}, 31108d318a50SLinus Walleij { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF}, 31118d318a50SLinus Walleij { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF}, 31128d318a50SLinus Walleij { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF} 31138d318a50SLinus Walleij }; 31148d318a50SLinus Walleij int i; 31158d318a50SLinus Walleij u32 prmseo[2] = {0, 0}; 31168d318a50SLinus Walleij u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF}; 31178d318a50SLinus Walleij u32 pcmis = 0; 31188d318a50SLinus Walleij u32 pcicr = 0; 31198d318a50SLinus Walleij 31208d318a50SLinus Walleij for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++) 31218d318a50SLinus Walleij writel(dma_init_reg[i].val, 31228d318a50SLinus Walleij base->virtbase + dma_init_reg[i].reg); 31238d318a50SLinus Walleij 31248d318a50SLinus Walleij /* Configure all our dma channels to default settings */ 31258d318a50SLinus Walleij for (i = 0; i < base->num_phy_chans; i++) { 31268d318a50SLinus Walleij 31278d318a50SLinus Walleij activeo[i % 2] = activeo[i % 2] << 2; 31288d318a50SLinus Walleij 31298d318a50SLinus Walleij if (base->phy_res[base->num_phy_chans - i - 1].allocated_src 31308d318a50SLinus Walleij == D40_ALLOC_PHY) { 31318d318a50SLinus Walleij activeo[i % 2] |= 3; 31328d318a50SLinus Walleij continue; 31338d318a50SLinus Walleij } 31348d318a50SLinus Walleij 31358d318a50SLinus Walleij /* Enable interrupt # */ 31368d318a50SLinus Walleij pcmis = (pcmis << 1) | 1; 31378d318a50SLinus Walleij 31388d318a50SLinus Walleij /* Clear interrupt # */ 31398d318a50SLinus Walleij pcicr = (pcicr << 1) | 1; 31408d318a50SLinus Walleij 31418d318a50SLinus Walleij /* Set channel to physical mode */ 31428d318a50SLinus Walleij prmseo[i % 2] = prmseo[i % 2] << 2; 31438d318a50SLinus Walleij prmseo[i % 2] |= 1; 31448d318a50SLinus Walleij 31458d318a50SLinus Walleij } 31468d318a50SLinus Walleij 31478d318a50SLinus Walleij writel(prmseo[1], base->virtbase + D40_DREG_PRMSE); 31488d318a50SLinus Walleij writel(prmseo[0], base->virtbase + D40_DREG_PRMSO); 31498d318a50SLinus Walleij writel(activeo[1], base->virtbase + D40_DREG_ACTIVE); 31508d318a50SLinus Walleij writel(activeo[0], base->virtbase + D40_DREG_ACTIVO); 31518d318a50SLinus Walleij 31528d318a50SLinus Walleij /* Write which interrupt to enable */ 31538d318a50SLinus Walleij writel(pcmis, base->virtbase + D40_DREG_PCMIS); 31548d318a50SLinus Walleij 31558d318a50SLinus Walleij /* Write which interrupt to clear */ 31568d318a50SLinus Walleij writel(pcicr, base->virtbase + D40_DREG_PCICR); 31578d318a50SLinus Walleij 31588d318a50SLinus Walleij } 31598d318a50SLinus Walleij 3160508849adSLinus Walleij static int __init d40_lcla_allocate(struct d40_base *base) 3161508849adSLinus Walleij { 3162026cbc42SRabin Vincent struct d40_lcla_pool *pool = &base->lcla_pool; 3163508849adSLinus Walleij unsigned long *page_list; 3164508849adSLinus Walleij int i, j; 3165508849adSLinus Walleij int ret = 0; 3166508849adSLinus Walleij 3167508849adSLinus Walleij /* 3168508849adSLinus Walleij * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned, 3169508849adSLinus Walleij * To full fill this hardware requirement without wasting 256 kb 3170508849adSLinus Walleij * we allocate pages until we get an aligned one. 3171508849adSLinus Walleij */ 3172508849adSLinus Walleij page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS, 3173508849adSLinus Walleij GFP_KERNEL); 3174508849adSLinus Walleij 3175508849adSLinus Walleij if (!page_list) { 3176508849adSLinus Walleij ret = -ENOMEM; 3177508849adSLinus Walleij goto failure; 3178508849adSLinus Walleij } 3179508849adSLinus Walleij 3180508849adSLinus Walleij /* Calculating how many pages that are required */ 3181508849adSLinus Walleij base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE; 3182508849adSLinus Walleij 3183508849adSLinus Walleij for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) { 3184508849adSLinus Walleij page_list[i] = __get_free_pages(GFP_KERNEL, 3185508849adSLinus Walleij base->lcla_pool.pages); 3186508849adSLinus Walleij if (!page_list[i]) { 3187508849adSLinus Walleij 31886db5a8baSRabin Vincent d40_err(base->dev, "Failed to allocate %d pages.\n", 31896db5a8baSRabin Vincent base->lcla_pool.pages); 3190508849adSLinus Walleij 3191508849adSLinus Walleij for (j = 0; j < i; j++) 3192508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages); 3193508849adSLinus Walleij goto failure; 3194508849adSLinus Walleij } 3195508849adSLinus Walleij 3196508849adSLinus Walleij if ((virt_to_phys((void *)page_list[i]) & 3197508849adSLinus Walleij (LCLA_ALIGNMENT - 1)) == 0) 3198508849adSLinus Walleij break; 3199508849adSLinus Walleij } 3200508849adSLinus Walleij 3201508849adSLinus Walleij for (j = 0; j < i; j++) 3202508849adSLinus Walleij free_pages(page_list[j], base->lcla_pool.pages); 3203508849adSLinus Walleij 3204508849adSLinus Walleij if (i < MAX_LCLA_ALLOC_ATTEMPTS) { 3205508849adSLinus Walleij base->lcla_pool.base = (void *)page_list[i]; 3206508849adSLinus Walleij } else { 3207767a9675SJonas Aaberg /* 3208767a9675SJonas Aaberg * After many attempts and no succees with finding the correct 3209767a9675SJonas Aaberg * alignment, try with allocating a big buffer. 3210767a9675SJonas Aaberg */ 3211508849adSLinus Walleij dev_warn(base->dev, 3212508849adSLinus Walleij "[%s] Failed to get %d pages @ 18 bit align.\n", 3213508849adSLinus Walleij __func__, base->lcla_pool.pages); 3214508849adSLinus Walleij base->lcla_pool.base_unaligned = kmalloc(SZ_1K * 3215508849adSLinus Walleij base->num_phy_chans + 3216508849adSLinus Walleij LCLA_ALIGNMENT, 3217508849adSLinus Walleij GFP_KERNEL); 3218508849adSLinus Walleij if (!base->lcla_pool.base_unaligned) { 3219508849adSLinus Walleij ret = -ENOMEM; 3220508849adSLinus Walleij goto failure; 3221508849adSLinus Walleij } 3222508849adSLinus Walleij 3223508849adSLinus Walleij base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned, 3224508849adSLinus Walleij LCLA_ALIGNMENT); 3225508849adSLinus Walleij } 3226508849adSLinus Walleij 3227026cbc42SRabin Vincent pool->dma_addr = dma_map_single(base->dev, pool->base, 3228026cbc42SRabin Vincent SZ_1K * base->num_phy_chans, 3229026cbc42SRabin Vincent DMA_TO_DEVICE); 3230026cbc42SRabin Vincent if (dma_mapping_error(base->dev, pool->dma_addr)) { 3231026cbc42SRabin Vincent pool->dma_addr = 0; 3232026cbc42SRabin Vincent ret = -ENOMEM; 3233026cbc42SRabin Vincent goto failure; 3234026cbc42SRabin Vincent } 3235026cbc42SRabin Vincent 3236508849adSLinus Walleij writel(virt_to_phys(base->lcla_pool.base), 3237508849adSLinus Walleij base->virtbase + D40_DREG_LCLA); 3238508849adSLinus Walleij failure: 3239508849adSLinus Walleij kfree(page_list); 3240508849adSLinus Walleij return ret; 3241508849adSLinus Walleij } 3242508849adSLinus Walleij 32438d318a50SLinus Walleij static int __init d40_probe(struct platform_device *pdev) 32448d318a50SLinus Walleij { 32458d318a50SLinus Walleij int err; 32468d318a50SLinus Walleij int ret = -ENOENT; 32478d318a50SLinus Walleij struct d40_base *base; 32488d318a50SLinus Walleij struct resource *res = NULL; 32498d318a50SLinus Walleij int num_reserved_chans; 32508d318a50SLinus Walleij u32 val; 32518d318a50SLinus Walleij 32528d318a50SLinus Walleij base = d40_hw_detect_init(pdev); 32538d318a50SLinus Walleij 32548d318a50SLinus Walleij if (!base) 32558d318a50SLinus Walleij goto failure; 32568d318a50SLinus Walleij 32578d318a50SLinus Walleij num_reserved_chans = d40_phy_res_init(base); 32588d318a50SLinus Walleij 32598d318a50SLinus Walleij platform_set_drvdata(pdev, base); 32608d318a50SLinus Walleij 32618d318a50SLinus Walleij spin_lock_init(&base->interrupt_lock); 32628d318a50SLinus Walleij spin_lock_init(&base->execmd_lock); 32638d318a50SLinus Walleij 32648d318a50SLinus Walleij /* Get IO for logical channel parameter address */ 32658d318a50SLinus Walleij res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa"); 32668d318a50SLinus Walleij if (!res) { 32678d318a50SLinus Walleij ret = -ENOENT; 32686db5a8baSRabin Vincent d40_err(&pdev->dev, "No \"lcpa\" memory resource\n"); 32698d318a50SLinus Walleij goto failure; 32708d318a50SLinus Walleij } 32718d318a50SLinus Walleij base->lcpa_size = resource_size(res); 32728d318a50SLinus Walleij base->phy_lcpa = res->start; 32738d318a50SLinus Walleij 32748d318a50SLinus Walleij if (request_mem_region(res->start, resource_size(res), 32758d318a50SLinus Walleij D40_NAME " I/O lcpa") == NULL) { 32768d318a50SLinus Walleij ret = -EBUSY; 32776db5a8baSRabin Vincent d40_err(&pdev->dev, 32786db5a8baSRabin Vincent "Failed to request LCPA region 0x%x-0x%x\n", 32796db5a8baSRabin Vincent res->start, res->end); 32808d318a50SLinus Walleij goto failure; 32818d318a50SLinus Walleij } 32828d318a50SLinus Walleij 32838d318a50SLinus Walleij /* We make use of ESRAM memory for this. */ 32848d318a50SLinus Walleij val = readl(base->virtbase + D40_DREG_LCPA); 32858d318a50SLinus Walleij if (res->start != val && val != 0) { 32868d318a50SLinus Walleij dev_warn(&pdev->dev, 32878d318a50SLinus Walleij "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n", 32888d318a50SLinus Walleij __func__, val, res->start); 32898d318a50SLinus Walleij } else 32908d318a50SLinus Walleij writel(res->start, base->virtbase + D40_DREG_LCPA); 32918d318a50SLinus Walleij 32928d318a50SLinus Walleij base->lcpa_base = ioremap(res->start, resource_size(res)); 32938d318a50SLinus Walleij if (!base->lcpa_base) { 32948d318a50SLinus Walleij ret = -ENOMEM; 32956db5a8baSRabin Vincent d40_err(&pdev->dev, "Failed to ioremap LCPA region\n"); 32968d318a50SLinus Walleij goto failure; 32978d318a50SLinus Walleij } 329828c7a19dSNarayanan G /* If lcla has to be located in ESRAM we don't need to allocate */ 329928c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) { 330028c7a19dSNarayanan G res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 330128c7a19dSNarayanan G "lcla_esram"); 330228c7a19dSNarayanan G if (!res) { 330328c7a19dSNarayanan G ret = -ENOENT; 330428c7a19dSNarayanan G d40_err(&pdev->dev, 330528c7a19dSNarayanan G "No \"lcla_esram\" memory resource\n"); 330628c7a19dSNarayanan G goto failure; 330728c7a19dSNarayanan G } 330828c7a19dSNarayanan G base->lcla_pool.base = ioremap(res->start, 330928c7a19dSNarayanan G resource_size(res)); 331028c7a19dSNarayanan G if (!base->lcla_pool.base) { 331128c7a19dSNarayanan G ret = -ENOMEM; 331228c7a19dSNarayanan G d40_err(&pdev->dev, "Failed to ioremap LCLA region\n"); 331328c7a19dSNarayanan G goto failure; 331428c7a19dSNarayanan G } 331528c7a19dSNarayanan G writel(res->start, base->virtbase + D40_DREG_LCLA); 3316508849adSLinus Walleij 331728c7a19dSNarayanan G } else { 3318508849adSLinus Walleij ret = d40_lcla_allocate(base); 3319508849adSLinus Walleij if (ret) { 33206db5a8baSRabin Vincent d40_err(&pdev->dev, "Failed to allocate LCLA area\n"); 33218d318a50SLinus Walleij goto failure; 33228d318a50SLinus Walleij } 332328c7a19dSNarayanan G } 33248d318a50SLinus Walleij 33258d318a50SLinus Walleij spin_lock_init(&base->lcla_pool.lock); 33268d318a50SLinus Walleij 33278d318a50SLinus Walleij base->irq = platform_get_irq(pdev, 0); 33288d318a50SLinus Walleij 33298d318a50SLinus Walleij ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base); 33308d318a50SLinus Walleij if (ret) { 33316db5a8baSRabin Vincent d40_err(&pdev->dev, "No IRQ defined\n"); 33328d318a50SLinus Walleij goto failure; 33338d318a50SLinus Walleij } 33348d318a50SLinus Walleij 33357fb3e75eSNarayanan G pm_runtime_irq_safe(base->dev); 33367fb3e75eSNarayanan G pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY); 33377fb3e75eSNarayanan G pm_runtime_use_autosuspend(base->dev); 33387fb3e75eSNarayanan G pm_runtime_enable(base->dev); 33397fb3e75eSNarayanan G pm_runtime_resume(base->dev); 334028c7a19dSNarayanan G 334128c7a19dSNarayanan G if (base->plat_data->use_esram_lcla) { 334228c7a19dSNarayanan G 334328c7a19dSNarayanan G base->lcpa_regulator = regulator_get(base->dev, "lcla_esram"); 334428c7a19dSNarayanan G if (IS_ERR(base->lcpa_regulator)) { 334528c7a19dSNarayanan G d40_err(&pdev->dev, "Failed to get lcpa_regulator\n"); 334628c7a19dSNarayanan G base->lcpa_regulator = NULL; 334728c7a19dSNarayanan G goto failure; 334828c7a19dSNarayanan G } 334928c7a19dSNarayanan G 335028c7a19dSNarayanan G ret = regulator_enable(base->lcpa_regulator); 335128c7a19dSNarayanan G if (ret) { 335228c7a19dSNarayanan G d40_err(&pdev->dev, 335328c7a19dSNarayanan G "Failed to enable lcpa_regulator\n"); 335428c7a19dSNarayanan G regulator_put(base->lcpa_regulator); 335528c7a19dSNarayanan G base->lcpa_regulator = NULL; 335628c7a19dSNarayanan G goto failure; 335728c7a19dSNarayanan G } 335828c7a19dSNarayanan G } 335928c7a19dSNarayanan G 33607fb3e75eSNarayanan G base->initialized = true; 33618d318a50SLinus Walleij err = d40_dmaengine_init(base, num_reserved_chans); 33628d318a50SLinus Walleij if (err) 33638d318a50SLinus Walleij goto failure; 33648d318a50SLinus Walleij 33658d318a50SLinus Walleij d40_hw_init(base); 33668d318a50SLinus Walleij 33678d318a50SLinus Walleij dev_info(base->dev, "initialized\n"); 33688d318a50SLinus Walleij return 0; 33698d318a50SLinus Walleij 33708d318a50SLinus Walleij failure: 33718d318a50SLinus Walleij if (base) { 3372c675b1b4SJonas Aaberg if (base->desc_slab) 3373c675b1b4SJonas Aaberg kmem_cache_destroy(base->desc_slab); 33748d318a50SLinus Walleij if (base->virtbase) 33758d318a50SLinus Walleij iounmap(base->virtbase); 3376026cbc42SRabin Vincent 337728c7a19dSNarayanan G if (base->lcla_pool.base && base->plat_data->use_esram_lcla) { 337828c7a19dSNarayanan G iounmap(base->lcla_pool.base); 337928c7a19dSNarayanan G base->lcla_pool.base = NULL; 338028c7a19dSNarayanan G } 338128c7a19dSNarayanan G 3382026cbc42SRabin Vincent if (base->lcla_pool.dma_addr) 3383026cbc42SRabin Vincent dma_unmap_single(base->dev, base->lcla_pool.dma_addr, 3384026cbc42SRabin Vincent SZ_1K * base->num_phy_chans, 3385026cbc42SRabin Vincent DMA_TO_DEVICE); 3386026cbc42SRabin Vincent 3387508849adSLinus Walleij if (!base->lcla_pool.base_unaligned && base->lcla_pool.base) 3388508849adSLinus Walleij free_pages((unsigned long)base->lcla_pool.base, 3389508849adSLinus Walleij base->lcla_pool.pages); 3390767a9675SJonas Aaberg 3391508849adSLinus Walleij kfree(base->lcla_pool.base_unaligned); 3392767a9675SJonas Aaberg 33938d318a50SLinus Walleij if (base->phy_lcpa) 33948d318a50SLinus Walleij release_mem_region(base->phy_lcpa, 33958d318a50SLinus Walleij base->lcpa_size); 33968d318a50SLinus Walleij if (base->phy_start) 33978d318a50SLinus Walleij release_mem_region(base->phy_start, 33988d318a50SLinus Walleij base->phy_size); 33998d318a50SLinus Walleij if (base->clk) { 34008d318a50SLinus Walleij clk_disable(base->clk); 34018d318a50SLinus Walleij clk_put(base->clk); 34028d318a50SLinus Walleij } 34038d318a50SLinus Walleij 340428c7a19dSNarayanan G if (base->lcpa_regulator) { 340528c7a19dSNarayanan G regulator_disable(base->lcpa_regulator); 340628c7a19dSNarayanan G regulator_put(base->lcpa_regulator); 340728c7a19dSNarayanan G } 340828c7a19dSNarayanan G 34098d318a50SLinus Walleij kfree(base->lcla_pool.alloc_map); 34108d318a50SLinus Walleij kfree(base->lookup_log_chans); 34118d318a50SLinus Walleij kfree(base->lookup_phy_chans); 34128d318a50SLinus Walleij kfree(base->phy_res); 34138d318a50SLinus Walleij kfree(base); 34148d318a50SLinus Walleij } 34158d318a50SLinus Walleij 34166db5a8baSRabin Vincent d40_err(&pdev->dev, "probe failed\n"); 34178d318a50SLinus Walleij return ret; 34188d318a50SLinus Walleij } 34198d318a50SLinus Walleij 34208d318a50SLinus Walleij static struct platform_driver d40_driver = { 34218d318a50SLinus Walleij .driver = { 34228d318a50SLinus Walleij .owner = THIS_MODULE, 34238d318a50SLinus Walleij .name = D40_NAME, 34247fb3e75eSNarayanan G .pm = DMA40_PM_OPS, 34258d318a50SLinus Walleij }, 34268d318a50SLinus Walleij }; 34278d318a50SLinus Walleij 3428cb9ab2d8SRabin Vincent static int __init stedma40_init(void) 34298d318a50SLinus Walleij { 34308d318a50SLinus Walleij return platform_driver_probe(&d40_driver, d40_probe); 34318d318a50SLinus Walleij } 3432a0eb221aSLinus Walleij subsys_initcall(stedma40_init); 3433