1 /*
2 * Copyright 2021-2024 NXP
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 /**
8 * @file
9 *
10 * @addtogroup osif_drv
11 * @{
12 */
13
14 #ifdef __cplusplus
15 extern "C"{
16 #endif
17
18 /*==================================================================================================
19 * INCLUDE FILES
20 * 1) system and project includes
21 * 2) needed interfaces from external units
22 * 3) internal and external interfaces from this unit
23 ==================================================================================================*/
24 #include "OsIf_DeviceRegisters.h"
25 #include "OsIf_Cfg.h"
26 #include "OsIf_Timer_System_Internal_GenericTimer.h"
27 #include "Mcal.h"
28
29 /*==================================================================================================
30 * SOURCE FILE VERSION INFORMATION
31 ==================================================================================================*/
32 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID_C 43
33 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION_C 4
34 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION_C 7
35 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION_C 0
36 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION_C 2
37 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION_C 0
38 #define OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION_C 0
39
40 /*==================================================================================================
41 * FILE VERSION CHECKS
42 ==================================================================================================*/
43 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_DeviceRegisters.h file are of the same vendor */
44 #if (OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID_C != OSIF_DEVICE_REGISTERS_VENDOR_ID)
45 #error "OsIf_Timer_System_Internal_GenericTimer.c and OsIf_DeviceRegisters.h have different vendor ids"
46 #endif
47 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_DeviceRegisters.h file are of the same Autosar version */
48 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION_C != OSIF_DEVICE_REGISTERS_AR_RELEASE_MAJOR_VERSION) || \
49 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION_C != OSIF_DEVICE_REGISTERS_AR_RELEASE_MINOR_VERSION) || \
50 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_DEVICE_REGISTERS_AR_RELEASE_REVISION_VERSION))
51 #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_DeviceRegisters.h are different"
52 #endif
53 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_DeviceRegisters.h file are of the same Software version */
54 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MAJOR_VERSION) || \
55 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION_C != OSIF_DEVICE_REGISTERS_SW_MINOR_VERSION) || \
56 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION_C != OSIF_DEVICE_REGISTERS_SW_PATCH_VERSION) \
57 )
58 #error "Software Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_DeviceRegisters.h are different"
59 #endif
60
61 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Cfg.h file are of the same vendor */
62 #if (OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID_C != OSIF_CFG_VENDOR_ID)
63 #error "OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Cfg.h have different vendor ids"
64 #endif
65 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Cfg.h file are of the same Autosar version */
66 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION_C != OSIF_CFG_AR_RELEASE_MAJOR_VERSION) || \
67 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION_C != OSIF_CFG_AR_RELEASE_MINOR_VERSION) || \
68 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_CFG_AR_RELEASE_REVISION_VERSION))
69 #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Cfg.h are different"
70 #endif
71 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Cfg.h file are of the same Software version */
72 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION_C != OSIF_CFG_SW_MAJOR_VERSION) || \
73 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION_C != OSIF_CFG_SW_MINOR_VERSION) || \
74 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION_C != OSIF_CFG_SW_PATCH_VERSION) \
75 )
76 #error "Software Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Cfg.h are different"
77 #endif
78
79 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Timer_System_Internal_GenericTimer.h file are of the same vendor */
80 #if (OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_VENDOR_ID)
81 #error "OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Timer_System_Internal_GenericTimer.h have different vendor ids"
82 #endif
83 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Timer_System_Internal_GenericTimer.h file are of the same Autosar version */
84 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION) || \
85 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION) || \
86 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_REVISION_VERSION))
87 #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Timer_System_Internal_GenericTimer.h are different"
88 #endif
89 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and OsIf_Timer_System_Internal_GenericTimer.h file are of the same Software version */
90 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MAJOR_VERSION) || \
91 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_MINOR_VERSION) || \
92 (OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION_C != OSIF_TIMER_SYS_INTER_GENERICTIMER_SW_PATCH_VERSION) \
93 )
94 #error "Software Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and OsIf_Timer_System_Internal_GenericTimer.h are different"
95 #endif
96
97 #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
98 /* Check if OsIf_Timer_System_Internal_GenericTimer.c file and Mcal.h file are of the same Autosar version */
99 #if ((OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MAJOR_VERSION_C != MCAL_AR_RELEASE_MAJOR_VERSION) || \
100 (OSIF_TIMER_SYS_INTER_GENERICTIMER_AR_RELEASE_MINOR_VERSION_C != MCAL_AR_RELEASE_MINOR_VERSION))
101 #error "AUTOSAR Version Numbers of OsIf_Timer_System_Internal_GenericTimer.c and Mcal.h are different"
102 #endif
103 #endif
104
105 /*==================================================================================================
106 * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
107 ==================================================================================================*/
108
109 /*==================================================================================================
110 * LOCAL MACROS
111 ==================================================================================================*/
112 #if defined(OSIF_USE_GENERICTIMER)
113 #if (OSIF_USE_GENERICTIMER == STD_ON)
114
115 #define CNTP_CTL_IMASK_MASK (0x2U)
116 #define CNTP_CTL_ENABLE_EN (0x1U)
117 #define CNTP_TVAL_TIMERVALUE_MASK (0xFFFFFFFFU)
118
119 #define GENERICTIMER_GET_COUNTER() ((read_CNTP_TVAL()) & CNTP_TVAL_TIMERVALUE_MASK)
120 #define GENERICTIMER_DELTA_OUTER(high, low, max) ((max) - ((high) - (low)))
121 #define GENERICTIMER_DELTA_INNER(high, low) ((high) - (low))
122 #define GENERICTIMER_TVAL_MAX (0xFFFFFFFFU)
123
124 #define GENERICTIMER_OVERFLOWED(curr, ref) ((curr) > (ref))
125 /*==================================================================================================
126 * LOCAL CONSTANTS
127 ==================================================================================================*/
128
129 /*==================================================================================================
130 * LOCAL VARIABLES
131 ==================================================================================================*/
132
133 /*==================================================================================================
134 * GLOBAL CONSTANTS
135 ==================================================================================================*/
136
137 /*==================================================================================================
138 * GLOBAL VARIABLES
139 ==================================================================================================*/
140
141 /*==================================================================================================
142 * LOCAL FUNCTION PROTOTYPES
143 ==================================================================================================*/
144 #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
145 #define WRITE_CNTFRQ_INSTR "msr CNTFRQ_EL0, %[Rd]"
146 #define WRITE_CNTP_CTL_INSTR "msr CNTP_CTL_EL0, %[Rd]"
147 #define WRITE_CNTP_TVAL_INSTR "msr CNTP_TVAL_EL0, %[Rd]"
148 #define READ_CNTP_TVAL_INSTR "mrs %[Rd], CNTP_TVAL_EL0"
149 #elif ((MCAL_PLATFORM_ARM == MCAL_ARM_AARCH32) || (MCAL_PLATFORM_ARM == MCAL_ARM_RARCH))
150 #define WRITE_CNTP_CTL_INSTR "mcr p15,0,%[Rd],c14,c2,1"
151 #define WRITE_CNTP_TVAL_INSTR "mcr p15,0,%[Rd],c14,c2,0"
152 #define READ_CNTP_TVAL_INSTR "mrc p15,0,%[Rd],c14,c2,0"
153 #else
154 #error "Unsupported architecture!"
155 #endif
156 /*==================================================================================================
157 * LOCAL FUNCTIONS
158 ==================================================================================================*/
159 #define BASENXP_START_SEC_CODE
160 #include "BaseNXP_MemMap.h"
161
162 #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
163 /*FUNCTION**********************************************************************
164 *
165 * Function Name : write_CNTFRQ.
166 * Description : Write to generic timer CNTFRQ register.
167 *
168 *END**************************************************************************/
169 static void write_CNTFRQ(uint32 val);
write_CNTFRQ(uint32 val)170 static void write_CNTFRQ(uint32 val)
171 {
172 /* Force assembler to use a 64-bit register on AARCH64 */
173 uint64 val64 = val;
174
175 /*LDRA_NOANALYSIS*/
176 ASMV_KEYWORD(
177 WRITE_CNTFRQ_INSTR
178 : /* No Output */
179 : [Rd]"r"(val64)
180 );
181 /*LDRA_ANALYSIS*/
182 }
183
184 /*FUNCTION**********************************************************************
185 *
186 * Function Name : write_CNTP_CTL.
187 * Description : Write to generic timer CNTP_CTL register.
188 *
189 *END**************************************************************************/
190 static void write_CNTP_CTL(uint32 val);
write_CNTP_CTL(uint32 val)191 static void write_CNTP_CTL(uint32 val)
192 {
193 /* Force assembler to use a 64-bit register on AARCH64 */
194 uint64 val64 = val;
195
196 /*LDRA_NOANALYSIS*/
197 ASMV_KEYWORD(
198 WRITE_CNTP_CTL_INSTR
199 : /* No Output */
200 : [Rd]"r"(val64)
201 );
202 /*LDRA_ANALYSIS*/
203 }
204
205 /*FUNCTION**********************************************************************
206 *
207 * Function Name : write_CNTP_TVAL.
208 * Description : Write to generic timer CNTP_TVAL register.
209 *
210 *END**************************************************************************/
211 static void write_CNTP_TVAL(uint32 val);
write_CNTP_TVAL(uint32 val)212 static void write_CNTP_TVAL(uint32 val)
213 {
214 /* Force assembler to use a 64-bit register on AARCH64 */
215 uint64 val64 = val;
216
217 /*LDRA_NOANALYSIS*/
218 ASMV_KEYWORD(
219 WRITE_CNTP_TVAL_INSTR
220 : /* No Output */
221 : [Rd]"r"(val64)
222 );
223 /*LDRA_ANALYSIS*/
224 }
225
226 /*FUNCTION**********************************************************************
227 *
228 * Function Name : read_CNTP_TVAL.
229 * Description : Read value from generic timer CNTP_TVAL register.
230 *
231 *END**************************************************************************/
232 static uint32 read_CNTP_TVAL(void);
read_CNTP_TVAL(void)233 static uint32 read_CNTP_TVAL(void)
234 {
235 uint64 cntp_tval = 0U;
236
237 /*LDRA_NOANALYSIS*/
238 ASMV_KEYWORD(
239 READ_CNTP_TVAL_INSTR
240 : [Rd]"=r"(cntp_tval)
241 );
242 /*LDRA_ANALYSIS*/
243 return cntp_tval;
244 }
245 #elif ((MCAL_PLATFORM_ARM == MCAL_ARM_AARCH32) || (MCAL_PLATFORM_ARM == MCAL_ARM_RARCH))
246 /*FUNCTION**********************************************************************
247 *
248 * Function Name : write_CNTP_CTL.
249 * Description : Write to generic timer CNTP_CTL register.
250 *
251 *END**************************************************************************/
252 static void write_CNTP_CTL(uint32 val);
253 static void write_CNTP_CTL(uint32 val)
254 {
255 uint32 val32 = val;
256
257 /*LDRA_NOANALYSIS*/
258 ASMV_KEYWORD(
259 WRITE_CNTP_CTL_INSTR
260 : /* No Output */
261 : [Rd]"r"(val32)
262 );
263 /*LDRA_ANALYSIS*/
264 }
265
266 /*FUNCTION**********************************************************************
267 *
268 * Function Name : write_CNTP_TVAL.
269 * Description : Write to generic timer CNTP_TVAL register.
270 *
271 *END**************************************************************************/
272 static void write_CNTP_TVAL(uint32 val);
273 static void write_CNTP_TVAL(uint32 val)
274 {
275 uint32 val32 = val;
276
277 /*LDRA_NOANALYSIS*/
278 ASMV_KEYWORD(
279 WRITE_CNTP_TVAL_INSTR
280 : /* No Output */
281 : [Rd]"r"(val32)
282 );
283 /*LDRA_ANALYSIS*/
284 }
285
286 /*FUNCTION**********************************************************************
287 *
288 * Function Name : read_CNTP_TVAL.
289 * Description : Read value from generic timer CNTP_TVAL register.
290 *
291 *END**************************************************************************/
292 static uint32 read_CNTP_TVAL(void);
293 static uint32 read_CNTP_TVAL(void)
294 {
295 uint32 cntp_tval = 0U;
296
297 /*LDRA_NOANALYSIS*/
298 ASMV_KEYWORD(
299 READ_CNTP_TVAL_INSTR
300 : [Rd]"=r"(cntp_tval)
301 );
302 /*LDRA_ANALYSIS*/
303 return cntp_tval;
304 }
305 #endif
306 #define BASENXP_STOP_SEC_CODE
307 #include "BaseNXP_MemMap.h"
308
309 /*==================================================================================================
310 * GLOBAL FUNCTIONS
311 ==================================================================================================*/
312 #define BASENXP_START_SEC_CODE
313 #include "BaseNXP_MemMap.h"
314
315 /*FUNCTION**********************************************************************
316 *
317 * Function Name : OsIf_Timer_System_Internal_Init.
318 * Description : Initialize generic timer.
319 *
320 *END**************************************************************************/
OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)321 void OsIf_Timer_System_Internal_Init(uint32 SystemCounterFreq)
322 {
323 /* The value of this register is not interpreted by hardware. It is provided
324 so that software can discover the frequency of the system counter */
325 #if (MCAL_PLATFORM_ARM == MCAL_ARM_AARCH64)
326 /*accessing register in T32 mode from R52 core is not possible. To be check later. Implementation now is only done for AArch64*/
327 write_CNTFRQ(SystemCounterFreq);
328 #elif ((MCAL_PLATFORM_ARM == MCAL_ARM_AARCH32) || (MCAL_PLATFORM_ARM == MCAL_ARM_RARCH))
329 (void)SystemCounterFreq;
330 #endif
331 write_CNTP_CTL(CNTP_CTL_IMASK_MASK & ~CNTP_CTL_ENABLE_EN);
332 write_CNTP_TVAL(GENERICTIMER_TVAL_MAX);
333 }
334
335 /*FUNCTION**********************************************************************
336 *
337 * Function Name : OsIf_Timer_System_Internal_GetCounter.
338 * Description : Get generic timer counter value.
339 *
340 *END**************************************************************************/
OsIf_Timer_System_Internal_GetCounter(void)341 uint32 OsIf_Timer_System_Internal_GetCounter(void)
342 {
343 return GENERICTIMER_GET_COUNTER();
344 }
345
346 /*FUNCTION**********************************************************************
347 *
348 * Function Name : OsIf_Timer_System_Internal_GetElapsed.
349 * Description : Get generic timer elapsed value.
350 *
351 *END**************************************************************************/
OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)352 uint32 OsIf_Timer_System_Internal_GetElapsed(uint32 * const CurrentRef)
353 {
354 uint32 CurrentVal = GENERICTIMER_GET_COUNTER();
355 uint32 dif = 0U;
356
357 if (GENERICTIMER_OVERFLOWED(CurrentVal, *CurrentRef))
358 {
359 /* Overflow occurred */
360 dif = GENERICTIMER_DELTA_OUTER(CurrentVal, *CurrentRef, GENERICTIMER_TVAL_MAX);
361 }
362 else
363 {
364 /* Overflow did not occur */
365 dif = GENERICTIMER_DELTA_INNER(*CurrentRef, CurrentVal);
366 }
367 *CurrentRef = CurrentVal;
368
369 return dif;
370 }
371
372 #define BASENXP_STOP_SEC_CODE
373 #include "BaseNXP_MemMap.h"
374
375 #endif /* (OSIF_USE_GENERICTIMER == STD_ON) */
376 #endif /* defined(OSIF_USE_GENERICTIMER) */
377
378 #ifdef __cplusplus
379 }
380 #endif
381
382 /** @} */
383