bootloader_esp32h2.c 10 KB

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