drivers: entropy: add iproc_rng200 (rpi_5) random generator driver
Add driver for iproc_rng200 entropy generator. This device is used by rpi_5. Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
This commit is contained in:
parent
10f614db9a
commit
431f202732
7 changed files with 183 additions and 0 deletions
|
@ -24,6 +24,7 @@
|
||||||
zephyr,console = &uart10;
|
zephyr,console = &uart10;
|
||||||
zephyr,shell-uart = &uart10;
|
zephyr,shell-uart = &uart10;
|
||||||
zephyr,pcie-controller = &pcie1;
|
zephyr,pcie-controller = &pcie1;
|
||||||
|
zephyr,entropy = &rng;
|
||||||
};
|
};
|
||||||
|
|
||||||
leds {
|
leds {
|
||||||
|
|
|
@ -16,6 +16,7 @@ endif()
|
||||||
zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.c)
|
zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.c)
|
||||||
|
|
||||||
# zephyr-keep-sorted-start
|
# zephyr-keep-sorted-start
|
||||||
|
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BRCM_IPROC_RNG200 entropy_iproc_rng200.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BT_HCI entropy_bt_hci.c)
|
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BT_HCI entropy_bt_hci.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c)
|
zephyr_library_sources_ifdef(CONFIG_ENTROPY_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c)
|
||||||
zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c)
|
zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c)
|
||||||
|
|
|
@ -26,6 +26,7 @@ source "drivers/entropy/Kconfig.bt_hci"
|
||||||
source "drivers/entropy/Kconfig.cc13xx_cc26xx"
|
source "drivers/entropy/Kconfig.cc13xx_cc26xx"
|
||||||
source "drivers/entropy/Kconfig.esp32"
|
source "drivers/entropy/Kconfig.esp32"
|
||||||
source "drivers/entropy/Kconfig.gecko"
|
source "drivers/entropy/Kconfig.gecko"
|
||||||
|
source "drivers/entropy/Kconfig.iproc"
|
||||||
source "drivers/entropy/Kconfig.litex"
|
source "drivers/entropy/Kconfig.litex"
|
||||||
source "drivers/entropy/Kconfig.max32"
|
source "drivers/entropy/Kconfig.max32"
|
||||||
source "drivers/entropy/Kconfig.maxq10xx"
|
source "drivers/entropy/Kconfig.maxq10xx"
|
||||||
|
|
10
drivers/entropy/Kconfig.iproc
Normal file
10
drivers/entropy/Kconfig.iproc
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Copyright (c) 2025 TOKITA Hiroshi
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
config ENTROPY_BRCM_IPROC_RNG200
|
||||||
|
bool "Broadcom iProc RNG200 driver"
|
||||||
|
default y
|
||||||
|
depends on DT_HAS_BRCM_IPROC_RNG200_ENABLED
|
||||||
|
select ENTROPY_HAS_DRIVER
|
||||||
|
help
|
||||||
|
Enable the Broadcom iProc RNG200 random number generator
|
152
drivers/entropy/entropy_iproc_rng200.c
Normal file
152
drivers/entropy/entropy_iproc_rng200.c
Normal file
|
@ -0,0 +1,152 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2025 TOKITA Hiroshi
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define DT_DRV_COMPAT brcm_iproc_rng200
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
LOG_MODULE_REGISTER(iproc_rng200_entropy, CONFIG_ENTROPY_LOG_LEVEL);
|
||||||
|
|
||||||
|
#include <zephyr/device.h>
|
||||||
|
#include <zephyr/drivers/entropy.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <zephyr/init.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
#define IPROC_RNG200_CTRL_OFFS 0x00
|
||||||
|
#define IPROC_RNG200_RNG_RESET_OFFS 0x04
|
||||||
|
#define IPROC_RNG200_RBG_RESET_OFFS 0x08
|
||||||
|
#define IPROC_RNG200_RESERVED1_OFFS 0x0c
|
||||||
|
#define IPROC_RNG200_RESERVED2_OFFS 0x10
|
||||||
|
#define IPROC_RNG200_RESERVED3_OFFS 0x14
|
||||||
|
#define IPROC_RNG200_INT_STATUS_OFFS 0x18
|
||||||
|
#define IPROC_RNG200_RESERVED4_OFFS 0x1c
|
||||||
|
#define IPROC_RNG200_FIFO_DATA_OFFS 0x20
|
||||||
|
#define IPROC_RNG200_FIFO_COUNT_OFFS 0x24
|
||||||
|
|
||||||
|
#define IPROC_RNG200_CTRL_RBG_EN BIT(0)
|
||||||
|
#define IPROC_RNG200_RESET_EN BIT(0)
|
||||||
|
#define IPROC_RNG200_INT_STATUS_NIST_FAIL BIT(5)
|
||||||
|
#define IPROC_RNG200_INT_STATUS_MASTER_FAIL_LOCKOUT BIT(31)
|
||||||
|
|
||||||
|
#define IPROC_RNG200_CTRL_RBG_EN_MASK BIT_MASK(13)
|
||||||
|
#define IPROC_RNG200_FIFO_COUNT_MASK BIT_MASK(8)
|
||||||
|
|
||||||
|
/* time needed to fill fifo when empty */
|
||||||
|
#define IPROC_RNG200_FIFO_REFILL_TIME_USEC 40
|
||||||
|
#define IPROC_RNG200_FIFO_REFILL_MAX_RETRIES 5
|
||||||
|
|
||||||
|
#define DEV_CFG(dev) ((const struct iproc_rng200_config *)(dev)->config)
|
||||||
|
#define DEV_DATA(dev) ((struct iproc_rng200_data *)(dev)->data)
|
||||||
|
|
||||||
|
struct iproc_rng200_config {
|
||||||
|
DEVICE_MMIO_NAMED_ROM(base_addr);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct iproc_rng200_data {
|
||||||
|
DEVICE_MMIO_NAMED_RAM(base_addr);
|
||||||
|
struct k_mutex mutex;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int iproc_rng200_driver_init(const struct device *dev)
|
||||||
|
{
|
||||||
|
struct iproc_rng200_data *const data = dev->data;
|
||||||
|
|
||||||
|
k_mutex_init(&data->mutex);
|
||||||
|
|
||||||
|
DEVICE_MMIO_NAMED_MAP(dev, base_addr, K_MEM_CACHE_NONE);
|
||||||
|
|
||||||
|
const mem_addr_t base = DEVICE_MMIO_NAMED_GET(dev, base_addr);
|
||||||
|
const uint32_t val =
|
||||||
|
sys_read32(base + IPROC_RNG200_CTRL_OFFS) & IPROC_RNG200_CTRL_RBG_EN_MASK;
|
||||||
|
|
||||||
|
sys_write32(val & ~IPROC_RNG200_CTRL_RBG_EN, base + IPROC_RNG200_CTRL_OFFS);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int iproc_rng200_driver_get_entropy(const struct device *dev, uint8_t *buffer,
|
||||||
|
uint16_t length)
|
||||||
|
{
|
||||||
|
const mem_addr_t base = DEVICE_MMIO_NAMED_GET(dev, base_addr);
|
||||||
|
const uint32_t word_count = DIV_ROUND_UP(length, 4);
|
||||||
|
struct iproc_rng200_data *const data = dev->data;
|
||||||
|
uint32_t retries_left;
|
||||||
|
uint32_t random_word;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < word_count; i++) {
|
||||||
|
retries_left = IPROC_RNG200_FIFO_REFILL_MAX_RETRIES;
|
||||||
|
k_mutex_lock(&data->mutex, K_FOREVER);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const uint32_t status = sys_read32(base + IPROC_RNG200_INT_STATUS_OFFS);
|
||||||
|
uint32_t val;
|
||||||
|
|
||||||
|
if (status & (IPROC_RNG200_INT_STATUS_MASTER_FAIL_LOCKOUT |
|
||||||
|
IPROC_RNG200_INT_STATUS_NIST_FAIL)) {
|
||||||
|
|
||||||
|
sys_write32(0xFFFFFFFF, base + IPROC_RNG200_INT_STATUS_OFFS);
|
||||||
|
|
||||||
|
val = sys_read32(base + IPROC_RNG200_RNG_RESET_OFFS);
|
||||||
|
sys_write32(val | IPROC_RNG200_RESET_EN,
|
||||||
|
base + IPROC_RNG200_RNG_RESET_OFFS);
|
||||||
|
|
||||||
|
val = sys_read32(base + IPROC_RNG200_RBG_RESET_OFFS);
|
||||||
|
sys_write32(val | IPROC_RNG200_RESET_EN,
|
||||||
|
base + IPROC_RNG200_RBG_RESET_OFFS);
|
||||||
|
|
||||||
|
val = sys_read32(base + IPROC_RNG200_RNG_RESET_OFFS);
|
||||||
|
sys_write32(val & ~IPROC_RNG200_RESET_EN,
|
||||||
|
base + IPROC_RNG200_RNG_RESET_OFFS);
|
||||||
|
|
||||||
|
val = sys_read32(base + IPROC_RNG200_RBG_RESET_OFFS);
|
||||||
|
sys_write32(val & ~IPROC_RNG200_RESET_EN,
|
||||||
|
base + IPROC_RNG200_RBG_RESET_OFFS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* make sure fifo has at least one random word */
|
||||||
|
const uint32_t fcnt = sys_read32(base + IPROC_RNG200_FIFO_COUNT_OFFS);
|
||||||
|
|
||||||
|
if ((fcnt & IPROC_RNG200_FIFO_COUNT_MASK) > 0) {
|
||||||
|
/* get new random word */
|
||||||
|
random_word = sys_read32(base + IPROC_RNG200_FIFO_DATA_OFFS);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* currently no random values available, thus wait */
|
||||||
|
retries_left--;
|
||||||
|
if (!retries_left) {
|
||||||
|
/* number of retries exhausted, give up */
|
||||||
|
k_mutex_unlock(&data->mutex);
|
||||||
|
return -ETIMEDOUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
k_sleep(K_USEC(IPROC_RNG200_FIFO_REFILL_TIME_USEC));
|
||||||
|
}
|
||||||
|
|
||||||
|
k_mutex_unlock(&data->mutex);
|
||||||
|
|
||||||
|
memcpy(&buffer[i * 4], &random_word, MIN(length, 4));
|
||||||
|
length -= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DEVICE_API(entropy, iproc_rng200_entropy_api) = {
|
||||||
|
.get_entropy = iproc_rng200_driver_get_entropy,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define IPROC_RNG200_INIT(n) \
|
||||||
|
static const struct iproc_rng200_config iproc_rng200_##n##_cfg = { \
|
||||||
|
DEVICE_MMIO_NAMED_ROM_INIT(base_addr, DT_DRV_INST(n)), \
|
||||||
|
}; \
|
||||||
|
static struct iproc_rng200_data iproc_rng200_##n##_data; \
|
||||||
|
\
|
||||||
|
DEVICE_DT_INST_DEFINE(n, iproc_rng200_driver_init, NULL, &iproc_rng200_##n##_data, \
|
||||||
|
&iproc_rng200_##n##_cfg, PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, \
|
||||||
|
&iproc_rng200_entropy_api);
|
||||||
|
|
||||||
|
DT_INST_FOREACH_STATUS_OKAY(IPROC_RNG200_INIT)
|
|
@ -52,6 +52,12 @@
|
||||||
status = "okay";
|
status = "okay";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
rng: rng@107d208000 {
|
||||||
|
compatible = "brcm,iproc-rng200";
|
||||||
|
reg = <0x10 0x7d208000 0x28>;
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
gpio2@107d517c00 {
|
gpio2@107d517c00 {
|
||||||
compatible = "simple-bus";
|
compatible = "simple-bus";
|
||||||
reg = <0x10 0x7d517c00 0x40>;
|
reg = <0x10 0x7d517c00 0x40>;
|
||||||
|
|
12
dts/bindings/rng/brcm,iproc-rng200.yaml
Normal file
12
dts/bindings/rng/brcm,iproc-rng200.yaml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
# Copyright (c) 2025 TOKITA Hiroshi
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
description: Broadcom iProc RNG200 Random Number Generator
|
||||||
|
|
||||||
|
compatible: "brcm,iproc-rng200"
|
||||||
|
|
||||||
|
include: base.yaml
|
||||||
|
|
||||||
|
properties:
|
||||||
|
reg:
|
||||||
|
required: true
|
Loading…
Add table
Add a link
Reference in a new issue