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