Skip to content

Commit

Permalink
spi_flash: initialize flash during bootloader
Browse files Browse the repository at this point in the history
This makes sure flash is initialized during bootloader
process in order to get chip information.

Signed-off-by: Sylvio Alves <[email protected]>
  • Loading branch information
sylvioalves authored and marekmatej committed Sep 13, 2024
1 parent 8d6116a commit aa6a967
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 37 deletions.
13 changes: 7 additions & 6 deletions components/bootloader_support/src/esp32/bootloader_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions components/bootloader_support/src/esp32s3/bootloader_esp32s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <zephyr/kernel.h>

#include <esp_intr_alloc.h>
#include "driver/spi_common.h"
#include "hal/spi_types.h"
#include "esp_pm.h"
Expand Down
16 changes: 4 additions & 12 deletions components/spi_flash/spi_flash_os_func_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32c2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32c3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32c6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32s2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions zephyr/esp32s3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit aa6a967

Please sign in to comment.