1 /*
2  * Copyright (c) 2021 Safran Passenger Innovations Germany GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT silabs_gecko_semailbox
8 
9  #include <zephyr/drivers/entropy.h>
10  #include <soc.h>
11  #include "em_cmu.h"
12  #include "sl_se_manager.h"
13  #include "sl_se_manager_entropy.h"
14 
15 
entropy_gecko_se_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)16 static int entropy_gecko_se_get_entropy(const struct device *dev,
17 					uint8_t *buffer,
18 					uint16_t length)
19 {
20 	ARG_UNUSED(dev);
21 
22 	int err = 0;
23 	sl_status_t status;
24 	sl_se_command_context_t cmd_ctx;
25 
26 	status = sl_se_init_command_context(&cmd_ctx);
27 
28 	if (status == SL_STATUS_OK) {
29 		status = sl_se_get_random(&cmd_ctx, buffer, length);
30 
31 		if (status != SL_STATUS_OK) {
32 			err = -EIO;
33 		}
34 
35 		sl_se_deinit_command_context(&cmd_ctx);
36 	} else {
37 		err = -EIO;
38 	}
39 
40 	return err;
41 }
42 
entropy_gecko_se_init(const struct device * dev)43 static int entropy_gecko_se_init(const struct device *dev)
44 {
45 	if (sl_se_init()) {
46 		return -EIO;
47 	}
48 
49 	return 0;
50 }
51 
52 static const struct entropy_driver_api entropy_gecko_se_api_funcs = {
53 	.get_entropy = entropy_gecko_se_get_entropy,
54 };
55 
56 #define GECKO_SE_INIT(n) \
57 	DEVICE_DT_INST_DEFINE(n, \
58 			      entropy_gecko_se_init, NULL, \
59 			      NULL, NULL, \
60 			      PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, \
61 			      &entropy_gecko_se_api_funcs); \
62 
63 DT_INST_FOREACH_STATUS_OKAY(GECKO_SE_INIT)
64