Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 2 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 3 | #ifndef _SPI_GENERIC_H_ |
| 4 | #define _SPI_GENERIC_H_ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | |
Uwe Poeche | 7724ceb | 2019-07-15 11:15:36 +0200 | [diff] [blame] | 6 | /* Common parameters -- kind of high, but they should only occur when there |
| 7 | * is a problem (and well your system already is broken), so err on the side |
| 8 | * of caution in case we're dealing with slower SPI buses and/or processors. |
| 9 | */ |
Uwe Poeche | 17362be | 2019-07-17 14:27:13 +0200 | [diff] [blame] | 10 | #define SPI_FLASH_PROG_TIMEOUT_MS 200 |
| 11 | #define SPI_FLASH_PAGE_ERASE_TIMEOUT_MS 500 |
Uwe Poeche | 7724ceb | 2019-07-15 11:15:36 +0200 | [diff] [blame] | 12 | |
Aaron Durbin | 10d65b0 | 2017-12-14 14:34:47 -0700 | [diff] [blame] | 13 | #include <commonlib/region.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 14 | #include <stdint.h> |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 15 | #include <stddef.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 16 | |
Richard Spiegel | 563b869 | 2019-09-03 11:54:55 -0700 | [diff] [blame] | 17 | /* SPI vendor IDs */ |
| 18 | #define VENDOR_ID_ADESTO 0x1f |
| 19 | #define VENDOR_ID_AMIC 0x37 |
| 20 | #define VENDOR_ID_ATMEL 0x1f |
| 21 | #define VENDOR_ID_EON 0x1c |
| 22 | #define VENDOR_ID_GIGADEVICE 0xc8 |
| 23 | #define VENDOR_ID_MACRONIX 0xc2 |
| 24 | #define VENDOR_ID_SPANSION 0x01 |
| 25 | #define VENDOR_ID_SST 0xbf |
| 26 | #define VENDOR_ID_STMICRO 0x20 |
Richard Spiegel | 563b869 | 2019-09-03 11:54:55 -0700 | [diff] [blame] | 27 | #define VENDOR_ID_WINBOND 0xef |
| 28 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 29 | /* Controller-specific definitions: */ |
| 30 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 31 | struct spi_ctrlr; |
| 32 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 33 | /*----------------------------------------------------------------------- |
| 34 | * Representation of a SPI slave, i.e. what we're communicating with. |
| 35 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 36 | * bus: ID of the bus that the slave is attached to. |
| 37 | * cs: ID of the chip select connected to the slave. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 38 | * ctrlr: Pointer to SPI controller structure. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 39 | */ |
| 40 | struct spi_slave { |
| 41 | unsigned int bus; |
| 42 | unsigned int cs; |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 43 | const struct spi_ctrlr *ctrlr; |
| 44 | }; |
| 45 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 46 | /* Representation of SPI operation status. */ |
| 47 | enum spi_op_status { |
| 48 | SPI_OP_NOT_EXECUTED = 0, |
| 49 | SPI_OP_SUCCESS = 1, |
| 50 | SPI_OP_FAILURE = 2, |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * Representation of a SPI operation. |
| 55 | * |
| 56 | * dout: Pointer to data to send. |
| 57 | * bytesout: Count of data in bytes to send. |
| 58 | * din: Pointer to store received data. |
| 59 | * bytesin: Count of data in bytes to receive. |
| 60 | */ |
| 61 | struct spi_op { |
| 62 | const void *dout; |
| 63 | size_t bytesout; |
| 64 | void *din; |
| 65 | size_t bytesin; |
| 66 | enum spi_op_status status; |
| 67 | }; |
| 68 | |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 69 | enum spi_clock_phase { |
| 70 | SPI_CLOCK_PHASE_FIRST, |
| 71 | SPI_CLOCK_PHASE_SECOND |
| 72 | }; |
| 73 | |
| 74 | enum spi_wire_mode { |
| 75 | SPI_4_WIRE_MODE, |
| 76 | SPI_3_WIRE_MODE |
| 77 | }; |
| 78 | |
| 79 | enum spi_polarity { |
| 80 | SPI_POLARITY_LOW, |
| 81 | SPI_POLARITY_HIGH |
| 82 | }; |
| 83 | |
| 84 | struct spi_cfg { |
| 85 | /* CLK phase - 0: Phase first, 1: Phase second */ |
| 86 | enum spi_clock_phase clk_phase; |
| 87 | /* CLK polarity - 0: Low, 1: High */ |
| 88 | enum spi_polarity clk_polarity; |
| 89 | /* CS polarity - 0: Low, 1: High */ |
| 90 | enum spi_polarity cs_polarity; |
| 91 | /* Wire mode - 0: 4-wire, 1: 3-wire */ |
| 92 | enum spi_wire_mode wire_mode; |
| 93 | /* Data bit length. */ |
| 94 | unsigned int data_bit_length; |
| 95 | }; |
| 96 | |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 97 | /* |
| 98 | * If there is no limit on the maximum transfer size for the controller, |
| 99 | * max_xfer_size can be set to SPI_CTRLR_DEFAULT_MAX_XFER_SIZE which is equal to |
| 100 | * UINT32_MAX. |
| 101 | */ |
| 102 | #define SPI_CTRLR_DEFAULT_MAX_XFER_SIZE (UINT32_MAX) |
| 103 | |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 104 | struct spi_flash; |
| 105 | |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 106 | enum ctrlr_prot_type { |
| 107 | READ_PROTECT = 1, |
| 108 | WRITE_PROTECT = 2, |
| 109 | READ_WRITE_PROTECT = 3, |
| 110 | }; |
| 111 | |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 112 | enum { |
| 113 | /* Deduct the command length from the spi_crop_chunk() calculation for |
| 114 | sizing a transaction. */ |
| 115 | SPI_CNTRLR_DEDUCT_CMD_LEN = 1 << 0, |
| 116 | /* Remove the opcode size from the command length used in the |
| 117 | spi_crop_chunk() calculation. Controllers which have a dedicated |
| 118 | register for the command byte would set this flag which would |
| 119 | allow the use of the maximum transfer size. */ |
| 120 | SPI_CNTRLR_DEDUCT_OPCODE_LEN = 1 << 1, |
| 121 | }; |
| 122 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 123 | /*----------------------------------------------------------------------- |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 124 | * Representation of a SPI controller. Note the xfer() and xfer_vector() |
| 125 | * callbacks are meant to process full duplex transactions. If the |
| 126 | * controller cannot handle these transactions then return an error when |
| 127 | * din and dout are both set. See spi_xfer() below for more details. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 128 | * |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 129 | * claim_bus: Claim SPI bus and prepare for communication. |
| 130 | * release_bus: Release SPI bus. |
| 131 | * setup: Setup given SPI device bus. |
| 132 | * xfer: Perform one SPI transfer operation. |
| 133 | * xfer_vector: Vector of SPI transfer operations. |
Julius Werner | 99e45ce | 2019-06-06 17:03:44 -0700 | [diff] [blame] | 134 | * xfer_dual: (optional) Perform one SPI transfer in Dual SPI mode. |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 135 | * max_xfer_size: Maximum transfer size supported by the controller |
| 136 | * (0 = invalid, |
| 137 | * SPI_CTRLR_DEFAULT_MAX_XFER_SIZE = unlimited) |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 138 | * flags: See SPI_CNTRLR_* enums above. |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 139 | * |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 140 | * Following member is provided by specialized SPI controllers that are |
| 141 | * actually SPI flash controllers. |
| 142 | * |
| 143 | * flash_probe: Specialized probe function provided by SPI flash |
| 144 | * controllers. |
Aaron Durbin | 10d65b0 | 2017-12-14 14:34:47 -0700 | [diff] [blame] | 145 | * flash_protect: Protect a region of flash using the SPI flash controller. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 146 | */ |
| 147 | struct spi_ctrlr { |
| 148 | int (*claim_bus)(const struct spi_slave *slave); |
| 149 | void (*release_bus)(const struct spi_slave *slave); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 150 | int (*setup)(const struct spi_slave *slave); |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 151 | int (*xfer)(const struct spi_slave *slave, const void *dout, |
| 152 | size_t bytesout, void *din, size_t bytesin); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 153 | int (*xfer_vector)(const struct spi_slave *slave, |
| 154 | struct spi_op vectors[], size_t count); |
Julius Werner | 99e45ce | 2019-06-06 17:03:44 -0700 | [diff] [blame] | 155 | int (*xfer_dual)(const struct spi_slave *slave, const void *dout, |
| 156 | size_t bytesout, void *din, size_t bytesin); |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 157 | uint32_t max_xfer_size; |
Aaron Durbin | 1fcc9f3 | 2018-01-29 11:30:17 -0700 | [diff] [blame] | 158 | uint32_t flags; |
Furquan Shaikh | a149157 | 2017-05-17 19:14:06 -0700 | [diff] [blame] | 159 | int (*flash_probe)(const struct spi_slave *slave, |
| 160 | struct spi_flash *flash); |
Aaron Durbin | 10d65b0 | 2017-12-14 14:34:47 -0700 | [diff] [blame] | 161 | int (*flash_protect)(const struct spi_flash *flash, |
Rizwan Qureshi | f9f5093 | 2018-12-31 15:19:16 +0530 | [diff] [blame] | 162 | const struct region *region, |
| 163 | const enum ctrlr_prot_type type); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /*----------------------------------------------------------------------- |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 167 | * Structure defining mapping of SPI buses to controller. |
| 168 | * |
| 169 | * ctrlr: Pointer to controller structure managing the given SPI buses. |
| 170 | * bus_start: Start bus number managed by the controller. |
| 171 | * bus_end: End bus number manager by the controller. |
| 172 | */ |
| 173 | struct spi_ctrlr_buses { |
| 174 | const struct spi_ctrlr *ctrlr; |
| 175 | unsigned int bus_start; |
| 176 | unsigned int bus_end; |
| 177 | }; |
| 178 | |
| 179 | /* Mapping of SPI buses to controllers - should be defined by platform. */ |
| 180 | extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[]; |
| 181 | extern const size_t spi_ctrlr_bus_map_count; |
| 182 | |
| 183 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 184 | * Initialization, must be called once on start up. |
| 185 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 186 | */ |
| 187 | void spi_init(void); |
| 188 | |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Get configuration of SPI bus. |
| 191 | * |
| 192 | * slave: Pointer to slave structure. |
| 193 | * cfg: Pointer to SPI configuration that needs to be filled. |
| 194 | * |
| 195 | * Returns: |
| 196 | * 0 on success, -1 on error |
| 197 | */ |
| 198 | int spi_get_config(const struct spi_slave *slave, struct spi_cfg *cfg); |
| 199 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 200 | /*----------------------------------------------------------------------- |
| 201 | * Set up communications parameters for a SPI slave. |
| 202 | * |
| 203 | * This must be called once for each slave. Note that this function |
| 204 | * usually doesn't touch any actual hardware, it only initializes the |
| 205 | * contents of spi_slave so that the hardware can be easily |
| 206 | * initialized later. |
| 207 | * |
| 208 | * bus: Bus ID of the slave chip. |
| 209 | * cs: Chip select ID of the slave chip on the specified bus. |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 210 | * slave: Pointer to slave structure that needs to be initialized. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 211 | * |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 212 | * Returns: |
| 213 | * 0 on success, -1 on error |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 214 | */ |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 215 | int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 216 | |
| 217 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 218 | * Claim the bus and prepare it for communication with a given slave. |
| 219 | * |
| 220 | * This must be called before doing any transfers with a SPI slave. It |
| 221 | * will enable and initialize any SPI hardware as necessary, and make |
| 222 | * sure that the SCK line is in the correct idle state. It is not |
| 223 | * allowed to claim the same bus for several slaves without releasing |
| 224 | * the bus in between. |
| 225 | * |
| 226 | * slave: The SPI slave |
| 227 | * |
| 228 | * Returns: 0 if the bus was claimed successfully, or a negative value |
| 229 | * if it wasn't. |
| 230 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 231 | int spi_claim_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 232 | |
| 233 | /*----------------------------------------------------------------------- |
| 234 | * Release the SPI bus |
| 235 | * |
| 236 | * This must be called once for every call to spi_claim_bus() after |
| 237 | * all transfers have finished. It may disable any SPI hardware as |
| 238 | * appropriate. |
| 239 | * |
| 240 | * slave: The SPI slave |
| 241 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 242 | void spi_release_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 243 | |
| 244 | /*----------------------------------------------------------------------- |
| 245 | * SPI transfer |
| 246 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 247 | * spi_xfer() interface: |
| 248 | * slave: The SPI slave which will be sending/receiving the data. |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 249 | * dout: Pointer to a string of bytes to send out. |
| 250 | * bytesout: How many bytes to write. |
| 251 | * din: Pointer to a string of bytes that will be filled in. |
| 252 | * bytesin: How many bytes to read. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 253 | * |
Elyes HAOUAS | 5f73e22 | 2020-01-15 21:13:45 +0100 | [diff] [blame] | 254 | * Note that din and dout are transferred simultaneously in a full duplex |
Aaron Durbin | 851dde8 | 2018-04-19 21:15:25 -0600 | [diff] [blame] | 255 | * transaction. The number of clocks within one transaction is calculated |
| 256 | * as: MAX(bytesout*8, bytesin*8). |
| 257 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 258 | * Returns: 0 on success, not 0 on failure |
| 259 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 260 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 261 | void *din, size_t bytesin); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 262 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 263 | /*----------------------------------------------------------------------- |
| 264 | * Vector of SPI transfer operations |
| 265 | * |
| 266 | * spi_xfer_vector() interface: |
| 267 | * slave: The SPI slave which will be sending/receiving the data. |
| 268 | * vectors: Array of SPI op structures. |
| 269 | * count: Number of SPI op vectors. |
| 270 | * |
| 271 | * Returns: 0 on success, not 0 on failure |
| 272 | */ |
| 273 | int spi_xfer_vector(const struct spi_slave *slave, |
| 274 | struct spi_op vectors[], size_t count); |
| 275 | |
Furquan Shaikh | de705fa | 2017-04-19 19:27:28 -0700 | [diff] [blame] | 276 | /*----------------------------------------------------------------------- |
| 277 | * Given command length and length of remaining data, return the maximum data |
| 278 | * that can be transferred in next spi_xfer. |
| 279 | * |
| 280 | * Returns: 0 on error, non-zero data size that can be xfered on success. |
| 281 | */ |
| 282 | unsigned int spi_crop_chunk(const struct spi_slave *slave, unsigned int cmd_len, |
| 283 | unsigned int buf_len); |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 284 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 285 | /*----------------------------------------------------------------------- |
| 286 | * Write 8 bits, then read 8 bits. |
| 287 | * slave: The SPI slave we're communicating with |
| 288 | * byte: Byte to be written |
| 289 | * |
| 290 | * Returns: The value that was read, or a negative value on error. |
| 291 | * |
| 292 | * TODO: This function probably shouldn't be inlined. |
| 293 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 294 | static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 295 | { |
| 296 | unsigned char dout[2]; |
| 297 | unsigned char din[2]; |
| 298 | int ret; |
| 299 | |
| 300 | dout[0] = byte; |
| 301 | dout[1] = 0; |
| 302 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 303 | ret = spi_xfer(slave, dout, 2, din, 2); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 304 | return ret < 0 ? ret : din[1]; |
| 305 | } |
| 306 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 307 | #endif /* _SPI_GENERIC_H_ */ |