2017-02-28 19:24:43 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 03:11:52 +08:00
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_trng
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/entropy.h>
|
2023-10-07 06:38:53 +08:00
|
|
|
#include <zephyr/random/random.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/init.h>
|
2017-02-28 19:24:43 +08:00
|
|
|
|
|
|
|
#include "fsl_trng.h"
|
|
|
|
|
2019-01-11 04:42:49 +08:00
|
|
|
struct mcux_entropy_config {
|
|
|
|
TRNG_Type *base;
|
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int entropy_mcux_trng_get_entropy(const struct device *dev,
|
|
|
|
uint8_t *buffer,
|
2020-05-28 00:26:57 +08:00
|
|
|
uint16_t length)
|
2017-02-28 19:24:43 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct mcux_entropy_config *config = dev->config;
|
2017-02-28 19:24:43 +08:00
|
|
|
status_t status;
|
|
|
|
|
2019-01-11 04:42:49 +08:00
|
|
|
status = TRNG_GetRandomData(config->base, buffer, length);
|
2017-02-28 19:24:43 +08:00
|
|
|
__ASSERT_NO_MSG(!status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static const struct entropy_driver_api entropy_mcux_trng_api_funcs = {
|
|
|
|
.get_entropy = entropy_mcux_trng_get_entropy
|
2017-02-28 19:24:43 +08:00
|
|
|
};
|
|
|
|
|
2019-01-11 04:42:49 +08:00
|
|
|
static struct mcux_entropy_config entropy_mcux_config = {
|
2020-03-25 03:11:52 +08:00
|
|
|
.base = (TRNG_Type *)DT_INST_REG_ADDR(0)
|
2019-01-11 04:42:49 +08:00
|
|
|
};
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int entropy_mcux_trng_init(const struct device *dev)
|
2017-02-28 19:24:43 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct mcux_entropy_config *config = dev->config;
|
2017-02-28 19:24:43 +08:00
|
|
|
trng_config_t conf;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
status = TRNG_GetDefaultConfig(&conf);
|
|
|
|
__ASSERT_NO_MSG(!status);
|
|
|
|
|
2019-01-11 04:42:49 +08:00
|
|
|
status = TRNG_Init(config->base, &conf);
|
2017-02-28 19:24:43 +08:00
|
|
|
__ASSERT_NO_MSG(!status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-11 05:22:41 +08:00
|
|
|
|
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
|
|
|
entropy_mcux_trng_init, NULL, NULL,
|
|
|
|
&entropy_mcux_config,
|
|
|
|
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
|
|
|
|
&entropy_mcux_trng_api_funcs);
|