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 | |
| 19 | #include <stdint.h> |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 20 | #include <stddef.h> |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 21 | |
| 22 | /* Controller-specific definitions: */ |
| 23 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 24 | struct spi_ctrlr; |
| 25 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 26 | /*----------------------------------------------------------------------- |
| 27 | * Representation of a SPI slave, i.e. what we're communicating with. |
| 28 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 29 | * bus: ID of the bus that the slave is attached to. |
| 30 | * cs: ID of the chip select connected to the slave. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 31 | * ctrlr: Pointer to SPI controller structure. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 32 | */ |
| 33 | struct spi_slave { |
| 34 | unsigned int bus; |
| 35 | unsigned int cs; |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 36 | const struct spi_ctrlr *ctrlr; |
| 37 | }; |
| 38 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 39 | /* Representation of SPI operation status. */ |
| 40 | enum spi_op_status { |
| 41 | SPI_OP_NOT_EXECUTED = 0, |
| 42 | SPI_OP_SUCCESS = 1, |
| 43 | SPI_OP_FAILURE = 2, |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * Representation of a SPI operation. |
| 48 | * |
| 49 | * dout: Pointer to data to send. |
| 50 | * bytesout: Count of data in bytes to send. |
| 51 | * din: Pointer to store received data. |
| 52 | * bytesin: Count of data in bytes to receive. |
| 53 | */ |
| 54 | struct spi_op { |
| 55 | const void *dout; |
| 56 | size_t bytesout; |
| 57 | void *din; |
| 58 | size_t bytesin; |
| 59 | enum spi_op_status status; |
| 60 | }; |
| 61 | |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 62 | enum spi_clock_phase { |
| 63 | SPI_CLOCK_PHASE_FIRST, |
| 64 | SPI_CLOCK_PHASE_SECOND |
| 65 | }; |
| 66 | |
| 67 | enum spi_wire_mode { |
| 68 | SPI_4_WIRE_MODE, |
| 69 | SPI_3_WIRE_MODE |
| 70 | }; |
| 71 | |
| 72 | enum spi_polarity { |
| 73 | SPI_POLARITY_LOW, |
| 74 | SPI_POLARITY_HIGH |
| 75 | }; |
| 76 | |
| 77 | struct spi_cfg { |
| 78 | /* CLK phase - 0: Phase first, 1: Phase second */ |
| 79 | enum spi_clock_phase clk_phase; |
| 80 | /* CLK polarity - 0: Low, 1: High */ |
| 81 | enum spi_polarity clk_polarity; |
| 82 | /* CS polarity - 0: Low, 1: High */ |
| 83 | enum spi_polarity cs_polarity; |
| 84 | /* Wire mode - 0: 4-wire, 1: 3-wire */ |
| 85 | enum spi_wire_mode wire_mode; |
| 86 | /* Data bit length. */ |
| 87 | unsigned int data_bit_length; |
| 88 | }; |
| 89 | |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 90 | /*----------------------------------------------------------------------- |
| 91 | * Representation of a SPI contoller. |
| 92 | * |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 93 | * get_config: Get configuration of SPI bus |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 94 | * claim_bus: Claim SPI bus and prepare for communication. |
| 95 | * release_bus: Release SPI bus. |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 96 | * setup: Setup given SPI device bus. |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 97 | * xfer: Perform one SPI transfer operation. |
| 98 | * xfer_vector: Vector of SPI transfer operations. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 99 | */ |
| 100 | struct spi_ctrlr { |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 101 | int (*get_config)(const struct spi_slave *slave, |
| 102 | struct spi_cfg *cfg); |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 103 | int (*claim_bus)(const struct spi_slave *slave); |
| 104 | void (*release_bus)(const struct spi_slave *slave); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 105 | int (*setup)(const struct spi_slave *slave); |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 106 | int (*xfer)(const struct spi_slave *slave, const void *dout, |
| 107 | size_t bytesout, void *din, size_t bytesin); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 108 | int (*xfer_vector)(const struct spi_slave *slave, |
| 109 | struct spi_op vectors[], size_t count); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /*----------------------------------------------------------------------- |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 113 | * Structure defining mapping of SPI buses to controller. |
| 114 | * |
| 115 | * ctrlr: Pointer to controller structure managing the given SPI buses. |
| 116 | * bus_start: Start bus number managed by the controller. |
| 117 | * bus_end: End bus number manager by the controller. |
| 118 | */ |
| 119 | struct spi_ctrlr_buses { |
| 120 | const struct spi_ctrlr *ctrlr; |
| 121 | unsigned int bus_start; |
| 122 | unsigned int bus_end; |
| 123 | }; |
| 124 | |
| 125 | /* Mapping of SPI buses to controllers - should be defined by platform. */ |
| 126 | extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[]; |
| 127 | extern const size_t spi_ctrlr_bus_map_count; |
| 128 | |
| 129 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 130 | * Initialization, must be called once on start up. |
| 131 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 132 | */ |
| 133 | void spi_init(void); |
| 134 | |
Furquan Shaikh | 3e01b63 | 2017-01-08 13:32:30 -0800 | [diff] [blame] | 135 | /* |
| 136 | * Get configuration of SPI bus. |
| 137 | * |
| 138 | * slave: Pointer to slave structure. |
| 139 | * cfg: Pointer to SPI configuration that needs to be filled. |
| 140 | * |
| 141 | * Returns: |
| 142 | * 0 on success, -1 on error |
| 143 | */ |
| 144 | int spi_get_config(const struct spi_slave *slave, struct spi_cfg *cfg); |
| 145 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 146 | /*----------------------------------------------------------------------- |
| 147 | * Set up communications parameters for a SPI slave. |
| 148 | * |
| 149 | * This must be called once for each slave. Note that this function |
| 150 | * usually doesn't touch any actual hardware, it only initializes the |
| 151 | * contents of spi_slave so that the hardware can be easily |
| 152 | * initialized later. |
| 153 | * |
| 154 | * bus: Bus ID of the slave chip. |
| 155 | * cs: Chip select ID of the slave chip on the specified bus. |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 156 | * slave: Pointer to slave structure that needs to be initialized. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 157 | * |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 158 | * Returns: |
| 159 | * 0 on success, -1 on error |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 160 | */ |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 161 | 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] | 162 | |
| 163 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 164 | * Claim the bus and prepare it for communication with a given slave. |
| 165 | * |
| 166 | * This must be called before doing any transfers with a SPI slave. It |
| 167 | * will enable and initialize any SPI hardware as necessary, and make |
| 168 | * sure that the SCK line is in the correct idle state. It is not |
| 169 | * allowed to claim the same bus for several slaves without releasing |
| 170 | * the bus in between. |
| 171 | * |
| 172 | * slave: The SPI slave |
| 173 | * |
| 174 | * Returns: 0 if the bus was claimed successfully, or a negative value |
| 175 | * if it wasn't. |
| 176 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 177 | int spi_claim_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 178 | |
| 179 | /*----------------------------------------------------------------------- |
| 180 | * Release the SPI bus |
| 181 | * |
| 182 | * This must be called once for every call to spi_claim_bus() after |
| 183 | * all transfers have finished. It may disable any SPI hardware as |
| 184 | * appropriate. |
| 185 | * |
| 186 | * slave: The SPI slave |
| 187 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 188 | void spi_release_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 189 | |
| 190 | /*----------------------------------------------------------------------- |
| 191 | * SPI transfer |
| 192 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 193 | * spi_xfer() interface: |
| 194 | * slave: The SPI slave which will be sending/receiving the data. |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 195 | * dout: Pointer to a string of bytes to send out. |
| 196 | * bytesout: How many bytes to write. |
| 197 | * din: Pointer to a string of bytes that will be filled in. |
| 198 | * bytesin: How many bytes to read. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 199 | * |
| 200 | * Returns: 0 on success, not 0 on failure |
| 201 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 202 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 203 | void *din, size_t bytesin); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 204 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 205 | /*----------------------------------------------------------------------- |
| 206 | * Vector of SPI transfer operations |
| 207 | * |
| 208 | * spi_xfer_vector() interface: |
| 209 | * slave: The SPI slave which will be sending/receiving the data. |
| 210 | * vectors: Array of SPI op structures. |
| 211 | * count: Number of SPI op vectors. |
| 212 | * |
| 213 | * Returns: 0 on success, not 0 on failure |
| 214 | */ |
| 215 | int spi_xfer_vector(const struct spi_slave *slave, |
| 216 | struct spi_op vectors[], size_t count); |
| 217 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 218 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len); |
| 219 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 220 | /*----------------------------------------------------------------------- |
| 221 | * Write 8 bits, then read 8 bits. |
| 222 | * slave: The SPI slave we're communicating with |
| 223 | * byte: Byte to be written |
| 224 | * |
| 225 | * Returns: The value that was read, or a negative value on error. |
| 226 | * |
| 227 | * TODO: This function probably shouldn't be inlined. |
| 228 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 229 | 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] | 230 | { |
| 231 | unsigned char dout[2]; |
| 232 | unsigned char din[2]; |
| 233 | int ret; |
| 234 | |
| 235 | dout[0] = byte; |
| 236 | dout[1] = 0; |
| 237 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 238 | ret = spi_xfer(slave, dout, 2, din, 2); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 239 | return ret < 0 ? ret : din[1]; |
| 240 | } |
| 241 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 242 | /* |
| 243 | * Helper function to allow chipsets to combine two vectors if possible. It can |
| 244 | * only handle upto 2 vectors. |
| 245 | * |
| 246 | * This function is provided to support command-response kind of transactions |
| 247 | * expected by users like flash. Some special SPI flash controllers can handle |
| 248 | * such command-response operations in a single transaction. For these special |
| 249 | * controllers, separate command and response vectors can be combined into a |
| 250 | * single operation. |
| 251 | * |
| 252 | * Two vectors are combined if first vector has a non-NULL dout and NULL din and |
| 253 | * second vector has a non-NULL din and NULL dout. Otherwise, each vector is |
| 254 | * operated upon one at a time. |
| 255 | * |
| 256 | * Returns 0 on success and non-zero on failure. |
| 257 | */ |
| 258 | int spi_xfer_two_vectors(const struct spi_slave *slave, |
| 259 | struct spi_op vectors[], size_t count); |
| 260 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 261 | #endif /* _SPI_GENERIC_H_ */ |