mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
Merge branch 'feat/c61_psram_timing_tuning' into 'master'
psram: psram timing tuning support on c61 Closes IDF-9256 See merge request espressif/esp-idf!40844
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
target_include_directories(${COMPONENT_LIB} PUBLIC . include)
|
||||
|
||||
set(srcs)
|
||||
|
||||
if(NOT BOOTLOADER_BUILD)
|
||||
if(NOT CONFIG_APP_BUILD_TYPE_PURE_RAM_APP)
|
||||
list(APPEND srcs "mspi_timing_config.c")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
target_sources(${COMPONENT_LIB} PRIVATE ${srcs})
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_types.h"
|
||||
#include "esp_log.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "esp_private/periph_ctrl.h"
|
||||
#include "esp_private/mspi_timing_config.h"
|
||||
#include "mspi_timing_tuning_configs.h"
|
||||
#include "hal/psram_ctrlr_ll.h"
|
||||
#include "hal/mspi_ll.h"
|
||||
|
||||
#define FLASH_LOW_SPEED_CORE_CLOCK_MHZ MSPI_TIMING_LL_CORE_CLOCK_MHZ_DEFAULT
|
||||
#define FLASH_HIGH_SPEED_CORE_CLOCK_MHZ MSPI_TIMING_CORE_CLOCK_MHZ
|
||||
#define PSRAM_LOW_SPEED_CORE_CLOCK_MHZ MSPI_TIMING_LL_CORE_CLOCK_MHZ_DEFAULT
|
||||
#define PSRAM_HIGH_SPEED_CORE_CLOCK_MHZ MSPI_TIMING_CORE_CLOCK_MHZ
|
||||
|
||||
const static char *TAG = "MSPI Timing";
|
||||
|
||||
//-------------------------------------MSPI Clock Setting-------------------------------------//
|
||||
static void s_mspi_flash_set_core_clock(uint8_t mspi_id, uint32_t core_clock_mhz)
|
||||
{
|
||||
ESP_EARLY_LOGV(TAG, "flash core clock: %d", core_clock_mhz);
|
||||
mspi_timing_ll_set_core_clock(mspi_id, core_clock_mhz);
|
||||
}
|
||||
|
||||
static void s_mspi_psram_set_core_clock(uint8_t mspi_id, uint32_t core_clock_mhz)
|
||||
{
|
||||
ESP_EARLY_LOGV(TAG, "psram core clock: %d", core_clock_mhz);
|
||||
mspi_timing_ll_set_core_clock(mspi_id, core_clock_mhz);
|
||||
}
|
||||
|
||||
void mspi_timing_config_set_flash_clock(uint32_t flash_freq_mhz, mspi_timing_speed_mode_t speed_mode, bool control_both_mspi)
|
||||
{
|
||||
uint32_t core_clock_mhz = 0;
|
||||
if (speed_mode == MSPI_TIMING_SPEED_MODE_LOW_PERF) {
|
||||
core_clock_mhz = FLASH_LOW_SPEED_CORE_CLOCK_MHZ;
|
||||
} else {
|
||||
core_clock_mhz = FLASH_HIGH_SPEED_CORE_CLOCK_MHZ;
|
||||
}
|
||||
//SPI0 and SPI1 share the register for core clock. So we only set SPI0 here.
|
||||
s_mspi_flash_set_core_clock(MSPI_TIMING_LL_MSPI_ID_0, core_clock_mhz);
|
||||
|
||||
uint32_t freqdiv = core_clock_mhz / flash_freq_mhz;
|
||||
ESP_EARLY_LOGV(TAG, "flash freqdiv: %d", freqdiv);
|
||||
assert(freqdiv > 0);
|
||||
uint32_t reg_val = mspi_timing_ll_calculate_clock_reg(freqdiv);
|
||||
mspi_timing_ll_set_flash_clock(MSPI_TIMING_LL_MSPI_ID_0, reg_val);
|
||||
if (control_both_mspi) {
|
||||
mspi_timing_ll_set_flash_clock(MSPI_TIMING_LL_MSPI_ID_1, reg_val);
|
||||
}
|
||||
}
|
||||
|
||||
void mspi_timing_config_set_psram_clock(uint32_t psram_freq_mhz, mspi_timing_speed_mode_t speed_mode, bool control_both_mspi)
|
||||
{
|
||||
(void)control_both_mspi; // for compatibility
|
||||
uint32_t core_clock_mhz = 0;
|
||||
if (speed_mode == MSPI_TIMING_SPEED_MODE_LOW_PERF) {
|
||||
core_clock_mhz = PSRAM_LOW_SPEED_CORE_CLOCK_MHZ;
|
||||
} else {
|
||||
core_clock_mhz = PSRAM_HIGH_SPEED_CORE_CLOCK_MHZ;
|
||||
}
|
||||
//SPI0 and SPI1 share the register for core clock. So we only set SPI0 here.
|
||||
s_mspi_psram_set_core_clock(MSPI_TIMING_LL_MSPI_ID_0, core_clock_mhz);
|
||||
|
||||
uint32_t freqdiv = core_clock_mhz / psram_freq_mhz;
|
||||
ESP_EARLY_LOGV(TAG, "psram freqdiv: %d", freqdiv);
|
||||
assert(freqdiv > 0);
|
||||
uint32_t reg_val = mspi_timing_ll_calculate_clock_reg(freqdiv);
|
||||
mspi_timing_ll_set_psram_clock(MSPI_TIMING_LL_MSPI_ID_0, reg_val);
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_assert.h"
|
||||
#include "esp_flash_partitions.h"
|
||||
|
||||
#define MSPI_TIMING_MSPI1_IS_INVOLVED CONFIG_ESPTOOLPY_FLASHFREQ_120M //This means esp flash driver needs to be notified
|
||||
#define MSPI_TIMING_CONFIG_NUM_MAX 32 //This should be larger than the max available timing config num
|
||||
#define MSPI_TIMING_TEST_DATA_LEN 128
|
||||
#define MSPI_TIMING_PSRAM_TEST_DATA_ADDR 0x100000
|
||||
|
||||
//--------------------------------------FLASH Sampling Mode --------------------------------------//
|
||||
#define MSPI_TIMING_FLASH_STR_MODE 1
|
||||
//--------------------------------------FLASH Module Clock --------------------------------------//
|
||||
#if CONFIG_ESPTOOLPY_FLASHFREQ_20M
|
||||
#define MSPI_TIMING_FLASH_MODULE_CLOCK 20
|
||||
#elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
|
||||
#define MSPI_TIMING_FLASH_MODULE_CLOCK 40
|
||||
#elif CONFIG_ESPTOOLPY_FLASHFREQ_80M
|
||||
#define MSPI_TIMING_FLASH_MODULE_CLOCK 80
|
||||
#elif CONFIG_ESPTOOLPY_FLASHFREQ_120M
|
||||
#define MSPI_TIMING_FLASH_MODULE_CLOCK 120
|
||||
#endif
|
||||
//------------------------------------FLASH Needs Tuning or not-------------------------------------//
|
||||
#define MSPI_TIMING_FLASH_NEEDS_TUNING 0
|
||||
|
||||
//--------------------------------------PSRAM Sampling Mode --------------------------------------//
|
||||
#define MSPI_TIMING_PSRAM_STR_MODE 1
|
||||
//--------------------------------------PSRAM Module Clock --------------------------------------//
|
||||
#if CONFIG_SPIRAM
|
||||
#if CONFIG_SPIRAM_SPEED_40M
|
||||
#define MSPI_TIMING_PSRAM_MODULE_CLOCK 40
|
||||
#elif CONFIG_SPIRAM_SPEED_80M
|
||||
#define MSPI_TIMING_PSRAM_MODULE_CLOCK 80
|
||||
#endif
|
||||
#else //Disable PSRAM
|
||||
#define MSPI_TIMING_PSRAM_MODULE_CLOCK 10 //Define this to 10MHz
|
||||
#endif
|
||||
//------------------------------------PSRAM Needs Tuning or not-------------------------------------//
|
||||
#if MSPI_TIMING_PSRAM_STR_MODE
|
||||
#define MSPI_TIMING_PSRAM_NEEDS_TUNING (MSPI_TIMING_PSRAM_MODULE_CLOCK > 40)
|
||||
#endif
|
||||
|
||||
///////////////////////////////////// FLASH/PSRAM CORE CLOCK /////////////////////////////////////
|
||||
#if ((CONFIG_ESPTOOLPY_FLASHFREQ_80M && !CONFIG_SPIRAM) || (CONFIG_ESPTOOLPY_FLASHFREQ_80M && CONFIG_SPIRAM_SPEED_80M))
|
||||
#define MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ 80
|
||||
#define MSPI_TIMING_PSRAM_EXPECTED_CORE_CLK_MHZ 80
|
||||
#define MSPI_TIMING_FLASH_CONSECUTIVE_LEN_MAX 6
|
||||
#else
|
||||
#define MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ 240
|
||||
#define MSPI_TIMING_PSRAM_EXPECTED_CORE_CLK_MHZ 240
|
||||
#define MSPI_TIMING_FLASH_CONSECUTIVE_LEN_MAX 4
|
||||
#endif
|
||||
|
||||
//------------------------------------------Determine the Core Clock-----------------------------------------------//
|
||||
/**
|
||||
* @note
|
||||
* Limitation 1:
|
||||
* MSPI FLASH and PSRAM share the core clock register. Therefore,
|
||||
* the expected CORE CLOCK frequencies should be the same.
|
||||
*/
|
||||
#if MSPI_TIMING_FLASH_NEEDS_TUNING && MSPI_TIMING_PSRAM_NEEDS_TUNING
|
||||
ESP_STATIC_ASSERT(MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ == MSPI_TIMING_PSRAM_EXPECTED_CORE_CLK_MHZ, "FLASH and PSRAM Mode configuration are not supported");
|
||||
#define MSPI_TIMING_CORE_CLOCK_MHZ MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ
|
||||
|
||||
//If only FLASH needs tuning, the core clock COULD be as FLASH expected
|
||||
#elif MSPI_TIMING_FLASH_NEEDS_TUNING && !MSPI_TIMING_PSRAM_NEEDS_TUNING
|
||||
ESP_STATIC_ASSERT(MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ % MSPI_TIMING_PSRAM_MODULE_CLOCK == 0, "FLASH and PSRAM Mode configuration are not supported");
|
||||
#define MSPI_TIMING_CORE_CLOCK_MHZ MSPI_TIMING_FLASH_EXPECTED_CORE_CLK_MHZ
|
||||
|
||||
//If only PSRAM needs tuning, the core clock COULD be as PSRAM expected
|
||||
#elif !MSPI_TIMING_FLASH_NEEDS_TUNING && MSPI_TIMING_PSRAM_NEEDS_TUNING
|
||||
ESP_STATIC_ASSERT(MSPI_TIMING_PSRAM_EXPECTED_CORE_CLK_MHZ % MSPI_TIMING_FLASH_MODULE_CLOCK == 0, "FLASH and PSRAM Mode configuration are not supported");
|
||||
#define MSPI_TIMING_CORE_CLOCK_MHZ MSPI_TIMING_PSRAM_EXPECTED_CORE_CLK_MHZ
|
||||
|
||||
#else
|
||||
#define MSPI_TIMING_CORE_CLOCK_MHZ 80
|
||||
#endif
|
||||
|
||||
|
||||
//------------------------------------------Helper Macros to get FLASH/PSRAM tuning configs-----------------------------------------------//
|
||||
#define __GET_TUNING_CONFIG(type, core_clock, module_clock, mode) \
|
||||
(mspi_timing_config_t) { .tuning_config_table = MSPI_TIMING_##type##_CONFIG_TABLE_CORE_CLK_##core_clock##M_MODULE_CLK_##module_clock##M_##mode, \
|
||||
.available_config_num = MSPI_TIMING_##type##_CONFIG_NUM_CORE_CLK_##core_clock##M_MODULE_CLK_##module_clock##M_##mode, \
|
||||
.default_config_id = MSPI_TIMING_##type##_DEFAULT_CONFIG_ID_CORE_CLK_##core_clock##M_MODULE_CLK_##module_clock##M_##mode }
|
||||
|
||||
#define _GET_TUNING_CONFIG(type, core_clock, module_clock, mode) __GET_TUNING_CONFIG(type, core_clock, module_clock, mode)
|
||||
|
||||
#define MSPI_TIMING_FLASH_GET_TUNING_CONFIG(core_clock_mhz, module_clock_mhz, mode) _GET_TUNING_CONFIG(FLASH, core_clock_mhz, module_clock_mhz, mode)
|
||||
#define MSPI_TIMING_PSRAM_GET_TUNING_CONFIG(core_clock_mhz, module_clock_mhz, mode) _GET_TUNING_CONFIG(PSRAM, core_clock_mhz, module_clock_mhz, mode)
|
||||
|
||||
|
||||
/**
|
||||
* Timing Tuning Parameters
|
||||
*/
|
||||
//FLASH: core clock 80M, module clock 80M, STR mode
|
||||
#define MSPI_TIMING_FLASH_CONFIG_TABLE_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE {{2, 2, 1}, {2, 1, 1}, {2, 0, 1}, {0, 0, 0}, {3, 1, 2}, {2, 3, 2}, {2, 2, 2}, {2, 1, 2}, {2, 0, 2}, {0, 0, 1}, {3, 1, 3}, {2, 3, 3}, {2, 2, 3}, {2, 1, 3}}
|
||||
#define MSPI_TIMING_FLASH_CONFIG_NUM_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE 14
|
||||
#define MSPI_TIMING_FLASH_DEFAULT_CONFIG_ID_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE 4
|
||||
|
||||
//PSRAM: core clock 240M, module clock 120M, STR mode
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_TABLE_CORE_CLK_240M_MODULE_CLK_120M_STR_MODE {{2, 0, 1}, {0, 0, 0}, {2, 2, 2}, {2, 1, 2}, {2, 0, 2}, {0, 0, 1}, {2, 2, 3}, {2, 1, 3}, {2, 0, 3}, {0, 0, 2}, {2, 2, 4}, {2, 1, 4}}
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_NUM_CORE_CLK_240M_MODULE_CLK_120M_STR_MODE 12
|
||||
#define MSPI_TIMING_PSRAM_DEFAULT_CONFIG_ID_CORE_CLK_240M_MODULE_CLK_120M_STR_MODE 4
|
||||
|
||||
//PSRAM: core clock 240M, module clock 80M, STR mode
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_TABLE_CORE_CLK_240M_MODULE_CLK_80M_STR_MODE {{2, 2, 1}, {2, 1, 1}, {2, 0, 1}, {0, 0, 0}, {3, 1, 2}, {2, 3, 2}, {2, 2, 2}, {2, 1, 2}, {2, 0, 2}, {0, 0, 1}, {3, 1, 3}, {2, 3, 3}, {2, 2, 3}, {2, 1, 3}}
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_NUM_CORE_CLK_240M_MODULE_CLK_80M_STR_MODE 14
|
||||
#define MSPI_TIMING_PSRAM_DEFAULT_CONFIG_ID_CORE_CLK_240M_MODULE_CLK_80M_STR_MODE 4
|
||||
|
||||
//PSRAM: core clock 80M, module clock 80M, STR mode
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_TABLE_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE {{2, 2, 1}, {2, 1, 1}, {2, 0, 1}, {0, 0, 0}, {3, 1, 2}, {2, 3, 2}, {2, 2, 2}, {2, 1, 2}, {2, 0, 2}, {0, 0, 1}, {3, 1, 3}, {2, 3, 3}, {2, 2, 3}, {2, 1, 3}}
|
||||
#define MSPI_TIMING_PSRAM_CONFIG_NUM_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE 14
|
||||
#define MSPI_TIMING_PSRAM_DEFAULT_CONFIG_ID_CORE_CLK_80M_MODULE_CLK_80M_STR_MODE 4
|
||||
Reference in New Issue
Block a user