Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 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 __UBOOT_SPI_MEM_H |
| 12 | #define __UBOOT_SPI_MEM_H |
| 13 | |
Simon Glass | 340fd10 | 2020-07-19 10:15:34 -0600 | [diff] [blame] | 14 | struct udevice; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 15 | |
| 16 | #define SPI_MEM_OP_CMD(__opcode, __buswidth) \ |
| 17 | { \ |
| 18 | .buswidth = __buswidth, \ |
| 19 | .opcode = __opcode, \ |
Pratyush Yadav | d15de62 | 2021-06-26 00:47:04 +0530 | [diff] [blame] | 20 | .nbytes = 1, \ |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | #define SPI_MEM_OP_ADDR(__nbytes, __val, __buswidth) \ |
| 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, __buswidth) \ |
| 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, __buswidth) \ |
| 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 direction of a SPI memory data |
| 60 | * transfer from the controller perspective |
Tudor Ambarus | 790c169 | 2020-03-20 09:35:31 +0000 | [diff] [blame] | 61 | * @SPI_MEM_NO_DATA: no data transferred |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 62 | * @SPI_MEM_DATA_IN: data coming from the SPI memory |
| 63 | * @SPI_MEM_DATA_OUT: data sent the SPI memory |
| 64 | */ |
| 65 | enum spi_mem_data_dir { |
Tudor Ambarus | 790c169 | 2020-03-20 09:35:31 +0000 | [diff] [blame] | 66 | SPI_MEM_NO_DATA, |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 67 | SPI_MEM_DATA_IN, |
| 68 | SPI_MEM_DATA_OUT, |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct spi_mem_op - describes a SPI memory operation |
Pratyush Yadav | d15de62 | 2021-06-26 00:47:04 +0530 | [diff] [blame] | 73 | * @cmd.nbytes: number of opcode bytes (only 1 or 2 are valid). The opcode is |
| 74 | * sent MSB-first. |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 75 | * @cmd.buswidth: number of IO lines used to transmit the command |
| 76 | * @cmd.opcode: operation opcode |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 77 | * @cmd.dtr: whether the command opcode should be sent in DTR mode or not |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 78 | * @addr.nbytes: number of address bytes to send. Can be zero if the operation |
| 79 | * does not need to send an address |
| 80 | * @addr.buswidth: number of IO lines used to transmit the address cycles |
| 81 | * @addr.val: address value. This value is always sent MSB first on the bus. |
| 82 | * Note that only @addr.nbytes are taken into account in this |
| 83 | * address value, so users should make sure the value fits in the |
| 84 | * assigned number of bytes. |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 85 | * @addr.dtr: whether the address should be sent in DTR mode or not |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 86 | * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can |
| 87 | * be zero if the operation does not require dummy bytes |
| 88 | * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 89 | * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 90 | * @data.buswidth: number of IO lanes used to send/receive the data |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 91 | * @data.dtr: whether the data should be sent in DTR mode or not |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 92 | * @data.dir: direction of the transfer |
| 93 | * @data.buf.in: input buffer |
| 94 | * @data.buf.out: output buffer |
| 95 | */ |
| 96 | struct spi_mem_op { |
| 97 | struct { |
Pratyush Yadav | d15de62 | 2021-06-26 00:47:04 +0530 | [diff] [blame] | 98 | u8 nbytes; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 99 | u8 buswidth; |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 100 | u8 dtr : 1; |
Pratyush Yadav | d15de62 | 2021-06-26 00:47:04 +0530 | [diff] [blame] | 101 | u16 opcode; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 102 | } cmd; |
| 103 | |
| 104 | struct { |
| 105 | u8 nbytes; |
| 106 | u8 buswidth; |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 107 | u8 dtr : 1; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 108 | u64 val; |
| 109 | } addr; |
| 110 | |
| 111 | struct { |
| 112 | u8 nbytes; |
| 113 | u8 buswidth; |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 114 | u8 dtr : 1; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 115 | } dummy; |
| 116 | |
| 117 | struct { |
| 118 | u8 buswidth; |
Pratyush Yadav | a1eb40b | 2021-06-26 00:47:03 +0530 | [diff] [blame] | 119 | u8 dtr : 1; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 120 | enum spi_mem_data_dir dir; |
| 121 | unsigned int nbytes; |
| 122 | /* buf.{in,out} must be DMA-able. */ |
| 123 | union { |
| 124 | void *in; |
| 125 | const void *out; |
| 126 | } buf; |
| 127 | } data; |
| 128 | }; |
| 129 | |
| 130 | #define SPI_MEM_OP(__cmd, __addr, __dummy, __data) \ |
| 131 | { \ |
| 132 | .cmd = __cmd, \ |
| 133 | .addr = __addr, \ |
| 134 | .dummy = __dummy, \ |
| 135 | .data = __data, \ |
| 136 | } |
| 137 | |
| 138 | #ifndef __UBOOT__ |
| 139 | /** |
| 140 | * struct spi_mem - describes a SPI memory device |
| 141 | * @spi: the underlying SPI device |
| 142 | * @drvpriv: spi_mem_driver private data |
| 143 | * |
| 144 | * Extra information that describe the SPI memory device and may be needed by |
| 145 | * the controller to properly handle this device should be placed here. |
| 146 | * |
| 147 | * One example would be the device size since some controller expose their SPI |
| 148 | * mem devices through a io-mapped region. |
| 149 | */ |
| 150 | struct spi_mem { |
| 151 | struct udevice *dev; |
| 152 | void *drvpriv; |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * struct spi_mem_set_drvdata() - attach driver private data to a SPI mem |
| 157 | * device |
| 158 | * @mem: memory device |
| 159 | * @data: data to attach to the memory device |
| 160 | */ |
| 161 | static inline void spi_mem_set_drvdata(struct spi_mem *mem, void *data) |
| 162 | { |
| 163 | mem->drvpriv = data; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * struct spi_mem_get_drvdata() - get driver private data attached to a SPI mem |
| 168 | * device |
| 169 | * @mem: memory device |
| 170 | * |
| 171 | * Return: the data attached to the mem device. |
| 172 | */ |
| 173 | static inline void *spi_mem_get_drvdata(struct spi_mem *mem) |
| 174 | { |
| 175 | return mem->drvpriv; |
| 176 | } |
| 177 | #endif /* __UBOOT__ */ |
| 178 | |
| 179 | /** |
| 180 | * struct spi_controller_mem_ops - SPI memory operations |
| 181 | * @adjust_op_size: shrink the data xfer of an operation to match controller's |
| 182 | * limitations (can be alignment of max RX/TX size |
| 183 | * limitations) |
| 184 | * @supports_op: check if an operation is supported by the controller |
| 185 | * @exec_op: execute a SPI memory operation |
| 186 | * |
| 187 | * This interface should be implemented by SPI controllers providing an |
| 188 | * high-level interface to execute SPI memory operation, which is usually the |
| 189 | * case for QSPI controllers. |
| 190 | */ |
| 191 | struct spi_controller_mem_ops { |
| 192 | int (*adjust_op_size)(struct spi_slave *slave, struct spi_mem_op *op); |
| 193 | bool (*supports_op)(struct spi_slave *slave, |
| 194 | const struct spi_mem_op *op); |
| 195 | int (*exec_op)(struct spi_slave *slave, |
| 196 | const struct spi_mem_op *op); |
| 197 | }; |
| 198 | |
| 199 | #ifndef __UBOOT__ |
| 200 | /** |
| 201 | * struct spi_mem_driver - SPI memory driver |
| 202 | * @spidrv: inherit from a SPI driver |
| 203 | * @probe: probe a SPI memory. Usually where detection/initialization takes |
| 204 | * place |
| 205 | * @remove: remove a SPI memory |
| 206 | * @shutdown: take appropriate action when the system is shutdown |
| 207 | * |
| 208 | * This is just a thin wrapper around a spi_driver. The core takes care of |
| 209 | * allocating the spi_mem object and forwarding the probe/remove/shutdown |
| 210 | * request to the spi_mem_driver. The reason we use this wrapper is because |
| 211 | * we might have to stuff more information into the spi_mem struct to let |
| 212 | * SPI controllers know more about the SPI memory they interact with, and |
| 213 | * having this intermediate layer allows us to do that without adding more |
| 214 | * useless fields to the spi_device object. |
| 215 | */ |
| 216 | struct spi_mem_driver { |
| 217 | struct spi_driver spidrv; |
| 218 | int (*probe)(struct spi_mem *mem); |
| 219 | int (*remove)(struct spi_mem *mem); |
| 220 | void (*shutdown)(struct spi_mem *mem); |
| 221 | }; |
| 222 | |
| 223 | #if IS_ENABLED(CONFIG_SPI_MEM) |
| 224 | int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, |
| 225 | const struct spi_mem_op *op, |
| 226 | struct sg_table *sg); |
| 227 | |
| 228 | void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, |
| 229 | const struct spi_mem_op *op, |
| 230 | struct sg_table *sg); |
| 231 | #else |
| 232 | static inline int |
| 233 | spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr, |
| 234 | const struct spi_mem_op *op, |
| 235 | struct sg_table *sg) |
| 236 | { |
Simon Glass | 24e3d5d | 2021-03-25 10:26:06 +1300 | [diff] [blame] | 237 | return -ENOSYS; |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static inline void |
| 241 | spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr, |
| 242 | const struct spi_mem_op *op, |
| 243 | struct sg_table *sg) |
| 244 | { |
| 245 | } |
| 246 | #endif /* CONFIG_SPI_MEM */ |
| 247 | #endif /* __UBOOT__ */ |
| 248 | |
| 249 | int spi_mem_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op); |
| 250 | |
| 251 | bool spi_mem_supports_op(struct spi_slave *slave, const struct spi_mem_op *op); |
Pratyush Yadav | 5752d6a | 2021-06-26 00:47:06 +0530 | [diff] [blame] | 252 | bool spi_mem_dtr_supports_op(struct spi_slave *slave, |
| 253 | const struct spi_mem_op *op); |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 254 | |
Pratyush Yadav | 2299076 | 2021-06-26 00:47:05 +0530 | [diff] [blame] | 255 | bool spi_mem_default_supports_op(struct spi_slave *slave, |
| 256 | const struct spi_mem_op *op); |
| 257 | |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 258 | int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op); |
| 259 | |
Mathew McBride | af6266c | 2021-01-25 03:55:20 +0000 | [diff] [blame] | 260 | bool spi_mem_default_supports_op(struct spi_slave *mem, |
| 261 | const struct spi_mem_op *op); |
| 262 | |
Boris Brezillon | d13f5b2 | 2018-08-16 17:30:11 +0200 | [diff] [blame] | 263 | #ifndef __UBOOT__ |
| 264 | int spi_mem_driver_register_with_owner(struct spi_mem_driver *drv, |
| 265 | struct module *owner); |
| 266 | |
| 267 | void spi_mem_driver_unregister(struct spi_mem_driver *drv); |
| 268 | |
| 269 | #define spi_mem_driver_register(__drv) \ |
| 270 | spi_mem_driver_register_with_owner(__drv, THIS_MODULE) |
| 271 | |
| 272 | #define module_spi_mem_driver(__drv) \ |
| 273 | module_driver(__drv, spi_mem_driver_register, \ |
| 274 | spi_mem_driver_unregister) |
| 275 | #endif |
| 276 | |
| 277 | #endif /* __LINUX_SPI_MEM_H */ |