spi_flash_chip_generic.c 16 KB

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