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.
26 #include <gpxe/bitbash.h>
31 * SPI bit-bashing interface
35 /** Delay between SCLK changes and around SS changes */
36 static void spi_delay ( void ) {
37 udelay ( SPI_UDELAY );
41 * Select/deselect slave
43 * @v spi SPI bit-bashing interface
44 * @v slave Slave number
45 * @v state Slave select state
47 * @c state must be set to zero to select the specified slave, or to
48 * @c SPI_MODE_SSPOL to deselect the slave.
50 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
52 unsigned int state ) {
53 struct bit_basher *basher = &spibit->basher;
55 state ^= ( spibit->spi.mode & SPI_MODE_SSPOL );
56 DBG ( "Setting slave %d select %s\n", slave,
57 ( state ? "high" : "low" ) );
60 write_bit ( basher, SPI_BIT_SS ( slave ), state );
67 * @v spi SPI interface
68 * @v slave Slave number
70 static void spi_bit_select_slave ( struct spi_interface *spi,
71 unsigned int slave ) {
72 struct spi_bit_basher *spibit
73 = container_of ( spi, struct spi_bit_basher, spi );
75 spibit->slave = slave;
76 spi_bit_set_slave_select ( spibit, slave, 0 );
82 * @v spi SPI interface
84 static void spi_bit_deselect_slave ( struct spi_interface *spi ) {
85 struct spi_bit_basher *spibit
86 = container_of ( spi, struct spi_bit_basher, spi );
88 spi_bit_set_slave_select ( spibit, spibit->slave, SPI_MODE_SSPOL );
92 * Transfer bits over SPI bit-bashing interface
94 * @v spi SPI interface
95 * @v data_out TX data buffer (or NULL)
96 * @v data_in RX data buffer (or NULL)
97 * @v len Length of transfer (in @b bits)
99 * This issues @c len clock cycles on the SPI bus, shifting out data
100 * from the @c data_out buffer to the MOSI line and shifting in data
101 * from the MISO line to the @c data_in buffer. If @c data_out is
102 * NULL, then the data sent will be all zeroes. If @c data_in is
103 * NULL, then the incoming data will be discarded.
105 static void spi_bit_transfer ( struct spi_interface *spi, const void *data_out,
106 void *data_in, unsigned int len ) {
107 struct spi_bit_basher *spibit
108 = container_of ( spi, struct spi_bit_basher, spi );
109 struct bit_basher *basher = &spibit->basher;
110 unsigned int sclk = ( ( spi->mode & SPI_MODE_CPOL ) ? 1 : 0 );
111 unsigned int cpha = ( ( spi->mode & SPI_MODE_CPHA ) ? 1 : 0 );
117 DBG ( "Transferring %d bits in mode %x\n", len, spi->mode );
119 for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
120 /* Calculate byte offset within data and bit mask */
121 offset = ( step / 16 );
122 mask = ( 1 << ( ( step % 16 ) / 2 ) );
124 /* Shift data in or out */
125 if ( sclk == cpha ) {
130 byte = ( data_out + offset );
131 bit = ( *byte & mask );
135 write_bit ( basher, SPI_BIT_MOSI, bit );
140 bit = read_bit ( basher, SPI_BIT_MISO );
142 byte = ( data_in + offset );
144 *byte |= ( bit & mask );
148 /* Toggle clock line */
151 write_bit ( basher, SPI_BIT_SCLK, sclk );
156 * Initialise SPI bit-bashing interface
158 * @v spibit SPI bit-bashing interface
160 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
161 assert ( &spibit->basher.read != NULL );
162 assert ( &spibit->basher.write != NULL );
163 spibit->spi.select_slave = spi_bit_select_slave;
164 spibit->spi.deselect_slave = spi_bit_deselect_slave;
165 spibit->spi.transfer = spi_bit_transfer;