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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #ifndef _GPXE_SPI_H
  2. #define _GPXE_SPI_H
  3. /** @file
  4. *
  5. * SPI interface
  6. *
  7. */
  8. #include <gpxe/bitbash.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. struct spi_device;
  55. /**
  56. * An SPI device type
  57. *
  58. * This data structure represents all the characteristics belonging to
  59. * a particular type of SPI device, e.g. "an Atmel 251024 serial flash",
  60. * or "a Microchip 25040 serial EEPROM".
  61. */
  62. struct spi_device_type {
  63. /** Word length, in bits */
  64. unsigned int word_len;
  65. /** Device size (in words) */
  66. unsigned int size;
  67. /** Data block size (in words)
  68. *
  69. * This is the block size used by the device. It must be a
  70. * power of two. Data reads and writes must not cross a block
  71. * boundary.
  72. *
  73. * Many devices allow reads to cross a block boundary, and
  74. * restrict only writes. For the sake of simplicity, we
  75. * assume that the same restriction applies to both reads and
  76. * writes.
  77. */
  78. unsigned int block_size;
  79. /** Command length, in bits */
  80. unsigned int command_len;
  81. /** Address length, in bits */
  82. unsigned int address_len;
  83. /** Address is munged
  84. *
  85. * Some devices with 9-bit addresses (e.g. AT25040A EEPROM)
  86. * use bit 3 of the command byte as address bit A8, rather
  87. * than having a two-byte address. If this flag is set, then
  88. * commands should be munged in this way.
  89. */
  90. unsigned int munge_address : 1;
  91. /** Read data from device
  92. *
  93. * @v device SPI device
  94. * @v address Address from which to read
  95. * @v data Data buffer
  96. * @v len Length of data buffer
  97. * @ret rc Return status code
  98. */
  99. int ( * read ) ( struct spi_device *device, unsigned int address,
  100. void *data, size_t len );
  101. /** Write data to device
  102. *
  103. * @v device SPI device
  104. * @v address Address to which to write
  105. * @v data Data buffer
  106. * @v len Length of data buffer
  107. * @ret rc Return status code
  108. */
  109. int ( * write ) ( struct spi_device *device, unsigned int address,
  110. const void *data, size_t len );
  111. };
  112. /**
  113. * @defgroup spidevs SPI device types
  114. * @{
  115. */
  116. /** Atmel AT25010 serial EEPROM */
  117. #define AT25010 { \
  118. .word_len = 8, \
  119. .size = 128, \
  120. .block_size = 8, \
  121. .command_len = 8, \
  122. .address_len = 8, \
  123. }
  124. /** @} */
  125. /**
  126. * An SPI device
  127. *
  128. * This data structure represents a real, physical SPI device attached
  129. * to an SPI controller. It comprises the device type plus
  130. * instantiation-specific information such as the slave number.
  131. */
  132. struct spi_device {
  133. /** SPI device type */
  134. struct spi_device_type *type;
  135. /** SPI bus to which device is attached */
  136. struct spi_bus *bus;
  137. /** Slave number */
  138. unsigned int slave;
  139. };
  140. /**
  141. * An SPI bus
  142. *
  143. *
  144. */
  145. struct spi_bus {
  146. /** SPI interface mode
  147. *
  148. * This is the bitwise OR of zero or more of @c SPI_MODE_CPHA
  149. * and @c SPI_MODE_CPOL. It is also the number conventionally
  150. * used to describe the SPI interface mode. For example, SPI
  151. * mode 1 is the mode in which CPOL=0 and CPHA=1, which
  152. * therefore corresponds to a mode value of (0|SPI_MODE_CPHA)
  153. * which, happily, equals 1.
  154. */
  155. unsigned int mode;
  156. /**
  157. * Read/write data via SPI bus
  158. *
  159. * @v bus SPI bus
  160. * @v device SPI device
  161. * @v command Command
  162. * @v address Address to read/write (<0 for no address)
  163. * @v data_out TX data buffer (or NULL)
  164. * @v data_in RX data buffer (or NULL)
  165. * @v len Length of data buffer(s)
  166. *
  167. * This issues the specified command and optional address to
  168. * the SPI device, then reads and/or writes data to/from the
  169. * data buffers.
  170. */
  171. int ( * rw ) ( struct spi_bus *bus, struct spi_device *device,
  172. unsigned int command, int address,
  173. const void *data_out, void *data_in, size_t len );
  174. };
  175. /** Clock phase (CPHA) mode bit
  176. *
  177. * Phase 0 is sample on rising edge, shift data on falling edge.
  178. *
  179. * Phase 1 is shift data on rising edge, sample data on falling edge.
  180. */
  181. #define SPI_MODE_CPHA 0x01
  182. /** Clock polarity (CPOL) mode bit
  183. *
  184. * This bit reflects the idle state of the clock line (SCLK).
  185. */
  186. #define SPI_MODE_CPOL 0x02
  187. /** Slave select polarity mode bit
  188. *
  189. * This bit reflects that active state of the slave select lines. It
  190. * is not part of the normal SPI mode number (which covers only @c
  191. * SPI_MODE_CPOL and @c SPI_MODE_CPHA), but is included here for
  192. * convenience.
  193. */
  194. #define SPI_MODE_SSPOL 0x10
  195. /** Microwire-compatible mode
  196. *
  197. * This is SPI mode 1 (i.e. CPOL=0, CPHA=1), and is compatible with
  198. * the original Microwire protocol.
  199. */
  200. #define SPI_MODE_MICROWIRE 1
  201. /** Microwire/Plus-compatible mode
  202. *
  203. * This is SPI mode 0 (i.e. CPOL=0, CPHA=0), and is compatible with
  204. * the Microwire/Plus protocol
  205. */
  206. #define SPI_MODE_MICROWIRE_PLUS 0
  207. /** Threewire-compatible mode
  208. *
  209. * This mode is compatible with Atmel's series of "three-wire"
  210. * interfaces.
  211. */
  212. #define SPI_MODE_THREEWIRE ( SPI_MODE_MICROWIRE_PLUS | SPI_MODE_SSPOL )
  213. #endif /* _GPXE_SPI_H */