spi_flash.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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 <linux/sizes.h>
  19. #include <dma.h>
  20. #include "sf_internal.h"
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static void spi_flash_addr(u32 addr, u8 *cmd)
  23. {
  24. /* cmd[0] is actual command */
  25. cmd[1] = addr >> 16;
  26. cmd[2] = addr >> 8;
  27. cmd[3] = addr >> 0;
  28. }
  29. static int read_sr(struct spi_flash *flash, u8 *rs)
  30. {
  31. int ret;
  32. u8 cmd;
  33. cmd = CMD_READ_STATUS;
  34. ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
  35. if (ret < 0) {
  36. debug("SF: fail to read status register\n");
  37. return ret;
  38. }
  39. return 0;
  40. }
  41. static int read_fsr(struct spi_flash *flash, u8 *fsr)
  42. {
  43. int ret;
  44. const u8 cmd = CMD_FLAG_STATUS;
  45. ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
  46. if (ret < 0) {
  47. debug("SF: fail to read flag status register\n");
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. static int write_sr(struct spi_flash *flash, u8 ws)
  53. {
  54. u8 cmd;
  55. int ret;
  56. cmd = CMD_WRITE_STATUS;
  57. ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
  58. if (ret < 0) {
  59. debug("SF: fail to write status register\n");
  60. return ret;
  61. }
  62. return 0;
  63. }
  64. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  65. static int read_cr(struct spi_flash *flash, u8 *rc)
  66. {
  67. int ret;
  68. u8 cmd;
  69. cmd = CMD_READ_CONFIG;
  70. ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
  71. if (ret < 0) {
  72. debug("SF: fail to read config register\n");
  73. return ret;
  74. }
  75. return 0;
  76. }
  77. static int write_cr(struct spi_flash *flash, u8 wc)
  78. {
  79. u8 data[2];
  80. u8 cmd;
  81. int ret;
  82. ret = read_sr(flash, &data[0]);
  83. if (ret < 0)
  84. return ret;
  85. cmd = CMD_WRITE_STATUS;
  86. data[1] = wc;
  87. ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
  88. if (ret) {
  89. debug("SF: fail to write config register\n");
  90. return ret;
  91. }
  92. return 0;
  93. }
  94. #endif
  95. #ifdef CONFIG_SPI_FLASH_BAR
  96. /*
  97. * This "clean_bar" is necessary in a situation when one was accessing
  98. * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
  99. *
  100. * After it the BA24 bit shall be cleared to allow access to correct
  101. * memory region after SW reset (by calling "reset" command).
  102. *
  103. * Otherwise, the BA24 bit may be left set and then after reset, the
  104. * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
  105. */
  106. static int clean_bar(struct spi_flash *flash)
  107. {
  108. u8 cmd, bank_sel = 0;
  109. if (flash->bank_curr == 0)
  110. return 0;
  111. cmd = flash->bank_write_cmd;
  112. return spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
  113. }
  114. static int write_bar(struct spi_flash *flash, u32 offset)
  115. {
  116. u8 cmd, bank_sel;
  117. int ret;
  118. bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
  119. if (bank_sel == flash->bank_curr)
  120. goto bar_end;
  121. cmd = flash->bank_write_cmd;
  122. ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
  123. if (ret < 0) {
  124. debug("SF: fail to write bank register\n");
  125. return ret;
  126. }
  127. bar_end:
  128. flash->bank_curr = bank_sel;
  129. return flash->bank_curr;
  130. }
  131. static int read_bar(struct spi_flash *flash, const struct spi_flash_info *info)
  132. {
  133. u8 curr_bank = 0;
  134. int ret;
  135. if (flash->size <= SPI_FLASH_16MB_BOUN)
  136. goto bar_end;
  137. switch (JEDEC_MFR(info)) {
  138. case SPI_FLASH_CFI_MFR_SPANSION:
  139. flash->bank_read_cmd = CMD_BANKADDR_BRRD;
  140. flash->bank_write_cmd = CMD_BANKADDR_BRWR;
  141. break;
  142. default:
  143. flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
  144. flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
  145. }
  146. ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  147. &curr_bank, 1);
  148. if (ret) {
  149. debug("SF: fail to read bank addr register\n");
  150. return ret;
  151. }
  152. bar_end:
  153. flash->bank_curr = curr_bank;
  154. return 0;
  155. }
  156. #endif
  157. #ifdef CONFIG_SF_DUAL_FLASH
  158. static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
  159. {
  160. switch (flash->dual_flash) {
  161. case SF_DUAL_STACKED_FLASH:
  162. if (*addr >= (flash->size >> 1)) {
  163. *addr -= flash->size >> 1;
  164. flash->flags |= SNOR_F_USE_UPAGE;
  165. } else {
  166. flash->flags &= ~SNOR_F_USE_UPAGE;
  167. }
  168. break;
  169. case SF_DUAL_PARALLEL_FLASH:
  170. *addr >>= flash->shift;
  171. break;
  172. default:
  173. debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
  174. break;
  175. }
  176. }
  177. #endif
  178. static int spi_flash_sr_ready(struct spi_flash *flash)
  179. {
  180. u8 sr;
  181. int ret;
  182. ret = read_sr(flash, &sr);
  183. if (ret < 0)
  184. return ret;
  185. return !(sr & STATUS_WIP);
  186. }
  187. static int spi_flash_fsr_ready(struct spi_flash *flash)
  188. {
  189. u8 fsr;
  190. int ret;
  191. ret = read_fsr(flash, &fsr);
  192. if (ret < 0)
  193. return ret;
  194. return fsr & STATUS_PEC;
  195. }
  196. static int spi_flash_ready(struct spi_flash *flash)
  197. {
  198. int sr, fsr;
  199. sr = spi_flash_sr_ready(flash);
  200. if (sr < 0)
  201. return sr;
  202. fsr = 1;
  203. if (flash->flags & SNOR_F_USE_FSR) {
  204. fsr = spi_flash_fsr_ready(flash);
  205. if (fsr < 0)
  206. return fsr;
  207. }
  208. return sr && fsr;
  209. }
  210. static int spi_flash_wait_till_ready(struct spi_flash *flash,
  211. unsigned long timeout)
  212. {
  213. unsigned long timebase;
  214. int ret;
  215. timebase = get_timer(0);
  216. while (get_timer(timebase) < timeout) {
  217. ret = spi_flash_ready(flash);
  218. if (ret < 0)
  219. return ret;
  220. if (ret)
  221. return 0;
  222. }
  223. printf("SF: Timeout!\n");
  224. return -ETIMEDOUT;
  225. }
  226. int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
  227. size_t cmd_len, const void *buf, size_t buf_len)
  228. {
  229. struct spi_slave *spi = flash->spi;
  230. unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
  231. int ret;
  232. if (buf == NULL)
  233. timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
  234. ret = spi_claim_bus(spi);
  235. if (ret) {
  236. debug("SF: unable to claim SPI bus\n");
  237. return ret;
  238. }
  239. ret = spi_flash_cmd_write_enable(flash);
  240. if (ret < 0) {
  241. debug("SF: enabling write failed\n");
  242. return ret;
  243. }
  244. ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
  245. if (ret < 0) {
  246. debug("SF: write cmd failed\n");
  247. return ret;
  248. }
  249. ret = spi_flash_wait_till_ready(flash, timeout);
  250. if (ret < 0) {
  251. debug("SF: write %s timed out\n",
  252. timeout == SPI_FLASH_PROG_TIMEOUT ?
  253. "program" : "page erase");
  254. return ret;
  255. }
  256. spi_release_bus(spi);
  257. return ret;
  258. }
  259. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
  260. {
  261. u32 erase_size, erase_addr;
  262. u8 cmd[SPI_FLASH_CMD_LEN];
  263. int ret = -1;
  264. erase_size = flash->erase_size;
  265. if (offset % erase_size || len % erase_size) {
  266. printf("SF: Erase offset/length not multiple of erase size\n");
  267. return -1;
  268. }
  269. if (flash->flash_is_locked) {
  270. if (flash->flash_is_locked(flash, offset, len) > 0) {
  271. printf("offset 0x%x is protected and cannot be erased\n",
  272. offset);
  273. return -EINVAL;
  274. }
  275. }
  276. cmd[0] = flash->erase_cmd;
  277. while (len) {
  278. erase_addr = offset;
  279. #ifdef CONFIG_SF_DUAL_FLASH
  280. if (flash->dual_flash > SF_SINGLE_FLASH)
  281. spi_flash_dual(flash, &erase_addr);
  282. #endif
  283. #ifdef CONFIG_SPI_FLASH_BAR
  284. ret = write_bar(flash, erase_addr);
  285. if (ret < 0)
  286. return ret;
  287. #endif
  288. spi_flash_addr(erase_addr, cmd);
  289. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  290. cmd[2], cmd[3], erase_addr);
  291. ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
  292. if (ret < 0) {
  293. debug("SF: erase failed\n");
  294. break;
  295. }
  296. offset += erase_size;
  297. len -= erase_size;
  298. }
  299. #ifdef CONFIG_SPI_FLASH_BAR
  300. ret = clean_bar(flash);
  301. #endif
  302. return ret;
  303. }
  304. int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  305. size_t len, const void *buf)
  306. {
  307. struct spi_slave *spi = flash->spi;
  308. unsigned long byte_addr, page_size;
  309. u32 write_addr;
  310. size_t chunk_len, actual;
  311. u8 cmd[SPI_FLASH_CMD_LEN];
  312. int ret = -1;
  313. page_size = flash->page_size;
  314. if (flash->flash_is_locked) {
  315. if (flash->flash_is_locked(flash, offset, len) > 0) {
  316. printf("offset 0x%x is protected and cannot be written\n",
  317. offset);
  318. return -EINVAL;
  319. }
  320. }
  321. cmd[0] = flash->write_cmd;
  322. for (actual = 0; actual < len; actual += chunk_len) {
  323. write_addr = offset;
  324. #ifdef CONFIG_SF_DUAL_FLASH
  325. if (flash->dual_flash > SF_SINGLE_FLASH)
  326. spi_flash_dual(flash, &write_addr);
  327. #endif
  328. #ifdef CONFIG_SPI_FLASH_BAR
  329. ret = write_bar(flash, write_addr);
  330. if (ret < 0)
  331. return ret;
  332. #endif
  333. byte_addr = offset % page_size;
  334. chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
  335. if (spi->max_write_size)
  336. chunk_len = min(chunk_len,
  337. spi->max_write_size - sizeof(cmd));
  338. spi_flash_addr(write_addr, cmd);
  339. debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  340. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  341. ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
  342. buf + actual, chunk_len);
  343. if (ret < 0) {
  344. debug("SF: write failed\n");
  345. break;
  346. }
  347. offset += chunk_len;
  348. }
  349. #ifdef CONFIG_SPI_FLASH_BAR
  350. ret = clean_bar(flash);
  351. #endif
  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. if (spi->max_read_size)
  431. read_len = min(read_len, spi->max_read_size);
  432. spi_flash_addr(read_addr, cmd);
  433. ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
  434. if (ret < 0) {
  435. debug("SF: read failed\n");
  436. break;
  437. }
  438. offset += read_len;
  439. len -= read_len;
  440. data += read_len;
  441. }
  442. #ifdef CONFIG_SPI_FLASH_BAR
  443. ret = clean_bar(flash);
  444. #endif
  445. free(cmd);
  446. return ret;
  447. }
  448. #ifdef CONFIG_SPI_FLASH_SST
  449. static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
  450. {
  451. switch (ctl) {
  452. case SST26_CTL_LOCK:
  453. cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
  454. break;
  455. case SST26_CTL_UNLOCK:
  456. cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
  457. break;
  458. case SST26_CTL_CHECK:
  459. return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
  460. }
  461. return false;
  462. }
  463. /*
  464. * sst26wf016/sst26wf032/sst26wf064 have next block protection:
  465. * 4x - 8 KByte blocks - read & write protection bits - upper addresses
  466. * 1x - 32 KByte blocks - write protection bits
  467. * rest - 64 KByte blocks - write protection bits
  468. * 1x - 32 KByte blocks - write protection bits
  469. * 4x - 8 KByte blocks - read & write protection bits - lower addresses
  470. *
  471. * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
  472. * will be treated as single block.
  473. */
  474. /*
  475. * Lock, unlock or check lock status of the flash region of the flash (depending
  476. * on the lock_ctl value)
  477. */
  478. static int sst26_lock_ctl(struct spi_flash *flash, u32 ofs, size_t len, enum lock_ctl ctl)
  479. {
  480. u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
  481. bool lower_64k = false, upper_64k = false;
  482. u8 cmd, bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
  483. int ret;
  484. /* Check length and offset for 64k alignment */
  485. if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1)))
  486. return -EINVAL;
  487. if (ofs + len > flash->size)
  488. return -EINVAL;
  489. /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
  490. if (flash->size != SZ_2M &&
  491. flash->size != SZ_4M &&
  492. flash->size != SZ_8M)
  493. return -EINVAL;
  494. bpr_size = 2 + (flash->size / SZ_64K / 8);
  495. cmd = SST26_CMD_READ_BPR;
  496. ret = spi_flash_read_common(flash, &cmd, 1, bpr_buff, bpr_size);
  497. if (ret < 0) {
  498. printf("SF: fail to read block-protection register\n");
  499. return ret;
  500. }
  501. rptr_64k = min_t(u32, ofs + len , flash->size - SST26_BOUND_REG_SIZE);
  502. lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
  503. upper_64k = ((ofs + len) > (flash->size - SST26_BOUND_REG_SIZE));
  504. lower_64k = (ofs < SST26_BOUND_REG_SIZE);
  505. /* Lower bits in block-protection register are about 64k region */
  506. bpr_ptr = lptr_64k / SZ_64K - 1;
  507. /* Process 64K blocks region */
  508. while (lptr_64k < rptr_64k) {
  509. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  510. return EACCES;
  511. bpr_ptr++;
  512. lptr_64k += SZ_64K;
  513. }
  514. /* 32K and 8K region bits in BPR are after 64k region bits */
  515. bpr_ptr = (flash->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
  516. /* Process lower 32K block region */
  517. if (lower_64k)
  518. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  519. return EACCES;
  520. bpr_ptr++;
  521. /* Process upper 32K block region */
  522. if (upper_64k)
  523. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  524. return EACCES;
  525. bpr_ptr++;
  526. /* Process lower 8K block regions */
  527. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  528. if (lower_64k)
  529. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  530. return EACCES;
  531. /* In 8K area BPR has both read and write protection bits */
  532. bpr_ptr += 2;
  533. }
  534. /* Process upper 8K block regions */
  535. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  536. if (upper_64k)
  537. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  538. return EACCES;
  539. /* In 8K area BPR has both read and write protection bits */
  540. bpr_ptr += 2;
  541. }
  542. /* If we check region status we don't need to write BPR back */
  543. if (ctl == SST26_CTL_CHECK)
  544. return 0;
  545. cmd = SST26_CMD_WRITE_BPR;
  546. ret = spi_flash_write_common(flash, &cmd, 1, bpr_buff, bpr_size);
  547. if (ret < 0) {
  548. printf("SF: fail to write block-protection register\n");
  549. return ret;
  550. }
  551. return 0;
  552. }
  553. static int sst26_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  554. {
  555. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_UNLOCK);
  556. }
  557. static int sst26_lock(struct spi_flash *flash, u32 ofs, size_t len)
  558. {
  559. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_LOCK);
  560. }
  561. /*
  562. * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
  563. * and negative on errors.
  564. */
  565. static int sst26_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  566. {
  567. /*
  568. * is_locked function is used for check before reading or erasing flash
  569. * region, so offset and length might be not 64k allighned, so adjust
  570. * them to be 64k allighned as sst26_lock_ctl works only with 64k
  571. * allighned regions.
  572. */
  573. ofs -= ofs & (SZ_64K - 1);
  574. len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
  575. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_CHECK);
  576. }
  577. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  578. {
  579. struct spi_slave *spi = flash->spi;
  580. int ret;
  581. u8 cmd[4] = {
  582. CMD_SST_BP,
  583. offset >> 16,
  584. offset >> 8,
  585. offset,
  586. };
  587. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  588. spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
  589. ret = spi_flash_cmd_write_enable(flash);
  590. if (ret)
  591. return ret;
  592. ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
  593. if (ret)
  594. return ret;
  595. return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  596. }
  597. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  598. const void *buf)
  599. {
  600. struct spi_slave *spi = flash->spi;
  601. size_t actual, cmd_len;
  602. int ret;
  603. u8 cmd[4];
  604. ret = spi_claim_bus(spi);
  605. if (ret) {
  606. debug("SF: Unable to claim SPI bus\n");
  607. return ret;
  608. }
  609. /* If the data is not word aligned, write out leading single byte */
  610. actual = offset % 2;
  611. if (actual) {
  612. ret = sst_byte_write(flash, offset, buf);
  613. if (ret)
  614. goto done;
  615. }
  616. offset += actual;
  617. ret = spi_flash_cmd_write_enable(flash);
  618. if (ret)
  619. goto done;
  620. cmd_len = 4;
  621. cmd[0] = CMD_SST_AAI_WP;
  622. cmd[1] = offset >> 16;
  623. cmd[2] = offset >> 8;
  624. cmd[3] = offset;
  625. for (; actual < len - 1; actual += 2) {
  626. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  627. spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
  628. cmd[0], offset);
  629. ret = spi_flash_cmd_write(spi, cmd, cmd_len,
  630. buf + actual, 2);
  631. if (ret) {
  632. debug("SF: sst word program failed\n");
  633. break;
  634. }
  635. ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  636. if (ret)
  637. break;
  638. cmd_len = 1;
  639. offset += 2;
  640. }
  641. if (!ret)
  642. ret = spi_flash_cmd_write_disable(flash);
  643. /* If there is a single trailing byte, write it out */
  644. if (!ret && actual != len)
  645. ret = sst_byte_write(flash, offset, buf + actual);
  646. done:
  647. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  648. ret ? "failure" : "success", len, offset - actual);
  649. spi_release_bus(spi);
  650. return ret;
  651. }
  652. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  653. const void *buf)
  654. {
  655. struct spi_slave *spi = flash->spi;
  656. size_t actual;
  657. int ret;
  658. ret = spi_claim_bus(spi);
  659. if (ret) {
  660. debug("SF: Unable to claim SPI bus\n");
  661. return ret;
  662. }
  663. for (actual = 0; actual < len; actual++) {
  664. ret = sst_byte_write(flash, offset, buf + actual);
  665. if (ret) {
  666. debug("SF: sst byte program failed\n");
  667. break;
  668. }
  669. offset++;
  670. }
  671. if (!ret)
  672. ret = spi_flash_cmd_write_disable(flash);
  673. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  674. ret ? "failure" : "success", len, offset - actual);
  675. spi_release_bus(spi);
  676. return ret;
  677. }
  678. #endif
  679. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  680. static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
  681. u64 *len)
  682. {
  683. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  684. int shift = ffs(mask) - 1;
  685. int pow;
  686. if (!(sr & mask)) {
  687. /* No protection */
  688. *ofs = 0;
  689. *len = 0;
  690. } else {
  691. pow = ((sr & mask) ^ mask) >> shift;
  692. *len = flash->size >> pow;
  693. *ofs = flash->size - *len;
  694. }
  695. }
  696. /*
  697. * Return 1 if the entire region is locked, 0 otherwise
  698. */
  699. static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
  700. u8 sr)
  701. {
  702. loff_t lock_offs;
  703. u64 lock_len;
  704. stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
  705. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  706. }
  707. /*
  708. * Check if a region of the flash is (completely) locked. See stm_lock() for
  709. * more info.
  710. *
  711. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  712. * negative on errors.
  713. */
  714. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  715. {
  716. int status;
  717. u8 sr;
  718. status = read_sr(flash, &sr);
  719. if (status < 0)
  720. return status;
  721. return stm_is_locked_sr(flash, ofs, len, sr);
  722. }
  723. /*
  724. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  725. * Supports only the block protection bits BP{0,1,2} in the status register
  726. * (SR). Does not support these features found in newer SR bitfields:
  727. * - TB: top/bottom protect - only handle TB=0 (top protect)
  728. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  729. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  730. *
  731. * Sample table portion for 8MB flash (Winbond w25q64fw):
  732. *
  733. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  734. * --------------------------------------------------------------------------
  735. * X | X | 0 | 0 | 0 | NONE | NONE
  736. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  737. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  738. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  739. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  740. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  741. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  742. * X | X | 1 | 1 | 1 | 8 MB | ALL
  743. *
  744. * Returns negative on errors, 0 on success.
  745. */
  746. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
  747. {
  748. u8 status_old, status_new;
  749. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  750. u8 shift = ffs(mask) - 1, pow, val;
  751. int ret;
  752. ret = read_sr(flash, &status_old);
  753. if (ret < 0)
  754. return ret;
  755. /* SPI NOR always locks to the end */
  756. if (ofs + len != flash->size) {
  757. /* Does combined region extend to end? */
  758. if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
  759. status_old))
  760. return -EINVAL;
  761. len = flash->size - ofs;
  762. }
  763. /*
  764. * Need smallest pow such that:
  765. *
  766. * 1 / (2^pow) <= (len / size)
  767. *
  768. * so (assuming power-of-2 size) we do:
  769. *
  770. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  771. */
  772. pow = ilog2(flash->size) - ilog2(len);
  773. val = mask - (pow << shift);
  774. if (val & ~mask)
  775. return -EINVAL;
  776. /* Don't "lock" with no region! */
  777. if (!(val & mask))
  778. return -EINVAL;
  779. status_new = (status_old & ~mask) | val;
  780. /* Only modify protection if it will not unlock other areas */
  781. if ((status_new & mask) <= (status_old & mask))
  782. return -EINVAL;
  783. write_sr(flash, status_new);
  784. return 0;
  785. }
  786. /*
  787. * Unlock a region of the flash. See stm_lock() for more info
  788. *
  789. * Returns negative on errors, 0 on success.
  790. */
  791. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  792. {
  793. uint8_t status_old, status_new;
  794. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  795. u8 shift = ffs(mask) - 1, pow, val;
  796. int ret;
  797. ret = read_sr(flash, &status_old);
  798. if (ret < 0)
  799. return ret;
  800. /* Cannot unlock; would unlock larger region than requested */
  801. if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
  802. status_old))
  803. return -EINVAL;
  804. /*
  805. * Need largest pow such that:
  806. *
  807. * 1 / (2^pow) >= (len / size)
  808. *
  809. * so (assuming power-of-2 size) we do:
  810. *
  811. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  812. */
  813. pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
  814. if (ofs + len == flash->size) {
  815. val = 0; /* fully unlocked */
  816. } else {
  817. val = mask - (pow << shift);
  818. /* Some power-of-two sizes are not supported */
  819. if (val & ~mask)
  820. return -EINVAL;
  821. }
  822. status_new = (status_old & ~mask) | val;
  823. /* Only modify protection if it will not lock other areas */
  824. if ((status_new & mask) >= (status_old & mask))
  825. return -EINVAL;
  826. write_sr(flash, status_new);
  827. return 0;
  828. }
  829. #endif
  830. #ifdef CONFIG_SPI_FLASH_MACRONIX
  831. static int macronix_quad_enable(struct spi_flash *flash)
  832. {
  833. u8 qeb_status;
  834. int ret;
  835. ret = read_sr(flash, &qeb_status);
  836. if (ret < 0)
  837. return ret;
  838. if (qeb_status & STATUS_QEB_MXIC)
  839. return 0;
  840. ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
  841. if (ret < 0)
  842. return ret;
  843. /* read SR and check it */
  844. ret = read_sr(flash, &qeb_status);
  845. if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
  846. printf("SF: Macronix SR Quad bit not clear\n");
  847. return -EINVAL;
  848. }
  849. return ret;
  850. }
  851. #endif
  852. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  853. static int spansion_quad_enable(struct spi_flash *flash)
  854. {
  855. u8 qeb_status;
  856. int ret;
  857. ret = read_cr(flash, &qeb_status);
  858. if (ret < 0)
  859. return ret;
  860. if (qeb_status & STATUS_QEB_WINSPAN)
  861. return 0;
  862. ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
  863. if (ret < 0)
  864. return ret;
  865. /* read CR and check it */
  866. ret = read_cr(flash, &qeb_status);
  867. if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
  868. printf("SF: Spansion CR Quad bit not clear\n");
  869. return -EINVAL;
  870. }
  871. return ret;
  872. }
  873. #endif
  874. static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
  875. {
  876. int tmp;
  877. u8 id[SPI_FLASH_MAX_ID_LEN];
  878. const struct spi_flash_info *info;
  879. tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
  880. if (tmp < 0) {
  881. printf("SF: error %d reading JEDEC ID\n", tmp);
  882. return ERR_PTR(tmp);
  883. }
  884. info = spi_flash_ids;
  885. for (; info->name != NULL; info++) {
  886. if (info->id_len) {
  887. if (!memcmp(info->id, id, info->id_len))
  888. return info;
  889. }
  890. }
  891. printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  892. id[0], id[1], id[2]);
  893. return ERR_PTR(-ENODEV);
  894. }
  895. static int set_quad_mode(struct spi_flash *flash,
  896. const struct spi_flash_info *info)
  897. {
  898. switch (JEDEC_MFR(info)) {
  899. #ifdef CONFIG_SPI_FLASH_MACRONIX
  900. case SPI_FLASH_CFI_MFR_MACRONIX:
  901. return macronix_quad_enable(flash);
  902. #endif
  903. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  904. case SPI_FLASH_CFI_MFR_SPANSION:
  905. case SPI_FLASH_CFI_MFR_WINBOND:
  906. return spansion_quad_enable(flash);
  907. #endif
  908. #ifdef CONFIG_SPI_FLASH_STMICRO
  909. case SPI_FLASH_CFI_MFR_STMICRO:
  910. debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
  911. return 0;
  912. #endif
  913. default:
  914. printf("SF: Need set QEB func for %02x flash\n",
  915. JEDEC_MFR(info));
  916. return -1;
  917. }
  918. }
  919. #if CONFIG_IS_ENABLED(OF_CONTROL)
  920. int spi_flash_decode_fdt(struct spi_flash *flash)
  921. {
  922. #ifdef CONFIG_DM_SPI_FLASH
  923. fdt_addr_t addr;
  924. fdt_size_t size;
  925. addr = dev_read_addr_size(flash->dev, "memory-map", &size);
  926. if (addr == FDT_ADDR_T_NONE) {
  927. debug("%s: Cannot decode address\n", __func__);
  928. return 0;
  929. }
  930. if (flash->size > size) {
  931. debug("%s: Memory map must cover entire device\n", __func__);
  932. return -1;
  933. }
  934. flash->memory_map = map_sysmem(addr, size);
  935. #endif
  936. return 0;
  937. }
  938. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  939. int spi_flash_scan(struct spi_flash *flash)
  940. {
  941. struct spi_slave *spi = flash->spi;
  942. const struct spi_flash_info *info = NULL;
  943. int ret;
  944. info = spi_flash_read_id(flash);
  945. if (IS_ERR_OR_NULL(info))
  946. return -ENOENT;
  947. /*
  948. * Flash powers up read-only, so clear BP# bits.
  949. *
  950. * Note on some flash (like Macronix), QE (quad enable) bit is in the
  951. * same status register as BP# bits, and we need preserve its original
  952. * value during a reboot cycle as this is required by some platforms
  953. * (like Intel ICH SPI controller working under descriptor mode).
  954. */
  955. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
  956. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
  957. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
  958. u8 sr = 0;
  959. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
  960. read_sr(flash, &sr);
  961. sr &= STATUS_QEB_MXIC;
  962. }
  963. write_sr(flash, sr);
  964. }
  965. flash->name = info->name;
  966. flash->memory_map = spi->memory_map;
  967. if (info->flags & SST_WR)
  968. flash->flags |= SNOR_F_SST_WR;
  969. #ifndef CONFIG_DM_SPI_FLASH
  970. flash->write = spi_flash_cmd_write_ops;
  971. #if defined(CONFIG_SPI_FLASH_SST)
  972. if (flash->flags & SNOR_F_SST_WR) {
  973. if (spi->mode & SPI_TX_BYTE)
  974. flash->write = sst_write_bp;
  975. else
  976. flash->write = sst_write_wp;
  977. }
  978. #endif
  979. flash->erase = spi_flash_cmd_erase_ops;
  980. flash->read = spi_flash_cmd_read_ops;
  981. #endif
  982. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  983. /* NOR protection support for STmicro/Micron chips and similar */
  984. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
  985. JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
  986. flash->flash_lock = stm_lock;
  987. flash->flash_unlock = stm_unlock;
  988. flash->flash_is_locked = stm_is_locked;
  989. }
  990. #endif
  991. /* sst26wf series block protection implementation differs from other series */
  992. #if defined(CONFIG_SPI_FLASH_SST)
  993. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST && info->id[1] == 0x26) {
  994. flash->flash_lock = sst26_lock;
  995. flash->flash_unlock = sst26_unlock;
  996. flash->flash_is_locked = sst26_is_locked;
  997. }
  998. #endif
  999. /* Compute the flash size */
  1000. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  1001. flash->page_size = info->page_size;
  1002. /*
  1003. * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
  1004. * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
  1005. * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
  1006. * have 256b pages.
  1007. */
  1008. if (JEDEC_EXT(info) == 0x4d00) {
  1009. if ((JEDEC_ID(info) != 0x0215) &&
  1010. (JEDEC_ID(info) != 0x0216))
  1011. flash->page_size = 512;
  1012. }
  1013. flash->page_size <<= flash->shift;
  1014. flash->sector_size = info->sector_size << flash->shift;
  1015. flash->size = flash->sector_size * info->n_sectors << flash->shift;
  1016. #ifdef CONFIG_SF_DUAL_FLASH
  1017. if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
  1018. flash->size <<= 1;
  1019. #endif
  1020. #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
  1021. /* Compute erase sector and command */
  1022. if (info->flags & SECT_4K) {
  1023. flash->erase_cmd = CMD_ERASE_4K;
  1024. flash->erase_size = 4096 << flash->shift;
  1025. } else
  1026. #endif
  1027. {
  1028. flash->erase_cmd = CMD_ERASE_64K;
  1029. flash->erase_size = flash->sector_size;
  1030. }
  1031. /* Now erase size becomes valid sector size */
  1032. flash->sector_size = flash->erase_size;
  1033. /* Look for read commands */
  1034. flash->read_cmd = CMD_READ_ARRAY_FAST;
  1035. if (spi->mode & SPI_RX_SLOW)
  1036. flash->read_cmd = CMD_READ_ARRAY_SLOW;
  1037. else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
  1038. flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
  1039. else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
  1040. flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
  1041. /* Look for write commands */
  1042. if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
  1043. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  1044. else
  1045. /* Go for default supported write cmd */
  1046. flash->write_cmd = CMD_PAGE_PROGRAM;
  1047. /* Set the quad enable bit - only for quad commands */
  1048. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  1049. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  1050. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  1051. ret = set_quad_mode(flash, info);
  1052. if (ret) {
  1053. debug("SF: Fail to set QEB for %02x\n",
  1054. JEDEC_MFR(info));
  1055. return -EINVAL;
  1056. }
  1057. }
  1058. /* Read dummy_byte: dummy byte is determined based on the
  1059. * dummy cycles of a particular command.
  1060. * Fast commands - dummy_byte = dummy_cycles/8
  1061. * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
  1062. * For I/O commands except cmd[0] everything goes on no.of lines
  1063. * based on particular command but incase of fast commands except
  1064. * data all go on single line irrespective of command.
  1065. */
  1066. switch (flash->read_cmd) {
  1067. case CMD_READ_QUAD_IO_FAST:
  1068. flash->dummy_byte = 2;
  1069. break;
  1070. case CMD_READ_ARRAY_SLOW:
  1071. flash->dummy_byte = 0;
  1072. break;
  1073. default:
  1074. flash->dummy_byte = 1;
  1075. }
  1076. #ifdef CONFIG_SPI_FLASH_STMICRO
  1077. if (info->flags & E_FSR)
  1078. flash->flags |= SNOR_F_USE_FSR;
  1079. #endif
  1080. /* Configure the BAR - discover bank cmds and read current bank */
  1081. #ifdef CONFIG_SPI_FLASH_BAR
  1082. ret = read_bar(flash, info);
  1083. if (ret < 0)
  1084. return ret;
  1085. #endif
  1086. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  1087. ret = spi_flash_decode_fdt(flash);
  1088. if (ret) {
  1089. debug("SF: FDT decode error\n");
  1090. return -EINVAL;
  1091. }
  1092. #endif
  1093. #ifndef CONFIG_SPL_BUILD
  1094. printf("SF: Detected %s with page size ", flash->name);
  1095. print_size(flash->page_size, ", erase size ");
  1096. print_size(flash->erase_size, ", total ");
  1097. print_size(flash->size, "");
  1098. if (flash->memory_map)
  1099. printf(", mapped at %p", flash->memory_map);
  1100. puts("\n");
  1101. #endif
  1102. #ifndef CONFIG_SPI_FLASH_BAR
  1103. if (((flash->dual_flash == SF_SINGLE_FLASH) &&
  1104. (flash->size > SPI_FLASH_16MB_BOUN)) ||
  1105. ((flash->dual_flash > SF_SINGLE_FLASH) &&
  1106. (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
  1107. puts("SF: Warning - Only lower 16MiB accessible,");
  1108. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  1109. }
  1110. #endif
  1111. return 0;
  1112. }