Skip to content

Commit

Permalink
drivers: entropy: Introduce SiWx91x entropy driver
Browse files Browse the repository at this point in the history
Support for random number generator is required for most of the
cryptographic operations, including support for WiFi and TLS.

This driver has been tested with tests/drivers/entropy:

    *** Booting Zephyr OS build v3.7.0-4339-g1ec5ce05f9f8 ***
    Running TESTSUITE entropy_api
    ===================================================================
    START - test_entropy_get_entropy
    random device is 0x8217298, name is rng@45090000
      0x93
      0x3e
      0xf1
      0x68
      0xd4
      0x22
      0xbf
      0x4d
      0xad
     PASS - test_entropy_get_entropy in 0.012 seconds
    ===================================================================
    TESTSUITE entropy_api succeeded

    ------ TESTSUITE SUMMARY START ------

    SUITE PASS - 100.00% [entropy_api]: pass = 1, fail = 0, skip = 0 ...
     - PASS - [entropy_api.test_entropy_get_entropy] duration = 0.01 ...

    ------ TESTSUITE SUMMARY END ------

    ===================================================================
    RunID: d1547c805699201af769cb01331efcce
    PROJECT EXECUTION SUCCESSFUL

Co-authored-by: Tibor Laczko <[email protected]>
Signed-off-by: Tibor Laczko <[email protected]>
Signed-off-by: Jérôme Pouiller <[email protected]>
  • Loading branch information
2 people authored and kartben committed Feb 11, 2025
1 parent 8575bc0 commit 938fb87
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_TRNG entropy_gecko_trng.c
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NEORV32_TRNG entropy_neorv32_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_BT_HCI entropy_bt_hci.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_GECKO_SE entropy_gecko_se.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX91X entropy_silabs_siwx91x.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_PSA_CRYPTO_RNG entropy_psa_crypto.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_NPCX_DRBG entropy_npcx_drbg.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_MAX32_TRNG entropy_max32.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ source "drivers/entropy/Kconfig.native_posix"
source "drivers/entropy/Kconfig.rv32m1"
source "drivers/entropy/Kconfig.litex"
source "drivers/entropy/Kconfig.gecko"
source "drivers/entropy/Kconfig.siwx91x"
source "drivers/entropy/Kconfig.neorv32"
source "drivers/entropy/Kconfig.bt_hci"
source "drivers/entropy/Kconfig.psa_crypto"
Expand Down
11 changes: 11 additions & 0 deletions drivers/entropy/Kconfig.siwx91x
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) 2024 Silicon Laboratories Inc.
# SPDX-License-Identifier: Apache-2.0

config ENTROPY_SILABS_SIWX91X
bool "SiWx91x RNG driver"
default y
depends on DT_HAS_SILABS_SIWX91X_RNG_ENABLED
select ENTROPY_HAS_DRIVER
help
Enable hardware Random Number Generator embedded on Silicon Labs
SiWx91x chips.
61 changes: 61 additions & 0 deletions drivers/entropy/entropy_silabs_siwx91x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 Silicon Laboratories Inc.
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT silabs_siwx91x_rng

#include <zephyr/drivers/entropy.h>

#include "rsi_rom_rng.h"
#include "rsi_rom_clks.h"

static int siwx91x_get_entropy_isr(const struct device *dev, uint8_t *buffer,
uint16_t length, uint32_t flags)
{
uint32_t u32_count = length / sizeof(uint32_t);
uint32_t u8_count = u32_count * sizeof(uint32_t);
uint32_t u8_remainder = length - u8_count;
uint32_t swap_space;

if (!(flags & ENTROPY_BUSYWAIT)) {
return -ENOTSUP;
}
RSI_RNG_GetBytes((void *)dev->config, (uint32_t *)buffer, u32_count);
if (length % sizeof(uint32_t)) {
RSI_RNG_GetBytes((void *)dev->config, &swap_space, 1);
memcpy(buffer + u8_count, &swap_space, u8_remainder);
}

return 0;
}

static int siwx91x_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
{
return siwx91x_get_entropy_isr(dev, buffer, length, ENTROPY_BUSYWAIT);
}

static int siwx91x_init(const struct device *dev)
{
int ret;

ret = RSI_CLK_PeripheralClkEnable1(M4CLK, HWRNG_PCLK_ENABLE);
if (ret) {
return -EIO;
}
ret = RSI_RNG_Start((void *)dev->config, RSI_RNG_TRUE_RANDOM);
if (ret) {
return -EIO;
}
return 0;
}

static DEVICE_API(entropy, siwx91x_api) = {
.get_entropy = siwx91x_get_entropy,
.get_entropy_isr = siwx91x_get_entropy_isr,
};

#define SIWX91X_RNG_INIT(idx) \
DEVICE_DT_INST_DEFINE(idx, siwx91x_init, NULL, NULL, (void *)DT_INST_REG_ADDR(idx), \
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, &siwx91x_api);

DT_INST_FOREACH_STATUS_OKAY(SIWX91X_RNG_INIT)
12 changes: 12 additions & 0 deletions dts/bindings/rng/silabs,siwx91x-rng.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) 2024 Silicon Laboratories Inc.
# SPDX-License-Identifier: Apache-2.0

description: Hardware Random Number Generator embedded on Silabs SiWx91x chips

compatible: "silabs,siwx91x-rng"

include: base.yaml

properties:
reg:
required: true
4 changes: 4 additions & 0 deletions modules/hal_silabs/wiseconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ zephyr_library_sources(
${WISECONNECT_DIR}/components/device/silabs/si91x/mcu/core/chip/src/iPMU_prog/iPMU_dotc/rsi_system_config_917.c
)

zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX91X
${WISECONNECT_DIR}/components/device/silabs/si91x/mcu/drivers/peripheral_drivers/src/rsi_rng.c
)

zephyr_linker_sources(ROM_SECTIONS linker/code_classification_text.ld)
zephyr_linker_sources(RAMFUNC_SECTION linker/code_classification_ramfunc.ld)

0 comments on commit 938fb87

Please sign in to comment.