efuse_hal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "sdkconfig.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 "esp_attr.h"
  13. #define ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block) ((error_reg) & (0x08 << (4 * (block))))
  14. #define ESP_EFUSE_BLOCK_ERROR_NUM_BITS(error_reg, block) ((error_reg) & (0x07 << (4 * (block))))
  15. IRAM_ATTR uint32_t efuse_hal_get_major_chip_version(void)
  16. {
  17. #ifdef CONFIG_ESP_REV_NEW_CHIP_TEST
  18. return CONFIG_ESP_REV_MIN_FULL / 100;
  19. #else
  20. return efuse_ll_get_chip_wafer_version_major();
  21. #endif
  22. }
  23. IRAM_ATTR uint32_t efuse_hal_get_minor_chip_version(void)
  24. {
  25. #ifdef CONFIG_ESP_REV_NEW_CHIP_TEST
  26. return CONFIG_ESP_REV_MIN_FULL % 100;
  27. #else
  28. return efuse_ll_get_chip_wafer_version_minor();
  29. #endif
  30. }
  31. /******************* eFuse control functions *************************/
  32. void efuse_hal_set_timing(uint32_t apb_freq_hz)
  33. {
  34. (void) apb_freq_hz;
  35. efuse_ll_set_dac_num(0xFF);
  36. efuse_ll_set_dac_clk_div(0x28);
  37. efuse_ll_set_pwr_on_num(0x3000);
  38. efuse_ll_set_pwr_off_num(0x190);
  39. }
  40. void efuse_hal_read(void)
  41. {
  42. efuse_hal_set_timing(0);
  43. efuse_ll_set_conf_read_op_code();
  44. efuse_ll_set_read_cmd();
  45. while (efuse_ll_get_read_cmd() != 0) { }
  46. /*Due to a hardware error, we have to read READ_CMD again to make sure the efuse clock is normal*/
  47. while (efuse_ll_get_read_cmd() != 0) { }
  48. }
  49. void efuse_hal_clear_program_registers(void)
  50. {
  51. ets_efuse_clear_program_registers();
  52. }
  53. void efuse_hal_program(uint32_t block)
  54. {
  55. efuse_hal_set_timing(0);
  56. efuse_ll_set_conf_write_op_code();
  57. efuse_ll_set_pgm_cmd(block);
  58. while (efuse_ll_get_pgm_cmd() != 0) { }
  59. efuse_hal_clear_program_registers();
  60. efuse_hal_read();
  61. }
  62. void efuse_hal_rs_calculate(const void *data, void *rs_values)
  63. {
  64. ets_efuse_rs_calculate(data, rs_values);
  65. }
  66. /******************* eFuse control functions *************************/
  67. bool efuse_hal_is_coding_error_in_block(unsigned block)
  68. {
  69. if (block == 0) {
  70. for (unsigned i = 0; i < 5; i++) {
  71. if (REG_READ(EFUSE_RD_REPEAT_ERR0_REG + i * 4)) {
  72. return true;
  73. }
  74. }
  75. } else if (block <= 10) {
  76. // EFUSE_RD_RS_ERR0_REG: (hi) BLOCK8, BLOCK7, BLOCK6, BLOCK5, BLOCK4, BLOCK3, BLOCK2, BLOCK1 (low)
  77. // EFUSE_RD_RS_ERR1_REG: BLOCK10, BLOCK9
  78. block--;
  79. uint32_t error_reg = REG_READ(EFUSE_RD_RS_ERR0_REG + (block / 8) * 4);
  80. return ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block % 8) != 0;
  81. }
  82. return false;
  83. }