iPXE
Defines | Functions
spi_bit.c File Reference

SPI bit-bashing interface. More...

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <ipxe/bitbash.h>
#include <ipxe/spi_bit.h>

Go to the source code of this file.

Defines

#define SELECT_SLAVE   0
 Chip select line will be asserted.
#define DESELECT_SLAVE   SPI_MODE_SSPOL
 Chip select line will be deasserted.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void spi_bit_delay (void)
 Delay between SCLK changes and around SS changes.
static void spi_bit_set_slave_select (struct spi_bit_basher *spibit, unsigned int slave, unsigned int state)
 Select/deselect slave.
static void spi_bit_transfer (struct spi_bit_basher *spibit, const void *data_out, void *data_in, unsigned int len, int endianness)
 Transfer bits over SPI bit-bashing bus.
static int spi_bit_rw (struct spi_bus *bus, struct spi_device *device, unsigned int command, int address, const void *data_out, void *data_in, size_t len)
 Read/write data via SPI bit-bashing bus.
void init_spi_bit_basher (struct spi_bit_basher *spibit)
 Initialise SPI bit-bashing interface.

Detailed Description

SPI bit-bashing interface.

Definition in file spi_bit.c.


Define Documentation

#define SELECT_SLAVE   0

Chip select line will be asserted.

Definition at line 48 of file spi_bit.c.

Referenced by spi_bit_rw().

Chip select line will be deasserted.

Definition at line 51 of file spi_bit.c.

Referenced by spi_bit_rw().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void spi_bit_delay ( void  ) [static]

Delay between SCLK changes and around SS changes.

Definition at line 43 of file spi_bit.c.

References SPI_BIT_UDELAY, and udelay().

Referenced by spi_bit_set_slave_select(), and spi_bit_transfer().

static void spi_bit_set_slave_select ( struct spi_bit_basher spibit,
unsigned int  slave,
unsigned int  state 
) [static]

Select/deselect slave.

Parameters:
spibitSPI bit-bashing interface
slaveSlave number
stateSlave select state

state must be SELECT_SLAVE or DESELECT_SLAVE.

Definition at line 62 of file spi_bit.c.

References spi_bit_basher::basher, spi_bit_basher::bus, DBGC2, spi_bus::mode, spi_bit_delay(), SPI_BIT_SS, SPI_MODE_SSPOL, and write_bit().

Referenced by spi_bit_rw().

                                                            {
        struct bit_basher *basher = &spibit->basher;

        state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
        DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
                spibit, slave, ( state ? "high" : "low" ) );

        spi_bit_delay();
        write_bit ( basher, SPI_BIT_SS ( slave ), state );
        spi_bit_delay();
}
static void spi_bit_transfer ( struct spi_bit_basher spibit,
const void *  data_out,
void *  data_in,
unsigned int  len,
int  endianness 
) [static]

Transfer bits over SPI bit-bashing bus.

Parameters:
busSPI bus
data_outTX data buffer (or NULL)
data_inRX data buffer (or NULL)
lenLength of transfer (in bits)
endiannessEndianness of this data transfer

This issues len clock cycles on the SPI bus, shifting out data from the data_out buffer to the MOSI line and shifting in data from the MISO line to the data_in buffer. If data_out is NULL, then the data sent will be all zeroes. If data_in is NULL, then the incoming data will be discarded.

Definition at line 91 of file spi_bit.c.

References spi_bit_basher::basher, spi_bit_basher::bus, bus, byte, DBGC2, DBGCP, spi_bus::mode, read_bit(), SPI_BIT_BIG_ENDIAN, spi_bit_delay(), SPI_BIT_MISO, SPI_BIT_MOSI, SPI_BIT_SCLK, SPI_MODE_CPHA, SPI_MODE_CPOL, step(), and write_bit().

Referenced by spi_bit_rw().

                                                                  {
        struct spi_bus *bus = &spibit->bus;
        struct bit_basher *basher = &spibit->basher;
        unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
        unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
        unsigned int bit_offset;
        unsigned int byte_offset;
        unsigned int byte_mask;
        unsigned int bit;
        unsigned int step;

        DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
                spibit, len, bus->mode );

        for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
                /* Calculate byte offset and byte mask */
                bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
                               ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
                byte_offset = ( bit_offset / 8 );
                byte_mask = ( 1 << ( bit_offset % 8 ) );

                /* Shift data in or out */
                if ( sclk == cpha ) {
                        const uint8_t *byte;

                        /* Shift data out */
                        if ( data_out ) {
                                byte = ( data_out + byte_offset );
                                bit = ( *byte & byte_mask );
                                DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
                                        spibit, ( bit ? 1 : 0 ) );
                        } else {
                                bit = 0;
                        }
                        write_bit ( basher, SPI_BIT_MOSI, bit );
                } else {
                        uint8_t *byte;

                        /* Shift data in */
                        bit = read_bit ( basher, SPI_BIT_MISO );
                        if ( data_in ) {
                                DBGCP ( spibit, "SPIBIT %p read bit %d\n",
                                        spibit, ( bit ? 1 : 0 ) );
                                byte = ( data_in + byte_offset );
                                *byte &= ~byte_mask;
                                *byte |= ( bit & byte_mask );
                        }
                }

                /* Toggle clock line */
                spi_bit_delay();
                sclk ^= 1;
                write_bit ( basher, SPI_BIT_SCLK, sclk );
        }
}
static int spi_bit_rw ( struct spi_bus bus,
struct spi_device device,
unsigned int  command,
int  address,
const void *  data_out,
void *  data_in,
size_t  len 
) [static]

Read/write data via SPI bit-bashing bus.

Parameters:
busSPI bus
deviceSPI device
commandCommand
addressAddress to read/write (<0 for no address)
data_outTX data buffer (or NULL)
data_inRX data buffer (or NULL)
lenLength of transfer
Return values:
rcReturn status code

Definition at line 161 of file spi_bit.c.

References spi_device::address_len, assert, spi_bit_basher::basher, close_bit(), spi_device::command_len, container_of, cpu_to_le32, DBGC, DESELECT_SLAVE, spi_bit_basher::endianness, le32_to_cpu, spi_bus::mode, NULL, open_bit(), SELECT_SLAVE, spi_device::slave, SPI_AUTODETECT_ADDRESS_LEN, SPI_BIT_BIG_ENDIAN, SPI_BIT_SCLK, spi_bit_set_slave_select(), spi_bit_transfer(), SPI_MODE_CPOL, and write_bit().

Referenced by init_spi_bit_basher().

                                                                          {
        struct spi_bit_basher *spibit
                = container_of ( bus, struct spi_bit_basher, bus );
        uint32_t tmp_command;
        uint32_t tmp_address;
        uint32_t tmp_address_detect;

        /* Open bit-bashing interface */
        open_bit ( &spibit->basher );

        /* Deassert chip select to reset specified slave */
        spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );

        /* Set clock line to idle state */
        write_bit ( &spibit->basher, SPI_BIT_SCLK, 
                    ( bus->mode & SPI_MODE_CPOL ) );

        /* Assert chip select on specified slave */
        spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );

        /* Transmit command */
        assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
        tmp_command = cpu_to_le32 ( command );
        spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
                           SPI_BIT_BIG_ENDIAN );

        /* Transmit address, if present */
        if ( address >= 0 ) {
                assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
                tmp_address = cpu_to_le32 ( address );
                if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
                        /* Autodetect address length.  This relies on
                         * the device responding with a dummy zero
                         * data bit before the first real data bit.
                         */
                        DBGC ( spibit, "SPIBIT %p autodetecting device "
                               "address length\n", spibit );
                        assert ( address == 0 );
                        device->address_len = 0;
                        do {
                                spi_bit_transfer ( spibit, &tmp_address,
                                                   &tmp_address_detect, 1,
                                                   SPI_BIT_BIG_ENDIAN );
                                device->address_len++;
                        } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
                        DBGC ( spibit, "SPIBIT %p autodetected device address "
                               "length %d\n", spibit, device->address_len );
                } else {
                        spi_bit_transfer ( spibit, &tmp_address, NULL,
                                           device->address_len,
                                           SPI_BIT_BIG_ENDIAN );
                }
        }

        /* Transmit/receive data */
        spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
                           spibit->endianness );

        /* Deassert chip select on specified slave */
        spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );

        /* Close bit-bashing interface */
        close_bit ( &spibit->basher );

        return 0;
}
void init_spi_bit_basher ( struct spi_bit_basher spibit)

Initialise SPI bit-bashing interface.

Parameters:
spibitSPI bit-bashing interface

Definition at line 235 of file spi_bit.c.

References assert, spi_bit_basher::basher, spi_bit_basher::bus, NULL, bit_basher::op, bit_basher_operations::read, spi_bus::rw, spi_bit_rw(), and bit_basher_operations::write.

Referenced by ifec_init_eeprom(), natsemi_init_eeprom(), realtek_init_eeprom(), and rtl818x_probe().

                                                           {
        assert ( &spibit->basher.op->read != NULL );
        assert ( &spibit->basher.op->write != NULL );
        spibit->bus.rw = spi_bit_rw;
}