Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

spi.h 5.9KB

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