efuse_hal.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include <sys/param.h>
  8. #include "soc/soc_caps.h"
  9. #include "hal/assert.h"
  10. #include "hal/efuse_hal.h"
  11. #include "hal/efuse_ll.h"
  12. #include "esp32s3/rom/efuse.h"
  13. #include "esp_attr.h"
  14. #define ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block) ((error_reg) & (0x0F << (4 * (block))))
  15. //The wafer_major and MSB of wafer_minor fields was allocated to other purposes when block version is v1.1.
  16. //Luckily only chip v0.0 have this kind of block version and efuse usage.
  17. //This workaround fixes the issue.
  18. __attribute__((always_inline))
  19. static inline bool is_eco0(uint32_t minor_raw)
  20. {
  21. return ((minor_raw & 0x7) == 0 &&
  22. efuse_ll_get_blk_version_major() == 1 && efuse_ll_get_blk_version_minor() == 1);
  23. }
  24. IRAM_ATTR uint32_t efuse_hal_get_major_chip_version(void)
  25. {
  26. uint32_t minor_raw = efuse_ll_get_chip_wafer_version_minor();
  27. if (is_eco0(minor_raw)) {
  28. return 0;
  29. }
  30. return efuse_ll_get_chip_wafer_version_major();
  31. }
  32. IRAM_ATTR uint32_t efuse_hal_get_minor_chip_version(void)
  33. {
  34. uint32_t minor_raw = efuse_ll_get_chip_wafer_version_minor();
  35. if (is_eco0(minor_raw)) {
  36. return 0;
  37. }
  38. return minor_raw;
  39. }
  40. /******************* eFuse control functions *************************/
  41. void efuse_hal_set_timing(uint32_t apb_freq_hz)
  42. {
  43. (void) apb_freq_hz;
  44. efuse_ll_set_dac_num(0xFF);
  45. efuse_ll_set_dac_clk_div(0x28);
  46. efuse_ll_set_pwr_on_num(0x3000);
  47. efuse_ll_set_pwr_off_num(0x190);
  48. }
  49. void efuse_hal_read(void)
  50. {
  51. efuse_hal_set_timing(0);
  52. efuse_ll_set_conf_read_op_code();
  53. efuse_ll_set_read_cmd();
  54. while (efuse_ll_get_read_cmd() != 0) { }
  55. /*Due to a hardware error, we have to read READ_CMD again to make sure the efuse clock is normal*/
  56. while (efuse_ll_get_read_cmd() != 0) { }
  57. }
  58. void efuse_hal_clear_program_registers(void)
  59. {
  60. ets_efuse_clear_program_registers();
  61. }
  62. void efuse_hal_program(uint32_t block)
  63. {
  64. efuse_hal_set_timing(0);
  65. efuse_ll_set_conf_write_op_code();
  66. efuse_ll_set_pgm_cmd(block);
  67. while (efuse_ll_get_pgm_cmd() != 0) { }
  68. efuse_hal_clear_program_registers();
  69. efuse_hal_read();
  70. }
  71. void efuse_hal_rs_calculate(const void *data, void *rs_values)
  72. {
  73. ets_efuse_rs_calculate(data, rs_values);
  74. }
  75. /******************* eFuse control functions *************************/
  76. bool efuse_hal_is_coding_error_in_block(unsigned block)
  77. {
  78. if (block == 0) {
  79. for (unsigned i = 0; i < 5; i++) {
  80. if (REG_READ(EFUSE_RD_REPEAT_ERR0_REG + i * 4)) {
  81. return true;
  82. }
  83. }
  84. } else if (block <= 10) {
  85. // EFUSE_RD_RS_ERR0_REG: (hi) BLOCK8, BLOCK7, BLOCK6, BLOCK5, BLOCK4, BLOCK3, BLOCK2, BLOCK1 (low)
  86. // EFUSE_RD_RS_ERR1_REG: BLOCK10, BLOCK9
  87. block--;
  88. uint32_t error_reg = REG_READ(EFUSE_RD_RS_ERR0_REG + (block / 8) * 4);
  89. return ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block % 8) != 0;
  90. }
  91. return false;
  92. }