spi_flash.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SPI Flash Core
  4. *
  5. * Copyright (C) 2015 Jagan Teki <jteki@openedev.com>
  6. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  7. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  8. * Copyright (C) 2008 Atmel Corporation
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <malloc.h>
  13. #include <mapmem.h>
  14. #include <spi.h>
  15. #include <spi_flash.h>
  16. #include <linux/log2.h>
  17. #include <linux/sizes.h>
  18. #include <dma.h>
  19. #include "sf_internal.h"
  20. static void spi_flash_addr(u32 addr, u8 *cmd)
  21. {
  22. /* cmd[0] is actual command */
  23. cmd[1] = addr >> 16;
  24. cmd[2] = addr >> 8;
  25. cmd[3] = addr >> 0;
  26. }
  27. static int read_sr(struct spi_flash *flash, u8 *rs)
  28. {
  29. int ret;
  30. u8 cmd;
  31. cmd = CMD_READ_STATUS;
  32. ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
  33. if (ret < 0) {
  34. debug("SF: fail to read status register\n");
  35. return ret;
  36. }
  37. return 0;
  38. }
  39. static int read_fsr(struct spi_flash *flash, u8 *fsr)
  40. {
  41. int ret;
  42. const u8 cmd = CMD_FLAG_STATUS;
  43. ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
  44. if (ret < 0) {
  45. debug("SF: fail to read flag status register\n");
  46. return ret;
  47. }
  48. return 0;
  49. }
  50. static int write_sr(struct spi_flash *flash, u8 ws)
  51. {
  52. u8 cmd;
  53. int ret;
  54. cmd = CMD_WRITE_STATUS;
  55. ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
  56. if (ret < 0) {
  57. debug("SF: fail to write status register\n");
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  63. static int read_cr(struct spi_flash *flash, u8 *rc)
  64. {
  65. int ret;
  66. u8 cmd;
  67. cmd = CMD_READ_CONFIG;
  68. ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
  69. if (ret < 0) {
  70. debug("SF: fail to read config register\n");
  71. return ret;
  72. }
  73. return 0;
  74. }
  75. static int write_cr(struct spi_flash *flash, u8 wc)
  76. {
  77. u8 data[2];
  78. u8 cmd;
  79. int ret;
  80. ret = read_sr(flash, &data[0]);
  81. if (ret < 0)
  82. return ret;
  83. cmd = CMD_WRITE_STATUS;
  84. data[1] = wc;
  85. ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
  86. if (ret) {
  87. debug("SF: fail to write config register\n");
  88. return ret;
  89. }
  90. return 0;
  91. }
  92. #endif
  93. #ifdef CONFIG_SPI_FLASH_BAR
  94. /*
  95. * This "clean_bar" is necessary in a situation when one was accessing
  96. * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
  97. *
  98. * After it the BA24 bit shall be cleared to allow access to correct
  99. * memory region after SW reset (by calling "reset" command).
  100. *
  101. * Otherwise, the BA24 bit may be left set and then after reset, the
  102. * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
  103. */
  104. static int clean_bar(struct spi_flash *flash)
  105. {
  106. u8 cmd, bank_sel = 0;
  107. if (flash->bank_curr == 0)
  108. return 0;
  109. cmd = flash->bank_write_cmd;
  110. flash->bank_curr = 0;
  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. printf("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 cmdsz;
  388. u32 remain_len, read_len, read_addr;
  389. int bank_sel = 0;
  390. int ret = 0;
  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 log_ret(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. u8 cmd[cmdsz];
  406. cmd[0] = flash->read_cmd;
  407. while (len) {
  408. read_addr = offset;
  409. #ifdef CONFIG_SF_DUAL_FLASH
  410. if (flash->dual_flash > SF_SINGLE_FLASH)
  411. spi_flash_dual(flash, &read_addr);
  412. #endif
  413. #ifdef CONFIG_SPI_FLASH_BAR
  414. ret = write_bar(flash, read_addr);
  415. if (ret < 0)
  416. return log_ret(ret);
  417. bank_sel = flash->bank_curr;
  418. #endif
  419. remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
  420. (bank_sel + 1)) - offset;
  421. if (len < remain_len)
  422. read_len = len;
  423. else
  424. read_len = remain_len;
  425. if (spi->max_read_size)
  426. read_len = min(read_len, spi->max_read_size);
  427. spi_flash_addr(read_addr, cmd);
  428. ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
  429. if (ret < 0) {
  430. debug("SF: read failed\n");
  431. break;
  432. }
  433. offset += read_len;
  434. len -= read_len;
  435. data += read_len;
  436. }
  437. #ifdef CONFIG_SPI_FLASH_BAR
  438. ret = clean_bar(flash);
  439. #endif
  440. return log_ret(ret);
  441. }
  442. #ifdef CONFIG_SPI_FLASH_SST
  443. static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
  444. {
  445. switch (ctl) {
  446. case SST26_CTL_LOCK:
  447. cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
  448. break;
  449. case SST26_CTL_UNLOCK:
  450. cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
  451. break;
  452. case SST26_CTL_CHECK:
  453. return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
  454. }
  455. return false;
  456. }
  457. /*
  458. * sst26wf016/sst26wf032/sst26wf064 have next block protection:
  459. * 4x - 8 KByte blocks - read & write protection bits - upper addresses
  460. * 1x - 32 KByte blocks - write protection bits
  461. * rest - 64 KByte blocks - write protection bits
  462. * 1x - 32 KByte blocks - write protection bits
  463. * 4x - 8 KByte blocks - read & write protection bits - lower addresses
  464. *
  465. * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
  466. * will be treated as single block.
  467. */
  468. /*
  469. * Lock, unlock or check lock status of the flash region of the flash (depending
  470. * on the lock_ctl value)
  471. */
  472. static int sst26_lock_ctl(struct spi_flash *flash, u32 ofs, size_t len, enum lock_ctl ctl)
  473. {
  474. u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
  475. bool lower_64k = false, upper_64k = false;
  476. u8 cmd, bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
  477. int ret;
  478. /* Check length and offset for 64k alignment */
  479. if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1)))
  480. return -EINVAL;
  481. if (ofs + len > flash->size)
  482. return -EINVAL;
  483. /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
  484. if (flash->size != SZ_2M &&
  485. flash->size != SZ_4M &&
  486. flash->size != SZ_8M)
  487. return -EINVAL;
  488. bpr_size = 2 + (flash->size / SZ_64K / 8);
  489. cmd = SST26_CMD_READ_BPR;
  490. ret = spi_flash_read_common(flash, &cmd, 1, bpr_buff, bpr_size);
  491. if (ret < 0) {
  492. printf("SF: fail to read block-protection register\n");
  493. return ret;
  494. }
  495. rptr_64k = min_t(u32, ofs + len , flash->size - SST26_BOUND_REG_SIZE);
  496. lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
  497. upper_64k = ((ofs + len) > (flash->size - SST26_BOUND_REG_SIZE));
  498. lower_64k = (ofs < SST26_BOUND_REG_SIZE);
  499. /* Lower bits in block-protection register are about 64k region */
  500. bpr_ptr = lptr_64k / SZ_64K - 1;
  501. /* Process 64K blocks region */
  502. while (lptr_64k < rptr_64k) {
  503. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  504. return EACCES;
  505. bpr_ptr++;
  506. lptr_64k += SZ_64K;
  507. }
  508. /* 32K and 8K region bits in BPR are after 64k region bits */
  509. bpr_ptr = (flash->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
  510. /* Process lower 32K block region */
  511. if (lower_64k)
  512. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  513. return EACCES;
  514. bpr_ptr++;
  515. /* Process upper 32K block region */
  516. if (upper_64k)
  517. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  518. return EACCES;
  519. bpr_ptr++;
  520. /* Process lower 8K block regions */
  521. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  522. if (lower_64k)
  523. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  524. return EACCES;
  525. /* In 8K area BPR has both read and write protection bits */
  526. bpr_ptr += 2;
  527. }
  528. /* Process upper 8K block regions */
  529. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  530. if (upper_64k)
  531. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  532. return EACCES;
  533. /* In 8K area BPR has both read and write protection bits */
  534. bpr_ptr += 2;
  535. }
  536. /* If we check region status we don't need to write BPR back */
  537. if (ctl == SST26_CTL_CHECK)
  538. return 0;
  539. cmd = SST26_CMD_WRITE_BPR;
  540. ret = spi_flash_write_common(flash, &cmd, 1, bpr_buff, bpr_size);
  541. if (ret < 0) {
  542. printf("SF: fail to write block-protection register\n");
  543. return ret;
  544. }
  545. return 0;
  546. }
  547. static int sst26_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  548. {
  549. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_UNLOCK);
  550. }
  551. static int sst26_lock(struct spi_flash *flash, u32 ofs, size_t len)
  552. {
  553. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_LOCK);
  554. }
  555. /*
  556. * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
  557. * and negative on errors.
  558. */
  559. static int sst26_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  560. {
  561. /*
  562. * is_locked function is used for check before reading or erasing flash
  563. * region, so offset and length might be not 64k allighned, so adjust
  564. * them to be 64k allighned as sst26_lock_ctl works only with 64k
  565. * allighned regions.
  566. */
  567. ofs -= ofs & (SZ_64K - 1);
  568. len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
  569. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_CHECK);
  570. }
  571. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  572. {
  573. struct spi_slave *spi = flash->spi;
  574. int ret;
  575. u8 cmd[4] = {
  576. CMD_SST_BP,
  577. offset >> 16,
  578. offset >> 8,
  579. offset,
  580. };
  581. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  582. spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
  583. ret = spi_flash_cmd_write_enable(flash);
  584. if (ret)
  585. return ret;
  586. ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
  587. if (ret)
  588. return ret;
  589. return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  590. }
  591. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  592. const void *buf)
  593. {
  594. struct spi_slave *spi = flash->spi;
  595. size_t actual, cmd_len;
  596. int ret;
  597. u8 cmd[4];
  598. ret = spi_claim_bus(spi);
  599. if (ret) {
  600. debug("SF: Unable to claim SPI bus\n");
  601. return ret;
  602. }
  603. /* If the data is not word aligned, write out leading single byte */
  604. actual = offset % 2;
  605. if (actual) {
  606. ret = sst_byte_write(flash, offset, buf);
  607. if (ret)
  608. goto done;
  609. }
  610. offset += actual;
  611. ret = spi_flash_cmd_write_enable(flash);
  612. if (ret)
  613. goto done;
  614. cmd_len = 4;
  615. cmd[0] = CMD_SST_AAI_WP;
  616. cmd[1] = offset >> 16;
  617. cmd[2] = offset >> 8;
  618. cmd[3] = offset;
  619. for (; actual < len - 1; actual += 2) {
  620. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  621. spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
  622. cmd[0], offset);
  623. ret = spi_flash_cmd_write(spi, cmd, cmd_len,
  624. buf + actual, 2);
  625. if (ret) {
  626. debug("SF: sst word program failed\n");
  627. break;
  628. }
  629. ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  630. if (ret)
  631. break;
  632. cmd_len = 1;
  633. offset += 2;
  634. }
  635. if (!ret)
  636. ret = spi_flash_cmd_write_disable(flash);
  637. /* If there is a single trailing byte, write it out */
  638. if (!ret && actual != len)
  639. ret = sst_byte_write(flash, offset, buf + actual);
  640. done:
  641. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  642. ret ? "failure" : "success", len, offset - actual);
  643. spi_release_bus(spi);
  644. return ret;
  645. }
  646. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  647. const void *buf)
  648. {
  649. struct spi_slave *spi = flash->spi;
  650. size_t actual;
  651. int ret;
  652. ret = spi_claim_bus(spi);
  653. if (ret) {
  654. debug("SF: Unable to claim SPI bus\n");
  655. return ret;
  656. }
  657. for (actual = 0; actual < len; actual++) {
  658. ret = sst_byte_write(flash, offset, buf + actual);
  659. if (ret) {
  660. debug("SF: sst byte program failed\n");
  661. break;
  662. }
  663. offset++;
  664. }
  665. if (!ret)
  666. ret = spi_flash_cmd_write_disable(flash);
  667. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  668. ret ? "failure" : "success", len, offset - actual);
  669. spi_release_bus(spi);
  670. return ret;
  671. }
  672. #endif
  673. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  674. static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
  675. u64 *len)
  676. {
  677. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  678. int shift = ffs(mask) - 1;
  679. int pow;
  680. if (!(sr & mask)) {
  681. /* No protection */
  682. *ofs = 0;
  683. *len = 0;
  684. } else {
  685. pow = ((sr & mask) ^ mask) >> shift;
  686. *len = flash->size >> pow;
  687. *ofs = flash->size - *len;
  688. }
  689. }
  690. /*
  691. * Return 1 if the entire region is locked, 0 otherwise
  692. */
  693. static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
  694. u8 sr)
  695. {
  696. loff_t lock_offs;
  697. u64 lock_len;
  698. stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
  699. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  700. }
  701. /*
  702. * Check if a region of the flash is (completely) locked. See stm_lock() for
  703. * more info.
  704. *
  705. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  706. * negative on errors.
  707. */
  708. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  709. {
  710. int status;
  711. u8 sr;
  712. status = read_sr(flash, &sr);
  713. if (status < 0)
  714. return status;
  715. return stm_is_locked_sr(flash, ofs, len, sr);
  716. }
  717. /*
  718. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  719. * Supports only the block protection bits BP{0,1,2} in the status register
  720. * (SR). Does not support these features found in newer SR bitfields:
  721. * - TB: top/bottom protect - only handle TB=0 (top protect)
  722. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  723. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  724. *
  725. * Sample table portion for 8MB flash (Winbond w25q64fw):
  726. *
  727. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  728. * --------------------------------------------------------------------------
  729. * X | X | 0 | 0 | 0 | NONE | NONE
  730. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  731. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  732. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  733. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  734. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  735. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  736. * X | X | 1 | 1 | 1 | 8 MB | ALL
  737. *
  738. * Returns negative on errors, 0 on success.
  739. */
  740. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
  741. {
  742. u8 status_old, status_new;
  743. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  744. u8 shift = ffs(mask) - 1, pow, val;
  745. int ret;
  746. ret = read_sr(flash, &status_old);
  747. if (ret < 0)
  748. return ret;
  749. /* SPI NOR always locks to the end */
  750. if (ofs + len != flash->size) {
  751. /* Does combined region extend to end? */
  752. if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
  753. status_old))
  754. return -EINVAL;
  755. len = flash->size - ofs;
  756. }
  757. /*
  758. * Need smallest pow such that:
  759. *
  760. * 1 / (2^pow) <= (len / size)
  761. *
  762. * so (assuming power-of-2 size) we do:
  763. *
  764. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  765. */
  766. pow = ilog2(flash->size) - ilog2(len);
  767. val = mask - (pow << shift);
  768. if (val & ~mask)
  769. return -EINVAL;
  770. /* Don't "lock" with no region! */
  771. if (!(val & mask))
  772. return -EINVAL;
  773. status_new = (status_old & ~mask) | val;
  774. /* Only modify protection if it will not unlock other areas */
  775. if ((status_new & mask) <= (status_old & mask))
  776. return -EINVAL;
  777. write_sr(flash, status_new);
  778. return 0;
  779. }
  780. /*
  781. * Unlock a region of the flash. See stm_lock() for more info
  782. *
  783. * Returns negative on errors, 0 on success.
  784. */
  785. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  786. {
  787. uint8_t status_old, status_new;
  788. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  789. u8 shift = ffs(mask) - 1, pow, val;
  790. int ret;
  791. ret = read_sr(flash, &status_old);
  792. if (ret < 0)
  793. return ret;
  794. /* Cannot unlock; would unlock larger region than requested */
  795. if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
  796. status_old))
  797. return -EINVAL;
  798. /*
  799. * Need largest pow such that:
  800. *
  801. * 1 / (2^pow) >= (len / size)
  802. *
  803. * so (assuming power-of-2 size) we do:
  804. *
  805. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  806. */
  807. pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
  808. if (ofs + len == flash->size) {
  809. val = 0; /* fully unlocked */
  810. } else {
  811. val = mask - (pow << shift);
  812. /* Some power-of-two sizes are not supported */
  813. if (val & ~mask)
  814. return -EINVAL;
  815. }
  816. status_new = (status_old & ~mask) | val;
  817. /* Only modify protection if it will not lock other areas */
  818. if ((status_new & mask) >= (status_old & mask))
  819. return -EINVAL;
  820. write_sr(flash, status_new);
  821. return 0;
  822. }
  823. #endif
  824. #ifdef CONFIG_SPI_FLASH_MACRONIX
  825. static int macronix_quad_enable(struct spi_flash *flash)
  826. {
  827. u8 qeb_status;
  828. int ret;
  829. ret = read_sr(flash, &qeb_status);
  830. if (ret < 0)
  831. return ret;
  832. if (qeb_status & STATUS_QEB_MXIC)
  833. return 0;
  834. ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
  835. if (ret < 0)
  836. return ret;
  837. /* read SR and check it */
  838. ret = read_sr(flash, &qeb_status);
  839. if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
  840. printf("SF: Macronix SR Quad bit not clear\n");
  841. return -EINVAL;
  842. }
  843. return ret;
  844. }
  845. #endif
  846. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  847. static int spansion_quad_enable(struct spi_flash *flash)
  848. {
  849. u8 qeb_status;
  850. int ret;
  851. ret = read_cr(flash, &qeb_status);
  852. if (ret < 0)
  853. return ret;
  854. if (qeb_status & STATUS_QEB_WINSPAN)
  855. return 0;
  856. ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
  857. if (ret < 0)
  858. return ret;
  859. /* read CR and check it */
  860. ret = read_cr(flash, &qeb_status);
  861. if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
  862. printf("SF: Spansion CR Quad bit not clear\n");
  863. return -EINVAL;
  864. }
  865. return ret;
  866. }
  867. #endif
  868. static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
  869. {
  870. int tmp;
  871. u8 id[SPI_FLASH_MAX_ID_LEN];
  872. const struct spi_flash_info *info;
  873. tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
  874. if (tmp < 0) {
  875. printf("SF: error %d reading JEDEC ID\n", tmp);
  876. return ERR_PTR(tmp);
  877. }
  878. info = spi_flash_ids;
  879. for (; info->name != NULL; info++) {
  880. if (info->id_len) {
  881. if (!memcmp(info->id, id, info->id_len))
  882. return info;
  883. }
  884. }
  885. printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  886. id[0], id[1], id[2]);
  887. return ERR_PTR(-ENODEV);
  888. }
  889. static int set_quad_mode(struct spi_flash *flash,
  890. const struct spi_flash_info *info)
  891. {
  892. switch (JEDEC_MFR(info)) {
  893. #ifdef CONFIG_SPI_FLASH_MACRONIX
  894. case SPI_FLASH_CFI_MFR_MACRONIX:
  895. return macronix_quad_enable(flash);
  896. #endif
  897. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  898. case SPI_FLASH_CFI_MFR_SPANSION:
  899. case SPI_FLASH_CFI_MFR_WINBOND:
  900. return spansion_quad_enable(flash);
  901. #endif
  902. #ifdef CONFIG_SPI_FLASH_STMICRO
  903. case SPI_FLASH_CFI_MFR_STMICRO:
  904. debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
  905. return 0;
  906. #endif
  907. default:
  908. printf("SF: Need set QEB func for %02x flash\n",
  909. JEDEC_MFR(info));
  910. return -1;
  911. }
  912. }
  913. #if CONFIG_IS_ENABLED(OF_CONTROL)
  914. int spi_flash_decode_fdt(struct spi_flash *flash)
  915. {
  916. #ifdef CONFIG_DM_SPI_FLASH
  917. fdt_addr_t addr;
  918. fdt_size_t size;
  919. addr = dev_read_addr_size(flash->dev, "memory-map", &size);
  920. if (addr == FDT_ADDR_T_NONE) {
  921. debug("%s: Cannot decode address\n", __func__);
  922. return 0;
  923. }
  924. if (flash->size > size) {
  925. debug("%s: Memory map must cover entire device\n", __func__);
  926. return -1;
  927. }
  928. flash->memory_map = map_sysmem(addr, size);
  929. #endif
  930. return 0;
  931. }
  932. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  933. int spi_flash_scan(struct spi_flash *flash)
  934. {
  935. struct spi_slave *spi = flash->spi;
  936. const struct spi_flash_info *info = NULL;
  937. int ret;
  938. info = spi_flash_read_id(flash);
  939. if (IS_ERR_OR_NULL(info))
  940. return -ENOENT;
  941. /*
  942. * Flash powers up read-only, so clear BP# bits.
  943. *
  944. * Note on some flash (like Macronix), QE (quad enable) bit is in the
  945. * same status register as BP# bits, and we need preserve its original
  946. * value during a reboot cycle as this is required by some platforms
  947. * (like Intel ICH SPI controller working under descriptor mode).
  948. */
  949. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
  950. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
  951. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
  952. u8 sr = 0;
  953. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
  954. read_sr(flash, &sr);
  955. sr &= STATUS_QEB_MXIC;
  956. }
  957. write_sr(flash, sr);
  958. }
  959. flash->name = info->name;
  960. flash->memory_map = spi->memory_map;
  961. if (info->flags & SST_WR)
  962. flash->flags |= SNOR_F_SST_WR;
  963. #ifndef CONFIG_DM_SPI_FLASH
  964. flash->write = spi_flash_cmd_write_ops;
  965. #if defined(CONFIG_SPI_FLASH_SST)
  966. if (flash->flags & SNOR_F_SST_WR) {
  967. if (spi->mode & SPI_TX_BYTE)
  968. flash->write = sst_write_bp;
  969. else
  970. flash->write = sst_write_wp;
  971. }
  972. #endif
  973. flash->erase = spi_flash_cmd_erase_ops;
  974. flash->read = spi_flash_cmd_read_ops;
  975. #endif
  976. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  977. /* NOR protection support for STmicro/Micron chips and similar */
  978. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
  979. JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
  980. flash->flash_lock = stm_lock;
  981. flash->flash_unlock = stm_unlock;
  982. flash->flash_is_locked = stm_is_locked;
  983. }
  984. #endif
  985. /* sst26wf series block protection implementation differs from other series */
  986. #if defined(CONFIG_SPI_FLASH_SST)
  987. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST && info->id[1] == 0x26) {
  988. flash->flash_lock = sst26_lock;
  989. flash->flash_unlock = sst26_unlock;
  990. flash->flash_is_locked = sst26_is_locked;
  991. }
  992. #endif
  993. /* Compute the flash size */
  994. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  995. flash->page_size = info->page_size;
  996. /*
  997. * The Spansion S25FS512S, S25FL032P and S25FL064P have 256b pages,
  998. * yet use the 0x4d00 Extended JEDEC code. The rest of the Spansion
  999. * flashes with the 0x4d00 Extended JEDEC code have 512b pages.
  1000. * All of the others have 256b pages.
  1001. */
  1002. if (JEDEC_EXT(info) == 0x4d00) {
  1003. if ((JEDEC_ID(info) != 0x0215) &&
  1004. (JEDEC_ID(info) != 0x0216) &&
  1005. (JEDEC_ID(info) != 0x0220))
  1006. flash->page_size = 512;
  1007. }
  1008. flash->page_size <<= flash->shift;
  1009. flash->sector_size = info->sector_size << flash->shift;
  1010. flash->size = flash->sector_size * info->n_sectors << flash->shift;
  1011. #ifdef CONFIG_SF_DUAL_FLASH
  1012. if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
  1013. flash->size <<= 1;
  1014. #endif
  1015. #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
  1016. /* Compute erase sector and command */
  1017. if (info->flags & SECT_4K) {
  1018. flash->erase_cmd = CMD_ERASE_4K;
  1019. flash->erase_size = 4096 << flash->shift;
  1020. } else
  1021. #endif
  1022. {
  1023. flash->erase_cmd = CMD_ERASE_64K;
  1024. flash->erase_size = flash->sector_size;
  1025. }
  1026. /* Now erase size becomes valid sector size */
  1027. flash->sector_size = flash->erase_size;
  1028. /* Look for read commands */
  1029. flash->read_cmd = CMD_READ_ARRAY_FAST;
  1030. if (spi->mode & SPI_RX_SLOW)
  1031. flash->read_cmd = CMD_READ_ARRAY_SLOW;
  1032. else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
  1033. flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
  1034. else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
  1035. flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
  1036. /* Look for write commands */
  1037. if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
  1038. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  1039. else
  1040. /* Go for default supported write cmd */
  1041. flash->write_cmd = CMD_PAGE_PROGRAM;
  1042. /* Set the quad enable bit - only for quad commands */
  1043. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  1044. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  1045. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  1046. ret = set_quad_mode(flash, info);
  1047. if (ret) {
  1048. debug("SF: Fail to set QEB for %02x\n",
  1049. JEDEC_MFR(info));
  1050. return -EINVAL;
  1051. }
  1052. }
  1053. /* Read dummy_byte: dummy byte is determined based on the
  1054. * dummy cycles of a particular command.
  1055. * Fast commands - dummy_byte = dummy_cycles/8
  1056. * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
  1057. * For I/O commands except cmd[0] everything goes on no.of lines
  1058. * based on particular command but incase of fast commands except
  1059. * data all go on single line irrespective of command.
  1060. */
  1061. switch (flash->read_cmd) {
  1062. case CMD_READ_QUAD_IO_FAST:
  1063. flash->dummy_byte = 2;
  1064. break;
  1065. case CMD_READ_ARRAY_SLOW:
  1066. flash->dummy_byte = 0;
  1067. break;
  1068. default:
  1069. flash->dummy_byte = 1;
  1070. }
  1071. #ifdef CONFIG_SPI_FLASH_STMICRO
  1072. if (info->flags & E_FSR)
  1073. flash->flags |= SNOR_F_USE_FSR;
  1074. #endif
  1075. /* Configure the BAR - discover bank cmds and read current bank */
  1076. #ifdef CONFIG_SPI_FLASH_BAR
  1077. ret = read_bar(flash, info);
  1078. if (ret < 0)
  1079. return ret;
  1080. #endif
  1081. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  1082. ret = spi_flash_decode_fdt(flash);
  1083. if (ret) {
  1084. debug("SF: FDT decode error\n");
  1085. return -EINVAL;
  1086. }
  1087. #endif
  1088. #ifndef CONFIG_SPL_BUILD
  1089. printf("SF: Detected %s with page size ", flash->name);
  1090. print_size(flash->page_size, ", erase size ");
  1091. print_size(flash->erase_size, ", total ");
  1092. print_size(flash->size, "");
  1093. if (flash->memory_map)
  1094. printf(", mapped at %p", flash->memory_map);
  1095. puts("\n");
  1096. #endif
  1097. #ifndef CONFIG_SPI_FLASH_BAR
  1098. if (((flash->dual_flash == SF_SINGLE_FLASH) &&
  1099. (flash->size > SPI_FLASH_16MB_BOUN)) ||
  1100. ((flash->dual_flash > SF_SINGLE_FLASH) &&
  1101. (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
  1102. puts("SF: Warning - Only lower 16MiB accessible,");
  1103. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  1104. }
  1105. #endif
  1106. return 0;
  1107. }