#include <stddef.h>
#include <errno.h>
#include <unistd.h>
#include <gpxe/spi.h>
Go to the source code of this file.
Functions | |
| FILE_LICENCE (GPL2_OR_LATER) | |
| static unsigned int | spi_command (unsigned int command, unsigned int address, int munge_address) |
| Munge SPI device address into command. | |
| static int | spi_wait (struct spi_device *device) |
| Wait for SPI device to complete operation. | |
| int | spi_read (struct nvs_device *nvs, unsigned int address, void *data, size_t len) |
| Read data from SPI device. | |
| int | spi_write (struct nvs_device *nvs, unsigned int address, const void *data, size_t len) |
| Write data to SPI device. | |
Definition in file spi.c.
| FILE_LICENCE | ( | GPL2_OR_LATER | ) |
| static unsigned int spi_command | ( | unsigned int | command, | |
| unsigned int | address, | |||
| int | munge_address | |||
| ) | [inline, static] |
Munge SPI device address into command.
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 45 of file spi.c.
Referenced by spi_read(), and spi_write().
00047 { 00048 return ( command | ( ( ( address >> 8 ) & munge_address ) << 3 ) ); 00049 }
| static int spi_wait | ( | struct spi_device * | device | ) | [static] |
Wait for SPI device to complete operation.
| rc | Return status code |
Definition at line 57 of file spi.c.
References spi_device::bus, DBG, ETIMEDOUT, NULL, spi_bus::rw, SPI_RDSR, SPI_STATUS_NRDY, and udelay().
Referenced by spi_write().
00057 { 00058 struct spi_bus *bus = device->bus; 00059 uint8_t status; 00060 int i; 00061 int rc; 00062 00063 for ( i = 0 ; i < 50 ; i++ ) { 00064 udelay ( 20 ); 00065 if ( ( rc = bus->rw ( bus, device, SPI_RDSR, -1, NULL, 00066 &status, sizeof ( status ) ) ) != 0 ) 00067 return rc; 00068 if ( ! ( status & SPI_STATUS_NRDY ) ) 00069 return 0; 00070 } 00071 DBG ( "SPI %p timed out\n", device ); 00072 return -ETIMEDOUT; 00073 }
| int spi_read | ( | struct nvs_device * | nvs, | |
| unsigned int | address, | |||
| void * | data, | |||
| size_t | len | |||
| ) |
Read data from SPI device.
| nvs | NVS device | |
| address | Address from which to read | |
| data | Data buffer | |
| len | Length of data buffer |
| rc | Return status code |
Definition at line 84 of file spi.c.
References spi_device::bus, DBG, spi_device::munge_address, NULL, spi_bus::rw, spi_command(), and SPI_READ.
00085 { 00086 struct spi_device *device = nvs_to_spi ( nvs ); 00087 struct spi_bus *bus = device->bus; 00088 unsigned int command = spi_command ( SPI_READ, address, 00089 device->munge_address ); 00090 int rc; 00091 00092 DBG ( "SPI %p reading %zd bytes from %#04x\n", device, len, address ); 00093 if ( ( rc = bus->rw ( bus, device, command, address, 00094 NULL, data, len ) ) != 0 ) { 00095 DBG ( "SPI %p failed to read data from device\n", device ); 00096 return rc; 00097 } 00098 00099 return 0; 00100 }
| int spi_write | ( | struct nvs_device * | nvs, | |
| unsigned int | address, | |||
| const void * | data, | |||
| size_t | len | |||
| ) |
Write data to SPI device.
| nvs | NVS device | |
| address | Address from which to read | |
| data | Data buffer | |
| len | Length of data buffer |
| rc | Return status code |
Definition at line 111 of file spi.c.
References spi_device::bus, DBG, spi_device::munge_address, NULL, spi_bus::rw, spi_command(), spi_wait(), SPI_WREN, and SPI_WRITE.
00112 { 00113 struct spi_device *device = nvs_to_spi ( nvs ); 00114 struct spi_bus *bus = device->bus; 00115 unsigned int command = spi_command ( SPI_WRITE, address, 00116 device->munge_address ); 00117 int rc; 00118 00119 DBG ( "SPI %p writing %zd bytes to %#04x\n", device, len, address ); 00120 00121 if ( ( rc = bus->rw ( bus, device, SPI_WREN, -1, 00122 NULL, NULL, 0 ) ) != 0 ) { 00123 DBG ( "SPI %p failed to write-enable device\n", device ); 00124 return rc; 00125 } 00126 00127 if ( ( rc = bus->rw ( bus, device, command, address, 00128 data, NULL, len ) ) != 0 ) { 00129 DBG ( "SPI %p failed to write data to device\n", device ); 00130 return rc; 00131 } 00132 00133 if ( ( rc = spi_wait ( device ) ) != 0 ) { 00134 DBG ( "SPI %p failed to complete write operation\n", device ); 00135 return rc; 00136 } 00137 00138 return 0; 00139 }
1.5.7.1