efuse.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ROM_EFUSE_H_
  7. #define _ROM_EFUSE_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <stdint.h>
  12. #include <stddef.h>
  13. #include <stdbool.h>
  14. /** \defgroup efuse_APIs efuse APIs
  15. * @brief ESP32 efuse read/write APIs
  16. * @attention
  17. *
  18. */
  19. /** For ESP32-C2, there's no key purpose region for efuse keys, In order to maintain
  20. * compatibility with the previous apis, we should set the parameter of 'ets_efuse_purpose_t'
  21. * as default value ETS_EFUSE_KEY_PURPOSE_INVALID.
  22. * (In fact, this parameter can be any value, the api in the rom will not process key_purpose region)
  23. */
  24. typedef enum {
  25. ETS_EFUSE_KEY_PURPOSE_INVALID = -1,
  26. } ets_efuse_purpose_t;
  27. typedef enum {
  28. ETS_EFUSE_BLOCK0 = 0,
  29. ETS_EFUSE_BLOCK_SYS_DATA1 = 1,
  30. ETS_EFUSE_BLOCK_SYS_DATA2 = 2,
  31. ETS_EFUSE_BLOCK_KEY0 = 3,
  32. ETS_EFUSE_BLOCK_MAX,
  33. } ets_efuse_block_t;
  34. /**
  35. * @brief set timing accroding the apb clock, so no read error or write error happens.
  36. *
  37. * @param clock: apb clock in HZ, only accept 5M(in FPGA), 10M(in FPGA), 20M, 40M, 80M.
  38. *
  39. * @return : 0 if success, others if clock not accepted
  40. */
  41. int ets_efuse_set_timing(uint32_t clock);
  42. /**
  43. * @brief Efuse read operation: copies data from physical efuses to efuse read registers.
  44. *
  45. * @param null
  46. *
  47. * @return : 0 if success, others if apb clock is not accepted
  48. */
  49. int ets_efuse_read(void);
  50. /**
  51. * @brief Efuse write operation: Copies data from efuse write registers to efuse. Operates on a single block of efuses at a time.
  52. *
  53. * @note This function does not update read efuses, call ets_efuse_read() once all programming is complete.
  54. *
  55. * @return : 0 if success, others if apb clock is not accepted
  56. */
  57. int ets_efuse_program(ets_efuse_block_t block);
  58. /**
  59. * @brief Set all Efuse program registers to zero.
  60. *
  61. * Call this before writing new data to the program registers.
  62. */
  63. void ets_efuse_clear_program_registers(void);
  64. /**
  65. * @brief Program a block of key data to an efuse block
  66. *
  67. * @param key_block Block to read purpose for. Must be in range ETS_EFUSE_BLOCK_KEY0 to ETS_EFUSE_BLOCK_KEY6. Key block must be unused (@ref ets_efuse_key_block_unused).
  68. * @param purpose Purpose to set for this key. Purpose must be already unset.
  69. * @param data Pointer to data to write.
  70. * @param data_len Length of data to write.
  71. *
  72. * @note This function also calls ets_efuse_program() for the specified block, and for block 0 (setting the purpose)
  73. */
  74. int ets_efuse_write_key(ets_efuse_block_t key_block, ets_efuse_purpose_t purpose, const void *data, size_t data_len);
  75. /* @brief Return the address of a particular efuse block's first read register
  76. *
  77. * @param block Index of efuse block to look up
  78. *
  79. * @return 0 if block is invalid, otherwise a numeric read register address
  80. * of the first word in the block.
  81. */
  82. uint32_t ets_efuse_get_read_register_address(ets_efuse_block_t block);
  83. /**
  84. * @brief Return the current purpose set for an efuse key block
  85. *
  86. * @param key_block Block to read purpose for. Must be in range ETS_EFUSE_BLOCK_KEY0 to ETS_EFUSE_BLOCK_KEY6.
  87. */
  88. ets_efuse_purpose_t ets_efuse_get_key_purpose(ets_efuse_block_t key_block);
  89. /**
  90. * @brief Find a key block with the particular purpose set
  91. *
  92. * @param purpose Purpose to search for.
  93. * @param[out] key_block Pointer which will be set to the key block if found. Can be NULL, if only need to test the key block exists.
  94. * @return true if found, false if not found. If false, value at key_block pointer is unchanged.
  95. */
  96. bool ets_efuse_find_purpose(ets_efuse_purpose_t purpose, ets_efuse_block_t *key_block);
  97. /**
  98. * Return true if the key block is unused, false otherwise.
  99. *
  100. * An unused key block is all zero content, not read or write protected,
  101. * and has purpose 0 (ETS_EFUSE_KEY_PURPOSE_USER)
  102. *
  103. * @param key_block key block to check.
  104. *
  105. * @return true if key block is unused, false if key block or used
  106. * or the specified block index is not a key block.
  107. */
  108. bool ets_efuse_key_block_unused(ets_efuse_block_t key_block);
  109. /**
  110. * @brief Search for an unused key block and return the first one found.
  111. *
  112. * See @ref ets_efuse_key_block_unused for a description of an unused key block.
  113. *
  114. * @return First unused key block, or ETS_EFUSE_BLOCK_MAX if no unused key block is found.
  115. */
  116. ets_efuse_block_t ets_efuse_find_unused_key_block(void);
  117. /**
  118. * @brief Return the number of unused efuse key blocks (0-6)
  119. */
  120. unsigned ets_efuse_count_unused_key_blocks(void);
  121. /**
  122. * @brief Calculate Reed-Solomon Encoding values for a block of efuse data.
  123. *
  124. * @param data Pointer to data buffer (length 32 bytes)
  125. * @param rs_values Pointer to write encoded data to (length 12 bytes)
  126. */
  127. void ets_efuse_rs_calculate(const void *data, void *rs_values);
  128. /**
  129. * @brief Read if download mode disabled from Efuse
  130. *
  131. * @return
  132. * - true for efuse disable download mode.
  133. * - false for efuse doesn't disable download mode.
  134. */
  135. bool ets_efuse_download_modes_disabled(void);
  136. /**
  137. * @brief Read if uart print control value from Efuse
  138. *
  139. * @return
  140. * - 0 for uart force print.
  141. * - 1 for uart print when GPIO8 is low when digital reset.
  142. * 2 for uart print when GPIO8 is high when digital reset.
  143. * 3 for uart force slient
  144. */
  145. uint32_t ets_efuse_get_uart_print_control(void);
  146. /**
  147. * @brief Read if security download modes enabled from Efuse
  148. *
  149. * @return
  150. * - true for efuse enable security download mode.
  151. * - false for efuse doesn't enable security download mode.
  152. */
  153. bool ets_efuse_security_download_modes_enabled(void);
  154. /**
  155. * @brief Return true if secure boot is enabled in EFuse
  156. */
  157. bool ets_efuse_secure_boot_enabled(void);
  158. /**
  159. * @brief Return true if secure boot aggressive revoke is enabled in EFuse
  160. */
  161. bool ets_efuse_secure_boot_aggressive_revoke_enabled(void);
  162. /**
  163. * @brief Return true if cache encryption (flash, etc) is enabled from boot via EFuse
  164. */
  165. bool ets_efuse_cache_encryption_enabled(void);
  166. /**
  167. * @brief Return true if EFuse indicates to send a flash resume command.
  168. */
  169. bool ets_efuse_force_send_resume(void);
  170. /**
  171. * @brief return the time in us ROM boot need wait flash to power on from Efuse
  172. *
  173. * @return
  174. * - uint32_t the time in us.
  175. */
  176. uint32_t ets_efuse_get_flash_delay_us(void);
  177. #define EFUSE_SPICONFIG_SPI_DEFAULTS 0
  178. #define EFUSE_SPICONFIG_HSPI_DEFAULTS 1
  179. #define EFUSE_SPICONFIG_RET_SPICLK_MASK 0x3f
  180. #define EFUSE_SPICONFIG_RET_SPICLK_SHIFT 0
  181. #define EFUSE_SPICONFIG_RET_SPICLK(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPICLK_SHIFT) & EFUSE_SPICONFIG_RET_SPICLK_MASK)
  182. #define EFUSE_SPICONFIG_RET_SPIQ_MASK 0x3f
  183. #define EFUSE_SPICONFIG_RET_SPIQ_SHIFT 6
  184. #define EFUSE_SPICONFIG_RET_SPIQ(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPIQ_SHIFT) & EFUSE_SPICONFIG_RET_SPIQ_MASK)
  185. #define EFUSE_SPICONFIG_RET_SPID_MASK 0x3f
  186. #define EFUSE_SPICONFIG_RET_SPID_SHIFT 12
  187. #define EFUSE_SPICONFIG_RET_SPID(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPID_SHIFT) & EFUSE_SPICONFIG_RET_SPID_MASK)
  188. #define EFUSE_SPICONFIG_RET_SPICS0_MASK 0x3f
  189. #define EFUSE_SPICONFIG_RET_SPICS0_SHIFT 18
  190. #define EFUSE_SPICONFIG_RET_SPICS0(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPICS0_SHIFT) & EFUSE_SPICONFIG_RET_SPICS0_MASK)
  191. #define EFUSE_SPICONFIG_RET_SPIHD_MASK 0x3f
  192. #define EFUSE_SPICONFIG_RET_SPIHD_SHIFT 24
  193. #define EFUSE_SPICONFIG_RET_SPIHD(ret) (((ret) >> EFUSE_SPICONFIG_RET_SPIHD_SHIFT) & EFUSE_SPICONFIG_RET_SPIHD_MASK)
  194. /**
  195. * @brief A crc8 algorithm used for MAC addresses in efuse
  196. *
  197. * @param unsigned char const *p : Pointer to original data.
  198. *
  199. * @param unsigned int len : Data length in byte.
  200. *
  201. * @return unsigned char: Crc value.
  202. */
  203. unsigned char esp_crc8(unsigned char const *p, unsigned int len);
  204. /**
  205. * @}
  206. */
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. #endif /* _ROM_EFUSE_H_ */