| 1 | /* |
| 2 | * Common SPI Interface: Controller-specific definitions |
| 3 | * |
| 4 | * (C) Copyright 2001 |
| 5 | * Gerald Van Baren, Custom IDEAS, [email protected]. |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #ifndef _SPI_H_ |
| 11 | #define _SPI_H_ |
| 12 | |
| 13 | #include <comdef.h> |
| 14 | |
| 15 | #define SPI_DATEMODE_32 32 |
| 16 | #define SPI_DATAMODE_16 16 |
| 17 | #define SPI_DATAMODE_8 8 |
| 18 | |
| 19 | /* SPI mode flags */ |
| 20 | #define SPI_CPHA 0x01 /* clock phase */ |
| 21 | #define SPI_CPOL 0x02 /* clock polarity */ |
| 22 | #define SPI_MODE_0 (0|0) /* (original MicroWire) */ |
| 23 | #define SPI_MODE_1 (0|SPI_CPHA) |
| 24 | #define SPI_MODE_2 (SPI_CPOL|0) |
| 25 | #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) |
| 26 | #define SPI_CS_HIGH 0x04 /* CS active high */ |
| 27 | #define SPI_LSB_FIRST 0x08 /* per-word bits-on-wire */ |
| 28 | #define SPI_3WIRE 0x10 /* SI/SO signals shared */ |
| 29 | #define SPI_LOOP 0x20 /* loopback mode */ |
| 30 | #define SPI_SLAVE 0x40 /* slave mode */ |
| 31 | #define SPI_PREAMBLE 0x80 /* Skip preamble bytes */ |
| 32 | |
| 33 | /* SPI transfer flags */ |
| 34 | #define SPI_XFER_BEGIN 0x01 /* Assert CS before transfer */ |
| 35 | #define SPI_XFER_END 0x02 /* Deassert CS after transfer */ |
| 36 | #define SPI_XFER_MMAP 0x08 /* Memory Mapped start */ |
| 37 | #define SPI_XFER_MMAP_END 0x10 /* Memory Mapped End */ |
| 38 | #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END) |
| 39 | #define SPI_XFER_U_PAGE (1 << 5) |
| 40 | |
| 41 | /* SPI TX operation modes */ |
| 42 | #define SPI_OPM_TX_QPP 1 << 0 |
| 43 | |
| 44 | /* SPI RX operation modes */ |
| 45 | #define SPI_OPM_RX_AS 1 << 0 |
| 46 | #define SPI_OPM_RX_DOUT 1 << 1 |
| 47 | #define SPI_OPM_RX_DIO 1 << 2 |
| 48 | #define SPI_OPM_RX_QOF 1 << 3 |
| 49 | #define SPI_OPM_RX_QIOF 1 << 4 |
| 50 | #define SPI_OPM_RX_EXTN SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \ |
| 51 | SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \ |
| 52 | SPI_OPM_RX_QIOF |
| 53 | |
| 54 | /* SPI bus connection options */ |
| 55 | #define SPI_CONN_DUAL_SHARED 1 << 0 |
| 56 | #define SPI_CONN_DUAL_SEPARATED 1 << 1 |
| 57 | |
| 58 | /* Header byte that marks the start of the message */ |
| 59 | #define SPI_PREAMBLE_END_BYTE 0xec |
| 60 | |
| 61 | #define SPI_DEFAULT_WORDLEN 8 |
| 62 | |
| 63 | /*----------------------------------------------------------------------- |
| 64 | * Representation of a SPI slave, i.e. what we're communicating with. |
| 65 | * |
| 66 | * Drivers are expected to extend this with controller-specific data. |
| 67 | * |
| 68 | * bus: ID of the bus that the slave is attached to. |
| 69 | * cs: ID of the chip select connected to the slave. |
| 70 | */ |
| 71 | |
| 72 | struct spi_slave { |
| 73 | unsigned int bus; |
| 74 | unsigned int bus_width; |
| 75 | #define NOT_SWAP_MODE 0 |
| 76 | #define SWAP_MODE 1 |
| 77 | unsigned int bus_order; |
| 78 | unsigned int cs; |
| 79 | }; |
| 80 | |
| 81 | struct spi_operation{ |
| 82 | struct spi_slave* (*setup_slave)(unsigned int bus, unsigned int cs, |
| 83 | unsigned int max_hz, u32 mode, u32 fifo_width); |
| 84 | int (*spi_xfer)(struct spi_slave *slave, unsigned int bitlen, void *dout, |
| 85 | void *din, unsigned long flags,int bit_mode); |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Initialization, must be called once on start up. |
| 91 | * |
| 92 | */ |
| 93 | void spi_init(void); |
| 94 | |
| 95 | /** |
| 96 | * spi_do_alloc_slave - Allocate a new SPI slave (internal) |
| 97 | * |
| 98 | * Allocate and zero all fields in the spi slave, and set the bus/chip |
| 99 | * select. Use the helper macro spi_alloc_slave() to call this. |
| 100 | * |
| 101 | * @offset: Offset of struct spi_slave within slave structure. |
| 102 | * @size: Size of slave structure. |
| 103 | * @bus: Bus ID of the slave chip. |
| 104 | * @cs: Chip select ID of the slave chip on the specified bus. |
| 105 | */ |
| 106 | void *spi_do_alloc_slave(int offset, int size, unsigned int bus, |
| 107 | unsigned int cs); |
| 108 | |
| 109 | /** |
| 110 | * spi_alloc_slave - Allocate a new SPI slave |
| 111 | * |
| 112 | * Allocate and zero all fields in the spi slave, and set the bus/chip |
| 113 | * select. |
| 114 | * |
| 115 | * @_struct: Name of structure to allocate (e.g. struct tegra_spi). |
| 116 | * This structure must contain a member 'struct spi_slave *slave'. |
| 117 | * @bus: Bus ID of the slave chip. |
| 118 | * @cs: Chip select ID of the slave chip on the specified bus. |
| 119 | */ |
| 120 | #define spi_alloc_slave(_struct, bus, cs) \ |
| 121 | spi_do_alloc_slave(offsetof(_struct, slave), \ |
| 122 | sizeof(_struct), bus, cs) |
| 123 | |
| 124 | /** |
| 125 | * spi_alloc_slave_base - Allocate a new SPI slave with no private data |
| 126 | * |
| 127 | * Allocate and zero all fields in the spi slave, and set the bus/chip |
| 128 | * select. |
| 129 | * |
| 130 | * @bus: Bus ID of the slave chip. |
| 131 | * @cs: Chip select ID of the slave chip on the specified bus. |
| 132 | */ |
| 133 | #define spi_alloc_slave_base(bus, cs) \ |
| 134 | spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs) |
| 135 | |
| 136 | int spi_register(unsigned int bus, struct spi_operation *operation); |
| 137 | |
| 138 | /** |
| 139 | * Set up communications parameters for a SPI slave. |
| 140 | * |
| 141 | * This must be called once for each slave. Note that this function |
| 142 | * usually doesn't touch any actual hardware, it only initializes the |
| 143 | * contents of spi_slave so that the hardware can be easily |
| 144 | * initialized later. |
| 145 | * |
| 146 | * @bus: Bus ID of the slave chip. |
| 147 | * @cs: Chip select ID of the slave chip on the specified bus. |
| 148 | * @max_hz: Maximum SCK rate in Hz. |
| 149 | * @mode: Clock polarity, clock phase and other parameters. |
| 150 | * |
| 151 | * Returns: A spi_slave reference that can be used in subsequent SPI |
| 152 | * calls, or NULL if one or more of the parameters are not supported. |
| 153 | */ |
| 154 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 155 | unsigned int max_hz, unsigned int mode, unsigned int bus_width); |
| 156 | |
| 157 | /** |
| 158 | * Free any memory associated with a SPI slave. |
| 159 | * |
| 160 | * @slave: The SPI slave |
| 161 | */ |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * SPI transfer |
| 166 | * |
| 167 | * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks |
| 168 | * "bitlen" bits in the SPI MISO port. That's just the way SPI works. |
| 169 | * |
| 170 | * The source of the outgoing bits is the "dout" parameter and the |
| 171 | * destination of the input bits is the "din" parameter. Note that "dout" |
| 172 | * and "din" can point to the same memory location, in which case the |
| 173 | * input data overwrites the output data (since both are buffered by |
| 174 | * temporary variables, this is OK). |
| 175 | * |
| 176 | * spi_xfer() interface: |
| 177 | * @slave: The SPI slave which will be sending/receiving the data. |
| 178 | * @bitlen: How many bits to write and read. |
| 179 | * @dout: Pointer to a string of bits to send out. The bits are |
| 180 | * held in a byte array and are sent MSB first. |
| 181 | * @din: Pointer to a string of bits that will be filled in. |
| 182 | * @flags: A bitwise combination of SPI_XFER_* flags. |
| 183 | * |
| 184 | * Returns: 0 on success, not 0 on failure |
| 185 | */ |
| 186 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, void *dout, |
| 187 | void *din, unsigned long flags, int bit_mode); |
| 188 | |
| 189 | |
| 190 | #endif /* _SPI_H_ */ |