Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ gpio0_lo: &gpio0 {
status = "okay";
};

&rng {
status = "okay";
};

zephyr_udc0: &usbd {
status = "okay";
};
Expand Down
1 change: 1 addition & 0 deletions boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ supported:
- clock
- counter
- dma
- entropy
- gpio
- hwinfo
- i2c
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions drivers/entropy/Kconfig.rpi_pico
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions drivers/entropy/entropy_rpi_pico.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Xudong Zheng
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <pico/rand.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/spinlock.h>

#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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it idiomatic for an entropy source to actually provide more entropy than it has? It only has a boot-time true random value, while this function is fueled by xoroshiro128 algorithm: https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/host/pico_rand/rand.c

This is a question to subsys maintainers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/rp2_common/pico_rand/rand.c#L350-L354, each get_rand_64() invocation calls capture_additional_trng_samples() for additional entropy.

The end result is fed into xoroshiro128 as you mentioned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not sure if that matches subsystem expectations, but that sounds good. Please keep this thread open

}
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);
8 changes: 8 additions & 0 deletions dts/bindings/rng/raspberrypi,pico-rng.yaml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions dts/vendor/raspberrypi/rpi_pico/rp2350.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
die-temp0 = &die_temp;
};

chosen {
zephyr,entropy = &rng;
};

cpus {
#address-cells = <1>;
#size-cells = <0>;
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions modules/hal_rpi_pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions modules/hal_rpi_pico/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions modules/hal_rpi_pico/uninit_data.ld
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? Maybe SDK functions using this could be avoided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pico SDK mixes the TRNG output with uninitialized memory: https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/rp2_common/pico_rand/rand.c#L365-L374

To work around that, one would have to use capture_additional_trng_samples(), but that's not public API.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.uninitialized_data (NOLOAD): {

Check warning on line 1 in modules/hal_rpi_pico/uninit_data.ld

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Copyright missing

modules/hal_rpi_pico/uninit_data.ld:1 File has no SPDX-FileCopyrightText header, consider adding one.

Check warning on line 1 in modules/hal_rpi_pico/uninit_data.ld

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

License missing

modules/hal_rpi_pico/uninit_data.ld:1 File has no SPDX-License-Identifier header, consider adding one.
. = ALIGN(4);
*(.uninitialized_data*)
} > RAM
Loading