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