bootloader_esp32c3.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/rtc_cntl_reg.h"
  24. #include "soc/spi_periph.h"
  25. #include "soc/extmem_reg.h"
  26. #include "soc/io_mux_reg.h"
  27. #include "soc/system_reg.h"
  28. #include "soc/chip_revision.h"
  29. #include "esp32c3/rom/efuse.h"
  30. #include "esp32c3/rom/ets_sys.h"
  31. #include "bootloader_common.h"
  32. #include "bootloader_init.h"
  33. #include "bootloader_clock.h"
  34. #include "bootloader_flash_config.h"
  35. #include "bootloader_mem.h"
  36. #include "esp_private/regi2c_ctrl.h"
  37. #include "soc/regi2c_lp_bias.h"
  38. #include "soc/regi2c_bias.h"
  39. #include "bootloader_console.h"
  40. #include "bootloader_flash_priv.h"
  41. #include "bootloader_soc.h"
  42. #include "esp_efuse.h"
  43. #include "hal/mmu_hal.h"
  44. #include "hal/cache_hal.h"
  45. #include "hal/efuse_hal.h"
  46. static const char *TAG = "boot.esp32c3";
  47. void IRAM_ATTR bootloader_configure_spi_pins(int drv)
  48. {
  49. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  50. uint8_t wp_pin = esp_rom_efuse_get_flash_wp_gpio();
  51. uint8_t clk_gpio_num = SPI_CLK_GPIO_NUM;
  52. uint8_t q_gpio_num = SPI_Q_GPIO_NUM;
  53. uint8_t d_gpio_num = SPI_D_GPIO_NUM;
  54. uint8_t cs0_gpio_num = SPI_CS0_GPIO_NUM;
  55. uint8_t hd_gpio_num = SPI_HD_GPIO_NUM;
  56. uint8_t wp_gpio_num = SPI_WP_GPIO_NUM;
  57. if (spiconfig == 0) {
  58. } else {
  59. clk_gpio_num = spiconfig & 0x3f;
  60. q_gpio_num = (spiconfig >> 6) & 0x3f;
  61. d_gpio_num = (spiconfig >> 12) & 0x3f;
  62. cs0_gpio_num = (spiconfig >> 18) & 0x3f;
  63. hd_gpio_num = (spiconfig >> 24) & 0x3f;
  64. wp_gpio_num = wp_pin;
  65. }
  66. esp_rom_gpio_pad_set_drv(clk_gpio_num, drv);
  67. esp_rom_gpio_pad_set_drv(q_gpio_num, drv);
  68. esp_rom_gpio_pad_set_drv(d_gpio_num, drv);
  69. esp_rom_gpio_pad_set_drv(cs0_gpio_num, drv);
  70. if (hd_gpio_num <= MAX_PAD_GPIO_NUM) {
  71. esp_rom_gpio_pad_set_drv(hd_gpio_num, drv);
  72. }
  73. if (wp_gpio_num <= MAX_PAD_GPIO_NUM) {
  74. esp_rom_gpio_pad_set_drv(wp_gpio_num, drv);
  75. }
  76. }
  77. static void update_flash_config(const esp_image_header_t *bootloader_hdr)
  78. {
  79. uint32_t size;
  80. switch (bootloader_hdr->spi_size) {
  81. case ESP_IMAGE_FLASH_SIZE_1MB:
  82. size = 1;
  83. break;
  84. case ESP_IMAGE_FLASH_SIZE_2MB:
  85. size = 2;
  86. break;
  87. case ESP_IMAGE_FLASH_SIZE_4MB:
  88. size = 4;
  89. break;
  90. case ESP_IMAGE_FLASH_SIZE_8MB:
  91. size = 8;
  92. break;
  93. case ESP_IMAGE_FLASH_SIZE_16MB:
  94. size = 16;
  95. break;
  96. default:
  97. size = 2;
  98. }
  99. cache_hal_disable(CACHE_TYPE_ALL);
  100. // Set flash chip size
  101. esp_rom_spiflash_config_param(rom_spiflash_legacy_data->chip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff); // TODO: set mode
  102. cache_hal_enable(CACHE_TYPE_ALL);
  103. }
  104. static void print_flash_info(const esp_image_header_t *bootloader_hdr)
  105. {
  106. ESP_LOGD(TAG, "magic %02x", bootloader_hdr->magic);
  107. ESP_LOGD(TAG, "segments %02x", bootloader_hdr->segment_count);
  108. ESP_LOGD(TAG, "spi_mode %02x", bootloader_hdr->spi_mode);
  109. ESP_LOGD(TAG, "spi_speed %02x", bootloader_hdr->spi_speed);
  110. ESP_LOGD(TAG, "spi_size %02x", bootloader_hdr->spi_size);
  111. const char *str;
  112. switch (bootloader_hdr->spi_speed) {
  113. case ESP_IMAGE_SPI_SPEED_DIV_2:
  114. str = "40MHz";
  115. break;
  116. case ESP_IMAGE_SPI_SPEED_DIV_3:
  117. str = "26.7MHz";
  118. break;
  119. case ESP_IMAGE_SPI_SPEED_DIV_4:
  120. str = "20MHz";
  121. break;
  122. case ESP_IMAGE_SPI_SPEED_DIV_1:
  123. str = "80MHz";
  124. break;
  125. default:
  126. str = "20MHz";
  127. break;
  128. }
  129. ESP_LOGI(TAG, "SPI Speed : %s", str);
  130. /* SPI mode could have been set to QIO during boot already,
  131. so test the SPI registers not the flash header */
  132. uint32_t spi_ctrl = REG_READ(SPI_MEM_CTRL_REG(0));
  133. if (spi_ctrl & SPI_MEM_FREAD_QIO) {
  134. str = "QIO";
  135. } else if (spi_ctrl & SPI_MEM_FREAD_QUAD) {
  136. str = "QOUT";
  137. } else if (spi_ctrl & SPI_MEM_FREAD_DIO) {
  138. str = "DIO";
  139. } else if (spi_ctrl & SPI_MEM_FREAD_DUAL) {
  140. str = "DOUT";
  141. } else if (spi_ctrl & SPI_MEM_FASTRD_MODE) {
  142. str = "FAST READ";
  143. } else {
  144. str = "SLOW READ";
  145. }
  146. ESP_LOGI(TAG, "SPI Mode : %s", str);
  147. switch (bootloader_hdr->spi_size) {
  148. case ESP_IMAGE_FLASH_SIZE_1MB:
  149. str = "1MB";
  150. break;
  151. case ESP_IMAGE_FLASH_SIZE_2MB:
  152. str = "2MB";
  153. break;
  154. case ESP_IMAGE_FLASH_SIZE_4MB:
  155. str = "4MB";
  156. break;
  157. case ESP_IMAGE_FLASH_SIZE_8MB:
  158. str = "8MB";
  159. break;
  160. case ESP_IMAGE_FLASH_SIZE_16MB:
  161. str = "16MB";
  162. break;
  163. default:
  164. str = "2MB";
  165. break;
  166. }
  167. ESP_LOGI(TAG, "SPI Flash Size : %s", str);
  168. }
  169. static void IRAM_ATTR bootloader_init_flash_configure(void)
  170. {
  171. bootloader_flash_dummy_config(&bootloader_image_hdr);
  172. bootloader_flash_cs_timing_config();
  173. }
  174. static void bootloader_spi_flash_resume(void)
  175. {
  176. bootloader_execute_flash_command(CMD_RESUME, 0, 0, 0);
  177. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  178. }
  179. static esp_err_t bootloader_init_spi_flash(void)
  180. {
  181. bootloader_init_flash_configure();
  182. #ifndef CONFIG_SPI_FLASH_ROM_DRIVER_PATCH
  183. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  184. if (spiconfig != ESP_ROM_EFUSE_FLASH_DEFAULT_SPI && spiconfig != ESP_ROM_EFUSE_FLASH_DEFAULT_HSPI) {
  185. ESP_LOGE(TAG, "SPI flash pins are overridden. Enable CONFIG_SPI_FLASH_ROM_DRIVER_PATCH in menuconfig");
  186. return ESP_FAIL;
  187. }
  188. #endif
  189. bootloader_spi_flash_resume();
  190. bootloader_flash_unlock();
  191. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  192. bootloader_enable_qio_mode();
  193. #endif
  194. print_flash_info(&bootloader_image_hdr);
  195. update_flash_config(&bootloader_image_hdr);
  196. //ensure the flash is write-protected
  197. bootloader_enable_wp();
  198. return ESP_OK;
  199. }
  200. static void wdt_reset_cpu0_info_enable(void)
  201. {
  202. REG_SET_BIT(SYSTEM_CPU_PERI_CLK_EN_REG, SYSTEM_CLK_EN_ASSIST_DEBUG);
  203. REG_CLR_BIT(SYSTEM_CPU_PERI_RST_EN_REG, SYSTEM_RST_EN_ASSIST_DEBUG);
  204. REG_WRITE(ASSIST_DEBUG_CORE_0_RCD_EN_REG, ASSIST_DEBUG_CORE_0_RCD_PDEBUGEN | ASSIST_DEBUG_CORE_0_RCD_RECORDEN);
  205. }
  206. static void wdt_reset_info_dump(int cpu)
  207. {
  208. (void) cpu;
  209. // saved PC was already printed by the ROM bootloader.
  210. // nothing to do here.
  211. }
  212. static void bootloader_check_wdt_reset(void)
  213. {
  214. int wdt_rst = 0;
  215. soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
  216. if (rst_reason == RESET_REASON_CORE_RTC_WDT || rst_reason == RESET_REASON_CORE_MWDT0 || rst_reason == RESET_REASON_CORE_MWDT1 ||
  217. rst_reason == RESET_REASON_CPU0_MWDT0 || rst_reason == RESET_REASON_CPU0_MWDT1 || rst_reason == RESET_REASON_CPU0_RTC_WDT) {
  218. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  219. wdt_rst = 1;
  220. }
  221. if (wdt_rst) {
  222. // if reset by WDT dump info from trace port
  223. wdt_reset_info_dump(0);
  224. }
  225. wdt_reset_cpu0_info_enable();
  226. }
  227. static void bootloader_super_wdt_auto_feed(void)
  228. {
  229. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY_VALUE);
  230. REG_SET_BIT(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
  231. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, 0);
  232. }
  233. static inline void bootloader_hardware_init(void)
  234. {
  235. // This check is always included in the bootloader so it can
  236. // print the minimum revision error message later in the boot
  237. if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 3)) {
  238. REGI2C_WRITE_MASK(I2C_ULP, I2C_ULP_IR_FORCE_XPD_IPH, 1);
  239. REGI2C_WRITE_MASK(I2C_BIAS, I2C_BIAS_DREG_1P1_PVT, 12);
  240. }
  241. }
  242. static inline void bootloader_ana_reset_config(void)
  243. {
  244. /*
  245. For origin chip & ECO1: only support swt reset;
  246. For ECO2: fix brownout reset bug, support swt & brownout reset;
  247. For ECO3: fix clock glitch reset bug, support all reset, include: swt & brownout & clock glitch reset.
  248. */
  249. switch (efuse_hal_chip_revision()) {
  250. case 0:
  251. case 1:
  252. //Enable WDT reset. Disable BOR and GLITCH reset
  253. bootloader_ana_super_wdt_reset_config(true);
  254. bootloader_ana_bod_reset_config(false);
  255. bootloader_ana_clock_glitch_reset_config(false);
  256. break;
  257. case 2:
  258. //Enable WDT and BOR reset. Disable GLITCH reset
  259. bootloader_ana_super_wdt_reset_config(true);
  260. bootloader_ana_bod_reset_config(true);
  261. bootloader_ana_clock_glitch_reset_config(false);
  262. break;
  263. case 3:
  264. default:
  265. //Enable WDT, BOR, and GLITCH reset
  266. bootloader_ana_super_wdt_reset_config(true);
  267. bootloader_ana_bod_reset_config(true);
  268. bootloader_ana_clock_glitch_reset_config(true);
  269. break;
  270. }
  271. }
  272. esp_err_t bootloader_init(void)
  273. {
  274. esp_err_t ret = ESP_OK;
  275. bootloader_hardware_init();
  276. bootloader_ana_reset_config();
  277. bootloader_super_wdt_auto_feed();
  278. // protect memory region
  279. bootloader_init_mem();
  280. /* check that static RAM is after the stack */
  281. assert(&_bss_start <= &_bss_end);
  282. assert(&_data_start <= &_data_end);
  283. // clear bss section
  284. bootloader_clear_bss_section();
  285. // init eFuse virtual mode (read eFuses to RAM)
  286. #ifdef CONFIG_EFUSE_VIRTUAL
  287. 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!");
  288. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  289. esp_efuse_init_virtual_mode_in_ram();
  290. #endif
  291. #endif
  292. //init cache hal
  293. cache_hal_init();
  294. //reset mmu
  295. mmu_hal_init();
  296. // config clock
  297. bootloader_clock_configure();
  298. // initialize console, from now on, we can use esp_log
  299. bootloader_console_init();
  300. /* print 2nd bootloader banner */
  301. bootloader_print_banner();
  302. // update flash ID
  303. bootloader_flash_update_id();
  304. // Check and run XMC startup flow
  305. if ((ret = bootloader_flash_xmc_startup()) != ESP_OK) {
  306. ESP_LOGE(TAG, "failed when running XMC startup flow, reboot!");
  307. goto err;
  308. }
  309. // read bootloader header
  310. if ((ret = bootloader_read_bootloader_header()) != ESP_OK) {
  311. goto err;
  312. }
  313. // read chip revision and check if it's compatible to bootloader
  314. if ((ret = bootloader_check_bootloader_validity()) != ESP_OK) {
  315. goto err;
  316. }
  317. // initialize spi flash
  318. if ((ret = bootloader_init_spi_flash()) != ESP_OK) {
  319. goto err;
  320. }
  321. // check whether a WDT reset happend
  322. bootloader_check_wdt_reset();
  323. // config WDT
  324. bootloader_config_wdt();
  325. // enable RNG early entropy source
  326. bootloader_enable_random();
  327. err:
  328. return ret;
  329. }