1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef ESP_SPI_FLASH_H 16 #define ESP_SPI_FLASH_H 17 18 #include <stdint.h> 19 #include <stdbool.h> 20 #include <stddef.h> 21 #include "esp_err.h" 22 #include "sdkconfig.h" 23 #include "esp_spi_flash_counters.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 #define ESP_ERR_FLASH_OP_FAIL (ESP_ERR_FLASH_BASE + 1) 30 #define ESP_ERR_FLASH_OP_TIMEOUT (ESP_ERR_FLASH_BASE + 2) 31 32 #define SPI_FLASH_SEC_SIZE 4096 /**< SPI Flash sector size */ 33 34 #define SPI_FLASH_MMU_PAGE_SIZE 0x10000 /**< Flash cache MMU mapping page size */ 35 36 typedef enum { 37 FLASH_WRAP_MODE_8B = 0, 38 FLASH_WRAP_MODE_16B = 2, 39 FLASH_WRAP_MODE_32B = 4, 40 FLASH_WRAP_MODE_64B = 6, 41 FLASH_WRAP_MODE_DISABLE = 1 42 } spi_flash_wrap_mode_t; 43 44 /** 45 * @brief set wrap mode of flash 46 * 47 * @param mode: wrap mode support disable, 16 32, 64 byte 48 * 49 * @return esp_err_t : ESP_OK for successful. 50 * 51 */ 52 esp_err_t spi_flash_wrap_set(spi_flash_wrap_mode_t mode); 53 54 /** 55 * @brief Initialize SPI flash access driver 56 * 57 * This function must be called exactly once, before any other 58 * spi_flash_* functions are called. 59 * Currently this function is called from startup code. There is 60 * no need to call it from application code. 61 * 62 */ 63 void spi_flash_init(void); 64 65 /** 66 * @brief Get flash chip size, as set in binary image header 67 * 68 * @note This value does not necessarily match real flash size. 69 * 70 * @return size of flash chip, in bytes 71 */ 72 size_t spi_flash_get_chip_size(void); 73 74 /** 75 * @brief Erase the Flash sector. 76 * 77 * @param sector: Sector number, the count starts at sector 0, 4KB per sector. 78 * 79 * @return esp_err_t 80 */ 81 esp_err_t spi_flash_erase_sector(size_t sector); 82 83 /** 84 * @brief Erase a range of flash sectors 85 * 86 * @param start_address Address where erase operation has to start. 87 * Must be 4kB-aligned 88 * @param size Size of erased range, in bytes. Must be divisible by 4kB. 89 * 90 * @return esp_err_t 91 */ 92 esp_err_t spi_flash_erase_range(size_t start_address, size_t size); 93 94 95 /** 96 * @brief Write data to Flash. 97 * 98 * @note For fastest write performance, write a 4 byte aligned size at a 99 * 4 byte aligned offset in flash from a source buffer in DRAM. Varying any of 100 * these parameters will still work, but will be slower due to buffering. 101 * 102 * @note Writing more than 8KB at a time will be split into multiple 103 * write operations to avoid disrupting other tasks in the system. 104 * 105 * @param dest_addr Destination address in Flash. 106 * @param src Pointer to the source buffer. 107 * @param size Length of data, in bytes. 108 * 109 * @return esp_err_t 110 */ 111 esp_err_t spi_flash_write(size_t dest_addr, const void *src, size_t size); 112 113 114 /** 115 * @brief Write data encrypted to Flash. 116 * 117 * @note Flash encryption must be enabled for this function to work. 118 * 119 * @note Flash encryption must be enabled when calling this function. 120 * If flash encryption is disabled, the function returns 121 * ESP_ERR_INVALID_STATE. Use esp_flash_encryption_enabled() 122 * function to determine if flash encryption is enabled. 123 * 124 * @note Both dest_addr and size must be multiples of 16 bytes. For 125 * absolute best performance, both dest_addr and size arguments should 126 * be multiples of 32 bytes. 127 * 128 * @param dest_addr Destination address in Flash. Must be a multiple of 16 bytes. 129 * @param src Pointer to the source buffer. 130 * @param size Length of data, in bytes. Must be a multiple of 16 bytes. 131 * 132 * @return esp_err_t 133 */ 134 esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size); 135 136 /** 137 * @brief Read data from Flash. 138 * 139 * @note For fastest read performance, all parameters should be 140 * 4 byte aligned. If source address and read size are not 4 byte 141 * aligned, read may be split into multiple flash operations. If 142 * destination buffer is not 4 byte aligned, a temporary buffer will 143 * be allocated on the stack. 144 * 145 * @note Reading more than 16KB of data at a time will be split 146 * into multiple reads to avoid disruption to other tasks in the 147 * system. Consider using spi_flash_mmap() to read large amounts 148 * of data. 149 * 150 * @param src_addr source address of the data in Flash. 151 * @param dest pointer to the destination buffer 152 * @param size length of data 153 * 154 * 155 * @return esp_err_t 156 */ 157 esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size); 158 159 160 /** 161 * @brief Read data from Encrypted Flash. 162 * 163 * If flash encryption is enabled, this function will transparently decrypt data as it is read. 164 * If flash encryption is not enabled, this function behaves the same as spi_flash_read(). 165 * 166 * See esp_flash_encryption_enabled() for a function to check if flash encryption is enabled. 167 * 168 * @param src source address of the data in Flash. 169 * @param dest pointer to the destination buffer 170 * @param size length of data 171 * 172 * @return esp_err_t 173 */ 174 esp_err_t spi_flash_read_encrypted(size_t src, void *dest, size_t size); 175 176 /** 177 * @brief Enumeration which specifies memory space requested in an mmap call 178 */ 179 typedef enum { 180 SPI_FLASH_MMAP_DATA, /**< map to data memory (Vaddr0), allows byte-aligned access, 4 MB total */ 181 SPI_FLASH_MMAP_INST, /**< map to instruction memory (Vaddr1-3), allows only 4-byte-aligned access, 11 MB total */ 182 } spi_flash_mmap_memory_t; 183 184 /** 185 * @brief Opaque handle for memory region obtained from spi_flash_mmap. 186 */ 187 typedef uint32_t spi_flash_mmap_handle_t; 188 189 /** 190 * @brief Map region of flash memory into data or instruction address space 191 * 192 * This function allocates sufficient number of 64kB MMU pages and configures 193 * them to map the requested region of flash memory into the address space. 194 * It may reuse MMU pages which already provide the required mapping. 195 * 196 * As with any allocator, if mmap/munmap are heavily used then the address space 197 * may become fragmented. To troubleshoot issues with page allocation, use 198 * spi_flash_mmap_dump() function. 199 * 200 * @param src_addr Physical address in flash where requested region starts. 201 * This address *must* be aligned to 64kB boundary 202 * (SPI_FLASH_MMU_PAGE_SIZE) 203 * @param size Size of region to be mapped. This size will be rounded 204 * up to a 64kB boundary 205 * @param memory Address space where the region should be mapped (data or instruction) 206 * @param[out] out_ptr Output, pointer to the mapped memory region 207 * @param[out] out_handle Output, handle which should be used for spi_flash_munmap call 208 * 209 * @return ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated 210 */ 211 esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory, 212 const void** out_ptr, spi_flash_mmap_handle_t* out_handle); 213 214 /** 215 * @brief Map sequences of pages of flash memory into data or instruction address space 216 * 217 * This function allocates sufficient number of 64kB MMU pages and configures 218 * them to map the indicated pages of flash memory contiguously into address space. 219 * In this respect, it works in a similar way as spi_flash_mmap() but it allows mapping 220 * a (maybe non-contiguous) set of pages into a contiguous region of memory. 221 * 222 * @param pages An array of numbers indicating the 64kB pages in flash to be mapped 223 * contiguously into memory. These indicate the indexes of the 64kB pages, 224 * not the byte-size addresses as used in other functions. 225 * Array must be located in internal memory. 226 * @param page_count Number of entries in the pages array 227 * @param memory Address space where the region should be mapped (instruction or data) 228 * @param[out] out_ptr Output, pointer to the mapped memory region 229 * @param[out] out_handle Output, handle which should be used for spi_flash_munmap call 230 * 231 * @return 232 * - ESP_OK on success 233 * - ESP_ERR_NO_MEM if pages can not be allocated 234 * - ESP_ERR_INVALID_ARG if pagecount is zero or pages array is not in 235 * internal memory 236 */ 237 esp_err_t spi_flash_mmap_pages(const int *pages, size_t page_count, spi_flash_mmap_memory_t memory, 238 const void** out_ptr, spi_flash_mmap_handle_t* out_handle); 239 240 241 /** 242 * @brief Release region previously obtained using spi_flash_mmap 243 * 244 * @note Calling this function will not necessarily unmap memory region. 245 * Region will only be unmapped when there are no other handles which 246 * reference this region. In case of partially overlapping regions 247 * it is possible that memory will be unmapped partially. 248 * 249 * @param handle Handle obtained from spi_flash_mmap 250 */ 251 void spi_flash_munmap(spi_flash_mmap_handle_t handle); 252 253 /** 254 * @brief Display information about mapped regions 255 * 256 * This function lists handles obtained using spi_flash_mmap, along with range 257 * of pages allocated to each handle. It also lists all non-zero entries of 258 * MMU table and corresponding reference counts. 259 */ 260 void spi_flash_mmap_dump(void); 261 262 /** 263 * @brief get free pages number which can be mmap 264 * 265 * This function will return number of free pages available in mmu table. This could be useful 266 * before calling actual spi_flash_mmap (maps flash range to DCache or ICache memory) to check 267 * if there is sufficient space available for mapping. 268 * 269 * @param memory memory type of MMU table free page 270 * 271 * @return number of free pages which can be mmaped 272 */ 273 uint32_t spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory); 274 275 276 #define SPI_FLASH_CACHE2PHYS_FAIL UINT32_MAX /*<! Result from spi_flash_cache2phys() if flash cache address is invalid */ 277 278 /** 279 * @brief Given a memory address where flash is mapped, return the corresponding physical flash offset. 280 * 281 * Cache address does not have have been assigned via spi_flash_mmap(), any address in memory mapped flash space can be looked up. 282 * 283 * @param cached Pointer to flashed cached memory. 284 * 285 * @return 286 * - SPI_FLASH_CACHE2PHYS_FAIL If cache address is outside flash cache region, or the address is not mapped. 287 * - Otherwise, returns physical offset in flash 288 */ 289 size_t spi_flash_cache2phys(const void *cached); 290 291 /** @brief Given a physical offset in flash, return the address where it is mapped in the memory space. 292 * 293 * Physical address does not have to have been assigned via spi_flash_mmap(), any address in flash can be looked up. 294 * 295 * @note Only the first matching cache address is returned. If MMU flash cache table is configured so multiple entries 296 * point to the same physical address, there may be more than one cache address corresponding to that physical 297 * address. It is also possible for a single physical address to be mapped to both the IROM and DROM regions. 298 * 299 * @note This function doesn't impose any alignment constraints, but if memory argument is SPI_FLASH_MMAP_INST and 300 * phys_offs is not 4-byte aligned, then reading from the returned pointer will result in a crash. 301 * 302 * @param phys_offs Physical offset in flash memory to look up. 303 * @param memory Address space type to look up a flash cache address mapping for (instruction or data) 304 * 305 * @return 306 * - NULL if the physical address is invalid or not mapped to flash cache of the specified memory type. 307 * - Cached memory address (in IROM or DROM space) corresponding to phys_offs. 308 */ 309 const void *spi_flash_phys2cache(size_t phys_offs, spi_flash_mmap_memory_t memory); 310 311 /** @brief Check at runtime if flash cache is enabled on both CPUs 312 * 313 * @return true if both CPUs have flash cache enabled, false otherwise. 314 */ 315 bool spi_flash_cache_enabled(void); 316 317 /** 318 * @brief Re-enable cache for the core defined as cpuid parameter. 319 * 320 * @param cpuid the core number to enable instruction cache for 321 */ 322 void spi_flash_enable_cache(uint32_t cpuid); 323 324 /** 325 * @brief SPI flash critical section enter function. 326 * 327 */ 328 typedef void (*spi_flash_guard_start_func_t)(void); 329 /** 330 * @brief SPI flash critical section exit function. 331 */ 332 typedef void (*spi_flash_guard_end_func_t)(void); 333 /** 334 * @brief SPI flash operation lock function. 335 */ 336 typedef void (*spi_flash_op_lock_func_t)(void); 337 /** 338 * @brief SPI flash operation unlock function. 339 */ 340 typedef void (*spi_flash_op_unlock_func_t)(void); 341 /** 342 * @brief Function to protect SPI flash critical regions corruption. 343 */ 344 typedef bool (*spi_flash_is_safe_write_address_t)(size_t addr, size_t size); 345 /** 346 * @brief Function to yield to the OS during erase operation. 347 */ 348 typedef void (*spi_flash_os_yield_t)(void); 349 350 /** 351 * Structure holding SPI flash access critical sections management functions. 352 * 353 * Flash API uses two types of flash access management functions: 354 * 1) Functions which prepare/restore flash cache and interrupts before calling 355 * appropriate ROM functions (SPIWrite, SPIRead and SPIEraseBlock): 356 * - 'start' function should disables flash cache and non-IRAM interrupts and 357 * is invoked before the call to one of ROM function above. 358 * - 'end' function should restore state of flash cache and non-IRAM interrupts and 359 * is invoked after the call to one of ROM function above. 360 * These two functions are not recursive. 361 * 2) Functions which synchronizes access to internal data used by flash API. 362 * This functions are mostly intended to synchronize access to flash API internal data 363 * in multithreaded environment and use OS primitives: 364 * - 'op_lock' locks access to flash API internal data. 365 * - 'op_unlock' unlocks access to flash API internal data. 366 * These two functions are recursive and can be used around the outside of multiple calls to 367 * 'start' & 'end', in order to create atomic multi-part flash operations. 368 * 3) When CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED is disabled, flash writing/erasing 369 * API checks for addresses provided by user to avoid corruption of critical flash regions 370 * (bootloader, partition table, running application etc.). 371 * 372 * Different versions of the guarding functions should be used depending on the context of 373 * execution (with or without functional OS). In normal conditions when flash API is called 374 * from task the functions use OS primitives. When there is no OS at all or when 375 * it is not guaranteed that OS is functional (accessing flash from exception handler) these 376 * functions cannot use OS primitives or even does not need them (multithreaded access is not possible). 377 * 378 * @note Structure and corresponding guard functions should not reside in flash. 379 * For example structure can be placed in DRAM and functions in IRAM sections. 380 */ 381 typedef struct { 382 spi_flash_guard_start_func_t start; /**< critical section start function. */ 383 spi_flash_guard_end_func_t end; /**< critical section end function. */ 384 spi_flash_op_lock_func_t op_lock; /**< flash access API lock function.*/ 385 spi_flash_op_unlock_func_t op_unlock; /**< flash access API unlock function.*/ 386 #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED 387 spi_flash_is_safe_write_address_t is_safe_write_address; /**< checks flash write addresses.*/ 388 #endif 389 spi_flash_os_yield_t yield; /**< yield to the OS during flash erase */ 390 } spi_flash_guard_funcs_t; 391 392 /** 393 * @brief Sets guard functions to access flash. 394 * 395 * @note Pointed structure and corresponding guard functions should not reside in flash. 396 * For example structure can be placed in DRAM and functions in IRAM sections. 397 * 398 * @param funcs pointer to structure holding flash access guard functions. 399 */ 400 void spi_flash_guard_set(const spi_flash_guard_funcs_t* funcs); 401 402 /** 403 * @brief Get the guard functions used for flash access 404 * 405 * @return The guard functions that were set via spi_flash_guard_set(). These functions 406 * can be called if implementing custom low-level SPI flash operations. 407 */ 408 const spi_flash_guard_funcs_t *spi_flash_guard_get(void); 409 410 /** 411 * @brief Default OS-aware flash access guard functions 412 */ 413 extern const spi_flash_guard_funcs_t g_flash_guard_default_ops; 414 415 /** 416 * @brief Non-OS flash access guard functions 417 * 418 * @note This version of flash guard functions is to be used when no OS is present or from panic handler. 419 * It does not use any OS primitives and IPC and implies that only calling CPU is active. 420 */ 421 extern const spi_flash_guard_funcs_t g_flash_guard_no_os_ops; 422 423 #ifdef __cplusplus 424 } 425 #endif 426 427 428 #endif /* ESP_SPI_FLASH_H */ 429