2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "fsl_device_registers.h"
35 #include "fsl_sim_hal.h"
36 #include "fsl_clock_manager.h"
37 #include "fsl_osc_hal.h"
39 /*******************************************************************************
41 ******************************************************************************/
43 /* Table of base addresses for instances. */
44 extern const uint32_t g_simBaseAddr[];
45 extern const uint32_t g_mcgBaseAddr[];
46 const uint32_t g_oscBaseAddr[] = OSC_BASE_ADDRS;
48 /*******************************************************************************
50 ******************************************************************************/
52 /*FUNCTION**********************************************************************
54 * Function Name : CLOCK_SYS_GetDmaFreq
55 * Description : Gets the clock frequency for DMA module
56 * This function gets the clock frequency for DMA moudle.
58 *END**************************************************************************/
59 uint32_t CLOCK_SYS_GetDmaFreq(uint32_t instance)
62 CLOCK_SYS_GetFreq(kSystemClock, &freq);
66 /*FUNCTION**********************************************************************
68 * Function Name : CLOCK_SYS_GetDmamuxFreq
69 * Description : Gets the clock frequency for DMAMUX module
70 * This function gets the clock frequency for DMAMUX moudle.
72 *END**************************************************************************/
73 uint32_t CLOCK_SYS_GetDmamuxFreq(uint32_t instance)
76 CLOCK_SYS_GetFreq(kBusClock, &freq);
80 /*FUNCTION**********************************************************************
82 * Function Name : CLOCK_SYS_GetPortFreq
83 * Description : Gets the clock frequency for PORT module
84 * This function gets the clock frequency for PORT moudle.
86 *END**************************************************************************/
87 uint32_t CLOCK_SYS_GetPortFreq(uint32_t instance)
90 CLOCK_SYS_GetFreq(kLpoClock, &freq);
94 /*FUNCTION**********************************************************************
96 * Function Name : CLOCK_SYS_GetEwmFreq
97 * Description : Gets the clock frequency for Ewm module
98 * This function gets the clock frequency for Ewm moudle.
100 *END**************************************************************************/
101 uint32_t CLOCK_SYS_GetEwmFreq(uint32_t instance)
104 CLOCK_SYS_GetFreq(kLpoClock, &freq);
108 /*FUNCTION**********************************************************************
110 * Function Name : CLOCK_SYS_GetFlexbusFreq
111 * Description : Gets the clock frequency for FLEXBUS module
112 * This function gets the clock frequency for FLEXBUS moudle.
114 *END**************************************************************************/
115 uint32_t CLOCK_SYS_GetFlexbusFreq(uint32_t instance)
118 CLOCK_SYS_GetFreq(kSystemClock, &freq);
122 /*FUNCTION**********************************************************************
124 * Function Name : CLOCK_SYS_GetFtfFreq
125 * Description : Gets the clock frequency for FTF module. (Flash Memory)
126 * This function gets the clock frequency for FTF moudle.
128 *END**************************************************************************/
129 uint32_t CLOCK_SYS_GetFtfFreq(uint32_t instance)
132 CLOCK_SYS_GetFreq(kFlashClock, &freq);
136 /*FUNCTION**********************************************************************
138 * Function Name : CLOCK_SYS_GetCrcFreq
139 * Description : Gets the clock frequency for CRC module
140 * This function gets the clock frequency for CRC moudle.
142 *END**************************************************************************/
143 uint32_t CLOCK_SYS_GetCrcFreq(uint32_t instance)
146 CLOCK_SYS_GetFreq(kBusClock, &freq);
150 /*FUNCTION**********************************************************************
152 * Function Name : CLOCK_SYS_GetRngaFreq
153 * Description : Gets the clock frequency for RNGA module
154 * This function gets the clock frequency for RNGA moudle.
156 *END**************************************************************************/
157 uint32_t CLOCK_SYS_GetRngaFreq(uint32_t instance)
160 CLOCK_SYS_GetFreq(kBusClock, &freq);
164 /*FUNCTION**********************************************************************
166 * Function Name : CLOCK_SYS_GetAdcFreq
167 * Description : Gets the clock frequency for ADC module
168 * This function gets the clock frequency for ADC moudle.
170 *END**************************************************************************/
171 uint32_t CLOCK_SYS_GetAdcFreq(uint32_t instance)
176 CLOCK_SYS_GetFreq(kOsc0ErClock, &freq);
178 divider = OSC_HAL_GetExternalRefClkDivCmd(g_oscBaseAddr[0]);
179 freq = freq >> divider; /* 2 bits divider, divide by 1/2/4/8 */
184 /*FUNCTION**********************************************************************
186 * Function Name : CLOCK_SYS_GetCmpFreq
187 * Description : Gets the clock frequency for CMP module
188 * This function gets the clock frequency for CMP moudle.
190 *END**************************************************************************/
191 uint32_t CLOCK_SYS_GetCmpFreq(uint32_t instance)
194 CLOCK_SYS_GetFreq(kBusClock, &freq);
198 /*FUNCTION**********************************************************************
200 * Function Name : CLOCK_SYS_GetVrefFreq
201 * Description : Gets the clock frequency for VREF module
202 * This function gets the clock frequency for VREF moudle.
204 *END**************************************************************************/
205 uint32_t CLOCK_SYS_GetVrefFreq(uint32_t instance)
208 CLOCK_SYS_GetFreq(kBusClock, &freq);
212 /*FUNCTION**********************************************************************
214 * Function Name : CLOCK_SYS_GetPdbFreq
215 * Description : Gets the clock frequency for PDB module
216 * This function gets the clock frequency for PDB moudle.
218 *END**************************************************************************/
219 uint32_t CLOCK_SYS_GetPdbFreq(uint32_t instance)
222 CLOCK_SYS_GetFreq(kBusClock, &freq);
226 /*FUNCTION**********************************************************************
228 * Function Name : CLOCK_SYS_GetFtmFreq
229 * Description : Gets the clock frequency for FTM module. (FlexTimers)
230 * This function gets the clock frequency for FTM moudle.
232 *END**************************************************************************/
233 uint32_t CLOCK_SYS_GetFtmFreq(uint32_t instance)
236 CLOCK_SYS_GetFreq(kMcgFfClock, &freq);
240 /*FUNCTION**********************************************************************
242 * Function Name : CLOCK_SYS_GetPitFreq
243 * Description : Gets the clock frequency for Pit module.
244 * This function gets the clock frequency for Pit moudle.
246 *END**************************************************************************/
247 uint32_t CLOCK_SYS_GetPitFreq(uint32_t instance)
250 CLOCK_SYS_GetFreq(kBusClock, &freq);
254 /*FUNCTION**********************************************************************
256 * Function Name : CLOCK_SYS_GetUsbFreq
257 * Description : Gets the clock frequency for USB FS OTG module.
258 * This function gets the clock frequency for USB FS OTG moudle.
260 *END**************************************************************************/
261 uint32_t CLOCK_SYS_GetUsbFreq(uint32_t instance)
265 clock_names_t clockName;
267 uint32_t divider = 0;
269 /* get the sim clock source setting*/
270 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockUsbSrc, &setting) != kSimHalSuccess)
275 switch ((sim_usb_clock_source_t)setting)
277 case kSimUsbSrcClkIn: /* Core/system clock */
278 clockName = kUSB_CLKIN;
280 case kSimUsbSrcPllFllSel: /* clock as selected by SOPT2[PLLFLLSEL]. */
281 /* get the sim clock source setting*/
282 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting) != kSimHalSuccess)
287 switch ((sim_pllfll_clock_sel_t)setting)
289 case kSimPllFllSelFll: /* Fll clock */
290 clockName = kMcgFllClock;
292 case kSimPllFllSelPll: /* Pll0 clock */
293 clockName = kMcgPll0Clock;
295 case kSimPllFllSelIrc: /* Irc 48Mhz clock */
296 clockName = kIrc48mClock;
299 clockName = kReserved;
304 clockName = kReserved;
308 /* Get ref clock freq */
309 CLOCK_SYS_GetFreq(clockName, &freq);
311 /* Get divider and frac */
312 CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbDiv, ÷r);
313 CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbFrac, &frac);
315 /* Divider output clock = Divider input clock × [ (FRAC+1) / (DIV+1) ]*/
316 freq = (freq) * (frac + 1) / (divider + 1);
321 /*FUNCTION**********************************************************************
323 * Function Name : CLOCK_SYS_GetSpiFreq
324 * Description : Gets the clock frequency for SPI module.
325 * This function gets the clock frequency for SPI moudle.
327 *END**************************************************************************/
328 uint32_t CLOCK_SYS_GetSpiFreq(uint32_t instance)
331 CLOCK_SYS_GetFreq(kBusClock, &freq);
335 /*FUNCTION**********************************************************************
337 * Function Name : CLOCK_SYS_GetI2cFreq
338 * Description : Gets the clock frequency for I2C module.
339 * This function gets the clock frequency for I2C moudle.
341 *END**************************************************************************/
342 uint32_t CLOCK_SYS_GetI2cFreq(uint32_t instance)
345 CLOCK_SYS_GetFreq(kBusClock, &freq);
349 /*FUNCTION**********************************************************************
351 * Function Name : CLOCK_SYS_GetUartFreq
352 * Description : Gets the clock frequency for UART module.
353 * This function gets the clock frequency for UART moudle.
355 *END**************************************************************************/
356 uint32_t CLOCK_SYS_GetUartFreq(uint32_t instance)
364 CLOCK_SYS_GetFreq(kSystemClock, &freq);
367 CLOCK_SYS_GetFreq(kBusClock, &freq);
376 /*FUNCTION**********************************************************************
378 * Function Name : CLOCK_SYS_GetLpuartFreq
379 * Description : Gets the clock frequency for LPUART module.
380 * This function gets the clock frequency for LPUART moudle.
382 *END**************************************************************************/
383 uint32_t CLOCK_SYS_GetLpuartFreq(uint32_t instance)
388 clock_names_t clockName;
389 uint32_t divider = 0;
391 /* get the sim clock source setting*/
392 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockLpuartSrc, &setting) != kSimHalSuccess)
397 switch ((sim_lpuart_clock_source_t)setting)
399 case kSimLpuartSrcPllFllSel: /* clock as selected by SOPT2[PLLFLLSEL]. */
400 /* get the sim clock source setting*/
401 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting1) != kSimHalSuccess)
406 switch ((sim_pllfll_clock_sel_t)setting1)
408 case kSimPllFllSelFll: /* Fll clock */
409 clockName = kMcgFllClock;
411 case kSimPllFllSelPll: /* Pll0 clock */
412 clockName = kMcgPll0Clock;
414 case kSimPllFllSelIrc: /* Irc 48Mhz clock */
415 clockName = kIrc48mClock;
418 clockName = kReserved;
422 case kSimLpuartSrcOscErclk: /* OscErClk with divider */
423 clockName = kOsc0ErClock;
425 case kSimLpuartSrcMcgIrclk: /* MCGIRCLK */
426 clockName = kMcgIrClock;
429 clockName = kReserved;
433 /* Get ref clock freq */
434 CLOCK_SYS_GetFreq(clockName, &freq);
436 if ((sim_lpuart_clock_source_t)setting == kSimLpuartSrcOscErclk)
438 divider = OSC_HAL_GetExternalRefClkDivCmd(g_oscBaseAddr[0]);
439 freq = freq >> divider; /* 2 bits divider, divide by 1/2/4/8 */
445 /*FUNCTION**********************************************************************
447 * Function Name : CLOCK_SYS_GetSaiFreq
448 * Description : Gets the clock frequency for I2S module
449 * This function gets the clock frequency for I2S moudle.
451 *END**************************************************************************/
452 uint32_t CLOCK_SYS_GetSaiFreq(uint32_t instance)
455 CLOCK_SYS_GetFreq(kBusClock, &freq);
459 /*FUNCTION**********************************************************************
461 * Function Name : CLOCK_SYS_GetGpioFreq
462 * Description : Gets the clock frequency for GPIO module.
463 * This function gets the clock frequency for GPIO moudle.
465 *END**************************************************************************/
466 uint32_t CLOCK_SYS_GetGpioFreq(uint32_t instance)
470 CLOCK_SYS_GetFreq(kPlatformClock, &freq);
475 /*******************************************************************************
477 ******************************************************************************/