You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spi.h 6.2KB

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