10 FILE_LICENCE ( GPL2_OR_LATER );
15 * @defgroup spicmds SPI commands
19 /** Write status register */
22 /** Write data to memory array */
23 #define SPI_WRITE 0x02
25 /** Read data from memory array */
28 /** Reset write enable latch */
31 /** Read status register */
34 /** Set write enable latch */
38 * @defgroup atmelcmds Atmel-specific SPI commands
42 /** Erase one sector in memory array (Not supported on all devices) */
43 #define ATMEL_SECTOR_ERASE 0x52
45 /** Erase all sections in memory array (Not supported on all devices) */
46 #define ATMEL_CHIP_ERASE 0x62
48 /** Read manufacturer and product ID (Not supported on all devices) */
49 #define ATMEL_RDID 0x15
56 * @defgroup spistatus SPI status register bits (not present on all devices)
60 /** Write-protect pin enabled */
61 #define SPI_STATUS_WPEN 0x80
63 /** Block protection bit 2 */
64 #define SPI_STATUS_BP2 0x10
66 /** Block protection bit 1 */
67 #define SPI_STATUS_BP1 0x08
69 /** Block protection bit 0 */
70 #define SPI_STATUS_BP0 0x04
72 /** State of the write enable latch */
73 #define SPI_STATUS_WEN 0x02
75 /** Device busy flag */
76 #define SPI_STATUS_NRDY 0x01
83 * This data structure represents a physical SPI device attached to an
88 struct nvs_device nvs;
89 /** SPI bus to which device is attached */
93 /** Command length, in bits */
94 unsigned int command_len;
95 /** Address length, in bits */
96 unsigned int address_len;
99 * Some devices with 9-bit addresses (e.g. AT25040A EEPROM)
100 * use bit 3 of the command byte as address bit A8, rather
101 * than having a two-byte address. If this flag is set, then
102 * commands should be munged in this way.
104 unsigned int munge_address : 1;
107 static inline __attribute__ (( always_inline )) struct spi_device *
108 nvs_to_spi ( struct nvs_device *nvs ) {
109 return container_of ( nvs, struct spi_device, nvs );
115 * This data structure represents an SPI bus controller capable of
116 * issuing commands to attached SPI devices.
119 /** SPI interface mode
121 * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
122 * and @c SPI_MODE_CPOL. It is also the number conventionally
123 * used to describe the SPI interface mode. For example, SPI
124 * mode 1 is the mode in which CPOL=0 and CPHA=1, which
125 * therefore corresponds to a mode value of (0|SPI_MODE_CPHA)
126 * which, happily, equals 1.
130 * Read/write data via SPI bus
133 * @v device SPI device
135 * @v address Address to read/write (<0 for no address)
136 * @v data_out TX data buffer (or NULL)
137 * @v data_in RX data buffer (or NULL)
138 * @v len Length of data buffer(s)
140 * This issues the specified command and optional address to
141 * the SPI device, then reads and/or writes data to/from the
144 int ( * rw ) ( struct spi_bus *bus, struct spi_device *device,
145 unsigned int command, int address,
146 const void *data_out, void *data_in, size_t len );
149 /** Clock phase (CPHA) mode bit
151 * Phase 0 is sample on rising edge, shift data on falling edge.
153 * Phase 1 is shift data on rising edge, sample data on falling edge.
155 #define SPI_MODE_CPHA 0x01
157 /** Clock polarity (CPOL) mode bit
159 * This bit reflects the idle state of the clock line (SCLK).
161 #define SPI_MODE_CPOL 0x02
163 /** Slave select polarity mode bit
165 * This bit reflects that active state of the slave select lines. It
166 * is not part of the normal SPI mode number (which covers only @c
167 * SPI_MODE_CPOL and @c SPI_MODE_CPHA), but is included here for
170 #define SPI_MODE_SSPOL 0x10
172 /** Microwire-compatible mode
174 * This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with
175 * the original Microwire protocol.
177 #define SPI_MODE_MICROWIRE 1
179 /** Microwire/Plus-compatible mode
181 * This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with
182 * the Microwire/Plus protocol
184 #define SPI_MODE_MICROWIRE_PLUS 0
186 /** Threewire-compatible mode
188 * This mode is compatible with Atmel's series of "three-wire"
191 #define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
193 extern int spi_read ( struct nvs_device *nvs, unsigned int address,
194 void *data, size_t len );
195 extern int spi_write ( struct nvs_device *nvs, unsigned int address,
196 const void *data, size_t len );
199 * @defgroup spidevs SPI device types
203 static inline __attribute__ (( always_inline )) void
204 init_spi ( struct spi_device *device ) {
205 device->nvs.word_len_log2 = 0;
206 device->command_len = 8,
207 device->nvs.read = spi_read;
208 device->nvs.write = spi_write;
211 /** Atmel AT25F1024 serial flash */
212 static inline __attribute__ (( always_inline )) void
213 init_at25f1024 ( struct spi_device *device ) {
214 device->address_len = 24;
215 device->nvs.size = ( 128 * 1024 );
216 device->nvs.block_size = 256;
220 /** Atmel 25040 serial EEPROM */
221 static inline __attribute__ (( always_inline )) void
222 init_at25040 ( struct spi_device *device ) {
223 device->address_len = 8;
224 device->munge_address = 1;
225 device->nvs.size = 512;
226 device->nvs.block_size = 8;
230 /** ST M25P32 serial flash */
231 static inline __attribute__ (( always_inline )) void
232 init_m25p32 ( struct spi_device *device ) {
233 device->address_len = 24;
234 device->nvs.size = ( 4 * 1024 * 1024 );
235 device->nvs.block_size = 256;
239 /** Microchip 25XX640 serial EEPROM */
240 static inline __attribute__ (( always_inline )) void
241 init_mc25xx640 ( struct spi_device *device ) {
242 device->address_len = 16;
243 device->nvs.size = ( 8 * 1024 );
244 device->nvs.block_size = 32;
250 #endif /* _GPXE_SPI_H */