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_dna0
8 
9 #include <zephyr/drivers/hwinfo.h>
10 #include <soc.h>
11 #include <string.h>
12 #include <zephyr/device.h>
13 #include <zephyr/sys/util.h>
14 
z_impl_hwinfo_get_device_id(uint8_t * buffer,size_t length)15 ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
16 {
17 	uint32_t addr = DT_INST_REG_ADDR(0);
18 	ssize_t end = MIN(length, DT_INST_REG_ADDR(0) / 4 *
19 			CONFIG_LITEX_CSR_DATA_WIDTH / 8);
20 	for (int i = 0; i < end; i++) {
21 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
22 		buffer[i] = litex_read8(addr);
23 		addr += 4;
24 #elif CONFIG_LITEX_CSR_DATA_WIDTH == 32
25 		buffer[i] = (uint8_t)(litex_read32(addr) >> (addr % 4 * 8));
26 		addr += 1;
27 #else
28 #error Unsupported CSR data width
29 #endif
30 	}
31 	return end;
32 }
33