iPXE
Functions
spi.c File Reference

SPI devices. More...

#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <ipxe/spi.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static unsigned int spi_command (unsigned int command, unsigned int address, int munge_address)
 Munge SPI device address into command. More...
 
static int spi_wait (struct spi_device *device)
 Wait for SPI device to complete operation. More...
 
int spi_read (struct nvs_device *nvs, unsigned int address, void *data, size_t len)
 Read data from SPI device. More...
 
int spi_write (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Write data to SPI device. More...
 

Detailed Description

SPI devices.

Definition in file spi.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ spi_command()

static unsigned int spi_command ( unsigned int  command,
unsigned int  address,
int  munge_address 
)
inlinestatic

Munge SPI device address into command.

Parameters
commandSPI command
addressAddress
munge_addressDevice requires address munging
Return values
commandActual SPI command to use

Some devices with 9-bit addresses (e.g. AT25040A EEPROM) use bit 3 of the command byte as address bit A8, rather than having a two-byte address. This function takes care of generating the appropriate command.

Definition at line 50 of file spi.c.

52  {
53  return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) );
54 }
A command-line command.
Definition: command.h:9
uint64_t address
Base address.
Definition: ena.h:24

References address.

Referenced by spi_read(), and spi_write().

◆ spi_wait()

static int spi_wait ( struct spi_device device)
static

Wait for SPI device to complete operation.

Parameters
deviceSPI device
Return values
rcReturn status code

Definition at line 62 of file spi.c.

62  {
63  struct spi_bus *bus = device->bus;
65  int i;
66  int rc;
67 
68  for ( i = 0 ; i < 50 ; i++ ) {
69  udelay ( 20 );
70  if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL,
71  &status, sizeof ( status ) ) ) != 0 )
72  return rc;
73  if ( ! ( status & SPI_STATUS_NRDY ) )
74  return 0;
75  }
76  DBG ( "SPI %p timed out\n", device );
77  return -ETIMEDOUT;
78 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define SPI_RDSR
Read status register.
Definition: spi.h:32
An SPI bus.
Definition: spi.h:126
uint8_t status
Status.
Definition: ena.h:16
A hardware device.
Definition: device.h:73
void udelay(unsigned long usecs)
Delay for a fixed number of microseconds.
Definition: timer.c:60
#define SPI_STATUS_NRDY
Device busy flag.
Definition: spi.h:76
unsigned char uint8_t
Definition: stdint.h:10
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
uint8_t bus
Bus.
Definition: edd.h:14

References bus, DBG, ETIMEDOUT, NULL, rc, SPI_RDSR, SPI_STATUS_NRDY, status, and udelay().

Referenced by spi_write().

◆ spi_read()

int spi_read ( struct nvs_device nvs,
unsigned int  address,
void *  data,
size_t  len 
)

Read data from SPI device.

Parameters
nvsNVS device
addressAddress from which to read
dataData buffer
lenLength of data buffer
Return values
rcReturn status code

Definition at line 89 of file spi.c.

90  {
91  struct spi_device *device = nvs_to_spi ( nvs );
92  struct spi_bus *bus = device->bus;
93  unsigned int command = spi_command ( SPI_READ, address,
94  device->munge_address );
95  int rc;
96 
97  DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address );
98  if ( ( rc = bus->rw ( bus, device, command, address,
99  NULL, data, len ) ) != 0 ) {
100  DBG ( "SPI %p failed to read data from device\n", device );
101  return rc;
102  }
103 
104  return 0;
105 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A command-line command.
Definition: command.h:9
uint64_t address
Base address.
Definition: ena.h:24
An SPI bus.
Definition: spi.h:126
A hardware device.
Definition: device.h:73
#define SPI_READ
Read data from memory array.
Definition: spi.h:26
static unsigned int spi_command(unsigned int command, unsigned int address, int munge_address)
Munge SPI device address into command.
Definition: spi.c:50
struct nvs_device nvs
NVS device.
Definition: spi.h:88
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
An SPI device.
Definition: spi.h:86
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t bus
Bus.
Definition: edd.h:14

References address, bus, data, DBG, len, NULL, spi_device::nvs, rc, spi_command(), and SPI_READ.

◆ spi_write()

int spi_write ( struct nvs_device nvs,
unsigned int  address,
const void *  data,
size_t  len 
)

Write data to SPI device.

Parameters
nvsNVS device
addressAddress from which to read
dataData buffer
lenLength of data buffer
Return values
rcReturn status code

Definition at line 116 of file spi.c.

117  {
118  struct spi_device *device = nvs_to_spi ( nvs );
119  struct spi_bus *bus = device->bus;
120  unsigned int command = spi_command ( SPI_WRITE, address,
121  device->munge_address );
122  int rc;
123 
124  DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address );
125 
126  if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1,
127  NULL, NULL, 0 ) ) != 0 ) {
128  DBG ( "SPI %p failed to write-enable device\n", device );
129  return rc;
130  }
131 
132  if ( ( rc = bus->rw ( bus, device, command, address,
133  data, NULL, len ) ) != 0 ) {
134  DBG ( "SPI %p failed to write data to device\n", device );
135  return rc;
136  }
137 
138  if ( ( rc = spi_wait ( device ) ) != 0 ) {
139  DBG ( "SPI %p failed to complete write operation\n", device );
140  return rc;
141  }
142 
143  return 0;
144 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define SPI_WREN
Set write enable latch.
Definition: spi.h:35
A command-line command.
Definition: command.h:9
uint64_t address
Base address.
Definition: ena.h:24
An SPI bus.
Definition: spi.h:126
A hardware device.
Definition: device.h:73
#define SPI_WRITE
Write data to memory array.
Definition: spi.h:23
static int spi_wait(struct spi_device *device)
Wait for SPI device to complete operation.
Definition: spi.c:62
static unsigned int spi_command(unsigned int command, unsigned int address, int munge_address)
Munge SPI device address into command.
Definition: spi.c:50
struct nvs_device nvs
NVS device.
Definition: spi.h:88
uint32_t len
Length.
Definition: ena.h:14
uint8_t data[48]
Additional event data.
Definition: ena.h:22
An SPI device.
Definition: spi.h:86
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t bus
Bus.
Definition: edd.h:14

References address, bus, data, DBG, len, NULL, spi_device::nvs, rc, spi_command(), spi_wait(), SPI_WREN, and SPI_WRITE.