blob: 9097f48c133c27101e2426135459625fc0658d88 [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>
20
21/* Controller-specific definitions: */
22
Duncan Lauriea2f1b952012-08-27 11:10:43 -070023/* SPI opcodes */
24#define SPI_OPCODE_WREN 0x06
Duncan Laurie23b00532012-10-10 14:21:23 -070025#define SPI_OPCODE_FAST_READ 0x0b
Duncan Lauriea2f1b952012-08-27 11:10:43 -070026
Martin Roth3316cf22012-12-05 16:22:54 -070027#define SPI_READ_FLAG 0x01
28#define SPI_WRITE_FLAG 0x02
29
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070030/*-----------------------------------------------------------------------
31 * Representation of a SPI slave, i.e. what we're communicating with.
32 *
33 * Drivers are expected to extend this with controller-specific data.
34 *
35 * bus: ID of the bus that the slave is attached to.
36 * cs: ID of the chip select connected to the slave.
Martin Roth3316cf22012-12-05 16:22:54 -070037 * rw: Read or Write flag
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -080038 * max_transfer_size: maximum amount of bytes which can be sent in a single
39 * read or write transaction, usually this is a controller
40 * property, kept in the slave structure for convenience. Zero in
41 * this field means 'unlimited'.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070042 */
43struct spi_slave {
44 unsigned int bus;
45 unsigned int cs;
Martin Roth3316cf22012-12-05 16:22:54 -070046 unsigned int rw;
Vadim Bendeburyf9ff3532014-11-29 15:06:26 -080047 unsigned int max_transfer_size;
Vladimir Serbinenkoe23bd0e2014-01-18 17:45:32 +010048 int force_programmer_specific;
49 struct spi_flash * (*programmer_specific_probe) (struct spi_slave *spi);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070050};
51
52/*-----------------------------------------------------------------------
53 * Initialization, must be called once on start up.
54 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070055 */
56void spi_init(void);
57
58/*-----------------------------------------------------------------------
59 * Set up communications parameters for a SPI slave.
60 *
61 * This must be called once for each slave. Note that this function
62 * usually doesn't touch any actual hardware, it only initializes the
63 * contents of spi_slave so that the hardware can be easily
64 * initialized later.
65 *
66 * bus: Bus ID of the slave chip.
67 * cs: Chip select ID of the slave chip on the specified bus.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070068 *
69 * Returns: A spi_slave reference that can be used in subsequent SPI
70 * calls, or NULL if one or more of the parameters are not supported.
71 */
Gabe Black1e187352014-03-27 20:37:03 -070072struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070073
74/*-----------------------------------------------------------------------
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070075 * Claim the bus and prepare it for communication with a given slave.
76 *
77 * This must be called before doing any transfers with a SPI slave. It
78 * will enable and initialize any SPI hardware as necessary, and make
79 * sure that the SCK line is in the correct idle state. It is not
80 * allowed to claim the same bus for several slaves without releasing
81 * the bus in between.
82 *
83 * slave: The SPI slave
84 *
85 * Returns: 0 if the bus was claimed successfully, or a negative value
86 * if it wasn't.
87 */
88int spi_claim_bus(struct spi_slave *slave);
89
90/*-----------------------------------------------------------------------
91 * Release the SPI bus
92 *
93 * This must be called once for every call to spi_claim_bus() after
94 * all transfers have finished. It may disable any SPI hardware as
95 * appropriate.
96 *
97 * slave: The SPI slave
98 */
99void spi_release_bus(struct spi_slave *slave);
100
101/*-----------------------------------------------------------------------
102 * SPI transfer
103 *
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700104 * spi_xfer() interface:
105 * slave: The SPI slave which will be sending/receiving the data.
Gabe Black93d9f922014-03-27 21:52:43 -0700106 * dout: Pointer to a string of bytes to send out.
107 * bytesout: How many bytes to write.
108 * din: Pointer to a string of bytes that will be filled in.
109 * bytesin: How many bytes to read.
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700110 *
111 * Returns: 0 on success, not 0 on failure
112 */
Gabe Black93d9f922014-03-27 21:52:43 -0700113int spi_xfer(struct spi_slave *slave, const void *dout, unsigned int bytesout,
114 void *din, unsigned int bytesin);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700115
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700116
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700117
Kyösti Mälkki11104952014-06-29 16:17:33 +0300118unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len);
119
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700120/*-----------------------------------------------------------------------
121 * Write 8 bits, then read 8 bits.
122 * slave: The SPI slave we're communicating with
123 * byte: Byte to be written
124 *
125 * Returns: The value that was read, or a negative value on error.
126 *
127 * TODO: This function probably shouldn't be inlined.
128 */
129static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
130{
131 unsigned char dout[2];
132 unsigned char din[2];
133 int ret;
134
135 dout[0] = byte;
136 dout[1] = 0;
137
Gabe Black93d9f922014-03-27 21:52:43 -0700138 ret = spi_xfer(slave, dout, 2, din, 2);
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700139 return ret < 0 ? ret : din[1];
140}
141
Patrick Georgibc64cae2013-02-11 22:12:55 +0100142#endif /* _SPI_GENERIC_H_ */