1 /*
2  * Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <soc.h>
8 #include <soc/rtc_cntl_reg.h>
9 #include <soc/timer_group_reg.h>
10 #include <zephyr/drivers/interrupt_controller/intc_esp32.h>
11 #include <xtensa/config/core-isa.h>
12 #include <xtensa/corebits.h>
13 
14 #include <zephyr/kernel_structs.h>
15 #include <string.h>
16 #include <zephyr/toolchain/gcc.h>
17 #include <zephyr/types.h>
18 #include <zephyr/linker/linker-defs.h>
19 #include <kernel_internal.h>
20 #include <zephyr/sys/util.h>
21 
22 #include <esp_private/system_internal.h>
23 #include <esp32s3/rom/cache.h>
24 #include <esp32s3/rom/rtc.h>
25 #include <soc/syscon_reg.h>
26 #include <hal/soc_hal.h>
27 #include <hal/wdt_hal.h>
28 #include <hal/cpu_hal.h>
29 #include <soc/gpio_periph.h>
30 #include "esp_cpu.h"
31 #include <esp_err.h>
32 #include <esp_timer.h>
33 #include <esp_app_format.h>
34 #include <esp_clk_internal.h>
35 
36 #define HDR_ATTR __attribute__((section(".entry_addr"))) __attribute__((used))
37 
38 void __appcpu_start(void);
39 static HDR_ATTR void (*_entry_point)(void) = &__appcpu_start;
40 
41 extern void z_prep_c(void);
42 
core_intr_matrix_clear(void)43 static void core_intr_matrix_clear(void)
44 {
45 	uint32_t core_id = esp_cpu_get_core_id();
46 
47 	for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
48 		intr_matrix_set(core_id, i, ETS_INVALID_INUM);
49 	}
50 }
51 
__appcpu_start(void)52 void IRAM_ATTR __appcpu_start(void)
53 {
54 	extern uint32_t _init_start;
55 
56 	/* Move the exception vector table to IRAM. */
57 	__asm__ __volatile__("wsr %0, vecbase" : : "r"(&_init_start));
58 
59 	/* Zero out BSS.  Clobber _bss_start to avoid memset() elision. */
60 	z_bss_zero();
61 
62 	__asm__ __volatile__("" : : "g"(&__bss_start) : "memory");
63 
64 	/* Disable normal interrupts. */
65 	__asm__ __volatile__("wsr %0, PS" : : "r"(PS_INTLEVEL(XCHAL_EXCM_LEVEL) | PS_UM | PS_WOE));
66 
67 	/* Initialize the architecture CPU pointer.  Some of the
68 	 * initialization code wants a valid arch_current_thread() before
69 	 * arch_kernel_init() is invoked.
70 	 */
71 	__asm__ __volatile__("wsr.MISC0 %0; rsync" : : "r"(&_kernel.cpus[1]));
72 
73 	core_intr_matrix_clear();
74 
75 	esp_intr_initialize();
76 
77 	/* Start Zephyr */
78 	z_prep_c();
79 
80 	CODE_UNREACHABLE;
81 }
82 
83 /* Boot-time static default printk handler, possibly to be overridden later. */
arch_printk_char_out(int c)84 int IRAM_ATTR arch_printk_char_out(int c)
85 {
86 	ARG_UNUSED(c);
87 	return 0;
88 }
89 
sys_arch_reboot(int type)90 void sys_arch_reboot(int type)
91 {
92 	esp_restart_noos();
93 }
94