spi_flash_chip_generic.c 24 KB

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