Skip to content

Commit b9da848

Browse files
erwangocarlescufi
authored andcommitted
drivers: entropy: stm32: Configure clock source using clock_control driver
RNG domain source clock is now configured via call to clock_control driver. Besides, add static checks to verify domain clock configuration is correct: - If HSI48 is used because it is default domain clock config, it should be enabled - If no HSI48 is available, a specific domain clock should be set - In L0 case, PLL could be used as domain clock only at a specific freq. Signed-off-by: Erwan Gouriou <[email protected]>
1 parent 5cbff34 commit b9da848

File tree

1 file changed

+26
-65
lines changed

1 file changed

+26
-65
lines changed

drivers/entropy/entropy_stm32.c

Lines changed: 26 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -578,73 +578,26 @@ static int entropy_stm32_rng_init(const struct device *dev)
578578
__ASSERT_NO_MSG(dev_data != NULL);
579579
__ASSERT_NO_MSG(dev_cfg != NULL);
580580

581-
#if CONFIG_SOC_SERIES_STM32L4X
582-
/* Configure PLLSA11 to enable 48M domain */
583-
LL_RCC_PLLSAI1_ConfigDomain_48M(LL_RCC_PLLSOURCE_MSI,
584-
LL_RCC_PLLM_DIV_1,
585-
24, LL_RCC_PLLSAI1Q_DIV_2);
586-
587-
/* Enable PLLSA1 */
588-
LL_RCC_PLLSAI1_Enable();
589-
590-
/* Enable PLLSAI1 output mapped on 48MHz domain clock */
591-
LL_RCC_PLLSAI1_EnableDomain_48M();
592-
593-
/* Wait for PLLSA1 ready flag */
594-
while (LL_RCC_PLLSAI1_IsReady() != 1) {
595-
}
596-
597-
/* Write the peripherals independent clock configuration register :
598-
* choose PLLSAI1 source as the 48 MHz clock is needed for the RNG
599-
* Linear Feedback Shift Register
600-
*/
601-
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_PLLSAI1);
602-
#elif CONFIG_SOC_SERIES_STM32WLX || CONFIG_SOC_SERIES_STM32G0X
603-
LL_RCC_PLL_EnableDomain_RNG();
604-
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_PLL);
605-
#elif defined(RCC_CR2_HSI48ON) || defined(RCC_CR_HSI48ON) \
606-
|| defined(RCC_CRRCR_HSI48ON)
607-
#if !STM32_HSI48_ENABLED
608-
/* Deprecated: enable HSI48 using device tree */
609-
#warning RNG requires HSI48 clock to be enabled using device tree
610-
611-
/* Keeping this sequence for legacy: */
612-
#if CONFIG_SOC_SERIES_STM32L0X
613-
/* We need SYSCFG to control VREFINT, so make sure it is clocked */
614-
if (!LL_APB2_GRP1_IsEnabledClock(LL_APB2_GRP1_PERIPH_SYSCFG)) {
615-
return -EINVAL;
616-
}
617-
/* HSI48 requires VREFINT (see RM0376 section 7.2.4). */
618-
LL_SYSCFG_VREFINT_EnableHSI48();
619-
#endif /* CONFIG_SOC_SERIES_STM32L0X */
620-
621-
z_stm32_hsem_lock(CFG_HW_CLK48_CONFIG_SEMID, HSEM_LOCK_DEFAULT_RETRY);
622-
/* Use the HSI48 for the RNG */
623-
LL_RCC_HSI48_Enable();
624-
while (!LL_RCC_HSI48_IsReady()) {
625-
/* Wait for HSI48 to become ready */
626-
}
627-
#else /* !STM32_HSI48_ENABLED */
628-
/* HSI48 is enabled by the DTS : lock the HSI48 clock for RNG use */
629-
z_stm32_hsem_lock(CFG_HW_CLK48_CONFIG_SEMID, HSEM_LOCK_DEFAULT_RETRY);
630-
#endif /* !STM32_HSI48_ENABLED */
631-
632-
/* HSI48 Clock is enabled through the DTS: set as RNG clock source */
633-
#if defined(CONFIG_SOC_SERIES_STM32WBX)
634-
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_CLK48);
635-
LL_RCC_SetCLK48ClockSource(LL_RCC_CLK48_CLKSOURCE_HSI48);
636-
637-
/* Don't unlock the HSEM to prevent M0 core
638-
* to disable HSI48 clock used for RNG.
639-
*/
640-
#else
641-
LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_HSI48);
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
642593

643-
/* Unlock the HSEM if it is not STM32WB */
644-
z_stm32_hsem_unlock(CFG_HW_CLK48_CONFIG_SEMID);
645-
#endif /* CONFIG_SOC_SERIES_STM32WBX */
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
646599

647-
#endif /* CONFIG_SOC_SERIES_STM32L4X */
600+
#endif /* (DT_INST_NUM_CLOCKS(0) == 1) */
648601

649602
dev_data->clock = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
650603

@@ -656,6 +609,14 @@ static int entropy_stm32_rng_init(const struct device *dev)
656609
(clock_control_subsys_t *)&dev_cfg->pclken[0]);
657610
__ASSERT_NO_MSG(res == 0);
658611

612+
/* Configure domain clock if any */
613+
if (DT_INST_NUM_CLOCKS(0) > 1) {
614+
res = clock_control_configure(dev_data->clock,
615+
(clock_control_subsys_t *)&dev_cfg->pclken[1],
616+
NULL);
617+
__ASSERT(res == 0, "Could not select RNG domain clock");
618+
}
619+
659620
/* Locking semaphore initialized to 1 (unlocked) */
660621
k_sem_init(&dev_data->sem_lock, 1, 1);
661622

0 commit comments

Comments
 (0)