Skip to content

Commit 8725edc

Browse files
erwangocarlescufi
authored andcommitted
drivers: entropy: stm32: Check clock config at runtime
RNG clock configuration constraints differ between each series. Rather than providing complex build time code to verify RNG clock configuration is correct, take advantage of CECS bit (Clock error current status) to assess clock configuration. This check is implemented under a specific ENTROPY_STM32_CLK_CHECK Kconfig option. This allows user to disable this feature in specific conditions: - CED bit disabled in application (in which case CECS status is not valid) - Clock configuration is deemed as correct by user. Note that RNG number are always generated, whatever the clock status. Signed-off-by: Erwan Gouriou <[email protected]>
1 parent eb61bd5 commit 8725edc

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

drivers/entropy/Kconfig.stm32

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,15 @@ config ENTROPY_STM32_ISR_THRESHOLD
5353
buffer goes below this number hardware entropy generation will be
5454
started.
5555

56+
config ENTROPY_STM32_CLK_CHECK
57+
bool "Runtime clock configuration check"
58+
default y
59+
help
60+
Enables a check on RNG clock configuration. Correct clock
61+
configuration depends on STM32 series. Check reference manual if an
62+
error is reported.
63+
This check assumes CED (Clock Error Detected) bit is enabled (when
64+
available, CED is enabeld by default). Disable this check if CED is
65+
disabled.
66+
5667
endif # ENTROPY_STM32_RNG

drivers/entropy/entropy_stm32.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ static int random_byte_get(void)
233233
unsigned int key;
234234
RNG_TypeDef *rng = entropy_stm32_rng_data.rng;
235235

236+
if (IS_ENABLED(CONFIG_ENTROPY_STM32_CLK_CHECK)) {
237+
__ASSERT(LL_RNG_IsActiveFlag_CECS(rng) == 0,
238+
"Clock configuration error. See reference manual");
239+
}
240+
236241
key = irq_lock();
237242

238243
if (LL_RNG_IsActiveFlag_SEIS(rng) && (recover_seed_error(rng) < 0)) {
@@ -578,27 +583,6 @@ static int entropy_stm32_rng_init(const struct device *dev)
578583
__ASSERT_NO_MSG(dev_data != NULL);
579584
__ASSERT_NO_MSG(dev_cfg != NULL);
580585

581-
#if (DT_INST_NUM_CLOCKS(0) == 1)
582-
/* No domain clock selected, let's check that the configuration is correct */
583-
584-
#if defined(CONFIG_SOC_SERIES_STM32L0X) && \
585-
(CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC * STM32_PLL_MULTIPLIER) != MHZ(96)
586-
/* PLL used as RNG clock source (default), but its frequency doesn't fit */
587-
/* Fix PLL freq or select HSI48 as RNG clock source */
588-
#warning PLL clock not properly configured to be used as RNG clock. Configure another clock.
589-
#elif !DT_NODE_HAS_COMPAT(DT_NODELABEL(clk_hsi48), fixed_clock)
590-
/* No HSI48 available, a specific RNG domain clock has to be selected */
591-
#warning RNG domain clock not configured
592-
#endif
593-
594-
#if DT_NODE_HAS_COMPAT(DT_NODELABEL(clk_hsi48), fixed_clock) && !STM32_HSI48_ENABLED
595-
/* On these series, HSI48 is available and set by default as RNG clock source */
596-
/* HSI48 clock not enabled */
597-
#warning HSI48 clock should be enabled or other domain clock selected
598-
#endif
599-
600-
#endif /* (DT_INST_NUM_CLOCKS(0) == 1) */
601-
602586
dev_data->clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
603587

604588
if (!device_is_ready(dev_data->clock)) {

0 commit comments

Comments
 (0)