2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
29 #include <ipxe/bitbash.h>
30 #include <ipxe/spi_bit.h>
34 * SPI bit-bashing interface
38 /** Delay between SCLK changes and around SS changes */
39 static void spi_bit_delay ( void ) {
40 udelay ( SPI_BIT_UDELAY );
43 /** Chip select line will be asserted */
44 #define SELECT_SLAVE 0
46 /** Chip select line will be deasserted */
47 #define DESELECT_SLAVE SPI_MODE_SSPOL
50 * Select/deselect slave
52 * @v spibit SPI bit-bashing interface
53 * @v slave Slave number
54 * @v state Slave select state
56 * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
58 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
60 unsigned int state ) {
61 struct bit_basher *basher = &spibit->basher;
63 state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
64 DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
65 spibit, slave, ( state ? "high" : "low" ) );
68 write_bit ( basher, SPI_BIT_SS ( slave ), state );
73 * Transfer bits over SPI bit-bashing bus
76 * @v data_out TX data buffer (or NULL)
77 * @v data_in RX data buffer (or NULL)
78 * @v len Length of transfer (in @b bits)
79 * @v endianness Endianness of this data transfer
81 * This issues @c len clock cycles on the SPI bus, shifting out data
82 * from the @c data_out buffer to the MOSI line and shifting in data
83 * from the MISO line to the @c data_in buffer. If @c data_out is
84 * NULL, then the data sent will be all zeroes. If @c data_in is
85 * NULL, then the incoming data will be discarded.
87 static void spi_bit_transfer ( struct spi_bit_basher *spibit,
88 const void *data_out, void *data_in,
89 unsigned int len, int endianness ) {
90 struct spi_bus *bus = &spibit->bus;
91 struct bit_basher *basher = &spibit->basher;
92 unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
93 unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
94 unsigned int bit_offset;
95 unsigned int byte_offset;
96 unsigned int byte_mask;
100 DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
101 spibit, len, bus->mode );
103 for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
104 /* Calculate byte offset and byte mask */
105 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
106 ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
107 byte_offset = ( bit_offset / 8 );
108 byte_mask = ( 1 << ( bit_offset % 8 ) );
110 /* Shift data in or out */
111 if ( sclk == cpha ) {
116 byte = ( data_out + byte_offset );
117 bit = ( *byte & byte_mask );
118 DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
119 spibit, ( bit ? 1 : 0 ) );
123 write_bit ( basher, SPI_BIT_MOSI, bit );
128 bit = read_bit ( basher, SPI_BIT_MISO );
130 DBGCP ( spibit, "SPIBIT %p read bit %d\n",
131 spibit, ( bit ? 1 : 0 ) );
132 byte = ( data_in + byte_offset );
134 *byte |= ( bit & byte_mask );
138 /* Toggle clock line */
141 write_bit ( basher, SPI_BIT_SCLK, sclk );
146 * Read/write data via SPI bit-bashing bus
149 * @v device SPI device
151 * @v address Address to read/write (<0 for no address)
152 * @v data_out TX data buffer (or NULL)
153 * @v data_in RX data buffer (or NULL)
154 * @v len Length of transfer
155 * @ret rc Return status code
157 static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
158 unsigned int command, int address,
159 const void *data_out, void *data_in, size_t len ) {
160 struct spi_bit_basher *spibit
161 = container_of ( bus, struct spi_bit_basher, bus );
162 uint32_t tmp_command;
163 uint32_t tmp_address;
164 uint32_t tmp_address_detect;
166 /* Open bit-bashing interface */
167 open_bit ( &spibit->basher );
169 /* Deassert chip select to reset specified slave */
170 spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
172 /* Set clock line to idle state */
173 write_bit ( &spibit->basher, SPI_BIT_SCLK,
174 ( bus->mode & SPI_MODE_CPOL ) );
176 /* Assert chip select on specified slave */
177 spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
179 /* Transmit command */
180 assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
181 tmp_command = cpu_to_le32 ( command );
182 spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
183 SPI_BIT_BIG_ENDIAN );
185 /* Transmit address, if present */
186 if ( address >= 0 ) {
187 assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
188 tmp_address = cpu_to_le32 ( address );
189 if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
190 /* Autodetect address length. This relies on
191 * the device responding with a dummy zero
192 * data bit before the first real data bit.
194 DBGC ( spibit, "SPIBIT %p autodetecting device "
195 "address length\n", spibit );
196 assert ( address == 0 );
197 device->address_len = 0;
199 spi_bit_transfer ( spibit, &tmp_address,
200 &tmp_address_detect, 1,
201 SPI_BIT_BIG_ENDIAN );
202 device->address_len++;
203 } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
204 DBGC ( spibit, "SPIBIT %p autodetected device address "
205 "length %d\n", spibit, device->address_len );
207 spi_bit_transfer ( spibit, &tmp_address, NULL,
209 SPI_BIT_BIG_ENDIAN );
213 /* Transmit/receive data */
214 spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
215 spibit->endianness );
217 /* Deassert chip select on specified slave */
218 spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
220 /* Close bit-bashing interface */
221 close_bit ( &spibit->basher );
227 * Initialise SPI bit-bashing interface
229 * @v spibit SPI bit-bashing interface
231 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
232 assert ( &spibit->basher.op->read != NULL );
233 assert ( &spibit->basher.op->write != NULL );
234 spibit->bus.rw = spi_bit_rw;