cpu_start.c 23 KB

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