From ebb65b27db24f9478c71788f3668bafcde57ffc6 Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Fri, 22 Dec 2023 13:45:26 +0800 Subject: [PATCH] bugfix(spi_flash): Fix build error when octal flash is enabled, Closes https://github.com/espressif/esp-idf/issues/12850 --- .../include/bootloader_flash.h | 17 +++++++ .../bootloader_support/src/bootloader_flash.c | 46 ++++++++++++++++++- components/spi_flash/spi_flash_hpm_enable.c | 2 + 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/components/bootloader_support/include/bootloader_flash.h b/components/bootloader_support/include/bootloader_flash.h index 90865e234d6..56ffdcb03b2 100644 --- a/components/bootloader_support/include/bootloader_flash.h +++ b/components/bootloader_support/include/bootloader_flash.h @@ -9,6 +9,17 @@ #include /* including in bootloader for error values */ #include "sdkconfig.h" #include "soc/soc_caps.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32C3 +#include "esp32c3/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32H2 +#include "esp32h2/rom/spi_flash.h" +#endif #include "bootloader_flash_override.h" #ifdef __cplusplus @@ -48,6 +59,12 @@ esp_err_t bootloader_flash_xmc_startup(void); */ esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void); +/** + * @brief Get the spi flash working mode. + * + * @return The mode of flash working mode, see `esp_rom_spiflash_read_mode_t` + */ +esp_rom_spiflash_read_mode_t bootloader_flash_get_spi_mode(void); #ifdef __cplusplus } diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/bootloader_flash.c index 98cd8221650..e73d77eb6ce 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/bootloader_flash.c @@ -145,6 +145,11 @@ esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size) #include "esp32h2/rom/cache.h" #include "soc/cache_memory.h" #endif + +#if CONFIG_IDF_TARGET_ESP32S3 +#include "esp32s3/rom/opi_flash.h" +#endif + static const char *TAG = "bootloader_flash"; #if CONFIG_IDF_TARGET_ESP32 @@ -503,9 +508,9 @@ void bootloader_flash_32bits_address_map_enable(esp_rom_spiflash_read_mode_t fla assert(false); break; } - cache_hal_disable(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + uint32_t autoload = Cache_Suspend_DCache(); esp_rom_opiflash_cache_mode_config(flash_mode, &cache_rd); - cache_hal_enable(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); + Cache_Resume_DCache(autoload); } #endif @@ -844,3 +849,40 @@ esp_err_t IRAM_ATTR bootloader_flash_xmc_startup(void) } #endif //XMC_SUPPORT + +esp_rom_spiflash_read_mode_t bootloader_flash_get_spi_mode(void) +{ + esp_rom_spiflash_read_mode_t spi_mode = ESP_ROM_SPIFLASH_FASTRD_MODE; +#if CONFIG_IDF_TARGET_ESP32 + uint32_t spi_ctrl = REG_READ(SPI_CTRL_REG(0)); + if (spi_ctrl & SPI_FREAD_QIO) { + spi_mode = ESP_ROM_SPIFLASH_QIO_MODE; + } else if (spi_ctrl & SPI_FREAD_QUAD) { + spi_mode = ESP_ROM_SPIFLASH_QOUT_MODE; + } else if (spi_ctrl & SPI_FREAD_DIO) { + spi_mode = ESP_ROM_SPIFLASH_DIO_MODE; + } else if (spi_ctrl & SPI_FREAD_DUAL) { + spi_mode = ESP_ROM_SPIFLASH_DOUT_MODE; + } else if (spi_ctrl & SPI_FASTRD_MODE) { + spi_mode = ESP_ROM_SPIFLASH_FASTRD_MODE; + } else { + spi_mode = ESP_ROM_SPIFLASH_SLOWRD_MODE; + } +#else + uint32_t spi_ctrl = REG_READ(SPI_MEM_CTRL_REG(0)); + if (spi_ctrl & SPI_MEM_FREAD_QIO) { + spi_mode = ESP_ROM_SPIFLASH_QIO_MODE; + } else if (spi_ctrl & SPI_MEM_FREAD_QUAD) { + spi_mode = ESP_ROM_SPIFLASH_QOUT_MODE; + } else if (spi_ctrl & SPI_MEM_FREAD_DIO) { + spi_mode = ESP_ROM_SPIFLASH_DIO_MODE; + } else if (spi_ctrl & SPI_MEM_FREAD_DUAL) { + spi_mode = ESP_ROM_SPIFLASH_DOUT_MODE; + } else if (spi_ctrl & SPI_MEM_FASTRD_MODE) { + spi_mode = ESP_ROM_SPIFLASH_FASTRD_MODE; + } else { + spi_mode = ESP_ROM_SPIFLASH_SLOWRD_MODE; + } +#endif + return spi_mode; +} diff --git a/components/spi_flash/spi_flash_hpm_enable.c b/components/spi_flash/spi_flash_hpm_enable.c index 5d11f532b78..d8f81a2e056 100644 --- a/components/spi_flash/spi_flash_hpm_enable.c +++ b/components/spi_flash/spi_flash_hpm_enable.c @@ -10,7 +10,9 @@ #include "esp_log.h" #include "spi_flash_defs.h" #include "esp_rom_sys.h" +#if CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/rom/spi_flash.h" +#endif #include "spi_flash_override.h" // TODO: These dependencies will be removed after remove bootloader_flash to G0.IDF-4609