1 /* 2 * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 /** 8 * System level MSPI APIs (private) 9 */ 10 #pragma once 11 12 #include <stdint.h> 13 #include <stdbool.h> 14 #include "sdkconfig.h" 15 #include "esp_rom_spiflash.h" 16 #include "esp_err.h" 17 #include "esp_flash.h" 18 #include "hal/spi_flash_hal.h" 19 #include "spi_flash_override.h" 20 #include "soc/soc_caps.h" 21 #include "soc/clk_tree_defs.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 // Type of MSPI IO 28 typedef enum { 29 ESP_MSPI_IO_CLK = 0, 30 ESP_MSPI_IO_Q, 31 ESP_MSPI_IO_D, 32 ESP_MSPI_IO_CS0, /* cs for spi flash */ 33 ESP_MSPI_IO_HD, 34 ESP_MSPI_IO_WP, 35 #if SOC_SPI_MEM_SUPPORT_OPI_MODE 36 ESP_MSPI_IO_DQS, 37 ESP_MSPI_IO_D4, 38 ESP_MSPI_IO_D5, 39 ESP_MSPI_IO_D6, 40 ESP_MSPI_IO_D7, 41 #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE 42 #if CONFIG_SPIRAM 43 ESP_MSPI_IO_CS1, /* cs for spi ram */ 44 #endif 45 ESP_MSPI_IO_MAX, /* Maximum IO MSPI occupied */ 46 } esp_mspi_io_t; 47 48 /** 49 * @brief To setup Flash chip 50 */ 51 esp_err_t spi_flash_init_chip_state(void); 52 53 /** 54 * @brief To initislize the MSPI pins 55 */ 56 void esp_mspi_pin_init(void); 57 58 /** 59 * @brief Get the number of the GPIO corresponding to the given MSPI io 60 * 61 * @param[in] io MSPI io 62 * 63 * @return MSPI IO number 64 */ 65 uint8_t esp_mspi_get_io(esp_mspi_io_t io); 66 67 /** 68 * @brief Set SPI1 registers to make ROM functions work 69 * @note This function is used for setting SPI1 registers to the state that ROM SPI functions work 70 */ 71 void spi_flash_set_rom_required_regs(void); 72 73 /** 74 * @brief Initialize main flash 75 * @param chip Pointer to main SPI flash(SPI1 CS0) chip to use.. 76 */ 77 esp_err_t esp_flash_init_main(esp_flash_t *chip); 78 79 /** 80 * @brief Should be only used by SPI1 Flash driver to know the necessary timing registers 81 * @param out_timing_config Pointer to timing_tuning parameters. 82 */ 83 void spi_timing_get_flash_timing_param(spi_flash_hal_timing_config_t *out_timing_config); 84 85 /** 86 * @brief Get the knowledge if the MSPI timing is tuned or not 87 */ 88 bool spi_timing_is_tuned(void); 89 90 /** 91 * @brief Set Flash chip specifically required MSPI register settings here 92 */ 93 void spi_flash_set_vendor_required_regs(void); 94 95 /** 96 * @brief Judge whether need to reset flash when brownout. 97 * Set` flash_brownout_needs_reset` inside the function if really need reset. 98 */ 99 void spi_flash_needs_reset_check(void); 100 101 /** 102 * @brief Set flag to reset flash. set when erase chip or program chip 103 * 104 * @param bool status. True if flash is eraing. False if flash is not erasing. 105 * 106 * @return None. 107 */ 108 void spi_flash_set_erasing_flag(bool status); 109 110 /** 111 * @brief Judge whether need to reset flash when brownout. 112 * 113 * @return true if need reset, otherwise false. 114 */ 115 bool spi_flash_brownout_need_reset(void); 116 117 /** 118 * @brief Enable SPI flash high performance mode. 119 * 120 * @return ESP_OK if success. 121 */ 122 esp_err_t spi_flash_enable_high_performance_mode(void); 123 124 /** 125 * @brief Get the flash dummy through this function 126 * This can be used when one flash has several dummy configurations to enable the high performance mode. 127 * @note Don't forget to subtract one when assign to the register of mspi e.g. if the value you get is 4, (4-1=3) should be assigned to the register. 128 * 129 * @return Pointer to spi_flash_hpm_dummy_conf_t. 130 */ 131 const spi_flash_hpm_dummy_conf_t *spi_flash_hpm_get_dummy(void); 132 133 /** 134 * @brief Used to judge whether flash works under HPM mode with dummy adjustment. 135 * 136 * @return true Yes, and work under HPM with adjusting dummy. Otherwise, false. 137 */ 138 bool spi_flash_hpm_dummy_adjust(void); 139 140 #if SOC_SPI_MEM_SUPPORT_WRAP 141 /** 142 * @brief set wrap size of flash 143 * 144 * @param wrap_size: wrap mode support disable, 16 32, 64 byte 145 * 146 * @return esp_err_t : ESP_OK for successful. 147 * 148 */ 149 esp_err_t spi_flash_wrap_enable(spi_flash_wrap_size_t wrap_size); 150 151 /** 152 * @brief Probe flash wrap method 153 * 154 * @return esp_err_t: ESP_OK for success 155 */ 156 esp_err_t spi_flash_wrap_probe(void); 157 158 /** 159 * @brief disable cache wrap 160 */ 161 esp_err_t spi_flash_wrap_disable(void); 162 163 /** 164 * @brief Check whether flash and esp chip supports wrap mode. 165 * 166 * @param wrap_size wrap size. 167 * @return true: wrap support, otherwise, false. 168 */ 169 bool spi_flash_support_wrap_size(uint32_t wrap_size); 170 171 #endif //SOC_SPI_MEM_SUPPORT_WRAP 172 173 /** 174 * @brief SPI flash critical section enter function. 175 * 176 */ 177 typedef void (*spi_flash_guard_start_func_t)(void); 178 /** 179 * @brief SPI flash critical section exit function. 180 */ 181 typedef void (*spi_flash_guard_end_func_t)(void); 182 183 /** 184 * Structure holding SPI flash access critical sections management functions. 185 * 186 * Flash API uses two types of flash access management functions: 187 * 1) Functions which prepare/restore flash cache and interrupts before calling 188 * appropriate ROM functions (SPIWrite, SPIRead and SPIEraseBlock): 189 * - 'start' function should disables flash cache and non-IRAM interrupts and 190 * is invoked before the call to one of ROM function above. 191 * - 'end' function should restore state of flash cache and non-IRAM interrupts and 192 * is invoked after the call to one of ROM function above. 193 * These two functions are not recursive. 194 * 195 * Different versions of the guarding functions should be used depending on the context of 196 * execution (with or without functional OS). In normal conditions when flash API is called 197 * from task the functions use OS primitives. When there is no OS at all or when 198 * it is not guaranteed that OS is functional (accessing flash from exception handler) these 199 * functions cannot use OS primitives or even does not need them (multithreaded access is not possible). 200 * 201 * @note Structure and corresponding guard functions should not reside in flash. 202 * For example structure can be placed in DRAM and functions in IRAM sections. 203 */ 204 typedef struct { 205 spi_flash_guard_start_func_t start; /**< critical section start function. */ 206 spi_flash_guard_end_func_t end; /**< critical section end function. */ 207 } spi_flash_guard_funcs_t; 208 209 210 /** 211 * @brief Sets guard functions to access flash. 212 * 213 * @note Pointed structure and corresponding guard functions should not reside in flash. 214 * For example structure can be placed in DRAM and functions in IRAM sections. 215 * 216 * @param funcs pointer to structure holding flash access guard functions. 217 */ 218 void spi_flash_guard_set(const spi_flash_guard_funcs_t* funcs); 219 220 /** 221 * @brief Get the guard functions used for flash access 222 * 223 * @return The guard functions that were set via spi_flash_guard_set(). These functions 224 * can be called if implementing custom low-level SPI flash operations. 225 */ 226 const spi_flash_guard_funcs_t *spi_flash_guard_get(void); 227 228 /** 229 * @brief Default OS-aware flash access guard functions 230 */ 231 extern const spi_flash_guard_funcs_t g_flash_guard_default_ops; 232 233 /** 234 * @brief Non-OS flash access guard functions 235 * 236 * @note This version of flash guard functions is to be used when no OS is present or from panic handler. 237 * It does not use any OS primitives and IPC and implies that only calling CPU is active. 238 */ 239 extern const spi_flash_guard_funcs_t g_flash_guard_no_os_ops; 240 241 /** 242 * @brief This function is used to re-initialize the flash mmap when using ROM flash 243 * implementations. 244 * 245 * @note Only called in startup. User should not call this function. 246 */ 247 void spi_flash_rom_impl_init(void); 248 249 #if SOC_MEMSPI_CLOCK_IS_INDEPENDENT 250 /** 251 * @brief This functions is used to change spi flash clock source between PLL and others, which is used after system wake up from a low power mode or 252 * enter low-power mode like sleep. 253 * @param clk_src mspi(flash) clock source. 254 * 255 * @note Only called in startup. User should not call this function. 256 */ 257 void spi_flash_set_clock_src(soc_periph_mspi_clk_src_t clk_src); 258 #endif 259 260 #ifdef __cplusplus 261 } 262 #endif 263