Warnings purge of drivers (continued)
[gpxe.git] / src / drivers / bitbash / spi_bit.c
blobe2175d60337570e8e323d0c6871cec0f178c5a0a
1 /*
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <byteswap.h>
23 #include <errno.h>
24 #include <assert.h>
25 #include <timer.h>
26 #include <gpxe/bitbash.h>
27 #include <gpxe/spi_bit.h>
29 /** @file
31 * SPI bit-bashing interface
35 /** Delay between SCLK changes and around SS changes */
36 static void spi_bit_delay ( void ) {
37 udelay ( SPI_BIT_UDELAY );
40 /** Chip select line will be asserted */
41 #define SELECT_SLAVE 0
43 /** Chip select line will be deasserted */
44 #define DESELECT_SLAVE SPI_MODE_SSPOL
46 /**
47 * Select/deselect slave
49 * @v spibit SPI bit-bashing interface
50 * @v slave Slave number
51 * @v state Slave select state
53 * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
55 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
56 unsigned int slave,
57 unsigned int state ) {
58 struct bit_basher *basher = &spibit->basher;
60 state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
61 DBG ( "Setting slave %d select %s\n", slave,
62 ( state ? "high" : "low" ) );
64 spi_bit_delay();
65 write_bit ( basher, SPI_BIT_SS ( slave ), state );
66 spi_bit_delay();
69 /**
70 * Transfer bits over SPI bit-bashing bus
72 * @v bus SPI bus
73 * @v data_out TX data buffer (or NULL)
74 * @v data_in RX data buffer (or NULL)
75 * @v len Length of transfer (in @b bits)
76 * @v endianness Endianness of this data transfer
78 * This issues @c len clock cycles on the SPI bus, shifting out data
79 * from the @c data_out buffer to the MOSI line and shifting in data
80 * from the MISO line to the @c data_in buffer. If @c data_out is
81 * NULL, then the data sent will be all zeroes. If @c data_in is
82 * NULL, then the incoming data will be discarded.
84 static void spi_bit_transfer ( struct spi_bit_basher *spibit,
85 const void *data_out, void *data_in,
86 unsigned int len, int endianness ) {
87 struct spi_bus *bus = &spibit->bus;
88 struct bit_basher *basher = &spibit->basher;
89 unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
90 unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
91 unsigned int bit_offset;
92 unsigned int byte_offset;
93 unsigned int byte_mask;
94 unsigned int bit;
95 unsigned int step;
97 DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
99 for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
100 /* Calculate byte offset and byte mask */
101 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
102 ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
103 byte_offset = ( bit_offset / 8 );
104 byte_mask = ( 1 << ( bit_offset % 8 ) );
106 /* Shift data in or out */
107 if ( sclk == cpha ) {
108 const uint8_t *byte;
110 /* Shift data out */
111 if ( data_out ) {
112 byte = ( data_out + byte_offset );
113 bit = ( *byte & byte_mask );
114 } else {
115 bit = 0;
117 write_bit ( basher, SPI_BIT_MOSI, bit );
118 } else {
119 uint8_t *byte;
121 /* Shift data in */
122 bit = read_bit ( basher, SPI_BIT_MISO );
123 if ( data_in ) {
124 byte = ( data_in + byte_offset );
125 *byte &= ~byte_mask;
126 *byte |= ( bit & byte_mask );
130 /* Toggle clock line */
131 spi_bit_delay();
132 sclk = ~sclk;
133 write_bit ( basher, SPI_BIT_SCLK, sclk );
138 * Read/write data via SPI bit-bashing bus
140 * @v bus SPI bus
141 * @v device SPI device
142 * @v command Command
143 * @v address Address to read/write (<0 for no address)
144 * @v data_out TX data buffer (or NULL)
145 * @v data_in RX data buffer (or NULL)
146 * @v len Length of transfer
147 * @ret rc Return status code
149 static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
150 unsigned int command, int address,
151 const void *data_out, void *data_in, size_t len ) {
152 struct spi_bit_basher *spibit
153 = container_of ( bus, struct spi_bit_basher, bus );
154 uint32_t tmp;
156 /* Assert chip select on specified slave */
157 spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
159 /* Transmit command */
160 assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
161 tmp = cpu_to_le32 ( command );
162 spi_bit_transfer ( spibit, &tmp, NULL, device->command_len,
163 SPI_BIT_BIG_ENDIAN );
165 /* Transmit address, if present */
166 if ( address >= 0 ) {
167 assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
168 tmp = cpu_to_le32 ( address );
169 spi_bit_transfer ( spibit, &tmp, NULL, device->address_len,
170 SPI_BIT_BIG_ENDIAN );
173 /* Transmit/receive data */
174 spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
175 spibit->endianness );
177 /* Deassert chip select on specified slave */
178 spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
180 return 0;
184 * Initialise SPI bit-bashing interface
186 * @v spibit SPI bit-bashing interface
188 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
189 assert ( &spibit->basher.op->read != NULL );
190 assert ( &spibit->basher.op->write != NULL );
191 spibit->bus.rw = spi_bit_rw;