flash_ops.c 8.2 KB

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