1 /*
2  * Copyright (c) 2023 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/timer_group_reg.h>
9 #include <soc/ext_mem_defs.h>
10 #include <soc/gpio_reg.h>
11 #include <soc/system_reg.h>
12 #include "hal/wdt_hal.h"
13 #include "esp_cpu.h"
14 #include "hal/soc_hal.h"
15 #include "hal/cpu_hal.h"
16 #include "esp_timer.h"
17 #include "esp_private/system_internal.h"
18 #include "esp_clk_internal.h"
19 #include <soc/interrupt_reg.h>
20 #include <esp_private/spi_flash_os.h>
21 #include "esp_private/esp_mmu_map_private.h"
22 #include <esp_flash_internal.h>
23 
24 #include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
25 
26 #include <zephyr/kernel_structs.h>
27 #include <kernel_internal.h>
28 #include <string.h>
29 #include <zephyr/toolchain/gcc.h>
30 #include <soc.h>
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  */
__esp_platform_start(void)37 void IRAM_ATTR __esp_platform_start(void)
38 {
39 	__asm__ __volatile__("la t0, _esp32c6_vector_table\n"
40 						"csrw mtvec, t0\n");
41 
42 	z_bss_zero();
43 
44 	/* Disable normal interrupts. */
45 	csr_read_clear(mstatus, MSTATUS_MIE);
46 
47 	esp_reset_reason_init();
48 
49 #ifndef CONFIG_MCUBOOT
50 	/* ESP-IDF 2nd stage bootloader enables RTC WDT to check on startup sequence
51 	 * related issues in application. Hence disable that as we are about to start
52 	 * Zephyr environment.
53 	 */
54 	wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &LP_WDT};
55 
56 	wdt_hal_write_protect_disable(&rtc_wdt_ctx);
57 	wdt_hal_disable(&rtc_wdt_ctx);
58 	wdt_hal_write_protect_enable(&rtc_wdt_ctx);
59 
60 	esp_timer_early_init();
61 
62 	esp_mspi_pin_init();
63 
64 	esp_flash_app_init();
65 
66 	esp_mmu_map_init();
67 
68 #endif /* !CONFIG_MCUBOOT */
69 
70 	/*Initialize the esp32c6 interrupt controller */
71 	esp_intr_initialize();
72 
73 	/* Start Zephyr */
74 	z_cstart();
75 
76 	CODE_UNREACHABLE;
77 }
78 
79 /* Boot-time static default printk handler, possibly to be overridden later. */
arch_printk_char_out(int c)80 int IRAM_ATTR arch_printk_char_out(int c)
81 {
82 	if (c == '\n') {
83 		esp_rom_uart_tx_one_char('\r');
84 	}
85 	esp_rom_uart_tx_one_char(c);
86 	return 0;
87 }
88 
sys_arch_reboot(int type)89 void sys_arch_reboot(int type)
90 {
91 	esp_restart_noos();
92 }
93