iPXE
spi_bit.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdint.h>
00028 #include <string.h>
00029 #include <byteswap.h>
00030 #include <errno.h>
00031 #include <assert.h>
00032 #include <unistd.h>
00033 #include <ipxe/bitbash.h>
00034 #include <ipxe/spi_bit.h>
00035 
00036 /** @file
00037  *
00038  * SPI bit-bashing interface
00039  *
00040  */
00041 
00042 /** Delay between SCLK changes and around SS changes */
00043 static void spi_bit_delay ( void ) {
00044         udelay ( SPI_BIT_UDELAY );
00045 }
00046 
00047 /** Chip select line will be asserted */
00048 #define SELECT_SLAVE 0
00049 
00050 /** Chip select line will be deasserted */
00051 #define DESELECT_SLAVE SPI_MODE_SSPOL
00052 
00053 /**
00054  * Select/deselect slave
00055  *
00056  * @v spibit            SPI bit-bashing interface
00057  * @v slave             Slave number
00058  * @v state             Slave select state
00059  *
00060  * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
00061  */
00062 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
00063                                        unsigned int slave,
00064                                        unsigned int state ) {
00065         struct bit_basher *basher = &spibit->basher;
00066 
00067         state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
00068         DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
00069                 spibit, slave, ( state ? "high" : "low" ) );
00070 
00071         spi_bit_delay();
00072         write_bit ( basher, SPI_BIT_SS ( slave ), state );
00073         spi_bit_delay();
00074 }
00075 
00076 /**
00077  * Transfer bits over SPI bit-bashing bus
00078  *
00079  * @v bus               SPI bus
00080  * @v data_out          TX data buffer (or NULL)
00081  * @v data_in           RX data buffer (or NULL)
00082  * @v len               Length of transfer (in @b bits)
00083  * @v endianness        Endianness of this data transfer
00084  *
00085  * This issues @c len clock cycles on the SPI bus, shifting out data
00086  * from the @c data_out buffer to the MOSI line and shifting in data
00087  * from the MISO line to the @c data_in buffer.  If @c data_out is
00088  * NULL, then the data sent will be all zeroes.  If @c data_in is
00089  * NULL, then the incoming data will be discarded.
00090  */
00091 static void spi_bit_transfer ( struct spi_bit_basher *spibit,
00092                                const void *data_out, void *data_in,
00093                                unsigned int len, int endianness ) {
00094         struct spi_bus *bus = &spibit->bus;
00095         struct bit_basher *basher = &spibit->basher;
00096         unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
00097         unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
00098         unsigned int bit_offset;
00099         unsigned int byte_offset;
00100         unsigned int byte_mask;
00101         unsigned int bit;
00102         unsigned int step;
00103 
00104         DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
00105                 spibit, len, bus->mode );
00106 
00107         for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
00108                 /* Calculate byte offset and byte mask */
00109                 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
00110                                ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
00111                 byte_offset = ( bit_offset / 8 );
00112                 byte_mask = ( 1 << ( bit_offset % 8 ) );
00113 
00114                 /* Shift data in or out */
00115                 if ( sclk == cpha ) {
00116                         const uint8_t *byte;
00117 
00118                         /* Shift data out */
00119                         if ( data_out ) {
00120                                 byte = ( data_out + byte_offset );
00121                                 bit = ( *byte & byte_mask );
00122                                 DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
00123                                         spibit, ( bit ? 1 : 0 ) );
00124                         } else {
00125                                 bit = 0;
00126                         }
00127                         write_bit ( basher, SPI_BIT_MOSI, bit );
00128                 } else {
00129                         uint8_t *byte;
00130 
00131                         /* Shift data in */
00132                         bit = read_bit ( basher, SPI_BIT_MISO );
00133                         if ( data_in ) {
00134                                 DBGCP ( spibit, "SPIBIT %p read bit %d\n",
00135                                         spibit, ( bit ? 1 : 0 ) );
00136                                 byte = ( data_in + byte_offset );
00137                                 *byte &= ~byte_mask;
00138                                 *byte |= ( bit & byte_mask );
00139                         }
00140                 }
00141 
00142                 /* Toggle clock line */
00143                 spi_bit_delay();
00144                 sclk ^= 1;
00145                 write_bit ( basher, SPI_BIT_SCLK, sclk );
00146         }
00147 }
00148 
00149 /**
00150  * Read/write data via SPI bit-bashing bus
00151  *
00152  * @v bus               SPI bus
00153  * @v device            SPI device
00154  * @v command           Command
00155  * @v address           Address to read/write (<0 for no address)
00156  * @v data_out          TX data buffer (or NULL)
00157  * @v data_in           RX data buffer (or NULL)
00158  * @v len               Length of transfer
00159  * @ret rc              Return status code
00160  */
00161 static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
00162                         unsigned int command, int address,
00163                         const void *data_out, void *data_in, size_t len ) {
00164         struct spi_bit_basher *spibit
00165                 = container_of ( bus, struct spi_bit_basher, bus );
00166         uint32_t tmp_command;
00167         uint32_t tmp_address;
00168         uint32_t tmp_address_detect;
00169 
00170         /* Open bit-bashing interface */
00171         open_bit ( &spibit->basher );
00172 
00173         /* Deassert chip select to reset specified slave */
00174         spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
00175 
00176         /* Set clock line to idle state */
00177         write_bit ( &spibit->basher, SPI_BIT_SCLK, 
00178                     ( bus->mode & SPI_MODE_CPOL ) );
00179 
00180         /* Assert chip select on specified slave */
00181         spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
00182 
00183         /* Transmit command */
00184         assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
00185         tmp_command = cpu_to_le32 ( command );
00186         spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
00187                            SPI_BIT_BIG_ENDIAN );
00188 
00189         /* Transmit address, if present */
00190         if ( address >= 0 ) {
00191                 assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
00192                 tmp_address = cpu_to_le32 ( address );
00193                 if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
00194                         /* Autodetect address length.  This relies on
00195                          * the device responding with a dummy zero
00196                          * data bit before the first real data bit.
00197                          */
00198                         DBGC ( spibit, "SPIBIT %p autodetecting device "
00199                                "address length\n", spibit );
00200                         assert ( address == 0 );
00201                         device->address_len = 0;
00202                         do {
00203                                 spi_bit_transfer ( spibit, &tmp_address,
00204                                                    &tmp_address_detect, 1,
00205                                                    SPI_BIT_BIG_ENDIAN );
00206                                 device->address_len++;
00207                         } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
00208                         DBGC ( spibit, "SPIBIT %p autodetected device address "
00209                                "length %d\n", spibit, device->address_len );
00210                 } else {
00211                         spi_bit_transfer ( spibit, &tmp_address, NULL,
00212                                            device->address_len,
00213                                            SPI_BIT_BIG_ENDIAN );
00214                 }
00215         }
00216 
00217         /* Transmit/receive data */
00218         spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
00219                            spibit->endianness );
00220 
00221         /* Deassert chip select on specified slave */
00222         spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
00223 
00224         /* Close bit-bashing interface */
00225         close_bit ( &spibit->basher );
00226 
00227         return 0;
00228 }
00229 
00230 /**
00231  * Initialise SPI bit-bashing interface
00232  *
00233  * @v spibit            SPI bit-bashing interface
00234  */
00235 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
00236         assert ( &spibit->basher.op->read != NULL );
00237         assert ( &spibit->basher.op->write != NULL );
00238         spibit->bus.rw = spi_bit_rw;
00239 }