flash_ops.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "soc/io_mux_reg.h"
  17. #include "sdkconfig.h"
  18. #include "esp_attr.h"
  19. #include "esp_cpu.h"
  20. #include "spi_flash_mmap.h"
  21. #include "esp_log.h"
  22. #include "esp_private/system_internal.h"
  23. #include "esp_private/spi_flash_os.h"
  24. #include "esp_private/esp_clk.h"
  25. #if CONFIG_IDF_TARGET_ESP32
  26. #include "esp32/rom/cache.h"
  27. #include "esp32/rom/spi_flash.h"
  28. #elif CONFIG_IDF_TARGET_ESP32S2
  29. #include "esp32s2/rom/cache.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S3
  31. #include "soc/spi_mem_reg.h"
  32. #include "esp32s3/rom/opi_flash.h"
  33. #include "esp32s3/rom/cache.h"
  34. #include "esp32s3/opi_flash_private.h"
  35. #elif CONFIG_IDF_TARGET_ESP32C3
  36. #include "esp32c3/rom/cache.h"
  37. #elif CONFIG_IDF_TARGET_ESP32H4
  38. #include "esp32h4/rom/cache.h"
  39. #elif CONFIG_IDF_TARGET_ESP32C2
  40. #include "esp32c2/rom/cache.h"
  41. #elif CONFIG_IDF_TARGET_ESP32C6
  42. #include "esp32c6/rom/cache.h"
  43. #endif
  44. #include "esp_rom_spiflash.h"
  45. #include "esp_flash_partitions.h"
  46. #include "esp_private/mspi_timing_tuning.h"
  47. #include "esp_private/cache_utils.h"
  48. #include "esp_flash.h"
  49. #include "esp_attr.h"
  50. #include "bootloader_flash.h"
  51. #include "bootloader_flash_config.h"
  52. #include "esp_compiler.h"
  53. #include "esp_rom_efuse.h"
  54. #if CONFIG_SPIRAM
  55. #include "esp_private/esp_psram_io.h"
  56. #endif
  57. #if SOC_MEMSPI_CLOCK_IS_INDEPENDENT
  58. #include "hal/cache_hal.h"
  59. #endif
  60. /* bytes erased by SPIEraseBlock() ROM function */
  61. #define BLOCK_ERASE_SIZE 65536
  62. /* Limit number of bytes written/read in a single SPI operation,
  63. as these operations disable all higher priority tasks from running.
  64. */
  65. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  66. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  67. #else
  68. #define MAX_WRITE_CHUNK 8192
  69. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  70. #define MAX_READ_CHUNK 16384
  71. static const char *TAG __attribute__((unused)) = "spi_flash";
  72. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  73. static spi_flash_counters_t s_flash_stats;
  74. #define COUNTER_START() uint32_t ts_begin = esp_cpu_get_cycle_count()
  75. #define COUNTER_STOP(counter) \
  76. do{ \
  77. s_flash_stats.counter.count++; \
  78. s_flash_stats.counter.time += (esp_cpu_get_cycle_count() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \
  79. } while(0)
  80. #define COUNTER_ADD_BYTES(counter, size) \
  81. do { \
  82. s_flash_stats.counter.bytes += size; \
  83. } while (0)
  84. #else
  85. #define COUNTER_START()
  86. #define COUNTER_STOP(counter)
  87. #define COUNTER_ADD_BYTES(counter, size)
  88. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  89. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  90. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  91. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  92. };
  93. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  94. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  95. .end = spi_flash_enable_interrupts_caches_no_os,
  96. };
  97. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  98. void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t *funcs)
  99. {
  100. s_flash_guard_ops = funcs;
  101. }
  102. const spi_flash_guard_funcs_t *IRAM_ATTR spi_flash_guard_get(void)
  103. {
  104. return s_flash_guard_ops;
  105. }
  106. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  107. #define UNSAFE_WRITE_ADDRESS abort()
  108. #else
  109. #define UNSAFE_WRITE_ADDRESS return false
  110. #endif
  111. static __attribute__((unused)) bool is_safe_write_address(size_t addr, size_t size)
  112. {
  113. if (!esp_partition_main_flash_region_safe(addr, size)) {
  114. UNSAFE_WRITE_ADDRESS;
  115. }
  116. return true;
  117. }
  118. #if CONFIG_SPI_FLASH_ROM_IMPL
  119. #include "esp_heap_caps.h"
  120. void IRAM_ATTR *spi_flash_malloc_internal(size_t size)
  121. {
  122. return heap_caps_malloc(size, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  123. }
  124. void IRAM_ATTR spi_flash_rom_impl_init(void)
  125. {
  126. spi_flash_guard_set(&g_flash_guard_default_ops);
  127. /* These two functions are in ROM only */
  128. extern void spi_flash_mmap_os_func_set(void *(*func1)(size_t size), void (*func2)(void *p));
  129. spi_flash_mmap_os_func_set(spi_flash_malloc_internal, heap_caps_free);
  130. extern esp_err_t spi_flash_mmap_page_num_init(uint32_t page_num);
  131. spi_flash_mmap_page_num_init(128);
  132. }
  133. #endif
  134. void IRAM_ATTR esp_mspi_pin_init(void)
  135. {
  136. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  137. bool octal_mspi_required = bootloader_flash_is_octal_mode_enabled();
  138. #if CONFIG_SPIRAM_MODE_OCT
  139. octal_mspi_required |= true;
  140. #endif
  141. if (octal_mspi_required) {
  142. esp_rom_opiflash_pin_config();
  143. mspi_timing_set_pin_drive_strength();
  144. }
  145. //Set F4R4 board pin drive strength. TODO: IDF-3663
  146. #endif
  147. }
  148. esp_err_t IRAM_ATTR spi_flash_init_chip_state(void)
  149. {
  150. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  151. if (bootloader_flash_is_octal_mode_enabled()) {
  152. return esp_opiflash_init(rom_spiflash_legacy_data->chip.device_id);
  153. } else
  154. #endif
  155. {
  156. #if CONFIG_IDF_TARGET_ESP32S3
  157. // Currently, only esp32s3 allows high performance mode.
  158. return spi_flash_enable_high_performance_mode();
  159. #else
  160. return ESP_OK;
  161. #endif // CONFIG_IDF_TARGET_ESP32S3
  162. }
  163. }
  164. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  165. static inline void dump_counter(spi_flash_counter_t *counter, const char *name)
  166. {
  167. ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name,
  168. counter->count, counter->time, counter->bytes);
  169. }
  170. const spi_flash_counters_t *spi_flash_get_counters(void)
  171. {
  172. return &s_flash_stats;
  173. }
  174. void spi_flash_reset_counters(void)
  175. {
  176. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  177. }
  178. void spi_flash_dump_counters(void)
  179. {
  180. dump_counter(&s_flash_stats.read, "read ");
  181. dump_counter(&s_flash_stats.write, "write");
  182. dump_counter(&s_flash_stats.erase, "erase");
  183. }
  184. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  185. void IRAM_ATTR spi_flash_set_rom_required_regs(void)
  186. {
  187. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  188. if (bootloader_flash_is_octal_mode_enabled()) {
  189. //Disable the variable dummy mode when doing timing tuning
  190. CLEAR_PERI_REG_MASK(SPI_MEM_DDR_REG(1), SPI_MEM_SPI_FMEM_VAR_DUMMY);
  191. /**
  192. * STR /DTR mode setting is done every time when `esp_rom_opiflash_exec_cmd` is called
  193. *
  194. * Add any registers that are not set in ROM SPI flash functions here in the future
  195. */
  196. }
  197. #endif
  198. }
  199. #if CONFIG_SPIRAM_MODE_OCT
  200. // This function will only be called when Octal PSRAM enabled.
  201. void IRAM_ATTR spi_flash_set_vendor_required_regs(void)
  202. {
  203. if (bootloader_flash_is_octal_mode_enabled()) {
  204. esp_opiflash_set_required_regs();
  205. SET_PERI_REG_BITS(SPI_MEM_CACHE_FCTRL_REG(1), SPI_MEM_CACHE_USR_CMD_4BYTE_V, 1, SPI_MEM_CACHE_USR_CMD_4BYTE_S);
  206. } else {
  207. //Flash chip requires MSPI specifically, call this function to set them
  208. // Set back MSPI registers after Octal PSRAM initialization.
  209. SET_PERI_REG_BITS(SPI_MEM_CACHE_FCTRL_REG(1), SPI_MEM_CACHE_USR_CMD_4BYTE_V, 0, SPI_MEM_CACHE_USR_CMD_4BYTE_S);
  210. }
  211. }
  212. #endif
  213. static const uint8_t s_mspi_io_num_default[] = {
  214. SPI_CLK_GPIO_NUM,
  215. SPI_Q_GPIO_NUM,
  216. SPI_D_GPIO_NUM,
  217. SPI_CS0_GPIO_NUM,
  218. SPI_HD_GPIO_NUM,
  219. SPI_WP_GPIO_NUM,
  220. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  221. SPI_DQS_GPIO_NUM,
  222. SPI_D4_GPIO_NUM,
  223. SPI_D5_GPIO_NUM,
  224. SPI_D6_GPIO_NUM,
  225. SPI_D7_GPIO_NUM
  226. #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
  227. };
  228. uint8_t esp_mspi_get_io(esp_mspi_io_t io)
  229. {
  230. #if CONFIG_SPIRAM
  231. if (io == ESP_MSPI_IO_CS1) {
  232. return esp_psram_io_get_cs_io();
  233. }
  234. #endif
  235. assert(io >= ESP_MSPI_IO_CLK);
  236. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  237. assert(io <= ESP_MSPI_IO_D7);
  238. #else
  239. assert(io <= ESP_MSPI_IO_WP);
  240. #endif
  241. #if SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
  242. uint8_t mspi_io = 0;
  243. uint32_t spiconfig = 0;
  244. if (io == ESP_MSPI_IO_WP) {
  245. /**
  246. * wp pad is a bit special:
  247. * 1. since 32's efuse does not have enough bits for wp pad, so wp pad config put in flash bin header
  248. * 2. rom code take 0x3f as invalid wp pad num, but take 0 as other invalid mspi pads num
  249. */
  250. #if CONFIG_IDF_TARGET_ESP32
  251. return bootloader_flash_get_wp_pin();
  252. #else
  253. spiconfig = esp_rom_efuse_get_flash_wp_gpio();
  254. return (spiconfig == 0x3f) ? s_mspi_io_num_default[io] : spiconfig & 0x3f;
  255. #endif
  256. }
  257. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  258. spiconfig = (io < ESP_MSPI_IO_WP) ? esp_rom_efuse_get_flash_gpio_info() : esp_rom_efuse_get_opiconfig();
  259. #else
  260. spiconfig = esp_rom_efuse_get_flash_gpio_info();
  261. #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
  262. if (spiconfig == ESP_ROM_EFUSE_FLASH_DEFAULT_SPI) {
  263. mspi_io = s_mspi_io_num_default[io];
  264. } else if (io < ESP_MSPI_IO_WP) {
  265. /**
  266. * [0 : 5] -- CLK
  267. * [6 :11] -- Q(D1)
  268. * [12:17] -- D(D0)
  269. * [18:23] -- CS
  270. * [24:29] -- HD(D3)
  271. */
  272. mspi_io = (spiconfig >> io * 6) & 0x3f;
  273. }
  274. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  275. else {
  276. /**
  277. * [0 : 5] -- DQS
  278. * [6 :11] -- D4
  279. * [12:17] -- D5
  280. * [18:23] -- D6
  281. * [24:29] -- D7
  282. */
  283. mspi_io = (spiconfig >> (io - ESP_MSPI_IO_DQS) * 6) & 0x3f;
  284. }
  285. #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
  286. return mspi_io;
  287. #else // SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
  288. return s_mspi_io_num_default[io];
  289. #endif // SOC_SPI_MEM_SUPPORT_CONFIG_GPIO_BY_EFUSE
  290. }
  291. #if SOC_MEMSPI_CLOCK_IS_INDEPENDENT
  292. IRAM_ATTR void spi_flash_set_clock_src(soc_periph_mspi_clk_src_t clk_src)
  293. {
  294. cache_hal_freeze(CACHE_TYPE_INSTRUCTION);
  295. spimem_flash_ll_set_clock_source(clk_src);
  296. cache_hal_unfreeze(CACHE_TYPE_INSTRUCTION);
  297. }
  298. #endif // SOC_MEMSPI_CLOCK_IS_INDEPENDENT