1 /*
2  * Copyright (c) 2017, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief System/hardware module for nxp_lpc54114 platform
10  *
11  * This module provides routines to initialize and support board-level
12  * hardware for the nxp_lpc54114 platform.
13  */
14 
15 #include <zephyr/kernel.h>
16 #include <zephyr/device.h>
17 #include <zephyr/init.h>
18 #include <soc.h>
19 #include <zephyr/drivers/uart.h>
20 #include <zephyr/linker/sections.h>
21 #include <zephyr/arch/cpu.h>
22 #include <cortex_m/exception.h>
23 #include <fsl_power.h>
24 #include <fsl_clock.h>
25 #include <fsl_common.h>
26 #include <fsl_device_registers.h>
27 #ifdef CONFIG_GPIO_MCUX_LPC
28 #include <fsl_pint.h>
29 #endif
30 #if  defined(CONFIG_SECOND_CORE_MCUX) && defined(CONFIG_SOC_LPC54114_M4)
31 #include <zephyr_image_info.h>
32 /* Memcpy macro to copy segments from secondary core image stored in flash
33  * to RAM section that secondary core boots from.
34  * n is the segment number, as defined in zephyr_image_info.h
35  */
36 #define MEMCPY_SEGMENT(n, _)							\
37 	memcpy((uint32_t *)((SEGMENT_LMA_ADDRESS_ ## n) - ADJUSTED_LMA),	\
38 		(uint32_t *)(SEGMENT_LMA_ADDRESS_ ## n),			\
39 		(SEGMENT_SIZE_ ## n))
40 #endif
41 
42 /**
43  *
44  * @brief Initialize the system clock
45  *
46  */
47 #define CPU_FREQ DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency)
48 
clock_init(void)49 static ALWAYS_INLINE void clock_init(void)
50 {
51 
52 #ifdef CONFIG_SOC_LPC54114_M4
53 	/* Set up the clock sources */
54 
55 	/* Ensure FRO is on */
56 	POWER_DisablePD(kPDRUNCFG_PD_FRO_EN);
57 
58 	/*
59 	 * Switch to FRO 12MHz first to ensure we can change voltage without
60 	 * accidentally being below the voltage for current speed.
61 	 */
62 	CLOCK_AttachClk(kFRO12M_to_MAIN_CLK);
63 
64 	/* Set FLASH wait states for core */
65 	CLOCK_SetFLASHAccessCyclesForFreq(CPU_FREQ);
66 
67 	/* Set up high frequency FRO output to selected frequency */
68 	CLOCK_SetupFROClocking(CPU_FREQ);
69 
70 	/* Set up dividers */
71 	/* Set AHBCLKDIV divider to value 1 */
72 	CLOCK_SetClkDiv(kCLOCK_DivAhbClk, 1U, false);
73 
74 	/* Set up clock selectors - Attach clocks to the peripheries */
75 	/* Switch MAIN_CLK to FRO_HF */
76 	CLOCK_AttachClk(kFRO_HF_to_MAIN_CLK);
77 
78 	/* Attach 12 MHz clock to FLEXCOMM0 */
79 	CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
80 
81 #if DT_NODE_HAS_COMPAT_STATUS(DT_NODELABEL(flexcomm4), nxp_lpc_i2c, okay)
82 	/* attach 12 MHz clock to FLEXCOMM4 */
83 	CLOCK_AttachClk(kFRO12M_to_FLEXCOMM4);
84 #endif
85 
86 #if DT_NODE_HAS_COMPAT_STATUS(DT_NODELABEL(flexcomm5), nxp_lpc_spi, okay)
87 	/* Attach 12 MHz clock to FLEXCOMM5 */
88 	CLOCK_AttachClk(kFRO_HF_to_FLEXCOMM5);
89 #endif
90 
91 #endif /* CONFIG_SOC_LPC54114_M4 */
92 }
93 
94 /**
95  *
96  * @brief Perform basic hardware initialization
97  *
98  * Initialize the interrupt controller device drivers.
99  * Also initialize the timer device driver, if required.
100  *
101  * @return 0
102  */
103 
nxp_lpc54114_init(void)104 static int nxp_lpc54114_init(void)
105 {
106 	/* Initialize FRO/system clock to 48 MHz */
107 	clock_init();
108 
109 #ifdef CONFIG_GPIO_MCUX_LPC
110 	/* Turn on PINT device*/
111 	PINT_Init(PINT);
112 #endif
113 
114 	return 0;
115 }
116 
117 SYS_INIT(nxp_lpc54114_init, PRE_KERNEL_1, 0);
118 
119 #if defined(CONFIG_SOC_RESET_HOOK) && defined(CONFIG_SOC_LPC54114_M0)
120 
121 /* M4 core has a custom platform initialization routine in assembly,
122  * but M0 core does not. install one here to call SystemInit.
123  */
soc_reset_hook(void)124 void soc_reset_hook(void)
125 {
126 	SystemInit();
127 }
128 
129 #endif /* CONFIG_SOC_RESET_HOOK */
130 
131 
132 #if defined(CONFIG_SECOND_CORE_MCUX) && defined(CONFIG_SOC_LPC54114_M4)
133 
134 #define CORE_M0_BOOT_ADDRESS ((void *)CONFIG_SECOND_CORE_BOOT_ADDRESS_MCUX)
135 
136 /**
137  *
138  * @brief Slave Init
139  *
140  * This routine boots the secondary core
141  *
142  * @retval 0 on success.
143  *
144  */
145 /* This function is also called at deep sleep resume. */
_slave_init(void)146 int _slave_init(void)
147 {
148 	int32_t temp;
149 
150 
151 	/* Enable SRAM2, used by other core */
152 	SYSCON->AHBCLKCTRLSET[0] = SYSCON_AHBCLKCTRL_SRAM2_MASK;
153 
154 	/* Copy second core image to SRAM */
155 	LISTIFY(SEGMENT_NUM, MEMCPY_SEGMENT, (;));
156 
157 	/* Setup the reset handler pointer (PC) and stack pointer value.
158 	 * This is used once the second core runs its startup code.
159 	 * The second core first boots from flash (address 0x00000000)
160 	 * and then detects its identity (Cortex-M0, slave) and checks
161 	 * registers CPBOOT and CPSTACK and use them to continue the
162 	 * boot process.
163 	 * Make sure the startup code for the current core (Cortex-M4) is
164 	 * appropriate and shareable with the Cortex-M0 core!
165 	 */
166 	SYSCON->CPBOOT = SYSCON_CPBOOT_BOOTADDR(
167 			*(uint32_t *)((uint8_t *)CORE_M0_BOOT_ADDRESS + 0x4));
168 	SYSCON->CPSTACK = SYSCON_CPSTACK_STACKADDR(
169 			*(uint32_t *)CORE_M0_BOOT_ADDRESS);
170 
171 	/* Reset the secondary core and start its clocks */
172 	temp = SYSCON->CPUCTRL;
173 	temp |= 0xc0c48000;
174 	SYSCON->CPUCTRL = (temp | SYSCON_CPUCTRL_CM0CLKEN_MASK
175 					| SYSCON_CPUCTRL_CM0RSTEN_MASK);
176 	SYSCON->CPUCTRL = (temp | SYSCON_CPUCTRL_CM0CLKEN_MASK)
177 					& (~SYSCON_CPUCTRL_CM0RSTEN_MASK);
178 
179 	return 0;
180 }
181 
182 SYS_INIT(_slave_init, PRE_KERNEL_2, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
183 
184 #endif /*CONFIG_SECOND_CORE_MCUX && CONFIG_SOC_LPC54114_M4 */
185