diff --git a/components/hal/esp32s31/include/hal/lp_clkrst_ll.h b/components/hal/esp32s31/include/hal/lp_clkrst_ll.h new file mode 100644 index 00000000000..14c8e6e5730 --- /dev/null +++ b/components/hal/esp32s31/include/hal/lp_clkrst_ll.h @@ -0,0 +1,31 @@ +/* + * SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include "hal/rng_ll.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * ESP32-S31 has no LP_CLKRST RNG clock gate. Reuse this hook so + * hw_random.c can enable the full RNG module without chip-specific ifdefs. + */ +static inline void _lp_clkrst_ll_enable_rng_clock(bool en) +{ + if (en) { + rng_ll_enable(); + } else { + rng_ll_disable(); + } +} + +#ifdef __cplusplus +} +#endif diff --git a/components/hal/esp32s31/include/hal/rng_ll.h b/components/hal/esp32s31/include/hal/rng_ll.h index 11775f8c325..5d4513b97ac 100644 --- a/components/hal/esp32s31/include/hal/rng_ll.h +++ b/components/hal/esp32s31/include/hal/rng_ll.h @@ -6,9 +6,12 @@ #pragma once +#include #include #include "soc/soc.h" #include "soc/trng_reg.h" +#include "soc/trng_struct.h" +#include "soc/lp_peri_clkrst_struct.h" #ifdef __cplusplus extern "C" { @@ -19,6 +22,80 @@ static inline uint32_t rng_ll_read_data(void) return REG_READ(TRNG_CRC_SYNC_DATA_REG); } +/** + * @brief Enable LP RNG bus clock + */ +static inline void rng_ll_enable_bus_clock(bool enable) +{ + LP_PERI_CLKRST.rng_ctrl.lp_rng_clk_en = enable; +} + +/** + * @brief Reset LP RNG bus clock + */ +static inline void rng_ll_reset(void) +{ + LP_PERI_CLKRST.rng_ctrl.lp_rng_rst_en = 1; + LP_PERI_CLKRST.rng_ctrl.lp_rng_rst_en = 0; +} + +/** + * @brief Enable or disable RNG module clock + * + * @param enable true to enable, false to disable + */ +static inline void rng_ll_enable_clock(bool enable) +{ + LP_TRNG.date.clk_en = enable; +} + +/** + * @brief Enable or disable RNG sampling + * + * @param enable true to enable, false to disable + */ +static inline void rng_ll_enable_sample(bool enable) +{ + LP_TRNG.conf.sample_enable = enable; +} + +/** + * @brief Enable or disable CRC RNG source + * + * @param enable true to enable CRC RNG, false to disable + */ +static inline void rng_ll_enable_noise_crc(bool enable) +{ + LP_TRNG.conf.noise_crc_en = enable; +} + +/** + * @brief Enable RNG module + * + * TODO: unify in rng_hal.c + */ +static inline void rng_ll_enable(void) +{ + rng_ll_enable_bus_clock(true); + rng_ll_enable_clock(true); + rng_ll_reset(); + rng_ll_enable_sample(true); + rng_ll_enable_noise_crc(true); +} + +/** + * @brief Disable RNG module + * + * TODO: unify in rng_hal.c + */ +static inline void rng_ll_disable(void) +{ + rng_ll_enable_noise_crc(false); + rng_ll_enable_sample(false); + rng_ll_enable_clock(false); + rng_ll_enable_bus_clock(false); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s31/include/hal/trng_ll.h b/components/hal/esp32s31/include/hal/trng_ll.h index 2925a857d3d..508d30f6c94 100644 --- a/components/hal/esp32s31/include/hal/trng_ll.h +++ b/components/hal/esp32s31/include/hal/trng_ll.h @@ -80,8 +80,8 @@ static inline uint32_t trng_ll_read_data(void) static inline void trng_ll_enable(void) { trng_ll_enable_bus_clock(true); - trng_ll_reset(); trng_ll_enable_clock(true); + trng_ll_reset(); trng_ll_enable_sample(true); trng_ll_enable_noise_crc(true); } @@ -93,8 +93,8 @@ static inline void trng_ll_enable(void) */ static inline void trng_ll_disable(void) { - trng_ll_enable_sample(false); trng_ll_enable_noise_crc(false); + trng_ll_enable_sample(false); trng_ll_enable_clock(false); trng_ll_enable_bus_clock(false); } diff --git a/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in b/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in index 30f72a7611e..54b1ed8b9c2 100644 --- a/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32s31/include/soc/Kconfig.soc_caps.in @@ -1391,6 +1391,10 @@ config SOC_MODEM_CLOCK_WIFI_BB_80X1_AS_APB bool default y +config SOC_RNG_CLOCK_IS_INDEPENDENT + bool + default y + config SOC_CLK_RC_FAST_SUPPORT_CALIBRATION bool default y diff --git a/components/soc/esp32s31/include/soc/soc_caps.h b/components/soc/esp32s31/include/soc/soc_caps.h index cd54063c888..88eb07f72c5 100644 --- a/components/soc/esp32s31/include/soc/soc_caps.h +++ b/components/soc/esp32s31/include/soc/soc_caps.h @@ -519,6 +519,7 @@ #define SOC_MODEM_APB_CLOCK_IS_INDEPENDENT (1) #define SOC_MODEM_CLOCK_SOC_PLL_SOURCE_CG_SUPPORTED (1) #define SOC_MODEM_CLOCK_WIFI_BB_80X1_AS_APB (1) +#define SOC_RNG_CLOCK_IS_INDEPENDENT (1) #define SOC_CLK_RC_FAST_SUPPORT_CALIBRATION (1)