1 /*
2  * Copyright 2020-2023 NXP
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 /**
7 *   @file       Clock_Ip_Pll.c
8 *   @version    2.0.0
9 *
10 *   @brief   CLOCK driver implementations.
11 *   @details CLOCK driver implementations.
12 *
13 *   @addtogroup CLOCK_DRIVER Clock Ip Driver
14 *   @{
15 */
16 
17 #ifdef __cplusplus
18 extern "C"{
19 #endif
20 
21 
22 /*==================================================================================================
23 *                                          INCLUDE FILES
24 * 1) system and project includes
25 * 2) needed interfaces from external units
26 * 3) internal and external interfaces from this unit
27 ==================================================================================================*/
28 
29 #include "Clock_Ip_Private.h"
30 
31 /*==================================================================================================
32                                SOURCE FILE VERSION INFORMATION
33 ==================================================================================================*/
34 #define CLOCK_IP_PLL_VENDOR_ID_C                      43
35 #define CLOCK_IP_PLL_AR_RELEASE_MAJOR_VERSION_C       4
36 #define CLOCK_IP_PLL_AR_RELEASE_MINOR_VERSION_C       7
37 #define CLOCK_IP_PLL_AR_RELEASE_REVISION_VERSION_C    0
38 #define CLOCK_IP_PLL_SW_MAJOR_VERSION_C               2
39 #define CLOCK_IP_PLL_SW_MINOR_VERSION_C               0
40 #define CLOCK_IP_PLL_SW_PATCH_VERSION_C               0
41 
42 /*==================================================================================================
43 *                                     FILE VERSION CHECKS
44 ==================================================================================================*/
45 /* Check if Clock_Ip_Pll.c file and Clock_Ip_Private.h file are of the same vendor */
46 #if (CLOCK_IP_PLL_VENDOR_ID_C != CLOCK_IP_PRIVATE_VENDOR_ID)
47     #error "Clock_Ip_Pll.c and Clock_Ip_Private.h have different vendor ids"
48 #endif
49 
50 /* Check if Clock_Ip_Pll.c file and Clock_Ip_Private.h file are of the same Autosar version */
51 #if ((CLOCK_IP_PLL_AR_RELEASE_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MAJOR_VERSION) || \
52      (CLOCK_IP_PLL_AR_RELEASE_MINOR_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_MINOR_VERSION) || \
53      (CLOCK_IP_PLL_AR_RELEASE_REVISION_VERSION_C != CLOCK_IP_PRIVATE_AR_RELEASE_REVISION_VERSION) \
54     )
55     #error "AutoSar Version Numbers of Clock_Ip_Pll.c and Clock_Ip_Private.h are different"
56 #endif
57 
58 /* Check if Clock_Ip_Pll.c file and Clock_Ip_Private.h file are of the same Software version */
59 #if ((CLOCK_IP_PLL_SW_MAJOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MAJOR_VERSION) || \
60      (CLOCK_IP_PLL_SW_MINOR_VERSION_C != CLOCK_IP_PRIVATE_SW_MINOR_VERSION) || \
61      (CLOCK_IP_PLL_SW_PATCH_VERSION_C != CLOCK_IP_PRIVATE_SW_PATCH_VERSION) \
62     )
63     #error "Software Version Numbers of Clock_Ip_Pll.c and Clock_Ip_Private.h are different"
64 #endif
65 
66 /*==================================================================================================
67 *                           LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
68 ==================================================================================================*/
69 
70 /*==================================================================================================
71 *                                          LOCAL MACROS
72 ==================================================================================================*/
73 
74 /*==================================================================================================
75 *                                         LOCAL CONSTANTS
76 ==================================================================================================*/
77 
78 /*==================================================================================================
79 *                                         LOCAL VARIABLES
80 ==================================================================================================*/
81 
82 /*==================================================================================================
83 *                                        GLOBAL CONSTANTS
84 ==================================================================================================*/
85 
86 /*==================================================================================================
87 *                                        GLOBAL VARIABLES
88 ==================================================================================================*/
89 
90 /*==================================================================================================
91 *                                    GLOBAL FUNCTION PROTOTYPES
92 ==================================================================================================*/
93 /* Clock start section code */
94 #define MCU_START_SEC_CODE
95 
96 #include "Mcu_MemMap.h"
97 
98 #ifdef CLOCK_IP_SPLL_ENABLE
99 void Clock_Ip_ResetSpll_TrustedCall(Clock_Ip_PllConfigType const* Config);
100 void Clock_Ip_SetSpll_TrustedCall(Clock_Ip_PllConfigType const* Config);
101 void Clock_Ip_DisableSpll_TrustedCall(Clock_Ip_NameType PllName);
102 void Clock_Ip_EnableSpll_TrustedCall(Clock_Ip_PllConfigType const* Config);
103 #endif
104 
105 /*==================================================================================================
106 *                                    LOCAL FUNCTION PROTOTYPES
107 ==================================================================================================*/
108 
109 
110 static void Clock_Ip_CallbackPllEmpty(Clock_Ip_PllConfigType const* Config);
111 static Clock_Ip_PllStatusReturnType Clock_Ip_CallbackPllEmptyComplete(Clock_Ip_NameType PllName);
112 static void Clock_Ip_CallbackPllEmptyDisable(Clock_Ip_NameType PllName);
113 
114 #ifdef CLOCK_IP_SPLL_ENABLE
115 static void Clock_Ip_ResetSpll(Clock_Ip_PllConfigType const* Config);
116 static void Clock_Ip_SetSpll(Clock_Ip_PllConfigType const* Config);
117 static Clock_Ip_PllStatusReturnType Clock_Ip_CompleteSpll(Clock_Ip_NameType PllName);
118 static void Clock_Ip_EnableSpll(Clock_Ip_PllConfigType const* Config);
119 static void Clock_Ip_DisableSpll(Clock_Ip_NameType PllName);
120 #endif
121 
122 
123 /* Clock stop section code */
124 #define MCU_STOP_SEC_CODE
125 
126 #include "Mcu_MemMap.h"
127 
128 /*==================================================================================================
129 *                                         LOCAL FUNCTIONS
130 ==================================================================================================*/
131 
132 #define MCU_START_SEC_CODE
133 /* Clock start section code */
134 
135 #include "Mcu_MemMap.h"
136 
137 
Clock_Ip_CallbackPllEmpty(Clock_Ip_PllConfigType const * Config)138 static void Clock_Ip_CallbackPllEmpty(Clock_Ip_PllConfigType const* Config)
139 {
140     (void)Config;
141     /* No implementation */
142 }
Clock_Ip_CallbackPllEmptyComplete(Clock_Ip_NameType PllName)143 static Clock_Ip_PllStatusReturnType Clock_Ip_CallbackPllEmptyComplete(Clock_Ip_NameType PllName)
144 {
145     (void)PllName;
146     /* No implementation */
147     return STATUS_PLL_LOCKED;
148 }
Clock_Ip_CallbackPllEmptyDisable(Clock_Ip_NameType PllName)149 static void Clock_Ip_CallbackPllEmptyDisable(Clock_Ip_NameType PllName)
150 {
151     (void)PllName;
152     /* No implementation */
153 }
154 
155 
156 #ifdef CLOCK_IP_SPLL_ENABLE
Clock_Ip_ResetSpll(Clock_Ip_PllConfigType const * Config)157 static void Clock_Ip_ResetSpll(Clock_Ip_PllConfigType const* Config)
158 {
159     if (NULL_PTR != Config)
160     {
161     #ifdef CLOCK_IP_ENABLE_USER_MODE_SUPPORT
162       #if (STD_ON == CLOCK_IP_ENABLE_USER_MODE_SUPPORT)
163         OsIf_Trusted_Call1param(Clock_Ip_ResetSpll_TrustedCall,(Config));
164       #else
165         Clock_Ip_ResetSpll_TrustedCall(Config);
166       #endif
167     #endif /* CLOCK_IP_ENABLE_USER_MODE_SUPPORT */
168     }
169 }
Clock_Ip_SetSpll(Clock_Ip_PllConfigType const * Config)170 static void Clock_Ip_SetSpll(Clock_Ip_PllConfigType const* Config)
171 {
172     if (NULL_PTR != Config)
173     {
174     #ifdef CLOCK_IP_ENABLE_USER_MODE_SUPPORT
175       #if (STD_ON == CLOCK_IP_ENABLE_USER_MODE_SUPPORT)
176         OsIf_Trusted_Call1param(Clock_Ip_SetSpll_TrustedCall,(Config));
177       #else
178         Clock_Ip_SetSpll_TrustedCall(Config);
179       #endif
180     #endif /* CLOCK_IP_ENABLE_USER_MODE_SUPPORT */
181     }
182 }
Clock_Ip_CompleteSpll(Clock_Ip_NameType PllName)183 static Clock_Ip_PllStatusReturnType Clock_Ip_CompleteSpll(Clock_Ip_NameType PllName)
184 {
185     Clock_Ip_PllStatusReturnType PllStatus = STATUS_PLL_UNLOCKED;
186     boolean TimeoutOccurred = FALSE;
187     uint32 StartTime;
188     uint32 ElapsedTime;
189     uint32 TimeoutTicks;
190     uint32 SpllStatus;
191 
192 
193     /* Configure SPLL. */
194     if ((IP_SCG->SPLLCSR & SCG_SPLLCSR_SPLLEN_MASK) != 0U)
195     {
196         Clock_Ip_StartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, CLOCK_IP_TIMEOUT_VALUE_US);
197         /* Wait until pll is locked */
198         do
199         {
200             SpllStatus = (((IP_SCG->SPLLCSR & SCG_SPLLCSR_SPLLVLD_MASK) >> SCG_SPLLCSR_SPLLVLD_SHIFT));
201             TimeoutOccurred = Clock_Ip_TimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
202         }
203         while ((0U == SpllStatus) && (FALSE == TimeoutOccurred));
204 
205         if (FALSE == TimeoutOccurred)
206         {
207             PllStatus = STATUS_PLL_LOCKED;
208         }
209         else
210         {
211             /* Report timeout error */
212             Clock_Ip_ReportClockErrors(CLOCK_IP_REPORT_TIMEOUT_ERROR, PllName);
213         }
214     }
215     else
216     {
217         PllStatus = STATUS_PLL_NOT_ENABLED;
218     }
219 
220     return PllStatus;
221 }
Clock_Ip_DisableSpll(Clock_Ip_NameType PllName)222 static void Clock_Ip_DisableSpll(Clock_Ip_NameType PllName)
223 {
224 #ifdef CLOCK_IP_ENABLE_USER_MODE_SUPPORT
225   #if (STD_ON == CLOCK_IP_ENABLE_USER_MODE_SUPPORT)
226     OsIf_Trusted_Call1param(Clock_Ip_DisableSpll_TrustedCall,(PllName));
227   #else
228     Clock_Ip_DisableSpll_TrustedCall(PllName);
229   #endif
230 #endif /* CLOCK_IP_ENABLE_USER_MODE_SUPPORT */
231 }
Clock_Ip_EnableSpll(Clock_Ip_PllConfigType const * Config)232 static void Clock_Ip_EnableSpll(Clock_Ip_PllConfigType const* Config)
233 {
234     if (NULL_PTR != Config)
235     {
236     #ifdef CLOCK_IP_ENABLE_USER_MODE_SUPPORT
237       #if (STD_ON == CLOCK_IP_ENABLE_USER_MODE_SUPPORT)
238         OsIf_Trusted_Call1param(Clock_Ip_EnableSpll_TrustedCall,(Config));
239       #else
240         Clock_Ip_EnableSpll_TrustedCall(Config);
241       #endif
242     #endif /* CLOCK_IP_ENABLE_USER_MODE_SUPPORT */
243     }
244 }
245 #endif
246 
247 /*==================================================================================================
248 *                                        GLOBAL FUNCTIONS
249 ==================================================================================================*/
250 
251 #ifdef CLOCK_IP_SPLL_ENABLE
Clock_Ip_ResetSpll_TrustedCall(Clock_Ip_PllConfigType const * Config)252 void Clock_Ip_ResetSpll_TrustedCall(Clock_Ip_PllConfigType const* Config)
253 {
254     (void)Config;
255 
256     /* Power down PLL */
257 
258     /* Clear LK bit field */
259     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_LK_MASK));
260 
261     /* Disable clock monitor */
262     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_SPLLCM_MASK));
263 
264     /* Disable clock monitor reset*/
265     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_SPLLCMRE_MASK));
266 
267     /* Disable clock */
268     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_SPLLEN_MASK));
269 
270     /* Reset PLL configuration. */
271     #if (defined (CLOCK_IP_S32K142W) || defined(CLOCK_IP_S32K144W) || defined(CLOCK_IP_S32M244))
272     IP_SCG->SPLLCFG &= (~((uint32)SCG_SPLLCFG_SOURCE_MASK));
273     #endif
274     IP_SCG->SPLLCFG &= (~((uint32)SCG_SPLLCFG_PREDIV_MASK));
275     IP_SCG->SPLLCFG &= (~((uint32)SCG_SPLLCFG_MULT_MASK));
276 }
Clock_Ip_SetSpll_TrustedCall(Clock_Ip_PllConfigType const * Config)277 void Clock_Ip_SetSpll_TrustedCall(Clock_Ip_PllConfigType const* Config)
278 {
279     /* Configure SPLL. */
280     if (1U == Config->Enable)
281     {
282         #if (defined (CLOCK_IP_S32K142W) || defined(CLOCK_IP_S32K144W) || defined(CLOCK_IP_S32M244))
283         /* Select input reference. */
284         if (Config->InputReference == SOSC_CLK)
285         {
286             IP_SCG->SPLLCFG |= SCG_SPLLCFG_SOURCE(0UL);
287         }
288         else
289         {
290             IP_SCG->SPLLCFG |= SCG_SPLLCFG_SOURCE(1UL);
291         }
292         #endif
293 
294         /* Set PLL configuration. */
295         IP_SCG->SPLLCFG |= SCG_SPLLCFG_PREDIV((uint32)(Config->Predivider) - 1U)  |
296                         SCG_SPLLCFG_MULT((uint32)(Config->MulFactorDiv) - 16U);
297 
298         /* Enable clock, configure monitor, lock register. */
299         switch (Config->Monitor)
300         {
301 #ifdef CLOCK_IP_HAS_MONITOR_DISABLE
302             case CLOCK_IP_HAS_MONITOR_DISABLE:
303             {
304                 IP_SCG->SPLLCSR = SCG_SPLLCSR_SPLLCM(0UL) |
305                                SCG_SPLLCSR_SPLLCMRE(0UL);
306             }
307             break;
308 #endif
309 #ifdef CLOCK_IP_HAS_MONITOR_INT
310             case CLOCK_IP_HAS_MONITOR_INT:
311             {
312                 IP_SCG->SPLLCSR = SCG_SPLLCSR_SPLLCM(1UL) |
313                                SCG_SPLLCSR_SPLLCMRE(0UL);
314             }
315             break;
316 #endif
317 #ifdef CLOCK_IP_HAS_MONITOR_RESET
318             case CLOCK_IP_HAS_MONITOR_RESET:
319             {
320                 IP_SCG->SPLLCSR = SCG_SPLLCSR_SPLLCM(1UL) |
321                                SCG_SPLLCSR_SPLLCMRE(1UL);
322             }
323             break;
324 #endif
325             default:
326                 /* Invalid monitor mode */
327                 CLOCK_IP_DEV_ASSERT(FALSE);
328                 break;
329         }
330     }
331 }
Clock_Ip_DisableSpll_TrustedCall(Clock_Ip_NameType PllName)332 void Clock_Ip_DisableSpll_TrustedCall(Clock_Ip_NameType PllName)
333 {
334     (void)PllName;
335 
336     /* Power down PLL */
337 
338     /* Clear LK bit field */
339     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_LK_MASK));
340 
341     /* Disable clock */
342     IP_SCG->SPLLCSR &= (~((uint32)SCG_SPLLCSR_SPLLEN_MASK));
343 }
Clock_Ip_EnableSpll_TrustedCall(Clock_Ip_PllConfigType const * Config)344 void Clock_Ip_EnableSpll_TrustedCall(Clock_Ip_PllConfigType const* Config)
345 {
346     /* Configure SPLL. */
347     if (1U == Config->Enable)
348     {
349         /* Enable SPLL */
350         IP_SCG->SPLLCSR |= SCG_SPLLCSR_SPLLEN(1UL);
351     }
352 }
353 #endif
354 
355 
356 
357 
358 
359 
360 
361 /* Clock stop section code */
362 #define MCU_STOP_SEC_CODE
363 
364 #include "Mcu_MemMap.h"
365 
366 /*==================================================================================================
367 *                                        GLOBAL CONSTANTS
368 ==================================================================================================*/
369 /* Clock start constant section data */
370 #define MCU_START_SEC_CONST_UNSPECIFIED
371 
372 #include "Mcu_MemMap.h"
373 const Clock_Ip_PllCallbackType Clock_Ip_axPllCallbacks[CLOCK_IP_PLL_CALLBACKS_COUNT] =
374 {
375     {
376         Clock_Ip_CallbackPllEmpty,            /* Reset */
377         Clock_Ip_CallbackPllEmpty,            /* Set */
378         Clock_Ip_CallbackPllEmptyComplete,    /* Complete */
379         Clock_Ip_CallbackPllEmpty,            /* Enable */
380         Clock_Ip_CallbackPllEmptyDisable,     /* Disable */
381     },
382     /* System Pll */
383 #ifdef CLOCK_IP_SPLL_ENABLE
384     {
385         Clock_Ip_ResetSpll,                                                                /* Reset */
386         Clock_Ip_SetSpll,                                                                  /* Set */
387         Clock_Ip_CompleteSpll,                                                             /* Complete */
388         Clock_Ip_EnableSpll,                                                               /* Enable */
389         Clock_Ip_DisableSpll,                                                              /* Disable */
390     },
391 #endif
392 };
393 
394 
395 /* Clock stop constant section data */
396 #define MCU_STOP_SEC_CONST_UNSPECIFIED
397 
398 #include "Mcu_MemMap.h"
399 
400 #ifdef __cplusplus
401 }
402 #endif
403 
404 /** @} */
405 
406