diff --git a/boards/raspberrypi/rpi_5/rpi_5.dts b/boards/raspberrypi/rpi_5/rpi_5.dts index 3f942d4e6f5..339adba011c 100644 --- a/boards/raspberrypi/rpi_5/rpi_5.dts +++ b/boards/raspberrypi/rpi_5/rpi_5.dts @@ -24,6 +24,7 @@ zephyr,console = &uart10; zephyr,shell-uart = &uart10; zephyr,pcie-controller = &pcie1; + zephyr,entropy = &rng; }; leds { diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt index 7d0d62026ce..eaf858add89 100644 --- a/drivers/entropy/CMakeLists.txt +++ b/drivers/entropy/CMakeLists.txt @@ -16,6 +16,7 @@ endif() zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.c) # 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_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c) diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig index 123b3b07647..e0a2080b343 100644 --- a/drivers/entropy/Kconfig +++ b/drivers/entropy/Kconfig @@ -26,6 +26,7 @@ source "drivers/entropy/Kconfig.bt_hci" source "drivers/entropy/Kconfig.cc13xx_cc26xx" source "drivers/entropy/Kconfig.esp32" source "drivers/entropy/Kconfig.gecko" +source "drivers/entropy/Kconfig.iproc" source "drivers/entropy/Kconfig.litex" source "drivers/entropy/Kconfig.max32" source "drivers/entropy/Kconfig.maxq10xx" diff --git a/drivers/entropy/Kconfig.iproc b/drivers/entropy/Kconfig.iproc new file mode 100644 index 00000000000..83233f8309d --- /dev/null +++ b/drivers/entropy/Kconfig.iproc @@ -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 diff --git a/drivers/entropy/entropy_iproc_rng200.c b/drivers/entropy/entropy_iproc_rng200.c new file mode 100644 index 00000000000..a15249655a1 --- /dev/null +++ b/drivers/entropy/entropy_iproc_rng200.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2025 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT brcm_iproc_rng200 + +#include +LOG_MODULE_REGISTER(iproc_rng200_entropy, CONFIG_ENTROPY_LOG_LEVEL); + +#include +#include +#include +#include +#include + +#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) diff --git a/dts/arm64/broadcom/bcm2712.dtsi b/dts/arm64/broadcom/bcm2712.dtsi index e99a640eb2e..c43da328a7c 100644 --- a/dts/arm64/broadcom/bcm2712.dtsi +++ b/dts/arm64/broadcom/bcm2712.dtsi @@ -52,6 +52,12 @@ status = "okay"; }; + rng: rng@107d208000 { + compatible = "brcm,iproc-rng200"; + reg = <0x10 0x7d208000 0x28>; + status = "okay"; + }; + gpio2@107d517c00 { compatible = "simple-bus"; reg = <0x10 0x7d517c00 0x40>; diff --git a/dts/bindings/rng/brcm,iproc-rng200.yaml b/dts/bindings/rng/brcm,iproc-rng200.yaml new file mode 100644 index 00000000000..a601db21806 --- /dev/null +++ b/dts/bindings/rng/brcm,iproc-rng200.yaml @@ -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