spiflash.h 10 KB

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