esp_efuse_utility.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_efuse_utility.h"
  7. #include "soc/efuse_periph.h"
  8. #include "esp32/clk.h"
  9. #include "esp_log.h"
  10. #include "assert.h"
  11. #include "sdkconfig.h"
  12. #include <sys/param.h>
  13. static const char *TAG = "efuse";
  14. #ifdef CONFIG_EFUSE_VIRTUAL
  15. extern uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK];
  16. #endif // CONFIG_EFUSE_VIRTUAL
  17. /*Range addresses to read blocks*/
  18. const esp_efuse_range_addr_t range_read_addr_blocks[] = {
  19. {EFUSE_BLK0_RDATA0_REG, EFUSE_BLK0_RDATA6_REG}, // range address of EFUSE_BLK0
  20. {EFUSE_BLK1_RDATA0_REG, EFUSE_BLK1_RDATA7_REG}, // range address of EFUSE_BLK1
  21. {EFUSE_BLK2_RDATA0_REG, EFUSE_BLK2_RDATA7_REG}, // range address of EFUSE_BLK2
  22. {EFUSE_BLK3_RDATA0_REG, EFUSE_BLK3_RDATA7_REG} // range address of EFUSE_BLK3
  23. };
  24. /*Range addresses to write blocks*/
  25. const esp_efuse_range_addr_t range_write_addr_blocks[] = {
  26. {EFUSE_BLK0_WDATA0_REG, EFUSE_BLK0_WDATA6_REG}, // range address of EFUSE_BLK0
  27. {EFUSE_BLK1_WDATA0_REG, EFUSE_BLK1_WDATA7_REG}, // range address of EFUSE_BLK1
  28. {EFUSE_BLK2_WDATA0_REG, EFUSE_BLK2_WDATA7_REG}, // range address of EFUSE_BLK2
  29. {EFUSE_BLK3_WDATA0_REG, EFUSE_BLK3_WDATA7_REG} // range address of EFUSE_BLK3
  30. };
  31. #define EFUSE_CONF_WRITE 0x5A5A /* eFuse_pgm_op_ena, force no rd/wr disable. */
  32. #define EFUSE_CONF_READ 0x5AA5 /* eFuse_read_op_ena, release force. */
  33. #define EFUSE_CMD_PGM 0x02 /* Command to program. */
  34. #define EFUSE_CMD_READ 0x01 /* Command to read. */
  35. #ifndef CONFIG_EFUSE_VIRTUAL
  36. // Update Efuse timing configuration
  37. static esp_err_t esp_efuse_set_timing(void)
  38. {
  39. uint32_t apb_freq_mhz = esp_clk_apb_freq() / 1000000;
  40. uint32_t clk_sel0, clk_sel1, dac_clk_div;
  41. if (apb_freq_mhz <= 26) {
  42. clk_sel0 = 250;
  43. clk_sel1 = 255;
  44. dac_clk_div = 52;
  45. } else if (apb_freq_mhz <= 40) {
  46. clk_sel0 = 160;
  47. clk_sel1 = 255;
  48. dac_clk_div = 80;
  49. } else {
  50. clk_sel0 = 80;
  51. clk_sel1 = 128;
  52. dac_clk_div = 100;
  53. }
  54. REG_SET_FIELD(EFUSE_DAC_CONF_REG, EFUSE_DAC_CLK_DIV, dac_clk_div);
  55. REG_SET_FIELD(EFUSE_CLK_REG, EFUSE_CLK_SEL0, clk_sel0);
  56. REG_SET_FIELD(EFUSE_CLK_REG, EFUSE_CLK_SEL1, clk_sel1);
  57. return ESP_OK;
  58. }
  59. #endif // ifndef CONFIG_EFUSE_VIRTUAL
  60. // Efuse read operation: copies data from physical efuses to efuse read registers.
  61. void esp_efuse_utility_clear_program_registers(void)
  62. {
  63. REG_WRITE(EFUSE_CONF_REG, EFUSE_CONF_READ);
  64. }
  65. // Burn values written to the efuse write registers
  66. void esp_efuse_utility_burn_chip(void)
  67. {
  68. #ifdef CONFIG_EFUSE_VIRTUAL
  69. ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
  70. for (int num_block = EFUSE_BLK0; num_block < EFUSE_BLK_MAX; num_block++) {
  71. esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
  72. if (scheme == EFUSE_CODING_SCHEME_3_4) {
  73. uint8_t buf[COUNT_EFUSE_REG_PER_BLOCK * 4] = { 0 };
  74. int i = 0;
  75. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4, ++i) {
  76. *((uint32_t*)buf + i) = REG_READ(addr_wr_block);
  77. }
  78. int j = 0;
  79. uint32_t out_buf[COUNT_EFUSE_REG_PER_BLOCK] = { 0 };
  80. for (int k = 0; k < 4; ++k, ++j) {
  81. memcpy((uint8_t*)out_buf + j * 6, &buf[k * 8], 6);
  82. }
  83. for (int k = 0; k < COUNT_EFUSE_REG_PER_BLOCK; ++k) {
  84. REG_WRITE(range_write_addr_blocks[num_block].start + k * 4, out_buf[k]);
  85. }
  86. }
  87. int subblock = 0;
  88. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
  89. virt_blocks[num_block][subblock++] |= REG_READ(addr_wr_block);
  90. }
  91. }
  92. #ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  93. esp_efuse_utility_write_efuses_to_flash();
  94. #endif
  95. #else
  96. esp_efuse_set_timing();
  97. // Permanently update values written to the efuse write registers
  98. REG_WRITE(EFUSE_CONF_REG, EFUSE_CONF_WRITE);
  99. REG_WRITE(EFUSE_CMD_REG, EFUSE_CMD_PGM);
  100. while (REG_READ(EFUSE_CMD_REG) != 0) {};
  101. REG_WRITE(EFUSE_CONF_REG, EFUSE_CONF_READ);
  102. REG_WRITE(EFUSE_CMD_REG, EFUSE_CMD_READ);
  103. while (REG_READ(EFUSE_CMD_REG) != 0) {};
  104. #endif // CONFIG_EFUSE_VIRTUAL
  105. esp_efuse_utility_reset();
  106. }
  107. esp_err_t esp_efuse_utility_apply_34_encoding(const uint8_t *in_bytes, uint32_t *out_words, size_t in_bytes_len)
  108. {
  109. if (in_bytes == NULL || out_words == NULL || in_bytes_len % 6 != 0) {
  110. return ESP_ERR_INVALID_ARG;
  111. }
  112. while (in_bytes_len > 0) {
  113. uint8_t out[8];
  114. uint8_t xor = 0;
  115. uint8_t mul = 0;
  116. for (int i = 0; i < 6; i++) {
  117. xor ^= in_bytes[i];
  118. mul += (i + 1) * __builtin_popcount(in_bytes[i]);
  119. }
  120. memcpy(out, in_bytes, 6); // Data bytes
  121. out[6] = xor;
  122. out[7] = mul;
  123. memcpy(out_words, out, 8);
  124. in_bytes_len -= 6;
  125. in_bytes += 6;
  126. out_words += 2;
  127. }
  128. return ESP_OK;
  129. }
  130. static bool read_w_data_and_check_fill(esp_efuse_block_t num_block, uint32_t *buf_w_data)
  131. {
  132. bool blk_is_filled = false;
  133. int i = 0;
  134. for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4, ++i) {
  135. buf_w_data[i] = REG_READ(addr_wr_block);
  136. if (buf_w_data[i] != 0) {
  137. REG_WRITE(addr_wr_block, 0);
  138. blk_is_filled = true;
  139. }
  140. }
  141. return blk_is_filled;
  142. }
  143. static void read_r_data(esp_efuse_block_t num_block, uint32_t* buf_r_data)
  144. {
  145. int i = 0;
  146. for (uint32_t addr_rd_block = range_read_addr_blocks[num_block].start; addr_rd_block <= range_read_addr_blocks[num_block].end; addr_rd_block += 4, ++i) {
  147. buf_r_data[i] = esp_efuse_utility_read_reg(num_block, i);
  148. }
  149. }
  150. // After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
  151. // This function reads EFUSE_BLKx_WDATAx_REG registers, applies coding scheme and writes encoded values back to EFUSE_BLKx_WDATAx_REG.
  152. esp_err_t esp_efuse_utility_apply_new_coding_scheme()
  153. {
  154. uint8_t buf_w_data[COUNT_EFUSE_REG_PER_BLOCK * 4];
  155. uint8_t buf_r_data[COUNT_EFUSE_REG_PER_BLOCK * 4];
  156. uint32_t reg[COUNT_EFUSE_REG_PER_BLOCK];
  157. // start with EFUSE_BLK1. EFUSE_BLK0 - always uses EFUSE_CODING_SCHEME_NONE.
  158. for (int num_block = EFUSE_BLK1; num_block < EFUSE_BLK_MAX; num_block++) {
  159. esp_efuse_coding_scheme_t scheme = esp_efuse_get_coding_scheme(num_block);
  160. // check and apply a new coding scheme.
  161. if (scheme != EFUSE_CODING_SCHEME_NONE) {
  162. memset(buf_w_data, 0, sizeof(buf_w_data));
  163. memset((uint8_t*)reg, 0, sizeof(reg));
  164. if (read_w_data_and_check_fill(num_block, (uint32_t*)buf_w_data) == true) {
  165. read_r_data(num_block, (uint32_t*)buf_r_data);
  166. if (scheme == EFUSE_CODING_SCHEME_3_4) {
  167. if (*((uint32_t*)buf_w_data + 6) != 0 || *((uint32_t*)buf_w_data + 7) != 0) {
  168. return ESP_ERR_CODING;
  169. }
  170. for (int i = 0; i < 24; ++i) {
  171. if (buf_w_data[i] != 0) {
  172. int st_offset_buf = (i / 6) * 6;
  173. // check that place is free.
  174. for (int n = st_offset_buf; n < st_offset_buf + 6; ++n) {
  175. if (buf_r_data[n] != 0) {
  176. ESP_LOGE(TAG, "Bits are not empty. Write operation is forbidden.");
  177. return ESP_ERR_CODING;
  178. }
  179. }
  180. esp_err_t err = esp_efuse_utility_apply_34_encoding(&buf_w_data[st_offset_buf], reg, 6);
  181. if (err != ESP_OK) {
  182. return err;
  183. }
  184. int num_reg = (st_offset_buf / 6) * 2;
  185. for (int r = 0; r < 2; r++) {
  186. REG_WRITE(range_write_addr_blocks[num_block].start + (num_reg + r) * 4, reg[r]);
  187. }
  188. i = st_offset_buf + 5;
  189. }
  190. }
  191. } else if (scheme == EFUSE_CODING_SCHEME_REPEAT) {
  192. uint32_t* buf_32 = (uint32_t*)buf_w_data;
  193. for (int i = 4; i < 8; ++i) {
  194. if (*(buf_32 + i) != 0) {
  195. return ESP_ERR_CODING;
  196. }
  197. }
  198. for (int i = 0; i < 4; ++i) {
  199. if (buf_32[i] != 0) {
  200. REG_WRITE(range_write_addr_blocks[num_block].start + i * 4, buf_32[i]);
  201. REG_WRITE(range_write_addr_blocks[num_block].start + (i + 4) * 4, buf_32[i]);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. return ESP_OK;
  209. }