spi_flash.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * SPI Flash Core
  3. *
  4. * Copyright (C) 2015 Jagan Teki <jteki@openedev.com>
  5. * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
  6. * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
  7. * Copyright (C) 2008 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <errno.h>
  13. #include <malloc.h>
  14. #include <mapmem.h>
  15. #include <spi.h>
  16. #include <spi_flash.h>
  17. #include <linux/log2.h>
  18. #include <dma.h>
  19. #include "sf_internal.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static void spi_flash_addr(u32 addr, u8 *cmd)
  22. {
  23. /* cmd[0] is actual command */
  24. cmd[1] = addr >> 16;
  25. cmd[2] = addr >> 8;
  26. cmd[3] = addr >> 0;
  27. }
  28. static int read_sr(struct spi_flash *flash, u8 *rs)
  29. {
  30. int ret;
  31. u8 cmd;
  32. cmd = CMD_READ_STATUS;
  33. ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
  34. if (ret < 0) {
  35. debug("SF: fail to read status register\n");
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. static int read_fsr(struct spi_flash *flash, u8 *fsr)
  41. {
  42. int ret;
  43. const u8 cmd = CMD_FLAG_STATUS;
  44. ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
  45. if (ret < 0) {
  46. debug("SF: fail to read flag status register\n");
  47. return ret;
  48. }
  49. return 0;
  50. }
  51. static int write_sr(struct spi_flash *flash, u8 ws)
  52. {
  53. u8 cmd;
  54. int ret;
  55. cmd = CMD_WRITE_STATUS;
  56. ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
  57. if (ret < 0) {
  58. debug("SF: fail to write status register\n");
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  64. static int read_cr(struct spi_flash *flash, u8 *rc)
  65. {
  66. int ret;
  67. u8 cmd;
  68. cmd = CMD_READ_CONFIG;
  69. ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
  70. if (ret < 0) {
  71. debug("SF: fail to read config register\n");
  72. return ret;
  73. }
  74. return 0;
  75. }
  76. static int write_cr(struct spi_flash *flash, u8 wc)
  77. {
  78. u8 data[2];
  79. u8 cmd;
  80. int ret;
  81. ret = read_sr(flash, &data[0]);
  82. if (ret < 0)
  83. return ret;
  84. cmd = CMD_WRITE_STATUS;
  85. data[1] = wc;
  86. ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
  87. if (ret) {
  88. debug("SF: fail to write config register\n");
  89. return ret;
  90. }
  91. return 0;
  92. }
  93. #endif
  94. #ifdef CONFIG_SPI_FLASH_STMICRO
  95. static int read_evcr(struct spi_flash *flash, u8 *evcr)
  96. {
  97. int ret;
  98. const u8 cmd = CMD_READ_EVCR;
  99. ret = spi_flash_read_common(flash, &cmd, 1, evcr, 1);
  100. if (ret < 0) {
  101. debug("SF: error reading EVCR\n");
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static int write_evcr(struct spi_flash *flash, u8 evcr)
  107. {
  108. u8 cmd;
  109. int ret;
  110. cmd = CMD_WRITE_EVCR;
  111. ret = spi_flash_write_common(flash, &cmd, 1, &evcr, 1);
  112. if (ret < 0) {
  113. debug("SF: error while writing EVCR register\n");
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. #endif
  119. #ifdef CONFIG_SPI_FLASH_BAR
  120. static int spi_flash_write_bar(struct spi_flash *flash, u32 offset)
  121. {
  122. u8 cmd, bank_sel;
  123. int ret;
  124. bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
  125. if (bank_sel == flash->bank_curr)
  126. goto bar_end;
  127. cmd = flash->bank_write_cmd;
  128. ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
  129. if (ret < 0) {
  130. debug("SF: fail to write bank register\n");
  131. return ret;
  132. }
  133. bar_end:
  134. flash->bank_curr = bank_sel;
  135. return flash->bank_curr;
  136. }
  137. static int spi_flash_read_bar(struct spi_flash *flash, u8 idcode0)
  138. {
  139. u8 curr_bank = 0;
  140. int ret;
  141. if (flash->size <= SPI_FLASH_16MB_BOUN)
  142. goto bar_end;
  143. switch (idcode0) {
  144. case SPI_FLASH_CFI_MFR_SPANSION:
  145. flash->bank_read_cmd = CMD_BANKADDR_BRRD;
  146. flash->bank_write_cmd = CMD_BANKADDR_BRWR;
  147. break;
  148. default:
  149. flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
  150. flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
  151. }
  152. ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  153. &curr_bank, 1);
  154. if (ret) {
  155. debug("SF: fail to read bank addr register\n");
  156. return ret;
  157. }
  158. bar_end:
  159. flash->bank_curr = curr_bank;
  160. return 0;
  161. }
  162. #endif
  163. #ifdef CONFIG_SF_DUAL_FLASH
  164. static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
  165. {
  166. struct spi_slave *spi = flash->spi;
  167. switch (flash->dual_flash) {
  168. case SF_DUAL_STACKED_FLASH:
  169. if (*addr >= (flash->size >> 1)) {
  170. *addr -= flash->size >> 1;
  171. spi->flags |= SPI_XFER_U_PAGE;
  172. } else {
  173. spi->flags &= ~SPI_XFER_U_PAGE;
  174. }
  175. break;
  176. case SF_DUAL_PARALLEL_FLASH:
  177. *addr >>= flash->shift;
  178. break;
  179. default:
  180. debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
  181. break;
  182. }
  183. }
  184. #endif
  185. static int spi_flash_sr_ready(struct spi_flash *flash)
  186. {
  187. u8 sr;
  188. int ret;
  189. ret = read_sr(flash, &sr);
  190. if (ret < 0)
  191. return ret;
  192. return !(sr & STATUS_WIP);
  193. }
  194. static int spi_flash_fsr_ready(struct spi_flash *flash)
  195. {
  196. u8 fsr;
  197. int ret;
  198. ret = read_fsr(flash, &fsr);
  199. if (ret < 0)
  200. return ret;
  201. return fsr & STATUS_PEC;
  202. }
  203. static int spi_flash_ready(struct spi_flash *flash)
  204. {
  205. int sr, fsr;
  206. sr = spi_flash_sr_ready(flash);
  207. if (sr < 0)
  208. return sr;
  209. fsr = 1;
  210. if (flash->flags & SNOR_F_USE_FSR) {
  211. fsr = spi_flash_fsr_ready(flash);
  212. if (fsr < 0)
  213. return fsr;
  214. }
  215. return sr && fsr;
  216. }
  217. static int spi_flash_cmd_wait_ready(struct spi_flash *flash,
  218. unsigned long timeout)
  219. {
  220. unsigned long timebase;
  221. int ret;
  222. timebase = get_timer(0);
  223. while (get_timer(timebase) < timeout) {
  224. ret = spi_flash_ready(flash);
  225. if (ret < 0)
  226. return ret;
  227. if (ret)
  228. return 0;
  229. }
  230. printf("SF: Timeout!\n");
  231. return -ETIMEDOUT;
  232. }
  233. int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
  234. size_t cmd_len, const void *buf, size_t buf_len)
  235. {
  236. struct spi_slave *spi = flash->spi;
  237. unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
  238. int ret;
  239. if (buf == NULL)
  240. timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
  241. ret = spi_claim_bus(spi);
  242. if (ret) {
  243. debug("SF: unable to claim SPI bus\n");
  244. return ret;
  245. }
  246. ret = spi_flash_cmd_write_enable(flash);
  247. if (ret < 0) {
  248. debug("SF: enabling write failed\n");
  249. return ret;
  250. }
  251. ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
  252. if (ret < 0) {
  253. debug("SF: write cmd failed\n");
  254. return ret;
  255. }
  256. ret = spi_flash_cmd_wait_ready(flash, timeout);
  257. if (ret < 0) {
  258. debug("SF: write %s timed out\n",
  259. timeout == SPI_FLASH_PROG_TIMEOUT ?
  260. "program" : "page erase");
  261. return ret;
  262. }
  263. spi_release_bus(spi);
  264. return ret;
  265. }
  266. int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
  267. {
  268. u32 erase_size, erase_addr;
  269. u8 cmd[SPI_FLASH_CMD_LEN];
  270. int ret = -1;
  271. erase_size = flash->erase_size;
  272. if (offset % erase_size || len % erase_size) {
  273. debug("SF: Erase offset/length not multiple of erase size\n");
  274. return -1;
  275. }
  276. if (flash->flash_is_locked) {
  277. if (flash->flash_is_locked(flash, offset, len) > 0) {
  278. printf("offset 0x%x is protected and cannot be erased\n",
  279. offset);
  280. return -EINVAL;
  281. }
  282. }
  283. cmd[0] = flash->erase_cmd;
  284. while (len) {
  285. erase_addr = offset;
  286. #ifdef CONFIG_SF_DUAL_FLASH
  287. if (flash->dual_flash > SF_SINGLE_FLASH)
  288. spi_flash_dual(flash, &erase_addr);
  289. #endif
  290. #ifdef CONFIG_SPI_FLASH_BAR
  291. ret = spi_flash_write_bar(flash, erase_addr);
  292. if (ret < 0)
  293. return ret;
  294. #endif
  295. spi_flash_addr(erase_addr, cmd);
  296. debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
  297. cmd[2], cmd[3], erase_addr);
  298. ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
  299. if (ret < 0) {
  300. debug("SF: erase failed\n");
  301. break;
  302. }
  303. offset += erase_size;
  304. len -= erase_size;
  305. }
  306. return ret;
  307. }
  308. int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
  309. size_t len, const void *buf)
  310. {
  311. struct spi_slave *spi = flash->spi;
  312. unsigned long byte_addr, page_size;
  313. u32 write_addr;
  314. size_t chunk_len, actual;
  315. u8 cmd[SPI_FLASH_CMD_LEN];
  316. int ret = -1;
  317. page_size = flash->page_size;
  318. if (flash->flash_is_locked) {
  319. if (flash->flash_is_locked(flash, offset, len) > 0) {
  320. printf("offset 0x%x is protected and cannot be written\n",
  321. offset);
  322. return -EINVAL;
  323. }
  324. }
  325. cmd[0] = flash->write_cmd;
  326. for (actual = 0; actual < len; actual += chunk_len) {
  327. write_addr = offset;
  328. #ifdef CONFIG_SF_DUAL_FLASH
  329. if (flash->dual_flash > SF_SINGLE_FLASH)
  330. spi_flash_dual(flash, &write_addr);
  331. #endif
  332. #ifdef CONFIG_SPI_FLASH_BAR
  333. ret = spi_flash_write_bar(flash, write_addr);
  334. if (ret < 0)
  335. return ret;
  336. #endif
  337. byte_addr = offset % page_size;
  338. chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
  339. if (spi->max_write_size)
  340. chunk_len = min(chunk_len,
  341. (size_t)spi->max_write_size);
  342. spi_flash_addr(write_addr, cmd);
  343. debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
  344. buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
  345. ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
  346. buf + actual, chunk_len);
  347. if (ret < 0) {
  348. debug("SF: write failed\n");
  349. break;
  350. }
  351. offset += chunk_len;
  352. }
  353. return ret;
  354. }
  355. int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
  356. size_t cmd_len, void *data, size_t data_len)
  357. {
  358. struct spi_slave *spi = flash->spi;
  359. int ret;
  360. ret = spi_claim_bus(spi);
  361. if (ret) {
  362. debug("SF: unable to claim SPI bus\n");
  363. return ret;
  364. }
  365. ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
  366. if (ret < 0) {
  367. debug("SF: read cmd failed\n");
  368. return ret;
  369. }
  370. spi_release_bus(spi);
  371. return ret;
  372. }
  373. /*
  374. * TODO: remove the weak after all the other spi_flash_copy_mmap
  375. * implementations removed from drivers
  376. */
  377. void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
  378. {
  379. #ifdef CONFIG_DMA
  380. if (!dma_memcpy(data, offset, len))
  381. return;
  382. #endif
  383. memcpy(data, offset, len);
  384. }
  385. int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
  386. size_t len, void *data)
  387. {
  388. struct spi_slave *spi = flash->spi;
  389. u8 *cmd, cmdsz;
  390. u32 remain_len, read_len, read_addr;
  391. int bank_sel = 0;
  392. int ret = -1;
  393. /* Handle memory-mapped SPI */
  394. if (flash->memory_map) {
  395. ret = spi_claim_bus(spi);
  396. if (ret) {
  397. debug("SF: unable to claim SPI bus\n");
  398. return ret;
  399. }
  400. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP);
  401. spi_flash_copy_mmap(data, flash->memory_map + offset, len);
  402. spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
  403. spi_release_bus(spi);
  404. return 0;
  405. }
  406. cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
  407. cmd = calloc(1, cmdsz);
  408. if (!cmd) {
  409. debug("SF: Failed to allocate cmd\n");
  410. return -ENOMEM;
  411. }
  412. cmd[0] = flash->read_cmd;
  413. while (len) {
  414. read_addr = offset;
  415. #ifdef CONFIG_SF_DUAL_FLASH
  416. if (flash->dual_flash > SF_SINGLE_FLASH)
  417. spi_flash_dual(flash, &read_addr);
  418. #endif
  419. #ifdef CONFIG_SPI_FLASH_BAR
  420. ret = spi_flash_write_bar(flash, read_addr);
  421. if (ret < 0)
  422. return ret;
  423. bank_sel = flash->bank_curr;
  424. #endif
  425. remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
  426. (bank_sel + 1)) - offset;
  427. if (len < remain_len)
  428. read_len = len;
  429. else
  430. read_len = remain_len;
  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. free(cmd);
  442. return ret;
  443. }
  444. #ifdef CONFIG_SPI_FLASH_SST
  445. static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
  446. {
  447. struct spi_slave *spi = flash->spi;
  448. int ret;
  449. u8 cmd[4] = {
  450. CMD_SST_BP,
  451. offset >> 16,
  452. offset >> 8,
  453. offset,
  454. };
  455. debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  456. spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
  457. ret = spi_flash_cmd_write_enable(flash);
  458. if (ret)
  459. return ret;
  460. ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
  461. if (ret)
  462. return ret;
  463. return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  464. }
  465. int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
  466. const void *buf)
  467. {
  468. struct spi_slave *spi = flash->spi;
  469. size_t actual, cmd_len;
  470. int ret;
  471. u8 cmd[4];
  472. ret = spi_claim_bus(spi);
  473. if (ret) {
  474. debug("SF: Unable to claim SPI bus\n");
  475. return ret;
  476. }
  477. /* If the data is not word aligned, write out leading single byte */
  478. actual = offset % 2;
  479. if (actual) {
  480. ret = sst_byte_write(flash, offset, buf);
  481. if (ret)
  482. goto done;
  483. }
  484. offset += actual;
  485. ret = spi_flash_cmd_write_enable(flash);
  486. if (ret)
  487. goto done;
  488. cmd_len = 4;
  489. cmd[0] = CMD_SST_AAI_WP;
  490. cmd[1] = offset >> 16;
  491. cmd[2] = offset >> 8;
  492. cmd[3] = offset;
  493. for (; actual < len - 1; actual += 2) {
  494. debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
  495. spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
  496. cmd[0], offset);
  497. ret = spi_flash_cmd_write(spi, cmd, cmd_len,
  498. buf + actual, 2);
  499. if (ret) {
  500. debug("SF: sst word program failed\n");
  501. break;
  502. }
  503. ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
  504. if (ret)
  505. break;
  506. cmd_len = 1;
  507. offset += 2;
  508. }
  509. if (!ret)
  510. ret = spi_flash_cmd_write_disable(flash);
  511. /* If there is a single trailing byte, write it out */
  512. if (!ret && actual != len)
  513. ret = sst_byte_write(flash, offset, buf + actual);
  514. done:
  515. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  516. ret ? "failure" : "success", len, offset - actual);
  517. spi_release_bus(spi);
  518. return ret;
  519. }
  520. int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
  521. const void *buf)
  522. {
  523. struct spi_slave *spi = flash->spi;
  524. size_t actual;
  525. int ret;
  526. ret = spi_claim_bus(spi);
  527. if (ret) {
  528. debug("SF: Unable to claim SPI bus\n");
  529. return ret;
  530. }
  531. for (actual = 0; actual < len; actual++) {
  532. ret = sst_byte_write(flash, offset, buf + actual);
  533. if (ret) {
  534. debug("SF: sst byte program failed\n");
  535. break;
  536. }
  537. offset++;
  538. }
  539. if (!ret)
  540. ret = spi_flash_cmd_write_disable(flash);
  541. debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
  542. ret ? "failure" : "success", len, offset - actual);
  543. spi_release_bus(spi);
  544. return ret;
  545. }
  546. #endif
  547. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  548. static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
  549. u64 *len)
  550. {
  551. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  552. int shift = ffs(mask) - 1;
  553. int pow;
  554. if (!(sr & mask)) {
  555. /* No protection */
  556. *ofs = 0;
  557. *len = 0;
  558. } else {
  559. pow = ((sr & mask) ^ mask) >> shift;
  560. *len = flash->size >> pow;
  561. *ofs = flash->size - *len;
  562. }
  563. }
  564. /*
  565. * Return 1 if the entire region is locked, 0 otherwise
  566. */
  567. static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
  568. u8 sr)
  569. {
  570. loff_t lock_offs;
  571. u64 lock_len;
  572. stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
  573. return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
  574. }
  575. /*
  576. * Check if a region of the flash is (completely) locked. See stm_lock() for
  577. * more info.
  578. *
  579. * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
  580. * negative on errors.
  581. */
  582. int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
  583. {
  584. int status;
  585. u8 sr;
  586. status = read_sr(flash, &sr);
  587. if (status < 0)
  588. return status;
  589. return stm_is_locked_sr(flash, ofs, len, sr);
  590. }
  591. /*
  592. * Lock a region of the flash. Compatible with ST Micro and similar flash.
  593. * Supports only the block protection bits BP{0,1,2} in the status register
  594. * (SR). Does not support these features found in newer SR bitfields:
  595. * - TB: top/bottom protect - only handle TB=0 (top protect)
  596. * - SEC: sector/block protect - only handle SEC=0 (block protect)
  597. * - CMP: complement protect - only support CMP=0 (range is not complemented)
  598. *
  599. * Sample table portion for 8MB flash (Winbond w25q64fw):
  600. *
  601. * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
  602. * --------------------------------------------------------------------------
  603. * X | X | 0 | 0 | 0 | NONE | NONE
  604. * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
  605. * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
  606. * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
  607. * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
  608. * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
  609. * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
  610. * X | X | 1 | 1 | 1 | 8 MB | ALL
  611. *
  612. * Returns negative on errors, 0 on success.
  613. */
  614. int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
  615. {
  616. u8 status_old, status_new;
  617. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  618. u8 shift = ffs(mask) - 1, pow, val;
  619. int ret;
  620. ret = read_sr(flash, &status_old);
  621. if (ret < 0)
  622. return ret;
  623. /* SPI NOR always locks to the end */
  624. if (ofs + len != flash->size) {
  625. /* Does combined region extend to end? */
  626. if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
  627. status_old))
  628. return -EINVAL;
  629. len = flash->size - ofs;
  630. }
  631. /*
  632. * Need smallest pow such that:
  633. *
  634. * 1 / (2^pow) <= (len / size)
  635. *
  636. * so (assuming power-of-2 size) we do:
  637. *
  638. * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
  639. */
  640. pow = ilog2(flash->size) - ilog2(len);
  641. val = mask - (pow << shift);
  642. if (val & ~mask)
  643. return -EINVAL;
  644. /* Don't "lock" with no region! */
  645. if (!(val & mask))
  646. return -EINVAL;
  647. status_new = (status_old & ~mask) | val;
  648. /* Only modify protection if it will not unlock other areas */
  649. if ((status_new & mask) <= (status_old & mask))
  650. return -EINVAL;
  651. write_sr(flash, status_new);
  652. return 0;
  653. }
  654. /*
  655. * Unlock a region of the flash. See stm_lock() for more info
  656. *
  657. * Returns negative on errors, 0 on success.
  658. */
  659. int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
  660. {
  661. uint8_t status_old, status_new;
  662. u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
  663. u8 shift = ffs(mask) - 1, pow, val;
  664. int ret;
  665. ret = read_sr(flash, &status_old);
  666. if (ret < 0)
  667. return ret;
  668. /* Cannot unlock; would unlock larger region than requested */
  669. if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
  670. status_old))
  671. return -EINVAL;
  672. /*
  673. * Need largest pow such that:
  674. *
  675. * 1 / (2^pow) >= (len / size)
  676. *
  677. * so (assuming power-of-2 size) we do:
  678. *
  679. * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
  680. */
  681. pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
  682. if (ofs + len == flash->size) {
  683. val = 0; /* fully unlocked */
  684. } else {
  685. val = mask - (pow << shift);
  686. /* Some power-of-two sizes are not supported */
  687. if (val & ~mask)
  688. return -EINVAL;
  689. }
  690. status_new = (status_old & ~mask) | val;
  691. /* Only modify protection if it will not lock other areas */
  692. if ((status_new & mask) >= (status_old & mask))
  693. return -EINVAL;
  694. write_sr(flash, status_new);
  695. return 0;
  696. }
  697. #endif
  698. #ifdef CONFIG_SPI_FLASH_MACRONIX
  699. static int macronix_quad_enable(struct spi_flash *flash)
  700. {
  701. u8 qeb_status;
  702. int ret;
  703. ret = read_sr(flash, &qeb_status);
  704. if (ret < 0)
  705. return ret;
  706. if (qeb_status & STATUS_QEB_MXIC)
  707. return 0;
  708. ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
  709. if (ret < 0)
  710. return ret;
  711. /* read SR and check it */
  712. ret = read_sr(flash, &qeb_status);
  713. if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
  714. printf("SF: Macronix SR Quad bit not clear\n");
  715. return -EINVAL;
  716. }
  717. return ret;
  718. }
  719. #endif
  720. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  721. static int spansion_quad_enable(struct spi_flash *flash)
  722. {
  723. u8 qeb_status;
  724. int ret;
  725. ret = read_cr(flash, &qeb_status);
  726. if (ret < 0)
  727. return ret;
  728. if (qeb_status & STATUS_QEB_WINSPAN)
  729. return 0;
  730. ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
  731. if (ret < 0)
  732. return ret;
  733. /* read CR and check it */
  734. ret = read_cr(flash, &qeb_status);
  735. if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
  736. printf("SF: Spansion CR Quad bit not clear\n");
  737. return -EINVAL;
  738. }
  739. return ret;
  740. }
  741. #endif
  742. #ifdef CONFIG_SPI_FLASH_STMICRO
  743. static int micron_quad_enable(struct spi_flash *flash)
  744. {
  745. u8 qeb_status;
  746. int ret;
  747. ret = read_evcr(flash, &qeb_status);
  748. if (ret < 0)
  749. return ret;
  750. if (!(qeb_status & STATUS_QEB_MICRON))
  751. return 0;
  752. ret = write_evcr(flash, qeb_status & ~STATUS_QEB_MICRON);
  753. if (ret < 0)
  754. return ret;
  755. /* read EVCR and check it */
  756. ret = read_evcr(flash, &qeb_status);
  757. if (!(ret >= 0 && !(qeb_status & STATUS_QEB_MICRON))) {
  758. printf("SF: Micron EVCR Quad bit not clear\n");
  759. return -EINVAL;
  760. }
  761. return ret;
  762. }
  763. #endif
  764. static int set_quad_mode(struct spi_flash *flash, u8 idcode0)
  765. {
  766. switch (idcode0) {
  767. #ifdef CONFIG_SPI_FLASH_MACRONIX
  768. case SPI_FLASH_CFI_MFR_MACRONIX:
  769. return macronix_quad_enable(flash);
  770. #endif
  771. #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
  772. case SPI_FLASH_CFI_MFR_SPANSION:
  773. case SPI_FLASH_CFI_MFR_WINBOND:
  774. return spansion_quad_enable(flash);
  775. #endif
  776. #ifdef CONFIG_SPI_FLASH_STMICRO
  777. case SPI_FLASH_CFI_MFR_STMICRO:
  778. return micron_quad_enable(flash);
  779. #endif
  780. default:
  781. printf("SF: Need set QEB func for %02x flash\n", idcode0);
  782. return -1;
  783. }
  784. }
  785. #if CONFIG_IS_ENABLED(OF_CONTROL)
  786. int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
  787. {
  788. #ifdef CONFIG_DM_SPI_FLASH
  789. fdt_addr_t addr;
  790. fdt_size_t size;
  791. int node = flash->dev->of_offset;
  792. addr = fdtdec_get_addr_size(blob, node, "memory-map", &size);
  793. if (addr == FDT_ADDR_T_NONE) {
  794. debug("%s: Cannot decode address\n", __func__);
  795. return 0;
  796. }
  797. if (flash->size != size) {
  798. debug("%s: Memory map must cover entire device\n", __func__);
  799. return -1;
  800. }
  801. flash->memory_map = map_sysmem(addr, size);
  802. #endif
  803. return 0;
  804. }
  805. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  806. #ifdef CONFIG_SPI_FLASH_SPANSION
  807. static int spansion_s25fss_disable_4KB_erase(struct spi_slave *spi)
  808. {
  809. u8 cmd[4];
  810. u32 offset = 0x800004; /* CR3V register offset */
  811. u8 cr3v;
  812. int ret;
  813. cmd[0] = CMD_SPANSION_RDAR;
  814. cmd[1] = offset >> 16;
  815. cmd[2] = offset >> 8;
  816. cmd[3] = offset >> 0;
  817. ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1);
  818. if (ret)
  819. return -EIO;
  820. /* CR3V bit3: 4-KB Erase */
  821. if (cr3v & 0x8)
  822. return 0;
  823. cmd[0] = CMD_SPANSION_WRAR;
  824. cr3v |= 0x8;
  825. ret = spi_flash_cmd_write(spi, cmd, 4, &cr3v, 1);
  826. if (ret)
  827. return -EIO;
  828. cmd[0] = CMD_SPANSION_RDAR;
  829. ret = spi_flash_cmd_read(spi, cmd, 4, &cr3v, 1);
  830. if (ret)
  831. return -EIO;
  832. if (!(cr3v & 0x8))
  833. return -EFAULT;
  834. return 0;
  835. }
  836. #endif
  837. int spi_flash_scan(struct spi_flash *flash)
  838. {
  839. struct spi_slave *spi = flash->spi;
  840. const struct spi_flash_params *params;
  841. u16 jedec, ext_jedec;
  842. u8 cmd, idcode[5];
  843. int ret;
  844. static u8 spi_read_cmds_array[] = {
  845. CMD_READ_ARRAY_SLOW,
  846. CMD_READ_ARRAY_FAST,
  847. CMD_READ_DUAL_OUTPUT_FAST,
  848. CMD_READ_QUAD_OUTPUT_FAST,
  849. CMD_READ_DUAL_IO_FAST,
  850. CMD_READ_QUAD_IO_FAST };
  851. /* Read the ID codes */
  852. ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
  853. if (ret) {
  854. printf("SF: Failed to get idcodes\n");
  855. return ret;
  856. }
  857. #ifdef DEBUG
  858. printf("SF: Got idcodes\n");
  859. print_buffer(0, idcode, 1, sizeof(idcode), 0);
  860. #endif
  861. jedec = idcode[1] << 8 | idcode[2];
  862. ext_jedec = idcode[3] << 8 | idcode[4];
  863. /* Validate params from spi_flash_params table */
  864. params = spi_flash_params_table;
  865. for (; params->name != NULL; params++) {
  866. if ((params->jedec >> 16) == idcode[0]) {
  867. if ((params->jedec & 0xFFFF) == jedec) {
  868. if (params->ext_jedec == 0)
  869. break;
  870. else if (params->ext_jedec == ext_jedec)
  871. break;
  872. }
  873. }
  874. }
  875. if (!params->name) {
  876. printf("SF: Unsupported flash IDs: ");
  877. printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
  878. idcode[0], jedec, ext_jedec);
  879. return -EPROTONOSUPPORT;
  880. }
  881. #ifdef CONFIG_SPI_FLASH_SPANSION
  882. /*
  883. * The S25FS-S family physical sectors may be configured as a
  884. * hybrid combination of eight 4-kB parameter sectors
  885. * at the top or bottom of the address space with all
  886. * but one of the remaining sectors being uniform size.
  887. * The Parameter Sector Erase commands (20h or 21h) must
  888. * be used to erase the 4-kB parameter sectors individually.
  889. * The Sector (uniform sector) Erase commands (D8h or DCh)
  890. * must be used to erase any of the remaining
  891. * sectors, including the portion of highest or lowest address
  892. * sector that is not overlaid by the parameter sectors.
  893. * The uniform sector erase command has no effect on parameter sectors.
  894. */
  895. if ((jedec == 0x0219 || (jedec == 0x0220)) &&
  896. (ext_jedec & 0xff00) == 0x4d00) {
  897. int ret;
  898. u8 id[6];
  899. /* Read the ID codes again, 6 bytes */
  900. ret = spi_flash_cmd(flash->spi, CMD_READ_ID, id, sizeof(id));
  901. if (ret)
  902. return -EIO;
  903. ret = memcmp(id, idcode, 5);
  904. if (ret)
  905. return -EIO;
  906. /* 0x81: S25FS-S family 0x80: S25FL-S family */
  907. if (id[5] == 0x81) {
  908. ret = spansion_s25fss_disable_4KB_erase(spi);
  909. if (ret)
  910. return ret;
  911. }
  912. }
  913. #endif
  914. /* Flash powers up read-only, so clear BP# bits */
  915. if (idcode[0] == SPI_FLASH_CFI_MFR_ATMEL ||
  916. idcode[0] == SPI_FLASH_CFI_MFR_MACRONIX ||
  917. idcode[0] == SPI_FLASH_CFI_MFR_SST)
  918. write_sr(flash, 0);
  919. /* Assign spi data */
  920. flash->name = params->name;
  921. flash->memory_map = spi->memory_map;
  922. flash->dual_flash = spi->option;
  923. /* Assign spi flash flags */
  924. if (params->flags & SST_WR)
  925. flash->flags |= SNOR_F_SST_WR;
  926. /* Assign spi_flash ops */
  927. #ifndef CONFIG_DM_SPI_FLASH
  928. flash->write = spi_flash_cmd_write_ops;
  929. #if defined(CONFIG_SPI_FLASH_SST)
  930. if (flash->flags & SNOR_F_SST_WR) {
  931. if (spi->mode & SPI_TX_BYTE)
  932. flash->write = sst_write_bp;
  933. else
  934. flash->write = sst_write_wp;
  935. }
  936. #endif
  937. flash->erase = spi_flash_cmd_erase_ops;
  938. flash->read = spi_flash_cmd_read_ops;
  939. #endif
  940. /* lock hooks are flash specific - assign them based on idcode0 */
  941. switch (idcode[0]) {
  942. #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
  943. case SPI_FLASH_CFI_MFR_STMICRO:
  944. case SPI_FLASH_CFI_MFR_SST:
  945. flash->flash_lock = stm_lock;
  946. flash->flash_unlock = stm_unlock;
  947. flash->flash_is_locked = stm_is_locked;
  948. #endif
  949. break;
  950. default:
  951. debug("SF: Lock ops not supported for %02x flash\n", idcode[0]);
  952. }
  953. /* Compute the flash size */
  954. flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
  955. /*
  956. * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
  957. * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
  958. * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
  959. * have 256b pages.
  960. */
  961. if (ext_jedec == 0x4d00) {
  962. if ((jedec == 0x0215) || (jedec == 0x216) || (jedec == 0x220))
  963. flash->page_size = 256;
  964. else
  965. flash->page_size = 512;
  966. } else {
  967. flash->page_size = 256;
  968. }
  969. flash->page_size <<= flash->shift;
  970. flash->sector_size = params->sector_size << flash->shift;
  971. flash->size = flash->sector_size * params->nr_sectors << flash->shift;
  972. #ifdef CONFIG_SF_DUAL_FLASH
  973. if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
  974. flash->size <<= 1;
  975. #endif
  976. /* Compute erase sector and command */
  977. if (params->flags & SECT_4K) {
  978. flash->erase_cmd = CMD_ERASE_4K;
  979. flash->erase_size = 4096 << flash->shift;
  980. } else if (params->flags & SECT_32K) {
  981. flash->erase_cmd = CMD_ERASE_32K;
  982. flash->erase_size = 32768 << flash->shift;
  983. } else {
  984. flash->erase_cmd = CMD_ERASE_64K;
  985. flash->erase_size = flash->sector_size;
  986. }
  987. /* Now erase size becomes valid sector size */
  988. flash->sector_size = flash->erase_size;
  989. /* Look for the fastest read cmd */
  990. cmd = fls(params->e_rd_cmd & spi->mode_rx);
  991. if (cmd) {
  992. cmd = spi_read_cmds_array[cmd - 1];
  993. flash->read_cmd = cmd;
  994. } else {
  995. /* Go for default supported read cmd */
  996. flash->read_cmd = CMD_READ_ARRAY_FAST;
  997. }
  998. /* Not require to look for fastest only two write cmds yet */
  999. if (params->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
  1000. flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
  1001. else
  1002. /* Go for default supported write cmd */
  1003. flash->write_cmd = CMD_PAGE_PROGRAM;
  1004. /* Set the quad enable bit - only for quad commands */
  1005. if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
  1006. (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
  1007. (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
  1008. ret = set_quad_mode(flash, idcode[0]);
  1009. if (ret) {
  1010. debug("SF: Fail to set QEB for %02x\n", idcode[0]);
  1011. return -EINVAL;
  1012. }
  1013. }
  1014. /* Read dummy_byte: dummy byte is determined based on the
  1015. * dummy cycles of a particular command.
  1016. * Fast commands - dummy_byte = dummy_cycles/8
  1017. * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
  1018. * For I/O commands except cmd[0] everything goes on no.of lines
  1019. * based on particular command but incase of fast commands except
  1020. * data all go on single line irrespective of command.
  1021. */
  1022. switch (flash->read_cmd) {
  1023. case CMD_READ_QUAD_IO_FAST:
  1024. flash->dummy_byte = 2;
  1025. break;
  1026. case CMD_READ_ARRAY_SLOW:
  1027. flash->dummy_byte = 0;
  1028. break;
  1029. default:
  1030. flash->dummy_byte = 1;
  1031. }
  1032. #ifdef CONFIG_SPI_FLASH_STMICRO
  1033. if (params->flags & E_FSR)
  1034. flash->flags |= SNOR_F_USE_FSR;
  1035. #endif
  1036. /* Configure the BAR - discover bank cmds and read current bank */
  1037. #ifdef CONFIG_SPI_FLASH_BAR
  1038. ret = spi_flash_read_bar(flash, idcode[0]);
  1039. if (ret < 0)
  1040. return ret;
  1041. #endif
  1042. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1043. ret = spi_flash_decode_fdt(gd->fdt_blob, flash);
  1044. if (ret) {
  1045. debug("SF: FDT decode error\n");
  1046. return -EINVAL;
  1047. }
  1048. #endif
  1049. #ifndef CONFIG_SPL_BUILD
  1050. printf("SF: Detected %s with page size ", flash->name);
  1051. print_size(flash->page_size, ", erase size ");
  1052. print_size(flash->erase_size, ", total ");
  1053. print_size(flash->size, "");
  1054. if (flash->memory_map)
  1055. printf(", mapped at %p", flash->memory_map);
  1056. puts("\n");
  1057. #endif
  1058. #ifndef CONFIG_SPI_FLASH_BAR
  1059. if (((flash->dual_flash == SF_SINGLE_FLASH) &&
  1060. (flash->size > SPI_FLASH_16MB_BOUN)) ||
  1061. ((flash->dual_flash > SF_SINGLE_FLASH) &&
  1062. (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
  1063. puts("SF: Warning - Only lower 16MiB accessible,");
  1064. puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
  1065. }
  1066. #endif
  1067. return ret;
  1068. }