spi_flash.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 *cmd, cmdsz;
  388. u32 remain_len, read_len, read_addr;
  389. int bank_sel = 0;
  390. int ret = -1;
  391. /* Handle memory-mapped SPI */
  392. if (flash->memory_map) {
  393. ret = spi_claim_bus(spi);
  394. if (ret) {
  395. debug("SF: unable to claim SPI bus\n");
  396. return ret;
  397. }
  398. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP);
  399. spi_flash_copy_mmap(data, flash->memory_map + offset, len);
  400. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
  401. spi_release_bus(spi);
  402. return 0;
  403. }
  404. cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
  405. cmd = calloc(1, cmdsz);
  406. if (!cmd) {
  407. debug("SF: Failed to allocate cmd\n");
  408. return -ENOMEM;
  409. }
  410. cmd[0] = flash->read_cmd;
  411. while (len) {
  412. read_addr = offset;
  413. #ifdef CONFIG_SF_DUAL_FLASH
  414. if (flash->dual_flash > SF_SINGLE_FLASH)
  415. spi_flash_dual(flash, &read_addr);
  416. #endif
  417. #ifdef CONFIG_SPI_FLASH_BAR
  418. ret = write_bar(flash, read_addr);
  419. if (ret < 0)
  420. return ret;
  421. bank_sel = flash->bank_curr;
  422. #endif
  423. remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
  424. (bank_sel + 1)) - offset;
  425. if (len < remain_len)
  426. read_len = len;
  427. else
  428. read_len = remain_len;
  429. if (spi->max_read_size)
  430. read_len = min(read_len, spi->max_read_size);
  431. spi_flash_addr(read_addr, cmd);
  432. ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
  433. if (ret < 0) {
  434. debug("SF: read failed\n");
  435. break;
  436. }
  437. offset += read_len;
  438. len -= read_len;
  439. data += read_len;
  440. }
  441. #ifdef CONFIG_SPI_FLASH_BAR
  442. ret = clean_bar(flash);
  443. #endif
  444. free(cmd);
  445. return ret;
  446. }
  447. #ifdef CONFIG_SPI_FLASH_SST
  448. static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
  449. {
  450. switch (ctl) {
  451. case SST26_CTL_LOCK:
  452. cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
  453. break;
  454. case SST26_CTL_UNLOCK:
  455. cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
  456. break;
  457. case SST26_CTL_CHECK:
  458. return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
  459. }
  460. return false;
  461. }
  462. /*
  463. * sst26wf016/sst26wf032/sst26wf064 have next block protection:
  464. * 4x - 8 KByte blocks - read & write protection bits - upper addresses
  465. * 1x - 32 KByte blocks - write protection bits
  466. * rest - 64 KByte blocks - write protection bits
  467. * 1x - 32 KByte blocks - write protection bits
  468. * 4x - 8 KByte blocks - read & write protection bits - lower addresses
  469. *
  470. * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
  471. * will be treated as single block.
  472. */
  473. /*
  474. * Lock, unlock or check lock status of the flash region of the flash (depending
  475. * on the lock_ctl value)
  476. */
  477. static int sst26_lock_ctl(struct spi_flash *flash, u32 ofs, size_t len, enum lock_ctl ctl)
  478. {
  479. u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
  480. bool lower_64k = false, upper_64k = false;
  481. u8 cmd, bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
  482. int ret;
  483. /* Check length and offset for 64k alignment */
  484. if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1)))
  485. return -EINVAL;
  486. if (ofs + len > flash->size)
  487. return -EINVAL;
  488. /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
  489. if (flash->size != SZ_2M &&
  490. flash->size != SZ_4M &&
  491. flash->size != SZ_8M)
  492. return -EINVAL;
  493. bpr_size = 2 + (flash->size / SZ_64K / 8);
  494. cmd = SST26_CMD_READ_BPR;
  495. ret = spi_flash_read_common(flash, &cmd, 1, bpr_buff, bpr_size);
  496. if (ret < 0) {
  497. printf("SF: fail to read block-protection register\n");
  498. return ret;
  499. }
  500. rptr_64k = min_t(u32, ofs + len , flash->size - SST26_BOUND_REG_SIZE);
  501. lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
  502. upper_64k = ((ofs + len) > (flash->size - SST26_BOUND_REG_SIZE));
  503. lower_64k = (ofs < SST26_BOUND_REG_SIZE);
  504. /* Lower bits in block-protection register are about 64k region */
  505. bpr_ptr = lptr_64k / SZ_64K - 1;
  506. /* Process 64K blocks region */
  507. while (lptr_64k < rptr_64k) {
  508. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  509. return EACCES;
  510. bpr_ptr++;
  511. lptr_64k += SZ_64K;
  512. }
  513. /* 32K and 8K region bits in BPR are after 64k region bits */
  514. bpr_ptr = (flash->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
  515. /* Process lower 32K block region */
  516. if (lower_64k)
  517. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  518. return EACCES;
  519. bpr_ptr++;
  520. /* Process upper 32K block region */
  521. if (upper_64k)
  522. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  523. return EACCES;
  524. bpr_ptr++;
  525. /* Process lower 8K block regions */
  526. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  527. if (lower_64k)
  528. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  529. return EACCES;
  530. /* In 8K area BPR has both read and write protection bits */
  531. bpr_ptr += 2;
  532. }
  533. /* Process upper 8K block regions */
  534. for (i = 0; i < SST26_BPR_8K_NUM; i++) {
  535. if (upper_64k)
  536. if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
  537. return EACCES;
  538. /* In 8K area BPR has both read and write protection bits */
  539. bpr_ptr += 2;
  540. }
  541. /* If we check region status we don't need to write BPR back */
  542. if (ctl == SST26_CTL_CHECK)
  543. return 0;
  544. cmd = SST26_CMD_WRITE_BPR;
  545. ret = spi_flash_write_common(flash, &cmd, 1, bpr_buff, bpr_size);
  546. if (ret < 0) {
  547. printf("SF: fail to write block-protection register\n");
  548. return ret;
  549. }
  550. return 0;
  551. }
  552. static int sst26_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  553. {
  554. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_UNLOCK);
  555. }
  556. static int sst26_lock(struct spi_flash *flash, u32 ofs, size_t len)
  557. {
  558. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_LOCK);
  559. }
  560. /*
  561. * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
  562. * and negative on errors.
  563. */
  564. static int sst26_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  565. {
  566. /*
  567. * is_locked function is used for check before reading or erasing flash
  568. * region, so offset and length might be not 64k allighned, so adjust
  569. * them to be 64k allighned as sst26_lock_ctl works only with 64k
  570. * allighned regions.
  571. */
  572. ofs -= ofs & (SZ_64K - 1);
  573. len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
  574. return sst26_lock_ctl(flash, ofs, len, SST26_CTL_CHECK);
  575. }
  576. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  577. {
  578. struct spi_slave *spi = flash->spi;
  579. int ret;
  580. u8 cmd[4] = {
  581. CMD_SST_BP,
  582. offset >> 16,
  583. offset >> 8,
  584. offset,
  585. };
  586. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  587. spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
  588. ret = spi_flash_cmd_write_enable(flash);
  589. if (ret)
  590. return ret;
  591. ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
  592. if (ret)
  593. return ret;
  594. return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  595. }
  596. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  597. const void *buf)
  598. {
  599. struct spi_slave *spi = flash->spi;
  600. size_t actual, cmd_len;
  601. int ret;
  602. u8 cmd[4];
  603. ret = spi_claim_bus(spi);
  604. if (ret) {
  605. debug("SF: Unable to claim SPI bus\n");
  606. return ret;
  607. }
  608. /* If the data is not word aligned, write out leading single byte */
  609. actual = offset % 2;
  610. if (actual) {
  611. ret = sst_byte_write(flash, offset, buf);
  612. if (ret)
  613. goto done;
  614. }
  615. offset += actual;
  616. ret = spi_flash_cmd_write_enable(flash);
  617. if (ret)
  618. goto done;
  619. cmd_len = 4;
  620. cmd[0] = CMD_SST_AAI_WP;
  621. cmd[1] = offset >> 16;
  622. cmd[2] = offset >> 8;
  623. cmd[3] = offset;
  624. for (; actual < len - 1; actual += 2) {
  625. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  626. spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
  627. cmd[0], offset);
  628. ret = spi_flash_cmd_write(spi, cmd, cmd_len,
  629. buf + actual, 2);
  630. if (ret) {
  631. debug("SF: sst word program failed\n");
  632. break;
  633. }
  634. ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  635. if (ret)
  636. break;
  637. cmd_len = 1;
  638. offset += 2;
  639. }
  640. if (!ret)
  641. ret = spi_flash_cmd_write_disable(flash);
  642. /* If there is a single trailing byte, write it out */
  643. if (!ret && actual != len)
  644. ret = sst_byte_write(flash, offset, buf + actual);
  645. done:
  646. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  647. ret ? "failure" : "success", len, offset - actual);
  648. spi_release_bus(spi);
  649. return ret;
  650. }
  651. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  652. const void *buf)
  653. {
  654. struct spi_slave *spi = flash->spi;
  655. size_t actual;
  656. int ret;
  657. ret = spi_claim_bus(spi);
  658. if (ret) {
  659. debug("SF: Unable to claim SPI bus\n");
  660. return ret;
  661. }
  662. for (actual = 0; actual < len; actual++) {
  663. ret = sst_byte_write(flash, offset, buf + actual);
  664. if (ret) {
  665. debug("SF: sst byte program failed\n");
  666. break;
  667. }
  668. offset++;
  669. }
  670. if (!ret)
  671. ret = spi_flash_cmd_write_disable(flash);
  672. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  673. ret ? "failure" : "success", len, offset - actual);
  674. spi_release_bus(spi);
  675. return ret;
  676. }
  677. #endif
  678. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  679. static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
  680. u64 *len)
  681. {
  682. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  683. int shift = ffs(mask) - 1;
  684. int pow;
  685. if (!(sr & mask)) {
  686. /* No protection */
  687. *ofs = 0;
  688. *len = 0;
  689. } else {
  690. pow = ((sr & mask) ^ mask) >> shift;
  691. *len = flash->size >> pow;
  692. *ofs = flash->size - *len;
  693. }
  694. }
  695. /*
  696. * Return 1 if the entire region is locked, 0 otherwise
  697. */
  698. static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
  699. u8 sr)
  700. {
  701. loff_t lock_offs;
  702. u64 lock_len;
  703. stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
  704. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  705. }
  706. /*
  707. * Check if a region of the flash is (completely) locked. See stm_lock() for
  708. * more info.
  709. *
  710. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  711. * negative on errors.
  712. */
  713. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  714. {
  715. int status;
  716. u8 sr;
  717. status = read_sr(flash, &sr);
  718. if (status < 0)
  719. return status;
  720. return stm_is_locked_sr(flash, ofs, len, sr);
  721. }
  722. /*
  723. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  724. * Supports only the block protection bits BP{0,1,2} in the status register
  725. * (SR). Does not support these features found in newer SR bitfields:
  726. * - TB: top/bottom protect - only handle TB=0 (top protect)
  727. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  728. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  729. *
  730. * Sample table portion for 8MB flash (Winbond w25q64fw):
  731. *
  732. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  733. * --------------------------------------------------------------------------
  734. * X | X | 0 | 0 | 0 | NONE | NONE
  735. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  736. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  737. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  738. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  739. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  740. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  741. * X | X | 1 | 1 | 1 | 8 MB | ALL
  742. *
  743. * Returns negative on errors, 0 on success.
  744. */
  745. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
  746. {
  747. u8 status_old, status_new;
  748. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  749. u8 shift = ffs(mask) - 1, pow, val;
  750. int ret;
  751. ret = read_sr(flash, &status_old);
  752. if (ret < 0)
  753. return ret;
  754. /* SPI NOR always locks to the end */
  755. if (ofs + len != flash->size) {
  756. /* Does combined region extend to end? */
  757. if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
  758. status_old))
  759. return -EINVAL;
  760. len = flash->size - ofs;
  761. }
  762. /*
  763. * Need smallest pow such that:
  764. *
  765. * 1 / (2^pow) <= (len / size)
  766. *
  767. * so (assuming power-of-2 size) we do:
  768. *
  769. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  770. */
  771. pow = ilog2(flash->size) - ilog2(len);
  772. val = mask - (pow << shift);
  773. if (val & ~mask)
  774. return -EINVAL;
  775. /* Don't "lock" with no region! */
  776. if (!(val & mask))
  777. return -EINVAL;
  778. status_new = (status_old & ~mask) | val;
  779. /* Only modify protection if it will not unlock other areas */
  780. if ((status_new & mask) <= (status_old & mask))
  781. return -EINVAL;
  782. write_sr(flash, status_new);
  783. return 0;
  784. }
  785. /*
  786. * Unlock a region of the flash. See stm_lock() for more info
  787. *
  788. * Returns negative on errors, 0 on success.
  789. */
  790. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  791. {
  792. uint8_t status_old, status_new;
  793. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  794. u8 shift = ffs(mask) - 1, pow, val;
  795. int ret;
  796. ret = read_sr(flash, &status_old);
  797. if (ret < 0)
  798. return ret;
  799. /* Cannot unlock; would unlock larger region than requested */
  800. if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
  801. status_old))
  802. return -EINVAL;
  803. /*
  804. * Need largest pow such that:
  805. *
  806. * 1 / (2^pow) >= (len / size)
  807. *
  808. * so (assuming power-of-2 size) we do:
  809. *
  810. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  811. */
  812. pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
  813. if (ofs + len == flash->size) {
  814. val = 0; /* fully unlocked */
  815. } else {
  816. val = mask - (pow << shift);
  817. /* Some power-of-two sizes are not supported */
  818. if (val & ~mask)
  819. return -EINVAL;
  820. }
  821. status_new = (status_old & ~mask) | val;
  822. /* Only modify protection if it will not lock other areas */
  823. if ((status_new & mask) >= (status_old & mask))
  824. return -EINVAL;
  825. write_sr(flash, status_new);
  826. return 0;
  827. }
  828. #endif
  829. #ifdef CONFIG_SPI_FLASH_MACRONIX
  830. static int macronix_quad_enable(struct spi_flash *flash)
  831. {
  832. u8 qeb_status;
  833. int ret;
  834. ret = read_sr(flash, &qeb_status);
  835. if (ret < 0)
  836. return ret;
  837. if (qeb_status & STATUS_QEB_MXIC)
  838. return 0;
  839. ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
  840. if (ret < 0)
  841. return ret;
  842. /* read SR and check it */
  843. ret = read_sr(flash, &qeb_status);
  844. if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
  845. printf("SF: Macronix SR Quad bit not clear\n");
  846. return -EINVAL;
  847. }
  848. return ret;
  849. }
  850. #endif
  851. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  852. static int spansion_quad_enable(struct spi_flash *flash)
  853. {
  854. u8 qeb_status;
  855. int ret;
  856. ret = read_cr(flash, &qeb_status);
  857. if (ret < 0)
  858. return ret;
  859. if (qeb_status & STATUS_QEB_WINSPAN)
  860. return 0;
  861. ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
  862. if (ret < 0)
  863. return ret;
  864. /* read CR and check it */
  865. ret = read_cr(flash, &qeb_status);
  866. if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
  867. printf("SF: Spansion CR Quad bit not clear\n");
  868. return -EINVAL;
  869. }
  870. return ret;
  871. }
  872. #endif
  873. static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
  874. {
  875. int tmp;
  876. u8 id[SPI_FLASH_MAX_ID_LEN];
  877. const struct spi_flash_info *info;
  878. tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
  879. if (tmp < 0) {
  880. printf("SF: error %d reading JEDEC ID\n", tmp);
  881. return ERR_PTR(tmp);
  882. }
  883. info = spi_flash_ids;
  884. for (; info->name != NULL; info++) {
  885. if (info->id_len) {
  886. if (!memcmp(info->id, id, info->id_len))
  887. return info;
  888. }
  889. }
  890. printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
  891. id[0], id[1], id[2]);
  892. return ERR_PTR(-ENODEV);
  893. }
  894. static int set_quad_mode(struct spi_flash *flash,
  895. const struct spi_flash_info *info)
  896. {
  897. switch (JEDEC_MFR(info)) {
  898. #ifdef CONFIG_SPI_FLASH_MACRONIX
  899. case SPI_FLASH_CFI_MFR_MACRONIX:
  900. return macronix_quad_enable(flash);
  901. #endif
  902. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  903. case SPI_FLASH_CFI_MFR_SPANSION:
  904. case SPI_FLASH_CFI_MFR_WINBOND:
  905. return spansion_quad_enable(flash);
  906. #endif
  907. #ifdef CONFIG_SPI_FLASH_STMICRO
  908. case SPI_FLASH_CFI_MFR_STMICRO:
  909. debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
  910. return 0;
  911. #endif
  912. default:
  913. printf("SF: Need set QEB func for %02x flash\n",
  914. JEDEC_MFR(info));
  915. return -1;
  916. }
  917. }
  918. #if CONFIG_IS_ENABLED(OF_CONTROL)
  919. int spi_flash_decode_fdt(struct spi_flash *flash)
  920. {
  921. #ifdef CONFIG_DM_SPI_FLASH
  922. fdt_addr_t addr;
  923. fdt_size_t size;
  924. addr = dev_read_addr_size(flash->dev, "memory-map", &size);
  925. if (addr == FDT_ADDR_T_NONE) {
  926. debug("%s: Cannot decode address\n", __func__);
  927. return 0;
  928. }
  929. if (flash->size > size) {
  930. debug("%s: Memory map must cover entire device\n", __func__);
  931. return -1;
  932. }
  933. flash->memory_map = map_sysmem(addr, size);
  934. #endif
  935. return 0;
  936. }
  937. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  938. int spi_flash_scan(struct spi_flash *flash)
  939. {
  940. struct spi_slave *spi = flash->spi;
  941. const struct spi_flash_info *info = NULL;
  942. int ret;
  943. info = spi_flash_read_id(flash);
  944. if (IS_ERR_OR_NULL(info))
  945. return -ENOENT;
  946. /*
  947. * Flash powers up read-only, so clear BP# bits.
  948. *
  949. * Note on some flash (like Macronix), QE (quad enable) bit is in the
  950. * same status register as BP# bits, and we need preserve its original
  951. * value during a reboot cycle as this is required by some platforms
  952. * (like Intel ICH SPI controller working under descriptor mode).
  953. */
  954. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
  955. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
  956. (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
  957. u8 sr = 0;
  958. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
  959. read_sr(flash, &sr);
  960. sr &= STATUS_QEB_MXIC;
  961. }
  962. write_sr(flash, sr);
  963. }
  964. flash->name = info->name;
  965. flash->memory_map = spi->memory_map;
  966. if (info->flags & SST_WR)
  967. flash->flags |= SNOR_F_SST_WR;
  968. #ifndef CONFIG_DM_SPI_FLASH
  969. flash->write = spi_flash_cmd_write_ops;
  970. #if defined(CONFIG_SPI_FLASH_SST)
  971. if (flash->flags & SNOR_F_SST_WR) {
  972. if (spi->mode & SPI_TX_BYTE)
  973. flash->write = sst_write_bp;
  974. else
  975. flash->write = sst_write_wp;
  976. }
  977. #endif
  978. flash->erase = spi_flash_cmd_erase_ops;
  979. flash->read = spi_flash_cmd_read_ops;
  980. #endif
  981. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  982. /* NOR protection support for STmicro/Micron chips and similar */
  983. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
  984. JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
  985. flash->flash_lock = stm_lock;
  986. flash->flash_unlock = stm_unlock;
  987. flash->flash_is_locked = stm_is_locked;
  988. }
  989. #endif
  990. /* sst26wf series block protection implementation differs from other series */
  991. #if defined(CONFIG_SPI_FLASH_SST)
  992. if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST && info->id[1] == 0x26) {
  993. flash->flash_lock = sst26_lock;
  994. flash->flash_unlock = sst26_unlock;
  995. flash->flash_is_locked = sst26_is_locked;
  996. }
  997. #endif
  998. /* Compute the flash size */
  999. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  1000. flash->page_size = info->page_size;
  1001. /*
  1002. * The Spansion S25FS512S, S25FL032P and S25FL064P have 256b pages,
  1003. * yet use the 0x4d00 Extended JEDEC code. The rest of the Spansion
  1004. * flashes with the 0x4d00 Extended JEDEC code have 512b pages.
  1005. * All of the others have 256b pages.
  1006. */
  1007. if (JEDEC_EXT(info) == 0x4d00) {
  1008. if ((JEDEC_ID(info) != 0x0215) &&
  1009. (JEDEC_ID(info) != 0x0216) &&
  1010. (JEDEC_ID(info) != 0x0220))
  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. }