1 /*
2  * Copyright (c) 2021 Basalte bv
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_imx_src
8 
9 #include <soc.h>
10 #include <zephyr/drivers/hwinfo.h>
11 #include <string.h>
12 #include <zephyr/sys/byteorder.h>
13 
14 #include <fsl_src.h>
15 
16 BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 1, "No nxp,imx-src compatible device found");
17 
z_impl_hwinfo_get_reset_cause(uint32_t * cause)18 int z_impl_hwinfo_get_reset_cause(uint32_t *cause)
19 {
20 	uint32_t flags = 0;
21 
22 	uint32_t reason = SRC_GetResetStatusFlags((SRC_Type *)DT_INST_REG_ADDR(0));
23 
24 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_IPP_RESET_B) && \
25 	FSL_FEATURE_SRC_HAS_SRSR_IPP_RESET_B)
26 	if (reason & kSRC_IppResetPinFlag) {
27 		flags |= RESET_PIN;
28 	}
29 #endif
30 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_POR) && FSL_FEATURE_SRC_HAS_SRSR_POR)
31 	if (reason & kSRC_PowerOnResetFlag) {
32 		flags |= RESET_POR;
33 	}
34 #endif
35 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_LOCKUP) && FSL_FEATURE_SRC_HAS_SRSR_LOCKUP)
36 	if (reason & kSRC_CoreLockupResetFlag) {
37 		flags |= RESET_CPU_LOCKUP;
38 	}
39 #endif
40 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_LOCKUP_SYSRESETREQ) && \
41 	FSL_FEATURE_SRC_HAS_SRSR_LOCKUP_SYSRESETREQ)
42 	if (reason & kSRC_LockupSysResetFlag) {
43 		flags |= RESET_CPU_LOCKUP | RESET_SOFTWARE;
44 	}
45 #endif
46 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_CSU_RESET_B) && FSL_FEATURE_SRC_HAS_SRSR_CSU_RESET_B)
47 	if (reason & kSRC_CsuResetFlag) {
48 		flags |= RESET_SECURITY;
49 	}
50 #endif
51 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_SNVS) && FSL_FEATURE_SRC_HAS_SRSR_SNVS)
52 	if (reason & kSRC_SNVSFailResetFlag) {
53 		flags |= RESET_HARDWARE;
54 	}
55 #endif
56 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_IPP_USER_RESET_B) && \
57 	FSL_FEATURE_SRC_HAS_SRSR_IPP_USER_RESET_B)
58 	if (reason & kSRC_IppUserResetFlag) {
59 		flags |= RESET_USER;
60 	}
61 #endif
62 	if (reason & kSRC_WatchdogResetFlag) {
63 		flags |= RESET_WATCHDOG;
64 	}
65 	if (reason & kSRC_JTAGGeneratedResetFlag) {
66 		flags |= RESET_DEBUG;
67 	}
68 	if (reason & kSRC_JTAGSoftwareResetFlag) {
69 		flags |= RESET_DEBUG;
70 	}
71 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_JTAG_SW_RST) && FSL_FEATURE_SRC_HAS_SRSR_JTAG_SW_RST)
72 	if (reason & kSRC_JTAGSystemResetFlag) {
73 		flags |= RESET_DEBUG;
74 	}
75 #endif
76 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_SW) && FSL_FEATURE_SRC_HAS_SRSR_SW)
77 	if (reason & kSRC_SoftwareResetFlag) {
78 		flags |= RESET_SOFTWARE;
79 	}
80 #endif
81 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_WDOG3_RST_B) && FSL_FEATURE_SRC_HAS_SRSR_WDOG3_RST_B)
82 	if (reason & kSRC_Wdog3ResetFlag) {
83 		flags |= RESET_WATCHDOG;
84 	}
85 #endif
86 	if (reason & kSRC_TemperatureSensorResetFlag) {
87 		flags |= RESET_TEMPERATURE;
88 	}
89 #if !(defined(FSL_FEATURE_SRC_HAS_NO_SRSR_WBI) && FSL_FEATURE_SRC_HAS_NO_SRSR_WBI)
90 	if (reason & kSRC_WarmBootIndicationFlag) {
91 		flags |= RESET_SOFTWARE;
92 	}
93 #endif
94 
95 	*cause = flags;
96 
97 	return 0;
98 }
99 
z_impl_hwinfo_clear_reset_cause(void)100 int z_impl_hwinfo_clear_reset_cause(void)
101 {
102 	uint32_t reason = -1;
103 
104 	SRC_ClearResetStatusFlags((SRC_Type *)DT_INST_REG_ADDR(0), reason);
105 
106 	return 0;
107 }
108 
z_impl_hwinfo_get_supported_reset_cause(uint32_t * supported)109 int z_impl_hwinfo_get_supported_reset_cause(uint32_t *supported)
110 {
111 	*supported = (RESET_WATCHDOG
112 		      | RESET_DEBUG
113 		      | RESET_TEMPERATURE
114 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_IPP_RESET_B) && \
115 		      FSL_FEATURE_SRC_HAS_SRSR_IPP_RESET_B)
116 		      | RESET_PIN
117 #endif
118 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_POR) && FSL_FEATURE_SRC_HAS_SRSR_POR)
119 		      | RESET_POR
120 #endif
121 #if (defined(FSL_FEATURE_SRC_HAS_SCR_LOCKUP_RST) && FSL_FEATURE_SRC_HAS_SCR_LOCKUP_RST)
122 		      | RESET_CPU_LOCKUP
123 #endif
124 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_LOCKUP_SYSRESETREQ) && \
125 		      FSL_FEATURE_SRC_HAS_SRSR_LOCKUP_SYSRESETREQ)
126 		      | RESET_CPU_LOCKUP | RESET_SOFTWARE
127 #endif
128 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_CSU_RESET_B) && FSL_FEATURE_SRC_HAS_SRSR_CSU_RESET_B)
129 		      | RESET_SECURITY
130 #endif
131 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_SNVS) && FSL_FEATURE_SRC_HAS_SRSR_SNVS)
132 		      | RESET_HARDWARE
133 #endif
134 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_IPP_USER_RESET_B) && \
135 		      FSL_FEATURE_SRC_HAS_SRSR_IPP_USER_RESET_B)
136 		      | RESET_USER
137 #endif
138 #if (defined(FSL_FEATURE_SRC_HAS_SRSR_SW) && FSL_FEATURE_SRC_HAS_SRSR_SW)
139 		      | RESET_SOFTWARE
140 #endif
141 		      );
142 
143 	return 0;
144 }
145