esp_flash_api.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <sys/param.h>
  9. #include <string.h>
  10. #include "esp_memory_utils.h"
  11. #include "spi_flash_chip_driver.h"
  12. #include "memspi_host_driver.h"
  13. #include "esp_log.h"
  14. #include "sdkconfig.h"
  15. #include "esp_flash_internal.h"
  16. #include "spi_flash_defs.h"
  17. #include "spi_flash_mmap.h"
  18. #include "esp_rom_caps.h"
  19. #include "esp_rom_spiflash.h"
  20. #if CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp_crypto_lock.h" // for locking flash encryption peripheral
  22. #endif //CONFIG_IDF_TARGET_ESP32S2
  23. static const char TAG[] = "spi_flash";
  24. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  25. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  26. #else
  27. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  28. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  29. #define MAX_READ_CHUNK 16384
  30. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  31. #define UNSAFE_WRITE_ADDRESS abort()
  32. #else
  33. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  34. #endif
  35. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  36. bootloader, partition table, or running application region.
  37. */
  38. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  39. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  40. #else /* FAILS or ABORTS */
  41. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  42. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  43. UNSAFE_WRITE_ADDRESS; \
  44. } \
  45. } while(0)
  46. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  47. /* Convenience macro for beginning of all API functions.
  48. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  49. * and the chip supports the operation in question.
  50. */
  51. #define VERIFY_CHIP_OP(op) do { \
  52. if (err != ESP_OK) return err; \
  53. if (chip->chip_drv->op == NULL) { \
  54. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  55. } \
  56. } while (0)
  57. #define IO_STR_LEN 10
  58. static const char io_mode_str[][IO_STR_LEN] = {
  59. "slowrd",
  60. "fastrd",
  61. "dout",
  62. "dio",
  63. "qout",
  64. "qio",
  65. [6 ... 15] = "not used", // reserved io mode for future, not used currently.
  66. "opi_str",
  67. "opi_dtr",
  68. };
  69. _Static_assert(sizeof(io_mode_str)/IO_STR_LEN == SPI_FLASH_READ_MODE_MAX, "the io_mode_str should be consistent with the esp_flash_io_mode_t defined in spi_flash_types.h");
  70. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id);
  71. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  72. static esp_err_t spiflash_start_default(esp_flash_t *chip);
  73. static esp_err_t spiflash_end_default(esp_flash_t *chip, esp_err_t err);
  74. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip);
  75. static esp_err_t flash_end_flush_cache(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  76. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  77. typedef struct {
  78. esp_err_t (*start)(esp_flash_t *chip);
  79. esp_err_t (*end)(esp_flash_t *chip, esp_err_t err);
  80. esp_err_t (*chip_check)(esp_flash_t **inout_chip);
  81. esp_err_t (*flash_end_flush_cache)(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  82. } rom_spiflash_api_func_t;
  83. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  84. // These functions can be placed in the ROM. For now we use the code in IDF.
  85. DRAM_ATTR static rom_spiflash_api_func_t default_spiflash_rom_api = {
  86. .start = spiflash_start_default,
  87. .end = spiflash_end_default,
  88. .chip_check = check_chip_pointer_default,
  89. .flash_end_flush_cache = flash_end_flush_cache,
  90. };
  91. DRAM_ATTR rom_spiflash_api_func_t *rom_spiflash_api_funcs = &default_spiflash_rom_api;
  92. #else
  93. extern rom_spiflash_api_func_t *esp_flash_api_funcs;
  94. #define rom_spiflash_api_funcs esp_flash_api_funcs
  95. #endif // CONFIG_SPI_FLASH_ROM_IMPL
  96. /* Static function to notify OS of a new SPI flash operation.
  97. If returns an error result, caller must abort. If returns ESP_OK, caller must
  98. call rom_spiflash_api_funcs->end() before returning.
  99. */
  100. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  101. static esp_err_t IRAM_ATTR spiflash_start_default(esp_flash_t *chip)
  102. {
  103. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  104. esp_err_t err = chip->os_func->start(chip->os_func_data);
  105. if (err != ESP_OK) {
  106. return err;
  107. }
  108. }
  109. chip->host->driver->dev_config(chip->host);
  110. return ESP_OK;
  111. }
  112. /* Static function to notify OS that SPI flash operation is complete.
  113. */
  114. static esp_err_t IRAM_ATTR spiflash_end_default(esp_flash_t *chip, esp_err_t err)
  115. {
  116. if (chip->os_func != NULL
  117. && chip->os_func->end != NULL) {
  118. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  119. if (err == ESP_OK) {
  120. err = end_err; // Only return the 'end' error if we haven't already failed
  121. }
  122. }
  123. return err;
  124. }
  125. // check that the 'chip' parameter is properly initialised
  126. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip)
  127. {
  128. esp_flash_t *chip = *inout_chip;
  129. if (chip == NULL) {
  130. chip = esp_flash_default_chip;
  131. }
  132. *inout_chip = chip;
  133. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  134. return ESP_ERR_FLASH_NOT_INITIALISED;
  135. }
  136. return ESP_OK;
  137. }
  138. static IRAM_ATTR esp_err_t flash_end_flush_cache(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length)
  139. {
  140. if (!bus_acquired) {
  141. // Try to acquire the bus again to flush the cache before exit.
  142. esp_err_t acquire_err = rom_spiflash_api_funcs->start(chip);
  143. if (acquire_err != ESP_OK) {
  144. return (err == ESP_OK)? acquire_err: err;
  145. }
  146. }
  147. if (chip->host->driver->flush_cache) {
  148. esp_err_t flush_err = chip->host->driver->flush_cache(chip->host, address, length);
  149. if (err == ESP_OK) {
  150. err = flush_err;
  151. }
  152. }
  153. return rom_spiflash_api_funcs->end(chip, err);
  154. }
  155. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  156. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  157. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  158. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  159. {
  160. if (!chip->chip_drv) return false;
  161. return true;
  162. }
  163. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  164. {
  165. // Chip init flow
  166. // 1. Read chip id
  167. // 2. (optional) Detect chip vendor
  168. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  169. // 4. Init chip into desired mode (without breaking the cache!)
  170. esp_err_t err = ESP_OK;
  171. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  172. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  173. return ESP_ERR_INVALID_ARG;
  174. }
  175. //read chip id
  176. uint32_t flash_id;
  177. int retries = 10;
  178. do {
  179. err = esp_flash_read_chip_id(chip, &flash_id);
  180. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  181. if (err != ESP_OK) {
  182. return err;
  183. }
  184. chip->chip_id = flash_id;
  185. if (!esp_flash_chip_driver_initialized(chip)) {
  186. // Detect chip_drv
  187. err = detect_spi_flash_chip(chip);
  188. if (err != ESP_OK) {
  189. return err;
  190. }
  191. }
  192. // Detect flash size
  193. uint32_t size;
  194. err = esp_flash_get_physical_size(chip, &size);
  195. if (err != ESP_OK) {
  196. ESP_LOGE(TAG, "failed to get chip size");
  197. return err;
  198. }
  199. if (chip->chip_drv->get_chip_caps == NULL) {
  200. // chip caps get failed, pass the flash capability check.
  201. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  202. } else {
  203. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  204. ESP_EARLY_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  205. size = (16 * 1024 * 1024);
  206. }
  207. }
  208. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  209. err = rom_spiflash_api_funcs->start(chip);
  210. if (err != ESP_OK) {
  211. return err;
  212. }
  213. if (err == ESP_OK) {
  214. // Try to set the flash mode to whatever default mode was chosen
  215. err = chip->chip_drv->set_io_mode(chip);
  216. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  217. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  218. err = ESP_OK;
  219. }
  220. }
  221. // Done: all fields on 'chip' are initialised
  222. return rom_spiflash_api_funcs->end(chip, err);
  223. }
  224. // Note: This function is only used for internal. Only call this function to initialize the main flash.
  225. // (flash chip on SPI1 CS0)
  226. esp_err_t IRAM_ATTR esp_flash_init_main(esp_flash_t *chip)
  227. {
  228. // Chip init flow
  229. // 1. Read chip id
  230. // 2. (optional) Detect chip vendor
  231. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  232. // 4. Init chip into desired mode (without breaking the cache!)
  233. esp_err_t err = ESP_OK;
  234. bool octal_mode;
  235. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  236. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  237. return ESP_ERR_INVALID_ARG;
  238. }
  239. octal_mode = (chip->read_mode >= SPI_FLASH_OPI_FLAG);
  240. //read chip id
  241. // This can indicate the MSPI support OPI, if the flash works on MSPI in OPI mode, we directly bypass read id.
  242. uint32_t flash_id = 0;
  243. if (octal_mode) {
  244. // bypass the reading but get the flash_id from the ROM variable, to avoid resetting the chip to QSPI mode and read the ID again
  245. flash_id = g_rom_flashchip.device_id;
  246. } else {
  247. int retries = 10;
  248. do {
  249. err = esp_flash_read_chip_id(chip, &flash_id);
  250. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  251. }
  252. if (err != ESP_OK) {
  253. return err;
  254. }
  255. chip->chip_id = flash_id;
  256. if (!esp_flash_chip_driver_initialized(chip)) {
  257. // Detect chip_drv
  258. err = detect_spi_flash_chip(chip);
  259. if (err != ESP_OK) {
  260. return err;
  261. }
  262. }
  263. // Detect flash size
  264. uint32_t size;
  265. err = esp_flash_get_physical_size(chip, &size);
  266. if (err != ESP_OK) {
  267. ESP_EARLY_LOGE(TAG, "failed to get chip size");
  268. return err;
  269. }
  270. if (chip->chip_drv->get_chip_caps == NULL) {
  271. // chip caps get failed, pass the flash capability check.
  272. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  273. } else {
  274. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  275. ESP_EARLY_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  276. size = (16 * 1024 * 1024);
  277. }
  278. }
  279. ESP_EARLY_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  280. err = rom_spiflash_api_funcs->start(chip);
  281. if (err != ESP_OK) {
  282. return err;
  283. }
  284. if (err == ESP_OK && !octal_mode) {
  285. // Try to set the flash mode to whatever default mode was chosen
  286. err = chip->chip_drv->set_io_mode(chip);
  287. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  288. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  289. err = ESP_OK;
  290. }
  291. }
  292. // Done: all fields on 'chip' are initialised
  293. return rom_spiflash_api_funcs->end(chip, err);
  294. }
  295. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  296. {
  297. bool installed = esp_flash_chip_driver_initialized(chip);
  298. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  299. if (err != ESP_OK) {
  300. return err;
  301. }
  302. esp_err_t (*read_id_func)(void*, uint32_t*);
  303. void* read_id_arg;
  304. if (installed && chip->chip_drv->read_id) {
  305. read_id_func = (void*)chip->chip_drv->read_id;
  306. read_id_arg = (void*)chip;
  307. } else {
  308. //default option if the chip is not detected/chosen yet.
  309. read_id_func = (void*)chip->host->driver->read_id;
  310. read_id_arg = (void*)chip->host;
  311. }
  312. // Inner function fails if it sees all-ones or all-zeroes.
  313. err = read_id_func(read_id_arg, out_id);
  314. if (sanity_check && err == ESP_OK) {
  315. // Send RDID command twice, check for a matching result and retry in case we just powered on
  316. uint32_t new_id;
  317. err = read_id_func(read_id_arg, &new_id);
  318. if (err == ESP_OK && (new_id != *out_id)) {
  319. err = ESP_ERR_FLASH_NOT_INITIALISED;
  320. }
  321. }
  322. return rom_spiflash_api_funcs->end(chip, err);
  323. }
  324. // Faster version with sanity check.
  325. // Called in esp_flash_init and unit test (though not public)
  326. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  327. {
  328. return read_id_core(chip, out_id, true);
  329. }
  330. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  331. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  332. {
  333. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  334. //Accept uninitialized chip when reading chip id
  335. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  336. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  337. return read_id_core(chip, out_id, false);
  338. }
  339. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  340. static esp_err_t IRAM_ATTR NOINLINE_ATTR read_unique_id(esp_flash_t* chip, uint64_t* out_uid)
  341. {
  342. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  343. if (err != ESP_OK) {
  344. return err;
  345. }
  346. err = chip->chip_drv->read_unique_id(chip, out_uid);
  347. return rom_spiflash_api_funcs->end(chip, err);
  348. }
  349. esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid)
  350. {
  351. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  352. if (err != ESP_OK) {
  353. return err;
  354. }
  355. if (chip->chip_drv->get_chip_caps == NULL) {
  356. // chip caps get failed, pass the flash capability check.
  357. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  358. } else {
  359. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_UNIQUE_ID) == 0) {
  360. ESP_EARLY_LOGE(TAG, "chip %s doesn't support reading unique id", chip->chip_drv->name);
  361. return ESP_ERR_NOT_SUPPORTED;
  362. }
  363. }
  364. if (out_uid == NULL) {
  365. return ESP_ERR_INVALID_ARG;
  366. };
  367. return read_unique_id(chip, out_uid);
  368. }
  369. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  370. {
  371. esp_err_t err;
  372. uint32_t flash_id = chip->chip_id;
  373. // Detect the chip and set the chip_drv structure for it
  374. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  375. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  376. chip->chip_drv = *drivers;
  377. // start/end SPI operation each time, for multitasking
  378. // and also so esp_flash_registered_flash_drivers can live in flash
  379. ESP_EARLY_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  380. err = rom_spiflash_api_funcs->start(chip);
  381. if (err != ESP_OK) {
  382. return err;
  383. }
  384. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  385. chip->chip_drv = NULL;
  386. }
  387. // if probe succeeded, chip->drv stays set
  388. drivers++;
  389. err = rom_spiflash_api_funcs->end(chip, err);
  390. if (err != ESP_OK) {
  391. return err;
  392. }
  393. }
  394. if (!esp_flash_chip_driver_initialized(chip)) {
  395. return ESP_ERR_NOT_FOUND;
  396. }
  397. ESP_EARLY_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  398. return ESP_OK;
  399. }
  400. esp_err_t IRAM_ATTR esp_flash_get_physical_size(esp_flash_t *chip, uint32_t *flash_size)
  401. {
  402. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  403. if (err != ESP_OK) {
  404. return err;
  405. }
  406. VERIFY_CHIP_OP(detect_size);
  407. if (flash_size == NULL) {
  408. return ESP_ERR_INVALID_ARG;
  409. }
  410. err = rom_spiflash_api_funcs->start(chip);
  411. if (err != ESP_OK) {
  412. return err;
  413. }
  414. uint32_t detect_size;
  415. err = chip->chip_drv->detect_size(chip, &detect_size);
  416. if (err == ESP_OK) {
  417. if (chip->size == 0) {
  418. // chip->size will not be changed if detected, it will always be equal to configured flash size.
  419. chip->size = detect_size;
  420. }
  421. *flash_size = detect_size;
  422. }
  423. return rom_spiflash_api_funcs->end(chip, err);
  424. }
  425. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  426. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  427. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  428. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  429. {
  430. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  431. if (err != ESP_OK) {
  432. return err;
  433. }
  434. if (out_size == NULL) {
  435. return ESP_ERR_INVALID_ARG;
  436. }
  437. if (chip->size != 0) {
  438. *out_size = chip->size;
  439. return ESP_OK;
  440. }
  441. //Return flash chip physical size, when this API is called before flash initialisation,
  442. //After initialization will return available size.
  443. return esp_flash_get_physical_size(chip, out_size);
  444. }
  445. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  446. {
  447. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  448. VERIFY_CHIP_OP(erase_chip);
  449. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  450. //check before the operation, in case this is called too close to the last operation
  451. if (chip->chip_drv->yield) {
  452. err = chip->chip_drv->yield(chip, 0);
  453. if (err != ESP_OK) {
  454. return err;
  455. }
  456. }
  457. err = rom_spiflash_api_funcs->start(chip);
  458. if (err != ESP_OK) {
  459. return err;
  460. }
  461. err = chip->chip_drv->erase_chip(chip);
  462. if (chip->host->driver->flush_cache) {
  463. esp_err_t flush_cache_err = chip->host->driver->flush_cache(chip->host, 0, chip->size);
  464. if (err == ESP_OK) {
  465. err = flush_cache_err;
  466. }
  467. }
  468. return rom_spiflash_api_funcs->end(chip, err);
  469. }
  470. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  471. {
  472. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  473. VERIFY_CHIP_OP(erase_sector);
  474. VERIFY_CHIP_OP(erase_block);
  475. CHECK_WRITE_ADDRESS(chip, start, len);
  476. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  477. uint32_t sector_size = chip->chip_drv->sector_size;
  478. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  479. return ESP_ERR_FLASH_NOT_INITIALISED;
  480. }
  481. if (start > chip->size || start + len > chip->size) {
  482. return ESP_ERR_INVALID_ARG;
  483. }
  484. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  485. // Can only erase multiples of the sector size, starting at sector boundary
  486. return ESP_ERR_INVALID_ARG;
  487. }
  488. if (len == 0) {
  489. return ESP_OK;
  490. }
  491. err = ESP_OK;
  492. // Check for write protected regions overlapping the erase region
  493. if (chip->chip_drv->get_protected_regions != NULL &&
  494. chip->chip_drv->num_protectable_regions > 0) {
  495. err = rom_spiflash_api_funcs->start(chip);
  496. if (err != ESP_OK) {
  497. return err;
  498. }
  499. uint64_t protected = 0;
  500. err = chip->chip_drv->get_protected_regions(chip, &protected);
  501. if (err == ESP_OK && protected != 0) {
  502. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  503. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  504. if ((protected & BIT64(i))
  505. && regions_overlap(start, len, region->offset, region->size)) {
  506. err = ESP_ERR_FLASH_PROTECTED;
  507. }
  508. }
  509. }
  510. // Don't lock the SPI flash for the entire erase, as this may be very long
  511. err = rom_spiflash_api_funcs->end(chip, err);
  512. }
  513. if (err != ESP_OK) {
  514. return err;
  515. }
  516. uint32_t erase_addr = start;
  517. uint32_t len_remain = len;
  518. // Indicate whether the bus is acquired by the driver, needs to be released before return
  519. bool bus_acquired = false;
  520. while (1) {
  521. //check before the operation, in case this is called too close to the last operation
  522. if (chip->chip_drv->yield) {
  523. err = chip->chip_drv->yield(chip, 0);
  524. if (err != ESP_OK) {
  525. return err;
  526. }
  527. }
  528. err = rom_spiflash_api_funcs->start(chip);
  529. if (err != ESP_OK) {
  530. break;
  531. }
  532. bus_acquired = true;
  533. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  534. // If possible erase an entire multi-sector block
  535. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  536. err = chip->chip_drv->erase_block(chip, erase_addr);
  537. erase_addr += block_erase_size;
  538. len_remain -= block_erase_size;
  539. } else
  540. #endif
  541. {
  542. // Otherwise erase individual sector only
  543. err = chip->chip_drv->erase_sector(chip, erase_addr);
  544. erase_addr += sector_size;
  545. len_remain -= sector_size;
  546. }
  547. assert(len_remain < len);
  548. if (err != ESP_OK || len_remain == 0) {
  549. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  550. // happen when the cache is still disabled on ESP32. Break before the end() function and
  551. // do end() later
  552. assert(bus_acquired);
  553. break;
  554. }
  555. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  556. if (err != ESP_OK) {
  557. break;
  558. }
  559. bus_acquired = false;
  560. }
  561. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  562. }
  563. #endif // !CONFIG_SPI_FLASH_ROM_IMPL
  564. #if defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  565. /* ROM esp_flash_erase_region implementation doesn't handle 0 erase size correctly.
  566. * Check the size and call ROM function instead of overriding it completely.
  567. * The behavior is slightly different from esp_flash_erase_region above, thought:
  568. * here the check for 0 size is done first, but in esp_flash_erase_region the check is
  569. * done after the other arguments are checked.
  570. */
  571. extern esp_err_t rom_esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
  572. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  573. {
  574. if (len == 0) {
  575. return ESP_OK;
  576. }
  577. return rom_esp_flash_erase_region(chip, start, len);
  578. }
  579. #endif // defined(CONFIG_SPI_FLASH_ROM_IMPL) && ESP_ROM_HAS_ERASE_0_REGION_BUG
  580. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  581. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  582. {
  583. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  584. VERIFY_CHIP_OP(get_chip_write_protect);
  585. if (out_write_protected == NULL) {
  586. return ESP_ERR_INVALID_ARG;
  587. }
  588. err = rom_spiflash_api_funcs->start(chip);
  589. if (err != ESP_OK) {
  590. return err;
  591. }
  592. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  593. return rom_spiflash_api_funcs->end(chip, err);
  594. }
  595. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  596. {
  597. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  598. VERIFY_CHIP_OP(set_chip_write_protect);
  599. //TODO: skip writing if already locked or unlocked
  600. err = rom_spiflash_api_funcs->start(chip);
  601. if (err != ESP_OK) {
  602. return err;
  603. }
  604. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  605. return rom_spiflash_api_funcs->end(chip, err);
  606. }
  607. esp_err_t esp_flash_get_protectable_regions(const esp_flash_t *chip, const esp_flash_region_t **out_regions, uint32_t *out_num_regions)
  608. {
  609. if(out_num_regions != NULL) {
  610. *out_num_regions = 0; // In case caller doesn't check result
  611. }
  612. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  613. VERIFY_CHIP_OP(get_protected_regions);
  614. if(out_regions == NULL || out_num_regions == NULL) {
  615. return ESP_ERR_INVALID_ARG;
  616. }
  617. *out_num_regions = chip->chip_drv->num_protectable_regions;
  618. *out_regions = chip->chip_drv->protectable_regions;
  619. return ESP_OK;
  620. }
  621. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  622. {
  623. if (region == NULL) {
  624. return ESP_ERR_INVALID_ARG;
  625. }
  626. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  627. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  628. region, sizeof(esp_flash_region_t)) == 0) {
  629. return ESP_OK;
  630. }
  631. }
  632. return ESP_ERR_NOT_FOUND;
  633. }
  634. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  635. {
  636. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  637. VERIFY_CHIP_OP(get_protected_regions);
  638. if (out_protected == NULL) {
  639. return ESP_ERR_INVALID_ARG;
  640. }
  641. uint8_t index;
  642. err = find_region(chip, region, &index);
  643. if (err != ESP_OK) {
  644. return err;
  645. }
  646. uint64_t protection_mask = 0;
  647. err = rom_spiflash_api_funcs->start(chip);
  648. if (err != ESP_OK) {
  649. return err;
  650. }
  651. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  652. if (err == ESP_OK) {
  653. *out_protected = protection_mask & (1LL << index);
  654. }
  655. return rom_spiflash_api_funcs->end(chip, err);
  656. }
  657. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  658. {
  659. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  660. VERIFY_CHIP_OP(set_protected_regions);
  661. uint8_t index;
  662. err = find_region(chip, region, &index);
  663. if (err != ESP_OK) {
  664. return err;
  665. }
  666. uint64_t protection_mask = 0;
  667. err = rom_spiflash_api_funcs->start(chip);
  668. if (err != ESP_OK) {
  669. return err;
  670. }
  671. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  672. if (err == ESP_OK) {
  673. if (protect) {
  674. protection_mask |= (1LL << index);
  675. } else {
  676. protection_mask &= ~(1LL << index);
  677. }
  678. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  679. }
  680. return rom_spiflash_api_funcs->end(chip, err);
  681. }
  682. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  683. {
  684. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  685. VERIFY_CHIP_OP(read);
  686. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  687. return ESP_ERR_INVALID_ARG;
  688. }
  689. if (length == 0) {
  690. return ESP_OK;
  691. }
  692. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  693. bool direct_read = false;
  694. //If the buffer is internal already, it's ok to use it directly
  695. direct_read |= esp_ptr_in_dram(buffer);
  696. //If not, we need to check if the HW support direct write
  697. direct_read |= chip->host->driver->supports_direct_read(chip->host, buffer);
  698. uint8_t* temp_buffer = NULL;
  699. //each time, we at most read this length
  700. //after that, we release the lock to allow some other operations
  701. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  702. if (!direct_read) {
  703. size_t actual_len = 0;
  704. if (chip->os_func->get_temp_buffer != NULL) {
  705. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  706. read_chunk_size = actual_len;
  707. }
  708. if (temp_buffer == NULL) {
  709. return ESP_ERR_NO_MEM;
  710. }
  711. }
  712. err = ESP_OK;
  713. do {
  714. err = rom_spiflash_api_funcs->start(chip);
  715. if (err != ESP_OK) {
  716. break;
  717. }
  718. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  719. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  720. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  721. size_t length_to_read = MIN(read_chunk_size, length);
  722. if (err == ESP_OK) {
  723. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  724. }
  725. if (err != ESP_OK) {
  726. rom_spiflash_api_funcs->end(chip, err);
  727. break;
  728. }
  729. //even if this is failed, the data is still valid, copy before quit
  730. err = rom_spiflash_api_funcs->end(chip, err);
  731. //copy back to the original buffer
  732. if (temp_buffer) {
  733. memcpy(buffer, temp_buffer, length_to_read);
  734. }
  735. address += length_to_read;
  736. length -= length_to_read;
  737. buffer = (void*)((intptr_t)buffer + length_to_read);
  738. } while (err == ESP_OK && length > 0);
  739. if (chip->os_func->release_temp_buffer != NULL) {
  740. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  741. }
  742. return err;
  743. }
  744. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  745. {
  746. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  747. VERIFY_CHIP_OP(write);
  748. CHECK_WRITE_ADDRESS(chip, address, length);
  749. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  750. return ESP_ERR_INVALID_ARG;
  751. }
  752. if (length == 0) {
  753. return ESP_OK;
  754. }
  755. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  756. bool direct_write = false;
  757. //If the buffer is internal already, it's ok to write it directly
  758. direct_write |= esp_ptr_in_dram(buffer);
  759. //If not, we need to check if the HW support direct write
  760. direct_write |= chip->host->driver->supports_direct_write(chip->host, buffer);
  761. // Indicate whether the bus is acquired by the driver, needs to be released before return
  762. bool bus_acquired = false;
  763. err = ESP_OK;
  764. /* Write output in chunks, either by buffering on stack or
  765. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  766. environment, this prevents writing from causing interrupt or higher priority task
  767. starvation.) */
  768. uint32_t write_addr = address;
  769. uint32_t len_remain = length;
  770. while (1) {
  771. uint32_t write_len;
  772. const void *write_buf;
  773. uint32_t temp_buf[8];
  774. if (direct_write) {
  775. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  776. write_buf = buffer;
  777. } else {
  778. write_len = MIN(len_remain, sizeof(temp_buf));
  779. memcpy(temp_buf, buffer, write_len);
  780. write_buf = temp_buf;
  781. }
  782. //check before the operation, in case this is called too close to the last operation
  783. if (chip->chip_drv->yield) {
  784. err = chip->chip_drv->yield(chip, 0);
  785. if (err != ESP_OK) {
  786. return err;
  787. }
  788. }
  789. err = rom_spiflash_api_funcs->start(chip);
  790. if (err != ESP_OK) {
  791. break;
  792. }
  793. bus_acquired = true;
  794. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  795. len_remain -= write_len;
  796. assert(len_remain < length);
  797. if (err != ESP_OK || len_remain == 0) {
  798. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  799. // happen when the cache is still disabled on ESP32. Break before the end() function and
  800. // do end() later
  801. assert(bus_acquired);
  802. break;
  803. }
  804. err = rom_spiflash_api_funcs->end(chip, err);
  805. if (err != ESP_OK) {
  806. break;
  807. }
  808. bus_acquired = false;
  809. write_addr += write_len;
  810. buffer = (void *)((intptr_t)buffer + write_len);
  811. }
  812. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  813. }
  814. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  815. {
  816. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  817. // Flash encryption only support on main flash.
  818. if (chip != esp_flash_default_chip) {
  819. return ESP_ERR_NOT_SUPPORTED;
  820. }
  821. if (err != ESP_OK) return err;
  822. if (buffer == NULL || address + length > chip->size) {
  823. return ESP_ERR_INVALID_ARG;
  824. }
  825. if ((address % 16) != 0) {
  826. ESP_EARLY_LOGE(TAG, "flash encrypted write address must be 16 bytes aligned");
  827. return ESP_ERR_INVALID_ARG;
  828. }
  829. if (length == 0) {
  830. return ESP_OK;
  831. }
  832. if ((length % 16) != 0) {
  833. ESP_EARLY_LOGE(TAG, "flash encrypted write length must be multiple of 16");
  834. return ESP_ERR_INVALID_SIZE;
  835. }
  836. bool bus_acquired = false;
  837. const uint8_t *ssrc = (const uint8_t *)buffer;
  838. /* On ESP32, write_encrypted encrypts data in RAM as it writes,
  839. so copy to a temporary buffer - 32 bytes at a time.
  840. Each call to write_encrypted takes a 32 byte "row" of
  841. data to encrypt, and each row is two 16 byte AES blocks
  842. that share a key (as derived from flash address).
  843. On ESP32-S2 and later, the temporary buffer need to be
  844. seperated into 16-bytes, 32-bytes, 64-bytes(if supported).
  845. So, on ESP32-S2 and later, here has a totally different
  846. data prepare implementation.
  847. */
  848. uint8_t encrypt_buf[64] __attribute__((aligned(4)));
  849. uint32_t row_size_length;
  850. for (size_t i = 0; i < length; i += row_size_length) {
  851. uint32_t row_addr = address + i;
  852. uint8_t row_size;
  853. uint8_t encrypt_byte;
  854. #if CONFIG_IDF_TARGET_ESP32
  855. if (i == 0 && (row_addr % 32) != 0) {
  856. /* writing to second block of a 32 byte row */
  857. row_size = 16;
  858. row_addr -= 16;
  859. /* copy to second block in buffer */
  860. memcpy(encrypt_buf + 16, ssrc + i, row_size);
  861. /* decrypt the first block from flash, will reencrypt to same bytes */
  862. esp_flash_read_encrypted(chip, row_addr, encrypt_buf, 16);
  863. } else if (length - i == 16) {
  864. /* 16 bytes left, is first block of a 32 byte row */
  865. row_size = 16;
  866. /* copy to first block in buffer */
  867. memcpy(encrypt_buf, ssrc + i, row_size);
  868. /* decrypt the second block from flash, will reencrypt to same bytes */
  869. esp_flash_read_encrypted(chip, row_addr + 16, encrypt_buf + 16, 16);
  870. } else {
  871. /* Writing a full 32 byte row (2 blocks) */
  872. row_size = 32;
  873. memcpy(encrypt_buf, ssrc + i, row_size);
  874. }
  875. encrypt_byte = 32;
  876. row_size_length = row_size;
  877. #else // FOR ESP32-S2, ESP32-S3, ESP32-C3
  878. if ((row_addr % 64) == 0 && (length - i) >= 64 && SOC_FLASH_ENCRYPTED_XTS_AES_BLOCK_MAX == 64) {
  879. row_size = 64;
  880. memcpy(encrypt_buf, ssrc + i, row_size);
  881. } else if ((row_addr % 32) == 0 && (length - i) >= 32) {
  882. row_size = 32;
  883. memcpy(encrypt_buf, ssrc + i, row_size);
  884. } else {
  885. row_size = 16;
  886. memcpy(encrypt_buf, ssrc + i, row_size);
  887. }
  888. encrypt_byte = row_size;
  889. row_size_length = row_size;
  890. #endif //CONFIG_IDF_TARGET_ESP32
  891. #if CONFIG_IDF_TARGET_ESP32S2
  892. esp_crypto_dma_lock_acquire();
  893. #endif //CONFIG_IDF_TARGET_ESP32S2
  894. err = rom_spiflash_api_funcs->start(chip);
  895. if (err != ESP_OK) {
  896. #if CONFIG_IDF_TARGET_ESP32S2
  897. esp_crypto_dma_lock_release();
  898. #endif //CONFIG_IDF_TARGET_ESP32S2
  899. break;
  900. }
  901. bus_acquired = true;
  902. err = chip->chip_drv->write_encrypted(chip, (uint32_t *)encrypt_buf, row_addr, encrypt_byte);
  903. if (err!= ESP_OK) {
  904. #if CONFIG_IDF_TARGET_ESP32S2
  905. esp_crypto_dma_lock_release();
  906. #endif //CONFIG_IDF_TARGET_ESP32S2
  907. bus_acquired = false;
  908. assert(bus_acquired);
  909. break;
  910. }
  911. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  912. #if CONFIG_IDF_TARGET_ESP32S2
  913. esp_crypto_dma_lock_release();
  914. #endif //CONFIG_IDF_TARGET_ESP32S2
  915. if (err != ESP_OK) {
  916. bus_acquired = false;
  917. break;
  918. }
  919. bus_acquired = false;
  920. }
  921. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  922. }
  923. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  924. {
  925. uint32_t a_end = a_start + a_len;
  926. uint32_t b_end = b_start + b_len;
  927. return (a_end > b_start && b_end > a_start);
  928. }
  929. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  930. {
  931. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  932. if (err != ESP_OK) return err;
  933. if (address + length > g_rom_flashchip.chip_size) {
  934. return ESP_ERR_INVALID_SIZE;
  935. }
  936. if (length == 0) {
  937. return ESP_OK;
  938. }
  939. if (out_buffer == NULL) {
  940. return ESP_ERR_INVALID_ARG;
  941. }
  942. const uint8_t *map;
  943. spi_flash_mmap_handle_t map_handle;
  944. size_t map_src = address & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  945. size_t map_size = length + (address - map_src);
  946. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  947. if (err != ESP_OK) {
  948. return err;
  949. }
  950. memcpy(out_buffer, map + (address - map_src), length);
  951. spi_flash_munmap(map_handle);
  952. return err;
  953. }
  954. // test only, non-public
  955. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  956. {
  957. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  958. VERIFY_CHIP_OP(get_io_mode);
  959. esp_flash_io_mode_t io_mode;
  960. err = rom_spiflash_api_funcs->start(chip);
  961. if (err != ESP_OK) {
  962. return err;
  963. }
  964. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  965. err = rom_spiflash_api_funcs->end(chip, err);
  966. if (err == ESP_OK) {
  967. *qe = (io_mode == SPI_FLASH_QOUT);
  968. }
  969. return err;
  970. }
  971. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  972. {
  973. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  974. VERIFY_CHIP_OP(set_io_mode);
  975. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  976. err = rom_spiflash_api_funcs->start(chip);
  977. if (err != ESP_OK) {
  978. return err;
  979. }
  980. err = chip->chip_drv->set_io_mode(chip);
  981. return rom_spiflash_api_funcs->end(chip, err);
  982. }
  983. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  984. //init suspend mode cmd, uses internal.
  985. esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip)
  986. {
  987. ESP_EARLY_LOGW(TAG, "Flash suspend feature is enabled");
  988. if (chip->chip_drv->get_chip_caps == NULL) {
  989. // chip caps get failed, pass the flash capability check.
  990. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  991. } else {
  992. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_SUSPEND) == 0) {
  993. ESP_EARLY_LOGW(TAG, "Suspend and resume may not supported for this flash model yet.");
  994. }
  995. }
  996. return chip->chip_drv->sus_setup(chip);
  997. }
  998. esp_err_t esp_flash_app_disable_protect(bool disable)
  999. {
  1000. if (disable) {
  1001. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  1002. } else {
  1003. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  1004. }
  1005. }