bootloader_esp32h2.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "esp32h2/rom/efuse.h"
  28. #include "esp32h2/rom/ets_sys.h"
  29. #include "bootloader_common.h"
  30. #include "bootloader_init.h"
  31. #include "bootloader_clock.h"
  32. #include "bootloader_flash_config.h"
  33. #include "bootloader_mem.h"
  34. #include "bootloader_console.h"
  35. #include "bootloader_flash_priv.h"
  36. #include "bootloader_soc.h"
  37. #include "hal/mmu_hal.h"
  38. #include "hal/cache_hal.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 update_flash_config(const esp_image_header_t *bootloader_hdr)
  71. {
  72. uint32_t size;
  73. switch (bootloader_hdr->spi_size) {
  74. case ESP_IMAGE_FLASH_SIZE_1MB:
  75. size = 1;
  76. break;
  77. case ESP_IMAGE_FLASH_SIZE_2MB:
  78. size = 2;
  79. break;
  80. case ESP_IMAGE_FLASH_SIZE_4MB:
  81. size = 4;
  82. break;
  83. case ESP_IMAGE_FLASH_SIZE_8MB:
  84. size = 8;
  85. break;
  86. case ESP_IMAGE_FLASH_SIZE_16MB:
  87. size = 16;
  88. break;
  89. default:
  90. size = 2;
  91. }
  92. cache_hal_disable(CACHE_TYPE_ALL);
  93. // Set flash chip size
  94. esp_rom_spiflash_config_param(rom_spiflash_legacy_data->chip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff); // TODO: set mode
  95. cache_hal_enable(CACHE_TYPE_ALL);
  96. }
  97. static void print_flash_info(const esp_image_header_t *bootloader_hdr)
  98. {
  99. ESP_LOGD(TAG, "magic %02x", bootloader_hdr->magic);
  100. ESP_LOGD(TAG, "segments %02x", bootloader_hdr->segment_count);
  101. ESP_LOGD(TAG, "spi_mode %02x", bootloader_hdr->spi_mode);
  102. ESP_LOGD(TAG, "spi_speed %02x", bootloader_hdr->spi_speed);
  103. ESP_LOGD(TAG, "spi_size %02x", bootloader_hdr->spi_size);
  104. const char *str;
  105. switch (bootloader_hdr->spi_speed) {
  106. case ESP_IMAGE_SPI_SPEED_DIV_2:
  107. str = "24MHz";
  108. break;
  109. case ESP_IMAGE_SPI_SPEED_DIV_3:
  110. str = "16MHz";
  111. break;
  112. case ESP_IMAGE_SPI_SPEED_DIV_4:
  113. str = "12MHz";
  114. break;
  115. case ESP_IMAGE_SPI_SPEED_DIV_1:
  116. str = "48MHz";
  117. break;
  118. default:
  119. str = "12MHz";
  120. break;
  121. }
  122. ESP_LOGI(TAG, "SPI Speed : %s", str);
  123. /* SPI mode could have been set to QIO during boot already,
  124. so test the SPI registers not the flash header */
  125. uint32_t spi_ctrl = REG_READ(SPI_MEM_CTRL_REG(0));
  126. if (spi_ctrl & SPI_MEM_FREAD_QIO) {
  127. str = "QIO";
  128. } else if (spi_ctrl & SPI_MEM_FREAD_QUAD) {
  129. str = "QOUT";
  130. } else if (spi_ctrl & SPI_MEM_FREAD_DIO) {
  131. str = "DIO";
  132. } else if (spi_ctrl & SPI_MEM_FREAD_DUAL) {
  133. str = "DOUT";
  134. } else if (spi_ctrl & SPI_MEM_FASTRD_MODE) {
  135. str = "FAST READ";
  136. } else {
  137. str = "SLOW READ";
  138. }
  139. ESP_LOGI(TAG, "SPI Mode : %s", str);
  140. switch (bootloader_hdr->spi_size) {
  141. case ESP_IMAGE_FLASH_SIZE_1MB:
  142. str = "1MB";
  143. break;
  144. case ESP_IMAGE_FLASH_SIZE_2MB:
  145. str = "2MB";
  146. break;
  147. case ESP_IMAGE_FLASH_SIZE_4MB:
  148. str = "4MB";
  149. break;
  150. case ESP_IMAGE_FLASH_SIZE_8MB:
  151. str = "8MB";
  152. break;
  153. case ESP_IMAGE_FLASH_SIZE_16MB:
  154. str = "16MB";
  155. break;
  156. default:
  157. str = "2MB";
  158. break;
  159. }
  160. ESP_LOGI(TAG, "SPI Flash Size : %s", str);
  161. }
  162. static void IRAM_ATTR bootloader_init_flash_configure(void)
  163. {
  164. bootloader_flash_dummy_config(&bootloader_image_hdr);
  165. bootloader_flash_cs_timing_config();
  166. }
  167. static void bootloader_spi_flash_resume(void)
  168. {
  169. bootloader_execute_flash_command(CMD_RESUME, 0, 0, 0);
  170. esp_rom_spiflash_wait_idle(&g_rom_flashchip);
  171. }
  172. static esp_err_t bootloader_init_spi_flash(void)
  173. {
  174. bootloader_init_flash_configure();
  175. #ifndef CONFIG_SPI_FLASH_ROM_DRIVER_PATCH
  176. const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info();
  177. if (spiconfig != ESP_ROM_EFUSE_FLASH_DEFAULT_SPI && spiconfig != ESP_ROM_EFUSE_FLASH_DEFAULT_HSPI) {
  178. ESP_LOGE(TAG, "SPI flash pins are overridden. Enable CONFIG_SPI_FLASH_ROM_DRIVER_PATCH in menuconfig");
  179. return ESP_FAIL;
  180. }
  181. #endif
  182. bootloader_spi_flash_resume();
  183. bootloader_flash_unlock();
  184. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  185. bootloader_enable_qio_mode();
  186. #endif
  187. print_flash_info(&bootloader_image_hdr);
  188. update_flash_config(&bootloader_image_hdr);
  189. //ensure the flash is write-protected
  190. bootloader_enable_wp();
  191. return ESP_OK;
  192. }
  193. static void wdt_reset_cpu0_info_enable(void)
  194. {
  195. REG_SET_BIT(SYSTEM_CPU_PERI_CLK_EN_REG, SYSTEM_CLK_EN_ASSIST_DEBUG);
  196. REG_CLR_BIT(SYSTEM_CPU_PERI_RST_EN_REG, SYSTEM_RST_EN_ASSIST_DEBUG);
  197. REG_WRITE(ASSIST_DEBUG_CORE_0_RCD_EN_REG, ASSIST_DEBUG_CORE_0_RCD_PDEBUGEN | ASSIST_DEBUG_CORE_0_RCD_RECORDEN);
  198. }
  199. static void wdt_reset_info_dump(int cpu)
  200. {
  201. (void) cpu;
  202. // saved PC was already printed by the ROM bootloader.
  203. // nothing to do here.
  204. }
  205. static void bootloader_check_wdt_reset(void)
  206. {
  207. int wdt_rst = 0;
  208. soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
  209. if (rst_reason == RESET_REASON_CORE_RTC_WDT || rst_reason == RESET_REASON_CORE_MWDT0 || rst_reason == RESET_REASON_CORE_MWDT1 ||
  210. rst_reason == RESET_REASON_CPU0_MWDT0 || rst_reason == RESET_REASON_CPU0_MWDT1 || rst_reason == RESET_REASON_CPU0_RTC_WDT) {
  211. ESP_LOGW(TAG, "PRO CPU has been reset by WDT.");
  212. wdt_rst = 1;
  213. }
  214. if (wdt_rst) {
  215. // if reset by WDT dump info from trace port
  216. wdt_reset_info_dump(0);
  217. }
  218. wdt_reset_cpu0_info_enable();
  219. }
  220. static void bootloader_super_wdt_auto_feed(void)
  221. {
  222. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, RTC_CNTL_SWD_WKEY_VALUE);
  223. REG_SET_BIT(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);
  224. REG_WRITE(RTC_CNTL_SWD_WPROTECT_REG, 0);
  225. }
  226. static inline void bootloader_hardware_init(void)
  227. {
  228. }
  229. static inline void bootloader_ana_reset_config(void)
  230. {
  231. //Enable WDT, BOR, and GLITCH reset
  232. bootloader_ana_super_wdt_reset_config(true);
  233. bootloader_ana_bod_reset_config(true);
  234. bootloader_ana_clock_glitch_reset_config(true);
  235. }
  236. esp_err_t bootloader_init(void)
  237. {
  238. esp_err_t ret = ESP_OK;
  239. bootloader_hardware_init();
  240. bootloader_ana_reset_config();
  241. bootloader_super_wdt_auto_feed();
  242. // protect memory region
  243. bootloader_init_mem();
  244. /* check that static RAM is after the stack */
  245. assert(&_bss_start <= &_bss_end);
  246. assert(&_data_start <= &_data_end);
  247. // clear bss section
  248. bootloader_clear_bss_section();
  249. //init cache hal
  250. cache_hal_init(); //TODO IDF-4649
  251. //reset mmu
  252. mmu_hal_init();
  253. // config clock
  254. bootloader_clock_configure();
  255. // initialize console, from now on, we can use esp_log
  256. bootloader_console_init();
  257. /* print 2nd bootloader banner */
  258. bootloader_print_banner();
  259. // update flash ID
  260. bootloader_flash_update_id();
  261. // Check and run XMC startup flow
  262. if ((ret = bootloader_flash_xmc_startup()) != ESP_OK) {
  263. ESP_LOGE(TAG, "failed when running XMC startup flow, reboot!");
  264. goto err;
  265. }
  266. // read bootloader header
  267. if ((ret = bootloader_read_bootloader_header()) != ESP_OK) {
  268. goto err;
  269. }
  270. // read chip revision and check if it's compatible to bootloader
  271. if ((ret = bootloader_check_bootloader_validity()) != ESP_OK) {
  272. goto err;
  273. }
  274. // initialize spi flash
  275. if ((ret = bootloader_init_spi_flash()) != ESP_OK) {
  276. goto err;
  277. }
  278. // check whether a WDT reset happend
  279. bootloader_check_wdt_reset();
  280. // config WDT
  281. bootloader_config_wdt();
  282. // enable RNG early entropy source
  283. bootloader_enable_random();
  284. err:
  285. return ret;
  286. }