1 /*
2  * Copyright (c) 2019 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT litex_prbs
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/entropy.h>
11 #include <errno.h>
12 #include <zephyr/init.h>
13 #include <soc.h>
14 #include <string.h>
15 #include <zephyr/kernel.h>
16 
17 #define PRBS_STATUS     DT_INST_REG_ADDR(0)
18 #define PRBS_WIDTH      DT_INST_REG_SIZE(0)
19 
entropy_prbs_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)20 static int entropy_prbs_get_entropy(const struct device *dev, uint8_t *buffer,
21 					 uint16_t length)
22 {
23 	while (length > 0) {
24 		size_t to_copy;
25 		uint32_t value;
26 
27 		value = litex_read(PRBS_STATUS, PRBS_WIDTH);
28 		to_copy = MIN(length, sizeof(value));
29 
30 		memcpy(buffer, &value, to_copy);
31 		buffer += to_copy;
32 		length -= to_copy;
33 	}
34 	return 0;
35 }
36 
37 static const struct entropy_driver_api entropy_prbs_api = {
38 	.get_entropy = entropy_prbs_get_entropy
39 };
40 
41 DEVICE_DT_INST_DEFINE(0, NULL, NULL, NULL, NULL, PRE_KERNEL_1,
42 		      CONFIG_ENTROPY_INIT_PRIORITY, &entropy_prbs_api);
43