Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 3 | * http://www.samsung.com |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2010 Samsung Electronics Co. Ltd. |
| 6 | * Jaswinder Singh <jassi.brar@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/module.h> |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 19 | #include <linux/string.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/dma-mapping.h> |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 23 | #include <linux/dmaengine.h> |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 24 | #include <linux/amba/bus.h> |
| 25 | #include <linux/amba/pl330.h> |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 26 | #include <linux/scatterlist.h> |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 27 | #include <linux/of.h> |
Padmavathi Venna | a80258f | 2013-02-14 09:10:06 +0530 | [diff] [blame] | 28 | #include <linux/of_dma.h> |
Sachin Kamat | bcc7fa9 | 2013-03-04 14:36:27 +0530 | [diff] [blame] | 29 | #include <linux/err.h> |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 30 | |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 31 | #include "dmaengine.h" |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 32 | #define PL330_MAX_CHAN 8 |
| 33 | #define PL330_MAX_IRQS 32 |
| 34 | #define PL330_MAX_PERI 32 |
| 35 | |
Lars-Peter Clausen | f0564c7 | 2014-07-06 20:32:19 +0200 | [diff] [blame] | 36 | enum pl330_cachectrl { |
| 37 | CCTRL0, /* Noncacheable and nonbufferable */ |
| 38 | CCTRL1, /* Bufferable only */ |
| 39 | CCTRL2, /* Cacheable, but do not allocate */ |
| 40 | CCTRL3, /* Cacheable and bufferable, but do not allocate */ |
| 41 | INVALID1, /* AWCACHE = 0x1000 */ |
| 42 | INVALID2, |
| 43 | CCTRL6, /* Cacheable write-through, allocate on writes only */ |
| 44 | CCTRL7, /* Cacheable write-back, allocate on writes only */ |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | enum pl330_byteswap { |
| 48 | SWAP_NO, |
| 49 | SWAP_2, |
| 50 | SWAP_4, |
| 51 | SWAP_8, |
| 52 | SWAP_16, |
| 53 | }; |
| 54 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 55 | /* Register and Bit field Definitions */ |
| 56 | #define DS 0x0 |
| 57 | #define DS_ST_STOP 0x0 |
| 58 | #define DS_ST_EXEC 0x1 |
| 59 | #define DS_ST_CMISS 0x2 |
| 60 | #define DS_ST_UPDTPC 0x3 |
| 61 | #define DS_ST_WFE 0x4 |
| 62 | #define DS_ST_ATBRR 0x5 |
| 63 | #define DS_ST_QBUSY 0x6 |
| 64 | #define DS_ST_WFP 0x7 |
| 65 | #define DS_ST_KILL 0x8 |
| 66 | #define DS_ST_CMPLT 0x9 |
| 67 | #define DS_ST_FLTCMP 0xe |
| 68 | #define DS_ST_FAULT 0xf |
| 69 | |
| 70 | #define DPC 0x4 |
| 71 | #define INTEN 0x20 |
| 72 | #define ES 0x24 |
| 73 | #define INTSTATUS 0x28 |
| 74 | #define INTCLR 0x2c |
| 75 | #define FSM 0x30 |
| 76 | #define FSC 0x34 |
| 77 | #define FTM 0x38 |
| 78 | |
| 79 | #define _FTC 0x40 |
| 80 | #define FTC(n) (_FTC + (n)*0x4) |
| 81 | |
| 82 | #define _CS 0x100 |
| 83 | #define CS(n) (_CS + (n)*0x8) |
| 84 | #define CS_CNS (1 << 21) |
| 85 | |
| 86 | #define _CPC 0x104 |
| 87 | #define CPC(n) (_CPC + (n)*0x8) |
| 88 | |
| 89 | #define _SA 0x400 |
| 90 | #define SA(n) (_SA + (n)*0x20) |
| 91 | |
| 92 | #define _DA 0x404 |
| 93 | #define DA(n) (_DA + (n)*0x20) |
| 94 | |
| 95 | #define _CC 0x408 |
| 96 | #define CC(n) (_CC + (n)*0x20) |
| 97 | |
| 98 | #define CC_SRCINC (1 << 0) |
| 99 | #define CC_DSTINC (1 << 14) |
| 100 | #define CC_SRCPRI (1 << 8) |
| 101 | #define CC_DSTPRI (1 << 22) |
| 102 | #define CC_SRCNS (1 << 9) |
| 103 | #define CC_DSTNS (1 << 23) |
| 104 | #define CC_SRCIA (1 << 10) |
| 105 | #define CC_DSTIA (1 << 24) |
| 106 | #define CC_SRCBRSTLEN_SHFT 4 |
| 107 | #define CC_DSTBRSTLEN_SHFT 18 |
| 108 | #define CC_SRCBRSTSIZE_SHFT 1 |
| 109 | #define CC_DSTBRSTSIZE_SHFT 15 |
| 110 | #define CC_SRCCCTRL_SHFT 11 |
| 111 | #define CC_SRCCCTRL_MASK 0x7 |
| 112 | #define CC_DSTCCTRL_SHFT 25 |
| 113 | #define CC_DRCCCTRL_MASK 0x7 |
| 114 | #define CC_SWAP_SHFT 28 |
| 115 | |
| 116 | #define _LC0 0x40c |
| 117 | #define LC0(n) (_LC0 + (n)*0x20) |
| 118 | |
| 119 | #define _LC1 0x410 |
| 120 | #define LC1(n) (_LC1 + (n)*0x20) |
| 121 | |
| 122 | #define DBGSTATUS 0xd00 |
| 123 | #define DBG_BUSY (1 << 0) |
| 124 | |
| 125 | #define DBGCMD 0xd04 |
| 126 | #define DBGINST0 0xd08 |
| 127 | #define DBGINST1 0xd0c |
| 128 | |
| 129 | #define CR0 0xe00 |
| 130 | #define CR1 0xe04 |
| 131 | #define CR2 0xe08 |
| 132 | #define CR3 0xe0c |
| 133 | #define CR4 0xe10 |
| 134 | #define CRD 0xe14 |
| 135 | |
| 136 | #define PERIPH_ID 0xfe0 |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 137 | #define PERIPH_REV_SHIFT 20 |
| 138 | #define PERIPH_REV_MASK 0xf |
| 139 | #define PERIPH_REV_R0P0 0 |
| 140 | #define PERIPH_REV_R1P0 1 |
| 141 | #define PERIPH_REV_R1P1 2 |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 142 | |
| 143 | #define CR0_PERIPH_REQ_SET (1 << 0) |
| 144 | #define CR0_BOOT_EN_SET (1 << 1) |
| 145 | #define CR0_BOOT_MAN_NS (1 << 2) |
| 146 | #define CR0_NUM_CHANS_SHIFT 4 |
| 147 | #define CR0_NUM_CHANS_MASK 0x7 |
| 148 | #define CR0_NUM_PERIPH_SHIFT 12 |
| 149 | #define CR0_NUM_PERIPH_MASK 0x1f |
| 150 | #define CR0_NUM_EVENTS_SHIFT 17 |
| 151 | #define CR0_NUM_EVENTS_MASK 0x1f |
| 152 | |
| 153 | #define CR1_ICACHE_LEN_SHIFT 0 |
| 154 | #define CR1_ICACHE_LEN_MASK 0x7 |
| 155 | #define CR1_NUM_ICACHELINES_SHIFT 4 |
| 156 | #define CR1_NUM_ICACHELINES_MASK 0xf |
| 157 | |
| 158 | #define CRD_DATA_WIDTH_SHIFT 0 |
| 159 | #define CRD_DATA_WIDTH_MASK 0x7 |
| 160 | #define CRD_WR_CAP_SHIFT 4 |
| 161 | #define CRD_WR_CAP_MASK 0x7 |
| 162 | #define CRD_WR_Q_DEP_SHIFT 8 |
| 163 | #define CRD_WR_Q_DEP_MASK 0xf |
| 164 | #define CRD_RD_CAP_SHIFT 12 |
| 165 | #define CRD_RD_CAP_MASK 0x7 |
| 166 | #define CRD_RD_Q_DEP_SHIFT 16 |
| 167 | #define CRD_RD_Q_DEP_MASK 0xf |
| 168 | #define CRD_DATA_BUFF_SHIFT 20 |
| 169 | #define CRD_DATA_BUFF_MASK 0x3ff |
| 170 | |
| 171 | #define PART 0x330 |
| 172 | #define DESIGNER 0x41 |
| 173 | #define REVISION 0x0 |
| 174 | #define INTEG_CFG 0x0 |
| 175 | #define PERIPH_ID_VAL ((PART << 0) | (DESIGNER << 12)) |
| 176 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 177 | #define PL330_STATE_STOPPED (1 << 0) |
| 178 | #define PL330_STATE_EXECUTING (1 << 1) |
| 179 | #define PL330_STATE_WFE (1 << 2) |
| 180 | #define PL330_STATE_FAULTING (1 << 3) |
| 181 | #define PL330_STATE_COMPLETING (1 << 4) |
| 182 | #define PL330_STATE_WFP (1 << 5) |
| 183 | #define PL330_STATE_KILLING (1 << 6) |
| 184 | #define PL330_STATE_FAULT_COMPLETING (1 << 7) |
| 185 | #define PL330_STATE_CACHEMISS (1 << 8) |
| 186 | #define PL330_STATE_UPDTPC (1 << 9) |
| 187 | #define PL330_STATE_ATBARRIER (1 << 10) |
| 188 | #define PL330_STATE_QUEUEBUSY (1 << 11) |
| 189 | #define PL330_STATE_INVALID (1 << 15) |
| 190 | |
| 191 | #define PL330_STABLE_STATES (PL330_STATE_STOPPED | PL330_STATE_EXECUTING \ |
| 192 | | PL330_STATE_WFE | PL330_STATE_FAULTING) |
| 193 | |
| 194 | #define CMD_DMAADDH 0x54 |
| 195 | #define CMD_DMAEND 0x00 |
| 196 | #define CMD_DMAFLUSHP 0x35 |
| 197 | #define CMD_DMAGO 0xa0 |
| 198 | #define CMD_DMALD 0x04 |
| 199 | #define CMD_DMALDP 0x25 |
| 200 | #define CMD_DMALP 0x20 |
| 201 | #define CMD_DMALPEND 0x28 |
| 202 | #define CMD_DMAKILL 0x01 |
| 203 | #define CMD_DMAMOV 0xbc |
| 204 | #define CMD_DMANOP 0x18 |
| 205 | #define CMD_DMARMB 0x12 |
| 206 | #define CMD_DMASEV 0x34 |
| 207 | #define CMD_DMAST 0x08 |
| 208 | #define CMD_DMASTP 0x29 |
| 209 | #define CMD_DMASTZ 0x0c |
| 210 | #define CMD_DMAWFE 0x36 |
| 211 | #define CMD_DMAWFP 0x30 |
| 212 | #define CMD_DMAWMB 0x13 |
| 213 | |
| 214 | #define SZ_DMAADDH 3 |
| 215 | #define SZ_DMAEND 1 |
| 216 | #define SZ_DMAFLUSHP 2 |
| 217 | #define SZ_DMALD 1 |
| 218 | #define SZ_DMALDP 2 |
| 219 | #define SZ_DMALP 2 |
| 220 | #define SZ_DMALPEND 2 |
| 221 | #define SZ_DMAKILL 1 |
| 222 | #define SZ_DMAMOV 6 |
| 223 | #define SZ_DMANOP 1 |
| 224 | #define SZ_DMARMB 1 |
| 225 | #define SZ_DMASEV 2 |
| 226 | #define SZ_DMAST 1 |
| 227 | #define SZ_DMASTP 2 |
| 228 | #define SZ_DMASTZ 1 |
| 229 | #define SZ_DMAWFE 2 |
| 230 | #define SZ_DMAWFP 2 |
| 231 | #define SZ_DMAWMB 1 |
| 232 | #define SZ_DMAGO 6 |
| 233 | |
| 234 | #define BRST_LEN(ccr) ((((ccr) >> CC_SRCBRSTLEN_SHFT) & 0xf) + 1) |
| 235 | #define BRST_SIZE(ccr) (1 << (((ccr) >> CC_SRCBRSTSIZE_SHFT) & 0x7)) |
| 236 | |
| 237 | #define BYTE_TO_BURST(b, ccr) ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr)) |
| 238 | #define BURST_TO_BYTE(c, ccr) ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr)) |
| 239 | |
| 240 | /* |
| 241 | * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req |
| 242 | * at 1byte/burst for P<->M and M<->M respectively. |
| 243 | * For typical scenario, at 1word/burst, 10MB and 20MB xfers per req |
| 244 | * should be enough for P<->M and M<->M respectively. |
| 245 | */ |
| 246 | #define MCODE_BUFF_PER_REQ 256 |
| 247 | |
| 248 | /* If the _pl330_req is available to the client */ |
| 249 | #define IS_FREE(req) (*((u8 *)((req)->mc_cpu)) == CMD_DMAEND) |
| 250 | |
| 251 | /* Use this _only_ to wait on transient states */ |
| 252 | #define UNTIL(t, s) while (!(_state(t) & (s))) cpu_relax(); |
| 253 | |
| 254 | #ifdef PL330_DEBUG_MCGEN |
| 255 | static unsigned cmd_line; |
| 256 | #define PL330_DBGCMD_DUMP(off, x...) do { \ |
| 257 | printk("%x:", cmd_line); \ |
| 258 | printk(x); \ |
| 259 | cmd_line += off; \ |
| 260 | } while (0) |
| 261 | #define PL330_DBGMC_START(addr) (cmd_line = addr) |
| 262 | #else |
| 263 | #define PL330_DBGCMD_DUMP(off, x...) do {} while (0) |
| 264 | #define PL330_DBGMC_START(addr) do {} while (0) |
| 265 | #endif |
| 266 | |
| 267 | /* The number of default descriptors */ |
Russell King - ARM Linux | d2ebfb3 | 2012-03-06 22:34:26 +0000 | [diff] [blame] | 268 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 269 | #define NR_DEFAULT_DESC 16 |
| 270 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 271 | /* Populated by the PL330 core driver for DMA API driver's info */ |
| 272 | struct pl330_config { |
| 273 | u32 periph_id; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 274 | #define DMAC_MODE_NS (1 << 0) |
| 275 | unsigned int mode; |
| 276 | unsigned int data_bus_width:10; /* In number of bits */ |
| 277 | unsigned int data_buf_dep:10; |
| 278 | unsigned int num_chan:4; |
| 279 | unsigned int num_peri:6; |
| 280 | u32 peri_ns; |
| 281 | unsigned int num_events:6; |
| 282 | u32 irq_ns; |
| 283 | }; |
| 284 | |
| 285 | /* Handle to the DMAC provided to the PL330 core */ |
| 286 | struct pl330_info { |
| 287 | /* Owning device */ |
| 288 | struct device *dev; |
| 289 | /* Size of MicroCode buffers for each channel. */ |
| 290 | unsigned mcbufsz; |
| 291 | /* ioremap'ed address of PL330 registers. */ |
| 292 | void __iomem *base; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 293 | /* PL330 core data, Client must not touch it. */ |
| 294 | void *pl330_data; |
| 295 | /* Populated by the PL330 core driver during pl330_add */ |
| 296 | struct pl330_config pcfg; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | /** |
| 300 | * Request Configuration. |
| 301 | * The PL330 core does not modify this and uses the last |
| 302 | * working configuration if the request doesn't provide any. |
| 303 | * |
| 304 | * The Client may want to provide this info only for the |
| 305 | * first request and a request with new settings. |
| 306 | */ |
| 307 | struct pl330_reqcfg { |
| 308 | /* Address Incrementing */ |
| 309 | unsigned dst_inc:1; |
| 310 | unsigned src_inc:1; |
| 311 | |
| 312 | /* |
| 313 | * For now, the SRC & DST protection levels |
| 314 | * and burst size/length are assumed same. |
| 315 | */ |
| 316 | bool nonsecure; |
| 317 | bool privileged; |
| 318 | bool insnaccess; |
| 319 | unsigned brst_len:5; |
| 320 | unsigned brst_size:3; /* in power of 2 */ |
| 321 | |
Lars-Peter Clausen | f0564c7 | 2014-07-06 20:32:19 +0200 | [diff] [blame] | 322 | enum pl330_cachectrl dcctl; |
| 323 | enum pl330_cachectrl scctl; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 324 | enum pl330_byteswap swap; |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 325 | struct pl330_config *pcfg; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 326 | }; |
| 327 | |
| 328 | /* |
| 329 | * One cycle of DMAC operation. |
| 330 | * There may be more than one xfer in a request. |
| 331 | */ |
| 332 | struct pl330_xfer { |
| 333 | u32 src_addr; |
| 334 | u32 dst_addr; |
| 335 | /* Size to xfer */ |
| 336 | u32 bytes; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | /* The xfer callbacks are made with one of these arguments. */ |
| 340 | enum pl330_op_err { |
| 341 | /* The all xfers in the request were success. */ |
| 342 | PL330_ERR_NONE, |
| 343 | /* If req aborted due to global error. */ |
| 344 | PL330_ERR_ABORT, |
| 345 | /* If req failed due to problem with Channel. */ |
| 346 | PL330_ERR_FAIL, |
| 347 | }; |
| 348 | |
| 349 | /* A request defining Scatter-Gather List ending with NULL xfer. */ |
| 350 | struct pl330_req { |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 351 | enum dma_transfer_direction rqtype; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 352 | /* Index of peripheral for the xfer. */ |
| 353 | unsigned peri:5; |
| 354 | /* Unique token for this xfer, set by the client. */ |
| 355 | void *token; |
| 356 | /* Callback to be called after xfer. */ |
| 357 | void (*xfer_cb)(void *token, enum pl330_op_err err); |
| 358 | /* If NULL, req will be done at last set parameters. */ |
| 359 | struct pl330_reqcfg *cfg; |
| 360 | /* Pointer to first xfer in the request. */ |
| 361 | struct pl330_xfer *x; |
Javi Merino | fdec53d | 2012-06-13 15:07:00 +0100 | [diff] [blame] | 362 | /* Hook to attach to DMAC's list of reqs with due callback */ |
| 363 | struct list_head rqd; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 364 | }; |
| 365 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 366 | enum pl330_chan_op { |
| 367 | /* Start the channel */ |
| 368 | PL330_OP_START, |
| 369 | /* Abort the active xfer */ |
| 370 | PL330_OP_ABORT, |
| 371 | /* Stop xfer and flush queue */ |
| 372 | PL330_OP_FLUSH, |
| 373 | }; |
| 374 | |
| 375 | struct _xfer_spec { |
| 376 | u32 ccr; |
| 377 | struct pl330_req *r; |
| 378 | struct pl330_xfer *x; |
| 379 | }; |
| 380 | |
| 381 | enum dmamov_dst { |
| 382 | SAR = 0, |
| 383 | CCR, |
| 384 | DAR, |
| 385 | }; |
| 386 | |
| 387 | enum pl330_dst { |
| 388 | SRC = 0, |
| 389 | DST, |
| 390 | }; |
| 391 | |
| 392 | enum pl330_cond { |
| 393 | SINGLE, |
| 394 | BURST, |
| 395 | ALWAYS, |
| 396 | }; |
| 397 | |
| 398 | struct _pl330_req { |
| 399 | u32 mc_bus; |
| 400 | void *mc_cpu; |
| 401 | /* Number of bytes taken to setup MC for the req */ |
| 402 | u32 mc_len; |
| 403 | struct pl330_req *r; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 404 | }; |
| 405 | |
| 406 | /* ToBeDone for tasklet */ |
| 407 | struct _pl330_tbd { |
| 408 | bool reset_dmac; |
| 409 | bool reset_mngr; |
| 410 | u8 reset_chan; |
| 411 | }; |
| 412 | |
| 413 | /* A DMAC Thread */ |
| 414 | struct pl330_thread { |
| 415 | u8 id; |
| 416 | int ev; |
| 417 | /* If the channel is not yet acquired by any client */ |
| 418 | bool free; |
| 419 | /* Parent DMAC */ |
| 420 | struct pl330_dmac *dmac; |
| 421 | /* Only two at a time */ |
| 422 | struct _pl330_req req[2]; |
| 423 | /* Index of the last enqueued request */ |
| 424 | unsigned lstenq; |
| 425 | /* Index of the last submitted request or -1 if the DMA is stopped */ |
| 426 | int req_running; |
| 427 | }; |
| 428 | |
| 429 | enum pl330_dmac_state { |
| 430 | UNINIT, |
| 431 | INIT, |
| 432 | DYING, |
| 433 | }; |
| 434 | |
| 435 | /* A DMAC */ |
| 436 | struct pl330_dmac { |
| 437 | spinlock_t lock; |
| 438 | /* Holds list of reqs with due callbacks */ |
| 439 | struct list_head req_done; |
| 440 | /* Pointer to platform specific stuff */ |
| 441 | struct pl330_info *pinfo; |
| 442 | /* Maximum possible events/irqs */ |
| 443 | int events[32]; |
| 444 | /* BUS address of MicroCode buffer */ |
Will Deacon | fed8c45 | 2013-06-10 19:34:38 +0100 | [diff] [blame] | 445 | dma_addr_t mcode_bus; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 446 | /* CPU address of MicroCode buffer */ |
| 447 | void *mcode_cpu; |
| 448 | /* List of all Channel threads */ |
| 449 | struct pl330_thread *channels; |
| 450 | /* Pointer to the MANAGER thread */ |
| 451 | struct pl330_thread *manager; |
| 452 | /* To handle bad news in interrupt */ |
| 453 | struct tasklet_struct tasks; |
| 454 | struct _pl330_tbd dmac_tbd; |
| 455 | /* State of DMAC operation */ |
| 456 | enum pl330_dmac_state state; |
| 457 | }; |
| 458 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 459 | enum desc_status { |
| 460 | /* In the DMAC pool */ |
| 461 | FREE, |
| 462 | /* |
Masanari Iida | d73111c | 2012-08-04 23:37:53 +0900 | [diff] [blame] | 463 | * Allocated to some channel during prep_xxx |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 464 | * Also may be sitting on the work_list. |
| 465 | */ |
| 466 | PREP, |
| 467 | /* |
| 468 | * Sitting on the work_list and already submitted |
| 469 | * to the PL330 core. Not more than two descriptors |
| 470 | * of a channel can be BUSY at any time. |
| 471 | */ |
| 472 | BUSY, |
| 473 | /* |
| 474 | * Sitting on the channel work_list but xfer done |
| 475 | * by PL330 core |
| 476 | */ |
| 477 | DONE, |
| 478 | }; |
| 479 | |
| 480 | struct dma_pl330_chan { |
| 481 | /* Schedule desc completion */ |
| 482 | struct tasklet_struct task; |
| 483 | |
| 484 | /* DMA-Engine Channel */ |
| 485 | struct dma_chan chan; |
| 486 | |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 487 | /* List of submitted descriptors */ |
| 488 | struct list_head submitted_list; |
| 489 | /* List of issued descriptors */ |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 490 | struct list_head work_list; |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 491 | /* List of completed descriptors */ |
| 492 | struct list_head completed_list; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 493 | |
| 494 | /* Pointer to the DMAC that manages this channel, |
| 495 | * NULL if the channel is available to be acquired. |
| 496 | * As the parent, this DMAC also provides descriptors |
| 497 | * to the channel. |
| 498 | */ |
| 499 | struct dma_pl330_dmac *dmac; |
| 500 | |
| 501 | /* To protect channel manipulation */ |
| 502 | spinlock_t lock; |
| 503 | |
| 504 | /* Token of a hardware channel thread of PL330 DMAC |
| 505 | * NULL if the channel is available to be acquired. |
| 506 | */ |
| 507 | void *pl330_chid; |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 508 | |
| 509 | /* For D-to-M and M-to-D channels */ |
| 510 | int burst_sz; /* the peripheral fifo width */ |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 511 | int burst_len; /* the number of burst */ |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 512 | dma_addr_t fifo_addr; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 513 | |
| 514 | /* for cyclic capability */ |
| 515 | bool cyclic; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | struct dma_pl330_dmac { |
| 519 | struct pl330_info pif; |
| 520 | |
| 521 | /* DMA-Engine Device */ |
| 522 | struct dma_device ddma; |
| 523 | |
Lars-Peter Clausen | b714b84 | 2013-11-25 16:07:46 +0100 | [diff] [blame] | 524 | /* Holds info about sg limitations */ |
| 525 | struct device_dma_parameters dma_parms; |
| 526 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 527 | /* Pool of descriptors available for the DMAC's channels */ |
| 528 | struct list_head desc_pool; |
| 529 | /* To protect desc_pool manipulation */ |
| 530 | spinlock_t pool_lock; |
| 531 | |
| 532 | /* Peripheral channels connected to this DMAC */ |
Lars-Peter Clausen | 70cbb16 | 2014-01-11 20:08:39 +0100 | [diff] [blame] | 533 | unsigned int num_peripherals; |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 534 | struct dma_pl330_chan *peripherals; /* keep at end */ |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
| 537 | struct dma_pl330_desc { |
| 538 | /* To attach to a queue as child */ |
| 539 | struct list_head node; |
| 540 | |
| 541 | /* Descriptor for the DMA Engine API */ |
| 542 | struct dma_async_tx_descriptor txd; |
| 543 | |
| 544 | /* Xfer for PL330 core */ |
| 545 | struct pl330_xfer px; |
| 546 | |
| 547 | struct pl330_reqcfg rqcfg; |
| 548 | struct pl330_req req; |
| 549 | |
| 550 | enum desc_status status; |
| 551 | |
| 552 | /* The channel which currently holds this desc */ |
| 553 | struct dma_pl330_chan *pchan; |
| 554 | }; |
| 555 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 556 | static inline void _callback(struct pl330_req *r, enum pl330_op_err err) |
| 557 | { |
| 558 | if (r && r->xfer_cb) |
| 559 | r->xfer_cb(r->token, err); |
| 560 | } |
| 561 | |
| 562 | static inline bool _queue_empty(struct pl330_thread *thrd) |
| 563 | { |
| 564 | return (IS_FREE(&thrd->req[0]) && IS_FREE(&thrd->req[1])) |
| 565 | ? true : false; |
| 566 | } |
| 567 | |
| 568 | static inline bool _queue_full(struct pl330_thread *thrd) |
| 569 | { |
| 570 | return (IS_FREE(&thrd->req[0]) || IS_FREE(&thrd->req[1])) |
| 571 | ? false : true; |
| 572 | } |
| 573 | |
| 574 | static inline bool is_manager(struct pl330_thread *thrd) |
| 575 | { |
| 576 | struct pl330_dmac *pl330 = thrd->dmac; |
| 577 | |
| 578 | /* MANAGER is indexed at the end */ |
| 579 | if (thrd->id == pl330->pinfo->pcfg.num_chan) |
| 580 | return true; |
| 581 | else |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | /* If manager of the thread is in Non-Secure mode */ |
| 586 | static inline bool _manager_ns(struct pl330_thread *thrd) |
| 587 | { |
| 588 | struct pl330_dmac *pl330 = thrd->dmac; |
| 589 | |
| 590 | return (pl330->pinfo->pcfg.mode & DMAC_MODE_NS) ? true : false; |
| 591 | } |
| 592 | |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 593 | static inline u32 get_revision(u32 periph_id) |
| 594 | { |
| 595 | return (periph_id >> PERIPH_REV_SHIFT) & PERIPH_REV_MASK; |
| 596 | } |
| 597 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 598 | static inline u32 _emit_ADDH(unsigned dry_run, u8 buf[], |
| 599 | enum pl330_dst da, u16 val) |
| 600 | { |
| 601 | if (dry_run) |
| 602 | return SZ_DMAADDH; |
| 603 | |
| 604 | buf[0] = CMD_DMAADDH; |
| 605 | buf[0] |= (da << 1); |
| 606 | *((u16 *)&buf[1]) = val; |
| 607 | |
| 608 | PL330_DBGCMD_DUMP(SZ_DMAADDH, "\tDMAADDH %s %u\n", |
| 609 | da == 1 ? "DA" : "SA", val); |
| 610 | |
| 611 | return SZ_DMAADDH; |
| 612 | } |
| 613 | |
| 614 | static inline u32 _emit_END(unsigned dry_run, u8 buf[]) |
| 615 | { |
| 616 | if (dry_run) |
| 617 | return SZ_DMAEND; |
| 618 | |
| 619 | buf[0] = CMD_DMAEND; |
| 620 | |
| 621 | PL330_DBGCMD_DUMP(SZ_DMAEND, "\tDMAEND\n"); |
| 622 | |
| 623 | return SZ_DMAEND; |
| 624 | } |
| 625 | |
| 626 | static inline u32 _emit_FLUSHP(unsigned dry_run, u8 buf[], u8 peri) |
| 627 | { |
| 628 | if (dry_run) |
| 629 | return SZ_DMAFLUSHP; |
| 630 | |
| 631 | buf[0] = CMD_DMAFLUSHP; |
| 632 | |
| 633 | peri &= 0x1f; |
| 634 | peri <<= 3; |
| 635 | buf[1] = peri; |
| 636 | |
| 637 | PL330_DBGCMD_DUMP(SZ_DMAFLUSHP, "\tDMAFLUSHP %u\n", peri >> 3); |
| 638 | |
| 639 | return SZ_DMAFLUSHP; |
| 640 | } |
| 641 | |
| 642 | static inline u32 _emit_LD(unsigned dry_run, u8 buf[], enum pl330_cond cond) |
| 643 | { |
| 644 | if (dry_run) |
| 645 | return SZ_DMALD; |
| 646 | |
| 647 | buf[0] = CMD_DMALD; |
| 648 | |
| 649 | if (cond == SINGLE) |
| 650 | buf[0] |= (0 << 1) | (1 << 0); |
| 651 | else if (cond == BURST) |
| 652 | buf[0] |= (1 << 1) | (1 << 0); |
| 653 | |
| 654 | PL330_DBGCMD_DUMP(SZ_DMALD, "\tDMALD%c\n", |
| 655 | cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A')); |
| 656 | |
| 657 | return SZ_DMALD; |
| 658 | } |
| 659 | |
| 660 | static inline u32 _emit_LDP(unsigned dry_run, u8 buf[], |
| 661 | enum pl330_cond cond, u8 peri) |
| 662 | { |
| 663 | if (dry_run) |
| 664 | return SZ_DMALDP; |
| 665 | |
| 666 | buf[0] = CMD_DMALDP; |
| 667 | |
| 668 | if (cond == BURST) |
| 669 | buf[0] |= (1 << 1); |
| 670 | |
| 671 | peri &= 0x1f; |
| 672 | peri <<= 3; |
| 673 | buf[1] = peri; |
| 674 | |
| 675 | PL330_DBGCMD_DUMP(SZ_DMALDP, "\tDMALDP%c %u\n", |
| 676 | cond == SINGLE ? 'S' : 'B', peri >> 3); |
| 677 | |
| 678 | return SZ_DMALDP; |
| 679 | } |
| 680 | |
| 681 | static inline u32 _emit_LP(unsigned dry_run, u8 buf[], |
| 682 | unsigned loop, u8 cnt) |
| 683 | { |
| 684 | if (dry_run) |
| 685 | return SZ_DMALP; |
| 686 | |
| 687 | buf[0] = CMD_DMALP; |
| 688 | |
| 689 | if (loop) |
| 690 | buf[0] |= (1 << 1); |
| 691 | |
| 692 | cnt--; /* DMAC increments by 1 internally */ |
| 693 | buf[1] = cnt; |
| 694 | |
| 695 | PL330_DBGCMD_DUMP(SZ_DMALP, "\tDMALP_%c %u\n", loop ? '1' : '0', cnt); |
| 696 | |
| 697 | return SZ_DMALP; |
| 698 | } |
| 699 | |
| 700 | struct _arg_LPEND { |
| 701 | enum pl330_cond cond; |
| 702 | bool forever; |
| 703 | unsigned loop; |
| 704 | u8 bjump; |
| 705 | }; |
| 706 | |
| 707 | static inline u32 _emit_LPEND(unsigned dry_run, u8 buf[], |
| 708 | const struct _arg_LPEND *arg) |
| 709 | { |
| 710 | enum pl330_cond cond = arg->cond; |
| 711 | bool forever = arg->forever; |
| 712 | unsigned loop = arg->loop; |
| 713 | u8 bjump = arg->bjump; |
| 714 | |
| 715 | if (dry_run) |
| 716 | return SZ_DMALPEND; |
| 717 | |
| 718 | buf[0] = CMD_DMALPEND; |
| 719 | |
| 720 | if (loop) |
| 721 | buf[0] |= (1 << 2); |
| 722 | |
| 723 | if (!forever) |
| 724 | buf[0] |= (1 << 4); |
| 725 | |
| 726 | if (cond == SINGLE) |
| 727 | buf[0] |= (0 << 1) | (1 << 0); |
| 728 | else if (cond == BURST) |
| 729 | buf[0] |= (1 << 1) | (1 << 0); |
| 730 | |
| 731 | buf[1] = bjump; |
| 732 | |
| 733 | PL330_DBGCMD_DUMP(SZ_DMALPEND, "\tDMALP%s%c_%c bjmpto_%x\n", |
| 734 | forever ? "FE" : "END", |
| 735 | cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A'), |
| 736 | loop ? '1' : '0', |
| 737 | bjump); |
| 738 | |
| 739 | return SZ_DMALPEND; |
| 740 | } |
| 741 | |
| 742 | static inline u32 _emit_KILL(unsigned dry_run, u8 buf[]) |
| 743 | { |
| 744 | if (dry_run) |
| 745 | return SZ_DMAKILL; |
| 746 | |
| 747 | buf[0] = CMD_DMAKILL; |
| 748 | |
| 749 | return SZ_DMAKILL; |
| 750 | } |
| 751 | |
| 752 | static inline u32 _emit_MOV(unsigned dry_run, u8 buf[], |
| 753 | enum dmamov_dst dst, u32 val) |
| 754 | { |
| 755 | if (dry_run) |
| 756 | return SZ_DMAMOV; |
| 757 | |
| 758 | buf[0] = CMD_DMAMOV; |
| 759 | buf[1] = dst; |
| 760 | *((u32 *)&buf[2]) = val; |
| 761 | |
| 762 | PL330_DBGCMD_DUMP(SZ_DMAMOV, "\tDMAMOV %s 0x%x\n", |
| 763 | dst == SAR ? "SAR" : (dst == DAR ? "DAR" : "CCR"), val); |
| 764 | |
| 765 | return SZ_DMAMOV; |
| 766 | } |
| 767 | |
| 768 | static inline u32 _emit_NOP(unsigned dry_run, u8 buf[]) |
| 769 | { |
| 770 | if (dry_run) |
| 771 | return SZ_DMANOP; |
| 772 | |
| 773 | buf[0] = CMD_DMANOP; |
| 774 | |
| 775 | PL330_DBGCMD_DUMP(SZ_DMANOP, "\tDMANOP\n"); |
| 776 | |
| 777 | return SZ_DMANOP; |
| 778 | } |
| 779 | |
| 780 | static inline u32 _emit_RMB(unsigned dry_run, u8 buf[]) |
| 781 | { |
| 782 | if (dry_run) |
| 783 | return SZ_DMARMB; |
| 784 | |
| 785 | buf[0] = CMD_DMARMB; |
| 786 | |
| 787 | PL330_DBGCMD_DUMP(SZ_DMARMB, "\tDMARMB\n"); |
| 788 | |
| 789 | return SZ_DMARMB; |
| 790 | } |
| 791 | |
| 792 | static inline u32 _emit_SEV(unsigned dry_run, u8 buf[], u8 ev) |
| 793 | { |
| 794 | if (dry_run) |
| 795 | return SZ_DMASEV; |
| 796 | |
| 797 | buf[0] = CMD_DMASEV; |
| 798 | |
| 799 | ev &= 0x1f; |
| 800 | ev <<= 3; |
| 801 | buf[1] = ev; |
| 802 | |
| 803 | PL330_DBGCMD_DUMP(SZ_DMASEV, "\tDMASEV %u\n", ev >> 3); |
| 804 | |
| 805 | return SZ_DMASEV; |
| 806 | } |
| 807 | |
| 808 | static inline u32 _emit_ST(unsigned dry_run, u8 buf[], enum pl330_cond cond) |
| 809 | { |
| 810 | if (dry_run) |
| 811 | return SZ_DMAST; |
| 812 | |
| 813 | buf[0] = CMD_DMAST; |
| 814 | |
| 815 | if (cond == SINGLE) |
| 816 | buf[0] |= (0 << 1) | (1 << 0); |
| 817 | else if (cond == BURST) |
| 818 | buf[0] |= (1 << 1) | (1 << 0); |
| 819 | |
| 820 | PL330_DBGCMD_DUMP(SZ_DMAST, "\tDMAST%c\n", |
| 821 | cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'A')); |
| 822 | |
| 823 | return SZ_DMAST; |
| 824 | } |
| 825 | |
| 826 | static inline u32 _emit_STP(unsigned dry_run, u8 buf[], |
| 827 | enum pl330_cond cond, u8 peri) |
| 828 | { |
| 829 | if (dry_run) |
| 830 | return SZ_DMASTP; |
| 831 | |
| 832 | buf[0] = CMD_DMASTP; |
| 833 | |
| 834 | if (cond == BURST) |
| 835 | buf[0] |= (1 << 1); |
| 836 | |
| 837 | peri &= 0x1f; |
| 838 | peri <<= 3; |
| 839 | buf[1] = peri; |
| 840 | |
| 841 | PL330_DBGCMD_DUMP(SZ_DMASTP, "\tDMASTP%c %u\n", |
| 842 | cond == SINGLE ? 'S' : 'B', peri >> 3); |
| 843 | |
| 844 | return SZ_DMASTP; |
| 845 | } |
| 846 | |
| 847 | static inline u32 _emit_STZ(unsigned dry_run, u8 buf[]) |
| 848 | { |
| 849 | if (dry_run) |
| 850 | return SZ_DMASTZ; |
| 851 | |
| 852 | buf[0] = CMD_DMASTZ; |
| 853 | |
| 854 | PL330_DBGCMD_DUMP(SZ_DMASTZ, "\tDMASTZ\n"); |
| 855 | |
| 856 | return SZ_DMASTZ; |
| 857 | } |
| 858 | |
| 859 | static inline u32 _emit_WFE(unsigned dry_run, u8 buf[], u8 ev, |
| 860 | unsigned invalidate) |
| 861 | { |
| 862 | if (dry_run) |
| 863 | return SZ_DMAWFE; |
| 864 | |
| 865 | buf[0] = CMD_DMAWFE; |
| 866 | |
| 867 | ev &= 0x1f; |
| 868 | ev <<= 3; |
| 869 | buf[1] = ev; |
| 870 | |
| 871 | if (invalidate) |
| 872 | buf[1] |= (1 << 1); |
| 873 | |
| 874 | PL330_DBGCMD_DUMP(SZ_DMAWFE, "\tDMAWFE %u%s\n", |
| 875 | ev >> 3, invalidate ? ", I" : ""); |
| 876 | |
| 877 | return SZ_DMAWFE; |
| 878 | } |
| 879 | |
| 880 | static inline u32 _emit_WFP(unsigned dry_run, u8 buf[], |
| 881 | enum pl330_cond cond, u8 peri) |
| 882 | { |
| 883 | if (dry_run) |
| 884 | return SZ_DMAWFP; |
| 885 | |
| 886 | buf[0] = CMD_DMAWFP; |
| 887 | |
| 888 | if (cond == SINGLE) |
| 889 | buf[0] |= (0 << 1) | (0 << 0); |
| 890 | else if (cond == BURST) |
| 891 | buf[0] |= (1 << 1) | (0 << 0); |
| 892 | else |
| 893 | buf[0] |= (0 << 1) | (1 << 0); |
| 894 | |
| 895 | peri &= 0x1f; |
| 896 | peri <<= 3; |
| 897 | buf[1] = peri; |
| 898 | |
| 899 | PL330_DBGCMD_DUMP(SZ_DMAWFP, "\tDMAWFP%c %u\n", |
| 900 | cond == SINGLE ? 'S' : (cond == BURST ? 'B' : 'P'), peri >> 3); |
| 901 | |
| 902 | return SZ_DMAWFP; |
| 903 | } |
| 904 | |
| 905 | static inline u32 _emit_WMB(unsigned dry_run, u8 buf[]) |
| 906 | { |
| 907 | if (dry_run) |
| 908 | return SZ_DMAWMB; |
| 909 | |
| 910 | buf[0] = CMD_DMAWMB; |
| 911 | |
| 912 | PL330_DBGCMD_DUMP(SZ_DMAWMB, "\tDMAWMB\n"); |
| 913 | |
| 914 | return SZ_DMAWMB; |
| 915 | } |
| 916 | |
| 917 | struct _arg_GO { |
| 918 | u8 chan; |
| 919 | u32 addr; |
| 920 | unsigned ns; |
| 921 | }; |
| 922 | |
| 923 | static inline u32 _emit_GO(unsigned dry_run, u8 buf[], |
| 924 | const struct _arg_GO *arg) |
| 925 | { |
| 926 | u8 chan = arg->chan; |
| 927 | u32 addr = arg->addr; |
| 928 | unsigned ns = arg->ns; |
| 929 | |
| 930 | if (dry_run) |
| 931 | return SZ_DMAGO; |
| 932 | |
| 933 | buf[0] = CMD_DMAGO; |
| 934 | buf[0] |= (ns << 1); |
| 935 | |
| 936 | buf[1] = chan & 0x7; |
| 937 | |
| 938 | *((u32 *)&buf[2]) = addr; |
| 939 | |
| 940 | return SZ_DMAGO; |
| 941 | } |
| 942 | |
| 943 | #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) |
| 944 | |
| 945 | /* Returns Time-Out */ |
| 946 | static bool _until_dmac_idle(struct pl330_thread *thrd) |
| 947 | { |
| 948 | void __iomem *regs = thrd->dmac->pinfo->base; |
| 949 | unsigned long loops = msecs_to_loops(5); |
| 950 | |
| 951 | do { |
| 952 | /* Until Manager is Idle */ |
| 953 | if (!(readl(regs + DBGSTATUS) & DBG_BUSY)) |
| 954 | break; |
| 955 | |
| 956 | cpu_relax(); |
| 957 | } while (--loops); |
| 958 | |
| 959 | if (!loops) |
| 960 | return true; |
| 961 | |
| 962 | return false; |
| 963 | } |
| 964 | |
| 965 | static inline void _execute_DBGINSN(struct pl330_thread *thrd, |
| 966 | u8 insn[], bool as_manager) |
| 967 | { |
| 968 | void __iomem *regs = thrd->dmac->pinfo->base; |
| 969 | u32 val; |
| 970 | |
| 971 | val = (insn[0] << 16) | (insn[1] << 24); |
| 972 | if (!as_manager) { |
| 973 | val |= (1 << 0); |
| 974 | val |= (thrd->id << 8); /* Channel Number */ |
| 975 | } |
| 976 | writel(val, regs + DBGINST0); |
| 977 | |
| 978 | val = *((u32 *)&insn[2]); |
| 979 | writel(val, regs + DBGINST1); |
| 980 | |
| 981 | /* If timed out due to halted state-machine */ |
| 982 | if (_until_dmac_idle(thrd)) { |
| 983 | dev_err(thrd->dmac->pinfo->dev, "DMAC halted!\n"); |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | /* Get going */ |
| 988 | writel(0, regs + DBGCMD); |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Mark a _pl330_req as free. |
| 993 | * We do it by writing DMAEND as the first instruction |
| 994 | * because no valid request is going to have DMAEND as |
| 995 | * its first instruction to execute. |
| 996 | */ |
| 997 | static void mark_free(struct pl330_thread *thrd, int idx) |
| 998 | { |
| 999 | struct _pl330_req *req = &thrd->req[idx]; |
| 1000 | |
| 1001 | _emit_END(0, req->mc_cpu); |
| 1002 | req->mc_len = 0; |
| 1003 | |
| 1004 | thrd->req_running = -1; |
| 1005 | } |
| 1006 | |
| 1007 | static inline u32 _state(struct pl330_thread *thrd) |
| 1008 | { |
| 1009 | void __iomem *regs = thrd->dmac->pinfo->base; |
| 1010 | u32 val; |
| 1011 | |
| 1012 | if (is_manager(thrd)) |
| 1013 | val = readl(regs + DS) & 0xf; |
| 1014 | else |
| 1015 | val = readl(regs + CS(thrd->id)) & 0xf; |
| 1016 | |
| 1017 | switch (val) { |
| 1018 | case DS_ST_STOP: |
| 1019 | return PL330_STATE_STOPPED; |
| 1020 | case DS_ST_EXEC: |
| 1021 | return PL330_STATE_EXECUTING; |
| 1022 | case DS_ST_CMISS: |
| 1023 | return PL330_STATE_CACHEMISS; |
| 1024 | case DS_ST_UPDTPC: |
| 1025 | return PL330_STATE_UPDTPC; |
| 1026 | case DS_ST_WFE: |
| 1027 | return PL330_STATE_WFE; |
| 1028 | case DS_ST_FAULT: |
| 1029 | return PL330_STATE_FAULTING; |
| 1030 | case DS_ST_ATBRR: |
| 1031 | if (is_manager(thrd)) |
| 1032 | return PL330_STATE_INVALID; |
| 1033 | else |
| 1034 | return PL330_STATE_ATBARRIER; |
| 1035 | case DS_ST_QBUSY: |
| 1036 | if (is_manager(thrd)) |
| 1037 | return PL330_STATE_INVALID; |
| 1038 | else |
| 1039 | return PL330_STATE_QUEUEBUSY; |
| 1040 | case DS_ST_WFP: |
| 1041 | if (is_manager(thrd)) |
| 1042 | return PL330_STATE_INVALID; |
| 1043 | else |
| 1044 | return PL330_STATE_WFP; |
| 1045 | case DS_ST_KILL: |
| 1046 | if (is_manager(thrd)) |
| 1047 | return PL330_STATE_INVALID; |
| 1048 | else |
| 1049 | return PL330_STATE_KILLING; |
| 1050 | case DS_ST_CMPLT: |
| 1051 | if (is_manager(thrd)) |
| 1052 | return PL330_STATE_INVALID; |
| 1053 | else |
| 1054 | return PL330_STATE_COMPLETING; |
| 1055 | case DS_ST_FLTCMP: |
| 1056 | if (is_manager(thrd)) |
| 1057 | return PL330_STATE_INVALID; |
| 1058 | else |
| 1059 | return PL330_STATE_FAULT_COMPLETING; |
| 1060 | default: |
| 1061 | return PL330_STATE_INVALID; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | static void _stop(struct pl330_thread *thrd) |
| 1066 | { |
| 1067 | void __iomem *regs = thrd->dmac->pinfo->base; |
| 1068 | u8 insn[6] = {0, 0, 0, 0, 0, 0}; |
| 1069 | |
| 1070 | if (_state(thrd) == PL330_STATE_FAULT_COMPLETING) |
| 1071 | UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING); |
| 1072 | |
| 1073 | /* Return if nothing needs to be done */ |
| 1074 | if (_state(thrd) == PL330_STATE_COMPLETING |
| 1075 | || _state(thrd) == PL330_STATE_KILLING |
| 1076 | || _state(thrd) == PL330_STATE_STOPPED) |
| 1077 | return; |
| 1078 | |
| 1079 | _emit_KILL(0, insn); |
| 1080 | |
| 1081 | /* Stop generating interrupts for SEV */ |
| 1082 | writel(readl(regs + INTEN) & ~(1 << thrd->ev), regs + INTEN); |
| 1083 | |
| 1084 | _execute_DBGINSN(thrd, insn, is_manager(thrd)); |
| 1085 | } |
| 1086 | |
| 1087 | /* Start doing req 'idx' of thread 'thrd' */ |
| 1088 | static bool _trigger(struct pl330_thread *thrd) |
| 1089 | { |
| 1090 | void __iomem *regs = thrd->dmac->pinfo->base; |
| 1091 | struct _pl330_req *req; |
| 1092 | struct pl330_req *r; |
| 1093 | struct _arg_GO go; |
| 1094 | unsigned ns; |
| 1095 | u8 insn[6] = {0, 0, 0, 0, 0, 0}; |
| 1096 | int idx; |
| 1097 | |
| 1098 | /* Return if already ACTIVE */ |
| 1099 | if (_state(thrd) != PL330_STATE_STOPPED) |
| 1100 | return true; |
| 1101 | |
| 1102 | idx = 1 - thrd->lstenq; |
| 1103 | if (!IS_FREE(&thrd->req[idx])) |
| 1104 | req = &thrd->req[idx]; |
| 1105 | else { |
| 1106 | idx = thrd->lstenq; |
| 1107 | if (!IS_FREE(&thrd->req[idx])) |
| 1108 | req = &thrd->req[idx]; |
| 1109 | else |
| 1110 | req = NULL; |
| 1111 | } |
| 1112 | |
| 1113 | /* Return if no request */ |
| 1114 | if (!req || !req->r) |
| 1115 | return true; |
| 1116 | |
| 1117 | r = req->r; |
| 1118 | |
| 1119 | if (r->cfg) |
| 1120 | ns = r->cfg->nonsecure ? 1 : 0; |
| 1121 | else if (readl(regs + CS(thrd->id)) & CS_CNS) |
| 1122 | ns = 1; |
| 1123 | else |
| 1124 | ns = 0; |
| 1125 | |
| 1126 | /* See 'Abort Sources' point-4 at Page 2-25 */ |
| 1127 | if (_manager_ns(thrd) && !ns) |
| 1128 | dev_info(thrd->dmac->pinfo->dev, "%s:%d Recipe for ABORT!\n", |
| 1129 | __func__, __LINE__); |
| 1130 | |
| 1131 | go.chan = thrd->id; |
| 1132 | go.addr = req->mc_bus; |
| 1133 | go.ns = ns; |
| 1134 | _emit_GO(0, insn, &go); |
| 1135 | |
| 1136 | /* Set to generate interrupts for SEV */ |
| 1137 | writel(readl(regs + INTEN) | (1 << thrd->ev), regs + INTEN); |
| 1138 | |
| 1139 | /* Only manager can execute GO */ |
| 1140 | _execute_DBGINSN(thrd, insn, true); |
| 1141 | |
| 1142 | thrd->req_running = idx; |
| 1143 | |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | static bool _start(struct pl330_thread *thrd) |
| 1148 | { |
| 1149 | switch (_state(thrd)) { |
| 1150 | case PL330_STATE_FAULT_COMPLETING: |
| 1151 | UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING); |
| 1152 | |
| 1153 | if (_state(thrd) == PL330_STATE_KILLING) |
| 1154 | UNTIL(thrd, PL330_STATE_STOPPED) |
| 1155 | |
| 1156 | case PL330_STATE_FAULTING: |
| 1157 | _stop(thrd); |
| 1158 | |
| 1159 | case PL330_STATE_KILLING: |
| 1160 | case PL330_STATE_COMPLETING: |
| 1161 | UNTIL(thrd, PL330_STATE_STOPPED) |
| 1162 | |
| 1163 | case PL330_STATE_STOPPED: |
| 1164 | return _trigger(thrd); |
| 1165 | |
| 1166 | case PL330_STATE_WFP: |
| 1167 | case PL330_STATE_QUEUEBUSY: |
| 1168 | case PL330_STATE_ATBARRIER: |
| 1169 | case PL330_STATE_UPDTPC: |
| 1170 | case PL330_STATE_CACHEMISS: |
| 1171 | case PL330_STATE_EXECUTING: |
| 1172 | return true; |
| 1173 | |
| 1174 | case PL330_STATE_WFE: /* For RESUME, nothing yet */ |
| 1175 | default: |
| 1176 | return false; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | static inline int _ldst_memtomem(unsigned dry_run, u8 buf[], |
| 1181 | const struct _xfer_spec *pxs, int cyc) |
| 1182 | { |
| 1183 | int off = 0; |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 1184 | struct pl330_config *pcfg = pxs->r->cfg->pcfg; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1185 | |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 1186 | /* check lock-up free version */ |
| 1187 | if (get_revision(pcfg->periph_id) >= PERIPH_REV_R1P0) { |
| 1188 | while (cyc--) { |
| 1189 | off += _emit_LD(dry_run, &buf[off], ALWAYS); |
| 1190 | off += _emit_ST(dry_run, &buf[off], ALWAYS); |
| 1191 | } |
| 1192 | } else { |
| 1193 | while (cyc--) { |
| 1194 | off += _emit_LD(dry_run, &buf[off], ALWAYS); |
| 1195 | off += _emit_RMB(dry_run, &buf[off]); |
| 1196 | off += _emit_ST(dry_run, &buf[off], ALWAYS); |
| 1197 | off += _emit_WMB(dry_run, &buf[off]); |
| 1198 | } |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | return off; |
| 1202 | } |
| 1203 | |
| 1204 | static inline int _ldst_devtomem(unsigned dry_run, u8 buf[], |
| 1205 | const struct _xfer_spec *pxs, int cyc) |
| 1206 | { |
| 1207 | int off = 0; |
| 1208 | |
| 1209 | while (cyc--) { |
| 1210 | off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri); |
| 1211 | off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->r->peri); |
| 1212 | off += _emit_ST(dry_run, &buf[off], ALWAYS); |
| 1213 | off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri); |
| 1214 | } |
| 1215 | |
| 1216 | return off; |
| 1217 | } |
| 1218 | |
| 1219 | static inline int _ldst_memtodev(unsigned dry_run, u8 buf[], |
| 1220 | const struct _xfer_spec *pxs, int cyc) |
| 1221 | { |
| 1222 | int off = 0; |
| 1223 | |
| 1224 | while (cyc--) { |
| 1225 | off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->r->peri); |
| 1226 | off += _emit_LD(dry_run, &buf[off], ALWAYS); |
| 1227 | off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->r->peri); |
| 1228 | off += _emit_FLUSHP(dry_run, &buf[off], pxs->r->peri); |
| 1229 | } |
| 1230 | |
| 1231 | return off; |
| 1232 | } |
| 1233 | |
| 1234 | static int _bursts(unsigned dry_run, u8 buf[], |
| 1235 | const struct _xfer_spec *pxs, int cyc) |
| 1236 | { |
| 1237 | int off = 0; |
| 1238 | |
| 1239 | switch (pxs->r->rqtype) { |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 1240 | case DMA_MEM_TO_DEV: |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1241 | off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc); |
| 1242 | break; |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 1243 | case DMA_DEV_TO_MEM: |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1244 | off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc); |
| 1245 | break; |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 1246 | case DMA_MEM_TO_MEM: |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1247 | off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc); |
| 1248 | break; |
| 1249 | default: |
| 1250 | off += 0x40000000; /* Scare off the Client */ |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | return off; |
| 1255 | } |
| 1256 | |
| 1257 | /* Returns bytes consumed and updates bursts */ |
| 1258 | static inline int _loop(unsigned dry_run, u8 buf[], |
| 1259 | unsigned long *bursts, const struct _xfer_spec *pxs) |
| 1260 | { |
| 1261 | int cyc, cycmax, szlp, szlpend, szbrst, off; |
| 1262 | unsigned lcnt0, lcnt1, ljmp0, ljmp1; |
| 1263 | struct _arg_LPEND lpend; |
| 1264 | |
| 1265 | /* Max iterations possible in DMALP is 256 */ |
| 1266 | if (*bursts >= 256*256) { |
| 1267 | lcnt1 = 256; |
| 1268 | lcnt0 = 256; |
| 1269 | cyc = *bursts / lcnt1 / lcnt0; |
| 1270 | } else if (*bursts > 256) { |
| 1271 | lcnt1 = 256; |
| 1272 | lcnt0 = *bursts / lcnt1; |
| 1273 | cyc = 1; |
| 1274 | } else { |
| 1275 | lcnt1 = *bursts; |
| 1276 | lcnt0 = 0; |
| 1277 | cyc = 1; |
| 1278 | } |
| 1279 | |
| 1280 | szlp = _emit_LP(1, buf, 0, 0); |
| 1281 | szbrst = _bursts(1, buf, pxs, 1); |
| 1282 | |
| 1283 | lpend.cond = ALWAYS; |
| 1284 | lpend.forever = false; |
| 1285 | lpend.loop = 0; |
| 1286 | lpend.bjump = 0; |
| 1287 | szlpend = _emit_LPEND(1, buf, &lpend); |
| 1288 | |
| 1289 | if (lcnt0) { |
| 1290 | szlp *= 2; |
| 1291 | szlpend *= 2; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Max bursts that we can unroll due to limit on the |
| 1296 | * size of backward jump that can be encoded in DMALPEND |
| 1297 | * which is 8-bits and hence 255 |
| 1298 | */ |
| 1299 | cycmax = (255 - (szlp + szlpend)) / szbrst; |
| 1300 | |
| 1301 | cyc = (cycmax < cyc) ? cycmax : cyc; |
| 1302 | |
| 1303 | off = 0; |
| 1304 | |
| 1305 | if (lcnt0) { |
| 1306 | off += _emit_LP(dry_run, &buf[off], 0, lcnt0); |
| 1307 | ljmp0 = off; |
| 1308 | } |
| 1309 | |
| 1310 | off += _emit_LP(dry_run, &buf[off], 1, lcnt1); |
| 1311 | ljmp1 = off; |
| 1312 | |
| 1313 | off += _bursts(dry_run, &buf[off], pxs, cyc); |
| 1314 | |
| 1315 | lpend.cond = ALWAYS; |
| 1316 | lpend.forever = false; |
| 1317 | lpend.loop = 1; |
| 1318 | lpend.bjump = off - ljmp1; |
| 1319 | off += _emit_LPEND(dry_run, &buf[off], &lpend); |
| 1320 | |
| 1321 | if (lcnt0) { |
| 1322 | lpend.cond = ALWAYS; |
| 1323 | lpend.forever = false; |
| 1324 | lpend.loop = 0; |
| 1325 | lpend.bjump = off - ljmp0; |
| 1326 | off += _emit_LPEND(dry_run, &buf[off], &lpend); |
| 1327 | } |
| 1328 | |
| 1329 | *bursts = lcnt1 * cyc; |
| 1330 | if (lcnt0) |
| 1331 | *bursts *= lcnt0; |
| 1332 | |
| 1333 | return off; |
| 1334 | } |
| 1335 | |
| 1336 | static inline int _setup_loops(unsigned dry_run, u8 buf[], |
| 1337 | const struct _xfer_spec *pxs) |
| 1338 | { |
| 1339 | struct pl330_xfer *x = pxs->x; |
| 1340 | u32 ccr = pxs->ccr; |
| 1341 | unsigned long c, bursts = BYTE_TO_BURST(x->bytes, ccr); |
| 1342 | int off = 0; |
| 1343 | |
| 1344 | while (bursts) { |
| 1345 | c = bursts; |
| 1346 | off += _loop(dry_run, &buf[off], &c, pxs); |
| 1347 | bursts -= c; |
| 1348 | } |
| 1349 | |
| 1350 | return off; |
| 1351 | } |
| 1352 | |
| 1353 | static inline int _setup_xfer(unsigned dry_run, u8 buf[], |
| 1354 | const struct _xfer_spec *pxs) |
| 1355 | { |
| 1356 | struct pl330_xfer *x = pxs->x; |
| 1357 | int off = 0; |
| 1358 | |
| 1359 | /* DMAMOV SAR, x->src_addr */ |
| 1360 | off += _emit_MOV(dry_run, &buf[off], SAR, x->src_addr); |
| 1361 | /* DMAMOV DAR, x->dst_addr */ |
| 1362 | off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr); |
| 1363 | |
| 1364 | /* Setup Loop(s) */ |
| 1365 | off += _setup_loops(dry_run, &buf[off], pxs); |
| 1366 | |
| 1367 | return off; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * A req is a sequence of one or more xfer units. |
| 1372 | * Returns the number of bytes taken to setup the MC for the req. |
| 1373 | */ |
| 1374 | static int _setup_req(unsigned dry_run, struct pl330_thread *thrd, |
| 1375 | unsigned index, struct _xfer_spec *pxs) |
| 1376 | { |
| 1377 | struct _pl330_req *req = &thrd->req[index]; |
| 1378 | struct pl330_xfer *x; |
| 1379 | u8 *buf = req->mc_cpu; |
| 1380 | int off = 0; |
| 1381 | |
| 1382 | PL330_DBGMC_START(req->mc_bus); |
| 1383 | |
| 1384 | /* DMAMOV CCR, ccr */ |
| 1385 | off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr); |
| 1386 | |
| 1387 | x = pxs->r->x; |
Lars-Peter Clausen | d5cef12 | 2014-07-06 20:32:23 +0200 | [diff] [blame^] | 1388 | /* Error if xfer length is not aligned at burst size */ |
| 1389 | if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr))) |
| 1390 | return -EINVAL; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1391 | |
Lars-Peter Clausen | d5cef12 | 2014-07-06 20:32:23 +0200 | [diff] [blame^] | 1392 | pxs->x = x; |
| 1393 | off += _setup_xfer(dry_run, &buf[off], pxs); |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1394 | |
| 1395 | /* DMASEV peripheral/event */ |
| 1396 | off += _emit_SEV(dry_run, &buf[off], thrd->ev); |
| 1397 | /* DMAEND */ |
| 1398 | off += _emit_END(dry_run, &buf[off]); |
| 1399 | |
| 1400 | return off; |
| 1401 | } |
| 1402 | |
| 1403 | static inline u32 _prepare_ccr(const struct pl330_reqcfg *rqc) |
| 1404 | { |
| 1405 | u32 ccr = 0; |
| 1406 | |
| 1407 | if (rqc->src_inc) |
| 1408 | ccr |= CC_SRCINC; |
| 1409 | |
| 1410 | if (rqc->dst_inc) |
| 1411 | ccr |= CC_DSTINC; |
| 1412 | |
| 1413 | /* We set same protection levels for Src and DST for now */ |
| 1414 | if (rqc->privileged) |
| 1415 | ccr |= CC_SRCPRI | CC_DSTPRI; |
| 1416 | if (rqc->nonsecure) |
| 1417 | ccr |= CC_SRCNS | CC_DSTNS; |
| 1418 | if (rqc->insnaccess) |
| 1419 | ccr |= CC_SRCIA | CC_DSTIA; |
| 1420 | |
| 1421 | ccr |= (((rqc->brst_len - 1) & 0xf) << CC_SRCBRSTLEN_SHFT); |
| 1422 | ccr |= (((rqc->brst_len - 1) & 0xf) << CC_DSTBRSTLEN_SHFT); |
| 1423 | |
| 1424 | ccr |= (rqc->brst_size << CC_SRCBRSTSIZE_SHFT); |
| 1425 | ccr |= (rqc->brst_size << CC_DSTBRSTSIZE_SHFT); |
| 1426 | |
| 1427 | ccr |= (rqc->scctl << CC_SRCCCTRL_SHFT); |
| 1428 | ccr |= (rqc->dcctl << CC_DSTCCTRL_SHFT); |
| 1429 | |
| 1430 | ccr |= (rqc->swap << CC_SWAP_SHFT); |
| 1431 | |
| 1432 | return ccr; |
| 1433 | } |
| 1434 | |
| 1435 | static inline bool _is_valid(u32 ccr) |
| 1436 | { |
Lars-Peter Clausen | f0564c7 | 2014-07-06 20:32:19 +0200 | [diff] [blame] | 1437 | enum pl330_cachectrl dcctl; |
| 1438 | enum pl330_cachectrl scctl; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1439 | |
| 1440 | dcctl = (ccr >> CC_DSTCCTRL_SHFT) & CC_DRCCCTRL_MASK; |
| 1441 | scctl = (ccr >> CC_SRCCCTRL_SHFT) & CC_SRCCCTRL_MASK; |
| 1442 | |
Lars-Peter Clausen | f0564c7 | 2014-07-06 20:32:19 +0200 | [diff] [blame] | 1443 | if (dcctl == INVALID1 || dcctl == INVALID2 |
| 1444 | || scctl == INVALID1 || scctl == INVALID2) |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1445 | return false; |
| 1446 | else |
| 1447 | return true; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Submit a list of xfers after which the client wants notification. |
| 1452 | * Client is not notified after each xfer unit, just once after all |
| 1453 | * xfer units are done or some error occurs. |
| 1454 | */ |
| 1455 | static int pl330_submit_req(void *ch_id, struct pl330_req *r) |
| 1456 | { |
| 1457 | struct pl330_thread *thrd = ch_id; |
| 1458 | struct pl330_dmac *pl330; |
| 1459 | struct pl330_info *pi; |
| 1460 | struct _xfer_spec xs; |
| 1461 | unsigned long flags; |
| 1462 | void __iomem *regs; |
| 1463 | unsigned idx; |
| 1464 | u32 ccr; |
| 1465 | int ret = 0; |
| 1466 | |
| 1467 | /* No Req or Unacquired Channel or DMAC */ |
| 1468 | if (!r || !thrd || thrd->free) |
| 1469 | return -EINVAL; |
| 1470 | |
| 1471 | pl330 = thrd->dmac; |
| 1472 | pi = pl330->pinfo; |
| 1473 | regs = pi->base; |
| 1474 | |
| 1475 | if (pl330->state == DYING |
| 1476 | || pl330->dmac_tbd.reset_chan & (1 << thrd->id)) { |
| 1477 | dev_info(thrd->dmac->pinfo->dev, "%s:%d\n", |
| 1478 | __func__, __LINE__); |
| 1479 | return -EAGAIN; |
| 1480 | } |
| 1481 | |
| 1482 | /* If request for non-existing peripheral */ |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 1483 | if (r->rqtype != DMA_MEM_TO_MEM && r->peri >= pi->pcfg.num_peri) { |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1484 | dev_info(thrd->dmac->pinfo->dev, |
| 1485 | "%s:%d Invalid peripheral(%u)!\n", |
| 1486 | __func__, __LINE__, r->peri); |
| 1487 | return -EINVAL; |
| 1488 | } |
| 1489 | |
| 1490 | spin_lock_irqsave(&pl330->lock, flags); |
| 1491 | |
| 1492 | if (_queue_full(thrd)) { |
| 1493 | ret = -EAGAIN; |
| 1494 | goto xfer_exit; |
| 1495 | } |
| 1496 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1497 | |
| 1498 | /* Use last settings, if not provided */ |
Sachin Kamat | 2e2c682 | 2012-09-17 15:20:22 +0530 | [diff] [blame] | 1499 | if (r->cfg) { |
| 1500 | /* Prefer Secure Channel */ |
| 1501 | if (!_manager_ns(thrd)) |
| 1502 | r->cfg->nonsecure = 0; |
| 1503 | else |
| 1504 | r->cfg->nonsecure = 1; |
| 1505 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1506 | ccr = _prepare_ccr(r->cfg); |
Sachin Kamat | 2e2c682 | 2012-09-17 15:20:22 +0530 | [diff] [blame] | 1507 | } else { |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1508 | ccr = readl(regs + CC(thrd->id)); |
Sachin Kamat | 2e2c682 | 2012-09-17 15:20:22 +0530 | [diff] [blame] | 1509 | } |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1510 | |
| 1511 | /* If this req doesn't have valid xfer settings */ |
| 1512 | if (!_is_valid(ccr)) { |
| 1513 | ret = -EINVAL; |
| 1514 | dev_info(thrd->dmac->pinfo->dev, "%s:%d Invalid CCR(%x)!\n", |
| 1515 | __func__, __LINE__, ccr); |
| 1516 | goto xfer_exit; |
| 1517 | } |
| 1518 | |
| 1519 | idx = IS_FREE(&thrd->req[0]) ? 0 : 1; |
| 1520 | |
| 1521 | xs.ccr = ccr; |
| 1522 | xs.r = r; |
| 1523 | |
| 1524 | /* First dry run to check if req is acceptable */ |
| 1525 | ret = _setup_req(1, thrd, idx, &xs); |
| 1526 | if (ret < 0) |
| 1527 | goto xfer_exit; |
| 1528 | |
| 1529 | if (ret > pi->mcbufsz / 2) { |
| 1530 | dev_info(thrd->dmac->pinfo->dev, |
| 1531 | "%s:%d Trying increasing mcbufsz\n", |
| 1532 | __func__, __LINE__); |
| 1533 | ret = -ENOMEM; |
| 1534 | goto xfer_exit; |
| 1535 | } |
| 1536 | |
| 1537 | /* Hook the request */ |
| 1538 | thrd->lstenq = idx; |
| 1539 | thrd->req[idx].mc_len = _setup_req(0, thrd, idx, &xs); |
| 1540 | thrd->req[idx].r = r; |
| 1541 | |
| 1542 | ret = 0; |
| 1543 | |
| 1544 | xfer_exit: |
| 1545 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1546 | |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
| 1550 | static void pl330_dotask(unsigned long data) |
| 1551 | { |
| 1552 | struct pl330_dmac *pl330 = (struct pl330_dmac *) data; |
| 1553 | struct pl330_info *pi = pl330->pinfo; |
| 1554 | unsigned long flags; |
| 1555 | int i; |
| 1556 | |
| 1557 | spin_lock_irqsave(&pl330->lock, flags); |
| 1558 | |
| 1559 | /* The DMAC itself gone nuts */ |
| 1560 | if (pl330->dmac_tbd.reset_dmac) { |
| 1561 | pl330->state = DYING; |
| 1562 | /* Reset the manager too */ |
| 1563 | pl330->dmac_tbd.reset_mngr = true; |
| 1564 | /* Clear the reset flag */ |
| 1565 | pl330->dmac_tbd.reset_dmac = false; |
| 1566 | } |
| 1567 | |
| 1568 | if (pl330->dmac_tbd.reset_mngr) { |
| 1569 | _stop(pl330->manager); |
| 1570 | /* Reset all channels */ |
| 1571 | pl330->dmac_tbd.reset_chan = (1 << pi->pcfg.num_chan) - 1; |
| 1572 | /* Clear the reset flag */ |
| 1573 | pl330->dmac_tbd.reset_mngr = false; |
| 1574 | } |
| 1575 | |
| 1576 | for (i = 0; i < pi->pcfg.num_chan; i++) { |
| 1577 | |
| 1578 | if (pl330->dmac_tbd.reset_chan & (1 << i)) { |
| 1579 | struct pl330_thread *thrd = &pl330->channels[i]; |
| 1580 | void __iomem *regs = pi->base; |
| 1581 | enum pl330_op_err err; |
| 1582 | |
| 1583 | _stop(thrd); |
| 1584 | |
| 1585 | if (readl(regs + FSC) & (1 << thrd->id)) |
| 1586 | err = PL330_ERR_FAIL; |
| 1587 | else |
| 1588 | err = PL330_ERR_ABORT; |
| 1589 | |
| 1590 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1591 | |
| 1592 | _callback(thrd->req[1 - thrd->lstenq].r, err); |
| 1593 | _callback(thrd->req[thrd->lstenq].r, err); |
| 1594 | |
| 1595 | spin_lock_irqsave(&pl330->lock, flags); |
| 1596 | |
| 1597 | thrd->req[0].r = NULL; |
| 1598 | thrd->req[1].r = NULL; |
| 1599 | mark_free(thrd, 0); |
| 1600 | mark_free(thrd, 1); |
| 1601 | |
| 1602 | /* Clear the reset flag */ |
| 1603 | pl330->dmac_tbd.reset_chan &= ~(1 << i); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1608 | |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | /* Returns 1 if state was updated, 0 otherwise */ |
| 1613 | static int pl330_update(const struct pl330_info *pi) |
| 1614 | { |
Javi Merino | fdec53d | 2012-06-13 15:07:00 +0100 | [diff] [blame] | 1615 | struct pl330_req *rqdone, *tmp; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1616 | struct pl330_dmac *pl330; |
| 1617 | unsigned long flags; |
| 1618 | void __iomem *regs; |
| 1619 | u32 val; |
| 1620 | int id, ev, ret = 0; |
| 1621 | |
| 1622 | if (!pi || !pi->pl330_data) |
| 1623 | return 0; |
| 1624 | |
| 1625 | regs = pi->base; |
| 1626 | pl330 = pi->pl330_data; |
| 1627 | |
| 1628 | spin_lock_irqsave(&pl330->lock, flags); |
| 1629 | |
| 1630 | val = readl(regs + FSM) & 0x1; |
| 1631 | if (val) |
| 1632 | pl330->dmac_tbd.reset_mngr = true; |
| 1633 | else |
| 1634 | pl330->dmac_tbd.reset_mngr = false; |
| 1635 | |
| 1636 | val = readl(regs + FSC) & ((1 << pi->pcfg.num_chan) - 1); |
| 1637 | pl330->dmac_tbd.reset_chan |= val; |
| 1638 | if (val) { |
| 1639 | int i = 0; |
| 1640 | while (i < pi->pcfg.num_chan) { |
| 1641 | if (val & (1 << i)) { |
| 1642 | dev_info(pi->dev, |
| 1643 | "Reset Channel-%d\t CS-%x FTC-%x\n", |
| 1644 | i, readl(regs + CS(i)), |
| 1645 | readl(regs + FTC(i))); |
| 1646 | _stop(&pl330->channels[i]); |
| 1647 | } |
| 1648 | i++; |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /* Check which event happened i.e, thread notified */ |
| 1653 | val = readl(regs + ES); |
| 1654 | if (pi->pcfg.num_events < 32 |
| 1655 | && val & ~((1 << pi->pcfg.num_events) - 1)) { |
| 1656 | pl330->dmac_tbd.reset_dmac = true; |
| 1657 | dev_err(pi->dev, "%s:%d Unexpected!\n", __func__, __LINE__); |
| 1658 | ret = 1; |
| 1659 | goto updt_exit; |
| 1660 | } |
| 1661 | |
| 1662 | for (ev = 0; ev < pi->pcfg.num_events; ev++) { |
| 1663 | if (val & (1 << ev)) { /* Event occurred */ |
| 1664 | struct pl330_thread *thrd; |
| 1665 | u32 inten = readl(regs + INTEN); |
| 1666 | int active; |
| 1667 | |
| 1668 | /* Clear the event */ |
| 1669 | if (inten & (1 << ev)) |
| 1670 | writel(1 << ev, regs + INTCLR); |
| 1671 | |
| 1672 | ret = 1; |
| 1673 | |
| 1674 | id = pl330->events[ev]; |
| 1675 | |
| 1676 | thrd = &pl330->channels[id]; |
| 1677 | |
| 1678 | active = thrd->req_running; |
| 1679 | if (active == -1) /* Aborted */ |
| 1680 | continue; |
| 1681 | |
Javi Merino | fdec53d | 2012-06-13 15:07:00 +0100 | [diff] [blame] | 1682 | /* Detach the req */ |
| 1683 | rqdone = thrd->req[active].r; |
| 1684 | thrd->req[active].r = NULL; |
| 1685 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1686 | mark_free(thrd, active); |
| 1687 | |
| 1688 | /* Get going again ASAP */ |
| 1689 | _start(thrd); |
| 1690 | |
| 1691 | /* For now, just make a list of callbacks to be done */ |
| 1692 | list_add_tail(&rqdone->rqd, &pl330->req_done); |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | /* Now that we are in no hurry, do the callbacks */ |
Javi Merino | fdec53d | 2012-06-13 15:07:00 +0100 | [diff] [blame] | 1697 | list_for_each_entry_safe(rqdone, tmp, &pl330->req_done, rqd) { |
| 1698 | list_del(&rqdone->rqd); |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1699 | |
| 1700 | spin_unlock_irqrestore(&pl330->lock, flags); |
Javi Merino | fdec53d | 2012-06-13 15:07:00 +0100 | [diff] [blame] | 1701 | _callback(rqdone, PL330_ERR_NONE); |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1702 | spin_lock_irqsave(&pl330->lock, flags); |
| 1703 | } |
| 1704 | |
| 1705 | updt_exit: |
| 1706 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1707 | |
| 1708 | if (pl330->dmac_tbd.reset_dmac |
| 1709 | || pl330->dmac_tbd.reset_mngr |
| 1710 | || pl330->dmac_tbd.reset_chan) { |
| 1711 | ret = 1; |
| 1712 | tasklet_schedule(&pl330->tasks); |
| 1713 | } |
| 1714 | |
| 1715 | return ret; |
| 1716 | } |
| 1717 | |
| 1718 | static int pl330_chan_ctrl(void *ch_id, enum pl330_chan_op op) |
| 1719 | { |
| 1720 | struct pl330_thread *thrd = ch_id; |
| 1721 | struct pl330_dmac *pl330; |
| 1722 | unsigned long flags; |
Linus Torvalds | ef08e78 | 2012-03-29 15:34:57 -0700 | [diff] [blame] | 1723 | int ret = 0, active; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1724 | |
| 1725 | if (!thrd || thrd->free || thrd->dmac->state == DYING) |
| 1726 | return -EINVAL; |
| 1727 | |
| 1728 | pl330 = thrd->dmac; |
Linus Torvalds | ef08e78 | 2012-03-29 15:34:57 -0700 | [diff] [blame] | 1729 | active = thrd->req_running; |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1730 | |
| 1731 | spin_lock_irqsave(&pl330->lock, flags); |
| 1732 | |
| 1733 | switch (op) { |
| 1734 | case PL330_OP_FLUSH: |
| 1735 | /* Make sure the channel is stopped */ |
| 1736 | _stop(thrd); |
| 1737 | |
| 1738 | thrd->req[0].r = NULL; |
| 1739 | thrd->req[1].r = NULL; |
| 1740 | mark_free(thrd, 0); |
| 1741 | mark_free(thrd, 1); |
| 1742 | break; |
| 1743 | |
| 1744 | case PL330_OP_ABORT: |
| 1745 | /* Make sure the channel is stopped */ |
| 1746 | _stop(thrd); |
| 1747 | |
| 1748 | /* ABORT is only for the active req */ |
| 1749 | if (active == -1) |
| 1750 | break; |
| 1751 | |
| 1752 | thrd->req[active].r = NULL; |
| 1753 | mark_free(thrd, active); |
| 1754 | |
| 1755 | /* Start the next */ |
| 1756 | case PL330_OP_START: |
| 1757 | if ((active == -1) && !_start(thrd)) |
| 1758 | ret = -EIO; |
| 1759 | break; |
| 1760 | |
| 1761 | default: |
| 1762 | ret = -EINVAL; |
| 1763 | } |
| 1764 | |
| 1765 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1766 | return ret; |
| 1767 | } |
| 1768 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1769 | /* Reserve an event */ |
| 1770 | static inline int _alloc_event(struct pl330_thread *thrd) |
| 1771 | { |
| 1772 | struct pl330_dmac *pl330 = thrd->dmac; |
| 1773 | struct pl330_info *pi = pl330->pinfo; |
| 1774 | int ev; |
| 1775 | |
| 1776 | for (ev = 0; ev < pi->pcfg.num_events; ev++) |
| 1777 | if (pl330->events[ev] == -1) { |
| 1778 | pl330->events[ev] = thrd->id; |
| 1779 | return ev; |
| 1780 | } |
| 1781 | |
| 1782 | return -1; |
| 1783 | } |
| 1784 | |
| 1785 | static bool _chan_ns(const struct pl330_info *pi, int i) |
| 1786 | { |
| 1787 | return pi->pcfg.irq_ns & (1 << i); |
| 1788 | } |
| 1789 | |
| 1790 | /* Upon success, returns IdentityToken for the |
| 1791 | * allocated channel, NULL otherwise. |
| 1792 | */ |
| 1793 | static void *pl330_request_channel(const struct pl330_info *pi) |
| 1794 | { |
| 1795 | struct pl330_thread *thrd = NULL; |
| 1796 | struct pl330_dmac *pl330; |
| 1797 | unsigned long flags; |
| 1798 | int chans, i; |
| 1799 | |
| 1800 | if (!pi || !pi->pl330_data) |
| 1801 | return NULL; |
| 1802 | |
| 1803 | pl330 = pi->pl330_data; |
| 1804 | |
| 1805 | if (pl330->state == DYING) |
| 1806 | return NULL; |
| 1807 | |
| 1808 | chans = pi->pcfg.num_chan; |
| 1809 | |
| 1810 | spin_lock_irqsave(&pl330->lock, flags); |
| 1811 | |
| 1812 | for (i = 0; i < chans; i++) { |
| 1813 | thrd = &pl330->channels[i]; |
| 1814 | if ((thrd->free) && (!_manager_ns(thrd) || |
| 1815 | _chan_ns(pi, i))) { |
| 1816 | thrd->ev = _alloc_event(thrd); |
| 1817 | if (thrd->ev >= 0) { |
| 1818 | thrd->free = false; |
| 1819 | thrd->lstenq = 1; |
| 1820 | thrd->req[0].r = NULL; |
| 1821 | mark_free(thrd, 0); |
| 1822 | thrd->req[1].r = NULL; |
| 1823 | mark_free(thrd, 1); |
| 1824 | break; |
| 1825 | } |
| 1826 | } |
| 1827 | thrd = NULL; |
| 1828 | } |
| 1829 | |
| 1830 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1831 | |
| 1832 | return thrd; |
| 1833 | } |
| 1834 | |
| 1835 | /* Release an event */ |
| 1836 | static inline void _free_event(struct pl330_thread *thrd, int ev) |
| 1837 | { |
| 1838 | struct pl330_dmac *pl330 = thrd->dmac; |
| 1839 | struct pl330_info *pi = pl330->pinfo; |
| 1840 | |
| 1841 | /* If the event is valid and was held by the thread */ |
| 1842 | if (ev >= 0 && ev < pi->pcfg.num_events |
| 1843 | && pl330->events[ev] == thrd->id) |
| 1844 | pl330->events[ev] = -1; |
| 1845 | } |
| 1846 | |
| 1847 | static void pl330_release_channel(void *ch_id) |
| 1848 | { |
| 1849 | struct pl330_thread *thrd = ch_id; |
| 1850 | struct pl330_dmac *pl330; |
| 1851 | unsigned long flags; |
| 1852 | |
| 1853 | if (!thrd || thrd->free) |
| 1854 | return; |
| 1855 | |
| 1856 | _stop(thrd); |
| 1857 | |
| 1858 | _callback(thrd->req[1 - thrd->lstenq].r, PL330_ERR_ABORT); |
| 1859 | _callback(thrd->req[thrd->lstenq].r, PL330_ERR_ABORT); |
| 1860 | |
| 1861 | pl330 = thrd->dmac; |
| 1862 | |
| 1863 | spin_lock_irqsave(&pl330->lock, flags); |
| 1864 | _free_event(thrd, thrd->ev); |
| 1865 | thrd->free = true; |
| 1866 | spin_unlock_irqrestore(&pl330->lock, flags); |
| 1867 | } |
| 1868 | |
| 1869 | /* Initialize the structure for PL330 configuration, that can be used |
| 1870 | * by the client driver the make best use of the DMAC |
| 1871 | */ |
| 1872 | static void read_dmac_config(struct pl330_info *pi) |
| 1873 | { |
| 1874 | void __iomem *regs = pi->base; |
| 1875 | u32 val; |
| 1876 | |
| 1877 | val = readl(regs + CRD) >> CRD_DATA_WIDTH_SHIFT; |
| 1878 | val &= CRD_DATA_WIDTH_MASK; |
| 1879 | pi->pcfg.data_bus_width = 8 * (1 << val); |
| 1880 | |
| 1881 | val = readl(regs + CRD) >> CRD_DATA_BUFF_SHIFT; |
| 1882 | val &= CRD_DATA_BUFF_MASK; |
| 1883 | pi->pcfg.data_buf_dep = val + 1; |
| 1884 | |
| 1885 | val = readl(regs + CR0) >> CR0_NUM_CHANS_SHIFT; |
| 1886 | val &= CR0_NUM_CHANS_MASK; |
| 1887 | val += 1; |
| 1888 | pi->pcfg.num_chan = val; |
| 1889 | |
| 1890 | val = readl(regs + CR0); |
| 1891 | if (val & CR0_PERIPH_REQ_SET) { |
| 1892 | val = (val >> CR0_NUM_PERIPH_SHIFT) & CR0_NUM_PERIPH_MASK; |
| 1893 | val += 1; |
| 1894 | pi->pcfg.num_peri = val; |
| 1895 | pi->pcfg.peri_ns = readl(regs + CR4); |
| 1896 | } else { |
| 1897 | pi->pcfg.num_peri = 0; |
| 1898 | } |
| 1899 | |
| 1900 | val = readl(regs + CR0); |
| 1901 | if (val & CR0_BOOT_MAN_NS) |
| 1902 | pi->pcfg.mode |= DMAC_MODE_NS; |
| 1903 | else |
| 1904 | pi->pcfg.mode &= ~DMAC_MODE_NS; |
| 1905 | |
| 1906 | val = readl(regs + CR0) >> CR0_NUM_EVENTS_SHIFT; |
| 1907 | val &= CR0_NUM_EVENTS_MASK; |
| 1908 | val += 1; |
| 1909 | pi->pcfg.num_events = val; |
| 1910 | |
| 1911 | pi->pcfg.irq_ns = readl(regs + CR3); |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | static inline void _reset_thread(struct pl330_thread *thrd) |
| 1915 | { |
| 1916 | struct pl330_dmac *pl330 = thrd->dmac; |
| 1917 | struct pl330_info *pi = pl330->pinfo; |
| 1918 | |
| 1919 | thrd->req[0].mc_cpu = pl330->mcode_cpu |
| 1920 | + (thrd->id * pi->mcbufsz); |
| 1921 | thrd->req[0].mc_bus = pl330->mcode_bus |
| 1922 | + (thrd->id * pi->mcbufsz); |
| 1923 | thrd->req[0].r = NULL; |
| 1924 | mark_free(thrd, 0); |
| 1925 | |
| 1926 | thrd->req[1].mc_cpu = thrd->req[0].mc_cpu |
| 1927 | + pi->mcbufsz / 2; |
| 1928 | thrd->req[1].mc_bus = thrd->req[0].mc_bus |
| 1929 | + pi->mcbufsz / 2; |
| 1930 | thrd->req[1].r = NULL; |
| 1931 | mark_free(thrd, 1); |
| 1932 | } |
| 1933 | |
| 1934 | static int dmac_alloc_threads(struct pl330_dmac *pl330) |
| 1935 | { |
| 1936 | struct pl330_info *pi = pl330->pinfo; |
| 1937 | int chans = pi->pcfg.num_chan; |
| 1938 | struct pl330_thread *thrd; |
| 1939 | int i; |
| 1940 | |
| 1941 | /* Allocate 1 Manager and 'chans' Channel threads */ |
| 1942 | pl330->channels = kzalloc((1 + chans) * sizeof(*thrd), |
| 1943 | GFP_KERNEL); |
| 1944 | if (!pl330->channels) |
| 1945 | return -ENOMEM; |
| 1946 | |
| 1947 | /* Init Channel threads */ |
| 1948 | for (i = 0; i < chans; i++) { |
| 1949 | thrd = &pl330->channels[i]; |
| 1950 | thrd->id = i; |
| 1951 | thrd->dmac = pl330; |
| 1952 | _reset_thread(thrd); |
| 1953 | thrd->free = true; |
| 1954 | } |
| 1955 | |
| 1956 | /* MANAGER is indexed at the end */ |
| 1957 | thrd = &pl330->channels[chans]; |
| 1958 | thrd->id = chans; |
| 1959 | thrd->dmac = pl330; |
| 1960 | thrd->free = false; |
| 1961 | pl330->manager = thrd; |
| 1962 | |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
| 1966 | static int dmac_alloc_resources(struct pl330_dmac *pl330) |
| 1967 | { |
| 1968 | struct pl330_info *pi = pl330->pinfo; |
| 1969 | int chans = pi->pcfg.num_chan; |
| 1970 | int ret; |
| 1971 | |
| 1972 | /* |
| 1973 | * Alloc MicroCode buffer for 'chans' Channel threads. |
| 1974 | * A channel's buffer offset is (Channel_Id * MCODE_BUFF_PERCHAN) |
| 1975 | */ |
| 1976 | pl330->mcode_cpu = dma_alloc_coherent(pi->dev, |
| 1977 | chans * pi->mcbufsz, |
| 1978 | &pl330->mcode_bus, GFP_KERNEL); |
| 1979 | if (!pl330->mcode_cpu) { |
| 1980 | dev_err(pi->dev, "%s:%d Can't allocate memory!\n", |
| 1981 | __func__, __LINE__); |
| 1982 | return -ENOMEM; |
| 1983 | } |
| 1984 | |
| 1985 | ret = dmac_alloc_threads(pl330); |
| 1986 | if (ret) { |
| 1987 | dev_err(pi->dev, "%s:%d Can't to create channels for DMAC!\n", |
| 1988 | __func__, __LINE__); |
| 1989 | dma_free_coherent(pi->dev, |
| 1990 | chans * pi->mcbufsz, |
| 1991 | pl330->mcode_cpu, pl330->mcode_bus); |
| 1992 | return ret; |
| 1993 | } |
| 1994 | |
| 1995 | return 0; |
| 1996 | } |
| 1997 | |
| 1998 | static int pl330_add(struct pl330_info *pi) |
| 1999 | { |
| 2000 | struct pl330_dmac *pl330; |
| 2001 | void __iomem *regs; |
| 2002 | int i, ret; |
| 2003 | |
| 2004 | if (!pi || !pi->dev) |
| 2005 | return -EINVAL; |
| 2006 | |
| 2007 | /* If already added */ |
| 2008 | if (pi->pl330_data) |
| 2009 | return -EINVAL; |
| 2010 | |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 2011 | regs = pi->base; |
| 2012 | |
| 2013 | /* Check if we can handle this DMAC */ |
Will Deacon | 0967717 | 2013-06-10 19:34:37 +0100 | [diff] [blame] | 2014 | if ((pi->pcfg.periph_id & 0xfffff) != PERIPH_ID_VAL) { |
| 2015 | dev_err(pi->dev, "PERIPH_ID 0x%x !\n", pi->pcfg.periph_id); |
Boojin Kim | b7d861d | 2011-12-26 18:49:52 +0900 | [diff] [blame] | 2016 | return -EINVAL; |
| 2017 | } |
| 2018 | |
| 2019 | /* Read the configuration of the DMAC */ |
| 2020 | read_dmac_config(pi); |
| 2021 | |
| 2022 | if (pi->pcfg.num_events == 0) { |
| 2023 | dev_err(pi->dev, "%s:%d Can't work without events!\n", |
| 2024 | __func__, __LINE__); |
| 2025 | return -EINVAL; |
| 2026 | } |
| 2027 | |
| 2028 | pl330 = kzalloc(sizeof(*pl330), GFP_KERNEL); |
| 2029 | if (!pl330) { |
| 2030 | dev_err(pi->dev, "%s:%d Can't allocate memory!\n", |
| 2031 | __func__, __LINE__); |
| 2032 | return -ENOMEM; |
| 2033 | } |
| 2034 | |
| 2035 | /* Assign the info structure and private data */ |
| 2036 | pl330->pinfo = pi; |
| 2037 | pi->pl330_data = pl330; |
| 2038 | |
| 2039 | spin_lock_init(&pl330->lock); |
| 2040 | |
| 2041 | INIT_LIST_HEAD(&pl330->req_done); |
| 2042 | |
| 2043 | /* Use default MC buffer size if not provided */ |
| 2044 | if (!pi->mcbufsz) |
| 2045 | pi->mcbufsz = MCODE_BUFF_PER_REQ * 2; |
| 2046 | |
| 2047 | /* Mark all events as free */ |
| 2048 | for (i = 0; i < pi->pcfg.num_events; i++) |
| 2049 | pl330->events[i] = -1; |
| 2050 | |
| 2051 | /* Allocate resources needed by the DMAC */ |
| 2052 | ret = dmac_alloc_resources(pl330); |
| 2053 | if (ret) { |
| 2054 | dev_err(pi->dev, "Unable to create channels for DMAC\n"); |
| 2055 | kfree(pl330); |
| 2056 | return ret; |
| 2057 | } |
| 2058 | |
| 2059 | tasklet_init(&pl330->tasks, pl330_dotask, (unsigned long) pl330); |
| 2060 | |
| 2061 | pl330->state = INIT; |
| 2062 | |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | static int dmac_free_threads(struct pl330_dmac *pl330) |
| 2067 | { |
| 2068 | struct pl330_info *pi = pl330->pinfo; |
| 2069 | int chans = pi->pcfg.num_chan; |
| 2070 | struct pl330_thread *thrd; |
| 2071 | int i; |
| 2072 | |
| 2073 | /* Release Channel threads */ |
| 2074 | for (i = 0; i < chans; i++) { |
| 2075 | thrd = &pl330->channels[i]; |
| 2076 | pl330_release_channel((void *)thrd); |
| 2077 | } |
| 2078 | |
| 2079 | /* Free memory */ |
| 2080 | kfree(pl330->channels); |
| 2081 | |
| 2082 | return 0; |
| 2083 | } |
| 2084 | |
| 2085 | static void dmac_free_resources(struct pl330_dmac *pl330) |
| 2086 | { |
| 2087 | struct pl330_info *pi = pl330->pinfo; |
| 2088 | int chans = pi->pcfg.num_chan; |
| 2089 | |
| 2090 | dmac_free_threads(pl330); |
| 2091 | |
| 2092 | dma_free_coherent(pi->dev, chans * pi->mcbufsz, |
| 2093 | pl330->mcode_cpu, pl330->mcode_bus); |
| 2094 | } |
| 2095 | |
| 2096 | static void pl330_del(struct pl330_info *pi) |
| 2097 | { |
| 2098 | struct pl330_dmac *pl330; |
| 2099 | |
| 2100 | if (!pi || !pi->pl330_data) |
| 2101 | return; |
| 2102 | |
| 2103 | pl330 = pi->pl330_data; |
| 2104 | |
| 2105 | pl330->state = UNINIT; |
| 2106 | |
| 2107 | tasklet_kill(&pl330->tasks); |
| 2108 | |
| 2109 | /* Free DMAC resources */ |
| 2110 | dmac_free_resources(pl330); |
| 2111 | |
| 2112 | kfree(pl330); |
| 2113 | pi->pl330_data = NULL; |
| 2114 | } |
| 2115 | |
Thomas Abraham | 3e2ec13 | 2011-10-24 11:43:02 +0200 | [diff] [blame] | 2116 | /* forward declaration */ |
| 2117 | static struct amba_driver pl330_driver; |
| 2118 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2119 | static inline struct dma_pl330_chan * |
| 2120 | to_pchan(struct dma_chan *ch) |
| 2121 | { |
| 2122 | if (!ch) |
| 2123 | return NULL; |
| 2124 | |
| 2125 | return container_of(ch, struct dma_pl330_chan, chan); |
| 2126 | } |
| 2127 | |
| 2128 | static inline struct dma_pl330_desc * |
| 2129 | to_desc(struct dma_async_tx_descriptor *tx) |
| 2130 | { |
| 2131 | return container_of(tx, struct dma_pl330_desc, txd); |
| 2132 | } |
| 2133 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2134 | static inline void fill_queue(struct dma_pl330_chan *pch) |
| 2135 | { |
| 2136 | struct dma_pl330_desc *desc; |
| 2137 | int ret; |
| 2138 | |
| 2139 | list_for_each_entry(desc, &pch->work_list, node) { |
| 2140 | |
| 2141 | /* If already submitted */ |
| 2142 | if (desc->status == BUSY) |
Jassi Brar | 30fb980 | 2013-02-13 16:13:14 +0530 | [diff] [blame] | 2143 | continue; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2144 | |
| 2145 | ret = pl330_submit_req(pch->pl330_chid, |
| 2146 | &desc->req); |
| 2147 | if (!ret) { |
| 2148 | desc->status = BUSY; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2149 | } else if (ret == -EAGAIN) { |
| 2150 | /* QFull or DMAC Dying */ |
| 2151 | break; |
| 2152 | } else { |
| 2153 | /* Unacceptable request */ |
| 2154 | desc->status = DONE; |
| 2155 | dev_err(pch->dmac->pif.dev, "%s:%d Bad Desc(%d)\n", |
| 2156 | __func__, __LINE__, desc->txd.cookie); |
| 2157 | tasklet_schedule(&pch->task); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | static void pl330_tasklet(unsigned long data) |
| 2163 | { |
| 2164 | struct dma_pl330_chan *pch = (struct dma_pl330_chan *)data; |
| 2165 | struct dma_pl330_desc *desc, *_dt; |
| 2166 | unsigned long flags; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2167 | |
| 2168 | spin_lock_irqsave(&pch->lock, flags); |
| 2169 | |
| 2170 | /* Pick up ripe tomatoes */ |
| 2171 | list_for_each_entry_safe(desc, _dt, &pch->work_list, node) |
| 2172 | if (desc->status == DONE) { |
Tushar Behera | 30c1dc0 | 2012-05-23 16:47:31 +0530 | [diff] [blame] | 2173 | if (!pch->cyclic) |
Vinod Koul | eab2158 | 2012-05-11 11:24:41 +0530 | [diff] [blame] | 2174 | dma_cookie_complete(&desc->txd); |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2175 | list_move_tail(&desc->node, &pch->completed_list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | /* Try to submit a req imm. next to the last completed cookie */ |
| 2179 | fill_queue(pch); |
| 2180 | |
| 2181 | /* Make sure the PL330 Channel thread is active */ |
| 2182 | pl330_chan_ctrl(pch->pl330_chid, PL330_OP_START); |
| 2183 | |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2184 | while (!list_empty(&pch->completed_list)) { |
| 2185 | dma_async_tx_callback callback; |
| 2186 | void *callback_param; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2187 | |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2188 | desc = list_first_entry(&pch->completed_list, |
| 2189 | struct dma_pl330_desc, node); |
| 2190 | |
| 2191 | callback = desc->txd.callback; |
| 2192 | callback_param = desc->txd.callback_param; |
| 2193 | |
| 2194 | if (pch->cyclic) { |
| 2195 | desc->status = PREP; |
| 2196 | list_move_tail(&desc->node, &pch->work_list); |
| 2197 | } else { |
| 2198 | desc->status = FREE; |
| 2199 | list_move_tail(&desc->node, &pch->dmac->desc_pool); |
| 2200 | } |
| 2201 | |
Dan Williams | d38a8c6 | 2013-10-18 19:35:23 +0200 | [diff] [blame] | 2202 | dma_descriptor_unmap(&desc->txd); |
| 2203 | |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2204 | if (callback) { |
| 2205 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2206 | callback(callback_param); |
| 2207 | spin_lock_irqsave(&pch->lock, flags); |
| 2208 | } |
| 2209 | } |
| 2210 | spin_unlock_irqrestore(&pch->lock, flags); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | static void dma_pl330_rqcb(void *token, enum pl330_op_err err) |
| 2214 | { |
| 2215 | struct dma_pl330_desc *desc = token; |
| 2216 | struct dma_pl330_chan *pch = desc->pchan; |
| 2217 | unsigned long flags; |
| 2218 | |
| 2219 | /* If desc aborted */ |
| 2220 | if (!pch) |
| 2221 | return; |
| 2222 | |
| 2223 | spin_lock_irqsave(&pch->lock, flags); |
| 2224 | |
| 2225 | desc->status = DONE; |
| 2226 | |
| 2227 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2228 | |
| 2229 | tasklet_schedule(&pch->task); |
| 2230 | } |
| 2231 | |
Thomas Abraham | 3e2ec13 | 2011-10-24 11:43:02 +0200 | [diff] [blame] | 2232 | bool pl330_filter(struct dma_chan *chan, void *param) |
| 2233 | { |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2234 | u8 *peri_id; |
Thomas Abraham | 3e2ec13 | 2011-10-24 11:43:02 +0200 | [diff] [blame] | 2235 | |
| 2236 | if (chan->device->dev->driver != &pl330_driver.drv) |
| 2237 | return false; |
| 2238 | |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2239 | peri_id = chan->private; |
Dan Carpenter | 2f986ec | 2013-11-08 12:51:16 +0300 | [diff] [blame] | 2240 | return *peri_id == (unsigned long)param; |
Thomas Abraham | 3e2ec13 | 2011-10-24 11:43:02 +0200 | [diff] [blame] | 2241 | } |
| 2242 | EXPORT_SYMBOL(pl330_filter); |
| 2243 | |
Padmavathi Venna | a80258f | 2013-02-14 09:10:06 +0530 | [diff] [blame] | 2244 | static struct dma_chan *of_dma_pl330_xlate(struct of_phandle_args *dma_spec, |
| 2245 | struct of_dma *ofdma) |
| 2246 | { |
| 2247 | int count = dma_spec->args_count; |
| 2248 | struct dma_pl330_dmac *pdmac = ofdma->of_dma_data; |
Lars-Peter Clausen | 70cbb16 | 2014-01-11 20:08:39 +0100 | [diff] [blame] | 2249 | unsigned int chan_id; |
Padmavathi Venna | a80258f | 2013-02-14 09:10:06 +0530 | [diff] [blame] | 2250 | |
| 2251 | if (count != 1) |
| 2252 | return NULL; |
| 2253 | |
Lars-Peter Clausen | 70cbb16 | 2014-01-11 20:08:39 +0100 | [diff] [blame] | 2254 | chan_id = dma_spec->args[0]; |
| 2255 | if (chan_id >= pdmac->num_peripherals) |
| 2256 | return NULL; |
Padmavathi Venna | a80258f | 2013-02-14 09:10:06 +0530 | [diff] [blame] | 2257 | |
Lars-Peter Clausen | 70cbb16 | 2014-01-11 20:08:39 +0100 | [diff] [blame] | 2258 | return dma_get_slave_channel(&pdmac->peripherals[chan_id].chan); |
Padmavathi Venna | a80258f | 2013-02-14 09:10:06 +0530 | [diff] [blame] | 2259 | } |
| 2260 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2261 | static int pl330_alloc_chan_resources(struct dma_chan *chan) |
| 2262 | { |
| 2263 | struct dma_pl330_chan *pch = to_pchan(chan); |
| 2264 | struct dma_pl330_dmac *pdmac = pch->dmac; |
| 2265 | unsigned long flags; |
| 2266 | |
| 2267 | spin_lock_irqsave(&pch->lock, flags); |
| 2268 | |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 2269 | dma_cookie_init(chan); |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2270 | pch->cyclic = false; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2271 | |
| 2272 | pch->pl330_chid = pl330_request_channel(&pdmac->pif); |
| 2273 | if (!pch->pl330_chid) { |
| 2274 | spin_unlock_irqrestore(&pch->lock, flags); |
Inderpal Singh | 0274788 | 2012-09-17 09:57:45 +0530 | [diff] [blame] | 2275 | return -ENOMEM; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | tasklet_init(&pch->task, pl330_tasklet, (unsigned long) pch); |
| 2279 | |
| 2280 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2281 | |
| 2282 | return 1; |
| 2283 | } |
| 2284 | |
| 2285 | static int pl330_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, unsigned long arg) |
| 2286 | { |
| 2287 | struct dma_pl330_chan *pch = to_pchan(chan); |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2288 | struct dma_pl330_desc *desc; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2289 | unsigned long flags; |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2290 | struct dma_pl330_dmac *pdmac = pch->dmac; |
| 2291 | struct dma_slave_config *slave_config; |
Boojin Kim | ae43b88 | 2011-09-02 09:44:32 +0900 | [diff] [blame] | 2292 | LIST_HEAD(list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2293 | |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2294 | switch (cmd) { |
| 2295 | case DMA_TERMINATE_ALL: |
| 2296 | spin_lock_irqsave(&pch->lock, flags); |
| 2297 | |
| 2298 | /* FLUSH the PL330 Channel thread */ |
| 2299 | pl330_chan_ctrl(pch->pl330_chid, PL330_OP_FLUSH); |
| 2300 | |
| 2301 | /* Mark all desc done */ |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2302 | list_for_each_entry(desc, &pch->submitted_list, node) { |
| 2303 | desc->status = FREE; |
| 2304 | dma_cookie_complete(&desc->txd); |
| 2305 | } |
| 2306 | |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2307 | list_for_each_entry(desc, &pch->work_list , node) { |
| 2308 | desc->status = FREE; |
| 2309 | dma_cookie_complete(&desc->txd); |
Boojin Kim | ae43b88 | 2011-09-02 09:44:32 +0900 | [diff] [blame] | 2310 | } |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2311 | |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2312 | list_for_each_entry(desc, &pch->completed_list , node) { |
| 2313 | desc->status = FREE; |
| 2314 | dma_cookie_complete(&desc->txd); |
| 2315 | } |
| 2316 | |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2317 | list_splice_tail_init(&pch->submitted_list, &pdmac->desc_pool); |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2318 | list_splice_tail_init(&pch->work_list, &pdmac->desc_pool); |
| 2319 | list_splice_tail_init(&pch->completed_list, &pdmac->desc_pool); |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2320 | spin_unlock_irqrestore(&pch->lock, flags); |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2321 | break; |
| 2322 | case DMA_SLAVE_CONFIG: |
| 2323 | slave_config = (struct dma_slave_config *)arg; |
| 2324 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2325 | if (slave_config->direction == DMA_MEM_TO_DEV) { |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2326 | if (slave_config->dst_addr) |
| 2327 | pch->fifo_addr = slave_config->dst_addr; |
| 2328 | if (slave_config->dst_addr_width) |
| 2329 | pch->burst_sz = __ffs(slave_config->dst_addr_width); |
| 2330 | if (slave_config->dst_maxburst) |
| 2331 | pch->burst_len = slave_config->dst_maxburst; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2332 | } else if (slave_config->direction == DMA_DEV_TO_MEM) { |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2333 | if (slave_config->src_addr) |
| 2334 | pch->fifo_addr = slave_config->src_addr; |
| 2335 | if (slave_config->src_addr_width) |
| 2336 | pch->burst_sz = __ffs(slave_config->src_addr_width); |
| 2337 | if (slave_config->src_maxburst) |
| 2338 | pch->burst_len = slave_config->src_maxburst; |
| 2339 | } |
| 2340 | break; |
| 2341 | default: |
| 2342 | dev_err(pch->dmac->pif.dev, "Not supported command.\n"); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2343 | return -ENXIO; |
Boojin Kim | 1d0c1d6 | 2011-09-02 09:44:31 +0900 | [diff] [blame] | 2344 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2345 | |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
| 2349 | static void pl330_free_chan_resources(struct dma_chan *chan) |
| 2350 | { |
| 2351 | struct dma_pl330_chan *pch = to_pchan(chan); |
| 2352 | unsigned long flags; |
| 2353 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2354 | tasklet_kill(&pch->task); |
| 2355 | |
Bartlomiej Zolnierkiewicz | da331ba | 2013-07-03 15:00:43 -0700 | [diff] [blame] | 2356 | spin_lock_irqsave(&pch->lock, flags); |
| 2357 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2358 | pl330_release_channel(pch->pl330_chid); |
| 2359 | pch->pl330_chid = NULL; |
| 2360 | |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2361 | if (pch->cyclic) |
| 2362 | list_splice_tail_init(&pch->work_list, &pch->dmac->desc_pool); |
| 2363 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2364 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2365 | } |
| 2366 | |
| 2367 | static enum dma_status |
| 2368 | pl330_tx_status(struct dma_chan *chan, dma_cookie_t cookie, |
| 2369 | struct dma_tx_state *txstate) |
| 2370 | { |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 2371 | return dma_cookie_status(chan, cookie, txstate); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void pl330_issue_pending(struct dma_chan *chan) |
| 2375 | { |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2376 | struct dma_pl330_chan *pch = to_pchan(chan); |
| 2377 | unsigned long flags; |
| 2378 | |
| 2379 | spin_lock_irqsave(&pch->lock, flags); |
| 2380 | list_splice_tail_init(&pch->submitted_list, &pch->work_list); |
| 2381 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2382 | |
| 2383 | pl330_tasklet((unsigned long)pch); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * We returned the last one of the circular list of descriptor(s) |
| 2388 | * from prep_xxx, so the argument to submit corresponds to the last |
| 2389 | * descriptor of the list. |
| 2390 | */ |
| 2391 | static dma_cookie_t pl330_tx_submit(struct dma_async_tx_descriptor *tx) |
| 2392 | { |
| 2393 | struct dma_pl330_desc *desc, *last = to_desc(tx); |
| 2394 | struct dma_pl330_chan *pch = to_pchan(tx->chan); |
| 2395 | dma_cookie_t cookie; |
| 2396 | unsigned long flags; |
| 2397 | |
| 2398 | spin_lock_irqsave(&pch->lock, flags); |
| 2399 | |
| 2400 | /* Assign cookies to all nodes */ |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2401 | while (!list_empty(&last->node)) { |
| 2402 | desc = list_entry(last->node.next, struct dma_pl330_desc, node); |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2403 | if (pch->cyclic) { |
| 2404 | desc->txd.callback = last->txd.callback; |
| 2405 | desc->txd.callback_param = last->txd.callback_param; |
| 2406 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2407 | |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 2408 | dma_cookie_assign(&desc->txd); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2409 | |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2410 | list_move_tail(&desc->node, &pch->submitted_list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2411 | } |
| 2412 | |
Russell King - ARM Linux | 884485e | 2012-03-06 22:34:46 +0000 | [diff] [blame] | 2413 | cookie = dma_cookie_assign(&last->txd); |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2414 | list_add_tail(&last->node, &pch->submitted_list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2415 | spin_unlock_irqrestore(&pch->lock, flags); |
| 2416 | |
| 2417 | return cookie; |
| 2418 | } |
| 2419 | |
| 2420 | static inline void _init_desc(struct dma_pl330_desc *desc) |
| 2421 | { |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2422 | desc->req.x = &desc->px; |
| 2423 | desc->req.token = desc; |
| 2424 | desc->rqcfg.swap = SWAP_NO; |
Lars-Peter Clausen | f0564c7 | 2014-07-06 20:32:19 +0200 | [diff] [blame] | 2425 | desc->rqcfg.scctl = CCTRL0; |
| 2426 | desc->rqcfg.dcctl = CCTRL0; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2427 | desc->req.cfg = &desc->rqcfg; |
| 2428 | desc->req.xfer_cb = dma_pl330_rqcb; |
| 2429 | desc->txd.tx_submit = pl330_tx_submit; |
| 2430 | |
| 2431 | INIT_LIST_HEAD(&desc->node); |
| 2432 | } |
| 2433 | |
| 2434 | /* Returns the number of descriptors added to the DMAC pool */ |
Sachin Kamat | 5a67ac5 | 2012-06-04 17:09:45 +0530 | [diff] [blame] | 2435 | static int add_desc(struct dma_pl330_dmac *pdmac, gfp_t flg, int count) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2436 | { |
| 2437 | struct dma_pl330_desc *desc; |
| 2438 | unsigned long flags; |
| 2439 | int i; |
| 2440 | |
| 2441 | if (!pdmac) |
| 2442 | return 0; |
| 2443 | |
Will Deacon | 0baf8f6 | 2013-12-02 18:01:30 +0000 | [diff] [blame] | 2444 | desc = kcalloc(count, sizeof(*desc), flg); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2445 | if (!desc) |
| 2446 | return 0; |
| 2447 | |
| 2448 | spin_lock_irqsave(&pdmac->pool_lock, flags); |
| 2449 | |
| 2450 | for (i = 0; i < count; i++) { |
| 2451 | _init_desc(&desc[i]); |
| 2452 | list_add_tail(&desc[i].node, &pdmac->desc_pool); |
| 2453 | } |
| 2454 | |
| 2455 | spin_unlock_irqrestore(&pdmac->pool_lock, flags); |
| 2456 | |
| 2457 | return count; |
| 2458 | } |
| 2459 | |
| 2460 | static struct dma_pl330_desc * |
| 2461 | pluck_desc(struct dma_pl330_dmac *pdmac) |
| 2462 | { |
| 2463 | struct dma_pl330_desc *desc = NULL; |
| 2464 | unsigned long flags; |
| 2465 | |
| 2466 | if (!pdmac) |
| 2467 | return NULL; |
| 2468 | |
| 2469 | spin_lock_irqsave(&pdmac->pool_lock, flags); |
| 2470 | |
| 2471 | if (!list_empty(&pdmac->desc_pool)) { |
| 2472 | desc = list_entry(pdmac->desc_pool.next, |
| 2473 | struct dma_pl330_desc, node); |
| 2474 | |
| 2475 | list_del_init(&desc->node); |
| 2476 | |
| 2477 | desc->status = PREP; |
| 2478 | desc->txd.callback = NULL; |
| 2479 | } |
| 2480 | |
| 2481 | spin_unlock_irqrestore(&pdmac->pool_lock, flags); |
| 2482 | |
| 2483 | return desc; |
| 2484 | } |
| 2485 | |
| 2486 | static struct dma_pl330_desc *pl330_get_desc(struct dma_pl330_chan *pch) |
| 2487 | { |
| 2488 | struct dma_pl330_dmac *pdmac = pch->dmac; |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2489 | u8 *peri_id = pch->chan.private; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2490 | struct dma_pl330_desc *desc; |
| 2491 | |
| 2492 | /* Pluck one desc from the pool of DMAC */ |
| 2493 | desc = pluck_desc(pdmac); |
| 2494 | |
| 2495 | /* If the DMAC pool is empty, alloc new */ |
| 2496 | if (!desc) { |
| 2497 | if (!add_desc(pdmac, GFP_ATOMIC, 1)) |
| 2498 | return NULL; |
| 2499 | |
| 2500 | /* Try again */ |
| 2501 | desc = pluck_desc(pdmac); |
| 2502 | if (!desc) { |
| 2503 | dev_err(pch->dmac->pif.dev, |
| 2504 | "%s:%d ALERT!\n", __func__, __LINE__); |
| 2505 | return NULL; |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | /* Initialize the descriptor */ |
| 2510 | desc->pchan = pch; |
| 2511 | desc->txd.cookie = 0; |
| 2512 | async_tx_ack(&desc->txd); |
| 2513 | |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2514 | desc->req.peri = peri_id ? pch->chan.chan_id : 0; |
Boojin Kim | 3ecf51a | 2011-12-26 18:55:47 +0900 | [diff] [blame] | 2515 | desc->rqcfg.pcfg = &pch->dmac->pif.pcfg; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2516 | |
| 2517 | dma_async_tx_descriptor_init(&desc->txd, &pch->chan); |
| 2518 | |
| 2519 | return desc; |
| 2520 | } |
| 2521 | |
| 2522 | static inline void fill_px(struct pl330_xfer *px, |
| 2523 | dma_addr_t dst, dma_addr_t src, size_t len) |
| 2524 | { |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2525 | px->bytes = len; |
| 2526 | px->dst_addr = dst; |
| 2527 | px->src_addr = src; |
| 2528 | } |
| 2529 | |
| 2530 | static struct dma_pl330_desc * |
| 2531 | __pl330_prep_dma_memcpy(struct dma_pl330_chan *pch, dma_addr_t dst, |
| 2532 | dma_addr_t src, size_t len) |
| 2533 | { |
| 2534 | struct dma_pl330_desc *desc = pl330_get_desc(pch); |
| 2535 | |
| 2536 | if (!desc) { |
| 2537 | dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n", |
| 2538 | __func__, __LINE__); |
| 2539 | return NULL; |
| 2540 | } |
| 2541 | |
| 2542 | /* |
| 2543 | * Ideally we should lookout for reqs bigger than |
| 2544 | * those that can be programmed with 256 bytes of |
| 2545 | * MC buffer, but considering a req size is seldom |
| 2546 | * going to be word-unaligned and more than 200MB, |
| 2547 | * we take it easy. |
| 2548 | * Also, should the limit is reached we'd rather |
| 2549 | * have the platform increase MC buffer size than |
| 2550 | * complicating this API driver. |
| 2551 | */ |
| 2552 | fill_px(&desc->px, dst, src, len); |
| 2553 | |
| 2554 | return desc; |
| 2555 | } |
| 2556 | |
| 2557 | /* Call after fixing burst size */ |
| 2558 | static inline int get_burst_len(struct dma_pl330_desc *desc, size_t len) |
| 2559 | { |
| 2560 | struct dma_pl330_chan *pch = desc->pchan; |
| 2561 | struct pl330_info *pi = &pch->dmac->pif; |
| 2562 | int burst_len; |
| 2563 | |
| 2564 | burst_len = pi->pcfg.data_bus_width / 8; |
| 2565 | burst_len *= pi->pcfg.data_buf_dep; |
| 2566 | burst_len >>= desc->rqcfg.brst_size; |
| 2567 | |
| 2568 | /* src/dst_burst_len can't be more than 16 */ |
| 2569 | if (burst_len > 16) |
| 2570 | burst_len = 16; |
| 2571 | |
| 2572 | while (burst_len > 1) { |
| 2573 | if (!(len % (burst_len << desc->rqcfg.brst_size))) |
| 2574 | break; |
| 2575 | burst_len--; |
| 2576 | } |
| 2577 | |
| 2578 | return burst_len; |
| 2579 | } |
| 2580 | |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2581 | static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic( |
| 2582 | struct dma_chan *chan, dma_addr_t dma_addr, size_t len, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 2583 | size_t period_len, enum dma_transfer_direction direction, |
Peter Ujfalusi | ec8b5e4 | 2012-09-14 15:05:47 +0300 | [diff] [blame] | 2584 | unsigned long flags, void *context) |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2585 | { |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2586 | struct dma_pl330_desc *desc = NULL, *first = NULL; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2587 | struct dma_pl330_chan *pch = to_pchan(chan); |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2588 | struct dma_pl330_dmac *pdmac = pch->dmac; |
| 2589 | unsigned int i; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2590 | dma_addr_t dst; |
| 2591 | dma_addr_t src; |
| 2592 | |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2593 | if (len % period_len != 0) |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2594 | return NULL; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2595 | |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2596 | if (!is_slave_direction(direction)) { |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2597 | dev_err(pch->dmac->pif.dev, "%s:%d Invalid dma direction\n", |
| 2598 | __func__, __LINE__); |
| 2599 | return NULL; |
| 2600 | } |
| 2601 | |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2602 | for (i = 0; i < len / period_len; i++) { |
| 2603 | desc = pl330_get_desc(pch); |
| 2604 | if (!desc) { |
| 2605 | dev_err(pch->dmac->pif.dev, "%s:%d Unable to fetch desc\n", |
| 2606 | __func__, __LINE__); |
| 2607 | |
| 2608 | if (!first) |
| 2609 | return NULL; |
| 2610 | |
| 2611 | spin_lock_irqsave(&pdmac->pool_lock, flags); |
| 2612 | |
| 2613 | while (!list_empty(&first->node)) { |
| 2614 | desc = list_entry(first->node.next, |
| 2615 | struct dma_pl330_desc, node); |
| 2616 | list_move_tail(&desc->node, &pdmac->desc_pool); |
| 2617 | } |
| 2618 | |
| 2619 | list_move_tail(&first->node, &pdmac->desc_pool); |
| 2620 | |
| 2621 | spin_unlock_irqrestore(&pdmac->pool_lock, flags); |
| 2622 | |
| 2623 | return NULL; |
| 2624 | } |
| 2625 | |
| 2626 | switch (direction) { |
| 2627 | case DMA_MEM_TO_DEV: |
| 2628 | desc->rqcfg.src_inc = 1; |
| 2629 | desc->rqcfg.dst_inc = 0; |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2630 | src = dma_addr; |
| 2631 | dst = pch->fifo_addr; |
| 2632 | break; |
| 2633 | case DMA_DEV_TO_MEM: |
| 2634 | desc->rqcfg.src_inc = 0; |
| 2635 | desc->rqcfg.dst_inc = 1; |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2636 | src = pch->fifo_addr; |
| 2637 | dst = dma_addr; |
| 2638 | break; |
| 2639 | default: |
| 2640 | break; |
| 2641 | } |
| 2642 | |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 2643 | desc->req.rqtype = direction; |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2644 | desc->rqcfg.brst_size = pch->burst_sz; |
| 2645 | desc->rqcfg.brst_len = 1; |
| 2646 | fill_px(&desc->px, dst, src, period_len); |
| 2647 | |
| 2648 | if (!first) |
| 2649 | first = desc; |
| 2650 | else |
| 2651 | list_add_tail(&desc->node, &first->node); |
| 2652 | |
| 2653 | dma_addr += period_len; |
| 2654 | } |
| 2655 | |
| 2656 | if (!desc) |
| 2657 | return NULL; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2658 | |
| 2659 | pch->cyclic = true; |
Lars-Peter Clausen | fc51446 | 2013-07-23 10:24:50 +0200 | [diff] [blame] | 2660 | desc->txd.flags = flags; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2661 | |
| 2662 | return &desc->txd; |
| 2663 | } |
| 2664 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2665 | static struct dma_async_tx_descriptor * |
| 2666 | pl330_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dst, |
| 2667 | dma_addr_t src, size_t len, unsigned long flags) |
| 2668 | { |
| 2669 | struct dma_pl330_desc *desc; |
| 2670 | struct dma_pl330_chan *pch = to_pchan(chan); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2671 | struct pl330_info *pi; |
| 2672 | int burst; |
| 2673 | |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 2674 | if (unlikely(!pch || !len)) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2675 | return NULL; |
| 2676 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2677 | pi = &pch->dmac->pif; |
| 2678 | |
| 2679 | desc = __pl330_prep_dma_memcpy(pch, dst, src, len); |
| 2680 | if (!desc) |
| 2681 | return NULL; |
| 2682 | |
| 2683 | desc->rqcfg.src_inc = 1; |
| 2684 | desc->rqcfg.dst_inc = 1; |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 2685 | desc->req.rqtype = DMA_MEM_TO_MEM; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2686 | |
| 2687 | /* Select max possible burst size */ |
| 2688 | burst = pi->pcfg.data_bus_width / 8; |
| 2689 | |
| 2690 | while (burst > 1) { |
| 2691 | if (!(len % burst)) |
| 2692 | break; |
| 2693 | burst /= 2; |
| 2694 | } |
| 2695 | |
| 2696 | desc->rqcfg.brst_size = 0; |
| 2697 | while (burst != (1 << desc->rqcfg.brst_size)) |
| 2698 | desc->rqcfg.brst_size++; |
| 2699 | |
| 2700 | desc->rqcfg.brst_len = get_burst_len(desc, len); |
| 2701 | |
| 2702 | desc->txd.flags = flags; |
| 2703 | |
| 2704 | return &desc->txd; |
| 2705 | } |
| 2706 | |
Chanho Park | 52a9d17 | 2013-08-09 20:11:33 +0900 | [diff] [blame] | 2707 | static void __pl330_giveback_desc(struct dma_pl330_dmac *pdmac, |
| 2708 | struct dma_pl330_desc *first) |
| 2709 | { |
| 2710 | unsigned long flags; |
| 2711 | struct dma_pl330_desc *desc; |
| 2712 | |
| 2713 | if (!first) |
| 2714 | return; |
| 2715 | |
| 2716 | spin_lock_irqsave(&pdmac->pool_lock, flags); |
| 2717 | |
| 2718 | while (!list_empty(&first->node)) { |
| 2719 | desc = list_entry(first->node.next, |
| 2720 | struct dma_pl330_desc, node); |
| 2721 | list_move_tail(&desc->node, &pdmac->desc_pool); |
| 2722 | } |
| 2723 | |
| 2724 | list_move_tail(&first->node, &pdmac->desc_pool); |
| 2725 | |
| 2726 | spin_unlock_irqrestore(&pdmac->pool_lock, flags); |
| 2727 | } |
| 2728 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2729 | static struct dma_async_tx_descriptor * |
| 2730 | pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2731 | unsigned int sg_len, enum dma_transfer_direction direction, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 2732 | unsigned long flg, void *context) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2733 | { |
| 2734 | struct dma_pl330_desc *first, *desc = NULL; |
| 2735 | struct dma_pl330_chan *pch = to_pchan(chan); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2736 | struct scatterlist *sg; |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 2737 | int i; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2738 | dma_addr_t addr; |
| 2739 | |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2740 | if (unlikely(!pch || !sgl || !sg_len)) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2741 | return NULL; |
| 2742 | |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 2743 | addr = pch->fifo_addr; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2744 | |
| 2745 | first = NULL; |
| 2746 | |
| 2747 | for_each_sg(sgl, sg, sg_len, i) { |
| 2748 | |
| 2749 | desc = pl330_get_desc(pch); |
| 2750 | if (!desc) { |
| 2751 | struct dma_pl330_dmac *pdmac = pch->dmac; |
| 2752 | |
| 2753 | dev_err(pch->dmac->pif.dev, |
| 2754 | "%s:%d Unable to fetch desc\n", |
| 2755 | __func__, __LINE__); |
Chanho Park | 52a9d17 | 2013-08-09 20:11:33 +0900 | [diff] [blame] | 2756 | __pl330_giveback_desc(pdmac, first); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2757 | |
| 2758 | return NULL; |
| 2759 | } |
| 2760 | |
| 2761 | if (!first) |
| 2762 | first = desc; |
| 2763 | else |
| 2764 | list_add_tail(&desc->node, &first->node); |
| 2765 | |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 2766 | if (direction == DMA_MEM_TO_DEV) { |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2767 | desc->rqcfg.src_inc = 1; |
| 2768 | desc->rqcfg.dst_inc = 0; |
| 2769 | fill_px(&desc->px, |
| 2770 | addr, sg_dma_address(sg), sg_dma_len(sg)); |
| 2771 | } else { |
| 2772 | desc->rqcfg.src_inc = 0; |
| 2773 | desc->rqcfg.dst_inc = 1; |
| 2774 | fill_px(&desc->px, |
| 2775 | sg_dma_address(sg), addr, sg_dma_len(sg)); |
| 2776 | } |
| 2777 | |
Boojin Kim | 1b9bb71 | 2011-09-02 09:44:30 +0900 | [diff] [blame] | 2778 | desc->rqcfg.brst_size = pch->burst_sz; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2779 | desc->rqcfg.brst_len = 1; |
Lars-Peter Clausen | 585a9d0 | 2014-07-06 20:32:18 +0200 | [diff] [blame] | 2780 | desc->req.rqtype = direction; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | /* Return the last desc in the chain */ |
| 2784 | desc->txd.flags = flg; |
| 2785 | return &desc->txd; |
| 2786 | } |
| 2787 | |
| 2788 | static irqreturn_t pl330_irq_handler(int irq, void *data) |
| 2789 | { |
| 2790 | if (pl330_update(data)) |
| 2791 | return IRQ_HANDLED; |
| 2792 | else |
| 2793 | return IRQ_NONE; |
| 2794 | } |
| 2795 | |
Lars-Peter Clausen | ca38ff1 | 2013-07-15 17:53:08 +0200 | [diff] [blame] | 2796 | #define PL330_DMA_BUSWIDTHS \ |
| 2797 | BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \ |
| 2798 | BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ |
| 2799 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ |
| 2800 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \ |
| 2801 | BIT(DMA_SLAVE_BUSWIDTH_8_BYTES) |
| 2802 | |
| 2803 | static int pl330_dma_device_slave_caps(struct dma_chan *dchan, |
| 2804 | struct dma_slave_caps *caps) |
| 2805 | { |
| 2806 | caps->src_addr_widths = PL330_DMA_BUSWIDTHS; |
| 2807 | caps->dstn_addr_widths = PL330_DMA_BUSWIDTHS; |
| 2808 | caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
| 2809 | caps->cmd_pause = false; |
| 2810 | caps->cmd_terminate = true; |
Lars-Peter Clausen | bfb9bb4 | 2014-01-11 14:02:17 +0100 | [diff] [blame] | 2811 | caps->residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR; |
Lars-Peter Clausen | ca38ff1 | 2013-07-15 17:53:08 +0200 | [diff] [blame] | 2812 | |
Lars-Peter Clausen | ca38ff1 | 2013-07-15 17:53:08 +0200 | [diff] [blame] | 2813 | return 0; |
| 2814 | } |
| 2815 | |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 2816 | static int |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 2817 | pl330_probe(struct amba_device *adev, const struct amba_id *id) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2818 | { |
| 2819 | struct dma_pl330_platdata *pdat; |
| 2820 | struct dma_pl330_dmac *pdmac; |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 2821 | struct dma_pl330_chan *pch, *_p; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2822 | struct pl330_info *pi; |
| 2823 | struct dma_device *pd; |
| 2824 | struct resource *res; |
| 2825 | int i, ret, irq; |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 2826 | int num_chan; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2827 | |
Jingoo Han | d4adcc0 | 2013-07-30 17:09:11 +0900 | [diff] [blame] | 2828 | pdat = dev_get_platdata(&adev->dev); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2829 | |
Russell King | 6411301 | 2013-06-27 10:29:32 +0100 | [diff] [blame] | 2830 | ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32)); |
| 2831 | if (ret) |
| 2832 | return ret; |
| 2833 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2834 | /* Allocate a new DMAC and its Channels */ |
Sachin Kamat | e4d43c1 | 2012-11-15 06:27:50 +0000 | [diff] [blame] | 2835 | pdmac = devm_kzalloc(&adev->dev, sizeof(*pdmac), GFP_KERNEL); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2836 | if (!pdmac) { |
| 2837 | dev_err(&adev->dev, "unable to allocate mem\n"); |
| 2838 | return -ENOMEM; |
| 2839 | } |
| 2840 | |
| 2841 | pi = &pdmac->pif; |
| 2842 | pi->dev = &adev->dev; |
| 2843 | pi->pl330_data = NULL; |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 2844 | pi->mcbufsz = pdat ? pdat->mcbuf_sz : 0; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2845 | |
| 2846 | res = &adev->res; |
Sachin Kamat | bcc7fa9 | 2013-03-04 14:36:27 +0530 | [diff] [blame] | 2847 | pi->base = devm_ioremap_resource(&adev->dev, res); |
| 2848 | if (IS_ERR(pi->base)) |
| 2849 | return PTR_ERR(pi->base); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2850 | |
Boojin Kim | a2f5203 | 2011-09-02 09:44:29 +0900 | [diff] [blame] | 2851 | amba_set_drvdata(adev, pdmac); |
| 2852 | |
Dan Carpenter | 02808b4 | 2013-11-08 12:50:24 +0300 | [diff] [blame] | 2853 | for (i = 0; i < AMBA_NR_IRQS; i++) { |
Michal Simek | e98b3ca | 2013-09-30 08:50:48 +0200 | [diff] [blame] | 2854 | irq = adev->irq[i]; |
| 2855 | if (irq) { |
| 2856 | ret = devm_request_irq(&adev->dev, irq, |
| 2857 | pl330_irq_handler, 0, |
| 2858 | dev_name(&adev->dev), pi); |
| 2859 | if (ret) |
| 2860 | return ret; |
| 2861 | } else { |
| 2862 | break; |
| 2863 | } |
| 2864 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2865 | |
Will Deacon | 0967717 | 2013-06-10 19:34:37 +0100 | [diff] [blame] | 2866 | pi->pcfg.periph_id = adev->periphid; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2867 | ret = pl330_add(pi); |
| 2868 | if (ret) |
Michal Simek | 173e838 | 2013-09-04 16:40:17 +0200 | [diff] [blame] | 2869 | return ret; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2870 | |
| 2871 | INIT_LIST_HEAD(&pdmac->desc_pool); |
| 2872 | spin_lock_init(&pdmac->pool_lock); |
| 2873 | |
| 2874 | /* Create a descriptor pool of default size */ |
| 2875 | if (!add_desc(pdmac, GFP_KERNEL, NR_DEFAULT_DESC)) |
| 2876 | dev_warn(&adev->dev, "unable to allocate desc\n"); |
| 2877 | |
| 2878 | pd = &pdmac->ddma; |
| 2879 | INIT_LIST_HEAD(&pd->channels); |
| 2880 | |
| 2881 | /* Initialize channel parameters */ |
Olof Johansson | c847382 | 2012-04-08 16:26:19 -0700 | [diff] [blame] | 2882 | if (pdat) |
| 2883 | num_chan = max_t(int, pdat->nr_valid_peri, pi->pcfg.num_chan); |
| 2884 | else |
| 2885 | num_chan = max_t(int, pi->pcfg.num_peri, pi->pcfg.num_chan); |
| 2886 | |
Lars-Peter Clausen | 70cbb16 | 2014-01-11 20:08:39 +0100 | [diff] [blame] | 2887 | pdmac->num_peripherals = num_chan; |
| 2888 | |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 2889 | pdmac->peripherals = kzalloc(num_chan * sizeof(*pch), GFP_KERNEL); |
Sachin Kamat | 61c6e75 | 2012-09-17 15:20:23 +0530 | [diff] [blame] | 2890 | if (!pdmac->peripherals) { |
| 2891 | ret = -ENOMEM; |
| 2892 | dev_err(&adev->dev, "unable to allocate pdmac->peripherals\n"); |
Sachin Kamat | e4d43c1 | 2012-11-15 06:27:50 +0000 | [diff] [blame] | 2893 | goto probe_err2; |
Sachin Kamat | 61c6e75 | 2012-09-17 15:20:23 +0530 | [diff] [blame] | 2894 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2895 | |
Rob Herring | 4e0e610 | 2011-07-25 16:05:04 -0500 | [diff] [blame] | 2896 | for (i = 0; i < num_chan; i++) { |
| 2897 | pch = &pdmac->peripherals[i]; |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 2898 | if (!adev->dev.of_node) |
| 2899 | pch->chan.private = pdat ? &pdat->peri_id[i] : NULL; |
| 2900 | else |
| 2901 | pch->chan.private = adev->dev.of_node; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2902 | |
Lars-Peter Clausen | 04abf5da | 2014-01-11 20:08:38 +0100 | [diff] [blame] | 2903 | INIT_LIST_HEAD(&pch->submitted_list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2904 | INIT_LIST_HEAD(&pch->work_list); |
Lars-Peter Clausen | 39ff861 | 2013-08-27 20:34:05 +0200 | [diff] [blame] | 2905 | INIT_LIST_HEAD(&pch->completed_list); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2906 | spin_lock_init(&pch->lock); |
| 2907 | pch->pl330_chid = NULL; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2908 | pch->chan.device = pd; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2909 | pch->dmac = pdmac; |
| 2910 | |
| 2911 | /* Add the channel to the DMAC list */ |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2912 | list_add_tail(&pch->chan.device_node, &pd->channels); |
| 2913 | } |
| 2914 | |
| 2915 | pd->dev = &adev->dev; |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 2916 | if (pdat) { |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2917 | pd->cap_mask = pdat->cap_mask; |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 2918 | } else { |
Thomas Abraham | cd07251 | 2011-10-24 11:43:11 +0200 | [diff] [blame] | 2919 | dma_cap_set(DMA_MEMCPY, pd->cap_mask); |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 2920 | if (pi->pcfg.num_peri) { |
| 2921 | dma_cap_set(DMA_SLAVE, pd->cap_mask); |
| 2922 | dma_cap_set(DMA_CYCLIC, pd->cap_mask); |
Tushar Behera | 5557a41 | 2012-08-29 10:16:25 +0530 | [diff] [blame] | 2923 | dma_cap_set(DMA_PRIVATE, pd->cap_mask); |
Thomas Abraham | 93ed554 | 2011-10-24 11:43:31 +0200 | [diff] [blame] | 2924 | } |
| 2925 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2926 | |
| 2927 | pd->device_alloc_chan_resources = pl330_alloc_chan_resources; |
| 2928 | pd->device_free_chan_resources = pl330_free_chan_resources; |
| 2929 | pd->device_prep_dma_memcpy = pl330_prep_dma_memcpy; |
Boojin Kim | 42bc9cf | 2011-09-02 09:44:33 +0900 | [diff] [blame] | 2930 | pd->device_prep_dma_cyclic = pl330_prep_dma_cyclic; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2931 | pd->device_tx_status = pl330_tx_status; |
| 2932 | pd->device_prep_slave_sg = pl330_prep_slave_sg; |
| 2933 | pd->device_control = pl330_control; |
| 2934 | pd->device_issue_pending = pl330_issue_pending; |
Lars-Peter Clausen | ca38ff1 | 2013-07-15 17:53:08 +0200 | [diff] [blame] | 2935 | pd->device_slave_caps = pl330_dma_device_slave_caps; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2936 | |
| 2937 | ret = dma_async_device_register(pd); |
| 2938 | if (ret) { |
| 2939 | dev_err(&adev->dev, "unable to register DMAC\n"); |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 2940 | goto probe_err3; |
| 2941 | } |
| 2942 | |
| 2943 | if (adev->dev.of_node) { |
| 2944 | ret = of_dma_controller_register(adev->dev.of_node, |
| 2945 | of_dma_pl330_xlate, pdmac); |
| 2946 | if (ret) { |
| 2947 | dev_err(&adev->dev, |
| 2948 | "unable to register DMA to the generic DT DMA helpers\n"); |
| 2949 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2950 | } |
Lars-Peter Clausen | b714b84 | 2013-11-25 16:07:46 +0100 | [diff] [blame] | 2951 | |
| 2952 | adev->dev.dma_parms = &pdmac->dma_parms; |
| 2953 | |
Vinod Koul | dbaf6d8 | 2013-09-02 21:54:48 +0530 | [diff] [blame] | 2954 | /* |
| 2955 | * This is the limit for transfers with a buswidth of 1, larger |
| 2956 | * buswidths will have larger limits. |
| 2957 | */ |
| 2958 | ret = dma_set_max_seg_size(&adev->dev, 1900800); |
| 2959 | if (ret) |
| 2960 | dev_err(&adev->dev, "unable to set the seg size\n"); |
| 2961 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2962 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2963 | dev_info(&adev->dev, |
| 2964 | "Loaded driver for PL330 DMAC-%d\n", adev->periphid); |
| 2965 | dev_info(&adev->dev, |
| 2966 | "\tDBUFF-%ux%ubytes Num_Chans-%u Num_Peri-%u Num_Events-%u\n", |
| 2967 | pi->pcfg.data_buf_dep, |
| 2968 | pi->pcfg.data_bus_width / 8, pi->pcfg.num_chan, |
| 2969 | pi->pcfg.num_peri, pi->pcfg.num_events); |
| 2970 | |
| 2971 | return 0; |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 2972 | probe_err3: |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 2973 | /* Idle the DMAC */ |
| 2974 | list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels, |
| 2975 | chan.device_node) { |
| 2976 | |
| 2977 | /* Remove the channel */ |
| 2978 | list_del(&pch->chan.device_node); |
| 2979 | |
| 2980 | /* Flush the channel */ |
| 2981 | pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0); |
| 2982 | pl330_free_chan_resources(&pch->chan); |
| 2983 | } |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2984 | probe_err2: |
Sachin Kamat | e4d43c1 | 2012-11-15 06:27:50 +0000 | [diff] [blame] | 2985 | pl330_del(pi); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2986 | |
| 2987 | return ret; |
| 2988 | } |
| 2989 | |
Greg Kroah-Hartman | 4bf27b8 | 2012-12-21 15:09:59 -0800 | [diff] [blame] | 2990 | static int pl330_remove(struct amba_device *adev) |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2991 | { |
| 2992 | struct dma_pl330_dmac *pdmac = amba_get_drvdata(adev); |
| 2993 | struct dma_pl330_chan *pch, *_p; |
| 2994 | struct pl330_info *pi; |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 2995 | |
| 2996 | if (!pdmac) |
| 2997 | return 0; |
| 2998 | |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 2999 | if (adev->dev.of_node) |
| 3000 | of_dma_controller_free(adev->dev.of_node); |
Padmavathi Venna | 421da89 | 2013-02-14 09:10:07 +0530 | [diff] [blame] | 3001 | |
Padmavathi Venna | 0b94c57 | 2013-03-05 14:55:31 +0530 | [diff] [blame] | 3002 | dma_async_device_unregister(&pdmac->ddma); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 3003 | |
| 3004 | /* Idle the DMAC */ |
| 3005 | list_for_each_entry_safe(pch, _p, &pdmac->ddma.channels, |
| 3006 | chan.device_node) { |
| 3007 | |
| 3008 | /* Remove the channel */ |
| 3009 | list_del(&pch->chan.device_node); |
| 3010 | |
| 3011 | /* Flush the channel */ |
| 3012 | pl330_control(&pch->chan, DMA_TERMINATE_ALL, 0); |
| 3013 | pl330_free_chan_resources(&pch->chan); |
| 3014 | } |
| 3015 | |
| 3016 | pi = &pdmac->pif; |
| 3017 | |
| 3018 | pl330_del(pi); |
| 3019 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | static struct amba_id pl330_ids[] = { |
| 3024 | { |
| 3025 | .id = 0x00041330, |
| 3026 | .mask = 0x000fffff, |
| 3027 | }, |
| 3028 | { 0, 0 }, |
| 3029 | }; |
| 3030 | |
Dave Martin | e8fa516 | 2011-10-05 15:15:20 +0100 | [diff] [blame] | 3031 | MODULE_DEVICE_TABLE(amba, pl330_ids); |
| 3032 | |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 3033 | static struct amba_driver pl330_driver = { |
| 3034 | .drv = { |
| 3035 | .owner = THIS_MODULE, |
| 3036 | .name = "dma-pl330", |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 3037 | }, |
| 3038 | .id_table = pl330_ids, |
| 3039 | .probe = pl330_probe, |
| 3040 | .remove = pl330_remove, |
| 3041 | }; |
| 3042 | |
viresh kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 3043 | module_amba_driver(pl330_driver); |
Jassi Brar | b3040e4 | 2010-05-23 20:28:19 -0700 | [diff] [blame] | 3044 | |
| 3045 | MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>"); |
| 3046 | MODULE_DESCRIPTION("API Driver for PL330 DMAC"); |
| 3047 | MODULE_LICENSE("GPL"); |