2021-09-09 03:34:42 -04:00
|
|
|
/*
|
2024-02-20 02:20:33 -05:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2021-09-09 03:34:42 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-10-19 05:08:05 -04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2016-12-09 12:29:31 -05:00
|
|
|
#include <sys/param.h> // For MIN/MAX(a, b)
|
2016-10-19 05:08:05 -04:00
|
|
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/task.h>
|
|
|
|
#include <freertos/semphr.h>
|
|
|
|
#include <soc/soc.h>
|
global: move the soc component out of the common list
This MR removes the common dependency from every IDF components to the SOC component.
Currently, in the ``idf_functions.cmake`` script, we include the header path of SOC component by default for all components.
But for better code organization (or maybe also benifits to the compiling speed), we may remove the dependency to SOC components for most components except the driver and kernel related components.
In CMAKE, we have two kinds of header visibilities (set by include path visibility):
(Assume component A --(depends on)--> B, B is the current component)
1. public (``COMPONENT_ADD_INCLUDEDIRS``): means this path is visible to other depending components (A) (visible to A and B)
2. private (``COMPONENT_PRIV_INCLUDEDIRS``): means this path is only visible to source files inside the component (visible to B only)
and we have two kinds of depending ways:
(Assume component A --(depends on)--> B --(depends on)--> C, B is the current component)
1. public (```COMPONENT_REQUIRES```): means B can access to public include path of C. All other components rely on you (A) will also be available for the public headers. (visible to A, B)
2. private (``COMPONENT_PRIV_REQUIRES``): means B can access to public include path of C, but don't propagate this relation to other components (A). (visible to B)
1. remove the common requirement in ``idf_functions.cmake``, this makes the SOC components invisible to all other components by default.
2. if a component (for example, DRIVER) really needs the dependency to SOC, add a private dependency to SOC for it.
3. some other components that don't really depends on the SOC may still meet some errors saying "can't find header soc/...", this is because it's depended component (DRIVER) incorrectly include the header of SOC in its public headers. Moving all this kind of #include into source files, or private headers
4. Fix the include requirements for some file which miss sufficient #include directives. (Previously they include some headers by the long long long header include link)
This is a breaking change. Previous code may depends on the long include chain.
You may need to include the following headers for some files after this commit:
- soc/soc.h
- soc/soc_memory_layout.h
- driver/gpio.h
- esp_sleep.h
The major broken include chain includes:
1. esp_system.h no longer includes esp_sleep.h. The latter includes driver/gpio.h and driver/touch_pad.h.
2. ets_sys.h no longer includes soc/soc.h
3. freertos/portmacro.h no longer includes soc/soc_memory_layout.h
some peripheral headers no longer includes their hw related headers, e.g. rom/gpio.h no longer includes soc/gpio_pins.h and soc/gpio_reg.h
BREAKING CHANGE
2019-04-03 01:17:38 -04:00
|
|
|
#include <soc/soc_memory_layout.h>
|
2022-07-21 07:14:26 -04:00
|
|
|
#include "soc/io_mux_reg.h"
|
2016-10-19 05:08:05 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_attr.h"
|
2022-07-21 07:24:42 -04:00
|
|
|
#include "esp_cpu.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "spi_flash_mmap.h"
|
2016-10-19 05:08:05 -04:00
|
|
|
#include "esp_log.h"
|
2020-09-03 06:17:24 -04:00
|
|
|
#include "esp_private/system_internal.h"
|
2021-09-09 03:34:42 -04:00
|
|
|
#include "esp_private/spi_flash_os.h"
|
2021-11-18 22:42:01 -05:00
|
|
|
#include "esp_private/esp_clk.h"
|
2023-02-02 01:25:18 -05:00
|
|
|
#include "esp_private/esp_gpio_reserve.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-12-26 02:25:24 -05:00
|
|
|
#include "esp32/rom/cache.h"
|
2021-11-08 02:10:13 -05:00
|
|
|
#include "esp32/rom/spi_flash.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/cache.h"
|
2020-07-29 01:13:51 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
2021-04-15 05:13:48 -04:00
|
|
|
#include "soc/spi_mem_reg.h"
|
2021-08-02 05:15:07 -04:00
|
|
|
#include "esp32s3/rom/opi_flash.h"
|
2020-07-29 01:13:51 -04:00
|
|
|
#include "esp32s3/rom/cache.h"
|
2021-09-09 03:34:42 -04:00
|
|
|
#include "esp32s3/opi_flash_private.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/cache.h"
|
2022-01-17 21:32:56 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C2
|
|
|
|
#include "esp32c2/rom/cache.h"
|
2022-07-12 09:19:35 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C6
|
|
|
|
#include "esp32c6/rom/cache.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#endif
|
2021-09-28 02:12:56 -04:00
|
|
|
#include "esp_rom_spiflash.h"
|
2017-10-25 03:22:30 -04:00
|
|
|
#include "esp_flash_partitions.h"
|
2023-01-05 02:24:08 -05:00
|
|
|
#include "esp_private/mspi_timing_tuning.h"
|
2022-06-27 03:24:07 -04:00
|
|
|
#include "esp_private/cache_utils.h"
|
2019-01-08 05:29:25 -05:00
|
|
|
#include "esp_flash.h"
|
2019-10-22 23:28:53 -04:00
|
|
|
#include "esp_attr.h"
|
2021-05-07 03:25:06 -04:00
|
|
|
#include "bootloader_flash.h"
|
2022-07-21 07:14:26 -04:00
|
|
|
#include "bootloader_flash_config.h"
|
2021-10-29 05:32:28 -04:00
|
|
|
#include "esp_compiler.h"
|
2022-07-21 07:14:26 -04:00
|
|
|
#include "esp_rom_efuse.h"
|
|
|
|
#if CONFIG_SPIRAM
|
|
|
|
#include "esp_private/esp_psram_io.h"
|
|
|
|
#endif
|
2023-02-22 04:31:48 -05:00
|
|
|
#if SOC_MEMSPI_CLOCK_IS_INDEPENDENT
|
|
|
|
#include "hal/cache_hal.h"
|
|
|
|
#endif
|
2019-10-22 23:28:53 -04:00
|
|
|
|
2016-11-24 00:58:36 -05:00
|
|
|
/* bytes erased by SPIEraseBlock() ROM function */
|
2016-12-08 22:18:58 -05:00
|
|
|
#define BLOCK_ERASE_SIZE 65536
|
2016-11-24 00:58:36 -05:00
|
|
|
|
2017-03-21 23:48:33 -04:00
|
|
|
/* Limit number of bytes written/read in a single SPI operation,
|
|
|
|
as these operations disable all higher priority tasks from running.
|
|
|
|
*/
|
2020-06-04 05:21:34 -04:00
|
|
|
#ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
|
|
|
|
#define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
|
|
|
|
#else
|
2017-03-21 23:48:33 -04:00
|
|
|
#define MAX_WRITE_CHUNK 8192
|
2020-06-04 05:21:34 -04:00
|
|
|
#endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
|
|
|
|
|
2017-03-21 23:48:33 -04:00
|
|
|
#define MAX_READ_CHUNK 16384
|
|
|
|
|
2017-10-06 00:38:01 -04:00
|
|
|
static const char *TAG __attribute__((unused)) = "spi_flash";
|
|
|
|
|
2017-01-03 14:01:40 -05:00
|
|
|
const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
|
2018-11-12 16:26:07 -05:00
|
|
|
.start = spi_flash_disable_interrupts_caches_and_other_cpu,
|
|
|
|
.end = spi_flash_enable_interrupts_caches_and_other_cpu,
|
2016-12-21 18:56:23 -05:00
|
|
|
};
|
|
|
|
|
2017-01-03 14:01:40 -05:00
|
|
|
const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
|
2018-11-12 16:26:07 -05:00
|
|
|
.start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
|
|
|
|
.end = spi_flash_enable_interrupts_caches_no_os,
|
2016-12-21 18:56:23 -05:00
|
|
|
};
|
2016-10-19 05:08:05 -04:00
|
|
|
|
2022-06-27 03:24:07 -04:00
|
|
|
static const spi_flash_guard_funcs_t *s_flash_guard_ops;
|
|
|
|
|
|
|
|
void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t *funcs)
|
|
|
|
{
|
|
|
|
s_flash_guard_ops = funcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const spi_flash_guard_funcs_t *IRAM_ATTR spi_flash_guard_get(void)
|
|
|
|
{
|
|
|
|
return s_flash_guard_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-11 04:45:26 -04:00
|
|
|
#ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
|
2017-10-25 03:22:30 -04:00
|
|
|
#define UNSAFE_WRITE_ADDRESS abort()
|
|
|
|
#else
|
|
|
|
#define UNSAFE_WRITE_ADDRESS return false
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static __attribute__((unused)) bool is_safe_write_address(size_t addr, size_t size)
|
|
|
|
{
|
2019-01-08 05:29:25 -05:00
|
|
|
if (!esp_partition_main_flash_region_safe(addr, size)) {
|
2017-10-25 03:22:30 -04:00
|
|
|
UNSAFE_WRITE_ADDRESS;
|
|
|
|
}
|
2019-01-08 05:29:25 -05:00
|
|
|
return true;
|
2017-10-25 03:22:30 -04:00
|
|
|
}
|
|
|
|
|
2020-12-15 22:50:13 -05:00
|
|
|
#if CONFIG_SPI_FLASH_ROM_IMPL
|
|
|
|
#include "esp_heap_caps.h"
|
|
|
|
|
|
|
|
void IRAM_ATTR *spi_flash_malloc_internal(size_t size)
|
|
|
|
{
|
|
|
|
return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
|
|
|
|
}
|
2022-06-27 03:24:07 -04:00
|
|
|
|
|
|
|
void IRAM_ATTR spi_flash_rom_impl_init(void)
|
|
|
|
{
|
|
|
|
spi_flash_guard_set(&g_flash_guard_default_ops);
|
|
|
|
|
|
|
|
/* These two functions are in ROM only */
|
|
|
|
extern void spi_flash_mmap_os_func_set(void *(*func1)(size_t size), void (*func2)(void *p));
|
|
|
|
spi_flash_mmap_os_func_set(spi_flash_malloc_internal, heap_caps_free);
|
|
|
|
|
|
|
|
extern esp_err_t spi_flash_mmap_page_num_init(uint32_t page_num);
|
|
|
|
spi_flash_mmap_page_num_init(128);
|
|
|
|
}
|
2020-12-15 22:50:13 -05:00
|
|
|
#endif
|
|
|
|
|
2021-08-02 05:15:07 -04:00
|
|
|
void IRAM_ATTR esp_mspi_pin_init(void)
|
|
|
|
{
|
2022-09-23 05:32:16 -04:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
bool octal_mspi_required = bootloader_flash_is_octal_mode_enabled();
|
|
|
|
#if CONFIG_SPIRAM_MODE_OCT
|
|
|
|
octal_mspi_required |= true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (octal_mspi_required) {
|
|
|
|
esp_rom_opiflash_pin_config();
|
2023-01-05 02:24:08 -05:00
|
|
|
mspi_timing_set_pin_drive_strength();
|
2022-09-23 05:32:16 -04:00
|
|
|
}
|
2021-08-02 05:15:07 -04:00
|
|
|
//Set F4R4 board pin drive strength. TODO: IDF-3663
|
|
|
|
#endif
|
2023-02-02 01:25:18 -05:00
|
|
|
/* Reserve the GPIO pins */
|
|
|
|
uint64_t reserve_pin_mask = 0;
|
|
|
|
for (esp_mspi_io_t i = 0; i < ESP_MSPI_IO_MAX; i++) {
|
|
|
|
reserve_pin_mask |= BIT64(esp_mspi_get_io(i));
|
|
|
|
}
|
2024-02-20 02:20:33 -05:00
|
|
|
esp_gpio_reserve(reserve_pin_mask);
|
2021-08-02 05:15:07 -04:00
|
|
|
}
|
|
|
|
|
2021-09-09 03:34:42 -04:00
|
|
|
esp_err_t IRAM_ATTR spi_flash_init_chip_state(void)
|
|
|
|
{
|
2022-09-23 05:32:16 -04:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
if (bootloader_flash_is_octal_mode_enabled()) {
|
|
|
|
return esp_opiflash_init(rom_spiflash_legacy_data->chip.device_id);
|
2023-06-24 08:38:53 -04:00
|
|
|
}
|
2022-09-23 05:32:16 -04:00
|
|
|
#endif
|
2023-06-24 08:38:53 -04:00
|
|
|
#if CONFIG_SPI_FLASH_HPM_ON
|
2022-09-23 05:32:16 -04:00
|
|
|
return spi_flash_enable_high_performance_mode();
|
2023-06-24 08:38:53 -04:00
|
|
|
#endif // CONFIG_SPI_FLASH_HPM_ON
|
|
|
|
return ESP_OK;
|
2021-09-09 03:34:42 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 05:13:48 -04:00
|
|
|
void IRAM_ATTR spi_flash_set_rom_required_regs(void)
|
|
|
|
{
|
2022-09-23 05:32:16 -04:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
if (bootloader_flash_is_octal_mode_enabled()) {
|
|
|
|
//Disable the variable dummy mode when doing timing tuning
|
|
|
|
CLEAR_PERI_REG_MASK(SPI_MEM_DDR_REG(1), SPI_MEM_SPI_FMEM_VAR_DUMMY);
|
|
|
|
/**
|
|
|
|
* STR /DTR mode setting is done every time when `esp_rom_opiflash_exec_cmd` is called
|
|
|
|
*
|
|
|
|
* Add any registers that are not set in ROM SPI flash functions here in the future
|
|
|
|
*/
|
|
|
|
}
|
2021-04-15 05:13:48 -04:00
|
|
|
#endif
|
|
|
|
}
|
2021-09-09 03:34:42 -04:00
|
|
|
|
2022-07-11 02:23:57 -04:00
|
|
|
#if CONFIG_SPIRAM_MODE_OCT
|
|
|
|
// This function will only be called when Octal PSRAM enabled.
|
2021-09-09 03:34:42 -04:00
|
|
|
void IRAM_ATTR spi_flash_set_vendor_required_regs(void)
|
|
|
|
{
|
2022-09-23 05:32:16 -04:00
|
|
|
if (bootloader_flash_is_octal_mode_enabled()) {
|
|
|
|
esp_opiflash_set_required_regs();
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CACHE_FCTRL_REG(1), SPI_MEM_CACHE_USR_CMD_4BYTE_V, 1, SPI_MEM_CACHE_USR_CMD_4BYTE_S);
|
|
|
|
} else {
|
|
|
|
//Flash chip requires MSPI specifically, call this function to set them
|
|
|
|
// Set back MSPI registers after Octal PSRAM initialization.
|
|
|
|
SET_PERI_REG_BITS(SPI_MEM_CACHE_FCTRL_REG(1), SPI_MEM_CACHE_USR_CMD_4BYTE_V, 0, SPI_MEM_CACHE_USR_CMD_4BYTE_S);
|
|
|
|
}
|
2021-09-09 03:34:42 -04:00
|
|
|
}
|
2022-07-11 02:23:57 -04:00
|
|
|
#endif
|
2022-07-21 07:14:26 -04:00
|
|
|
|
|
|
|
static const uint8_t s_mspi_io_num_default[] = {
|
|
|
|
SPI_CLK_GPIO_NUM,
|
|
|
|
SPI_Q_GPIO_NUM,
|
|
|
|
SPI_D_GPIO_NUM,
|
|
|
|
SPI_CS0_GPIO_NUM,
|
|
|
|
SPI_HD_GPIO_NUM,
|
|
|
|
SPI_WP_GPIO_NUM,
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
SPI_DQS_GPIO_NUM,
|
|
|
|
SPI_D4_GPIO_NUM,
|
|
|
|
SPI_D5_GPIO_NUM,
|
|
|
|
SPI_D6_GPIO_NUM,
|
|
|
|
SPI_D7_GPIO_NUM
|
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
};
|
|
|
|
|
|
|
|
uint8_t esp_mspi_get_io(esp_mspi_io_t io)
|
|
|
|
{
|
|
|
|
#if CONFIG_SPIRAM
|
|
|
|
if (io == ESP_MSPI_IO_CS1) {
|
|
|
|
return esp_psram_io_get_cs_io();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(io >= ESP_MSPI_IO_CLK);
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
assert(io <= ESP_MSPI_IO_D7);
|
|
|
|
#else
|
|
|
|
assert(io <= ESP_MSPI_IO_WP);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
|
|
|
|
uint8_t mspi_io = 0;
|
|
|
|
uint32_t spiconfig = 0;
|
|
|
|
|
|
|
|
if (io == ESP_MSPI_IO_WP) {
|
|
|
|
/**
|
|
|
|
* wp pad is a bit special:
|
|
|
|
* 1. since 32's efuse does not have enough bits for wp pad, so wp pad config put in flash bin header
|
|
|
|
* 2. rom code take 0x3f as invalid wp pad num, but take 0 as other invalid mspi pads num
|
|
|
|
*/
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
return bootloader_flash_get_wp_pin();
|
|
|
|
#else
|
|
|
|
spiconfig = esp_rom_efuse_get_flash_wp_gpio();
|
|
|
|
return (spiconfig == 0x3f) ? s_mspi_io_num_default[io] : spiconfig & 0x3f;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
spiconfig = (io < ESP_MSPI_IO_WP) ? esp_rom_efuse_get_flash_gpio_info() : esp_rom_efuse_get_opiconfig();
|
|
|
|
#else
|
|
|
|
spiconfig = esp_rom_efuse_get_flash_gpio_info();
|
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
|
|
|
|
if (spiconfig == ESP_ROM_EFUSE_FLASH_DEFAULT_SPI) {
|
|
|
|
mspi_io = s_mspi_io_num_default[io];
|
|
|
|
} else if (io < ESP_MSPI_IO_WP) {
|
|
|
|
/**
|
|
|
|
* [0 : 5] -- CLK
|
|
|
|
* [6 :11] -- Q(D1)
|
|
|
|
* [12:17] -- D(D0)
|
|
|
|
* [18:23] -- CS
|
|
|
|
* [24:29] -- HD(D3)
|
|
|
|
*/
|
|
|
|
mspi_io = (spiconfig >> io * 6) & 0x3f;
|
|
|
|
}
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
else {
|
|
|
|
/**
|
|
|
|
* [0 : 5] -- DQS
|
|
|
|
* [6 :11] -- D4
|
|
|
|
* [12:17] -- D5
|
|
|
|
* [18:23] -- D6
|
|
|
|
* [24:29] -- D7
|
|
|
|
*/
|
|
|
|
mspi_io = (spiconfig >> (io - ESP_MSPI_IO_DQS) * 6) & 0x3f;
|
|
|
|
}
|
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
|
|
|
|
return mspi_io;
|
|
|
|
#else // SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
|
|
|
|
return s_mspi_io_num_default[io];
|
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
|
|
|
|
}
|