2022-08-11 03:22:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2022, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT nxp_imx_caam
|
|
|
|
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/entropy.h>
|
2023-10-07 06:38:53 +08:00
|
|
|
#include <zephyr/random/random.h>
|
2022-08-11 03:22:45 +08:00
|
|
|
#include <zephyr/init.h>
|
2023-05-06 00:44:41 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2022-08-11 03:22:45 +08:00
|
|
|
|
|
|
|
#include "fsl_caam.h"
|
|
|
|
|
|
|
|
struct mcux_entropy_config {
|
|
|
|
CAAM_Type *base;
|
|
|
|
};
|
|
|
|
|
2023-01-25 12:49:31 +08:00
|
|
|
static caam_job_ring_interface_t jrif0 __attribute__((__section__(".nocache")));
|
|
|
|
static uint8_t rng_buff_pool[CONFIG_ENTRY_MCUX_CAAM_POOL_SIZE]
|
|
|
|
__attribute__((__section__(".nocache")));
|
2022-08-11 03:22:45 +08:00
|
|
|
|
2023-05-06 00:44:41 +08:00
|
|
|
|
|
|
|
/* This semaphore is needed to prevent race condition to static variables in the HAL driver */
|
|
|
|
K_SEM_DEFINE(mcux_caam_sem, 1, 1)
|
|
|
|
|
2022-08-11 03:22:45 +08:00
|
|
|
static int entropy_mcux_caam_get_entropy(const struct device *dev,
|
|
|
|
uint8_t *buffer,
|
|
|
|
uint16_t length)
|
|
|
|
{
|
|
|
|
const struct mcux_entropy_config *config = dev->config;
|
|
|
|
status_t status;
|
|
|
|
caam_handle_t handle;
|
2023-01-25 12:49:31 +08:00
|
|
|
uint16_t read_length = 0;
|
|
|
|
uint16_t insert_idx = 0;
|
2023-05-06 00:44:41 +08:00
|
|
|
int ret = 0;
|
|
|
|
k_timeout_t sem_timeout = K_MSEC(10);
|
2022-08-11 03:22:45 +08:00
|
|
|
|
|
|
|
handle.jobRing = kCAAM_JobRing0;
|
|
|
|
|
2023-01-25 12:49:31 +08:00
|
|
|
/*
|
|
|
|
* The buffer passed to the CAAM RNG function needs to be in non-cache
|
|
|
|
* memory. Use an intermediate buffer to stage the data to the user
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
while (insert_idx < length) {
|
|
|
|
read_length = MIN(sizeof(rng_buff_pool), (length - insert_idx));
|
|
|
|
|
2023-05-06 00:44:41 +08:00
|
|
|
ret = k_sem_take(&mcux_caam_sem, sem_timeout);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-25 12:49:31 +08:00
|
|
|
status = CAAM_RNG_GetRandomData(
|
|
|
|
config->base, &handle, kCAAM_RngStateHandle0,
|
|
|
|
&rng_buff_pool[0], read_length, kCAAM_RngDataAny, NULL);
|
|
|
|
|
2023-05-06 00:44:41 +08:00
|
|
|
k_sem_give(&mcux_caam_sem);
|
|
|
|
|
2023-01-25 12:49:31 +08:00
|
|
|
memcpy(&buffer[insert_idx], &rng_buff_pool[0], read_length);
|
|
|
|
insert_idx += read_length;
|
|
|
|
}
|
2022-08-11 03:22:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct entropy_driver_api entropy_mcux_caam_api_funcs = {
|
|
|
|
.get_entropy = entropy_mcux_caam_get_entropy
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct mcux_entropy_config entropy_mcux_config = {
|
|
|
|
.base = (CAAM_Type *)DT_INST_REG_ADDR(0)
|
|
|
|
};
|
|
|
|
|
|
|
|
static int entropy_mcux_caam_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct mcux_entropy_config *config = dev->config;
|
|
|
|
caam_config_t conf;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
CAAM_GetDefaultConfig(&conf);
|
2023-01-25 12:49:31 +08:00
|
|
|
conf.jobRingInterface[0] = &jrif0;
|
2022-08-11 03:22:45 +08:00
|
|
|
|
|
|
|
status = CAAM_Init(config->base, &conf);
|
|
|
|
__ASSERT_NO_MSG(!status);
|
|
|
|
|
2022-08-19 07:46:07 +08:00
|
|
|
if (status != 0) {
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2022-08-11 03:22:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
|
|
|
entropy_mcux_caam_init, NULL, NULL,
|
|
|
|
&entropy_mcux_config,
|
|
|
|
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
|
|
|
|
&entropy_mcux_caam_api_funcs);
|