spi_flash.c 31 KB

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