2021-11-06 05:24:45 -04:00
|
|
|
/*
|
2023-10-18 23:37:20 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
2021-11-06 05:24:45 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-11-26 00:06:21 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
#include "hal/spi_flash_hal.h"
|
2023-05-11 08:10:30 -04:00
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
|
|
|
void spi_flash_hal_setup_auto_suspend_mode(spi_flash_host_inst_t *host);
|
|
|
|
void spi_flash_hal_disable_auto_resume_mode(spi_flash_host_inst_t *host);
|
|
|
|
void spi_flash_hal_disable_auto_suspend_mode(spi_flash_host_inst_t *host);
|
|
|
|
void spi_flash_hal_setup_auto_resume_mode(spi_flash_host_inst_t *host);
|
2023-05-11 08:10:30 -04:00
|
|
|
#define SPI_FLASH_TSHSL2_SAFE_VAL_NS (30)
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif //SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
2020-11-26 00:06:21 -05:00
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
#ifndef CONFIG_SPI_FLASH_ROM_IMPL
|
2019-01-08 05:29:25 -05:00
|
|
|
|
2021-03-11 06:39:27 -05:00
|
|
|
#include "spi_flash_hal_common.inc"
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
// HAL for
|
|
|
|
// - MEMSPI
|
2022-10-19 03:57:24 -04:00
|
|
|
// - SPI1~3 on ESP32/S2/S3/C3/H4/C2
|
2020-07-26 15:13:07 -04:00
|
|
|
// The common part is in spi_flash_hal_common.inc
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
void spi_flash_hal_erase_chip(spi_flash_host_inst_t *host)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2019-09-05 01:17:11 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
spi_flash_ll_erase_chip(dev);
|
2020-12-17 23:57:55 -05:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE
|
|
|
|
if((((spi_flash_hal_context_t*)host)->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND) == 0) {
|
|
|
|
host->driver->poll_cmd_done(host);
|
|
|
|
}
|
|
|
|
#else
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif
|
2019-01-08 05:29:25 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
// Only support 24bit address
|
2020-05-07 02:46:41 -04:00
|
|
|
void spi_flash_hal_erase_sector(spi_flash_host_inst_t *host, uint32_t start_address)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2019-09-05 01:17:11 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
spi_flash_ll_set_addr_bitlen(dev, 24);
|
|
|
|
spi_flash_ll_set_address(dev, start_address & ADDRESS_MASK_24BIT);
|
|
|
|
spi_flash_ll_erase_sector(dev);
|
2020-12-17 23:57:55 -05:00
|
|
|
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE
|
|
|
|
if((((spi_flash_hal_context_t*)host)->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND) == 0) {
|
|
|
|
host->driver->poll_cmd_done(host);
|
|
|
|
}
|
|
|
|
#else
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif
|
2019-01-08 05:29:25 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
// Only support 24bit address
|
2020-05-07 02:46:41 -04:00
|
|
|
void spi_flash_hal_erase_block(spi_flash_host_inst_t *host, uint32_t start_address)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2019-09-05 01:17:11 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
spi_flash_ll_set_addr_bitlen(dev, 24);
|
|
|
|
spi_flash_ll_set_address(dev, start_address & ADDRESS_MASK_24BIT);
|
|
|
|
spi_flash_ll_erase_block(dev);
|
2020-12-17 23:57:55 -05:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE
|
|
|
|
if((((spi_flash_hal_context_t*)host)->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND) == 0) {
|
|
|
|
host->driver->poll_cmd_done(host);
|
|
|
|
}
|
|
|
|
#else
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif
|
2019-01-08 05:29:25 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
// Only support 24bit address
|
2020-05-07 02:46:41 -04:00
|
|
|
void spi_flash_hal_program_page(spi_flash_host_inst_t *host, const void *buffer, uint32_t address, uint32_t length)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2019-09-05 01:17:11 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
spi_flash_ll_set_addr_bitlen(dev, 24);
|
|
|
|
spi_flash_ll_set_address(dev, (address & ADDRESS_MASK_24BIT) | (length << 24));
|
|
|
|
spi_flash_ll_program_page(dev, buffer, length);
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
}
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
esp_err_t spi_flash_hal_set_write_protect(spi_flash_host_inst_t *host, bool wp)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2019-09-05 01:17:11 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2019-11-27 20:20:00 -05:00
|
|
|
spi_flash_ll_set_write_protect(dev, wp);
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2019-01-08 05:29:25 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-03-11 06:39:27 -05:00
|
|
|
#else // defined CONFIG_SPI_FLASH_ROM_IMPL
|
|
|
|
|
|
|
|
static inline spi_dev_t *get_spi_dev(spi_flash_host_inst_t *host)
|
|
|
|
{
|
|
|
|
return ((spi_flash_hal_context_t*)host)->spi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_host_id(spi_flash_host_inst_t* host)
|
|
|
|
{
|
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
|
|
|
return spi_flash_ll_hw_get_id(dev);
|
|
|
|
}
|
|
|
|
|
2021-01-23 11:48:07 -05:00
|
|
|
#endif // !CONFIG_SPI_FLASH_ROM_IMPL
|
|
|
|
|
|
|
|
uint32_t spi_flash_hal_check_status(spi_flash_host_inst_t *host)
|
2019-01-08 05:29:25 -05:00
|
|
|
{
|
2020-05-07 02:46:41 -04:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2020-12-17 23:57:55 -05:00
|
|
|
uint32_t status = spi_flash_ll_host_idle(dev);
|
2021-01-23 11:48:07 -05:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_WAIT_IDLE
|
|
|
|
uint32_t sus_status = spimem_flash_ll_sus_status((spi_mem_dev_t*)dev) << 1;
|
|
|
|
#else
|
|
|
|
uint32_t sus_status = 0;
|
|
|
|
#endif
|
2019-01-08 05:29:25 -05:00
|
|
|
// Not clear if this is necessary, or only necessary if
|
|
|
|
// chip->spi == SPI1. But probably doesn't hurt...
|
2021-03-05 03:20:33 -05:00
|
|
|
if ((void*) dev == spi_flash_ll_get_hw(SPI1_HOST)) {
|
2019-11-27 20:20:00 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-12-17 23:57:55 -05:00
|
|
|
status &= spi_flash_ll_host_idle(&SPI0);
|
2019-11-27 20:20:00 -05:00
|
|
|
#endif
|
2019-01-08 05:29:25 -05:00
|
|
|
}
|
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
//status and sus_status should be mutual exclusion
|
|
|
|
return (status | sus_status);
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|
2020-11-26 00:06:21 -05:00
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
esp_err_t spi_flash_hal_setup_read_suspend(spi_flash_host_inst_t *host, const spi_flash_sus_cmd_conf *sus_conf)
|
|
|
|
{
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
2021-03-05 03:20:33 -05:00
|
|
|
spi_mem_dev_t *dev = (spi_mem_dev_t *)spi_flash_ll_get_hw(SPI1_HOST);
|
2020-12-17 23:57:55 -05:00
|
|
|
spi_flash_hal_context_t* ctx = (spi_flash_hal_context_t*)host;
|
|
|
|
memcpy(&(ctx->sus_cfg), sus_conf, sizeof(spi_flash_sus_cmd_conf));
|
|
|
|
spimem_flash_ll_suspend_cmd_setup(dev, sus_conf->sus_cmd);
|
|
|
|
spimem_flash_ll_resume_cmd_setup(dev, sus_conf->res_cmd);
|
2023-05-11 08:10:30 -04:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
|
|
|
spimem_flash_ll_set_read_sus_status(dev, sus_conf->sus_mask);
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_rd_sus_cmd_setup(dev, sus_conf->cmd_rdsr);
|
2023-05-11 08:10:30 -04:00
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
|
|
|
void spi_flash_hal_setup_auto_suspend_mode(spi_flash_host_inst_t *host)
|
|
|
|
{
|
2021-03-05 03:20:33 -05:00
|
|
|
spi_mem_dev_t *dev = (spi_mem_dev_t*)spi_flash_ll_get_hw(SPI1_HOST);
|
2023-05-11 08:10:30 -04:00
|
|
|
spi_flash_hal_context_t* ctx = (spi_flash_hal_context_t*)host;
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_auto_wait_idle_init(dev, true);
|
2023-11-14 02:38:44 -05:00
|
|
|
if (ctx->freq_mhz == 120) {
|
|
|
|
spimem_flash_ll_set_wait_idle_dummy_phase(dev, ctx->extra_dummy);
|
|
|
|
}
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_auto_suspend_init(dev, true);
|
2023-10-18 23:37:20 -04:00
|
|
|
// tsus = ceil(ctx->tsus_val * ctx->freq_mhz / spimem_flash_ll_get_tsus_unit_in_cycles);
|
|
|
|
uint32_t tsus = (ctx->tsus_val * ctx->freq_mhz / spimem_flash_ll_get_tsus_unit_in_cycles(dev)) + ((ctx->tsus_val * ctx->freq_mhz) % spimem_flash_ll_get_tsus_unit_in_cycles(dev) != 0);
|
2023-05-11 08:10:30 -04:00
|
|
|
spimem_flash_ll_set_sus_delay(dev, tsus);
|
|
|
|
// tshsl2 = ceil(SPI_FLASH_TSHSL2_SAFE_VAL_NS * spimem_flash_ll_get_source_freq_mhz() * 0.001);
|
|
|
|
uint32_t tshsl2 = (SPI_FLASH_TSHSL2_SAFE_VAL_NS * spimem_flash_ll_get_source_freq_mhz() / 1000) + ((SPI_FLASH_TSHSL2_SAFE_VAL_NS * spimem_flash_ll_get_source_freq_mhz()) % 1000 != 0);
|
|
|
|
spimem_flash_set_cs_hold_delay(dev, tshsl2);
|
|
|
|
spimem_flash_ll_sus_set_spi0_lock_trans(dev, SPIMEM_FLASH_LL_SPI0_MAX_LOCK_VAL_MSPI_TICKS);
|
2020-12-17 23:57:55 -05:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
|
|
|
spimem_flash_ll_sus_check_sus_setup(dev, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_flash_hal_setup_auto_resume_mode(spi_flash_host_inst_t *host)
|
|
|
|
{
|
2021-03-05 03:20:33 -05:00
|
|
|
spi_mem_dev_t *dev = (spi_mem_dev_t*)spi_flash_ll_get_hw(SPI1_HOST);
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_auto_resume_init(dev, true);
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
|
|
|
spimem_flash_ll_res_check_sus_setup(dev, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_flash_hal_disable_auto_suspend_mode(spi_flash_host_inst_t *host)
|
|
|
|
{
|
2021-03-05 03:20:33 -05:00
|
|
|
spi_mem_dev_t *dev = (spi_mem_dev_t *)spi_flash_ll_get_hw(SPI1_HOST);
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_auto_wait_idle_init(dev, false);
|
|
|
|
spimem_flash_ll_auto_suspend_init(dev, false);
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
|
|
|
spimem_flash_ll_sus_check_sus_setup(dev, false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_flash_hal_disable_auto_resume_mode(spi_flash_host_inst_t *host)
|
|
|
|
{
|
2021-03-05 03:20:33 -05:00
|
|
|
spi_mem_dev_t *dev = (spi_mem_dev_t*)spi_flash_ll_get_hw(SPI1_HOST);
|
2020-12-17 23:57:55 -05:00
|
|
|
spimem_flash_ll_auto_resume_init(dev, false);
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_CHECK_SUS
|
|
|
|
spimem_flash_ll_res_check_sus_setup(dev, false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif // SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
|
|
|
|
|
|
|
void spi_flash_hal_resume(spi_flash_host_inst_t *host)
|
|
|
|
{
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_SW_SUSPEND
|
|
|
|
spimem_flash_ll_resume((spi_mem_dev_t*)(((spi_flash_hal_context_t *)host)->spi));
|
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_flash_hal_suspend(spi_flash_host_inst_t *host)
|
|
|
|
{
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_SW_SUSPEND
|
|
|
|
spimem_flash_ll_suspend((spi_mem_dev_t *)(((spi_flash_hal_context_t *)host)->spi));
|
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
|
|
|
}
|