drivers: trng: Add TRNG driver for MAX32xxx MCUs

Added TRNG driver for MAX32xxx MCUs

Signed-off-by: Sadik Ozer <sadik.ozer@analog.com>
This commit is contained in:
Sadik Ozer
2023-11-01 13:51:40 +03:00
committed by Anas Nashif
parent f6b10bb7ac
commit 915ce7548a
4 changed files with 101 additions and 0 deletions

View File

@@ -33,5 +33,6 @@ 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_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)
zephyr_library_link_libraries_ifdef(CONFIG_BUILD_WITH_TFM tfm_api)

View File

@@ -36,6 +36,7 @@ source "drivers/entropy/Kconfig.neorv32"
source "drivers/entropy/Kconfig.bt_hci"
source "drivers/entropy/Kconfig.psa_crypto"
source "drivers/entropy/Kconfig.npcx"
source "drivers/entropy/Kconfig.max32"
config ENTROPY_HAS_DRIVER
bool

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2023-2024 Analog Devices, Inc.
# SPDX-License-Identifier: Apache-2.0
config ENTROPY_MAX32_TRNG
bool "ADI MAX32XXX MCU Family True Random Number Generator (TRNG) Driver"
default y
depends on DT_HAS_ADI_MAX32_TRNG_ENABLED
select ENTROPY_HAS_DRIVER
help
Enable True Random Number Generator (TRNG) driver for ADI MAX32XXX MCUs.

View File

@@ -0,0 +1,89 @@
/*
* Copyright (c) 2023-2024 Analog Devices, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <zephyr/sys/util.h>
#define DT_DRV_COMPAT adi_max32_trng
#include <zephyr/device.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/drivers/clock_control/adi_max32_clock_control.h>
#include <wrap_max32_trng.h>
struct max32_trng_config {
const struct device *clock;
struct max32_perclk perclk;
};
static int api_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len)
{
return MXC_TRNG_Random(buf, len);
}
static int api_get_entropy_isr(const struct device *dev, uint8_t *buf, uint16_t len, uint32_t flags)
{
int ret = 0;
if ((flags & ENTROPY_BUSYWAIT) == 0) {
uint32_t temp;
uint32_t copy_len;
uint32_t count = 0;
while (len) {
ret = Wrap_MXC_TRNG_RandomInt_NonBlocking(&temp);
if (ret != 0) {
break; /* Data not ready do not wait */
}
copy_len = MIN(len, 4);
memcpy(buf, (uint8_t *)&temp, copy_len);
len -= copy_len;
buf += copy_len;
count += copy_len;
}
/* User would like to read len bytes but in non-blocking mode
* the function might read less, in that case return value will be
* number of bytes read, if its 0 that means no data reads function
* will return -ENODATA
*/
ret = count ? count : -ENODATA;
} else {
/* Allowed to busy-wait */
ret = api_get_entropy(dev, buf, len);
if (ret == 0) {
ret = len; /* Data retrieved successfully. */
}
}
return ret;
}
static const struct entropy_driver_api entropy_max32_api = {.get_entropy = api_get_entropy,
.get_entropy_isr = api_get_entropy_isr};
static int entropy_max32_init(const struct device *dev)
{
int ret;
const struct max32_trng_config *cfg = dev->config;
/* Enable clock */
ret = clock_control_on(cfg->clock, (clock_control_subsys_t)&cfg->perclk);
return ret;
}
static const struct max32_trng_config max32_trng_cfg = {
.clock = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(0)),
.perclk.bus = DT_INST_CLOCKS_CELL(0, offset),
.perclk.bit = DT_INST_CLOCKS_CELL(0, bit),
};
DEVICE_DT_INST_DEFINE(0, entropy_max32_init, NULL, NULL, &max32_trng_cfg, PRE_KERNEL_1,
CONFIG_ENTROPY_INIT_PRIORITY, &entropy_max32_api);