From 55471982f68fed13671353585609507d2096bade Mon Sep 17 00:00:00 2001 From: Mohamed ElShahawi Date: Tue, 22 Oct 2019 10:04:13 +0200 Subject: [PATCH] esp32: drivers: Add Entropy Device tree support - Add TRNG module to esp32.dtsi - Updated the note about TRNG register address Signed-off-by: Mohamed ElShahawi --- boards/xtensa/esp32/esp32.dts | 4 ++++ drivers/entropy/Kconfig.esp32 | 1 + drivers/entropy/entropy_esp32.c | 17 ++++++++-------- dts/bindings/rng/espressif,esp32-trng.yaml | 23 ++++++++++++++++++++++ dts/xtensa/espressif/esp32.dtsi | 8 ++++++++ soc/xtensa/esp32/dts_fixup.h | 2 +- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 dts/bindings/rng/espressif,esp32-trng.yaml diff --git a/boards/xtensa/esp32/esp32.dts b/boards/xtensa/esp32/esp32.dts index 7b3f808ec8b..aab8efb17bb 100644 --- a/boards/xtensa/esp32/esp32.dts +++ b/boards/xtensa/esp32/esp32.dts @@ -70,3 +70,7 @@ sda-pin = <18>; scl-pin = <5>; }; + +&trng0 { + status = "okay"; +}; diff --git a/drivers/entropy/Kconfig.esp32 b/drivers/entropy/Kconfig.esp32 index 5e6ae85c014..c1eed88c08e 100644 --- a/drivers/entropy/Kconfig.esp32 +++ b/drivers/entropy/Kconfig.esp32 @@ -8,6 +8,7 @@ config ENTROPY_ESP32_RNG bool "ESP32 entropy number generator driver" depends on SOC_ESP32 select ENTROPY_HAS_DRIVER + select HAS_DTS_ENTROPY help This option enables the entropy number generator for ESP32 SoCs. diff --git a/drivers/entropy/entropy_esp32.c b/drivers/entropy/entropy_esp32.c index 3b93573b300..42fda8c86e0 100644 --- a/drivers/entropy/entropy_esp32.c +++ b/drivers/entropy/entropy_esp32.c @@ -9,14 +9,15 @@ static inline u32_t entropy_esp32_get_u32(void) { - /* The documentation specifies the random number generator at the - * following address, which is at odds with the SDK, that specifies - * it at 0x60035144. The fact that they're 0x200c0000 bytes apart - * (lower 16 bits are the same) suggests this might be the same - * register, just mirrored somewhere else in the address space. - * Confirmation is required. + /* + * APB Address: 0x60035144 (Safe,slower writes) + * DPORT Address: 0x3ff75144 (write bug, fast writes) + * In this case it won't make a difference because it is read only + * More info available at: + * https://www.esp32.com/viewtopic.php?f=2&t=3033&p=14227 + * also check: ECO and Workarounds for Bugs Document, point 3.3 */ - volatile u32_t *rng_data_reg = (u32_t *)0x3ff75144; + volatile u32_t *rng_data_reg = (u32_t *)DT_INST_0_ESPRESSIF_ESP32_TRNG_BASE_ADDRESS; /* Read just once. This is not optimal as the generator has * limited throughput due to scarce sources of entropy, specially @@ -53,7 +54,7 @@ static struct entropy_driver_api entropy_esp32_api_funcs = { .get_entropy = entropy_esp32_get_entropy }; -DEVICE_AND_API_INIT(entropy_esp32, CONFIG_ENTROPY_NAME, +DEVICE_AND_API_INIT(entropy_esp32, DT_INST_0_ESPRESSIF_ESP32_TRNG_LABEL, entropy_esp32_init, NULL, NULL, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &entropy_esp32_api_funcs); diff --git a/dts/bindings/rng/espressif,esp32-trng.yaml b/dts/bindings/rng/espressif,esp32-trng.yaml new file mode 100644 index 00000000000..0dc9fed56e0 --- /dev/null +++ b/dts/bindings/rng/espressif,esp32-trng.yaml @@ -0,0 +1,23 @@ +# Copyright (c) 2019, Mohamed ElShahawi +# SPDX-License-Identifier: Apache-2.0 + +title: Espressif ESP32 TRNG (True Random Number Generator) + +description: > + The TRNG use the noise in the Wi-Fi/BT RF system. + When Wi-Fi and BT are disabled, the random number generator will give out + pseudo-random numbers. + +compatible: "espressif,esp32-trng" + +include: base.yaml + +properties: + reg: + required: true + + interrupts: + required: false + + label: + required: true diff --git a/dts/xtensa/espressif/esp32.dtsi b/dts/xtensa/espressif/esp32.dtsi index 87dcd84a279..b8afb1f240b 100644 --- a/dts/xtensa/espressif/esp32.dtsi +++ b/dts/xtensa/espressif/esp32.dtsi @@ -92,5 +92,13 @@ label = "I2C_1"; status = "disabled"; }; + + trng0: trng@3ff75144 { + compatible = "espressif,esp32-trng"; + reg = <0x3FF75144 0x4>; + /* interrupts = <33 0>; - FIXME: Enable interrupts when interrupt-controller got supported in device tree */ + label = "TRNG_0"; + status = "disabled"; + }; }; }; diff --git a/soc/xtensa/esp32/dts_fixup.h b/soc/xtensa/esp32/dts_fixup.h index f17d2865fba..ed1aa15c6bc 100644 --- a/soc/xtensa/esp32/dts_fixup.h +++ b/soc/xtensa/esp32/dts_fixup.h @@ -7,5 +7,5 @@ /* SoC level DTS fixup file */ #define DT_CPU_CLOCK_FREQUENCY DT_CADENCE_TENSILICA_XTENSA_LX6_0_CLOCK_FREQUENCY - +#define CONFIG_ENTROPY_NAME DT_INST_0_ESPRESSIF_ESP32_TRNG_LABEL /* End of SoC Level DTS fixup file */