Skip to content

Commit 3e4e949

Browse files
Yuriy Vynnychekcarlescufi
authored andcommitted
drivers: entropy: introduce new Telink B91 Entropy driver
Entropy driver basic support for new Telink B91 platform. Signed-off-by: Yuriy Vynnychek <[email protected]>
1 parent d155129 commit 3e4e949

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
/drivers/edac/ @finikorg
212212
/drivers/eeprom/ @henrikbrixandersen
213213
/drivers/eeprom/eeprom_stm32.c @KwonTae-young
214+
/drivers/entropy/*b91* @yurvyn
214215
/drivers/entropy/*rv32m1* @MaureenHelm
215216
/drivers/entropy/*gecko* @chrta
216217
/drivers/entropy/*litex* @mateusz-holenko @kgugala @pgielda

drivers/entropy/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
zephyr_library()
44

5+
zephyr_library_sources_ifdef(CONFIG_ENTROPY_TELINK_B91_TRNG entropy_b91_trng.c)
56
zephyr_library_sources_ifdef(CONFIG_ENTROPY_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c)
67
zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c)
78
zephyr_library_sources_ifdef(CONFIG_ENTROPY_MCUX_RNG entropy_mcux_rng.c)

drivers/entropy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ menuconfig ENTROPY_GENERATOR
1010

1111
if ENTROPY_GENERATOR
1212

13+
source "drivers/entropy/Kconfig.b91"
1314
source "drivers/entropy/Kconfig.cc13xx_cc26xx"
1415
source "drivers/entropy/Kconfig.mcux"
1516
source "drivers/entropy/Kconfig.stm32"

drivers/entropy/Kconfig.b91

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2021 Telink Semiconductor
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Telink B91 GPIO configuration options
5+
6+
config ENTROPY_TELINK_B91_TRNG
7+
bool "Telink B91 Entropy driver"
8+
depends on SOC_RISCV_TELINK_B91
9+
select ENTROPY_HAS_DRIVER
10+
help
11+
Enable the B91 Entropy driver.

drivers/entropy/entropy_b91_trng.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2021 Telink Semiconductor
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT telink_b91_trng
8+
9+
#include <trng.h>
10+
#include <drivers/entropy.h>
11+
#include <string.h>
12+
13+
14+
/* API implementation: driver initialization */
15+
static int entropy_b91_trng_init(const struct device *dev)
16+
{
17+
ARG_UNUSED(dev);
18+
19+
trng_init();
20+
21+
return 0;
22+
}
23+
24+
/* API implementation: get_entropy */
25+
static int entropy_b91_trng_get_entropy(const struct device *dev,
26+
uint8_t *buffer, uint16_t length)
27+
{
28+
ARG_UNUSED(dev);
29+
30+
uint32_t value = 0;
31+
32+
while (length) {
33+
value = trng_rand();
34+
35+
if (length >= sizeof(value)) {
36+
memcpy(buffer, &value, sizeof(value));
37+
buffer += sizeof(value);
38+
length -= sizeof(value);
39+
} else {
40+
memcpy(buffer, &value, length);
41+
break;
42+
}
43+
}
44+
45+
return 0;
46+
}
47+
48+
/* API implementation: get_entropy_isr */
49+
static int entropy_b91_trng_get_entropy_isr(const struct device *dev,
50+
uint8_t *buffer, uint16_t length,
51+
uint32_t flags)
52+
{
53+
ARG_UNUSED(flags);
54+
55+
/* No specific handling in case of running from ISR, just call standard API */
56+
entropy_b91_trng_get_entropy(dev, buffer, length);
57+
58+
return 0;
59+
}
60+
61+
/* Entropy driver APIs structure */
62+
static const struct entropy_driver_api entropy_b91_trng_api = {
63+
.get_entropy = entropy_b91_trng_get_entropy,
64+
.get_entropy_isr = entropy_b91_trng_get_entropy_isr
65+
};
66+
67+
/* Entropy driver registration */
68+
DEVICE_DT_INST_DEFINE(0, entropy_b91_trng_init,
69+
NULL, NULL, NULL,
70+
PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
71+
&entropy_b91_trng_api);

0 commit comments

Comments
 (0)