cache_utils.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <freertos/semphr.h>
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "soc/dport_reg.h"
  23. #include <esp32/rom/spi_flash.h>
  24. #include <esp32/rom/cache.h>
  25. #elif CONFIG_IDF_TARGET_ESP32S2
  26. #include "esp32s2/rom/spi_flash.h"
  27. #include "esp32s2/rom/cache.h"
  28. #include "soc/extmem_reg.h"
  29. #include "soc/cache_memory.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S3
  31. #include "esp32s3/rom/spi_flash.h"
  32. #include "esp32s3/rom/cache.h"
  33. #include "soc/extmem_reg.h"
  34. #include "soc/cache_memory.h"
  35. #elif CONFIG_IDF_TARGET_ESP32C3
  36. #include "esp32c3/rom/spi_flash.h"
  37. #include "esp32c3/rom/cache.h"
  38. #include "soc/extmem_reg.h"
  39. #include "soc/cache_memory.h"
  40. #endif
  41. #include <soc/soc.h>
  42. #include "sdkconfig.h"
  43. #ifndef CONFIG_FREERTOS_UNICORE
  44. #include "esp_ipc.h"
  45. #endif
  46. #include "esp_attr.h"
  47. #include "esp_intr_alloc.h"
  48. #include "esp_spi_flash.h"
  49. #include "esp_log.h"
  50. static __attribute__((unused)) const char *TAG = "cache";
  51. #define DPORT_CACHE_BIT(cpuid, regid) DPORT_ ## cpuid ## regid
  52. #define DPORT_CACHE_MASK(cpuid) (DPORT_CACHE_BIT(cpuid, _CACHE_MASK_OPSDRAM) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  53. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IROM0) | \
  54. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM1) | DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0) )
  55. #define DPORT_CACHE_VAL(cpuid) (~(DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DROM0) | \
  56. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_DRAM1) | \
  57. DPORT_CACHE_BIT(cpuid, _CACHE_MASK_IRAM0)))
  58. #define DPORT_CACHE_GET_VAL(cpuid) (cpuid == 0) ? DPORT_CACHE_VAL(PRO) : DPORT_CACHE_VAL(APP)
  59. #define DPORT_CACHE_GET_MASK(cpuid) (cpuid == 0) ? DPORT_CACHE_MASK(PRO) : DPORT_CACHE_MASK(APP)
  60. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state);
  61. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state);
  62. static uint32_t s_flash_op_cache_state[2];
  63. #ifndef CONFIG_FREERTOS_UNICORE
  64. static SemaphoreHandle_t s_flash_op_mutex;
  65. static volatile bool s_flash_op_can_start = false;
  66. static volatile bool s_flash_op_complete = false;
  67. #ifndef NDEBUG
  68. static volatile int s_flash_op_cpu = -1;
  69. #endif
  70. void spi_flash_init_lock(void)
  71. {
  72. s_flash_op_mutex = xSemaphoreCreateRecursiveMutex();
  73. assert(s_flash_op_mutex != NULL);
  74. }
  75. void spi_flash_op_lock(void)
  76. {
  77. xSemaphoreTakeRecursive(s_flash_op_mutex, portMAX_DELAY);
  78. }
  79. void spi_flash_op_unlock(void)
  80. {
  81. xSemaphoreGiveRecursive(s_flash_op_mutex);
  82. }
  83. /*
  84. If you're going to modify this, keep in mind that while the flash caches of the pro and app
  85. cpu are separate, the psram cache is *not*. If one of the CPUs returns from a flash routine
  86. with its cache enabled but the other CPUs cache is not enabled yet, you will have problems
  87. when accessing psram from the former CPU.
  88. */
  89. void IRAM_ATTR spi_flash_op_block_func(void *arg)
  90. {
  91. // Disable scheduler on this CPU
  92. vTaskSuspendAll();
  93. // Restore interrupts that aren't located in IRAM
  94. esp_intr_noniram_disable();
  95. uint32_t cpuid = (uint32_t) arg;
  96. // s_flash_op_complete flag is cleared on *this* CPU, otherwise the other
  97. // CPU may reset the flag back to false before IPC task has a chance to check it
  98. // (if it is preempted by an ISR taking non-trivial amount of time)
  99. s_flash_op_complete = false;
  100. s_flash_op_can_start = true;
  101. while (!s_flash_op_complete) {
  102. // busy loop here and wait for the other CPU to finish flash operation
  103. }
  104. // Flash operation is complete, re-enable cache
  105. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  106. // Restore interrupts that aren't located in IRAM
  107. esp_intr_noniram_enable();
  108. // Re-enable scheduler
  109. xTaskResumeAll();
  110. }
  111. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  112. {
  113. assert(esp_ptr_in_dram((const void *)esp_cpu_get_sp()));
  114. spi_flash_op_lock();
  115. const int cpuid = xPortGetCoreID();
  116. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  117. #ifndef NDEBUG
  118. // For sanity check later: record the CPU which has started doing flash operation
  119. assert(s_flash_op_cpu == -1);
  120. s_flash_op_cpu = cpuid;
  121. #endif
  122. if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
  123. // Scheduler hasn't been started yet, it means that spi_flash API is being
  124. // called from the 2nd stage bootloader or from user_start_cpu0, i.e. from
  125. // PRO CPU. APP CPU is either in reset or spinning inside user_start_cpu1,
  126. // which is in IRAM. So it is safe to disable cache for the other_cpuid here.
  127. assert(other_cpuid == 1);
  128. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  129. } else {
  130. // Temporarily raise current task priority to prevent a deadlock while
  131. // waiting for IPC task to start on the other CPU
  132. int old_prio = uxTaskPriorityGet(NULL);
  133. vTaskPrioritySet(NULL, configMAX_PRIORITIES - 1);
  134. // Signal to the spi_flash_op_block_task on the other CPU that we need it to
  135. // disable cache there and block other tasks from executing.
  136. s_flash_op_can_start = false;
  137. ESP_ERROR_CHECK(esp_ipc_call(other_cpuid, &spi_flash_op_block_func, (void *) other_cpuid));
  138. while (!s_flash_op_can_start) {
  139. // Busy loop and wait for spi_flash_op_block_func to disable cache
  140. // on the other CPU
  141. }
  142. // Disable scheduler on the current CPU
  143. vTaskSuspendAll();
  144. // Can now set the priority back to the normal one
  145. vTaskPrioritySet(NULL, old_prio);
  146. // This is guaranteed to run on CPU <cpuid> because the other CPU is now
  147. // occupied by highest priority task
  148. assert(xPortGetCoreID() == cpuid);
  149. }
  150. // Kill interrupts that aren't located in IRAM
  151. esp_intr_noniram_disable();
  152. // This CPU executes this routine, with non-IRAM interrupts and the scheduler
  153. // disabled. The other CPU is spinning in the spi_flash_op_block_func task, also
  154. // with non-iram interrupts and the scheduler disabled. None of these CPUs will
  155. // touch external RAM or flash this way, so we can safely disable caches.
  156. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  157. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  158. }
  159. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  160. {
  161. const int cpuid = xPortGetCoreID();
  162. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  163. #ifndef NDEBUG
  164. // Sanity check: flash operation ends on the same CPU as it has started
  165. assert(cpuid == s_flash_op_cpu);
  166. // More sanity check: if scheduler isn't started, only CPU0 can call this.
  167. assert(!(xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED && cpuid != 0));
  168. s_flash_op_cpu = -1;
  169. #endif
  170. // Re-enable cache on both CPUs. After this, cache (flash and external RAM) should work again.
  171. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  172. spi_flash_restore_cache(other_cpuid, s_flash_op_cache_state[other_cpuid]);
  173. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  174. // Signal to spi_flash_op_block_task that flash operation is complete
  175. s_flash_op_complete = true;
  176. }
  177. // Re-enable non-iram interrupts
  178. esp_intr_noniram_enable();
  179. // Resume tasks on the current CPU, if the scheduler has started.
  180. // NOTE: enabling non-IRAM interrupts has to happen before this,
  181. // because once the scheduler has started, due to preemption the
  182. // current task can end up being moved to the other CPU.
  183. // But esp_intr_noniram_enable has to be called on the same CPU which
  184. // called esp_intr_noniram_disable
  185. if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
  186. xTaskResumeAll();
  187. }
  188. // Release API lock
  189. spi_flash_op_unlock();
  190. }
  191. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  192. {
  193. const uint32_t cpuid = xPortGetCoreID();
  194. const uint32_t other_cpuid = (cpuid == 0) ? 1 : 0;
  195. // do not care about other CPU, it was halted upon entering panic handler
  196. spi_flash_disable_cache(other_cpuid, &s_flash_op_cache_state[other_cpuid]);
  197. // Kill interrupts that aren't located in IRAM
  198. esp_intr_noniram_disable();
  199. // Disable cache on this CPU as well
  200. spi_flash_disable_cache(cpuid, &s_flash_op_cache_state[cpuid]);
  201. }
  202. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  203. {
  204. const uint32_t cpuid = xPortGetCoreID();
  205. // Re-enable cache on this CPU
  206. spi_flash_restore_cache(cpuid, s_flash_op_cache_state[cpuid]);
  207. // Re-enable non-iram interrupts
  208. esp_intr_noniram_enable();
  209. }
  210. #else // CONFIG_FREERTOS_UNICORE
  211. void spi_flash_init_lock(void)
  212. {
  213. }
  214. void spi_flash_op_lock(void)
  215. {
  216. vTaskSuspendAll();
  217. }
  218. void spi_flash_op_unlock(void)
  219. {
  220. xTaskResumeAll();
  221. }
  222. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu(void)
  223. {
  224. spi_flash_op_lock();
  225. esp_intr_noniram_disable();
  226. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  227. }
  228. void IRAM_ATTR spi_flash_enable_interrupts_caches_and_other_cpu(void)
  229. {
  230. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  231. esp_intr_noniram_enable();
  232. spi_flash_op_unlock();
  233. }
  234. void IRAM_ATTR spi_flash_disable_interrupts_caches_and_other_cpu_no_os(void)
  235. {
  236. // Kill interrupts that aren't located in IRAM
  237. esp_intr_noniram_disable();
  238. // Disable cache on this CPU as well
  239. spi_flash_disable_cache(0, &s_flash_op_cache_state[0]);
  240. }
  241. void IRAM_ATTR spi_flash_enable_interrupts_caches_no_os(void)
  242. {
  243. // Re-enable cache on this CPU
  244. spi_flash_restore_cache(0, s_flash_op_cache_state[0]);
  245. // Re-enable non-iram interrupts
  246. esp_intr_noniram_enable();
  247. }
  248. #endif // CONFIG_FREERTOS_UNICORE
  249. /**
  250. * The following two functions are replacements for Cache_Read_Disable and Cache_Read_Enable
  251. * function in ROM. They are used to work around a bug where Cache_Read_Disable requires a call to
  252. * Cache_Flush before Cache_Read_Enable, even if cached data was not modified.
  253. */
  254. static void IRAM_ATTR spi_flash_disable_cache(uint32_t cpuid, uint32_t *saved_state)
  255. {
  256. #if CONFIG_IDF_TARGET_ESP32
  257. uint32_t ret = 0;
  258. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  259. if (cpuid == 0) {
  260. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, 0);
  261. while (DPORT_GET_PERI_REG_BITS2(DPORT_PRO_DCACHE_DBUG0_REG, DPORT_PRO_CACHE_STATE, DPORT_PRO_CACHE_STATE_S) != 1) {
  262. ;
  263. }
  264. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 0, DPORT_PRO_CACHE_ENABLE_S);
  265. }
  266. #if !CONFIG_FREERTOS_UNICORE
  267. else {
  268. ret |= DPORT_GET_PERI_REG_BITS2(DPORT_APP_CACHE_CTRL1_REG, cache_mask, 0);
  269. while (DPORT_GET_PERI_REG_BITS2(DPORT_APP_DCACHE_DBUG0_REG, DPORT_APP_CACHE_STATE, DPORT_APP_CACHE_STATE_S) != 1) {
  270. ;
  271. }
  272. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 0, DPORT_APP_CACHE_ENABLE_S);
  273. }
  274. #endif
  275. *saved_state = ret;
  276. #elif CONFIG_IDF_TARGET_ESP32S2
  277. *saved_state = Cache_Suspend_ICache();
  278. #elif CONFIG_IDF_TARGET_ESP32S3
  279. uint32_t icache_state, dcache_state;
  280. icache_state = Cache_Suspend_ICache() << 16;
  281. dcache_state = Cache_Suspend_DCache();
  282. *saved_state = icache_state | dcache_state;
  283. #elif CONFIG_IDF_TARGET_ESP32C3
  284. uint32_t icache_state;
  285. icache_state = Cache_Suspend_ICache() << 16;
  286. *saved_state = icache_state;
  287. #endif
  288. }
  289. static void IRAM_ATTR spi_flash_restore_cache(uint32_t cpuid, uint32_t saved_state)
  290. {
  291. #if CONFIG_IDF_TARGET_ESP32
  292. const uint32_t cache_mask = DPORT_CACHE_GET_MASK(cpuid);
  293. if (cpuid == 0) {
  294. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL_REG, 1, 1, DPORT_PRO_CACHE_ENABLE_S);
  295. DPORT_SET_PERI_REG_BITS(DPORT_PRO_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  296. }
  297. #if !CONFIG_FREERTOS_UNICORE
  298. else {
  299. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL_REG, 1, 1, DPORT_APP_CACHE_ENABLE_S);
  300. DPORT_SET_PERI_REG_BITS(DPORT_APP_CACHE_CTRL1_REG, cache_mask, saved_state, 0);
  301. }
  302. #endif
  303. #elif CONFIG_IDF_TARGET_ESP32S2
  304. Cache_Resume_ICache(saved_state);
  305. #elif CONFIG_IDF_TARGET_ESP32S3
  306. Cache_Resume_DCache(saved_state & 0xffff);
  307. Cache_Resume_ICache(saved_state >> 16);
  308. #elif CONFIG_IDF_TARGET_ESP32C3
  309. Cache_Resume_ICache(saved_state >> 16);
  310. #endif
  311. }
  312. IRAM_ATTR bool spi_flash_cache_enabled(void)
  313. {
  314. #if CONFIG_IDF_TARGET_ESP32
  315. bool result = (DPORT_REG_GET_BIT(DPORT_PRO_CACHE_CTRL_REG, DPORT_PRO_CACHE_ENABLE) != 0);
  316. #if portNUM_PROCESSORS == 2
  317. result = result && (DPORT_REG_GET_BIT(DPORT_APP_CACHE_CTRL_REG, DPORT_APP_CACHE_ENABLE) != 0);
  318. #endif
  319. #elif CONFIG_IDF_TARGET_ESP32S2
  320. bool result = (REG_GET_BIT(EXTMEM_PRO_ICACHE_CTRL_REG, EXTMEM_PRO_ICACHE_ENABLE) != 0);
  321. #elif CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  322. bool result = (REG_GET_BIT(EXTMEM_ICACHE_CTRL_REG, EXTMEM_ICACHE_ENABLE) != 0);
  323. #endif
  324. return result;
  325. }
  326. #if CONFIG_IDF_TARGET_ESP32S2
  327. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  328. {
  329. cache_size_t cache_size;
  330. cache_ways_t cache_ways;
  331. cache_line_size_t cache_line_size;
  332. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  333. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  334. cache_size = CACHE_SIZE_8KB;
  335. #else
  336. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  337. cache_size = CACHE_SIZE_16KB;
  338. #endif
  339. cache_ways = CACHE_4WAYS_ASSOC;
  340. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B
  341. cache_line_size = CACHE_LINE_SIZE_16B;
  342. #else
  343. cache_line_size = CACHE_LINE_SIZE_32B;
  344. #endif
  345. ESP_EARLY_LOGI(TAG, "Instruction cache \t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16, 4, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : 32);
  346. Cache_Suspend_ICache();
  347. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  348. Cache_Invalidate_ICache_All();
  349. Cache_Resume_ICache(0);
  350. }
  351. IRAM_ATTR void esp_config_data_cache_mode(void)
  352. {
  353. cache_size_t cache_size;
  354. cache_ways_t cache_ways;
  355. cache_line_size_t cache_line_size;
  356. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_8KB
  357. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  358. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID, CACHE_MEMORY_INVALID);
  359. cache_size = CACHE_SIZE_8KB;
  360. #else
  361. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH, CACHE_MEMORY_INVALID);
  362. cache_size = CACHE_SIZE_16KB;
  363. #endif
  364. #else
  365. #if CONFIG_ESP32S2_DATA_CACHE_8KB
  366. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_INVALID);
  367. cache_size = CACHE_SIZE_8KB;
  368. #else
  369. Cache_Allocate_SRAM(CACHE_MEMORY_ICACHE_LOW, CACHE_MEMORY_ICACHE_HIGH, CACHE_MEMORY_DCACHE_LOW, CACHE_MEMORY_DCACHE_HIGH);
  370. cache_size = CACHE_SIZE_16KB;
  371. #endif
  372. #endif
  373. cache_ways = CACHE_4WAYS_ASSOC;
  374. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B
  375. cache_line_size = CACHE_LINE_SIZE_16B;
  376. #else
  377. cache_line_size = CACHE_LINE_SIZE_32B;
  378. #endif
  379. ESP_EARLY_LOGI(TAG, "Data cache \t\t: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_8KB ? 8 : 16, 4, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : 32);
  380. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  381. Cache_Invalidate_DCache_All();
  382. }
  383. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  384. {
  385. uint32_t i_autoload, d_autoload;
  386. if (icache) {
  387. i_autoload = Cache_Suspend_ICache();
  388. }
  389. if (dcache) {
  390. d_autoload = Cache_Suspend_DCache();
  391. }
  392. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_FLASH_WRAP_AROUND);
  393. if (icache) {
  394. Cache_Resume_ICache(i_autoload);
  395. }
  396. if (dcache) {
  397. Cache_Resume_DCache(d_autoload);
  398. }
  399. }
  400. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  401. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  402. {
  403. uint32_t i_autoload, d_autoload;
  404. if (icache) {
  405. i_autoload = Cache_Suspend_ICache();
  406. }
  407. if (dcache) {
  408. d_autoload = Cache_Suspend_DCache();
  409. }
  410. REG_SET_BIT(EXTMEM_PRO_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_PRO_CACHE_SRAM_RD_WRAP_AROUND);
  411. if (icache) {
  412. Cache_Resume_ICache(i_autoload);
  413. }
  414. if (dcache) {
  415. Cache_Resume_DCache(d_autoload);
  416. }
  417. }
  418. #endif
  419. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  420. {
  421. int icache_wrap_size = 0, dcache_wrap_size = 0;
  422. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  423. int flash_wrap_size = 0, spiram_wrap_size = 0;
  424. int flash_count = 0, spiram_count = 0;
  425. int i;
  426. bool flash_spiram_wrap_together, flash_support_wrap = true, spiram_support_wrap = true;
  427. uint32_t drom0_in_icache = 1;//always 1 in esp32s2
  428. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3
  429. drom0_in_icache = 0;
  430. #endif
  431. if (icache_wrap_enable) {
  432. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B
  433. icache_wrap_size = 16;
  434. #else
  435. icache_wrap_size = 32;
  436. #endif
  437. }
  438. if (dcache_wrap_enable) {
  439. #if CONFIG_ESP32S2_DATA_CACHE_LINE_16B || CONFIG_ESP32S3_DATA_CACHE_LINE_16B || CONFIG_ESP32C3_INSTRUCTION_CACHE_LINE_16B
  440. dcache_wrap_size = 16;
  441. #else
  442. dcache_wrap_size = 32;
  443. #endif
  444. }
  445. uint32_t instruction_use_spiram = 0;
  446. uint32_t rodata_use_spiram = 0;
  447. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  448. extern uint32_t esp_spiram_instruction_access_enabled(void);
  449. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  450. #endif
  451. #if CONFIG_SPIRAM_RODATA
  452. extern uint32_t esp_spiram_rodata_access_enabled(void);
  453. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  454. #endif
  455. if (instruction_use_spiram) {
  456. spiram_wrap_sizes[0] = icache_wrap_size;
  457. } else {
  458. flash_wrap_sizes[0] = icache_wrap_size;
  459. }
  460. if (rodata_use_spiram) {
  461. if (drom0_in_icache) {
  462. spiram_wrap_sizes[0] = icache_wrap_size;
  463. } else {
  464. spiram_wrap_sizes[1] = dcache_wrap_size;
  465. flash_wrap_sizes[1] = dcache_wrap_size;
  466. }
  467. #ifdef CONFIG_EXT_RODATA_SUPPORT
  468. spiram_wrap_sizes[1] = dcache_wrap_size;
  469. #endif
  470. } else {
  471. if (drom0_in_icache) {
  472. flash_wrap_sizes[0] = icache_wrap_size;
  473. } else {
  474. flash_wrap_sizes[1] = dcache_wrap_size;
  475. }
  476. #ifdef CONFIG_EXT_RODATA_SUPPORT
  477. flash_wrap_sizes[1] = dcache_wrap_size;
  478. #endif
  479. }
  480. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  481. spiram_wrap_sizes[1] = dcache_wrap_size;
  482. #endif
  483. for (i = 0; i < 2; i++) {
  484. if (flash_wrap_sizes[i] != -1) {
  485. flash_count++;
  486. flash_wrap_size = flash_wrap_sizes[i];
  487. }
  488. }
  489. for (i = 0; i < 2; i++) {
  490. if (spiram_wrap_sizes[i] != -1) {
  491. spiram_count++;
  492. spiram_wrap_size = spiram_wrap_sizes[i];
  493. }
  494. }
  495. if (flash_count + spiram_count <= 2) {
  496. flash_spiram_wrap_together = false;
  497. } else {
  498. flash_spiram_wrap_together = true;
  499. }
  500. ESP_EARLY_LOGI(TAG, "flash_count=%d, size=%d, spiram_count=%d, size=%d,together=%d", flash_count, flash_wrap_size, spiram_count, spiram_wrap_size, flash_spiram_wrap_together);
  501. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  502. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  503. if (spiram_wrap_size == 0) {
  504. return ESP_FAIL;
  505. }
  506. if (flash_spiram_wrap_together) {
  507. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  508. return ESP_FAIL;
  509. }
  510. }
  511. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  512. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  513. if (flash_wrap_size == 0) {
  514. return ESP_FAIL;
  515. }
  516. if (flash_spiram_wrap_together) {
  517. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  518. return ESP_FAIL;
  519. }
  520. }
  521. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  522. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  523. return ESP_FAIL;
  524. }
  525. #ifdef CONFIG_FLASHMODE_QIO
  526. flash_support_wrap = true;
  527. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  528. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  529. flash_support_wrap = false;
  530. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  531. }
  532. #else
  533. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  534. #endif
  535. #ifdef CONFIG_ESP32S2_SPIRAM_SUPPORT
  536. extern bool psram_support_wrap_size(uint32_t wrap_size);
  537. if (!psram_support_wrap_size(spiram_wrap_size)) {
  538. spiram_support_wrap = false;
  539. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  540. }
  541. #endif
  542. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  543. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  544. return ESP_FAIL;
  545. }
  546. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  547. if (flash_support_wrap && flash_wrap_size > 0) {
  548. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  549. spi_flash_enable_wrap(flash_wrap_size);
  550. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  551. }
  552. #if CONFIG_ESP32S2_SPIRAM_SUPPORT
  553. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  554. if (spiram_support_wrap && spiram_wrap_size > 0) {
  555. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  556. psram_enable_wrap(spiram_wrap_size);
  557. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  558. }
  559. #endif
  560. return ESP_OK;
  561. }
  562. #endif
  563. #if CONFIG_IDF_TARGET_ESP32S3
  564. IRAM_ATTR void esp_config_instruction_cache_mode(void)
  565. {
  566. cache_size_t cache_size;
  567. cache_ways_t cache_ways;
  568. cache_line_size_t cache_line_size;
  569. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_16KB
  570. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_INVALID);
  571. cache_size = CACHE_SIZE_HALF;
  572. #else
  573. Cache_Occupy_ICache_MEMORY(CACHE_MEMORY_IBANK0, CACHE_MEMORY_IBANK1);
  574. cache_size = CACHE_SIZE_FULL;
  575. #endif
  576. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_4WAYS
  577. cache_ways = CACHE_4WAYS_ASSOC;
  578. #else
  579. cache_ways = CACHE_8WAYS_ASSOC;
  580. #endif
  581. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  582. cache_line_size = CACHE_LINE_SIZE_16B;
  583. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  584. cache_line_size = CACHE_LINE_SIZE_32B;
  585. #else
  586. cache_line_size = CACHE_LINE_SIZE_64B;
  587. #endif
  588. ESP_EARLY_LOGI(TAG, "Instruction cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 16 : 32, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  589. Cache_Set_ICache_Mode(cache_size, cache_ways, cache_line_size);
  590. Cache_Invalidate_ICache_All();
  591. extern void Cache_Enable_ICache(uint32_t autoload);
  592. Cache_Enable_ICache(0);
  593. }
  594. IRAM_ATTR void esp_config_data_cache_mode(void)
  595. {
  596. cache_size_t cache_size;
  597. cache_ways_t cache_ways;
  598. cache_line_size_t cache_line_size;
  599. #if CONFIG_ESP32S3_DATA_CACHE_32KB
  600. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK1, CACHE_MEMORY_INVALID);
  601. cache_size = CACHE_SIZE_HALF;
  602. #else
  603. Cache_Occupy_DCache_MEMORY(CACHE_MEMORY_DBANK0, CACHE_MEMORY_DBANK1);
  604. cache_size = CACHE_SIZE_FULL;
  605. #endif
  606. #if CONFIG_ESP32S3_DATA_CACHE_4WAYS
  607. cache_ways = CACHE_4WAYS_ASSOC;
  608. #else
  609. cache_ways = CACHE_8WAYS_ASSOC;
  610. #endif
  611. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  612. cache_line_size = CACHE_LINE_SIZE_16B;
  613. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  614. cache_line_size = CACHE_LINE_SIZE_32B;
  615. #else
  616. cache_line_size = CACHE_LINE_SIZE_64B;
  617. #endif
  618. // ESP_EARLY_LOGI(TAG, "Data cache: size %dKB, %dWays, cache line size %dByte", cache_size == CACHE_SIZE_HALF ? 32 : 64, cache_ways == CACHE_4WAYS_ASSOC ? 4 : 8, cache_line_size == CACHE_LINE_SIZE_16B ? 16 : (cache_line_size == CACHE_LINE_SIZE_32B ? 32 : 64));
  619. Cache_Set_DCache_Mode(cache_size, cache_ways, cache_line_size);
  620. Cache_Invalidate_DCache_All();
  621. }
  622. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache, bool dcache)
  623. {
  624. uint32_t i_autoload, d_autoload;
  625. if (icache) {
  626. i_autoload = Cache_Suspend_ICache();
  627. }
  628. if (dcache) {
  629. d_autoload = Cache_Suspend_DCache();
  630. }
  631. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  632. if (icache) {
  633. Cache_Resume_ICache(i_autoload);
  634. }
  635. if (dcache) {
  636. Cache_Resume_DCache(d_autoload);
  637. }
  638. }
  639. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  640. static IRAM_ATTR void esp_enable_cache_spiram_wrap(bool icache, bool dcache)
  641. {
  642. uint32_t i_autoload, d_autoload;
  643. if (icache) {
  644. i_autoload = Cache_Suspend_ICache();
  645. }
  646. if (dcache) {
  647. d_autoload = Cache_Suspend_DCache();
  648. }
  649. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_SRAM_RD_WRAP_AROUND);
  650. if (icache) {
  651. Cache_Resume_ICache(i_autoload);
  652. }
  653. if (dcache) {
  654. Cache_Resume_DCache(d_autoload);
  655. }
  656. }
  657. #endif
  658. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable, bool dcache_wrap_enable)
  659. {
  660. int icache_wrap_size = 0, dcache_wrap_size = 0;
  661. int flash_wrap_sizes[2] = {-1, -1}, spiram_wrap_sizes[2] = {-1, -1};
  662. int flash_wrap_size = 0, spiram_wrap_size = 0;
  663. int flash_count = 0, spiram_count = 0;
  664. int i;
  665. bool flash_spiram_wrap_together, flash_support_wrap = false, spiram_support_wrap = true;
  666. uint32_t drom0_in_icache = 0;//always 0 in chip7.2.4
  667. if (icache_wrap_enable) {
  668. #if CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_16B
  669. icache_wrap_size = 16;
  670. #elif CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_32B
  671. icache_wrap_size = 32;
  672. #else
  673. icache_wrap_size = 64;
  674. #endif
  675. }
  676. if (dcache_wrap_enable) {
  677. #if CONFIG_ESP32S3_DATA_CACHE_LINE_16B
  678. dcache_wrap_size = 16;
  679. #elif CONFIG_ESP32S3_DATA_CACHE_LINE_32B
  680. dcache_wrap_size = 32;
  681. #else
  682. dcache_wrap_size = 64;
  683. #endif
  684. }
  685. uint32_t instruction_use_spiram = 0;
  686. uint32_t rodata_use_spiram = 0;
  687. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  688. extern uint32_t esp_spiram_instruction_access_enabled();
  689. instruction_use_spiram = esp_spiram_instruction_access_enabled();
  690. #endif
  691. #if CONFIG_SPIRAM_RODATA
  692. extern uint32_t esp_spiram_rodata_access_enabled();
  693. rodata_use_spiram = esp_spiram_rodata_access_enabled();
  694. #endif
  695. if (instruction_use_spiram) {
  696. spiram_wrap_sizes[0] = icache_wrap_size;
  697. } else {
  698. flash_wrap_sizes[0] = icache_wrap_size;
  699. }
  700. if (rodata_use_spiram) {
  701. if (drom0_in_icache) {
  702. spiram_wrap_sizes[0] = icache_wrap_size;
  703. } else {
  704. spiram_wrap_sizes[1] = dcache_wrap_size;
  705. }
  706. #ifdef CONFIG_EXT_RODATA_SUPPORT
  707. spiram_wrap_sizes[1] = dcache_wrap_size;
  708. #endif
  709. } else {
  710. if (drom0_in_icache) {
  711. flash_wrap_sizes[0] = icache_wrap_size;
  712. } else {
  713. flash_wrap_sizes[1] = dcache_wrap_size;
  714. }
  715. #ifdef CONFIG_EXT_RODATA_SUPPORT
  716. flash_wrap_sizes[1] = dcache_wrap_size;
  717. #endif
  718. }
  719. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  720. spiram_wrap_sizes[1] = dcache_wrap_size;
  721. #endif
  722. for (i = 0; i < 2; i++) {
  723. if (flash_wrap_sizes[i] != -1) {
  724. flash_count++;
  725. flash_wrap_size = flash_wrap_sizes[i];
  726. }
  727. }
  728. for (i = 0; i < 2; i++) {
  729. if (spiram_wrap_sizes[i] != -1) {
  730. spiram_count++;
  731. spiram_wrap_size = spiram_wrap_sizes[i];
  732. }
  733. }
  734. if (flash_count + spiram_count <= 2) {
  735. flash_spiram_wrap_together = false;
  736. } else {
  737. flash_spiram_wrap_together = true;
  738. }
  739. if (flash_count > 1 && flash_wrap_sizes[0] != flash_wrap_sizes[1]) {
  740. ESP_EARLY_LOGW(TAG, "Flash wrap with different length %d and %d, abort wrap.", flash_wrap_sizes[0], flash_wrap_sizes[1]);
  741. if (spiram_wrap_size == 0) {
  742. return ESP_FAIL;
  743. }
  744. if (flash_spiram_wrap_together) {
  745. ESP_EARLY_LOGE(TAG, "Abort spiram wrap because flash wrap length not fixed.");
  746. return ESP_FAIL;
  747. }
  748. }
  749. if (spiram_count > 1 && spiram_wrap_sizes[0] != spiram_wrap_sizes[1]) {
  750. ESP_EARLY_LOGW(TAG, "SPIRAM wrap with different length %d and %d, abort wrap.", spiram_wrap_sizes[0], spiram_wrap_sizes[1]);
  751. if (flash_wrap_size == 0) {
  752. return ESP_FAIL;
  753. }
  754. if (flash_spiram_wrap_together) {
  755. ESP_EARLY_LOGW(TAG, "Abort flash wrap because spiram wrap length not fixed.");
  756. return ESP_FAIL;
  757. }
  758. }
  759. if (flash_spiram_wrap_together && flash_wrap_size != spiram_wrap_size) {
  760. ESP_EARLY_LOGW(TAG, "SPIRAM has different wrap length with flash, %d and %d, abort wrap.", spiram_wrap_size, flash_wrap_size);
  761. return ESP_FAIL;
  762. }
  763. #ifdef CONFIG_FLASHMODE_QIO
  764. flash_support_wrap = true;
  765. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  766. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  767. flash_support_wrap = false;
  768. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  769. }
  770. #else
  771. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  772. #endif
  773. #ifdef CONFIG_ESP32S3_SPIRAM_SUPPORT
  774. extern bool psram_support_wrap_size(uint32_t wrap_size);
  775. if (!psram_support_wrap_size(spiram_wrap_size)) {
  776. spiram_support_wrap = false;
  777. ESP_EARLY_LOGW(TAG, "SPIRAM do not support wrap size %d.", spiram_wrap_size);
  778. }
  779. #endif
  780. if (flash_spiram_wrap_together && !(flash_support_wrap && spiram_support_wrap)) {
  781. ESP_EARLY_LOGW(TAG, "Flash and SPIRAM should support wrap together.");
  782. return ESP_FAIL;
  783. }
  784. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  785. if (flash_support_wrap && flash_wrap_size > 0) {
  786. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  787. spi_flash_enable_wrap(flash_wrap_size);
  788. esp_enable_cache_flash_wrap((flash_wrap_sizes[0] > 0), (flash_wrap_sizes[1] > 0));
  789. }
  790. #if CONFIG_ESP32S3_SPIRAM_SUPPORT
  791. extern esp_err_t psram_enable_wrap(uint32_t wrap_size);
  792. if (spiram_support_wrap && spiram_wrap_size > 0) {
  793. ESP_EARLY_LOGI(TAG, "SPIRAM wrap enabled, size = %d.", spiram_wrap_size);
  794. psram_enable_wrap(spiram_wrap_size);
  795. esp_enable_cache_spiram_wrap((spiram_wrap_sizes[0] > 0), (spiram_wrap_sizes[1] > 0));
  796. }
  797. #endif
  798. return ESP_OK;
  799. }
  800. #endif
  801. #if CONFIG_IDF_TARGET_ESP32C3
  802. static IRAM_ATTR void esp_enable_cache_flash_wrap(bool icache)
  803. {
  804. uint32_t i_autoload;
  805. if (icache) {
  806. i_autoload = Cache_Suspend_ICache();
  807. }
  808. REG_SET_BIT(EXTMEM_CACHE_WRAP_AROUND_CTRL_REG, EXTMEM_CACHE_FLASH_WRAP_AROUND);
  809. if (icache) {
  810. Cache_Resume_ICache(i_autoload);
  811. }
  812. }
  813. esp_err_t esp_enable_cache_wrap(bool icache_wrap_enable)
  814. {
  815. int flash_wrap_size = 0;
  816. bool flash_support_wrap = false;
  817. if (icache_wrap_enable) {
  818. flash_wrap_size = 32;
  819. }
  820. #ifdef CONFIG_FLASHMODE_QIO
  821. flash_support_wrap = true;
  822. extern bool spi_flash_support_wrap_size(uint32_t wrap_size);
  823. if (!spi_flash_support_wrap_size(flash_wrap_size)) {
  824. flash_support_wrap = false;
  825. ESP_EARLY_LOGW(TAG, "Flash do not support wrap size %d.", flash_wrap_size);
  826. }
  827. #else
  828. ESP_EARLY_LOGW(TAG, "Flash is not in QIO mode, do not support wrap.");
  829. #endif // CONFIG_FLASHMODE_QIO
  830. extern esp_err_t spi_flash_enable_wrap(uint32_t wrap_size);
  831. if (flash_support_wrap && flash_wrap_size > 0) {
  832. ESP_EARLY_LOGI(TAG, "Flash wrap enabled, size = %d.", flash_wrap_size);
  833. spi_flash_enable_wrap(flash_wrap_size);
  834. esp_enable_cache_flash_wrap((flash_wrap_size > 0));
  835. }
  836. return ESP_OK;
  837. }
  838. #endif // CONFIG_IDF_TARGET_ESP32C3
  839. void IRAM_ATTR spi_flash_enable_cache(uint32_t cpuid)
  840. {
  841. #if CONFIG_IDF_TARGET_ESP32
  842. uint32_t cache_value = DPORT_CACHE_GET_VAL(cpuid);
  843. cache_value &= DPORT_CACHE_GET_MASK(cpuid);
  844. // Re-enable cache on this CPU
  845. spi_flash_restore_cache(cpuid, cache_value);
  846. #else
  847. spi_flash_restore_cache(0, 0); // TODO cache_value should be non-zero
  848. #endif
  849. }