Skip to content

Commit

Permalink
drivers: stm32_rng: ensure conditional reset sequence is done
Browse files Browse the repository at this point in the history
Add a check on RNG_CR_CONDRST being cleared before continuing the
program to ensure that the conditional reset sequence is done.

Signed-off-by: Gatien Chevallier <[email protected]>
Reviewed-by: Etienne Carriere <[email protected]>
  • Loading branch information
GseoC authored and jforissier committed Jan 10, 2024
1 parent c2c5b4b commit eb5cf77
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion core/drivers/stm32_rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,15 @@ static TEE_Result init_rng(void)
io_clrsetbits32(rng_base + RNG_CR, RNG_CR_CLKDIV,
clock_div << RNG_CR_CLKDIV_SHIFT);

/* No need to wait for RNG_CR_CONDRST toggle as we enable clk */
io_clrsetbits32(rng_base + RNG_CR, RNG_CR_CONDRST,
RNG_CR_RNGEN);

timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US);
while (io_read32(rng_base + RNG_CR) & RNG_CR_CONDRST)
if (timeout_elapsed(timeout_ref))
break;
if (io_read32(rng_base + RNG_CR) & RNG_CR_CONDRST)
panic();
} else {
io_setbits32(rng_base + RNG_CR, RNG_CR_RNGEN | cr_ced_mask);
}
Expand Down Expand Up @@ -391,6 +397,8 @@ static TEE_Result stm32_rng_pm_resume(uint32_t pm_cr)
io_write32(base + RNG_SR, 0);

if (stm32_rng->ddata->has_cond_reset) {
uint64_t timeout_ref = 0;

/*
* Configuration must be set in the same access that sets
* RNG_CR_CONDRST bit. Otherwise, the configuration setting is
Expand All @@ -400,6 +408,13 @@ static TEE_Result stm32_rng_pm_resume(uint32_t pm_cr)
io_write32(base + RNG_CR, pm_cr | RNG_CR_CONDRST);

io_clrsetbits32(base + RNG_CR, RNG_CR_CONDRST, RNG_CR_RNGEN);

timeout_ref = timeout_init_us(RNG_READY_TIMEOUT_US);
while (io_read32(base + RNG_CR) & RNG_CR_CONDRST)
if (timeout_elapsed(timeout_ref))
break;
if (io_read32(base + RNG_CR) & RNG_CR_CONDRST)
panic();
} else {
io_write32(base + RNG_CR, RNG_CR_RNGEN | pm_cr);
}
Expand Down

0 comments on commit eb5cf77

Please sign in to comment.