efuse_hal.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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. #define ESP_EFUSE_BLOCK_ERROR_BITS(error_reg, block) ((error_reg) & (0x08 << (4 * (block))))
  13. #define ESP_EFUSE_BLOCK_ERROR_NUM_BITS(error_reg, block) ((error_reg) & (0x07 << (4 * (block))))
  14. uint32_t efuse_hal_get_major_chip_version(void)
  15. {
  16. return efuse_ll_get_chip_wafer_version_major();
  17. }
  18. uint32_t efuse_hal_get_minor_chip_version(void)
  19. {
  20. return efuse_ll_get_chip_wafer_version_minor();
  21. }
  22. /******************* eFuse control functions *************************/
  23. void efuse_hal_set_timing(uint32_t apb_freq_hz)
  24. {
  25. (void) apb_freq_hz;
  26. efuse_ll_set_pwr_off_num(0x190);
  27. }
  28. void efuse_hal_read(void)
  29. {
  30. efuse_hal_set_timing(0);
  31. efuse_ll_set_conf_read_op_code();
  32. efuse_ll_set_read_cmd();
  33. while (efuse_ll_get_read_cmd() != 0) { }
  34. /*Due to a hardware error, we have to read READ_CMD again to make sure the efuse clock is normal*/
  35. while (efuse_ll_get_read_cmd() != 0) { }
  36. }
  37. void efuse_hal_clear_program_registers(void)
  38. {
  39. ets_efuse_clear_program_registers();
  40. }
  41. void efuse_hal_program(uint32_t block)
  42. {
  43. efuse_hal_set_timing(0);
  44. efuse_ll_set_conf_write_op_code();
  45. efuse_ll_set_pgm_cmd(block);
  46. while (efuse_ll_get_pgm_cmd() != 0) { }
  47. efuse_hal_clear_program_registers();
  48. efuse_hal_read();
  49. }
  50. void efuse_hal_rs_calculate(const void *data, void *rs_values)
  51. {
  52. ets_efuse_rs_calculate(data, rs_values);
  53. }
  54. /******************* eFuse control functions *************************/
  55. bool efuse_hal_is_coding_error_in_block(unsigned block)
  56. {
  57. if (block == 0) {
  58. for (unsigned i = 0; i < 5; i++) {
  59. if (REG_READ(EFUSE_RD_REPEAT_ERR0_REG + i * 4)) {
  60. return true;
  61. }
  62. }
  63. } else if (block <= 10) {
  64. // The order of error in these regs is different only for the C3 chip.
  65. // Fail bit (mask=0x8):
  66. // EFUSE_RD_RS_ERR0_REG: (hi) BLOCK7, BLOCK6, BLOCK5, BLOCK4, BLOCK3, BLOCK2, BLOCK1, ------ (low)
  67. // EFUSE_RD_RS_ERR1_REG: BLOCK9, BLOCK8
  68. // Error num bits (mask=0x7):
  69. // EFUSE_RD_RS_ERR0_REG: (hi) BLOCK8, BLOCK7, BLOCK6, BLOCK5, BLOCK4, BLOCK3, BLOCK2, BLOCK1 (low)
  70. // EFUSE_RD_RS_ERR1_REG: BLOCK10, BLOCK9
  71. // BLOCK10 is not presented in the error regs.
  72. uint32_t err_fail_reg = REG_READ(EFUSE_RD_RS_ERR0_REG + (block / 8) * 4);
  73. uint32_t err_num_reg = REG_READ(EFUSE_RD_RS_ERR0_REG + ((block - 1) / 8) * 4);
  74. return (ESP_EFUSE_BLOCK_ERROR_BITS(err_fail_reg, block % 8) != 0) || (ESP_EFUSE_BLOCK_ERROR_NUM_BITS(err_num_reg, (block - 1) % 8) != 0);
  75. }
  76. return false;
  77. }