1 /* 2 * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include "hw_init.h" 8 #include <stdint.h> 9 #include <esp_cpu.h> 10 #include <soc/rtc.h> 11 #include <esp_rom_sys.h> 12 13 #include <hal/cache_hal.h> 14 #include <hal/mmu_hal.h> 15 16 #include <bootloader_clock.h> 17 #include <bootloader_flash.h> 18 #include <esp_flash_internal.h> 19 #include <esp_log.h> 20 21 #include <console_init.h> 22 #include <flash_init.h> 23 #include <soc_flash_init.h> 24 #include <soc_init.h> 25 #include <soc_random.h> 26 27 const static char *TAG = "hw_init"; 28 hardware_init(void)29int hardware_init(void) 30 { 31 int err = 0; 32 33 soc_hw_init(); 34 35 ana_reset_config(); 36 super_wdt_auto_feed(); 37 38 #ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE 39 esp_cpu_configure_region_protection(); 40 #endif 41 #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V 42 rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); 43 44 if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { 45 cfg.drefh = 3; 46 cfg.drefm = 3; 47 cfg.drefl = 3; 48 cfg.force = 1; 49 rtc_vddsdio_set_config(cfg); 50 esp_rom_delay_us(10); 51 } 52 #endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ 53 54 bootloader_clock_configure(); 55 56 /* initialize console, from now on, we can log */ 57 esp_console_init(); 58 print_banner(); 59 60 spi_flash_init_chip_state(); 61 err = esp_flash_init_default_chip(); 62 if (err != 0) { 63 ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); 64 return err; 65 } 66 67 cache_hal_init(); 68 mmu_hal_init(); 69 flash_update_id(); 70 71 err = bootloader_flash_xmc_startup(); 72 if (err != 0) { 73 ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); 74 return err; 75 } 76 77 err = read_bootloader_header(); 78 if (err != 0) { 79 return err; 80 } 81 82 err = check_bootloader_validity(); 83 if (err != 0) { 84 return err; 85 } 86 87 err = init_spi_flash(); 88 if (err != 0) { 89 return err; 90 } 91 92 check_wdt_reset(); 93 config_wdt(); 94 95 soc_random_enable(); 96 97 return 0; 98 } 99