iPXE
Data Structures | Macros | Functions
spi.h File Reference

SPI interface. More...

#include <ipxe/nvs.h>

Go to the source code of this file.

Data Structures

struct  spi_device
 An SPI device. More...
 
struct  spi_bus
 An SPI bus. More...
 

Macros

#define SPI_WRSR   0x01
 Write status register. More...
 
#define SPI_WRITE   0x02
 Write data to memory array. More...
 
#define SPI_READ   0x03
 Read data from memory array. More...
 
#define SPI_WRDI   0x04
 Reset write enable latch. More...
 
#define SPI_RDSR   0x05
 Read status register. More...
 
#define SPI_WREN   0x06
 Set write enable latch. More...
 
#define ATMEL_SECTOR_ERASE   0x52
 Erase one sector in memory array (Not supported on all devices) More...
 
#define ATMEL_CHIP_ERASE   0x62
 Erase all sections in memory array (Not supported on all devices) More...
 
#define ATMEL_RDID   0x15
 Read manufacturer and product ID (Not supported on all devices) More...
 
#define SPI_STATUS_WPEN   0x80
 Write-protect pin enabled. More...
 
#define SPI_STATUS_BP2   0x10
 Block protection bit 2. More...
 
#define SPI_STATUS_BP1   0x08
 Block protection bit 1. More...
 
#define SPI_STATUS_BP0   0x04
 Block protection bit 0. More...
 
#define SPI_STATUS_WEN   0x02
 State of the write enable latch. More...
 
#define SPI_STATUS_NRDY   0x01
 Device busy flag. More...
 
#define SPI_AUTODETECT_ADDRESS_LEN   0
 SPI magic autodetection address length. More...
 
#define SPI_MODE_CPHA   0x01
 Clock phase (CPHA) mode bit. More...
 
#define SPI_MODE_CPOL   0x02
 Clock polarity (CPOL) mode bit. More...
 
#define SPI_MODE_SSPOL   0x10
 Slave select polarity mode bit. More...
 
#define SPI_MODE_MICROWIRE   1
 Microwire-compatible mode. More...
 
#define SPI_MODE_MICROWIRE_PLUS   0
 Microwire/Plus-compatible mode. More...
 
#define SPI_MODE_THREEWIRE   ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
 Threewire-compatible mode. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static __attribute__ ((always_inline)) void init_at25f1024(struct spi_device *device)
 Atmel AT25F1024 serial flash. 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 interface.

Definition in file spi.h.

Macro Definition Documentation

◆ SPI_AUTODETECT_ADDRESS_LEN

#define SPI_AUTODETECT_ADDRESS_LEN   0

SPI magic autodetection address length.

Set spi_device::address_len to SPI_AUTODETECT_ADDRESS_LEN if the address length should be autodetected.

Definition at line 113 of file spi.h.

◆ SPI_MODE_CPHA

#define SPI_MODE_CPHA   0x01

Clock phase (CPHA) mode bit.

Phase 0 is sample on rising edge, shift data on falling edge.

Phase 1 is shift data on rising edge, sample data on falling edge.

Definition at line 163 of file spi.h.

◆ SPI_MODE_CPOL

#define SPI_MODE_CPOL   0x02

Clock polarity (CPOL) mode bit.

This bit reflects the idle state of the clock line (SCLK).

Definition at line 169 of file spi.h.

◆ SPI_MODE_SSPOL

#define SPI_MODE_SSPOL   0x10

Slave select polarity mode bit.

This bit reflects that active state of the slave select lines. It is not part of the normal SPI mode number (which covers only SPI_MODE_CPOL and SPI_MODE_CPHA), but is included here for convenience.

Definition at line 178 of file spi.h.

◆ SPI_MODE_MICROWIRE

#define SPI_MODE_MICROWIRE   1

Microwire-compatible mode.

This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with the original Microwire protocol.

Definition at line 185 of file spi.h.

◆ SPI_MODE_MICROWIRE_PLUS

#define SPI_MODE_MICROWIRE_PLUS   0

Microwire/Plus-compatible mode.

This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with the Microwire/Plus protocol

Definition at line 192 of file spi.h.

◆ SPI_MODE_THREEWIRE

#define SPI_MODE_THREEWIRE   ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )

Threewire-compatible mode.

This mode is compatible with Atmel's series of "three-wire" interfaces.

Definition at line 199 of file spi.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ 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
An SPI device.
Definition: spi.h:86
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
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
An SPI device.
Definition: spi.h:86
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
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.