bootloader_esp8684.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "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 "esp8684/rom/efuse.h"
  28. #include "esp8684/rom/cache.h"
  29. #include "esp8684/rom/ets_sys.h"
  30. #include "esp8684/rom/rtc.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. #include "esp_efuse.h"
  40. static const char *TAG = "boot.esp8684";
  41. void IRAM_ATTR bootloader_configure_spi_pins(int drv)
  42. {
  43. // IDF-4066
  44. const uint32_t spiconfig = 0;
  45. uint8_t clk_gpio_num = SPI_CLK_GPIO_NUM;
  46. uint8_t q_gpio_num = SPI_Q_GPIO_NUM;
  47. uint8_t d_gpio_num = SPI_D_GPIO_NUM;
  48. uint8_t cs0_gpio_num = SPI_CS0_GPIO_NUM;
  49. uint8_t hd_gpio_num = SPI_HD_GPIO_NUM;
  50. uint8_t wp_gpio_num = SPI_WP_GPIO_NUM;
  51. if (spiconfig == 0) {
  52. }
  53. esp_rom_gpio_pad_set_drv(clk_gpio_num, drv);
  54. esp_rom_gpio_pad_set_drv(q_gpio_num, drv);
  55. esp_rom_gpio_pad_set_drv(d_gpio_num, drv);
  56. esp_rom_gpio_pad_set_drv(cs0_gpio_num, drv);
  57. if (hd_gpio_num <= MAX_PAD_GPIO_NUM) {
  58. esp_rom_gpio_pad_set_drv(hd_gpio_num, drv);
  59. }
  60. if (wp_gpio_num <= MAX_PAD_GPIO_NUM) {
  61. esp_rom_gpio_pad_set_drv(wp_gpio_num, drv);
  62. }
  63. }
  64. static void bootloader_reset_mmu(void)
  65. {
  66. Cache_Suspend_ICache();
  67. Cache_Invalidate_ICache_All();
  68. Cache_MMU_Init();
  69. REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_IBUS);
  70. REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_DBUS);
  71. }
  72. static void update_flash_config(const esp_image_header_t *bootloader_hdr)
  73. {
  74. uint32_t size;
  75. switch (bootloader_hdr->spi_size) {
  76. case ESP_IMAGE_FLASH_SIZE_1MB:
  77. size = 1;
  78. break;
  79. case ESP_IMAGE_FLASH_SIZE_2MB:
  80. size = 2;
  81. break;
  82. case ESP_IMAGE_FLASH_SIZE_4MB:
  83. size = 4;
  84. break;
  85. case ESP_IMAGE_FLASH_SIZE_8MB:
  86. size = 8;
  87. break;
  88. case ESP_IMAGE_FLASH_SIZE_16MB:
  89. size = 16;
  90. break;
  91. default:
  92. size = 2;
  93. }
  94. uint32_t autoload = Cache_Suspend_ICache();
  95. // Set flash chip size
  96. esp_rom_spiflash_config_param(rom_spiflash_legacy_data->chip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff); // TODO: set mode
  97. Cache_Resume_ICache(autoload);
  98. }
  99. static void print_flash_info(const esp_image_header_t *bootloader_hdr)
  100. {
  101. ESP_LOGD(TAG, "magic %02x", bootloader_hdr->magic);
  102. ESP_LOGD(TAG, "segments %02x", bootloader_hdr->segment_count);
  103. ESP_LOGD(TAG, "spi_mode %02x", bootloader_hdr->spi_mode);
  104. ESP_LOGD(TAG, "spi_speed %02x", bootloader_hdr->spi_speed);
  105. ESP_LOGD(TAG, "spi_size %02x", bootloader_hdr->spi_size);
  106. const char *str;
  107. switch (bootloader_hdr->spi_speed) {
  108. case ESP_IMAGE_SPI_SPEED_40M:
  109. str = "40MHz";
  110. break;
  111. case ESP_IMAGE_SPI_SPEED_26M:
  112. str = "26.7MHz";
  113. break;
  114. case ESP_IMAGE_SPI_SPEED_20M:
  115. str = "20MHz";
  116. break;
  117. default:
  118. str = "20MHz";
  119. break;
  120. }
  121. ESP_LOGI(TAG, "SPI Speed : %s", str);
  122. /* SPI mode could have been set to QIO during boot already,
  123. so test the SPI registers not the flash header */
  124. uint32_t spi_ctrl = REG_READ(SPI_MEM_CTRL_REG(0));
  125. if (spi_ctrl & SPI_MEM_FREAD_QIO) {
  126. str = "QIO";
  127. } else if (spi_ctrl & SPI_MEM_FREAD_QUAD) {
  128. str = "QOUT";
  129. } else if (spi_ctrl & SPI_MEM_FREAD_DIO) {
  130. str = "DIO";
  131. } else if (spi_ctrl & SPI_MEM_FREAD_DUAL) {
  132. str = "DOUT";
  133. } else if (spi_ctrl & SPI_MEM_FASTRD_MODE) {
  134. str = "FAST READ";
  135. } else {
  136. str = "SLOW READ";
  137. }
  138. ESP_LOGI(TAG, "SPI Mode : %s", str);
  139. switch (bootloader_hdr->spi_size) {
  140. case ESP_IMAGE_FLASH_SIZE_1MB:
  141. str = "1MB";
  142. break;
  143. case ESP_IMAGE_FLASH_SIZE_2MB:
  144. str = "2MB";
  145. break;
  146. case ESP_IMAGE_FLASH_SIZE_4MB:
  147. str = "4MB";
  148. break;
  149. case ESP_IMAGE_FLASH_SIZE_8MB:
  150. str = "8MB";
  151. break;
  152. case ESP_IMAGE_FLASH_SIZE_16MB:
  153. str = "16MB";
  154. break;
  155. default:
  156. str = "2MB";
  157. break;
  158. }
  159. ESP_LOGI(TAG, "SPI Flash Size : %s", str);
  160. }
  161. static void bootloader_print_mmu_page_size(void)
  162. {
  163. int page_mode = MMU_Get_Page_Mode();
  164. int size = (page_mode == 0 ? 16 :
  165. page_mode == 1 ? 32 :
  166. page_mode == 2 ? 64 : 0);
  167. ESP_LOGI(TAG, "MMU Page Size : %dK", size);
  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. bootloader_print_mmu_page_size();
  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 ||
  218. rst_reason == RESET_REASON_CPU0_MWDT0 || 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. esp_err_t bootloader_init(void)
  235. {
  236. esp_err_t ret = ESP_OK;
  237. bootloader_super_wdt_auto_feed();
  238. // protect memory region
  239. bootloader_init_mem();
  240. /* check that static RAM is after the stack */
  241. assert(&_bss_start <= &_bss_end);
  242. assert(&_data_start <= &_data_end);
  243. // clear bss section
  244. bootloader_clear_bss_section();
  245. // init eFuse virtual mode (read eFuses to RAM)
  246. #ifdef CONFIG_EFUSE_VIRTUAL
  247. 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!");
  248. #ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
  249. esp_efuse_init_virtual_mode_in_ram();
  250. #endif
  251. #endif
  252. // reset MMU
  253. bootloader_reset_mmu();
  254. // config clock
  255. bootloader_clock_configure();
  256. // initialize console, from now on, we can use esp_log
  257. bootloader_console_init();
  258. /* print 2nd bootloader banner */
  259. bootloader_print_banner();
  260. // update flash ID
  261. bootloader_flash_update_id();
  262. // read bootloader header
  263. if ((ret = bootloader_read_bootloader_header()) != ESP_OK) {
  264. goto err;
  265. }
  266. // read chip revision and check if it's compatible to bootloader
  267. if ((ret = bootloader_check_bootloader_validity()) != ESP_OK) {
  268. goto err;
  269. }
  270. // initialize spi flash
  271. if ((ret = bootloader_init_spi_flash()) != ESP_OK) {
  272. goto err;
  273. }
  274. // check whether a WDT reset happend
  275. bootloader_check_wdt_reset();
  276. // config WDT
  277. bootloader_config_wdt();
  278. // enable RNG early entropy source
  279. bootloader_enable_random();
  280. err:
  281. return ret;
  282. }