spiflash.h 7.5 KB

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