1 /*
2  * Copyright (c) 2017 Intel Corporation
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 <drivers/interrupt_controller/intc_esp32.h>
12 #include <xtensa/config/core-isa.h>
13 #include <xtensa/corebits.h>
14 
15 #include <kernel_structs.h>
16 #include <string.h>
17 #include <toolchain/gcc.h>
18 #include <zephyr/types.h>
19 
20 #include "esp_private/system_internal.h"
21 #include "esp32/rom/cache.h"
22 #include "hal/soc_ll.h"
23 #include "soc/cpu.h"
24 #include "soc/gpio_periph.h"
25 #include "esp_spi_flash.h"
26 #include "esp_err.h"
27 #include "esp32/spiram.h"
28 #include "sys/printk.h"
29 
30 extern void z_cstart(void);
31 
32 /*
33  * This is written in C rather than assembly since, during the port bring up,
34  * Zephyr is being booted by the Espressif bootloader.  With it, the C stack
35  * is already set up.
36  */
__start(void)37 void __attribute__((section(".iram1"))) __start(void)
38 {
39 	volatile uint32_t *wdt_rtc_protect = (uint32_t *)RTC_CNTL_WDTWPROTECT_REG;
40 	volatile uint32_t *wdt_rtc_reg = (uint32_t *)RTC_CNTL_WDTCONFIG0_REG;
41 	volatile uint32_t *app_cpu_config_reg = (uint32_t *)DPORT_APPCPU_CTRL_B_REG;
42 	extern uint32_t _init_start;
43 	extern uint32_t _bss_start;
44 	extern uint32_t _bss_end;
45 
46 	/* Move the exception vector table to IRAM. */
47 	__asm__ __volatile__ (
48 		"wsr %0, vecbase"
49 		:
50 		: "r"(&_init_start));
51 
52 	/* Zero out BSS.  Clobber _bss_start to avoid memset() elision. */
53 	(void)memset(&_bss_start, 0,
54 		     (&_bss_end - &_bss_start) * sizeof(_bss_start));
55 	__asm__ __volatile__ (
56 		""
57 		:
58 		: "g"(&_bss_start)
59 		: "memory");
60 
61 #if !CONFIG_BOOTLOADER_ESP_IDF
62 	/* The watchdog timer is enabled in the 1st stage (ROM) bootloader.
63 	 * We're done booting, so disable it.
64 	 * If 2nd stage bootloader from IDF is enabled, then that will take
65 	 * care of this.
66 	 */
67 	volatile uint32_t *wdt_timg_protect = (uint32_t *)TIMG_WDTWPROTECT_REG(0);
68 	volatile uint32_t *wdt_timg_reg = (uint32_t *)TIMG_WDTCONFIG0_REG(0);
69 
70 	*wdt_rtc_protect = RTC_CNTL_WDT_WKEY_VALUE;
71 	*wdt_rtc_reg &= ~RTC_CNTL_WDT_FLASHBOOT_MOD_EN;
72 	*wdt_rtc_protect = 0;
73 	*wdt_timg_protect = TIMG_WDT_WKEY_VALUE;
74 	*wdt_timg_reg &= ~TIMG_WDT_FLASHBOOT_MOD_EN;
75 	*wdt_timg_protect = 0;
76 #endif
77 
78 	/* Disable normal interrupts. */
79 	__asm__ __volatile__ (
80 		"wsr %0, PS"
81 		:
82 		: "r"(PS_INTLEVEL(XCHAL_EXCM_LEVEL) | PS_UM | PS_WOE));
83 
84 	/* Disable CPU1 while we figure out how to have SMP in Zephyr. */
85 	*app_cpu_config_reg &= ~DPORT_APPCPU_CLKGATE_EN;
86 
87 	/* Initialize the architecture CPU pointer.  Some of the
88 	 * initialization code wants a valid _current before
89 	 * arch_kernel_init() is invoked.
90 	 */
91 	__asm__ volatile("wsr.MISC0 %0; rsync" : : "r"(&_kernel.cpus[0]));
92 
93 #if CONFIG_BOOTLOADER_ESP_IDF
94 	/* ESP-IDF 2nd stage bootloader enables RTC WDT to check on startup sequence
95 	 * related issues in application. Hence disable that as we are about to start
96 	 * Zephyr environment.
97 	 */
98 	*wdt_rtc_protect = RTC_CNTL_WDT_WKEY_VALUE;
99 	*wdt_rtc_reg &= ~RTC_CNTL_WDT_EN;
100 	*wdt_rtc_protect = 0;
101 #endif
102 
103 #if CONFIG_ESP_SPIRAM
104 	esp_err_t err = esp_spiram_init();
105 
106 	if (err != ESP_OK) {
107 		printk("Failed to Initialize SPIRAM, aborting.\n");
108 		abort();
109 	}
110 	esp_spiram_init_cache();
111 	if (esp_spiram_get_size() < CONFIG_ESP_SPIRAM_SIZE) {
112 		printk("SPIRAM size is less than configured size, aborting.\n");
113 		abort();
114 	}
115 #endif
116 
117 /* Scheduler is not started at this point. Hence, guard functions
118  * must be initialized after esp_spiram_init_cache which internally
119  * uses guard functions. Setting guard functions before SPIRAM
120  * cache initialization will result in a crash.
121  */
122 #if CONFIG_SOC_FLASH_ESP32 || CONFIG_ESP_SPIRAM
123 	spi_flash_guard_set(&g_flash_guard_default_ops);
124 #endif
125 	esp_intr_initialize();
126 	/* Start Zephyr */
127 	z_cstart();
128 
129 	CODE_UNREACHABLE;
130 }
131 
132 /* Boot-time static default printk handler, possibly to be overridden later. */
arch_printk_char_out(int c)133 int IRAM_ATTR arch_printk_char_out(int c)
134 {
135 	if (c == '\n') {
136 		esp32_rom_uart_tx_one_char('\r');
137 	}
138 	esp32_rom_uart_tx_one_char(c);
139 	return 0;
140 }
141 
sys_arch_reboot(int type)142 void sys_arch_reboot(int type)
143 {
144 	esp_restart_noos();
145 }
146 
esp_restart_noos(void)147 void IRAM_ATTR esp_restart_noos(void)
148 {
149 	/* Disable interrupts */
150 	z_xt_ints_off(0xFFFFFFFF);
151 
152 	const uint32_t core_id = cpu_hal_get_core_id();
153 	const uint32_t other_core_id = (core_id == 0) ? 1 : 0;
154 
155 	soc_ll_reset_core(other_core_id);
156 	soc_ll_stall_core(other_core_id);
157 
158 	/* Flush any data left in UART FIFOs */
159 	esp32_rom_uart_tx_wait_idle(0);
160 	esp32_rom_uart_tx_wait_idle(1);
161 	esp32_rom_uart_tx_wait_idle(2);
162 
163 	/* Disable cache */
164 	Cache_Read_Disable(0);
165 	Cache_Read_Disable(1);
166 
167 	/* 2nd stage bootloader reconfigures SPI flash signals. */
168 	/* Reset them to the defaults expected by ROM */
169 	WRITE_PERI_REG(GPIO_FUNC0_IN_SEL_CFG_REG, 0x30);
170 	WRITE_PERI_REG(GPIO_FUNC1_IN_SEL_CFG_REG, 0x30);
171 	WRITE_PERI_REG(GPIO_FUNC2_IN_SEL_CFG_REG, 0x30);
172 	WRITE_PERI_REG(GPIO_FUNC3_IN_SEL_CFG_REG, 0x30);
173 	WRITE_PERI_REG(GPIO_FUNC4_IN_SEL_CFG_REG, 0x30);
174 	WRITE_PERI_REG(GPIO_FUNC5_IN_SEL_CFG_REG, 0x30);
175 
176 	/* Reset wifi/bluetooth/ethernet/sdio (bb/mac) */
177 	DPORT_SET_PERI_REG_MASK(DPORT_CORE_RST_EN_REG,
178 				DPORT_BB_RST | DPORT_FE_RST | DPORT_MAC_RST |
179 				DPORT_BT_RST | DPORT_BTMAC_RST |
180 				DPORT_SDIO_RST | DPORT_SDIO_HOST_RST |
181 				DPORT_EMAC_RST | DPORT_MACPWR_RST |
182 				DPORT_RW_BTMAC_RST | DPORT_RW_BTLP_RST);
183 	DPORT_REG_WRITE(DPORT_CORE_RST_EN_REG, 0);
184 
185 	/* Reset timer/spi/uart */
186 	DPORT_SET_PERI_REG_MASK(
187 		DPORT_PERIP_RST_EN_REG,
188 		/* UART TX FIFO cannot be reset correctly on ESP32, */
189 		/* so reset the UART memory by DPORT here. */
190 		DPORT_TIMERS_RST | DPORT_SPI01_RST | DPORT_UART_RST |
191 		DPORT_UART1_RST | DPORT_UART2_RST | DPORT_UART_MEM_RST);
192 	DPORT_REG_WRITE(DPORT_PERIP_RST_EN_REG, 0);
193 
194 	/* Clear entry point for APP CPU */
195 	DPORT_REG_WRITE(DPORT_APPCPU_CTRL_D_REG, 0);
196 
197 	/* Reset CPUs */
198 	if (core_id == 0) {
199 		/* Running on PRO CPU: APP CPU is stalled. Can reset both CPUs. */
200 		soc_ll_reset_core(1);
201 		soc_ll_reset_core(0);
202 	} else {
203 		/* Running on APP CPU: need to reset PRO CPU and unstall it, */
204 		/* then reset APP CPU */
205 		soc_ll_reset_core(0);
206 		soc_ll_stall_core(0);
207 		soc_ll_reset_core(1);
208 	}
209 
210 	while (true) {
211 		;
212 	}
213 }
214