|
| 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