1 /*
2 * Copyright (c) 2017 Linaro Limited.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT nxp_lpc_rng
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_rng.h"
15
16 struct mcux_entropy_config {
17 RNG_Type *base;
18 };
19
entropy_mcux_rng_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_mcux_rng_get_entropy(const struct device *dev,
21 uint8_t *buffer,
22 uint16_t length)
23 {
24 const struct mcux_entropy_config *config = dev->config;
25 status_t status;
26
27 status = RNG_GetRandomData(config->base, buffer, length);
28 __ASSERT_NO_MSG(!status);
29
30 return 0;
31 }
32
33 static const struct entropy_driver_api entropy_mcux_rng_api_funcs = {
34 .get_entropy = entropy_mcux_rng_get_entropy
35 };
36
37 static const struct mcux_entropy_config entropy_mcux_config = {
38 .base = (RNG_Type *)DT_INST_REG_ADDR(0)
39 };
40
entropy_mcux_rng_init(const struct device * dev)41 static int entropy_mcux_rng_init(const struct device *dev)
42 {
43 ARG_UNUSED(dev);
44
45 RNG_Init(entropy_mcux_config.base);
46
47 return 0;
48 }
49
50 DEVICE_DT_INST_DEFINE(0,
51 entropy_mcux_rng_init, NULL, NULL,
52 &entropy_mcux_config,
53 PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
54 &entropy_mcux_rng_api_funcs);
55