From b36a31fd7ab19608a57fca787da75c8f747d7e23 Mon Sep 17 00:00:00 2001 From: Mateusz Sierszulski Date: Tue, 13 Sep 2022 13:30:52 +0200 Subject: [PATCH] drivers: entropy: Add Gecko trng driver for EFR32BG22 This commit enables entropy driver on EFR32BG22 SoC. Signed-off-by: Mateusz Sierszulski --- .../arm/efr32bg_sltb010a/efr32bg_sltb010a.dts | 4 ++++ drivers/entropy/Kconfig.gecko | 1 + drivers/entropy/entropy_gecko_trng.c | 21 +++++++++++++++++++ dts/arm/silabs/efr32bg22.dtsi | 8 +++++++ soc/arm/silabs_exx32/Kconfig | 6 ++++++ 5 files changed, 40 insertions(+) diff --git a/boards/arm/efr32bg_sltb010a/efr32bg_sltb010a.dts b/boards/arm/efr32bg_sltb010a/efr32bg_sltb010a.dts index 1ec30d47366..a9b292afb03 100644 --- a/boards/arm/efr32bg_sltb010a/efr32bg_sltb010a.dts +++ b/boards/arm/efr32bg_sltb010a/efr32bg_sltb010a.dts @@ -115,3 +115,7 @@ &stimer0 { status = "okay"; }; + +&trng { + status = "okay"; +}; diff --git a/drivers/entropy/Kconfig.gecko b/drivers/entropy/Kconfig.gecko index 35be96dd7d4..8de97483f14 100644 --- a/drivers/entropy/Kconfig.gecko +++ b/drivers/entropy/Kconfig.gecko @@ -9,6 +9,7 @@ config ENTROPY_GECKO_TRNG default y depends on DT_HAS_SILABS_GECKO_TRNG_ENABLED select ENTROPY_HAS_DRIVER + select CRYPTO_ACC_GECKO_TRNG if SOC_SERIES_EFR32BG22 help This option enables the true random number generator driver based on the TRNG. diff --git a/drivers/entropy/entropy_gecko_trng.c b/drivers/entropy/entropy_gecko_trng.c index 47855e3d60c..2bdd1f1a9c7 100644 --- a/drivers/entropy/entropy_gecko_trng.c +++ b/drivers/entropy/entropy_gecko_trng.c @@ -13,6 +13,7 @@ static void entropy_gecko_trng_read(uint8_t *output, size_t len) { +#ifndef CONFIG_CRYPTO_ACC_GECKO_TRNG uint32_t tmp; uint32_t *data = (uint32_t *) output; @@ -28,6 +29,9 @@ static void entropy_gecko_trng_read(uint8_t *output, size_t len) tmp = TRNG0->FIFO; memcpy(data, (const uint8_t *) &tmp, len); } +#else + memcpy(output, ((const uint8_t *) CRYPTOACC_RNGOUT_FIFO_S_MEM_BASE), len); +#endif } static int entropy_gecko_trng_get_entropy(const struct device *dev, @@ -40,7 +44,11 @@ static int entropy_gecko_trng_get_entropy(const struct device *dev, ARG_UNUSED(dev); while (length) { +#ifndef CONFIG_CRYPTO_ACC_GECKO_TRNG available = TRNG0->FIFOLEVEL * 4; +#else + available = CRYPTOACC_RNGCTRL->FIFOLEVEL * 4; +#endif if (available == 0) { return -EINVAL; } @@ -63,7 +71,11 @@ static int entropy_gecko_trng_get_entropy_isr(const struct device *dev, /* No busy wait; return whatever data is available. */ size_t count; +#ifndef CONFIG_CRYPTO_ACC_GECKO_TRNG size_t available = TRNG0->FIFOLEVEL * 4; +#else + size_t available = CRYPTOACC_RNGCTRL->FIFOLEVEL * 4; +#endif if (available == 0) { return -ENODATA; @@ -87,10 +99,19 @@ static int entropy_gecko_trng_get_entropy_isr(const struct device *dev, static int entropy_gecko_trng_init(const struct device *dev) { /* Enable the TRNG0 clock. */ +#ifndef CONFIG_CRYPTO_ACC_GECKO_TRNG CMU_ClockEnable(cmuClock_TRNG0, true); /* Enable TRNG0. */ TRNG0->CONTROL = TRNG_CONTROL_ENABLE; +#else + /* Enable the CRYPTO ACC clock. */ + CMU_ClockEnable(cmuClock_CRYPTOACC, true); + + /* Enable TRNG */ + CRYPTOACC_RNGCTRL->RNGCTRL |= CRYPTOACC_RNGCTRL_ENABLE; +#endif + return 0; } diff --git a/dts/arm/silabs/efr32bg22.dtsi b/dts/arm/silabs/efr32bg22.dtsi index 93dccd5ec2a..1e33462f4c1 100644 --- a/dts/arm/silabs/efr32bg22.dtsi +++ b/dts/arm/silabs/efr32bg22.dtsi @@ -13,6 +13,7 @@ / { chosen { zephyr,flash-controller = &msc; + zephyr,entropy = &trng; }; power-states { @@ -69,6 +70,13 @@ status = "disabled"; }; + trng: trng@4c021000 { + compatible = "silabs,gecko-trng"; + reg = <0x4C021000 0x1000>; + status = "disabled"; + interrupts = <0x1 0x0>; + }; + i2c0: i2c@5a010000 { compatible = "silabs,gecko-i2c"; clock-frequency = ; diff --git a/soc/arm/silabs_exx32/Kconfig b/soc/arm/silabs_exx32/Kconfig index 8fde72bc3a4..5642ed74e61 100644 --- a/soc/arm/silabs_exx32/Kconfig +++ b/soc/arm/silabs_exx32/Kconfig @@ -134,6 +134,12 @@ choice SOC_GECKO_EMU_DCDC_MODE bool "Bypass" endchoice +config CRYPTO_ACC_GECKO_TRNG + bool + help + Enable Entropy driver based on the CRYPTO_ACC module for Silicon Labs + Gecko chips. + config SOC_GECKO_DEV_INIT bool help