iPXE
spi.h
Go to the documentation of this file.
1#ifndef _IPXE_SPI_H
2#define _IPXE_SPI_H
3
4/** @file
5 *
6 * SPI interface
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <ipxe/nvs.h>
14
15/**
16 * @defgroup spicmds SPI commands
17 * @{
18 */
19
20/** Write status register */
21#define SPI_WRSR 0x01
22
23/** Write data to memory array */
24#define SPI_WRITE 0x02
25
26/** Read data from memory array */
27#define SPI_READ 0x03
28
29/** Reset write enable latch */
30#define SPI_WRDI 0x04
31
32/** Read status register */
33#define SPI_RDSR 0x05
34
35/** Set write enable latch */
36#define SPI_WREN 0x06
37
38/**
39 * @defgroup atmelcmds Atmel-specific SPI commands
40 * @{
41 */
42
43/** Erase one sector in memory array (Not supported on all devices) */
44#define ATMEL_SECTOR_ERASE 0x52
45
46/** Erase all sections in memory array (Not supported on all devices) */
47#define ATMEL_CHIP_ERASE 0x62
48
49/** Read manufacturer and product ID (Not supported on all devices) */
50#define ATMEL_RDID 0x15
51
52/** @} */
53
54/** @} */
55
56/**
57 * @defgroup spistatus SPI status register bits (not present on all devices)
58 * @{
59 */
60
61/** Write-protect pin enabled */
62#define SPI_STATUS_WPEN 0x80
63
64/** Block protection bit 2 */
65#define SPI_STATUS_BP2 0x10
66
67/** Block protection bit 1 */
68#define SPI_STATUS_BP1 0x08
69
70/** Block protection bit 0 */
71#define SPI_STATUS_BP0 0x04
72
73/** State of the write enable latch */
74#define SPI_STATUS_WEN 0x02
75
76/** Device busy flag */
77#define SPI_STATUS_NRDY 0x01
78
79/** @} */
80
81/**
82 * An SPI device
83 *
84 * This data structure represents a physical SPI device attached to an
85 * SPI bus.
86 */
87struct spi_device {
88 /** NVS device */
90 /** SPI bus to which device is attached */
91 struct spi_bus *bus;
92 /** Slave number */
93 unsigned int slave;
94 /** Command length, in bits */
95 unsigned int command_len;
96 /** Address length, in bits */
97 unsigned int address_len;
98 /** Address is munged
99 *
100 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM)
101 * use bit 3 of the command byte as address bit A8, rather
102 * than having a two-byte address. If this flag is set, then
103 * commands should be munged in this way.
104 */
105 unsigned int munge_address : 1;
106};
107
108/**
109 * SPI magic autodetection address length
110 *
111 * Set @c spi_device::address_len to @c SPI_AUTODETECT_ADDRESS_LEN if
112 * the address length should be autodetected.
113 */
114#define SPI_AUTODETECT_ADDRESS_LEN 0
115
116static inline __attribute__ (( always_inline )) struct spi_device *
117nvs_to_spi ( struct nvs_device *nvs ) {
118 return container_of ( nvs, struct spi_device, nvs );
119}
120
121/**
122 * An SPI bus
123 *
124 * This data structure represents an SPI bus controller capable of
125 * issuing commands to attached SPI devices.
126 */
127struct spi_bus {
128 /** SPI interface mode
129 *
130 * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
131 * and @c SPI_MODE_CPOL. It is also the number conventionally
132 * used to describe the SPI interface mode. For example, SPI
133 * mode 1 is the mode in which CPOL=0 and CPHA=1, which
134 * therefore corresponds to a mode value of (0|SPI_MODE_CPHA)
135 * which, happily, equals 1.
136 */
137 unsigned int mode;
138 /**
139 * Read/write data via SPI bus
140 *
141 * @v bus SPI bus
142 * @v device SPI device
143 * @v command Command
144 * @v address Address to read/write (<0 for no address)
145 * @v data_out TX data buffer (or NULL)
146 * @v data_in RX data buffer (or NULL)
147 * @v len Length of data buffer(s)
148 *
149 * This issues the specified command and optional address to
150 * the SPI device, then reads and/or writes data to/from the
151 * data buffers.
152 */
153 int ( * rw ) ( struct spi_bus *bus, struct spi_device *device,
154 unsigned int command, int address,
155 const void *data_out, void *data_in, size_t len );
156};
157
158/** Clock phase (CPHA) mode bit
159 *
160 * Phase 0 is sample on rising edge, shift data on falling edge.
161 *
162 * Phase 1 is shift data on rising edge, sample data on falling edge.
163 */
164#define SPI_MODE_CPHA 0x01
165
166/** Clock polarity (CPOL) mode bit
167 *
168 * This bit reflects the idle state of the clock line (SCLK).
169 */
170#define SPI_MODE_CPOL 0x02
171
172/** Slave select polarity mode bit
173 *
174 * This bit reflects that active state of the slave select lines. It
175 * is not part of the normal SPI mode number (which covers only @c
176 * SPI_MODE_CPOL and @c SPI_MODE_CPHA), but is included here for
177 * convenience.
178 */
179#define SPI_MODE_SSPOL 0x10
180
181/** Microwire-compatible mode
182 *
183 * This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with
184 * the original Microwire protocol.
185 */
186#define SPI_MODE_MICROWIRE 1
187
188/** Microwire/Plus-compatible mode
189 *
190 * This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with
191 * the Microwire/Plus protocol
192 */
193#define SPI_MODE_MICROWIRE_PLUS 0
194
195/** Threewire-compatible mode
196 *
197 * This mode is compatible with Atmel's series of "three-wire"
198 * interfaces.
199 */
200#define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
201
202extern int spi_read ( struct nvs_device *nvs, unsigned int address,
203 void *data, size_t len );
204extern int spi_write ( struct nvs_device *nvs, unsigned int address,
205 const void *data, size_t len );
206
207/**
208 * @defgroup spidevs SPI device types
209 * @{
210 */
211
212static inline __attribute__ (( always_inline )) void
213init_spi ( struct spi_device *device ) {
214 device->nvs.word_len_log2 = 0;
215 device->command_len = 8,
216 device->nvs.read = spi_read;
217 device->nvs.write = spi_write;
218}
219
220/** Atmel AT25F1024 serial flash */
221static inline __attribute__ (( always_inline )) void
222init_at25f1024 ( struct spi_device *device ) {
223 device->address_len = 24;
224 device->nvs.size = ( 128 * 1024 );
225 device->nvs.block_size = 256;
226 init_spi ( device );
227}
228
229/** Atmel 25040 serial EEPROM */
230static inline __attribute__ (( always_inline )) void
231init_at25040 ( struct spi_device *device ) {
232 device->address_len = 8;
233 device->munge_address = 1;
234 device->nvs.size = 512;
235 device->nvs.block_size = 8;
236 init_spi ( device );
237}
238
239/** ST M25P32 serial flash */
240static inline __attribute__ (( always_inline )) void
241init_m25p32 ( struct spi_device *device ) {
242 device->address_len = 24;
243 device->nvs.size = ( 4 * 1024 * 1024 );
244 device->nvs.block_size = 256;
245 init_spi ( device );
246}
247
248/** Microchip 25XX640 serial EEPROM */
249static inline __attribute__ (( always_inline )) void
250init_mc25xx640 ( struct spi_device *device ) {
251 device->address_len = 16;
252 device->nvs.size = ( 8 * 1024 );
253 device->nvs.block_size = 32;
254 init_spi ( device );
255}
256
257/** @} */
258
259#endif /* _IPXE_SPI_H */
ring len
Length.
Definition dwmac.h:226
uint8_t bus
Bus.
Definition edd.h:1
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint64_t address
Base address.
Definition ena.h:13
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define __attribute__(x)
Definition compiler.h:10
Non-volatile storage.
int spi_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from SPI device.
Definition spi.c:89
int spi_write(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write data to SPI device.
Definition spi.c:116
int spi_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from SPI device.
Definition spi.c:89
int spi_write(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write data to SPI device.
Definition spi.c:116
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
A command-line command.
Definition command.h:10
A hardware device.
Definition device.h:77
A non-volatile storage device.
Definition nvs.h:16
An SPI bus.
Definition spi.h:127
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:153
unsigned int mode
SPI interface mode.
Definition spi.h:137
An SPI device.
Definition spi.h:87
unsigned int slave
Slave number.
Definition spi.h:93
unsigned int munge_address
Address is munged.
Definition spi.h:105
struct nvs_device nvs
NVS device.
Definition spi.h:89
unsigned int command_len
Command length, in bits.
Definition spi.h:95
struct spi_bus * bus
SPI bus to which device is attached.
Definition spi.h:91
unsigned int address_len
Address length, in bits.
Definition spi.h:97