spiflash.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 flash_data_type {
  70. FDT_END, /* End of stream */
  71. FDT_DATA, /* Data to be flashed */
  72. FDT_TARGET, /* Subsystem string (must match) */
  73. FDT_NOTE /* Version: XXXXX or similar */
  74. };
  75. enum flash_data_flags {
  76. FDF_OPTIONAL = 0x0001 /* Ignore if chunk data type unknown */
  77. };
  78. /*
  79. * A page is an amount that can be programmed in one operation.
  80. * A sector is the minimum amount that can be erased in one operation.
  81. * A block is the optimal amount that can be erased in one operation.
  82. *
  83. * These are defined in hardware!
  84. */
  85. #define SPIFLASH_PAGE_SHIFT 8 /* May be smaller than an actual page */
  86. #define SPIFLASH_PAGE_SIZE (1 << SPIFLASH_PAGE_SHIFT)
  87. #define SPIFLASH_SECTOR_SHIFT 12
  88. #define SPIFLASH_SECTOR_SIZE (1 << SPIFLASH_SECTOR_SHIFT)
  89. #define SPIFLASH_BLOCK_SHIFT 16
  90. #define SPIFLASH_BLOCK_SIZE (1 << SPIFLASH_BLOCK_SHIFT)
  91. /*
  92. * Interface to the host. This structure should be passed in to the
  93. * initialization routine and will not be modified by the spiflash
  94. * routines.
  95. *
  96. * The spiflash code is reentrant if there are multiple SPI flash
  97. * devices. None are timing critical and may be preempted at any
  98. * time if applicable. When CS# is active (during spi_read or spi_write),
  99. * it MUST NOT be deasserted; if the bus is shared HOLD# can be asserted
  100. * (without deasserting CS#) to make the device release the bus without
  101. * affecting the state of the device.
  102. *
  103. * CS# will not be asserted while running in the core code or when
  104. * blocking for I/O; the host is obviously allowed to prefetch I/O
  105. * within the above constraints if the bus is shared.
  106. *
  107. * A private cookie pointer is passed to each function; this can be a
  108. * pointer back to the spiflash_ops structure, but does not have to
  109. * be.
  110. */
  111. struct spiflash_ops {
  112. /*
  113. * Perform a SPI write operation. The SPI write operation consists of:
  114. * 1. Assert CS# (and deassert HOLD# if applicable)
  115. * 2. Transmit the command bytes (discard MISO input)
  116. * 3. Transmit the data bytes (discard MISO input)
  117. * 4. Deassert CS#
  118. * 5. Wait tCHSL (see SPI data table below)
  119. *
  120. * The number of data bytes may be zero. Note that the command
  121. * and data operations are identical and are separated only to
  122. * avoid unnecessary data copies.
  123. *
  124. * If this returns nonzero, no further operations are performed
  125. * and the top-level flash routine terminates immediately with
  126. * the returned value as a status code.
  127. *
  128. * It is not required that this routine blocks until tCHSL
  129. * is satisfied, however, the host is responsible to not assert
  130. * CS# again until tCHSL is satisfied. The SPI clock may run
  131. * or not during that time period.
  132. *
  133. * The SPI flash supports SPI modes 0 and 3.
  134. */
  135. int (*spi_write)(void *cookie,
  136. const void *cmd, unsigned int cmd_len,
  137. const void *data, unsigned int data_len,
  138. int tshsl);
  139. /*
  140. * Perform a SPI read operation. The SPI read operation consists of:
  141. * 1. Assert CS# (and deassert HOLD# if applicable)
  142. * 2. Transmit the command bytes (discard MISO input)
  143. * 3. Receive the data bytes (MOSI is don't care)
  144. * 4. Deassert CS#
  145. * 5. Wait tCHSL (see SPI data table below)
  146. *
  147. * The number of data bytes may be zero. Note that the command
  148. * and data operations are identical and are separated only to
  149. * avoid unnecessary data copies.
  150. *
  151. * If this returns nonzero, no further operations are performed
  152. * and the top-level flash routine terminates immediately with
  153. * the returned value as a status code.
  154. *
  155. * It is not required that this routine blocks until tCHSL
  156. * is satisfied, however, the host is responsible to not assert
  157. * CS# again until tCHSL is satisfied. The SPI clock may run
  158. * or not during that time period.
  159. *
  160. * The SPI flash supports SPI modes 0 and 3.
  161. */
  162. int (*spi_read)(void *cookie,
  163. const void *cmd, unsigned int cmd_len,
  164. void *data, unsigned int data_len,
  165. int tshsl);
  166. /*
  167. * Inform the host that the spiflash code is waiting for an
  168. * program or erase operation to complete. This can be used to
  169. * yield the host for other operations.
  170. *
  171. * The value passed in is the corresponding from the SPI data
  172. * table below; these are arbitrary cookies/units as far as the
  173. * spiflash code is concerned.
  174. *
  175. * This function may be NULL, in which case the SPI flash is polled
  176. * continously.
  177. */
  178. void (*yield)(void *cookie, int delay);
  179. };
  180. /*
  181. * This table provides some parameters for the SPI flash.
  182. */
  183. enum spiflash_addr_mode {
  184. SPIFLASH_ADDR_DYNAMIC, /* 24-bit for < 16 MB, otherwise 32 bit */
  185. SPIFLASH_ADDR_24BIT, /* 24-bit addressing only */
  186. SPIFLASH_ADDR_32BIT /* 32-bit addressing only */
  187. };
  188. struct spiflash_param {
  189. /*
  190. * Addressing mode (see above.) If SPIFLASH_ADDR_DYNAMIC is
  191. * specified (default), the chip is assumed to be in 24-bit-default
  192. * mode, and 32-bit opcodes will be used as needed.
  193. */
  194. enum spiflash_addr_mode addr;
  195. /*
  196. * CS# deselect times passed to spi_read() and spi_write().
  197. * Arbitrary units or cookies that are only interpreted by
  198. * the spi_read and spi_write routines.
  199. */
  200. int tshsl; /* All other operations */
  201. int tshsl1; /* Read operations */
  202. int tshsl2; /* Erase, Program, and Write operations */
  203. /*
  204. * Delay values to pass to the yield operation. Arbitrary units
  205. * or cookies that are only interpreted by the yield routine.
  206. *
  207. * Not all of these are used by the current code, but are specified
  208. * for future-proofing reasons.
  209. */
  210. int trst; /* Reset command to next instruction */
  211. int tw; /* Write Status Register Time */
  212. int tpp; /* Page Program Time */
  213. int tse; /* Sector Erase Time (4K) */
  214. int tbe1; /* Block Erase Time (32K) */
  215. int tbe2; /* Block Erase Time (64K) */
  216. int tce; /* Chip Erase Time */
  217. };
  218. /* Common structure for the above */
  219. struct spiflash {
  220. /*
  221. * Read input data for flash write. Return the number of bytes
  222. * read. A short read or a 0 byte return value represents end of
  223. * file/end of data; a negative value is treated as 0, and will be
  224. * returned from the top-level operation as a status code.
  225. *
  226. * It is not required to detect end of input if and only if the
  227. * input is a gzip file, as in that case the gzip data will contain
  228. * an end of stream indicator.
  229. *
  230. * The buffer initially passed to this function will always be
  231. * aligned to a malloc() alignment boundary; it will preserve
  232. * alignment boundaries if and only if short read returns only byte
  233. * counts in multiple of those alignment boundaries.
  234. *
  235. * If there is a memory buffer available containing full or
  236. * partial input data on entry, pass it to spiflash_flash_file();
  237. * If this memory buffer contains all available input, this
  238. * function can be NULL.
  239. *
  240. * A partial memory buffer must contain a full stream header block.
  241. */
  242. int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
  243. void *cookie; /* Pointer passed to spiflash_ops functions */
  244. /*
  245. * Operations on the SPI flash itself; if ops == NULL then this is a
  246. * dry run operation.
  247. */
  248. const struct spiflash_ops *ops;
  249. const struct spiflash_param *param;
  250. const char *target; /* What are we programming? */
  251. };
  252. /*
  253. * Additional error codes
  254. */
  255. #define SPIFLASH_ERR_ERASE_FAILED (-7)
  256. #define SPIFLASH_ERR_PROGRAM_FAILED (-8)
  257. #define SPIFLASH_ERR_WRITE_PROTECT (-9)
  258. #define SPIFLASH_ERR_NOT_READY (-10)
  259. #define SPIFLASH_ERR_DETECT (-11)
  260. /*
  261. * Top-level operations. These may return an error value from the ops
  262. * functions, any of the negative error values defined in zlib.h,
  263. * or one of the above error codes.
  264. */
  265. int spiflash_flash_file(const struct spiflash *flash,
  266. void *data, size_t datalen);
  267. /*
  268. * Read identifying data from SPI flash.
  269. */
  270. #define SPIFLASH_ID_LEN 8
  271. int spiflash_read_id(const struct spiflash *flash, void *id);
  272. #define SPIFLASH_VDID_LEN 2
  273. int spiflash_read_vdid(const struct spiflash *flash, void *vdid);
  274. #endif