blob: cc3d3cf1b98dc8931c326e1798bde6af01d232b3 [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * (C) Copyright 2001
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07005 * 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 Reinauer1c56d9b2012-05-10 11:27:32 -070014 */
15
Patrick Georgibc64cae2013-02-11 22:12:55 +010016#ifndef _SPI_GENERIC_H_
17#define _SPI_GENERIC_H_
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070018
19#include <stdint.h>
Furquan Shaikh0dba0252016-11-30 04:34:22 -080020#include <stddef.h>
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070021
22/* Controller-specific definitions: */
23
Furquan Shaikh94f86992016-12-01 07:12:32 -080024struct spi_ctrlr;
25
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026/*-----------------------------------------------------------------------
27 * Representation of a SPI slave, i.e. what we're communicating with.
28 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070029 * bus: ID of the bus that the slave is attached to.
30 * cs: ID of the chip select connected to the slave.
Furquan Shaikh94f86992016-12-01 07:12:32 -080031 * ctrlr: Pointer to SPI controller structure.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070032 */
33struct spi_slave {
34 unsigned int bus;
35 unsigned int cs;
Furquan Shaikh94f86992016-12-01 07:12:32 -080036 const struct spi_ctrlr *ctrlr;
37};
38
Furquan Shaikhc2973d12016-11-29 22:07:42 -080039/* Representation of SPI operation status. */
40enum 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 */
54struct 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 Shaikh3e01b632017-01-08 13:32:30 -080062enum spi_clock_phase {
63 SPI_CLOCK_PHASE_FIRST,
64 SPI_CLOCK_PHASE_SECOND
65};
66
67enum spi_wire_mode {
68 SPI_4_WIRE_MODE,
69 SPI_3_WIRE_MODE
70};
71
72enum spi_polarity {
73 SPI_POLARITY_LOW,
74 SPI_POLARITY_HIGH
75};
76
77struct 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 Shaikh94f86992016-12-01 07:12:32 -080090/*-----------------------------------------------------------------------
91 * Representation of a SPI contoller.
92 *
Furquan Shaikh3e01b632017-01-08 13:32:30 -080093 * get_config: Get configuration of SPI bus
Furquan Shaikh94f86992016-12-01 07:12:32 -080094 * claim_bus: Claim SPI bus and prepare for communication.
95 * release_bus: Release SPI bus.
Furquan Shaikhb5d41cb2016-12-01 07:25:31 -080096 * setup: Setup given SPI device bus.
Furquan Shaikhc2973d12016-11-29 22:07:42 -080097 * xfer: Perform one SPI transfer operation.
98 * xfer_vector: Vector of SPI transfer operations.
Furquan Shaikh94f86992016-12-01 07:12:32 -080099 */
100struct spi_ctrlr {
Furquan Shaikh3e01b632017-01-08 13:32:30 -0800101 int (*get_config)(const struct spi_slave *slave,
102 struct spi_cfg *cfg);
Furquan Shaikh94f86992016-12-01 07:12:32 -0800103 int (*claim_bus)(const struct spi_slave *slave);
104 void (*release_bus)(const struct spi_slave *slave);
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800105 int (*setup)(const struct spi_slave *slave);
Furquan Shaikh94f86992016-12-01 07:12:32 -0800106 int (*xfer)(const struct spi_slave *slave, const void *dout,
107 size_t bytesout, void *din, size_t bytesin);
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800108 int (*xfer_vector)(const struct spi_slave *slave,
109 struct spi_op vectors[], size_t count);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700110};
111
112/*-----------------------------------------------------------------------
Furquan Shaikhb5d41cb2016-12-01 07:25:31 -0800113 * 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 */
119struct 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. */
126extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[];
127extern const size_t spi_ctrlr_bus_map_count;
128
129/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700130 * Initialization, must be called once on start up.
131 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700132 */
133void spi_init(void);
134
Furquan Shaikh3e01b632017-01-08 13:32:30 -0800135/*
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 */
144int spi_get_config(const struct spi_slave *slave, struct spi_cfg *cfg);
145
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700146/*-----------------------------------------------------------------------
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 Shaikh36b81af2016-12-01 01:02:44 -0800156 * slave: Pointer to slave structure that needs to be initialized.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700157 *
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800158 * Returns:
159 * 0 on success, -1 on error
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700160 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800161int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700162
163/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700164 * 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 Shaikh0dba0252016-11-30 04:34:22 -0800177int spi_claim_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700178
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 Shaikh0dba0252016-11-30 04:34:22 -0800188void spi_release_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700189
190/*-----------------------------------------------------------------------
191 * SPI transfer
192 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700193 * spi_xfer() interface:
194 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -0700195 * 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 Reinauer1c56d9b2012-05-10 11:27:32 -0700199 *
200 * Returns: 0 on success, not 0 on failure
201 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800202int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout,
203 void *din, size_t bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700204
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800205/*-----------------------------------------------------------------------
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 */
215int spi_xfer_vector(const struct spi_slave *slave,
216 struct spi_op vectors[], size_t count);
217
Kyösti Mälkki11104952014-06-29 16:17:33 +0300218unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
219
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700220/*-----------------------------------------------------------------------
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 Shaikh0dba0252016-11-30 04:34:22 -0800229static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700230{
231 unsigned char dout[2];
232 unsigned char din[2];
233 int ret;
234
235 dout[0] = byte;
236 dout[1] = 0;
237
Gabe Black93d9f922014-03-27 21:52:43 -0700238 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700239 return ret < 0 ? ret : din[1];
240}
241
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800242/*
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 */
258int spi_xfer_two_vectors(const struct spi_slave *slave,
259 struct spi_op vectors[], size_t count);
260
Patrick Georgibc64cae2013-02-11 22:12:55 +0100261#endif /* _SPI_GENERIC_H_ */