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