esp_flash_api.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  188. err = rom_spiflash_api_funcs->start(chip);
  189. if (err != ESP_OK) {
  190. return err;
  191. }
  192. if (err == ESP_OK) {
  193. // Try to set the flash mode to whatever default mode was chosen
  194. err = chip->chip_drv->set_io_mode(chip);
  195. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  196. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  197. err = ESP_OK;
  198. }
  199. }
  200. // Done: all fields on 'chip' are initialised
  201. return rom_spiflash_api_funcs->end(chip, err);
  202. }
  203. static esp_err_t IRAM_ATTR read_id_core(esp_flash_t* chip, uint32_t* out_id, bool sanity_check)
  204. {
  205. bool installed = esp_flash_chip_driver_initialized(chip);
  206. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  207. if (err != ESP_OK) {
  208. return err;
  209. }
  210. esp_err_t (*read_id_func)(void*, uint32_t*);
  211. void* read_id_arg;
  212. if (installed && chip->chip_drv->read_id) {
  213. read_id_func = (void*)chip->chip_drv->read_id;
  214. read_id_arg = (void*)chip;
  215. } else {
  216. //default option if the chip is not detected/chosen yet.
  217. read_id_func = (void*)chip->host->driver->read_id;
  218. read_id_arg = (void*)chip->host;
  219. }
  220. // Inner function fails if it sees all-ones or all-zeroes.
  221. err = read_id_func(read_id_arg, out_id);
  222. if (sanity_check && err == ESP_OK) {
  223. // Send RDID command twice, check for a matching result and retry in case we just powered on
  224. uint32_t new_id;
  225. err = read_id_func(read_id_arg, &new_id);
  226. if (err == ESP_OK && (new_id != *out_id)) {
  227. err = ESP_ERR_FLASH_NOT_INITIALISED;
  228. }
  229. }
  230. return rom_spiflash_api_funcs->end(chip, err);
  231. }
  232. // Faster version with sanity check.
  233. // Called in esp_flash_init and unit test (though not public)
  234. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* out_id)
  235. {
  236. return read_id_core(chip, out_id, true);
  237. }
  238. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  239. esp_err_t esp_flash_read_id(esp_flash_t* chip, uint32_t* out_id)
  240. {
  241. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  242. //Accept uninitialized chip when reading chip id
  243. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  244. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  245. return read_id_core(chip, out_id, false);
  246. }
  247. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  248. static esp_err_t IRAM_ATTR NOINLINE_ATTR read_unique_id(esp_flash_t* chip, uint64_t* out_uid)
  249. {
  250. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  251. if (err != ESP_OK) {
  252. return err;
  253. }
  254. err = chip->chip_drv->read_unique_id(chip, out_uid);
  255. return rom_spiflash_api_funcs->end(chip, err);
  256. }
  257. esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t* out_uid)
  258. {
  259. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  260. if (err != ESP_OK) {
  261. return err;
  262. }
  263. if (out_uid == NULL) {
  264. return ESP_ERR_INVALID_ARG;
  265. };
  266. return read_unique_id(chip, out_uid);
  267. }
  268. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  269. {
  270. esp_err_t err;
  271. uint32_t flash_id = chip->chip_id;
  272. // Detect the chip and set the chip_drv structure for it
  273. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  274. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  275. chip->chip_drv = *drivers;
  276. // start/end SPI operation each time, for multitasking
  277. // and also so esp_flash_registered_flash_drivers can live in flash
  278. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  279. err = rom_spiflash_api_funcs->start(chip);
  280. if (err != ESP_OK) {
  281. return err;
  282. }
  283. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  284. chip->chip_drv = NULL;
  285. }
  286. // if probe succeeded, chip->drv stays set
  287. drivers++;
  288. err = rom_spiflash_api_funcs->end(chip, err);
  289. if (err != ESP_OK) {
  290. return err;
  291. }
  292. }
  293. if (!esp_flash_chip_driver_initialized(chip)) {
  294. return ESP_ERR_NOT_FOUND;
  295. }
  296. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  297. return ESP_OK;
  298. }
  299. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  300. /* Convenience macro for beginning of all API functions.
  301. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  302. * and the chip supports the operation in question.
  303. */
  304. #define VERIFY_CHIP_OP(OP) do { \
  305. if (err != ESP_OK) return err; \
  306. if (chip->chip_drv->OP == NULL) { \
  307. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  308. } \
  309. } while (0)
  310. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  311. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  312. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  313. {
  314. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  315. VERIFY_CHIP_OP(detect_size);
  316. if (out_size == NULL) {
  317. return ESP_ERR_INVALID_ARG;
  318. }
  319. if (chip->size != 0) {
  320. *out_size = chip->size;
  321. return ESP_OK;
  322. }
  323. err = rom_spiflash_api_funcs->start(chip);
  324. if (err != ESP_OK) {
  325. return err;
  326. }
  327. uint32_t detect_size;
  328. err = chip->chip_drv->detect_size(chip, &detect_size);
  329. if (err == ESP_OK) {
  330. chip->size = detect_size;
  331. }
  332. return rom_spiflash_api_funcs->end(chip, err);
  333. }
  334. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  335. {
  336. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  337. VERIFY_CHIP_OP(erase_chip);
  338. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  339. //check before the operation, in case this is called too close to the last operation
  340. if (chip->chip_drv->yield) {
  341. err = chip->chip_drv->yield(chip, 0);
  342. if (err != ESP_OK) {
  343. return err;
  344. }
  345. }
  346. err = rom_spiflash_api_funcs->start(chip);
  347. if (err != ESP_OK) {
  348. return err;
  349. }
  350. err = chip->chip_drv->erase_chip(chip);
  351. if (chip->host->driver->flush_cache) {
  352. esp_err_t flush_cache_err = chip->host->driver->flush_cache(chip->host, 0, chip->size);
  353. if (err == ESP_OK) {
  354. err = flush_cache_err;
  355. }
  356. }
  357. return rom_spiflash_api_funcs->end(chip, err);
  358. }
  359. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  360. {
  361. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  362. VERIFY_CHIP_OP(erase_sector);
  363. VERIFY_CHIP_OP(erase_block);
  364. CHECK_WRITE_ADDRESS(chip, start, len);
  365. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  366. uint32_t sector_size = chip->chip_drv->sector_size;
  367. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  368. return ESP_ERR_FLASH_NOT_INITIALISED;
  369. }
  370. if (start > chip->size || start + len > chip->size) {
  371. return ESP_ERR_INVALID_ARG;
  372. }
  373. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  374. // Can only erase multiples of the sector size, starting at sector boundary
  375. return ESP_ERR_INVALID_ARG;
  376. }
  377. err = ESP_OK;
  378. // Check for write protected regions overlapping the erase region
  379. if (chip->chip_drv->get_protected_regions != NULL &&
  380. chip->chip_drv->num_protectable_regions > 0) {
  381. err = rom_spiflash_api_funcs->start(chip);
  382. if (err != ESP_OK) {
  383. return err;
  384. }
  385. uint64_t protected = 0;
  386. err = chip->chip_drv->get_protected_regions(chip, &protected);
  387. if (err == ESP_OK && protected != 0) {
  388. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  389. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  390. if ((protected & BIT64(i))
  391. && regions_overlap(start, len, region->offset, region->size)) {
  392. err = ESP_ERR_FLASH_PROTECTED;
  393. }
  394. }
  395. }
  396. // Don't lock the SPI flash for the entire erase, as this may be very long
  397. err = rom_spiflash_api_funcs->end(chip, err);
  398. }
  399. if (err != ESP_OK) {
  400. return err;
  401. }
  402. uint32_t erase_addr = start;
  403. uint32_t len_remain = len;
  404. // Indicate whether the bus is acquired by the driver, needs to be released before return
  405. bool bus_acquired = false;
  406. while (1) {
  407. //check before the operation, in case this is called too close to the last operation
  408. if (chip->chip_drv->yield) {
  409. err = chip->chip_drv->yield(chip, 0);
  410. if (err != ESP_OK) {
  411. return err;
  412. }
  413. }
  414. err = rom_spiflash_api_funcs->start(chip);
  415. if (err != ESP_OK) {
  416. break;
  417. }
  418. bus_acquired = true;
  419. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  420. // If possible erase an entire multi-sector block
  421. if (block_erase_size > 0 && len_remain >= block_erase_size && (erase_addr % block_erase_size) == 0) {
  422. err = chip->chip_drv->erase_block(chip, erase_addr);
  423. erase_addr += block_erase_size;
  424. len_remain -= block_erase_size;
  425. } else
  426. #endif
  427. {
  428. // Otherwise erase individual sector only
  429. err = chip->chip_drv->erase_sector(chip, erase_addr);
  430. erase_addr += sector_size;
  431. len_remain -= sector_size;
  432. }
  433. if (err != ESP_OK || len_remain == 0) {
  434. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  435. // happen when the cache is still disabled on ESP32. Break before the end() function and
  436. // do end() later
  437. assert(bus_acquired);
  438. break;
  439. }
  440. err = rom_spiflash_api_funcs->end(chip, ESP_OK);
  441. if (err != ESP_OK) {
  442. break;
  443. }
  444. bus_acquired = false;
  445. }
  446. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, start, len);
  447. }
  448. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  449. {
  450. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  451. VERIFY_CHIP_OP(get_chip_write_protect);
  452. if (out_write_protected == NULL) {
  453. return ESP_ERR_INVALID_ARG;
  454. }
  455. err = rom_spiflash_api_funcs->start(chip);
  456. if (err != ESP_OK) {
  457. return err;
  458. }
  459. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  460. return rom_spiflash_api_funcs->end(chip, err);
  461. }
  462. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  463. {
  464. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  465. VERIFY_CHIP_OP(set_chip_write_protect);
  466. //TODO: skip writing if already locked or unlocked
  467. err = rom_spiflash_api_funcs->start(chip);
  468. if (err != ESP_OK) {
  469. return err;
  470. }
  471. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  472. return rom_spiflash_api_funcs->end(chip, err);
  473. }
  474. 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)
  475. {
  476. if(out_num_regions != NULL) {
  477. *out_num_regions = 0; // In case caller doesn't check result
  478. }
  479. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  480. VERIFY_CHIP_OP(get_protected_regions);
  481. if(out_regions == NULL || out_num_regions == NULL) {
  482. return ESP_ERR_INVALID_ARG;
  483. }
  484. *out_num_regions = chip->chip_drv->num_protectable_regions;
  485. *out_regions = chip->chip_drv->protectable_regions;
  486. return ESP_OK;
  487. }
  488. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  489. {
  490. if (region == NULL) {
  491. return ESP_ERR_INVALID_ARG;
  492. }
  493. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  494. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  495. region, sizeof(esp_flash_region_t)) == 0) {
  496. return ESP_OK;
  497. }
  498. }
  499. return ESP_ERR_NOT_FOUND;
  500. }
  501. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  502. {
  503. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  504. VERIFY_CHIP_OP(get_protected_regions);
  505. if (out_protected == NULL) {
  506. return ESP_ERR_INVALID_ARG;
  507. }
  508. uint8_t index;
  509. err = find_region(chip, region, &index);
  510. if (err != ESP_OK) {
  511. return err;
  512. }
  513. uint64_t protection_mask = 0;
  514. err = rom_spiflash_api_funcs->start(chip);
  515. if (err != ESP_OK) {
  516. return err;
  517. }
  518. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  519. if (err == ESP_OK) {
  520. *out_protected = protection_mask & (1LL << index);
  521. }
  522. return rom_spiflash_api_funcs->end(chip, err);
  523. }
  524. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  525. {
  526. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  527. VERIFY_CHIP_OP(set_protected_regions);
  528. uint8_t index;
  529. err = find_region(chip, region, &index);
  530. if (err != ESP_OK) {
  531. return err;
  532. }
  533. uint64_t protection_mask = 0;
  534. err = rom_spiflash_api_funcs->start(chip);
  535. if (err != ESP_OK) {
  536. return err;
  537. }
  538. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  539. if (err == ESP_OK) {
  540. if (protect) {
  541. protection_mask |= (1LL << index);
  542. } else {
  543. protection_mask &= ~(1LL << index);
  544. }
  545. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  546. }
  547. return rom_spiflash_api_funcs->end(chip, err);
  548. }
  549. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  550. {
  551. if (length == 0) {
  552. return ESP_OK;
  553. }
  554. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  555. VERIFY_CHIP_OP(read);
  556. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  557. return ESP_ERR_INVALID_ARG;
  558. }
  559. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  560. bool direct_read = chip->host->driver->supports_direct_read(chip->host, buffer);
  561. uint8_t* temp_buffer = NULL;
  562. //each time, we at most read this length
  563. //after that, we release the lock to allow some other operations
  564. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  565. if (!direct_read) {
  566. size_t actual_len = 0;
  567. if (chip->os_func->get_temp_buffer != NULL) {
  568. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  569. read_chunk_size = actual_len;
  570. }
  571. if (temp_buffer == NULL) {
  572. return ESP_ERR_NO_MEM;
  573. }
  574. }
  575. err = ESP_OK;
  576. do {
  577. err = rom_spiflash_api_funcs->start(chip);
  578. if (err != ESP_OK) {
  579. break;
  580. }
  581. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  582. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  583. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  584. size_t length_to_read = MIN(read_chunk_size, length);
  585. if (err == ESP_OK) {
  586. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  587. }
  588. if (err != ESP_OK) {
  589. rom_spiflash_api_funcs->end(chip, err);
  590. break;
  591. }
  592. //even if this is failed, the data is still valid, copy before quit
  593. err = rom_spiflash_api_funcs->end(chip, err);
  594. //copy back to the original buffer
  595. if (temp_buffer) {
  596. memcpy(buffer, temp_buffer, length_to_read);
  597. }
  598. address += length_to_read;
  599. length -= length_to_read;
  600. buffer = (void*)((intptr_t)buffer + length_to_read);
  601. } while (err == ESP_OK && length > 0);
  602. if (chip->os_func->release_temp_buffer != NULL) {
  603. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  604. }
  605. return err;
  606. }
  607. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  608. {
  609. if (length == 0) {
  610. return ESP_OK;
  611. }
  612. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  613. VERIFY_CHIP_OP(write);
  614. CHECK_WRITE_ADDRESS(chip, address, length);
  615. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  616. return ESP_ERR_INVALID_ARG;
  617. }
  618. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  619. bool direct_write = chip->host->driver->supports_direct_write(chip->host, buffer);
  620. // Indicate whether the bus is acquired by the driver, needs to be released before return
  621. bool bus_acquired = false;
  622. err = ESP_OK;
  623. /* Write output in chunks, either by buffering on stack or
  624. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  625. environment, this prevents writing from causing interrupt or higher priority task
  626. starvation.) */
  627. uint32_t write_addr = address;
  628. uint32_t len_remain = length;
  629. while (1) {
  630. uint32_t write_len;
  631. const void *write_buf;
  632. uint32_t temp_buf[8];
  633. if (direct_write) {
  634. write_len = MIN(len_remain, MAX_WRITE_CHUNK);
  635. write_buf = buffer;
  636. } else {
  637. write_len = MIN(len_remain, sizeof(temp_buf));
  638. memcpy(temp_buf, buffer, write_len);
  639. write_buf = temp_buf;
  640. }
  641. //check before the operation, in case this is called too close to the last operation
  642. if (chip->chip_drv->yield) {
  643. err = chip->chip_drv->yield(chip, 0);
  644. if (err != ESP_OK) {
  645. return err;
  646. }
  647. }
  648. err = rom_spiflash_api_funcs->start(chip);
  649. if (err != ESP_OK) {
  650. break;
  651. }
  652. bus_acquired = true;
  653. err = chip->chip_drv->write(chip, write_buf, write_addr, write_len);
  654. len_remain -= write_len;
  655. if (err != ESP_OK || len_remain == 0) {
  656. // On ESP32, the cache re-enable is in the end() function, while flush_cache should
  657. // happen when the cache is still disabled on ESP32. Break before the end() function and
  658. // do end() later
  659. assert(bus_acquired);
  660. break;
  661. }
  662. err = rom_spiflash_api_funcs->end(chip, err);
  663. if (err != ESP_OK) {
  664. break;
  665. }
  666. bus_acquired = false;
  667. write_addr += write_len;
  668. buffer = (void *)((intptr_t)buffer + write_len);
  669. }
  670. return rom_spiflash_api_funcs->flash_end_flush_cache(chip, err, bus_acquired, address, length);
  671. }
  672. //currently the legacy implementation is used, from flash_ops.c
  673. esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
  674. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  675. {
  676. /*
  677. * Since currently this feature is supported only by the hardware, there
  678. * is no way to support non-standard chips. We use the legacy
  679. * implementation and skip the chip and driver layers.
  680. */
  681. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  682. if (err != ESP_OK) return err;
  683. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  684. return ESP_ERR_INVALID_ARG;
  685. }
  686. return spi_flash_write_encrypted(address, buffer, length);
  687. }
  688. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  689. {
  690. uint32_t a_end = a_start + a_len;
  691. uint32_t b_end = b_start + b_len;
  692. return (a_end > b_start && b_end > a_start);
  693. }
  694. //currently the legacy implementation is used, from flash_ops.c
  695. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  696. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  697. {
  698. /*
  699. * Since currently this feature is supported only by the hardware, there
  700. * is no way to support non-standard chips. We use the legacy
  701. * implementation and skip the chip and driver layers.
  702. */
  703. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  704. if (err != ESP_OK) return err;
  705. return spi_flash_read_encrypted(address, out_buffer, length);
  706. }
  707. // test only, non-public
  708. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  709. {
  710. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  711. VERIFY_CHIP_OP(get_io_mode);
  712. esp_flash_io_mode_t io_mode;
  713. err = rom_spiflash_api_funcs->start(chip);
  714. if (err != ESP_OK) {
  715. return err;
  716. }
  717. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  718. err = rom_spiflash_api_funcs->end(chip, err);
  719. if (err == ESP_OK) {
  720. *qe = (io_mode == SPI_FLASH_QOUT);
  721. }
  722. return err;
  723. }
  724. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  725. {
  726. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  727. VERIFY_CHIP_OP(set_io_mode);
  728. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  729. err = rom_spiflash_api_funcs->start(chip);
  730. if (err != ESP_OK) {
  731. return err;
  732. }
  733. err = chip->chip_drv->set_io_mode(chip);
  734. return rom_spiflash_api_funcs->end(chip, err);
  735. }
  736. #endif //CONFIG_SPI_FLASH_ROM_IMPL
  737. //init suspend mode cmd, uses internal.
  738. esp_err_t esp_flash_suspend_cmd_init(esp_flash_t* chip)
  739. {
  740. ESP_EARLY_LOGW(TAG, "Flash suspend feature is enabled");
  741. return chip->chip_drv->sus_setup(chip);
  742. }
  743. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  744. esp_err_t esp_flash_app_disable_protect(bool disable)
  745. {
  746. if (disable) {
  747. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  748. } else {
  749. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  750. }
  751. }
  752. #endif
  753. /*------------------------------------------------------------------------------
  754. Adapter layer to original api before IDF v4.0
  755. ------------------------------------------------------------------------------*/
  756. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  757. /* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
  758. */
  759. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  760. {
  761. switch (err) {
  762. case ESP_OK:
  763. case ESP_ERR_INVALID_ARG:
  764. case ESP_ERR_NO_MEM:
  765. return err;
  766. case ESP_ERR_FLASH_NOT_INITIALISED:
  767. case ESP_ERR_FLASH_PROTECTED:
  768. return ESP_ERR_INVALID_STATE;
  769. case ESP_ERR_NOT_FOUND:
  770. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  771. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  772. return ESP_ERR_NOT_SUPPORTED;
  773. case ESP_ERR_FLASH_NO_RESPONSE:
  774. return ESP_ERR_INVALID_RESPONSE;
  775. default:
  776. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
  777. abort();
  778. }
  779. }
  780. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  781. {
  782. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  783. return spi_flash_translate_rc(err);
  784. }
  785. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  786. {
  787. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  788. return spi_flash_translate_rc(err);
  789. }
  790. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  791. {
  792. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  793. return spi_flash_translate_rc(err);
  794. }
  795. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL