From 15fa7843ae1222d944d6a575568eb6b13f0ce9f1 Mon Sep 17 00:00:00 2001 From: "C.S.M" Date: Thu, 10 Apr 2025 15:31:06 +0800 Subject: [PATCH] refactor(spi_flash): Add spi_flash driver list linked check --- components/spi_flash/esp_flash_spi_init.c | 4 ++ .../include/spi_flash/spi_flash_defs.h | 12 +++- .../spi_flash/include/spi_flash_chip_boya.h | 23 ++++---- .../spi_flash/include/spi_flash_chip_driver.h | 14 ++++- .../spi_flash/include/spi_flash_chip_gd.h | 23 ++++---- .../spi_flash/include/spi_flash_chip_issi.h | 23 ++++---- .../spi_flash/include/spi_flash_chip_mxic.h | 28 +++++----- .../spi_flash/include/spi_flash_chip_th.h | 7 ++- .../include/spi_flash_chip_winbond.h | 25 ++++----- components/spi_flash/spi_flash_chip_drivers.c | 56 ++++++++++++++++++- tools/ci/check_copyright_ignore.txt | 5 -- 11 files changed, 144 insertions(+), 76 deletions(-) diff --git a/components/spi_flash/esp_flash_spi_init.c b/components/spi_flash/esp_flash_spi_init.c index 6bca730e98..e0f30a9d0b 100644 --- a/components/spi_flash/esp_flash_spi_init.c +++ b/components/spi_flash/esp_flash_spi_init.c @@ -17,6 +17,7 @@ #include "esp_private/spi_share_hw_ctrl.h" #include "esp_ldo_regulator.h" #include "hal/spi_flash_hal.h" +#include "spi_flash_chip_driver.h" #include "hal/gpio_hal.h" #include "esp_flash_internal.h" #include "esp_rom_gpio.h" @@ -370,6 +371,9 @@ esp_err_t esp_flash_init_default_chip(void) s_esp_flash_choose_correct_mode(&cfg); #endif + #if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST + spi_flash_chip_list_check(&default_chip, legacy_chip->device_id); + #endif // For chips need time tuning, get value directly from system here. #if SOC_SPI_MEM_SUPPORT_TIMING_TUNING diff --git a/components/spi_flash/include/spi_flash/spi_flash_defs.h b/components/spi_flash/include/spi_flash/spi_flash_defs.h index c8629e1bf1..a7d5cbe81f 100644 --- a/components/spi_flash/include/spi_flash/spi_flash_defs.h +++ b/components/spi_flash/include/spi_flash/spi_flash_defs.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -80,3 +80,13 @@ #define SPI_FLASH_OPIDTR_DUMMY_BITLEN 40 #define SPI_FLASH_QIO_HPM_DUMMY_BITLEN 10 #define SPI_FLASH_DIO_HPM_DUMMY_BITLEN 8 + +// Flash vendors. +#define SPI_FLASH_GD 0xC8 +#define SPI_FLASH_ISSI 0x9D +#define SPI_FLASH_MXIC 0xC2 +#define SPI_FLASH_XMC_1 0x20 +#define SPI_FLASH_XMC_2 0x46 +#define SPI_FLASH_WINBOND 0xEF +#define SPI_FLASH_TH 0xCD +#define SPI_FLASH_BY 0x68 diff --git a/components/spi_flash/include/spi_flash_chip_boya.h b/components/spi_flash/include/spi_flash_chip_boya.h index 58139b3c94..02fd05c6b1 100644 --- a/components/spi_flash/include/spi_flash_chip_boya.h +++ b/components/spi_flash/include/spi_flash_chip_boya.h @@ -1,21 +1,18 @@ -// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" +#include "sdkconfig.h" +#ifdef CONFIG_SPI_FLASH_SUPPORT_BOYA_CHIP extern const spi_flash_chip_t esp_flash_chip_boya; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_boya; +#endif diff --git a/components/spi_flash/include/spi_flash_chip_driver.h b/components/spi_flash/include/spi_flash_chip_driver.h index 5c05da72c5..5e769aa2e0 100644 --- a/components/spi_flash/include/spi_flash_chip_driver.h +++ b/components/spi_flash/include/spi_flash_chip_driver.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -212,3 +212,15 @@ struct spi_flash_chip_t { This pointer can be overwritten with a pointer to a new array, to update the list of known flash chips. */ extern const spi_flash_chip_t **esp_flash_registered_chips; + +/** +* @brief Helper function to check if a specific flash chip driver is linked. + * + * Logs a warning if the driver for the detected chip is not linked. + * + * @param chip Pointer to an `esp_flash_t` structure representing the flash chip instance. + * This structure typically contains information about the chip's characteristics and state. + * @param device_id A 24-bit value representing the unique identifier (ID) of the flash chip. + * This ID is used to match the chip against known drivers. + */ +void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id); diff --git a/components/spi_flash/include/spi_flash_chip_gd.h b/components/spi_flash/include/spi_flash_chip_gd.h index 09e1661174..34b84f841d 100644 --- a/components/spi_flash/include/spi_flash_chip_gd.h +++ b/components/spi_flash/include/spi_flash_chip_gd.h @@ -1,22 +1,15 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" +#include "sdkconfig.h" /** @@ -33,4 +26,8 @@ esp_err_t spi_flash_chip_gd_probe(esp_flash_t *chip, uint32_t flash_id); esp_err_t spi_flash_chip_gd_set_io_mode(esp_flash_t *chip); esp_err_t spi_flash_chip_gd_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode); +#ifdef CONFIG_SPI_FLASH_SUPPORT_GD_CHIP extern const spi_flash_chip_t esp_flash_chip_gd; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_gd; +#endif diff --git a/components/spi_flash/include/spi_flash_chip_issi.h b/components/spi_flash/include/spi_flash_chip_issi.h index 2f1b063539..58a8aac222 100644 --- a/components/spi_flash/include/spi_flash_chip_issi.h +++ b/components/spi_flash/include/spi_flash_chip_issi.h @@ -1,22 +1,15 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" +#include "sdkconfig.h" /** @@ -28,4 +21,8 @@ esp_err_t spi_flash_chip_issi_probe(esp_flash_t *chip, uint32_t flash_id); esp_err_t spi_flash_chip_issi_set_io_mode(esp_flash_t *chip); esp_err_t spi_flash_chip_issi_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode); +#ifdef CONFIG_SPI_FLASH_SUPPORT_ISSI_CHIP extern const spi_flash_chip_t esp_flash_chip_issi; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_issi; +#endif diff --git a/components/spi_flash/include/spi_flash_chip_mxic.h b/components/spi_flash/include/spi_flash_chip_mxic.h index 201ad496e2..cf77ad92d8 100644 --- a/components/spi_flash/include/spi_flash_chip_mxic.h +++ b/components/spi_flash/include/spi_flash_chip_mxic.h @@ -1,34 +1,34 @@ -// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" - +#include "sdkconfig.h" /** * MXIC SPI flash chip_drv, uses all the above functions for its operations. In * default autodetection, this is used as a catchall if a more specific chip_drv * is not found. */ +#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_CHIP extern const spi_flash_chip_t esp_flash_chip_mxic; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic; +#endif /** * MXIC OPI flash chip_drv, uses all the above functions for its operations. In * default autodetection, this is used as a catchall if a more specific chip_drv * is not found. */ +#ifdef CONFIG_SPI_FLASH_SUPPORT_MXIC_OPI_CHIP extern const spi_flash_chip_t esp_flash_chip_mxic_opi; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_mxic_opi; +#endif diff --git a/components/spi_flash/include/spi_flash_chip_th.h b/components/spi_flash/include/spi_flash_chip_th.h index 7f631faf13..bdab41ee56 100644 --- a/components/spi_flash/include/spi_flash_chip_th.h +++ b/components/spi_flash/include/spi_flash_chip_th.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,5 +9,10 @@ #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" +#include "sdkconfig.h" +#ifdef CONFIG_SPI_FLASH_SUPPORT_TH_CHIP extern const spi_flash_chip_t esp_flash_chip_th; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_th; +#endif diff --git a/components/spi_flash/include/spi_flash_chip_winbond.h b/components/spi_flash/include/spi_flash_chip_winbond.h index 23dc19c259..b6b4287c19 100644 --- a/components/spi_flash/include/spi_flash_chip_winbond.h +++ b/components/spi_flash/include/spi_flash_chip_winbond.h @@ -1,27 +1,24 @@ -// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once #include #include "esp_flash.h" #include "spi_flash_chip_driver.h" - +#include "sdkconfig.h" /** * Winbond SPI flash chip_drv, uses all the above functions for its operations. In * default autodetection, this is used as a catchall if a more specific chip_drv * is not found. */ + +#ifdef CONFIG_SPI_FLASH_SUPPORT_WINBOND_CHIP extern const spi_flash_chip_t esp_flash_chip_winbond; +#else +extern __attribute__((weak)) const spi_flash_chip_t esp_flash_chip_winbond; +#endif diff --git a/components/spi_flash/spi_flash_chip_drivers.c b/components/spi_flash/spi_flash_chip_drivers.c index 51247f7b9e..b513cad6e2 100644 --- a/components/spi_flash/spi_flash_chip_drivers.c +++ b/components/spi_flash/spi_flash_chip_drivers.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -13,7 +13,11 @@ #include "spi_flash_chip_winbond.h" #include "spi_flash_chip_boya.h" #include "spi_flash_chip_th.h" +#include "spi_flash_defs.h" #include "sdkconfig.h" +#include "esp_log.h" + +#define TAG "spi_flash" #if !CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST /* @@ -52,6 +56,56 @@ static const spi_flash_chip_t *default_registered_chips[] = { &esp_flash_chip_generic, NULL, }; + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Waddress" +void spi_flash_chip_list_check(esp_flash_t *chip, uint32_t device_id) { + uint8_t vendor_id = device_id >> 16; + switch (vendor_id) + { + case SPI_FLASH_GD: + if (&esp_flash_chip_gd == NULL) { + ESP_EARLY_LOGW(TAG, "GigaDevice detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_GD_CHIP`"); + } + break; + case SPI_FLASH_ISSI: + if (&esp_flash_chip_issi == NULL) { + ESP_EARLY_LOGW(TAG, "ISSI detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_ISSI_CHIP`"); + } + break; + case SPI_FLASH_TH: + if (&esp_flash_chip_th == NULL) { + ESP_EARLY_LOGW(TAG, "TH detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_TH_CHIP`"); + } + break; + case SPI_FLASH_WINBOND: + if (&esp_flash_chip_winbond == NULL) { + ESP_EARLY_LOGW(TAG, "winbond detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_WINBOND_CHIP`"); + } + break; + case SPI_FLASH_MXIC: + // Need to tell the difference between octal and quad flash. + if (chip->read_mode < SPI_FLASH_OPI_FLAG) { + if (&esp_flash_chip_mxic == NULL) { + ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_CHIP`"); + } + } else { + if (&esp_flash_chip_mxic_opi == NULL) { + ESP_EARLY_LOGW(TAG, "MXIC detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_MXIC_OPI_CHIP`"); + } + } + break; + case SPI_FLASH_BY: + if (&esp_flash_chip_boya == NULL) { + ESP_EARLY_LOGW(TAG, "boya detected but related driver is not linked, please check option `SPI_FLASH_SUPPORT_BOYA_CHIP`"); + } + break; + default: + break; + } +} +#pragma GCC diagnostic pop + #else //When the config option is enabled, user should provide this struct themselves. extern const spi_flash_chip_t *default_registered_chips[]; diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 93258a6647..9ba42000c9 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -515,12 +515,7 @@ components/soc/esp32s2/include/soc/nrx_reg.h components/soc/esp32s2/include/soc/soc_ulp.h components/soc/esp32s2/include/soc/touch_sensor_channel.h components/soc/esp32s2/include/soc/touch_sensor_pins.h -components/spi_flash/include/spi_flash_chip_boya.h -components/spi_flash/include/spi_flash_chip_gd.h components/spi_flash/include/spi_flash_chip_generic.h -components/spi_flash/include/spi_flash_chip_issi.h -components/spi_flash/include/spi_flash_chip_mxic.h -components/spi_flash/include/spi_flash_chip_winbond.h components/spi_flash/spi_flash_chip_boya.c components/spi_flash/spi_flash_chip_issi.c components/tcp_transport/include/esp_transport_ws.h