|
| 1 | +/* |
| 2 | + * Copyright 2024 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/device.h> |
| 9 | +#include <zephyr/init.h> |
| 10 | +#include <soc.h> |
| 11 | +#include <fsl_common.h> |
| 12 | +#include <fsl_clock.h> |
| 13 | +#include <zephyr/arch/cpu.h> |
| 14 | + |
| 15 | +/******************************************************************************* |
| 16 | + * Definitions |
| 17 | + ******************************************************************************/ |
| 18 | +#define MCG_NODE DT_NODELABEL(mcg) |
| 19 | +#define OSC_NODE DT_NODELABEL(osc) |
| 20 | + |
| 21 | +#define SIM_LPUART_CLK_SEL_DISABLED 0U /*!< LPUART clock select: Disabled */ |
| 22 | +#define SIM_LPUART_CLK_SEL_IRC48M_CLK 1U /*!< LPUART clock select: IRC48M clock */ |
| 23 | +#define SIM_LPUART_CLK_SEL_OSCERCLK_CLK 2U /*!< LPUART clock select: OSCERCLK clock */ |
| 24 | +#define SIM_LPUART_CLK_SEL_MCGIRCLK_CLK 3U /*!< LPUART clock select: MCGIRCLK clock */ |
| 25 | + |
| 26 | +#define CLOCK_NODEID(clk) DT_CHILD(DT_INST(0, nxp_kinetis_sim), clk) |
| 27 | + |
| 28 | +#define CLOCK_DIVIDER(clk) DT_PROP_OR(CLOCK_NODEID(clk), clock_div, 1) - 1 |
| 29 | + |
| 30 | +#define LPUART_CLOCK_SEL(label) \ |
| 31 | + (DT_PHA(DT_NODELABEL(label), clocks, name) == kCLOCK_McgIrc48MClk \ |
| 32 | + ? SIM_LPUART_CLK_SEL_IRC48M_CLK \ |
| 33 | + : DT_PHA(DT_NODELABEL(label), clocks, name) == kCLOCK_Osc0ErClk \ |
| 34 | + ? SIM_LPUART_CLK_SEL_OSCERCLK_CLK \ |
| 35 | + : DT_PHA(DT_NODELABEL(label), clocks, name) == kCLOCK_McgInternalRefClk \ |
| 36 | + ? SIM_LPUART_CLK_SEL_MCGIRCLK_CLK \ |
| 37 | + : SIM_LPUART_CLK_SEL_DISABLED) |
| 38 | + |
| 39 | +/******************************************************************************* |
| 40 | + * Variables |
| 41 | + ******************************************************************************/ |
| 42 | + |
| 43 | +const mcglite_config_t mcgliteConfig_BOARD_BootClockRUN = { |
| 44 | + .outSrc = kMCGLITE_ClkSrcHirc, /* MCGOUTCLK source is HIRC */ |
| 45 | + .irclkEnableMode = kMCGLITE_IrclkEnable, /* MCGIRCLK enabled */ |
| 46 | + .ircs = kMCGLITE_Lirc8M, /* Slow internal reference (LIRC) 8 MHz clock */ |
| 47 | + /* Low-frequency Reference Clock Divider */ |
| 48 | + .fcrdiv = DT_PROP_OR(MCG_NODE, fcrdiv, 0), |
| 49 | + /* Second Low-frequency Reference Clock Divider */ |
| 50 | + .lircDiv2 = DT_PROP_OR(MCG_NODE, lircdiv2, 0), |
| 51 | + .hircEnableInNotHircMode = true, /* HIRC source is enabled */ |
| 52 | +}; |
| 53 | + |
| 54 | +const sim_clock_config_t simConfig_BOARD_BootClockRUN = { |
| 55 | + .er32kSrc = DT_PROP(DT_INST(0, nxp_kinetis_sim), er32k_select), |
| 56 | + .clkdiv1 = SIM_CLKDIV1_OUTDIV1(CLOCK_DIVIDER(core_clk)) | |
| 57 | + SIM_CLKDIV1_OUTDIV4(CLOCK_DIVIDER(flash_clk)), |
| 58 | +}; |
| 59 | + |
| 60 | +const osc_config_t oscConfig_BOARD_BootClockRUN = { |
| 61 | + .freq = DT_PROP(OSC_NODE, clock_frequency), |
| 62 | + .capLoad = 0, |
| 63 | +#if DT_ENUM_HAS_VALUE(OSC_NODE, mode, external) |
| 64 | + .workMode = kOSC_ModeExt, |
| 65 | +#elif DT_ENUM_HAS_VALUE(OSC_NODE, mode, low_power) |
| 66 | + .workMode = kOSC_ModeOscLowPower, |
| 67 | +#elif DT_ENUM_HAS_VALUE(OSC_NODE, mode, high_gain) |
| 68 | + .workMode = kOSC_ModeOscHighGain, |
| 69 | +#else |
| 70 | + #error "An oscillator mode must be defined" |
| 71 | +#endif |
| 72 | + .oscerConfig = { |
| 73 | + .enableMode = kOSC_ErClkEnable, |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +static void clock_init(void) |
| 78 | +{ |
| 79 | + /* Set the system clock dividers in SIM to safe value. */ |
| 80 | + CLOCK_SetSimSafeDivs(); |
| 81 | + /* Initializes OSC0 according to board configuration. */ |
| 82 | + CLOCK_InitOsc0(&oscConfig_BOARD_BootClockRUN); |
| 83 | + CLOCK_SetXtal0Freq(oscConfig_BOARD_BootClockRUN.freq); |
| 84 | + /* Set MCG to HIRC mode. */ |
| 85 | + CLOCK_SetMcgliteConfig(&mcgliteConfig_BOARD_BootClockRUN); |
| 86 | + /* Set the clock configuration in SIM module. */ |
| 87 | + CLOCK_SetSimConfig(&simConfig_BOARD_BootClockRUN); |
| 88 | + /* Set SystemCoreClock variable. */ |
| 89 | + SystemCoreClock = DT_PROP(DT_NODELABEL(cpu0), clock_frequency); |
| 90 | + /* Set LPUART0 clock source. */ |
| 91 | +#if DT_NODE_HAS_STATUS(DT_NODELABEL(lpuart0), okay) |
| 92 | + CLOCK_SetLpuart0Clock(LPUART_CLOCK_SEL(lpuart0)); |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +static int mcxc_init(void) |
| 97 | +{ |
| 98 | + clock_init(); |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +#ifdef CONFIG_PLATFORM_SPECIFIC_INIT |
| 103 | + |
| 104 | +void z_arm_platform_init(void) |
| 105 | +{ |
| 106 | + SystemInit(); |
| 107 | +} |
| 108 | + |
| 109 | +#endif /* CONFIG_PLATFORM_SPECIFIC_INIT */ |
| 110 | + |
| 111 | +SYS_INIT(mcxc_init, PRE_KERNEL_1, 0); |
0 commit comments