|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Silicon Laboratories Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#define DT_DRV_COMPAT silabs_siwx917_rng |
| 6 | + |
| 7 | +#include <zephyr/drivers/entropy.h> |
| 8 | + |
| 9 | +#include "rsi_rom_rng.h" |
| 10 | +#include "rsi_rom_clks.h" |
| 11 | + |
| 12 | +static int siwx917_get_entropy_isr(const struct device *dev, uint8_t *buffer, |
| 13 | + uint16_t length, uint32_t flags) |
| 14 | +{ |
| 15 | + uint32_t u32_count = length / sizeof(uint32_t); |
| 16 | + uint32_t u8_count = u32_count * sizeof(uint32_t); |
| 17 | + uint32_t u8_remainder = length - u8_count; |
| 18 | + uint32_t swap_space; |
| 19 | + |
| 20 | + if (!(flags & ENTROPY_BUSYWAIT)) { |
| 21 | + return -ENOTSUP; |
| 22 | + } |
| 23 | + RSI_RNG_GetBytes((void *)dev->config, (uint32_t *)buffer, u32_count); |
| 24 | + if (length % sizeof(uint32_t)) { |
| 25 | + RSI_RNG_GetBytes((void *)dev->config, &swap_space, 1); |
| 26 | + memcpy(buffer + u8_count, &swap_space, u8_remainder); |
| 27 | + } |
| 28 | + |
| 29 | + return 0; |
| 30 | +} |
| 31 | + |
| 32 | +static int siwx917_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length) |
| 33 | +{ |
| 34 | + return siwx917_get_entropy_isr(dev, buffer, length, ENTROPY_BUSYWAIT); |
| 35 | +} |
| 36 | + |
| 37 | +static int siwx917_init(const struct device *dev) |
| 38 | +{ |
| 39 | + int ret; |
| 40 | + |
| 41 | + ret = RSI_CLK_PeripheralClkEnable1(M4CLK, HWRNG_PCLK_ENABLE); |
| 42 | + if (ret) { |
| 43 | + return -EIO; |
| 44 | + } |
| 45 | + ret = RSI_RNG_Start((void *)dev->config, RSI_RNG_TRUE_RANDOM); |
| 46 | + if (ret) { |
| 47 | + return -EIO; |
| 48 | + } |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +static const struct entropy_driver_api siwx917_api = { |
| 53 | + .get_entropy = siwx917_get_entropy, |
| 54 | + .get_entropy_isr = siwx917_get_entropy_isr, |
| 55 | +}; |
| 56 | + |
| 57 | +#define SIWX917_RNG_INIT(idx) \ |
| 58 | + DEVICE_DT_INST_DEFINE(idx, siwx917_init, NULL, NULL, (void *)DT_INST_REG_ADDR(idx), \ |
| 59 | + PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, &siwx917_api); |
| 60 | + |
| 61 | +DT_INST_FOREACH_STATUS_OKAY(SIWX917_RNG_INIT) |
0 commit comments