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.
31 #include "fsl_device_registers.h"
32 #include "fsl_clock_manager.h"
34 /*******************************************************************************
36 ******************************************************************************/
37 /* Table of base addresses for instances. */
38 const uint32_t g_simBaseAddr[] = SIM_BASE_ADDRS;
39 const uint32_t g_mcgBaseAddr[] = MCG_BASE_ADDRS;
42 /*******************************************************************************
44 ******************************************************************************/
46 /*FUNCTION**********************************************************************
48 * Function Name : CLOCK_SYS_GetSysClkFreq
49 * Description : Internal function to get the system clock frequency
50 * This function will check the clock name configuration table for specific
51 * chip family and find out the supported clock name for that chip family
52 * then it will call the mcg hal function to get the basic system clock,
53 * calculate the clock frequency for specified clock name.
55 *END**************************************************************************/
56 clock_manager_error_code_t CLOCK_SYS_GetSysClkFreq(clock_names_t clockName,
59 /* system clock out divider*/
62 const clock_name_config_t *table = &kClockNameConfigTable[clockName];
64 /* check if we need to use a reference clock*/
65 if (table->useOtherRefClock)
67 /* get other specified ref clock*/
68 if ( kClockManagerSuccess != CLOCK_SYS_GetFreq(table->otherRefClockName,
71 return kClockManagerNoSuchClockName;
76 /* get default ref clock */
77 *frequency = CLOCK_HAL_GetOutClk(g_mcgBaseAddr[0]);
80 /* get system clock divider*/
81 if ( CLOCK_HAL_GetDivider(g_simBaseAddr[0], table->dividerName, ÷r) == kSimHalSuccess)
83 /* get the frequency for the specified clock*/
84 *frequency = (*frequency) / (divider + 1);
85 return kClockManagerSuccess;
89 return kClockManagerNoSuchDivider;
93 /*FUNCTION**********************************************************************
95 * Function Name : CLOCK_SYS_GetFreq
96 * Description : Internal function to get the frequency by clock name
97 * This function will get/calculate the clock frequency based on clock name
98 * and current configuration of clock generator.
100 *END**************************************************************************/
101 clock_manager_error_code_t CLOCK_SYS_GetFreq(clock_names_t clockName,
104 clock_manager_error_code_t returnCode = kClockManagerSuccess;
106 /* branch according to clock name */
111 *frequency = CPU_XTAL32k_CLK_HZ;
114 #if FSL_FEATURE_MCG_HAS_OSC1
115 /* System oscillator 0 drives MCG clock */
116 *frequency = CPU_XTAL0_CLK_HZ;
118 /* System oscillator 0 drives MCG clock */
119 *frequency = CPU_XTAL_CLK_HZ;
123 #if FSL_FEATURE_MCG_HAS_OSC1
125 *frequency = CPU_XTAL1_CLK_HZ;
129 #if FSL_FEATURE_MCG_HAS_IRC_48M
132 *frequency = CPU_INT_IRC_CLK_HZ;
138 *frequency = CPU_XTAL32k_CLK_HZ;
142 *frequency = CPU_XTAL1hz_CLK_HZ; // defined in fsl_clock_manager.h for now
147 *frequency = CPU_LPO_CLK_HZ; // defined in fsl_clock_manager.h for now
150 /* mcg clocks, calling mcg clock functions */
152 *frequency = CLOCK_HAL_GetFllRefClk(g_mcgBaseAddr[0]);
155 *frequency = CLOCK_HAL_GetFllClk(g_mcgBaseAddr[0]);
157 #if FSL_FEATURE_MCG_HAS_PLL
159 *frequency = CLOCK_HAL_GetPll0Clk(g_mcgBaseAddr[0]);
163 *frequency = CLOCK_HAL_GetOutClk(g_mcgBaseAddr[0]);
166 *frequency = CLOCK_HAL_GetInternalRefClk(g_mcgBaseAddr[0]);
170 *frequency = SDHC0_CLKIN; // defined in fsl_clock_manager.h for now
172 case kENET_1588_CLKIN:
173 *frequency = ENET_1588_CLKIN; // defined in fsl_clock_manager.h for now
176 *frequency = EXTAL_Clock; // defined in fsl_clock_manager.h for now
179 *frequency = EXTAL1_Clock; // defined in fsl_clock_manager.h for now
182 *frequency = USB_CLKIN; // defined in fsl_clock_manager.h for now
192 returnCode = CLOCK_SYS_GetSysClkFreq(clockName, frequency);
197 *frequency = 55555; /* for testing use purpose*/
198 returnCode = kClockManagerNoSuchClockName;
206 /*FUNCTION**********************************************************************
208 * Function Name : CLOCK_SYS_SetSource
209 * Description : Set clock source setting
210 * This function will set the settings for specified clock source. Each clock
211 * source has its clock selection settings. Refer to reference manual for
212 * details of settings for each clock source. Refer to clock_source_names_t
215 *END**************************************************************************/
216 clock_manager_error_code_t CLOCK_SYS_SetSource(clock_source_names_t clockSource,
219 clock_manager_error_code_t returnCode = kClockManagerSuccess;
221 if (CLOCK_HAL_SetSource(g_simBaseAddr[0], clockSource, setting) != kSimHalSuccess)
223 returnCode = kClockManagerNoSuchClockSource;
229 /*FUNCTION**********************************************************************
231 * Function Name : CLOCK_SYS_GetSource
232 * Description : Get clock source setting
233 * This function will get the settings for specified clock source. Each clock
234 * source has its clock selection settings. Refer to reference manual for
235 * details of settings for each clock source. Refer to clock_source_names_t
238 *END**************************************************************************/
239 clock_manager_error_code_t CLOCK_SYS_GetSource(clock_source_names_t clockSource,
242 clock_manager_error_code_t returnCode = kClockManagerSuccess;
244 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], clockSource, setting) != kSimHalSuccess)
246 returnCode = kClockManagerNoSuchClockSource;
252 /*FUNCTION**********************************************************************
254 * Function Name : CLOCK_SYS_SetDivider
255 * Description : Set clock divider setting
256 * This function will set the setting for specified clock divider. Refer to
257 * reference manual for supported clock divider and value range. Refer to
258 * clock_divider_names_t for dividers.
260 *END**************************************************************************/
261 clock_manager_error_code_t CLOCK_SYS_SetDivider(clock_divider_names_t clockDivider,
264 clock_manager_error_code_t returnCode = kClockManagerSuccess;
266 if (CLOCK_HAL_SetDivider(g_simBaseAddr[0], clockDivider, setting) != kSimHalSuccess)
268 returnCode = kClockManagerNoSuchDivider;
274 /*FUNCTION**********************************************************************
276 * Function Name : CLOCK_SYS_GetDivider
277 * Description : Get clock divider setting
278 * This function will get the setting for specified clock divider. Refer to
279 * reference manual for supported clock divider and value range. Refer to
280 * clock_divider_names_t for dividers.
282 *END**************************************************************************/
283 clock_manager_error_code_t CLOCK_SYS_GetDivider(clock_divider_names_t clockDivider,
286 clock_manager_error_code_t returnCode = kClockManagerSuccess;
288 if (CLOCK_HAL_GetDivider(g_simBaseAddr[0], clockDivider, setting) != kSimHalSuccess)
290 returnCode = kClockManagerNoSuchDivider;
296 /*******************************************************************************
298 ******************************************************************************/