1 /*
2  * Copyright (c) 2017 Linaro Limited.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_kinetis_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 mcux_entropy_config {
17 	TRNG_Type *base;
18 };
19 
entropy_mcux_trng_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_mcux_trng_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 = TRNG_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_trng_api_funcs = {
34 	.get_entropy = entropy_mcux_trng_get_entropy
35 };
36 
37 static struct mcux_entropy_config entropy_mcux_config = {
38 	.base = (TRNG_Type *)DT_INST_REG_ADDR(0)
39 };
40 
entropy_mcux_trng_init(const struct device * dev)41 static int entropy_mcux_trng_init(const struct device *dev)
42 {
43 	const struct mcux_entropy_config *config = dev->config;
44 	trng_config_t conf;
45 	status_t status;
46 
47 	status = TRNG_GetDefaultConfig(&conf);
48 	__ASSERT_NO_MSG(!status);
49 
50 	status = TRNG_Init(config->base, &conf);
51 	__ASSERT_NO_MSG(!status);
52 
53 	return 0;
54 }
55 
56 DEVICE_DT_INST_DEFINE(0,
57 		    entropy_mcux_trng_init, NULL, NULL,
58 		    &entropy_mcux_config,
59 		    PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
60 		    &entropy_mcux_trng_api_funcs);
61