1 /*
2  * Copyright (c) 2019 Alexander Wachter
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/hwinfo.h>
8 #include <string.h>
9 #include <fsl_sim.h>
10 #include <zephyr/sys/byteorder.h>
11 
12 #if defined(SIM_UIDH)
13 #define HWINFO_DEVICE_ID_LENGTH_H 1
14 #else
15 #define HWINFO_DEVICE_ID_LENGTH_H 0
16 #endif
17 #if (defined(FSL_FEATURE_SIM_HAS_UIDM) && FSL_FEATURE_SIM_HAS_UIDM)
18 #define HWINFO_DEVICE_ID_LENGTH_M 1
19 #else
20 #define HWINFO_DEVICE_ID_LENGTH_M 2
21 #endif
22 #define HWINFO_DEVICE_ID_LENGTH_L 1
23 
24 #define HWINFO_DEVICE_ID_LENGTH_TOTAL (HWINFO_DEVICE_ID_LENGTH_L + \
25 				       HWINFO_DEVICE_ID_LENGTH_M + \
26 				       HWINFO_DEVICE_ID_LENGTH_H)
27 
z_impl_hwinfo_get_device_id(uint8_t * buffer,size_t length)28 ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
29 {
30 	uint32_t id[HWINFO_DEVICE_ID_LENGTH_TOTAL];
31 	uint32_t *idp = id;
32 
33 #if defined(SIM_UIDH)
34 	*idp++ = sys_cpu_to_be32(SIM->UIDH);
35 #endif /* SIM_UIDH */
36 
37 #if (defined(FSL_FEATURE_SIM_HAS_UIDM) && FSL_FEATURE_SIM_HAS_UIDM)
38 	*idp++ = sys_cpu_to_be32(SIM->UIDM);
39 #else
40 	*idp++ = sys_cpu_to_be32(SIM->UIDMH);
41 	*idp++ = sys_cpu_to_be32(SIM->UIDML);
42 #endif /* defined(FSL_FEATURE_SIM_HAS_UIDM) && FSL_FEATURE_SIM_HAS_UIDM */
43 	*idp++ = sys_cpu_to_be32(SIM->UIDL);
44 
45 	if (length > sizeof(id)) {
46 		length = sizeof(id);
47 	}
48 
49 	memcpy(buffer, id, length);
50 
51 	return length;
52 }
53