From 9fe9b83ab9657201c850d3bf2ac8ae4fa642504c Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Mon, 9 Mar 2026 16:13:35 +0800 Subject: [PATCH] fix(esp_rom): sync spi_flash_disable/enable_cache patches to rom implementation --- components/esp_rom/esp32c2/ld/esp32c2.rom.ld | 4 +-- components/esp_rom/esp32c3/ld/esp32c3.rom.ld | 4 +-- .../esp32c5/ld/esp32c5.rom.spiflash.ld | 4 +-- .../esp32c6/ld/esp32c6.rom.spiflash.ld | 4 +-- .../esp32c61/ld/esp32c61.rom.spiflash.ld | 4 +-- .../esp32h2/ld/esp32h2.rom.spiflash.ld | 4 +-- .../esp32h21/ld/esp32h21.rom.spiflash.ld | 4 +-- .../esp32h4/ld/esp32h4.rom.spiflash.ld | 4 +-- components/esp_rom/esp32s3/ld/esp32s3.rom.ld | 4 +-- components/esp_rom/patches/esp_rom_spiflash.c | 27 ++++++++++++++++++- components/spi_flash/cache_utils.c | 2 ++ 11 files changed, 46 insertions(+), 19 deletions(-) diff --git a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld index ad5dcf6b89c..857dcb2b0ef 100644 --- a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld +++ b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld @@ -139,8 +139,8 @@ PROVIDE( spi_flash_check_and_flush_cache = 0x400001a4 ); PROVIDE( spi_flash_mmap_get_free_pages = 0x400001a8 ); PROVIDE( spi_flash_cache2phys = 0x400001ac ); PROVIDE( spi_flash_phys2cache = 0x400001b0 ); -PROVIDE( spi_flash_disable_cache = 0x400001b4 ); -PROVIDE( spi_flash_restore_cache = 0x400001b8 ); +PROVIDE( rom_spi_flash_disable_cache = 0x400001b4 ); +PROVIDE( rom_spi_flash_restore_cache = 0x400001b8 ); PROVIDE( spi_flash_cache_enabled = 0x400001bc ); PROVIDE( spi_flash_enable_cache = 0x400001c0 ); PROVIDE( spi_cache_mode_switch = 0x400001c4 ); diff --git a/components/esp_rom/esp32c3/ld/esp32c3.rom.ld b/components/esp_rom/esp32c3/ld/esp32c3.rom.ld index 133dca5af56..f8379d1cb8d 100644 --- a/components/esp_rom/esp32c3/ld/esp32c3.rom.ld +++ b/components/esp_rom/esp32c3/ld/esp32c3.rom.ld @@ -144,8 +144,8 @@ PROVIDE( spi_flash_check_and_flush_cache = 0x400001b4 ); PROVIDE( spi_flash_mmap_get_free_pages = 0x400001b8 ); PROVIDE( spi_flash_cache2phys = 0x400001bc ); PROVIDE( spi_flash_phys2cache = 0x400001c0 ); -PROVIDE( spi_flash_disable_cache = 0x400001c4 ); -PROVIDE( spi_flash_restore_cache = 0x400001c8 ); +PROVIDE( rom_spi_flash_disable_cache = 0x400001c4 ); +PROVIDE( rom_spi_flash_restore_cache = 0x400001c8 ); PROVIDE( spi_flash_cache_enabled = 0x400001cc ); PROVIDE( spi_flash_enable_cache = 0x400001d0 ); PROVIDE( spi_cache_mode_switch = 0x400001d4 ); diff --git a/components/esp_rom/esp32c5/ld/esp32c5.rom.spiflash.ld b/components/esp_rom/esp32c5/ld/esp32c5.rom.spiflash.ld index ab55a4292f5..40c6dfbe71c 100644 --- a/components/esp_rom/esp32c5/ld/esp32c5.rom.spiflash.ld +++ b/components/esp_rom/esp32c5/ld/esp32c5.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x40000204; -spi_flash_restore_cache = 0x40000208; +rom_spi_flash_disable_cache = 0x40000204; +rom_spi_flash_restore_cache = 0x40000208; spi_flash_cache_enabled = 0x4000020c; spi_flash_enable_cache = 0x40000210; esp_enable_cache_flash_wrap = 0x40000214; diff --git a/components/esp_rom/esp32c6/ld/esp32c6.rom.spiflash.ld b/components/esp_rom/esp32c6/ld/esp32c6.rom.spiflash.ld index 4954c044b79..aebbae567f6 100644 --- a/components/esp_rom/esp32c6/ld/esp32c6.rom.spiflash.ld +++ b/components/esp_rom/esp32c6/ld/esp32c6.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x400001f0; -spi_flash_restore_cache = 0x400001f4; +rom_spi_flash_disable_cache = 0x400001f0; +rom_spi_flash_restore_cache = 0x400001f4; spi_flash_cache_enabled = 0x400001f8; esp_enable_cache_flash_wrap = 0x40000200; diff --git a/components/esp_rom/esp32c61/ld/esp32c61.rom.spiflash.ld b/components/esp_rom/esp32c61/ld/esp32c61.rom.spiflash.ld index d0b9e410d3c..ddddba14c55 100644 --- a/components/esp_rom/esp32c61/ld/esp32c61.rom.spiflash.ld +++ b/components/esp_rom/esp32c61/ld/esp32c61.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x40000204; -spi_flash_restore_cache = 0x40000208; +rom_spi_flash_disable_cache = 0x40000204; +rom_spi_flash_restore_cache = 0x40000208; spi_flash_cache_enabled = 0x4000020c; spi_flash_enable_cache = 0x40000210; esp_enable_cache_flash_wrap = 0x40000214; diff --git a/components/esp_rom/esp32h2/ld/esp32h2.rom.spiflash.ld b/components/esp_rom/esp32h2/ld/esp32h2.rom.spiflash.ld index 94481c09cd5..8b37078b6e1 100644 --- a/components/esp_rom/esp32h2/ld/esp32h2.rom.spiflash.ld +++ b/components/esp_rom/esp32h2/ld/esp32h2.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x400001e8; -spi_flash_restore_cache = 0x400001ec; +rom_spi_flash_disable_cache = 0x400001e8; +rom_spi_flash_restore_cache = 0x400001ec; spi_flash_cache_enabled = 0x400001f0; esp_enable_cache_flash_wrap = 0x400001f8; diff --git a/components/esp_rom/esp32h21/ld/esp32h21.rom.spiflash.ld b/components/esp_rom/esp32h21/ld/esp32h21.rom.spiflash.ld index d430fabd771..dbfbfac1a45 100644 --- a/components/esp_rom/esp32h21/ld/esp32h21.rom.spiflash.ld +++ b/components/esp_rom/esp32h21/ld/esp32h21.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x400001e8; -spi_flash_restore_cache = 0x400001ec; +rom_spi_flash_disable_cache = 0x400001e8; +rom_spi_flash_restore_cache = 0x400001ec; spi_flash_cache_enabled = 0x400001f0; spi_flash_enable_cache = 0x400001f4; esp_enable_cache_flash_wrap = 0x400001f8; diff --git a/components/esp_rom/esp32h4/ld/esp32h4.rom.spiflash.ld b/components/esp_rom/esp32h4/ld/esp32h4.rom.spiflash.ld index 68d4edec420..2042ffd1b6a 100644 --- a/components/esp_rom/esp32h4/ld/esp32h4.rom.spiflash.ld +++ b/components/esp_rom/esp32h4/ld/esp32h4.rom.spiflash.ld @@ -18,8 +18,8 @@ ***************************************/ /* Functions */ -spi_flash_disable_cache = 0x400001f8; -spi_flash_restore_cache = 0x400001fc; +rom_spi_flash_disable_cache = 0x400001f8; +rom_spi_flash_restore_cache = 0x400001fc; spi_flash_cache_enabled = 0x40000200; spi_flash_enable_cache = 0x40000204; esp_enable_cache_flash_wrap = 0x40000208; diff --git a/components/esp_rom/esp32s3/ld/esp32s3.rom.ld b/components/esp_rom/esp32s3/ld/esp32s3.rom.ld index 29efd9036fd..e76d0b3b5f1 100644 --- a/components/esp_rom/esp32s3/ld/esp32s3.rom.ld +++ b/components/esp_rom/esp32s3/ld/esp32s3.rom.ld @@ -196,8 +196,8 @@ PROVIDE( spi_flash_check_and_flush_cache = 0x40000bdc ); PROVIDE( spi_flash_mmap_get_free_pages = 0x40000be8 ); PROVIDE( spi_flash_cache2phys = 0x40000bf4 ); /* patched when XIP */ PROVIDE( spi_flash_phys2cache = 0x40000c00 ); /* patched when XIP */ -PROVIDE( spi_flash_disable_cache = 0x40000c0c ); -PROVIDE( spi_flash_restore_cache = 0x40000c18 ); +PROVIDE( rom_spi_flash_disable_cache = 0x40000c0c ); +PROVIDE( rom_spi_flash_restore_cache = 0x40000c18 ); PROVIDE( spi_flash_cache_enabled = 0x40000c24 ); PROVIDE( spi_flash_enable_cache = 0x40000c30 ); PROVIDE( spi_cache_mode_switch = 0x40000c3c ); diff --git a/components/esp_rom/patches/esp_rom_spiflash.c b/components/esp_rom/patches/esp_rom_spiflash.c index b811f061a91..005b44ba630 100644 --- a/components/esp_rom/patches/esp_rom_spiflash.c +++ b/components/esp_rom/patches/esp_rom_spiflash.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -22,6 +22,10 @@ #include "esp32c5/rom/opi_flash.h" #endif +#if SOC_BRANCH_PREDICTOR_SUPPORTED +#include "riscv/rv_utils.h" +#endif + #define SPI_IDX 1 #if CONFIG_IDF_TARGET_ESP32 @@ -803,3 +807,24 @@ void esp_rom_opiflash_cache_mode_config(esp_rom_spiflash_read_mode_t mode, const #endif // IDF_TARGET #endif // CONFIG_SPI_FLASH_ROM_DRIVER_PATCH + +#if CONFIG_SPI_FLASH_ROM_IMPL +extern void rom_spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state); +void spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state) +{ +#if SOC_BRANCH_PREDICTOR_SUPPORTED + //branch predictor will start cache request as well + rv_utils_dis_branch_predictor(); +#endif + rom_spi_flash_disable_cache(cpuid, saved_state); +} + +extern void rom_spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state); +void spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state) +{ + rom_spi_flash_restore_cache(cpuid, saved_state); +#if SOC_BRANCH_PREDICTOR_SUPPORTED + rv_utils_en_branch_predictor(); +#endif +} +#endif diff --git a/components/spi_flash/cache_utils.c b/components/spi_flash/cache_utils.c index 66fe47fab10..dc6e9168eb4 100644 --- a/components/spi_flash/cache_utils.c +++ b/components/spi_flash/cache_utils.c @@ -337,6 +337,7 @@ void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid) #endif } +#if !CONFIG_SPI_FLASH_ROM_IMPL void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state) { #if SOC_BRANCH_PREDICTOR_SUPPORTED @@ -358,6 +359,7 @@ bool IRAM_ATTR spi_flash_cache_enabled(void) { return cache_hal_is_cache_enabled(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL); } +#endif #if CONFIG_IDF_TARGET_ESP32S2 IRAM_ATTR void esp_config_instruction_cache_mode(void)