2019-11-27 20:20:00 -05:00
|
|
|
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-05-18 22:53:21 -04:00
|
|
|
#include <string.h>
|
2019-11-27 20:20:00 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "hal/spi_flash_hal.h"
|
2021-05-18 22:53:21 -04:00
|
|
|
#include "hal/assert.h"
|
2020-07-26 15:13:07 -04:00
|
|
|
#include "soc/soc_caps.h"
|
2024-05-27 00:03:49 -04:00
|
|
|
#include "soc/chip_revision.h"
|
|
|
|
#include "hal/efuse_hal.h"
|
2019-11-27 20:20:00 -05:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
#define ADDRESS_MASK_24BIT 0xFFFFFF
|
2020-05-07 02:46:41 -04:00
|
|
|
#define COMPUTE_DUMMY_CYCLELEN(host, base) ((base) + ((spi_flash_hal_context_t*)host)->extra_dummy)
|
2019-11-27 20:20:00 -05:00
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
static inline spi_dev_t *get_spi_dev(spi_flash_host_inst_t *host)
|
2019-11-27 20:20:00 -05:00
|
|
|
{
|
2020-05-07 02:46:41 -04:00
|
|
|
return ((spi_flash_hal_context_t*)host)->spi;
|
2019-11-27 20:20:00 -05:00
|
|
|
}
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
void spi_flash_hal_poll_cmd_done(spi_flash_host_inst_t *host)
|
2019-11-27 20:20:00 -05:00
|
|
|
{
|
|
|
|
while (!spi_flash_ll_cmd_is_done(get_spi_dev(host))) {
|
|
|
|
//nop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
esp_err_t spi_flash_hal_device_config(spi_flash_host_inst_t *host)
|
2019-11-27 20:20:00 -05:00
|
|
|
{
|
2020-07-26 15:13:07 -04:00
|
|
|
spi_flash_hal_context_t* ctx = (spi_flash_hal_context_t*)host;
|
2019-11-27 20:20:00 -05:00
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2020-10-10 03:53:44 -04:00
|
|
|
|
2019-11-27 20:20:00 -05:00
|
|
|
spi_flash_ll_reset(dev);
|
2020-07-26 15:13:07 -04:00
|
|
|
spi_flash_ll_set_cs_pin(dev, ctx->cs_num);
|
|
|
|
spi_flash_ll_set_clock(dev, &ctx->clock_conf);
|
|
|
|
int cs_hold = ctx->cs_hold;
|
|
|
|
spi_flash_ll_set_hold(dev, cs_hold);
|
2021-05-20 08:51:38 -04:00
|
|
|
spi_flash_ll_set_cs_setup(dev, ctx->cs_setup);
|
|
|
|
|
2020-12-17 23:57:55 -05:00
|
|
|
#ifndef GPSPI_BUILD
|
|
|
|
#if SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
|
|
|
if ((ctx->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_SUSPEND) != 0) {
|
|
|
|
spi_flash_hal_setup_auto_suspend_mode(host);
|
|
|
|
} else {
|
|
|
|
spi_flash_hal_disable_auto_suspend_mode(host);
|
|
|
|
}
|
|
|
|
if ((ctx->flags & SPI_FLASH_HOST_CONTEXT_FLAG_AUTO_RESUME) != 0) {
|
|
|
|
spi_flash_hal_setup_auto_resume_mode(host);
|
|
|
|
} else {
|
|
|
|
spi_flash_hal_disable_auto_resume_mode(host);
|
|
|
|
}
|
|
|
|
#endif //SOC_SPI_MEM_SUPPORT_AUTO_SUSPEND
|
2023-04-04 03:49:45 -04:00
|
|
|
#if SOC_SPI_MEM_SUPPORT_TIMING_TUNING
|
2021-09-01 03:58:15 -04:00
|
|
|
// Always keep the extra dummy on SPI1 is 0, add extra dummy to user dummy
|
|
|
|
spimem_flash_ll_set_extra_dummy((spi_mem_dev_t*)dev, 0);
|
|
|
|
#endif
|
2021-06-21 07:43:51 -04:00
|
|
|
#else
|
|
|
|
gpspi_flash_ll_set_hold_pol(dev, 1);
|
2020-12-17 23:57:55 -05:00
|
|
|
#endif //GPSPI_BUILD
|
2021-06-21 07:43:51 -04:00
|
|
|
|
2019-11-27 20:20:00 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t spi_flash_hal_configure_host_io_mode(
|
2020-05-07 02:46:41 -04:00
|
|
|
spi_flash_host_inst_t *host,
|
2019-11-27 20:20:00 -05:00
|
|
|
uint32_t command,
|
|
|
|
uint32_t addr_bitlen,
|
|
|
|
int dummy_cyclelen_base,
|
|
|
|
esp_flash_io_mode_t io_mode)
|
|
|
|
{
|
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2020-05-11 14:31:30 -04:00
|
|
|
int host_id = spi_flash_ll_hw_get_id(dev);
|
2019-11-27 20:20:00 -05:00
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
uint32_t extra_bits = io_mode & 0xFFFF0000;
|
|
|
|
io_mode = io_mode & 0xFFFF;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some flash chips, when working under some IO modes (DIO, QIO and OIO in the future), treat
|
|
|
|
* the first 8 bits of the dummy bits as the bits. When the bits meet some pattern, the chip
|
|
|
|
* will go into a "continuous (XIP)" mode, where the command field will be skipped in the next
|
|
|
|
* transaction. We have to output all ones in these cycles because we don't need this feature.
|
|
|
|
*/
|
|
|
|
bool conf_required = ((extra_bits & SPI_FLASH_CONFIG_CONF_BITS) != 0);
|
|
|
|
|
2020-05-11 14:31:30 -04:00
|
|
|
if (!SOC_SPI_PERIPH_SUPPORT_MULTILINE_MODE(host_id) && io_mode > SPI_FLASH_FASTRD) {
|
2019-11-27 20:20:00 -05:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
2020-07-26 15:13:07 -04:00
|
|
|
|
2024-07-29 23:10:29 -04:00
|
|
|
#ifndef GPSPI_BUILD
|
2021-11-05 05:23:24 -04:00
|
|
|
#if SOC_SPI_PERIPH_SUPPORT_CONTROL_DUMMY_OUT
|
2020-07-26 15:13:07 -04:00
|
|
|
// The CONTROL_DUMMY_OUTPUT feature is used to control M7-M0 bits.
|
|
|
|
spi_flash_ll_set_dummy_out(dev, (conf_required? 1: 0), 1);
|
|
|
|
#else
|
2022-10-20 01:59:08 -04:00
|
|
|
/**
|
|
|
|
* - On current chips, addr phase can support 32 bits at most.
|
|
|
|
* - Flash chip requires continuous mode bits
|
|
|
|
*
|
|
|
|
* We send continuous mode bits via the dummy output feature, so as to support
|
|
|
|
* 32-bit address.
|
|
|
|
*
|
|
|
|
* On chips without dummy output feature (ESP32, ESP32C6), we fallback to use
|
|
|
|
* addr phase to send the continuous mode bits:
|
|
|
|
* - On ESP32 (QIO), qio_dummy: 6 - 4 / 4 = 5, addr_bitlen: 24 + 4 = 28. (This
|
|
|
|
* setting exists for long time, we keep this on ESP32)
|
|
|
|
* - On ESP32C6 (QIO), qio_dummy: 6 - 8 / 4 = 4, addr_bitlen: 24 + 8 = 32
|
|
|
|
* - On future chips without dummy output feature, we follow the ESP32C6 (QIO)
|
|
|
|
* way.
|
|
|
|
* - Above two ways, the timings are same.
|
|
|
|
* - DIO is similar.
|
|
|
|
*/
|
2020-07-26 15:13:07 -04:00
|
|
|
if (conf_required) {
|
2020-02-09 20:05:34 -05:00
|
|
|
int line_width = (io_mode == SPI_FLASH_DIO? 2: 4);
|
2022-10-20 01:59:08 -04:00
|
|
|
dummy_cyclelen_base -= SPI_FLASH_LL_CONTINUOUS_MODE_BIT_NUMS / line_width;
|
|
|
|
addr_bitlen += SPI_FLASH_LL_CONTINUOUS_MODE_BIT_NUMS;
|
|
|
|
spi_flash_ll_set_extra_address(dev, 0);
|
2020-02-09 20:05:34 -05:00
|
|
|
}
|
2020-07-26 15:13:07 -04:00
|
|
|
#endif
|
2024-07-29 23:10:29 -04:00
|
|
|
#else
|
|
|
|
gpspi_flash_ll_set_dummy_out(dev, (conf_required? 1: 0), 1);
|
|
|
|
#endif
|
2019-11-27 20:20:00 -05:00
|
|
|
|
2024-05-27 00:03:49 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32P4
|
|
|
|
// TODO: This is temporarily for ESP32P4-ECO0, please remove it when eco0 is not widly used. IDF-10019
|
|
|
|
unsigned chip_version = efuse_hal_chip_revision();
|
|
|
|
if (unlikely(!ESP_CHIP_REV_ABOVE(chip_version, 1))) {
|
|
|
|
if (conf_required) {
|
|
|
|
int line_width = (io_mode == SPI_FLASH_DIO? 2: 4);
|
|
|
|
dummy_cyclelen_base -= SPI_FLASH_LL_CONTINUOUS_MODE_BIT_NUMS / line_width;
|
|
|
|
addr_bitlen += SPI_FLASH_LL_CONTINUOUS_MODE_BIT_NUMS;
|
|
|
|
spi_flash_ll_set_extra_address(dev, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
if (command >= 0x100) {
|
|
|
|
spi_flash_ll_set_command(dev, command, 16);
|
|
|
|
} else {
|
|
|
|
spi_flash_ll_set_command(dev, command, 8);
|
|
|
|
}
|
2019-11-27 20:20:00 -05:00
|
|
|
spi_flash_ll_set_addr_bitlen(dev, addr_bitlen);
|
|
|
|
// Add dummy cycles to compensate for latency of GPIO matrix and external delay, if necessary...
|
|
|
|
spi_flash_ll_set_dummy(dev, COMPUTE_DUMMY_CYCLELEN(host, dummy_cyclelen_base));
|
|
|
|
//disable all data phases, enable them later if needed
|
|
|
|
spi_flash_ll_set_miso_bitlen(dev, 0);
|
|
|
|
spi_flash_ll_set_mosi_bitlen(dev, 0);
|
|
|
|
spi_flash_ll_set_read_mode(dev, io_mode);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
esp_err_t spi_flash_hal_common_command(spi_flash_host_inst_t *host, spi_flash_trans_t *trans)
|
2019-11-27 20:20:00 -05:00
|
|
|
{
|
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2020-07-26 15:13:07 -04:00
|
|
|
esp_flash_io_mode_t io_mode = ((spi_flash_hal_context_t*)host)->base_io_mode;
|
2020-12-15 22:50:13 -05:00
|
|
|
uint16_t command;
|
|
|
|
uint8_t dummy_bitlen;
|
2023-09-06 02:56:27 -04:00
|
|
|
bool pe_ops = false;
|
2022-06-13 23:23:21 -04:00
|
|
|
|
|
|
|
command = trans->command;
|
|
|
|
dummy_bitlen = trans->dummy_bitlen;
|
|
|
|
if ((trans->flags & SPI_FLASH_TRANS_FLAG_IGNORE_BASEIO) != 0) {
|
|
|
|
io_mode = trans->io_mode;
|
2020-07-26 15:13:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
host->driver->configure_host_io_mode(host, command, trans->address_bitlen, dummy_bitlen, io_mode);
|
2019-11-27 20:20:00 -05:00
|
|
|
|
2020-07-26 15:13:07 -04:00
|
|
|
spi_flash_ll_set_usr_address(dev, trans->address, trans->address_bitlen);
|
|
|
|
//No extra dummy cycles for compensation if no input data
|
2019-11-27 20:20:00 -05:00
|
|
|
if (trans->miso_len == 0) {
|
2020-07-26 15:13:07 -04:00
|
|
|
spi_flash_ll_set_dummy(dev, dummy_bitlen);
|
2019-11-27 20:20:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
spi_flash_ll_set_mosi_bitlen(dev, trans->mosi_len * 8);
|
|
|
|
spi_flash_ll_set_buffer_data(dev, trans->mosi_data, trans->mosi_len);
|
|
|
|
|
|
|
|
spi_flash_ll_set_miso_bitlen(dev, trans->miso_len * 8);
|
2023-05-11 08:10:30 -04:00
|
|
|
if ((trans->flags & SPI_FLASH_TRANS_FLAG_PE_CMD) != 0) {
|
2023-09-06 02:56:27 -04:00
|
|
|
pe_ops = true;
|
2023-05-11 08:10:30 -04:00
|
|
|
}
|
2023-09-06 02:56:27 -04:00
|
|
|
spi_flash_ll_user_start(dev, pe_ops);
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2021-07-09 05:24:25 -04:00
|
|
|
if (trans->miso_len > 0) {
|
|
|
|
spi_flash_ll_get_buffer_data(dev, trans->miso_data, trans->miso_len);
|
|
|
|
}
|
2019-11-27 20:20:00 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-07 02:46:41 -04:00
|
|
|
esp_err_t spi_flash_hal_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len)
|
2019-11-27 20:20:00 -05:00
|
|
|
{
|
|
|
|
spi_dev_t *dev = get_spi_dev(host);
|
2020-02-05 11:28:18 -05:00
|
|
|
int bitlen = spi_flash_ll_get_addr_bitlen(dev);
|
2020-07-26 15:13:07 -04:00
|
|
|
//Only 24-bit and 32-bit address are supported. The extra length are for M7-M0, which should be
|
|
|
|
//filled with ones by the function below
|
|
|
|
spi_flash_ll_set_usr_address(dev, address, bitlen & (~7));
|
2019-11-27 20:20:00 -05:00
|
|
|
spi_flash_ll_set_miso_bitlen(dev, read_len * 8);
|
2023-09-06 02:56:27 -04:00
|
|
|
spi_flash_ll_user_start(dev, false);
|
2020-05-07 02:46:41 -04:00
|
|
|
host->driver->poll_cmd_done(host);
|
2021-07-09 05:24:25 -04:00
|
|
|
if (read_len > 0) {
|
|
|
|
spi_flash_ll_get_buffer_data(dev, buffer, read_len);
|
|
|
|
}
|
2019-11-27 20:20:00 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|