diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt index d5588378f6f344..921649a2aa8a9c 100644 --- a/drivers/entropy/CMakeLists.txt +++ b/drivers/entropy/CMakeLists.txt @@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_RNGA entropy_mcux_rnga zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_TRNG entropy_mcux_trng.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_CAAM entropy_mcux_caam.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_NRF5_RNG entropy_nrf5.c) +zephyr_library_sources_ifdef(CONFIG_ENTROPY_RPI_PICO_RNG entropy_rpi_pico.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_SMARTBOND_TRNG entropy_smartbond.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c) diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig index debd0f8d7a110d..39546526d54a95 100644 --- a/drivers/entropy/Kconfig +++ b/drivers/entropy/Kconfig @@ -26,6 +26,7 @@ source "drivers/entropy/Kconfig.mcux" source "drivers/entropy/Kconfig.stm32" source "drivers/entropy/Kconfig.esp32" source "drivers/entropy/Kconfig.nrf5" +source "drivers/entropy/Kconfig.rpi_pico" source "drivers/entropy/Kconfig.sam" source "drivers/entropy/Kconfig.smartbond" source "drivers/entropy/Kconfig.native_posix" diff --git a/drivers/entropy/Kconfig.rpi_pico b/drivers/entropy/Kconfig.rpi_pico new file mode 100644 index 00000000000000..b181a8ad45226d --- /dev/null +++ b/drivers/entropy/Kconfig.rpi_pico @@ -0,0 +1,12 @@ +# Raspberry Pi Pico entropy generator driver configuration + +# Copyright (c) 2024 Xudong Zheng +# SPDX-License-Identifier: Apache-2.0 + +config ENTROPY_RPI_PICO_RNG + bool "Raspberry Pi Pico entropy number generator driver" + default y + depends on DT_HAS_RASPBERRYPI_PICO_RNG_ENABLED + select ENTROPY_HAS_DRIVER + select PICOSDK_USE_CLAIM + select PICOSDK_USE_RAND diff --git a/drivers/entropy/entropy_rpi_pico.c b/drivers/entropy/entropy_rpi_pico.c new file mode 100644 index 00000000000000..cc071e556540a3 --- /dev/null +++ b/drivers/entropy/entropy_rpi_pico.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024 Xudong Zheng + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#define DT_DRV_COMPAT raspberrypi_pico_rng + +static struct k_spinlock entropy_lock; + +static int entropy_rpi_pico_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len) +{ + k_spinlock_key_t key = k_spin_lock(&entropy_lock); + + __ASSERT_NO_MSG(buf != NULL); + uint8_t *buf_bytes = buf; + + while (len > 0) { + uint64_t value = get_rand_64(); + uint64_t to_copy = MIN(sizeof(value), len); + + memcpy(buf_bytes, &value, to_copy); + buf_bytes += to_copy; + len -= to_copy; + } + + k_spin_unlock(&entropy_lock, key); + return 0; +} + +static DEVICE_API(entropy, entropy_rpi_pico_api_funcs) = { + .get_entropy = entropy_rpi_pico_get_entropy +}; + +static int entropy_rpi_pico_init(const struct device *dev) +{ + return 0; +} + +DEVICE_DT_INST_DEFINE(0, + entropy_rpi_pico_init, NULL, NULL, NULL, + PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, + &entropy_rpi_pico_api_funcs); diff --git a/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi b/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi index 687cafc19aade7..8a747627d2cc07 100644 --- a/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi +++ b/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi @@ -21,6 +21,10 @@ die-temp0 = &die_temp; }; + chosen { + zephyr,entropy = &rng; + }; + cpus { #address-cells = <1>; #size-cells = <0>; @@ -424,6 +428,11 @@ resets = <&reset RPI_PICO_RESETS_RESET_PIO2>; status = "disabled"; }; + + rng: rng { + status = "okay"; + compatible = "raspberrypi,pico-rng"; + }; }; pinctrl: pin-controller { diff --git a/dts/bindings/rng/raspberrypi,pico-rng.yaml b/dts/bindings/rng/raspberrypi,pico-rng.yaml new file mode 100644 index 00000000000000..0f3e0789531d10 --- /dev/null +++ b/dts/bindings/rng/raspberrypi,pico-rng.yaml @@ -0,0 +1,8 @@ +# Copyright (c) 2024 Xudong Zheng +# SPDX-License-Identifier: Apache-2.0 + +description: Raspberry Pi Pico RNG/Entropy + +compatible: "raspberrypi,pico-rng" + +include: base.yaml diff --git a/modules/hal_rpi_pico/CMakeLists.txt b/modules/hal_rpi_pico/CMakeLists.txt index 0898795a86d64f..373cf1588e5a0f 100644 --- a/modules/hal_rpi_pico/CMakeLists.txt +++ b/modules/hal_rpi_pico/CMakeLists.txt @@ -151,6 +151,18 @@ if(CONFIG_HAS_RPI_PICO) zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM ${common_dir}/hardware_claim/include) + zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_RAND + ${rp2_common_dir}/hardware_timer/timer.c + ${rp2_common_dir}/pico_rand/rand.c + ) + zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_RAND + ${common_dir}/pico_sync/include + ${common_dir}/pico_time/include + ${rp2_common_dir}/pico_rand/include + ${rp2_common_dir}/pico_time_adapter/include + ${rp2_common_dir}/pico_unique_id/include + ) + zephyr_library_sources_ifdef(CONFIG_SOC_SERIES_RP2350 ${rp2_common_dir}/pico_runtime_init/runtime_init.c) zephyr_include_directories_ifdef(CONFIG_SOC_SERIES_RP2350 diff --git a/modules/hal_rpi_pico/Kconfig b/modules/hal_rpi_pico/Kconfig index 3c37c84f4c0109..43e0d5ce6858b5 100644 --- a/modules/hal_rpi_pico/Kconfig +++ b/modules/hal_rpi_pico/Kconfig @@ -54,3 +54,8 @@ config PICOSDK_USE_RTC bool help Use the RTC driver from pico-sdk + +config PICOSDK_USE_RAND + bool + help + Use the "rand" driver from pico-sdk