1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Copyright (C) 2018 Exceet Electronics GmbH 4 * Copyright (C) 2018 Bootlin 5 * 6 * Author: 7 * Peter Pan <peterpandong@micron.com> 8 * Boris Brezillon <boris.brezillon@bootlin.com> 9 */ 10 11 #ifndef __LINUX_SPI_MEM_H 12 #define __LINUX_SPI_MEM_H 13 14 #include <linux/spi/spi.h> 15 16 #define SPI_MEM_OP_CMD(__opcode, __buswidth) \ 17 { \ 18 .nbytes = 1, \ 19 .buswidth = __buswidth, \ 20 .opcode = __opcode, \ 21 } 22 23 #define SPI_MEM_DTR_OP_RPT_CMD(__opcode, __buswidth) \ 24 { \ 25 .nbytes = 2, \ 26 .opcode = __opcode | __opcode << 8, \ 27 .buswidth = __buswidth, \ 28 .dtr = true, \ 29 } 30 31 #define SPI_MEM_DTR_OP_PACKED_CMD(__opcode, __addr, __buswidth) \ 32 { \ 33 .nbytes = 2, \ 34 .opcode = __opcode << 8 | __addr, \ 35 .buswidth = __buswidth, \ 36 .dtr = true, \ 37 } 38 39 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \ 40 { \ 41 .nbytes = __nbytes, \ 42 .buswidth = __buswidth, \ 43 .val = __val, \ 44 } 45 46 #define SPI_MEM_DTR_OP_ADDR(__nbytes, __val, __buswidth) \ 47 { \ 48 .nbytes = __nbytes, \ 49 .val = __val, \ 50 .buswidth = __buswidth, \ 51 .dtr = true, \ 52 } 53 54 #define SPI_MEM_DTR_OP_RPT_ADDR(__val, __buswidth) \ 55 { \ 56 .nbytes = 2, \ 57 .val = __val | __val << 8, \ 58 .buswidth = __buswidth, \ 59 .dtr = true, \ 60 } 61 62 #define SPI_MEM_DTR_OP_RPT_ADDR(__val, __buswidth) \ 63 { \ 64 .nbytes = 2, \ 65 .val = __val | __val << 8, \ 66 .buswidth = __buswidth, \ 67 .dtr = true, \ 68 } 69 70 #define SPI_MEM_OP_NO_ADDR { } 71 72 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \ 73 { \ 74 .nbytes = __nbytes, \ 75 .buswidth = __buswidth, \ 76 } 77 78 #define SPI_MEM_DTR_OP_DUMMY(__nbytes, __buswidth) \ 79 { \ 80 .nbytes = __nbytes, \ 81 .buswidth = __buswidth, \ 82 .dtr = true, \ 83 } 84 85 #define SPI_MEM_OP_NO_DUMMY { } 86 87 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \ 88 { \ 89 .buswidth = __buswidth, \ 90 .dir = SPI_MEM_DATA_IN, \ 91 .nbytes = __nbytes, \ 92 .buf.in = __buf, \ 93 } 94 95 #define SPI_MEM_DTR_OP_DATA_IN(__nbytes, __buf, __buswidth) \ 96 { \ 97 .dir = SPI_MEM_DATA_IN, \ 98 .nbytes = __nbytes, \ 99 .buf.in = __buf, \ 100 .buswidth = __buswidth, \ 101 .dtr = true, \ 102 } 103 104 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \ 105 { \ 106 .buswidth = __buswidth, \ 107 .dir = SPI_MEM_DATA_OUT, \ 108 .nbytes = __nbytes, \ 109 .buf.out = __buf, \ 110 } 111 112 #define SPI_MEM_DTR_OP_DATA_OUT(__nbytes, __buf, __buswidth) \ 113 { \ 114 .dir = SPI_MEM_DATA_OUT, \ 115 .nbytes = __nbytes, \ 116 .buf.out = __buf, \ 117 .buswidth = __buswidth, \ 118 .dtr = true, \ 119 } 120 121 #define SPI_MEM_OP_NO_DATA { } 122 123 /** 124 * enum spi_mem_data_dir - describes the direction of a SPI memory data 125 * transfer from the controller perspective 126 * @SPI_MEM_NO_DATA: no data transferred 127 * @SPI_MEM_DATA_IN: data coming from the SPI memory 128 * @SPI_MEM_DATA_OUT: data sent to the SPI memory 129 */ 130 enum spi_mem_data_dir { 131 SPI_MEM_NO_DATA, 132 SPI_MEM_DATA_IN, 133 SPI_MEM_DATA_OUT, 134 }; 135 136 #define SPI_MEM_OP_MAX_FREQ(__freq) \ 137 .max_freq = __freq 138 139 /** 140 * struct spi_mem_op - describes a SPI memory operation 141 * @cmd: the complete command 142 * @cmd.nbytes: number of opcode bytes (only 1 or 2 are valid). The opcode is 143 * sent MSB-first. 144 * @cmd.buswidth: number of IO lines used to transmit the command 145 * @cmd.opcode: operation opcode 146 * @cmd.dtr: whether the command opcode should be sent in DTR mode or not 147 * @addr: the address attributes 148 * @addr.nbytes: number of address bytes to send. Can be zero if the operation 149 * does not need to send an address 150 * @addr.buswidth: number of IO lines used to transmit the address cycles 151 * @addr.dtr: whether the address should be sent in DTR mode or not 152 * @addr.val: address value. This value is always sent MSB first on the bus. 153 * Note that only @addr.nbytes are taken into account in this 154 * address value, so users should make sure the value fits in the 155 * assigned number of bytes. 156 * @dummy: data for dummy operation 157 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can 158 * be zero if the operation does not require dummy bytes 159 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes 160 * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not 161 * @data: the data attributes 162 * @data.buswidth: number of IO lanes used to send/receive the data 163 * @data.dtr: whether the data should be sent in DTR mode or not 164 * @data.ecc: whether error correction is required or not 165 * @data.swap16: whether the byte order of 16-bit words is swapped when read 166 * or written in Octal DTR mode compared to STR mode. 167 * @data.dir: direction of the transfer 168 * @data.nbytes: number of data bytes to send/receive. Can be zero if the 169 * operation does not involve transferring data 170 * @data.buf.in: input buffer (must be DMA-able) 171 * @data.buf.out: output buffer (must be DMA-able) 172 * @max_freq: frequency limitation wrt this operation. 0 means there is no 173 * specific constraint and the highest achievable frequency can be 174 * attempted. 175 */ 176 struct spi_mem_op { 177 struct { 178 u8 nbytes; 179 u8 buswidth; 180 u8 dtr : 1; 181 u8 __pad : 7; 182 u16 opcode; 183 } cmd; 184 185 struct { 186 u8 nbytes; 187 u8 buswidth; 188 u8 dtr : 1; 189 u8 __pad : 7; 190 u64 val; 191 } addr; 192 193 struct { 194 u8 nbytes; 195 u8 buswidth; 196 u8 dtr : 1; 197 u8 __pad : 7; 198 } dummy; 199 200 struct { 201 u8 buswidth; 202 u8 dtr : 1; 203 u8 ecc : 1; 204 u8 swap16 : 1; 205 u8 __pad : 5; 206 enum spi_mem_data_dir dir; 207 unsigned int nbytes; 208 union { 209 void *in; 210 const void *out; 211 } buf; 212 } data; 213 214 unsigned int max_freq; 215 }; 216 217 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data, ...) \ 218 { \ 219 .cmd = __cmd, \ 220 .addr = __addr, \ 221 .dummy = __dummy, \ 222 .data = __data, \ 223 __VA_ARGS__ \ 224 } 225 226 /** 227 * struct spi_mem_dirmap_info - Direct mapping information 228 * @op_tmpl: operation template that should be used by the direct mapping when 229 * the memory device is accessed 230 * @offset: absolute offset this direct mapping is pointing to 231 * @length: length in byte of this direct mapping 232 * 233 * These information are used by the controller specific implementation to know 234 * the portion of memory that is directly mapped and the spi_mem_op that should 235 * be used to access the device. 236 * A direct mapping is only valid for one direction (read or write) and this 237 * direction is directly encoded in the ->op_tmpl.data.dir field. 238 */ 239 struct spi_mem_dirmap_info { 240 struct spi_mem_op op_tmpl; 241 u64 offset; 242 u64 length; 243 }; 244 245 /** 246 * struct spi_mem_dirmap_desc - Direct mapping descriptor 247 * @mem: the SPI memory device this direct mapping is attached to 248 * @info: information passed at direct mapping creation time 249 * @nodirmap: set to 1 if the SPI controller does not implement 250 * ->mem_ops->dirmap_create() or when this function returned an 251 * error. If @nodirmap is true, all spi_mem_dirmap_{read,write}() 252 * calls will use spi_mem_exec_op() to access the memory. This is a 253 * degraded mode that allows spi_mem drivers to use the same code 254 * no matter whether the controller supports direct mapping or not 255 * @priv: field pointing to controller specific data 256 * 257 * Common part of a direct mapping descriptor. This object is created by 258 * spi_mem_dirmap_create() and controller implementation of ->create_dirmap() 259 * can create/attach direct mapping resources to the descriptor in the ->priv 260 * field. 261 */ 262 struct spi_mem_dirmap_desc { 263 struct spi_mem *mem; 264 struct spi_mem_dirmap_info info; 265 unsigned int nodirmap; 266 void *priv; 267 }; 268 269 /** 270 * struct spi_mem - describes a SPI memory device 271 * @spi: the underlying SPI device 272 * @drvpriv: spi_mem_driver private data 273 * @name: name of the SPI memory device 274 * 275 * Extra information that describe the SPI memory device and may be needed by 276 * the controller to properly handle this device should be placed here. 277 * 278 * One example would be the device size since some controller expose their SPI 279 * mem devices through a io-mapped region. 280 */ 281 struct spi_mem { 282 struct spi_device *spi; 283 void *drvpriv; 284 const char *name; 285 }; 286 287 /** 288 * spi_mem_set_drvdata() - attach driver private data to a SPI mem 289 * device 290 * @mem: memory device 291 * @data: data to attach to the memory device 292 */ 293 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data) 294 { 295 mem->drvpriv = data; 296 } 297 298 /** 299 * spi_mem_get_drvdata() - get driver private data attached to a SPI mem 300 * device 301 * @mem: memory device 302 * 303 * Return: the data attached to the mem device. 304 */ 305 static inline void *spi_mem_get_drvdata(struct spi_mem *mem) 306 { 307 return mem->drvpriv; 308 } 309 310 /** 311 * struct spi_controller_mem_ops - SPI memory operations 312 * @adjust_op_size: shrink the data xfer of an operation to match controller's 313 * limitations (can be alignment or max RX/TX size 314 * limitations) 315 * @supports_op: check if an operation is supported by the controller 316 * @exec_op: execute a SPI memory operation 317 * not all driver provides supports_op(), so it can return -EOPNOTSUPP 318 * if the op is not supported by the driver/controller 319 * @get_name: get a custom name for the SPI mem device from the controller. 320 * This might be needed if the controller driver has been ported 321 * to use the SPI mem layer and a custom name is used to keep 322 * mtdparts compatible. 323 * Note that if the implementation of this function allocates memory 324 * dynamically, then it should do so with devm_xxx(), as we don't 325 * have a ->free_name() function. 326 * @dirmap_create: create a direct mapping descriptor that can later be used to 327 * access the memory device. This method is optional 328 * @dirmap_destroy: destroy a memory descriptor previous created by 329 * ->dirmap_create() 330 * @dirmap_read: read data from the memory device using the direct mapping 331 * created by ->dirmap_create(). The function can return less 332 * data than requested (for example when the request is crossing 333 * the currently mapped area), and the caller of 334 * spi_mem_dirmap_read() is responsible for calling it again in 335 * this case. 336 * @dirmap_write: write data to the memory device using the direct mapping 337 * created by ->dirmap_create(). The function can return less 338 * data than requested (for example when the request is crossing 339 * the currently mapped area), and the caller of 340 * spi_mem_dirmap_write() is responsible for calling it again in 341 * this case. 342 * @poll_status: poll memory device status until (status & mask) == match or 343 * when the timeout has expired. It fills the data buffer with 344 * the last status value. 345 * 346 * This interface should be implemented by SPI controllers providing an 347 * high-level interface to execute SPI memory operation, which is usually the 348 * case for QSPI controllers. 349 * 350 * Note on ->dirmap_{read,write}(): drivers should avoid accessing the direct 351 * mapping from the CPU because doing that can stall the CPU waiting for the 352 * SPI mem transaction to finish, and this will make real-time maintainers 353 * unhappy and might make your system less reactive. Instead, drivers should 354 * use DMA to access this direct mapping. 355 */ 356 struct spi_controller_mem_ops { 357 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op); 358 bool (*supports_op)(struct spi_mem *mem, 359 const struct spi_mem_op *op); 360 int (*exec_op)(struct spi_mem *mem, 361 const struct spi_mem_op *op); 362 const char *(*get_name)(struct spi_mem *mem); 363 int (*dirmap_create)(struct spi_mem_dirmap_desc *desc); 364 void (*dirmap_destroy)(struct spi_mem_dirmap_desc *desc); 365 ssize_t (*dirmap_read)(struct spi_mem_dirmap_desc *desc, 366 u64 offs, size_t len, void *buf); 367 ssize_t (*dirmap_write)(struct spi_mem_dirmap_desc *desc, 368 u64 offs, size_t len, const void *buf); 369 int (*poll_status)(struct spi_mem *mem, 370 const struct spi_mem_op *op, 371 u16 mask, u16 match, 372 unsigned long initial_delay_us, 373 unsigned long polling_rate_us, 374 unsigned long timeout_ms); 375 }; 376 377 /** 378 * struct spi_controller_mem_caps - SPI memory controller capabilities 379 * @dtr: Supports DTR operations 380 * @ecc: Supports operations with error correction 381 * @swap16: Supports swapping bytes on a 16 bit boundary when configured in 382 * Octal DTR 383 * @per_op_freq: Supports per operation frequency switching 384 */ 385 struct spi_controller_mem_caps { 386 bool dtr; 387 bool ecc; 388 bool swap16; 389 bool per_op_freq; 390 }; 391 392 #define spi_mem_controller_is_capable(ctlr, cap) \ 393 ((ctlr)->mem_caps && (ctlr)->mem_caps->cap) 394 395 /** 396 * struct spi_mem_driver - SPI memory driver 397 * @spidrv: inherit from a SPI driver 398 * @probe: probe a SPI memory. Usually where detection/initialization takes 399 * place 400 * @remove: remove a SPI memory 401 * @shutdown: take appropriate action when the system is shutdown 402 * 403 * This is just a thin wrapper around a spi_driver. The core takes care of 404 * allocating the spi_mem object and forwarding the probe/remove/shutdown 405 * request to the spi_mem_driver. The reason we use this wrapper is because 406 * we might have to stuff more information into the spi_mem struct to let 407 * SPI controllers know more about the SPI memory they interact with, and 408 * having this intermediate layer allows us to do that without adding more 409 * useless fields to the spi_device object. 410 */ 411 struct spi_mem_driver { 412 struct spi_driver spidrv; 413 int (*probe)(struct spi_mem *mem); 414 int (*remove)(struct spi_mem *mem); 415 void (*shutdown)(struct spi_mem *mem); 416 }; 417 418 #if IS_ENABLED(CONFIG_SPI_MEM) 419 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 420 const struct spi_mem_op *op, 421 struct sg_table *sg); 422 423 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 424 const struct spi_mem_op *op, 425 struct sg_table *sg); 426 427 bool spi_mem_default_supports_op(struct spi_mem *mem, 428 const struct spi_mem_op *op); 429 #else 430 static inline int 431 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 432 const struct spi_mem_op *op, 433 struct sg_table *sg) 434 { 435 return -ENOTSUPP; 436 } 437 438 static inline void 439 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 440 const struct spi_mem_op *op, 441 struct sg_table *sg) 442 { 443 } 444 445 static inline 446 bool spi_mem_default_supports_op(struct spi_mem *mem, 447 const struct spi_mem_op *op) 448 { 449 return false; 450 } 451 #endif /* CONFIG_SPI_MEM */ 452 453 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op); 454 void spi_mem_adjust_op_freq(struct spi_mem *mem, struct spi_mem_op *op); 455 u64 spi_mem_calc_op_duration(struct spi_mem *mem, struct spi_mem_op *op); 456 457 bool spi_mem_supports_op(struct spi_mem *mem, 458 const struct spi_mem_op *op); 459 460 int spi_mem_exec_op(struct spi_mem *mem, 461 const struct spi_mem_op *op); 462 463 const char *spi_mem_get_name(struct spi_mem *mem); 464 465 struct spi_mem_dirmap_desc * 466 spi_mem_dirmap_create(struct spi_mem *mem, 467 const struct spi_mem_dirmap_info *info); 468 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc); 469 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc, 470 u64 offs, size_t len, void *buf); 471 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc, 472 u64 offs, size_t len, const void *buf); 473 struct spi_mem_dirmap_desc * 474 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, 475 const struct spi_mem_dirmap_info *info); 476 void devm_spi_mem_dirmap_destroy(struct device *dev, 477 struct spi_mem_dirmap_desc *desc); 478 479 int spi_mem_poll_status(struct spi_mem *mem, 480 const struct spi_mem_op *op, 481 u16 mask, u16 match, 482 unsigned long initial_delay_us, 483 unsigned long polling_delay_us, 484 u16 timeout_ms); 485 486 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv, 487 struct module *owner); 488 489 void spi_mem_driver_unregister(struct spi_mem_driver *drv); 490 491 #define spi_mem_driver_register(__drv) \ 492 spi_mem_driver_register_with_owner(__drv, THIS_MODULE) 493 494 #define module_spi_mem_driver(__drv) \ 495 module_driver(__drv, spi_mem_driver_register, \ 496 spi_mem_driver_unregister) 497 498 #endif /* __LINUX_SPI_MEM_H */ 499