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_get_entropy_isr(const struct device * dev,uint8_t * buf,uint16_t len,uint32_t flags)43 static int entropy_gecko_se_get_entropy_isr(const struct device *dev,
44 					    uint8_t *buf,
45 					    uint16_t len, uint32_t flags)
46 {
47 	return -ENOTSUP;
48 }
49 
entropy_gecko_se_init(const struct device * dev)50 static int entropy_gecko_se_init(const struct device *dev)
51 {
52 	if (sl_se_init()) {
53 		return -EIO;
54 	}
55 
56 	return 0;
57 }
58 
59 static const struct entropy_driver_api entropy_gecko_se_api_funcs = {
60 	.get_entropy = entropy_gecko_se_get_entropy,
61 	.get_entropy_isr = entropy_gecko_se_get_entropy_isr
62 };
63 
64 #define GECKO_SE_INIT(n) \
65 	DEVICE_DT_INST_DEFINE(n, \
66 			      entropy_gecko_se_init, NULL, \
67 			      NULL, NULL, \
68 			      PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, \
69 			      &entropy_gecko_se_api_funcs); \
70 
71 DT_INST_FOREACH_STATUS_OKAY(GECKO_SE_INIT)
72