Skip to content

Commit d638267

Browse files
committed
Merge branch 'fix/fix_flash_encryption_esp32p4_v5.3' into 'release/v5.3'
fix(bootloader_support): Fix flash encryption for esp32p4 (v5.3) See merge request espressif/esp-idf!30921
2 parents 41ff5a2 + 3640c1e commit d638267

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

components/bootloader_support/src/flash_encryption/flash_encrypt.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#if SOC_KEY_MANAGER_SUPPORTED
2020
#include "hal/key_mgr_hal.h"
21+
#include "hal/mspi_timing_tuning_ll.h"
2122
#include "soc/keymng_reg.h"
2223
#endif
2324

@@ -217,8 +218,15 @@ static esp_err_t check_and_generate_encryption_keys(void)
217218
}
218219

219220
#if SOC_KEY_MANAGER_SUPPORTED
221+
#if CONFIG_IDF_TARGET_ESP32C5 && SOC_KEY_MANAGER_SUPPORTED
222+
// TODO: [ESP32C5] IDF-8622 find a more proper place for these codes
223+
REG_SET_BIT(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY_FLASH);
224+
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
225+
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
226+
#endif
220227
// Force Key Manager to use eFuse key for XTS-AES operation
221228
key_mgr_hal_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
229+
_mspi_timing_ll_reset_mspi();
222230
#endif
223231

224232
return ESP_OK;
@@ -263,13 +271,6 @@ esp_err_t esp_flash_encrypt_contents(void)
263271
esp_partition_info_t partition_table[ESP_PARTITION_TABLE_MAX_ENTRIES];
264272
int num_partitions;
265273

266-
#if CONFIG_IDF_TARGET_ESP32C5 && SOC_KEY_MANAGER_SUPPORTED
267-
// TODO: [ESP32C5] IDF-8622 find a more proper place for these codes
268-
REG_SET_BIT(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY_FLASH);
269-
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
270-
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
271-
#endif
272-
273274
#ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
274275
REG_WRITE(SENSITIVE_XTS_AES_KEY_UPDATE_REG, 1);
275276
#endif

components/hal/esp32p4/include/hal/mspi_timing_tuning_ll.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "soc/soc.h"
1919
#include "soc/iomux_mspi_pin_reg.h"
2020
#include "soc/iomux_mspi_pin_struct.h"
21+
#include "soc/hp_sys_clkrst_reg.h"
2122

2223
#ifdef __cplusplus
2324
extern "C" {
@@ -71,6 +72,20 @@ typedef enum {
7172
MSPI_LL_PIN_MAX,
7273
} mspi_ll_pin_t;
7374

75+
/**
76+
* Reset the MSPI clock
77+
*/
78+
__attribute__((always_inline))
79+
static inline void _mspi_timing_ll_reset_mspi(void)
80+
{
81+
REG_SET_BIT(HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_AXI);
82+
REG_CLR_BIT(HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_MSPI_AXI);
83+
}
84+
85+
/// use a macro to wrap the function, force the caller to use it in a critical section
86+
/// the critical section needs to declare the __DECLARE_RCC_RC_ATOMIC_ENV variable in advance
87+
#define mspi_timing_ll_reset_mspi(...) (void)__DECLARE_RCC_RC_ATOMIC_ENV; _mspi_timing_ll_reset_mspi(__VA_ARGS__)
88+
7489
/**
7590
* Set all MSPI DQS phase
7691
*

0 commit comments

Comments
 (0)