From bf2f50c1328abe7b5f5d631c79276b15dfabe2bf Mon Sep 17 00:00:00 2001 From: Lucas Tamborrino Date: Thu, 26 Sep 2024 15:04:17 -0300 Subject: [PATCH] soc: espressif: Add hardware initialization Bring hardware initialization to zephyr code base. Signed-off-by: Lucas Tamborrino --- soc/espressif/common/include/hw_init.h | 12 ++ soc/espressif/common/loader.c | 169 ++++++++++++++----------- soc/espressif/esp32/CMakeLists.txt | 2 + soc/espressif/esp32/default.ld | 23 ++-- soc/espressif/esp32/hw_init.c | 94 ++++++++++++++ soc/espressif/esp32c2/CMakeLists.txt | 2 + soc/espressif/esp32c2/default.ld | 21 ++- soc/espressif/esp32c2/hw_init.c | 94 ++++++++++++++ soc/espressif/esp32c3/CMakeLists.txt | 2 + soc/espressif/esp32c3/default.ld | 26 ++-- soc/espressif/esp32c3/hw_init.c | 98 ++++++++++++++ soc/espressif/esp32c6/CMakeLists.txt | 2 + soc/espressif/esp32c6/default.ld | 27 ++-- soc/espressif/esp32c6/hw_init.c | 115 +++++++++++++++++ soc/espressif/esp32s2/CMakeLists.txt | 2 + soc/espressif/esp32s2/default.ld | 24 ++-- soc/espressif/esp32s2/hw_init.c | 102 +++++++++++++++ soc/espressif/esp32s3/CMakeLists.txt | 2 + soc/espressif/esp32s3/default.ld | 25 ++-- soc/espressif/esp32s3/hw_init.c | 103 +++++++++++++++ west.yml | 2 +- 21 files changed, 802 insertions(+), 145 deletions(-) create mode 100644 soc/espressif/common/include/hw_init.h create mode 100644 soc/espressif/esp32/hw_init.c create mode 100644 soc/espressif/esp32c2/hw_init.c create mode 100644 soc/espressif/esp32c3/hw_init.c create mode 100644 soc/espressif/esp32c6/hw_init.c create mode 100644 soc/espressif/esp32s2/hw_init.c create mode 100644 soc/espressif/esp32s3/hw_init.c diff --git a/soc/espressif/common/include/hw_init.h b/soc/espressif/common/include/hw_init.h new file mode 100644 index 00000000000000..1c318fdb8261d5 --- /dev/null +++ b/soc/espressif/common/include/hw_init.h @@ -0,0 +1,12 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef _SOC_ESPRESSIF_COMMON_HW_INIT_H_ +#define _SOC_ESPRESSIF_COMMON_HW_INIT_H_ + +int hardware_init(void); + +#endif /* _SOC_ESPRESSIF_COMMON_HW_INIT_H_ */ diff --git a/soc/espressif/common/loader.c b/soc/espressif/common/loader.c index 01e27bc588656f..cf66bfd356f0bc 100644 --- a/soc/espressif/common/loader.c +++ b/soc/espressif/common/loader.c @@ -14,46 +14,69 @@ #include #include -#define MMU_FLASH_MASK (~(CONFIG_MMU_PAGE_SIZE - 1)) - #include #include -#include "esp_rom_uart.h" -#include "esp_flash.h" -#include "esp_log.h" -#include "bootloader_init.h" -#include "bootloader_random.h" -#include "bootloader_soc.h" +#include +#include +#include +#include +#include + +#include + +#if CONFIG_SOC_SERIES_ESP32C6 +#include +#include +#include +#include +#endif /* CONFIG_SOC_SERIES_ESP32C6 */ + +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef CONFIG_SOC_SERIES_ESP32 +#include +#include +#endif + +#include "hw_init.h" +#include "soc_init.h" +#include "soc_random.h" #define TAG "boot" #define CHECKSUM_ALIGN 16 -#define IS_PADD(addr) (addr == 0) -#define IS_DRAM(addr) (addr >= SOC_DRAM_LOW && addr < SOC_DRAM_HIGH) -#define IS_IRAM(addr) (addr >= SOC_IRAM_LOW && addr < SOC_IRAM_HIGH) -#define IS_IROM(addr) (addr >= SOC_IROM_LOW && addr < SOC_IROM_HIGH) -#define IS_DROM(addr) (addr >= SOC_DROM_LOW && addr < SOC_DROM_HIGH) -#define IS_SRAM(addr) (IS_IRAM(addr) || IS_DRAM(addr)) -#define IS_MMAP(addr) (IS_IROM(addr) || IS_DROM(addr)) -#define IS_NONE(addr) (!IS_IROM(addr) && !IS_DROM(addr) \ - && !IS_IRAM(addr) && !IS_DRAM(addr) && !IS_PADD(addr)) +#define IS_PADD(addr) (addr == 0) +#define IS_DRAM(addr) (addr >= SOC_DRAM_LOW && addr < SOC_DRAM_HIGH) +#define IS_IRAM(addr) (addr >= SOC_IRAM_LOW && addr < SOC_IRAM_HIGH) +#define IS_IROM(addr) (addr >= SOC_IROM_LOW && addr < SOC_IROM_HIGH) +#define IS_DROM(addr) (addr >= SOC_DROM_LOW && addr < SOC_DROM_HIGH) +#define IS_SRAM(addr) (IS_IRAM(addr) || IS_DRAM(addr)) +#define IS_MMAP(addr) (IS_IROM(addr) || IS_DROM(addr)) +#define IS_NONE(addr) \ + (!IS_IROM(addr) && !IS_DROM(addr) && !IS_IRAM(addr) && !IS_DRAM(addr) && !IS_PADD(addr)) #define HDR_ATTR __attribute__((section(".entry_addr"))) __attribute__((used)) void __start(void); static HDR_ATTR void (*_entry_point)(void) = &__start; -extern esp_image_header_t bootloader_image_hdr; +esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr; extern uint32_t _image_irom_start, _image_irom_size, _image_irom_vaddr; extern uint32_t _image_drom_start, _image_drom_size, _image_drom_vaddr; #ifndef CONFIG_MCUBOOT -static uint32_t _app_irom_start = (FIXED_PARTITION_OFFSET(slot0_partition) + - (uint32_t)&_image_irom_start); +static uint32_t _app_irom_start = + (FIXED_PARTITION_OFFSET(slot0_partition) + (uint32_t)&_image_irom_start); static uint32_t _app_irom_size = (uint32_t)&_image_irom_size; -static uint32_t _app_drom_start = (FIXED_PARTITION_OFFSET(slot0_partition) + - (uint32_t)&_image_drom_start); +static uint32_t _app_drom_start = + (FIXED_PARTITION_OFFSET(slot0_partition) + (uint32_t)&_image_drom_start); static uint32_t _app_drom_size = (uint32_t)&_image_drom_size; #endif @@ -61,15 +84,14 @@ static uint32_t _app_irom_vaddr = ((uint32_t)&_image_irom_vaddr); static uint32_t _app_drom_vaddr = ((uint32_t)&_image_drom_vaddr); #ifndef CONFIG_BOOTLOADER_MCUBOOT -static esp_err_t spi_flash_read(uint32_t address, void *buffer, size_t length) +static int spi_flash_read(uint32_t address, void *buffer, size_t length) { return esp_flash_read(NULL, buffer, address, length); } #endif /* CONFIG_BOOTLOADER_MCUBOOT */ -void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, - uint32_t app_drom_size, uint32_t app_irom_start, - uint32_t app_irom_vaddr, uint32_t app_irom_size) +void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, uint32_t app_drom_size, + uint32_t app_irom_start, uint32_t app_irom_vaddr, uint32_t app_irom_size) { uint32_t app_irom_start_aligned = app_irom_start & MMU_FLASH_MASK; uint32_t app_irom_vaddr_aligned = app_irom_vaddr & MMU_FLASH_MASK; @@ -88,8 +110,7 @@ void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, while (segments++ < 16) { - if (spi_flash_read(offset, &segment_hdr, - sizeof(esp_image_segment_header_t)) != ESP_OK) { + if (spi_flash_read(offset, &segment_hdr, sizeof(esp_image_segment_header_t)) != 0) { ESP_EARLY_LOGE(TAG, "Failed to read segment header at %x", offset); abort(); } @@ -101,13 +122,14 @@ void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, } ESP_EARLY_LOGI(TAG, "%s: lma 0x%08x vma 0x%08x len 0x%-6x (%u)", - IS_NONE(segment_hdr.load_addr) ? "???" : - IS_MMAP(segment_hdr.load_addr) ? - IS_IROM(segment_hdr.load_addr) ? "IMAP" : "DMAP" : - IS_PADD(segment_hdr.load_addr) ? "padd" : - IS_DRAM(segment_hdr.load_addr) ? "DRAM" : "IRAM", - offset + sizeof(esp_image_segment_header_t), - segment_hdr.load_addr, segment_hdr.data_len, segment_hdr.data_len); + IS_NONE(segment_hdr.load_addr) ? "???" + : IS_MMAP(segment_hdr.load_addr) + ? IS_IROM(segment_hdr.load_addr) ? "IMAP" : "DMAP" + : IS_PADD(segment_hdr.load_addr) ? "padd" + : IS_DRAM(segment_hdr.load_addr) ? "DRAM" + : "IRAM", + offset + sizeof(esp_image_segment_header_t), segment_hdr.load_addr, + segment_hdr.data_len, segment_hdr.data_len); /* Fix drom and irom produced be the linker, as it could * be invalidated by the elf2image and flash load offset @@ -153,20 +175,20 @@ void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, #if CONFIG_SOC_SERIES_ESP32 int rc = 0; uint32_t drom_page_count = - (app_drom_size + CONFIG_MMU_PAGE_SIZE - 1) / CONFIG_MMU_PAGE_SIZE; + (app_drom_size + CONFIG_MMU_PAGE_SIZE - 1) / CONFIG_MMU_PAGE_SIZE; - rc |= cache_flash_mmu_set(0, 0, app_drom_vaddr_aligned, - app_drom_start_aligned, 64, drom_page_count); - rc |= cache_flash_mmu_set(1, 0, app_drom_vaddr_aligned, - app_drom_start_aligned, 64, drom_page_count); + rc |= cache_flash_mmu_set(0, 0, app_drom_vaddr_aligned, app_drom_start_aligned, 64, + drom_page_count); + rc |= cache_flash_mmu_set(1, 0, app_drom_vaddr_aligned, app_drom_start_aligned, 64, + drom_page_count); uint32_t irom_page_count = - (app_irom_size + CONFIG_MMU_PAGE_SIZE - 1) / CONFIG_MMU_PAGE_SIZE; + (app_irom_size + CONFIG_MMU_PAGE_SIZE - 1) / CONFIG_MMU_PAGE_SIZE; - rc |= cache_flash_mmu_set(0, 0, app_irom_vaddr_aligned, - app_irom_start_aligned, 64, irom_page_count); - rc |= cache_flash_mmu_set(1, 0, app_irom_vaddr_aligned, - app_irom_start_aligned, 64, irom_page_count); + rc |= cache_flash_mmu_set(0, 0, app_irom_vaddr_aligned, app_irom_start_aligned, 64, + irom_page_count); + rc |= cache_flash_mmu_set(1, 0, app_irom_vaddr_aligned, app_irom_start_aligned, 64, + irom_page_count); if (rc != 0) { ESP_EARLY_LOGE(TAG, "Failed to setup XIP, aborting"); abort(); @@ -174,13 +196,11 @@ void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, #else uint32_t actual_mapped_len = 0; - mmu_hal_map_region(0, MMU_TARGET_FLASH0, - app_drom_vaddr_aligned, app_drom_start_aligned, - app_drom_size, &actual_mapped_len); + mmu_hal_map_region(0, MMU_TARGET_FLASH0, app_drom_vaddr_aligned, app_drom_start_aligned, + app_drom_size, &actual_mapped_len); - mmu_hal_map_region(0, MMU_TARGET_FLASH0, - app_irom_vaddr_aligned, app_irom_start_aligned, - app_irom_size, &actual_mapped_len); + mmu_hal_map_region(0, MMU_TARGET_FLASH0, app_irom_vaddr_aligned, app_irom_start_aligned, + app_irom_size, &actual_mapped_len); #endif /* CONFIG_SOC_SERIES_ESP32 */ /* ----------------------Enable corresponding buses---------------- */ @@ -206,22 +226,20 @@ void map_rom_segments(uint32_t app_drom_start, uint32_t app_drom_vaddr, #if !defined(CONFIG_SOC_SERIES_ESP32) && !defined(CONFIG_SOC_SERIES_ESP32S2) /* Configure the Cache MMU size for instruction and rodata in flash. */ - uint32_t cache_mmu_irom_size = ((app_irom_size + CONFIG_MMU_PAGE_SIZE - 1) / - CONFIG_MMU_PAGE_SIZE) * sizeof(uint32_t); + uint32_t cache_mmu_irom_size = + ((app_irom_size + CONFIG_MMU_PAGE_SIZE - 1) / CONFIG_MMU_PAGE_SIZE) * + sizeof(uint32_t); /* Split the cache usage by the segment sizes */ - Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, - CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size); + Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size); #endif /* Show map segments continue using same log format as during MCUboot phase */ - ESP_EARLY_LOGI(TAG, "%s segment: paddr=%08xh, vaddr=%08xh, size=%05Xh (%6d) map", - "DROM", - app_drom_start_aligned, app_drom_vaddr_aligned, - app_drom_size, app_drom_size); - ESP_EARLY_LOGI(TAG, "%s segment: paddr=%08xh, vaddr=%08xh, size=%05Xh (%6d) map", - "IROM", - app_irom_start_aligned, app_irom_vaddr_aligned, - app_irom_size, app_irom_size); + ESP_EARLY_LOGI(TAG, "%s segment: paddr=%08xh, vaddr=%08xh, size=%05Xh (%6d) map", "DROM", + app_drom_start_aligned, app_drom_vaddr_aligned, app_drom_size, + app_drom_size); + ESP_EARLY_LOGI(TAG, "%s segment: paddr=%08xh, vaddr=%08xh, size=%05Xh (%6d) map", "IROM", + app_irom_start_aligned, app_irom_vaddr_aligned, app_irom_size, + app_irom_size); esp_rom_uart_tx_wait_idle(0); } @@ -233,29 +251,32 @@ void __start(void) * relaxed by the linker to access something relative to __global_pointer$) */ __asm__ __volatile__(".option push\n" - ".option norelax\n" - "la gp, __global_pointer$\n" - ".option pop"); + ".option norelax\n" + "la gp, __global_pointer$\n" + ".option pop"); #endif /* CONFIG_RISCV_GP */ #ifndef CONFIG_BOOTLOADER_MCUBOOT /* Init fundamental components */ - if (bootloader_init()) { + if (hardware_init()) { ESP_EARLY_LOGE(TAG, "HW init failed, aborting"); abort(); } #endif #if !defined(CONFIG_MCUBOOT) && !defined(CONFIG_SOC_ESP32S3_APPCPU) - map_rom_segments(_app_drom_start, _app_drom_vaddr, _app_drom_size, - _app_irom_start, _app_irom_vaddr, _app_irom_size); + map_rom_segments(_app_drom_start, _app_drom_vaddr, _app_drom_size, _app_irom_start, + _app_irom_vaddr, _app_irom_size); #endif - +#ifndef CONFIG_SOC_SERIES_ESP32C2 /* Disable RNG entropy source as it was already used */ - bootloader_random_disable(); - + soc_random_disable(); +#endif /* CONFIG_SOC_SERIES_ESP32C2 */ +#if defined(CONFIG_SOC_SERIES_ESP32S3) || defined(CONFIG_SOC_SERIES_ESP32C3) /* Disable glitch detection as it can be falsely triggered by EMI interference */ - bootloader_ana_clock_glitch_reset_config(false); - + ESP_EARLY_LOGI(TAG, "Disabling glitch detection"); + ana_clock_glitch_reset_config(false); +#endif /* CONFIG_SOC_SERIES_ESP32S2 */ + ESP_EARLY_LOGI(TAG, "Jumping to the main image..."); __esp_platform_start(); } diff --git a/soc/espressif/esp32/CMakeLists.txt b/soc/espressif/esp32/CMakeLists.txt index d5373b0b78fef4..eb4ba71f56d776 100644 --- a/soc/espressif/esp32/CMakeLists.txt +++ b/soc/espressif/esp32/CMakeLists.txt @@ -12,6 +12,8 @@ endif() zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + zephyr_library_sources_ifdef(CONFIG_NEWLIB_LIBC newlib_fix.c) zephyr_library_sources_ifdef(CONFIG_GDBSTUB gdbstub.c) diff --git a/soc/espressif/esp32/default.ld b/soc/espressif/esp32/default.ld index 86a25901d03be8..82aaa3699e371e 100644 --- a/soc/espressif/esp32/default.ld +++ b/soc/espressif/esp32/default.ld @@ -274,6 +274,12 @@ SECTIONS *libzephyr.a:log_output.*(.literal .text .literal.* .text.*) *libzephyr.a:log_backend_uart.*(.literal .text .literal.* .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_random.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) *libzephyr.a:rtc_*.*(.literal .text .literal.* .text.*) *libzephyr.a:cpu_util.*(.literal .text .literal.* .text.*) *libdrivers__flash.a:flash_esp32.*(.literal .text .literal.* .text.*) @@ -403,21 +409,14 @@ SECTIONS .loader.text : { . = ALIGN(4); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_flash_config_esp32.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) @@ -520,6 +519,12 @@ SECTIONS *libzephyr.a:log_backend_uart.*(.rodata .rodata.*) *libzephyr.a:log_output.*(.rodata .rodata.*) *libzephyr.a:loader.*(.rodata .rodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.*) + *libzephyr.a:soc_random.*(.rodata .rodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.*) *libdrivers__flash.a:flash_esp32.*(.rodata .rodata.*) *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.*) *libzephyr.a:esp_memory_utils.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) @@ -625,14 +630,10 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_esp32.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_flash_config_esp32.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_common.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_common_loader.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_efuse.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:cpu_util.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) diff --git a/soc/espressif/esp32/hw_init.c b/soc/espressif/esp32/hw_init.c new file mode 100644 index 00000000000000..8ff2a3f684b442 --- /dev/null +++ b/soc/espressif/esp32/hw_init.c @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + +#if XCHAL_ERRATUM_572 + uint32_t memctl = XCHAL_CACHE_MEMCTL_DEFAULT; + + WSR(MEMCTL, memctl); +#endif /*XCHAL_ERRATUM_572*/ + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + reset_mmu(); + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + soc_random_enable(); + + return 0; +} diff --git a/soc/espressif/esp32c2/CMakeLists.txt b/soc/espressif/esp32c2/CMakeLists.txt index d8cb333e9d4b98..2d70c8cd7de6f5 100644 --- a/soc/espressif/esp32c2/CMakeLists.txt +++ b/soc/espressif/esp32c2/CMakeLists.txt @@ -10,6 +10,8 @@ zephyr_sources( zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + # get flash size to use in esptool as string math(EXPR esptoolpy_flashsize "${CONFIG_FLASH_SIZE} / 0x100000") diff --git a/soc/espressif/esp32c2/default.ld b/soc/espressif/esp32c2/default.ld index 1805d45a472304..8febd61ec627e6 100644 --- a/soc/espressif/esp32c2/default.ld +++ b/soc/espressif/esp32c2/default.ld @@ -170,6 +170,11 @@ SECTIONS *(.iram0.literal .iram.literal .iram.text.literal .iram0.text .iram.text) *libzephyr.a:panic.*(.literal .text .literal.* .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) *libarch__riscv__core.a:(.literal .text .literal.* .text.*) *libsubsys__net__l2__ethernet.a:(.literal .text .literal.* .text.*) *libsubsys__net__lib__config.a:(.literal .text .literal.* .text.*) @@ -303,21 +308,15 @@ SECTIONS { . = ALIGN(4); _loader_text_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32c2.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_flash_config_esp32c2.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) *libzephyr.a:flash_ops.*(.literal .text .literal.* .text.*) @@ -422,6 +421,11 @@ SECTIONS *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:periph_ctrl.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:loader.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.*) *libzephyr.a:cache_utils.*(.rodata .rodata.* .srodata .srodata.*) /* [mapping:hal] */ @@ -502,15 +506,10 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_soc.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_esp32c2.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.srodata .srodata.* .rodata .rodata.*) - *libzephyr.a:bootloader_flash_config_esp32c2.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_loader.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_common_loader.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_panic.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:cpu_region_protect.*(.rodata .rodata.* .srodata .srodata.*) diff --git a/soc/espressif/esp32c2/hw_init.c b/soc/espressif/esp32c2/hw_init.c new file mode 100644 index 00000000000000..3a57704040d666 --- /dev/null +++ b/soc/espressif/esp32c2/hw_init.c @@ -0,0 +1,94 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + + ana_reset_config(); + super_wdt_auto_feed(); + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + cache_hal_init(); + mmu_hal_init(); + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + + return 0; +} diff --git a/soc/espressif/esp32c3/CMakeLists.txt b/soc/espressif/esp32c3/CMakeLists.txt index 80b90a76379f16..bfec91fbc4c94f 100644 --- a/soc/espressif/esp32c3/CMakeLists.txt +++ b/soc/espressif/esp32c3/CMakeLists.txt @@ -10,6 +10,8 @@ zephyr_sources( zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + zephyr_library_sources_ifdef(CONFIG_PM power.c) zephyr_library_sources_ifdef(CONFIG_POWEROFF poweroff.c) diff --git a/soc/espressif/esp32c3/default.ld b/soc/espressif/esp32c3/default.ld index 50f41b6cd997df..5edb5047f2b2b0 100644 --- a/soc/espressif/esp32c3/default.ld +++ b/soc/espressif/esp32c3/default.ld @@ -230,6 +230,13 @@ SECTIONS *(.iram0.literal .iram.literal .iram.text.literal .iram0.text .iram.text) *libzephyr.a:panic.*(.literal .text .literal.* .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_random.*(.literal .text .literal.* .text.*) + *libarch__riscv__core.a:(.literal .text .literal.* .text.*) *libsubsys__net__l2__ethernet.a:(.literal .text .literal.* .text.*) *libsubsys__net__lib__config.a:(.literal .text .literal.* .text.*) @@ -330,7 +337,6 @@ SECTIONS *libzephyr.a:esp_cache.*(.literal .literal.* .text .text.*) *libzephyr.a:cache_utils.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_soc.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_disable .text.bootloader_random_disable) *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_enable .text.bootloader_random_enable) @@ -363,21 +369,15 @@ SECTIONS { . = ALIGN(4); _loader_text_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32c3.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_flash_config_esp32c3.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) *libzephyr.a:flash_encrypt.*(.literal .text .literal.* .text.*) @@ -480,6 +480,13 @@ SECTIONS *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:periph_ctrl.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:loader.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.*) + *libzephyr.a:soc_random.*(.rodata .rodata.*) + *libzephyr.a:cache_utils.*(.rodata .rodata.* .srodata .srodata.*) /* [mapping:hal] */ @@ -560,15 +567,10 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_soc.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_esp32c3.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.srodata .srodata.* .rodata .rodata.*) - *libzephyr.a:bootloader_flash_config_esp32c3.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_loader.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_common_loader.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_panic.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:cpu_region_protect.*(.rodata .rodata.* .srodata .srodata.*) diff --git a/soc/espressif/esp32c3/hw_init.c b/soc/espressif/esp32c3/hw_init.c new file mode 100644 index 00000000000000..37aacd3bbbda9d --- /dev/null +++ b/soc/espressif/esp32c3/hw_init.c @@ -0,0 +1,98 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + + soc_hw_init(); + + ana_reset_config(); + super_wdt_auto_feed(); + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + cache_hal_init(); + mmu_hal_init(); + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + + soc_random_enable(); + + return 0; +} diff --git a/soc/espressif/esp32c6/CMakeLists.txt b/soc/espressif/esp32c6/CMakeLists.txt index 1bf55e1a31554a..6bd5b99e6ff536 100644 --- a/soc/espressif/esp32c6/CMakeLists.txt +++ b/soc/espressif/esp32c6/CMakeLists.txt @@ -10,6 +10,8 @@ zephyr_sources( zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + zephyr_library_sources_ifdef(CONFIG_PM power.c) zephyr_library_sources_ifdef(CONFIG_POWEROFF poweroff.c) diff --git a/soc/espressif/esp32c6/default.ld b/soc/espressif/esp32c6/default.ld index 39bdac633aee86..cf617020e58cb9 100644 --- a/soc/espressif/esp32c6/default.ld +++ b/soc/espressif/esp32c6/default.ld @@ -272,6 +272,13 @@ SECTIONS *(.iram0.literal .iram.literal .iram.text.literal .iram0.text .iram.text) *libzephyr.a:panic.*(.literal .text .literal.* .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_random.*(.literal .text .literal.* .text.*) + *libarch__riscv__core.a:(.literal .text .literal.* .text.*) *libsubsys__net__l2__ethernet.a:(.literal .text .literal.* .text.*) *libsubsys__net__lib__config.a:(.literal .text .literal.* .text.*) @@ -383,10 +390,6 @@ SECTIONS *libzephyr.a:esp_cache.*(.literal .literal.* .text .text.*) *libzephyr.a:cache_utils.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_soc.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_disable .text.bootloader_random_disable) - *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_enable .text.bootloader_random_enable) - #if defined(CONFIG_ESP32_WIFI_IRAM_OPT) *libnet80211.a:( .wifi0iram .wifi0iram.* .wifislpiram .wifislpiram.* .wifiextrairam .wifiextrairam.* .wifi_extra_iram.*) *libpp.a:( .wifi0iram .wifi0iram.* .wifislpiram .wifislpiram.* .wifiextrairam .wifiextrairam.* .wifi_extra_iram.*) @@ -415,21 +418,16 @@ SECTIONS { . = ALIGN(4); _loader_text_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32c6.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_flash_config_esp32c6.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) *libzephyr.a:flash_ops.*(.literal .text .literal.* .text.*) @@ -527,6 +525,13 @@ SECTIONS *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:periph_ctrl.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:loader.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:soc_random.*(.rodata .rodata.* .srodata .srodata.*) + *libzephyr.a:cache_utils.*(.rodata .rodata.* .srodata .srodata.*) /* [mapping:hal] */ @@ -622,13 +627,9 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_soc.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .srodata .srodata.*) - *libzephyr.a:bootloader_esp32c6.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.srodata .srodata.* .rodata .rodata.*) - *libzephyr.a:bootloader_flash_config_esp32c6.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_loader.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_common_loader.*(.rodata .rodata.* .srodata .srodata.*) *libzephyr.a:bootloader_panic.*(.rodata .rodata.* .srodata .srodata.*) diff --git a/soc/espressif/esp32c6/hw_init.c b/soc/espressif/esp32c6/hw_init.c new file mode 100644 index 00000000000000..e4fc9b34d9e786 --- /dev/null +++ b/soc/espressif/esp32c6/hw_init.c @@ -0,0 +1,115 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + + soc_hw_init(); + + ana_reset_config(); + super_wdt_auto_feed(); + + /* By default, these access path filters are enable and allow the + * access to masters only if they are in TEE mode. Since all masters + * except HP CPU boots in REE mode, default setting of these filters + * will deny the access to all masters except HP CPU. + * So, at boot disabling these filters. They will enable as per the + * use case by TEE initialization code. + */ + REG_WRITE(LP_APM_FUNC_CTRL_REG, 0); + REG_WRITE(LP_APM0_FUNC_CTRL_REG, 0); + REG_WRITE(HP_APM_FUNC_CTRL_REG, 0); + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + cache_hal_init(); + mmu_hal_init(); + + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + + soc_random_enable(); + + return 0; +} diff --git a/soc/espressif/esp32s2/CMakeLists.txt b/soc/espressif/esp32s2/CMakeLists.txt index d4f6a7c5c945b8..ef1d907a64c8b0 100644 --- a/soc/espressif/esp32s2/CMakeLists.txt +++ b/soc/espressif/esp32s2/CMakeLists.txt @@ -8,6 +8,8 @@ zephyr_sources( zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + zephyr_library_sources_ifdef(CONFIG_NEWLIB_LIBC newlib_fix.c) zephyr_library_sources_ifdef(CONFIG_PM power.c) diff --git a/soc/espressif/esp32s2/default.ld b/soc/espressif/esp32s2/default.ld index f3dfcac5015513..eaaeee11b03204 100644 --- a/soc/espressif/esp32s2/default.ld +++ b/soc/espressif/esp32s2/default.ld @@ -343,6 +343,13 @@ SECTIONS *libzephyr.a:log_backend_uart.*(.literal .text .literal.* .text.*) *libzephyr.a:log_minimal.*(.literal .literal.* .text .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_random.*(.literal .text .literal.* .text.*) + *libzephyr.a:esp_mmu_map.*(.literal .literal.* .text .text.*) *libdrivers__flash.a:flash_esp32.*(.literal .text .literal.* .text.*) *libdrivers__timer.a:xtensa_sys_timer.*(.literal .text .literal.* .text.*) @@ -469,21 +476,14 @@ SECTIONS { . = ALIGN(4); _loader_text_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32s2.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_flash_config_esp32s2.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) @@ -590,6 +590,13 @@ SECTIONS *libzephyr.a:log_output.*(.rodata .rodata.*) *libzephyr.a:log_minimal.*(.rodata .rodata.*) *libzephyr.a:loader.*(.rodata .rodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.*) + *libzephyr.a:soc_random.*(.rodata .rodata.*) + *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.*) *libdrivers__flash.a:flash_esp32.*(.rodata .rodata.*) *libzephyr.a:esp_mmu_map.*(.rodata .rodata.*) @@ -702,12 +709,9 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_esp32s3.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_flash_config_esp32s2.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_efuse.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_random_esp32s2.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) diff --git a/soc/espressif/esp32s2/hw_init.c b/soc/espressif/esp32s2/hw_init.c new file mode 100644 index 00000000000000..e5381245559669 --- /dev/null +++ b/soc/espressif/esp32s2/hw_init.c @@ -0,0 +1,102 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + + super_wdt_auto_feed(); + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + cache_hal_init(); + mmu_hal_init(); + + /* Workaround: normal ROM bootloader exits with DROM0 cache unmasked, but 2nd bootloader + * exits with it masked. + */ + REG_CLR_BIT(EXTMEM_PRO_ICACHE_CTRL1_REG, EXTMEM_PRO_ICACHE_MASK_DROM0); + + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + + soc_random_enable(); + + return 0; +} diff --git a/soc/espressif/esp32s3/CMakeLists.txt b/soc/espressif/esp32s3/CMakeLists.txt index 706fe6eb39fc88..00d26e14df8605 100644 --- a/soc/espressif/esp32s3/CMakeLists.txt +++ b/soc/espressif/esp32s3/CMakeLists.txt @@ -19,6 +19,8 @@ endif() zephyr_include_directories(.) +zephyr_sources_ifndef(CONFIG_BOOTLOADER_MCUBOOT hw_init.c) + zephyr_library_sources_ifdef(CONFIG_NEWLIB_LIBC newlib_fix.c) # Power Management diff --git a/soc/espressif/esp32s3/default.ld b/soc/espressif/esp32s3/default.ld index b51f1d5282c7bc..0f40119015f5d3 100644 --- a/soc/espressif/esp32s3/default.ld +++ b/soc/espressif/esp32s3/default.ld @@ -300,6 +300,12 @@ SECTIONS *libzephyr.a:log_backend_uart.*(.literal .text .literal.* .text.*) *libzephyr.a:log_minimal.*(.literal .literal.* .text .text.*) *libzephyr.a:loader.*(.literal .text .literal.* .text.*) + *libzephyr.a:flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_flash_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:console_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:hw_init.*(.literal .text .literal.* .text.*) + *libzephyr.a:soc_random.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_mmu_map.*(.literal .literal.* .text .text.*) *libdrivers__interrupt_controller.a:(.literal .literal.* .text .text.*) *liblib__libc__minimal.a:string.*(.literal .text .literal.* .text.*) @@ -394,10 +400,6 @@ SECTIONS /* [mapping:esp_mm] */ *libzephyr.a:esp_cache.*(.literal .literal.* .text .text.*) - *libzephyr.a:bootloader_soc.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_disable .text.bootloader_random_disable) - *libzephyr.a:bootloader_random*.*(.literal.bootloader_random_enable .text.bootloader_random_enable) - #if defined(CONFIG_ESP32_WIFI_IRAM_OPT) *libnet80211.a:(.wifi0iram .wifi0iram.* .wifislpiram .wifislpiram.* .wifiextrairam .wifiextrairam.*) *libpp.a:(.wifi0iram .wifi0iram.* .wifislpiram .wifislpiram.* .wifiorslpiram .wifiorslpiram.* .wifiextrairam .wifiextrairam.*) @@ -426,20 +428,14 @@ SECTIONS { . = ALIGN(4); _loader_text_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_esp32s3.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_init.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_wdt.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_flash_config_esp32s3.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_mem.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_random.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_efuse.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_sha.*(.literal .text .literal.* .text.*) - *libzephyr.a:bootloader_console.*(.literal .text .literal.* .text.*) *libzephyr.a:bootloader_panic.*(.literal .text .literal.* .text.*) *libzephyr.a:esp_image_format.*(.literal .text .literal.* .text.*) @@ -548,6 +544,12 @@ SECTIONS *libzephyr.a:log_output.*(.rodata .rodata.*) *libzephyr.a:log_minimal.*(.rodata .rodata.*) *libzephyr.a:loader.*(.rodata .rodata.*) + *libzephyr.a:flash_init.*(.rodata .rodata.*) + *libzephyr.a:soc_flash_init.*(.rodata .rodata.*) + *libzephyr.a:console_init.*(.rodata .rodata.*) + *libzephyr.a:soc_init.*(.rodata .rodata.*) + *libzephyr.a:hw_init.*(.rodata .rodata.*) + *libzephyr.a:soc_random.*(.rodata .rodata.*) *libdrivers__serial.a:uart_esp32.*(.rodata .rodata.*) *libdrivers__flash.a:flash_esp32.*(.rodata .rodata.*) *libzephyr.a:esp_mmu_map.*(.rodata .rodata.*) @@ -663,12 +665,9 @@ SECTIONS { . = ALIGN(4); _loader_data_start = ABSOLUTE(.); - *libzephyr.a:bootloader_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_esp32s3.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_clock_init.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_wdt.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_flash.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) - *libzephyr.a:bootloader_flash_config_esp32s3.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:bootloader_efuse.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) *libzephyr.a:cpu_util.*(.rodata .rodata.* .sdata2 .sdata2.* .srodata .srodata.*) diff --git a/soc/espressif/esp32s3/hw_init.c b/soc/espressif/esp32s3/hw_init.c new file mode 100644 index 00000000000000..97ffb0ef8265c8 --- /dev/null +++ b/soc/espressif/esp32s3/hw_init.c @@ -0,0 +1,103 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "hw_init.h" +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +const static char *TAG = "hw_init"; + +int hardware_init(void) +{ + int err = 0; + +#if XCHAL_ERRATUM_572 + uint32_t memctl = XCHAL_CACHE_MEMCTL_DEFAULT; + + WSR(MEMCTL, memctl); +#endif /*XCHAL_ERRATUM_572*/ + + ana_reset_config(); + super_wdt_auto_feed(); + +#ifdef CONFIG_BOOTLOADER_REGION_PROTECTION_ENABLE + esp_cpu_configure_region_protection(); +#endif +#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V + rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config(); + + if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { + cfg.drefh = 3; + cfg.drefm = 3; + cfg.drefl = 3; + cfg.force = 1; + rtc_vddsdio_set_config(cfg); + esp_rom_delay_us(10); + } +#endif /* CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V */ + + bootloader_clock_configure(); + + /* initialize console, from now on, we can log */ + esp_console_init(); + print_banner(); + + spi_flash_init_chip_state(); + err = esp_flash_init_default_chip(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "Failed to init flash chip, error %d", err); + return err; + } + + cache_hal_init(); + mmu_hal_init(); + + flash_update_id(); + + err = bootloader_flash_xmc_startup(); + if (err != 0) { + ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); + return err; + } + + err = read_bootloader_header(); + if (err != 0) { + return err; + } + + err = check_bootloader_validity(); + if (err != 0) { + return err; + } + + err = init_spi_flash(); + if (err != 0) { + return err; + } + + check_wdt_reset(); + config_wdt(); + + soc_random_enable(); + + return 0; +} diff --git a/west.yml b/west.yml index e3c82b44821525..b8faa607b0f99b 100644 --- a/west.yml +++ b/west.yml @@ -157,7 +157,7 @@ manifest: groups: - hal - name: hal_espressif - revision: 8d7054f34e6d05e2917410ff0630ee88f553521d + revision: 6605f649d151f255011c7dfdd35cd568caa871a4 path: modules/hal/espressif west-commands: west/west-commands.yml groups: