Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1 | /* |
Per Forlin | d49278e | 2010-12-20 18:31:38 +0100 | [diff] [blame] | 2 | * Copyright (C) Ericsson AB 2007-2008 |
| 3 | * Copyright (C) ST-Ericsson SA 2008-2010 |
Per Forlin | 661385f | 2010-10-06 09:05:28 +0000 | [diff] [blame] | 4 | * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 5 | * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 6 | * License terms: GNU General Public License (GPL) version 2 |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 9 | #include <linux/dma-mapping.h> |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
Paul Gortmaker | f492b21 | 2011-07-31 16:17:36 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 13 | #include <linux/dmaengine.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/delay.h> |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 17 | #include <linux/pm.h> |
| 18 | #include <linux/pm_runtime.h> |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 19 | #include <linux/err.h> |
Linus Walleij | f4b8976 | 2011-06-27 11:33:46 +0200 | [diff] [blame] | 20 | #include <linux/amba/bus.h> |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 21 | |
| 22 | #include <plat/ste_dma40.h> |
| 23 | |
| 24 | #include "ste_dma40_ll.h" |
| 25 | |
| 26 | #define D40_NAME "dma40" |
| 27 | |
| 28 | #define D40_PHY_CHAN -1 |
| 29 | |
| 30 | /* For masking out/in 2 bit channel positions */ |
| 31 | #define D40_CHAN_POS(chan) (2 * (chan / 2)) |
| 32 | #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan)) |
| 33 | |
| 34 | /* Maximum iterations taken before giving up suspending a channel */ |
| 35 | #define D40_SUSPEND_MAX_IT 500 |
| 36 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 37 | /* Milliseconds */ |
| 38 | #define DMA40_AUTOSUSPEND_DELAY 100 |
| 39 | |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 40 | /* Hardware requirement on LCLA alignment */ |
| 41 | #define LCLA_ALIGNMENT 0x40000 |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 42 | |
| 43 | /* Max number of links per event group */ |
| 44 | #define D40_LCLA_LINK_PER_EVENT_GRP 128 |
| 45 | #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP |
| 46 | |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 47 | /* Attempts before giving up to trying to get pages that are aligned */ |
| 48 | #define MAX_LCLA_ALLOC_ATTEMPTS 256 |
| 49 | |
| 50 | /* Bit markings for allocation map */ |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 51 | #define D40_ALLOC_FREE (1 << 31) |
| 52 | #define D40_ALLOC_PHY (1 << 30) |
| 53 | #define D40_ALLOC_LOG_FREE 0 |
| 54 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 55 | /** |
| 56 | * enum 40_command - The different commands and/or statuses. |
| 57 | * |
| 58 | * @D40_DMA_STOP: DMA channel command STOP or status STOPPED, |
| 59 | * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN. |
| 60 | * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible. |
| 61 | * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED. |
| 62 | */ |
| 63 | enum d40_command { |
| 64 | D40_DMA_STOP = 0, |
| 65 | D40_DMA_RUN = 1, |
| 66 | D40_DMA_SUSPEND_REQ = 2, |
| 67 | D40_DMA_SUSPENDED = 3 |
| 68 | }; |
| 69 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 70 | /* |
| 71 | * These are the registers that has to be saved and later restored |
| 72 | * when the DMA hw is powered off. |
| 73 | * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works. |
| 74 | */ |
| 75 | static u32 d40_backup_regs[] = { |
| 76 | D40_DREG_LCPA, |
| 77 | D40_DREG_LCLA, |
| 78 | D40_DREG_PRMSE, |
| 79 | D40_DREG_PRMSO, |
| 80 | D40_DREG_PRMOE, |
| 81 | D40_DREG_PRMOO, |
| 82 | }; |
| 83 | |
| 84 | #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs) |
| 85 | |
| 86 | /* TODO: Check if all these registers have to be saved/restored on dma40 v3 */ |
| 87 | static u32 d40_backup_regs_v3[] = { |
| 88 | D40_DREG_PSEG1, |
| 89 | D40_DREG_PSEG2, |
| 90 | D40_DREG_PSEG3, |
| 91 | D40_DREG_PSEG4, |
| 92 | D40_DREG_PCEG1, |
| 93 | D40_DREG_PCEG2, |
| 94 | D40_DREG_PCEG3, |
| 95 | D40_DREG_PCEG4, |
| 96 | D40_DREG_RSEG1, |
| 97 | D40_DREG_RSEG2, |
| 98 | D40_DREG_RSEG3, |
| 99 | D40_DREG_RSEG4, |
| 100 | D40_DREG_RCEG1, |
| 101 | D40_DREG_RCEG2, |
| 102 | D40_DREG_RCEG3, |
| 103 | D40_DREG_RCEG4, |
| 104 | }; |
| 105 | |
| 106 | #define BACKUP_REGS_SZ_V3 ARRAY_SIZE(d40_backup_regs_v3) |
| 107 | |
| 108 | static u32 d40_backup_regs_chan[] = { |
| 109 | D40_CHAN_REG_SSCFG, |
| 110 | D40_CHAN_REG_SSELT, |
| 111 | D40_CHAN_REG_SSPTR, |
| 112 | D40_CHAN_REG_SSLNK, |
| 113 | D40_CHAN_REG_SDCFG, |
| 114 | D40_CHAN_REG_SDELT, |
| 115 | D40_CHAN_REG_SDPTR, |
| 116 | D40_CHAN_REG_SDLNK, |
| 117 | }; |
| 118 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 119 | /** |
| 120 | * struct d40_lli_pool - Structure for keeping LLIs in memory |
| 121 | * |
| 122 | * @base: Pointer to memory area when the pre_alloc_lli's are not large |
| 123 | * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if |
| 124 | * pre_alloc_lli is used. |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 125 | * @dma_addr: DMA address, if mapped |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 126 | * @size: The size in bytes of the memory at base or the size of pre_alloc_lli. |
| 127 | * @pre_alloc_lli: Pre allocated area for the most common case of transfers, |
| 128 | * one buffer to one buffer. |
| 129 | */ |
| 130 | struct d40_lli_pool { |
| 131 | void *base; |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 132 | int size; |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 133 | dma_addr_t dma_addr; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 134 | /* Space for dst and src, plus an extra for padding */ |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 135 | u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)]; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * struct d40_desc - A descriptor is one DMA job. |
| 140 | * |
| 141 | * @lli_phy: LLI settings for physical channel. Both src and dst= |
| 142 | * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if |
| 143 | * lli_len equals one. |
| 144 | * @lli_log: Same as above but for logical channels. |
| 145 | * @lli_pool: The pool with two entries pre-allocated. |
Per Friden | 941b77a | 2010-06-20 21:24:45 +0000 | [diff] [blame] | 146 | * @lli_len: Number of llis of current descriptor. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 147 | * @lli_current: Number of transferred llis. |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 148 | * @lcla_alloc: Number of LCLA entries allocated. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 149 | * @txd: DMA engine struct. Used for among other things for communication |
| 150 | * during a transfer. |
| 151 | * @node: List entry. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 152 | * @is_in_client_list: true if the client owns this descriptor. |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 153 | * @cyclic: true if this is a cyclic job |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 154 | * |
| 155 | * This descriptor is used for both logical and physical transfers. |
| 156 | */ |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 157 | struct d40_desc { |
| 158 | /* LLI physical */ |
| 159 | struct d40_phy_lli_bidir lli_phy; |
| 160 | /* LLI logical */ |
| 161 | struct d40_log_lli_bidir lli_log; |
| 162 | |
| 163 | struct d40_lli_pool lli_pool; |
Per Friden | 941b77a | 2010-06-20 21:24:45 +0000 | [diff] [blame] | 164 | int lli_len; |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 165 | int lli_current; |
| 166 | int lcla_alloc; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 167 | |
| 168 | struct dma_async_tx_descriptor txd; |
| 169 | struct list_head node; |
| 170 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 171 | bool is_in_client_list; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 172 | bool cyclic; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * struct d40_lcla_pool - LCLA pool settings and data. |
| 177 | * |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 178 | * @base: The virtual address of LCLA. 18 bit aligned. |
| 179 | * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used. |
| 180 | * This pointer is only there for clean-up on error. |
| 181 | * @pages: The number of pages needed for all physical channels. |
| 182 | * Only used later for clean-up on error |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 183 | * @lock: Lock to protect the content in this struct. |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 184 | * @alloc_map: big map over which LCLA entry is own by which job. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 185 | */ |
| 186 | struct d40_lcla_pool { |
| 187 | void *base; |
Rabin Vincent | 026cbc4 | 2011-01-25 11:18:14 +0100 | [diff] [blame] | 188 | dma_addr_t dma_addr; |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 189 | void *base_unaligned; |
| 190 | int pages; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 191 | spinlock_t lock; |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 192 | struct d40_desc **alloc_map; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | /** |
| 196 | * struct d40_phy_res - struct for handling eventlines mapped to physical |
| 197 | * channels. |
| 198 | * |
| 199 | * @lock: A lock protection this entity. |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 200 | * @reserved: True if used by secure world or otherwise. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 201 | * @num: The physical channel number of this entity. |
| 202 | * @allocated_src: Bit mapped to show which src event line's are mapped to |
| 203 | * this physical channel. Can also be free or physically allocated. |
| 204 | * @allocated_dst: Same as for src but is dst. |
| 205 | * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 206 | * event line number. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 207 | */ |
| 208 | struct d40_phy_res { |
| 209 | spinlock_t lock; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 210 | bool reserved; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 211 | int num; |
| 212 | u32 allocated_src; |
| 213 | u32 allocated_dst; |
| 214 | }; |
| 215 | |
| 216 | struct d40_base; |
| 217 | |
| 218 | /** |
| 219 | * struct d40_chan - Struct that describes a channel. |
| 220 | * |
| 221 | * @lock: A spinlock to protect this struct. |
| 222 | * @log_num: The logical number, if any of this channel. |
| 223 | * @completed: Starts with 1, after first interrupt it is set to dma engine's |
| 224 | * current cookie. |
| 225 | * @pending_tx: The number of pending transfers. Used between interrupt handler |
| 226 | * and tasklet. |
| 227 | * @busy: Set to true when transfer is ongoing on this channel. |
Jonas Aaberg | 2a61434 | 2010-06-20 21:25:24 +0000 | [diff] [blame] | 228 | * @phy_chan: Pointer to physical channel which this instance runs on. If this |
| 229 | * point is NULL, then the channel is not allocated. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 230 | * @chan: DMA engine handle. |
| 231 | * @tasklet: Tasklet that gets scheduled from interrupt context to complete a |
| 232 | * transfer and call client callback. |
| 233 | * @client: Cliented owned descriptor list. |
Per Forlin | da063d2 | 2011-08-29 13:33:32 +0200 | [diff] [blame] | 234 | * @pending_queue: Submitted jobs, to be issued by issue_pending() |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 235 | * @active: Active descriptor. |
| 236 | * @queue: Queued jobs. |
Per Forlin | 82babbb36 | 2011-08-29 13:33:35 +0200 | [diff] [blame] | 237 | * @prepare_queue: Prepared jobs. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 238 | * @dma_cfg: The client configuration of this dma channel. |
Rabin Vincent | ce2ca12 | 2010-10-12 13:00:49 +0000 | [diff] [blame] | 239 | * @configured: whether the dma_cfg configuration is valid |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 240 | * @base: Pointer to the device instance struct. |
| 241 | * @src_def_cfg: Default cfg register setting for src. |
| 242 | * @dst_def_cfg: Default cfg register setting for dst. |
| 243 | * @log_def: Default logical channel settings. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 244 | * @lcpa: Pointer to dst and src lcpa settings. |
om prakash | ae752bf | 2011-06-27 11:33:31 +0200 | [diff] [blame] | 245 | * @runtime_addr: runtime configured address. |
| 246 | * @runtime_direction: runtime configured direction. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 247 | * |
| 248 | * This struct can either "be" a logical or a physical channel. |
| 249 | */ |
| 250 | struct d40_chan { |
| 251 | spinlock_t lock; |
| 252 | int log_num; |
| 253 | /* ID of the most recent completed transfer */ |
| 254 | int completed; |
| 255 | int pending_tx; |
| 256 | bool busy; |
| 257 | struct d40_phy_res *phy_chan; |
| 258 | struct dma_chan chan; |
| 259 | struct tasklet_struct tasklet; |
| 260 | struct list_head client; |
Per Forlin | a8f3067 | 2011-06-26 23:29:52 +0200 | [diff] [blame] | 261 | struct list_head pending_queue; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 262 | struct list_head active; |
| 263 | struct list_head queue; |
Per Forlin | 82babbb36 | 2011-08-29 13:33:35 +0200 | [diff] [blame] | 264 | struct list_head prepare_queue; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 265 | struct stedma40_chan_cfg dma_cfg; |
Rabin Vincent | ce2ca12 | 2010-10-12 13:00:49 +0000 | [diff] [blame] | 266 | bool configured; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 267 | struct d40_base *base; |
| 268 | /* Default register configurations */ |
| 269 | u32 src_def_cfg; |
| 270 | u32 dst_def_cfg; |
| 271 | struct d40_def_lcsp log_def; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 272 | struct d40_log_lli_full *lcpa; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 273 | /* Runtime reconfiguration */ |
| 274 | dma_addr_t runtime_addr; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 275 | enum dma_transfer_direction runtime_direction; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * struct d40_base - The big global struct, one for each probe'd instance. |
| 280 | * |
| 281 | * @interrupt_lock: Lock used to make sure one interrupt is handle a time. |
| 282 | * @execmd_lock: Lock for execute command usage since several channels share |
| 283 | * the same physical register. |
| 284 | * @dev: The device structure. |
| 285 | * @virtbase: The virtual base address of the DMA's register. |
Linus Walleij | f418559 | 2010-06-22 18:06:42 -0700 | [diff] [blame] | 286 | * @rev: silicon revision detected. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 287 | * @clk: Pointer to the DMA clock structure. |
| 288 | * @phy_start: Physical memory start of the DMA registers. |
| 289 | * @phy_size: Size of the DMA register map. |
| 290 | * @irq: The IRQ number. |
| 291 | * @num_phy_chans: The number of physical channels. Read from HW. This |
| 292 | * is the number of available channels for this driver, not counting "Secure |
| 293 | * mode" allocated physical channels. |
| 294 | * @num_log_chans: The number of logical channels. Calculated from |
| 295 | * num_phy_chans. |
| 296 | * @dma_both: dma_device channels that can do both memcpy and slave transfers. |
| 297 | * @dma_slave: dma_device channels that can do only do slave transfers. |
| 298 | * @dma_memcpy: dma_device channels that can do only do memcpy transfers. |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 299 | * @phy_chans: Room for all possible physical channels in system. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 300 | * @log_chans: Room for all possible logical channels in system. |
| 301 | * @lookup_log_chans: Used to map interrupt number to logical channel. Points |
| 302 | * to log_chans entries. |
| 303 | * @lookup_phy_chans: Used to map interrupt number to physical channel. Points |
| 304 | * to phy_chans entries. |
| 305 | * @plat_data: Pointer to provided platform_data which is the driver |
| 306 | * configuration. |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 307 | * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla. |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 308 | * @phy_res: Vector containing all physical channels. |
| 309 | * @lcla_pool: lcla pool settings and data. |
| 310 | * @lcpa_base: The virtual mapped address of LCPA. |
| 311 | * @phy_lcpa: The physical address of the LCPA. |
| 312 | * @lcpa_size: The size of the LCPA area. |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 313 | * @desc_slab: cache for descriptors. |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 314 | * @reg_val_backup: Here the values of some hardware registers are stored |
| 315 | * before the DMA is powered off. They are restored when the power is back on. |
| 316 | * @reg_val_backup_v3: Backup of registers that only exits on dma40 v3 and |
| 317 | * later. |
| 318 | * @reg_val_backup_chan: Backup data for standard channel parameter registers. |
| 319 | * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off. |
| 320 | * @initialized: true if the dma has been initialized |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 321 | */ |
| 322 | struct d40_base { |
| 323 | spinlock_t interrupt_lock; |
| 324 | spinlock_t execmd_lock; |
| 325 | struct device *dev; |
| 326 | void __iomem *virtbase; |
Linus Walleij | f418559 | 2010-06-22 18:06:42 -0700 | [diff] [blame] | 327 | u8 rev:4; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 328 | struct clk *clk; |
| 329 | phys_addr_t phy_start; |
| 330 | resource_size_t phy_size; |
| 331 | int irq; |
| 332 | int num_phy_chans; |
| 333 | int num_log_chans; |
| 334 | struct dma_device dma_both; |
| 335 | struct dma_device dma_slave; |
| 336 | struct dma_device dma_memcpy; |
| 337 | struct d40_chan *phy_chans; |
| 338 | struct d40_chan *log_chans; |
| 339 | struct d40_chan **lookup_log_chans; |
| 340 | struct d40_chan **lookup_phy_chans; |
| 341 | struct stedma40_platform_data *plat_data; |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 342 | struct regulator *lcpa_regulator; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 343 | /* Physical half channels */ |
| 344 | struct d40_phy_res *phy_res; |
| 345 | struct d40_lcla_pool lcla_pool; |
| 346 | void *lcpa_base; |
| 347 | dma_addr_t phy_lcpa; |
| 348 | resource_size_t lcpa_size; |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 349 | struct kmem_cache *desc_slab; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 350 | u32 reg_val_backup[BACKUP_REGS_SZ]; |
| 351 | u32 reg_val_backup_v3[BACKUP_REGS_SZ_V3]; |
| 352 | u32 *reg_val_backup_chan; |
| 353 | u16 gcc_pwr_off_mask; |
| 354 | bool initialized; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | /** |
| 358 | * struct d40_interrupt_lookup - lookup table for interrupt handler |
| 359 | * |
| 360 | * @src: Interrupt mask register. |
| 361 | * @clr: Interrupt clear register. |
| 362 | * @is_error: true if this is an error interrupt. |
| 363 | * @offset: start delta in the lookup_log_chans in d40_base. If equals to |
| 364 | * D40_PHY_CHAN, the lookup_phy_chans shall be used instead. |
| 365 | */ |
| 366 | struct d40_interrupt_lookup { |
| 367 | u32 src; |
| 368 | u32 clr; |
| 369 | bool is_error; |
| 370 | int offset; |
| 371 | }; |
| 372 | |
| 373 | /** |
| 374 | * struct d40_reg_val - simple lookup struct |
| 375 | * |
| 376 | * @reg: The register. |
| 377 | * @val: The value that belongs to the register in reg. |
| 378 | */ |
| 379 | struct d40_reg_val { |
| 380 | unsigned int reg; |
| 381 | unsigned int val; |
| 382 | }; |
| 383 | |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 384 | static struct device *chan2dev(struct d40_chan *d40c) |
| 385 | { |
| 386 | return &d40c->chan.dev->device; |
| 387 | } |
| 388 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 389 | static bool chan_is_physical(struct d40_chan *chan) |
| 390 | { |
| 391 | return chan->log_num == D40_PHY_CHAN; |
| 392 | } |
| 393 | |
| 394 | static bool chan_is_logical(struct d40_chan *chan) |
| 395 | { |
| 396 | return !chan_is_physical(chan); |
| 397 | } |
| 398 | |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 399 | static void __iomem *chan_base(struct d40_chan *chan) |
| 400 | { |
| 401 | return chan->base->virtbase + D40_DREG_PCBASE + |
| 402 | chan->phy_chan->num * D40_DREG_PCDELTA; |
| 403 | } |
| 404 | |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 405 | #define d40_err(dev, format, arg...) \ |
| 406 | dev_err(dev, "[%s] " format, __func__, ## arg) |
| 407 | |
| 408 | #define chan_err(d40c, format, arg...) \ |
| 409 | d40_err(chan2dev(d40c), format, ## arg) |
| 410 | |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 411 | static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d, |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 412 | int lli_len) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 413 | { |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 414 | bool is_log = chan_is_logical(d40c); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 415 | u32 align; |
| 416 | void *base; |
| 417 | |
| 418 | if (is_log) |
| 419 | align = sizeof(struct d40_log_lli); |
| 420 | else |
| 421 | align = sizeof(struct d40_phy_lli); |
| 422 | |
| 423 | if (lli_len == 1) { |
| 424 | base = d40d->lli_pool.pre_alloc_lli; |
| 425 | d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli); |
| 426 | d40d->lli_pool.base = NULL; |
| 427 | } else { |
Rabin Vincent | 594ece4 | 2011-01-25 11:18:12 +0100 | [diff] [blame] | 428 | d40d->lli_pool.size = lli_len * 2 * align; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 429 | |
| 430 | base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT); |
| 431 | d40d->lli_pool.base = base; |
| 432 | |
| 433 | if (d40d->lli_pool.base == NULL) |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
| 437 | if (is_log) { |
Rabin Vincent | d924aba | 2011-01-25 11:18:16 +0100 | [diff] [blame] | 438 | d40d->lli_log.src = PTR_ALIGN(base, align); |
Rabin Vincent | 594ece4 | 2011-01-25 11:18:12 +0100 | [diff] [blame] | 439 | d40d->lli_log.dst = d40d->lli_log.src + lli_len; |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 440 | |
| 441 | d40d->lli_pool.dma_addr = 0; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 442 | } else { |
Rabin Vincent | d924aba | 2011-01-25 11:18:16 +0100 | [diff] [blame] | 443 | d40d->lli_phy.src = PTR_ALIGN(base, align); |
Rabin Vincent | 594ece4 | 2011-01-25 11:18:12 +0100 | [diff] [blame] | 444 | d40d->lli_phy.dst = d40d->lli_phy.src + lli_len; |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 445 | |
| 446 | d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev, |
| 447 | d40d->lli_phy.src, |
| 448 | d40d->lli_pool.size, |
| 449 | DMA_TO_DEVICE); |
| 450 | |
| 451 | if (dma_mapping_error(d40c->base->dev, |
| 452 | d40d->lli_pool.dma_addr)) { |
| 453 | kfree(d40d->lli_pool.base); |
| 454 | d40d->lli_pool.base = NULL; |
| 455 | d40d->lli_pool.dma_addr = 0; |
| 456 | return -ENOMEM; |
| 457 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 463 | static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 464 | { |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 465 | if (d40d->lli_pool.dma_addr) |
| 466 | dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr, |
| 467 | d40d->lli_pool.size, DMA_TO_DEVICE); |
| 468 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 469 | kfree(d40d->lli_pool.base); |
| 470 | d40d->lli_pool.base = NULL; |
| 471 | d40d->lli_pool.size = 0; |
| 472 | d40d->lli_log.src = NULL; |
| 473 | d40d->lli_log.dst = NULL; |
| 474 | d40d->lli_phy.src = NULL; |
| 475 | d40d->lli_phy.dst = NULL; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 478 | static int d40_lcla_alloc_one(struct d40_chan *d40c, |
| 479 | struct d40_desc *d40d) |
| 480 | { |
| 481 | unsigned long flags; |
| 482 | int i; |
| 483 | int ret = -EINVAL; |
| 484 | int p; |
| 485 | |
| 486 | spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); |
| 487 | |
| 488 | p = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP; |
| 489 | |
| 490 | /* |
| 491 | * Allocate both src and dst at the same time, therefore the half |
| 492 | * start on 1 since 0 can't be used since zero is used as end marker. |
| 493 | */ |
| 494 | for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { |
| 495 | if (!d40c->base->lcla_pool.alloc_map[p + i]) { |
| 496 | d40c->base->lcla_pool.alloc_map[p + i] = d40d; |
| 497 | d40d->lcla_alloc++; |
| 498 | ret = i; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); |
| 504 | |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | static int d40_lcla_free_all(struct d40_chan *d40c, |
| 509 | struct d40_desc *d40d) |
| 510 | { |
| 511 | unsigned long flags; |
| 512 | int i; |
| 513 | int ret = -EINVAL; |
| 514 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 515 | if (chan_is_physical(d40c)) |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 516 | return 0; |
| 517 | |
| 518 | spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags); |
| 519 | |
| 520 | for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) { |
| 521 | if (d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * |
| 522 | D40_LCLA_LINK_PER_EVENT_GRP + i] == d40d) { |
| 523 | d40c->base->lcla_pool.alloc_map[d40c->phy_chan->num * |
| 524 | D40_LCLA_LINK_PER_EVENT_GRP + i] = NULL; |
| 525 | d40d->lcla_alloc--; |
| 526 | if (d40d->lcla_alloc == 0) { |
| 527 | ret = 0; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags); |
| 534 | |
| 535 | return ret; |
| 536 | |
| 537 | } |
| 538 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 539 | static void d40_desc_remove(struct d40_desc *d40d) |
| 540 | { |
| 541 | list_del(&d40d->node); |
| 542 | } |
| 543 | |
| 544 | static struct d40_desc *d40_desc_get(struct d40_chan *d40c) |
| 545 | { |
Rabin Vincent | a2c15fa | 2010-10-06 08:20:37 +0000 | [diff] [blame] | 546 | struct d40_desc *desc = NULL; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 547 | |
| 548 | if (!list_empty(&d40c->client)) { |
Rabin Vincent | a2c15fa | 2010-10-06 08:20:37 +0000 | [diff] [blame] | 549 | struct d40_desc *d; |
| 550 | struct d40_desc *_d; |
| 551 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 552 | list_for_each_entry_safe(d, _d, &d40c->client, node) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 553 | if (async_tx_test_ack(&d->txd)) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 554 | d40_desc_remove(d); |
Rabin Vincent | a2c15fa | 2010-10-06 08:20:37 +0000 | [diff] [blame] | 555 | desc = d; |
| 556 | memset(desc, 0, sizeof(*desc)); |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 557 | break; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 558 | } |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 559 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 560 | } |
Rabin Vincent | a2c15fa | 2010-10-06 08:20:37 +0000 | [diff] [blame] | 561 | |
| 562 | if (!desc) |
| 563 | desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT); |
| 564 | |
| 565 | if (desc) |
| 566 | INIT_LIST_HEAD(&desc->node); |
| 567 | |
| 568 | return desc; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d) |
| 572 | { |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 573 | |
Rabin Vincent | b00f938 | 2011-01-25 11:18:15 +0100 | [diff] [blame] | 574 | d40_pool_lli_free(d40c, d40d); |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 575 | d40_lcla_free_all(d40c, d40d); |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 576 | kmem_cache_free(d40c->base->desc_slab, d40d); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc) |
| 580 | { |
| 581 | list_add_tail(&desc->node, &d40c->active); |
| 582 | } |
| 583 | |
Rabin Vincent | 1c4b092 | 2011-01-25 11:18:24 +0100 | [diff] [blame] | 584 | static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc) |
| 585 | { |
| 586 | struct d40_phy_lli *lli_dst = desc->lli_phy.dst; |
| 587 | struct d40_phy_lli *lli_src = desc->lli_phy.src; |
| 588 | void __iomem *base = chan_base(chan); |
| 589 | |
| 590 | writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG); |
| 591 | writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT); |
| 592 | writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR); |
| 593 | writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK); |
| 594 | |
| 595 | writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG); |
| 596 | writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT); |
| 597 | writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR); |
| 598 | writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK); |
| 599 | } |
| 600 | |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 601 | static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc) |
| 602 | { |
| 603 | struct d40_lcla_pool *pool = &chan->base->lcla_pool; |
| 604 | struct d40_log_lli_bidir *lli = &desc->lli_log; |
| 605 | int lli_current = desc->lli_current; |
| 606 | int lli_len = desc->lli_len; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 607 | bool cyclic = desc->cyclic; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 608 | int curr_lcla = -EINVAL; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 609 | int first_lcla = 0; |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 610 | bool use_esram_lcla = chan->base->plat_data->use_esram_lcla; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 611 | bool linkback; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 612 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 613 | /* |
| 614 | * We may have partially running cyclic transfers, in case we did't get |
| 615 | * enough LCLA entries. |
| 616 | */ |
| 617 | linkback = cyclic && lli_current == 0; |
| 618 | |
| 619 | /* |
| 620 | * For linkback, we need one LCLA even with only one link, because we |
| 621 | * can't link back to the one in LCPA space |
| 622 | */ |
| 623 | if (linkback || (lli_len - lli_current > 1)) { |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 624 | curr_lcla = d40_lcla_alloc_one(chan, desc); |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 625 | first_lcla = curr_lcla; |
| 626 | } |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 627 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 628 | /* |
| 629 | * For linkback, we normally load the LCPA in the loop since we need to |
| 630 | * link it to the second LCLA and not the first. However, if we |
| 631 | * couldn't even get a first LCLA, then we have to run in LCPA and |
| 632 | * reload manually. |
| 633 | */ |
| 634 | if (!linkback || curr_lcla == -EINVAL) { |
| 635 | unsigned int flags = 0; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 636 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 637 | if (curr_lcla == -EINVAL) |
| 638 | flags |= LLI_TERM_INT; |
| 639 | |
| 640 | d40_log_lli_lcpa_write(chan->lcpa, |
| 641 | &lli->dst[lli_current], |
| 642 | &lli->src[lli_current], |
| 643 | curr_lcla, |
| 644 | flags); |
| 645 | lli_current++; |
| 646 | } |
Rabin Vincent | 6045f0b | 2011-01-25 11:18:32 +0100 | [diff] [blame] | 647 | |
| 648 | if (curr_lcla < 0) |
| 649 | goto out; |
| 650 | |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 651 | for (; lli_current < lli_len; lli_current++) { |
| 652 | unsigned int lcla_offset = chan->phy_chan->num * 1024 + |
| 653 | 8 * curr_lcla * 2; |
| 654 | struct d40_log_lli *lcla = pool->base + lcla_offset; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 655 | unsigned int flags = 0; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 656 | int next_lcla; |
| 657 | |
| 658 | if (lli_current + 1 < lli_len) |
| 659 | next_lcla = d40_lcla_alloc_one(chan, desc); |
| 660 | else |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 661 | next_lcla = linkback ? first_lcla : -EINVAL; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 662 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 663 | if (cyclic || next_lcla == -EINVAL) |
| 664 | flags |= LLI_TERM_INT; |
| 665 | |
| 666 | if (linkback && curr_lcla == first_lcla) { |
| 667 | /* First link goes in both LCPA and LCLA */ |
| 668 | d40_log_lli_lcpa_write(chan->lcpa, |
| 669 | &lli->dst[lli_current], |
| 670 | &lli->src[lli_current], |
| 671 | next_lcla, flags); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * One unused LCLA in the cyclic case if the very first |
| 676 | * next_lcla fails... |
| 677 | */ |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 678 | d40_log_lli_lcla_write(lcla, |
| 679 | &lli->dst[lli_current], |
| 680 | &lli->src[lli_current], |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 681 | next_lcla, flags); |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 682 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 683 | /* |
| 684 | * Cache maintenance is not needed if lcla is |
| 685 | * mapped in esram |
| 686 | */ |
| 687 | if (!use_esram_lcla) { |
| 688 | dma_sync_single_range_for_device(chan->base->dev, |
| 689 | pool->dma_addr, lcla_offset, |
| 690 | 2 * sizeof(struct d40_log_lli), |
| 691 | DMA_TO_DEVICE); |
| 692 | } |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 693 | curr_lcla = next_lcla; |
| 694 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 695 | if (curr_lcla == -EINVAL || curr_lcla == first_lcla) { |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 696 | lli_current++; |
| 697 | break; |
| 698 | } |
| 699 | } |
| 700 | |
Rabin Vincent | 6045f0b | 2011-01-25 11:18:32 +0100 | [diff] [blame] | 701 | out: |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 702 | desc->lli_current = lli_current; |
| 703 | } |
| 704 | |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 705 | static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d) |
| 706 | { |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 707 | if (chan_is_physical(d40c)) { |
Rabin Vincent | 1c4b092 | 2011-01-25 11:18:24 +0100 | [diff] [blame] | 708 | d40_phy_lli_load(d40c, d40d); |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 709 | d40d->lli_current = d40d->lli_len; |
Rabin Vincent | e65889c | 2011-01-25 11:18:31 +0100 | [diff] [blame] | 710 | } else |
| 711 | d40_log_lli_to_lcxa(d40c, d40d); |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 714 | static struct d40_desc *d40_first_active_get(struct d40_chan *d40c) |
| 715 | { |
| 716 | struct d40_desc *d; |
| 717 | |
| 718 | if (list_empty(&d40c->active)) |
| 719 | return NULL; |
| 720 | |
| 721 | d = list_first_entry(&d40c->active, |
| 722 | struct d40_desc, |
| 723 | node); |
| 724 | return d; |
| 725 | } |
| 726 | |
Per Forlin | 7404368 | 2011-08-29 13:33:34 +0200 | [diff] [blame] | 727 | /* remove desc from current queue and add it to the pending_queue */ |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 728 | static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc) |
| 729 | { |
Per Forlin | 7404368 | 2011-08-29 13:33:34 +0200 | [diff] [blame] | 730 | d40_desc_remove(desc); |
| 731 | desc->is_in_client_list = false; |
Per Forlin | a8f3067 | 2011-06-26 23:29:52 +0200 | [diff] [blame] | 732 | list_add_tail(&desc->node, &d40c->pending_queue); |
| 733 | } |
| 734 | |
| 735 | static struct d40_desc *d40_first_pending(struct d40_chan *d40c) |
| 736 | { |
| 737 | struct d40_desc *d; |
| 738 | |
| 739 | if (list_empty(&d40c->pending_queue)) |
| 740 | return NULL; |
| 741 | |
| 742 | d = list_first_entry(&d40c->pending_queue, |
| 743 | struct d40_desc, |
| 744 | node); |
| 745 | return d; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static struct d40_desc *d40_first_queued(struct d40_chan *d40c) |
| 749 | { |
| 750 | struct d40_desc *d; |
| 751 | |
| 752 | if (list_empty(&d40c->queue)) |
| 753 | return NULL; |
| 754 | |
| 755 | d = list_first_entry(&d40c->queue, |
| 756 | struct d40_desc, |
| 757 | node); |
| 758 | return d; |
| 759 | } |
| 760 | |
Per Forlin | d49278e | 2010-12-20 18:31:38 +0100 | [diff] [blame] | 761 | static int d40_psize_2_burst_size(bool is_log, int psize) |
| 762 | { |
| 763 | if (is_log) { |
| 764 | if (psize == STEDMA40_PSIZE_LOG_1) |
| 765 | return 1; |
| 766 | } else { |
| 767 | if (psize == STEDMA40_PSIZE_PHY_1) |
| 768 | return 1; |
| 769 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 770 | |
Per Forlin | d49278e | 2010-12-20 18:31:38 +0100 | [diff] [blame] | 771 | return 2 << psize; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * The dma only supports transmitting packages up to |
| 776 | * STEDMA40_MAX_SEG_SIZE << data_width. Calculate the total number of |
| 777 | * dma elements required to send the entire sg list |
| 778 | */ |
| 779 | static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2) |
| 780 | { |
| 781 | int dmalen; |
| 782 | u32 max_w = max(data_width1, data_width2); |
| 783 | u32 min_w = min(data_width1, data_width2); |
| 784 | u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE << min_w, 1 << max_w); |
| 785 | |
| 786 | if (seg_max > STEDMA40_MAX_SEG_SIZE) |
| 787 | seg_max -= (1 << max_w); |
| 788 | |
| 789 | if (!IS_ALIGNED(size, 1 << max_w)) |
| 790 | return -EINVAL; |
| 791 | |
| 792 | if (size <= seg_max) |
| 793 | dmalen = 1; |
| 794 | else { |
| 795 | dmalen = size / seg_max; |
| 796 | if (dmalen * seg_max < size) |
| 797 | dmalen++; |
| 798 | } |
| 799 | return dmalen; |
| 800 | } |
| 801 | |
| 802 | static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len, |
| 803 | u32 data_width1, u32 data_width2) |
| 804 | { |
| 805 | struct scatterlist *sg; |
| 806 | int i; |
| 807 | int len = 0; |
| 808 | int ret; |
| 809 | |
| 810 | for_each_sg(sgl, sg, sg_len, i) { |
| 811 | ret = d40_size_2_dmalen(sg_dma_len(sg), |
| 812 | data_width1, data_width2); |
| 813 | if (ret < 0) |
| 814 | return ret; |
| 815 | len += ret; |
| 816 | } |
| 817 | return len; |
| 818 | } |
| 819 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 820 | |
| 821 | #ifdef CONFIG_PM |
| 822 | static void dma40_backup(void __iomem *baseaddr, u32 *backup, |
| 823 | u32 *regaddr, int num, bool save) |
| 824 | { |
| 825 | int i; |
| 826 | |
| 827 | for (i = 0; i < num; i++) { |
| 828 | void __iomem *addr = baseaddr + regaddr[i]; |
| 829 | |
| 830 | if (save) |
| 831 | backup[i] = readl_relaxed(addr); |
| 832 | else |
| 833 | writel_relaxed(backup[i], addr); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | static void d40_save_restore_registers(struct d40_base *base, bool save) |
| 838 | { |
| 839 | int i; |
| 840 | |
| 841 | /* Save/Restore channel specific registers */ |
| 842 | for (i = 0; i < base->num_phy_chans; i++) { |
| 843 | void __iomem *addr; |
| 844 | int idx; |
| 845 | |
| 846 | if (base->phy_res[i].reserved) |
| 847 | continue; |
| 848 | |
| 849 | addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA; |
| 850 | idx = i * ARRAY_SIZE(d40_backup_regs_chan); |
| 851 | |
| 852 | dma40_backup(addr, &base->reg_val_backup_chan[idx], |
| 853 | d40_backup_regs_chan, |
| 854 | ARRAY_SIZE(d40_backup_regs_chan), |
| 855 | save); |
| 856 | } |
| 857 | |
| 858 | /* Save/Restore global registers */ |
| 859 | dma40_backup(base->virtbase, base->reg_val_backup, |
| 860 | d40_backup_regs, ARRAY_SIZE(d40_backup_regs), |
| 861 | save); |
| 862 | |
| 863 | /* Save/Restore registers only existing on dma40 v3 and later */ |
| 864 | if (base->rev >= 3) |
| 865 | dma40_backup(base->virtbase, base->reg_val_backup_v3, |
| 866 | d40_backup_regs_v3, |
| 867 | ARRAY_SIZE(d40_backup_regs_v3), |
| 868 | save); |
| 869 | } |
| 870 | #else |
| 871 | static void d40_save_restore_registers(struct d40_base *base, bool save) |
| 872 | { |
| 873 | } |
| 874 | #endif |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 875 | |
| 876 | static int d40_channel_execute_command(struct d40_chan *d40c, |
| 877 | enum d40_command command) |
| 878 | { |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 879 | u32 status; |
| 880 | int i; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 881 | void __iomem *active_reg; |
| 882 | int ret = 0; |
| 883 | unsigned long flags; |
Jonas Aaberg | 1d392a7 | 2010-06-20 21:26:01 +0000 | [diff] [blame] | 884 | u32 wmask; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 885 | |
| 886 | spin_lock_irqsave(&d40c->base->execmd_lock, flags); |
| 887 | |
| 888 | if (d40c->phy_chan->num % 2 == 0) |
| 889 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; |
| 890 | else |
| 891 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; |
| 892 | |
| 893 | if (command == D40_DMA_SUSPEND_REQ) { |
| 894 | status = (readl(active_reg) & |
| 895 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> |
| 896 | D40_CHAN_POS(d40c->phy_chan->num); |
| 897 | |
| 898 | if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) |
| 899 | goto done; |
| 900 | } |
| 901 | |
Jonas Aaberg | 1d392a7 | 2010-06-20 21:26:01 +0000 | [diff] [blame] | 902 | wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num)); |
| 903 | writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)), |
| 904 | active_reg); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 905 | |
| 906 | if (command == D40_DMA_SUSPEND_REQ) { |
| 907 | |
| 908 | for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) { |
| 909 | status = (readl(active_reg) & |
| 910 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> |
| 911 | D40_CHAN_POS(d40c->phy_chan->num); |
| 912 | |
| 913 | cpu_relax(); |
| 914 | /* |
| 915 | * Reduce the number of bus accesses while |
| 916 | * waiting for the DMA to suspend. |
| 917 | */ |
| 918 | udelay(3); |
| 919 | |
| 920 | if (status == D40_DMA_STOP || |
| 921 | status == D40_DMA_SUSPENDED) |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | if (i == D40_SUSPEND_MAX_IT) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 926 | chan_err(d40c, |
| 927 | "unable to suspend the chl %d (log: %d) status %x\n", |
| 928 | d40c->phy_chan->num, d40c->log_num, |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 929 | status); |
| 930 | dump_stack(); |
| 931 | ret = -EBUSY; |
| 932 | } |
| 933 | |
| 934 | } |
| 935 | done: |
| 936 | spin_unlock_irqrestore(&d40c->base->execmd_lock, flags); |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | static void d40_term_all(struct d40_chan *d40c) |
| 941 | { |
| 942 | struct d40_desc *d40d; |
Per Forlin | 7404368 | 2011-08-29 13:33:34 +0200 | [diff] [blame] | 943 | struct d40_desc *_d; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 944 | |
| 945 | /* Release active descriptors */ |
| 946 | while ((d40d = d40_first_active_get(d40c))) { |
| 947 | d40_desc_remove(d40d); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 948 | d40_desc_free(d40c, d40d); |
| 949 | } |
| 950 | |
| 951 | /* Release queued descriptors waiting for transfer */ |
| 952 | while ((d40d = d40_first_queued(d40c))) { |
| 953 | d40_desc_remove(d40d); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 954 | d40_desc_free(d40c, d40d); |
| 955 | } |
| 956 | |
Per Forlin | a8f3067 | 2011-06-26 23:29:52 +0200 | [diff] [blame] | 957 | /* Release pending descriptors */ |
| 958 | while ((d40d = d40_first_pending(d40c))) { |
| 959 | d40_desc_remove(d40d); |
| 960 | d40_desc_free(d40c, d40d); |
| 961 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 962 | |
Per Forlin | 7404368 | 2011-08-29 13:33:34 +0200 | [diff] [blame] | 963 | /* Release client owned descriptors */ |
| 964 | if (!list_empty(&d40c->client)) |
| 965 | list_for_each_entry_safe(d40d, _d, &d40c->client, node) { |
| 966 | d40_desc_remove(d40d); |
| 967 | d40_desc_free(d40c, d40d); |
| 968 | } |
| 969 | |
Per Forlin | 82babbb36 | 2011-08-29 13:33:35 +0200 | [diff] [blame] | 970 | /* Release descriptors in prepare queue */ |
| 971 | if (!list_empty(&d40c->prepare_queue)) |
| 972 | list_for_each_entry_safe(d40d, _d, |
| 973 | &d40c->prepare_queue, node) { |
| 974 | d40_desc_remove(d40d); |
| 975 | d40_desc_free(d40c, d40d); |
| 976 | } |
Per Forlin | 7404368 | 2011-08-29 13:33:34 +0200 | [diff] [blame] | 977 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 978 | d40c->pending_tx = 0; |
| 979 | d40c->busy = false; |
| 980 | } |
| 981 | |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 982 | static void __d40_config_set_event(struct d40_chan *d40c, bool enable, |
| 983 | u32 event, int reg) |
| 984 | { |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 985 | void __iomem *addr = chan_base(d40c) + reg; |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 986 | int tries; |
| 987 | |
| 988 | if (!enable) { |
| 989 | writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) |
| 990 | | ~D40_EVENTLINE_MASK(event), addr); |
| 991 | return; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * The hardware sometimes doesn't register the enable when src and dst |
| 996 | * event lines are active on the same logical channel. Retry to ensure |
| 997 | * it does. Usually only one retry is sufficient. |
| 998 | */ |
| 999 | tries = 100; |
| 1000 | while (--tries) { |
| 1001 | writel((D40_ACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event)) |
| 1002 | | ~D40_EVENTLINE_MASK(event), addr); |
| 1003 | |
| 1004 | if (readl(addr) & D40_EVENTLINE_MASK(event)) |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | if (tries != 99) |
| 1009 | dev_dbg(chan2dev(d40c), |
| 1010 | "[%s] workaround enable S%cLNK (%d tries)\n", |
| 1011 | __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D', |
| 1012 | 100 - tries); |
| 1013 | |
| 1014 | WARN_ON(!tries); |
| 1015 | } |
| 1016 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1017 | static void d40_config_set_event(struct d40_chan *d40c, bool do_enable) |
| 1018 | { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1019 | unsigned long flags; |
| 1020 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1021 | spin_lock_irqsave(&d40c->phy_chan->lock, flags); |
| 1022 | |
| 1023 | /* Enable event line connected to device (or memcpy) */ |
| 1024 | if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || |
| 1025 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) { |
| 1026 | u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); |
| 1027 | |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 1028 | __d40_config_set_event(d40c, do_enable, event, |
| 1029 | D40_CHAN_REG_SSLNK); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1030 | } |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 1031 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1032 | if (d40c->dma_cfg.dir != STEDMA40_PERIPH_TO_MEM) { |
| 1033 | u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); |
| 1034 | |
Rabin Vincent | 262d291 | 2011-01-25 11:18:05 +0100 | [diff] [blame] | 1035 | __d40_config_set_event(d40c, do_enable, event, |
| 1036 | D40_CHAN_REG_SDLNK); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | spin_unlock_irqrestore(&d40c->phy_chan->lock, flags); |
| 1040 | } |
| 1041 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1042 | static u32 d40_chan_has_events(struct d40_chan *d40c) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1043 | { |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1044 | void __iomem *chanbase = chan_base(d40c); |
Jonas Aaberg | be8cb7d | 2010-08-09 12:07:44 +0000 | [diff] [blame] | 1045 | u32 val; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1046 | |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1047 | val = readl(chanbase + D40_CHAN_REG_SSLNK); |
| 1048 | val |= readl(chanbase + D40_CHAN_REG_SDLNK); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1049 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1050 | return val; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Rabin Vincent | 20a5b6d | 2010-10-12 13:00:52 +0000 | [diff] [blame] | 1053 | static u32 d40_get_prmo(struct d40_chan *d40c) |
| 1054 | { |
| 1055 | static const unsigned int phy_map[] = { |
| 1056 | [STEDMA40_PCHAN_BASIC_MODE] |
| 1057 | = D40_DREG_PRMO_PCHAN_BASIC, |
| 1058 | [STEDMA40_PCHAN_MODULO_MODE] |
| 1059 | = D40_DREG_PRMO_PCHAN_MODULO, |
| 1060 | [STEDMA40_PCHAN_DOUBLE_DST_MODE] |
| 1061 | = D40_DREG_PRMO_PCHAN_DOUBLE_DST, |
| 1062 | }; |
| 1063 | static const unsigned int log_map[] = { |
| 1064 | [STEDMA40_LCHAN_SRC_PHY_DST_LOG] |
| 1065 | = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG, |
| 1066 | [STEDMA40_LCHAN_SRC_LOG_DST_PHY] |
| 1067 | = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY, |
| 1068 | [STEDMA40_LCHAN_SRC_LOG_DST_LOG] |
| 1069 | = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG, |
| 1070 | }; |
| 1071 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1072 | if (chan_is_physical(d40c)) |
Rabin Vincent | 20a5b6d | 2010-10-12 13:00:52 +0000 | [diff] [blame] | 1073 | return phy_map[d40c->dma_cfg.mode_opt]; |
| 1074 | else |
| 1075 | return log_map[d40c->dma_cfg.mode_opt]; |
| 1076 | } |
| 1077 | |
Jonas Aaberg | b55912c | 2010-08-09 12:08:02 +0000 | [diff] [blame] | 1078 | static void d40_config_write(struct d40_chan *d40c) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1079 | { |
| 1080 | u32 addr_base; |
| 1081 | u32 var; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1082 | |
| 1083 | /* Odd addresses are even addresses + 4 */ |
| 1084 | addr_base = (d40c->phy_chan->num % 2) * 4; |
| 1085 | /* Setup channel mode to logical or physical */ |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1086 | var = ((u32)(chan_is_logical(d40c)) + 1) << |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1087 | D40_CHAN_POS(d40c->phy_chan->num); |
| 1088 | writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base); |
| 1089 | |
| 1090 | /* Setup operational mode option register */ |
Rabin Vincent | 20a5b6d | 2010-10-12 13:00:52 +0000 | [diff] [blame] | 1091 | var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1092 | |
| 1093 | writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base); |
| 1094 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1095 | if (chan_is_logical(d40c)) { |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1096 | int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS) |
| 1097 | & D40_SREG_ELEM_LOG_LIDX_MASK; |
| 1098 | void __iomem *chanbase = chan_base(d40c); |
| 1099 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1100 | /* Set default config for CFG reg */ |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1101 | writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG); |
| 1102 | writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1103 | |
Jonas Aaberg | b55912c | 2010-08-09 12:08:02 +0000 | [diff] [blame] | 1104 | /* Set LIDX for lcla */ |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1105 | writel(lidx, chanbase + D40_CHAN_REG_SSELT); |
| 1106 | writel(lidx, chanbase + D40_CHAN_REG_SDELT); |
Rabin Vincent | e9f3a49 | 2011-12-28 11:27:40 +0530 | [diff] [blame] | 1107 | |
| 1108 | /* Clear LNK which will be used by d40_chan_has_events() */ |
| 1109 | writel(0, chanbase + D40_CHAN_REG_SSLNK); |
| 1110 | writel(0, chanbase + D40_CHAN_REG_SDLNK); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1111 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1114 | static u32 d40_residue(struct d40_chan *d40c) |
| 1115 | { |
| 1116 | u32 num_elt; |
| 1117 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1118 | if (chan_is_logical(d40c)) |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1119 | num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK) |
| 1120 | >> D40_MEM_LCSP2_ECNT_POS; |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1121 | else { |
| 1122 | u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT); |
| 1123 | num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK) |
| 1124 | >> D40_SREG_ELEM_PHY_ECNT_POS; |
| 1125 | } |
| 1126 | |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1127 | return num_elt * (1 << d40c->dma_cfg.dst_info.data_width); |
| 1128 | } |
| 1129 | |
| 1130 | static bool d40_tx_is_linked(struct d40_chan *d40c) |
| 1131 | { |
| 1132 | bool is_link; |
| 1133 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1134 | if (chan_is_logical(d40c)) |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1135 | is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK; |
| 1136 | else |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1137 | is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK) |
| 1138 | & D40_SREG_LNK_PHYS_LNK_MASK; |
| 1139 | |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1140 | return is_link; |
| 1141 | } |
| 1142 | |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 1143 | static int d40_pause(struct d40_chan *d40c) |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1144 | { |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1145 | int res = 0; |
| 1146 | unsigned long flags; |
| 1147 | |
Jonas Aaberg | 3ac012a | 2010-08-09 12:09:12 +0000 | [diff] [blame] | 1148 | if (!d40c->busy) |
| 1149 | return 0; |
| 1150 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1151 | pm_runtime_get_sync(d40c->base->dev); |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1152 | spin_lock_irqsave(&d40c->lock, flags); |
| 1153 | |
| 1154 | res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); |
| 1155 | if (res == 0) { |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1156 | if (chan_is_logical(d40c)) { |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1157 | d40_config_set_event(d40c, false); |
| 1158 | /* Resume the other logical channels if any */ |
| 1159 | if (d40_chan_has_events(d40c)) |
| 1160 | res = d40_channel_execute_command(d40c, |
| 1161 | D40_DMA_RUN); |
| 1162 | } |
| 1163 | } |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1164 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 1165 | pm_runtime_put_autosuspend(d40c->base->dev); |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1166 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1167 | return res; |
| 1168 | } |
| 1169 | |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 1170 | static int d40_resume(struct d40_chan *d40c) |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1171 | { |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1172 | int res = 0; |
| 1173 | unsigned long flags; |
| 1174 | |
Jonas Aaberg | 3ac012a | 2010-08-09 12:09:12 +0000 | [diff] [blame] | 1175 | if (!d40c->busy) |
| 1176 | return 0; |
| 1177 | |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1178 | spin_lock_irqsave(&d40c->lock, flags); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1179 | pm_runtime_get_sync(d40c->base->dev); |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1180 | if (d40c->base->rev == 0) |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1181 | if (chan_is_logical(d40c)) { |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1182 | res = d40_channel_execute_command(d40c, |
| 1183 | D40_DMA_SUSPEND_REQ); |
| 1184 | goto no_suspend; |
| 1185 | } |
| 1186 | |
| 1187 | /* If bytes left to transfer or linked tx resume job */ |
| 1188 | if (d40_residue(d40c) || d40_tx_is_linked(d40c)) { |
| 1189 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1190 | if (chan_is_logical(d40c)) |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1191 | d40_config_set_event(d40c, true); |
| 1192 | |
| 1193 | res = d40_channel_execute_command(d40c, D40_DMA_RUN); |
| 1194 | } |
| 1195 | |
| 1196 | no_suspend: |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1197 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 1198 | pm_runtime_put_autosuspend(d40c->base->dev); |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1199 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1200 | return res; |
| 1201 | } |
| 1202 | |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 1203 | static int d40_terminate_all(struct d40_chan *chan) |
| 1204 | { |
| 1205 | unsigned long flags; |
| 1206 | int ret = 0; |
| 1207 | |
| 1208 | ret = d40_pause(chan); |
| 1209 | if (!ret && chan_is_physical(chan)) |
| 1210 | ret = d40_channel_execute_command(chan, D40_DMA_STOP); |
| 1211 | |
| 1212 | spin_lock_irqsave(&chan->lock, flags); |
| 1213 | d40_term_all(chan); |
| 1214 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1215 | |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1219 | static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx) |
| 1220 | { |
| 1221 | struct d40_chan *d40c = container_of(tx->chan, |
| 1222 | struct d40_chan, |
| 1223 | chan); |
| 1224 | struct d40_desc *d40d = container_of(tx, struct d40_desc, txd); |
| 1225 | unsigned long flags; |
| 1226 | |
| 1227 | spin_lock_irqsave(&d40c->lock, flags); |
| 1228 | |
Jonas Aaberg | aa182ae | 2010-08-09 12:08:26 +0000 | [diff] [blame] | 1229 | d40c->chan.cookie++; |
| 1230 | |
| 1231 | if (d40c->chan.cookie < 0) |
| 1232 | d40c->chan.cookie = 1; |
| 1233 | |
| 1234 | d40d->txd.cookie = d40c->chan.cookie; |
| 1235 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1236 | d40_desc_queue(d40c, d40d); |
| 1237 | |
| 1238 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1239 | |
| 1240 | return tx->cookie; |
| 1241 | } |
| 1242 | |
| 1243 | static int d40_start(struct d40_chan *d40c) |
| 1244 | { |
Linus Walleij | f418559 | 2010-06-22 18:06:42 -0700 | [diff] [blame] | 1245 | if (d40c->base->rev == 0) { |
| 1246 | int err; |
| 1247 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1248 | if (chan_is_logical(d40c)) { |
Linus Walleij | f418559 | 2010-06-22 18:06:42 -0700 | [diff] [blame] | 1249 | err = d40_channel_execute_command(d40c, |
| 1250 | D40_DMA_SUSPEND_REQ); |
| 1251 | if (err) |
| 1252 | return err; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1256 | if (chan_is_logical(d40c)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1257 | d40_config_set_event(d40c, true); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1258 | |
Jonas Aaberg | 0c32269 | 2010-06-20 21:25:46 +0000 | [diff] [blame] | 1259 | return d40_channel_execute_command(d40c, D40_DMA_RUN); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | static struct d40_desc *d40_queue_start(struct d40_chan *d40c) |
| 1263 | { |
| 1264 | struct d40_desc *d40d; |
| 1265 | int err; |
| 1266 | |
| 1267 | /* Start queued jobs, if any */ |
| 1268 | d40d = d40_first_queued(d40c); |
| 1269 | |
| 1270 | if (d40d != NULL) { |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1271 | if (!d40c->busy) |
| 1272 | d40c->busy = true; |
| 1273 | |
| 1274 | pm_runtime_get_sync(d40c->base->dev); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1275 | |
| 1276 | /* Remove from queue */ |
| 1277 | d40_desc_remove(d40d); |
| 1278 | |
| 1279 | /* Add to active queue */ |
| 1280 | d40_desc_submit(d40c, d40d); |
| 1281 | |
Rabin Vincent | 7d83a85 | 2011-01-25 11:18:06 +0100 | [diff] [blame] | 1282 | /* Initiate DMA job */ |
| 1283 | d40_desc_load(d40c, d40d); |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 1284 | |
Rabin Vincent | 7d83a85 | 2011-01-25 11:18:06 +0100 | [diff] [blame] | 1285 | /* Start dma job */ |
| 1286 | err = d40_start(d40c); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1287 | |
Rabin Vincent | 7d83a85 | 2011-01-25 11:18:06 +0100 | [diff] [blame] | 1288 | if (err) |
| 1289 | return NULL; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | return d40d; |
| 1293 | } |
| 1294 | |
| 1295 | /* called from interrupt context */ |
| 1296 | static void dma_tc_handle(struct d40_chan *d40c) |
| 1297 | { |
| 1298 | struct d40_desc *d40d; |
| 1299 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1300 | /* Get first active entry from list */ |
| 1301 | d40d = d40_first_active_get(d40c); |
| 1302 | |
| 1303 | if (d40d == NULL) |
| 1304 | return; |
| 1305 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1306 | if (d40d->cyclic) { |
| 1307 | /* |
| 1308 | * If this was a paritially loaded list, we need to reloaded |
| 1309 | * it, and only when the list is completed. We need to check |
| 1310 | * for done because the interrupt will hit for every link, and |
| 1311 | * not just the last one. |
| 1312 | */ |
| 1313 | if (d40d->lli_current < d40d->lli_len |
| 1314 | && !d40_tx_is_linked(d40c) |
| 1315 | && !d40_residue(d40c)) { |
| 1316 | d40_lcla_free_all(d40c, d40d); |
| 1317 | d40_desc_load(d40c, d40d); |
| 1318 | (void) d40_start(d40c); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1319 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1320 | if (d40d->lli_current == d40d->lli_len) |
| 1321 | d40d->lli_current = 0; |
| 1322 | } |
| 1323 | } else { |
| 1324 | d40_lcla_free_all(d40c, d40d); |
| 1325 | |
| 1326 | if (d40d->lli_current < d40d->lli_len) { |
| 1327 | d40_desc_load(d40c, d40d); |
| 1328 | /* Start dma job */ |
| 1329 | (void) d40_start(d40c); |
| 1330 | return; |
| 1331 | } |
| 1332 | |
| 1333 | if (d40_queue_start(d40c) == NULL) |
| 1334 | d40c->busy = false; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1335 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 1336 | pm_runtime_put_autosuspend(d40c->base->dev); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1339 | d40c->pending_tx++; |
| 1340 | tasklet_schedule(&d40c->tasklet); |
| 1341 | |
| 1342 | } |
| 1343 | |
| 1344 | static void dma_tasklet(unsigned long data) |
| 1345 | { |
| 1346 | struct d40_chan *d40c = (struct d40_chan *) data; |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1347 | struct d40_desc *d40d; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1348 | unsigned long flags; |
| 1349 | dma_async_tx_callback callback; |
| 1350 | void *callback_param; |
| 1351 | |
| 1352 | spin_lock_irqsave(&d40c->lock, flags); |
| 1353 | |
| 1354 | /* Get first active entry from list */ |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1355 | d40d = d40_first_active_get(d40c); |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1356 | if (d40d == NULL) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1357 | goto err; |
| 1358 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1359 | if (!d40d->cyclic) |
| 1360 | d40c->completed = d40d->txd.cookie; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1361 | |
| 1362 | /* |
| 1363 | * If terminating a channel pending_tx is set to zero. |
| 1364 | * This prevents any finished active jobs to return to the client. |
| 1365 | */ |
| 1366 | if (d40c->pending_tx == 0) { |
| 1367 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | /* Callback to client */ |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1372 | callback = d40d->txd.callback; |
| 1373 | callback_param = d40d->txd.callback_param; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1374 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1375 | if (!d40d->cyclic) { |
| 1376 | if (async_tx_test_ack(&d40d->txd)) { |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1377 | d40_desc_remove(d40d); |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1378 | d40_desc_free(d40c, d40d); |
| 1379 | } else { |
| 1380 | if (!d40d->is_in_client_list) { |
| 1381 | d40_desc_remove(d40d); |
| 1382 | d40_lcla_free_all(d40c, d40d); |
| 1383 | list_add_tail(&d40d->node, &d40c->client); |
| 1384 | d40d->is_in_client_list = true; |
| 1385 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | d40c->pending_tx--; |
| 1390 | |
| 1391 | if (d40c->pending_tx) |
| 1392 | tasklet_schedule(&d40c->tasklet); |
| 1393 | |
| 1394 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1395 | |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 1396 | if (callback && (d40d->txd.flags & DMA_PREP_INTERRUPT)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1397 | callback(callback_param); |
| 1398 | |
| 1399 | return; |
| 1400 | |
| 1401 | err: |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1402 | /* Rescue manoeuvre if receiving double interrupts */ |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1403 | if (d40c->pending_tx > 0) |
| 1404 | d40c->pending_tx--; |
| 1405 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1406 | } |
| 1407 | |
| 1408 | static irqreturn_t d40_handle_interrupt(int irq, void *data) |
| 1409 | { |
| 1410 | static const struct d40_interrupt_lookup il[] = { |
| 1411 | {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0}, |
| 1412 | {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32}, |
| 1413 | {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64}, |
| 1414 | {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96}, |
| 1415 | {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0}, |
| 1416 | {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32}, |
| 1417 | {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64}, |
| 1418 | {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96}, |
| 1419 | {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN}, |
| 1420 | {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN}, |
| 1421 | }; |
| 1422 | |
| 1423 | int i; |
| 1424 | u32 regs[ARRAY_SIZE(il)]; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1425 | u32 idx; |
| 1426 | u32 row; |
| 1427 | long chan = -1; |
| 1428 | struct d40_chan *d40c; |
| 1429 | unsigned long flags; |
| 1430 | struct d40_base *base = data; |
| 1431 | |
| 1432 | spin_lock_irqsave(&base->interrupt_lock, flags); |
| 1433 | |
| 1434 | /* Read interrupt status of both logical and physical channels */ |
| 1435 | for (i = 0; i < ARRAY_SIZE(il); i++) |
| 1436 | regs[i] = readl(base->virtbase + il[i].src); |
| 1437 | |
| 1438 | for (;;) { |
| 1439 | |
| 1440 | chan = find_next_bit((unsigned long *)regs, |
| 1441 | BITS_PER_LONG * ARRAY_SIZE(il), chan + 1); |
| 1442 | |
| 1443 | /* No more set bits found? */ |
| 1444 | if (chan == BITS_PER_LONG * ARRAY_SIZE(il)) |
| 1445 | break; |
| 1446 | |
| 1447 | row = chan / BITS_PER_LONG; |
| 1448 | idx = chan & (BITS_PER_LONG - 1); |
| 1449 | |
| 1450 | /* ACK interrupt */ |
Jonas Aaberg | 1b00348 | 2010-08-09 12:07:54 +0000 | [diff] [blame] | 1451 | writel(1 << idx, base->virtbase + il[row].clr); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1452 | |
| 1453 | if (il[row].offset == D40_PHY_CHAN) |
| 1454 | d40c = base->lookup_phy_chans[idx]; |
| 1455 | else |
| 1456 | d40c = base->lookup_log_chans[il[row].offset + idx]; |
| 1457 | spin_lock(&d40c->lock); |
| 1458 | |
| 1459 | if (!il[row].is_error) |
| 1460 | dma_tc_handle(d40c); |
| 1461 | else |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1462 | d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n", |
| 1463 | chan, il[row].offset, idx); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1464 | |
| 1465 | spin_unlock(&d40c->lock); |
| 1466 | } |
| 1467 | |
| 1468 | spin_unlock_irqrestore(&base->interrupt_lock, flags); |
| 1469 | |
| 1470 | return IRQ_HANDLED; |
| 1471 | } |
| 1472 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1473 | static int d40_validate_conf(struct d40_chan *d40c, |
| 1474 | struct stedma40_chan_cfg *conf) |
| 1475 | { |
| 1476 | int res = 0; |
| 1477 | u32 dst_event_group = D40_TYPE_TO_GROUP(conf->dst_dev_type); |
| 1478 | u32 src_event_group = D40_TYPE_TO_GROUP(conf->src_dev_type); |
Rabin Vincent | 38bdbf0 | 2010-10-12 13:00:51 +0000 | [diff] [blame] | 1479 | bool is_log = conf->mode == STEDMA40_MODE_LOGICAL; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1480 | |
Linus Walleij | 0747c7ba | 2010-08-09 12:07:36 +0000 | [diff] [blame] | 1481 | if (!conf->dir) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1482 | chan_err(d40c, "Invalid direction.\n"); |
Linus Walleij | 0747c7ba | 2010-08-09 12:07:36 +0000 | [diff] [blame] | 1483 | res = -EINVAL; |
| 1484 | } |
| 1485 | |
| 1486 | if (conf->dst_dev_type != STEDMA40_DEV_DST_MEMORY && |
| 1487 | d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 && |
| 1488 | d40c->runtime_addr == 0) { |
| 1489 | |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1490 | chan_err(d40c, "Invalid TX channel address (%d)\n", |
| 1491 | conf->dst_dev_type); |
Linus Walleij | 0747c7ba | 2010-08-09 12:07:36 +0000 | [diff] [blame] | 1492 | res = -EINVAL; |
| 1493 | } |
| 1494 | |
| 1495 | if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY && |
| 1496 | d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 && |
| 1497 | d40c->runtime_addr == 0) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1498 | chan_err(d40c, "Invalid RX channel address (%d)\n", |
| 1499 | conf->src_dev_type); |
Linus Walleij | 0747c7ba | 2010-08-09 12:07:36 +0000 | [diff] [blame] | 1500 | res = -EINVAL; |
| 1501 | } |
| 1502 | |
| 1503 | if (conf->dir == STEDMA40_MEM_TO_PERIPH && |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1504 | dst_event_group == STEDMA40_DEV_DST_MEMORY) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1505 | chan_err(d40c, "Invalid dst\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1506 | res = -EINVAL; |
| 1507 | } |
| 1508 | |
Linus Walleij | 0747c7ba | 2010-08-09 12:07:36 +0000 | [diff] [blame] | 1509 | if (conf->dir == STEDMA40_PERIPH_TO_MEM && |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1510 | src_event_group == STEDMA40_DEV_SRC_MEMORY) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1511 | chan_err(d40c, "Invalid src\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1512 | res = -EINVAL; |
| 1513 | } |
| 1514 | |
| 1515 | if (src_event_group == STEDMA40_DEV_SRC_MEMORY && |
| 1516 | dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1517 | chan_err(d40c, "No event line\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1518 | res = -EINVAL; |
| 1519 | } |
| 1520 | |
| 1521 | if (conf->dir == STEDMA40_PERIPH_TO_PERIPH && |
| 1522 | (src_event_group != dst_event_group)) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1523 | chan_err(d40c, "Invalid event group\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1524 | res = -EINVAL; |
| 1525 | } |
| 1526 | |
| 1527 | if (conf->dir == STEDMA40_PERIPH_TO_PERIPH) { |
| 1528 | /* |
| 1529 | * DMAC HW supports it. Will be added to this driver, |
| 1530 | * in case any dma client requires it. |
| 1531 | */ |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1532 | chan_err(d40c, "periph to periph not supported\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1533 | res = -EINVAL; |
| 1534 | } |
| 1535 | |
Per Forlin | d49278e | 2010-12-20 18:31:38 +0100 | [diff] [blame] | 1536 | if (d40_psize_2_burst_size(is_log, conf->src_info.psize) * |
| 1537 | (1 << conf->src_info.data_width) != |
| 1538 | d40_psize_2_burst_size(is_log, conf->dst_info.psize) * |
| 1539 | (1 << conf->dst_info.data_width)) { |
| 1540 | /* |
| 1541 | * The DMAC hardware only supports |
| 1542 | * src (burst x width) == dst (burst x width) |
| 1543 | */ |
| 1544 | |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1545 | chan_err(d40c, "src (burst x width) != dst (burst x width)\n"); |
Per Forlin | d49278e | 2010-12-20 18:31:38 +0100 | [diff] [blame] | 1546 | res = -EINVAL; |
| 1547 | } |
| 1548 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1549 | return res; |
| 1550 | } |
| 1551 | |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1552 | static bool d40_alloc_mask_set(struct d40_phy_res *phy, |
| 1553 | bool is_src, int log_event_line, bool is_log, |
| 1554 | bool *first_user) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1555 | { |
| 1556 | unsigned long flags; |
| 1557 | spin_lock_irqsave(&phy->lock, flags); |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1558 | |
| 1559 | *first_user = ((phy->allocated_src | phy->allocated_dst) |
| 1560 | == D40_ALLOC_FREE); |
| 1561 | |
Marcin Mielczarczyk | 4aed79b | 2010-05-18 00:41:21 +0200 | [diff] [blame] | 1562 | if (!is_log) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1563 | /* Physical interrupts are masked per physical full channel */ |
| 1564 | if (phy->allocated_src == D40_ALLOC_FREE && |
| 1565 | phy->allocated_dst == D40_ALLOC_FREE) { |
| 1566 | phy->allocated_dst = D40_ALLOC_PHY; |
| 1567 | phy->allocated_src = D40_ALLOC_PHY; |
| 1568 | goto found; |
| 1569 | } else |
| 1570 | goto not_found; |
| 1571 | } |
| 1572 | |
| 1573 | /* Logical channel */ |
| 1574 | if (is_src) { |
| 1575 | if (phy->allocated_src == D40_ALLOC_PHY) |
| 1576 | goto not_found; |
| 1577 | |
| 1578 | if (phy->allocated_src == D40_ALLOC_FREE) |
| 1579 | phy->allocated_src = D40_ALLOC_LOG_FREE; |
| 1580 | |
| 1581 | if (!(phy->allocated_src & (1 << log_event_line))) { |
| 1582 | phy->allocated_src |= 1 << log_event_line; |
| 1583 | goto found; |
| 1584 | } else |
| 1585 | goto not_found; |
| 1586 | } else { |
| 1587 | if (phy->allocated_dst == D40_ALLOC_PHY) |
| 1588 | goto not_found; |
| 1589 | |
| 1590 | if (phy->allocated_dst == D40_ALLOC_FREE) |
| 1591 | phy->allocated_dst = D40_ALLOC_LOG_FREE; |
| 1592 | |
| 1593 | if (!(phy->allocated_dst & (1 << log_event_line))) { |
| 1594 | phy->allocated_dst |= 1 << log_event_line; |
| 1595 | goto found; |
| 1596 | } else |
| 1597 | goto not_found; |
| 1598 | } |
| 1599 | |
| 1600 | not_found: |
| 1601 | spin_unlock_irqrestore(&phy->lock, flags); |
| 1602 | return false; |
| 1603 | found: |
| 1604 | spin_unlock_irqrestore(&phy->lock, flags); |
| 1605 | return true; |
| 1606 | } |
| 1607 | |
| 1608 | static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src, |
| 1609 | int log_event_line) |
| 1610 | { |
| 1611 | unsigned long flags; |
| 1612 | bool is_free = false; |
| 1613 | |
| 1614 | spin_lock_irqsave(&phy->lock, flags); |
| 1615 | if (!log_event_line) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1616 | phy->allocated_dst = D40_ALLOC_FREE; |
| 1617 | phy->allocated_src = D40_ALLOC_FREE; |
| 1618 | is_free = true; |
| 1619 | goto out; |
| 1620 | } |
| 1621 | |
| 1622 | /* Logical channel */ |
| 1623 | if (is_src) { |
| 1624 | phy->allocated_src &= ~(1 << log_event_line); |
| 1625 | if (phy->allocated_src == D40_ALLOC_LOG_FREE) |
| 1626 | phy->allocated_src = D40_ALLOC_FREE; |
| 1627 | } else { |
| 1628 | phy->allocated_dst &= ~(1 << log_event_line); |
| 1629 | if (phy->allocated_dst == D40_ALLOC_LOG_FREE) |
| 1630 | phy->allocated_dst = D40_ALLOC_FREE; |
| 1631 | } |
| 1632 | |
| 1633 | is_free = ((phy->allocated_src | phy->allocated_dst) == |
| 1634 | D40_ALLOC_FREE); |
| 1635 | |
| 1636 | out: |
| 1637 | spin_unlock_irqrestore(&phy->lock, flags); |
| 1638 | |
| 1639 | return is_free; |
| 1640 | } |
| 1641 | |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1642 | static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1643 | { |
| 1644 | int dev_type; |
| 1645 | int event_group; |
| 1646 | int event_line; |
| 1647 | struct d40_phy_res *phys; |
| 1648 | int i; |
| 1649 | int j; |
| 1650 | int log_num; |
| 1651 | bool is_src; |
Rabin Vincent | 38bdbf0 | 2010-10-12 13:00:51 +0000 | [diff] [blame] | 1652 | bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1653 | |
| 1654 | phys = d40c->base->phy_res; |
| 1655 | |
| 1656 | if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { |
| 1657 | dev_type = d40c->dma_cfg.src_dev_type; |
| 1658 | log_num = 2 * dev_type; |
| 1659 | is_src = true; |
| 1660 | } else if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || |
| 1661 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { |
| 1662 | /* dst event lines are used for logical memcpy */ |
| 1663 | dev_type = d40c->dma_cfg.dst_dev_type; |
| 1664 | log_num = 2 * dev_type + 1; |
| 1665 | is_src = false; |
| 1666 | } else |
| 1667 | return -EINVAL; |
| 1668 | |
| 1669 | event_group = D40_TYPE_TO_GROUP(dev_type); |
| 1670 | event_line = D40_TYPE_TO_EVENT(dev_type); |
| 1671 | |
| 1672 | if (!is_log) { |
| 1673 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { |
| 1674 | /* Find physical half channel */ |
| 1675 | for (i = 0; i < d40c->base->num_phy_chans; i++) { |
| 1676 | |
Marcin Mielczarczyk | 4aed79b | 2010-05-18 00:41:21 +0200 | [diff] [blame] | 1677 | if (d40_alloc_mask_set(&phys[i], is_src, |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1678 | 0, is_log, |
| 1679 | first_phy_user)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1680 | goto found_phy; |
| 1681 | } |
| 1682 | } else |
| 1683 | for (j = 0; j < d40c->base->num_phy_chans; j += 8) { |
| 1684 | int phy_num = j + event_group * 2; |
| 1685 | for (i = phy_num; i < phy_num + 2; i++) { |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 1686 | if (d40_alloc_mask_set(&phys[i], |
| 1687 | is_src, |
| 1688 | 0, |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1689 | is_log, |
| 1690 | first_phy_user)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1691 | goto found_phy; |
| 1692 | } |
| 1693 | } |
| 1694 | return -EINVAL; |
| 1695 | found_phy: |
| 1696 | d40c->phy_chan = &phys[i]; |
| 1697 | d40c->log_num = D40_PHY_CHAN; |
| 1698 | goto out; |
| 1699 | } |
| 1700 | if (dev_type == -1) |
| 1701 | return -EINVAL; |
| 1702 | |
| 1703 | /* Find logical channel */ |
| 1704 | for (j = 0; j < d40c->base->num_phy_chans; j += 8) { |
| 1705 | int phy_num = j + event_group * 2; |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1706 | |
| 1707 | if (d40c->dma_cfg.use_fixed_channel) { |
| 1708 | i = d40c->dma_cfg.phy_channel; |
| 1709 | |
| 1710 | if ((i != phy_num) && (i != phy_num + 1)) { |
| 1711 | dev_err(chan2dev(d40c), |
| 1712 | "invalid fixed phy channel %d\n", i); |
| 1713 | return -EINVAL; |
| 1714 | } |
| 1715 | |
| 1716 | if (d40_alloc_mask_set(&phys[i], is_src, event_line, |
| 1717 | is_log, first_phy_user)) |
| 1718 | goto found_log; |
| 1719 | |
| 1720 | dev_err(chan2dev(d40c), |
| 1721 | "could not allocate fixed phy channel %d\n", i); |
| 1722 | return -EINVAL; |
| 1723 | } |
| 1724 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1725 | /* |
| 1726 | * Spread logical channels across all available physical rather |
| 1727 | * than pack every logical channel at the first available phy |
| 1728 | * channels. |
| 1729 | */ |
| 1730 | if (is_src) { |
| 1731 | for (i = phy_num; i < phy_num + 2; i++) { |
| 1732 | if (d40_alloc_mask_set(&phys[i], is_src, |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1733 | event_line, is_log, |
| 1734 | first_phy_user)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1735 | goto found_log; |
| 1736 | } |
| 1737 | } else { |
| 1738 | for (i = phy_num + 1; i >= phy_num; i--) { |
| 1739 | if (d40_alloc_mask_set(&phys[i], is_src, |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 1740 | event_line, is_log, |
| 1741 | first_phy_user)) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1742 | goto found_log; |
| 1743 | } |
| 1744 | } |
| 1745 | } |
| 1746 | return -EINVAL; |
| 1747 | |
| 1748 | found_log: |
| 1749 | d40c->phy_chan = &phys[i]; |
| 1750 | d40c->log_num = log_num; |
| 1751 | out: |
| 1752 | |
| 1753 | if (is_log) |
| 1754 | d40c->base->lookup_log_chans[d40c->log_num] = d40c; |
| 1755 | else |
| 1756 | d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c; |
| 1757 | |
| 1758 | return 0; |
| 1759 | |
| 1760 | } |
| 1761 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1762 | static int d40_config_memcpy(struct d40_chan *d40c) |
| 1763 | { |
| 1764 | dma_cap_mask_t cap = d40c->chan.device->cap_mask; |
| 1765 | |
| 1766 | if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) { |
| 1767 | d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_log; |
| 1768 | d40c->dma_cfg.src_dev_type = STEDMA40_DEV_SRC_MEMORY; |
| 1769 | d40c->dma_cfg.dst_dev_type = d40c->base->plat_data-> |
| 1770 | memcpy[d40c->chan.chan_id]; |
| 1771 | |
| 1772 | } else if (dma_has_cap(DMA_MEMCPY, cap) && |
| 1773 | dma_has_cap(DMA_SLAVE, cap)) { |
| 1774 | d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy; |
| 1775 | } else { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1776 | chan_err(d40c, "No memcpy\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1777 | return -EINVAL; |
| 1778 | } |
| 1779 | |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
| 1783 | |
| 1784 | static int d40_free_dma(struct d40_chan *d40c) |
| 1785 | { |
| 1786 | |
| 1787 | int res = 0; |
Jonas Aaberg | d181b3a | 2010-06-20 21:26:38 +0000 | [diff] [blame] | 1788 | u32 event; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1789 | struct d40_phy_res *phy = d40c->phy_chan; |
| 1790 | bool is_src; |
| 1791 | |
| 1792 | /* Terminate all queued and active transfers */ |
| 1793 | d40_term_all(d40c); |
| 1794 | |
| 1795 | if (phy == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1796 | chan_err(d40c, "phy == null\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1797 | return -EINVAL; |
| 1798 | } |
| 1799 | |
| 1800 | if (phy->allocated_src == D40_ALLOC_FREE && |
| 1801 | phy->allocated_dst == D40_ALLOC_FREE) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1802 | chan_err(d40c, "channel already free\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1803 | return -EINVAL; |
| 1804 | } |
| 1805 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1806 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || |
| 1807 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { |
| 1808 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1809 | is_src = false; |
| 1810 | } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { |
| 1811 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1812 | is_src = true; |
| 1813 | } else { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1814 | chan_err(d40c, "Unknown direction\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1815 | return -EINVAL; |
| 1816 | } |
| 1817 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1818 | pm_runtime_get_sync(d40c->base->dev); |
Jonas Aaberg | d181b3a | 2010-06-20 21:26:38 +0000 | [diff] [blame] | 1819 | res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ); |
| 1820 | if (res) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1821 | chan_err(d40c, "suspend failed\n"); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1822 | goto out; |
Jonas Aaberg | d181b3a | 2010-06-20 21:26:38 +0000 | [diff] [blame] | 1823 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1824 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1825 | if (chan_is_logical(d40c)) { |
Jonas Aaberg | d181b3a | 2010-06-20 21:26:38 +0000 | [diff] [blame] | 1826 | /* Release logical channel, deactivate the event line */ |
| 1827 | |
| 1828 | d40_config_set_event(d40c, false); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1829 | d40c->base->lookup_log_chans[d40c->log_num] = NULL; |
| 1830 | |
| 1831 | /* |
| 1832 | * Check if there are more logical allocation |
| 1833 | * on this phy channel. |
| 1834 | */ |
| 1835 | if (!d40_alloc_mask_free(phy, is_src, event)) { |
| 1836 | /* Resume the other logical channels if any */ |
| 1837 | if (d40_chan_has_events(d40c)) { |
| 1838 | res = d40_channel_execute_command(d40c, |
| 1839 | D40_DMA_RUN); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1840 | if (res) |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1841 | chan_err(d40c, |
| 1842 | "Executing RUN command\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1843 | } |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1844 | goto out; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1845 | } |
Jonas Aaberg | d181b3a | 2010-06-20 21:26:38 +0000 | [diff] [blame] | 1846 | } else { |
| 1847 | (void) d40_alloc_mask_free(phy, is_src, 0); |
| 1848 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1849 | |
| 1850 | /* Release physical channel */ |
| 1851 | res = d40_channel_execute_command(d40c, D40_DMA_STOP); |
| 1852 | if (res) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1853 | chan_err(d40c, "Failed to stop channel\n"); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1854 | goto out; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1855 | } |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1856 | |
| 1857 | if (d40c->busy) { |
| 1858 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 1859 | pm_runtime_put_autosuspend(d40c->base->dev); |
| 1860 | } |
| 1861 | |
| 1862 | d40c->busy = false; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1863 | d40c->phy_chan = NULL; |
Rabin Vincent | ce2ca12 | 2010-10-12 13:00:49 +0000 | [diff] [blame] | 1864 | d40c->configured = false; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1865 | d40c->base->lookup_phy_chans[phy->num] = NULL; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1866 | out: |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1867 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 1868 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 1869 | pm_runtime_put_autosuspend(d40c->base->dev); |
| 1870 | return res; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1873 | static bool d40_is_paused(struct d40_chan *d40c) |
| 1874 | { |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1875 | void __iomem *chanbase = chan_base(d40c); |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1876 | bool is_paused = false; |
| 1877 | unsigned long flags; |
| 1878 | void __iomem *active_reg; |
| 1879 | u32 status; |
| 1880 | u32 event; |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1881 | |
| 1882 | spin_lock_irqsave(&d40c->lock, flags); |
| 1883 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 1884 | if (chan_is_physical(d40c)) { |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1885 | if (d40c->phy_chan->num % 2 == 0) |
| 1886 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVE; |
| 1887 | else |
| 1888 | active_reg = d40c->base->virtbase + D40_DREG_ACTIVO; |
| 1889 | |
| 1890 | status = (readl(active_reg) & |
| 1891 | D40_CHAN_POS_MASK(d40c->phy_chan->num)) >> |
| 1892 | D40_CHAN_POS(d40c->phy_chan->num); |
| 1893 | if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP) |
| 1894 | is_paused = true; |
| 1895 | |
| 1896 | goto _exit; |
| 1897 | } |
| 1898 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1899 | if (d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH || |
Jonas Aaberg | 9dbfbd35c | 2010-08-09 12:08:41 +0000 | [diff] [blame] | 1900 | d40c->dma_cfg.dir == STEDMA40_MEM_TO_MEM) { |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1901 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dst_dev_type); |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1902 | status = readl(chanbase + D40_CHAN_REG_SDLNK); |
Jonas Aaberg | 9dbfbd35c | 2010-08-09 12:08:41 +0000 | [diff] [blame] | 1903 | } else if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) { |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1904 | event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type); |
Rabin Vincent | 8ca8468 | 2011-01-25 11:18:07 +0100 | [diff] [blame] | 1905 | status = readl(chanbase + D40_CHAN_REG_SSLNK); |
Jonas Aaberg | 9dbfbd35c | 2010-08-09 12:08:41 +0000 | [diff] [blame] | 1906 | } else { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 1907 | chan_err(d40c, "Unknown direction\n"); |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1908 | goto _exit; |
| 1909 | } |
Jonas Aaberg | 9dbfbd35c | 2010-08-09 12:08:41 +0000 | [diff] [blame] | 1910 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1911 | status = (status & D40_EVENTLINE_MASK(event)) >> |
| 1912 | D40_EVENTLINE_POS(event); |
| 1913 | |
| 1914 | if (status != D40_DMA_RUN) |
| 1915 | is_paused = true; |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 1916 | _exit: |
| 1917 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1918 | return is_paused; |
| 1919 | |
| 1920 | } |
| 1921 | |
| 1922 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 1923 | static u32 stedma40_residue(struct dma_chan *chan) |
| 1924 | { |
| 1925 | struct d40_chan *d40c = |
| 1926 | container_of(chan, struct d40_chan, chan); |
| 1927 | u32 bytes_left; |
| 1928 | unsigned long flags; |
| 1929 | |
| 1930 | spin_lock_irqsave(&d40c->lock, flags); |
| 1931 | bytes_left = d40_residue(d40c); |
| 1932 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 1933 | |
| 1934 | return bytes_left; |
| 1935 | } |
| 1936 | |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1937 | static int |
| 1938 | d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc, |
| 1939 | struct scatterlist *sg_src, struct scatterlist *sg_dst, |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 1940 | unsigned int sg_len, dma_addr_t src_dev_addr, |
| 1941 | dma_addr_t dst_dev_addr) |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1942 | { |
| 1943 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; |
| 1944 | struct stedma40_half_channel_info *src_info = &cfg->src_info; |
| 1945 | struct stedma40_half_channel_info *dst_info = &cfg->dst_info; |
Rabin Vincent | 5ed04b8 | 2011-01-25 11:18:26 +0100 | [diff] [blame] | 1946 | int ret; |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1947 | |
Rabin Vincent | 5ed04b8 | 2011-01-25 11:18:26 +0100 | [diff] [blame] | 1948 | ret = d40_log_sg_to_lli(sg_src, sg_len, |
| 1949 | src_dev_addr, |
| 1950 | desc->lli_log.src, |
| 1951 | chan->log_def.lcsp1, |
| 1952 | src_info->data_width, |
| 1953 | dst_info->data_width); |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1954 | |
Rabin Vincent | 5ed04b8 | 2011-01-25 11:18:26 +0100 | [diff] [blame] | 1955 | ret = d40_log_sg_to_lli(sg_dst, sg_len, |
| 1956 | dst_dev_addr, |
| 1957 | desc->lli_log.dst, |
| 1958 | chan->log_def.lcsp3, |
| 1959 | dst_info->data_width, |
| 1960 | src_info->data_width); |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1961 | |
Rabin Vincent | 5ed04b8 | 2011-01-25 11:18:26 +0100 | [diff] [blame] | 1962 | return ret < 0 ? ret : 0; |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | static int |
| 1966 | d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc, |
| 1967 | struct scatterlist *sg_src, struct scatterlist *sg_dst, |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 1968 | unsigned int sg_len, dma_addr_t src_dev_addr, |
| 1969 | dma_addr_t dst_dev_addr) |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1970 | { |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1971 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; |
| 1972 | struct stedma40_half_channel_info *src_info = &cfg->src_info; |
| 1973 | struct stedma40_half_channel_info *dst_info = &cfg->dst_info; |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1974 | unsigned long flags = 0; |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1975 | int ret; |
| 1976 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1977 | if (desc->cyclic) |
| 1978 | flags |= LLI_CYCLIC | LLI_TERM_INT; |
| 1979 | |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1980 | ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr, |
| 1981 | desc->lli_phy.src, |
| 1982 | virt_to_phys(desc->lli_phy.src), |
| 1983 | chan->src_def_cfg, |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1984 | src_info, dst_info, flags); |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1985 | |
| 1986 | ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr, |
| 1987 | desc->lli_phy.dst, |
| 1988 | virt_to_phys(desc->lli_phy.dst), |
| 1989 | chan->dst_def_cfg, |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 1990 | dst_info, src_info, flags); |
Rabin Vincent | 3e3a076 | 2011-01-25 11:18:21 +0100 | [diff] [blame] | 1991 | |
| 1992 | dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr, |
| 1993 | desc->lli_pool.size, DMA_TO_DEVICE); |
| 1994 | |
| 1995 | return ret < 0 ? ret : 0; |
| 1996 | } |
| 1997 | |
| 1998 | |
Rabin Vincent | 5f81158 | 2011-01-25 11:18:18 +0100 | [diff] [blame] | 1999 | static struct d40_desc * |
| 2000 | d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg, |
| 2001 | unsigned int sg_len, unsigned long dma_flags) |
| 2002 | { |
| 2003 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; |
| 2004 | struct d40_desc *desc; |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 2005 | int ret; |
Rabin Vincent | 5f81158 | 2011-01-25 11:18:18 +0100 | [diff] [blame] | 2006 | |
| 2007 | desc = d40_desc_get(chan); |
| 2008 | if (!desc) |
| 2009 | return NULL; |
| 2010 | |
| 2011 | desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width, |
| 2012 | cfg->dst_info.data_width); |
| 2013 | if (desc->lli_len < 0) { |
| 2014 | chan_err(chan, "Unaligned size\n"); |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 2015 | goto err; |
Rabin Vincent | 5f81158 | 2011-01-25 11:18:18 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 2018 | ret = d40_pool_lli_alloc(chan, desc, desc->lli_len); |
| 2019 | if (ret < 0) { |
| 2020 | chan_err(chan, "Could not allocate lli\n"); |
| 2021 | goto err; |
| 2022 | } |
| 2023 | |
| 2024 | |
Rabin Vincent | 5f81158 | 2011-01-25 11:18:18 +0100 | [diff] [blame] | 2025 | desc->lli_current = 0; |
| 2026 | desc->txd.flags = dma_flags; |
| 2027 | desc->txd.tx_submit = d40_tx_submit; |
| 2028 | |
| 2029 | dma_async_tx_descriptor_init(&desc->txd, &chan->chan); |
| 2030 | |
| 2031 | return desc; |
Rabin Vincent | dbd8878 | 2011-01-25 11:18:19 +0100 | [diff] [blame] | 2032 | |
| 2033 | err: |
| 2034 | d40_desc_free(chan, desc); |
| 2035 | return NULL; |
Rabin Vincent | 5f81158 | 2011-01-25 11:18:18 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2038 | static dma_addr_t |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2039 | d40_get_dev_addr(struct d40_chan *chan, enum dma_transfer_direction direction) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2040 | { |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2041 | struct stedma40_platform_data *plat = chan->base->plat_data; |
| 2042 | struct stedma40_chan_cfg *cfg = &chan->dma_cfg; |
Philippe Langlais | 711b9ce | 2011-05-07 17:09:43 +0200 | [diff] [blame] | 2043 | dma_addr_t addr = 0; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2044 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2045 | if (chan->runtime_addr) |
| 2046 | return chan->runtime_addr; |
| 2047 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2048 | if (direction == DMA_DEV_TO_MEM) |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2049 | addr = plat->dev_rx[cfg->src_dev_type]; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2050 | else if (direction == DMA_MEM_TO_DEV) |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2051 | addr = plat->dev_tx[cfg->dst_dev_type]; |
| 2052 | |
| 2053 | return addr; |
| 2054 | } |
| 2055 | |
| 2056 | static struct dma_async_tx_descriptor * |
| 2057 | d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src, |
| 2058 | struct scatterlist *sg_dst, unsigned int sg_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2059 | enum dma_transfer_direction direction, unsigned long dma_flags) |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2060 | { |
| 2061 | struct d40_chan *chan = container_of(dchan, struct d40_chan, chan); |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2062 | dma_addr_t src_dev_addr = 0; |
| 2063 | dma_addr_t dst_dev_addr = 0; |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2064 | struct d40_desc *desc; |
| 2065 | unsigned long flags; |
| 2066 | int ret; |
| 2067 | |
| 2068 | if (!chan->phy_chan) { |
| 2069 | chan_err(chan, "Cannot prepare unallocated channel\n"); |
| 2070 | return NULL; |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2073 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2074 | spin_lock_irqsave(&chan->lock, flags); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2075 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2076 | desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags); |
| 2077 | if (desc == NULL) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2078 | goto err; |
| 2079 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2080 | if (sg_next(&sg_src[sg_len - 1]) == sg_src) |
| 2081 | desc->cyclic = true; |
| 2082 | |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2083 | if (direction != DMA_NONE) { |
| 2084 | dma_addr_t dev_addr = d40_get_dev_addr(chan, direction); |
| 2085 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2086 | if (direction == DMA_DEV_TO_MEM) |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2087 | src_dev_addr = dev_addr; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2088 | else if (direction == DMA_MEM_TO_DEV) |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2089 | dst_dev_addr = dev_addr; |
| 2090 | } |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2091 | |
| 2092 | if (chan_is_logical(chan)) |
| 2093 | ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst, |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2094 | sg_len, src_dev_addr, dst_dev_addr); |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2095 | else |
| 2096 | ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst, |
Rabin Vincent | 822c567 | 2011-01-25 11:18:28 +0100 | [diff] [blame] | 2097 | sg_len, src_dev_addr, dst_dev_addr); |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2098 | |
| 2099 | if (ret) { |
| 2100 | chan_err(chan, "Failed to prepare %s sg job: %d\n", |
| 2101 | chan_is_logical(chan) ? "log" : "phy", ret); |
| 2102 | goto err; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2103 | } |
| 2104 | |
Per Forlin | 82babbb36 | 2011-08-29 13:33:35 +0200 | [diff] [blame] | 2105 | /* |
| 2106 | * add descriptor to the prepare queue in order to be able |
| 2107 | * to free them later in terminate_all |
| 2108 | */ |
| 2109 | list_add_tail(&desc->node, &chan->prepare_queue); |
| 2110 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2111 | spin_unlock_irqrestore(&chan->lock, flags); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2112 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2113 | return &desc->txd; |
| 2114 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2115 | err: |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2116 | if (desc) |
| 2117 | d40_desc_free(chan, desc); |
| 2118 | spin_unlock_irqrestore(&chan->lock, flags); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2119 | return NULL; |
| 2120 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2121 | |
| 2122 | bool stedma40_filter(struct dma_chan *chan, void *data) |
| 2123 | { |
| 2124 | struct stedma40_chan_cfg *info = data; |
| 2125 | struct d40_chan *d40c = |
| 2126 | container_of(chan, struct d40_chan, chan); |
| 2127 | int err; |
| 2128 | |
| 2129 | if (data) { |
| 2130 | err = d40_validate_conf(d40c, info); |
| 2131 | if (!err) |
| 2132 | d40c->dma_cfg = *info; |
| 2133 | } else |
| 2134 | err = d40_config_memcpy(d40c); |
| 2135 | |
Rabin Vincent | ce2ca12 | 2010-10-12 13:00:49 +0000 | [diff] [blame] | 2136 | if (!err) |
| 2137 | d40c->configured = true; |
| 2138 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2139 | return err == 0; |
| 2140 | } |
| 2141 | EXPORT_SYMBOL(stedma40_filter); |
| 2142 | |
Rabin Vincent | ac2c0a3 | 2011-01-25 11:18:11 +0100 | [diff] [blame] | 2143 | static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src) |
| 2144 | { |
| 2145 | bool realtime = d40c->dma_cfg.realtime; |
| 2146 | bool highprio = d40c->dma_cfg.high_priority; |
| 2147 | u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1; |
| 2148 | u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1; |
| 2149 | u32 event = D40_TYPE_TO_EVENT(dev_type); |
| 2150 | u32 group = D40_TYPE_TO_GROUP(dev_type); |
| 2151 | u32 bit = 1 << event; |
| 2152 | |
| 2153 | /* Destination event lines are stored in the upper halfword */ |
| 2154 | if (!src) |
| 2155 | bit <<= 16; |
| 2156 | |
| 2157 | writel(bit, d40c->base->virtbase + prioreg + group * 4); |
| 2158 | writel(bit, d40c->base->virtbase + rtreg + group * 4); |
| 2159 | } |
| 2160 | |
| 2161 | static void d40_set_prio_realtime(struct d40_chan *d40c) |
| 2162 | { |
| 2163 | if (d40c->base->rev < 3) |
| 2164 | return; |
| 2165 | |
| 2166 | if ((d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) || |
| 2167 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) |
| 2168 | __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true); |
| 2169 | |
| 2170 | if ((d40c->dma_cfg.dir == STEDMA40_MEM_TO_PERIPH) || |
| 2171 | (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH)) |
| 2172 | __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false); |
| 2173 | } |
| 2174 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2175 | /* DMA ENGINE functions */ |
| 2176 | static int d40_alloc_chan_resources(struct dma_chan *chan) |
| 2177 | { |
| 2178 | int err; |
| 2179 | unsigned long flags; |
| 2180 | struct d40_chan *d40c = |
| 2181 | container_of(chan, struct d40_chan, chan); |
Linus Walleij | ef1872e | 2010-06-20 21:24:52 +0000 | [diff] [blame] | 2182 | bool is_free_phy; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2183 | spin_lock_irqsave(&d40c->lock, flags); |
| 2184 | |
| 2185 | d40c->completed = chan->cookie = 1; |
| 2186 | |
Rabin Vincent | ce2ca12 | 2010-10-12 13:00:49 +0000 | [diff] [blame] | 2187 | /* If no dma configuration is set use default configuration (memcpy) */ |
| 2188 | if (!d40c->configured) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2189 | err = d40_config_memcpy(d40c); |
Jonas Aaberg | ff0b12b | 2010-06-20 21:25:15 +0000 | [diff] [blame] | 2190 | if (err) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2191 | chan_err(d40c, "Failed to configure memcpy channel\n"); |
Jonas Aaberg | ff0b12b | 2010-06-20 21:25:15 +0000 | [diff] [blame] | 2192 | goto fail; |
| 2193 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 2196 | err = d40_allocate_channel(d40c, &is_free_phy); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2197 | if (err) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2198 | chan_err(d40c, "Failed to allocate channel\n"); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2199 | d40c->configured = false; |
Jonas Aaberg | ff0b12b | 2010-06-20 21:25:15 +0000 | [diff] [blame] | 2200 | goto fail; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2201 | } |
| 2202 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2203 | pm_runtime_get_sync(d40c->base->dev); |
Linus Walleij | ef1872e | 2010-06-20 21:24:52 +0000 | [diff] [blame] | 2204 | /* Fill in basic CFG register values */ |
| 2205 | d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg, |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 2206 | &d40c->dst_def_cfg, chan_is_logical(d40c)); |
Linus Walleij | ef1872e | 2010-06-20 21:24:52 +0000 | [diff] [blame] | 2207 | |
Rabin Vincent | ac2c0a3 | 2011-01-25 11:18:11 +0100 | [diff] [blame] | 2208 | d40_set_prio_realtime(d40c); |
| 2209 | |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 2210 | if (chan_is_logical(d40c)) { |
Linus Walleij | ef1872e | 2010-06-20 21:24:52 +0000 | [diff] [blame] | 2211 | d40_log_cfg(&d40c->dma_cfg, |
| 2212 | &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); |
| 2213 | |
| 2214 | if (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_MEM) |
| 2215 | d40c->lcpa = d40c->base->lcpa_base + |
| 2216 | d40c->dma_cfg.src_dev_type * D40_LCPA_CHAN_SIZE; |
| 2217 | else |
| 2218 | d40c->lcpa = d40c->base->lcpa_base + |
| 2219 | d40c->dma_cfg.dst_dev_type * |
| 2220 | D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA; |
| 2221 | } |
| 2222 | |
Narayanan G | 5cd326f | 2011-11-30 19:20:42 +0530 | [diff] [blame] | 2223 | dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n", |
| 2224 | chan_is_logical(d40c) ? "logical" : "physical", |
| 2225 | d40c->phy_chan->num, |
| 2226 | d40c->dma_cfg.use_fixed_channel ? ", fixed" : ""); |
| 2227 | |
| 2228 | |
Linus Walleij | ef1872e | 2010-06-20 21:24:52 +0000 | [diff] [blame] | 2229 | /* |
| 2230 | * Only write channel configuration to the DMA if the physical |
| 2231 | * resource is free. In case of multiple logical channels |
| 2232 | * on the same physical resource, only the first write is necessary. |
| 2233 | */ |
Jonas Aaberg | b55912c | 2010-08-09 12:08:02 +0000 | [diff] [blame] | 2234 | if (is_free_phy) |
| 2235 | d40_config_write(d40c); |
Jonas Aaberg | ff0b12b | 2010-06-20 21:25:15 +0000 | [diff] [blame] | 2236 | fail: |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2237 | pm_runtime_mark_last_busy(d40c->base->dev); |
| 2238 | pm_runtime_put_autosuspend(d40c->base->dev); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2239 | spin_unlock_irqrestore(&d40c->lock, flags); |
Jonas Aaberg | ff0b12b | 2010-06-20 21:25:15 +0000 | [diff] [blame] | 2240 | return err; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | static void d40_free_chan_resources(struct dma_chan *chan) |
| 2244 | { |
| 2245 | struct d40_chan *d40c = |
| 2246 | container_of(chan, struct d40_chan, chan); |
| 2247 | int err; |
| 2248 | unsigned long flags; |
| 2249 | |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2250 | if (d40c->phy_chan == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2251 | chan_err(d40c, "Cannot free unallocated channel\n"); |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2252 | return; |
| 2253 | } |
| 2254 | |
| 2255 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2256 | spin_lock_irqsave(&d40c->lock, flags); |
| 2257 | |
| 2258 | err = d40_free_dma(d40c); |
| 2259 | |
| 2260 | if (err) |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2261 | chan_err(d40c, "Failed to free channel\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2262 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 2263 | } |
| 2264 | |
| 2265 | static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan, |
| 2266 | dma_addr_t dst, |
| 2267 | dma_addr_t src, |
| 2268 | size_t size, |
Jonas Aaberg | 2a61434 | 2010-06-20 21:25:24 +0000 | [diff] [blame] | 2269 | unsigned long dma_flags) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2270 | { |
Rabin Vincent | 95944c6 | 2011-01-25 11:18:17 +0100 | [diff] [blame] | 2271 | struct scatterlist dst_sg; |
| 2272 | struct scatterlist src_sg; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2273 | |
Rabin Vincent | 95944c6 | 2011-01-25 11:18:17 +0100 | [diff] [blame] | 2274 | sg_init_table(&dst_sg, 1); |
| 2275 | sg_init_table(&src_sg, 1); |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2276 | |
Rabin Vincent | 95944c6 | 2011-01-25 11:18:17 +0100 | [diff] [blame] | 2277 | sg_dma_address(&dst_sg) = dst; |
| 2278 | sg_dma_address(&src_sg) = src; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2279 | |
Rabin Vincent | 95944c6 | 2011-01-25 11:18:17 +0100 | [diff] [blame] | 2280 | sg_dma_len(&dst_sg) = size; |
| 2281 | sg_dma_len(&src_sg) = size; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2282 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2283 | return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2284 | } |
| 2285 | |
Ira Snyder | 0d68866 | 2010-09-30 11:46:47 +0000 | [diff] [blame] | 2286 | static struct dma_async_tx_descriptor * |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2287 | d40_prep_memcpy_sg(struct dma_chan *chan, |
| 2288 | struct scatterlist *dst_sg, unsigned int dst_nents, |
| 2289 | struct scatterlist *src_sg, unsigned int src_nents, |
| 2290 | unsigned long dma_flags) |
Ira Snyder | 0d68866 | 2010-09-30 11:46:47 +0000 | [diff] [blame] | 2291 | { |
| 2292 | if (dst_nents != src_nents) |
| 2293 | return NULL; |
| 2294 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2295 | return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags); |
Rabin Vincent | 00ac034 | 2011-01-25 11:18:20 +0100 | [diff] [blame] | 2296 | } |
| 2297 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2298 | static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan, |
| 2299 | struct scatterlist *sgl, |
| 2300 | unsigned int sg_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2301 | enum dma_transfer_direction direction, |
Jonas Aaberg | 2a61434 | 2010-06-20 21:25:24 +0000 | [diff] [blame] | 2302 | unsigned long dma_flags) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2303 | { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2304 | if (direction != DMA_DEV_TO_MEM && direction != DMA_MEM_TO_DEV) |
Rabin Vincent | 00ac034 | 2011-01-25 11:18:20 +0100 | [diff] [blame] | 2305 | return NULL; |
| 2306 | |
Rabin Vincent | cade1d3 | 2011-01-25 11:18:23 +0100 | [diff] [blame] | 2307 | return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2310 | static struct dma_async_tx_descriptor * |
| 2311 | dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr, |
| 2312 | size_t buf_len, size_t period_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2313 | enum dma_transfer_direction direction) |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2314 | { |
| 2315 | unsigned int periods = buf_len / period_len; |
| 2316 | struct dma_async_tx_descriptor *txd; |
| 2317 | struct scatterlist *sg; |
| 2318 | int i; |
| 2319 | |
Robert Marklund | 79ca7ec | 2011-06-27 11:33:24 +0200 | [diff] [blame] | 2320 | sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT); |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2321 | for (i = 0; i < periods; i++) { |
| 2322 | sg_dma_address(&sg[i]) = dma_addr; |
| 2323 | sg_dma_len(&sg[i]) = period_len; |
| 2324 | dma_addr += period_len; |
| 2325 | } |
| 2326 | |
| 2327 | sg[periods].offset = 0; |
| 2328 | sg[periods].length = 0; |
| 2329 | sg[periods].page_link = |
| 2330 | ((unsigned long)sg | 0x01) & ~0x02; |
| 2331 | |
| 2332 | txd = d40_prep_sg(chan, sg, sg, periods, direction, |
| 2333 | DMA_PREP_INTERRUPT); |
| 2334 | |
| 2335 | kfree(sg); |
| 2336 | |
| 2337 | return txd; |
| 2338 | } |
| 2339 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2340 | static enum dma_status d40_tx_status(struct dma_chan *chan, |
| 2341 | dma_cookie_t cookie, |
| 2342 | struct dma_tx_state *txstate) |
| 2343 | { |
| 2344 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); |
| 2345 | dma_cookie_t last_used; |
| 2346 | dma_cookie_t last_complete; |
| 2347 | int ret; |
| 2348 | |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2349 | if (d40c->phy_chan == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2350 | chan_err(d40c, "Cannot read status of unallocated channel\n"); |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2351 | return -EINVAL; |
| 2352 | } |
| 2353 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2354 | last_complete = d40c->completed; |
| 2355 | last_used = chan->cookie; |
| 2356 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 2357 | if (d40_is_paused(d40c)) |
| 2358 | ret = DMA_PAUSED; |
| 2359 | else |
| 2360 | ret = dma_async_is_complete(cookie, last_complete, last_used); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2361 | |
Jonas Aaberg | a5ebca4 | 2010-05-18 00:41:09 +0200 | [diff] [blame] | 2362 | dma_set_tx_state(txstate, last_complete, last_used, |
| 2363 | stedma40_residue(chan)); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2364 | |
| 2365 | return ret; |
| 2366 | } |
| 2367 | |
| 2368 | static void d40_issue_pending(struct dma_chan *chan) |
| 2369 | { |
| 2370 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); |
| 2371 | unsigned long flags; |
| 2372 | |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2373 | if (d40c->phy_chan == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2374 | chan_err(d40c, "Channel is not allocated!\n"); |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2375 | return; |
| 2376 | } |
| 2377 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2378 | spin_lock_irqsave(&d40c->lock, flags); |
| 2379 | |
Per Forlin | a8f3067 | 2011-06-26 23:29:52 +0200 | [diff] [blame] | 2380 | list_splice_tail_init(&d40c->pending_queue, &d40c->queue); |
| 2381 | |
| 2382 | /* Busy means that queued jobs are already being processed */ |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2383 | if (!d40c->busy) |
| 2384 | (void) d40_queue_start(d40c); |
| 2385 | |
| 2386 | spin_unlock_irqrestore(&d40c->lock, flags); |
| 2387 | } |
| 2388 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2389 | static int |
| 2390 | dma40_config_to_halfchannel(struct d40_chan *d40c, |
| 2391 | struct stedma40_half_channel_info *info, |
| 2392 | enum dma_slave_buswidth width, |
| 2393 | u32 maxburst) |
| 2394 | { |
| 2395 | enum stedma40_periph_data_width addr_width; |
| 2396 | int psize; |
| 2397 | |
| 2398 | switch (width) { |
| 2399 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 2400 | addr_width = STEDMA40_BYTE_WIDTH; |
| 2401 | break; |
| 2402 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 2403 | addr_width = STEDMA40_HALFWORD_WIDTH; |
| 2404 | break; |
| 2405 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
| 2406 | addr_width = STEDMA40_WORD_WIDTH; |
| 2407 | break; |
| 2408 | case DMA_SLAVE_BUSWIDTH_8_BYTES: |
| 2409 | addr_width = STEDMA40_DOUBLEWORD_WIDTH; |
| 2410 | break; |
| 2411 | default: |
| 2412 | dev_err(d40c->base->dev, |
| 2413 | "illegal peripheral address width " |
| 2414 | "requested (%d)\n", |
| 2415 | width); |
| 2416 | return -EINVAL; |
| 2417 | } |
| 2418 | |
| 2419 | if (chan_is_logical(d40c)) { |
| 2420 | if (maxburst >= 16) |
| 2421 | psize = STEDMA40_PSIZE_LOG_16; |
| 2422 | else if (maxburst >= 8) |
| 2423 | psize = STEDMA40_PSIZE_LOG_8; |
| 2424 | else if (maxburst >= 4) |
| 2425 | psize = STEDMA40_PSIZE_LOG_4; |
| 2426 | else |
| 2427 | psize = STEDMA40_PSIZE_LOG_1; |
| 2428 | } else { |
| 2429 | if (maxburst >= 16) |
| 2430 | psize = STEDMA40_PSIZE_PHY_16; |
| 2431 | else if (maxburst >= 8) |
| 2432 | psize = STEDMA40_PSIZE_PHY_8; |
| 2433 | else if (maxburst >= 4) |
| 2434 | psize = STEDMA40_PSIZE_PHY_4; |
| 2435 | else |
| 2436 | psize = STEDMA40_PSIZE_PHY_1; |
| 2437 | } |
| 2438 | |
| 2439 | info->data_width = addr_width; |
| 2440 | info->psize = psize; |
| 2441 | info->flow_ctrl = STEDMA40_NO_FLOW_CTRL; |
| 2442 | |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2446 | /* Runtime reconfiguration extension */ |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2447 | static int d40_set_runtime_config(struct dma_chan *chan, |
| 2448 | struct dma_slave_config *config) |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2449 | { |
| 2450 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); |
| 2451 | struct stedma40_chan_cfg *cfg = &d40c->dma_cfg; |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2452 | enum dma_slave_buswidth src_addr_width, dst_addr_width; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2453 | dma_addr_t config_addr; |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2454 | u32 src_maxburst, dst_maxburst; |
| 2455 | int ret; |
| 2456 | |
| 2457 | src_addr_width = config->src_addr_width; |
| 2458 | src_maxburst = config->src_maxburst; |
| 2459 | dst_addr_width = config->dst_addr_width; |
| 2460 | dst_maxburst = config->dst_maxburst; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2461 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2462 | if (config->direction == DMA_DEV_TO_MEM) { |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2463 | dma_addr_t dev_addr_rx = |
| 2464 | d40c->base->plat_data->dev_rx[cfg->src_dev_type]; |
| 2465 | |
| 2466 | config_addr = config->src_addr; |
| 2467 | if (dev_addr_rx) |
| 2468 | dev_dbg(d40c->base->dev, |
| 2469 | "channel has a pre-wired RX address %08x " |
| 2470 | "overriding with %08x\n", |
| 2471 | dev_addr_rx, config_addr); |
| 2472 | if (cfg->dir != STEDMA40_PERIPH_TO_MEM) |
| 2473 | dev_dbg(d40c->base->dev, |
| 2474 | "channel was not configured for peripheral " |
| 2475 | "to memory transfer (%d) overriding\n", |
| 2476 | cfg->dir); |
| 2477 | cfg->dir = STEDMA40_PERIPH_TO_MEM; |
| 2478 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2479 | /* Configure the memory side */ |
| 2480 | if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) |
| 2481 | dst_addr_width = src_addr_width; |
| 2482 | if (dst_maxburst == 0) |
| 2483 | dst_maxburst = src_maxburst; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2484 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2485 | } else if (config->direction == DMA_MEM_TO_DEV) { |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2486 | dma_addr_t dev_addr_tx = |
| 2487 | d40c->base->plat_data->dev_tx[cfg->dst_dev_type]; |
| 2488 | |
| 2489 | config_addr = config->dst_addr; |
| 2490 | if (dev_addr_tx) |
| 2491 | dev_dbg(d40c->base->dev, |
| 2492 | "channel has a pre-wired TX address %08x " |
| 2493 | "overriding with %08x\n", |
| 2494 | dev_addr_tx, config_addr); |
| 2495 | if (cfg->dir != STEDMA40_MEM_TO_PERIPH) |
| 2496 | dev_dbg(d40c->base->dev, |
| 2497 | "channel was not configured for memory " |
| 2498 | "to peripheral transfer (%d) overriding\n", |
| 2499 | cfg->dir); |
| 2500 | cfg->dir = STEDMA40_MEM_TO_PERIPH; |
| 2501 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2502 | /* Configure the memory side */ |
| 2503 | if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) |
| 2504 | src_addr_width = dst_addr_width; |
| 2505 | if (src_maxburst == 0) |
| 2506 | src_maxburst = dst_maxburst; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2507 | } else { |
| 2508 | dev_err(d40c->base->dev, |
| 2509 | "unrecognized channel direction %d\n", |
| 2510 | config->direction); |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2511 | return -EINVAL; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2512 | } |
| 2513 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2514 | if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) { |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2515 | dev_err(d40c->base->dev, |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2516 | "src/dst width/maxburst mismatch: %d*%d != %d*%d\n", |
| 2517 | src_maxburst, |
| 2518 | src_addr_width, |
| 2519 | dst_maxburst, |
| 2520 | dst_addr_width); |
| 2521 | return -EINVAL; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2524 | ret = dma40_config_to_halfchannel(d40c, &cfg->src_info, |
| 2525 | src_addr_width, |
| 2526 | src_maxburst); |
| 2527 | if (ret) |
| 2528 | return ret; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2529 | |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2530 | ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info, |
| 2531 | dst_addr_width, |
| 2532 | dst_maxburst); |
| 2533 | if (ret) |
| 2534 | return ret; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2535 | |
Per Forlin | a59670a | 2010-10-06 09:05:27 +0000 | [diff] [blame] | 2536 | /* Fill in register values */ |
Rabin Vincent | 724a857 | 2011-01-25 11:18:08 +0100 | [diff] [blame] | 2537 | if (chan_is_logical(d40c)) |
Per Forlin | a59670a | 2010-10-06 09:05:27 +0000 | [diff] [blame] | 2538 | d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3); |
| 2539 | else |
| 2540 | d40_phy_cfg(cfg, &d40c->src_def_cfg, |
| 2541 | &d40c->dst_def_cfg, false); |
| 2542 | |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2543 | /* These settings will take precedence later */ |
| 2544 | d40c->runtime_addr = config_addr; |
| 2545 | d40c->runtime_direction = config->direction; |
| 2546 | dev_dbg(d40c->base->dev, |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2547 | "configured channel %s for %s, data width %d/%d, " |
| 2548 | "maxburst %d/%d elements, LE, no flow control\n", |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2549 | dma_chan_name(chan), |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2550 | (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX", |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2551 | src_addr_width, dst_addr_width, |
| 2552 | src_maxburst, dst_maxburst); |
| 2553 | |
| 2554 | return 0; |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
Linus Walleij | 0582763 | 2010-05-17 16:30:42 -0700 | [diff] [blame] | 2557 | static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
| 2558 | unsigned long arg) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2559 | { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2560 | struct d40_chan *d40c = container_of(chan, struct d40_chan, chan); |
| 2561 | |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2562 | if (d40c->phy_chan == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2563 | chan_err(d40c, "Channel is not allocated!\n"); |
Jonas Aaberg | 0d0f6b8 | 2010-06-20 21:25:31 +0000 | [diff] [blame] | 2564 | return -EINVAL; |
| 2565 | } |
| 2566 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2567 | switch (cmd) { |
| 2568 | case DMA_TERMINATE_ALL: |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 2569 | return d40_terminate_all(d40c); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2570 | case DMA_PAUSE: |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 2571 | return d40_pause(d40c); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2572 | case DMA_RESUME: |
Rabin Vincent | 86eb5fb | 2011-01-25 11:18:34 +0100 | [diff] [blame] | 2573 | return d40_resume(d40c); |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2574 | case DMA_SLAVE_CONFIG: |
Rabin Vincent | 98ca528 | 2011-06-27 11:33:38 +0200 | [diff] [blame] | 2575 | return d40_set_runtime_config(chan, |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2576 | (struct dma_slave_config *) arg); |
Linus Walleij | 95e1400 | 2010-08-04 13:37:45 +0200 | [diff] [blame] | 2577 | default: |
| 2578 | break; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /* Other commands are unimplemented */ |
| 2582 | return -ENXIO; |
| 2583 | } |
| 2584 | |
| 2585 | /* Initialization functions */ |
| 2586 | |
| 2587 | static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma, |
| 2588 | struct d40_chan *chans, int offset, |
| 2589 | int num_chans) |
| 2590 | { |
| 2591 | int i = 0; |
| 2592 | struct d40_chan *d40c; |
| 2593 | |
| 2594 | INIT_LIST_HEAD(&dma->channels); |
| 2595 | |
| 2596 | for (i = offset; i < offset + num_chans; i++) { |
| 2597 | d40c = &chans[i]; |
| 2598 | d40c->base = base; |
| 2599 | d40c->chan.device = dma; |
| 2600 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2601 | spin_lock_init(&d40c->lock); |
| 2602 | |
| 2603 | d40c->log_num = D40_PHY_CHAN; |
| 2604 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2605 | INIT_LIST_HEAD(&d40c->active); |
| 2606 | INIT_LIST_HEAD(&d40c->queue); |
Per Forlin | a8f3067 | 2011-06-26 23:29:52 +0200 | [diff] [blame] | 2607 | INIT_LIST_HEAD(&d40c->pending_queue); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2608 | INIT_LIST_HEAD(&d40c->client); |
Per Forlin | 82babbb36 | 2011-08-29 13:33:35 +0200 | [diff] [blame] | 2609 | INIT_LIST_HEAD(&d40c->prepare_queue); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2610 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2611 | tasklet_init(&d40c->tasklet, dma_tasklet, |
| 2612 | (unsigned long) d40c); |
| 2613 | |
| 2614 | list_add_tail(&d40c->chan.device_node, |
| 2615 | &dma->channels); |
| 2616 | } |
| 2617 | } |
| 2618 | |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2619 | static void d40_ops_init(struct d40_base *base, struct dma_device *dev) |
| 2620 | { |
| 2621 | if (dma_has_cap(DMA_SLAVE, dev->cap_mask)) |
| 2622 | dev->device_prep_slave_sg = d40_prep_slave_sg; |
| 2623 | |
| 2624 | if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) { |
| 2625 | dev->device_prep_dma_memcpy = d40_prep_memcpy; |
| 2626 | |
| 2627 | /* |
| 2628 | * This controller can only access address at even |
| 2629 | * 32bit boundaries, i.e. 2^2 |
| 2630 | */ |
| 2631 | dev->copy_align = 2; |
| 2632 | } |
| 2633 | |
| 2634 | if (dma_has_cap(DMA_SG, dev->cap_mask)) |
| 2635 | dev->device_prep_dma_sg = d40_prep_memcpy_sg; |
| 2636 | |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2637 | if (dma_has_cap(DMA_CYCLIC, dev->cap_mask)) |
| 2638 | dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic; |
| 2639 | |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2640 | dev->device_alloc_chan_resources = d40_alloc_chan_resources; |
| 2641 | dev->device_free_chan_resources = d40_free_chan_resources; |
| 2642 | dev->device_issue_pending = d40_issue_pending; |
| 2643 | dev->device_tx_status = d40_tx_status; |
| 2644 | dev->device_control = d40_control; |
| 2645 | dev->dev = base->dev; |
| 2646 | } |
| 2647 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2648 | static int __init d40_dmaengine_init(struct d40_base *base, |
| 2649 | int num_reserved_chans) |
| 2650 | { |
| 2651 | int err ; |
| 2652 | |
| 2653 | d40_chan_init(base, &base->dma_slave, base->log_chans, |
| 2654 | 0, base->num_log_chans); |
| 2655 | |
| 2656 | dma_cap_zero(base->dma_slave.cap_mask); |
| 2657 | dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask); |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2658 | dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2659 | |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2660 | d40_ops_init(base, &base->dma_slave); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2661 | |
| 2662 | err = dma_async_device_register(&base->dma_slave); |
| 2663 | |
| 2664 | if (err) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2665 | d40_err(base->dev, "Failed to register slave channels\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2666 | goto failure1; |
| 2667 | } |
| 2668 | |
| 2669 | d40_chan_init(base, &base->dma_memcpy, base->log_chans, |
| 2670 | base->num_log_chans, base->plat_data->memcpy_len); |
| 2671 | |
| 2672 | dma_cap_zero(base->dma_memcpy.cap_mask); |
| 2673 | dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask); |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2674 | dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2675 | |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2676 | d40_ops_init(base, &base->dma_memcpy); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2677 | |
| 2678 | err = dma_async_device_register(&base->dma_memcpy); |
| 2679 | |
| 2680 | if (err) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2681 | d40_err(base->dev, |
| 2682 | "Failed to regsiter memcpy only channels\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2683 | goto failure2; |
| 2684 | } |
| 2685 | |
| 2686 | d40_chan_init(base, &base->dma_both, base->phy_chans, |
| 2687 | 0, num_reserved_chans); |
| 2688 | |
| 2689 | dma_cap_zero(base->dma_both.cap_mask); |
| 2690 | dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask); |
| 2691 | dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask); |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2692 | dma_cap_set(DMA_SG, base->dma_both.cap_mask); |
Rabin Vincent | 0c842b5 | 2011-01-25 11:18:35 +0100 | [diff] [blame] | 2693 | dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2694 | |
Rabin Vincent | 7ad74a7 | 2011-01-25 11:18:33 +0100 | [diff] [blame] | 2695 | d40_ops_init(base, &base->dma_both); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2696 | err = dma_async_device_register(&base->dma_both); |
| 2697 | |
| 2698 | if (err) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2699 | d40_err(base->dev, |
| 2700 | "Failed to register logical and physical capable channels\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2701 | goto failure3; |
| 2702 | } |
| 2703 | return 0; |
| 2704 | failure3: |
| 2705 | dma_async_device_unregister(&base->dma_memcpy); |
| 2706 | failure2: |
| 2707 | dma_async_device_unregister(&base->dma_slave); |
| 2708 | failure1: |
| 2709 | return err; |
| 2710 | } |
| 2711 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2712 | /* Suspend resume functionality */ |
| 2713 | #ifdef CONFIG_PM |
| 2714 | static int dma40_pm_suspend(struct device *dev) |
| 2715 | { |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 2716 | struct platform_device *pdev = to_platform_device(dev); |
| 2717 | struct d40_base *base = platform_get_drvdata(pdev); |
| 2718 | int ret = 0; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2719 | if (!pm_runtime_suspended(dev)) |
| 2720 | return -EBUSY; |
| 2721 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 2722 | if (base->lcpa_regulator) |
| 2723 | ret = regulator_disable(base->lcpa_regulator); |
| 2724 | return ret; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | static int dma40_runtime_suspend(struct device *dev) |
| 2728 | { |
| 2729 | struct platform_device *pdev = to_platform_device(dev); |
| 2730 | struct d40_base *base = platform_get_drvdata(pdev); |
| 2731 | |
| 2732 | d40_save_restore_registers(base, true); |
| 2733 | |
| 2734 | /* Don't disable/enable clocks for v1 due to HW bugs */ |
| 2735 | if (base->rev != 1) |
| 2736 | writel_relaxed(base->gcc_pwr_off_mask, |
| 2737 | base->virtbase + D40_DREG_GCC); |
| 2738 | |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
| 2742 | static int dma40_runtime_resume(struct device *dev) |
| 2743 | { |
| 2744 | struct platform_device *pdev = to_platform_device(dev); |
| 2745 | struct d40_base *base = platform_get_drvdata(pdev); |
| 2746 | |
| 2747 | if (base->initialized) |
| 2748 | d40_save_restore_registers(base, false); |
| 2749 | |
| 2750 | writel_relaxed(D40_DREG_GCC_ENABLE_ALL, |
| 2751 | base->virtbase + D40_DREG_GCC); |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 2755 | static int dma40_resume(struct device *dev) |
| 2756 | { |
| 2757 | struct platform_device *pdev = to_platform_device(dev); |
| 2758 | struct d40_base *base = platform_get_drvdata(pdev); |
| 2759 | int ret = 0; |
| 2760 | |
| 2761 | if (base->lcpa_regulator) |
| 2762 | ret = regulator_enable(base->lcpa_regulator); |
| 2763 | |
| 2764 | return ret; |
| 2765 | } |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2766 | |
| 2767 | static const struct dev_pm_ops dma40_pm_ops = { |
| 2768 | .suspend = dma40_pm_suspend, |
| 2769 | .runtime_suspend = dma40_runtime_suspend, |
| 2770 | .runtime_resume = dma40_runtime_resume, |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 2771 | .resume = dma40_resume, |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2772 | }; |
| 2773 | #define DMA40_PM_OPS (&dma40_pm_ops) |
| 2774 | #else |
| 2775 | #define DMA40_PM_OPS NULL |
| 2776 | #endif |
| 2777 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2778 | /* Initialization functions. */ |
| 2779 | |
| 2780 | static int __init d40_phy_res_init(struct d40_base *base) |
| 2781 | { |
| 2782 | int i; |
| 2783 | int num_phy_chans_avail = 0; |
| 2784 | u32 val[2]; |
| 2785 | int odd_even_bit = -2; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2786 | int gcc = D40_DREG_GCC_ENA; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2787 | |
| 2788 | val[0] = readl(base->virtbase + D40_DREG_PRSME); |
| 2789 | val[1] = readl(base->virtbase + D40_DREG_PRSMO); |
| 2790 | |
| 2791 | for (i = 0; i < base->num_phy_chans; i++) { |
| 2792 | base->phy_res[i].num = i; |
| 2793 | odd_even_bit += 2 * ((i % 2) == 0); |
| 2794 | if (((val[i % 2] >> odd_even_bit) & 3) == 1) { |
| 2795 | /* Mark security only channels as occupied */ |
| 2796 | base->phy_res[i].allocated_src = D40_ALLOC_PHY; |
| 2797 | base->phy_res[i].allocated_dst = D40_ALLOC_PHY; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2798 | base->phy_res[i].reserved = true; |
| 2799 | gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), |
| 2800 | D40_DREG_GCC_SRC); |
| 2801 | gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i), |
| 2802 | D40_DREG_GCC_DST); |
| 2803 | |
| 2804 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2805 | } else { |
| 2806 | base->phy_res[i].allocated_src = D40_ALLOC_FREE; |
| 2807 | base->phy_res[i].allocated_dst = D40_ALLOC_FREE; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2808 | base->phy_res[i].reserved = false; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2809 | num_phy_chans_avail++; |
| 2810 | } |
| 2811 | spin_lock_init(&base->phy_res[i].lock); |
| 2812 | } |
Jonas Aaberg | 6b7acd8 | 2010-06-20 21:26:59 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* Mark disabled channels as occupied */ |
| 2815 | for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) { |
Rabin Vincent | f57b407 | 2010-10-06 08:20:35 +0000 | [diff] [blame] | 2816 | int chan = base->plat_data->disabled_channels[i]; |
| 2817 | |
| 2818 | base->phy_res[chan].allocated_src = D40_ALLOC_PHY; |
| 2819 | base->phy_res[chan].allocated_dst = D40_ALLOC_PHY; |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2820 | base->phy_res[chan].reserved = true; |
| 2821 | gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), |
| 2822 | D40_DREG_GCC_SRC); |
| 2823 | gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan), |
| 2824 | D40_DREG_GCC_DST); |
Rabin Vincent | f57b407 | 2010-10-06 08:20:35 +0000 | [diff] [blame] | 2825 | num_phy_chans_avail--; |
Jonas Aaberg | 6b7acd8 | 2010-06-20 21:26:59 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2828 | dev_info(base->dev, "%d of %d physical DMA channels available\n", |
| 2829 | num_phy_chans_avail, base->num_phy_chans); |
| 2830 | |
| 2831 | /* Verify settings extended vs standard */ |
| 2832 | val[0] = readl(base->virtbase + D40_DREG_PRTYP); |
| 2833 | |
| 2834 | for (i = 0; i < base->num_phy_chans; i++) { |
| 2835 | |
| 2836 | if (base->phy_res[i].allocated_src == D40_ALLOC_FREE && |
| 2837 | (val[0] & 0x3) != 1) |
| 2838 | dev_info(base->dev, |
| 2839 | "[%s] INFO: channel %d is misconfigured (%d)\n", |
| 2840 | __func__, i, val[0] & 0x3); |
| 2841 | |
| 2842 | val[0] = val[0] >> 2; |
| 2843 | } |
| 2844 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2845 | /* |
| 2846 | * To keep things simple, Enable all clocks initially. |
| 2847 | * The clocks will get managed later post channel allocation. |
| 2848 | * The clocks for the event lines on which reserved channels exists |
| 2849 | * are not managed here. |
| 2850 | */ |
| 2851 | writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC); |
| 2852 | base->gcc_pwr_off_mask = gcc; |
| 2853 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2854 | return num_phy_chans_avail; |
| 2855 | } |
| 2856 | |
| 2857 | static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev) |
| 2858 | { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2859 | struct stedma40_platform_data *plat_data; |
| 2860 | struct clk *clk = NULL; |
| 2861 | void __iomem *virtbase = NULL; |
| 2862 | struct resource *res = NULL; |
| 2863 | struct d40_base *base = NULL; |
| 2864 | int num_log_chans = 0; |
| 2865 | int num_phy_chans; |
| 2866 | int i; |
Linus Walleij | f4b8976 | 2011-06-27 11:33:46 +0200 | [diff] [blame] | 2867 | u32 pid; |
| 2868 | u32 cid; |
| 2869 | u8 rev; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2870 | |
| 2871 | clk = clk_get(&pdev->dev, NULL); |
| 2872 | |
| 2873 | if (IS_ERR(clk)) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2874 | d40_err(&pdev->dev, "No matching clock found\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2875 | goto failure; |
| 2876 | } |
| 2877 | |
| 2878 | clk_enable(clk); |
| 2879 | |
| 2880 | /* Get IO for DMAC base address */ |
| 2881 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base"); |
| 2882 | if (!res) |
| 2883 | goto failure; |
| 2884 | |
| 2885 | if (request_mem_region(res->start, resource_size(res), |
| 2886 | D40_NAME " I/O base") == NULL) |
| 2887 | goto failure; |
| 2888 | |
| 2889 | virtbase = ioremap(res->start, resource_size(res)); |
| 2890 | if (!virtbase) |
| 2891 | goto failure; |
| 2892 | |
Linus Walleij | f4b8976 | 2011-06-27 11:33:46 +0200 | [diff] [blame] | 2893 | /* This is just a regular AMBA PrimeCell ID actually */ |
| 2894 | for (pid = 0, i = 0; i < 4; i++) |
| 2895 | pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i) |
| 2896 | & 255) << (i * 8); |
| 2897 | for (cid = 0, i = 0; i < 4; i++) |
| 2898 | cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i) |
| 2899 | & 255) << (i * 8); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2900 | |
Linus Walleij | f4b8976 | 2011-06-27 11:33:46 +0200 | [diff] [blame] | 2901 | if (cid != AMBA_CID) { |
| 2902 | d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2903 | goto failure; |
| 2904 | } |
Linus Walleij | f4b8976 | 2011-06-27 11:33:46 +0200 | [diff] [blame] | 2905 | if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) { |
| 2906 | d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n", |
| 2907 | AMBA_MANF_BITS(pid), |
| 2908 | AMBA_VENDOR_ST); |
| 2909 | goto failure; |
| 2910 | } |
| 2911 | /* |
| 2912 | * HW revision: |
| 2913 | * DB8500ed has revision 0 |
| 2914 | * ? has revision 1 |
| 2915 | * DB8500v1 has revision 2 |
| 2916 | * DB8500v2 has revision 3 |
| 2917 | */ |
| 2918 | rev = AMBA_REV_BITS(pid); |
Jonas Aaberg | 3ae0267 | 2010-08-09 12:08:18 +0000 | [diff] [blame] | 2919 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2920 | /* The number of physical channels on this HW */ |
| 2921 | num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4; |
| 2922 | |
| 2923 | dev_info(&pdev->dev, "hardware revision: %d @ 0x%x\n", |
Jonas Aaberg | 3ae0267 | 2010-08-09 12:08:18 +0000 | [diff] [blame] | 2924 | rev, res->start); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2925 | |
| 2926 | plat_data = pdev->dev.platform_data; |
| 2927 | |
| 2928 | /* Count the number of logical channels in use */ |
| 2929 | for (i = 0; i < plat_data->dev_len; i++) |
| 2930 | if (plat_data->dev_rx[i] != 0) |
| 2931 | num_log_chans++; |
| 2932 | |
| 2933 | for (i = 0; i < plat_data->dev_len; i++) |
| 2934 | if (plat_data->dev_tx[i] != 0) |
| 2935 | num_log_chans++; |
| 2936 | |
| 2937 | base = kzalloc(ALIGN(sizeof(struct d40_base), 4) + |
| 2938 | (num_phy_chans + num_log_chans + plat_data->memcpy_len) * |
| 2939 | sizeof(struct d40_chan), GFP_KERNEL); |
| 2940 | |
| 2941 | if (base == NULL) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 2942 | d40_err(&pdev->dev, "Out of memory\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2943 | goto failure; |
| 2944 | } |
| 2945 | |
Jonas Aaberg | 3ae0267 | 2010-08-09 12:08:18 +0000 | [diff] [blame] | 2946 | base->rev = rev; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2947 | base->clk = clk; |
| 2948 | base->num_phy_chans = num_phy_chans; |
| 2949 | base->num_log_chans = num_log_chans; |
| 2950 | base->phy_start = res->start; |
| 2951 | base->phy_size = resource_size(res); |
| 2952 | base->virtbase = virtbase; |
| 2953 | base->plat_data = plat_data; |
| 2954 | base->dev = &pdev->dev; |
| 2955 | base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4); |
| 2956 | base->log_chans = &base->phy_chans[num_phy_chans]; |
| 2957 | |
| 2958 | base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res), |
| 2959 | GFP_KERNEL); |
| 2960 | if (!base->phy_res) |
| 2961 | goto failure; |
| 2962 | |
| 2963 | base->lookup_phy_chans = kzalloc(num_phy_chans * |
| 2964 | sizeof(struct d40_chan *), |
| 2965 | GFP_KERNEL); |
| 2966 | if (!base->lookup_phy_chans) |
| 2967 | goto failure; |
| 2968 | |
| 2969 | if (num_log_chans + plat_data->memcpy_len) { |
| 2970 | /* |
| 2971 | * The max number of logical channels are event lines for all |
| 2972 | * src devices and dst devices |
| 2973 | */ |
| 2974 | base->lookup_log_chans = kzalloc(plat_data->dev_len * 2 * |
| 2975 | sizeof(struct d40_chan *), |
| 2976 | GFP_KERNEL); |
| 2977 | if (!base->lookup_log_chans) |
| 2978 | goto failure; |
| 2979 | } |
Jonas Aaberg | 698e473 | 2010-08-09 12:08:56 +0000 | [diff] [blame] | 2980 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2981 | base->reg_val_backup_chan = kmalloc(base->num_phy_chans * |
| 2982 | sizeof(d40_backup_regs_chan), |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2983 | GFP_KERNEL); |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 2984 | if (!base->reg_val_backup_chan) |
| 2985 | goto failure; |
| 2986 | |
| 2987 | base->lcla_pool.alloc_map = |
| 2988 | kzalloc(num_phy_chans * sizeof(struct d40_desc *) |
| 2989 | * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2990 | if (!base->lcla_pool.alloc_map) |
| 2991 | goto failure; |
| 2992 | |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 2993 | base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc), |
| 2994 | 0, SLAB_HWCACHE_ALIGN, |
| 2995 | NULL); |
| 2996 | if (base->desc_slab == NULL) |
| 2997 | goto failure; |
| 2998 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 2999 | return base; |
| 3000 | |
| 3001 | failure: |
Rabin Vincent | c6134c9 | 2010-10-06 08:20:36 +0000 | [diff] [blame] | 3002 | if (!IS_ERR(clk)) { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3003 | clk_disable(clk); |
| 3004 | clk_put(clk); |
| 3005 | } |
| 3006 | if (virtbase) |
| 3007 | iounmap(virtbase); |
| 3008 | if (res) |
| 3009 | release_mem_region(res->start, |
| 3010 | resource_size(res)); |
| 3011 | if (virtbase) |
| 3012 | iounmap(virtbase); |
| 3013 | |
| 3014 | if (base) { |
| 3015 | kfree(base->lcla_pool.alloc_map); |
| 3016 | kfree(base->lookup_log_chans); |
| 3017 | kfree(base->lookup_phy_chans); |
| 3018 | kfree(base->phy_res); |
| 3019 | kfree(base); |
| 3020 | } |
| 3021 | |
| 3022 | return NULL; |
| 3023 | } |
| 3024 | |
| 3025 | static void __init d40_hw_init(struct d40_base *base) |
| 3026 | { |
| 3027 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 3028 | static struct d40_reg_val dma_init_reg[] = { |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3029 | /* Clock every part of the DMA block from start */ |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 3030 | { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL}, |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* Interrupts on all logical channels */ |
| 3033 | { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF}, |
| 3034 | { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF}, |
| 3035 | { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF}, |
| 3036 | { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF}, |
| 3037 | { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF}, |
| 3038 | { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF}, |
| 3039 | { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF}, |
| 3040 | { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF}, |
| 3041 | { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF}, |
| 3042 | { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF}, |
| 3043 | { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF}, |
| 3044 | { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF} |
| 3045 | }; |
| 3046 | int i; |
| 3047 | u32 prmseo[2] = {0, 0}; |
| 3048 | u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF}; |
| 3049 | u32 pcmis = 0; |
| 3050 | u32 pcicr = 0; |
| 3051 | |
| 3052 | for (i = 0; i < ARRAY_SIZE(dma_init_reg); i++) |
| 3053 | writel(dma_init_reg[i].val, |
| 3054 | base->virtbase + dma_init_reg[i].reg); |
| 3055 | |
| 3056 | /* Configure all our dma channels to default settings */ |
| 3057 | for (i = 0; i < base->num_phy_chans; i++) { |
| 3058 | |
| 3059 | activeo[i % 2] = activeo[i % 2] << 2; |
| 3060 | |
| 3061 | if (base->phy_res[base->num_phy_chans - i - 1].allocated_src |
| 3062 | == D40_ALLOC_PHY) { |
| 3063 | activeo[i % 2] |= 3; |
| 3064 | continue; |
| 3065 | } |
| 3066 | |
| 3067 | /* Enable interrupt # */ |
| 3068 | pcmis = (pcmis << 1) | 1; |
| 3069 | |
| 3070 | /* Clear interrupt # */ |
| 3071 | pcicr = (pcicr << 1) | 1; |
| 3072 | |
| 3073 | /* Set channel to physical mode */ |
| 3074 | prmseo[i % 2] = prmseo[i % 2] << 2; |
| 3075 | prmseo[i % 2] |= 1; |
| 3076 | |
| 3077 | } |
| 3078 | |
| 3079 | writel(prmseo[1], base->virtbase + D40_DREG_PRMSE); |
| 3080 | writel(prmseo[0], base->virtbase + D40_DREG_PRMSO); |
| 3081 | writel(activeo[1], base->virtbase + D40_DREG_ACTIVE); |
| 3082 | writel(activeo[0], base->virtbase + D40_DREG_ACTIVO); |
| 3083 | |
| 3084 | /* Write which interrupt to enable */ |
| 3085 | writel(pcmis, base->virtbase + D40_DREG_PCMIS); |
| 3086 | |
| 3087 | /* Write which interrupt to clear */ |
| 3088 | writel(pcicr, base->virtbase + D40_DREG_PCICR); |
| 3089 | |
| 3090 | } |
| 3091 | |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3092 | static int __init d40_lcla_allocate(struct d40_base *base) |
| 3093 | { |
Rabin Vincent | 026cbc4 | 2011-01-25 11:18:14 +0100 | [diff] [blame] | 3094 | struct d40_lcla_pool *pool = &base->lcla_pool; |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3095 | unsigned long *page_list; |
| 3096 | int i, j; |
| 3097 | int ret = 0; |
| 3098 | |
| 3099 | /* |
| 3100 | * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned, |
| 3101 | * To full fill this hardware requirement without wasting 256 kb |
| 3102 | * we allocate pages until we get an aligned one. |
| 3103 | */ |
| 3104 | page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS, |
| 3105 | GFP_KERNEL); |
| 3106 | |
| 3107 | if (!page_list) { |
| 3108 | ret = -ENOMEM; |
| 3109 | goto failure; |
| 3110 | } |
| 3111 | |
| 3112 | /* Calculating how many pages that are required */ |
| 3113 | base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE; |
| 3114 | |
| 3115 | for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) { |
| 3116 | page_list[i] = __get_free_pages(GFP_KERNEL, |
| 3117 | base->lcla_pool.pages); |
| 3118 | if (!page_list[i]) { |
| 3119 | |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3120 | d40_err(base->dev, "Failed to allocate %d pages.\n", |
| 3121 | base->lcla_pool.pages); |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3122 | |
| 3123 | for (j = 0; j < i; j++) |
| 3124 | free_pages(page_list[j], base->lcla_pool.pages); |
| 3125 | goto failure; |
| 3126 | } |
| 3127 | |
| 3128 | if ((virt_to_phys((void *)page_list[i]) & |
| 3129 | (LCLA_ALIGNMENT - 1)) == 0) |
| 3130 | break; |
| 3131 | } |
| 3132 | |
| 3133 | for (j = 0; j < i; j++) |
| 3134 | free_pages(page_list[j], base->lcla_pool.pages); |
| 3135 | |
| 3136 | if (i < MAX_LCLA_ALLOC_ATTEMPTS) { |
| 3137 | base->lcla_pool.base = (void *)page_list[i]; |
| 3138 | } else { |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 3139 | /* |
| 3140 | * After many attempts and no succees with finding the correct |
| 3141 | * alignment, try with allocating a big buffer. |
| 3142 | */ |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3143 | dev_warn(base->dev, |
| 3144 | "[%s] Failed to get %d pages @ 18 bit align.\n", |
| 3145 | __func__, base->lcla_pool.pages); |
| 3146 | base->lcla_pool.base_unaligned = kmalloc(SZ_1K * |
| 3147 | base->num_phy_chans + |
| 3148 | LCLA_ALIGNMENT, |
| 3149 | GFP_KERNEL); |
| 3150 | if (!base->lcla_pool.base_unaligned) { |
| 3151 | ret = -ENOMEM; |
| 3152 | goto failure; |
| 3153 | } |
| 3154 | |
| 3155 | base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned, |
| 3156 | LCLA_ALIGNMENT); |
| 3157 | } |
| 3158 | |
Rabin Vincent | 026cbc4 | 2011-01-25 11:18:14 +0100 | [diff] [blame] | 3159 | pool->dma_addr = dma_map_single(base->dev, pool->base, |
| 3160 | SZ_1K * base->num_phy_chans, |
| 3161 | DMA_TO_DEVICE); |
| 3162 | if (dma_mapping_error(base->dev, pool->dma_addr)) { |
| 3163 | pool->dma_addr = 0; |
| 3164 | ret = -ENOMEM; |
| 3165 | goto failure; |
| 3166 | } |
| 3167 | |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3168 | writel(virt_to_phys(base->lcla_pool.base), |
| 3169 | base->virtbase + D40_DREG_LCLA); |
| 3170 | failure: |
| 3171 | kfree(page_list); |
| 3172 | return ret; |
| 3173 | } |
| 3174 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3175 | static int __init d40_probe(struct platform_device *pdev) |
| 3176 | { |
| 3177 | int err; |
| 3178 | int ret = -ENOENT; |
| 3179 | struct d40_base *base; |
| 3180 | struct resource *res = NULL; |
| 3181 | int num_reserved_chans; |
| 3182 | u32 val; |
| 3183 | |
| 3184 | base = d40_hw_detect_init(pdev); |
| 3185 | |
| 3186 | if (!base) |
| 3187 | goto failure; |
| 3188 | |
| 3189 | num_reserved_chans = d40_phy_res_init(base); |
| 3190 | |
| 3191 | platform_set_drvdata(pdev, base); |
| 3192 | |
| 3193 | spin_lock_init(&base->interrupt_lock); |
| 3194 | spin_lock_init(&base->execmd_lock); |
| 3195 | |
| 3196 | /* Get IO for logical channel parameter address */ |
| 3197 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa"); |
| 3198 | if (!res) { |
| 3199 | ret = -ENOENT; |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3200 | d40_err(&pdev->dev, "No \"lcpa\" memory resource\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3201 | goto failure; |
| 3202 | } |
| 3203 | base->lcpa_size = resource_size(res); |
| 3204 | base->phy_lcpa = res->start; |
| 3205 | |
| 3206 | if (request_mem_region(res->start, resource_size(res), |
| 3207 | D40_NAME " I/O lcpa") == NULL) { |
| 3208 | ret = -EBUSY; |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3209 | d40_err(&pdev->dev, |
| 3210 | "Failed to request LCPA region 0x%x-0x%x\n", |
| 3211 | res->start, res->end); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3212 | goto failure; |
| 3213 | } |
| 3214 | |
| 3215 | /* We make use of ESRAM memory for this. */ |
| 3216 | val = readl(base->virtbase + D40_DREG_LCPA); |
| 3217 | if (res->start != val && val != 0) { |
| 3218 | dev_warn(&pdev->dev, |
| 3219 | "[%s] Mismatch LCPA dma 0x%x, def 0x%x\n", |
| 3220 | __func__, val, res->start); |
| 3221 | } else |
| 3222 | writel(res->start, base->virtbase + D40_DREG_LCPA); |
| 3223 | |
| 3224 | base->lcpa_base = ioremap(res->start, resource_size(res)); |
| 3225 | if (!base->lcpa_base) { |
| 3226 | ret = -ENOMEM; |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3227 | d40_err(&pdev->dev, "Failed to ioremap LCPA region\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3228 | goto failure; |
| 3229 | } |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 3230 | /* If lcla has to be located in ESRAM we don't need to allocate */ |
| 3231 | if (base->plat_data->use_esram_lcla) { |
| 3232 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 3233 | "lcla_esram"); |
| 3234 | if (!res) { |
| 3235 | ret = -ENOENT; |
| 3236 | d40_err(&pdev->dev, |
| 3237 | "No \"lcla_esram\" memory resource\n"); |
| 3238 | goto failure; |
| 3239 | } |
| 3240 | base->lcla_pool.base = ioremap(res->start, |
| 3241 | resource_size(res)); |
| 3242 | if (!base->lcla_pool.base) { |
| 3243 | ret = -ENOMEM; |
| 3244 | d40_err(&pdev->dev, "Failed to ioremap LCLA region\n"); |
| 3245 | goto failure; |
| 3246 | } |
| 3247 | writel(res->start, base->virtbase + D40_DREG_LCLA); |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3248 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 3249 | } else { |
| 3250 | ret = d40_lcla_allocate(base); |
| 3251 | if (ret) { |
| 3252 | d40_err(&pdev->dev, "Failed to allocate LCLA area\n"); |
| 3253 | goto failure; |
| 3254 | } |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3255 | } |
| 3256 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3257 | spin_lock_init(&base->lcla_pool.lock); |
| 3258 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3259 | base->irq = platform_get_irq(pdev, 0); |
| 3260 | |
| 3261 | ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3262 | if (ret) { |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3263 | d40_err(&pdev->dev, "No IRQ defined\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3264 | goto failure; |
| 3265 | } |
| 3266 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 3267 | pm_runtime_irq_safe(base->dev); |
| 3268 | pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY); |
| 3269 | pm_runtime_use_autosuspend(base->dev); |
| 3270 | pm_runtime_enable(base->dev); |
| 3271 | pm_runtime_resume(base->dev); |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 3272 | |
| 3273 | if (base->plat_data->use_esram_lcla) { |
| 3274 | |
| 3275 | base->lcpa_regulator = regulator_get(base->dev, "lcla_esram"); |
| 3276 | if (IS_ERR(base->lcpa_regulator)) { |
| 3277 | d40_err(&pdev->dev, "Failed to get lcpa_regulator\n"); |
| 3278 | base->lcpa_regulator = NULL; |
| 3279 | goto failure; |
| 3280 | } |
| 3281 | |
| 3282 | ret = regulator_enable(base->lcpa_regulator); |
| 3283 | if (ret) { |
| 3284 | d40_err(&pdev->dev, |
| 3285 | "Failed to enable lcpa_regulator\n"); |
| 3286 | regulator_put(base->lcpa_regulator); |
| 3287 | base->lcpa_regulator = NULL; |
| 3288 | goto failure; |
| 3289 | } |
| 3290 | } |
| 3291 | |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 3292 | base->initialized = true; |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3293 | err = d40_dmaengine_init(base, num_reserved_chans); |
| 3294 | if (err) |
| 3295 | goto failure; |
| 3296 | |
| 3297 | d40_hw_init(base); |
| 3298 | |
| 3299 | dev_info(base->dev, "initialized\n"); |
| 3300 | return 0; |
| 3301 | |
| 3302 | failure: |
| 3303 | if (base) { |
Jonas Aaberg | c675b1b | 2010-06-20 21:25:08 +0000 | [diff] [blame] | 3304 | if (base->desc_slab) |
| 3305 | kmem_cache_destroy(base->desc_slab); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3306 | if (base->virtbase) |
| 3307 | iounmap(base->virtbase); |
Rabin Vincent | 026cbc4 | 2011-01-25 11:18:14 +0100 | [diff] [blame] | 3308 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 3309 | if (base->lcla_pool.base && base->plat_data->use_esram_lcla) { |
| 3310 | iounmap(base->lcla_pool.base); |
| 3311 | base->lcla_pool.base = NULL; |
| 3312 | } |
| 3313 | |
Rabin Vincent | 026cbc4 | 2011-01-25 11:18:14 +0100 | [diff] [blame] | 3314 | if (base->lcla_pool.dma_addr) |
| 3315 | dma_unmap_single(base->dev, base->lcla_pool.dma_addr, |
| 3316 | SZ_1K * base->num_phy_chans, |
| 3317 | DMA_TO_DEVICE); |
| 3318 | |
Linus Walleij | 508849a | 2010-06-20 21:26:07 +0000 | [diff] [blame] | 3319 | if (!base->lcla_pool.base_unaligned && base->lcla_pool.base) |
| 3320 | free_pages((unsigned long)base->lcla_pool.base, |
| 3321 | base->lcla_pool.pages); |
Jonas Aaberg | 767a967 | 2010-08-09 12:08:34 +0000 | [diff] [blame] | 3322 | |
| 3323 | kfree(base->lcla_pool.base_unaligned); |
| 3324 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3325 | if (base->phy_lcpa) |
| 3326 | release_mem_region(base->phy_lcpa, |
| 3327 | base->lcpa_size); |
| 3328 | if (base->phy_start) |
| 3329 | release_mem_region(base->phy_start, |
| 3330 | base->phy_size); |
| 3331 | if (base->clk) { |
| 3332 | clk_disable(base->clk); |
| 3333 | clk_put(base->clk); |
| 3334 | } |
| 3335 | |
Narayanan G | 28c7a19 | 2011-11-22 13:56:55 +0530 | [diff] [blame] | 3336 | if (base->lcpa_regulator) { |
| 3337 | regulator_disable(base->lcpa_regulator); |
| 3338 | regulator_put(base->lcpa_regulator); |
| 3339 | } |
| 3340 | |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3341 | kfree(base->lcla_pool.alloc_map); |
| 3342 | kfree(base->lookup_log_chans); |
| 3343 | kfree(base->lookup_phy_chans); |
| 3344 | kfree(base->phy_res); |
| 3345 | kfree(base); |
| 3346 | } |
| 3347 | |
Rabin Vincent | 6db5a8b | 2011-01-25 11:18:09 +0100 | [diff] [blame] | 3348 | d40_err(&pdev->dev, "probe failed\n"); |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3349 | return ret; |
| 3350 | } |
| 3351 | |
| 3352 | static struct platform_driver d40_driver = { |
| 3353 | .driver = { |
| 3354 | .owner = THIS_MODULE, |
| 3355 | .name = D40_NAME, |
Narayanan G | 7fb3e75 | 2011-11-17 17:26:41 +0530 | [diff] [blame] | 3356 | .pm = DMA40_PM_OPS, |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3357 | }, |
| 3358 | }; |
| 3359 | |
Rabin Vincent | cb9ab2d | 2011-01-25 11:18:04 +0100 | [diff] [blame] | 3360 | static int __init stedma40_init(void) |
Linus Walleij | 8d318a5 | 2010-03-30 15:33:42 +0200 | [diff] [blame] | 3361 | { |
| 3362 | return platform_driver_probe(&d40_driver, d40_probe); |
| 3363 | } |
Linus Walleij | a0eb221 | 2011-05-18 14:18:57 +0200 | [diff] [blame] | 3364 | subsys_initcall(stedma40_init); |