2
0

spiflash.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef SPIFLASH_H
  2. #define SPIFLASH_H
  3. #include <inttypes.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include "fwimg.h"
  7. /* SPI flash command opcodes */
  8. enum romcmd {
  9. /* Standard SPI mode commands */
  10. ROM_WRITE_ENABLE = 0x06,
  11. ROM_VOLATILE_SR_WRITE_ENABLE = 0x50,
  12. ROM_WRITE_DISABLE = 0x04,
  13. ROM_RELEASE_POWERDOWN_ID = 0xab,
  14. ROM_MANUFACTURER_DEVICE_ID = 0x90,
  15. ROM_JEDEC_ID = 0x9f,
  16. ROM_READ_UNIQUE_ID = 0x4b,
  17. ROM_READ_DATA = 0x03, /* DO NOT USE */
  18. ROM_READ_DATA_32BIT = 0x13, /* DO NOT USE */
  19. ROM_FAST_READ = 0x0b,
  20. ROM_FAST_READ_32BIT = 0x0c,
  21. ROM_PAGE_PROGRAM = 0x02,
  22. ROM_PAGE_PROGRAM_32BIT = 0x12,
  23. ROM_ERASE_4K = 0x20,
  24. ROM_ERASE_4K_32BIT = 0x21,
  25. ROM_ERASE_32K = 0x52,
  26. ROM_ERASE_64K = 0xd8,
  27. ROM_ERASE_64K_32BIT = 0xdc,
  28. ROM_ERASE_ALL = 0xc7,
  29. ROM_READ_SR1 = 0x05,
  30. ROM_WRITE_SR1 = 0x01,
  31. ROM_READ_SR2 = 0x35,
  32. ROM_WRITE_SR2 = 0x31,
  33. ROM_READ_SR3 = 0x15,
  34. ROM_WRITE_SR3 = 0x11,
  35. ROM_READ_EAR = 0xc8, /* Extended address register */
  36. ROM_WRITE_EAR = 0xc5,
  37. ROM_READ_SFDP = 0x5a,
  38. ROM_ERASE_SECURITY = 0x44,
  39. ROM_PROGRAM_SECURITY = 0x42,
  40. ROM_READ_SECURITY = 0x48,
  41. ROM_GLOBAL_BLOCK_LOCK = 0x7e,
  42. ROM_GLOBAL_BLOCK_UNLOCK = 0x98,
  43. ROM_READ_BLOCK_LOCK = 0x3d,
  44. ROM_ONE_BLOCK_LOCK = 0x36,
  45. ROM_ONE_BLOCK_UNLOCK = 0x39,
  46. ROM_ERASE_PROGRAM_SUSPEND = 0x75,
  47. ROM_ERASE_PROGRAM_RESUME = 0x7a,
  48. ROM_POWER_DOWN = 0xb9,
  49. ROM_ENTER_32BIT = 0xb7,
  50. ROM_LEAVE_32BIT = 0xe9,
  51. ROM_ENTER_QPI = 0x48,
  52. ROM_ENABLE_RESET = 0x66,
  53. ROM_RESET = 0x99,
  54. /* Dual SPI commands */
  55. ROM_FAST_READ_DUAL = 0x3b,
  56. ROM_FAST_READ_DUAL_32BIT = 0x3c
  57. };
  58. #define SPIFLASH_SFDP_SIZE 256
  59. /*
  60. * A page is an amount that can be programmed in one operation.
  61. * A sector is the minimum amount that can be erased in one operation.
  62. * A block is the optimal amount that can be erased in one operation.
  63. *
  64. * These are defined in hardware!
  65. */
  66. #define SPIFLASH_PAGE_SHIFT 8 /* May be smaller than an actual page */
  67. #define SPIFLASH_PAGE_SIZE (1 << SPIFLASH_PAGE_SHIFT)
  68. #define SPIFLASH_SECTOR_SHIFT 12
  69. #define SPIFLASH_SECTOR_SIZE (1 << SPIFLASH_SECTOR_SHIFT)
  70. #define SPIFLASH_BLOCK_SHIFT 16
  71. #define SPIFLASH_BLOCK_SIZE (1 << SPIFLASH_BLOCK_SHIFT)
  72. /*
  73. * Interface to the host. This structure should be passed in to the
  74. * initialization routine and will not be modified by the spiflash
  75. * routines.
  76. *
  77. * The spiflash code is reentrant if there are multiple SPI flash
  78. * devices. None are timing critical and may be preempted at any
  79. * time if applicable. When CS# is active (during spi_read or spi_write),
  80. * it MUST NOT be deasserted; if the bus is shared HOLD# can be asserted
  81. * (without deasserting CS#) to make the device release the bus without
  82. * affecting the state of the device.
  83. *
  84. * CS# will not be asserted while running in the core code or when
  85. * blocking for I/O; the host is obviously allowed to prefetch I/O
  86. * within the above constraints if the bus is shared.
  87. *
  88. * A private cookie pointer is passed to each function; this can be a
  89. * pointer back to the spiflash_ops structure, but does not have to
  90. * be.
  91. */
  92. struct spiflash_ops {
  93. /*
  94. * Perform a SPI write operation. The SPI write operation consists of:
  95. * 1. Assert CS# (and deassert HOLD# if applicable)
  96. * 2. Transmit the command bytes (discard MISO input)
  97. * 3. Transmit the data bytes (discard MISO input)
  98. * 4. Deassert CS#
  99. * 5. Wait tCHSL (see SPI data table below)
  100. *
  101. * The number of data bytes may be zero. Note that the command
  102. * and data operations are identical and are separated only to
  103. * avoid unnecessary data copies.
  104. *
  105. * If this returns nonzero, no further operations are performed
  106. * and the top-level flash routine terminates immediately with
  107. * the returned value as a status code.
  108. *
  109. * It is not required that this routine blocks until tCHSL
  110. * is satisfied, however, the host is responsible to not assert
  111. * CS# again until tCHSL is satisfied. The SPI clock may run
  112. * or not during that time period.
  113. *
  114. * The SPI flash supports SPI modes 0 and 3.
  115. */
  116. int (*spi_write)(void *cookie,
  117. const void *cmd, unsigned int cmd_len,
  118. const void *data, unsigned int data_len,
  119. int tshsl);
  120. /*
  121. * Perform a SPI read operation. The SPI read operation consists of:
  122. * 1. Assert CS# (and deassert HOLD# if applicable)
  123. * 2. Transmit the command bytes (discard MISO input)
  124. * 3. Receive the data bytes (MOSI is don't care)
  125. * 4. Deassert CS#
  126. * 5. Wait tCHSL (see SPI data table below)
  127. *
  128. * The number of data bytes may be zero. Note that the command
  129. * and data operations are identical and are separated only to
  130. * avoid unnecessary data copies.
  131. *
  132. * If this returns nonzero, no further operations are performed
  133. * and the top-level flash routine terminates immediately with
  134. * the returned value as a status code.
  135. *
  136. * It is not required that this routine blocks until tCHSL
  137. * is satisfied, however, the host is responsible to not assert
  138. * CS# again until tCHSL is satisfied. The SPI clock may run
  139. * or not during that time period.
  140. *
  141. * The SPI flash supports SPI modes 0 and 3.
  142. */
  143. int (*spi_read)(void *cookie,
  144. const void *cmd, unsigned int cmd_len,
  145. void *data, unsigned int data_len,
  146. int tshsl);
  147. /*
  148. * Inform the host that the spiflash code is waiting for an
  149. * program or erase operation to complete. This can be used to
  150. * yield the host for other operations.
  151. *
  152. * The value passed in is the corresponding from the SPI data
  153. * table below; these are arbitrary cookies/units as far as the
  154. * spiflash code is concerned.
  155. *
  156. * This function may be NULL, in which case the SPI flash is polled
  157. * continously.
  158. */
  159. void (*yield)(void *cookie, int delay);
  160. };
  161. /*
  162. * This table provides some parameters for the SPI flash.
  163. */
  164. enum spiflash_addr_mode {
  165. SPIFLASH_ADDR_DYNAMIC, /* 24-bit for < 16 MB, otherwise 32 bit */
  166. SPIFLASH_ADDR_24BIT, /* 24-bit addressing only */
  167. SPIFLASH_ADDR_32BIT /* 32-bit addressing only */
  168. };
  169. struct spiflash_param {
  170. /*
  171. * Addressing mode (see above.) If SPIFLASH_ADDR_DYNAMIC is
  172. * specified (default), the chip is assumed to be in 24-bit-default
  173. * mode, and 32-bit opcodes will be used as needed.
  174. */
  175. enum spiflash_addr_mode addr;
  176. /*
  177. * CS# deselect times passed to spi_read() and spi_write().
  178. * Arbitrary units or cookies that are only interpreted by
  179. * the spi_read and spi_write routines.
  180. */
  181. int tshsl; /* All other operations */
  182. int tshsl1; /* Read operations */
  183. int tshsl2; /* Erase, Program, and Write operations */
  184. /*
  185. * Delay values to pass to the yield operation. Arbitrary units
  186. * or cookies that are only interpreted by the yield routine.
  187. *
  188. * Not all of these are used by the current code, but are specified
  189. * for future-proofing reasons.
  190. */
  191. int trst; /* Reset command to next instruction */
  192. int tw; /* Write Status Register Time */
  193. int tpp; /* Page Program Time */
  194. int tse; /* Sector Erase Time (4K) */
  195. int tbe1; /* Block Erase Time (32K) */
  196. int tbe2; /* Block Erase Time (64K) */
  197. int tce; /* Chip Erase Time */
  198. };
  199. /* Common structure for the above */
  200. struct spiflash {
  201. /*
  202. * Read input data for flash write. Return the number of bytes
  203. * read. A short read or a 0 byte return value represents end of
  204. * file/end of data; a negative value is treated as 0, and will be
  205. * returned from the top-level operation as a status code.
  206. *
  207. * It is not required to detect end of input if and only if the
  208. * input is a gzip file, as in that case the gzip data will contain
  209. * an end of stream indicator.
  210. *
  211. * The buffer initially passed to this function will always be
  212. * aligned to a malloc() alignment boundary; it will preserve
  213. * alignment boundaries if and only if short read returns only byte
  214. * counts in multiple of those alignment boundaries.
  215. *
  216. * If there is a memory buffer available containing full or
  217. * partial input data on entry, pass it to spiflash_flash_file();
  218. * If this memory buffer contains all available input, this
  219. * function can be NULL.
  220. *
  221. * A partial memory buffer must contain a full stream header block.
  222. */
  223. int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
  224. void *cookie; /* Pointer passed to spiflash_ops functions */
  225. /*
  226. * Operations on the SPI flash itself; if ops == NULL then this is a
  227. * dry run operation.
  228. */
  229. const struct spiflash_ops *ops;
  230. const struct spiflash_param *param;
  231. const char *target; /* What are we programming? */
  232. };
  233. /*
  234. * Top-level operations. These may return an error value from the ops
  235. * functions, any of the negative error values defined in zlib.h,
  236. * or one of the above error codes.
  237. */
  238. int spiflash_flash_file(const struct spiflash *flash,
  239. void *data, size_t datalen);
  240. /*
  241. * Flash a single region of data to the SPI flash.
  242. */
  243. int spiflash_flash_data(const struct spiflash *flash, uint32_t addr,
  244. const void *data, size_t datalen);
  245. /*
  246. * Read identifying data from SPI flash.
  247. */
  248. #define SPIFLASH_ID_LEN 8
  249. int spiflash_read_id(const struct spiflash *flash, void *id);
  250. #define SPIFLASH_VDID_LEN 2
  251. int spiflash_read_vdid(const struct spiflash *flash, void *vdid);
  252. #endif