1 /*
2 * Copyright 2019 NXP
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT openisa_rv32m1_trng
8
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/entropy.h>
11 #include <zephyr/random/random.h>
12 #include <zephyr/init.h>
13
14 #include "fsl_trng.h"
15
16 struct rv32m1_entropy_config {
17 TRNG_Type *base;
18 };
19
entropy_rv32m1_trng_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_rv32m1_trng_get_entropy(const struct device *dev,
21 uint8_t *buffer,
22 uint16_t length)
23 {
24 const struct rv32m1_entropy_config *config = dev->config;
25 status_t status;
26
27 ARG_UNUSED(dev);
28
29 status = TRNG_GetRandomData(config->base, buffer, length);
30 __ASSERT_NO_MSG(!status);
31
32 return 0;
33 }
34
35 static const struct entropy_driver_api entropy_rv32m1_trng_api_funcs = {
36 .get_entropy = entropy_rv32m1_trng_get_entropy
37 };
38
39 static struct rv32m1_entropy_config entropy_rv32m1_config = {
40 .base = (TRNG_Type *)DT_INST_REG_ADDR(0)
41 };
42
43 static int entropy_rv32m1_trng_init(const struct device *);
44
45 DEVICE_DT_INST_DEFINE(0,
46 entropy_rv32m1_trng_init, NULL,
47 NULL, &entropy_rv32m1_config,
48 PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
49 &entropy_rv32m1_trng_api_funcs);
50
entropy_rv32m1_trng_init(const struct device * dev)51 static int entropy_rv32m1_trng_init(const struct device *dev)
52 {
53 const struct rv32m1_entropy_config *config = dev->config;
54 trng_config_t conf;
55 status_t status;
56
57 ARG_UNUSED(dev);
58
59 status = TRNG_GetDefaultConfig(&conf);
60 __ASSERT_NO_MSG(!status);
61
62 status = TRNG_Init(config->base, &conf);
63 __ASSERT_NO_MSG(!status);
64
65 return 0;
66 }
67