1 /*
2  * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "flash_init.h"
8 
9 #include <stdbool.h>
10 #include <bootloader_flash_priv.h>
11 #include <esp_flash_internal.h>
12 #include <esp_private/mspi_timing_tuning.h>
13 #include <esp_private/esp_mmu_map_private.h>
14 #include <hal/efuse_ll.h>
15 #include <hal/efuse_hal.h>
16 #include "esp_private/spi_flash_os.h"
17 #include "esp_log.h"
18 #if CONFIG_SOC_SERIES_ESP32S3
19 #include <esp32s3/opi_flash_private.h>
20 #endif
21 
22 static const char *TAG = "flash_init";
23 
flash_is_octal_mode_enabled(void)24 bool flash_is_octal_mode_enabled(void)
25 {
26 #if SOC_SPI_MEM_SUPPORT_OPI_MODE
27 	return efuse_ll_get_flash_type();
28 #else
29 	return false;
30 #endif
31 }
32 
spi_flash_init_chip_state(void)33 int spi_flash_init_chip_state(void)
34 {
35 #if SOC_SPI_MEM_SUPPORT_OPI_MODE
36 	if (flash_is_octal_mode_enabled()) {
37 		return esp_opiflash_init(rom_spiflash_legacy_data->chip.device_id);
38 	}
39 #endif
40 #if CONFIG_SPI_FLASH_HPM_ON
41 	/* Currently, only esp32s3 allows high performance mode. */
42 	return spi_flash_enable_high_performance_mode();
43 #else
44 	return 0;
45 #endif /* CONFIG_SOC_SERIES_ESP32S3 */
46 }
47 
esp_flash_config(void)48 void esp_flash_config(void)
49 {
50 	esp_err_t ret;
51 
52 	spi_flash_init_chip_state();
53 
54 	esp_flash_app_init();
55 
56 	ret = esp_flash_init_default_chip();
57 	if (ret != ESP_OK) {
58 		ESP_EARLY_LOGE(TAG, "Failed to init flash chip: %d ", ret);
59 		abort();
60 	}
61 
62 	esp_mspi_pin_init();
63 
64 #if SOC_MEMSPI_SRC_FREQ_120M
65 	mspi_timing_flash_tuning();
66 #endif
67 
68 	esp_mmu_map_init();
69 }
70