1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 /*
10 * How to setup clock using clock driver functions:
11 *
12 * 1. CLOCK_SetSimSafeDivs, to make sure core clock, bus clock, flexbus clock
13 * and flash clock are in allowed range during clock mode switch.
14 *
15 * 2. Call CLOCK_Osc0Init to setup OSC clock, if it is used in target mode.
16 *
17 * 3. Set MCG configuration, MCG includes three parts: FLL clock, PLL clock and
18 * internal reference clock(MCGIRCLK). Follow the steps to setup:
19 *
20 * 1). Call CLOCK_BootToXxxMode to set MCG to target mode.
21 *
22 * 2). If target mode is FBI/BLPI/PBI mode, the MCGIRCLK has been configured
23 * correctly. For other modes, need to call CLOCK_SetInternalRefClkConfig
24 * explicitly to setup MCGIRCLK.
25 *
26 * 3). Don't need to configure FLL explicitly, because if target mode is FLL
27 * mode, then FLL has been configured by the function CLOCK_BootToXxxMode,
28 * if the target mode is not FLL mode, the FLL is disabled.
29 *
30 * 4). If target mode is PEE/PBE/PEI/PBI mode, then the related PLL has been
31 * setup by CLOCK_BootToXxxMode. In FBE/FBI/FEE/FBE mode, the PLL could
32 * be enabled independently, call CLOCK_EnablePll0 explicitly in this case.
33 *
34 * 4. Call CLOCK_SetSimConfig to set the clock configuration in SIM.
35 */
36
37 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
38 !!ClocksProfile
39 product: Clocks v1.0
40 processor: MKL25Z128xxx4
41 package_id: MKL25Z128VLK4
42 mcu_data: ksdk2_0
43 processor_version: 1.1.0
44 board: FRDM-KL25Z
45 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
46
47 #include "fsl_smc.h"
48 #include "clock_config.h"
49
50 /*******************************************************************************
51 * Definitions
52 ******************************************************************************/
53 #define MCG_PLL_DISABLE 0U /*!< MCGPLLCLK disabled */
54 #define OSC_CAP0P 0U /*!< Oscillator 0pF capacitor load */
55 #define OSC_ER_CLK_DISABLE 0U /*!< Disable external reference clock */
56 #define SIM_OSC32KSEL_LPO_CLK 3U /*!< OSC32KSEL select: LPO clock */
57 #define SIM_PLLFLLSEL_MCGFLLCLK_CLK 0U /*!< PLLFLL select: MCGFLLCLK clock */
58 #define SIM_PLLFLLSEL_MCGPLLCLK_CLK 1U /*!< PLLFLL select: MCGPLLCLK clock */
59
60 /*******************************************************************************
61 * Variables
62 ******************************************************************************/
63 /* System clock frequency. */
64 extern uint32_t SystemCoreClock;
65
66 /*******************************************************************************
67 * Code
68 ******************************************************************************/
69 /*FUNCTION**********************************************************************
70 *
71 * Function Name : CLOCK_CONFIG_SetFllExtRefDiv
72 * Description : Configure FLL external reference divider (FRDIV).
73 * Param frdiv : The value to set FRDIV.
74 *
75 *END**************************************************************************/
CLOCK_CONFIG_SetFllExtRefDiv(uint8_t frdiv)76 static void CLOCK_CONFIG_SetFllExtRefDiv(uint8_t frdiv)
77 {
78 MCG->C1 = ((MCG->C1 & ~MCG_C1_FRDIV_MASK) | MCG_C1_FRDIV(frdiv));
79 }
80
81 /*******************************************************************************
82 ********************** Configuration BOARD_BootClockRUN ***********************
83 ******************************************************************************/
84 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
85 !!Configuration
86 name: BOARD_BootClockRUN
87 outputs:
88 - {id: Bus_clock.outFreq, value: 24 MHz}
89 - {id: Core_clock.outFreq, value: 48 MHz, locked: true, accuracy: '0.001'}
90 - {id: ERCLK32K.outFreq, value: 1 kHz}
91 - {id: Flash_clock.outFreq, value: 24 MHz}
92 - {id: LPO_clock.outFreq, value: 1 kHz}
93 - {id: MCGIRCLK.outFreq, value: 32.768 kHz}
94 - {id: OSCERCLK.outFreq, value: 8 MHz}
95 - {id: PLLFLLCLK.outFreq, value: 48 MHz}
96 - {id: System_clock.outFreq, value: 48 MHz}
97 settings:
98 - {id: MCGMode, value: PEE}
99 - {id: MCG.FCRDIV.scale, value: '1', locked: true}
100 - {id: MCG.FRDIV.scale, value: '32'}
101 - {id: MCG.IREFS.sel, value: MCG.FRDIV}
102 - {id: MCG.PLLS.sel, value: MCG.PLL}
103 - {id: MCG.PRDIV.scale, value: '2', locked: true}
104 - {id: MCG.VDIV.scale, value: '24', locked: true}
105 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
106 - {id: MCG_C2_OSC_MODE_CFG, value: ModeOscLowPower}
107 - {id: MCG_C2_RANGE0_CFG, value: High}
108 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: High}
109 - {id: OSC0_CR_ERCLKEN_CFG, value: Enabled}
110 - {id: OSC_CR_ERCLKEN_CFG, value: Enabled}
111 - {id: SIM.CLKOUTSEL.sel, value: SIM.OUTDIV4}
112 - {id: SIM.OSC32KSEL.sel, value: PMC.LPOCLK}
113 - {id: SIM.OUTDIV1.scale, value: '2'}
114 - {id: SIM.PLLFLLSEL.sel, value: SIM.MCGPLLCLK_DIV2}
115 - {id: SIM.TPMSRCSEL.sel, value: SIM.PLLFLLSEL}
116 - {id: SIM.UART0SRCSEL.sel, value: SIM.PLLFLLSEL}
117 - {id: SIM.USBSRCSEL.sel, value: SIM.PLLFLLSEL}
118 sources:
119 - {id: OSC.OSC.outFreq, value: 8 MHz, enabled: true}
120 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
121
122 /*******************************************************************************
123 * Variables for BOARD_BootClockRUN configuration
124 ******************************************************************************/
125 const mcg_config_t mcgConfig_BOARD_BootClockRUN =
126 {
127 .mcgMode = kMCG_ModePEE, /* PEE - PLL Engaged External */
128 .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
129 .ircs = kMCG_IrcSlow, /* Slow internal reference clock selected */
130 .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
131 .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
132 .drs = kMCG_DrsLow, /* Low frequency range */
133 .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
134 .pll0Config =
135 {
136 .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
137 .prdiv = 0x1U, /* PLL Reference divider: divided by 2 */
138 .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
139 },
140 };
141 const sim_clock_config_t simConfig_BOARD_BootClockRUN =
142 {
143 .pllFllSel = SIM_PLLFLLSEL_MCGPLLCLK_CLK, /* PLLFLL select: MCGPLLCLK clock */
144 .er32kSrc = SIM_OSC32KSEL_LPO_CLK, /* OSC32KSEL select: LPO clock */
145 .clkdiv1 = 0x10010000U, /* SIM_CLKDIV1 - OUTDIV1: /2, OUTDIV4: /2 */
146 };
147 const osc_config_t oscConfig_BOARD_BootClockRUN =
148 {
149 .freq = 8000000U, /* Oscillator frequency: 8000000Hz */
150 .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
151 .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
152 .oscerConfig =
153 {
154 .enableMode = kOSC_ErClkEnable, /* Enable external reference clock, disable external reference clock in STOP mode */
155 }
156 };
157
158 /*******************************************************************************
159 * Code for BOARD_BootClockRUN configuration
160 ******************************************************************************/
BOARD_BootClockRUN(void)161 void BOARD_BootClockRUN(void)
162 {
163 /* Set the system clock dividers in SIM to safe value. */
164 CLOCK_SetSimSafeDivs();
165 /* Initializes OSC0 according to board configuration. */
166 CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN);
167 CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq);
168 /* Configure FLL external reference divider (FRDIV). */
169 CLOCK_CONFIG_SetFllExtRefDiv(mcgConfig_BOARD_BootClockRUN.frdiv);
170 /* Set MCG to PEE mode. */
171 CLOCK_BootToPeeMode(kMCG_OscselOsc,
172 kMCG_PllClkSelPll0,
173 &mcgConfig_BOARD_BootClockRUN.pll0Config);
174 /* Configure the Internal Reference clock (MCGIRCLK). */
175 CLOCK_SetInternalRefClkConfig(mcgConfig_BOARD_BootClockRUN.irclkEnableMode,
176 mcgConfig_BOARD_BootClockRUN.ircs,
177 mcgConfig_BOARD_BootClockRUN.fcrdiv);
178 /* Set the clock configuration in SIM module. */
179 CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN);
180 /* Set SystemCoreClock variable. */
181 SystemCoreClock = BOARD_BOOTCLOCKRUN_CORE_CLOCK;
182 }
183
184 /*******************************************************************************
185 ********************* Configuration BOARD_BootClockVLPR ***********************
186 ******************************************************************************/
187 /* TEXT BELOW IS USED AS SETTING FOR THE CLOCKS TOOL *****************************
188 !!Configuration
189 name: BOARD_BootClockVLPR
190 outputs:
191 - {id: Bus_clock.outFreq, value: 800 kHz}
192 - {id: Core_clock.outFreq, value: 4 MHz}
193 - {id: ERCLK32K.outFreq, value: 1 kHz}
194 - {id: Flash_clock.outFreq, value: 800 kHz}
195 - {id: LPO_clock.outFreq, value: 1 kHz}
196 - {id: MCGIRCLK.outFreq, value: 4 MHz}
197 - {id: System_clock.outFreq, value: 4 MHz}
198 settings:
199 - {id: MCGMode, value: BLPI}
200 - {id: powerMode, value: VLPR}
201 - {id: MCG.CLKS.sel, value: MCG.IRCS}
202 - {id: MCG.FCRDIV.scale, value: '1', locked: true}
203 - {id: MCG.FRDIV.scale, value: '32'}
204 - {id: MCG.IRCS.sel, value: MCG.FCRDIV}
205 - {id: MCG_C1_IRCLKEN_CFG, value: Enabled}
206 - {id: MCG_C2_OSC_MODE_CFG, value: ModeOscLowPower}
207 - {id: MCG_C2_RANGE0_CFG, value: High}
208 - {id: MCG_C2_RANGE0_FRDIV_CFG, value: High}
209 - {id: SIM.OSC32KSEL.sel, value: PMC.LPOCLK}
210 - {id: SIM.OUTDIV4.scale, value: '5'}
211 sources:
212 - {id: OSC.OSC.outFreq, value: 8 MHz}
213 * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE CLOCKS TOOL **/
214
215 /*******************************************************************************
216 * Variables for BOARD_BootClockVLPR configuration
217 ******************************************************************************/
218 const mcg_config_t mcgConfig_BOARD_BootClockVLPR =
219 {
220 .mcgMode = kMCG_ModeBLPI, /* BLPI - Bypassed Low Power Internal */
221 .irclkEnableMode = kMCG_IrclkEnable, /* MCGIRCLK enabled, MCGIRCLK disabled in STOP mode */
222 .ircs = kMCG_IrcFast, /* Fast internal reference clock selected */
223 .fcrdiv = 0x0U, /* Fast IRC divider: divided by 1 */
224 .frdiv = 0x0U, /* FLL reference clock divider: divided by 32 */
225 .drs = kMCG_DrsLow, /* Low frequency range */
226 .dmx32 = kMCG_Dmx32Default, /* DCO has a default range of 25% */
227 .pll0Config =
228 {
229 .enableMode = MCG_PLL_DISABLE, /* MCGPLLCLK disabled */
230 .prdiv = 0x0U, /* PLL Reference divider: divided by 1 */
231 .vdiv = 0x0U, /* VCO divider: multiplied by 24 */
232 },
233 };
234 const sim_clock_config_t simConfig_BOARD_BootClockVLPR =
235 {
236 .pllFllSel = SIM_PLLFLLSEL_MCGFLLCLK_CLK, /* PLLFLL select: MCGFLLCLK clock */
237 .er32kSrc = SIM_OSC32KSEL_LPO_CLK, /* OSC32KSEL select: LPO clock */
238 .clkdiv1 = 0x40000U, /* SIM_CLKDIV1 - OUTDIV1: /1, OUTDIV4: /5 */
239 };
240 const osc_config_t oscConfig_BOARD_BootClockVLPR =
241 {
242 .freq = 0U, /* Oscillator frequency: 0Hz */
243 .capLoad = (OSC_CAP0P), /* Oscillator capacity load: 0pF */
244 .workMode = kOSC_ModeOscLowPower, /* Oscillator low power */
245 .oscerConfig =
246 {
247 .enableMode = OSC_ER_CLK_DISABLE, /* Disable external reference clock */
248 }
249 };
250
251 /*******************************************************************************
252 * Code for BOARD_BootClockVLPR configuration
253 ******************************************************************************/
BOARD_BootClockVLPR(void)254 void BOARD_BootClockVLPR(void)
255 {
256 /* Set the system clock dividers in SIM to safe value. */
257 CLOCK_SetSimSafeDivs();
258 /* Set MCG to BLPI mode. */
259 CLOCK_BootToBlpiMode(mcgConfig_BOARD_BootClockVLPR.fcrdiv,
260 mcgConfig_BOARD_BootClockVLPR.ircs,
261 mcgConfig_BOARD_BootClockVLPR.irclkEnableMode);
262 /* Set the clock configuration in SIM module. */
263 CLOCK_SetSimConfig(&simConfig_BOARD_BootClockVLPR);
264 /* Set VLPR power mode. */
265 SMC_SetPowerModeProtection(SMC, kSMC_AllowPowerModeAll);
266 #if (defined(FSL_FEATURE_SMC_HAS_LPWUI) && FSL_FEATURE_SMC_HAS_LPWUI)
267 SMC_SetPowerModeVlpr(SMC, false);
268 #else
269 SMC_SetPowerModeVlpr(SMC);
270 #endif
271 while (SMC_GetPowerModeState(SMC) != kSMC_PowerStateVlpr)
272 {
273 }
274 /* Set SystemCoreClock variable. */
275 SystemCoreClock = BOARD_BOOTCLOCKVLPR_CORE_CLOCK;
276 }
277
278