flash_ops.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <sys/param.h> // For MIN/MAX(a, b)
  11. #include <freertos/FreeRTOS.h>
  12. #include <freertos/task.h>
  13. #include <freertos/semphr.h>
  14. #include <soc/soc.h>
  15. #include <soc/soc_memory_layout.h>
  16. #include "sdkconfig.h"
  17. #include "esp_attr.h"
  18. #include "spi_flash_mmap.h"
  19. #include "esp_log.h"
  20. #include "esp_private/system_internal.h"
  21. #include "esp_private/spi_flash_os.h"
  22. #include "esp_private/esp_clk.h"
  23. #if CONFIG_IDF_TARGET_ESP32
  24. #include "esp32/rom/cache.h"
  25. #include "esp32/rom/spi_flash.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S2
  27. #include "esp32s2/rom/cache.h"
  28. #elif CONFIG_IDF_TARGET_ESP32S3
  29. #include "soc/spi_mem_reg.h"
  30. #include "esp32s3/rom/opi_flash.h"
  31. #include "esp32s3/rom/cache.h"
  32. #include "esp32s3/opi_flash_private.h"
  33. #elif CONFIG_IDF_TARGET_ESP32C3
  34. #include "esp32c3/rom/cache.h"
  35. #elif CONFIG_IDF_TARGET_ESP32H2
  36. #include "esp32h2/rom/cache.h"
  37. #elif CONFIG_IDF_TARGET_ESP32C2
  38. #include "esp32c2/rom/cache.h"
  39. #endif
  40. #include "esp_rom_spiflash.h"
  41. #include "esp_flash_partitions.h"
  42. #include "esp_private/cache_utils.h"
  43. #include "esp_flash.h"
  44. #include "esp_attr.h"
  45. #include "bootloader_flash.h"
  46. #include "esp_compiler.h"
  47. /* bytes erased by SPIEraseBlock() ROM function */
  48. #define BLOCK_ERASE_SIZE 65536
  49. /* Limit number of bytes written/read in a single SPI operation,
  50. as these operations disable all higher priority tasks from running.
  51. */
  52. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  53. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  54. #else
  55. #define MAX_WRITE_CHUNK 8192
  56. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  57. #define MAX_READ_CHUNK 16384
  58. static const char *TAG __attribute__((unused)) = "spi_flash";
  59. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  60. static spi_flash_counters_t s_flash_stats;
  61. #define COUNTER_START() uint32_t ts_begin = cpu_hal_get_cycle_count()
  62. #define COUNTER_STOP(counter) \
  63. do{ \
  64. s_flash_stats.counter.count++; \
  65. s_flash_stats.counter.time += (cpu_hal_get_cycle_count() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \
  66. } while(0)
  67. #define COUNTER_ADD_BYTES(counter, size) \
  68. do { \
  69. s_flash_stats.counter.bytes += size; \
  70. } while (0)
  71. #else
  72. #define COUNTER_START()
  73. #define COUNTER_STOP(counter)
  74. #define COUNTER_ADD_BYTES(counter, size)
  75. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  76. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  77. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  78. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  79. };
  80. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  81. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  82. .end = spi_flash_enable_interrupts_caches_no_os,
  83. };
  84. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  85. void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t *funcs)
  86. {
  87. s_flash_guard_ops = funcs;
  88. }
  89. const spi_flash_guard_funcs_t *IRAM_ATTR spi_flash_guard_get(void)
  90. {
  91. return s_flash_guard_ops;
  92. }
  93. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  94. #define UNSAFE_WRITE_ADDRESS abort()
  95. #else
  96. #define UNSAFE_WRITE_ADDRESS return false
  97. #endif
  98. static __attribute__((unused)) bool is_safe_write_address(size_t addr, size_t size)
  99. {
  100. if (!esp_partition_main_flash_region_safe(addr, size)) {
  101. UNSAFE_WRITE_ADDRESS;
  102. }
  103. return true;
  104. }
  105. #if CONFIG_SPI_FLASH_ROM_IMPL
  106. #include "esp_heap_caps.h"
  107. void IRAM_ATTR *spi_flash_malloc_internal(size_t size)
  108. {
  109. return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  110. }
  111. void IRAM_ATTR spi_flash_rom_impl_init(void)
  112. {
  113. spi_flash_guard_set(&g_flash_guard_default_ops);
  114. /* These two functions are in ROM only */
  115. extern void spi_flash_mmap_os_func_set(void *(*func1)(size_t size), void (*func2)(void *p));
  116. spi_flash_mmap_os_func_set(spi_flash_malloc_internal, heap_caps_free);
  117. extern esp_err_t spi_flash_mmap_page_num_init(uint32_t page_num);
  118. spi_flash_mmap_page_num_init(128);
  119. }
  120. #endif
  121. void IRAM_ATTR esp_mspi_pin_init(void)
  122. {
  123. #if CONFIG_ESPTOOLPY_OCT_FLASH || CONFIG_SPIRAM_MODE_OCT
  124. esp_rom_opiflash_pin_config();
  125. extern void spi_timing_set_pin_drive_strength(void);
  126. spi_timing_set_pin_drive_strength();
  127. #else
  128. //Set F4R4 board pin drive strength. TODO: IDF-3663
  129. #endif
  130. }
  131. esp_err_t IRAM_ATTR spi_flash_init_chip_state(void)
  132. {
  133. #if CONFIG_ESPTOOLPY_OCT_FLASH
  134. return esp_opiflash_init(rom_spiflash_legacy_data->chip.device_id);
  135. #else
  136. #if CONFIG_IDF_TARGET_ESP32S3
  137. // Currently, only esp32s3 allows high performance mode.
  138. return spi_flash_enable_high_performance_mode();
  139. #else
  140. return ESP_OK;
  141. #endif // CONFIG_IDF_TARGET_ESP32S3
  142. #endif // CONFIG_ESPTOOLPY_OCT_FLASH
  143. }
  144. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  145. static inline void dump_counter(spi_flash_counter_t *counter, const char *name)
  146. {
  147. ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name,
  148. counter->count, counter->time, counter->bytes);
  149. }
  150. const spi_flash_counters_t *spi_flash_get_counters(void)
  151. {
  152. return &s_flash_stats;
  153. }
  154. void spi_flash_reset_counters(void)
  155. {
  156. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  157. }
  158. void spi_flash_dump_counters(void)
  159. {
  160. dump_counter(&s_flash_stats.read, "read ");
  161. dump_counter(&s_flash_stats.write, "write");
  162. dump_counter(&s_flash_stats.erase, "erase");
  163. }
  164. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  165. void IRAM_ATTR spi_flash_set_rom_required_regs(void)
  166. {
  167. #if CONFIG_ESPTOOLPY_OCT_FLASH
  168. //Disable the variable dummy mode when doing timing tuning
  169. CLEAR_PERI_REG_MASK(SPI_MEM_DDR_REG(1), SPI_MEM_SPI_FMEM_VAR_DUMMY);
  170. /**
  171. * STR /DTR mode setting is done every time when `esp_rom_opiflash_exec_cmd` is called
  172. *
  173. * Add any registers that are not set in ROM SPI flash functions here in the future
  174. */
  175. #endif
  176. }
  177. void IRAM_ATTR spi_flash_set_vendor_required_regs(void)
  178. {
  179. #if CONFIG_ESPTOOLPY_OCT_FLASH
  180. //Flash chip requires MSPI specifically, call this function to set them
  181. esp_opiflash_set_required_regs();
  182. #else
  183. //currently we don't need to set other MSPI registers for Quad Flash
  184. #endif
  185. }