1 /*
2  * Copyright 2022 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_css_v2
8 
9 #include "mcuxClCss_Rng.h"
10 
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/entropy.h>
13 #include <zephyr/init.h>
14 #include <zephyr/random/random.h>
15 
entropy_mcux_css_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)16 static int entropy_mcux_css_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
17 {
18 	ARG_UNUSED(dev);
19 	uint8_t status = 0;
20 
21 	MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClCss_Prng_GetRandom(buffer, length));
22 	if ((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClCss_Prng_GetRandom) != token) ||
23 	    (result != MCUXCLCSS_STATUS_OK)) {
24 		status = -EAGAIN;
25 	}
26 	MCUX_CSSL_FP_FUNCTION_CALL_END();
27 
28 	__ASSERT_NO_MSG(!status);
29 
30 	return status;
31 }
32 
33 static const struct entropy_driver_api entropy_mcux_css_api_funcs = {
34 	.get_entropy = entropy_mcux_css_get_entropy
35 };
36 
entropy_mcux_css_init(const struct device * dev)37 static int entropy_mcux_css_init(const struct device *dev)
38 {
39 	ARG_UNUSED(dev);
40 	uint8_t status = 0;
41 
42 	MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(result, token, mcuxClCss_Enable_Async());
43 	if ((MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClCss_Enable_Async) != token) ||
44 	    (result != MCUXCLCSS_STATUS_OK_WAIT)) {
45 		status = -ENODEV;
46 	}
47 	MCUX_CSSL_FP_FUNCTION_CALL_END();
48 
49 	__ASSERT_NO_MSG(!status);
50 
51 	return status;
52 }
53 
54 DEVICE_DT_INST_DEFINE(0, entropy_mcux_css_init, NULL, NULL, NULL, PRE_KERNEL_1,
55 		      CONFIG_ENTROPY_INIT_PRIORITY, &entropy_mcux_css_api_funcs);
56