1 /*
2  * Copyright 2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef _FSL_CWT_H_
8 #define _FSL_CWT_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup cwt
14  * @{
15  */
16 
17 /*! @file */
18 
19 /*******************************************************************************
20  * Definitions
21  *******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief Defines CWT driver version 2.0.2.
26  *
27  * Change log:
28  * - Version 2.0.2
29  *   - Fix MISRA-2012 issues
30  * - Version 2.0.1
31  *   - Fix doxygen issues
32  * - Version 2.0.0
33  *   - initial version
34  */
35 #define FSL_CWT_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
36 /*@}*/
37 
38 typedef struct
39 {
40     uint8_t lock : 2;
41     uint8_t timeout : 3;
42     uint8_t miscompare : 3;
43     uint8_t sequence : 3;
44     uint8_t control : 3;
45     uint8_t state : 3;
46     uint8_t address : 3;
47     uint8_t reserved : 8;
48     uint8_t irq_pause : 2;
49     uint8_t debug_halt : 2;
50 } cwt_config_t;
51 
52 enum __cdog_debug_Action_ctrl_enum
53 {
54     kCDOG_DebugHaltCtrl_Run   = 0x1,
55     kCDOG_DebugHaltCtrl_Pause = 0x2,
56 };
57 
58 enum __cdog_irq_pause_ctrl_enum
59 {
60     kCDOG_IrqPauseCtrl_Run   = 0x1,
61     kCDOG_IrqPauseCtrl_Pause = 0x2,
62 };
63 
64 enum __cdog_fault_ctrl_enum
65 {
66     kCDOG_FaultCtrl_EnableReset     = 0x1U,
67     kCDOG_FaultCtrl_EnableInterrupt = 0x2U,
68     kCDOG_FaultCtrl_NoAction        = 0x4U,
69 };
70 
71 enum __code_lock_ctrl_enum
72 {
73     kCDOG_LockCtrl_Lock   = 0x1,
74     kCDOG_LockCtrl_Unlock = 0x2,
75 };
76 
77 typedef uint32_t secure_counter_t;
78 
79 #define SC_ADD(add)                          \
80     do                                       \
81     {                                        \
82         CDOG->ADD = (secure_counter_t)(add); \
83     } while (0)
84 
85 #define SC_ADD1                              \
86     do                                       \
87     {                                        \
88         CDOG->ADD1 = (secure_counter_t)0x1U; \
89     } while (0)
90 
91 #define SC_ADD16                              \
92     do                                        \
93     {                                         \
94         CDOG->ADD16 = (secure_counter_t)0x1U; \
95     } while (0)
96 
97 #define SC_ADD256                              \
98     do                                         \
99     {                                          \
100         CDOG->ADD256 = (secure_counter_t)0x1U; \
101     } while (0)
102 
103 #define SC_SUB(sub)                          \
104     do                                       \
105     {                                        \
106         CDOG->SUB = (secure_counter_t)(sub); \
107     } while (0)
108 
109 #define SC_SUB1                              \
110     do                                       \
111     {                                        \
112         CDOG->SUB1 = (secure_counter_t)0x1U; \
113     } while (0)
114 
115 #define SC_SUB16                              \
116     do                                        \
117     {                                         \
118         CDOG->SUB16 = (secure_counter_t)0x1U; \
119     } while (0)
120 
121 #define SC_SUB256                              \
122     do                                         \
123     {                                          \
124         CDOG->SUB256 = (secure_counter_t)0x1U; \
125     } while (0)
126 
127 #define SC_CHECK(val)                          \
128     do                                         \
129     {                                          \
130         CDOG->RESTART = (secure_counter_t)val; \
131     } while (0)
132 
133 /*******************************************************************************
134  * API
135  *******************************************************************************/
136 
137 extern void CodeWDG_DriverIRQHandler(void);
138 
139 #if defined(__cplusplus)
140 extern "C" {
141 #endif /* __cplusplus */
142 
143 /*!
144  * @name CWT Functional Operation
145  * @{
146  */
147 
148 /*!
149  * @brief Initialize CWT
150  *
151  * This function initializes CWT block and setting.
152  *
153  * @param base CWT peripheral base address
154  * @param conf CWT configuration structure
155  * @return Status of the init operation
156  */
157 status_t CWT_Init(CDOG_Type *base, cwt_config_t *conf);
158 
159 /*!
160  * @brief Deinitialize CWT
161  *
162  * This function deinitializes CWT secure counter.
163  *
164  * @param base CWT peripheral base address
165  */
166 void CWT_Deinit(CDOG_Type *base);
167 
168 /*!
169  * @brief Sets the default configuration of CWT
170  *
171  * This function initialize CWT config structure to default values.
172  *
173  * @param conf CWT configuration structure
174  */
175 void CWT_GetDefaultConfig(cwt_config_t *conf);
176 
177 /*!
178  * @brief Stops secure counter and instruction timer
179  *
180  * This function stops instruction timer and secure counter.
181  * This also change state od CWT to IDLE.
182  *
183  * @param base CWT peripheral base address
184  * @param stop expected value which will be compared with value of secure counter
185  */
186 void CWT_Stop(CDOG_Type *base, uint32_t stop);
187 
188 /*!
189  * @brief Sets secure counter and instruction timer values
190  *
191  * This function sets value in RELOAD and START registers
192  * for instruction timer and secure counter
193  *
194  * @param base CWT peripheral base address
195  * @param reload reload value
196  * @param start start value
197  */
198 void CWT_Start(CDOG_Type *base, uint32_t reload, uint32_t start);
199 
200 /*!
201  * @brief Checks secure counter.
202  *
203  * This function compares stop value in handler with secure counter value
204  * by writting to RELOAD refister.
205  *
206  * @param base CWT peripheral base address
207  * @param check expected (stop) value
208  */
209 void CWT_Check(CDOG_Type *base, uint32_t check);
210 
211 /*!
212  * @brief Sets secure counter and instruction timer values
213  *
214  * This function sets value in STOP, RELOAD and START registers
215  * for instruction timer and secure counter.
216  *
217  * @param base CWT peripheral base address
218  * @param stop expected value which will be compared with value of secure counter
219  * @param reload reload value for instruction timer
220  * @param start start value for secure timer
221  */
222 void CWT_Set(CDOG_Type *base, uint32_t stop, uint32_t reload, uint32_t start);
223 
224 /*!
225  * @brief Add value to secure counter
226  *
227  * This function add specified value to secure counter.
228  *
229  * @param base CWT peripheral base address.
230  * @param add Value to be added.
231  */
232 void CWT_Add(CDOG_Type *base, uint32_t add);
233 
234 /*!
235  * @brief Add 1 to secure counter
236  *
237  * This function add 1 to secure counter.
238  *
239  * @param base CWT peripheral base address.
240  */
241 void CWT_Add1(CDOG_Type *base);
242 
243 /*!
244  * @brief Add 16 to secure counter
245  *
246  * This function add 16 to secure counter.
247  *
248  * @param base CWT peripheral base address.
249  */
250 void CWT_Add16(CDOG_Type *base);
251 
252 /*!
253  * @brief Add 256 to secure counter
254  *
255  * This function add 256 to secure counter.
256  *
257  * @param base CWT peripheral base address.
258  */
259 void CWT_Add256(CDOG_Type *base);
260 
261 /*!
262  * brief Substract value to secure counter
263  *
264  * This function substract specified value to secure counter.
265  *
266  * param base CWT peripheral base address.
267  * param sub Value to be substracted.
268  */
269 void CWT_Sub(CDOG_Type *base, uint32_t sub);
270 
271 /*!
272  * @brief Substract 1 from secure counter
273  *
274  * This function substract specified 1 from secure counter.
275  *
276  * @param base CWT peripheral base address.
277  */
278 void CWT_Sub1(CDOG_Type *base);
279 
280 /*!
281  * @brief Substract 16 from secure counter
282  *
283  * This function substract specified 16 from secure counter.
284  *
285  * @param base CWT peripheral base address.
286  */
287 void CWT_Sub16(CDOG_Type *base);
288 
289 /*!
290  * @brief Substract 256 from secure counter
291  *
292  * This function substract specified 256 from secure counter.
293  *
294  * @param base CWT peripheral base address.
295  */
296 void CWT_Sub256(CDOG_Type *base);
297 
298 /*!
299  * @brief Set the CWT persistent word.
300  *
301  * @param base CWT peripheral base address.
302  * @param value The value to be written.
303  */
304 void CWT_WritePersistent(CDOG_Type *base, uint32_t value);
305 
306 /*!
307  * @brief Get the CWT persistent word.
308  *
309  * @param base CWT peripheral base address.
310  * @return The persistent word.
311  */
312 uint32_t CWT_ReadPersistent(CDOG_Type *base);
313 
314 /*! @}*/
315 
316 #if defined(__cplusplus)
317 }
318 #endif /* __cplusplus */
319 
320 /*! @}*/ /* end of group cwt */
321 
322 #endif /* _FSL_CWT_H_ */
323