2020-02-21 22:22:05 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Lemonbeat GmbH
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-01 23:15:13 +08:00
|
|
|
#define DT_DRV_COMPAT silabs_gecko_trng
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/entropy.h>
|
2020-02-21 22:22:05 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include "soc.h"
|
|
|
|
#include "em_cmu.h"
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static void entropy_gecko_trng_read(uint8_t *output, size_t len)
|
2020-02-21 22:22:05 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t tmp;
|
|
|
|
uint32_t *data = (uint32_t *) output;
|
2020-02-21 22:22:05 +08:00
|
|
|
|
|
|
|
/* Read known good available data. */
|
|
|
|
while (len >= 4) {
|
|
|
|
*data++ = TRNG0->FIFO;
|
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
if (len > 0) {
|
|
|
|
/* Handle the case where len is not a multiple of 4
|
|
|
|
* and FIFO data is available.
|
|
|
|
*/
|
|
|
|
tmp = TRNG0->FIFO;
|
2020-05-28 00:26:57 +08:00
|
|
|
memcpy(data, (const uint8_t *) &tmp, len);
|
2020-02-21 22:22:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int entropy_gecko_trng_get_entropy(const struct device *dev,
|
|
|
|
uint8_t *buffer,
|
|
|
|
uint16_t length)
|
2020-02-21 22:22:05 +08:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
size_t available;
|
|
|
|
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
while (length) {
|
|
|
|
available = TRNG0->FIFOLEVEL * 4;
|
|
|
|
if (available == 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = SL_MIN(length, available);
|
|
|
|
entropy_gecko_trng_read(buffer, count);
|
|
|
|
buffer += count;
|
|
|
|
length -= count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int entropy_gecko_trng_get_entropy_isr(const struct device *dev,
|
|
|
|
uint8_t *buf,
|
|
|
|
uint16_t len, uint32_t flags)
|
2020-03-20 22:59:02 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if ((flags & ENTROPY_BUSYWAIT) == 0U) {
|
|
|
|
|
|
|
|
/* No busy wait; return whatever data is available. */
|
|
|
|
size_t count;
|
|
|
|
size_t available = TRNG0->FIFOLEVEL * 4;
|
|
|
|
|
|
|
|
if (available == 0) {
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
count = SL_MIN(len, available);
|
|
|
|
entropy_gecko_trng_read(buf, count);
|
|
|
|
return count;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Allowed to busy-wait */
|
|
|
|
int ret = entropy_gecko_trng_get_entropy(dev, buf, len);
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
/* Data retrieved successfully. */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 22:28:25 +08:00
|
|
|
static int entropy_gecko_trng_init(const struct device *dev)
|
2020-02-21 22:22:05 +08:00
|
|
|
{
|
|
|
|
/* Enable the TRNG0 clock. */
|
|
|
|
CMU_ClockEnable(cmuClock_TRNG0, true);
|
|
|
|
|
|
|
|
/* Enable TRNG0. */
|
|
|
|
TRNG0->CONTROL = TRNG_CONTROL_ENABLE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct entropy_driver_api entropy_gecko_trng_api_funcs = {
|
2020-03-20 22:59:02 +08:00
|
|
|
.get_entropy = entropy_gecko_trng_get_entropy,
|
|
|
|
.get_entropy_isr = entropy_gecko_trng_get_entropy_isr
|
2020-02-21 22:22:05 +08:00
|
|
|
};
|
|
|
|
|
2020-12-11 00:51:54 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
2021-04-28 16:39:21 +08:00
|
|
|
entropy_gecko_trng_init, NULL,
|
2020-12-11 00:51:54 +08:00
|
|
|
NULL, NULL,
|
2021-10-20 05:14:27 +08:00
|
|
|
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
|
2020-02-21 22:22:05 +08:00
|
|
|
&entropy_gecko_trng_api_funcs);
|