Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RNG driver #54

Merged
merged 4 commits into from
Oct 24, 2024
Merged
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
88 changes: 53 additions & 35 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name: Build
on:
pull_request:
types:
- edited
- opened
- reopened
- synchronize
- edited
- opened
- reopened
- synchronize

jobs:
build:
Expand All @@ -16,34 +16,52 @@ jobs:
os: [ubuntu-22.04, macos-14, windows-2022]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
path: zephyr-silabs

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Setup Zephyr project
uses: zephyrproject-rtos/action-zephyr-setup@v1
with:
app-path: zephyr-silabs
toolchains: arm-zephyr-eabi

- name: Fetch blobs
run: |
west blobs fetch hal_silabs

- name: Build hello world
working-directory: zephyr-silabs
shell: bash
run: |
if [ "${{ runner.os }}" = "Windows" ]; then
EXTRA_TWISTER_FLAGS="--short-build-path -O/tmp/twister-out"
fi
west twister --test sample.basic.helloworld -p siwx917_rb4338a -v --inline-logs $EXTRA_TWISTER_FLAGS
west twister --test sample.net.wifi -p siwx917_rb4338a -v --inline-logs -K $EXTRA_TWISTER_FLAGS
west twister --test sample.bluetooth.peripheral_hr -p siwx917_rb4338a -v --inline-logs -K $EXTRA_TWISTER_FLAGS
west twister --test sample.rail.simple_txrx -T samples -v --inline-logs $EXTRA_TWISTER_FLAGS
- name: Checkout
uses: actions/checkout@v4
with:
path: zephyr-silabs

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Setup Zephyr project
uses: zephyrproject-rtos/action-zephyr-setup@v1
with:
app-path: zephyr-silabs
toolchains: arm-zephyr-eabi

- name: Fetch blobs
working-directory: zephyr-silabs
shell: bash
run: |
west blobs fetch hal_silabs

- name: Build Entropy test
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s drivers.entropy -p siwx917_rb4338a -v --inline-logs

- name: Build Bluetooth samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s sample.bluetooth.peripheral_hr -p siwx917_rb4338a -v --inline-logs -K

- name: Build Rail samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s sample.rail.simple_txrx -T samples -v --inline-logs

- name: Build Wifi samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s sample.net.wifi -p siwx917_rb4338a -v --inline-logs -K
35 changes: 25 additions & 10 deletions .github/workflows/upstream-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,38 @@ jobs:
sed s/$Z_REV/main/ west.yml > west.yml.new
mv west.yml.new west.yml
west update

- name: Fetch blobs
working-directory: zephyr-silabs
shell: bash
run: |
west blobs fetch hal_silabs

- name: Build Bluetooth sample for xG27
- name: Build Entropy test
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -K -p xg27_dk2602a --test sample.bluetooth.peripheral_hr
west twister -s drivers.entropy -p siwx917_rb4338a -v --inline-logs -K

- name: Build hello world for siwx917_rb4338a
- name: Build Bluetooth samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s sample.bluetooth.peripheral_hr -p xg27_dk2602a -v --inline-logs -K
west twister -s sample.bluetooth.peripheral_hr -p siwx917_rb4338a -v --inline-logs -K

- name: Build Rail samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
west twister -s sample.rail.simple_txrx -T samples -v --inline-logs

- name: Build Wifi samples
continue-on-error: true
working-directory: zephyr-silabs
shell: bash
run: |
if [ "${{ runner.os }}" = "Windows" ]; then
EXTRA_TWISTER_FLAGS="--short-build-path -O/tmp/twister-out"
fi
west twister -T ../zephyr/samples -s sample.basic.helloworld -p siwx917_rb4338a -v --inline-logs $EXTRA_TWISTER_FLAGS
west twister -T ../zephyr/samples -s sample.net.wifi -p siwx917_rb4338a -v --inline-logs -K $EXTRA_TWISTER_FLAGS
west twister -T ../zephyr/samples -s sample.bluetooth.peripheral_hr -p siwx917_rb4338a -v --inline-logs -K $EXTRA_TWISTER_FLAGS
west twister -T samples -s sample.rail.simple_txrx -v --inline-logs $EXTRA_TWISTER_FLAGS
west twister -s sample.net.wifi -p siwx917_rb4338a -v --inline-logs -K
1 change: 1 addition & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

add_subdirectory(bluetooth)
add_subdirectory(entropy)
add_subdirectory(gpio)
add_subdirectory(pinctrl)
add_subdirectory(wifi)
5 changes: 5 additions & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2024 Silicon Laboratories Inc.
# SPDX-License-Identifier: Apache-2.0

zephyr_library_amend()
zephyr_library_sources_ifdef(CONFIG_ENTROPY_SILABS_SIWX917 entropy_silabs_siwx917.c)
8 changes: 8 additions & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Silicon Laboratories Inc.
# SPDX-License-Identifier: Apache-2.0

if ENTROPY_GENERATOR

rsource "Kconfig.siwx917"

endif
11 changes: 11 additions & 0 deletions drivers/entropy/Kconfig.siwx917
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_SIWX917
bool "SiWx917 RNG driver"
default y
depends on DT_HAS_SILABS_SIWX917_RNG_ENABLED
select ENTROPY_HAS_DRIVER
help
Enable hardware Random Number Generator embedded on Silicon Labs
SiWx917 chips.
61 changes: 61 additions & 0 deletions drivers/entropy/entropy_silabs_siwx917.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_siwx917_rng

#include <zephyr/drivers/entropy.h>

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

static int siwx917_get_entropy_isr(const struct device *dev, uint8_t *buffer,
uint16_t length, uint32_t flags)
{

Check notice on line 14 in drivers/entropy/entropy_silabs_siwx917.c

View workflow job for this annotation

GitHub Actions / build

You may want to run clang-format on this change

drivers/entropy/entropy_silabs_siwx917.c:14 -static int siwx917_get_entropy_isr(const struct device *dev, uint8_t *buffer, - uint16_t length, uint32_t flags) +static int siwx917_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 siwx917_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
{
return siwx917_get_entropy_isr(dev, buffer, length, ENTROPY_BUSYWAIT);
Copy link
Contributor

Choose a reason for hiding this comment

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

The API description says: "This call has to be thread safe to satisfy requirements of the random subsystem."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, I believe RSI_RNG_GetBytes() and all the subfunctions (mainly rng_get_bytes()) are thread safe. So I didn't keep the spinlock.

}

static int siwx917_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 const struct entropy_driver_api siwx917_api = {
.get_entropy = siwx917_get_entropy,
.get_entropy_isr = siwx917_get_entropy_isr,
};

#define SIWX917_RNG_INIT(idx) \
DEVICE_DT_INST_DEFINE(idx, siwx917_init, NULL, NULL, (void *)DT_INST_REG_ADDR(idx), \
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, &siwx917_api);

Check notice on line 59 in drivers/entropy/entropy_silabs_siwx917.c

View workflow job for this annotation

GitHub Actions / build

You may want to run clang-format on this change

drivers/entropy/entropy_silabs_siwx917.c:59 -#define SIWX917_RNG_INIT(idx) \ - DEVICE_DT_INST_DEFINE(idx, siwx917_init, NULL, NULL, (void *)DT_INST_REG_ADDR(idx), \ +#define SIWX917_RNG_INIT(idx) \ + DEVICE_DT_INST_DEFINE(idx, siwx917_init, NULL, NULL, (void *)DT_INST_REG_ADDR(idx), \

DT_INST_FOREACH_STATUS_OKAY(SIWX917_RNG_INIT)
9 changes: 9 additions & 0 deletions dts/arm/silabs/siwg917.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <zephyr/dt-bindings/spi/spi.h>

/ {
chosen {
zephyr,entropy = &rng0;
};

cpus {
#address-cells = <1>;
#size-cells = <0>;
Expand Down Expand Up @@ -202,6 +206,11 @@
interrupt-names = "i2c2";
status = "disabled";
};

rng0: rng@45090000 {
compatible = "silabs,siwx917-rng";
reg = <0x45090000 0x8>;
};
};
};

Expand Down
12 changes: 12 additions & 0 deletions dts/bindings/rng/silabs,siwx917-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 SiWx917 chips

compatible: "silabs,siwx917-rng"

include: base.yaml

properties:
reg:
required: true
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ manifest:
projects:
- name: hal_silabs
remote: silabs
revision: 2eb82930b376900f1b6949e218199ab33ba6f34c
revision: ca720524a33c4bb55446ceb7e94502e37153d941
path: modules/hal/silabs
- name: zephyr
remote: zephyrproject-rtos
Expand Down
Loading