cpu_start.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include "esp_attr.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #include "esp_system.h"
  13. #include "esp_efuse.h"
  14. #include "cache_err_int.h"
  15. #include "esp_clk_internal.h"
  16. #include "esp_rom_efuse.h"
  17. #include "esp_rom_uart.h"
  18. #include "esp_rom_sys.h"
  19. #include "sdkconfig.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #include "soc/dport_reg.h"
  22. #include "esp32/rtc.h"
  23. #include "esp32/rom/cache.h"
  24. #include "esp32/spiram.h"
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #include "esp32s2/rtc.h"
  27. #include "esp32s2/rom/cache.h"
  28. #include "esp32s2/spiram.h"
  29. #include "esp32s2/dport_access.h"
  30. #include "esp32s2/memprot.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S3
  32. #include "esp32s3/rtc.h"
  33. #include "esp32s3/rom/cache.h"
  34. #include "esp32s3/spiram.h"
  35. #include "esp32s3/dport_access.h"
  36. #include "esp32s3/memprot.h"
  37. #include "soc/assist_debug_reg.h"
  38. #include "soc/cache_memory.h"
  39. #include "soc/system_reg.h"
  40. #include "esp32s3/rom/opi_flash.h"
  41. #elif CONFIG_IDF_TARGET_ESP32C3
  42. #include "esp32c3/rtc.h"
  43. #include "esp32c3/rom/cache.h"
  44. #include "soc/cache_memory.h"
  45. #include "esp32c3/memprot.h"
  46. #elif CONFIG_IDF_TARGET_ESP32H2
  47. #include "esp32h2/rtc.h"
  48. #include "esp32h2/rom/cache.h"
  49. #include "soc/cache_memory.h"
  50. #include "esp32h2/memprot.h"
  51. #endif
  52. #include "esp_private/spi_flash_os.h"
  53. #include "bootloader_flash_config.h"
  54. #include "bootloader_flash.h"
  55. #include "esp_private/crosscore_int.h"
  56. #include "esp_flash_encrypt.h"
  57. #include "hal/rtc_io_hal.h"
  58. #include "hal/gpio_hal.h"
  59. #include "hal/wdt_hal.h"
  60. #include "soc/rtc.h"
  61. #include "soc/efuse_reg.h"
  62. #include "soc/periph_defs.h"
  63. #include "soc/cpu.h"
  64. #include "soc/rtc.h"
  65. #include "soc/spinlock.h"
  66. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  67. #include "trax.h"
  68. #endif
  69. #include "bootloader_mem.h"
  70. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  71. #if CONFIG_IDF_TARGET_ESP32
  72. #include "esp32/rom/spi_flash.h"
  73. #elif CONFIG_IDF_TARGET_ESP32S2
  74. #include "esp32s2/rom/spi_flash.h"
  75. #elif CONFIG_IDF_TARGET_ESP32S3
  76. #include "esp32s3/rom/spi_flash.h"
  77. #elif CONFIG_IDF_TARGET_ESP32C3
  78. #include "esp32c3/rom/spi_flash.h"
  79. #elif CONFIG_IDF_TARGET_ESP32H2
  80. #include "esp32h2/rom/spi_flash.h"
  81. #endif
  82. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  83. // Set efuse ROM_LOG_MODE on first boot
  84. //
  85. // For CONFIG_BOOT_ROM_LOG_ALWAYS_ON (default) or undefined (ESP32), leave
  86. // ROM_LOG_MODE undefined (no need to call this function during startup)
  87. #if CONFIG_BOOT_ROM_LOG_ALWAYS_OFF
  88. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ALWAYS_OFF
  89. #elif CONFIG_BOOT_ROM_LOG_ON_GPIO_LOW
  90. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_LOW
  91. #elif CONFIG_BOOT_ROM_LOG_ON_GPIO_HIGH
  92. #define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH
  93. #endif
  94. #include "esp_private/startup_internal.h"
  95. #include "esp_private/system_internal.h"
  96. extern int _bss_start;
  97. extern int _bss_end;
  98. extern int _rtc_bss_start;
  99. extern int _rtc_bss_end;
  100. extern int _vector_table;
  101. static const char *TAG = "cpu_start";
  102. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  103. extern int _ext_ram_bss_start;
  104. extern int _ext_ram_bss_end;
  105. #endif
  106. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  107. extern int _iram_bss_start;
  108. extern int _iram_bss_end;
  109. #endif
  110. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  111. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  112. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  113. static volatile bool s_resume_cores;
  114. #endif
  115. // If CONFIG_SPIRAM_IGNORE_NOTFOUND is set and external RAM is not found or errors out on testing, this is set to false.
  116. bool g_spiram_ok = true;
  117. static void core_intr_matrix_clear(void)
  118. {
  119. uint32_t core_id = cpu_hal_get_core_id();
  120. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  121. intr_matrix_set(core_id, i, ETS_INVALID_INUM);
  122. }
  123. }
  124. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  125. void startup_resume_other_cores(void)
  126. {
  127. s_resume_cores = true;
  128. }
  129. void IRAM_ATTR call_start_cpu1(void)
  130. {
  131. cpu_hal_set_vecbase(&_vector_table);
  132. ets_set_appcpu_boot_addr(0);
  133. bootloader_init_mem();
  134. #if CONFIG_ESP_CONSOLE_UART_NONE
  135. esp_rom_install_channel_putc(1, NULL);
  136. esp_rom_install_channel_putc(2, NULL);
  137. #else // CONFIG_ESP_CONSOLE_UART_NONE
  138. esp_rom_install_uart_printf();
  139. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  140. #endif
  141. #if CONFIG_IDF_TARGET_ESP32
  142. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  143. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  144. #else
  145. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  146. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  147. #endif
  148. s_cpu_up[1] = true;
  149. ESP_EARLY_LOGI(TAG, "App cpu up.");
  150. // Clear interrupt matrix for APP CPU core
  151. core_intr_matrix_clear();
  152. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  153. //has started, but it isn't active *on this CPU* yet.
  154. esp_cache_err_int_init();
  155. #if CONFIG_IDF_TARGET_ESP32
  156. #if CONFIG_ESP32_TRAX_TWOBANKS
  157. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  158. #endif
  159. #endif
  160. s_cpu_inited[1] = true;
  161. while (!s_resume_cores) {
  162. esp_rom_delay_us(100);
  163. }
  164. SYS_STARTUP_FN();
  165. }
  166. static void start_other_core(void)
  167. {
  168. esp_chip_info_t chip_info;
  169. esp_chip_info(&chip_info);
  170. // If not the single core variant of a target - check this since there is
  171. // no separate soc_caps.h for the single core variant.
  172. if (!(chip_info.cores > 1)) {
  173. ESP_EARLY_LOGE(TAG, "Running on single core variant of a chip, but app is built with multi-core support.");
  174. ESP_EARLY_LOGE(TAG, "Check that CONFIG_FREERTOS_UNICORE is enabled in menuconfig");
  175. abort();
  176. }
  177. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  178. #if CONFIG_IDF_TARGET_ESP32
  179. Cache_Flush(1);
  180. Cache_Read_Enable(1);
  181. #endif
  182. esp_cpu_unstall(1);
  183. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  184. // enabled clock and taken APP CPU out of reset. In this case don't reset
  185. // APP CPU again, as that will clear the breakpoints which may have already
  186. // been set.
  187. #if CONFIG_IDF_TARGET_ESP32
  188. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  189. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  190. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  191. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  192. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  193. }
  194. #elif CONFIG_IDF_TARGET_ESP32S3
  195. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  196. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  197. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  198. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  199. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  200. }
  201. #endif
  202. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  203. bool cpus_up = false;
  204. while (!cpus_up) {
  205. cpus_up = true;
  206. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  207. cpus_up &= s_cpu_up[i];
  208. }
  209. esp_rom_delay_us(100);
  210. }
  211. }
  212. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  213. /*
  214. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  215. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  216. */
  217. void IRAM_ATTR call_start_cpu0(void)
  218. {
  219. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  220. soc_reset_reason_t rst_reas[SOC_CPU_CORES_NUM];
  221. #else
  222. soc_reset_reason_t rst_reas[1];
  223. #endif
  224. #ifdef __riscv
  225. if (cpu_hal_is_debugger_attached()) {
  226. /* Let debugger some time to detect that target started, halt it, enable ebreaks and resume.
  227. 500ms should be enough. */
  228. for (uint32_t ms_num = 0; ms_num < 2; ms_num++) {
  229. esp_rom_delay_us(100000);
  230. }
  231. }
  232. // Configure the global pointer register
  233. // (This should be the first thing IDF app does, as any other piece of code could be
  234. // relaxed by the linker to access something relative to __global_pointer$)
  235. __asm__ __volatile__ (
  236. ".option push\n"
  237. ".option norelax\n"
  238. "la gp, __global_pointer$\n"
  239. ".option pop"
  240. );
  241. #endif
  242. // Move exception vectors to IRAM
  243. cpu_hal_set_vecbase(&_vector_table);
  244. rst_reas[0] = esp_rom_get_reset_reason(0);
  245. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  246. rst_reas[1] = esp_rom_get_reset_reason(1);
  247. #endif
  248. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  249. // from panic handler we can be reset by RWDT or TG0WDT
  250. if (rst_reas[0] == RESET_REASON_CORE_RTC_WDT || rst_reas[0] == RESET_REASON_CORE_MWDT0
  251. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  252. || rst_reas[1] == RESET_REASON_CORE_RTC_WDT || rst_reas[1] == RESET_REASON_CORE_MWDT0
  253. #endif
  254. ) {
  255. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  256. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  257. wdt_hal_disable(&rtc_wdt_ctx);
  258. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  259. }
  260. #endif
  261. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  262. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  263. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  264. // Clear IRAM BSS
  265. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  266. #endif
  267. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  268. if (rst_reas[0] != RESET_REASON_CORE_DEEP_SLEEP) {
  269. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  270. }
  271. #if CONFIG_IDF_TARGET_ESP32S2
  272. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  273. extern void esp_config_instruction_cache_mode(void);
  274. esp_config_instruction_cache_mode();
  275. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  276. Configure the mode of data : cache size, cache associated ways, cache line size.
  277. Enable data cache, so if we don't use SPIRAM, it just works. */
  278. #if CONFIG_SPIRAM_BOOT_INIT
  279. extern void esp_config_data_cache_mode(void);
  280. esp_config_data_cache_mode();
  281. Cache_Enable_DCache(0);
  282. #endif
  283. #endif
  284. #if CONFIG_IDF_TARGET_ESP32S3
  285. /* Configure the mode of instruction cache : cache size, cache line size. */
  286. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  287. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  288. /* If we need use SPIRAM, we should use data cache.
  289. Configure the mode of data : cache size, cache line size.*/
  290. Cache_Suspend_DCache();
  291. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  292. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  293. Cache_Resume_DCache(0);
  294. #endif // CONFIG_IDF_TARGET_ESP32S3
  295. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  296. /* Configure the Cache MMU size for instruction and rodata in flash. */
  297. extern uint32_t Cache_Set_IDROM_MMU_Size(uint32_t irom_size, uint32_t drom_size);
  298. extern int _rodata_reserved_start;
  299. uint32_t rodata_reserved_start_align = (uint32_t)&_rodata_reserved_start & ~(MMU_PAGE_SIZE - 1);
  300. uint32_t cache_mmu_irom_size = ((rodata_reserved_start_align - SOC_DROM_LOW) / MMU_PAGE_SIZE) * sizeof(uint32_t);
  301. #if CONFIG_IDF_TARGET_ESP32S3
  302. extern int _rodata_reserved_end;
  303. uint32_t cache_mmu_drom_size = (((uint32_t)&_rodata_reserved_end - rodata_reserved_start_align + MMU_PAGE_SIZE - 1)/MMU_PAGE_SIZE)*sizeof(uint32_t);
  304. #endif
  305. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  306. #endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  307. #if CONFIG_ESPTOOLPY_OCT_FLASH
  308. bool efuse_opflash_en = REG_GET_FIELD(EFUSE_RD_REPEAT_DATA3_REG, EFUSE_FLASH_TYPE);
  309. if (!efuse_opflash_en) {
  310. ESP_EARLY_LOGE(TAG, "Octal Flash option selected, but EFUSE not configured!");
  311. abort();
  312. }
  313. #endif
  314. esp_mspi_pin_init();
  315. // For Octal flash, it's hard to implement a read_id function in OPI mode for all vendors.
  316. // So we have to read it here in SPI mode, before entering the OPI mode.
  317. bootloader_flash_update_id();
  318. /**
  319. * This function initialise the Flash chip to the user-defined settings.
  320. *
  321. * In bootloader, we only init Flash (and MSPI) to a preliminary state, for being flexible to
  322. * different chips.
  323. * In this stage, we re-configure the Flash (and MSPI) to required configuration
  324. */
  325. spi_flash_init_chip_state();
  326. #if CONFIG_IDF_TARGET_ESP32S3
  327. //On other chips, this feature is not provided by HW, or hasn't been tested yet.
  328. spi_timing_flash_tuning();
  329. #endif
  330. bootloader_init_mem();
  331. #if CONFIG_SPIRAM_BOOT_INIT
  332. if (esp_spiram_init() != ESP_OK) {
  333. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  334. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  335. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  336. abort();
  337. #endif
  338. #endif
  339. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  340. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  341. g_spiram_ok = false;
  342. #else
  343. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  344. abort();
  345. #endif
  346. }
  347. if (g_spiram_ok) {
  348. esp_spiram_init_cache();
  349. }
  350. #endif
  351. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  352. s_cpu_up[0] = true;
  353. #endif
  354. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  355. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  356. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  357. start_other_core();
  358. #else
  359. ESP_EARLY_LOGI(TAG, "Single core mode");
  360. #if CONFIG_IDF_TARGET_ESP32
  361. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  362. #elif CONFIG_IDF_TARGET_ESP32S3
  363. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  364. #if SOC_APPCPU_HAS_CLOCK_GATING_BUG
  365. /* The clock gating signal of the App core is invalid. We use RUNSTALL and RESETING
  366. signals to ensure that the App core stops running in single-core mode. */
  367. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  368. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  369. #endif
  370. #endif // CONFIG_IDF_TARGET_ESP32
  371. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  372. #endif // SOC_CPU_CORES_NUM > 1
  373. #if CONFIG_SPIRAM_MEMTEST
  374. if (g_spiram_ok) {
  375. bool ext_ram_ok = esp_spiram_test();
  376. if (!ext_ram_ok) {
  377. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  378. abort();
  379. }
  380. }
  381. #endif
  382. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  383. extern void instruction_flash_page_info_init(void);
  384. instruction_flash_page_info_init();
  385. #endif
  386. #if CONFIG_SPIRAM_RODATA
  387. extern void rodata_flash_page_info_init(void);
  388. rodata_flash_page_info_init();
  389. #endif
  390. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  391. extern void esp_spiram_enable_instruction_access(void);
  392. esp_spiram_enable_instruction_access();
  393. #endif
  394. #if CONFIG_SPIRAM_RODATA
  395. extern void esp_spiram_enable_rodata_access(void);
  396. esp_spiram_enable_rodata_access();
  397. #endif
  398. #if CONFIG_IDF_TARGET_ESP32S3
  399. int s_instr_flash2spiram_off = 0;
  400. int s_rodata_flash2spiram_off = 0;
  401. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  402. s_instr_flash2spiram_off = instruction_flash2spiram_offset();
  403. #endif
  404. #if CONFIG_SPIRAM_RODATA
  405. s_rodata_flash2spiram_off = rodata_flash2spiram_offset();
  406. #endif
  407. extern void Cache_Set_IDROM_MMU_Info(uint32_t instr_page_num, uint32_t rodata_page_num, uint32_t rodata_start, uint32_t rodata_end, int i_off, int ro_off);
  408. Cache_Set_IDROM_MMU_Info(cache_mmu_irom_size/sizeof(uint32_t), \
  409. cache_mmu_drom_size/sizeof(uint32_t), \
  410. (uint32_t)&_rodata_reserved_start, \
  411. (uint32_t)&_rodata_reserved_end, \
  412. s_instr_flash2spiram_off, \
  413. s_rodata_flash2spiram_off);
  414. #endif
  415. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP || \
  416. CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  417. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  418. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP
  419. icache_wrap_enable = 1;
  420. #endif
  421. #if CONFIG_ESP32S2_DATA_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  422. dcache_wrap_enable = 1;
  423. #endif
  424. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  425. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  426. #endif
  427. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  428. Cache_Invalidate_DCache_All();
  429. Cache_Occupy_Addr(SOC_DROM_LOW, 0x4000);
  430. #endif
  431. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  432. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  433. #endif
  434. //Enable trace memory and immediately start trace.
  435. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  436. #if CONFIG_IDF_TARGET_ESP32
  437. #if CONFIG_ESP32_TRAX_TWOBANKS
  438. trax_enable(TRAX_ENA_PRO_APP);
  439. #else
  440. trax_enable(TRAX_ENA_PRO);
  441. #endif
  442. #elif CONFIG_IDF_TARGET_ESP32S2
  443. trax_enable(TRAX_ENA_PRO);
  444. #endif
  445. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  446. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX
  447. esp_clk_init();
  448. esp_perip_clk_init();
  449. // Now that the clocks have been set-up, set the startup time from RTC
  450. // and default RTC-backed system time provider.
  451. g_startup_time = esp_rtc_get_time_us();
  452. // Clear interrupt matrix for PRO CPU core
  453. core_intr_matrix_clear();
  454. #ifndef CONFIG_IDF_ENV_FPGA // TODO: on FPGA it should be possible to configure this, not currently working with APB_CLK_FREQ changed
  455. #ifdef CONFIG_ESP_CONSOLE_UART
  456. uint32_t clock_hz = rtc_clk_apb_freq_get();
  457. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  458. clock_hz = UART_CLK_FREQ_ROM; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  459. #endif
  460. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  461. esp_rom_uart_set_clock_baudrate(CONFIG_ESP_CONSOLE_UART_NUM, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  462. #endif
  463. #endif
  464. #if SOC_RTCIO_HOLD_SUPPORTED
  465. rtcio_hal_unhold_all();
  466. #else
  467. gpio_hal_force_unhold_all();
  468. #endif
  469. esp_cache_err_int_init();
  470. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE
  471. // Memprot cannot be locked during OS startup as the lock-on prevents any PMS changes until a next reboot
  472. // If such a situation appears, it is likely an malicious attempt to bypass the system safety setup -> print error & reset
  473. if (esp_memprot_is_locked_any()) {
  474. ESP_EARLY_LOGE(TAG, "Memprot feature locked after the system reset! Potential safety corruption, rebooting.");
  475. esp_restart_noos_dig();
  476. }
  477. esp_err_t memp_err = ESP_OK;
  478. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  479. #if CONFIG_IDF_TARGET_ESP32S2 //specific for ESP32S2 unless IDF-3024 is merged
  480. memp_err = esp_memprot_set_prot(true, true, NULL);
  481. #else
  482. esp_memprot_set_prot(true, true, NULL);
  483. #endif
  484. #else
  485. #if CONFIG_IDF_TARGET_ESP32S2 //specific for ESP32S2 unless IDF-3024 is merged
  486. memp_err = esp_memprot_set_prot(true, false, NULL);
  487. #else
  488. esp_memprot_set_prot(true, false, NULL);
  489. #endif
  490. #endif
  491. if (memp_err != ESP_OK) {
  492. ESP_EARLY_LOGE(TAG, "Failed to set Memprot feature (error 0x%08X), rebooting.", memp_err);
  493. esp_restart_noos_dig();
  494. }
  495. #endif
  496. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  497. __attribute__((unused)) esp_image_header_t fhdr = {0};
  498. #ifdef CONFIG_APP_BUILD_TYPE_ELF_RAM
  499. fhdr.spi_mode = ESP_IMAGE_SPI_MODE_DIO;
  500. fhdr.spi_speed = ESP_IMAGE_SPI_SPEED_40M;
  501. fhdr.spi_size = ESP_IMAGE_FLASH_SIZE_4MB;
  502. extern void esp_rom_spiflash_attach(uint32_t, bool);
  503. esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false);
  504. bootloader_flash_unlock();
  505. #else
  506. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  507. // the binary header through cache by accessing SOC_DROM_LOW address.
  508. memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  509. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  510. #if CONFIG_IDF_TARGET_ESP32
  511. #if !CONFIG_SPIRAM_BOOT_INIT
  512. // If psram is uninitialized, we need to improve some flash configuration.
  513. bootloader_flash_clock_config(&fhdr);
  514. bootloader_flash_gpio_config(&fhdr);
  515. bootloader_flash_dummy_config(&fhdr);
  516. bootloader_flash_cs_timing_config();
  517. #endif //!CONFIG_SPIRAM_BOOT_INIT
  518. #endif //CONFIG_IDF_TARGET_ESP32
  519. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  520. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  521. if (app_flash_size < 1 * 1024 * 1024) {
  522. ESP_LOGE(TAG, "Invalid flash size in app image header.");
  523. abort();
  524. }
  525. bootloader_flash_update_size(app_flash_size);
  526. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  527. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  528. s_cpu_inited[0] = true;
  529. volatile bool cpus_inited = false;
  530. while (!cpus_inited) {
  531. cpus_inited = true;
  532. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  533. cpus_inited &= s_cpu_inited[i];
  534. }
  535. esp_rom_delay_us(100);
  536. }
  537. #endif
  538. #ifdef ROM_LOG_MODE
  539. esp_efuse_set_rom_log_scheme(ROM_LOG_MODE);
  540. #endif
  541. SYS_STARTUP_FN();
  542. }