bootloader_esp32c2.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "esp_efuse.h"
  38. #include "hal/mmu_hal.h"
  39. #include "hal/cache_hal.h"
  40. #include "hal/mmu_ll.h"
  41. static const char *TAG = "boot.esp32c2";
  42. void IRAM_ATTR bootloader_configure_spi_pins(int drv)
  43. {
  44. // IDF-4066
  45. const uint32_t spiconfig = 0;
  46. uint8_t clk_gpio_num = SPI_CLK_GPIO_NUM;
  47. uint8_t q_gpio_num = SPI_Q_GPIO_NUM;
  48. uint8_t d_gpio_num = SPI_D_GPIO_NUM;
  49. uint8_t cs0_gpio_num = SPI_CS0_GPIO_NUM;
  50. uint8_t hd_gpio_num = SPI_HD_GPIO_NUM;
  51. uint8_t wp_gpio_num = SPI_WP_GPIO_NUM;
  52. if (spiconfig == 0) {
  53. }
  54. esp_rom_gpio_pad_set_drv(clk_gpio_num, drv);
  55. esp_rom_gpio_pad_set_drv(q_gpio_num, drv);
  56. esp_rom_gpio_pad_set_drv(d_gpio_num, drv);
  57. esp_rom_gpio_pad_set_drv(cs0_gpio_num, drv);
  58. if (hd_gpio_num <= MAX_PAD_GPIO_NUM) {
  59. esp_rom_gpio_pad_set_drv(hd_gpio_num, drv);
  60. }
  61. if (wp_gpio_num <= MAX_PAD_GPIO_NUM) {
  62. esp_rom_gpio_pad_set_drv(wp_gpio_num, drv);
  63. }
  64. }
  65. static void update_flash_config(const esp_image_header_t *bootloader_hdr)
  66. {
  67. uint32_t size;
  68. switch (bootloader_hdr->spi_size) {
  69. case ESP_IMAGE_FLASH_SIZE_1MB:
  70. size = 1;
  71. break;
  72. case ESP_IMAGE_FLASH_SIZE_2MB:
  73. size = 2;
  74. break;
  75. case ESP_IMAGE_FLASH_SIZE_4MB:
  76. size = 4;
  77. break;
  78. case ESP_IMAGE_FLASH_SIZE_8MB:
  79. size = 8;
  80. break;
  81. case ESP_IMAGE_FLASH_SIZE_16MB:
  82. size = 16;
  83. break;
  84. default:
  85. size = 2;
  86. }
  87. cache_hal_disable(CACHE_TYPE_ALL);
  88. // Set flash chip size
  89. esp_rom_spiflash_config_param(rom_spiflash_legacy_data->chip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff); // TODO: set mode
  90. cache_hal_enable(CACHE_TYPE_ALL);
  91. }
  92. static void print_flash_info(const esp_image_header_t *bootloader_hdr)
  93. {
  94. ESP_LOGD(TAG, "magic %02x", bootloader_hdr->magic);
  95. ESP_LOGD(TAG, "segments %02x", bootloader_hdr->segment_count);
  96. ESP_LOGD(TAG, "spi_mode %02x", bootloader_hdr->spi_mode);
  97. ESP_LOGD(TAG, "spi_speed %02x", bootloader_hdr->spi_speed);
  98. ESP_LOGD(TAG, "spi_size %02x", bootloader_hdr->spi_size);
  99. const char *str;
  100. switch (bootloader_hdr->spi_speed) {
  101. case ESP_IMAGE_SPI_SPEED_DIV_2:
  102. str = "30MHz";
  103. break;
  104. case ESP_IMAGE_SPI_SPEED_DIV_3:
  105. str = "20MHz";
  106. break;
  107. case ESP_IMAGE_SPI_SPEED_DIV_4:
  108. str = "15MHz";
  109. break;
  110. case ESP_IMAGE_SPI_SPEED_DIV_1:
  111. str = "60MHz";
  112. break;
  113. default:
  114. str = "15MHz";
  115. break;
  116. }
  117. ESP_LOGI(TAG, "SPI Speed : %s", str);
  118. /* SPI mode could have been set to QIO during boot already,
  119. so test the SPI registers not the flash header */
  120. uint32_t spi_ctrl = REG_READ(SPI_MEM_CTRL_REG(0));
  121. if (spi_ctrl & SPI_MEM_FREAD_QIO) {
  122. str = "QIO";
  123. } else if (spi_ctrl & SPI_MEM_FREAD_QUAD) {
  124. str = "QOUT";
  125. } else if (spi_ctrl & SPI_MEM_FREAD_DIO) {
  126. str = "DIO";
  127. } else if (spi_ctrl & SPI_MEM_FREAD_DUAL) {
  128. str = "DOUT";
  129. } else if (spi_ctrl & SPI_MEM_FASTRD_MODE) {
  130. str = "FAST READ";
  131. } else {
  132. str = "SLOW READ";
  133. }
  134. ESP_LOGI(TAG, "SPI Mode : %s", str);
  135. switch (bootloader_hdr->spi_size) {
  136. case ESP_IMAGE_FLASH_SIZE_1MB:
  137. str = "1MB";
  138. break;
  139. case ESP_IMAGE_FLASH_SIZE_2MB:
  140. str = "2MB";
  141. break;
  142. case ESP_IMAGE_FLASH_SIZE_4MB:
  143. str = "4MB";
  144. break;
  145. case ESP_IMAGE_FLASH_SIZE_8MB:
  146. str = "8MB";
  147. break;
  148. case ESP_IMAGE_FLASH_SIZE_16MB:
  149. str = "16MB";
  150. break;
  151. default:
  152. str = "2MB";
  153. break;
  154. }
  155. ESP_LOGI(TAG, "SPI Flash Size : %s", str);
  156. }
  157. static void bootloader_print_mmu_page_size(void)
  158. {
  159. mmu_page_size_t page_size = mmu_ll_get_page_size(0);
  160. int size = (page_size == MMU_PAGE_16KB ? 16 :
  161. page_size == MMU_PAGE_32KB ? 32 :
  162. page_size == MMU_PAGE_64KB ? 64 : 0);
  163. ESP_LOGI(TAG, "MMU Page Size : %dK", size);
  164. }
  165. static void IRAM_ATTR bootloader_init_flash_configure(void)
  166. {
  167. bootloader_flash_dummy_config(&bootloader_image_hdr);
  168. bootloader_flash_cs_timing_config();
  169. }
  170. static void bootloader_spi_flash_resume(void)
  171. {
  172. bootloader_execute_flash_command(CMD_RESUME, 0, 0, 0);
  173. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  174. }
  175. static esp_err_t bootloader_init_spi_flash(void)
  176. {
  177. bootloader_init_flash_configure();
  178. bootloader_spi_flash_resume();
  179. bootloader_flash_unlock();
  180. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  181. bootloader_enable_qio_mode();
  182. #endif
  183. bootloader_print_mmu_page_size();
  184. print_flash_info(&bootloader_image_hdr);
  185. update_flash_config(&bootloader_image_hdr);
  186. //ensure the flash is write-protected
  187. bootloader_enable_wp();
  188. return ESP_OK;
  189. }
  190. static void wdt_reset_cpu0_info_enable(void)
  191. {
  192. REG_SET_BIT(SYSTEM_CPU_PERI_CLK_EN_REG, SYSTEM_CLK_EN_ASSIST_DEBUG);
  193. REG_CLR_BIT(SYSTEM_CPU_PERI_RST_EN_REG, SYSTEM_RST_EN_ASSIST_DEBUG);
  194. REG_WRITE(ASSIST_DEBUG_CORE_0_RCD_EN_REG, ASSIST_DEBUG_CORE_0_RCD_PDEBUGEN | ASSIST_DEBUG_CORE_0_RCD_RECORDEN);
  195. }
  196. static void wdt_reset_info_dump(int cpu)
  197. {
  198. (void) cpu;
  199. // saved PC was already printed by the ROM bootloader.
  200. // nothing to do here.
  201. }
  202. static void bootloader_check_wdt_reset(void)
  203. {
  204. int wdt_rst = 0;
  205. soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
  206. if (rst_reason == RESET_REASON_CORE_RTC_WDT || rst_reason == RESET_REASON_CORE_MWDT0 ||
  207. rst_reason == RESET_REASON_CPU0_MWDT0 || rst_reason == RESET_REASON_CPU0_RTC_WDT) {
  208. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  209. wdt_rst = 1;
  210. }
  211. if (wdt_rst) {
  212. // if reset by WDT dump info from trace port
  213. wdt_reset_info_dump(0);
  214. }
  215. wdt_reset_cpu0_info_enable();
  216. }
  217. static void bootloader_super_wdt_auto_feed(void)
  218. {
  219. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY_VALUE);
  220. REG_SET_BIT(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
  221. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, 0);
  222. }
  223. esp_err_t bootloader_init(void)
  224. {
  225. esp_err_t ret = ESP_OK;
  226. bootloader_super_wdt_auto_feed();
  227. // protect memory region
  228. bootloader_init_mem();
  229. /* check that static RAM is after the stack */
  230. assert(&_bss_start <= &_bss_end);
  231. assert(&_data_start <= &_data_end);
  232. // clear bss section
  233. bootloader_clear_bss_section();
  234. // init eFuse virtual mode (read eFuses to RAM)
  235. #ifdef CONFIG_EFUSE_VIRTUAL
  236. 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!");
  237. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  238. esp_efuse_init_virtual_mode_in_ram();
  239. #endif
  240. #endif
  241. //init cache hal
  242. cache_hal_init();
  243. //reset mmu
  244. mmu_hal_init();
  245. // config clock
  246. bootloader_clock_configure();
  247. // initialize console, from now on, we can use esp_log
  248. bootloader_console_init();
  249. /* print 2nd bootloader banner */
  250. bootloader_print_banner();
  251. // update flash ID
  252. bootloader_flash_update_id();
  253. // read bootloader header
  254. if ((ret = bootloader_read_bootloader_header()) != ESP_OK) {
  255. goto err;
  256. }
  257. // read chip revision and check if it's compatible to bootloader
  258. if ((ret = bootloader_check_bootloader_validity()) != ESP_OK) {
  259. goto err;
  260. }
  261. // initialize spi flash
  262. if ((ret = bootloader_init_spi_flash()) != ESP_OK) {
  263. goto err;
  264. }
  265. // check whether a WDT reset happend
  266. bootloader_check_wdt_reset();
  267. // config WDT
  268. bootloader_config_wdt();
  269. // enable RNG early entropy source
  270. bootloader_enable_random();
  271. err:
  272. return ret;
  273. }