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 | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 62 | /*----------------------------------------------------------------------- |
| 63 | * Representation of a SPI contoller. |
| 64 | * |
| 65 | * claim_bus: Claim SPI bus and prepare for communication. |
| 66 | * release_bus: Release SPI bus. |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 67 | * setup: Setup given SPI device bus. |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 68 | * xfer: Perform one SPI transfer operation. |
| 69 | * xfer_vector: Vector of SPI transfer operations. |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 70 | */ |
| 71 | struct spi_ctrlr { |
| 72 | int (*claim_bus)(const struct spi_slave *slave); |
| 73 | void (*release_bus)(const struct spi_slave *slave); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 74 | int (*setup)(const struct spi_slave *slave); |
Furquan Shaikh | 94f8699 | 2016-12-01 07:12:32 -0800 | [diff] [blame] | 75 | int (*xfer)(const struct spi_slave *slave, const void *dout, |
| 76 | size_t bytesout, void *din, size_t bytesin); |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 77 | int (*xfer_vector)(const struct spi_slave *slave, |
| 78 | struct spi_op vectors[], size_t count); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /*----------------------------------------------------------------------- |
Furquan Shaikh | b5d41cb | 2016-12-01 07:25:31 -0800 | [diff] [blame] | 82 | * Structure defining mapping of SPI buses to controller. |
| 83 | * |
| 84 | * ctrlr: Pointer to controller structure managing the given SPI buses. |
| 85 | * bus_start: Start bus number managed by the controller. |
| 86 | * bus_end: End bus number manager by the controller. |
| 87 | */ |
| 88 | struct spi_ctrlr_buses { |
| 89 | const struct spi_ctrlr *ctrlr; |
| 90 | unsigned int bus_start; |
| 91 | unsigned int bus_end; |
| 92 | }; |
| 93 | |
| 94 | /* Mapping of SPI buses to controllers - should be defined by platform. */ |
| 95 | extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[]; |
| 96 | extern const size_t spi_ctrlr_bus_map_count; |
| 97 | |
| 98 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 99 | * Initialization, must be called once on start up. |
| 100 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 101 | */ |
| 102 | void spi_init(void); |
| 103 | |
| 104 | /*----------------------------------------------------------------------- |
| 105 | * Set up communications parameters for a SPI slave. |
| 106 | * |
| 107 | * This must be called once for each slave. Note that this function |
| 108 | * usually doesn't touch any actual hardware, it only initializes the |
| 109 | * contents of spi_slave so that the hardware can be easily |
| 110 | * initialized later. |
| 111 | * |
| 112 | * bus: Bus ID of the slave chip. |
| 113 | * cs: Chip select ID of the slave chip on the specified bus. |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 114 | * slave: Pointer to slave structure that needs to be initialized. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 115 | * |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 116 | * Returns: |
| 117 | * 0 on success, -1 on error |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 118 | */ |
Furquan Shaikh | 36b81af | 2016-12-01 01:02:44 -0800 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 122 | * Claim the bus and prepare it for communication with a given slave. |
| 123 | * |
| 124 | * This must be called before doing any transfers with a SPI slave. It |
| 125 | * will enable and initialize any SPI hardware as necessary, and make |
| 126 | * sure that the SCK line is in the correct idle state. It is not |
| 127 | * allowed to claim the same bus for several slaves without releasing |
| 128 | * the bus in between. |
| 129 | * |
| 130 | * slave: The SPI slave |
| 131 | * |
| 132 | * Returns: 0 if the bus was claimed successfully, or a negative value |
| 133 | * if it wasn't. |
| 134 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 135 | int spi_claim_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 136 | |
| 137 | /*----------------------------------------------------------------------- |
| 138 | * Release the SPI bus |
| 139 | * |
| 140 | * This must be called once for every call to spi_claim_bus() after |
| 141 | * all transfers have finished. It may disable any SPI hardware as |
| 142 | * appropriate. |
| 143 | * |
| 144 | * slave: The SPI slave |
| 145 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 146 | void spi_release_bus(const struct spi_slave *slave); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 147 | |
| 148 | /*----------------------------------------------------------------------- |
| 149 | * SPI transfer |
| 150 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 151 | * spi_xfer() interface: |
| 152 | * slave: The SPI slave which will be sending/receiving the data. |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 153 | * dout: Pointer to a string of bytes to send out. |
| 154 | * bytesout: How many bytes to write. |
| 155 | * din: Pointer to a string of bytes that will be filled in. |
| 156 | * bytesin: How many bytes to read. |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 157 | * |
| 158 | * Returns: 0 on success, not 0 on failure |
| 159 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 160 | int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout, |
| 161 | void *din, size_t bytesin); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 162 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 163 | /*----------------------------------------------------------------------- |
| 164 | * Vector of SPI transfer operations |
| 165 | * |
| 166 | * spi_xfer_vector() interface: |
| 167 | * slave: The SPI slave which will be sending/receiving the data. |
| 168 | * vectors: Array of SPI op structures. |
| 169 | * count: Number of SPI op vectors. |
| 170 | * |
| 171 | * Returns: 0 on success, not 0 on failure |
| 172 | */ |
| 173 | int spi_xfer_vector(const struct spi_slave *slave, |
| 174 | struct spi_op vectors[], size_t count); |
| 175 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 176 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len); |
| 177 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 178 | /*----------------------------------------------------------------------- |
| 179 | * Write 8 bits, then read 8 bits. |
| 180 | * slave: The SPI slave we're communicating with |
| 181 | * byte: Byte to be written |
| 182 | * |
| 183 | * Returns: The value that was read, or a negative value on error. |
| 184 | * |
| 185 | * TODO: This function probably shouldn't be inlined. |
| 186 | */ |
Furquan Shaikh | 0dba025 | 2016-11-30 04:34:22 -0800 | [diff] [blame] | 187 | 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] | 188 | { |
| 189 | unsigned char dout[2]; |
| 190 | unsigned char din[2]; |
| 191 | int ret; |
| 192 | |
| 193 | dout[0] = byte; |
| 194 | dout[1] = 0; |
| 195 | |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 196 | ret = spi_xfer(slave, dout, 2, din, 2); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 197 | return ret < 0 ? ret : din[1]; |
| 198 | } |
| 199 | |
Furquan Shaikh | c2973d1 | 2016-11-29 22:07:42 -0800 | [diff] [blame] | 200 | /* |
| 201 | * Helper function to allow chipsets to combine two vectors if possible. It can |
| 202 | * only handle upto 2 vectors. |
| 203 | * |
| 204 | * This function is provided to support command-response kind of transactions |
| 205 | * expected by users like flash. Some special SPI flash controllers can handle |
| 206 | * such command-response operations in a single transaction. For these special |
| 207 | * controllers, separate command and response vectors can be combined into a |
| 208 | * single operation. |
| 209 | * |
| 210 | * Two vectors are combined if first vector has a non-NULL dout and NULL din and |
| 211 | * second vector has a non-NULL din and NULL dout. Otherwise, each vector is |
| 212 | * operated upon one at a time. |
| 213 | * |
| 214 | * Returns 0 on success and non-zero on failure. |
| 215 | */ |
| 216 | int spi_xfer_two_vectors(const struct spi_slave *slave, |
| 217 | struct spi_op vectors[], size_t count); |
| 218 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 219 | #endif /* _SPI_GENERIC_H_ */ |