spi_flash_chip_generic.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/param.h> // For MIN/MAX
  9. #include "spi_flash_chip_generic.h"
  10. #include "spi_flash_defs.h"
  11. #include "hal/spi_flash_encrypt_hal.h"
  12. #include "esp_log.h"
  13. #include "esp_attr.h"
  14. #include "esp_private/spi_flash_os.h"
  15. typedef struct flash_chip_dummy {
  16. uint8_t dio_dummy_bitlen;
  17. uint8_t qio_dummy_bitlen;
  18. uint8_t qout_dummy_bitlen;
  19. uint8_t dout_dummy_bitlen;
  20. uint8_t fastrd_dummy_bitlen;
  21. uint8_t slowrd_dummy_bitlen;
  22. } flash_chip_dummy_t;
  23. // These parameters can be placed in the ROM. For now we use the code in IDF.
  24. DRAM_ATTR const static flash_chip_dummy_t default_flash_chip_dummy = {
  25. .dio_dummy_bitlen = SPI_FLASH_DIO_DUMMY_BITLEN,
  26. .qio_dummy_bitlen = SPI_FLASH_QIO_DUMMY_BITLEN,
  27. .qout_dummy_bitlen = SPI_FLASH_QOUT_DUMMY_BITLEN,
  28. .dout_dummy_bitlen = SPI_FLASH_DOUT_DUMMY_BITLEN,
  29. .fastrd_dummy_bitlen = SPI_FLASH_FASTRD_DUMMY_BITLEN,
  30. .slowrd_dummy_bitlen = SPI_FLASH_SLOWRD_DUMMY_BITLEN,
  31. };
  32. DRAM_ATTR const static flash_chip_dummy_t hpm_flash_chip_dummy = {
  33. .dio_dummy_bitlen = SPI_FLASH_DIO_HPM_DUMMY_BITLEN,
  34. .qio_dummy_bitlen = SPI_FLASH_QIO_HPM_DUMMY_BITLEN,
  35. .qout_dummy_bitlen = SPI_FLASH_QOUT_DUMMY_BITLEN,
  36. .dout_dummy_bitlen = SPI_FLASH_DOUT_DUMMY_BITLEN,
  37. .fastrd_dummy_bitlen = SPI_FLASH_FASTRD_DUMMY_BITLEN,
  38. .slowrd_dummy_bitlen = SPI_FLASH_SLOWRD_DUMMY_BITLEN,
  39. };
  40. DRAM_ATTR flash_chip_dummy_t *rom_flash_chip_dummy = (flash_chip_dummy_t *)&default_flash_chip_dummy;
  41. DRAM_ATTR flash_chip_dummy_t *rom_flash_chip_dummy_hpm = (flash_chip_dummy_t *)&hpm_flash_chip_dummy;
  42. // These are the pointer to HW flash encryption. Default using hardware encryption.
  43. DRAM_ATTR static spi_flash_encryption_t esp_flash_encryption_default __attribute__((__unused__)) = {
  44. .flash_encryption_enable = spi_flash_encryption_hal_enable,
  45. .flash_encryption_disable = spi_flash_encryption_hal_disable,
  46. .flash_encryption_data_prepare = spi_flash_encryption_hal_prepare,
  47. .flash_encryption_done = spi_flash_encryption_hal_done,
  48. .flash_encryption_destroy = spi_flash_encryption_hal_destroy,
  49. .flash_encryption_check = spi_flash_encryption_hal_check,
  50. };
  51. #define SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS 200
  52. #define SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS 4000
  53. #define SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS 600 //according to GD25Q127(125°) + 100ms
  54. #define SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS 4100 //according to GD25Q127(125°) + 100ms
  55. #define SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS 500
  56. #define HOST_DELAY_INTERVAL_US 1
  57. #define CHIP_WAIT_IDLE_INTERVAL_US 20
  58. #define SPI_FLASH_LINEAR_DENSITY_LAST_VALUE (0x19)
  59. #define SPI_FLASH_HEX_A_F_RANGE (6)
  60. const DRAM_ATTR flash_chip_op_timeout_t spi_flash_chip_generic_timeout = {
  61. .idle_timeout = SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000,
  62. .chip_erase_timeout = SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS * 1000,
  63. .block_erase_timeout = SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS * 1000,
  64. .sector_erase_timeout = SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS * 1000,
  65. .page_program_timeout = SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS * 1000,
  66. };
  67. #define SET_FLASH_ERASE_STATUS(CHIP, status) do { \
  68. if (CHIP->os_func->set_flash_op_status) { \
  69. CHIP->os_func->set_flash_op_status(status); \
  70. } \
  71. } while(0)
  72. static const char TAG[] = "chip_generic";
  73. esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size)
  74. {
  75. uint32_t id = chip->chip_id;
  76. *size = 0;
  77. /* Can't detect size unless the high byte of the product ID matches the same convention, which is usually 0x40 or
  78. * 0xC0 or similar. */
  79. if (((id & 0xFFFF) == 0x0000) || ((id & 0xFFFF) == 0xFFFF)) {
  80. return ESP_ERR_FLASH_UNSUPPORTED_CHIP;
  81. }
  82. /* Get flash capacity from flash chip id depends on different vendors. According to majority of flash datasheets,
  83. Flash 256Mb to 512Mb directly from 0x19 to 0x20, instead of from 0x19 to 0x1a. So here we leave the common behavior.
  84. However, some other flash vendors also have their own rule, we will add them in chip specific files.
  85. */
  86. uint32_t mem_density = (id & 0xFF);
  87. if (mem_density > SPI_FLASH_LINEAR_DENSITY_LAST_VALUE ) {
  88. mem_density -= SPI_FLASH_HEX_A_F_RANGE;
  89. }
  90. *size = 1 << mem_density;
  91. return ESP_OK;
  92. }
  93. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  94. esp_err_t spi_flash_chip_generic_probe(esp_flash_t *chip, uint32_t flash_id)
  95. {
  96. // This is the catch-all probe function, claim the chip always if nothing
  97. // else has claimed it yet.
  98. return ESP_OK;
  99. }
  100. esp_err_t spi_flash_chip_generic_reset(esp_flash_t *chip)
  101. {
  102. //this is written following the winbond spec..
  103. spi_flash_trans_t t;
  104. t = (spi_flash_trans_t) {
  105. .command = CMD_RST_EN,
  106. };
  107. esp_err_t err = chip->host->driver->common_command(chip->host, &t);
  108. if (err != ESP_OK) {
  109. return err;
  110. }
  111. t = (spi_flash_trans_t) {
  112. .command = CMD_RST_DEV,
  113. };
  114. err = chip->host->driver->common_command(chip->host, &t);
  115. if (err != ESP_OK) {
  116. return err;
  117. }
  118. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  119. return err;
  120. }
  121. esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip)
  122. {
  123. esp_err_t err;
  124. err = chip->chip_drv->set_chip_write_protect(chip, false);
  125. if (err == ESP_OK) {
  126. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  127. }
  128. //The chip didn't accept the previous write command. Ignore this in preparation stage.
  129. if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
  130. SET_FLASH_ERASE_STATUS(chip, SPI_FLASH_OS_IS_ERASING_STATUS_FLAG);
  131. chip->host->driver->erase_chip(chip->host);
  132. chip->busy = 1;
  133. #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
  134. err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
  135. #else
  136. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->chip_erase_timeout);
  137. #endif
  138. SET_FLASH_ERASE_STATUS(chip, 0);
  139. }
  140. // Ensure WEL is 0, even if the erase failed.
  141. if (err == ESP_ERR_NOT_SUPPORTED) {
  142. err = chip->chip_drv->set_chip_write_protect(chip, true);
  143. }
  144. return err;
  145. }
  146. esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address)
  147. {
  148. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  149. if (err == ESP_OK) {
  150. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  151. }
  152. //The chip didn't accept the previous write command. Ignore this in preparationstage.
  153. if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
  154. SET_FLASH_ERASE_STATUS(chip, SPI_FLASH_OS_IS_ERASING_STATUS_FLAG);
  155. chip->host->driver->erase_sector(chip->host, start_address);
  156. chip->busy = 1;
  157. #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
  158. err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
  159. #else
  160. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->sector_erase_timeout);
  161. #endif
  162. SET_FLASH_ERASE_STATUS(chip, 0);
  163. }
  164. // Ensure WEL is 0, even if the erase failed.
  165. if (err == ESP_ERR_NOT_SUPPORTED) {
  166. err = chip->chip_drv->set_chip_write_protect(chip, true);
  167. }
  168. return err;
  169. }
  170. esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address)
  171. {
  172. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  173. if (err == ESP_OK) {
  174. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  175. }
  176. //The chip didn't accept the previous write command. Ignore this in preparationstage.
  177. if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
  178. SET_FLASH_ERASE_STATUS(chip, SPI_FLASH_OS_IS_ERASING_STATUS_FLAG);
  179. chip->host->driver->erase_block(chip->host, start_address);
  180. chip->busy = 1;
  181. #ifdef CONFIG_SPI_FLASH_CHECK_ERASE_TIMEOUT_DISABLED
  182. err = chip->chip_drv->wait_idle(chip, ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
  183. #else
  184. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->block_erase_timeout);
  185. #endif
  186. SET_FLASH_ERASE_STATUS(chip, 0);
  187. }
  188. // Ensure WEL is 0, even if the erase failed.
  189. if (err == ESP_ERR_NOT_SUPPORTED) {
  190. err = chip->chip_drv->set_chip_write_protect(chip, true);
  191. }
  192. return err;
  193. }
  194. esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  195. {
  196. esp_err_t err = ESP_OK;
  197. const uint32_t page_size = chip->chip_drv->page_size;
  198. uint32_t align_address;
  199. uint8_t temp_buffer[64]; //spiflash hal max length of read no longer than 64byte
  200. uint32_t config_io_flags = 0;
  201. // Configure the host, and return
  202. err = chip->chip_drv->config_host_io_mode(chip, config_io_flags);
  203. if (err == ESP_ERR_NOT_SUPPORTED) {
  204. ESP_LOGE(TAG, "configure host io mode failed - unsupported");
  205. return err;
  206. }
  207. while (err == ESP_OK && length > 0) {
  208. memset(temp_buffer, 0xFF, sizeof(temp_buffer));
  209. uint32_t read_len = chip->host->driver->read_data_slicer(chip->host, address, length, &align_address, page_size);
  210. uint32_t left_off = address - align_address;
  211. uint32_t data_len = MIN(align_address + read_len, address + length) - address;
  212. err = chip->host->driver->read(chip->host, temp_buffer, align_address, read_len);
  213. memcpy(buffer, temp_buffer + left_off, data_len);
  214. address += data_len;
  215. buffer = (void *)((intptr_t)buffer + data_len);
  216. length = length - data_len;
  217. }
  218. return err;
  219. }
  220. esp_err_t spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  221. {
  222. esp_err_t err;
  223. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  224. //The chip didn't accept the previous write command. Ignore this in preparationstage.
  225. if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
  226. // Perform the actual Page Program command
  227. chip->host->driver->program_page(chip->host, buffer, address, length);
  228. chip->busy = 1;
  229. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
  230. }
  231. // Ensure WEL is 0, even if the page program failed.
  232. if (err == ESP_ERR_NOT_SUPPORTED) {
  233. err = chip->chip_drv->set_chip_write_protect(chip, true);
  234. }
  235. return err;
  236. }
  237. esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  238. {
  239. esp_err_t err = ESP_OK;
  240. const uint32_t page_size = chip->chip_drv->page_size;
  241. uint32_t align_address;
  242. uint8_t temp_buffer[64]; //spiflash hal max length of write no longer than 64byte
  243. while (err == ESP_OK && length > 0) {
  244. memset(temp_buffer, 0xFF, sizeof(temp_buffer));
  245. uint32_t page_len = chip->host->driver->write_data_slicer(chip->host, address, length, &align_address, page_size);
  246. uint32_t left_off = address - align_address;
  247. uint32_t write_len = MIN(align_address + page_len, address + length) - address;
  248. memcpy(temp_buffer + left_off, buffer, write_len);
  249. err = chip->chip_drv->set_chip_write_protect(chip, false);
  250. if (err == ESP_OK && length > 0) {
  251. err = chip->chip_drv->program_page(chip, temp_buffer, align_address, page_len);
  252. address += write_len;
  253. buffer = (void *)((intptr_t)buffer + write_len);
  254. length -= write_len;
  255. }
  256. }
  257. // The caller is responsible to do host->driver->flush_cache, because this function may be
  258. // called in small pieces. Frequency call of flush cache will do harm to the performance.
  259. return err;
  260. }
  261. esp_err_t spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  262. {
  263. spi_flash_encryption_t *esp_flash_encryption = &esp_flash_encryption_default;
  264. esp_err_t err = ESP_OK;
  265. // Encryption must happen on main flash.
  266. if (chip != esp_flash_default_chip) {
  267. return ESP_ERR_NOT_SUPPORTED;
  268. }
  269. /* Check if the buffer and length can qualify the requirments */
  270. if (esp_flash_encryption->flash_encryption_check(address, length) != true) {
  271. return ESP_ERR_NOT_SUPPORTED;
  272. }
  273. const uint8_t *data_bytes = (const uint8_t *)buffer;
  274. esp_flash_encryption->flash_encryption_enable();
  275. while (length > 0) {
  276. int block_size;
  277. /* Write the largest block if possible */
  278. if (address % 64 == 0 && length >= 64) {
  279. block_size = 64;
  280. } else if (address % 32 == 0 && length >= 32) {
  281. block_size = 32;
  282. } else {
  283. block_size = 16;
  284. }
  285. // Prepare the flash chip (same time as AES operation, for performance)
  286. esp_flash_encryption->flash_encryption_data_prepare(address, (uint32_t *)data_bytes, block_size);
  287. err = chip->chip_drv->set_chip_write_protect(chip, false);
  288. if (err != ESP_OK) {
  289. return err;
  290. }
  291. // Waiting for encrypting buffer to finish and making result visible for SPI1
  292. esp_flash_encryption->flash_encryption_done();
  293. // Note: For encryption function, after write flash command is sent. The hardware will write the encrypted buffer
  294. // prepared in XTS_FLASH_ENCRYPTION register in function `flash_encryption_data_prepare`, instead of the origin
  295. // buffer named `data_bytes`.
  296. err = chip->chip_drv->write(chip, (uint32_t *)data_bytes, address, length);
  297. if (err != ESP_OK) {
  298. return err;
  299. }
  300. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->page_program_timeout);
  301. if (err != ESP_OK) {
  302. return err;
  303. }
  304. // Note: we don't wait for idle status here, because this way
  305. // the AES peripheral can start encrypting the next
  306. // block while the SPI flash chip is busy completing the write
  307. esp_flash_encryption->flash_encryption_destroy();
  308. length -= block_size;
  309. data_bytes += block_size;
  310. address += block_size;
  311. }
  312. esp_flash_encryption->flash_encryption_disable();
  313. return err;
  314. }
  315. esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect)
  316. {
  317. esp_err_t err = ESP_OK;
  318. err = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  319. //The chip didn't accept the previous write command. Ignore this in preparationstage.
  320. if (err == ESP_OK || err == ESP_ERR_NOT_SUPPORTED) {
  321. chip->host->driver->set_write_protect(chip->host, write_protect);
  322. }
  323. bool wp_read;
  324. err = chip->chip_drv->get_chip_write_protect(chip, &wp_read);
  325. if (err == ESP_OK && wp_read != write_protect) {
  326. // WREN flag has not been set!
  327. err = ESP_ERR_NOT_FOUND;
  328. }
  329. return err;
  330. }
  331. esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect)
  332. {
  333. esp_err_t err = ESP_OK;
  334. uint32_t status;
  335. assert(out_write_protect!=NULL);
  336. err = chip->chip_drv->read_reg(chip, SPI_FLASH_REG_STATUS, &status);
  337. if (err != ESP_OK) {
  338. return err;
  339. }
  340. *out_write_protect = ((status & SR_WREN) == 0);
  341. return err;
  342. }
  343. esp_err_t spi_flash_chip_generic_read_reg(esp_flash_t* chip, spi_flash_register_t reg_id, uint32_t* out_reg)
  344. {
  345. return chip->host->driver->read_status(chip->host, (uint8_t*)out_reg);
  346. }
  347. esp_err_t spi_flash_chip_generic_yield(esp_flash_t* chip, uint32_t wip)
  348. {
  349. esp_err_t err = ESP_OK;
  350. uint32_t flags = wip? 1: 0; //check_yield() and yield() impls should not issue suspend/resume if this flag is zero
  351. if (chip->os_func->check_yield) {
  352. uint32_t request;
  353. //According to the implementation, the check_yield() function may block, poll, delay or do nothing but return
  354. err = chip->os_func->check_yield(chip->os_func_data, flags, &request);
  355. if (err == ESP_OK) {
  356. if (err == ESP_OK && (request & SPI_FLASH_YIELD_REQ_YIELD) != 0) {
  357. uint32_t status;
  358. //According to the implementation, the yield() function may block until something happen
  359. err = chip->os_func->yield(chip->os_func_data, &status);
  360. }
  361. } else if (err == ESP_ERR_TIMEOUT) {
  362. err = ESP_OK;
  363. } else {
  364. abort();
  365. }
  366. }
  367. return err;
  368. }
  369. esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_us)
  370. {
  371. bool timeout_en = (timeout_us != ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT);
  372. if (timeout_us == ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT) {
  373. timeout_us = 0;// In order to go into while
  374. }
  375. timeout_us++; // allow at least one pass before timeout, last one has no sleep cycle
  376. uint8_t status = 0;
  377. const int interval = CHIP_WAIT_IDLE_INTERVAL_US;
  378. while (timeout_us > 0) {
  379. while (!chip->host->driver->host_status(chip->host) && timeout_us > 0) {
  380. #if HOST_DELAY_INTERVAL_US > 0
  381. if (timeout_us > 1) {
  382. int delay = MIN(HOST_DELAY_INTERVAL_US, timeout_us);
  383. chip->os_func->delay_us(chip->os_func_data, delay);
  384. timeout_us -= delay;
  385. }
  386. #endif
  387. }
  388. uint32_t read;
  389. esp_err_t err = chip->chip_drv->read_reg(chip, SPI_FLASH_REG_STATUS, &read);
  390. if (err != ESP_OK) {
  391. return err;
  392. }
  393. status = read;
  394. if ((status & SR_WIP) == 0) { // Verify write in progress is complete
  395. if (chip->busy == 1) {
  396. chip->busy = 0;
  397. if ((status & SR_WREN) != 0) { // The previous command is not accepted, leaving the WEL still set.
  398. return ESP_ERR_NOT_SUPPORTED;
  399. }
  400. }
  401. break;
  402. }
  403. if (timeout_us > 0 && interval > 0) {
  404. int delay = MIN(interval, timeout_us);
  405. chip->os_func->delay_us(chip->os_func_data, delay);
  406. if (timeout_en) {
  407. timeout_us -= delay;
  408. }
  409. }
  410. }
  411. return (timeout_us > 0) ? ESP_OK : ESP_ERR_TIMEOUT;
  412. }
  413. esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip, uint32_t flags)
  414. {
  415. uint32_t dummy_cyclelen_base;
  416. uint32_t addr_bitlen;
  417. uint32_t read_command;
  418. bool conf_required = false;
  419. esp_flash_io_mode_t read_mode = chip->read_mode;
  420. bool addr_32bit = (flags & SPI_FLASH_CONFIG_IO_MODE_32B_ADDR);
  421. switch (read_mode & 0xFFFF) {
  422. case SPI_FLASH_QIO:
  423. //for QIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
  424. addr_bitlen = SPI_FLASH_QIO_ADDR_BITLEN;
  425. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->qio_dummy_bitlen : rom_flash_chip_dummy->qio_dummy_bitlen);
  426. read_command = (addr_32bit? CMD_FASTRD_QIO_4B: CMD_FASTRD_QIO);
  427. conf_required = true;
  428. break;
  429. case SPI_FLASH_QOUT:
  430. addr_bitlen = SPI_FLASH_QOUT_ADDR_BITLEN;
  431. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->qout_dummy_bitlen : rom_flash_chip_dummy->qout_dummy_bitlen);
  432. read_command = (addr_32bit? CMD_FASTRD_QUAD_4B: CMD_FASTRD_QUAD);
  433. break;
  434. case SPI_FLASH_DIO:
  435. //for DIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
  436. addr_bitlen = SPI_FLASH_DIO_ADDR_BITLEN;
  437. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->dio_dummy_bitlen : rom_flash_chip_dummy->dio_dummy_bitlen);
  438. read_command = (addr_32bit? CMD_FASTRD_DIO_4B: CMD_FASTRD_DIO);
  439. conf_required = true;
  440. break;
  441. case SPI_FLASH_DOUT:
  442. addr_bitlen = SPI_FLASH_DOUT_ADDR_BITLEN;
  443. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->dout_dummy_bitlen : rom_flash_chip_dummy->dout_dummy_bitlen);
  444. read_command = (addr_32bit? CMD_FASTRD_DUAL_4B: CMD_FASTRD_DUAL);
  445. break;
  446. case SPI_FLASH_FASTRD:
  447. addr_bitlen = SPI_FLASH_FASTRD_ADDR_BITLEN;
  448. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->fastrd_dummy_bitlen : rom_flash_chip_dummy->fastrd_dummy_bitlen);
  449. read_command = (addr_32bit? CMD_FASTRD_4B: CMD_FASTRD);
  450. break;
  451. case SPI_FLASH_SLOWRD:
  452. addr_bitlen = SPI_FLASH_SLOWRD_ADDR_BITLEN;
  453. dummy_cyclelen_base = (chip->hpm_dummy_ena ? rom_flash_chip_dummy_hpm->slowrd_dummy_bitlen : rom_flash_chip_dummy->slowrd_dummy_bitlen);
  454. read_command = (addr_32bit? CMD_READ_4B: CMD_READ);
  455. break;
  456. default:
  457. return ESP_ERR_FLASH_NOT_INITIALISED;
  458. }
  459. //For W25Q256 chip, the only difference between 4-Byte address command and 3-Byte version is the command value and the address bit length.
  460. if (addr_32bit) {
  461. addr_bitlen += 8;
  462. }
  463. if (conf_required) {
  464. read_mode |= SPI_FLASH_CONFIG_CONF_BITS;
  465. }
  466. return chip->host->driver->configure_host_io_mode(chip->host, read_command, addr_bitlen, dummy_cyclelen_base, read_mode);
  467. }
  468. esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
  469. {
  470. // On "generic" chips, this involves checking
  471. // bit 1 (QE) of RDSR2 (35h) result
  472. // (it works this way on GigaDevice & Fudan Micro chips, probably others...)
  473. const uint8_t BIT_QE = 1 << 1;
  474. uint32_t sr;
  475. esp_err_t ret = spi_flash_common_read_status_8b_rdsr2(chip, &sr);
  476. if (ret == ESP_OK) {
  477. *out_io_mode = ((sr & BIT_QE)? SPI_FLASH_QOUT: 0);
  478. }
  479. return ret;
  480. }
  481. esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip)
  482. {
  483. // On "generic" chips, this involves checking
  484. // bit 9 (QE) of RDSR (05h) result
  485. const uint32_t BIT_QE = 1 << 9;
  486. return spi_flash_common_set_io_mode(chip,
  487. spi_flash_common_write_status_16b_wrsr,
  488. spi_flash_common_read_status_16b_rdsr_rdsr2,
  489. BIT_QE);
  490. }
  491. #endif // CONFIG_SPI_FLASH_ROM_IMPL
  492. esp_err_t spi_flash_chip_generic_read_unique_id(esp_flash_t *chip, uint64_t* flash_unique_id)
  493. {
  494. uint64_t unique_id_buf = 0;
  495. spi_flash_trans_t transfer = {
  496. .command = CMD_RDUID,
  497. .miso_len = 8,
  498. .miso_data = ((uint8_t *)&unique_id_buf),
  499. .dummy_bitlen = 32, //RDUID command followed by 4 bytes (32 bits) of dummy clocks.
  500. };
  501. esp_err_t err = chip->host->driver->common_command(chip->host, &transfer);
  502. if (unique_id_buf == 0 || unique_id_buf == UINT64_MAX) {
  503. ESP_EARLY_LOGE(TAG, "No response from device when trying to retrieve Unique ID\n");
  504. *flash_unique_id = unique_id_buf;
  505. return ESP_ERR_NOT_SUPPORTED;
  506. }
  507. *flash_unique_id = __builtin_bswap64(unique_id_buf);
  508. return err;
  509. }
  510. esp_err_t spi_flash_chip_generic_read_unique_id_none(esp_flash_t *chip, uint64_t* flash_unique_id)
  511. {
  512. // For flash doesn't support read unique id.
  513. return ESP_ERR_NOT_SUPPORTED;
  514. }
  515. spi_flash_caps_t spi_flash_chip_generic_get_caps(esp_flash_t *chip)
  516. {
  517. // For generic part flash capability, take the XMC chip as reference.
  518. spi_flash_caps_t caps_flags = 0;
  519. // 32M-bits address support
  520. // flash suspend support
  521. // XMC support suspend
  522. if (chip->chip_id >> 16 == 0x20) {
  523. caps_flags |= SPI_FLASH_CHIP_CAP_SUSPEND;
  524. }
  525. // FM support suspend
  526. if (chip->chip_id >> 16 == 0xa1) {
  527. caps_flags |= SPI_FLASH_CHIP_CAP_SUSPEND;
  528. }
  529. // flash read unique id.
  530. caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID;
  531. return caps_flags;
  532. }
  533. static const char chip_name[] = "generic";
  534. const spi_flash_chip_t esp_flash_chip_generic = {
  535. .name = chip_name,
  536. .timeout = &spi_flash_chip_generic_timeout,
  537. .probe = spi_flash_chip_generic_probe,
  538. .reset = spi_flash_chip_generic_reset,
  539. .detect_size = spi_flash_chip_generic_detect_size,
  540. .erase_chip = spi_flash_chip_generic_erase_chip,
  541. .erase_sector = spi_flash_chip_generic_erase_sector,
  542. .erase_block = spi_flash_chip_generic_erase_block,
  543. .sector_size = 4 * 1024,
  544. .block_erase_size = 64 * 1024,
  545. // TODO: figure out if generic chip-wide protection bits exist across some manufacturers
  546. .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
  547. .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
  548. // Chip write protection regions do not appear to be standardised
  549. // at all, this is implemented in chip-specific drivers only.
  550. .num_protectable_regions = 0,
  551. .protectable_regions = NULL,
  552. .get_protected_regions = NULL,
  553. .set_protected_regions = NULL,
  554. .read = spi_flash_chip_generic_read,
  555. .write = spi_flash_chip_generic_write,
  556. .program_page = spi_flash_chip_generic_page_program,
  557. .page_size = 256,
  558. .write_encrypted = spi_flash_chip_generic_write_encrypted,
  559. .wait_idle = spi_flash_chip_generic_wait_idle,
  560. .set_io_mode = spi_flash_chip_generic_set_io_mode,
  561. .get_io_mode = spi_flash_chip_generic_get_io_mode,
  562. .read_reg = spi_flash_chip_generic_read_reg,
  563. .yield = spi_flash_chip_generic_yield,
  564. .sus_setup = spi_flash_chip_generic_suspend_cmd_conf,
  565. .read_unique_id = spi_flash_chip_generic_read_unique_id,
  566. .get_chip_caps = spi_flash_chip_generic_get_caps,
  567. .config_host_io_mode = spi_flash_chip_generic_config_host_io_mode,
  568. };
  569. #ifndef CONFIG_SPI_FLASH_ROM_IMPL
  570. /*******************************************************************************
  571. * Utility functions
  572. ******************************************************************************/
  573. static esp_err_t spi_flash_common_read_qe_sr(esp_flash_t *chip, uint8_t qe_rdsr_command, uint8_t qe_sr_bitwidth, uint32_t *sr)
  574. {
  575. uint32_t sr_buf = 0;
  576. spi_flash_trans_t t = {
  577. .command = qe_rdsr_command,
  578. .miso_data = (uint8_t*) &sr_buf,
  579. .miso_len = qe_sr_bitwidth / 8,
  580. };
  581. esp_err_t ret = chip->host->driver->common_command(chip->host, &t);
  582. *sr = sr_buf;
  583. return ret;
  584. }
  585. static esp_err_t spi_flash_common_write_qe_sr(esp_flash_t *chip, uint8_t qe_wrsr_command, uint8_t qe_sr_bitwidth, uint32_t qe)
  586. {
  587. spi_flash_trans_t t = {
  588. .command = qe_wrsr_command,
  589. .mosi_data = ((uint8_t*) &qe),
  590. .mosi_len = qe_sr_bitwidth / 8,
  591. .miso_len = 0,
  592. };
  593. return chip->host->driver->common_command(chip->host, &t);
  594. }
  595. esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
  596. {
  597. uint32_t sr, sr2;
  598. esp_err_t ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, &sr2);
  599. if (ret == ESP_OK) {
  600. ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, &sr);
  601. }
  602. if (ret == ESP_OK) {
  603. *out_sr = (sr & 0xff) | ((sr2 & 0xff) << 8);
  604. }
  605. return ret;
  606. }
  607. esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
  608. {
  609. return spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, out_sr);
  610. }
  611. esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr)
  612. {
  613. return spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, out_sr);
  614. }
  615. esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr)
  616. {
  617. return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 16, sr);
  618. }
  619. esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr)
  620. {
  621. return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 8, sr);
  622. }
  623. esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr)
  624. {
  625. return spi_flash_common_write_qe_sr(chip, CMD_WRSR2, 8, sr);
  626. }
  627. esp_err_t spi_flash_common_set_io_mode(esp_flash_t *chip, esp_flash_wrsr_func_t wrsr_func, esp_flash_rdsr_func_t rdsr_func, uint32_t qe_sr_bit)
  628. {
  629. esp_err_t ret = ESP_OK;
  630. const bool is_quad_mode = esp_flash_is_quad_mode(chip);
  631. bool update_config = false;
  632. /*
  633. * By default, we don't clear the QE bit even the flash mode is not QIO or QOUT. Force clearing
  634. * QE bit by the generic chip driver (command 01H with 2 bytes) may cause the output of some
  635. * chips (MXIC) no longer valid.
  636. * Enable this option when testing a new flash chip for clearing of QE.
  637. */
  638. const bool force_check = false;
  639. bool need_check = is_quad_mode || force_check;
  640. uint32_t sr_update;
  641. if (need_check) {
  642. // Ensure quad modes are enabled, using the Quad Enable parameters supplied.
  643. uint32_t sr;
  644. ret = (*rdsr_func)(chip, &sr);
  645. if (ret != ESP_OK) {
  646. return ret;
  647. }
  648. ESP_EARLY_LOGD(TAG, "set_io_mode: status before 0x%x", sr);
  649. if (is_quad_mode) {
  650. sr_update = sr | qe_sr_bit;
  651. } else {
  652. sr_update = sr & (~qe_sr_bit);
  653. }
  654. ESP_EARLY_LOGV(TAG, "set_io_mode: status update 0x%x", sr_update);
  655. if (sr != sr_update) {
  656. update_config = true;
  657. }
  658. }
  659. if (update_config) {
  660. //some chips needs the write protect to be disabled before writing to Status Register
  661. chip->chip_drv->set_chip_write_protect(chip, false);
  662. ret = (*wrsr_func)(chip, sr_update);
  663. if (ret != ESP_OK) {
  664. chip->chip_drv->set_chip_write_protect(chip, true);
  665. return ret;
  666. }
  667. ret = chip->chip_drv->wait_idle(chip, chip->chip_drv->timeout->idle_timeout);
  668. if (ret == ESP_ERR_NOT_SUPPORTED) {
  669. chip->chip_drv->set_chip_write_protect(chip, true);
  670. }
  671. /* This function is the fallback approach, so we give it higher tolerance.
  672. * When the previous WRSR is rejected by the flash,
  673. * the result of this function is determined by the result -whether the value of RDSR meets the expectation.
  674. */
  675. if (ret != ESP_OK && ret != ESP_ERR_NOT_SUPPORTED) {
  676. return ret;
  677. }
  678. /* Check the new QE bit has stayed set */
  679. uint32_t sr;
  680. ret = (*rdsr_func)(chip, &sr);
  681. if (ret != ESP_OK) {
  682. return ret;
  683. }
  684. ESP_EARLY_LOGD(TAG, "set_io_mode: status after 0x%x", sr);
  685. if (sr != sr_update) {
  686. ret = ESP_ERR_FLASH_NO_RESPONSE;
  687. }
  688. }
  689. return ret;
  690. }
  691. #endif // !CONFIG_SPI_FLASH_ROM_IMPL
  692. esp_err_t spi_flash_chip_generic_suspend_cmd_conf(esp_flash_t *chip)
  693. {
  694. // chips which support auto-suspend
  695. if (chip->chip_id >> 16 != 0x20 && chip->chip_id >> 16 != 0xa1) {
  696. ESP_EARLY_LOGE(TAG, "The flash you use doesn't support auto suspend, only \'XMC\' is supported");
  697. return ESP_ERR_NOT_SUPPORTED;
  698. }
  699. spi_flash_sus_cmd_conf sus_conf = {
  700. .sus_mask = 0x80,
  701. .cmd_rdsr = CMD_RDSR2,
  702. .sus_cmd = CMD_SUSPEND,
  703. .res_cmd = CMD_RESUME,
  704. };
  705. return chip->host->driver->sus_setup(chip->host, &sus_conf);
  706. }