Skip to content

Commit 6946bc2

Browse files
GseoCAMISTM
authored andcommitted
rng: stm32: add RNG clock frequency restraint
In order to ensure a good RNG quality and compatibility with certified RNG configuration, add RNG clock frequency restraint. Change-Id: I5014a808964c3834dfbfe6aae75cf86689702113 Signed-off-by: Gatien Chevallier <gatien.chevallier@foss.st.com> Reviewed-on: https://gerrit.st.com/c/mpu/oe/st/u-boot/+/363092 Reviewed-by: Gatien CHEVALLIER <gatien.chevallier@st.com> ACI: CIBUILD <MDG-smet-aci-builds@list.st.com> ACI: CITOOLS <MDG-smet-aci-reviews@list.st.com> Domain-Review: Gatien CHEVALLIER <gatien.chevallier@st.com> Tested-by: Amit MITTAL <amit.mittal@st.com> Reviewed-by: Patrice CHOTARD <patrice.chotard@foss.st.com>
1 parent b060d63 commit 6946bc2

1 file changed

Lines changed: 38 additions & 5 deletions

File tree

drivers/rng/stm32mp1_rng.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include <linux/iopoll.h>
1919
#include <linux/kernel.h>
2020

21-
#define RNG_CR 0x00
22-
#define RNG_CR_RNGEN BIT(2)
23-
#define RNG_CR_CED BIT(5)
24-
#define RNG_CR_CONDRST BIT(30)
21+
#define RNG_CR 0x00
22+
#define RNG_CR_RNGEN BIT(2)
23+
#define RNG_CR_CED BIT(5)
24+
#define RNG_CR_CLKDIV_SHIFT 16
25+
#define RNG_CR_CONDRST BIT(30)
2526

2627
#define RNG_SR 0x04
2728
#define RNG_SR_SEIS BIT(6)
@@ -31,7 +32,15 @@
3132

3233
#define RNG_DR 0x08
3334

35+
/*
36+
* struct stm32_rng_data - RNG compat data
37+
*
38+
* @max_clock_rate: Max RNG clock frequency, in Hertz
39+
* @has_cond_reset: True if conditionnal reset is supported
40+
*
41+
*/
3442
struct stm32_rng_data {
43+
uint max_clock_rate;
3544
bool has_cond_reset;
3645
};
3746

@@ -86,6 +95,26 @@ static int stm32_rng_read(struct udevice *dev, void *data, size_t len)
8695
return 0;
8796
}
8897

98+
static uint stm32_rng_clock_freq_restrain(struct stm32_rng_plat *pdata)
99+
{
100+
ulong clock_rate = 0;
101+
uint clock_div = 0;
102+
103+
clock_rate = clk_get_rate(&pdata->clk);
104+
105+
/*
106+
* Get the exponent to apply on the CLKDIV field in RNG_CR register.
107+
* No need to handle the case when clock-div > 0xF as it is physically
108+
* impossible.
109+
*/
110+
while ((clock_rate >> clock_div) > pdata->data->max_clock_rate)
111+
clock_div++;
112+
113+
log_debug("RNG clk rate : %lu\n", clk_get_rate(&pdata->clk) >> clock_div);
114+
115+
return clock_div;
116+
}
117+
89118
static int stm32_rng_init(struct stm32_rng_plat *pdata)
90119
{
91120
int err;
@@ -100,7 +129,9 @@ static int stm32_rng_init(struct stm32_rng_plat *pdata)
100129
/* Disable CED */
101130
cr |= RNG_CR_CED;
102131
if (pdata->data->has_cond_reset) {
103-
cr |= RNG_CR_CONDRST;
132+
uint clock_div = stm32_rng_clock_freq_restrain(pdata);
133+
134+
cr |= RNG_CR_CONDRST | (clock_div << RNG_CR_CLKDIV_SHIFT);
104135
writel(cr, pdata->base + RNG_CR);
105136
cr &= ~RNG_CR_CONDRST;
106137
writel(cr, pdata->base + RNG_CR);
@@ -174,10 +205,12 @@ static const struct dm_rng_ops stm32_rng_ops = {
174205

175206
static const struct stm32_rng_data stm32mp13_rng_data = {
176207
.has_cond_reset = true,
208+
.max_clock_rate = 48000000,
177209
};
178210

179211
static const struct stm32_rng_data stm32_rng_data = {
180212
.has_cond_reset = false,
213+
.max_clock_rate = 3000000,
181214
};
182215

183216
static const struct udevice_id stm32_rng_match[] = {

0 commit comments

Comments
 (0)