feat(spi_flash): New customise flash drivers framework, including bootloader

This commit is contained in:
C.S.M
2024-12-02 17:48:40 +08:00
parent 604ccb8f8e
commit af31ec11f1
14 changed files with 68 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -28,7 +28,7 @@ uint32_t bootloader_read_flash_id(void);
/** /**
* @brief Startup flow recommended by XMC. Call at startup before any erase/write operation. * @brief Startup flow recommended by XMC. Call at startup before any erase/write operation.
* *
* @return ESP_OK When startup successfully, otherwise ESP_FAIL (indiciating you should reboot before erase/write). * @return ESP_OK When startup successfully, otherwise ESP_FAIL (indicating you should reboot before erase/write).
*/ */
esp_err_t bootloader_flash_xmc_startup(void); esp_err_t bootloader_flash_xmc_startup(void);
@@ -36,9 +36,16 @@ esp_err_t bootloader_flash_xmc_startup(void);
* @brief Unlock Flash write protect. * @brief Unlock Flash write protect.
* Please do not call this function in SDK. * Please do not call this function in SDK.
* *
* @note This can be overridden because it's attribute weak. * @note This can be overridden because it's attribute weak, when there is a same name symbol.
*/ */
esp_err_t __attribute__((weak)) bootloader_flash_unlock(void); esp_err_t bootloader_flash_unlock(void);
/**
* @brief Unlock Flash write protect.
* This is alias to `bootloader_flash_unlock`.
* Please do not call this function in SDK.
*/
esp_err_t bootloader_flash_unlock_default(void);
/** /**
* @brief Reset the flash chip (66H + 99H). * @brief Reset the flash chip (66H + 99H).

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -99,15 +99,20 @@ void bootloader_write_status_8b_xmc25qu64a(unsigned new_status);
Searching of this table stops when the first match is found. Searching of this table stops when the first match is found.
*/ */
extern const bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_list[]; extern const bootloader_qio_info_t* bootloader_flash_qe_support_list;
/**
* @brief The bootloader flash qe list count number.
*/
extern uint8_t bootloader_flash_qe_list_count;
/** /**
* @brief Unlock Flash write protect. * @brief Unlock Flash write protect.
* Please do not call this function in SDK. * Please do not call this function in SDK.
* *
* @note This can be overridden because it's attribute weak. * @note This can be overridden because it's attribute weak, when there is a same name symbol.
*/ */
esp_err_t __attribute__((weak)) bootloader_flash_unlock(void); esp_err_t bootloader_flash_unlock(void);
#if CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_QUAD_FLASH || CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_OCTAL_FLASH #if CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_QUAD_FLASH || CONFIG_BOOTLOADER_CACHE_32BIT_ADDR_OCTAL_FLASH
/** /**
@@ -115,7 +120,7 @@ esp_err_t __attribute__((weak)) bootloader_flash_unlock(void);
* *
* @param flash_mode SPI flash working mode. * @param flash_mode SPI flash working mode.
* *
* @note This can be overridden because it's attribute weak. * @note This can be overridden because it's attribute weak, when there is a same name symbol.
*/ */
void __attribute__((weak)) bootloader_flash_32bits_address_map_enable(esp_rom_spiflash_read_mode_t flash_mode); void __attribute__((weak)) bootloader_flash_32bits_address_map_enable(esp_rom_spiflash_read_mode_t flash_mode);
#endif #endif

View File

@@ -499,7 +499,7 @@ FORCE_INLINE_ATTR bool is_mxic_chip(const esp_rom_spiflash_chip_t* chip)
return BYTESHIFT(chip->device_id, 2) == MXIC_ID; return BYTESHIFT(chip->device_id, 2) == MXIC_ID;
} }
esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void) esp_err_t IRAM_ATTR bootloader_flash_unlock_default(void)
{ {
// At the beginning status == new_status == status_sr2 == new_status_sr2 == 0. // At the beginning status == new_status == status_sr2 == new_status_sr2 == 0.
// If the register doesn't need to be updated, keep them the same (0), so that no command will be actually sent. // If the register doesn't need to be updated, keep them the same (0), so that no command will be actually sent.
@@ -568,6 +568,8 @@ esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void)
return err; return err;
} }
esp_err_t __attribute__((weak, alias("bootloader_flash_unlock_default"))) bootloader_flash_unlock(void);
IRAM_ATTR uint32_t bootloader_flash_execute_command_common( IRAM_ATTR uint32_t bootloader_flash_execute_command_common(
uint8_t command, uint8_t command,
uint32_t addr_len, uint32_t address, uint32_t addr_len, uint32_t address,

View File

@@ -371,6 +371,10 @@ esp_err_t bootloader_init_spi_flash(void)
} }
#endif #endif
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -238,6 +238,9 @@ esp_err_t bootloader_init_spi_flash(void)
bootloader_init_flash_configure(); bootloader_init_flash_configure();
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -247,6 +247,9 @@ esp_err_t bootloader_init_spi_flash(void)
#endif #endif
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -218,6 +218,9 @@ esp_err_t bootloader_init_spi_flash(void)
bootloader_init_flash_configure(); bootloader_init_flash_configure();
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -201,6 +201,9 @@ esp_err_t bootloader_init_spi_flash(void)
{ {
bootloader_init_flash_configure(); bootloader_init_flash_configure();
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -212,6 +212,9 @@ esp_err_t bootloader_init_spi_flash(void)
bootloader_init_mspi_clock(); bootloader_init_mspi_clock();
bootloader_init_flash_configure(); bootloader_init_flash_configure();
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -209,6 +209,9 @@ esp_err_t bootloader_init_spi_flash(void)
{ {
bootloader_init_flash_configure(); bootloader_init_flash_configure();
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -232,6 +232,9 @@ esp_err_t bootloader_init_spi_flash(void)
#endif #endif
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -262,6 +262,10 @@ esp_err_t bootloader_init_spi_flash(void)
} }
#endif #endif
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -286,6 +286,9 @@ esp_err_t bootloader_init_spi_flash(void)
#endif #endif
bootloader_spi_flash_resume(); bootloader_spi_flash_resume();
if ((void*)bootloader_flash_unlock != (void*)bootloader_flash_unlock_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_unlock");
}
bootloader_flash_unlock(); bootloader_flash_unlock();
#if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT #if CONFIG_ESPTOOLPY_FLASHMODE_QIO || CONFIG_ESPTOOLPY_FLASHMODE_QOUT

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -18,6 +18,7 @@
#include "soc/efuse_periph.h" #include "soc/efuse_periph.h"
#include "soc/io_mux_reg.h" #include "soc/io_mux_reg.h"
#include "esp_private/spi_flash_os.h" #include "esp_private/spi_flash_os.h"
#include "bootloader_flash_override.h"
static const char *TAG = "qio_mode"; static const char *TAG = "qio_mode";
@@ -34,7 +35,7 @@ static const char *TAG = "qio_mode";
Searching of this table stops when the first match is found. Searching of this table stops when the first match is found.
*/ */
const bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_list[] = { const DRAM_ATTR bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_list_default[] = {
/* Manufacturer, mfg_id, flash_id, id mask, Read Status, Write Status, QIE Bit */ /* Manufacturer, mfg_id, flash_id, id mask, Read Status, Write Status, QIE Bit */
{ "MXIC", 0xC2, 0x2000, 0xFF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 }, { "MXIC", 0xC2, 0x2000, 0xFF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 },
{ "ISSI", 0x9D, 0x4000, 0xCF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 }, /* IDs 0x40xx, 0x70xx */ { "ISSI", 0x9D, 0x4000, 0xCF00, bootloader_read_status_8b_rdsr, bootloader_write_status_8b_wrsr, 6 }, /* IDs 0x40xx, 0x70xx */
@@ -53,7 +54,9 @@ const bootloader_qio_info_t __attribute__((weak)) bootloader_flash_qe_support_li
{ NULL, 0xFF, 0xFFFF, 0xFFFF, bootloader_read_status_8b_rdsr2, bootloader_write_status_8b_wrsr2, 1 }, { NULL, 0xFF, 0xFFFF, 0xFFFF, bootloader_read_status_8b_rdsr2, bootloader_write_status_8b_wrsr2, 1 },
}; };
#define NUM_CHIPS (sizeof(bootloader_flash_qe_support_list) / sizeof(bootloader_qio_info_t)) const DRAM_ATTR bootloader_qio_info_t* bootloader_flash_qe_support_list __attribute__((weak)) = bootloader_flash_qe_support_list_default;
uint8_t DRAM_ATTR __attribute__((weak)) bootloader_flash_qe_list_count = (sizeof(bootloader_flash_qe_support_list_default) / sizeof(bootloader_qio_info_t));
static esp_err_t enable_qio_mode(bootloader_flash_read_status_fn_t read_status_fn, static esp_err_t enable_qio_mode(bootloader_flash_read_status_fn_t read_status_fn,
bootloader_flash_write_status_fn_t write_status_fn, bootloader_flash_write_status_fn_t write_status_fn,
@@ -82,7 +85,11 @@ void bootloader_enable_qio_mode(void)
flash_id = raw_flash_id & 0xFFFF; flash_id = raw_flash_id & 0xFFFF;
ESP_LOGD(TAG, "Manufacturer ID 0x%02x chip ID 0x%04x", mfg_id, flash_id); ESP_LOGD(TAG, "Manufacturer ID 0x%02x chip ID 0x%04x", mfg_id, flash_id);
for (i = 0; i < NUM_CHIPS - 1; i++) { if ((intptr_t)bootloader_flash_qe_support_list != (intptr_t)bootloader_flash_qe_support_list_default) {
ESP_EARLY_LOGD(TAG, "Using overridden bootloader_flash_qio, the list number is %d", bootloader_flash_qe_list_count);
}
for (i = 0; i < bootloader_flash_qe_list_count - 1; i++) {
const bootloader_qio_info_t *chip = &bootloader_flash_qe_support_list[i]; const bootloader_qio_info_t *chip = &bootloader_flash_qe_support_list[i];
if (mfg_id == chip->mfg_id && (flash_id & chip->id_mask) == (chip->flash_id & chip->id_mask)) { if (mfg_id == chip->mfg_id && (flash_id & chip->id_mask) == (chip->flash_id & chip->id_mask)) {
ESP_LOGI(TAG, "Enabling QIO for flash chip %s", bootloader_flash_qe_support_list[i].manufacturer); ESP_LOGI(TAG, "Enabling QIO for flash chip %s", bootloader_flash_qe_support_list[i].manufacturer);
@@ -90,7 +97,7 @@ void bootloader_enable_qio_mode(void)
} }
} }
if (i == NUM_CHIPS - 1) { if (i == bootloader_flash_qe_list_count - 1) {
ESP_LOGI(TAG, "Enabling default flash chip QIO"); ESP_LOGI(TAG, "Enabling default flash chip QIO");
} }
enable_qio_mode(bootloader_flash_qe_support_list[i].read_status_fn, enable_qio_mode(bootloader_flash_qe_support_list[i].read_status_fn,