1 /*
2  * Copyright (c) 2023 Piotr Dymacz
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <string.h>
9 #include <zephyr/drivers/hwinfo.h>
10 #include <zephyr/sys/byteorder.h>
11 
12 #include <driverlib/sys_ctrl.h>
13 #include <inc/hw_ccfg.h>
14 #include <inc/hw_fcfg1.h>
15 #include <inc/hw_memmap.h>
16 
17 #ifdef CONFIG_HWINFO_CC13XX_CC26XX_USE_BLE_MAC
18 #define CC13XX_CC26XX_DEVID_SIZE	6
19 #else
20 #define CC13XX_CC26XX_DEVID_SIZE	8
21 #endif
22 
z_impl_hwinfo_get_device_id(uint8_t * buffer,size_t length)23 ssize_t z_impl_hwinfo_get_device_id(uint8_t *buffer, size_t length)
24 {
25 	uint8_t *mac;
26 
27 	if (IS_ENABLED(CONFIG_HWINFO_CC13XX_CC26XX_USE_BLE_MAC)) {
28 		if (IS_ENABLED(CONFIG_HWINFO_CC13XX_CC26XX_ALWAYS_USE_FACTORY_DEFAULT) ||
29 		    sys_read32(CCFG_BASE + CCFG_O_IEEE_BLE_0) == 0xFFFFFFFF ||
30 		    sys_read32(CCFG_BASE + CCFG_O_IEEE_BLE_1) == 0xFFFFFFFF) {
31 			mac = (uint8_t *)(FCFG1_BASE + FCFG1_O_MAC_BLE_0);
32 		} else {
33 			mac = (uint8_t *)(CCFG_BASE + CCFG_O_IEEE_BLE_0);
34 		}
35 	} else if (IS_ENABLED(CONFIG_HWINFO_CC13XX_CC26XX_USE_IEEE_MAC)) {
36 		if (IS_ENABLED(CONFIG_HWINFO_CC13XX_CC26XX_ALWAYS_USE_FACTORY_DEFAULT) ||
37 		    sys_read32(CCFG_BASE + CCFG_O_IEEE_MAC_0) == 0xFFFFFFFF ||
38 		    sys_read32(CCFG_BASE + CCFG_O_IEEE_MAC_1) == 0xFFFFFFFF) {
39 			mac = (uint8_t *)(FCFG1_BASE + FCFG1_O_MAC_15_4_0);
40 		} else {
41 			mac = (uint8_t *)(CCFG_BASE + CCFG_O_IEEE_MAC_0);
42 		}
43 	}
44 
45 	if (length > CC13XX_CC26XX_DEVID_SIZE) {
46 		length = CC13XX_CC26XX_DEVID_SIZE;
47 	}
48 
49 	/* Provide device ID (MAC) in big endian */
50 	sys_memcpy_swap(buffer, mac, length);
51 
52 	return length;
53 }
54 
z_impl_hwinfo_get_reset_cause(uint32_t * cause)55 int z_impl_hwinfo_get_reset_cause(uint32_t *cause)
56 {
57 	uint32_t reset_src;
58 
59 	reset_src = SysCtrlResetSourceGet();
60 
61 	switch (reset_src) {
62 	case RSTSRC_PWR_ON:
63 		*cause = RESET_POR;
64 		break;
65 	case RSTSRC_PIN_RESET:
66 		*cause = RESET_PIN;
67 		break;
68 	case RSTSRC_VDDS_LOSS:
69 		__fallthrough;
70 	case RSTSRC_VDDR_LOSS:
71 		*cause = RESET_BROWNOUT;
72 		break;
73 	case RSTSRC_CLK_LOSS:
74 		*cause = RESET_CLOCK;
75 		break;
76 	case RSTSRC_SYSRESET:
77 		*cause = RESET_SOFTWARE;
78 		break;
79 	}
80 
81 	return 0;
82 }
83 
z_impl_hwinfo_clear_reset_cause(void)84 int z_impl_hwinfo_clear_reset_cause(void)
85 {
86 	return -ENOSYS;
87 }
88 
z_impl_hwinfo_get_supported_reset_cause(uint32_t * supported)89 int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported)
90 {
91 	*supported = (RESET_PIN
92 		      | RESET_SOFTWARE
93 		      | RESET_BROWNOUT
94 		      | RESET_POR
95 		      | RESET_CLOCK);
96 
97 	return 0;
98 }
99