mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
121 lines
5.5 KiB
C
121 lines
5.5 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
#include "sdkconfig.h"
|
|
#include "esp_log.h"
|
|
#include "assert.h"
|
|
#include "esp_efuse_utility.h"
|
|
#include "soc/efuse_periph.h"
|
|
#include "esp_private/esp_clk.h"
|
|
#include "esp32c2/rom/efuse.h"
|
|
|
|
static const char *TAG = "efuse";
|
|
|
|
#ifdef CONFIG_EFUSE_VIRTUAL
|
|
extern uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK];
|
|
#endif // CONFIG_EFUSE_VIRTUAL
|
|
|
|
/*Range addresses to read blocks*/
|
|
const esp_efuse_range_addr_t range_read_addr_blocks[] = {
|
|
{EFUSE_RD_WR_DIS_REG, EFUSE_RD_REPEAT_DATA0_REG}, // range address of EFUSE_BLK0 (2 regs) REPEAT
|
|
{EFUSE_RD_BLK1_DATA0_REG, EFUSE_RD_BLK1_DATA2_REG}, // range address of EFUSE_BLK1 (3 regs) SYS_DATA_PART0
|
|
{EFUSE_RD_BLK2_DATA0_REG, EFUSE_RD_BLK2_DATA7_REG}, // range address of EFUSE_BLK2 (8 regs) SYS_DATA_PART_1
|
|
{EFUSE_RD_BLK3_DATA0_REG, EFUSE_RD_BLK3_DATA7_REG}, // range address of EFUSE_BLK3 (8 regs) KEY0
|
|
};
|
|
|
|
static uint32_t write_mass_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK] = { 0 };
|
|
|
|
/*Range addresses to write blocks (it is not real regs, it is buffer) */
|
|
const esp_efuse_range_addr_t range_write_addr_blocks[] = {
|
|
{(uint32_t) &write_mass_blocks[EFUSE_BLK0][0], (uint32_t) &write_mass_blocks[EFUSE_BLK0][1]},
|
|
{(uint32_t) &write_mass_blocks[EFUSE_BLK1][0], (uint32_t) &write_mass_blocks[EFUSE_BLK1][2]},
|
|
{(uint32_t) &write_mass_blocks[EFUSE_BLK2][0], (uint32_t) &write_mass_blocks[EFUSE_BLK2][7]},
|
|
{(uint32_t) &write_mass_blocks[EFUSE_BLK3][0], (uint32_t) &write_mass_blocks[EFUSE_BLK3][7]},
|
|
};
|
|
|
|
#ifndef CONFIG_EFUSE_VIRTUAL
|
|
// Update Efuse timing configuration
|
|
static esp_err_t esp_efuse_set_timing(void)
|
|
{
|
|
// no need to set special timing values
|
|
return ESP_OK;
|
|
}
|
|
#endif // ifndef CONFIG_EFUSE_VIRTUAL
|
|
|
|
// Efuse read operation: copies data from physical efuses to efuse read registers.
|
|
void esp_efuse_utility_clear_program_registers(void)
|
|
{
|
|
ets_efuse_read();
|
|
ets_efuse_clear_program_registers();
|
|
}
|
|
|
|
// Burn values written to the efuse write registers
|
|
void esp_efuse_utility_burn_chip(void)
|
|
{
|
|
#ifdef CONFIG_EFUSE_VIRTUAL
|
|
ESP_LOGW(TAG, "Virtual efuses enabled: Not really burning eFuses");
|
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
|
int subblock = 0;
|
|
for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
|
|
virt_blocks[num_block][subblock++] |= REG_READ(addr_wr_block);
|
|
}
|
|
}
|
|
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
|
|
esp_efuse_utility_write_efuses_to_flash();
|
|
#endif
|
|
#else
|
|
if (esp_efuse_set_timing() != ESP_OK) {
|
|
ESP_LOGE(TAG, "Efuse fields are not burnt");
|
|
} else {
|
|
// Permanently update values written to the efuse write registers
|
|
// It is necessary to process blocks in the order from MAX-> EFUSE_BLK0, because EFUSE_BLK0 has protection bits for other blocks.
|
|
for (int num_block = EFUSE_BLK_MAX - 1; num_block >= EFUSE_BLK0; num_block--) {
|
|
for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
|
|
if (REG_READ(addr_wr_block) != 0) {
|
|
if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
|
|
uint8_t block_rs[12];
|
|
ets_efuse_rs_calculate((void *)range_write_addr_blocks[num_block].start, block_rs);
|
|
memcpy((void *)EFUSE_PGM_CHECK_VALUE0_REG, block_rs, sizeof(block_rs));
|
|
}
|
|
int data_len = (range_write_addr_blocks[num_block].end - range_write_addr_blocks[num_block].start) + sizeof(uint32_t);
|
|
memcpy((void *)EFUSE_PGM_DATA0_REG, (void *)range_write_addr_blocks[num_block].start, data_len);
|
|
ets_efuse_program(num_block);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif // CONFIG_EFUSE_VIRTUAL
|
|
esp_efuse_utility_reset();
|
|
}
|
|
|
|
// After esp_efuse_write.. functions EFUSE_BLKx_WDATAx_REG were filled is not coded values.
|
|
// This function reads EFUSE_BLKx_WDATAx_REG registers, and checks possible to write these data with RS coding scheme.
|
|
// The RS coding scheme does not require data changes for the encoded data. esp32s2 has special registers for this.
|
|
// They will be filled during the burn operation.
|
|
esp_err_t esp_efuse_utility_apply_new_coding_scheme()
|
|
{
|
|
// start with EFUSE_BLK1. EFUSE_BLK0 - always uses EFUSE_CODING_SCHEME_NONE.
|
|
for (int num_block = EFUSE_BLK1; num_block < EFUSE_BLK_MAX; num_block++) {
|
|
if (esp_efuse_get_coding_scheme(num_block) == EFUSE_CODING_SCHEME_RS) {
|
|
for (uint32_t addr_wr_block = range_write_addr_blocks[num_block].start; addr_wr_block <= range_write_addr_blocks[num_block].end; addr_wr_block += 4) {
|
|
if (REG_READ(addr_wr_block)) {
|
|
int num_reg = 0;
|
|
for (uint32_t addr_rd_block = range_read_addr_blocks[num_block].start; addr_rd_block <= range_read_addr_blocks[num_block].end; addr_rd_block += 4, ++num_reg) {
|
|
if (esp_efuse_utility_read_reg(num_block, num_reg)) {
|
|
ESP_LOGE(TAG, "Bits are not empty. Write operation is forbidden.");
|
|
return ESP_ERR_CODING;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ESP_OK;
|
|
}
|