spi_flash_chip_generic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 <sys/param.h> // For MIN/MAX
  16. #include "spi_flash_chip_generic.h"
  17. #include "spi_flash_defs.h"
  18. #include "esp_log.h"
  19. static const char TAG[] = "chip_generic";
  20. #define SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS 200
  21. #define SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS 4000
  22. #define SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS 500 //according to GD25Q127 + 100ms
  23. #define SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS 1300 //according to GD25Q127 + 100ms
  24. #define SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS 500
  25. #define HOST_DELAY_INTERVAL_US 1
  26. #define CHIP_WAIT_IDLE_INTERVAL_US 20
  27. esp_err_t spi_flash_chip_generic_probe(esp_flash_t *chip, uint32_t flash_id)
  28. {
  29. // This is the catch-all probe function, claim the chip always if nothing
  30. // else has claimed it yet.
  31. return ESP_OK;
  32. }
  33. esp_err_t spi_flash_chip_generic_reset(esp_flash_t *chip)
  34. {
  35. //this is written following the winbond spec..
  36. spi_flash_trans_t t;
  37. t = (spi_flash_trans_t) {
  38. .command = CMD_RST_EN,
  39. };
  40. esp_err_t err = chip->host->common_command(chip->host, &t);
  41. if (err != ESP_OK) {
  42. return err;
  43. }
  44. t = (spi_flash_trans_t) {
  45. .command = CMD_RST_DEV,
  46. };
  47. err = chip->host->common_command(chip->host, &t);
  48. if (err != ESP_OK) {
  49. return err;
  50. }
  51. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  52. return err;
  53. }
  54. esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size)
  55. {
  56. uint32_t id = chip->chip_id;
  57. *size = 0;
  58. /* Can't detect size unless the high byte of the product ID matches the same convention, which is usually 0x40 or
  59. * 0xC0 or similar. */
  60. if ((id & 0x0F00) != 0) {
  61. return ESP_ERR_FLASH_UNSUPPORTED_CHIP;
  62. }
  63. *size = 1 << (id & 0xFF);
  64. return ESP_OK;
  65. }
  66. esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip)
  67. {
  68. esp_err_t err;
  69. err = chip->chip_drv->set_chip_write_protect(chip, false);
  70. if (err == ESP_OK) {
  71. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  72. }
  73. if (err == ESP_OK) {
  74. chip->host->erase_chip(chip->host);
  75. //to save time, flush cache here
  76. if (chip->host->flush_cache) {
  77. err = chip->host->flush_cache(chip->host, 0, chip->size);
  78. if (err != ESP_OK) {
  79. return err;
  80. }
  81. }
  82. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_GENERIC_CHIP_ERASE_TIMEOUT_MS * 1000);
  83. }
  84. return err;
  85. }
  86. esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address)
  87. {
  88. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  89. if (err == ESP_OK) {
  90. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  91. }
  92. if (err == ESP_OK) {
  93. chip->host->erase_sector(chip->host, start_address);
  94. //to save time, flush cache here
  95. if (chip->host->flush_cache) {
  96. err = chip->host->flush_cache(chip->host, start_address, chip->chip_drv->sector_size);
  97. if (err != ESP_OK) {
  98. return err;
  99. }
  100. }
  101. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_GENERIC_SECTOR_ERASE_TIMEOUT_MS * 1000);
  102. }
  103. return err;
  104. }
  105. esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address)
  106. {
  107. esp_err_t err = chip->chip_drv->set_chip_write_protect(chip, false);
  108. if (err == ESP_OK) {
  109. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  110. }
  111. if (err == ESP_OK) {
  112. chip->host->erase_block(chip->host, start_address);
  113. //to save time, flush cache here
  114. if (chip->host->flush_cache) {
  115. err = chip->host->flush_cache(chip->host, start_address, chip->chip_drv->block_erase_size);
  116. if (err != ESP_OK) {
  117. return err;
  118. }
  119. }
  120. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_GENERIC_BLOCK_ERASE_TIMEOUT_MS * 1000);
  121. }
  122. return err;
  123. }
  124. esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  125. {
  126. esp_err_t err = ESP_OK;
  127. // Configure the host, and return
  128. err = spi_flash_chip_generic_config_host_io_mode(chip);
  129. if (err == ESP_ERR_NOT_SUPPORTED) {
  130. ESP_LOGE(TAG, "configure host io mode failed - unsupported");
  131. return err;
  132. }
  133. while (err == ESP_OK && length > 0) {
  134. uint32_t read_len = MIN(length, chip->host->max_read_bytes);
  135. err = chip->host->read(chip->host, buffer, address, read_len);
  136. buffer += read_len;
  137. length -= read_len;
  138. address += read_len;
  139. }
  140. return err;
  141. }
  142. esp_err_t spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  143. {
  144. esp_err_t err;
  145. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  146. if (err == ESP_OK) {
  147. // Perform the actual Page Program command
  148. chip->host->program_page(chip->host, buffer, address, length);
  149. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_GENERIC_PAGE_PROGRAM_TIMEOUT_MS * 1000);
  150. }
  151. return err;
  152. }
  153. esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  154. {
  155. esp_err_t err = ESP_OK;
  156. const uint32_t page_size = chip->chip_drv->page_size;
  157. while (err == ESP_OK && length > 0) {
  158. uint32_t page_len = MIN(chip->host->max_write_bytes, MIN(page_size, length));
  159. if ((address + page_len) / page_size != address / page_size) {
  160. // Most flash chips can't page write across a page boundary
  161. page_len = page_size - (address % page_size);
  162. }
  163. err = chip->chip_drv->set_chip_write_protect(chip, false);
  164. if (err == ESP_OK) {
  165. err = chip->chip_drv->program_page(chip, buffer, address, page_len);
  166. address += page_len;
  167. buffer = (void *)((intptr_t)buffer + page_len);
  168. length -= page_len;
  169. }
  170. }
  171. if (err == ESP_OK && chip->host->flush_cache) {
  172. err = chip->host->flush_cache(chip->host, address, length);
  173. }
  174. return err;
  175. }
  176. esp_err_t spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  177. {
  178. return ESP_ERR_FLASH_UNSUPPORTED_HOST; // TODO
  179. }
  180. esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect)
  181. {
  182. esp_err_t err = ESP_OK;
  183. err = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  184. if (err == ESP_OK) {
  185. chip->host->set_write_protect(chip->host, write_protect);
  186. }
  187. bool wp_read;
  188. err = chip->chip_drv->get_chip_write_protect(chip, &wp_read);
  189. if (err == ESP_OK && wp_read != write_protect) {
  190. // WREN flag has not been set!
  191. err = ESP_ERR_NOT_FOUND;
  192. }
  193. return err;
  194. }
  195. esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect)
  196. {
  197. esp_err_t err = ESP_OK;
  198. uint8_t status;
  199. assert(out_write_protect!=NULL);
  200. err = chip->host->read_status(chip->host, &status);
  201. if (err != ESP_OK) {
  202. return err;
  203. }
  204. *out_write_protect = ((status & SR_WREN) == 0);
  205. return err;
  206. }
  207. esp_err_t spi_flash_generic_wait_host_idle(esp_flash_t *chip, uint32_t *timeout_us)
  208. {
  209. while (chip->host->host_idle(chip->host) && *timeout_us > 0) {
  210. #if HOST_DELAY_INTERVAL_US > 0
  211. if (*timeout_us > 1) {
  212. int delay = MIN(HOST_DELAY_INTERVAL_US, *timeout_us);
  213. chip->os_func->delay_us(chip->os_func_data, delay);
  214. *timeout_us -= delay;
  215. } else {
  216. return ESP_ERR_TIMEOUT;
  217. }
  218. #endif
  219. }
  220. return ESP_OK;
  221. }
  222. esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_us)
  223. {
  224. timeout_us++; // allow at least one pass before timeout, last one has no sleep cycle
  225. uint8_t status = 0;
  226. const int interval = CHIP_WAIT_IDLE_INTERVAL_US;
  227. while (timeout_us > 0) {
  228. esp_err_t err = spi_flash_generic_wait_host_idle(chip, & timeout_us);
  229. if (err != ESP_OK) {
  230. return err;
  231. }
  232. err = chip->host->read_status(chip->host, &status);
  233. if (err != ESP_OK) {
  234. return err;
  235. }
  236. if ((status & SR_WIP) == 0) {
  237. break; // Write in progress is complete
  238. }
  239. if (timeout_us > 0 && interval > 0) {
  240. int delay = MIN(interval, timeout_us);
  241. chip->os_func->delay_us(chip->os_func_data, delay);
  242. timeout_us -= delay;
  243. }
  244. }
  245. return (timeout_us > 0) ? ESP_OK : ESP_ERR_TIMEOUT;
  246. }
  247. esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip)
  248. {
  249. uint32_t dummy_cyclelen_base;
  250. uint32_t addr_bitlen;
  251. uint32_t read_command;
  252. switch (chip->read_mode) {
  253. case SPI_FLASH_QIO:
  254. //for QIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
  255. addr_bitlen = SPI_FLASH_QIO_ADDR_BITLEN;
  256. dummy_cyclelen_base = SPI_FLASH_QIO_DUMMY_BITLEN;
  257. read_command = CMD_FASTRD_QIO;
  258. break;
  259. case SPI_FLASH_QOUT:
  260. addr_bitlen = SPI_FLASH_QOUT_ADDR_BITLEN;
  261. dummy_cyclelen_base = SPI_FLASH_QOUT_DUMMY_BITLEN;
  262. read_command = CMD_FASTRD_QUAD;
  263. break;
  264. case SPI_FLASH_DIO:
  265. //for DIO mode, the 4 bit right after the address are used for continuous mode, should be set to 0 to avoid that.
  266. addr_bitlen = SPI_FLASH_DIO_ADDR_BITLEN;
  267. dummy_cyclelen_base = SPI_FLASH_DIO_DUMMY_BITLEN;
  268. read_command = CMD_FASTRD_DIO;
  269. break;
  270. case SPI_FLASH_DOUT:
  271. addr_bitlen = SPI_FLASH_DOUT_ADDR_BITLEN;
  272. dummy_cyclelen_base = SPI_FLASH_DOUT_DUMMY_BITLEN;
  273. read_command = CMD_FASTRD_DUAL;
  274. break;
  275. case SPI_FLASH_FASTRD:
  276. addr_bitlen = SPI_FLASH_FASTRD_ADDR_BITLEN;
  277. dummy_cyclelen_base = SPI_FLASH_FASTRD_DUMMY_BITLEN;
  278. read_command = CMD_FASTRD;
  279. break;
  280. case SPI_FLASH_SLOWRD:
  281. addr_bitlen = SPI_FLASH_SLOWRD_ADDR_BITLEN;
  282. dummy_cyclelen_base = SPI_FLASH_SLOWRD_DUMMY_BITLEN;
  283. read_command = CMD_READ;
  284. break;
  285. default:
  286. return ESP_ERR_FLASH_NOT_INITIALISED;
  287. }
  288. return chip->host->configure_host_io_mode(chip->host, read_command, addr_bitlen, dummy_cyclelen_base,
  289. chip->read_mode);
  290. }
  291. esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_io_mode)
  292. {
  293. // On "generic" chips, this involves checking
  294. // bit 1 (QE) of RDSR2 (35h) result
  295. // (it works this way on GigaDevice & Fudan Micro chips, probably others...)
  296. const uint8_t BIT_QE = 1 << 1;
  297. uint32_t sr;
  298. esp_err_t ret = spi_flash_common_read_status_8b_rdsr2(chip, &sr);
  299. if (ret == ESP_OK) {
  300. *out_io_mode = ((sr & BIT_QE)? SPI_FLASH_QOUT: 0);
  301. }
  302. return ret;
  303. }
  304. esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip)
  305. {
  306. // On "generic" chips, this involves checking
  307. // bit 9 (QE) of RDSR (05h) result
  308. const uint32_t BIT_QE = 1 << 9;
  309. return spi_flash_common_set_io_mode(chip,
  310. spi_flash_common_write_status_16b_wrsr,
  311. spi_flash_common_read_status_16b_rdsr_rdsr2,
  312. BIT_QE);
  313. }
  314. static const char chip_name[] = "generic";
  315. const spi_flash_chip_t esp_flash_chip_generic = {
  316. .name = chip_name,
  317. .probe = spi_flash_chip_generic_probe,
  318. .reset = spi_flash_chip_generic_reset,
  319. .detect_size = spi_flash_chip_generic_detect_size,
  320. .erase_chip = spi_flash_chip_generic_erase_chip,
  321. .erase_sector = spi_flash_chip_generic_erase_sector,
  322. .erase_block = spi_flash_chip_generic_erase_block,
  323. .sector_size = 4 * 1024,
  324. .block_erase_size = 64 * 1024,
  325. // TODO: figure out if generic chip-wide protection bits exist across some manufacturers
  326. .get_chip_write_protect = spi_flash_chip_generic_get_write_protect,
  327. .set_chip_write_protect = spi_flash_chip_generic_set_write_protect,
  328. // Chip write protection regions do not appear to be standardised
  329. // at all, this is implemented in chip-specific drivers only.
  330. .num_protectable_regions = 0,
  331. .protectable_regions = NULL,
  332. .get_protected_regions = NULL,
  333. .set_protected_regions = NULL,
  334. .read = spi_flash_chip_generic_read,
  335. .write = spi_flash_chip_generic_write,
  336. .program_page = spi_flash_chip_generic_page_program,
  337. .page_size = 256,
  338. .write_encrypted = spi_flash_chip_generic_write_encrypted,
  339. .wait_idle = spi_flash_chip_generic_wait_idle,
  340. .set_io_mode = spi_flash_chip_generic_set_io_mode,
  341. .get_io_mode = spi_flash_chip_generic_get_io_mode,
  342. };
  343. /*******************************************************************************
  344. * Utility functions
  345. ******************************************************************************/
  346. 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)
  347. {
  348. uint32_t sr_buf = 0;
  349. spi_flash_trans_t t = {
  350. .command = qe_rdsr_command,
  351. .miso_data = (uint8_t*) &sr_buf,
  352. .miso_len = qe_sr_bitwidth / 8,
  353. };
  354. esp_err_t ret = chip->host->common_command(chip->host, &t);
  355. *sr = sr_buf;
  356. return ret;
  357. }
  358. 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)
  359. {
  360. spi_flash_trans_t t = {
  361. .command = qe_wrsr_command,
  362. .mosi_data = ((uint8_t*) &qe),
  363. .mosi_len = qe_sr_bitwidth / 8,
  364. .miso_len = 0,
  365. };
  366. return chip->host->common_command(chip->host, &t);
  367. }
  368. esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
  369. {
  370. uint32_t sr, sr2;
  371. esp_err_t ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, &sr2);
  372. if (ret == ESP_OK) {
  373. ret = spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, &sr);
  374. }
  375. if (ret == ESP_OK) {
  376. *out_sr = (sr & 0xff) | ((sr2 & 0xff) << 8);
  377. }
  378. return ret;
  379. }
  380. esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr)
  381. {
  382. return spi_flash_common_read_qe_sr(chip, CMD_RDSR2, 8, out_sr);
  383. }
  384. esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr)
  385. {
  386. return spi_flash_common_read_qe_sr(chip, CMD_RDSR, 8, out_sr);
  387. }
  388. esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr)
  389. {
  390. return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 16, sr);
  391. }
  392. esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr)
  393. {
  394. return spi_flash_common_write_qe_sr(chip, CMD_WRSR, 8, sr);
  395. }
  396. esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr)
  397. {
  398. return spi_flash_common_write_qe_sr(chip, CMD_WRSR2, 8, sr);
  399. }
  400. 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)
  401. {
  402. esp_err_t ret = ESP_OK;
  403. const bool is_quad_mode = esp_flash_is_quad_mode(chip);
  404. bool update_config = false;
  405. /*
  406. * By default, we don't clear the QE bit even the flash mode is not QIO or QOUT. Force clearing
  407. * QE bit by the generic chip driver (command 01H with 2 bytes) may cause the output of some
  408. * chips (MXIC) no longer valid.
  409. * Enable this option when testing a new flash chip for clearing of QE.
  410. */
  411. const bool force_check = false;
  412. bool need_check = is_quad_mode || force_check;
  413. uint32_t sr_update;
  414. if (need_check) {
  415. // Ensure quad modes are enabled, using the Quad Enable parameters supplied.
  416. uint32_t sr;
  417. ret = (*rdsr_func)(chip, &sr);
  418. if (ret != ESP_OK) {
  419. return ret;
  420. }
  421. ESP_EARLY_LOGD(TAG, "set_io_mode: status before 0x%x", sr);
  422. if (is_quad_mode) {
  423. sr_update = sr | qe_sr_bit;
  424. } else {
  425. sr_update = sr & (~qe_sr_bit);
  426. }
  427. ESP_EARLY_LOGV(TAG, "set_io_mode: status update 0x%x", sr_update);
  428. if (sr != sr_update) {
  429. update_config = true;
  430. }
  431. }
  432. if (update_config) {
  433. //some chips needs the write protect to be disabled before writing to Status Register
  434. chip->chip_drv->set_chip_write_protect(chip, false);
  435. ret = (*wrsr_func)(chip, sr_update);
  436. if (ret != ESP_OK) {
  437. return ret;
  438. }
  439. ret = chip->chip_drv->wait_idle(chip, SPI_FLASH_DEFAULT_IDLE_TIMEOUT_MS * 1000);
  440. if (ret != ESP_OK) {
  441. return ret;
  442. }
  443. /* Check the new QE bit has stayed set */
  444. uint32_t sr;
  445. ret = (*rdsr_func)(chip, &sr);
  446. if (ret != ESP_OK) {
  447. return ret;
  448. }
  449. ESP_EARLY_LOGD(TAG, "set_io_mode: status after 0x%x", sr);
  450. if (sr != sr_update) {
  451. ret = ESP_ERR_FLASH_NO_RESPONSE;
  452. }
  453. chip->chip_drv->set_chip_write_protect(chip, true);
  454. }
  455. return ret;
  456. }