From aa6a967d1ab4077691aa046229a782102960218a Mon Sep 17 00:00:00 2001 From: Sylvio Alves Date: Tue, 10 Sep 2024 10:17:32 -0300 Subject: [PATCH] spi_flash: initialize flash during bootloader This makes sure flash is initialized during bootloader process in order to get chip information. Signed-off-by: Sylvio Alves --- .../src/esp32/bootloader_esp32.c | 13 +++++++------ .../src/esp32c2/bootloader_esp32c2.c | 5 +---- .../src/esp32c3/bootloader_esp32c3.c | 6 ++---- .../src/esp32c6/bootloader_esp32c6.c | 6 +----- .../src/esp32s2/bootloader_esp32s2.c | 3 ++- .../src/esp32s3/bootloader_esp32s3.c | 10 ++++++---- .../include/esp_private/spi_common_internal.h | 1 - components/spi_flash/spi_flash_os_func_app.c | 16 ++++------------ zephyr/esp32/CMakeLists.txt | 1 + zephyr/esp32c2/CMakeLists.txt | 1 + zephyr/esp32c3/CMakeLists.txt | 1 + zephyr/esp32c6/CMakeLists.txt | 1 + zephyr/esp32s2/CMakeLists.txt | 1 + zephyr/esp32s3/CMakeLists.txt | 1 + 14 files changed, 29 insertions(+), 37 deletions(-) diff --git a/components/bootloader_support/src/esp32/bootloader_esp32.c b/components/bootloader_support/src/esp32/bootloader_esp32.c index 0cad252e10..1cc83a2531 100644 --- a/components/bootloader_support/src/esp32/bootloader_esp32.c +++ b/components/bootloader_support/src/esp32/bootloader_esp32.c @@ -211,6 +211,13 @@ esp_err_t bootloader_init(void) // print 2nd bootloader banner bootloader_print_banner(); +#ifndef CONFIG_BOOTLOADER_MCUBOOT + spi_flash_init_chip_state(); + if ((ret = esp_flash_init_default_chip()) != ESP_OK) { + return ret; + } +#endif + #if !CONFIG_APP_BUILD_TYPE_RAM // reset MMU bootloader_reset_mmu(); @@ -221,12 +228,6 @@ esp_err_t bootloader_init(void) ESP_LOGE(TAG, "failed when running XMC startup flow, reboot!"); return ret; } - // Workaround to make flash accesible if no bootloader is enabled -#ifdef CONFIG_ESP_SIMPLE_BOOT - if ((ret = esp_flash_init_default_chip()) != ESP_OK) { - return ret; - } -#endif // read bootloader header if ((ret = bootloader_read_bootloader_header()) != ESP_OK) { return ret; diff --git a/components/bootloader_support/src/esp32c2/bootloader_esp32c2.c b/components/bootloader_support/src/esp32c2/bootloader_esp32c2.c index 6e8fc35fea..3e3dcff56e 100644 --- a/components/bootloader_support/src/esp32c2/bootloader_esp32c2.c +++ b/components/bootloader_support/src/esp32c2/bootloader_esp32c2.c @@ -124,12 +124,9 @@ esp_err_t bootloader_init(void) /* print 2nd bootloader banner */ bootloader_print_banner(); -#ifdef CONFIG_ESP_SIMPLE_BOOT - esp_mspi_pin_init(); +#ifndef CONFIG_BOOTLOADER_MCUBOOT spi_flash_init_chip_state(); - if ((ret = esp_flash_init_default_chip()) != ESP_OK) { - ESP_EARLY_LOGE(TAG, "esp_flash_init_default_chip err=%x\n", ret); return ret; } #endif diff --git a/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c b/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c index e43763d5eb..681d91b073 100644 --- a/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c +++ b/components/bootloader_support/src/esp32c3/bootloader_esp32c3.c @@ -163,11 +163,9 @@ esp_err_t bootloader_init(void) /* print 2nd bootloader banner */ bootloader_print_banner(); -#ifdef CONFIG_ESP_SIMPLE_BOOT - esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false); - esp_mspi_pin_init(); +#ifndef CONFIG_BOOTLOADER_MCUBOOT + spi_flash_init_chip_state(); if ((ret = esp_flash_init_default_chip()) != ESP_OK) { - ESP_EARLY_LOGE(TAG, "esp_flash_init_default_chip err=%x\n", ret); return ret; } #endif diff --git a/components/bootloader_support/src/esp32c6/bootloader_esp32c6.c b/components/bootloader_support/src/esp32c6/bootloader_esp32c6.c index 55064d1b70..fe4b3cd05c 100644 --- a/components/bootloader_support/src/esp32c6/bootloader_esp32c6.c +++ b/components/bootloader_support/src/esp32c6/bootloader_esp32c6.c @@ -145,13 +145,9 @@ esp_err_t bootloader_init(void) /* print 2nd bootloader banner */ bootloader_print_banner(); - /* TODO: fix the flash init for all scenarios */ -#ifdef CONFIG_ESP_SIMPLE_BOOT - esp_mspi_pin_init(); +#ifndef CONFIG_BOOTLOADER_MCUBOOT spi_flash_init_chip_state(); - if ((ret = esp_flash_init_default_chip()) != ESP_OK) { - ESP_EARLY_LOGE(TAG, "esp_flash_init_default_chip err=%x\n", ret); return ret; } #endif diff --git a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c index 9de2a8d5c3..05bd088d56 100644 --- a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c +++ b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c @@ -147,7 +147,8 @@ esp_err_t bootloader_init(void) /* print 2nd bootloader banner */ bootloader_print_banner(); -#ifdef CONFIG_ESP_SIMPLE_BOOT +#ifndef CONFIG_BOOTLOADER_MCUBOOT + spi_flash_init_chip_state(); if ((ret = esp_flash_init_default_chip()) != ESP_OK) { return ret; } diff --git a/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c b/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c index 104f5a3413..9d45be3104 100644 --- a/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c +++ b/components/bootloader_support/src/esp32s3/bootloader_esp32s3.c @@ -181,8 +181,11 @@ esp_err_t bootloader_init(void) /* print 2nd bootloader banner */ bootloader_print_banner(); -#ifdef CONFIG_ESP_SIMPLE_BOOT - esp_flash_init_default_chip(); +#ifndef CONFIG_BOOTLOADER_MCUBOOT + spi_flash_init_chip_state(); + if ((ret = esp_flash_init_default_chip()) != ESP_OK) { + return ret; + } #endif #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP @@ -191,8 +194,7 @@ esp_err_t bootloader_init(void) //init mmu mmu_hal_init(); // update flash ID - /* disabled it to enable octal support */ - // bootloader_flash_update_id(); + bootloader_flash_update_id(); // Check and run XMC startup flow if ((ret = bootloader_flash_xmc_startup()) != ESP_OK) { ESP_EARLY_LOGE(TAG, "failed when running XMC startup flow, reboot!"); diff --git a/components/driver/include/esp_private/spi_common_internal.h b/components/driver/include/esp_private/spi_common_internal.h index 2653df4bd1..0881b2bd16 100644 --- a/components/driver/include/esp_private/spi_common_internal.h +++ b/components/driver/include/esp_private/spi_common_internal.h @@ -10,7 +10,6 @@ #include -#include #include "driver/spi_common.h" #include "hal/spi_types.h" #include "esp_pm.h" diff --git a/components/spi_flash/spi_flash_os_func_app.c b/components/spi_flash/spi_flash_os_func_app.c index a12388f44a..07cc255098 100644 --- a/components/spi_flash/spi_flash_os_func_app.c +++ b/components/spi_flash/spi_flash_os_func_app.c @@ -76,16 +76,13 @@ static IRAM_ATTR esp_err_t spi1_start(void *arg) esp_err_t ret = ESP_OK; /** * There are three ways for ESP Flash API lock: - * 1. spi bus lock, this is used when SPI1 is shared with GPSPI Master Driver + * 1. spi bus lock, this is used when SPI1 is shared with GPSPI Master Driver (not yet supported in Zephyr) * 2. mutex, this is used when the Cache isn't need to be disabled. * 3. cache lock (from cache_utils.h), this is used when we need to disable Cache to avoid access from SPI0 * * From 1 to 3, the lock efficiency decreases. */ -#if CONFIG_SPI_FLASH_SHARE_SPI1_BUS - //use the lock to disable the cache and interrupts before using the SPI bus - ret = acquire_spi_bus_lock(arg); -#elif SPI_FLASH_CACHE_NO_DISABLE +#if SPI_FLASH_CACHE_NO_DISABLE k_mutex_lock(&s_spi1_flash_mutex, K_FOREVER); #else //directly disable the cache and interrupts when lock is not used @@ -102,9 +99,7 @@ static IRAM_ATTR esp_err_t spi1_end(void *arg) /** * There are three ways for ESP Flash API lock, see `spi1_start` */ -#if CONFIG_SPI_FLASH_SHARE_SPI1_BUS - ret = release_spi_bus_lock(arg); -#elif SPI_FLASH_CACHE_NO_DISABLE +#if SPI_FLASH_CACHE_NO_DISABLE k_mutex_unlock(&s_spi1_flash_mutex); #else cache_enable(NULL); @@ -210,11 +205,8 @@ static bool use_bus_lock(int host_id) if (host_id != SPI1_HOST) { return true; } -#if CONFIG_SPI_FLASH_SHARE_SPI1_BUS - return true; -#else + return false; -#endif } // This function is only called by users usually via `spi_bus_add_flash_device` to initialise os functions. diff --git a/zephyr/esp32/CMakeLists.txt b/zephyr/esp32/CMakeLists.txt index 632b185860..89c6c6ab5b 100644 --- a/zephyr/esp32/CMakeLists.txt +++ b/zephyr/esp32/CMakeLists.txt @@ -147,6 +147,7 @@ if(CONFIG_SOC_SERIES_ESP32) ../../components/spi_flash/spi_flash_chip_th.c ../../components/spi_flash/spi_flash_chip_winbond.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c ) endif() diff --git a/zephyr/esp32c2/CMakeLists.txt b/zephyr/esp32c2/CMakeLists.txt index 9299a847fd..4945ff6555 100644 --- a/zephyr/esp32c2/CMakeLists.txt +++ b/zephyr/esp32c2/CMakeLists.txt @@ -150,6 +150,7 @@ if(CONFIG_SOC_SERIES_ESP32C2) ../../components/spi_flash/spi_flash_chip_th.c ../../components/spi_flash/spi_flash_chip_winbond.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c ) endif() diff --git a/zephyr/esp32c3/CMakeLists.txt b/zephyr/esp32c3/CMakeLists.txt index a4d901ce9c..3f58eb24be 100644 --- a/zephyr/esp32c3/CMakeLists.txt +++ b/zephyr/esp32c3/CMakeLists.txt @@ -154,6 +154,7 @@ if(CONFIG_SOC_SERIES_ESP32C3) ../../components/spi_flash/spi_flash_chip_th.c ../../components/spi_flash/spi_flash_chip_winbond.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c ) endif() diff --git a/zephyr/esp32c6/CMakeLists.txt b/zephyr/esp32c6/CMakeLists.txt index d7414accb3..7a02e57e3e 100644 --- a/zephyr/esp32c6/CMakeLists.txt +++ b/zephyr/esp32c6/CMakeLists.txt @@ -129,6 +129,7 @@ if(CONFIG_SOC_SERIES_ESP32C6) ../../components/spi_flash/spi_flash_chip_th.c ../../components/spi_flash/spi_flash_chip_winbond.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c ) endif() diff --git a/zephyr/esp32s2/CMakeLists.txt b/zephyr/esp32s2/CMakeLists.txt index ca64c9c856..1da7ccd670 100644 --- a/zephyr/esp32s2/CMakeLists.txt +++ b/zephyr/esp32s2/CMakeLists.txt @@ -127,6 +127,7 @@ if(CONFIG_SOC_SERIES_ESP32S2) ../../components/spi_flash/flash_mmap.c ../../components/spi_flash/flash_ops.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c ../../components/spi_flash/memspi_host_driver.c ../../components/spi_flash/spi_flash_chip_boya.c ../../components/spi_flash/spi_flash_chip_drivers.c diff --git a/zephyr/esp32s3/CMakeLists.txt b/zephyr/esp32s3/CMakeLists.txt index f550105032..86dade6ff6 100644 --- a/zephyr/esp32s3/CMakeLists.txt +++ b/zephyr/esp32s3/CMakeLists.txt @@ -147,6 +147,7 @@ if(CONFIG_SOC_SERIES_ESP32S3) ../../components/spi_flash/spi_flash_chip_winbond.c ../../components/spi_flash/spi_flash_hpm_enable.c ../../components/spi_flash/spi_flash_os_func_noos.c + ../../components/spi_flash/spi_flash_os_func_app.c # todo: ../../components/spi_flash/spi_flash_wrap.c ) endif()