diff --git a/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi b/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi index 12f9c5ed44d45..095d370069f0b 100644 --- a/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi +++ b/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi @@ -113,6 +113,10 @@ gpio0_lo: &gpio0 { status = "okay"; }; +&rng { + status = "okay"; +}; + zephyr_udc0: &usbd { status = "okay"; }; diff --git a/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml b/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml index f83082393e076..bebbc89110189 100644 --- a/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml +++ b/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml @@ -12,6 +12,7 @@ supported: - clock - counter - dma + - entropy - gpio - hwinfo - i2c diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt index 5e329a9c45769..d6a6c8192aef8 100644 --- a/drivers/entropy/CMakeLists.txt +++ b/drivers/entropy/CMakeLists.txt @@ -34,6 +34,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_NXP_ELE_TRNG entropy_nxp_ele. zephyr_library_sources_ifdef(CONFIG_ENTROPY_NXP_ELS_TRNG entropy_nxp_els_trng.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_PSA_CRYPTO_RNG entropy_psa_crypto.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_RENESAS_RA entropy_renesas_ra.c) +zephyr_library_sources_ifdef(CONFIG_ENTROPY_RPI_PICO_RNG entropy_rpi_pico.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX91X entropy_silabs_siwx91x.c) diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig index 59b93ccf022b2..c81ef28b256ca 100644 --- a/drivers/entropy/Kconfig +++ b/drivers/entropy/Kconfig @@ -40,6 +40,7 @@ source "drivers/entropy/Kconfig.nrf_cracen" source "drivers/entropy/Kconfig.nxp" source "drivers/entropy/Kconfig.psa_crypto" source "drivers/entropy/Kconfig.renesas_ra" +source "drivers/entropy/Kconfig.rpi_pico" source "drivers/entropy/Kconfig.rv32m1" source "drivers/entropy/Kconfig.sam" source "drivers/entropy/Kconfig.siwx91x" diff --git a/drivers/entropy/Kconfig.rpi_pico b/drivers/entropy/Kconfig.rpi_pico new file mode 100644 index 0000000000000..b7dc1a3436741 --- /dev/null +++ b/drivers/entropy/Kconfig.rpi_pico @@ -0,0 +1,14 @@ +# 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 + depends on SOC_SERIES_RP2350 + select ENTROPY_HAS_DRIVER + select PICOSDK_USE_CLAIM + select PICOSDK_USE_RAND + select PICOSDK_USE_TIMER diff --git a/drivers/entropy/entropy_rpi_pico.c b/drivers/entropy/entropy_rpi_pico.c new file mode 100644 index 0000000000000..47ee832a42be1 --- /dev/null +++ b/drivers/entropy/entropy_rpi_pico.c @@ -0,0 +1,43 @@ +/* + * 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) +{ + __ASSERT_NO_MSG(buf != NULL); + uint8_t *buf_bytes = buf; + + while (len > 0) { + uint64_t value; + size_t to_copy = MIN(sizeof(value), len); + + K_SPINLOCK(&entropy_lock) { + value = get_rand_64(); + } + memcpy(buf_bytes, &value, to_copy); + buf_bytes += to_copy; + len -= to_copy; + } + + return 0; +} + +static DEVICE_API(entropy, entropy_rpi_pico_api_funcs) = { + .get_entropy = entropy_rpi_pico_get_entropy +}; + +DEVICE_DT_INST_DEFINE(0, + NULL, NULL, NULL, NULL, + PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, + &entropy_rpi_pico_api_funcs); diff --git a/dts/bindings/rng/raspberrypi,pico-rng.yaml b/dts/bindings/rng/raspberrypi,pico-rng.yaml new file mode 100644 index 0000000000000..0f3e0789531d1 --- /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/dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi b/dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi index fc311e97ce5ba..4d30cc701e305 100644 --- a/dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi +++ b/dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi @@ -21,6 +21,10 @@ die-temp0 = &die_temp; }; + chosen { + zephyr,entropy = &rng; + }; + cpus { #address-cells = <1>; #size-cells = <0>; @@ -453,6 +457,12 @@ interrupt-names = "irq0", "irq1"; status = "disabled"; }; + + rng: rng@400f0000 { + compatible = "raspberrypi,pico-rng"; + reg = <0x400f0000 DT_SIZE_K(4)>; + status = "disabled"; + }; }; pinctrl: pin-controller { diff --git a/modules/hal_rpi_pico/CMakeLists.txt b/modules/hal_rpi_pico/CMakeLists.txt index 1c5c55e90133a..9a1a271a214a2 100644 --- a/modules/hal_rpi_pico/CMakeLists.txt +++ b/modules/hal_rpi_pico/CMakeLists.txt @@ -157,6 +157,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}/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_linker_sources_ifdef(CONFIG_PICOSDK_USE_RAND DATA_SECTIONS uninit_data.ld) + zephyr_include_directories_ifdef(CONFIG_RISCV ${common_dir}/pico_sync/include ${common_dir}/pico_time/include diff --git a/modules/hal_rpi_pico/Kconfig b/modules/hal_rpi_pico/Kconfig index 3c37c84f4c010..43e0d5ce6858b 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 diff --git a/modules/hal_rpi_pico/uninit_data.ld b/modules/hal_rpi_pico/uninit_data.ld new file mode 100644 index 0000000000000..c04ae32cd8a33 --- /dev/null +++ b/modules/hal_rpi_pico/uninit_data.ld @@ -0,0 +1,4 @@ +.uninitialized_data (NOLOAD): { + . = ALIGN(4); + *(.uninitialized_data*) +} > RAM