2017-02-28 19:24:43 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
2019-06-26 03:53:49 +08:00
|
|
|
#include <drivers/entropy.h>
|
2017-10-14 06:45:02 +08:00
|
|
|
#include <random/rand32.h>
|
2017-02-28 19:24:43 +08:00
|
|
|
#include <init.h>
|
|
|
|
|
|
|
|
#include "fsl_trng.h"
|
|
|
|
|
2019-01-11 04:42:49 +08:00
|
|
|
struct mcux_entropy_config {
|
|
|
|
TRNG_Type *base;
|
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_trng_get_entropy(struct device *dev, u8_t *buffer,
|
|
|
|
u16_t length)
|
2017-02-28 19:24:43 +08:00
|
|
|
{
|
2019-01-11 04:42:49 +08:00
|
|
|
const struct mcux_entropy_config *config = dev->config->config_info;
|
2017-02-28 19:24:43 +08:00
|
|
|
status_t status;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
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 = {
|
|
|
|
.base = (TRNG_Type *)DT_ENTROPY_MCUX_TRNG_BASE_ADDRESS
|
|
|
|
};
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_trng_init(struct device *);
|
2017-02-28 19:24:43 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
DEVICE_AND_API_INIT(entropy_mcux_trng, CONFIG_ENTROPY_NAME,
|
2019-01-11 04:42:49 +08:00
|
|
|
entropy_mcux_trng_init, NULL, &entropy_mcux_config,
|
2018-05-23 06:08:38 +08:00
|
|
|
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2017-10-14 07:30:55 +08:00
|
|
|
&entropy_mcux_trng_api_funcs);
|
2017-02-28 19:24:43 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_mcux_trng_init(struct device *dev)
|
2017-02-28 19:24:43 +08:00
|
|
|
{
|
2019-01-11 04:42:49 +08:00
|
|
|
const struct mcux_entropy_config *config = dev->config->config_info;
|
2017-02-28 19:24:43 +08:00
|
|
|
trng_config_t conf;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|