spi_flash.c 26 KB

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