spiflash.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #ifndef SPIFLASH_H
  2. #define SPIFLASH_H
  3. #include <inttypes.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. /*
  7. * Firmware blob data header. A dlen of 0 means no futher blobs.
  8. * dlen == zlen means raw binary data, not compressed.
  9. */
  10. #define SPIFLASH_MAGIC 0xe301e7eb
  11. #define SPIFLASH_HEADER_CRC32 0x99d8ef20
  12. struct spiflash_header {
  13. uint32_t magic; /* Magic number */
  14. uint32_t zlen; /* Compressed data length */
  15. uint32_t dlen; /* Uncompressed data length */
  16. uint32_t crc32; /* CRC32 of (raw) data block */
  17. uint32_t address; /* Target address in flash (block aligned) */
  18. uint32_t resv[2]; /* Set to zero for now */
  19. uint32_t header_crc32; /* CRC32 of above fields */
  20. };
  21. /*
  22. * A page is the maximum amount that can be programmed in one operation.
  23. * A sector is the minimum amount that can be erased in one operation.
  24. * A block is the optimal amount that can be erased in one operation.
  25. *
  26. * These are defined in hardware!
  27. */
  28. #define SPIFLASH_PAGE_SHIFT 8
  29. #define SPIFLASH_PAGE_SIZE (1 << SPIFLASH_PAGE_SHIFT)
  30. #define SPIFLASH_SECTOR_SHIFT 12
  31. #define SPIFLASH_SECTOR_SIZE (1 << SPIFLASH_SECTOR_SHIFT)
  32. #define SPIFLASH_BLOCK_SHIFT 16
  33. #define SPIFLASH_BLOCK_SIZE (1 << SPIFLASH_BLOCK_SHIFT)
  34. /*
  35. * Interface to the host. This structure should be passed in to the
  36. * initialization routine and will not be modified by the spiflash
  37. * routines.
  38. *
  39. * The spiflash code is reentrant if there are multiple SPI flash
  40. * devices. None are timing critical and may be preempted at any
  41. * time if applicable. When CS# is active (during spi_read or spi_write),
  42. * it MUST NOT be deasserted; if the bus is shared HOLD# can be asserted
  43. * (without deasserting CS#) to make the device release the bus without
  44. * affecting the state of the device.
  45. *
  46. * CS# will not be asserted while running in the core code or when
  47. * blocking for I/O; the host is obviously allowed to prefetch I/O
  48. * within the above constraints if the bus is shared.
  49. *
  50. * A private cookie pointer is passed to each function; this can be a
  51. * pointer back to the spiflash_ops structure, but does not have to
  52. * be.
  53. */
  54. struct spiflash_ops {
  55. /*
  56. * Read input data for flash write. Return the number of bytes
  57. * read. A short read or a 0 byte return value represents end of
  58. * file/end of data; a negative value is treated as 0, and will be
  59. * returned from the top-level operation as a status code.
  60. *
  61. * It is not required to detect end of input if and only if the
  62. * input is a gzip file, as in that case the gzip data will contain
  63. * an end of stream indicator.
  64. *
  65. * The buffer initially passed to this function will always be
  66. * aligned to a malloc() alignment boundary; it will preserve
  67. * alignment boundaries if and only if short read returns only byte
  68. * counts in multiple of those alignment boundaries.
  69. *
  70. * If there is a memory buffer available containing full or
  71. * partial input data on entry, pass it to spiflash_flash_file();
  72. * If this memory buffer contains all available input, this
  73. * function can be NULL.
  74. *
  75. * A partial memory buffer must contain the full stream header.
  76. *
  77. * A full memory buffer must either be expanded to a multiple
  78. * of SPIFLASH_BLOCK_SIZE or safely allow the flash code to do so.
  79. */
  80. int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
  81. /*
  82. * Indicates that no more data will be read (end of stream detected
  83. * or an error happened.) May be NULL. A nonzero value will be passed
  84. * to the top-level routine as an error.
  85. */
  86. int (*close_data)(void *cookie);
  87. /*
  88. * Perform a SPI write operation. The SPI write operation consists of:
  89. * 1. Assert CS# (and deassert HOLD# if applicable)
  90. * 2. Transmit the command bytes (discard MISO input)
  91. * 3. Transmit the data bytes (discard MISO input)
  92. * 4. Deassert CS#
  93. * 5. Wait tCHSL (see SPI data table below)
  94. *
  95. * The number of data bytes may be zero. Note that the command
  96. * and data operations are identical and are separated only to
  97. * avoid unnecessary data copies.
  98. *
  99. * If this returns nonzero, no further operations are performed
  100. * and the top-level flash routine terminates immediately with
  101. * the returned value as a status code.
  102. *
  103. * It is not required that this routine blocks until tCHSL
  104. * is satisfied, however, the host is responsible to not assert
  105. * CS# again until tCHSL is satisfied. The SPI clock may run
  106. * or not during that time period.
  107. *
  108. * The SPI flash supports SPI modes 0 and 3.
  109. */
  110. int (*spi_write)(void *cookie,
  111. const void *cmd, unsigned int cmd_len,
  112. const void *data, unsigned int data_len,
  113. int tshsl);
  114. /*
  115. * Perform a SPI read operation. The SPI read operation consists of:
  116. * 1. Assert CS# (and deassert HOLD# if applicable)
  117. * 2. Transmit the command bytes (discard MISO input)
  118. * 3. Receive the data bytes (MOSI is don't care)
  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_read)(void *cookie,
  138. const void *cmd, unsigned int cmd_len,
  139. void *data, unsigned int data_len,
  140. int tshsl);
  141. /*
  142. * Inform the host that the spiflash code is waiting for an
  143. * program or erase operation to complete. This can be used to
  144. * yield the host for other operations.
  145. *
  146. * The value passed in is the corresponding from the SPI data
  147. * table below; these are arbitrary cookies/units as far as the
  148. * spiflash code is concerned.
  149. *
  150. * This function may be NULL, in which case the SPI flash is polled
  151. * continously.
  152. */
  153. void (*yield)(void *cookie, int delay);
  154. };
  155. /*
  156. * This table provides some parameters for the SPI flash.
  157. */
  158. enum spiflash_addr_mode {
  159. SPIFLASH_ADDR_DYNAMIC, /* 24-bit for < 16 MB, otherwise 32 bit */
  160. SPIFLASH_ADDR_24BIT, /* 24-bit addressing only */
  161. SPIFLASH_ADDR_32BIT /* 32-bit addressing only */
  162. };
  163. struct spiflash_param {
  164. /*
  165. * Addressing mode (see above.) If SPIFLASH_ADDR_DYNAMIC is
  166. * specified (default), the chip is assumed to be in 24-bit-default
  167. * mode, and 32-bit opcodes will be used as needed.
  168. */
  169. enum spiflash_addr_mode addr;
  170. /*
  171. * CS# deselect times passed to spi_read() and spi_write().
  172. * Arbitrary units or cookies that are only interpreted by
  173. * the spi_read and spi_write routines.
  174. */
  175. int tshsl; /* All other operations */
  176. int tshsl1; /* Read operations */
  177. int tshsl2; /* Erase, Program, and Write operations */
  178. /*
  179. * Delay values to pass to the yield operation. Arbitrary units
  180. * or cookies that are only interpreted by the yield routine.
  181. *
  182. * Not all of these are used by the current code, but are specified
  183. * for future-proofing reasons.
  184. */
  185. int trst; /* Reset command to next instruction */
  186. int tw; /* Write Status Register Time */
  187. int tpp; /* Page Program Time */
  188. int tse; /* Sector Erase Time (4K) */
  189. int tbe1; /* Block Erase Time (32K) */
  190. int tbe2; /* Block Erase Time (64K) */
  191. int tce; /* Chip Erase Time */
  192. };
  193. /* Common structure for the above */
  194. struct spiflash {
  195. const struct spiflash_ops *ops;
  196. void *cookie; /* Pointer passed to spiflash_ops functions */
  197. const struct spiflash_param *param;
  198. };
  199. /*
  200. * Additional error codes
  201. */
  202. #define SPIFLASH_ERR_ERASE_FAILED (-7)
  203. #define SPIFLASH_ERR_PROGRAM_FAILED (-8)
  204. /*
  205. * Top-level operations. These may return an error value from the ops
  206. * functions, any of the negative error values defined in zlib.h,
  207. * or one of the above error codes.
  208. */
  209. int spiflash_flash_files(const struct spiflash *flash,
  210. void *data, size_t datalen);
  211. /*
  212. * Read identifying data from SPI flash.
  213. */
  214. #define SPIFLASH_ID_LEN 8
  215. int spiflash_read_id(const struct spiflash *flash, void *id);
  216. #define SPIFLASH_VDID_LEN 2
  217. int spiflash_read_vdid(const struct spiflash *flash, void *vdid);
  218. #endif