From 9ff2f9ab2ff3b353d5a9d53a87c4db3252bdec76 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 20 Jun 2024 10:41:20 +0800 Subject: [PATCH] fix(hal): correct the power up sequence for MPI/ECC peripherals in ESP32-C5 --- components/hal/esp32c2/include/hal/ecc_ll.h | 3 +++ components/hal/esp32c6/include/hal/ecc_ll.h | 16 +++++++++++++++- components/hal/esp32h2/include/hal/ecc_ll.h | 17 ++++++++++++++++- components/hal/test_apps/ecc/main/test_ecc.c | 4 +++- components/mbedtls/port/ecc/esp_ecc.c | 3 +++ components/mbedtls/port/ecdsa/ecdsa_alt.c | 3 +++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/components/hal/esp32c2/include/hal/ecc_ll.h b/components/hal/esp32c2/include/hal/ecc_ll.h index 8a7c57a04a..214dd8e676 100644 --- a/components/hal/esp32c2/include/hal/ecc_ll.h +++ b/components/hal/esp32c2/include/hal/ecc_ll.h @@ -20,6 +20,9 @@ typedef enum { ECC_PARAM_K, } ecc_ll_param_t; +static inline void ecc_ll_power_up(void) {} +static inline void ecc_ll_power_down(void) {} + static inline void ecc_ll_enable_interrupt(void) { REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 1); diff --git a/components/hal/esp32c6/include/hal/ecc_ll.h b/components/hal/esp32c6/include/hal/ecc_ll.h index 9df9655e5d..8e58e7b05b 100644 --- a/components/hal/esp32c6/include/hal/ecc_ll.h +++ b/components/hal/esp32c6/include/hal/ecc_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,8 @@ #include #include "hal/assert.h" #include "soc/ecc_mult_reg.h" +#include "soc/pcr_struct.h" +#include "soc/pcr_reg.h" #ifdef __cplusplus extern "C" { @@ -20,6 +22,18 @@ typedef enum { ECC_PARAM_K, } ecc_ll_param_t; +static inline void ecc_ll_power_up(void) +{ + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD); + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PD); +} + +static inline void ecc_ll_power_down(void) +{ + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PU); + REG_SET_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD); +} + static inline void ecc_ll_enable_interrupt(void) { REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 1); diff --git a/components/hal/esp32h2/include/hal/ecc_ll.h b/components/hal/esp32h2/include/hal/ecc_ll.h index 967d9857ef..6b5c0be4c4 100644 --- a/components/hal/esp32h2/include/hal/ecc_ll.h +++ b/components/hal/esp32h2/include/hal/ecc_ll.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -8,7 +8,10 @@ #include #include #include "hal/assert.h" +#include "hal/ecc_types.h" #include "soc/ecc_mult_reg.h" +#include "soc/pcr_struct.h" +#include "soc/pcr_reg.h" #ifdef __cplusplus extern "C" { @@ -23,6 +26,18 @@ typedef enum { ECC_PARAM_QZ, } ecc_ll_param_t; +static inline void ecc_ll_power_up(void) +{ + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD); + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PD); +} + +static inline void ecc_ll_power_down(void) +{ + REG_CLR_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_FORCE_PU); + REG_SET_BIT(PCR_ECC_PD_CTRL_REG, PCR_ECC_MEM_PD); +} + static inline void ecc_ll_enable_interrupt(void) { REG_SET_FIELD(ECC_MULT_INT_ENA_REG, ECC_MULT_CALC_DONE_INT_ENA, 1); diff --git a/components/hal/test_apps/ecc/main/test_ecc.c b/components/hal/test_apps/ecc/main/test_ecc.c index a42ca80fe4..9b8997b107 100644 --- a/components/hal/test_apps/ecc/main/test_ecc.c +++ b/components/hal/test_apps/ecc/main/test_ecc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: CC0-1.0 */ @@ -12,6 +12,7 @@ #include "test_params.h" #include "soc/soc_caps.h" #include "hal/ecc_hal.h" +#include "hal/ecc_ll.h" #include "hal/clk_gate_ll.h" #include "unity.h" @@ -43,6 +44,7 @@ static void ecc_be_to_le(const uint8_t* be_point, uint8_t *le_point, uint8_t len static void ecc_enable_and_reset(void) { periph_ll_enable_clk_clear_rst(PERIPH_ECC_MODULE); + ecc_ll_power_up(); } #if SOC_ECC_SUPPORT_POINT_MULT diff --git a/components/mbedtls/port/ecc/esp_ecc.c b/components/mbedtls/port/ecc/esp_ecc.c index 939f0070ff..ecd87a2567 100644 --- a/components/mbedtls/port/ecc/esp_ecc.c +++ b/components/mbedtls/port/ecc/esp_ecc.c @@ -11,17 +11,20 @@ #include "esp_private/periph_ctrl.h" #include "ecc_impl.h" #include "hal/ecc_hal.h" +#include "hal/ecc_ll.h" static void esp_ecc_acquire_hardware(void) { esp_crypto_ecc_lock_acquire(); periph_module_enable(PERIPH_ECC_MODULE); + ecc_ll_power_up(); } static void esp_ecc_release_hardware(void) { periph_module_disable(PERIPH_ECC_MODULE); + ecc_ll_power_down(); esp_crypto_ecc_lock_release(); } diff --git a/components/mbedtls/port/ecdsa/ecdsa_alt.c b/components/mbedtls/port/ecdsa/ecdsa_alt.c index 8b0e0bb025..25048afc82 100644 --- a/components/mbedtls/port/ecdsa/ecdsa_alt.c +++ b/components/mbedtls/port/ecdsa/ecdsa_alt.c @@ -14,6 +14,7 @@ #include "mbedtls/platform_util.h" #include "esp_private/periph_ctrl.h" #include "ecdsa/ecdsa_alt.h" +#include "hal/ecc_ll.h" #define ECDSA_KEY_MAGIC (short) 0xECD5A #define ECDSA_SHA_LEN 32 @@ -26,11 +27,13 @@ static void esp_ecdsa_acquire_hardware(void) esp_crypto_ecdsa_lock_acquire(); periph_module_enable(PERIPH_ECDSA_MODULE); + ecc_ll_power_up(); } static void esp_ecdsa_release_hardware(void) { periph_module_disable(PERIPH_ECDSA_MODULE); + ecc_ll_power_down(); esp_crypto_ecdsa_lock_release(); }