bootloader_esp32c2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include "sdkconfig.h"
  8. #include "esp_attr.h"
  9. #include "esp_log.h"
  10. #include "esp_image_format.h"
  11. #include "flash_qio_mode.h"
  12. #include "esp_rom_gpio.h"
  13. #include "esp_rom_efuse.h"
  14. #include "esp_rom_uart.h"
  15. #include "esp_rom_sys.h"
  16. #include "esp_rom_spiflash.h"
  17. #include "soc/efuse_reg.h"
  18. #include "soc/gpio_sig_map.h"
  19. #include "soc/io_mux_reg.h"
  20. #include "soc/assist_debug_reg.h"
  21. #include "esp_cpu.h"
  22. #include "soc/rtc.h"
  23. #include "soc/spi_periph.h"
  24. #include "soc/extmem_reg.h"
  25. #include "soc/io_mux_reg.h"
  26. #include "soc/system_reg.h"
  27. #include "esp32c2/rom/efuse.h"
  28. #include "esp32c2/rom/ets_sys.h"
  29. #include "esp32c2/rom/rtc.h"
  30. #include "bootloader_common.h"
  31. #include "bootloader_init.h"
  32. #include "bootloader_clock.h"
  33. #include "bootloader_flash_config.h"
  34. #include "bootloader_mem.h"
  35. #include "bootloader_console.h"
  36. #include "bootloader_flash_priv.h"
  37. #include "bootloader_soc.h"
  38. #include "esp_private/bootloader_flash_internal.h"
  39. #include "esp_efuse.h"
  40. #include "hal/mmu_hal.h"
  41. #include "hal/cache_hal.h"
  42. #include "hal/rwdt_ll.h"
  43. static const char *TAG = "boot.esp32c2";
  44. static void wdt_reset_cpu0_info_enable(void)
  45. {
  46. REG_SET_BIT(SYSTEM_CPU_PERI_CLK_EN_REG, SYSTEM_CLK_EN_ASSIST_DEBUG);
  47. REG_CLR_BIT(SYSTEM_CPU_PERI_RST_EN_REG, SYSTEM_RST_EN_ASSIST_DEBUG);
  48. REG_WRITE(ASSIST_DEBUG_CORE_0_RCD_EN_REG, ASSIST_DEBUG_CORE_0_RCD_PDEBUGEN | ASSIST_DEBUG_CORE_0_RCD_RECORDEN);
  49. }
  50. static void wdt_reset_info_dump(int cpu)
  51. {
  52. (void) cpu;
  53. // saved PC was already printed by the ROM bootloader.
  54. // nothing to do here.
  55. }
  56. static void bootloader_check_wdt_reset(void)
  57. {
  58. int wdt_rst = 0;
  59. soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
  60. if (rst_reason == RESET_REASON_CORE_RTC_WDT || rst_reason == RESET_REASON_CORE_MWDT0 ||
  61. rst_reason == RESET_REASON_CPU0_MWDT0 || rst_reason == RESET_REASON_CPU0_RTC_WDT) {
  62. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  63. wdt_rst = 1;
  64. }
  65. if (wdt_rst) {
  66. // if reset by WDT dump info from trace port
  67. wdt_reset_info_dump(0);
  68. }
  69. wdt_reset_cpu0_info_enable();
  70. }
  71. static void bootloader_super_wdt_auto_feed(void)
  72. {
  73. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY_VALUE);
  74. REG_SET_BIT(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
  75. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, 0);
  76. }
  77. static inline void bootloader_ana_reset_config(void)
  78. {
  79. //Enable super WDT reset.
  80. bootloader_ana_super_wdt_reset_config(true);
  81. //Enable BOD reset
  82. bootloader_ana_bod_reset_config(true);
  83. }
  84. esp_err_t bootloader_init(void)
  85. {
  86. esp_err_t ret = ESP_OK;
  87. bootloader_ana_reset_config();
  88. bootloader_super_wdt_auto_feed();
  89. // In RAM_APP, memory will be initialized in `call_start_cpu0`
  90. #if !CONFIG_APP_BUILD_TYPE_RAM
  91. // protect memory region
  92. bootloader_init_mem();
  93. /* check that static RAM is after the stack */
  94. assert(&_bss_start <= &_bss_end);
  95. assert(&_data_start <= &_data_end);
  96. // clear bss section
  97. bootloader_clear_bss_section();
  98. #endif // !CONFIG_APP_BUILD_TYPE_RAM
  99. // init eFuse virtual mode (read eFuses to RAM)
  100. #ifdef CONFIG_EFUSE_VIRTUAL
  101. ESP_LOGW(TAG, "eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY!");
  102. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  103. esp_efuse_init_virtual_mode_in_ram();
  104. #endif
  105. #endif
  106. // config clock
  107. bootloader_clock_configure();
  108. // initialize console, from now on, we can use esp_log
  109. bootloader_console_init();
  110. /* print 2nd bootloader banner */
  111. bootloader_print_banner();
  112. #if !CONFIG_APP_BUILD_TYPE_RAM
  113. //init cache hal
  114. cache_hal_init();
  115. //init mmu
  116. mmu_hal_init();
  117. // update flash ID
  118. bootloader_flash_update_id();
  119. // read bootloader header
  120. if ((ret = bootloader_read_bootloader_header()) != ESP_OK) {
  121. return ret;
  122. }
  123. // read chip revision and check if it's compatible to bootloader
  124. if ((ret = bootloader_check_bootloader_validity()) != ESP_OK) {
  125. return ret;
  126. }
  127. // initialize spi flash
  128. if ((ret = bootloader_init_spi_flash()) != ESP_OK) {
  129. return ret;
  130. }
  131. #endif // !CONFIG_APP_BUILD_TYPE_RAM
  132. // check whether a WDT reset happend
  133. bootloader_check_wdt_reset();
  134. // config WDT
  135. bootloader_config_wdt();
  136. // enable RNG early entropy source
  137. bootloader_enable_random();
  138. return ret;
  139. }