spi_flash.c 26 KB

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