Support cards such as natsemi which treat the data as little-endian
(i.e. LSB transmitted first on the wire), even though SPI commands and addresses always have to be big-endian.
This commit is contained in:
parent
3bf5eb49d0
commit
95cb7aaacf
|
@ -73,6 +73,7 @@ static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
|
||||||
* @v data_out TX data buffer (or NULL)
|
* @v data_out TX data buffer (or NULL)
|
||||||
* @v data_in RX data buffer (or NULL)
|
* @v data_in RX data buffer (or NULL)
|
||||||
* @v len Length of transfer (in @b bits)
|
* @v len Length of transfer (in @b bits)
|
||||||
|
* @v endianness Endianness of this data transfer
|
||||||
*
|
*
|
||||||
* This issues @c len clock cycles on the SPI bus, shifting out data
|
* This issues @c len clock cycles on the SPI bus, shifting out data
|
||||||
* from the @c data_out buffer to the MOSI line and shifting in data
|
* from the @c data_out buffer to the MOSI line and shifting in data
|
||||||
|
@ -82,22 +83,25 @@ static void spi_bit_set_slave_select ( struct spi_bit_basher *spibit,
|
||||||
*/
|
*/
|
||||||
static void spi_bit_transfer ( struct spi_bit_basher *spibit,
|
static void spi_bit_transfer ( struct spi_bit_basher *spibit,
|
||||||
const void *data_out, void *data_in,
|
const void *data_out, void *data_in,
|
||||||
unsigned int len ) {
|
unsigned int len, int endianness ) {
|
||||||
struct spi_bus *bus = &spibit->bus;
|
struct spi_bus *bus = &spibit->bus;
|
||||||
struct bit_basher *basher = &spibit->basher;
|
struct bit_basher *basher = &spibit->basher;
|
||||||
unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
|
unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
|
||||||
unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
|
unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
|
||||||
unsigned int offset;
|
unsigned int bit_offset;
|
||||||
unsigned int mask;
|
unsigned int byte_offset;
|
||||||
|
unsigned int byte_mask;
|
||||||
unsigned int bit;
|
unsigned int bit;
|
||||||
int step;
|
unsigned int step;
|
||||||
|
|
||||||
DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
|
DBG ( "Transferring %d bits in mode %x\n", len, bus->mode );
|
||||||
|
|
||||||
for ( step = ( ( len * 2 ) - 1 ) ; step >= 0 ; step-- ) {
|
for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
|
||||||
/* Calculate byte offset within data and bit mask */
|
/* Calculate byte offset and byte mask */
|
||||||
offset = ( step / 16 );
|
bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
|
||||||
mask = ( 1 << ( ( step % 16 ) / 2 ) );
|
( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
|
||||||
|
byte_offset = ( bit_offset / 8 );
|
||||||
|
byte_mask = ( 1 << ( bit_offset % 8 ) );
|
||||||
|
|
||||||
/* Shift data in or out */
|
/* Shift data in or out */
|
||||||
if ( sclk == cpha ) {
|
if ( sclk == cpha ) {
|
||||||
|
@ -105,8 +109,8 @@ static void spi_bit_transfer ( struct spi_bit_basher *spibit,
|
||||||
|
|
||||||
/* Shift data out */
|
/* Shift data out */
|
||||||
if ( data_out ) {
|
if ( data_out ) {
|
||||||
byte = ( data_out + offset );
|
byte = ( data_out + byte_offset );
|
||||||
bit = ( *byte & mask );
|
bit = ( *byte & byte_mask );
|
||||||
} else {
|
} else {
|
||||||
bit = 0;
|
bit = 0;
|
||||||
}
|
}
|
||||||
|
@ -117,9 +121,9 @@ static void spi_bit_transfer ( struct spi_bit_basher *spibit,
|
||||||
/* Shift data in */
|
/* Shift data in */
|
||||||
bit = read_bit ( basher, SPI_BIT_MISO );
|
bit = read_bit ( basher, SPI_BIT_MISO );
|
||||||
if ( data_in ) {
|
if ( data_in ) {
|
||||||
byte = ( data_in + offset );
|
byte = ( data_in + byte_offset );
|
||||||
*byte &= ~mask;
|
*byte &= ~byte_mask;
|
||||||
*byte |= ( bit & mask );
|
*byte |= ( bit & byte_mask );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,17 +159,20 @@ static int spi_bit_rw ( struct spi_bus *bus, struct spi_device *device,
|
||||||
/* Transmit command */
|
/* Transmit command */
|
||||||
assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
|
assert ( device->command_len <= ( 8 * sizeof ( tmp ) ) );
|
||||||
tmp = cpu_to_le32 ( command );
|
tmp = cpu_to_le32 ( command );
|
||||||
spi_bit_transfer ( spibit, &tmp, NULL, device->command_len );
|
spi_bit_transfer ( spibit, &tmp, NULL, device->command_len,
|
||||||
|
SPI_BIT_BIG_ENDIAN );
|
||||||
|
|
||||||
/* Transmit address, if present */
|
/* Transmit address, if present */
|
||||||
if ( address >= 0 ) {
|
if ( address >= 0 ) {
|
||||||
assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
|
assert ( device->address_len <= ( 8 * sizeof ( tmp ) ) );
|
||||||
tmp = cpu_to_le32 ( address );
|
tmp = cpu_to_le32 ( address );
|
||||||
spi_bit_transfer ( spibit, &tmp, NULL, device->address_len );
|
spi_bit_transfer ( spibit, &tmp, NULL, device->address_len,
|
||||||
|
SPI_BIT_BIG_ENDIAN );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Transmit/receive data */
|
/* Transmit/receive data */
|
||||||
spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ) );
|
spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
|
||||||
|
spibit->endianness );
|
||||||
|
|
||||||
/* Deassert chip select on specified slave */
|
/* Deassert chip select on specified slave */
|
||||||
spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
|
spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
|
||||||
|
|
|
@ -16,6 +16,15 @@ struct spi_bit_basher {
|
||||||
struct spi_bus bus;
|
struct spi_bus bus;
|
||||||
/** Bit-bashing interface */
|
/** Bit-bashing interface */
|
||||||
struct bit_basher basher;
|
struct bit_basher basher;
|
||||||
|
/** Endianness of data
|
||||||
|
*
|
||||||
|
* SPI commands and addresses are always big-endian (i.e. MSB
|
||||||
|
* transmitted first on the wire), but some cards
|
||||||
|
* (e.g. natsemi) choose to regard the data stored in the
|
||||||
|
* EEPROM as little-endian (i.e. LSB transmitted first on the
|
||||||
|
* wire).
|
||||||
|
*/
|
||||||
|
int endianness;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Bit indices used for SPI bit-bashing interface */
|
/** Bit indices used for SPI bit-bashing interface */
|
||||||
|
@ -41,6 +50,12 @@ enum {
|
||||||
/** Delay between SCLK transitions */
|
/** Delay between SCLK transitions */
|
||||||
#define SPI_BIT_UDELAY 1
|
#define SPI_BIT_UDELAY 1
|
||||||
|
|
||||||
|
/** SPI bit basher treats data as big-endian */
|
||||||
|
#define SPI_BIT_BIG_ENDIAN 0
|
||||||
|
|
||||||
|
/** SPI bit basher treats data as little-endian */
|
||||||
|
#define SPI_BIT_LITTLE_ENDIAN 1
|
||||||
|
|
||||||
extern void init_spi_bit_basher ( struct spi_bit_basher *spibit );
|
extern void init_spi_bit_basher ( struct spi_bit_basher *spibit );
|
||||||
|
|
||||||
#endif /* _GPXE_SPI_BIT_H */
|
#endif /* _GPXE_SPI_BIT_H */
|
||||||
|
|
Reference in New Issue