1 /*
2 * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /* Include esp-idf headers first to avoid redefining BIT() macro */
8 #include "soc.h"
9 #include <soc/rtc_cntl_reg.h>
10 #include <soc/timer_group_reg.h>
11 #include <zephyr/drivers/interrupt_controller/intc_esp32.h>
12 #include <xtensa/config/core-isa.h>
13 #include <xtensa/corebits.h>
14
15 #include <zephyr/kernel_structs.h>
16 #include <kernel_internal.h>
17 #include <string.h>
18 #include <zephyr/toolchain.h>
19 #include <zephyr/types.h>
20
21 #include "esp_private/system_internal.h"
22 #include "esp32s2/rom/cache.h"
23 #include "soc/gpio_periph.h"
24 #include "esp_spi_flash.h"
25 #include "esp_cpu.h"
26 #include "hal/cpu_ll.h"
27 #include "hal/soc_ll.h"
28 #include "hal/wdt_hal.h"
29 #include "esp_timer.h"
30 #include "esp_err.h"
31 #include "esp32s2/spiram.h"
32 #include "esp_clk_internal.h"
33 #include <zephyr/sys/printk.h>
34
35 #ifdef CONFIG_MCUBOOT
36 #include "bootloader_init.h"
37 #endif /* CONFIG_MCUBOOT */
38
39 extern void rtc_clk_cpu_freq_set_xtal(void);
40
41 #if CONFIG_ESP_SPIRAM
42 extern int _ext_ram_bss_start;
43 extern int _ext_ram_bss_end;
44 #endif
45
46 /*
47 * This is written in C rather than assembly since, during the port bring up,
48 * Zephyr is being booted by the Espressif bootloader. With it, the C stack
49 * is already set up.
50 */
__esp_platform_start(void)51 void __attribute__((section(".iram1"))) __esp_platform_start(void)
52 {
53 extern uint32_t _init_start;
54
55 /* Move the exception vector table to IRAM. */
56 __asm__ __volatile__ (
57 "wsr %0, vecbase"
58 :
59 : "r"(&_init_start));
60
61 /* Zero out BSS */
62 z_bss_zero();
63
64 /*
65 * Configure the mode of instruction cache :
66 * cache size, cache associated ways, cache line size.
67 */
68 esp_config_instruction_cache_mode();
69
70 /*
71 * If we need use SPIRAM, we should use data cache, or if we want to
72 * access rodata, we also should use data cache.
73 * Configure the mode of data : cache size, cache associated ways, cache
74 * line size.
75 * Enable data cache, so if we don't use SPIRAM, it just works.
76 */
77 #if CONFIG_ESP_SPIRAM
78 esp_config_data_cache_mode();
79 esp_rom_Cache_Enable_DCache(0);
80 #endif
81
82 /* Disable normal interrupts. */
83 __asm__ __volatile__ (
84 "wsr %0, PS"
85 :
86 : "r"(PS_INTLEVEL(XCHAL_EXCM_LEVEL) | PS_UM | PS_WOE));
87
88 /* Initialize the architecture CPU pointer. Some of the
89 * initialization code wants a valid _current before
90 * arch_kernel_init() is invoked.
91 */
92 __asm__ volatile("wsr.MISC0 %0; rsync" : : "r"(&_kernel.cpus[0]));
93
94 #ifdef CONFIG_MCUBOOT
95 /* MCUboot early initialisation. */
96 if (bootloader_init()) {
97 abort();
98 }
99 #else
100 /* ESP-IDF 2nd stage bootloader enables RTC WDT to check on startup sequence
101 * related issues in application. Hence disable that as we are about to start
102 * Zephyr environment.
103 */
104 wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
105
106 wdt_hal_write_protect_disable(&rtc_wdt_ctx);
107 wdt_hal_disable(&rtc_wdt_ctx);
108 wdt_hal_write_protect_enable(&rtc_wdt_ctx);
109
110 /* Configures the CPU clock, RTC slow and fast clocks, and performs
111 * RTC slow clock calibration.
112 */
113 esp_clk_init();
114
115 esp_timer_early_init();
116
117 #if CONFIG_ESP_SPIRAM
118
119 memset(&_ext_ram_bss_start,
120 0,
121 (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
122
123 esp_err_t err = esp_spiram_init();
124
125 if (err != ESP_OK) {
126 printk("Failed to Initialize SPIRAM, aborting.\n");
127 abort();
128 }
129 esp_spiram_init_cache();
130 if (esp_spiram_get_size() < CONFIG_ESP_SPIRAM_SIZE) {
131 printk("SPIRAM size is less than configured size, aborting.\n");
132 abort();
133 }
134
135 #endif /* CONFIG_ESP_SPIRAM */
136
137 /* Scheduler is not started at this point. Hence, guard functions
138 * must be initialized after esp_spiram_init_cache which internally
139 * uses guard functions. Setting guard functions before SPIRAM
140 * cache initialization will result in a crash.
141 */
142 #if CONFIG_SOC_FLASH_ESP32 || CONFIG_ESP_SPIRAM
143 spi_flash_guard_set(&g_flash_guard_default_ops);
144 #endif
145
146 #endif /* CONFIG_MCUBOOT */
147
148 esp_intr_initialize();
149 /* Start Zephyr */
150 z_cstart();
151
152 CODE_UNREACHABLE;
153 }
154
155 /* Boot-time static default printk handler, possibly to be overridden later. */
arch_printk_char_out(int c)156 int IRAM_ATTR arch_printk_char_out(int c)
157 {
158 if (c == '\n') {
159 esp_rom_uart_tx_one_char('\r');
160 }
161 esp_rom_uart_tx_one_char(c);
162 return 0;
163 }
164
sys_arch_reboot(int type)165 void sys_arch_reboot(int type)
166 {
167 esp_restart_noos();
168 }
169
esp_restart_noos(void)170 void IRAM_ATTR esp_restart_noos(void)
171 {
172 /* Disable interrupts */
173 z_xt_ints_off(0xFFFFFFFF);
174
175 /*
176 * Reset and stall the other CPU.
177 * CPU must be reset before stalling, in case it was running a s32c1i
178 * instruction. This would cause memory pool to be locked by arbiter
179 * to the stalled CPU, preventing current CPU from accessing this pool.
180 */
181 const uint32_t core_id = cpu_ll_get_core_id();
182
183 /* Flush any data left in UART FIFOs */
184 esp_rom_uart_tx_wait_idle(0);
185 esp_rom_uart_tx_wait_idle(1);
186 /* Disable cache */
187 esp_rom_Cache_Disable_ICache();
188 esp_rom_Cache_Disable_DCache();
189
190 /*
191 * 2nd stage bootloader reconfigures SPI flash signals.
192 * Reset them to the defaults expected by ROM
193 */
194 WRITE_PERI_REG(GPIO_FUNC0_IN_SEL_CFG_REG, 0x30);
195 WRITE_PERI_REG(GPIO_FUNC1_IN_SEL_CFG_REG, 0x30);
196 WRITE_PERI_REG(GPIO_FUNC2_IN_SEL_CFG_REG, 0x30);
197 WRITE_PERI_REG(GPIO_FUNC3_IN_SEL_CFG_REG, 0x30);
198 WRITE_PERI_REG(GPIO_FUNC4_IN_SEL_CFG_REG, 0x30);
199 WRITE_PERI_REG(GPIO_FUNC5_IN_SEL_CFG_REG, 0x30);
200
201 /* Reset wifi/ethernet/sdio (bb/mac) */
202 DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,
203 DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST | DPORT_BT_RST |
204 DPORT_BTMAC_RST | DPORT_SDIO_RST | DPORT_SDIO_RST |
205 DPORT_SDIO_HOST_RST | DPORT_EMAC_RST | DPORT_MACPWR_RST |
206 DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST);
207 DPORT_REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
208
209 /* Reset timer/spi/uart */
210 DPORT_SET_PERI_REG_MASK(
211 DPORT_PERIP_RST_EN_REG,
212 DPORT_TIMERS_RST | DPORT_SPI01_RST | DPORT_SPI2_RST |
213 DPORT_SPI3_RST | DPORT_SPI2_DMA_RST | DPORT_SPI3_DMA_RST |
214 DPORT_UART_RST);
215 DPORT_REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
216
217 /* Reset CPUs */
218 if (core_id == 0) {
219 soc_ll_reset_core(0);
220 }
221
222 while (true) {
223 ;
224 }
225 }
226