2021-05-09 22:35:07 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-11-01 19:41:58 -04:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2020-07-12 15:23:12 -04:00
|
|
|
#include <bootloader_flash_priv.h>
|
2016-11-01 19:41:58 -04:00
|
|
|
#include <esp_log.h>
|
2017-01-26 02:30:32 -05:00
|
|
|
#include <esp_flash_encrypt.h>
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
# include "soc/spi_struct.h"
|
|
|
|
# include "soc/spi_reg.h"
|
|
|
|
/* SPI flash controller */
|
|
|
|
# define SPIFLASH SPI1
|
|
|
|
#else
|
|
|
|
# include "soc/spi_mem_struct.h"
|
|
|
|
# include "soc/spi_mem_reg.h"
|
|
|
|
/* SPI flash controller */
|
|
|
|
# define SPIFLASH SPIMEM1
|
|
|
|
#endif
|
|
|
|
|
2021-05-07 03:25:06 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/spi_flash.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2020-07-29 10:03:46 -04:00
|
|
|
#include "esp32s2/rom/spi_flash.h"
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/spi_flash.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/spi_flash.h"
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
#include "esp32h2/rom/spi_flash.h"
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-01 19:41:58 -04:00
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
|
|
|
|
#define ENCRYPTION_IS_VIRTUAL 1
|
|
|
|
#else
|
|
|
|
#define ENCRYPTION_IS_VIRTUAL 0
|
|
|
|
#endif
|
|
|
|
|
2021-05-07 03:25:06 -04:00
|
|
|
#define BYTESHIFT(VAR, IDX) (((VAR) >> ((IDX) * 8)) & 0xFF)
|
|
|
|
#define ISSI_ID 0x9D
|
|
|
|
#define GD_Q_ID_HIGH 0xC8
|
|
|
|
#define GD_Q_ID_MID 0x40
|
|
|
|
#define GD_Q_ID_LOW 0x16
|
|
|
|
|
|
|
|
#define ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI (BIT7 | BIT5 | BIT4 | BIT3 | BIT2)
|
|
|
|
#define ESP_BOOTLOADER_SPIFLASH_QE_16B BIT9 // QE position when you write 16 bits at one time.
|
|
|
|
#define ESP_BOOTLOADER_SPIFLASH_QE_8B BIT1 // QE position when you write 8 bits(for SR2) at one time.
|
|
|
|
#define ESP_BOOTLOADER_SPIFLASH_WRITE_8B (8)
|
|
|
|
#define ESP_BOOTLOADER_SPIFLASH_WRITE_16B (16)
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
|
2016-11-01 19:41:58 -04:00
|
|
|
#ifndef BOOTLOADER_BUILD
|
|
|
|
/* Normal app version maps to esp_spi_flash.h operations...
|
|
|
|
*/
|
|
|
|
static const char *TAG = "bootloader_mmap";
|
|
|
|
|
2018-03-15 07:58:02 -04:00
|
|
|
static spi_flash_mmap_handle_t map;
|
2016-11-01 19:41:58 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
uint32_t bootloader_mmap_get_free_pages(void)
|
2019-06-15 05:43:51 -04:00
|
|
|
{
|
|
|
|
return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:41:58 -04:00
|
|
|
const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
|
|
|
|
{
|
|
|
|
if (map) {
|
|
|
|
ESP_LOGE(TAG, "tried to bootloader_mmap twice");
|
|
|
|
return NULL; /* existing mapping in use... */
|
|
|
|
}
|
|
|
|
const void *result = NULL;
|
2019-05-27 02:29:43 -04:00
|
|
|
uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
|
2017-06-27 03:25:30 -04:00
|
|
|
size += (src_addr - src_page);
|
|
|
|
esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
|
2016-11-01 19:41:58 -04:00
|
|
|
if (err != ESP_OK) {
|
2018-03-15 07:58:02 -04:00
|
|
|
ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
|
|
|
|
return NULL;
|
2016-11-01 19:41:58 -04:00
|
|
|
}
|
2017-06-27 03:25:30 -04:00
|
|
|
return (void *)((intptr_t)result + (src_addr - src_page));
|
2016-11-01 19:41:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-06 23:45:57 -05:00
|
|
|
void bootloader_munmap(const void *mapping)
|
2016-11-01 19:41:58 -04:00
|
|
|
{
|
2019-05-27 02:29:43 -04:00
|
|
|
if (mapping && map) {
|
2016-11-01 19:41:58 -04:00
|
|
|
spi_flash_munmap(map);
|
|
|
|
}
|
|
|
|
map = 0;
|
|
|
|
}
|
|
|
|
|
2016-11-11 01:00:34 -05:00
|
|
|
esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
|
2016-11-01 19:41:58 -04:00
|
|
|
{
|
2017-01-26 02:30:32 -05:00
|
|
|
if (allow_decrypt && esp_flash_encryption_enabled()) {
|
|
|
|
return spi_flash_read_encrypted(src, dest, size);
|
|
|
|
} else {
|
|
|
|
return spi_flash_read(src, dest, size);
|
|
|
|
}
|
2016-11-01 19:41:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-11 01:00:34 -05:00
|
|
|
esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
|
|
|
|
{
|
2021-06-16 19:21:36 -04:00
|
|
|
if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
return spi_flash_write_encrypted(dest_addr, src, size);
|
2020-11-26 03:56:13 -05:00
|
|
|
#else
|
|
|
|
return esp_rom_spiflash_write_encrypted(dest_addr, src, size);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
} else {
|
|
|
|
return spi_flash_write(dest_addr, src, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t bootloader_flash_erase_sector(size_t sector)
|
|
|
|
{
|
|
|
|
return spi_flash_erase_sector(sector);
|
|
|
|
}
|
|
|
|
|
2018-08-24 08:28:04 -04:00
|
|
|
esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
|
|
|
|
{
|
|
|
|
return spi_flash_erase_range(start_addr, size);
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:23:36 -04:00
|
|
|
#else //BOOTLOADER_BUILD
|
2016-11-01 19:41:58 -04:00
|
|
|
/* Bootloader version, uses ROM functions only */
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-12-26 02:25:24 -05:00
|
|
|
#include "esp32/rom/spi_flash.h"
|
|
|
|
#include "esp32/rom/cache.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/spi_flash.h"
|
|
|
|
#include "esp32s2/rom/cache.h"
|
2019-12-26 02:25:24 -05:00
|
|
|
#include "soc/cache_memory.h"
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/spi_flash.h"
|
|
|
|
#include "esp32s3/rom/cache.h"
|
|
|
|
#include "soc/cache_memory.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/spi_flash.h"
|
|
|
|
#include "esp32c3/rom/cache.h"
|
|
|
|
#include "soc/cache_memory.h"
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
#include "esp32h2/rom/spi_flash.h"
|
|
|
|
#include "esp32h2/rom/cache.h"
|
|
|
|
#include "soc/cache_memory.h"
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-01 19:41:58 -04:00
|
|
|
static const char *TAG = "bootloader_flash";
|
|
|
|
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
/* Use first 50 blocks in MMU for bootloader_mmap,
|
|
|
|
50th block for bootloader_flash_read
|
|
|
|
*/
|
2019-05-27 02:29:43 -04:00
|
|
|
#define MMU_BLOCK0_VADDR SOC_DROM_LOW
|
|
|
|
#define MMU_SIZE (0x320000)
|
|
|
|
#define MMU_BLOCK50_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
|
2019-08-07 23:44:24 -04:00
|
|
|
#define FLASH_READ_VADDR MMU_BLOCK50_VADDR
|
2020-11-26 03:56:13 -05:00
|
|
|
|
|
|
|
#else // !CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2019-05-27 02:29:43 -04:00
|
|
|
/* Use first 63 blocks in MMU for bootloader_mmap,
|
|
|
|
63th block for bootloader_flash_read
|
|
|
|
*/
|
|
|
|
#define MMU_BLOCK0_VADDR SOC_DROM_LOW
|
|
|
|
#define MMU_SIZE (0x3f0000)
|
|
|
|
#define MMU_BLOCK63_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
|
2019-08-07 23:44:24 -04:00
|
|
|
#define FLASH_READ_VADDR MMU_BLOCK63_VADDR
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2019-08-07 23:44:24 -04:00
|
|
|
#define MMU_FREE_PAGES (MMU_SIZE / FLASH_BLOCK_SIZE)
|
2019-12-26 02:25:24 -05:00
|
|
|
|
2016-11-01 19:41:58 -04:00
|
|
|
static bool mapped;
|
|
|
|
|
2017-06-16 02:30:21 -04:00
|
|
|
// Current bootloader mapping (ab)used for bootloader_read()
|
2016-11-11 01:00:34 -05:00
|
|
|
static uint32_t current_read_mapping = UINT32_MAX;
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
uint32_t bootloader_mmap_get_free_pages(void)
|
2019-06-15 05:43:51 -04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads)
|
|
|
|
* Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this.
|
|
|
|
*/
|
|
|
|
return MMU_FREE_PAGES;
|
|
|
|
}
|
|
|
|
|
2016-11-01 19:41:58 -04:00
|
|
|
const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
|
|
|
|
{
|
|
|
|
if (mapped) {
|
|
|
|
ESP_LOGE(TAG, "tried to bootloader_mmap twice");
|
|
|
|
return NULL; /* can't map twice */
|
|
|
|
}
|
2019-05-27 02:29:43 -04:00
|
|
|
if (size > MMU_SIZE) {
|
2016-11-11 01:00:34 -05:00
|
|
|
ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-11-01 19:41:58 -04:00
|
|
|
|
2016-11-11 01:00:34 -05:00
|
|
|
uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
|
2018-09-03 06:15:20 -04:00
|
|
|
uint32_t count = bootloader_cache_pages_to_map(size, src_addr);
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-01 19:41:58 -04:00
|
|
|
Cache_Read_Disable(0);
|
|
|
|
Cache_Flush(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
uint32_t autoload = Cache_Suspend_DCache();
|
|
|
|
Cache_Invalidate_DCache_All();
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2018-09-03 06:15:20 -04:00
|
|
|
ESP_LOGD(TAG, "mmu set paddr=%08x count=%d size=%x src_addr=%x src_addr_aligned=%x",
|
2019-05-27 02:29:43 -04:00
|
|
|
src_addr & MMU_FLASH_MASK, count, size, src_addr, src_addr_aligned );
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2017-04-05 05:26:37 -04:00
|
|
|
int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-12-26 02:25:24 -05:00
|
|
|
int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
|
2021-06-10 07:28:18 -04:00
|
|
|
#else // S3, C3, H2
|
2020-07-29 10:03:46 -04:00
|
|
|
int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2017-04-05 05:26:37 -04:00
|
|
|
if (e != 0) {
|
|
|
|
ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2017-04-05 05:26:37 -04:00
|
|
|
Cache_Read_Enable(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
Cache_Resume_ICache(autoload);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Resume_DCache(autoload);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
Cache_Resume_ICache(autoload);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Resume_ICache(autoload);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2017-04-05 05:26:37 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2017-04-05 05:26:37 -04:00
|
|
|
Cache_Read_Enable(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
Cache_Resume_ICache(autoload);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Resume_DCache(autoload);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
Cache_Resume_ICache(autoload);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Resume_ICache(autoload);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-01 19:41:58 -04:00
|
|
|
|
|
|
|
mapped = true;
|
|
|
|
|
2016-11-11 01:00:34 -05:00
|
|
|
return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
|
2016-11-01 19:41:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-06 23:45:57 -05:00
|
|
|
void bootloader_munmap(const void *mapping)
|
2016-11-01 19:41:58 -04:00
|
|
|
{
|
|
|
|
if (mapped) {
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-01 19:41:58 -04:00
|
|
|
/* Full MMU reset */
|
|
|
|
Cache_Read_Disable(0);
|
|
|
|
Cache_Flush(0);
|
|
|
|
mmu_init(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
//TODO, save the autoload value.
|
|
|
|
Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
|
|
|
Cache_MMU_Init();
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Suspend_DCache();
|
|
|
|
Cache_Invalidate_DCache_All();
|
|
|
|
Cache_MMU_Init();
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
//TODO, save the autoload value.
|
|
|
|
Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
|
|
|
Cache_MMU_Init();
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
|
|
|
Cache_MMU_Init();
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-01 19:41:58 -04:00
|
|
|
mapped = false;
|
2016-11-11 01:00:34 -05:00
|
|
|
current_read_mapping = UINT32_MAX;
|
2016-11-01 19:41:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:29:00 -05:00
|
|
|
static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
|
2016-11-01 19:41:58 -04:00
|
|
|
{
|
2019-05-27 02:29:43 -04:00
|
|
|
switch (r) {
|
2017-03-09 02:29:00 -05:00
|
|
|
case ESP_ROM_SPIFLASH_RESULT_OK:
|
2016-11-01 19:41:58 -04:00
|
|
|
return ESP_OK;
|
2017-03-09 02:29:00 -05:00
|
|
|
case ESP_ROM_SPIFLASH_RESULT_ERR:
|
2016-11-01 19:41:58 -04:00
|
|
|
return ESP_ERR_FLASH_OP_FAIL;
|
2017-03-09 02:29:00 -05:00
|
|
|
case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
|
2016-11-01 19:41:58 -04:00
|
|
|
return ESP_ERR_FLASH_OP_TIMEOUT;
|
|
|
|
default:
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 01:00:34 -05:00
|
|
|
static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
|
|
|
|
{
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
Cache_Read_Disable(0);
|
|
|
|
Cache_Flush(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
uint32_t autoload = Cache_Suspend_DCache();
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2017-03-09 02:29:00 -05:00
|
|
|
esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
Cache_Read_Enable(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
Cache_Resume_ICache(autoload);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Resume_DCache(autoload);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
Cache_Resume_ICache(autoload);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Resume_ICache(autoload);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
|
|
|
|
return spi_to_esp_err(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
|
|
|
|
{
|
|
|
|
uint32_t *dest_words = (uint32_t *)dest;
|
|
|
|
|
2020-11-16 23:48:35 -05:00
|
|
|
for (size_t word = 0; word < size / 4; word++) {
|
2016-11-11 01:00:34 -05:00
|
|
|
uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
|
|
|
|
uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
|
|
|
|
uint32_t *map_ptr;
|
|
|
|
if (map_at != current_read_mapping) {
|
|
|
|
/* Move the 64KB mmu mapping window to fit map_at */
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
Cache_Read_Disable(0);
|
|
|
|
Cache_Flush(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
uint32_t autoload = Cache_Suspend_DCache();
|
|
|
|
Cache_Invalidate_DCache_All();
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
uint32_t autoload = Cache_Suspend_ICache();
|
|
|
|
Cache_Invalidate_ICache_All();
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-08-07 23:44:24 -04:00
|
|
|
int e = cache_flash_mmu_set(0, 0, FLASH_READ_VADDR, map_at, 64, 1);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-12-26 02:25:24 -05:00
|
|
|
int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
if (e != 0) {
|
|
|
|
ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
Cache_Read_Enable(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
Cache_Resume_ICache(autoload);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Resume_DCache(autoload);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
Cache_Resume_ICache(autoload);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Resume_ICache(autoload);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
current_read_mapping = map_at;
|
2019-05-27 02:29:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2016-11-11 01:00:34 -05:00
|
|
|
Cache_Read_Enable(0);
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-05-27 02:29:43 -04:00
|
|
|
Cache_Resume_ICache(autoload);
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
Cache_Resume_DCache(autoload);
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
Cache_Resume_ICache(autoload);
|
2021-06-10 07:28:18 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
Cache_Resume_ICache(autoload);
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2016-11-11 01:00:34 -05:00
|
|
|
}
|
2019-08-07 23:44:24 -04:00
|
|
|
map_ptr = (uint32_t *)(FLASH_READ_VADDR + (word_src - map_at));
|
2016-11-11 01:00:34 -05:00
|
|
|
dest_words[word] = *map_ptr;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
|
|
|
|
{
|
|
|
|
if (src_addr & 3) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
if (size & 3) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
if ((intptr_t)dest & 3) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allow_decrypt) {
|
|
|
|
return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
|
|
|
|
} else {
|
|
|
|
return bootloader_flash_read_no_decrypt(src_addr, dest, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
|
|
|
size_t alignment = write_encrypted ? 32 : 4;
|
|
|
|
if ((dest_addr % alignment) != 0) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
if ((size % alignment) != 0) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
if (((intptr_t)src % 4) != 0) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2021-05-07 03:25:06 -04:00
|
|
|
err = bootloader_flash_unlock();
|
2016-11-11 01:00:34 -05:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:21:36 -04:00
|
|
|
if (write_encrypted && !ENCRYPTION_IS_VIRTUAL) {
|
2017-03-09 02:29:00 -05:00
|
|
|
return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
|
2016-11-11 01:00:34 -05:00
|
|
|
} else {
|
2017-03-09 02:29:00 -05:00
|
|
|
return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
|
2016-11-11 01:00:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t bootloader_flash_erase_sector(size_t sector)
|
|
|
|
{
|
2017-03-09 02:29:00 -05:00
|
|
|
return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
|
2016-11-11 01:00:34 -05:00
|
|
|
}
|
|
|
|
|
2018-08-24 08:28:04 -04:00
|
|
|
esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
|
|
|
|
{
|
|
|
|
if (start_addr % FLASH_SECTOR_SIZE != 0) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (size % FLASH_SECTOR_SIZE != 0) {
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
size_t start = start_addr / FLASH_SECTOR_SIZE;
|
|
|
|
size_t end = start + size / FLASH_SECTOR_SIZE;
|
|
|
|
const size_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
|
|
|
|
|
|
|
|
esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
|
|
|
|
for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
|
|
|
|
if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
|
|
|
|
rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
|
|
|
|
sector += sectors_per_block;
|
|
|
|
} else {
|
|
|
|
rc = esp_rom_spiflash_erase_sector(sector);
|
|
|
|
++sector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return spi_to_esp_err(rc);
|
|
|
|
}
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
|
2021-08-01 02:23:36 -04:00
|
|
|
#endif // BOOTLOADER_BUILD
|
|
|
|
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
|
2021-05-07 03:25:06 -04:00
|
|
|
FORCE_INLINE_ATTR bool is_issi_chip(const esp_rom_spiflash_chip_t* chip)
|
|
|
|
{
|
|
|
|
return BYTESHIFT(chip->device_id, 2) == ISSI_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For GD25Q32, GD25Q64, GD25Q127C, GD25Q128, which use single command to read/write different SR.
|
|
|
|
FORCE_INLINE_ATTR bool is_gd_q_chip(const esp_rom_spiflash_chip_t* chip)
|
|
|
|
{
|
|
|
|
return BYTESHIFT(chip->device_id, 2) == GD_Q_ID_HIGH && BYTESHIFT(chip->device_id, 1) == GD_Q_ID_MID && BYTESHIFT(chip->device_id, 0) >= GD_Q_ID_LOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR __attribute__((weak)) bootloader_flash_unlock(void)
|
|
|
|
{
|
|
|
|
uint16_t status = 0; // status for SR1 or SR1+SR2 if writing SR with 01H + 2Bytes.
|
|
|
|
uint16_t new_status = 0;
|
|
|
|
uint8_t status_sr2 = 0; // status_sr2 for SR2.
|
|
|
|
uint8_t new_status_sr2 = 0;
|
|
|
|
uint8_t write_sr_bit = 0;
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
if (is_issi_chip(&g_rom_flashchip)) {
|
|
|
|
write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_8B;
|
|
|
|
// ISSI chips have different QE position
|
|
|
|
|
|
|
|
status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
|
|
|
|
|
|
|
|
/* Clear all bits in the mask.
|
|
|
|
(This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
|
|
|
|
*/
|
|
|
|
new_status = status & (~ESP_BOOTLOADER_SPIFLASH_BP_MASK_ISSI);
|
|
|
|
// Skip if nothing needs to be cleared. Otherwise will waste time waiting for the flash to clear nothing.
|
|
|
|
} else if (is_gd_q_chip(&g_rom_flashchip)) {
|
|
|
|
/* The GD chips behaviour is to clear all bits in SR1 and clear bits in SR2 except QE bit.
|
|
|
|
Use 01H to write SR1 and 31H to write SR2.
|
|
|
|
*/
|
|
|
|
write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_8B;
|
|
|
|
|
|
|
|
status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8);
|
|
|
|
new_status = 0;
|
|
|
|
|
|
|
|
status_sr2 = bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8);
|
|
|
|
new_status_sr2 = status_sr2 & ESP_BOOTLOADER_SPIFLASH_QE_8B;
|
|
|
|
} else {
|
|
|
|
/* For common behaviour, like XMC chips, Use 01H+2Bytes to write both SR1 and SR2*/
|
|
|
|
write_sr_bit = ESP_BOOTLOADER_SPIFLASH_WRITE_16B;
|
|
|
|
status = bootloader_execute_flash_command(CMD_RDSR, 0, 0, 8) | (bootloader_execute_flash_command(CMD_RDSR2, 0, 0, 8) << 8);
|
|
|
|
|
|
|
|
/* Clear all bits except QE, if it is set.
|
|
|
|
(This is different from ROM esp_rom_spiflash_unlock, which keeps all bits as-is.)
|
|
|
|
*/
|
|
|
|
new_status = status & ESP_BOOTLOADER_SPIFLASH_QE_16B;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != new_status) {
|
|
|
|
/* if the status in SR not equal to the ideal status, the status need to be updated */
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
bootloader_execute_flash_command(CMD_WRSR, new_status, write_sr_bit, 0);
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_sr2 != new_status_sr2) {
|
|
|
|
/* If the status in SR2 not equal to the ideal status, the status need to be updated.
|
|
|
|
It doesn't need to be updated if status in SR2 is 0.
|
|
|
|
Note: if we need to update both SR1 and SR2, the `CMD_WREN` needs to be sent again.
|
|
|
|
*/
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
bootloader_execute_flash_command(CMD_WREN, 0, 0, 0);
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
bootloader_execute_flash_command(CMD_WRSR2, new_status_sr2, write_sr_bit, 0);
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
}
|
|
|
|
|
|
|
|
bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0);
|
|
|
|
esp_rom_spiflash_wait_idle(&g_rom_flashchip);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:23:36 -04:00
|
|
|
/* dummy_len_plus values defined in ROM for SPI flash configuration */
|
2020-12-01 08:34:53 -05:00
|
|
|
#ifndef g_rom_spiflash_dummy_len_plus // ESP32-C3 uses a macro to access ROM data here
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
extern uint8_t g_rom_spiflash_dummy_len_plus[];
|
2020-12-01 08:34:53 -05:00
|
|
|
#endif
|
2021-08-01 02:23:36 -04:00
|
|
|
IRAM_ATTR static uint32_t bootloader_flash_execute_command_common(
|
|
|
|
uint8_t command,
|
|
|
|
uint32_t addr_len, uint32_t address,
|
|
|
|
uint8_t dummy_len,
|
|
|
|
uint8_t mosi_len, uint32_t mosi_data,
|
|
|
|
uint8_t miso_len)
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
{
|
2021-08-01 02:23:36 -04:00
|
|
|
assert(mosi_len <= 32);
|
|
|
|
assert(miso_len <= 32);
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
uint32_t old_ctrl_reg = SPIFLASH.ctrl.val;
|
2021-08-18 11:55:39 -04:00
|
|
|
uint32_t old_user_reg = SPIFLASH.user.val;
|
2021-08-19 05:02:58 -04:00
|
|
|
uint32_t old_user1_reg = SPIFLASH.user1.val;
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
SPIFLASH.ctrl.val = SPI_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
|
2020-11-26 03:56:13 -05:00
|
|
|
#else
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
SPIFLASH.ctrl.val = SPI_MEM_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
|
|
|
|
#endif
|
2021-08-01 02:23:36 -04:00
|
|
|
//command phase
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
SPIFLASH.user.usr_command = 1;
|
|
|
|
SPIFLASH.user2.usr_command_bitlen = 7;
|
|
|
|
SPIFLASH.user2.usr_command_value = command;
|
2021-08-01 02:23:36 -04:00
|
|
|
//addr phase
|
|
|
|
SPIFLASH.user.usr_addr = addr_len > 0;
|
|
|
|
SPIFLASH.user1.usr_addr_bitlen = addr_len - 1;
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2021-08-01 02:23:36 -04:00
|
|
|
SPIFLASH.addr = (addr_len > 0)? (address << (32-addr_len)) : 0;
|
2020-11-26 03:56:13 -05:00
|
|
|
#else
|
2021-08-01 02:23:36 -04:00
|
|
|
SPIFLASH.addr = address;
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
#endif
|
2021-08-01 02:23:36 -04:00
|
|
|
//dummy phase
|
|
|
|
if (miso_len > 0) {
|
|
|
|
uint32_t total_dummy = dummy_len + g_rom_spiflash_dummy_len_plus[1];
|
|
|
|
SPIFLASH.user.usr_dummy = total_dummy > 0;
|
|
|
|
SPIFLASH.user1.usr_dummy_cyclelen = total_dummy - 1;
|
|
|
|
} else {
|
|
|
|
SPIFLASH.user.usr_dummy = 0;
|
|
|
|
SPIFLASH.user1.usr_dummy_cyclelen = 0;
|
|
|
|
}
|
|
|
|
//output data
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
SPIFLASH.user.usr_mosi = mosi_len > 0;
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
SPIFLASH.mosi_dlen.usr_mosi_dbitlen = mosi_len ? (mosi_len - 1) : 0;
|
2020-11-26 03:56:13 -05:00
|
|
|
#else
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
SPIFLASH.mosi_dlen.usr_mosi_bit_len = mosi_len ? (mosi_len - 1) : 0;
|
2016-11-01 19:41:58 -04:00
|
|
|
#endif
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
SPIFLASH.data_buf[0] = mosi_data;
|
2021-08-01 02:23:36 -04:00
|
|
|
//input data
|
|
|
|
SPIFLASH.user.usr_miso = miso_len > 0;
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
SPIFLASH.miso_dlen.usr_miso_dbitlen = miso_len ? (miso_len - 1) : 0;
|
|
|
|
#else
|
|
|
|
SPIFLASH.miso_dlen.usr_miso_bit_len = miso_len ? (miso_len - 1) : 0;
|
|
|
|
#endif
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
|
|
|
|
SPIFLASH.cmd.usr = 1;
|
|
|
|
while (SPIFLASH.cmd.usr != 0) {
|
|
|
|
}
|
|
|
|
SPIFLASH.ctrl.val = old_ctrl_reg;
|
2021-08-18 11:55:39 -04:00
|
|
|
SPIFLASH.user.val = old_user_reg;
|
2021-08-19 05:02:58 -04:00
|
|
|
SPIFLASH.user1.val = old_user1_reg;
|
2021-08-01 02:23:36 -04:00
|
|
|
|
|
|
|
uint32_t ret = SPIFLASH.data_buf[0];
|
|
|
|
if (miso_len < 32) {
|
|
|
|
//set unused bits to 0
|
|
|
|
ret &= ~(UINT32_MAX << miso_len);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t IRAM_ATTR bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len)
|
|
|
|
{
|
|
|
|
const uint8_t addr_len = 0;
|
|
|
|
const uint8_t address = 0;
|
|
|
|
const uint8_t dummy_len = 0;
|
|
|
|
|
|
|
|
return bootloader_flash_execute_command_common(command, addr_len, address,
|
|
|
|
dummy_len, mosi_len, mosi_data, miso_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmd(0x5A) + 24bit address + 8 cycles dummy
|
|
|
|
uint32_t IRAM_ATTR bootloader_flash_read_sfdp(uint32_t sfdp_addr, unsigned int miso_byte_num)
|
|
|
|
{
|
|
|
|
assert(miso_byte_num <= 4);
|
|
|
|
const uint8_t command = CMD_RDSFDP;
|
|
|
|
const uint8_t addr_len = 24;
|
|
|
|
const uint8_t dummy_len = 8;
|
|
|
|
const uint8_t mosi_len = 0;
|
|
|
|
const uint32_t mosi_data = 0;
|
|
|
|
const uint8_t miso_len = miso_byte_num * 8;
|
|
|
|
|
|
|
|
return bootloader_flash_execute_command_common(command, addr_len, sfdp_addr,
|
|
|
|
dummy_len, mosi_len, mosi_data, miso_len);
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_enable_wp(void)
|
|
|
|
{
|
|
|
|
bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
|
|
|
|
}
|
|
|
|
|
2021-08-01 02:23:36 -04:00
|
|
|
uint32_t IRAM_ATTR bootloader_read_flash_id(void)
|
|
|
|
{
|
|
|
|
uint32_t id = bootloader_execute_flash_command(CMD_RDID, 0, 0, 24);
|
|
|
|
id = ((id & 0xff) << 16) | ((id >> 16) & 0xff) | (id & 0xff00);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
bootloader: fix the WRSR format for ISSI flash chips
1. The 2nd bootloader always call `rom_spiflash_unlock()`, but never help to clear the WEL bit when exit. This may cause system unstability.
This commit helps to clear WEL when flash configuration is done.
**RISK:** When the app starts, it didn't have to clear the WEL before it actually write/erase. But now the very first write/erase operation should be done after a WEL clear. Though the risk is little (all the following write/erase also need to clear the WEL), we still have to test this carefully, especially for those functions used by the OTA.
2. The `rom_spiflash_unlock()` function in the patch of ESP32 may (1) trigger the QPI, (2) clear the QE or (3) fail to unlock the ISSI chips.
Status register bitmap of ISSI chip and GD chip:
| SR | ISSI | GD25LQ32C |
| -- | ---- | --------- |
| 0 | WIP | WIP |
| 1 | WEL | WEL |
| 2 | BP0 | BP0 |
| 3 | BP1 | BP1 |
| 4 | BP2 | BP2 |
| 5 | BP3 | BP3 |
| 6 | QE | BP4 |
| 7 | SRWD | SRP0 |
| 8 | | SRP1 |
| 9 | | QE |
| 10 | | SUS2 |
| 11 | | LB1 |
| 12 | | LB2 |
| 13 | | LB3 |
| 14 | | CMP |
| 15 | | SUS1 |
QE bit of other chips are at the bit 9 of the status register (i.e. bit 1 of SR2), which should be read by RDSR2 command.
However, the RDSR2 (35H, Read Status 2) command for chip of other vendors happens to be the QIOEN (Enter QPI mode) command of ISSI chips. When the `rom_spiflash_unlock()` function trys to read SR2, it may trigger the QPI of ISSI chips.
Moreover, when `rom_spiflash_unlock()` try to clear the BP4 bit in the status register, QE (bit 6) of ISSI chip may be cleared by accident. Or if the ISSI chip doesn't accept WRSR command with argument of two bytes (since it only have status register of one byte), it may fail to clear the other protect bits (BP0~BP3) as expected.
This commit makes the `rom_spiflash_unlock()` check whether the vendor is issi. if so, `rom_spiflash_unlock()` only send RDSR to read the status register, send WRSR with only 1 byte argument, and also avoid clearing the QE bit (bit 6).
3. `rom_spiflash_unlock()` always send WRSR command to clear protection bits even when there is no protection bit active. And the execution of clearing status registers, which takes about 700us, will also happen even when there's no bits cleared.
This commit skips the clearing of status register if there is no protection bits active.
Also move the execute_flash_command to be a bootloader API; move
implementation of spi_flash_wrap_set to the bootloader
2020-03-12 06:20:31 -04:00
|
|
|
#if SOC_CACHE_SUPPORT_WRAP
|
|
|
|
esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode)
|
|
|
|
{
|
|
|
|
uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
|
|
|
|
uint32_t reg_bkp_usr = SPIFLASH.user.val;
|
|
|
|
SPIFLASH.user.fwrite_dio = 0;
|
|
|
|
SPIFLASH.user.fwrite_dual = 0;
|
|
|
|
SPIFLASH.user.fwrite_qio = 1;
|
|
|
|
SPIFLASH.user.fwrite_quad = 0;
|
|
|
|
SPIFLASH.ctrl.fcmd_dual = 0;
|
|
|
|
SPIFLASH.ctrl.fcmd_quad = 0;
|
|
|
|
SPIFLASH.user.usr_dummy = 0;
|
|
|
|
SPIFLASH.user.usr_addr = 1;
|
|
|
|
SPIFLASH.user.usr_command = 1;
|
|
|
|
SPIFLASH.user2.usr_command_bitlen = 7;
|
|
|
|
SPIFLASH.user2.usr_command_value = CMD_WRAP;
|
|
|
|
SPIFLASH.user1.usr_addr_bitlen = 23;
|
|
|
|
SPIFLASH.addr = 0;
|
|
|
|
SPIFLASH.user.usr_miso = 0;
|
|
|
|
SPIFLASH.user.usr_mosi = 1;
|
|
|
|
SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
|
|
|
|
SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
|
|
|
|
SPIFLASH.cmd.usr = 1;
|
|
|
|
while(SPIFLASH.cmd.usr != 0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SPIFLASH.ctrl.val = reg_bkp_ctrl;
|
|
|
|
SPIFLASH.user.val = reg_bkp_usr;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
#endif //SOC_CACHE_SUPPORT_WRAP
|
2021-08-01 02:23:36 -04:00
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* XMC startup flow
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#define XMC_SUPPORT CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT
|
|
|
|
#define XMC_VENDOR_ID 0x20
|
|
|
|
|
|
|
|
#if BOOTLOADER_BUILD
|
|
|
|
#define BOOTLOADER_FLASH_LOG(level, ...) ESP_LOG##level(TAG, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
static DRAM_ATTR char bootloader_flash_tag[] = "bootloader_flash";
|
|
|
|
#define BOOTLOADER_FLASH_LOG(level, ...) ESP_DRAM_LOG##level(bootloader_flash_tag, ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if XMC_SUPPORT
|
|
|
|
//strictly check the model
|
|
|
|
static IRAM_ATTR bool is_xmc_chip_strict(uint32_t rdid)
|
|
|
|
{
|
|
|
|
uint32_t vendor_id = BYTESHIFT(rdid, 2);
|
|
|
|
uint32_t mfid = BYTESHIFT(rdid, 1);
|
|
|
|
uint32_t cpid = BYTESHIFT(rdid, 0);
|
|
|
|
|
|
|
|
if (vendor_id != XMC_VENDOR_ID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matched = false;
|
|
|
|
if (mfid == 0x40) {
|
|
|
|
if (cpid >= 0x13 && cpid <= 0x20) {
|
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
} else if (mfid == 0x41) {
|
|
|
|
if (cpid >= 0x17 && cpid <= 0x20) {
|
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
} else if (mfid == 0x50) {
|
|
|
|
if (cpid >= 0x15 && cpid <= 0x16) {
|
|
|
|
matched = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matched;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR bootloader_flash_xmc_startup(void)
|
|
|
|
{
|
|
|
|
// If the RDID value is a valid XMC one, may skip the flow
|
|
|
|
const bool fast_check = true;
|
|
|
|
if (fast_check && is_xmc_chip_strict(g_rom_flashchip.device_id)) {
|
|
|
|
BOOTLOADER_FLASH_LOG(D, "XMC chip detected by RDID (%08X), skip.", g_rom_flashchip.device_id);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the Manufacturer ID in SFDP registers (JEDEC standard). If not XMC chip, no need to run the flow
|
|
|
|
const int sfdp_mfid_addr = 0x10;
|
|
|
|
uint8_t mf_id = (bootloader_flash_read_sfdp(sfdp_mfid_addr, 1) & 0xff);
|
|
|
|
if (mf_id != XMC_VENDOR_ID) {
|
|
|
|
BOOTLOADER_FLASH_LOG(D, "non-XMC chip detected by SFDP Read (%02X), skip.", mf_id);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOTLOADER_FLASH_LOG(I, "XM25QHxxC startup flow");
|
|
|
|
// Enter DPD
|
|
|
|
bootloader_execute_flash_command(0xB9, 0, 0, 0);
|
|
|
|
// Enter UDPD
|
|
|
|
bootloader_execute_flash_command(0x79, 0, 0, 0);
|
|
|
|
// Exit UDPD
|
|
|
|
bootloader_execute_flash_command(0xFF, 0, 0, 0);
|
|
|
|
// Delay tXUDPD
|
|
|
|
esp_rom_delay_us(2000);
|
|
|
|
// Release Power-down
|
|
|
|
bootloader_execute_flash_command(0xAB, 0, 0, 0);
|
|
|
|
esp_rom_delay_us(20);
|
|
|
|
// Read flash ID and check again
|
|
|
|
g_rom_flashchip.device_id = bootloader_read_flash_id();
|
|
|
|
if (!is_xmc_chip_strict(g_rom_flashchip.device_id)) {
|
|
|
|
BOOTLOADER_FLASH_LOG(E, "XMC flash startup fail");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
//only compare the vendor id
|
|
|
|
static IRAM_ATTR bool is_xmc_chip(uint32_t rdid)
|
|
|
|
{
|
|
|
|
uint32_t vendor_id = (rdid >> 16) & 0xFF;
|
|
|
|
return (vendor_id == XMC_VENDOR_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t IRAM_ATTR bootloader_flash_xmc_startup(void)
|
|
|
|
{
|
|
|
|
if (is_xmc_chip(g_rom_flashchip.device_id)) {
|
|
|
|
BOOTLOADER_FLASH_LOG(E, "XMC chip detected (%08X) while support disabled.", g_rom_flashchip.device_id);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //XMC_SUPPORT
|