iPXE
Functions
threewire.c File Reference

Three-wire serial devices. More...

#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <ipxe/threewire.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int threewire_read (struct nvs_device *nvs, unsigned int address, void *data, size_t len)
 Read data from three-wire device. More...
 
int threewire_write (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Write data to three-wire device. More...
 
int threewire_detect_address_len (struct spi_device *device)
 Autodetect device address length. More...
 

Detailed Description

Three-wire serial devices.

Definition in file threewire.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ threewire_read()

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

Read data from three-wire device.

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

Definition at line 47 of file threewire.c.

48  {
49  struct spi_device *device = nvs_to_spi ( nvs );
50  struct spi_bus *bus = device->bus;
51  int rc;
52 
53  assert ( bus->mode == SPI_MODE_THREEWIRE );
54 
55  DBGC ( device, "3wire %p reading %zd bytes at %04x\n",
56  device, len, address );
57 
58  if ( ( rc = bus->rw ( bus, device, THREEWIRE_READ, address,
59  NULL, data, len ) ) != 0 ) {
60  DBGC ( device, "3wire %p could not read: %s\n",
61  device, strerror ( rc ) );
62  return rc;
63  }
64 
65  return 0;
66 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t address
Base address.
Definition: ena.h:24
An SPI bus.
Definition: spi.h:126
#define DBGC(...)
Definition: compiler.h:505
#define THREEWIRE_READ
Read data from memory array.
Definition: threewire.h:24
A hardware device.
Definition: device.h:73
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
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 SPI_MODE_THREEWIRE
Threewire-compatible mode.
Definition: spi.h:199
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t bus
Bus.
Definition: edd.h:14

References address, assert(), bus, data, DBGC, len, NULL, spi_device::nvs, rc, SPI_MODE_THREEWIRE, strerror(), and THREEWIRE_READ.

Referenced by threewire_detect_address_len().

◆ threewire_write()

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

Write data to three-wire device.

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

Definition at line 77 of file threewire.c.

78  {
79  struct spi_device *device = nvs_to_spi ( nvs );
80  struct spi_bus *bus = device->bus;
81  int rc;
82 
83  assert ( bus->mode == SPI_MODE_THREEWIRE );
84 
85  DBGC ( device, "3wire %p writing %zd bytes at %04x\n",
86  device, len, address );
87 
88  /* Enable device for writing */
89  if ( ( rc = bus->rw ( bus, device, THREEWIRE_EWEN,
90  THREEWIRE_EWEN_ADDRESS, NULL, NULL, 0 ) ) != 0 ){
91  DBGC ( device, "3wire %p could not enable writing: %s\n",
92  device, strerror ( rc ) );
93  return rc;
94  }
95 
96  /* Write data */
97  if ( ( rc = bus->rw ( bus, device, THREEWIRE_WRITE, address,
98  data, NULL, len ) ) != 0 ) {
99  DBGC ( device, "3wire %p could not write: %s\n",
100  device, strerror ( rc ) );
101  return rc;
102  }
103 
104  /* Our model of an SPI bus doesn't provide a mechanism for
105  * "assert CS, wait for MISO to become high, so just wait for
106  * long enough to ensure that the write has completed.
107  */
109 
110  return 0;
111 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t address
Base address.
Definition: ena.h:24
An SPI bus.
Definition: spi.h:126
#define DBGC(...)
Definition: compiler.h:505
A hardware device.
Definition: device.h:73
#define THREEWIRE_WRITE_MDELAY
Time to wait for write cycles to complete.
Definition: threewire.h:40
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define THREEWIRE_EWEN_ADDRESS
Address to be used for write enable command.
Definition: threewire.h:33
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct nvs_device nvs
NVS device.
Definition: spi.h:88
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
uint32_t len
Length.
Definition: ena.h:14
#define THREEWIRE_WRITE
Write data to memory array.
Definition: threewire.h:27
uint8_t data[48]
Additional event data.
Definition: ena.h:22
An SPI device.
Definition: spi.h:86
#define SPI_MODE_THREEWIRE
Threewire-compatible mode.
Definition: spi.h:199
#define THREEWIRE_EWEN
Write enable.
Definition: threewire.h:30
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
uint8_t bus
Bus.
Definition: edd.h:14

References address, assert(), bus, data, DBGC, len, mdelay(), NULL, spi_device::nvs, rc, SPI_MODE_THREEWIRE, strerror(), THREEWIRE_EWEN, THREEWIRE_EWEN_ADDRESS, THREEWIRE_WRITE, and THREEWIRE_WRITE_MDELAY.

◆ threewire_detect_address_len()

int threewire_detect_address_len ( struct spi_device device)

Autodetect device address length.

Parameters
deviceSPI device
Return values
rcReturn status code

Definition at line 119 of file threewire.c.

119  {
120  struct nvs_device *nvs = &device->nvs;
121  int rc;
122 
123  DBGC ( device, "3wire %p autodetecting address length\n", device );
124 
125  device->address_len = SPI_AUTODETECT_ADDRESS_LEN;
126  if ( ( rc = threewire_read ( nvs, 0, NULL,
127  ( 1 << nvs->word_len_log2 ) ) ) != 0 ) {
128  DBGC ( device, "3wire %p could not autodetect address "
129  "length: %s\n", device, strerror ( rc ) );
130  return rc;
131  }
132 
133  DBGC ( device, "3wire %p autodetected address length %d\n",
134  device, device->address_len );
135  return 0;
136 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int word_len_log2
Word length.
Definition: nvs.h:22
A non-volatile storage device.
Definition: nvs.h:15
#define DBGC(...)
Definition: compiler.h:505
int threewire_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from three-wire device.
Definition: threewire.c:47
A hardware device.
Definition: device.h:73
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define SPI_AUTODETECT_ADDRESS_LEN
SPI magic autodetection address length.
Definition: spi.h:113

References DBGC, NULL, rc, SPI_AUTODETECT_ADDRESS_LEN, strerror(), threewire_read(), and nvs_device::word_len_log2.

Referenced by ifec_init_eeprom().