Skip to content

Commit 4519041

Browse files
ck-telecomnashif
authored andcommitted
drivers: entropy: sf32lb: add trng driver support
Add trng driver for sf32lb platform Signed-off-by: Qingsong Gou <[email protected]>
1 parent b346fc3 commit 4519041

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

drivers/entropy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_PSA_CRYPTO_RNG entropy_psa_crypto.c)
3737
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RENESAS_RA entropy_renesas_ra.c)
3838
zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c)
3939
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c)
40+
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SF32LB entropy_sf32lb.c)
4041
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX91X entropy_silabs_siwx91x.c)
4142
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SMARTBOND_TRNG entropy_smartbond.c)
4243
zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c)

drivers/entropy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ source "drivers/entropy/Kconfig.psa_crypto"
4343
source "drivers/entropy/Kconfig.renesas_ra"
4444
source "drivers/entropy/Kconfig.rv32m1"
4545
source "drivers/entropy/Kconfig.sam"
46+
source "drivers/entropy/Kconfig.sf32lb"
4647
source "drivers/entropy/Kconfig.siwx91x"
4748
source "drivers/entropy/Kconfig.smartbond"
4849
source "drivers/entropy/Kconfig.stm32"

drivers/entropy/Kconfig.sf32lb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025, Qingsong Gou <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config ENTROPY_SF32LB
5+
bool "SF32LB Entropy driver"
6+
default y
7+
depends on DT_HAS_SIFLI_SF32LB_TRNG_ENABLED
8+
select ENTROPY_HAS_DRIVER
9+
help
10+
Enable driver for SF32LB True Random Number Generator (TRNG).

drivers/entropy/entropy_sf32lb.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2025 Qingsong Gou <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#define DT_DRV_COMPAT sifli_sf32lb_trng
6+
7+
#include <zephyr/arch/cpu.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/clock_control/sf32lb.h>
10+
#include <zephyr/drivers/entropy.h>
11+
#include <zephyr/logging/log.h>
12+
13+
#include <register.h>
14+
15+
LOG_MODULE_REGISTER(entropy_sf32lb, CONFIG_ENTROPY_LOG_LEVEL);
16+
17+
#define TRNG_CTRL offsetof(TRNG_TypeDef, CTRL)
18+
#define TRNG_STAT offsetof(TRNG_TypeDef, STAT)
19+
#define TRNG_RAND offsetof(TRNG_TypeDef, RAND_NUM0)
20+
21+
#define TRNG_RAND_NUM_MAX (8U)
22+
23+
#define TRNG_RAND_MASK (TRNG_RAND_NUM_MAX - 1U)
24+
25+
struct entropy_sf32lb_config {
26+
uintptr_t base;
27+
struct sf32lb_clock_dt_spec clock;
28+
};
29+
30+
static int entropy_sf32lb_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
31+
{
32+
const struct entropy_sf32lb_config *config = dev->config;
33+
uint32_t buf[TRNG_RAND_NUM_MAX];
34+
uint16_t bytes;
35+
36+
while (length) {
37+
sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_SEED_START_Pos);
38+
while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_SEED_VALID_Pos)) {
39+
}
40+
41+
/* Generate random data */
42+
sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_RAND_NUM_START_Pos);
43+
while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_RAND_NUM_VALID_Pos)) {
44+
}
45+
46+
for (uint8_t i = 0U; i < TRNG_RAND_NUM_MAX; i++) {
47+
buf[i] = sys_read32(config->base + TRNG_RAND + (i * 4U));
48+
}
49+
50+
bytes = MIN(length, sizeof(buf));
51+
52+
memcpy(buffer, buf, bytes);
53+
54+
length -= bytes;
55+
buffer += bytes;
56+
}
57+
58+
return 0;
59+
}
60+
61+
static DEVICE_API(entropy, entropy_sf32lb_api) = {
62+
.get_entropy = entropy_sf32lb_get_entropy,
63+
};
64+
65+
static int entropy_sf32lb_init(const struct device *dev)
66+
{
67+
const struct entropy_sf32lb_config *config = dev->config;
68+
69+
if (!sf32lb_clock_is_ready_dt(&config->clock)) {
70+
return -ENODEV;
71+
}
72+
73+
return sf32lb_clock_control_on_dt(&config->clock);
74+
}
75+
76+
#define ENTROPY_SF32LB_DEFINE(n) \
77+
static const struct entropy_sf32lb_config entropy_sf32lb_config_##n = { \
78+
.base = DT_INST_REG_ADDR(n), \
79+
.clock = SF32LB_CLOCK_DT_INST_SPEC_GET(n), \
80+
}; \
81+
\
82+
DEVICE_DT_INST_DEFINE(n, entropy_sf32lb_init, NULL, NULL, \
83+
&entropy_sf32lb_config_##n, PRE_KERNEL_1, \
84+
CONFIG_ENTROPY_INIT_PRIORITY, &entropy_sf32lb_api);
85+
86+
DT_INST_FOREACH_STATUS_OKAY(ENTROPY_SF32LB_DEFINE)

0 commit comments

Comments
 (0)