-
Notifications
You must be signed in to change notification settings - Fork 8.3k
drivers: entropy: sf32lb: add trng driver support #98467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+129
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
80d565f
dts: bindings: rng: add sifli,sf32lb-trng
ck-telecom 7297398
dts: arm: sifli: sf32lb52x: add trng
ck-telecom 209d9ef
drivers: entropy: sf32lb: add trng driver support
ck-telecom 4bc581a
boards: sifli: sf32lb52_devkit_lcd: enable trng
ck-telecom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config ENTROPY_SF32LB | ||
| bool "SF32LB Entropy driver" | ||
| default y | ||
| depends on DT_HAS_SIFLI_SF32LB_TRNG_ENABLED | ||
| select ENTROPY_HAS_DRIVER | ||
| help | ||
| Enable driver for SF32LB True Random Number Generator (TRNG). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright (c) 2025 Qingsong Gou <gouqs@hotmail.com> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #define DT_DRV_COMPAT sifli_sf32lb_trng | ||
|
|
||
| #include <zephyr/arch/cpu.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/drivers/clock_control/sf32lb.h> | ||
| #include <zephyr/drivers/entropy.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| #include <register.h> | ||
|
|
||
| LOG_MODULE_REGISTER(entropy_sf32lb, CONFIG_ENTROPY_LOG_LEVEL); | ||
|
|
||
| #define TRNG_CTRL offsetof(TRNG_TypeDef, CTRL) | ||
| #define TRNG_STAT offsetof(TRNG_TypeDef, STAT) | ||
| #define TRNG_RAND offsetof(TRNG_TypeDef, RAND_NUM0) | ||
|
|
||
| #define TRNG_RAND_NUM_MAX (8U) | ||
|
|
||
| #define TRNG_RAND_MASK (TRNG_RAND_NUM_MAX - 1U) | ||
|
|
||
| struct entropy_sf32lb_config { | ||
| uintptr_t base; | ||
| struct sf32lb_clock_dt_spec clock; | ||
| }; | ||
|
|
||
| static int entropy_sf32lb_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length) | ||
| { | ||
| const struct entropy_sf32lb_config *config = dev->config; | ||
| uint32_t buf[TRNG_RAND_NUM_MAX]; | ||
| uint16_t bytes; | ||
|
|
||
| while (length) { | ||
| sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_SEED_START_Pos); | ||
| while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_SEED_VALID_Pos)) { | ||
| } | ||
|
|
||
| /* Generate random data */ | ||
| sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_RAND_NUM_START_Pos); | ||
| while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_RAND_NUM_VALID_Pos)) { | ||
| } | ||
|
|
||
| for (uint8_t i = 0U; i < TRNG_RAND_NUM_MAX; i++) { | ||
| buf[i] = sys_read32(config->base + TRNG_RAND + (i * 4U)); | ||
| } | ||
|
|
||
| bytes = MIN(length, sizeof(buf)); | ||
|
|
||
| memcpy(buffer, buf, bytes); | ||
|
|
||
| length -= bytes; | ||
| buffer += bytes; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static DEVICE_API(entropy, entropy_sf32lb_api) = { | ||
| .get_entropy = entropy_sf32lb_get_entropy, | ||
ck-telecom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| static int entropy_sf32lb_init(const struct device *dev) | ||
| { | ||
| const struct entropy_sf32lb_config *config = dev->config; | ||
|
|
||
| if (!sf32lb_clock_is_ready_dt(&config->clock)) { | ||
| return -ENODEV; | ||
| } | ||
|
|
||
| return sf32lb_clock_control_on_dt(&config->clock); | ||
| } | ||
ck-telecom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #define ENTROPY_SF32LB_DEFINE(n) \ | ||
| static const struct entropy_sf32lb_config entropy_sf32lb_config_##n = { \ | ||
| .base = DT_INST_REG_ADDR(n), \ | ||
| .clock = SF32LB_CLOCK_DT_INST_SPEC_GET(n), \ | ||
| }; \ | ||
| \ | ||
| DEVICE_DT_INST_DEFINE(n, entropy_sf32lb_init, NULL, NULL, \ | ||
| &entropy_sf32lb_config_##n, PRE_KERNEL_1, \ | ||
| CONFIG_ENTROPY_INIT_PRIORITY, &entropy_sf32lb_api); | ||
|
|
||
| DT_INST_FOREACH_STATUS_OKAY(ENTROPY_SF32LB_DEFINE) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Sifli SF32LB TRNG (True Random Number Generator). | ||
|
|
||
| compatible: "sifli,sf32lb-trng" | ||
|
|
||
| include: base.yaml | ||
|
|
||
| properties: | ||
| reg: | ||
| required: true | ||
|
|
||
| interrupts: | ||
| required: true | ||
|
|
||
| clocks: | ||
| required: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.