From 3e4e9496a1b6ad00a2a9526040d8a570068499b0 Mon Sep 17 00:00:00 2001 From: Yuriy Vynnychek Date: Mon, 26 Jul 2021 11:27:10 +0300 Subject: [PATCH] drivers: entropy: introduce new Telink B91 Entropy driver Entropy driver basic support for new Telink B91 platform. Signed-off-by: Yuriy Vynnychek --- CODEOWNERS | 1 + drivers/entropy/CMakeLists.txt | 1 + drivers/entropy/Kconfig | 1 + drivers/entropy/Kconfig.b91 | 11 +++++ drivers/entropy/entropy_b91_trng.c | 71 ++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 drivers/entropy/Kconfig.b91 create mode 100644 drivers/entropy/entropy_b91_trng.c diff --git a/CODEOWNERS b/CODEOWNERS index 50cb2f78984..dc8c93fdd1d 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -211,6 +211,7 @@ /drivers/edac/ @finikorg /drivers/eeprom/ @henrikbrixandersen /drivers/eeprom/eeprom_stm32.c @KwonTae-young +/drivers/entropy/*b91* @yurvyn /drivers/entropy/*rv32m1* @MaureenHelm /drivers/entropy/*gecko* @chrta /drivers/entropy/*litex* @mateusz-holenko @kgugala @pgielda diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt index 3d643b5d0d3..526a91a9b4b 100644 --- a/drivers/entropy/CMakeLists.txt +++ b/drivers/entropy/CMakeLists.txt @@ -2,6 +2,7 @@ zephyr_library() +zephyr_library_sources_ifdef(CONFIG_ENTROPY_TELINK_B91_TRNG entropy_b91_trng.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_RNG entropy_mcux_rng.c) diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig index 199257c7888..e73945b921a 100644 --- a/drivers/entropy/Kconfig +++ b/drivers/entropy/Kconfig @@ -10,6 +10,7 @@ menuconfig ENTROPY_GENERATOR if ENTROPY_GENERATOR +source "drivers/entropy/Kconfig.b91" source "drivers/entropy/Kconfig.cc13xx_cc26xx" source "drivers/entropy/Kconfig.mcux" source "drivers/entropy/Kconfig.stm32" diff --git a/drivers/entropy/Kconfig.b91 b/drivers/entropy/Kconfig.b91 new file mode 100644 index 00000000000..e59ad1ffb56 --- /dev/null +++ b/drivers/entropy/Kconfig.b91 @@ -0,0 +1,11 @@ +# Copyright (c) 2021 Telink Semiconductor +# SPDX-License-Identifier: Apache-2.0 + +# Telink B91 GPIO configuration options + +config ENTROPY_TELINK_B91_TRNG + bool "Telink B91 Entropy driver" + depends on SOC_RISCV_TELINK_B91 + select ENTROPY_HAS_DRIVER + help + Enable the B91 Entropy driver. diff --git a/drivers/entropy/entropy_b91_trng.c b/drivers/entropy/entropy_b91_trng.c new file mode 100644 index 00000000000..20cacea6436 --- /dev/null +++ b/drivers/entropy/entropy_b91_trng.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021 Telink Semiconductor + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT telink_b91_trng + +#include +#include +#include + + +/* API implementation: driver initialization */ +static int entropy_b91_trng_init(const struct device *dev) +{ + ARG_UNUSED(dev); + + trng_init(); + + return 0; +} + +/* API implementation: get_entropy */ +static int entropy_b91_trng_get_entropy(const struct device *dev, + uint8_t *buffer, uint16_t length) +{ + ARG_UNUSED(dev); + + uint32_t value = 0; + + while (length) { + value = trng_rand(); + + if (length >= sizeof(value)) { + memcpy(buffer, &value, sizeof(value)); + buffer += sizeof(value); + length -= sizeof(value); + } else { + memcpy(buffer, &value, length); + break; + } + } + + return 0; +} + +/* API implementation: get_entropy_isr */ +static int entropy_b91_trng_get_entropy_isr(const struct device *dev, + uint8_t *buffer, uint16_t length, + uint32_t flags) +{ + ARG_UNUSED(flags); + + /* No specific handling in case of running from ISR, just call standard API */ + entropy_b91_trng_get_entropy(dev, buffer, length); + + return 0; +} + +/* Entropy driver APIs structure */ +static const struct entropy_driver_api entropy_b91_trng_api = { + .get_entropy = entropy_b91_trng_get_entropy, + .get_entropy_isr = entropy_b91_trng_get_entropy_isr +}; + +/* Entropy driver registration */ +DEVICE_DT_INST_DEFINE(0, entropy_b91_trng_init, + NULL, NULL, NULL, + PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, + &entropy_b91_trng_api);