Skip to content

Commit 8397d64

Browse files
Mathieu Choplainkartben
authored andcommitted
drivers: entropy: stm32: add support for STM32WB09 TRNG
Add support for the STM32WB09-specific TRNG IP in STM32 entropy driver. Signed-off-by: Mathieu Choplain <[email protected]>
1 parent 97c8e5b commit 8397d64

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

drivers/entropy/entropy_stm32.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ BUILD_ASSERT((CONFIG_ENTROPY_STM32_THR_POOL_SIZE &
7777
(CONFIG_ENTROPY_STM32_THR_POOL_SIZE - 1)) == 0,
7878
"The CONFIG_ENTROPY_STM32_THR_POOL_SIZE must be a power of 2!");
7979

80+
/**
81+
* RM0505 §14.4 "TRNG functional description":
82+
* To use the TRNG peripheral the system clock frequency must be
83+
* at least 32 MHz. See also: §6.2.2 "Peripheral clock details".
84+
*/
85+
BUILD_ASSERT(!IS_ENABLED(CONFIG_SOC_STM32WB09XX) ||
86+
CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC >= (32 * 1000 * 1000),
87+
"STM32WB09: TRNG requires system clock frequency >= 32MHz");
88+
8089
struct entropy_stm32_rng_dev_cfg {
8190
struct stm32_pclken *pclken;
8291
};
@@ -120,6 +129,14 @@ static int entropy_stm32_suspend(void)
120129
z_stm32_hsem_lock(CFG_HW_RNG_SEMID, HSEM_LOCK_WAIT_FOREVER);
121130
#endif /* CONFIG_SOC_SERIES_STM32WBX || CONFIG_STM32H7_DUAL_CORE */
122131
LL_RNG_Disable(rng);
132+
#if defined(CONFIG_SOC_STM32WB09XX)
133+
/* RM0505 Rev.2 §14.4:
134+
* "After the TRNG IP is disabled by setting CR.DISABLE, in order to
135+
* properly restart the TRNG IP, the AES_RESET bit must be set to 1
136+
* (that is, resetting the AES core and restarting all health tests)."
137+
*/
138+
LL_RNG_SetAesReset(rng, 1);
139+
#endif /* CONFIG_SOC_STM32WB09XX */
123140

124141
#ifdef CONFIG_SOC_SERIES_STM32WBAX
125142
uint32_t wait_cycles, rng_rate;

drivers/entropy/entropy_stm32.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ static inline void ll_rng_enable_it(RNG_TypeDef *RNGx)
2929
/* Silence "unused" warning on IRQ-less hardware*/
3030
ARG_UNUSED(RNGx);
3131
#if !IRQLESS_TRNG
32-
LL_RNG_EnableIT(RNGx);
32+
# if defined(CONFIG_SOC_STM32WB09XX)
33+
LL_RNG_EnableEnErrorIrq(RNGx);
34+
LL_RNG_EnableEnFfFullIrq(RNGx);
35+
# else
36+
LL_RNG_EnableIT(RNGx);
37+
# endif
3338
#endif /* !IRQLESS_TRNG */
3439
}
3540

3641
static inline uint32_t ll_rng_is_active_seis(RNG_TypeDef *RNGx)
3742
{
3843
#if defined(CONFIG_SOC_SERIES_STM32WB0X)
39-
return LL_RNG_IsActiveFlag_FAULT(RNGx);
44+
# if defined(CONFIG_SOC_STM32WB09XX)
45+
return LL_RNG_IsActiveFlag_ENTROPY_ERR(RNGx);
46+
# else
47+
return LL_RNG_IsActiveFlag_FAULT(RNGx);
48+
# endif
4049
#else
4150
return LL_RNG_IsActiveFlag_SEIS(RNGx);
4251
#endif /* CONFIG_SOC_SERIES_STM32WB0X */
@@ -45,7 +54,11 @@ static inline uint32_t ll_rng_is_active_seis(RNG_TypeDef *RNGx)
4554
static inline void ll_rng_clear_seis(RNG_TypeDef *RNGx)
4655
{
4756
#if defined(CONFIG_SOC_SERIES_STM32WB0X)
48-
LL_RNG_ClearFlag_FAULT(RNGx);
57+
# if defined(CONFIG_SOC_STM32WB09XX)
58+
LL_RNG_SetResetHealthErrorFlags(RNGx, 1);
59+
# else
60+
LL_RNG_ClearFlag_FAULT(RNGx);
61+
# endif
4962
#else
5063
LL_RNG_ClearFlag_SEIS(RNGx);
5164
#endif /* CONFIG_SOC_SERIES_STM32WB0X */
@@ -68,7 +81,11 @@ static inline uint32_t ll_rng_is_active_secs(RNG_TypeDef *RNGx)
6881
static inline uint32_t ll_rng_is_active_drdy(RNG_TypeDef *RNGx)
6982
{
7083
#if defined(CONFIG_SOC_SERIES_STM32WB0X)
71-
return LL_RNG_IsActiveFlag_RNGRDY(RNGx);
84+
# if defined(CONFIG_SOC_STM32WB09XX)
85+
return LL_RNG_IsActiveFlag_VAL_READY(RNGx);
86+
# else
87+
return LL_RNG_IsActiveFlag_RNGRDY(RNGx);
88+
# endif
7289
#else
7390
return LL_RNG_IsActiveFlag_DRDY(RNGx);
7491
#endif /* CONFIG_SOC_SERIES_STM32WB0X */
@@ -77,7 +94,11 @@ static inline uint32_t ll_rng_is_active_drdy(RNG_TypeDef *RNGx)
7794
static inline uint16_t ll_rng_read_rand_data(RNG_TypeDef *RNGx)
7895
{
7996
#if defined(CONFIG_SOC_SERIES_STM32WB0X)
80-
return LL_RNG_ReadRandData16(RNGx);
97+
# if defined(CONFIG_SOC_STM32WB09XX)
98+
return (uint16_t)LL_RNG_GetRndVal(RNGx);
99+
# else
100+
return LL_RNG_ReadRandData16(RNGx);
101+
# endif
81102
#else
82103
return (uint16_t)LL_RNG_ReadRandData32(RNGx);
83104
#endif /* CONFIG_SOC_SERIES_STM32WB0X */

0 commit comments

Comments
 (0)