efuse_hal.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "esp_attr.h"
  8. #include <sys/param.h>
  9. #include "soc/soc_caps.h"
  10. #include "hal/assert.h"
  11. #include "hal/efuse_hal.h"
  12. #include "hal/efuse_ll.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. return efuse_ll_get_chip_wafer_version_major();
  18. }
  19. IRAM_ATTR uint32_t efuse_hal_get_minor_chip_version(void)
  20. {
  21. return efuse_ll_get_chip_wafer_version_minor();
  22. }
  23. /******************* eFuse control functions *************************/
  24. void efuse_hal_set_timing(uint32_t apb_freq_hz)
  25. {
  26. (void) apb_freq_hz;
  27. /* keep timing settings by default */
  28. }
  29. void efuse_hal_read(void)
  30. {
  31. efuse_hal_set_timing(0);
  32. efuse_ll_set_conf_read_op_code();
  33. efuse_ll_set_read_cmd();
  34. while (efuse_ll_get_read_cmd() != 0) { }
  35. /*Due to a hardware error, we have to read READ_CMD again to make sure the efuse clock is normal*/
  36. while (efuse_ll_get_read_cmd() != 0) { }
  37. }
  38. void efuse_hal_clear_program_registers(void)
  39. {
  40. ets_efuse_clear_program_registers();
  41. }
  42. void efuse_hal_program(uint32_t block)
  43. {
  44. efuse_hal_set_timing(0);
  45. efuse_ll_set_conf_write_op_code();
  46. efuse_ll_set_pgm_cmd(block);
  47. while (efuse_ll_get_pgm_cmd() != 0) { }
  48. efuse_hal_clear_program_registers();
  49. efuse_hal_read();
  50. }
  51. void efuse_hal_rs_calculate(const void *data, void *rs_values)
  52. {
  53. ets_efuse_rs_calculate(data, rs_values);
  54. }
  55. /******************* eFuse control functions *************************/
  56. bool efuse_hal_is_coding_error_in_block(unsigned block)
  57. {
  58. if (block == 0) {
  59. for (unsigned i = 0; i < 5; i++) {
  60. if (REG_READ(EFUSE_RD_REPEAT_ERR0_REG + i * 4)) {
  61. return true;
  62. }
  63. }
  64. } else if (block <= 10) {
  65. // EFUSE_RD_RS_ERR0_REG: (hi) BLOCK8, BLOCK7, BLOCK6, BLOCK5, BLOCK4, BLOCK3, BLOCK2, BLOCK1 (low)
  66. // EFUSE_RD_RS_ERR1_REG: BLOCK10, BLOCK9
  67. block--;
  68. uint32_t error_reg = REG_READ(EFUSE_RD_RS_ERR0_REG + (block / 8) * 4);
  69. return ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block % 8) != 0;
  70. }
  71. return false;
  72. }