2017-07-01 01:12:07 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 05:00:26 +08:00
|
|
|
#define DT_DRV_COMPAT espressif_esp32_trng
|
|
|
|
|
2017-07-01 01:12:07 +08:00
|
|
|
#include <string.h>
|
2019-06-26 03:53:49 +08:00
|
|
|
#include <drivers/entropy.h>
|
2017-07-01 01:12:07 +08:00
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static inline u32_t entropy_esp32_get_u32(void)
|
2017-07-01 01:12:07 +08:00
|
|
|
{
|
2019-10-22 16:04:13 +08:00
|
|
|
/*
|
|
|
|
* APB Address: 0x60035144 (Safe,slower writes)
|
|
|
|
* DPORT Address: 0x3ff75144 (write bug, fast writes)
|
|
|
|
* In this case it won't make a difference because it is read only
|
|
|
|
* More info available at:
|
|
|
|
* https://www.esp32.com/viewtopic.php?f=2&t=3033&p=14227
|
|
|
|
* also check: ECO and Workarounds for Bugs Document, point 3.3
|
2017-07-01 01:12:07 +08:00
|
|
|
*/
|
2020-03-25 05:00:26 +08:00
|
|
|
volatile u32_t *rng_data_reg = (u32_t *)DT_INST_REG_ADDR(0);
|
2017-07-01 01:12:07 +08:00
|
|
|
|
|
|
|
/* Read just once. This is not optimal as the generator has
|
|
|
|
* limited throughput due to scarce sources of entropy, specially
|
|
|
|
* with the radios turned off. Might want to revisit this.
|
|
|
|
*/
|
|
|
|
return *rng_data_reg;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_esp32_get_entropy(struct device *device, u8_t *buf, u16_t len)
|
2017-07-01 01:12:07 +08:00
|
|
|
{
|
|
|
|
while (len) {
|
2017-10-14 07:30:55 +08:00
|
|
|
u32_t v = entropy_esp32_get_u32();
|
2017-07-01 01:12:07 +08:00
|
|
|
|
|
|
|
if (len >= sizeof(v)) {
|
|
|
|
memcpy(buf, &v, sizeof(v));
|
|
|
|
|
|
|
|
buf += sizeof(v);
|
|
|
|
len -= sizeof(v);
|
|
|
|
} else {
|
|
|
|
memcpy(buf, &v, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static int entropy_esp32_init(struct device *device)
|
2017-07-01 01:12:07 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-14 07:30:55 +08:00
|
|
|
static struct entropy_driver_api entropy_esp32_api_funcs = {
|
|
|
|
.get_entropy = entropy_esp32_get_entropy
|
2017-07-01 01:12:07 +08:00
|
|
|
};
|
|
|
|
|
2020-03-25 05:00:26 +08:00
|
|
|
DEVICE_AND_API_INIT(entropy_esp32, DT_INST_LABEL(0),
|
2017-10-14 07:30:55 +08:00
|
|
|
entropy_esp32_init, NULL, NULL,
|
2017-07-01 01:12:07 +08:00
|
|
|
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
|
2017-10-14 07:30:55 +08:00
|
|
|
&entropy_esp32_api_funcs);
|