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