cpu_start.c 22 KB

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