bootloader_flash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stddef.h>
  14. #include <bootloader_flash_priv.h>
  15. #include <esp_log.h>
  16. #include <esp_flash_encrypt.h>
  17. #include "sdkconfig.h"
  18. #include "soc/soc_caps.h"
  19. #if CONFIG_IDF_TARGET_ESP32
  20. # include "soc/spi_struct.h"
  21. # include "soc/spi_reg.h"
  22. /* SPI flash controller */
  23. # define SPIFLASH SPI1
  24. #else
  25. # include "soc/spi_mem_struct.h"
  26. # include "soc/spi_mem_reg.h"
  27. /* SPI flash controller */
  28. # define SPIFLASH SPIMEM1
  29. #endif
  30. #if CONFIG_IDF_TARGET_ESP32S2
  31. #include "esp32s2/rom/spi_flash.h"
  32. #elif CONFIG_IDF_TARGET_ESP32S3
  33. #include "esp32s3/rom/spi_flash.h"
  34. #endif
  35. #ifndef BOOTLOADER_BUILD
  36. /* Normal app version maps to esp_spi_flash.h operations...
  37. */
  38. static const char *TAG = "bootloader_mmap";
  39. static spi_flash_mmap_handle_t map;
  40. uint32_t bootloader_mmap_get_free_pages(void)
  41. {
  42. return spi_flash_mmap_get_free_pages(SPI_FLASH_MMAP_DATA);
  43. }
  44. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  45. {
  46. if (map) {
  47. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  48. return NULL; /* existing mapping in use... */
  49. }
  50. const void *result = NULL;
  51. uint32_t src_page = src_addr & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  52. size += (src_addr - src_page);
  53. esp_err_t err = spi_flash_mmap(src_page, size, SPI_FLASH_MMAP_DATA, &result, &map);
  54. if (err != ESP_OK) {
  55. ESP_LOGE(TAG, "spi_flash_mmap failed: 0x%x", err);
  56. return NULL;
  57. }
  58. return (void *)((intptr_t)result + (src_addr - src_page));
  59. }
  60. void bootloader_munmap(const void *mapping)
  61. {
  62. if (mapping && map) {
  63. spi_flash_munmap(map);
  64. }
  65. map = 0;
  66. }
  67. esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt)
  68. {
  69. if (allow_decrypt && esp_flash_encryption_enabled()) {
  70. return spi_flash_read_encrypted(src, dest, size);
  71. } else {
  72. return spi_flash_read(src, dest, size);
  73. }
  74. }
  75. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  76. {
  77. if (write_encrypted) {
  78. #if CONFIG_IDF_TARGET_ESP32
  79. return spi_flash_write_encrypted(dest_addr, src, size);
  80. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  81. return SPI_Encrypt_Write(dest_addr, src, size);
  82. #endif
  83. } else {
  84. return spi_flash_write(dest_addr, src, size);
  85. }
  86. }
  87. esp_err_t bootloader_flash_erase_sector(size_t sector)
  88. {
  89. return spi_flash_erase_sector(sector);
  90. }
  91. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  92. {
  93. return spi_flash_erase_range(start_addr, size);
  94. }
  95. #else
  96. /* Bootloader version, uses ROM functions only */
  97. #include "soc/dport_reg.h"
  98. #if CONFIG_IDF_TARGET_ESP32
  99. #include "esp32/rom/spi_flash.h"
  100. #include "esp32/rom/cache.h"
  101. #elif CONFIG_IDF_TARGET_ESP32S2
  102. #include "esp32s2/rom/spi_flash.h"
  103. #include "esp32s2/rom/cache.h"
  104. #include "soc/cache_memory.h"
  105. #elif CONFIG_IDF_TARGET_ESP32S3
  106. #include "esp32s3/rom/spi_flash.h"
  107. #include "esp32s3/rom/cache.h"
  108. #include "soc/cache_memory.h"
  109. #endif
  110. static const char *TAG = "bootloader_flash";
  111. #if CONFIG_IDF_TARGET_ESP32
  112. /* Use first 50 blocks in MMU for bootloader_mmap,
  113. 50th block for bootloader_flash_read
  114. */
  115. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  116. #define MMU_SIZE (0x320000)
  117. #define MMU_BLOCK50_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  118. #define FLASH_READ_VADDR MMU_BLOCK50_VADDR
  119. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  120. /* Use first 63 blocks in MMU for bootloader_mmap,
  121. 63th block for bootloader_flash_read
  122. */
  123. #define MMU_BLOCK0_VADDR SOC_DROM_LOW
  124. #define MMU_SIZE (0x3f0000)
  125. #define MMU_BLOCK63_VADDR (MMU_BLOCK0_VADDR + MMU_SIZE)
  126. #define FLASH_READ_VADDR MMU_BLOCK63_VADDR
  127. #endif
  128. #define MMU_FREE_PAGES (MMU_SIZE / FLASH_BLOCK_SIZE)
  129. static bool mapped;
  130. // Current bootloader mapping (ab)used for bootloader_read()
  131. static uint32_t current_read_mapping = UINT32_MAX;
  132. uint32_t bootloader_mmap_get_free_pages(void)
  133. {
  134. /**
  135. * Allow mapping up to 50 of the 51 available MMU blocks (last one used for reads)
  136. * Since, bootloader_mmap function below assumes it to be 0x320000 (50 pages), we can safely do this.
  137. */
  138. return MMU_FREE_PAGES;
  139. }
  140. const void *bootloader_mmap(uint32_t src_addr, uint32_t size)
  141. {
  142. if (mapped) {
  143. ESP_LOGE(TAG, "tried to bootloader_mmap twice");
  144. return NULL; /* can't map twice */
  145. }
  146. if (size > MMU_SIZE) {
  147. ESP_LOGE(TAG, "bootloader_mmap excess size %x", size);
  148. return NULL;
  149. }
  150. uint32_t src_addr_aligned = src_addr & MMU_FLASH_MASK;
  151. uint32_t count = bootloader_cache_pages_to_map(size, src_addr);
  152. #if CONFIG_IDF_TARGET_ESP32
  153. Cache_Read_Disable(0);
  154. Cache_Flush(0);
  155. #elif CONFIG_IDF_TARGET_ESP32S2
  156. uint32_t autoload = Cache_Suspend_ICache();
  157. Cache_Invalidate_ICache_All();
  158. #elif CONFIG_IDF_TARGET_ESP32S3
  159. uint32_t autoload = Cache_Suspend_DCache();
  160. Cache_Invalidate_DCache_All();
  161. #endif
  162. ESP_LOGD(TAG, "mmu set paddr=%08x count=%d size=%x src_addr=%x src_addr_aligned=%x",
  163. src_addr & MMU_FLASH_MASK, count, size, src_addr, src_addr_aligned );
  164. #if CONFIG_IDF_TARGET_ESP32
  165. int e = cache_flash_mmu_set(0, 0, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count);
  166. #elif CONFIG_IDF_TARGET_ESP32S2
  167. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  168. #elif CONFIG_IDF_TARGET_ESP32S3
  169. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK0_VADDR, src_addr_aligned, 64, count, 0);
  170. #endif
  171. if (e != 0) {
  172. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  173. #if CONFIG_IDF_TARGET_ESP32
  174. Cache_Read_Enable(0);
  175. #elif CONFIG_IDF_TARGET_ESP32S2
  176. Cache_Resume_ICache(autoload);
  177. #elif CONFIG_IDF_TARGET_ESP32S3
  178. Cache_Resume_DCache(autoload);
  179. #endif
  180. return NULL;
  181. }
  182. #if CONFIG_IDF_TARGET_ESP32
  183. Cache_Read_Enable(0);
  184. #elif CONFIG_IDF_TARGET_ESP32S2
  185. Cache_Resume_ICache(autoload);
  186. #elif CONFIG_IDF_TARGET_ESP32S3
  187. Cache_Resume_DCache(autoload);
  188. #endif
  189. mapped = true;
  190. return (void *)(MMU_BLOCK0_VADDR + (src_addr - src_addr_aligned));
  191. }
  192. void bootloader_munmap(const void *mapping)
  193. {
  194. if (mapped) {
  195. #if CONFIG_IDF_TARGET_ESP32
  196. /* Full MMU reset */
  197. Cache_Read_Disable(0);
  198. Cache_Flush(0);
  199. mmu_init(0);
  200. #elif CONFIG_IDF_TARGET_ESP32S2
  201. //TODO, save the autoload value.
  202. Cache_Suspend_ICache();
  203. Cache_Invalidate_ICache_All();
  204. Cache_MMU_Init();
  205. #elif CONFIG_IDF_TARGET_ESP32S3
  206. //TODO, save the autoload value.
  207. Cache_Suspend_DCache();
  208. Cache_Invalidate_DCache_All();
  209. Cache_MMU_Init();
  210. #endif
  211. mapped = false;
  212. current_read_mapping = UINT32_MAX;
  213. }
  214. }
  215. static esp_err_t spi_to_esp_err(esp_rom_spiflash_result_t r)
  216. {
  217. switch (r) {
  218. case ESP_ROM_SPIFLASH_RESULT_OK:
  219. return ESP_OK;
  220. case ESP_ROM_SPIFLASH_RESULT_ERR:
  221. return ESP_ERR_FLASH_OP_FAIL;
  222. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  223. return ESP_ERR_FLASH_OP_TIMEOUT;
  224. default:
  225. return ESP_FAIL;
  226. }
  227. }
  228. static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
  229. {
  230. #if CONFIG_IDF_TARGET_ESP32
  231. Cache_Read_Disable(0);
  232. Cache_Flush(0);
  233. #elif CONFIG_IDF_TARGET_ESP32S2
  234. uint32_t autoload = Cache_Suspend_ICache();
  235. #elif CONFIG_IDF_TARGET_ESP32S3
  236. uint32_t autoload = Cache_Suspend_DCache();
  237. #endif
  238. esp_rom_spiflash_result_t r = esp_rom_spiflash_read(src_addr, dest, size);
  239. #if CONFIG_IDF_TARGET_ESP32
  240. Cache_Read_Enable(0);
  241. #elif CONFIG_IDF_TARGET_ESP32S2
  242. Cache_Resume_ICache(autoload);
  243. #elif CONFIG_IDF_TARGET_ESP32S3
  244. Cache_Resume_DCache(autoload);
  245. #endif
  246. return spi_to_esp_err(r);
  247. }
  248. static esp_err_t bootloader_flash_read_allow_decrypt(size_t src_addr, void *dest, size_t size)
  249. {
  250. uint32_t *dest_words = (uint32_t *)dest;
  251. for (int word = 0; word < size / 4; word++) {
  252. uint32_t word_src = src_addr + word * 4; /* Read this offset from flash */
  253. uint32_t map_at = word_src & MMU_FLASH_MASK; /* Map this 64KB block from flash */
  254. uint32_t *map_ptr;
  255. if (map_at != current_read_mapping) {
  256. /* Move the 64KB mmu mapping window to fit map_at */
  257. #if CONFIG_IDF_TARGET_ESP32
  258. Cache_Read_Disable(0);
  259. Cache_Flush(0);
  260. #elif CONFIG_IDF_TARGET_ESP32S2
  261. uint32_t autoload = Cache_Suspend_ICache();
  262. Cache_Invalidate_ICache_All();
  263. #elif CONFIG_IDF_TARGET_ESP32S3
  264. uint32_t autoload = Cache_Suspend_DCache();
  265. Cache_Invalidate_DCache_All();
  266. #endif
  267. ESP_LOGD(TAG, "mmu set block paddr=0x%08x (was 0x%08x)", map_at, current_read_mapping);
  268. #if CONFIG_IDF_TARGET_ESP32
  269. int e = cache_flash_mmu_set(0, 0, FLASH_READ_VADDR, map_at, 64, 1);
  270. #elif CONFIG_IDF_TARGET_ESP32S2
  271. int e = Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  272. #elif CONFIG_IDF_TARGET_ESP32S3
  273. int e = Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, MMU_BLOCK63_VADDR, map_at, 64, 1, 0);
  274. #endif
  275. if (e != 0) {
  276. ESP_LOGE(TAG, "cache_flash_mmu_set failed: %d\n", e);
  277. #if CONFIG_IDF_TARGET_ESP32
  278. Cache_Read_Enable(0);
  279. #elif CONFIG_IDF_TARGET_ESP32S2
  280. Cache_Resume_ICache(autoload);
  281. #elif CONFIG_IDF_TARGET_ESP32S3
  282. Cache_Resume_DCache(autoload);
  283. #endif
  284. return ESP_FAIL;
  285. }
  286. current_read_mapping = map_at;
  287. #if CONFIG_IDF_TARGET_ESP32
  288. Cache_Read_Enable(0);
  289. #elif CONFIG_IDF_TARGET_ESP32S2
  290. Cache_Resume_ICache(autoload);
  291. #elif CONFIG_IDF_TARGET_ESP32S3
  292. Cache_Resume_DCache(autoload);
  293. #endif
  294. }
  295. map_ptr = (uint32_t *)(FLASH_READ_VADDR + (word_src - map_at));
  296. dest_words[word] = *map_ptr;
  297. }
  298. return ESP_OK;
  299. }
  300. esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
  301. {
  302. if (src_addr & 3) {
  303. ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
  304. return ESP_FAIL;
  305. }
  306. if (size & 3) {
  307. ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
  308. return ESP_FAIL;
  309. }
  310. if ((intptr_t)dest & 3) {
  311. ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
  312. return ESP_FAIL;
  313. }
  314. if (allow_decrypt) {
  315. return bootloader_flash_read_allow_decrypt(src_addr, dest, size);
  316. } else {
  317. return bootloader_flash_read_no_decrypt(src_addr, dest, size);
  318. }
  319. }
  320. esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
  321. {
  322. esp_err_t err;
  323. size_t alignment = write_encrypted ? 32 : 4;
  324. if ((dest_addr % alignment) != 0) {
  325. ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
  326. return ESP_FAIL;
  327. }
  328. if ((size % alignment) != 0) {
  329. ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
  330. return ESP_FAIL;
  331. }
  332. if (((intptr_t)src % 4) != 0) {
  333. ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
  334. return ESP_FAIL;
  335. }
  336. err = spi_to_esp_err(esp_rom_spiflash_unlock());
  337. if (err != ESP_OK) {
  338. return err;
  339. }
  340. if (write_encrypted) {
  341. #if CONFIG_IDF_TARGET_ESP32
  342. return spi_to_esp_err(esp_rom_spiflash_write_encrypted(dest_addr, src, size));
  343. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  344. // TODO: use the same ROM API here
  345. return spi_to_esp_err(SPI_Encrypt_Write(dest_addr, src, size));
  346. #endif
  347. } else {
  348. return spi_to_esp_err(esp_rom_spiflash_write(dest_addr, src, size));
  349. }
  350. }
  351. esp_err_t bootloader_flash_erase_sector(size_t sector)
  352. {
  353. return spi_to_esp_err(esp_rom_spiflash_erase_sector(sector));
  354. }
  355. esp_err_t bootloader_flash_erase_range(uint32_t start_addr, uint32_t size)
  356. {
  357. if (start_addr % FLASH_SECTOR_SIZE != 0) {
  358. return ESP_ERR_INVALID_ARG;
  359. }
  360. if (size % FLASH_SECTOR_SIZE != 0) {
  361. return ESP_ERR_INVALID_SIZE;
  362. }
  363. size_t start = start_addr / FLASH_SECTOR_SIZE;
  364. size_t end = start + size / FLASH_SECTOR_SIZE;
  365. const size_t sectors_per_block = FLASH_BLOCK_SIZE / FLASH_SECTOR_SIZE;
  366. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  367. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  368. if (sector % sectors_per_block == 0 && end - sector >= sectors_per_block) {
  369. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  370. sector += sectors_per_block;
  371. } else {
  372. rc = esp_rom_spiflash_erase_sector(sector);
  373. ++sector;
  374. }
  375. }
  376. return spi_to_esp_err(rc);
  377. }
  378. #endif
  379. extern uint8_t g_rom_spiflash_dummy_len_plus[];
  380. uint32_t bootloader_execute_flash_command(uint8_t command, uint32_t mosi_data, uint8_t mosi_len, uint8_t miso_len)
  381. {
  382. uint32_t old_ctrl_reg = SPIFLASH.ctrl.val;
  383. #if CONFIG_IDF_TARGET_ESP32
  384. SPIFLASH.ctrl.val = SPI_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  385. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  386. SPIFLASH.ctrl.val = SPI_MEM_WP_REG_M; // keep WP high while idle, otherwise leave DIO mode
  387. #endif
  388. SPIFLASH.user.usr_dummy = 0;
  389. SPIFLASH.user.usr_addr = 0;
  390. SPIFLASH.user.usr_command = 1;
  391. SPIFLASH.user2.usr_command_bitlen = 7;
  392. SPIFLASH.user2.usr_command_value = command;
  393. SPIFLASH.user.usr_miso = miso_len > 0;
  394. #if CONFIG_IDF_TARGET_ESP32
  395. SPIFLASH.miso_dlen.usr_miso_dbitlen = miso_len ? (miso_len - 1) : 0;
  396. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  397. SPIFLASH.miso_dlen.usr_miso_bit_len = miso_len ? (miso_len - 1) : 0;
  398. #endif
  399. SPIFLASH.user.usr_mosi = mosi_len > 0;
  400. #if CONFIG_IDF_TARGET_ESP32
  401. SPIFLASH.mosi_dlen.usr_mosi_dbitlen = mosi_len ? (mosi_len - 1) : 0;
  402. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  403. SPIFLASH.mosi_dlen.usr_mosi_bit_len = mosi_len ? (mosi_len - 1) : 0;
  404. #endif
  405. SPIFLASH.data_buf[0] = mosi_data;
  406. if (g_rom_spiflash_dummy_len_plus[1]) {
  407. /* When flash pins are mapped via GPIO matrix, need a dummy cycle before reading via MISO */
  408. if (miso_len > 0) {
  409. SPIFLASH.user.usr_dummy = 1;
  410. SPIFLASH.user1.usr_dummy_cyclelen = g_rom_spiflash_dummy_len_plus[1] - 1;
  411. } else {
  412. SPIFLASH.user.usr_dummy = 0;
  413. SPIFLASH.user1.usr_dummy_cyclelen = 0;
  414. }
  415. }
  416. SPIFLASH.cmd.usr = 1;
  417. while (SPIFLASH.cmd.usr != 0) {
  418. }
  419. SPIFLASH.ctrl.val = old_ctrl_reg;
  420. return SPIFLASH.data_buf[0];
  421. }
  422. void bootloader_enable_wp(void)
  423. {
  424. bootloader_execute_flash_command(CMD_WRDI, 0, 0, 0); /* Exit OTP mode */
  425. }
  426. #if SOC_CACHE_SUPPORT_WRAP
  427. esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode)
  428. {
  429. uint32_t reg_bkp_ctrl = SPIFLASH.ctrl.val;
  430. uint32_t reg_bkp_usr = SPIFLASH.user.val;
  431. SPIFLASH.user.fwrite_dio = 0;
  432. SPIFLASH.user.fwrite_dual = 0;
  433. SPIFLASH.user.fwrite_qio = 1;
  434. SPIFLASH.user.fwrite_quad = 0;
  435. SPIFLASH.ctrl.fcmd_dual = 0;
  436. SPIFLASH.ctrl.fcmd_quad = 0;
  437. SPIFLASH.user.usr_dummy = 0;
  438. SPIFLASH.user.usr_addr = 1;
  439. SPIFLASH.user.usr_command = 1;
  440. SPIFLASH.user2.usr_command_bitlen = 7;
  441. SPIFLASH.user2.usr_command_value = CMD_WRAP;
  442. SPIFLASH.user1.usr_addr_bitlen = 23;
  443. SPIFLASH.addr = 0;
  444. SPIFLASH.user.usr_miso = 0;
  445. SPIFLASH.user.usr_mosi = 1;
  446. SPIFLASH.mosi_dlen.usr_mosi_bit_len = 7;
  447. SPIFLASH.data_buf[0] = (uint32_t) mode << 4;;
  448. SPIFLASH.cmd.usr = 1;
  449. while(SPIFLASH.cmd.usr != 0)
  450. { }
  451. SPIFLASH.ctrl.val = reg_bkp_ctrl;
  452. SPIFLASH.user.val = reg_bkp_usr;
  453. return ESP_OK;
  454. }
  455. #endif //SOC_CACHE_SUPPORT_WRAP