blob: bd0020f5d2f07d17ce4ca19f68fbe34524b84abb [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 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
Patrick Georgibc64cae2013-02-11 22:12:55 +010024#ifndef _SPI_GENERIC_H_
25#define _SPI_GENERIC_H_
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026
27#include <stdint.h>
28
29/* Controller-specific definitions: */
30
Duncan Lauriea2f1b952012-08-27 11:10:43 -070031/* SPI opcodes */
32#define SPI_OPCODE_WREN 0x06
Duncan Laurie23b00532012-10-10 14:21:23 -070033#define SPI_OPCODE_FAST_READ 0x0b
Duncan Lauriea2f1b952012-08-27 11:10:43 -070034
Martin Roth3316cf22012-12-05 16:22:54 -070035#define SPI_READ_FLAG 0x01
36#define SPI_WRITE_FLAG 0x02
37
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070038/*-----------------------------------------------------------------------
39 * Representation of a SPI slave, i.e. what we're communicating with.
40 *
41 * Drivers are expected to extend this with controller-specific data.
42 *
43 * bus: ID of the bus that the slave is attached to.
44 * cs: ID of the chip select connected to the slave.
Martin Roth3316cf22012-12-05 16:22:54 -070045 * rw: Read or Write flag
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070046 */
47struct spi_slave {
48 unsigned int bus;
49 unsigned int cs;
Martin Roth3316cf22012-12-05 16:22:54 -070050 unsigned int rw;
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +010051 int force_programmer_specific;
52 struct spi_flash * (*programmer_specific_probe) (struct spi_slave *spi);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070053};
54
55/*-----------------------------------------------------------------------
56 * Initialization, must be called once on start up.
57 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070058 */
59void spi_init(void);
60
61/*-----------------------------------------------------------------------
62 * Set up communications parameters for a SPI slave.
63 *
64 * This must be called once for each slave. Note that this function
65 * usually doesn't touch any actual hardware, it only initializes the
66 * contents of spi_slave so that the hardware can be easily
67 * initialized later.
68 *
69 * bus: Bus ID of the slave chip.
70 * cs: Chip select ID of the slave chip on the specified bus.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070071 *
72 * Returns: A spi_slave reference that can be used in subsequent SPI
73 * calls, or NULL if one or more of the parameters are not supported.
74 */
Gabe Black1e187352014-03-27 20:37:03 -070075struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070076
77/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070078 * Claim the bus and prepare it for communication with a given slave.
79 *
80 * This must be called before doing any transfers with a SPI slave. It
81 * will enable and initialize any SPI hardware as necessary, and make
82 * sure that the SCK line is in the correct idle state. It is not
83 * allowed to claim the same bus for several slaves without releasing
84 * the bus in between.
85 *
86 * slave: The SPI slave
87 *
88 * Returns: 0 if the bus was claimed successfully, or a negative value
89 * if it wasn't.
90 */
91int spi_claim_bus(struct spi_slave *slave);
92
93/*-----------------------------------------------------------------------
94 * Release the SPI bus
95 *
96 * This must be called once for every call to spi_claim_bus() after
97 * all transfers have finished. It may disable any SPI hardware as
98 * appropriate.
99 *
100 * slave: The SPI slave
101 */
102void spi_release_bus(struct spi_slave *slave);
103
104/*-----------------------------------------------------------------------
105 * SPI transfer
106 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700107 * spi_xfer() interface:
108 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -0700109 * dout: Pointer to a string of bytes to send out.
110 * bytesout: How many bytes to write.
111 * din: Pointer to a string of bytes that will be filled in.
112 * bytesin: How many bytes to read.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700113 *
114 * Returns: 0 on success, not 0 on failure
115 */
Gabe Black93d9f922014-03-27 21:52:43 -0700116int spi_xfer(struct spi_slave *slave, const void *dout, unsigned int bytesout,
117 void *din, unsigned int bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700118
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700119
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120
Kyösti Mälkki11104952014-06-29 16:17:33 +0300121unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
122
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700123/*-----------------------------------------------------------------------
124 * Write 8 bits, then read 8 bits.
125 * slave: The SPI slave we're communicating with
126 * byte: Byte to be written
127 *
128 * Returns: The value that was read, or a negative value on error.
129 *
130 * TODO: This function probably shouldn't be inlined.
131 */
132static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
133{
134 unsigned char dout[2];
135 unsigned char din[2];
136 int ret;
137
138 dout[0] = byte;
139 dout[1] = 0;
140
Gabe Black93d9f922014-03-27 21:52:43 -0700141 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700142 return ret < 0 ? ret : din[1];
143}
144
Patrick Georgibc64cae2013-02-11 22:12:55 +0100145#endif /* _SPI_GENERIC_H_ */