esp_spi_flash.h 16 KB

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