efuse_hal.c 2.8 KB

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