]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/clock/fsl_clock_manager.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / drivers / clock / fsl_clock_manager.c
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
29  */
30
31 #include "fsl_device_registers.h"
32 #include "fsl_clock_manager.h"
33
34 /*******************************************************************************
35  * Definitions
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;
40                                                                  
41
42 /*******************************************************************************
43  * Code
44  ******************************************************************************/
45
46 /*FUNCTION**********************************************************************
47  *
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.
54  *
55  *END**************************************************************************/
56 clock_manager_error_code_t CLOCK_SYS_GetSysClkFreq(clock_names_t clockName,
57                                                         uint32_t *frequency)
58 {
59     /* system clock out divider*/
60     uint32_t divider;
61
62     const clock_name_config_t *table = &kClockNameConfigTable[clockName];
63
64     /* check if we need to use a reference clock*/
65     if (table->useOtherRefClock)
66     {
67         /* get other specified ref clock*/
68         if ( kClockManagerSuccess != CLOCK_SYS_GetFreq(table->otherRefClockName,
69                                                                     frequency) )
70         {
71             return kClockManagerNoSuchClockName;
72         }
73     }
74     else
75     {
76         /* get default ref clock */
77         *frequency = CLOCK_HAL_GetOutClk(g_mcgBaseAddr[0]);
78     }
79
80     /* get system clock divider*/
81     if ( CLOCK_HAL_GetDivider(g_simBaseAddr[0], table->dividerName, &divider) == kSimHalSuccess)
82     {
83         /* get the frequency for the specified clock*/
84         *frequency = (*frequency) / (divider + 1);
85         return kClockManagerSuccess;
86     }
87     else
88     {
89         return kClockManagerNoSuchDivider;
90     }
91 }
92
93 /*FUNCTION**********************************************************************
94  *
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.
99  *
100  *END**************************************************************************/
101 clock_manager_error_code_t CLOCK_SYS_GetFreq(clock_names_t clockName,
102                                                                    uint32_t *frequency)
103 {
104     clock_manager_error_code_t returnCode = kClockManagerSuccess;
105
106     /* branch according to clock name */
107     switch(clockName)
108     {
109     /* osc clock*/
110     case kOsc32kClock:
111         *frequency = CPU_XTAL32k_CLK_HZ;    
112         break;
113     case kOsc0ErClock:
114 #if FSL_FEATURE_MCG_HAS_OSC1
115             /* System oscillator 0 drives MCG clock */
116         *frequency = CPU_XTAL0_CLK_HZ;
117 #else
118             /* System oscillator 0 drives MCG clock */
119         *frequency = CPU_XTAL_CLK_HZ;
120 #endif      
121         break;
122
123 #if FSL_FEATURE_MCG_HAS_OSC1
124     case kOsc1ErClock:
125         *frequency = CPU_XTAL1_CLK_HZ;           
126         break;
127 #endif          
128
129 #if FSL_FEATURE_MCG_HAS_IRC_48M  
130     /* irc clock*/
131     case kIrc48mClock:
132         *frequency = CPU_INT_IRC_CLK_HZ;  
133         break;
134 #endif
135         
136     /* rtc clock*/
137     case kRtc32kClock:
138         *frequency = CPU_XTAL32k_CLK_HZ;    
139         break;
140
141     case kRtc1hzClock:
142         *frequency = CPU_XTAL1hz_CLK_HZ;    // defined in fsl_clock_manager.h for now
143         break;
144
145     /* lpo clcok*/
146     case kLpoClock:
147         *frequency = CPU_LPO_CLK_HZ;       // defined in fsl_clock_manager.h for now
148         break;
149
150     /* mcg clocks, calling mcg clock functions */
151     case kMcgFfClock:
152         *frequency = CLOCK_HAL_GetFllRefClk(g_mcgBaseAddr[0]);
153         break;
154     case kMcgFllClock:
155         *frequency = CLOCK_HAL_GetFllClk(g_mcgBaseAddr[0]);
156         break;
157 #if FSL_FEATURE_MCG_HAS_PLL
158     case kMcgPll0Clock:
159         *frequency = CLOCK_HAL_GetPll0Clk(g_mcgBaseAddr[0]);
160         break;
161 #endif
162     case kMcgOutClock:
163         *frequency = CLOCK_HAL_GetOutClk(g_mcgBaseAddr[0]);
164         break;
165     case kMcgIrClock:
166         *frequency = CLOCK_HAL_GetInternalRefClk(g_mcgBaseAddr[0]);
167         break;
168
169     case kSDHC0_CLKIN:
170         *frequency = SDHC0_CLKIN;            // defined in fsl_clock_manager.h for now
171         break;
172     case kENET_1588_CLKIN:
173         *frequency = ENET_1588_CLKIN;        // defined in fsl_clock_manager.h for now
174         break;
175     case kEXTAL_Clock:
176         *frequency = EXTAL_Clock;            // defined in fsl_clock_manager.h for now
177         break;
178     case kEXTAL1_Clock:
179         *frequency = EXTAL1_Clock;           // defined in fsl_clock_manager.h for now
180         break;
181     case kUSB_CLKIN:
182         *frequency = USB_CLKIN;              // defined in fsl_clock_manager.h for now
183         break;
184
185     /* system clocks */
186     case kCoreClock:
187     case kSystemClock:
188     case kPlatformClock:
189     case kBusClock:
190     case kFlexBusClock:
191     case kFlashClock:
192         returnCode = CLOCK_SYS_GetSysClkFreq(clockName, frequency);
193         break;
194         /* reserved value*/
195     case kReserved:
196     default:
197         *frequency = 55555;                     /* for testing use purpose*/
198         returnCode = kClockManagerNoSuchClockName;
199         break;
200     }
201
202     return returnCode;
203 }
204
205
206 /*FUNCTION**********************************************************************
207  *
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 
213  * for clock sources.
214  * 
215  *END**************************************************************************/
216 clock_manager_error_code_t CLOCK_SYS_SetSource(clock_source_names_t clockSource,
217                                                uint8_t setting)
218 {
219     clock_manager_error_code_t returnCode = kClockManagerSuccess;
220
221     if (CLOCK_HAL_SetSource(g_simBaseAddr[0], clockSource, setting) != kSimHalSuccess)
222     {
223         returnCode =  kClockManagerNoSuchClockSource;
224     }
225
226     return returnCode;
227 }
228
229 /*FUNCTION**********************************************************************
230  *
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
236  * for clock sources.
237  * 
238  *END**************************************************************************/
239 clock_manager_error_code_t CLOCK_SYS_GetSource(clock_source_names_t clockSource,
240                                                uint8_t *setting)
241 {
242     clock_manager_error_code_t returnCode = kClockManagerSuccess;
243
244     if (CLOCK_HAL_GetSource(g_simBaseAddr[0], clockSource, setting) != kSimHalSuccess)
245     {
246         returnCode =  kClockManagerNoSuchClockSource;
247     }
248
249     return returnCode;
250 }
251
252 /*FUNCTION**********************************************************************
253  *
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.
259  * 
260  *END**************************************************************************/
261 clock_manager_error_code_t CLOCK_SYS_SetDivider(clock_divider_names_t clockDivider, 
262                                                 uint32_t setting)
263 {
264     clock_manager_error_code_t returnCode = kClockManagerSuccess;
265
266     if (CLOCK_HAL_SetDivider(g_simBaseAddr[0], clockDivider, setting) != kSimHalSuccess)
267     {
268         returnCode = kClockManagerNoSuchDivider;
269     }
270
271     return returnCode;
272 }
273
274 /*FUNCTION**********************************************************************
275  *
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.
281  * 
282  *END**************************************************************************/
283 clock_manager_error_code_t CLOCK_SYS_GetDivider(clock_divider_names_t clockDivider,
284                                                 uint32_t *setting)
285 {
286     clock_manager_error_code_t returnCode = kClockManagerSuccess;
287
288     if (CLOCK_HAL_GetDivider(g_simBaseAddr[0], clockDivider, setting) != kSimHalSuccess)
289     {
290         returnCode = kClockManagerNoSuchDivider;
291     }
292
293     return returnCode;
294 }
295
296 /*******************************************************************************
297  * EOF
298  ******************************************************************************/
299