spi_flash_chip_generic.c 29 KB

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