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