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 | * |
| 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 Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 24 | #ifndef _SPI_GENERIC_H_ |
| 25 | #define _SPI_GENERIC_H_ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 26 | |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | /* Controller-specific definitions: */ |
| 30 | |
Duncan Laurie | a2f1b95 | 2012-08-27 11:10:43 -0700 | [diff] [blame] | 31 | /* SPI opcodes */ |
| 32 | #define SPI_OPCODE_WREN 0x06 |
Duncan Laurie | 23b0053 | 2012-10-10 14:21:23 -0700 | [diff] [blame] | 33 | #define SPI_OPCODE_FAST_READ 0x0b |
Duncan Laurie | a2f1b95 | 2012-08-27 11:10:43 -0700 | [diff] [blame] | 34 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 35 | #define SPI_READ_FLAG 0x01 |
| 36 | #define SPI_WRITE_FLAG 0x02 |
| 37 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 38 | /*----------------------------------------------------------------------- |
| 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 Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 45 | * rw: Read or Write flag |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 46 | */ |
| 47 | struct spi_slave { |
| 48 | unsigned int bus; |
| 49 | unsigned int cs; |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 50 | unsigned int rw; |
Vladimir Serbinenko | e23bd0e | 2014-01-18 17:45:32 +0100 | [diff] [blame] | 51 | int force_programmer_specific; |
| 52 | struct spi_flash * (*programmer_specific_probe) (struct spi_slave *spi); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /*----------------------------------------------------------------------- |
| 56 | * Initialization, must be called once on start up. |
| 57 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 58 | */ |
| 59 | void 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 Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 71 | * |
| 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 Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 75 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 76 | |
| 77 | /*----------------------------------------------------------------------- |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 78 | * 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 | */ |
| 91 | int 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 | */ |
| 102 | void spi_release_bus(struct spi_slave *slave); |
| 103 | |
| 104 | /*----------------------------------------------------------------------- |
| 105 | * SPI transfer |
| 106 | * |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 107 | * spi_xfer() interface: |
| 108 | * slave: The SPI slave which will be sending/receiving the data. |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 109 | * 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 Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 113 | * |
| 114 | * Returns: 0 on success, not 0 on failure |
| 115 | */ |
Gabe Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 116 | int spi_xfer(struct spi_slave *slave, const void *dout, unsigned int bytesout, |
| 117 | void *din, unsigned int bytesin); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 118 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 119 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 120 | |
Kyösti Mälkki | 1110495 | 2014-06-29 16:17:33 +0300 | [diff] [blame] | 121 | unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len); |
| 122 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 123 | /*----------------------------------------------------------------------- |
| 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 | */ |
| 132 | static 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 Black | 93d9f92 | 2014-03-27 21:52:43 -0700 | [diff] [blame] | 141 | ret = spi_xfer(slave, dout, 2, din, 2); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 142 | return ret < 0 ? ret : din[1]; |
| 143 | } |
| 144 | |
Patrick Georgi | bc64cae | 2013-02-11 22:12:55 +0100 | [diff] [blame] | 145 | #endif /* _SPI_GENERIC_H_ */ |