spi_flash_chip_generic.c 28 KB

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