blob: 7eb18a6ac870dc95cbf47b83d37690c1220dd93e [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 Shaikh94f86992016-12-01 07:12:32 -080062/*-----------------------------------------------------------------------
63 * Representation of a SPI contoller.
64 *
65 * claim_bus: Claim SPI bus and prepare for communication.
66 * release_bus: Release SPI bus.
Furquan Shaikhb5d41cb2016-12-01 07:25:31 -080067 * setup: Setup given SPI device bus.
Furquan Shaikhc2973d12016-11-29 22:07:42 -080068 * xfer: Perform one SPI transfer operation.
69 * xfer_vector: Vector of SPI transfer operations.
Furquan Shaikh94f86992016-12-01 07:12:32 -080070 */
71struct spi_ctrlr {
72 int (*claim_bus)(const struct spi_slave *slave);
73 void (*release_bus)(const struct spi_slave *slave);
Furquan Shaikhc2973d12016-11-29 22:07:42 -080074 int (*setup)(const struct spi_slave *slave);
Furquan Shaikh94f86992016-12-01 07:12:32 -080075 int (*xfer)(const struct spi_slave *slave, const void *dout,
76 size_t bytesout, void *din, size_t bytesin);
Furquan Shaikhc2973d12016-11-29 22:07:42 -080077 int (*xfer_vector)(const struct spi_slave *slave,
78 struct spi_op vectors[], size_t count);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070079};
80
81/*-----------------------------------------------------------------------
Furquan Shaikhb5d41cb2016-12-01 07:25:31 -080082 * 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 */
88struct 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. */
95extern const struct spi_ctrlr_buses spi_ctrlr_bus_map[];
96extern const size_t spi_ctrlr_bus_map_count;
97
98/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070099 * Initialization, must be called once on start up.
100 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700101 */
102void 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 Shaikh36b81af2016-12-01 01:02:44 -0800114 * slave: Pointer to slave structure that needs to be initialized.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700115 *
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800116 * Returns:
117 * 0 on success, -1 on error
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700118 */
Furquan Shaikh36b81af2016-12-01 01:02:44 -0800119int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120
121/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122 * 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 Shaikh0dba0252016-11-30 04:34:22 -0800135int spi_claim_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700136
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 Shaikh0dba0252016-11-30 04:34:22 -0800146void spi_release_bus(const struct spi_slave *slave);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700147
148/*-----------------------------------------------------------------------
149 * SPI transfer
150 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700151 * spi_xfer() interface:
152 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -0700153 * 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 Reinauer1c56d9b2012-05-10 11:27:32 -0700157 *
158 * Returns: 0 on success, not 0 on failure
159 */
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800160int spi_xfer(const struct spi_slave *slave, const void *dout, size_t bytesout,
161 void *din, size_t bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700162
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800163/*-----------------------------------------------------------------------
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 */
173int spi_xfer_vector(const struct spi_slave *slave,
174 struct spi_op vectors[], size_t count);
175
Kyösti Mälkki11104952014-06-29 16:17:33 +0300176unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
177
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700178/*-----------------------------------------------------------------------
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 Shaikh0dba0252016-11-30 04:34:22 -0800187static inline int spi_w8r8(const struct spi_slave *slave, unsigned char byte)
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700188{
189 unsigned char dout[2];
190 unsigned char din[2];
191 int ret;
192
193 dout[0] = byte;
194 dout[1] = 0;
195
Gabe Black93d9f922014-03-27 21:52:43 -0700196 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700197 return ret < 0 ? ret : din[1];
198}
199
Furquan Shaikhc2973d12016-11-29 22:07:42 -0800200/*
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 */
216int spi_xfer_two_vectors(const struct spi_slave *slave,
217 struct spi_op vectors[], size_t count);
218
Patrick Georgibc64cae2013-02-11 22:12:55 +0100219#endif /* _SPI_GENERIC_H_ */