diff --git a/components/hal/esp32c2/include/hal/gpspi_flash_ll.h b/components/hal/esp32c2/include/hal/gpspi_flash_ll.h index 7b63e6fb925..96974a9ea67 100644 --- a/components/hal/esp32c2/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32c2/include/hal/gpspi_flash_ll.h @@ -28,7 +28,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (40) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32c3/include/hal/gpspi_flash_ll.h b/components/hal/esp32c3/include/hal/gpspi_flash_ll.h index 28285336fe5..bfbce6e99d3 100644 --- a/components/hal/esp32c3/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32c3/include/hal/gpspi_flash_ll.h @@ -30,7 +30,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32c5/include/hal/gpspi_flash_ll.h b/components/hal/esp32c5/include/hal/gpspi_flash_ll.h index 67e588da2ad..c9e9b10bd06 100644 --- a/components/hal/esp32c5/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32c5/include/hal/gpspi_flash_ll.h @@ -32,7 +32,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) #define GPSPI_FLASH_LL_SUPPORT_CLK_SRC_PRE_DIV (1) #define GPSPI_FLASH_LL_PERIPH_CLK_DIV_MAX ((SPI_CLKCNT_N + 1) * (SPI_CLKDIV_PRE + 1)) //peripheral internal maxmum clock divider diff --git a/components/hal/esp32c6/include/hal/gpspi_flash_ll.h b/components/hal/esp32c6/include/hal/gpspi_flash_ll.h index db096c7f184..04641f98ec0 100644 --- a/components/hal/esp32c6/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32c6/include/hal/gpspi_flash_ll.h @@ -31,7 +31,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32c61/include/hal/gpspi_flash_ll.h b/components/hal/esp32c61/include/hal/gpspi_flash_ll.h index 6a3ce7894d7..5e47b18826b 100644 --- a/components/hal/esp32c61/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32c61/include/hal/gpspi_flash_ll.h @@ -31,7 +31,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32h2/include/hal/gpspi_flash_ll.h b/components/hal/esp32h2/include/hal/gpspi_flash_ll.h index 8c101b3ee9f..97c4c30e351 100644 --- a/components/hal/esp32h2/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32h2/include/hal/gpspi_flash_ll.h @@ -31,7 +31,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32h21/include/hal/gpspi_flash_ll.h b/components/hal/esp32h21/include/hal/gpspi_flash_ll.h index 49424e110d5..15c84fe92b5 100644 --- a/components/hal/esp32h21/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32h21/include/hal/gpspi_flash_ll.h @@ -31,7 +31,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (32) // (register default)0: XTAL_CLK /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32h4/include/hal/gpspi_flash_ll.h b/components/hal/esp32h4/include/hal/gpspi_flash_ll.h index 82f3597f48d..5cb46379160 100644 --- a/components/hal/esp32h4/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32h4/include/hal/gpspi_flash_ll.h @@ -33,7 +33,6 @@ extern "C" { #define gpspi_flash_ll_hw_get_id(dev) ( ((dev) == (void*)&GPSPI2) ? SPI2_HOST : -1 ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) /*------------------------------------------------------------------------------ * Control diff --git a/components/hal/esp32p4/include/hal/gpspi_flash_ll.h b/components/hal/esp32p4/include/hal/gpspi_flash_ll.h index 4ada4c24ed8..403c1ba1212 100644 --- a/components/hal/esp32p4/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32p4/include/hal/gpspi_flash_ll.h @@ -38,7 +38,6 @@ extern "C" { )) ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ (80) #define GPSPI_FLASH_LL_SUPPORT_CLK_SRC_PRE_DIV (1) #define GPSPI_FLASH_LL_PERIPH_CLK_DIV_MAX ((SPI_CLKCNT_N + 1) * (SPI_CLKDIV_PRE + 1)) //peripheral internal maxmum clock divider diff --git a/components/hal/esp32s3/include/hal/gpspi_flash_ll.h b/components/hal/esp32s3/include/hal/gpspi_flash_ll.h index c5a44f124c5..a41742c978f 100644 --- a/components/hal/esp32s3/include/hal/gpspi_flash_ll.h +++ b/components/hal/esp32s3/include/hal/gpspi_flash_ll.h @@ -36,7 +36,6 @@ extern "C" { )) ) typedef typeof(GPSPI2.clock.val) gpspi_flash_ll_clock_reg_t; -#define GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ 80 /*------------------------------------------------------------------------------ * Control diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index c2785110b2f..dcf3e4f961c 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.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 */ @@ -48,6 +48,8 @@ __attribute__((unused)) static const char TAG[] = "spi_flash"; esp_flash_t *esp_flash_default_chip = NULL; #endif +#define ESP_FLASH_GPSPI_PERIPH_SRC_FREQ_MAX (80*1000*1000) //peripheral hardware limitation for clock source into peripheral + #if defined CONFIG_ESPTOOLPY_FLASHFREQ_120M #define DEFAULT_FLASH_SPEED 120 #elif defined CONFIG_ESPTOOLPY_FLASHFREQ_80M @@ -254,13 +256,13 @@ static esp_err_t acquire_spi_device(const esp_flash_spi_device_config_t *config, #if GPSPI_FLASH_LL_SUPPORT_CLK_SRC_PRE_DIV static uint32_t s_spi_find_clock_src_pre_div(uint32_t src_freq, uint32_t target_freq) { - // pre division must be even and at least 2 - uint32_t min_div = ((src_freq / GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ) + 1) & (~0x01UL); - min_div = min_div < 2 ? 2 : min_div; + // no timing tuning, no need pre division to be even + uint32_t min_div = (src_freq / ESP_FLASH_GPSPI_PERIPH_SRC_FREQ_MAX); + min_div = min_div < 1 ? 1 : min_div; uint32_t total_div = src_freq / target_freq; // Loop the `div` to find a divisible value of `total_div` - for (uint32_t pre_div = min_div; pre_div <= total_div; pre_div += 2) { + for (uint32_t pre_div = min_div; pre_div <= total_div; pre_div += 1) { if ((total_div % pre_div) || (total_div / pre_div) > GPSPI_FLASH_LL_PERIPH_CLK_DIV_MAX) { continue; } @@ -304,9 +306,9 @@ static uint32_t init_gpspi_clock(esp_flash_t *chip, const esp_flash_spi_device_c // Calculate final clock source frequency uint32_t final_freq_mhz; #if GPSPI_FLASH_LL_SUPPORT_CLK_SRC_PRE_DIV - uint32_t pre_div = s_spi_find_clock_src_pre_div(clk_src_freq, GPSPI_FLASH_LL_PERIPHERAL_FREQUENCY_MHZ * 1000 * 1000); - gpspi_flash_ll_clk_source_pre_div(spi_flash_ll_get_hw(config->host_id), pre_div / 2, 2); - final_freq_mhz = clk_src_freq / (pre_div); + uint32_t pre_div = s_spi_find_clock_src_pre_div(clk_src_freq, config->freq_mhz * 1000 * 1000); + gpspi_flash_ll_clk_source_pre_div(spi_flash_ll_get_hw(config->host_id), pre_div, 1); + final_freq_mhz = clk_src_freq / (1000 * 1000) / pre_div; #else final_freq_mhz = clk_src_freq / (1 * 1000 * 1000); #endif diff --git a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c index d887ab89fea..893281b62be 100644 --- a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c +++ b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -597,9 +597,11 @@ TEST_CASE_MULTI_FLASH_IGNORE("Test esp_flash_write can toggle QE bit", test_togg // This table could be chip specific in the future. #if CONFIG_IDF_TARGET_ESP32C2 -uint8_t flash_frequency_table[5] = {5, 10, 20, 40}; +uint8_t flash_frequency_table[] = {5, 10, 20, 40}; +#elif CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32H21 || CONFIG_IDF_TARGET_ESP32H4 +uint8_t flash_frequency_table[] = {8, 16, 24, 48}; #else -uint8_t flash_frequency_table[6] = {5, 10, 20, 26, 40, 80}; +uint8_t flash_frequency_table[] = {5, 10, 20, 26, 40, 80}; #endif #define TEST_FLASH_SPEED_MIN 5 void test_permutations_part(const flashtest_config_t* config, esp_partition_t* part, void* source_buf, size_t length)