1 /* SPDX-License-Identifier: GPL-2.0+ */ 1 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@bootl 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 .buswidth = __buswidth, 19 .opcode = __opcode, 20 .nbytes = 1, 21 } 22 23 #define SPI_MEM_OP_ADDR(__nbytes, __val, __bus 24 { 25 .nbytes = __nbytes, 26 .val = __val, 27 .buswidth = __buswidth, 28 } 29 30 #define SPI_MEM_OP_NO_ADDR { } 31 32 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) 33 { 34 .nbytes = __nbytes, 35 .buswidth = __buswidth, 36 } 37 38 #define SPI_MEM_OP_NO_DUMMY { } 39 40 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __ 41 { 42 .dir = SPI_MEM_DATA_IN, 43 .nbytes = __nbytes, 44 .buf.in = __buf, 45 .buswidth = __buswidth, 46 } 47 48 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, _ 49 { 50 .dir = SPI_MEM_DATA_OUT, 51 .nbytes = __nbytes, 52 .buf.out = __buf, 53 .buswidth = __buswidth, 54 } 55 56 #define SPI_MEM_OP_NO_DATA { } 57 58 /** 59 * enum spi_mem_data_dir - describes the direc 60 * transfer from the c 61 * @SPI_MEM_NO_DATA: no data transferred 62 * @SPI_MEM_DATA_IN: data coming from the SPI 63 * @SPI_MEM_DATA_OUT: data sent to the SPI mem 64 */ 65 enum spi_mem_data_dir { 66 SPI_MEM_NO_DATA, 67 SPI_MEM_DATA_IN, 68 SPI_MEM_DATA_OUT, 69 }; 70 71 /** 72 * struct spi_mem_op - describes a SPI memory 73 * @cmd.nbytes: number of opcode bytes (only 1 74 * sent MSB-first. 75 * @cmd.buswidth: number of IO lines used to t 76 * @cmd.opcode: operation opcode 77 * @cmd.dtr: whether the command opcode should 78 * @addr.nbytes: number of address bytes to se 79 * does not need to send an addr 80 * @addr.buswidth: number of IO lines used to 81 * @addr.dtr: whether the address should be se 82 * @addr.val: address value. This value is alw 83 * Note that only @addr.nbytes are 84 * address value, so users should m 85 * assigned number of bytes. 86 * @dummy.nbytes: number of dummy bytes to sen 87 * be zero if the operation doe 88 * @dummy.buswidth: number of IO lanes used to 89 * @dummy.dtr: whether the dummy bytes should 90 * @data.buswidth: number of IO lanes used to 91 * @data.dtr: whether the data should be sent 92 * @data.ecc: whether error correction is requ 93 * @data.dir: direction of the transfer 94 * @data.nbytes: number of data bytes to send/ 95 * operation does not involve tr 96 * @data.buf.in: input buffer (must be DMA-abl 97 * @data.buf.out: output buffer (must be DMA-a 98 */ 99 struct spi_mem_op { 100 struct { 101 u8 nbytes; 102 u8 buswidth; 103 u8 dtr : 1; 104 u8 __pad : 7; 105 u16 opcode; 106 } cmd; 107 108 struct { 109 u8 nbytes; 110 u8 buswidth; 111 u8 dtr : 1; 112 u8 __pad : 7; 113 u64 val; 114 } addr; 115 116 struct { 117 u8 nbytes; 118 u8 buswidth; 119 u8 dtr : 1; 120 u8 __pad : 7; 121 } dummy; 122 123 struct { 124 u8 buswidth; 125 u8 dtr : 1; 126 u8 ecc : 1; 127 u8 __pad : 6; 128 enum spi_mem_data_dir dir; 129 unsigned int nbytes; 130 union { 131 void *in; 132 const void *out; 133 } buf; 134 } data; 135 }; 136 137 #define SPI_MEM_OP(__cmd, __addr, __dummy, __d 138 { 139 .cmd = __cmd, 140 .addr = __addr, 141 .dummy = __dummy, 142 .data = __data, 143 } 144 145 /** 146 * struct spi_mem_dirmap_info - Direct mapping 147 * @op_tmpl: operation template that should be 148 * the memory device is accessed 149 * @offset: absolute offset this direct mappin 150 * @length: length in byte of this direct mapp 151 * 152 * These information are used by the controlle 153 * the portion of memory that is directly mapp 154 * be used to access the device. 155 * A direct mapping is only valid for one dire 156 * direction is directly encoded in the ->op_t 157 */ 158 struct spi_mem_dirmap_info { 159 struct spi_mem_op op_tmpl; 160 u64 offset; 161 u64 length; 162 }; 163 164 /** 165 * struct spi_mem_dirmap_desc - Direct mapping 166 * @mem: the SPI memory device this direct map 167 * @info: information passed at direct mapping 168 * @nodirmap: set to 1 if the SPI controller d 169 * ->mem_ops->dirmap_create() or wh 170 * error. If @nodirmap is true, all 171 * calls will use spi_mem_exec_op() 172 * degraded mode that allows spi_me 173 * no matter whether the controller 174 * @priv: field pointing to controller specifi 175 * 176 * Common part of a direct mapping descriptor. 177 * spi_mem_dirmap_create() and controller impl 178 * can create/attach direct mapping resources 179 * field. 180 */ 181 struct spi_mem_dirmap_desc { 182 struct spi_mem *mem; 183 struct spi_mem_dirmap_info info; 184 unsigned int nodirmap; 185 void *priv; 186 }; 187 188 /** 189 * struct spi_mem - describes a SPI memory dev 190 * @spi: the underlying SPI device 191 * @drvpriv: spi_mem_driver private data 192 * @name: name of the SPI memory device 193 * 194 * Extra information that describe the SPI mem 195 * the controller to properly handle this devi 196 * 197 * One example would be the device size since 198 * mem devices through a io-mapped region. 199 */ 200 struct spi_mem { 201 struct spi_device *spi; 202 void *drvpriv; 203 const char *name; 204 }; 205 206 /** 207 * struct spi_mem_set_drvdata() - attach drive 208 * device 209 * @mem: memory device 210 * @data: data to attach to the memory device 211 */ 212 static inline void spi_mem_set_drvdata(struct 213 { 214 mem->drvpriv = data; 215 } 216 217 /** 218 * struct spi_mem_get_drvdata() - get driver p 219 * device 220 * @mem: memory device 221 * 222 * Return: the data attached to the mem device 223 */ 224 static inline void *spi_mem_get_drvdata(struct 225 { 226 return mem->drvpriv; 227 } 228 229 /** 230 * struct spi_controller_mem_ops - SPI memory 231 * @adjust_op_size: shrink the data xfer of an 232 * limitations (can be alignm 233 * limitations) 234 * @supports_op: check if an operation is supp 235 * @exec_op: execute a SPI memory operation 236 * not all driver provides supports_ 237 * if the op is not supported by the 238 * @get_name: get a custom name for the SPI me 239 * This might be needed if the cont 240 * to use the SPI mem layer and a c 241 * mtdparts compatible. 242 * Note that if the implementation 243 * dynamically, then it should do s 244 * have a ->free_name() function. 245 * @dirmap_create: create a direct mapping des 246 * access the memory device. T 247 * @dirmap_destroy: destroy a memory descripto 248 * ->dirmap_create() 249 * @dirmap_read: read data from the memory dev 250 * created by ->dirmap_create(). 251 * data than requested (for exam 252 * the currently mapped area), a 253 * spi_mem_dirmap_read() is resp 254 * this case. 255 * @dirmap_write: write data to the memory dev 256 * created by ->dirmap_create() 257 * data than requested (for exa 258 * the currently mapped area), 259 * spi_mem_dirmap_write() is re 260 * this case. 261 * @poll_status: poll memory device status unt 262 * when the timeout has expired. 263 * the last status value. 264 * 265 * This interface should be implemented by SPI 266 * high-level interface to execute SPI memory 267 * case for QSPI controllers. 268 * 269 * Note on ->dirmap_{read,write}(): drivers sh 270 * mapping from the CPU because doing that can 271 * SPI mem transaction to finish, and this wil 272 * unhappy and might make your system less rea 273 * use DMA to access this direct mapping. 274 */ 275 struct spi_controller_mem_ops { 276 int (*adjust_op_size)(struct spi_mem * 277 bool (*supports_op)(struct spi_mem *me 278 const struct spi_m 279 int (*exec_op)(struct spi_mem *mem, 280 const struct spi_mem_op 281 const char *(*get_name)(struct spi_mem 282 int (*dirmap_create)(struct spi_mem_di 283 void (*dirmap_destroy)(struct spi_mem_ 284 ssize_t (*dirmap_read)(struct spi_mem_ 285 u64 offs, size_ 286 ssize_t (*dirmap_write)(struct spi_mem 287 u64 offs, size 288 int (*poll_status)(struct spi_mem *mem 289 const struct spi_me 290 u16 mask, u16 match 291 unsigned long initi 292 unsigned long polli 293 unsigned long timeo 294 }; 295 296 /** 297 * struct spi_controller_mem_caps - SPI memory 298 * @dtr: Supports DTR operations 299 * @ecc: Supports operations with error correc 300 */ 301 struct spi_controller_mem_caps { 302 bool dtr; 303 bool ecc; 304 }; 305 306 #define spi_mem_controller_is_capable(ctlr, ca 307 ((ctlr)->mem_caps && (ctlr)->mem_caps- 308 309 /** 310 * struct spi_mem_driver - SPI memory driver 311 * @spidrv: inherit from a SPI driver 312 * @probe: probe a SPI memory. Usually where d 313 * place 314 * @remove: remove a SPI memory 315 * @shutdown: take appropriate action when the 316 * 317 * This is just a thin wrapper around a spi_dr 318 * allocating the spi_mem object and forwardin 319 * request to the spi_mem_driver. The reason w 320 * we might have to stuff more information int 321 * SPI controllers know more about the SPI mem 322 * having this intermediate layer allows us to 323 * useless fields to the spi_device object. 324 */ 325 struct spi_mem_driver { 326 struct spi_driver spidrv; 327 int (*probe)(struct spi_mem *mem); 328 int (*remove)(struct spi_mem *mem); 329 void (*shutdown)(struct spi_mem *mem); 330 }; 331 332 #if IS_ENABLED(CONFIG_SPI_MEM) 333 int spi_controller_dma_map_mem_op_data(struct 334 const s 335 struct 336 337 void spi_controller_dma_unmap_mem_op_data(stru 338 cons 339 stru 340 341 bool spi_mem_default_supports_op(struct spi_me 342 const struct 343 #else 344 static inline int 345 spi_controller_dma_map_mem_op_data(struct spi_ 346 const struc 347 struct sg_t 348 { 349 return -ENOTSUPP; 350 } 351 352 static inline void 353 spi_controller_dma_unmap_mem_op_data(struct sp 354 const str 355 struct sg 356 { 357 } 358 359 static inline 360 bool spi_mem_default_supports_op(struct spi_me 361 const struct 362 { 363 return false; 364 } 365 #endif /* CONFIG_SPI_MEM */ 366 367 int spi_mem_adjust_op_size(struct spi_mem *mem 368 369 bool spi_mem_supports_op(struct spi_mem *mem, 370 const struct spi_mem_ 371 372 int spi_mem_exec_op(struct spi_mem *mem, 373 const struct spi_mem_op *o 374 375 const char *spi_mem_get_name(struct spi_mem *m 376 377 struct spi_mem_dirmap_desc * 378 spi_mem_dirmap_create(struct spi_mem *mem, 379 const struct spi_mem_dir 380 void spi_mem_dirmap_destroy(struct spi_mem_dir 381 ssize_t spi_mem_dirmap_read(struct spi_mem_dir 382 u64 offs, size_t l 383 ssize_t spi_mem_dirmap_write(struct spi_mem_di 384 u64 offs, size_t 385 struct spi_mem_dirmap_desc * 386 devm_spi_mem_dirmap_create(struct device *dev, 387 const struct spi_me 388 void devm_spi_mem_dirmap_destroy(struct device 389 struct spi_me 390 391 int spi_mem_poll_status(struct spi_mem *mem, 392 const struct spi_mem_o 393 u16 mask, u16 match, 394 unsigned long initial_ 395 unsigned long polling_ 396 u16 timeout_ms); 397 398 int spi_mem_driver_register_with_owner(struct 399 struct 400 401 void spi_mem_driver_unregister(struct spi_mem_ 402 403 #define spi_mem_driver_register(__drv) 404 spi_mem_driver_register_with_owner(__d 405 406 #define module_spi_mem_driver(__drv) 407 module_driver(__drv, spi_mem_driver_re 408 spi_mem_driver_unregiste 409 410 #endif /* __LINUX_SPI_MEM_H */ 411
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.