spi_flash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /*
  2. * SPI Flash Core
  3. *
  4. * Copyright (C) 2015 Jagan Teki <jteki@openedev.com>
  5. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  6. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  7. * Copyright (C) 2008 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <errno.h>
  13. #include <malloc.h>
  14. #include <mapmem.h>
  15. #include <spi.h>
  16. #include <spi_flash.h>
  17. #include <linux/log2.h>
  18. #include <dma.h>
  19. #include "sf_internal.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static void spi_flash_addr(u32 addr, u8 *cmd)
  22. {
  23. /* cmd[0] is actual command */
  24. cmd[1] = addr >> 16;
  25. cmd[2] = addr >> 8;
  26. cmd[3] = addr >> 0;
  27. }
  28. static int read_sr(struct spi_flash *flash, u8 *rs)
  29. {
  30. int ret;
  31. u8 cmd;
  32. cmd = CMD_READ_STATUS;
  33. ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
  34. if (ret < 0) {
  35. debug("SF: fail to read status register\n");
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. static int read_fsr(struct spi_flash *flash, u8 *fsr)
  41. {
  42. int ret;
  43. const u8 cmd = CMD_FLAG_STATUS;
  44. ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
  45. if (ret < 0) {
  46. debug("SF: fail to read flag status register\n");
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. static int write_sr(struct spi_flash *flash, u8 ws)
  52. {
  53. u8 cmd;
  54. int ret;
  55. cmd = CMD_WRITE_STATUS;
  56. ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
  57. if (ret < 0) {
  58. debug("SF: fail to write status register\n");
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  64. static int read_cr(struct spi_flash *flash, u8 *rc)
  65. {
  66. int ret;
  67. u8 cmd;
  68. cmd = CMD_READ_CONFIG;
  69. ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
  70. if (ret < 0) {
  71. debug("SF: fail to read config register\n");
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. static int write_cr(struct spi_flash *flash, u8 wc)
  77. {
  78. u8 data[2];
  79. u8 cmd;
  80. int ret;
  81. ret = read_sr(flash, &data[0]);
  82. if (ret < 0)
  83. return ret;
  84. cmd = CMD_WRITE_STATUS;
  85. data[1] = wc;
  86. ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
  87. if (ret) {
  88. debug("SF: fail to write config register\n");
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. #endif
  94. #ifdef CONFIG_SPI_FLASH_BAR
  95. static int write_bar(struct spi_flash *flash, u32 offset)
  96. {
  97. u8 cmd, bank_sel;
  98. int ret;
  99. bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
  100. if (bank_sel == flash->bank_curr)
  101. goto bar_end;
  102. cmd = flash->bank_write_cmd;
  103. ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
  104. if (ret < 0) {
  105. debug("SF: fail to write bank register\n");
  106. return ret;
  107. }
  108. bar_end:
  109. flash->bank_curr = bank_sel;
  110. return flash->bank_curr;
  111. }
  112. static int read_bar(struct spi_flash *flash, const struct spi_flash_info *info)
  113. {
  114. u8 curr_bank = 0;
  115. int ret;
  116. if (flash->size <= SPI_FLASH_16MB_BOUN)
  117. goto bar_end;
  118. switch (JEDEC_MFR(info)) {
  119. case SPI_FLASH_CFI_MFR_SPANSION:
  120. flash->bank_read_cmd = CMD_BANKADDR_BRRD;
  121. flash->bank_write_cmd = CMD_BANKADDR_BRWR;
  122. break;
  123. default:
  124. flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
  125. flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
  126. }
  127. ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  128. &curr_bank, 1);
  129. if (ret) {
  130. debug("SF: fail to read bank addr register\n");
  131. return ret;
  132. }
  133. bar_end:
  134. flash->bank_curr = curr_bank;
  135. return 0;
  136. }
  137. #endif
  138. #ifdef CONFIG_SF_DUAL_FLASH
  139. static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
  140. {
  141. switch (flash->dual_flash) {
  142. case SF_DUAL_STACKED_FLASH:
  143. if (*addr >= (flash->size >> 1)) {
  144. *addr -= flash->size >> 1;
  145. flash->flags |= SNOR_F_USE_UPAGE;
  146. } else {
  147. flash->flags &= ~SNOR_F_USE_UPAGE;
  148. }
  149. break;
  150. case SF_DUAL_PARALLEL_FLASH:
  151. *addr >>= flash->shift;
  152. break;
  153. default:
  154. debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
  155. break;
  156. }
  157. }
  158. #endif
  159. static int spi_flash_sr_ready(struct spi_flash *flash)
  160. {
  161. u8 sr;
  162. int ret;
  163. ret = read_sr(flash, &sr);
  164. if (ret < 0)
  165. return ret;
  166. return !(sr & STATUS_WIP);
  167. }
  168. static int spi_flash_fsr_ready(struct spi_flash *flash)
  169. {
  170. u8 fsr;
  171. int ret;
  172. ret = read_fsr(flash, &fsr);
  173. if (ret < 0)
  174. return ret;
  175. return fsr & STATUS_PEC;
  176. }
  177. static int spi_flash_ready(struct spi_flash *flash)
  178. {
  179. int sr, fsr;
  180. sr = spi_flash_sr_ready(flash);
  181. if (sr < 0)
  182. return sr;
  183. fsr = 1;
  184. if (flash->flags & SNOR_F_USE_FSR) {
  185. fsr = spi_flash_fsr_ready(flash);
  186. if (fsr < 0)
  187. return fsr;
  188. }
  189. return sr && fsr;
  190. }
  191. static int spi_flash_wait_till_ready(struct spi_flash *flash,
  192. unsigned long timeout)
  193. {
  194. unsigned long timebase;
  195. int ret;
  196. timebase = get_timer(0);
  197. while (get_timer(timebase) < timeout) {
  198. ret = spi_flash_ready(flash);
  199. if (ret < 0)
  200. return ret;
  201. if (ret)
  202. return 0;
  203. }
  204. printf("SF: Timeout!\n");
  205. return -ETIMEDOUT;
  206. }
  207. int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
  208. size_t cmd_len, const void *buf, size_t buf_len)
  209. {
  210. struct spi_slave *spi = flash->spi;
  211. unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
  212. int ret;
  213. if (buf == NULL)
  214. timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
  215. ret = spi_claim_bus(spi);
  216. if (ret) {
  217. debug("SF: unable to claim SPI bus\n");
  218. return ret;
  219. }
  220. ret = spi_flash_cmd_write_enable(flash);
  221. if (ret < 0) {
  222. debug("SF: enabling write failed\n");
  223. return ret;
  224. }
  225. ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
  226. if (ret < 0) {
  227. debug("SF: write cmd failed\n");
  228. return ret;
  229. }
  230. ret = spi_flash_wait_till_ready(flash, timeout);
  231. if (ret < 0) {
  232. debug("SF: write %s timed out\n",
  233. timeout == SPI_FLASH_PROG_TIMEOUT ?
  234. "program" : "page erase");
  235. return ret;
  236. }
  237. spi_release_bus(spi);
  238. return ret;
  239. }
  240. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
  241. {
  242. u32 erase_size, erase_addr;
  243. u8 cmd[SPI_FLASH_CMD_LEN];
  244. int ret = -1;
  245. erase_size = flash->erase_size;
  246. if (offset % erase_size || len % erase_size) {
  247. debug("SF: Erase offset/length not multiple of erase size\n");
  248. return -1;
  249. }
  250. if (flash->flash_is_locked) {
  251. if (flash->flash_is_locked(flash, offset, len) > 0) {
  252. printf("offset 0x%x is protected and cannot be erased\n",
  253. offset);
  254. return -EINVAL;
  255. }
  256. }
  257. cmd[0] = flash->erase_cmd;
  258. while (len) {
  259. erase_addr = offset;
  260. #ifdef CONFIG_SF_DUAL_FLASH
  261. if (flash->dual_flash > SF_SINGLE_FLASH)
  262. spi_flash_dual(flash, &erase_addr);
  263. #endif
  264. #ifdef CONFIG_SPI_FLASH_BAR
  265. ret = write_bar(flash, erase_addr);
  266. if (ret < 0)
  267. return ret;
  268. #endif
  269. spi_flash_addr(erase_addr, cmd);
  270. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  271. cmd[2], cmd[3], erase_addr);
  272. ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
  273. if (ret < 0) {
  274. debug("SF: erase failed\n");
  275. break;
  276. }
  277. offset += erase_size;
  278. len -= erase_size;
  279. }
  280. return ret;
  281. }
  282. int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  283. size_t len, const void *buf)
  284. {
  285. struct spi_slave *spi = flash->spi;
  286. unsigned long byte_addr, page_size;
  287. u32 write_addr;
  288. size_t chunk_len, actual;
  289. u8 cmd[SPI_FLASH_CMD_LEN];
  290. int ret = -1;
  291. page_size = flash->page_size;
  292. if (flash->flash_is_locked) {
  293. if (flash->flash_is_locked(flash, offset, len) > 0) {
  294. printf("offset 0x%x is protected and cannot be written\n",
  295. offset);
  296. return -EINVAL;
  297. }
  298. }
  299. cmd[0] = flash->write_cmd;
  300. for (actual = 0; actual < len; actual += chunk_len) {
  301. write_addr = offset;
  302. #ifdef CONFIG_SF_DUAL_FLASH
  303. if (flash->dual_flash > SF_SINGLE_FLASH)
  304. spi_flash_dual(flash, &write_addr);
  305. #endif
  306. #ifdef CONFIG_SPI_FLASH_BAR
  307. ret = write_bar(flash, write_addr);
  308. if (ret < 0)
  309. return ret;
  310. #endif
  311. byte_addr = offset % page_size;
  312. chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
  313. if (spi->max_write_size)
  314. chunk_len = min(chunk_len,
  315. (size_t)spi->max_write_size);
  316. spi_flash_addr(write_addr, cmd);
  317. debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  318. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  319. ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
  320. buf + actual, chunk_len);
  321. if (ret < 0) {
  322. debug("SF: write failed\n");
  323. break;
  324. }
  325. offset += chunk_len;
  326. }
  327. return ret;
  328. }
  329. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  330. size_t cmd_len, void *data, size_t data_len)
  331. {
  332. struct spi_slave *spi = flash->spi;
  333. int ret;
  334. ret = spi_claim_bus(spi);
  335. if (ret) {
  336. debug("SF: unable to claim SPI bus\n");
  337. return ret;
  338. }
  339. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  340. if (ret < 0) {
  341. debug("SF: read cmd failed\n");
  342. return ret;
  343. }
  344. spi_release_bus(spi);
  345. return ret;
  346. }
  347. /*
  348. * TODO: remove the weak after all the other spi_flash_copy_mmap
  349. * implementations removed from drivers
  350. */
  351. void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
  352. {
  353. #ifdef CONFIG_DMA
  354. if (!dma_memcpy(data, offset, len))
  355. return;
  356. #endif
  357. memcpy(data, offset, len);
  358. }
  359. int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
  360. size_t len, void *data)
  361. {
  362. struct spi_slave *spi = flash->spi;
  363. u8 *cmd, cmdsz;
  364. u32 remain_len, read_len, read_addr;
  365. int bank_sel = 0;
  366. int ret = -1;
  367. /* Handle memory-mapped SPI */
  368. if (flash->memory_map) {
  369. ret = spi_claim_bus(spi);
  370. if (ret) {
  371. debug("SF: unable to claim SPI bus\n");
  372. return ret;
  373. }
  374. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP);
  375. spi_flash_copy_mmap(data, flash->memory_map + offset, len);
  376. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
  377. spi_release_bus(spi);
  378. return 0;
  379. }
  380. cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
  381. cmd = calloc(1, cmdsz);
  382. if (!cmd) {
  383. debug("SF: Failed to allocate cmd\n");
  384. return -ENOMEM;
  385. }
  386. cmd[0] = flash->read_cmd;
  387. while (len) {
  388. read_addr = offset;
  389. #ifdef CONFIG_SF_DUAL_FLASH
  390. if (flash->dual_flash > SF_SINGLE_FLASH)
  391. spi_flash_dual(flash, &read_addr);
  392. #endif
  393. #ifdef CONFIG_SPI_FLASH_BAR
  394. ret = write_bar(flash, read_addr);
  395. if (ret < 0)
  396. return ret;
  397. bank_sel = flash->bank_curr;
  398. #endif
  399. remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
  400. (bank_sel + 1)) - offset;
  401. if (len < remain_len)
  402. read_len = len;
  403. else
  404. read_len = remain_len;
  405. spi_flash_addr(read_addr, cmd);
  406. ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
  407. if (ret < 0) {
  408. debug("SF: read failed\n");
  409. break;
  410. }
  411. offset += read_len;
  412. len -= read_len;
  413. data += read_len;
  414. }
  415. free(cmd);
  416. return ret;
  417. }
  418. #ifdef CONFIG_SPI_FLASH_SST
  419. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  420. {
  421. struct spi_slave *spi = flash->spi;
  422. int ret;
  423. u8 cmd[4] = {
  424. CMD_SST_BP,
  425. offset >> 16,
  426. offset >> 8,
  427. offset,
  428. };
  429. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  430. spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
  431. ret = spi_flash_cmd_write_enable(flash);
  432. if (ret)
  433. return ret;
  434. ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
  435. if (ret)
  436. return ret;
  437. return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  438. }
  439. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  440. const void *buf)
  441. {
  442. struct spi_slave *spi = flash->spi;
  443. size_t actual, cmd_len;
  444. int ret;
  445. u8 cmd[4];
  446. ret = spi_claim_bus(spi);
  447. if (ret) {
  448. debug("SF: Unable to claim SPI bus\n");
  449. return ret;
  450. }
  451. /* If the data is not word aligned, write out leading single byte */
  452. actual = offset % 2;
  453. if (actual) {
  454. ret = sst_byte_write(flash, offset, buf);
  455. if (ret)
  456. goto done;
  457. }
  458. offset += actual;
  459. ret = spi_flash_cmd_write_enable(flash);
  460. if (ret)
  461. goto done;
  462. cmd_len = 4;
  463. cmd[0] = CMD_SST_AAI_WP;
  464. cmd[1] = offset >> 16;
  465. cmd[2] = offset >> 8;
  466. cmd[3] = offset;
  467. for (; actual < len - 1; actual += 2) {
  468. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  469. spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
  470. cmd[0], offset);
  471. ret = spi_flash_cmd_write(spi, cmd, cmd_len,
  472. buf + actual, 2);
  473. if (ret) {
  474. debug("SF: sst word program failed\n");
  475. break;
  476. }
  477. ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  478. if (ret)
  479. break;
  480. cmd_len = 1;
  481. offset += 2;
  482. }
  483. if (!ret)
  484. ret = spi_flash_cmd_write_disable(flash);
  485. /* If there is a single trailing byte, write it out */
  486. if (!ret && actual != len)
  487. ret = sst_byte_write(flash, offset, buf + actual);
  488. done:
  489. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  490. ret ? "failure" : "success", len, offset - actual);
  491. spi_release_bus(spi);
  492. return ret;
  493. }
  494. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  495. const void *buf)
  496. {
  497. struct spi_slave *spi = flash->spi;
  498. size_t actual;
  499. int ret;
  500. ret = spi_claim_bus(spi);
  501. if (ret) {
  502. debug("SF: Unable to claim SPI bus\n");
  503. return ret;
  504. }
  505. for (actual = 0; actual < len; actual++) {
  506. ret = sst_byte_write(flash, offset, buf + actual);
  507. if (ret) {
  508. debug("SF: sst byte program failed\n");
  509. break;
  510. }
  511. offset++;
  512. }
  513. if (!ret)
  514. ret = spi_flash_cmd_write_disable(flash);
  515. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  516. ret ? "failure" : "success", len, offset - actual);
  517. spi_release_bus(spi);
  518. return ret;
  519. }
  520. #endif
  521. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  522. static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
  523. u64 *len)
  524. {
  525. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  526. int shift = ffs(mask) - 1;
  527. int pow;
  528. if (!(sr & mask)) {
  529. /* No protection */
  530. *ofs = 0;
  531. *len = 0;
  532. } else {
  533. pow = ((sr & mask) ^ mask) >> shift;
  534. *len = flash->size >> pow;
  535. *ofs = flash->size - *len;
  536. }
  537. }
  538. /*
  539. * Return 1 if the entire region is locked, 0 otherwise
  540. */
  541. static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
  542. u8 sr)
  543. {
  544. loff_t lock_offs;
  545. u64 lock_len;
  546. stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
  547. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  548. }
  549. /*
  550. * Check if a region of the flash is (completely) locked. See stm_lock() for
  551. * more info.
  552. *
  553. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  554. * negative on errors.
  555. */
  556. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  557. {
  558. int status;
  559. u8 sr;
  560. status = read_sr(flash, &sr);
  561. if (status < 0)
  562. return status;
  563. return stm_is_locked_sr(flash, ofs, len, sr);
  564. }
  565. /*
  566. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  567. * Supports only the block protection bits BP{0,1,2} in the status register
  568. * (SR). Does not support these features found in newer SR bitfields:
  569. * - TB: top/bottom protect - only handle TB=0 (top protect)
  570. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  571. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  572. *
  573. * Sample table portion for 8MB flash (Winbond w25q64fw):
  574. *
  575. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  576. * --------------------------------------------------------------------------
  577. * X | X | 0 | 0 | 0 | NONE | NONE
  578. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  579. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  580. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  581. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  582. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  583. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  584. * X | X | 1 | 1 | 1 | 8 MB | ALL
  585. *
  586. * Returns negative on errors, 0 on success.
  587. */
  588. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
  589. {
  590. u8 status_old, status_new;
  591. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  592. u8 shift = ffs(mask) - 1, pow, val;
  593. int ret;
  594. ret = read_sr(flash, &status_old);
  595. if (ret < 0)
  596. return ret;
  597. /* SPI NOR always locks to the end */
  598. if (ofs + len != flash->size) {
  599. /* Does combined region extend to end? */
  600. if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
  601. status_old))
  602. return -EINVAL;
  603. len = flash->size - ofs;
  604. }
  605. /*
  606. * Need smallest pow such that:
  607. *
  608. * 1 / (2^pow) <= (len / size)
  609. *
  610. * so (assuming power-of-2 size) we do:
  611. *
  612. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  613. */
  614. pow = ilog2(flash->size) - ilog2(len);
  615. val = mask - (pow << shift);
  616. if (val & ~mask)
  617. return -EINVAL;
  618. /* Don't "lock" with no region! */
  619. if (!(val & mask))
  620. return -EINVAL;
  621. status_new = (status_old & ~mask) | val;
  622. /* Only modify protection if it will not unlock other areas */
  623. if ((status_new & mask) <= (status_old & mask))
  624. return -EINVAL;
  625. write_sr(flash, status_new);
  626. return 0;
  627. }
  628. /*
  629. * Unlock a region of the flash. See stm_lock() for more info
  630. *
  631. * Returns negative on errors, 0 on success.
  632. */
  633. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  634. {
  635. uint8_t status_old, status_new;
  636. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  637. u8 shift = ffs(mask) - 1, pow, val;
  638. int ret;
  639. ret = read_sr(flash, &status_old);
  640. if (ret < 0)
  641. return ret;
  642. /* Cannot unlock; would unlock larger region than requested */
  643. if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
  644. status_old))
  645. return -EINVAL;
  646. /*
  647. * Need largest pow such that:
  648. *
  649. * 1 / (2^pow) >= (len / size)
  650. *
  651. * so (assuming power-of-2 size) we do:
  652. *
  653. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  654. */
  655. pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
  656. if (ofs + len == flash->size) {
  657. val = 0; /* fully unlocked */
  658. } else {
  659. val = mask - (pow << shift);
  660. /* Some power-of-two sizes are not supported */
  661. if (val & ~mask)
  662. return -EINVAL;
  663. }
  664. status_new = (status_old & ~mask) | val;
  665. /* Only modify protection if it will not lock other areas */
  666. if ((status_new & mask) >= (status_old & mask))
  667. return -EINVAL;
  668. write_sr(flash, status_new);
  669. return 0;
  670. }
  671. #endif
  672. #ifdef CONFIG_SPI_FLASH_MACRONIX
  673. static int macronix_quad_enable(struct spi_flash *flash)
  674. {
  675. u8 qeb_status;
  676. int ret;
  677. ret = read_sr(flash, &qeb_status);
  678. if (ret < 0)
  679. return ret;
  680. if (qeb_status & STATUS_QEB_MXIC)
  681. return 0;
  682. ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
  683. if (ret < 0)
  684. return ret;
  685. /* read SR and check it */
  686. ret = read_sr(flash, &qeb_status);
  687. if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
  688. printf("SF: Macronix SR Quad bit not clear\n");
  689. return -EINVAL;
  690. }
  691. return ret;
  692. }
  693. #endif
  694. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  695. static int spansion_quad_enable(struct spi_flash *flash)
  696. {
  697. u8 qeb_status;
  698. int ret;
  699. ret = read_cr(flash, &qeb_status);
  700. if (ret < 0)
  701. return ret;
  702. if (qeb_status & STATUS_QEB_WINSPAN)
  703. return 0;
  704. ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
  705. if (ret < 0)
  706. return ret;
  707. /* read CR and check it */
  708. ret = read_cr(flash, &qeb_status);
  709. if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
  710. printf("SF: Spansion CR Quad bit not clear\n");
  711. return -EINVAL;
  712. }
  713. return ret;
  714. }
  715. #endif
  716. static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
  717. {
  718. int tmp;
  719. u8 id[SPI_FLASH_MAX_ID_LEN];
  720. const struct spi_flash_info *info;
  721. tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
  722. if (tmp < 0) {
  723. printf("SF: error %d reading JEDEC ID\n", tmp);
  724. return ERR_PTR(tmp);
  725. }
  726. info = spi_flash_ids;
  727. for (; info->name != NULL; info++) {
  728. if (info->id_len) {
  729. if (!memcmp(info->id, id, info->id_len))
  730. return info;
  731. }
  732. }
  733. printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  734. id[0], id[1], id[2]);
  735. return ERR_PTR(-ENODEV);
  736. }
  737. static int set_quad_mode(struct spi_flash *flash,
  738. const struct spi_flash_info *info)
  739. {
  740. switch (JEDEC_MFR(info)) {
  741. #ifdef CONFIG_SPI_FLASH_MACRONIX
  742. case SPI_FLASH_CFI_MFR_MACRONIX:
  743. return macronix_quad_enable(flash);
  744. #endif
  745. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  746. case SPI_FLASH_CFI_MFR_SPANSION:
  747. case SPI_FLASH_CFI_MFR_WINBOND:
  748. return spansion_quad_enable(flash);
  749. #endif
  750. #ifdef CONFIG_SPI_FLASH_STMICRO
  751. case SPI_FLASH_CFI_MFR_STMICRO:
  752. debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
  753. return 0;
  754. #endif
  755. default:
  756. printf("SF: Need set QEB func for %02x flash\n",
  757. JEDEC_MFR(info));
  758. return -1;
  759. }
  760. }
  761. #if CONFIG_IS_ENABLED(OF_CONTROL)
  762. int spi_flash_decode_fdt(struct spi_flash *flash)
  763. {
  764. #ifdef CONFIG_DM_SPI_FLASH
  765. fdt_addr_t addr;
  766. fdt_size_t size;
  767. addr = dev_read_addr_size(flash->dev, "memory-map", &size);
  768. if (addr == FDT_ADDR_T_NONE) {
  769. debug("%s: Cannot decode address\n", __func__);
  770. return 0;
  771. }
  772. if (flash->size > size) {
  773. debug("%s: Memory map must cover entire device\n", __func__);
  774. return -1;
  775. }
  776. flash->memory_map = map_sysmem(addr, size);
  777. #endif
  778. return 0;
  779. }
  780. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  781. int spi_flash_scan(struct spi_flash *flash)
  782. {
  783. struct spi_slave *spi = flash->spi;
  784. const struct spi_flash_info *info = NULL;
  785. int ret;
  786. info = spi_flash_read_id(flash);
  787. if (IS_ERR_OR_NULL(info))
  788. return -ENOENT;
  789. /*
  790. * Flash powers up read-only, so clear BP# bits.
  791. *
  792. * Note on some flash (like Macronix), QE (quad enable) bit is in the
  793. * same status register as BP# bits, and we need preserve its original
  794. * value during a reboot cycle as this is required by some platforms
  795. * (like Intel ICH SPI controller working under descriptor mode).
  796. */
  797. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
  798. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
  799. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
  800. u8 sr = 0;
  801. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
  802. read_sr(flash, &sr);
  803. sr &= STATUS_QEB_MXIC;
  804. }
  805. write_sr(flash, sr);
  806. }
  807. flash->name = info->name;
  808. flash->memory_map = spi->memory_map;
  809. if (info->flags & SST_WR)
  810. flash->flags |= SNOR_F_SST_WR;
  811. #ifndef CONFIG_DM_SPI_FLASH
  812. flash->write = spi_flash_cmd_write_ops;
  813. #if defined(CONFIG_SPI_FLASH_SST)
  814. if (flash->flags & SNOR_F_SST_WR) {
  815. if (spi->mode & SPI_TX_BYTE)
  816. flash->write = sst_write_bp;
  817. else
  818. flash->write = sst_write_wp;
  819. }
  820. #endif
  821. flash->erase = spi_flash_cmd_erase_ops;
  822. flash->read = spi_flash_cmd_read_ops;
  823. #endif
  824. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  825. /* NOR protection support for STmicro/Micron chips and similar */
  826. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
  827. JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
  828. flash->flash_lock = stm_lock;
  829. flash->flash_unlock = stm_unlock;
  830. flash->flash_is_locked = stm_is_locked;
  831. }
  832. #endif
  833. /* Compute the flash size */
  834. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  835. flash->page_size = info->page_size;
  836. /*
  837. * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
  838. * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
  839. * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
  840. * have 256b pages.
  841. */
  842. if (JEDEC_EXT(info) == 0x4d00) {
  843. if ((JEDEC_ID(info) != 0x0215) &&
  844. (JEDEC_ID(info) != 0x0216))
  845. flash->page_size = 512;
  846. }
  847. flash->page_size <<= flash->shift;
  848. flash->sector_size = info->sector_size << flash->shift;
  849. flash->size = flash->sector_size * info->n_sectors << flash->shift;
  850. #ifdef CONFIG_SF_DUAL_FLASH
  851. if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
  852. flash->size <<= 1;
  853. #endif
  854. #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
  855. /* Compute erase sector and command */
  856. if (info->flags & SECT_4K) {
  857. flash->erase_cmd = CMD_ERASE_4K;
  858. flash->erase_size = 4096 << flash->shift;
  859. } else
  860. #endif
  861. {
  862. flash->erase_cmd = CMD_ERASE_64K;
  863. flash->erase_size = flash->sector_size;
  864. }
  865. /* Now erase size becomes valid sector size */
  866. flash->sector_size = flash->erase_size;
  867. /* Look for read commands */
  868. flash->read_cmd = CMD_READ_ARRAY_FAST;
  869. if (spi->mode & SPI_RX_SLOW)
  870. flash->read_cmd = CMD_READ_ARRAY_SLOW;
  871. else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
  872. flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
  873. else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
  874. flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
  875. /* Look for write commands */
  876. if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
  877. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  878. else
  879. /* Go for default supported write cmd */
  880. flash->write_cmd = CMD_PAGE_PROGRAM;
  881. /* Set the quad enable bit - only for quad commands */
  882. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  883. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  884. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  885. ret = set_quad_mode(flash, info);
  886. if (ret) {
  887. debug("SF: Fail to set QEB for %02x\n",
  888. JEDEC_MFR(info));
  889. return -EINVAL;
  890. }
  891. }
  892. /* Read dummy_byte: dummy byte is determined based on the
  893. * dummy cycles of a particular command.
  894. * Fast commands - dummy_byte = dummy_cycles/8
  895. * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
  896. * For I/O commands except cmd[0] everything goes on no.of lines
  897. * based on particular command but incase of fast commands except
  898. * data all go on single line irrespective of command.
  899. */
  900. switch (flash->read_cmd) {
  901. case CMD_READ_QUAD_IO_FAST:
  902. flash->dummy_byte = 2;
  903. break;
  904. case CMD_READ_ARRAY_SLOW:
  905. flash->dummy_byte = 0;
  906. break;
  907. default:
  908. flash->dummy_byte = 1;
  909. }
  910. #ifdef CONFIG_SPI_FLASH_STMICRO
  911. if (info->flags & E_FSR)
  912. flash->flags |= SNOR_F_USE_FSR;
  913. #endif
  914. /* Configure the BAR - discover bank cmds and read current bank */
  915. #ifdef CONFIG_SPI_FLASH_BAR
  916. ret = read_bar(flash, info);
  917. if (ret < 0)
  918. return ret;
  919. #endif
  920. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  921. ret = spi_flash_decode_fdt(flash);
  922. if (ret) {
  923. debug("SF: FDT decode error\n");
  924. return -EINVAL;
  925. }
  926. #endif
  927. #ifndef CONFIG_SPL_BUILD
  928. printf("SF: Detected %s with page size ", flash->name);
  929. print_size(flash->page_size, ", erase size ");
  930. print_size(flash->erase_size, ", total ");
  931. print_size(flash->size, "");
  932. if (flash->memory_map)
  933. printf(", mapped at %p", flash->memory_map);
  934. puts("\n");
  935. #endif
  936. #ifndef CONFIG_SPI_FLASH_BAR
  937. if (((flash->dual_flash == SF_SINGLE_FLASH) &&
  938. (flash->size > SPI_FLASH_16MB_BOUN)) ||
  939. ((flash->dual_flash > SF_SINGLE_FLASH) &&
  940. (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
  941. puts("SF: Warning - Only lower 16MiB accessible,");
  942. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  943. }
  944. #endif
  945. return 0;
  946. }