diff --git a/CODEOWNERS b/CODEOWNERS index 00dec63ef44..d359bbd1757 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -121,6 +121,7 @@ /drivers/dma/*sam0* @Sizurka /drivers/dma/dma_stm32* @cybertale /drivers/eeprom/ @henrikbrixandersen +/drivers/entropy/*rv32m1* @MaureenHelm /drivers/espi/ @albertofloyd @franciscomunoz @scottwcpg /drivers/ps2/ @albertofloyd @franciscomunoz @scottwcpg /drivers/kscan/ @albertofloyd @franciscomunoz @scottwcpg diff --git a/drivers/entropy/CMakeLists.txt b/drivers/entropy/CMakeLists.txt index 2b26f089778..902143fc77c 100644 --- a/drivers/entropy/CMakeLists.txt +++ b/drivers/entropy/CMakeLists.txt @@ -11,3 +11,4 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_SAM_RNG entropy_sam.c) zephyr_library_sources_ifdef(CONFIG_ENTROPY_STM32_RNG entropy_stm32.c) zephyr_library_sources_ifdef(CONFIG_FAKE_ENTROPY_NATIVE_POSIX fake_entropy_native_posix.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE entropy_handlers.c) +zephyr_library_sources_ifdef(CONFIG_ENTROPY_RV32M1_TRNG entropy_rv32m1_trng.c) diff --git a/drivers/entropy/Kconfig b/drivers/entropy/Kconfig index c2351b8a9cc..e1c4e81dc83 100644 --- a/drivers/entropy/Kconfig +++ b/drivers/entropy/Kconfig @@ -17,6 +17,7 @@ source "drivers/entropy/Kconfig.esp32" source "drivers/entropy/Kconfig.nrf5" source "drivers/entropy/Kconfig.sam" source "drivers/entropy/Kconfig.native_posix" +source "drivers/entropy/Kconfig.rv32m1" config ENTROPY_HAS_DRIVER bool diff --git a/drivers/entropy/Kconfig.rv32m1 b/drivers/entropy/Kconfig.rv32m1 new file mode 100644 index 00000000000..53032fd4a58 --- /dev/null +++ b/drivers/entropy/Kconfig.rv32m1 @@ -0,0 +1,13 @@ +# Kconfig.rv32m1 - RV32M1 entropy generator driver configuration +# +# Copyright 2019 NXP +# +# SPDX-License-Identifier: Apache-2.0 + +menuconfig ENTROPY_RV32M1_TRNG + bool "RV32M1 TRNG driver" + depends on ENTROPY_GENERATOR && SOC_OPENISA_RV32M1_RISCV32 + select ENTROPY_HAS_DRIVER + help + This option enables the true random number generator (TRNG) + driver based on the RV32M1 TRNG driver. diff --git a/drivers/entropy/entropy_rv32m1_trng.c b/drivers/entropy/entropy_rv32m1_trng.c new file mode 100644 index 00000000000..3f2fde3b244 --- /dev/null +++ b/drivers/entropy/entropy_rv32m1_trng.c @@ -0,0 +1,62 @@ +/* + * Copyright 2019 NXP + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#include "fsl_trng.h" + +struct rv32m1_entropy_config { + TRNG_Type *base; +}; + +static int entropy_rv32m1_trng_get_entropy(struct device *dev, u8_t *buffer, + u16_t length) +{ + const struct rv32m1_entropy_config *config = dev->config->config_info; + status_t status; + + ARG_UNUSED(dev); + + status = TRNG_GetRandomData(config->base, buffer, length); + __ASSERT_NO_MSG(!status); + + return 0; +} + +static const struct entropy_driver_api entropy_rv32m1_trng_api_funcs = { + .get_entropy = entropy_rv32m1_trng_get_entropy +}; + +static struct rv32m1_entropy_config entropy_rv32m1_config = { + .base = TRNG +}; + +static int entropy_rv32m1_trng_init(struct device *); + +DEVICE_AND_API_INIT(entropy_rv32m1_trng, CONFIG_ENTROPY_NAME, + entropy_rv32m1_trng_init, NULL, &entropy_rv32m1_config, + PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, + &entropy_rv32m1_trng_api_funcs); + +static int entropy_rv32m1_trng_init(struct device *dev) +{ + const struct rv32m1_entropy_config *config = dev->config->config_info; + trng_config_t conf; + status_t status; + + ARG_UNUSED(dev); + + status = TRNG_GetDefaultConfig(&conf); + __ASSERT_NO_MSG(!status); + + status = TRNG_Init(config->base, &conf); + __ASSERT_NO_MSG(!status); + + return 0; +}