1 /* SPDX-License-Identifier: GPL-2.0+ */ 1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 2 /* 3 * Copyright (C) 2018 Exceet Electronics GmbH 3 * Copyright (C) 2018 Exceet Electronics GmbH 4 * Copyright (C) 2018 Bootlin 4 * Copyright (C) 2018 Bootlin 5 * 5 * 6 * Author: !! 6 * Author: Boris Brezillon <boris.brezillon@bootlin.com> 7 * Peter Pan <peterpandong@micron.com> << 8 * Boris Brezillon <boris.brezillon@bootl << 9 */ 7 */ 10 8 11 #ifndef __LINUX_SPI_MEM_H 9 #ifndef __LINUX_SPI_MEM_H 12 #define __LINUX_SPI_MEM_H 10 #define __LINUX_SPI_MEM_H 13 11 14 #include <linux/spi/spi.h> 12 #include <linux/spi/spi.h> 15 13 16 #define SPI_MEM_OP_CMD(__opcode, __buswidth) 14 #define SPI_MEM_OP_CMD(__opcode, __buswidth) \ 17 { 15 { \ 18 .buswidth = __buswidth, 16 .buswidth = __buswidth, \ 19 .opcode = __opcode, 17 .opcode = __opcode, \ 20 .nbytes = 1, << 21 } 18 } 22 19 23 #define SPI_MEM_OP_ADDR(__nbytes, __val, __bus 20 #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \ 24 { 21 { \ 25 .nbytes = __nbytes, 22 .nbytes = __nbytes, \ 26 .val = __val, 23 .val = __val, \ 27 .buswidth = __buswidth, 24 .buswidth = __buswidth, \ 28 } 25 } 29 26 30 #define SPI_MEM_OP_NO_ADDR { } 27 #define SPI_MEM_OP_NO_ADDR { } 31 28 32 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) 29 #define SPI_MEM_OP_DUMMY(__nbytes, __buswidth) \ 33 { 30 { \ 34 .nbytes = __nbytes, 31 .nbytes = __nbytes, \ 35 .buswidth = __buswidth, 32 .buswidth = __buswidth, \ 36 } 33 } 37 34 38 #define SPI_MEM_OP_NO_DUMMY { } 35 #define SPI_MEM_OP_NO_DUMMY { } 39 36 40 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __ 37 #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \ 41 { 38 { \ 42 .dir = SPI_MEM_DATA_IN, 39 .dir = SPI_MEM_DATA_IN, \ 43 .nbytes = __nbytes, 40 .nbytes = __nbytes, \ 44 .buf.in = __buf, 41 .buf.in = __buf, \ 45 .buswidth = __buswidth, 42 .buswidth = __buswidth, \ 46 } 43 } 47 44 48 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, _ 45 #define SPI_MEM_OP_DATA_OUT(__nbytes, __buf, __buswidth) \ 49 { 46 { \ 50 .dir = SPI_MEM_DATA_OUT, 47 .dir = SPI_MEM_DATA_OUT, \ 51 .nbytes = __nbytes, 48 .nbytes = __nbytes, \ 52 .buf.out = __buf, 49 .buf.out = __buf, \ 53 .buswidth = __buswidth, 50 .buswidth = __buswidth, \ 54 } 51 } 55 52 56 #define SPI_MEM_OP_NO_DATA { } 53 #define SPI_MEM_OP_NO_DATA { } 57 54 58 /** 55 /** 59 * enum spi_mem_data_dir - describes the direc 56 * enum spi_mem_data_dir - describes the direction of a SPI memory data 60 * transfer from the c 57 * transfer from the controller perspective 61 * @SPI_MEM_NO_DATA: no data transferred << 62 * @SPI_MEM_DATA_IN: data coming from the SPI 58 * @SPI_MEM_DATA_IN: data coming from the SPI memory 63 * @SPI_MEM_DATA_OUT: data sent to the SPI mem !! 59 * @SPI_MEM_DATA_OUT: data sent the SPI memory 64 */ 60 */ 65 enum spi_mem_data_dir { 61 enum spi_mem_data_dir { 66 SPI_MEM_NO_DATA, << 67 SPI_MEM_DATA_IN, 62 SPI_MEM_DATA_IN, 68 SPI_MEM_DATA_OUT, 63 SPI_MEM_DATA_OUT, 69 }; 64 }; 70 65 71 /** 66 /** 72 * struct spi_mem_op - describes a SPI memory 67 * struct spi_mem_op - describes a SPI memory operation 73 * @cmd.nbytes: number of opcode bytes (only 1 << 74 * sent MSB-first. << 75 * @cmd.buswidth: number of IO lines used to t 68 * @cmd.buswidth: number of IO lines used to transmit the command 76 * @cmd.opcode: operation opcode 69 * @cmd.opcode: operation opcode 77 * @cmd.dtr: whether the command opcode should << 78 * @addr.nbytes: number of address bytes to se 70 * @addr.nbytes: number of address bytes to send. Can be zero if the operation 79 * does not need to send an addr 71 * does not need to send an address 80 * @addr.buswidth: number of IO lines used to 72 * @addr.buswidth: number of IO lines used to transmit the address cycles 81 * @addr.dtr: whether the address should be se << 82 * @addr.val: address value. This value is alw 73 * @addr.val: address value. This value is always sent MSB first on the bus. 83 * Note that only @addr.nbytes are 74 * Note that only @addr.nbytes are taken into account in this 84 * address value, so users should m 75 * address value, so users should make sure the value fits in the 85 * assigned number of bytes. 76 * assigned number of bytes. 86 * @dummy.nbytes: number of dummy bytes to sen 77 * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can 87 * be zero if the operation doe 78 * be zero if the operation does not require dummy bytes 88 * @dummy.buswidth: number of IO lanes used to 79 * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes 89 * @dummy.dtr: whether the dummy bytes should << 90 * @data.buswidth: number of IO lanes used to 80 * @data.buswidth: number of IO lanes used to send/receive the data 91 * @data.dtr: whether the data should be sent << 92 * @data.ecc: whether error correction is requ << 93 * @data.dir: direction of the transfer 81 * @data.dir: direction of the transfer 94 * @data.nbytes: number of data bytes to send/ !! 82 * @data.buf.in: input buffer 95 * operation does not involve tr !! 83 * @data.buf.out: output buffer 96 * @data.buf.in: input buffer (must be DMA-abl << 97 * @data.buf.out: output buffer (must be DMA-a << 98 */ 84 */ 99 struct spi_mem_op { 85 struct spi_mem_op { 100 struct { 86 struct { 101 u8 nbytes; << 102 u8 buswidth; 87 u8 buswidth; 103 u8 dtr : 1; !! 88 u8 opcode; 104 u8 __pad : 7; << 105 u16 opcode; << 106 } cmd; 89 } cmd; 107 90 108 struct { 91 struct { 109 u8 nbytes; 92 u8 nbytes; 110 u8 buswidth; 93 u8 buswidth; 111 u8 dtr : 1; << 112 u8 __pad : 7; << 113 u64 val; 94 u64 val; 114 } addr; 95 } addr; 115 96 116 struct { 97 struct { 117 u8 nbytes; 98 u8 nbytes; 118 u8 buswidth; 99 u8 buswidth; 119 u8 dtr : 1; << 120 u8 __pad : 7; << 121 } dummy; 100 } dummy; 122 101 123 struct { 102 struct { 124 u8 buswidth; 103 u8 buswidth; 125 u8 dtr : 1; << 126 u8 ecc : 1; << 127 u8 __pad : 6; << 128 enum spi_mem_data_dir dir; 104 enum spi_mem_data_dir dir; 129 unsigned int nbytes; 105 unsigned int nbytes; >> 106 /* buf.{in,out} must be DMA-able. */ 130 union { 107 union { 131 void *in; 108 void *in; 132 const void *out; 109 const void *out; 133 } buf; 110 } buf; 134 } data; 111 } data; 135 }; 112 }; 136 113 137 #define SPI_MEM_OP(__cmd, __addr, __dummy, __d 114 #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \ 138 { 115 { \ 139 .cmd = __cmd, 116 .cmd = __cmd, \ 140 .addr = __addr, 117 .addr = __addr, \ 141 .dummy = __dummy, 118 .dummy = __dummy, \ 142 .data = __data, 119 .data = __data, \ 143 } 120 } 144 121 145 /** 122 /** 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 123 * struct spi_mem - describes a SPI memory device 190 * @spi: the underlying SPI device 124 * @spi: the underlying SPI device 191 * @drvpriv: spi_mem_driver private data !! 125 * @drvpriv: spi_mem_drviver private data 192 * @name: name of the SPI memory device << 193 * 126 * 194 * Extra information that describe the SPI mem 127 * Extra information that describe the SPI memory device and may be needed by 195 * the controller to properly handle this devi 128 * the controller to properly handle this device should be placed here. 196 * 129 * 197 * One example would be the device size since 130 * One example would be the device size since some controller expose their SPI 198 * mem devices through a io-mapped region. 131 * mem devices through a io-mapped region. 199 */ 132 */ 200 struct spi_mem { 133 struct spi_mem { 201 struct spi_device *spi; 134 struct spi_device *spi; 202 void *drvpriv; 135 void *drvpriv; 203 const char *name; << 204 }; 136 }; 205 137 206 /** 138 /** 207 * struct spi_mem_set_drvdata() - attach drive 139 * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem 208 * device 140 * device 209 * @mem: memory device 141 * @mem: memory device 210 * @data: data to attach to the memory device 142 * @data: data to attach to the memory device 211 */ 143 */ 212 static inline void spi_mem_set_drvdata(struct 144 static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data) 213 { 145 { 214 mem->drvpriv = data; 146 mem->drvpriv = data; 215 } 147 } 216 148 217 /** 149 /** 218 * struct spi_mem_get_drvdata() - get driver p 150 * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem 219 * device 151 * device 220 * @mem: memory device 152 * @mem: memory device 221 * 153 * 222 * Return: the data attached to the mem device 154 * Return: the data attached to the mem device. 223 */ 155 */ 224 static inline void *spi_mem_get_drvdata(struct 156 static inline void *spi_mem_get_drvdata(struct spi_mem *mem) 225 { 157 { 226 return mem->drvpriv; 158 return mem->drvpriv; 227 } 159 } 228 160 229 /** 161 /** 230 * struct spi_controller_mem_ops - SPI memory 162 * struct spi_controller_mem_ops - SPI memory operations 231 * @adjust_op_size: shrink the data xfer of an 163 * @adjust_op_size: shrink the data xfer of an operation to match controller's 232 * limitations (can be alignm !! 164 * limitations (can be alignment of max RX/TX size 233 * limitations) 165 * limitations) 234 * @supports_op: check if an operation is supp 166 * @supports_op: check if an operation is supported by the controller 235 * @exec_op: execute a SPI memory operation 167 * @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 * 168 * 265 * This interface should be implemented by SPI 169 * This interface should be implemented by SPI controllers providing an 266 * high-level interface to execute SPI memory 170 * high-level interface to execute SPI memory operation, which is usually the 267 * case for QSPI controllers. 171 * 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 */ 172 */ 275 struct spi_controller_mem_ops { 173 struct spi_controller_mem_ops { 276 int (*adjust_op_size)(struct spi_mem * 174 int (*adjust_op_size)(struct spi_mem *mem, struct spi_mem_op *op); 277 bool (*supports_op)(struct spi_mem *me 175 bool (*supports_op)(struct spi_mem *mem, 278 const struct spi_m 176 const struct spi_mem_op *op); 279 int (*exec_op)(struct spi_mem *mem, 177 int (*exec_op)(struct spi_mem *mem, 280 const struct spi_mem_op 178 const struct spi_mem_op *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 }; 179 }; 295 180 296 /** 181 /** 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 182 * struct spi_mem_driver - SPI memory driver 311 * @spidrv: inherit from a SPI driver 183 * @spidrv: inherit from a SPI driver 312 * @probe: probe a SPI memory. Usually where d 184 * @probe: probe a SPI memory. Usually where detection/initialization takes 313 * place 185 * place 314 * @remove: remove a SPI memory 186 * @remove: remove a SPI memory 315 * @shutdown: take appropriate action when the 187 * @shutdown: take appropriate action when the system is shutdown 316 * 188 * 317 * This is just a thin wrapper around a spi_dr 189 * This is just a thin wrapper around a spi_driver. The core takes care of 318 * allocating the spi_mem object and forwardin 190 * allocating the spi_mem object and forwarding the probe/remove/shutdown 319 * request to the spi_mem_driver. The reason w 191 * request to the spi_mem_driver. The reason we use this wrapper is because 320 * we might have to stuff more information int 192 * we might have to stuff more information into the spi_mem struct to let 321 * SPI controllers know more about the SPI mem 193 * SPI controllers know more about the SPI memory they interact with, and 322 * having this intermediate layer allows us to 194 * having this intermediate layer allows us to do that without adding more 323 * useless fields to the spi_device object. 195 * useless fields to the spi_device object. 324 */ 196 */ 325 struct spi_mem_driver { 197 struct spi_mem_driver { 326 struct spi_driver spidrv; 198 struct spi_driver spidrv; 327 int (*probe)(struct spi_mem *mem); 199 int (*probe)(struct spi_mem *mem); 328 int (*remove)(struct spi_mem *mem); 200 int (*remove)(struct spi_mem *mem); 329 void (*shutdown)(struct spi_mem *mem); 201 void (*shutdown)(struct spi_mem *mem); 330 }; 202 }; 331 203 332 #if IS_ENABLED(CONFIG_SPI_MEM) 204 #if IS_ENABLED(CONFIG_SPI_MEM) 333 int spi_controller_dma_map_mem_op_data(struct 205 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 334 const s 206 const struct spi_mem_op *op, 335 struct 207 struct sg_table *sg); 336 208 337 void spi_controller_dma_unmap_mem_op_data(stru 209 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 338 cons 210 const struct spi_mem_op *op, 339 stru 211 struct sg_table *sg); 340 << 341 bool spi_mem_default_supports_op(struct spi_me << 342 const struct << 343 #else 212 #else 344 static inline int 213 static inline int 345 spi_controller_dma_map_mem_op_data(struct spi_ 214 spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, 346 const struc 215 const struct spi_mem_op *op, 347 struct sg_t 216 struct sg_table *sg) 348 { 217 { 349 return -ENOTSUPP; 218 return -ENOTSUPP; 350 } 219 } 351 220 352 static inline void 221 static inline void 353 spi_controller_dma_unmap_mem_op_data(struct sp 222 spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, 354 const str 223 const struct spi_mem_op *op, 355 struct sg 224 struct sg_table *sg) 356 { 225 { 357 } 226 } 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 */ 227 #endif /* CONFIG_SPI_MEM */ 366 228 367 int spi_mem_adjust_op_size(struct spi_mem *mem 229 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op); 368 230 369 bool spi_mem_supports_op(struct spi_mem *mem, 231 bool spi_mem_supports_op(struct spi_mem *mem, 370 const struct spi_mem_ 232 const struct spi_mem_op *op); 371 233 372 int spi_mem_exec_op(struct spi_mem *mem, 234 int spi_mem_exec_op(struct spi_mem *mem, 373 const struct spi_mem_op *o 235 const struct spi_mem_op *op); 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 236 398 int spi_mem_driver_register_with_owner(struct 237 int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv, 399 struct 238 struct module *owner); 400 239 401 void spi_mem_driver_unregister(struct spi_mem_ 240 void spi_mem_driver_unregister(struct spi_mem_driver *drv); 402 241 403 #define spi_mem_driver_register(__drv) 242 #define spi_mem_driver_register(__drv) \ 404 spi_mem_driver_register_with_owner(__d 243 spi_mem_driver_register_with_owner(__drv, THIS_MODULE) 405 244 406 #define module_spi_mem_driver(__drv) 245 #define module_spi_mem_driver(__drv) \ 407 module_driver(__drv, spi_mem_driver_re 246 module_driver(__drv, spi_mem_driver_register, \ 408 spi_mem_driver_unregiste 247 spi_mem_driver_unregister) 409 248 410 #endif /* __LINUX_SPI_MEM_H */ 249 #endif /* __LINUX_SPI_MEM_H */ 411 250
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.