flash_ops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/param.h> // For MIN/MAX(a, b)
  19. #include <freertos/FreeRTOS.h>
  20. #include <freertos/task.h>
  21. #include <freertos/semphr.h>
  22. #include <rom/spi_flash.h>
  23. #include <rom/cache.h>
  24. #include <soc/soc.h>
  25. #include <soc/dport_reg.h>
  26. #include "sdkconfig.h"
  27. #include "esp_ipc.h"
  28. #include "esp_attr.h"
  29. #include "esp_spi_flash.h"
  30. #include "esp_log.h"
  31. #include "esp_clk.h"
  32. #include "cache_utils.h"
  33. /* bytes erased by SPIEraseBlock() ROM function */
  34. #define BLOCK_ERASE_SIZE 65536
  35. /* Limit number of bytes written/read in a single SPI operation,
  36. as these operations disable all higher priority tasks from running.
  37. */
  38. #define MAX_WRITE_CHUNK 8192
  39. #define MAX_READ_CHUNK 16384
  40. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  41. static const char *TAG = "spi_flash";
  42. static spi_flash_counters_t s_flash_stats;
  43. #define COUNTER_START() uint32_t ts_begin = xthal_get_ccount()
  44. #define COUNTER_STOP(counter) \
  45. do{ \
  46. s_flash_stats.counter.count++; \
  47. s_flash_stats.counter.time += (xthal_get_ccount() - ts_begin) / (esp_clk_cpu_freq() / 1000000); \
  48. } while(0)
  49. #define COUNTER_ADD_BYTES(counter, size) \
  50. do { \
  51. s_flash_stats.counter.bytes += size; \
  52. } while (0)
  53. #else
  54. #define COUNTER_START()
  55. #define COUNTER_STOP(counter)
  56. #define COUNTER_ADD_BYTES(counter, size)
  57. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
  58. static esp_err_t spi_flash_translate_rc(esp_rom_spiflash_result_t rc);
  59. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_default_ops = {
  60. .start = spi_flash_disable_interrupts_caches_and_other_cpu,
  61. .end = spi_flash_enable_interrupts_caches_and_other_cpu,
  62. .op_lock = spi_flash_op_lock,
  63. .op_unlock = spi_flash_op_unlock
  64. };
  65. const DRAM_ATTR spi_flash_guard_funcs_t g_flash_guard_no_os_ops = {
  66. .start = spi_flash_disable_interrupts_caches_and_other_cpu_no_os,
  67. .end = spi_flash_enable_interrupts_caches_no_os,
  68. .op_lock = 0,
  69. .op_unlock = 0
  70. };
  71. static const spi_flash_guard_funcs_t *s_flash_guard_ops;
  72. void spi_flash_init()
  73. {
  74. spi_flash_init_lock();
  75. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  76. spi_flash_reset_counters();
  77. #endif
  78. }
  79. void IRAM_ATTR spi_flash_guard_set(const spi_flash_guard_funcs_t *funcs)
  80. {
  81. s_flash_guard_ops = funcs;
  82. }
  83. size_t IRAM_ATTR spi_flash_get_chip_size()
  84. {
  85. return g_rom_flashchip.chip_size;
  86. }
  87. static inline void IRAM_ATTR spi_flash_guard_start()
  88. {
  89. if (s_flash_guard_ops && s_flash_guard_ops->start) {
  90. s_flash_guard_ops->start();
  91. }
  92. }
  93. static inline void IRAM_ATTR spi_flash_guard_end()
  94. {
  95. if (s_flash_guard_ops && s_flash_guard_ops->end) {
  96. s_flash_guard_ops->end();
  97. }
  98. }
  99. static inline void IRAM_ATTR spi_flash_guard_op_lock()
  100. {
  101. if (s_flash_guard_ops && s_flash_guard_ops->op_lock) {
  102. s_flash_guard_ops->op_lock();
  103. }
  104. }
  105. static inline void IRAM_ATTR spi_flash_guard_op_unlock()
  106. {
  107. if (s_flash_guard_ops && s_flash_guard_ops->op_unlock) {
  108. s_flash_guard_ops->op_unlock();
  109. }
  110. }
  111. static esp_rom_spiflash_result_t IRAM_ATTR spi_flash_unlock()
  112. {
  113. static bool unlocked = false;
  114. if (!unlocked) {
  115. spi_flash_guard_start();
  116. esp_rom_spiflash_result_t rc = esp_rom_spiflash_unlock();
  117. spi_flash_guard_end();
  118. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  119. return rc;
  120. }
  121. unlocked = true;
  122. }
  123. return ESP_ROM_SPIFLASH_RESULT_OK;
  124. }
  125. esp_err_t IRAM_ATTR spi_flash_erase_sector(size_t sec)
  126. {
  127. return spi_flash_erase_range(sec * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);
  128. }
  129. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  130. {
  131. if (start_addr % SPI_FLASH_SEC_SIZE != 0) {
  132. return ESP_ERR_INVALID_ARG;
  133. }
  134. if (size % SPI_FLASH_SEC_SIZE != 0) {
  135. return ESP_ERR_INVALID_SIZE;
  136. }
  137. if (size + start_addr > spi_flash_get_chip_size()) {
  138. return ESP_ERR_INVALID_SIZE;
  139. }
  140. size_t start = start_addr / SPI_FLASH_SEC_SIZE;
  141. size_t end = start + size / SPI_FLASH_SEC_SIZE;
  142. const size_t sectors_per_block = BLOCK_ERASE_SIZE / SPI_FLASH_SEC_SIZE;
  143. COUNTER_START();
  144. esp_rom_spiflash_result_t rc;
  145. rc = spi_flash_unlock();
  146. if (rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  147. for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
  148. spi_flash_guard_start();
  149. if (sector % sectors_per_block == 0 && end - sector > sectors_per_block) {
  150. rc = esp_rom_spiflash_erase_block(sector / sectors_per_block);
  151. sector += sectors_per_block;
  152. COUNTER_ADD_BYTES(erase, sectors_per_block * SPI_FLASH_SEC_SIZE);
  153. } else {
  154. rc = esp_rom_spiflash_erase_sector(sector);
  155. ++sector;
  156. COUNTER_ADD_BYTES(erase, SPI_FLASH_SEC_SIZE);
  157. }
  158. spi_flash_guard_end();
  159. }
  160. }
  161. COUNTER_STOP(erase);
  162. return spi_flash_translate_rc(rc);
  163. }
  164. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  165. {
  166. // Out of bound writes are checked in ROM code, but we can give better
  167. // error code here
  168. if (dst + size > g_rom_flashchip.chip_size) {
  169. return ESP_ERR_INVALID_SIZE;
  170. }
  171. if (size == 0) {
  172. return ESP_OK;
  173. }
  174. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  175. COUNTER_START();
  176. const uint8_t *srcc = (const uint8_t *) srcv;
  177. /*
  178. * Large operations are split into (up to) 3 parts:
  179. * - Left padding: 4 bytes up to the first 4-byte aligned destination offset.
  180. * - Middle part
  181. * - Right padding: 4 bytes from the last 4-byte aligned offset covered.
  182. */
  183. size_t left_off = dst & ~3U;
  184. size_t left_size = MIN(((dst + 3) & ~3U) - dst, size);
  185. size_t mid_off = left_size;
  186. size_t mid_size = (size - left_size) & ~3U;
  187. size_t right_off = left_size + mid_size;
  188. size_t right_size = size - mid_size - left_size;
  189. rc = spi_flash_unlock();
  190. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  191. goto out;
  192. }
  193. if (left_size > 0) {
  194. uint32_t t = 0xffffffff;
  195. memcpy(((uint8_t *) &t) + (dst - left_off), srcc, left_size);
  196. spi_flash_guard_start();
  197. rc = esp_rom_spiflash_write(left_off, &t, 4);
  198. spi_flash_guard_end();
  199. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  200. goto out;
  201. }
  202. COUNTER_ADD_BYTES(write, 4);
  203. }
  204. if (mid_size > 0) {
  205. /* If src buffer is 4-byte aligned as well and is not in a region that requires cache access to be enabled, we
  206. * can write directly without buffering in RAM. */
  207. #ifdef ESP_PLATFORM
  208. bool direct_write = esp_ptr_internal(srcc)
  209. && esp_ptr_byte_accessible(srcc)
  210. && ((uintptr_t) srcc + mid_off) % 4 == 0;
  211. #else
  212. bool direct_write = true;
  213. #endif
  214. while(mid_size > 0 && rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  215. uint32_t write_buf[8];
  216. uint32_t write_size = MIN(mid_size, MAX_WRITE_CHUNK);
  217. const uint8_t *write_src = srcc + mid_off;
  218. if (!direct_write) {
  219. write_size = MIN(write_size, sizeof(write_buf));
  220. memcpy(write_buf, write_src, write_size);
  221. write_src = (const uint8_t *)write_buf;
  222. }
  223. spi_flash_guard_start();
  224. rc = esp_rom_spiflash_write(dst + mid_off, (const uint32_t *) write_src, write_size);
  225. spi_flash_guard_end();
  226. COUNTER_ADD_BYTES(write, write_size);
  227. mid_size -= write_size;
  228. mid_off += write_size;
  229. }
  230. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  231. goto out;
  232. }
  233. }
  234. if (right_size > 0) {
  235. uint32_t t = 0xffffffff;
  236. memcpy(&t, srcc + right_off, right_size);
  237. spi_flash_guard_start();
  238. rc = esp_rom_spiflash_write(dst + right_off, &t, 4);
  239. spi_flash_guard_end();
  240. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  241. goto out;
  242. }
  243. COUNTER_ADD_BYTES(write, 4);
  244. }
  245. out:
  246. COUNTER_STOP(write);
  247. spi_flash_guard_op_lock();
  248. spi_flash_mark_modified_region(dst, size);
  249. spi_flash_guard_op_unlock();
  250. return spi_flash_translate_rc(rc);
  251. }
  252. esp_err_t IRAM_ATTR spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size)
  253. {
  254. const uint8_t *ssrc = (const uint8_t *)src;
  255. if ((dest_addr % 16) != 0) {
  256. return ESP_ERR_INVALID_ARG;
  257. }
  258. if ((size % 16) != 0) {
  259. return ESP_ERR_INVALID_SIZE;
  260. }
  261. COUNTER_START();
  262. esp_rom_spiflash_result_t rc;
  263. rc = spi_flash_unlock();
  264. if (rc == ESP_ROM_SPIFLASH_RESULT_OK) {
  265. /* esp_rom_spiflash_write_encrypted encrypts data in RAM as it writes,
  266. so copy to a temporary buffer - 32 bytes at a time.
  267. Each call to esp_rom_spiflash_write_encrypted takes a 32 byte "row" of
  268. data to encrypt, and each row is two 16 byte AES blocks
  269. that share a key (as derived from flash address).
  270. */
  271. uint8_t encrypt_buf[32] __attribute__((aligned(4)));
  272. uint32_t row_size;
  273. for (size_t i = 0; i < size; i += row_size) {
  274. uint32_t row_addr = dest_addr + i;
  275. if (i == 0 && (row_addr % 32) != 0) {
  276. /* writing to second block of a 32 byte row */
  277. row_size = 16;
  278. row_addr -= 16;
  279. /* copy to second block in buffer */
  280. memcpy(encrypt_buf + 16, ssrc + i, 16);
  281. /* decrypt the first block from flash, will reencrypt to same bytes */
  282. spi_flash_read_encrypted(row_addr, encrypt_buf, 16);
  283. } else if (size - i == 16) {
  284. /* 16 bytes left, is first block of a 32 byte row */
  285. row_size = 16;
  286. /* copy to first block in buffer */
  287. memcpy(encrypt_buf, ssrc + i, 16);
  288. /* decrypt the second block from flash, will reencrypt to same bytes */
  289. spi_flash_read_encrypted(row_addr + 16, encrypt_buf + 16, 16);
  290. } else {
  291. /* Writing a full 32 byte row (2 blocks) */
  292. row_size = 32;
  293. memcpy(encrypt_buf, ssrc + i, 32);
  294. }
  295. spi_flash_guard_start();
  296. rc = esp_rom_spiflash_write_encrypted(row_addr, (uint32_t *)encrypt_buf, 32);
  297. spi_flash_guard_end();
  298. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  299. break;
  300. }
  301. }
  302. bzero(encrypt_buf, sizeof(encrypt_buf));
  303. }
  304. COUNTER_ADD_BYTES(write, size);
  305. COUNTER_STOP(write);
  306. spi_flash_guard_op_lock();
  307. spi_flash_mark_modified_region(dest_addr, size);
  308. spi_flash_guard_op_unlock();
  309. return spi_flash_translate_rc(rc);
  310. }
  311. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  312. {
  313. // Out of bound reads are checked in ROM code, but we can give better
  314. // error code here
  315. if (src + size > g_rom_flashchip.chip_size) {
  316. return ESP_ERR_INVALID_SIZE;
  317. }
  318. if (size == 0) {
  319. return ESP_OK;
  320. }
  321. esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
  322. COUNTER_START();
  323. spi_flash_guard_start();
  324. /* To simplify boundary checks below, we handle small reads separately. */
  325. if (size < 16) {
  326. uint32_t t[6]; /* Enough for 16 bytes + 4 on either side for padding. */
  327. uint32_t read_src = src & ~3U;
  328. uint32_t left_off = src & 3U;
  329. uint32_t read_size = (left_off + size + 3) & ~3U;
  330. rc = esp_rom_spiflash_read(read_src, t, read_size);
  331. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  332. goto out;
  333. }
  334. COUNTER_ADD_BYTES(read, read_size);
  335. memcpy(dstv, ((uint8_t *) t) + left_off, size);
  336. goto out;
  337. }
  338. uint8_t *dstc = (uint8_t *) dstv;
  339. intptr_t dsti = (intptr_t) dstc;
  340. /*
  341. * Large operations are split into (up to) 3 parts:
  342. * - The middle part: from the first 4-aligned position in src to the first
  343. * 4-aligned position in dst.
  344. */
  345. size_t src_mid_off = (src % 4 == 0 ? 0 : 4 - (src % 4));
  346. size_t dst_mid_off = (dsti % 4 == 0 ? 0 : 4 - (dsti % 4));
  347. size_t mid_size = (size - MAX(src_mid_off, dst_mid_off)) & ~3U;
  348. /*
  349. * - Once the middle part is in place, src_mid_off bytes from the preceding
  350. * 4-aligned source location are added on the left.
  351. */
  352. size_t pad_left_src = src & ~3U;
  353. size_t pad_left_size = src_mid_off;
  354. /*
  355. * - Finally, the right part is added: from the end of the middle part to
  356. * the end. Depending on the alignment of source and destination, this may
  357. * be a 4 or 8 byte read from pad_right_src.
  358. */
  359. size_t pad_right_src = (src + pad_left_size + mid_size) & ~3U;
  360. size_t pad_right_off = (pad_right_src - src);
  361. size_t pad_right_size = (size - pad_right_off);
  362. #ifdef ESP_PLATFORM
  363. bool direct_read = esp_ptr_internal(dstc)
  364. && esp_ptr_byte_accessible(dstc)
  365. && ((uintptr_t) dstc + dst_mid_off) % 4 == 0;
  366. #else
  367. bool direct_read = true;
  368. #endif
  369. if (mid_size > 0) {
  370. uint32_t mid_remaining = mid_size;
  371. uint32_t mid_read = 0;
  372. while (mid_remaining > 0) {
  373. uint32_t read_size = MIN(mid_remaining, MAX_READ_CHUNK);
  374. uint32_t read_buf[8];
  375. uint8_t *read_dst_final = dstc + dst_mid_off + mid_read;
  376. uint8_t *read_dst = read_dst_final;
  377. if (!direct_read) {
  378. read_size = MIN(read_size, sizeof(read_buf));
  379. read_dst = (uint8_t *) read_buf;
  380. }
  381. rc = esp_rom_spiflash_read(src + src_mid_off + mid_read,
  382. (uint32_t *) read_dst, read_size);
  383. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  384. goto out;
  385. }
  386. mid_remaining -= read_size;
  387. mid_read += read_size;
  388. if (!direct_read) {
  389. spi_flash_guard_end();
  390. memcpy(read_dst_final, read_buf, read_size);
  391. spi_flash_guard_start();
  392. } else if (mid_remaining > 0) {
  393. /* Drop guard momentarily, allows other tasks to preempt */
  394. spi_flash_guard_end();
  395. spi_flash_guard_start();
  396. }
  397. }
  398. COUNTER_ADD_BYTES(read, mid_size);
  399. /*
  400. * If offsets in src and dst are different, perform an in-place shift
  401. * to put destination data into its final position.
  402. * Note that the shift can be left (src_mid_off < dst_mid_off) or right.
  403. */
  404. if (src_mid_off != dst_mid_off) {
  405. if (!direct_read) {
  406. spi_flash_guard_end();
  407. }
  408. memmove(dstc + src_mid_off, dstc + dst_mid_off, mid_size);
  409. if (!direct_read) {
  410. spi_flash_guard_start();
  411. }
  412. }
  413. }
  414. if (pad_left_size > 0) {
  415. uint32_t t;
  416. rc = esp_rom_spiflash_read(pad_left_src, &t, 4);
  417. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  418. goto out;
  419. }
  420. COUNTER_ADD_BYTES(read, 4);
  421. if (!direct_read) {
  422. spi_flash_guard_end();
  423. }
  424. memcpy(dstc, ((uint8_t *) &t) + (4 - pad_left_size), pad_left_size);
  425. if (!direct_read) {
  426. spi_flash_guard_start();
  427. }
  428. }
  429. if (pad_right_size > 0) {
  430. uint32_t t[2];
  431. int32_t read_size = (pad_right_size <= 4 ? 4 : 8);
  432. rc = esp_rom_spiflash_read(pad_right_src, t, read_size);
  433. if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
  434. goto out;
  435. }
  436. COUNTER_ADD_BYTES(read, read_size);
  437. if (!direct_read) {
  438. spi_flash_guard_end();
  439. }
  440. memcpy(dstc + pad_right_off, t, pad_right_size);
  441. if (!direct_read) {
  442. spi_flash_guard_start();
  443. }
  444. }
  445. out:
  446. spi_flash_guard_end();
  447. COUNTER_STOP(read);
  448. return spi_flash_translate_rc(rc);
  449. }
  450. esp_err_t IRAM_ATTR spi_flash_read_encrypted(size_t src, void *dstv, size_t size)
  451. {
  452. if (src + size > g_rom_flashchip.chip_size) {
  453. return ESP_ERR_INVALID_SIZE;
  454. }
  455. if (size == 0) {
  456. return ESP_OK;
  457. }
  458. esp_err_t err;
  459. const uint8_t *map;
  460. spi_flash_mmap_handle_t map_handle;
  461. size_t map_src = src & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  462. size_t map_size = size + (src - map_src);
  463. err = spi_flash_mmap(map_src, map_size, SPI_FLASH_MMAP_DATA, (const void **)&map, &map_handle);
  464. if (err != ESP_OK) {
  465. return err;
  466. }
  467. memcpy(dstv, map + (src - map_src), size);
  468. spi_flash_munmap(map_handle);
  469. return err;
  470. }
  471. static esp_err_t IRAM_ATTR spi_flash_translate_rc(esp_rom_spiflash_result_t rc)
  472. {
  473. switch (rc) {
  474. case ESP_ROM_SPIFLASH_RESULT_OK:
  475. return ESP_OK;
  476. case ESP_ROM_SPIFLASH_RESULT_TIMEOUT:
  477. return ESP_ERR_FLASH_OP_TIMEOUT;
  478. case ESP_ROM_SPIFLASH_RESULT_ERR:
  479. default:
  480. return ESP_ERR_FLASH_OP_FAIL;
  481. }
  482. }
  483. #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
  484. static inline void dump_counter(spi_flash_counter_t *counter, const char *name)
  485. {
  486. ESP_LOGI(TAG, "%s count=%8d time=%8dus bytes=%8d\n", name,
  487. counter->count, counter->time, counter->bytes);
  488. }
  489. const spi_flash_counters_t *spi_flash_get_counters()
  490. {
  491. return &s_flash_stats;
  492. }
  493. void spi_flash_reset_counters()
  494. {
  495. memset(&s_flash_stats, 0, sizeof(s_flash_stats));
  496. }
  497. void spi_flash_dump_counters()
  498. {
  499. dump_counter(&s_flash_stats.read, "read ");
  500. dump_counter(&s_flash_stats.write, "write");
  501. dump_counter(&s_flash_stats.erase, "erase");
  502. }
  503. #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS