esp_flash_api.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. // Copyright 2015-2019 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 <stdio.h>
  16. #include <sys/param.h>
  17. #include <string.h>
  18. #include "spi_flash_chip_driver.h"
  19. #include "memspi_host_driver.h"
  20. #include "esp_log.h"
  21. #include "sdkconfig.h"
  22. #include "esp_flash_internal.h"
  23. #include "spi_flash_defs.h"
  24. static const char TAG[] = "spi_flash";
  25. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  26. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  27. #else
  28. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  29. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  30. #define MAX_READ_CHUNK 16384
  31. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  32. #define UNSAFE_WRITE_ADDRESS abort()
  33. #else
  34. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  35. #endif
  36. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  37. bootloader, partition table, or running application region.
  38. */
  39. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  40. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  41. #else /* FAILS or ABORTS */
  42. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  43. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  44. UNSAFE_WRITE_ADDRESS; \
  45. } \
  46. } while(0)
  47. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  48. #define IO_STR_LEN 7
  49. static const char io_mode_str[][IO_STR_LEN] = {
  50. "slowrd",
  51. "fastrd",
  52. "dout",
  53. "dio",
  54. "qout",
  55. "qio",
  56. };
  57. _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_ll.h");
  58. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id);
  59. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  60. static esp_err_t spiflash_start_default(esp_flash_t *chip);
  61. static esp_err_t spiflash_end_default(esp_flash_t *chip, esp_err_t err);
  62. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip);
  63. 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);
  64. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  65. typedef struct {
  66. esp_err_t (*start)(esp_flash_t *chip);
  67. esp_err_t (*end)(esp_flash_t *chip, esp_err_t err);
  68. esp_err_t (*chip_check)(esp_flash_t **inout_chip);
  69. esp_err_t (*flash_end_flush_cache)(esp_flash_t* chip, esp_err_t err, bool bus_acquired, uint32_t address, uint32_t length);
  70. } rom_spiflash_api_func_t;
  71. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  72. // These functions can be placed in the ROM. For now we use the code in IDF.
  73. DRAM_ATTR static rom_spiflash_api_func_t default_spiflash_rom_api = {
  74. .start = spiflash_start_default,
  75. .end = spiflash_end_default,
  76. .chip_check = check_chip_pointer_default,
  77. .flash_end_flush_cache = flash_end_flush_cache,
  78. };
  79. DRAM_ATTR rom_spiflash_api_func_t *rom_spiflash_api_funcs = &default_spiflash_rom_api;
  80. #else
  81. extern rom_spiflash_api_func_t *esp_flash_api_funcs;
  82. #define rom_spiflash_api_funcs esp_flash_api_funcs
  83. #endif // CONFIG_SPI_FLASH_ROM_IMPL
  84. /* Static function to notify OS of a new SPI flash operation.
  85. If returns an error result, caller must abort. If returns ESP_OK, caller must
  86. call rom_spiflash_api_funcs->end() before returning.
  87. */
  88. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  89. static esp_err_t IRAM_ATTR spiflash_start_default(esp_flash_t *chip)
  90. {
  91. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  92. esp_err_t err = chip->os_func->start(chip->os_func_data);
  93. if (err != ESP_OK) {
  94. return err;
  95. }
  96. }
  97. chip->host->driver->dev_config(chip->host);
  98. return ESP_OK;
  99. }
  100. /* Static function to notify OS that SPI flash operation is complete.
  101. */
  102. static esp_err_t IRAM_ATTR spiflash_end_default(esp_flash_t *chip, esp_err_t err)
  103. {
  104. if (chip->os_func != NULL
  105. && chip->os_func->end != NULL) {
  106. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  107. if (err == ESP_OK) {
  108. err = end_err; // Only return the 'end' error if we haven't already failed
  109. }
  110. }
  111. return err;
  112. }
  113. // check that the 'chip' parameter is properly initialised
  114. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip)
  115. {
  116. esp_flash_t *chip = *inout_chip;
  117. if (chip == NULL) {
  118. chip = esp_flash_default_chip;
  119. }
  120. *inout_chip = chip;
  121. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  122. return ESP_ERR_FLASH_NOT_INITIALISED;
  123. }
  124. return ESP_OK;
  125. }
  126. 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)
  127. {
  128. if (!bus_acquired) {
  129. // Try to acquire the bus again to flush the cache before exit.
  130. esp_err_t acquire_err = rom_spiflash_api_funcs->start(chip);
  131. if (acquire_err != ESP_OK) {
  132. return (err == ESP_OK)? acquire_err: err;
  133. }
  134. }
  135. if (chip->host->driver->flush_cache) {
  136. esp_err_t flush_err = chip->host->driver->flush_cache(chip->host, address, length);
  137. if (err == ESP_OK) {
  138. err = flush_err;
  139. }
  140. }
  141. return rom_spiflash_api_funcs->end(chip, err);
  142. }
  143. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  144. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  145. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  146. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  147. {
  148. if (!chip->chip_drv) return false;
  149. return true;
  150. }
  151. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  152. {
  153. // Chip init flow
  154. // 1. Read chip id
  155. // 2. (optional) Detect chip vendor
  156. // 3. Get basic parameters of the chip (size, dummy count, etc.)
  157. // 4. Init chip into desired mode (without breaking the cache!)
  158. esp_err_t err = ESP_OK;
  159. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  160. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  161. return ESP_ERR_INVALID_ARG;
  162. }
  163. //read chip id
  164. uint32_t flash_id;
  165. int retries = 10;
  166. do {
  167. err = esp_flash_read_chip_id(chip, &flash_id);
  168. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  169. if (err != ESP_OK) {
  170. return err;
  171. }
  172. chip->chip_id = flash_id;
  173. if (!esp_flash_chip_driver_initialized(chip)) {
  174. // Detect chip_drv
  175. err = detect_spi_flash_chip(chip);
  176. if (err != ESP_OK) {
  177. return err;
  178. }
  179. }
  180. // Detect flash size
  181. uint32_t size;
  182. err = esp_flash_get_size(chip, &size);
  183. if (err != ESP_OK) {
  184. ESP_LOGE(TAG, "failed to get chip size");
  185. return err;
  186. }
  187. if (chip->chip_drv->get_chip_caps == NULL) {
  188. // chip caps get failed, pass the flash capability check.
  189. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  190. } else {
  191. if (((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_32MB_SUPPORT) == 0) && (size > (16 *1024 * 1024))) {
  192. ESP_EARLY_LOGW(TAG, "Detected flash size > 16 MB, but access beyond 16 MB is not supported for this flash model yet.");
  193. size = (16 * 1024 * 1024);
  194. }
  195. }
  196. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  197. err = rom_spiflash_api_funcs->start(chip);
  198. if (err != ESP_OK) {
  199. return err;
  200. }
  201. if (err == ESP_OK) {
  202. // Try to set the flash mode to whatever default mode was chosen
  203. err = chip->chip_drv->set_io_mode(chip);
  204. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  205. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  206. err = ESP_OK;
  207. }
  208. }
  209. // Done: all fields on 'chip' are initialised
  210. return rom_spiflash_api_funcs->end(chip, err);
  211. }
  212. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  213. {
  214. bool installed = esp_flash_chip_driver_initialized(chip);
  215. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  216. if (err != ESP_OK) {
  217. return err;
  218. }
  219. esp_err_t (*read_id_func)(void*, uint32_t*);
  220. void* read_id_arg;
  221. if (installed && chip->chip_drv->read_id) {
  222. read_id_func = (void*)chip->chip_drv->read_id;
  223. read_id_arg = (void*)chip;
  224. } else {
  225. //default option if the chip is not detected/chosen yet.
  226. read_id_func = (void*)chip->host->driver->read_id;
  227. read_id_arg = (void*)chip->host;
  228. }
  229. // Inner function fails if it sees all-ones or all-zeroes.
  230. err = read_id_func(read_id_arg, out_id);
  231. if (sanity_check && err == ESP_OK) {
  232. // Send RDID command twice, check for a matching result and retry in case we just powered on
  233. uint32_t new_id;
  234. err = read_id_func(read_id_arg, &new_id);
  235. if (err == ESP_OK && (new_id != *out_id)) {
  236. err = ESP_ERR_FLASH_NOT_INITIALISED;
  237. }
  238. }
  239. return rom_spiflash_api_funcs->end(chip, err);
  240. }
  241. // Faster version with sanity check.
  242. // Called in esp_flash_init and unit test (though not public)
  243. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  244. {
  245. return read_id_core(chip, out_id, true);
  246. }
  247. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  248. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  249. {
  250. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  251. //Accept uninitialized chip when reading chip id
  252. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  253. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  254. return read_id_core(chip, out_id, false);
  255. }
  256. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  257. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  258. {
  259. esp_err_t err;
  260. uint32_t flash_id = chip->chip_id;
  261. // Detect the chip and set the chip_drv structure for it
  262. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  263. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  264. chip->chip_drv = *drivers;
  265. // start/end SPI operation each time, for multitasking
  266. // and also so esp_flash_registered_flash_drivers can live in flash
  267. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  268. err = rom_spiflash_api_funcs->start(chip);
  269. if (err != ESP_OK) {
  270. return err;
  271. }
  272. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  273. chip->chip_drv = NULL;
  274. }
  275. // if probe succeeded, chip->drv stays set
  276. drivers++;
  277. err = rom_spiflash_api_funcs->end(chip, err);
  278. if (err != ESP_OK) {
  279. return err;
  280. }
  281. }
  282. if (!esp_flash_chip_driver_initialized(chip)) {
  283. return ESP_ERR_NOT_FOUND;
  284. }
  285. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  286. return ESP_OK;
  287. }
  288. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  289. /* Convenience macro for beginning of all API functions.
  290. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  291. * and the chip supports the operation in question.
  292. */
  293. #define VERIFY_CHIP_OP(OP) do { \
  294. if (err != ESP_OK) return err; \
  295. if (chip->chip_drv->OP == NULL) { \
  296. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  297. } \
  298. } while (0)
  299. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  300. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  301. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  302. {
  303. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  304. VERIFY_CHIP_OP(detect_size);
  305. if (out_size == NULL) {
  306. return ESP_ERR_INVALID_ARG;
  307. }
  308. if (chip->size != 0) {
  309. *out_size = chip->size;
  310. return ESP_OK;
  311. }
  312. err = rom_spiflash_api_funcs->start(chip);
  313. if (err != ESP_OK) {
  314. return err;
  315. }
  316. uint32_t detect_size;
  317. err = chip->chip_drv->detect_size(chip, &detect_size);
  318. if (err == ESP_OK) {
  319. chip->size = detect_size;
  320. *out_size = chip->size;
  321. }
  322. return rom_spiflash_api_funcs->end(chip, err);
  323. }
  324. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  325. {
  326. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  327. VERIFY_CHIP_OP(erase_chip);
  328. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  329. //check before the operation, in case this is called too close to the last operation
  330. if (chip->chip_drv->yield) {
  331. err = chip->chip_drv->yield(chip, 0);
  332. if (err != ESP_OK) {
  333. return err;
  334. }
  335. }
  336. err = rom_spiflash_api_funcs->start(chip);
  337. if (err != ESP_OK) {
  338. return err;
  339. }
  340. err = chip->chip_drv->erase_chip(chip);
  341. if (chip->host->driver->flush_cache) {
  342. esp_err_t flush_cache_err = chip->host->driver->flush_cache(chip->host, 0, chip->size);
  343. if (err == ESP_OK) {
  344. err = flush_cache_err;
  345. }
  346. }
  347. return rom_spiflash_api_funcs->end(chip, err);
  348. }
  349. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  350. {
  351. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  352. VERIFY_CHIP_OP(erase_sector);
  353. VERIFY_CHIP_OP(erase_block);
  354. CHECK_WRITE_ADDRESS(chip, start, len);
  355. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  356. uint32_t sector_size = chip->chip_drv->sector_size;
  357. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  358. return ESP_ERR_FLASH_NOT_INITIALISED;
  359. }
  360. if (start > chip->size || start + len > chip->size) {
  361. return ESP_ERR_INVALID_ARG;
  362. }
  363. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  364. // Can only erase multiples of the sector size, starting at sector boundary
  365. return ESP_ERR_INVALID_ARG;
  366. }
  367. err = ESP_OK;
  368. // Check for write protected regions overlapping the erase region
  369. if (chip->chip_drv->get_protected_regions != NULL &&
  370. chip->chip_drv->num_protectable_regions > 0) {
  371. err = rom_spiflash_api_funcs->start(chip);
  372. if (err != ESP_OK) {
  373. return err;
  374. }
  375. uint64_t protected = 0;
  376. err = chip->chip_drv->get_protected_regions(chip, &protected);
  377. if (err == ESP_OK && protected != 0) {
  378. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  379. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  380. if ((protected & BIT64(i))
  381. && regions_overlap(start, len, region->offset, region->size)) {
  382. err = ESP_ERR_FLASH_PROTECTED;
  383. }
  384. }
  385. }
  386. // Don't lock the SPI flash for the entire erase, as this may be very long
  387. err = rom_spiflash_api_funcs->end(chip, err);
  388. }
  389. if (err != ESP_OK) {
  390. return err;
  391. }
  392. uint32_t erase_addr = start;
  393. uint32_t len_remain = len;
  394. // Indicate whether the bus is acquired by the driver, needs to be released before return
  395. bool bus_acquired = false;
  396. while (1) {
  397. //check before the operation, in case this is called too close to the last operation
  398. if (chip->chip_drv->yield) {
  399. err = chip->chip_drv->yield(chip, 0);
  400. if (err != ESP_OK) {
  401. return err;
  402. }
  403. }
  404. err = rom_spiflash_api_funcs->start(chip);
  405. if (err != ESP_OK) {
  406. break;
  407. }
  408. bus_acquired = true;
  409. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  410. // If possible erase an entire multi-sector block
  411. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  412. err = chip->chip_drv->erase_block(chip, erase_addr);
  413. erase_addr += block_erase_size;
  414. len_remain -= block_erase_size;
  415. } else
  416. #endif
  417. {
  418. // Otherwise erase individual sector only
  419. err = chip->chip_drv->erase_sector(chip, erase_addr);
  420. erase_addr += sector_size;
  421. len_remain -= sector_size;
  422. }
  423. if (err != ESP_OK || len_remain == 0) {
  424. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  425. // happen when the cache is still disabled on ESP32. Break before the end() function and
  426. // do end() later
  427. assert(bus_acquired);
  428. break;
  429. }
  430. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  431. if (err != ESP_OK) {
  432. break;
  433. }
  434. bus_acquired = false;
  435. }
  436. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  437. }
  438. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  439. {
  440. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  441. VERIFY_CHIP_OP(get_chip_write_protect);
  442. if (out_write_protected == NULL) {
  443. return ESP_ERR_INVALID_ARG;
  444. }
  445. err = rom_spiflash_api_funcs->start(chip);
  446. if (err != ESP_OK) {
  447. return err;
  448. }
  449. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  450. return rom_spiflash_api_funcs->end(chip, err);
  451. }
  452. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  453. {
  454. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  455. VERIFY_CHIP_OP(set_chip_write_protect);
  456. //TODO: skip writing if already locked or unlocked
  457. err = rom_spiflash_api_funcs->start(chip);
  458. if (err != ESP_OK) {
  459. return err;
  460. }
  461. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  462. return rom_spiflash_api_funcs->end(chip, err);
  463. }
  464. 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)
  465. {
  466. if(out_num_regions != NULL) {
  467. *out_num_regions = 0; // In case caller doesn't check result
  468. }
  469. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  470. VERIFY_CHIP_OP(get_protected_regions);
  471. if(out_regions == NULL || out_num_regions == NULL) {
  472. return ESP_ERR_INVALID_ARG;
  473. }
  474. *out_num_regions = chip->chip_drv->num_protectable_regions;
  475. *out_regions = chip->chip_drv->protectable_regions;
  476. return ESP_OK;
  477. }
  478. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  479. {
  480. if (region == NULL) {
  481. return ESP_ERR_INVALID_ARG;
  482. }
  483. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  484. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  485. region, sizeof(esp_flash_region_t)) == 0) {
  486. return ESP_OK;
  487. }
  488. }
  489. return ESP_ERR_NOT_FOUND;
  490. }
  491. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  492. {
  493. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  494. VERIFY_CHIP_OP(get_protected_regions);
  495. if (out_protected == NULL) {
  496. return ESP_ERR_INVALID_ARG;
  497. }
  498. uint8_t index;
  499. err = find_region(chip, region, &index);
  500. if (err != ESP_OK) {
  501. return err;
  502. }
  503. uint64_t protection_mask = 0;
  504. err = rom_spiflash_api_funcs->start(chip);
  505. if (err != ESP_OK) {
  506. return err;
  507. }
  508. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  509. if (err == ESP_OK) {
  510. *out_protected = protection_mask & (1LL << index);
  511. }
  512. return rom_spiflash_api_funcs->end(chip, err);
  513. }
  514. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  515. {
  516. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  517. VERIFY_CHIP_OP(set_protected_regions);
  518. uint8_t index;
  519. err = find_region(chip, region, &index);
  520. if (err != ESP_OK) {
  521. return err;
  522. }
  523. uint64_t protection_mask = 0;
  524. err = rom_spiflash_api_funcs->start(chip);
  525. if (err != ESP_OK) {
  526. return err;
  527. }
  528. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  529. if (err == ESP_OK) {
  530. if (protect) {
  531. protection_mask |= (1LL << index);
  532. } else {
  533. protection_mask &= ~(1LL << index);
  534. }
  535. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  536. }
  537. return rom_spiflash_api_funcs->end(chip, err);
  538. }
  539. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  540. {
  541. if (length == 0) {
  542. return ESP_OK;
  543. }
  544. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  545. VERIFY_CHIP_OP(read);
  546. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  547. return ESP_ERR_INVALID_ARG;
  548. }
  549. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  550. bool direct_read = chip->host->driver->supports_direct_read(chip->host, buffer);
  551. uint8_t* temp_buffer = NULL;
  552. //each time, we at most read this length
  553. //after that, we release the lock to allow some other operations
  554. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  555. if (!direct_read) {
  556. size_t actual_len = 0;
  557. if (chip->os_func->get_temp_buffer != NULL) {
  558. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  559. read_chunk_size = actual_len;
  560. }
  561. if (temp_buffer == NULL) {
  562. return ESP_ERR_NO_MEM;
  563. }
  564. }
  565. err = ESP_OK;
  566. do {
  567. err = rom_spiflash_api_funcs->start(chip);
  568. if (err != ESP_OK) {
  569. break;
  570. }
  571. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  572. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  573. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  574. size_t length_to_read = MIN(read_chunk_size, length);
  575. if (err == ESP_OK) {
  576. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  577. }
  578. if (err != ESP_OK) {
  579. rom_spiflash_api_funcs->end(chip, err);
  580. break;
  581. }
  582. //even if this is failed, the data is still valid, copy before quit
  583. err = rom_spiflash_api_funcs->end(chip, err);
  584. //copy back to the original buffer
  585. if (temp_buffer) {
  586. memcpy(buffer, temp_buffer, length_to_read);
  587. }
  588. address += length_to_read;
  589. length -= length_to_read;
  590. buffer = (void*)((intptr_t)buffer + length_to_read);
  591. } while (err == ESP_OK && length > 0);
  592. if (chip->os_func->release_temp_buffer != NULL) {
  593. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  594. }
  595. return err;
  596. }
  597. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  598. {
  599. if (length == 0) {
  600. return ESP_OK;
  601. }
  602. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  603. VERIFY_CHIP_OP(write);
  604. CHECK_WRITE_ADDRESS(chip, address, length);
  605. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  606. return ESP_ERR_INVALID_ARG;
  607. }
  608. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  609. bool direct_write = chip->host->driver->supports_direct_write(chip->host, buffer);
  610. // Indicate whether the bus is acquired by the driver, needs to be released before return
  611. bool bus_acquired = false;
  612. err = ESP_OK;
  613. /* Write output in chunks, either by buffering on stack or
  614. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  615. environment, this prevents writing from causing interrupt or higher priority task
  616. starvation.) */
  617. uint32_t write_addr = address;
  618. uint32_t len_remain = length;
  619. while (1) {
  620. uint32_t write_len;
  621. const void *write_buf;
  622. uint32_t temp_buf[8];
  623. if (direct_write) {
  624. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  625. write_buf = buffer;
  626. } else {
  627. write_len = MIN(len_remain, sizeof(temp_buf));
  628. memcpy(temp_buf, buffer, write_len);
  629. write_buf = temp_buf;
  630. }
  631. //check before the operation, in case this is called too close to the last operation
  632. if (chip->chip_drv->yield) {
  633. err = chip->chip_drv->yield(chip, 0);
  634. if (err != ESP_OK) {
  635. return err;
  636. }
  637. }
  638. err = rom_spiflash_api_funcs->start(chip);
  639. if (err != ESP_OK) {
  640. break;
  641. }
  642. bus_acquired = true;
  643. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  644. len_remain -= write_len;
  645. if (err != ESP_OK || len_remain == 0) {
  646. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  647. // happen when the cache is still disabled on ESP32. Break before the end() function and
  648. // do end() later
  649. assert(bus_acquired);
  650. break;
  651. }
  652. err = rom_spiflash_api_funcs->end(chip, err);
  653. if (err != ESP_OK) {
  654. break;
  655. }
  656. bus_acquired = false;
  657. write_addr += write_len;
  658. buffer = (void *)((intptr_t)buffer + write_len);
  659. }
  660. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  661. }
  662. //currently the legacy implementation is used, from flash_ops.c
  663. esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
  664. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  665. {
  666. /*
  667. * Since currently this feature is supported only by the hardware, there
  668. * is no way to support non-standard chips. We use the legacy
  669. * implementation and skip the chip and driver layers.
  670. */
  671. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  672. if (err != ESP_OK) return err;
  673. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  674. return ESP_ERR_INVALID_ARG;
  675. }
  676. return spi_flash_write_encrypted(address, buffer, length);
  677. }
  678. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  679. {
  680. uint32_t a_end = a_start + a_len;
  681. uint32_t b_end = b_start + b_len;
  682. return (a_end > b_start && b_end > a_start);
  683. }
  684. //currently the legacy implementation is used, from flash_ops.c
  685. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  686. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  687. {
  688. /*
  689. * Since currently this feature is supported only by the hardware, there
  690. * is no way to support non-standard chips. We use the legacy
  691. * implementation and skip the chip and driver layers.
  692. */
  693. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  694. if (err != ESP_OK) return err;
  695. return spi_flash_read_encrypted(address, out_buffer, length);
  696. }
  697. // test only, non-public
  698. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  699. {
  700. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  701. VERIFY_CHIP_OP(get_io_mode);
  702. esp_flash_io_mode_t io_mode;
  703. err = rom_spiflash_api_funcs->start(chip);
  704. if (err != ESP_OK) {
  705. return err;
  706. }
  707. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  708. err = rom_spiflash_api_funcs->end(chip, err);
  709. if (err == ESP_OK) {
  710. *qe = (io_mode == SPI_FLASH_QOUT);
  711. }
  712. return err;
  713. }
  714. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  715. {
  716. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  717. VERIFY_CHIP_OP(set_io_mode);
  718. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  719. err = rom_spiflash_api_funcs->start(chip);
  720. if (err != ESP_OK) {
  721. return err;
  722. }
  723. err = chip->chip_drv->set_io_mode(chip);
  724. return rom_spiflash_api_funcs->end(chip, err);
  725. }
  726. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  727. //init suspend mode cmd, uses internal.
  728. esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip)
  729. {
  730. ESP_EARLY_LOGW(TAG, "Flash suspend feature is enabled");
  731. if (chip->chip_drv->get_chip_caps == NULL) {
  732. // chip caps get failed, pass the flash capability check.
  733. ESP_EARLY_LOGW(TAG, "get_chip_caps function pointer hasn't been initialized");
  734. } else {
  735. if ((chip->chip_drv->get_chip_caps(chip) & SPI_FLASH_CHIP_CAP_SUSPEND) == 0) {
  736. ESP_EARLY_LOGW(TAG, "Suspend and resume may not supported for this flash model yet.");
  737. }
  738. }
  739. return chip->chip_drv->sus_setup(chip);
  740. }
  741. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  742. esp_err_t esp_flash_app_disable_protect(bool disable)
  743. {
  744. if (disable) {
  745. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  746. } else {
  747. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  748. }
  749. }
  750. #endif
  751. /*------------------------------------------------------------------------------
  752. Adapter layer to original api before IDF v4.0
  753. ------------------------------------------------------------------------------*/
  754. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  755. /* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
  756. */
  757. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  758. {
  759. switch (err) {
  760. case ESP_OK:
  761. case ESP_ERR_INVALID_ARG:
  762. case ESP_ERR_NO_MEM:
  763. return err;
  764. case ESP_ERR_FLASH_NOT_INITIALISED:
  765. case ESP_ERR_FLASH_PROTECTED:
  766. return ESP_ERR_INVALID_STATE;
  767. case ESP_ERR_NOT_FOUND:
  768. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  769. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  770. return ESP_ERR_NOT_SUPPORTED;
  771. case ESP_ERR_FLASH_NO_RESPONSE:
  772. return ESP_ERR_INVALID_RESPONSE;
  773. default:
  774. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
  775. abort();
  776. }
  777. }
  778. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  779. {
  780. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  781. return spi_flash_translate_rc(err);
  782. }
  783. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  784. {
  785. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  786. return spi_flash_translate_rc(err);
  787. }
  788. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  789. {
  790. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  791. return spi_flash_translate_rc(err);
  792. }
  793. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL