]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/MK64F12/fsl_clock_K64F12.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_MCU_K64F / MK64F12 / fsl_clock_K64F12.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 <stdint.h>
32 #include <stdlib.h>
33 #include <stdbool.h>
34 #include "fsl_device_registers.h"
35 #include "fsl_sim_hal.h"
36 #include "fsl_clock_manager.h"
37
38 /*******************************************************************************
39 * Definitions
40 ******************************************************************************/
41
42 /* Table of base addresses for instances. */
43 extern const uint32_t g_simBaseAddr[];
44 extern const uint32_t g_mcgBaseAddr[];
45
46 /*******************************************************************************
47 * Code
48 ******************************************************************************/
49
50 /*FUNCTION**********************************************************************
51 *
52 * Function Name : CLOCK_SYS_GetDmaFreq
53 * Description : Gets the clock frequency for DMA module
54 * This function gets the clock frequency for DMA moudle.
55 *
56 *END**************************************************************************/
57 uint32_t CLOCK_SYS_GetDmaFreq(uint32_t instance)
58 {
59 uint32_t freq = 0;
60 CLOCK_SYS_GetFreq(kSystemClock, &freq);
61 return freq;
62 }
63
64 /*FUNCTION**********************************************************************
65 *
66 * Function Name : CLOCK_SYS_GetDmamuxFreq
67 * Description : Gets the clock frequency for DMAMUX module
68 * This function gets the clock frequency for DMAMUX moudle.
69 *
70 *END**************************************************************************/
71 uint32_t CLOCK_SYS_GetDmamuxFreq(uint32_t instance)
72 {
73 uint32_t freq = 0;
74 CLOCK_SYS_GetFreq(kBusClock, &freq);
75 return freq;
76 }
77
78 /*FUNCTION**********************************************************************
79 *
80 * Function Name : CLOCK_SYS_GetPortFreq
81 * Description : Gets the clock frequency for PORT module
82 * This function gets the clock frequency for PORT moudle.
83 *
84 *END**************************************************************************/
85 uint32_t CLOCK_SYS_GetPortFreq(uint32_t instance)
86 {
87 uint32_t freq = 0;
88 CLOCK_SYS_GetFreq(kLpoClock, &freq);
89 return freq;
90 }
91
92 /*FUNCTION**********************************************************************
93 *
94 * Function Name : CLOCK_SYS_GetMpuFreq
95 * Description : Gets the clock frequency for MPU module
96 * This function gets the clock frequency for MPU moudle.
97 *
98 *END**************************************************************************/
99 uint32_t CLOCK_SYS_GetMpuFreq(uint32_t instance)
100 {
101 uint32_t freq = 0;
102 CLOCK_SYS_GetFreq(kSystemClock, &freq);
103 return freq;
104 }
105
106 /*FUNCTION**********************************************************************
107 *
108 * Function Name : CLOCK_SYS_GetEwmFreq
109 * Description : Gets the clock frequency for Ewm module
110 * This function gets the clock frequency for Ewm moudle.
111 *
112 *END**************************************************************************/
113 uint32_t CLOCK_SYS_GetEwmFreq(uint32_t instance)
114 {
115 uint32_t freq = 0;
116 CLOCK_SYS_GetFreq(kLpoClock, &freq);
117 return freq;
118 }
119
120 /*FUNCTION**********************************************************************
121 *
122 * Function Name : CLOCK_SYS_GetFlexbusFreq
123 * Description : Gets the clock frequency for FLEXBUS module
124 * This function gets the clock frequency for FLEXBUS moudle.
125 *
126 *END**************************************************************************/
127 uint32_t CLOCK_SYS_GetFlexbusFreq(uint32_t instance)
128 {
129 uint32_t freq = 0;
130 CLOCK_SYS_GetFreq(kSystemClock, &freq);
131 return freq;
132 }
133
134 /*FUNCTION**********************************************************************
135 *
136 * Function Name : CLOCK_SYS_GetFtfFreq
137 * Description : Gets the clock frequency for FTF module. (Flash Memory)
138 * This function gets the clock frequency for FTF moudle.
139 *
140 *END**************************************************************************/
141 uint32_t CLOCK_SYS_GetFtfFreq(uint32_t instance)
142 {
143 uint32_t freq = 0;
144 CLOCK_SYS_GetFreq(kFlashClock, &freq);
145 return freq;
146 }
147
148 /*FUNCTION**********************************************************************
149 *
150 * Function Name : CLOCK_SYS_GetCrcFreq
151 * Description : Gets the clock frequency for CRC module
152 * This function gets the clock frequency for CRC moudle.
153 *
154 *END**************************************************************************/
155 uint32_t CLOCK_SYS_GetCrcFreq(uint32_t instance)
156 {
157 uint32_t freq = 0;
158 CLOCK_SYS_GetFreq(kBusClock, &freq);
159 return freq;
160 }
161
162 /*FUNCTION**********************************************************************
163 *
164 * Function Name : CLOCK_SYS_GetRngaFreq
165 * Description : Gets the clock frequency for RNGA module
166 * This function gets the clock frequency for RNGA moudle.
167 *
168 *END**************************************************************************/
169 uint32_t CLOCK_SYS_GetRngaFreq(uint32_t instance)
170 {
171 uint32_t freq = 0;
172 CLOCK_SYS_GetFreq(kBusClock, &freq);
173 return freq;
174 }
175
176 /*FUNCTION**********************************************************************
177 *
178 * Function Name : CLOCK_SYS_GetAdcFreq
179 * Description : Gets the clock frequency for ADC module
180 * This function gets the clock frequency for ADC moudle.
181 *
182 *END**************************************************************************/
183 uint32_t CLOCK_SYS_GetAdcFreq(uint32_t instance)
184 {
185 uint32_t freq = 0;
186 CLOCK_SYS_GetFreq(kOsc0ErClock, &freq);
187 return freq;
188 }
189
190 /*FUNCTION**********************************************************************
191 *
192 * Function Name : CLOCK_SYS_GetCmpFreq
193 * Description : Gets the clock frequency for CMP module
194 * This function gets the clock frequency for CMP moudle.
195 *
196 *END**************************************************************************/
197 uint32_t CLOCK_SYS_GetCmpFreq(uint32_t instance)
198 {
199 uint32_t freq = 0;
200 CLOCK_SYS_GetFreq(kBusClock, &freq);
201 return freq;
202 }
203
204 /*FUNCTION**********************************************************************
205 *
206 * Function Name : CLOCK_SYS_GetVrefFreq
207 * Description : Gets the clock frequency for VREF module
208 * This function gets the clock frequency for VREF moudle.
209 *
210 *END**************************************************************************/
211 uint32_t CLOCK_SYS_GetVrefFreq(uint32_t instance)
212 {
213 uint32_t freq = 0;
214 CLOCK_SYS_GetFreq(kBusClock, &freq);
215 return freq;
216 }
217
218 /*FUNCTION**********************************************************************
219 *
220 * Function Name : CLOCK_SYS_GetPdbFreq
221 * Description : Gets the clock frequency for PDB module
222 * This function gets the clock frequency for PDB moudle.
223 *
224 *END**************************************************************************/
225 uint32_t CLOCK_SYS_GetPdbFreq(uint32_t instance)
226 {
227 uint32_t freq = 0;
228 CLOCK_SYS_GetFreq(kBusClock, &freq);
229 return freq;
230 }
231
232 /*FUNCTION**********************************************************************
233 *
234 * Function Name : CLOCK_SYS_GetFtmFreq
235 * Description : Gets the clock frequency for FTM module. (FlexTimers)
236 * This function gets the clock frequency for FTM moudle.
237 *
238 *END**************************************************************************/
239 uint32_t CLOCK_SYS_GetFtmFreq(uint32_t instance)
240 {
241 uint32_t freq = 0;
242 CLOCK_SYS_GetFreq(kMcgFfClock, &freq);
243 return freq;
244 }
245
246 /*FUNCTION**********************************************************************
247 *
248 * Function Name : CLOCK_SYS_GetPitFreq
249 * Description : Gets the clock frequency for Pit module.
250 * This function gets the clock frequency for Pit moudle.
251 *
252 *END**************************************************************************/
253 uint32_t CLOCK_SYS_GetPitFreq(uint32_t instance)
254 {
255 uint32_t freq = 0;
256 CLOCK_SYS_GetFreq(kBusClock, &freq);
257 return freq;
258 }
259
260 /*FUNCTION**********************************************************************
261 *
262 * Function Name : CLOCK_SYS_GetCmtFreq
263 * Description : Gets the clock frequency for CMT module.
264 * This function gets the clock frequency for CMT moudle.
265 *
266 *END**************************************************************************/
267 uint32_t CLOCK_SYS_GetCmtFreq(uint32_t instance)
268 {
269 uint32_t freq = 0;
270 CLOCK_SYS_GetFreq(kBusClock, &freq);
271 return freq;
272 }
273
274 /*FUNCTION**********************************************************************
275 *
276 * Function Name : CLOCK_SYS_GetEnetRmiiFreq
277 * Description : Gets the clock frequency for ENET module RMII clock.
278 * This function gets the clock frequency for ENET moudle RMII clock.
279 *
280 *END**************************************************************************/
281 uint32_t CLOCK_SYS_GetEnetRmiiFreq(uint32_t instance)
282 {
283 uint32_t freq = 0;
284 uint8_t setting;
285 clock_names_t clockName;
286 /* get the sim clock source setting*/
287 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockRmiiSrc, &setting) != kSimHalSuccess)
288 {
289 return freq;
290 }
291
292 if ((sim_rmii_clock_source_t)setting == kSimRmiiSrcExtalClk)
293 {
294 clockName = kEXTAL_Clock;
295 }
296 else
297 {
298 clockName = kENET_1588_CLKIN;
299 }
300
301 CLOCK_SYS_GetFreq(clockName, &freq);
302 return freq;
303 }
304
305 /*FUNCTION**********************************************************************
306 *
307 * Function Name : CLOCK_SYS_GetEnetTimeStampFreq
308 * Description : Gets the clock frequency for ENET module TIME clock.
309 * This function gets the clock frequency for ENET moudle TIME clock.
310 *
311 *END**************************************************************************/
312 uint32_t CLOCK_SYS_GetEnetTimeStampFreq(uint32_t instance)
313 {
314 uint32_t freq = 0;
315 uint8_t setting;
316 clock_names_t clockName;
317
318 /* get the sim clock source setting*/
319 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockTimeSrc, &setting) != kSimHalSuccess)
320 {
321 return freq;
322 }
323
324 switch ((sim_time_clock_source_t)setting)
325 {
326 case kSimTimeSrcCoreSysClk: /* Core/system clock */
327 clockName = kCoreClock;
328 break;
329 case kSimTimeSrcPllFllSel: /* clock as selected by SOPT2[PLLFLLSEL]. */
330 /* get the sim clock source setting*/
331 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting) != kSimHalSuccess)
332 {
333 return freq;
334 }
335
336 switch ((sim_pllfll_clock_sel_t)setting)
337 {
338 case kSimPllFllSelFll: /* Fll clock */
339 clockName = kMcgFllClock;
340 break;
341 case kSimPllFllSelPll: /* Pll0 clock */
342 clockName = kMcgPll0Clock;
343 break;
344 default:
345 clockName = kReserved;
346 break;
347 }
348 break;
349 case kSimTimeSrcOscerclk: /* OSCERCLK clock */
350 clockName = kOsc0ErClock;
351 break;
352 case kSimTimeSrcExt: /* Enet 1588 clock */
353 clockName = kENET_1588_CLKIN;
354 break;
355 default:
356 clockName = kReserved;
357 break;
358 }
359
360 /* Get ref clock freq */
361 CLOCK_SYS_GetFreq(clockName, &freq);
362 return freq;
363 }
364
365 /*FUNCTION**********************************************************************
366 *
367 * Function Name : CLOCK_SYS_GetUsbFreq
368 * Description : Gets the clock frequency for USB FS OTG module.
369 * This function gets the clock frequency for USB FS OTG moudle.
370 *
371 *END**************************************************************************/
372 uint32_t CLOCK_SYS_GetUsbFreq(uint32_t instance)
373 {
374 uint32_t freq = 0;
375 uint8_t setting;
376 clock_names_t clockName;
377 uint32_t frac = 0;
378 uint32_t divider = 0;
379
380 /* get the sim clock source setting*/
381 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockUsbSrc, &setting) != kSimHalSuccess)
382 {
383 return freq;
384 }
385
386 switch ((sim_usb_clock_source_t)setting)
387 {
388 case kSimUsbSrcClkIn: /* Core/system clock */
389 clockName = kUSB_CLKIN;
390 break;
391 case kSimUsbSrcPllFllSel: /* clock as selected by SOPT2[PLLFLLSEL]. */
392 /* get the sim clock source setting*/
393 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting) != kSimHalSuccess)
394 {
395 return freq;
396 }
397
398 switch ((sim_pllfll_clock_sel_t)setting)
399 {
400 case kSimPllFllSelFll: /* Fll clock */
401 clockName = kMcgFllClock;
402 break;
403 case kSimPllFllSelPll: /* Pll0 clock */
404 clockName = kMcgPll0Clock;
405 break;
406 default:
407 clockName = kReserved;
408 break;
409 }
410 break;
411 default:
412 clockName = kReserved;
413 break;
414 }
415
416 /* Get ref clock freq */
417 CLOCK_SYS_GetFreq(clockName, &freq);
418
419 /* Get divider and frac */
420 CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbDiv, &divider);
421 CLOCK_HAL_GetDivider(g_simBaseAddr[0], kClockDividerUsbFrac, &frac);
422
423 /* Divider output clock = Divider input clock × [ (FRAC+1) / (DIV+1) ]*/
424 freq = (freq) * (frac + 1) / (divider + 1);
425
426 return freq;
427 }
428
429 /*FUNCTION**********************************************************************
430 *
431 * Function Name : CLOCK_SYS_GetUsbdcdFreq
432 * Description : Gets the clock frequency for USB DCD module.
433 * This function gets the clock frequency for USB DCD moudle.
434 *
435 *END**************************************************************************/
436 uint32_t CLOCK_SYS_GetUsbdcdFreq(uint32_t instance)
437 {
438 uint32_t freq = 0;
439 CLOCK_SYS_GetFreq(kBusClock, &freq);
440 return freq;
441 }
442
443 /*FUNCTION**********************************************************************
444 *
445 * Function Name : CLOCK_SYS_GetSpiFreq
446 * Description : Gets the clock frequency for SPI module.
447 * This function gets the clock frequency for SPI moudle.
448 *
449 *END**************************************************************************/
450 uint32_t CLOCK_SYS_GetSpiFreq(uint32_t instance)
451 {
452 uint32_t freq = 0;
453 CLOCK_SYS_GetFreq(kBusClock, &freq);
454 return freq;
455 }
456
457 /*FUNCTION**********************************************************************
458 *
459 * Function Name : CLOCK_SYS_GetI2cFreq
460 * Description : Gets the clock frequency for I2C module.
461 * This function gets the clock frequency for I2C moudle.
462 *
463 *END**************************************************************************/
464 uint32_t CLOCK_SYS_GetI2cFreq(uint32_t instance)
465 {
466 uint32_t freq = 0;
467 CLOCK_SYS_GetFreq(kBusClock, &freq);
468 return freq;
469 }
470
471 /*FUNCTION**********************************************************************
472 *
473 * Function Name : CLOCK_SYS_GetUartFreq
474 * Description : Gets the clock frequency for UART module.
475 * This function gets the clock frequency for UART moudle.
476 *
477 *END**************************************************************************/
478 uint32_t CLOCK_SYS_GetUartFreq(uint32_t instance)
479 {
480 uint32_t freq = 0;
481
482 switch (instance)
483 {
484 case 0:
485 case 1:
486 CLOCK_SYS_GetFreq(kSystemClock, &freq);
487 break;
488 case 2:
489 case 3:
490 case 4:
491 case 5:
492 CLOCK_SYS_GetFreq(kBusClock, &freq);
493 break;
494 default:
495 break;
496 }
497
498 return freq;
499 }
500
501 /*FUNCTION**********************************************************************
502 *
503 * Function Name : CLOCK_SYS_GetSdhcFreq
504 * Description : Gets the clock frequency for SDHC module
505 * This function gets the clock frequency for SDHC moudle
506 *
507 *END**************************************************************************/
508 uint32_t CLOCK_SYS_GetSdhcFreq(uint32_t instance)
509 {
510 uint32_t freq = 0;
511 uint8_t setting;
512 clock_names_t clockName;
513
514 /* get the sim clock source setting*/
515 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockSdhcSrc, &setting) != kSimHalSuccess)
516 {
517 return freq;
518 }
519
520 switch ((sim_sdhc_clock_source_t)setting)
521 {
522 case kSimSdhcSrcCoreSysClk: /* Core/system clock */
523 clockName = kCoreClock;
524 break;
525 case kSimSdhcSrcPllFllSel: /* clock as selected by SOPT2[PLLFLLSEL]. */
526 /* get the sim clock source setting*/
527 if (CLOCK_HAL_GetSource(g_simBaseAddr[0], kClockPllfllSel, &setting) != kSimHalSuccess)
528 {
529 return freq;
530 }
531
532 switch ((sim_pllfll_clock_sel_t)setting)
533 {
534 case kSimPllFllSelFll: /* Fll clock */
535 clockName = kMcgFllClock;
536 break;
537 case kSimPllFllSelPll: /* Pll0 clock */
538 clockName = kMcgPll0Clock;
539 break;
540 default:
541 clockName = kReserved;
542 break;
543 }
544 break;
545 case kSimSdhcSrcOscerclk: /* OSCERCLK clock */
546 clockName = kOsc0ErClock;
547 break;
548 case kSimSdhcSrcExt: /* SDHC CLKIN clock */
549 clockName = kSDHC0_CLKIN;
550 break;
551 default:
552 clockName = kReserved;
553 break;
554 }
555
556 /* Get ref clock freq */
557 CLOCK_SYS_GetFreq(clockName, &freq);
558 return freq;
559 }
560
561 /*FUNCTION**********************************************************************
562 *
563 * Function Name : CLOCK_SYS_GetSaiFreq
564 * Description : Gets the clock frequency for I2S module
565 * This function gets the clock frequency for I2S moudle.
566 *
567 *END**************************************************************************/
568 uint32_t CLOCK_SYS_GetSaiFreq(uint32_t instance)
569 {
570 uint32_t freq = 0;
571 CLOCK_SYS_GetFreq(kBusClock, &freq);
572 return freq;
573 }
574
575 /*FUNCTION**********************************************************************
576 *
577 * Function Name : CLOCK_SYS_GetGpioFreq
578 * Description : Gets the clock frequency for GPIO module.
579 * This function gets the clock frequency for GPIO moudle.
580 *
581 *END**************************************************************************/
582 uint32_t CLOCK_SYS_GetGpioFreq(uint32_t instance)
583 {
584 uint32_t freq = 0;
585
586 CLOCK_SYS_GetFreq(kSystemClock, &freq);
587
588 return freq;
589 }
590 /*******************************************************************************
591 * EOF
592 ******************************************************************************/
Imprint / Impressum