iPXE
Macros | 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.

Macros

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

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void spi_bit_delay (void)
 Delay between SCLK changes and around SS changes. More...
 
static void spi_bit_set_slave_select (struct spi_bit_basher *spibit, unsigned int slave, unsigned int state)
 Select/deselect slave. More...
 
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. More...
 
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. More...
 
void init_spi_bit_basher (struct spi_bit_basher *spibit)
 Initialise SPI bit-bashing interface. More...
 

Detailed Description

SPI bit-bashing interface.

Definition in file spi_bit.c.

Macro Definition Documentation

◆ SELECT_SLAVE

#define SELECT_SLAVE   0

Chip select line will be asserted.

Definition at line 48 of file spi_bit.c.

◆ DESELECT_SLAVE

#define DESELECT_SLAVE   SPI_MODE_SSPOL

Chip select line will be deasserted.

Definition at line 51 of file spi_bit.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ spi_bit_delay()

static void spi_bit_delay ( void  )
static

Delay between SCLK changes and around SS changes.

Definition at line 43 of file spi_bit.c.

43  {
45 }
#define SPI_BIT_UDELAY
Delay between SCLK transitions.
Definition: spi_bit.h:53
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60

References SPI_BIT_UDELAY, and udelay().

Referenced by spi_bit_set_slave_select(), and spi_bit_transfer().

◆ spi_bit_set_slave_select()

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.

64  {
65  struct bit_basher *basher = &spibit->basher;
66 
67  state ^= ( spibit->bus.mode & SPI_MODE_SSPOL );
68  DBGC2 ( spibit, "SPIBIT %p setting slave %d select %s\n",
69  spibit, slave, ( state ? "high" : "low" ) );
70 
71  spi_bit_delay();
72  write_bit ( basher, SPI_BIT_SS ( slave ), state );
73  spi_bit_delay();
74 }
uint8_t slave
Slave.
Definition: edd.h:30
struct bit_basher basher
Bit-bashing interface.
Definition: spi_bit.h:20
uint8_t state
State.
Definition: eth_slow.h:47
unsigned int mode
SPI interface mode.
Definition: spi.h:136
A bit-bashing interface.
Definition: bitbash.h:55
#define SPI_BIT_SS(slave)
Determine bit index for a particular slave.
Definition: spi_bit.h:50
struct spi_bus bus
SPI bus.
Definition: spi_bit.h:18
#define SPI_MODE_SSPOL
Slave select polarity mode bit.
Definition: spi.h:178
#define DBGC2(...)
Definition: compiler.h:522
static void spi_bit_delay(void)
Delay between SCLK changes and around SS changes.
Definition: spi_bit.c:43
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.c:44

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

Referenced by spi_bit_rw().

◆ spi_bit_transfer()

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.

93  {
94  struct spi_bus *bus = &spibit->bus;
95  struct bit_basher *basher = &spibit->basher;
96  unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
97  unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
98  unsigned int bit_offset;
99  unsigned int byte_offset;
100  unsigned int byte_mask;
101  unsigned int bit;
102  unsigned int step;
103 
104  DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
105  spibit, len, bus->mode );
106 
107  for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
108  /* Calculate byte offset and byte mask */
109  bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
110  ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
111  byte_offset = ( bit_offset / 8 );
112  byte_mask = ( 1 << ( bit_offset % 8 ) );
113 
114  /* Shift data in or out */
115  if ( sclk == cpha ) {
116  const uint8_t *byte;
117 
118  /* Shift data out */
119  if ( data_out ) {
120  byte = ( data_out + byte_offset );
121  bit = ( *byte & byte_mask );
122  DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
123  spibit, ( bit ? 1 : 0 ) );
124  } else {
125  bit = 0;
126  }
127  write_bit ( basher, SPI_BIT_MOSI, bit );
128  } else {
129  uint8_t *byte;
130 
131  /* Shift data in */
132  bit = read_bit ( basher, SPI_BIT_MISO );
133  if ( data_in ) {
134  DBGCP ( spibit, "SPIBIT %p read bit %d\n",
135  spibit, ( bit ? 1 : 0 ) );
136  byte = ( data_in + byte_offset );
137  *byte &= ~byte_mask;
138  *byte |= ( bit & byte_mask );
139  }
140  }
141 
142  /* Toggle clock line */
143  spi_bit_delay();
144  sclk ^= 1;
145  write_bit ( basher, SPI_BIT_SCLK, sclk );
146  }
147 }
struct bit_basher basher
Bit-bashing interface.
Definition: spi_bit.h:20
static unsigned int unsigned int bit
Definition: bigint.h:208
An SPI bus.
Definition: spi.h:126
#define SPI_MODE_CPHA
Clock phase (CPHA) mode bit.
Definition: spi.h:163
#define SPI_MODE_CPOL
Clock polarity (CPOL) mode bit.
Definition: spi.h:169
Master Out Slave In.
Definition: spi_bit.h:37
A bit-bashing interface.
Definition: bitbash.h:55
unsigned char uint8_t
Definition: stdint.h:10
Master In Slave Out.
Definition: spi_bit.h:39
unsigned char byte
Definition: smc9000.h:38
struct spi_bus bus
SPI bus.
Definition: spi_bit.h:18
int read_bit(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.c:60
#define SPI_BIT_BIG_ENDIAN
SPI bit basher treats data as big-endian.
Definition: spi_bit.h:56
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
void step(void)
Single-step a single process.
Definition: process.c:98
Serial clock.
Definition: spi_bit.h:35
#define DBGCP(...)
Definition: compiler.h:539
static void spi_bit_delay(void)
Delay between SCLK changes and around SS changes.
Definition: spi_bit.c:43
uint8_t bus
Bus.
Definition: edd.h:14
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.c:44

References spi_bit_basher::basher, bit, bus, spi_bit_basher::bus, DBGC2, DBGCP, len, 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().

◆ spi_bit_rw()

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.

163  {
164  struct spi_bit_basher *spibit
165  = container_of ( bus, struct spi_bit_basher, bus );
166  uint32_t tmp_command;
167  uint32_t tmp_address;
168  uint32_t tmp_address_detect;
169 
170  /* Open bit-bashing interface */
171  open_bit ( &spibit->basher );
172 
173  /* Deassert chip select to reset specified slave */
174  spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
175 
176  /* Set clock line to idle state */
177  write_bit ( &spibit->basher, SPI_BIT_SCLK,
178  ( bus->mode & SPI_MODE_CPOL ) );
179 
180  /* Assert chip select on specified slave */
181  spi_bit_set_slave_select ( spibit, device->slave, SELECT_SLAVE );
182 
183  /* Transmit command */
184  assert ( device->command_len <= ( 8 * sizeof ( tmp_command ) ) );
185  tmp_command = cpu_to_le32 ( command );
186  spi_bit_transfer ( spibit, &tmp_command, NULL, device->command_len,
188 
189  /* Transmit address, if present */
190  if ( address >= 0 ) {
191  assert ( device->address_len <= ( 8 * sizeof ( tmp_address )));
192  tmp_address = cpu_to_le32 ( address );
193  if ( device->address_len == SPI_AUTODETECT_ADDRESS_LEN ) {
194  /* Autodetect address length. This relies on
195  * the device responding with a dummy zero
196  * data bit before the first real data bit.
197  */
198  DBGC ( spibit, "SPIBIT %p autodetecting device "
199  "address length\n", spibit );
200  assert ( address == 0 );
201  device->address_len = 0;
202  do {
203  spi_bit_transfer ( spibit, &tmp_address,
204  &tmp_address_detect, 1,
206  device->address_len++;
207  } while ( le32_to_cpu ( tmp_address_detect ) & 1 );
208  DBGC ( spibit, "SPIBIT %p autodetected device address "
209  "length %d\n", spibit, device->address_len );
210  } else {
211  spi_bit_transfer ( spibit, &tmp_address, NULL,
212  device->address_len,
214  }
215  }
216 
217  /* Transmit/receive data */
218  spi_bit_transfer ( spibit, data_out, data_in, ( len * 8 ),
219  spibit->endianness );
220 
221  /* Deassert chip select on specified slave */
222  spi_bit_set_slave_select ( spibit, device->slave, DESELECT_SLAVE );
223 
224  /* Close bit-bashing interface */
225  close_bit ( &spibit->basher );
226 
227  return 0;
228 }
struct bit_basher basher
Bit-bashing interface.
Definition: spi_bit.h:20
static void spi_bit_set_slave_select(struct spi_bit_basher *spibit, unsigned int slave, unsigned int state)
Select/deselect slave.
Definition: spi_bit.c:62
static void open_bit(struct bit_basher *basher)
Open bit-bashing interface.
Definition: bitbash.h:65
#define le32_to_cpu(value)
Definition: byteswap.h:113
A command-line command.
Definition: command.h:9
uint64_t address
Base address.
Definition: ena.h:24
#define DBGC(...)
Definition: compiler.h:505
#define SPI_MODE_CPOL
Clock polarity (CPOL) mode bit.
Definition: spi.h:169
A hardware device.
Definition: device.h:73
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A bit-bashing SPI bus.
Definition: spi_bit.h:16
#define cpu_to_le32(value)
Definition: byteswap.h:107
#define DESELECT_SLAVE
Chip select line will be deasserted.
Definition: spi_bit.c:51
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.
Definition: spi_bit.c:91
unsigned int uint32_t
Definition: stdint.h:12
#define SPI_BIT_BIG_ENDIAN
SPI bit basher treats data as big-endian.
Definition: spi_bit.h:56
uint32_t len
Length.
Definition: ena.h:14
Serial clock.
Definition: spi_bit.h:35
#define SELECT_SLAVE
Chip select line will be asserted.
Definition: spi_bit.c:48
static void close_bit(struct bit_basher *basher)
Close bit-bashing interface.
Definition: bitbash.h:75
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t bus
Bus.
Definition: edd.h:14
#define SPI_AUTODETECT_ADDRESS_LEN
SPI magic autodetection address length.
Definition: spi.h:113
void write_bit(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.c:44
int endianness
Endianness of data.
Definition: spi_bit.h:29

References address, assert(), spi_bit_basher::basher, bus, close_bit(), container_of, cpu_to_le32, DBGC, DESELECT_SLAVE, spi_bit_basher::endianness, le32_to_cpu, len, NULL, open_bit(), SELECT_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().

◆ init_spi_bit_basher()

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.

235  {
236  assert ( &spibit->basher.op->read != NULL );
237  assert ( &spibit->basher.op->write != NULL );
238  spibit->bus.rw = spi_bit_rw;
239 }
struct bit_basher basher
Bit-bashing interface.
Definition: spi_bit.h:20
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.
Definition: spi_bit.c:161
int(* 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 bus.
Definition: spi.h:152
struct bit_basher_operations * op
Bit-bashing operations.
Definition: bitbash.h:57
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int(* read)(struct bit_basher *basher, unsigned int bit_id)
Read input bit.
Definition: bitbash.h:51
struct spi_bus bus
SPI bus.
Definition: spi_bit.h:18
void(* write)(struct bit_basher *basher, unsigned int bit_id, unsigned long data)
Set/clear output bit.
Definition: bitbash.h:41
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

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().