diff --git a/components/esp_hal_mspi/esp32c2/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32c2/include/hal/gpspi_flash_ll.h index 56dd2431ca7..d81c10fb21f 100644 --- a/components/esp_hal_mspi/esp32c2/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/esp32c2/include/hal/gpspi_flash_ll.h @@ -29,7 +29,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/esp_hal_mspi/esp32c3/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32c3/include/hal/gpspi_flash_ll.h index 95fa20b9296..417848a0dc3 100644 --- a/components/esp_hal_mspi/esp32c3/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32c5/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32c5/include/hal/gpspi_flash_ll.h index 503d3417ec8..2245972d0e6 100644 --- a/components/esp_hal_mspi/esp32c5/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32c6/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32c6/include/hal/gpspi_flash_ll.h index 08209651bc1..f9bff2ade2c 100644 --- a/components/esp_hal_mspi/esp32c6/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32c61/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32c61/include/hal/gpspi_flash_ll.h index 3d3fb019c59..80782bc2984 100644 --- a/components/esp_hal_mspi/esp32c61/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32h2/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32h2/include/hal/gpspi_flash_ll.h index 136b12aebf6..07b9a1ab027 100644 --- a/components/esp_hal_mspi/esp32h2/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32h4/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32h4/include/hal/gpspi_flash_ll.h index bffcc612d43..718a4e7d5ac 100644 --- a/components/esp_hal_mspi/esp32h4/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32p4/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32p4/include/hal/gpspi_flash_ll.h index a4db74e33c8..021edd197bd 100644 --- a/components/esp_hal_mspi/esp32p4/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/esp_hal_mspi/esp32s2/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32s2/include/hal/gpspi_flash_ll.h index 834faeb0fe6..c9e1a2c9837 100644 --- a/components/esp_hal_mspi/esp32s2/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/esp32s2/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/esp_hal_mspi/esp32s3/include/hal/gpspi_flash_ll.h b/components/esp_hal_mspi/esp32s3/include/hal/gpspi_flash_ll.h index 71e3cfe1036..e4f57357167 100644 --- a/components/esp_hal_mspi/esp32s3/include/hal/gpspi_flash_ll.h +++ b/components/esp_hal_mspi/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/soc/esp32h4/include/soc/clk_tree_defs.h b/components/soc/esp32h4/include/soc/clk_tree_defs.h index 6eb3af27919..03406804cec 100644 --- a/components/soc/esp32h4/include/soc/clk_tree_defs.h +++ b/components/soc/esp32h4/include/soc/clk_tree_defs.h @@ -251,7 +251,7 @@ typedef enum { * @brief Type of SPI clock source. */ typedef enum { - SPI_CLK_SRC_DEFAULT = SOC_MOD_CLK_PLL_F48M, /*!< Select XTAL as SPI source clock */ + SPI_CLK_SRC_DEFAULT = SOC_MOD_CLK_PLL_F48M, /*!< Select PLL_F48M as SPI source clock */ SPI_CLK_SRC_XTAL = SOC_MOD_CLK_XTAL, /*!< Select XTAL as SPI source clock */ SPI_CLK_SRC_PLL_F48M = SOC_MOD_CLK_PLL_F48M, /*!< Select PLL_48M as SPI source clock */ SPI_CLK_SRC_RC_FAST = SOC_MOD_CLK_RC_FAST, /*!< Select RC_FAST as SPI source clock */ diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index f66a82ab03c..22db660c7ff 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -56,6 +56,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 @@ -262,13 +264,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; } @@ -306,9 +308,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 d32ee9649b7..1d50dc31893 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,11 +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[4] = {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[4] = {6, 12, 24, 48}; +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)