flash_ops.c 17 KB

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