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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stddef.h>
00022 #include <stdint.h>
00023 #include <string.h>
00024 #include <byteswap.h>
00025 #include <errno.h>
00026 #include <assert.h>
00027 #include <unistd.h>
00028 #include <gpxe/bitbash.h>
00029 #include <gpxe/spi_bit.h>
00030 
00031 /** @file
00032  *
00033  * SPI bit-bashing interface
00034  *
00035  */
00036 
00037 /** Delay between SCLK changes and around SS changes */
00038 static void spi_bit_delay ( void ) {
00039         udelay ( SPI_BIT_UDELAY );
00040 }
00041 
00042 /** Chip select line will be asserted */
00043 #define SELECT_SLAVE 0
00044 
00045 /** Chip select line will be deasserted */
00046 #define DESELECT_SLAVE SPI_MODE_SSPOL
00047 
00048 /**
00049  * Select/deselect slave
00050  *
00051  * @v spibit            SPI bit-bashing interface
00052  * @v slave             Slave number
00053  * @v state             Slave select state
00054  *
00055  * @c state must be @c SELECT_SLAVE or @c DESELECT_SLAVE.
00056  */
00057 static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
00058                                        unsigned int slave,
00059                                        unsigned int state ) {
00060         struct bit_basher *basher = &spibit->basher;
00061 
00062         state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
00063         DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
00064                 spibit, slave, ( state ? "high" : "low" ) );
00065 
00066         spi_bit_delay();
00067         write_bit ( basher, SPI_BIT_SS ( slave ), state );
00068         spi_bit_delay();
00069 }
00070 
00071 /**
00072  * Transfer bits over SPI bit-bashing bus
00073  *
00074  * @v bus               SPI bus
00075  * @v data_out          TX data buffer (or NULL)
00076  * @v data_in           RX data buffer (or NULL)
00077  * @v len               Length of transfer (in @b bits)
00078  * @v endianness        Endianness of this data transfer
00079  *
00080  * This issues @c len clock cycles on the SPI bus, shifting out data
00081  * from the @c data_out buffer to the MOSI line and shifting in data
00082  * from the MISO line to the @c data_in buffer.  If @c data_out is
00083  * NULL, then the data sent will be all zeroes.  If @c data_in is
00084  * NULL, then the incoming data will be discarded.
00085  */
00086 static void spi_bit_transfer ( struct spi_bit_basher *spibit,
00087                                const void *data_out, void *data_in,
00088                                unsigned int len, int endianness ) {
00089         struct spi_bus *bus = &spibit->bus;
00090         struct bit_basher *basher = &spibit->basher;
00091         unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
00092         unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
00093         unsigned int bit_offset;
00094         unsigned int byte_offset;
00095         unsigned int byte_mask;
00096         unsigned int bit;
00097         unsigned int step;
00098 
00099         DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
00100                 spibit, len, bus->mode );
00101 
00102         for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
00103                 /* Calculate byte offset and byte mask */
00104                 bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
00105                                ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
00106                 byte_offset = ( bit_offset / 8 );
00107                 byte_mask = ( 1 << ( bit_offset % 8 ) );
00108 
00109                 /* Shift data in or out */
00110                 if ( sclk == cpha ) {
00111                         const uint8_t *byte;
00112 
00113                         /* Shift data out */
00114                         if ( data_out ) {
00115                                 byte = ( data_out + byte_offset );
00116                                 bit = ( *byte & byte_mask );
00117                                 DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
00118                                         spibit, ( bit ? 1 : 0 ) );
00119                         } else {
00120                                 bit = 0;
00121                         }
00122                         write_bit ( basher, SPI_BIT_MOSI, bit );
00123                 } else {
00124                         uint8_t *byte;
00125 
00126                         /* Shift data in */
00127                         bit = read_bit ( basher, SPI_BIT_MISO );
00128                         if ( data_in ) {
00129                                 DBGCP ( spibit, "SPIBIT %p read bit %d\n",
00130                                         spibit, ( bit ? 1 : 0 ) );
00131                                 byte = ( data_in + byte_offset );
00132                                 *byte &= ~byte_mask;
00133                                 *byte |= ( bit & byte_mask );
00134                         }
00135                 }
00136 
00137                 /* Toggle clock line */
00138                 spi_bit_delay();
00139                 sclk ^= 1;
00140                 write_bit ( basher, SPI_BIT_SCLK, sclk );
00141         }
00142 }
00143 
00144 /**
00145  * Read/write data via SPI bit-bashing bus
00146  *
00147  * @v bus               SPI bus
00148  * @v device            SPI device
00149  * @v command           Command
00150  * @v address           Address to read/write (<0 for no address)
00151  * @v data_out          TX data buffer (or NULL)
00152  * @v data_in           RX data buffer (or NULL)
00153  * @v len               Length of transfer
00154  * @ret rc              Return status code
00155  */
00156 static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
00157                         unsigned int command, int address,
00158                         const void *data_out, void *data_in, size_t len ) {
00159         struct spi_bit_basher *spibit
00160                 = container_of ( bus, struct spi_bit_basher, bus );
00161         uint32_t tmp_command;
00162         uint32_t tmp_address;
00163         uint32_t tmp_address_detect;
00164 
00165         /* Set clock line to idle state */
00166         write_bit ( &spibit->basher, SPI_BIT_SCLK, 
00167                     ( bus->mode & SPI_MODE_CPOL ) );
00168 
00169         /* Assert chip select on specified slave */
00170         spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
00171 
00172         /* Transmit command */
00173         assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
00174         tmp_command = cpu_to_le32 ( command );
00175         spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
00176                            SPI_BIT_BIG_ENDIAN );
00177 
00178         /* Transmit address, if present */
00179         if ( address >= 0 ) {
00180                 assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
00181                 tmp_address = cpu_to_le32 ( address );
00182                 if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
00183                         /* Autodetect address length.  This relies on
00184                          * the device responding with a dummy zero
00185                          * data bit before the first real data bit.
00186                          */
00187                         DBGC ( spibit, "SPIBIT %p autodetecting device "
00188                                "address length\n", spibit );
00189                         assert ( address == 0 );
00190                         device->address_len = 0;
00191                         do {
00192                                 spi_bit_transfer ( spibit, &tmp_address,
00193                                                    &tmp_address_detect, 1,
00194                                                    SPI_BIT_BIG_ENDIAN );
00195                                 device->address_len++;
00196                         } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
00197                         DBGC ( spibit, "SPIBIT %p autodetected device address "
00198                                "length %d\n", spibit, device->address_len );
00199                 } else {
00200                         spi_bit_transfer ( spibit, &tmp_address, NULL,
00201                                            device->address_len,
00202                                            SPI_BIT_BIG_ENDIAN );
00203                 }
00204         }
00205 
00206         /* Transmit/receive data */
00207         spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
00208                            spibit->endianness );
00209 
00210         /* Deassert chip select on specified slave */
00211         spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
00212 
00213         return 0;
00214 }
00215 
00216 /**
00217  * Initialise SPI bit-bashing interface
00218  *
00219  * @v spibit            SPI bit-bashing interface
00220  */
00221 void init_spi_bit_basher ( struct spi_bit_basher *spibit ) {
00222         assert ( &spibit->basher.op->read != NULL );
00223         assert ( &spibit->basher.op->write != NULL );
00224         spibit->bus.rw = spi_bit_rw;
00225 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1