spi25.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. * This file is part of the flashrom project.
  3. *
  4. * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
  5. * Copyright (C) 2008 coresystems GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. * Contains the common SPI chip driver functions
  22. */
  23. #include <string.h>
  24. #include "flash.h"
  25. #include "flashchips.h"
  26. #include "chipdrivers.h"
  27. #include "programmer.h"
  28. #include "spi.h"
  29. enum id_type {
  30. RDID,
  31. RDID4,
  32. REMS,
  33. // RES1, /* TODO */
  34. RES2,
  35. NUM_ID_TYPES,
  36. };
  37. static struct {
  38. int is_cached;
  39. unsigned char bytes[4]; /* enough to hold largest ID type */
  40. } id_cache[NUM_ID_TYPES];
  41. void clear_spi_id_cache(void)
  42. {
  43. memset(id_cache, 0, sizeof(id_cache));
  44. return;
  45. }
  46. static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
  47. {
  48. static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
  49. int ret;
  50. int i;
  51. ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
  52. if (ret)
  53. return ret;
  54. msg_cspew("RDID returned");
  55. for (i = 0; i < bytes; i++)
  56. msg_cspew(" 0x%02x", readarr[i]);
  57. msg_cspew(". ");
  58. return 0;
  59. }
  60. static int spi_rems(struct flashctx *flash, unsigned char *readarr)
  61. {
  62. unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
  63. uint32_t readaddr;
  64. int ret;
  65. ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
  66. if (ret == SPI_INVALID_ADDRESS) {
  67. /* Find the lowest even address allowed for reads. */
  68. readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
  69. cmd[1] = (readaddr >> 16) & 0xff,
  70. cmd[2] = (readaddr >> 8) & 0xff,
  71. cmd[3] = (readaddr >> 0) & 0xff,
  72. ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
  73. }
  74. if (ret)
  75. return ret;
  76. msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
  77. return 0;
  78. }
  79. static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
  80. {
  81. unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
  82. uint32_t readaddr;
  83. int ret;
  84. int i;
  85. ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
  86. if (ret == SPI_INVALID_ADDRESS) {
  87. /* Find the lowest even address allowed for reads. */
  88. readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
  89. cmd[1] = (readaddr >> 16) & 0xff,
  90. cmd[2] = (readaddr >> 8) & 0xff,
  91. cmd[3] = (readaddr >> 0) & 0xff,
  92. ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
  93. }
  94. if (ret)
  95. return ret;
  96. msg_cspew("RES returned");
  97. for (i = 0; i < bytes; i++)
  98. msg_cspew(" 0x%02x", readarr[i]);
  99. msg_cspew(". ");
  100. return 0;
  101. }
  102. int spi_write_enable(struct flashctx *flash)
  103. {
  104. static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
  105. int result;
  106. /* Send WREN (Write Enable) */
  107. result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
  108. if (result)
  109. msg_cerr("%s failed\n", __func__);
  110. return result;
  111. }
  112. int spi_write_disable(struct flashctx *flash)
  113. {
  114. static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
  115. /* Send WRDI (Write Disable) */
  116. return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
  117. }
  118. static void rdid_get_ids(unsigned char *readarr,
  119. int bytes, uint32_t *id1, uint32_t *id2)
  120. {
  121. if (!oddparity(readarr[0]))
  122. msg_cdbg("RDID byte 0 parity violation. ");
  123. /* Check if this is a continuation vendor ID.
  124. * FIXME: Handle continuation device IDs.
  125. */
  126. if (readarr[0] == 0x7f) {
  127. if (!oddparity(readarr[1]))
  128. msg_cdbg("RDID byte 1 parity violation. ");
  129. *id1 = (readarr[0] << 8) | readarr[1];
  130. *id2 = readarr[2];
  131. if (bytes > 3) {
  132. *id2 <<= 8;
  133. *id2 |= readarr[3];
  134. }
  135. } else {
  136. *id1 = readarr[0];
  137. *id2 = (readarr[1] << 8) | readarr[2];
  138. }
  139. }
  140. static int compare_id(struct flashctx *flash, uint32_t id1, uint32_t id2)
  141. {
  142. msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2);
  143. if (id1 == flash->manufacture_id && id2 == flash->model_id) {
  144. /* Print the status register to tell the
  145. * user about possible write protection.
  146. */
  147. spi_prettyprint_status_register(flash);
  148. return 1;
  149. }
  150. /* Test if this is a pure vendor match. */
  151. if (id1 == flash->manufacture_id &&
  152. GENERIC_DEVICE_ID == flash->model_id)
  153. return 1;
  154. /* Test if there is any vendor ID. */
  155. if (GENERIC_MANUF_ID == flash->manufacture_id &&
  156. id1 != 0xff)
  157. return 1;
  158. return 0;
  159. }
  160. int probe_spi_rdid(struct flashctx *flash)
  161. {
  162. uint32_t id1, id2;
  163. if (!id_cache[RDID].is_cached) {
  164. if (spi_rdid(flash, id_cache[RDID].bytes, 3))
  165. return 0;
  166. id_cache[RDID].is_cached = 1;
  167. }
  168. rdid_get_ids(id_cache[RDID].bytes, 3, &id1, &id2);
  169. return compare_id(flash, id1, id2);
  170. }
  171. int probe_spi_rdid4(struct flashctx *flash)
  172. {
  173. uint32_t id1, id2;
  174. /* Some SPI controllers do not support commands with writecnt=1 and
  175. * readcnt=4.
  176. */
  177. switch (spi_programmer->type) {
  178. #if CONFIG_INTERNAL == 1
  179. #if defined(__i386__) || defined(__x86_64__)
  180. case SPI_CONTROLLER_IT87XX:
  181. case SPI_CONTROLLER_WBSIO:
  182. msg_cinfo("4 byte RDID not supported on this SPI controller\n");
  183. break;
  184. #endif
  185. #endif
  186. default:
  187. break;
  188. }
  189. if (!id_cache[RDID4].is_cached) {
  190. if (spi_rdid(flash, id_cache[RDID4].bytes, 4))
  191. return 0;
  192. id_cache[RDID4].is_cached = 1;
  193. }
  194. rdid_get_ids(id_cache[RDID4].bytes, 4, &id1, &id2);
  195. return compare_id(flash, id1, id2);
  196. }
  197. int probe_spi_rems(struct flashctx *flash)
  198. {
  199. uint32_t id1, id2;
  200. if (!id_cache[REMS].is_cached) {
  201. if (spi_rems(flash, id_cache[REMS].bytes))
  202. return 0;
  203. id_cache[REMS].is_cached = 1;
  204. }
  205. id1 = id_cache[REMS].bytes[0];
  206. id2 = id_cache[REMS].bytes[1];
  207. return compare_id(flash, id1, id2);
  208. }
  209. int probe_spi_res1(struct flashctx *flash)
  210. {
  211. static const unsigned char allff[] = {0xff, 0xff, 0xff};
  212. static const unsigned char all00[] = {0x00, 0x00, 0x00};
  213. unsigned char readarr[3];
  214. uint32_t id2;
  215. /* We only want one-byte RES if RDID and REMS are unusable. */
  216. /* Check if RDID is usable and does not return 0xff 0xff 0xff or
  217. * 0x00 0x00 0x00. In that case, RES is pointless.
  218. */
  219. if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
  220. memcmp(readarr, all00, 3)) {
  221. msg_cdbg("Ignoring RES in favour of RDID.\n");
  222. return 0;
  223. }
  224. /* Check if REMS is usable and does not return 0xff 0xff or
  225. * 0x00 0x00. In that case, RES is pointless.
  226. */
  227. if (!spi_rems(flash, readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
  228. memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
  229. msg_cdbg("Ignoring RES in favour of REMS.\n");
  230. return 0;
  231. }
  232. if (spi_res(flash, readarr, 1)) {
  233. return 0;
  234. }
  235. id2 = readarr[0];
  236. msg_cdbg("%s: id 0x%x\n", __func__, id2);
  237. if (id2 != flash->model_id)
  238. return 0;
  239. /* Print the status register to tell the
  240. * user about possible write protection.
  241. */
  242. spi_prettyprint_status_register(flash);
  243. return 1;
  244. }
  245. int probe_spi_res2(struct flashctx *flash)
  246. {
  247. uint32_t id1, id2;
  248. if (!id_cache[RES2].is_cached) {
  249. if (spi_res(flash, id_cache[RES2].bytes, 2))
  250. return 0;
  251. id_cache[RES2].is_cached = 1;
  252. }
  253. id1 = id_cache[RES2].bytes[0];
  254. id2 = id_cache[RES2].bytes[1];
  255. msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
  256. if (id1 != flash->manufacture_id || id2 != flash->model_id)
  257. return 0;
  258. /* Print the status register to tell the
  259. * user about possible write protection.
  260. */
  261. spi_prettyprint_status_register(flash);
  262. return 1;
  263. }
  264. uint8_t spi_read_status_register(const struct flashctx *flash)
  265. {
  266. static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
  267. /* FIXME: No workarounds for driver/hardware bugs in generic code. */
  268. unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
  269. int ret;
  270. /* Read Status Register */
  271. if (flash->read_status)
  272. ret = flash->read_status(flash);
  273. else
  274. ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
  275. if (ret)
  276. msg_cerr("RDSR failed!\n");
  277. return readarr[0];
  278. }
  279. /* Prettyprint the status register. Common definitions. */
  280. void spi_prettyprint_status_register_welwip(uint8_t status)
  281. {
  282. msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
  283. "%sset\n", (status & (1 << 1)) ? "" : "not ");
  284. msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
  285. "%sset\n", (status & (1 << 0)) ? "" : "not ");
  286. }
  287. /* Prettyprint the status register. Common definitions. */
  288. void spi_prettyprint_status_register_bp3210(uint8_t status, int bp)
  289. {
  290. switch (bp) {
  291. /* Fall through. */
  292. case 3:
  293. msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) "
  294. "is %sset\n", (status & (1 << 5)) ? "" : "not ");
  295. case 2:
  296. msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) "
  297. "is %sset\n", (status & (1 << 4)) ? "" : "not ");
  298. case 1:
  299. msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) "
  300. "is %sset\n", (status & (1 << 3)) ? "" : "not ");
  301. case 0:
  302. msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) "
  303. "is %sset\n", (status & (1 << 2)) ? "" : "not ");
  304. }
  305. }
  306. /* Prettyprint the status register. Unnamed bits. */
  307. void spi_prettyprint_status_register_bit(uint8_t status, int bit)
  308. {
  309. msg_cdbg("Chip status register: Bit %i "
  310. "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
  311. }
  312. static void spi_prettyprint_status_register_common(uint8_t status)
  313. {
  314. spi_prettyprint_status_register_bp3210(status, 3);
  315. spi_prettyprint_status_register_welwip(status);
  316. }
  317. /* Prettyprint the status register. Works for
  318. * ST M25P series
  319. * MX MX25L series
  320. */
  321. void spi_prettyprint_status_register_st_m25p(uint8_t status)
  322. {
  323. msg_cdbg("Chip status register: Status Register Write Disable "
  324. "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
  325. msg_cdbg("Chip status register: Bit 6 is "
  326. "%sset\n", (status & (1 << 6)) ? "" : "not ");
  327. spi_prettyprint_status_register_common(status);
  328. }
  329. void spi_prettyprint_status_register_sst25(uint8_t status)
  330. {
  331. msg_cdbg("Chip status register: Block Protect Write Disable "
  332. "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
  333. msg_cdbg("Chip status register: Auto Address Increment Programming "
  334. "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
  335. spi_prettyprint_status_register_common(status);
  336. }
  337. /* Prettyprint the status register. Works for
  338. * SST 25VF016
  339. */
  340. void spi_prettyprint_status_register_sst25vf016(uint8_t status)
  341. {
  342. static const char *const bpt[] = {
  343. "none",
  344. "1F0000H-1FFFFFH",
  345. "1E0000H-1FFFFFH",
  346. "1C0000H-1FFFFFH",
  347. "180000H-1FFFFFH",
  348. "100000H-1FFFFFH",
  349. "all", "all"
  350. };
  351. spi_prettyprint_status_register_sst25(status);
  352. msg_cdbg("Resulting block protection : %s\n",
  353. bpt[(status & 0x1c) >> 2]);
  354. }
  355. void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
  356. {
  357. static const char *const bpt[] = {
  358. "none",
  359. "0x70000-0x7ffff",
  360. "0x60000-0x7ffff",
  361. "0x40000-0x7ffff",
  362. "all blocks", "all blocks", "all blocks", "all blocks"
  363. };
  364. spi_prettyprint_status_register_sst25(status);
  365. msg_cdbg("Resulting block protection : %s\n",
  366. bpt[(status & 0x1c) >> 2]);
  367. }
  368. int spi_prettyprint_status_register(struct flashctx *flash)
  369. {
  370. uint8_t status;
  371. status = spi_read_status_register(flash);
  372. msg_cdbg("Chip status register is %02x\n", status);
  373. switch (flash->manufacture_id) {
  374. case ST_ID:
  375. if (((flash->model_id & 0xff00) == 0x2000) ||
  376. ((flash->model_id & 0xff00) == 0x2500))
  377. spi_prettyprint_status_register_st_m25p(status);
  378. break;
  379. case MACRONIX_ID:
  380. if ((flash->model_id & 0xff00) == 0x2000)
  381. spi_prettyprint_status_register_st_m25p(status);
  382. break;
  383. case SST_ID:
  384. switch (flash->model_id) {
  385. case 0x2541:
  386. spi_prettyprint_status_register_sst25vf016(status);
  387. break;
  388. case 0x8d:
  389. case 0x258d:
  390. spi_prettyprint_status_register_sst25vf040b(status);
  391. break;
  392. default:
  393. spi_prettyprint_status_register_sst25(status);
  394. break;
  395. }
  396. break;
  397. }
  398. return 0;
  399. }
  400. int spi_chip_erase_60(struct flashctx *flash)
  401. {
  402. int result;
  403. struct spi_command cmds[] = {
  404. {
  405. .writecnt = JEDEC_WREN_OUTSIZE,
  406. .writearr = (const unsigned char[]){ JEDEC_WREN },
  407. .readcnt = 0,
  408. .readarr = NULL,
  409. }, {
  410. .writecnt = JEDEC_CE_60_OUTSIZE,
  411. .writearr = (const unsigned char[]){ JEDEC_CE_60 },
  412. .readcnt = 0,
  413. .readarr = NULL,
  414. }, {
  415. .writecnt = 0,
  416. .writearr = NULL,
  417. .readcnt = 0,
  418. .readarr = NULL,
  419. }};
  420. result = spi_send_multicommand(flash, cmds);
  421. if (result) {
  422. msg_cerr("%s failed during command execution\n",
  423. __func__);
  424. return result;
  425. }
  426. /* Wait until the Write-In-Progress bit is cleared.
  427. * This usually takes 1-85 s, so wait in 1 s steps.
  428. */
  429. /* FIXME: We assume spi_read_status_register will never fail. */
  430. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  431. programmer_delay(1000 * 1000);
  432. /* FIXME: Check the status register for errors. */
  433. return 0;
  434. }
  435. int spi_chip_erase_c7(struct flashctx *flash)
  436. {
  437. int result;
  438. struct spi_command cmds[] = {
  439. {
  440. .writecnt = JEDEC_WREN_OUTSIZE,
  441. .writearr = (const unsigned char[]){ JEDEC_WREN },
  442. .readcnt = 0,
  443. .readarr = NULL,
  444. }, {
  445. .writecnt = JEDEC_CE_C7_OUTSIZE,
  446. .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
  447. .readcnt = 0,
  448. .readarr = NULL,
  449. }, {
  450. .writecnt = 0,
  451. .writearr = NULL,
  452. .readcnt = 0,
  453. .readarr = NULL,
  454. }};
  455. result = spi_send_multicommand(flash, cmds);
  456. if (result) {
  457. msg_cerr("%s failed during command execution\n", __func__);
  458. return result;
  459. }
  460. /* Wait until the Write-In-Progress bit is cleared.
  461. * This usually takes 1-85 s, so wait in 1 s steps.
  462. */
  463. /* FIXME: We assume spi_read_status_register will never fail. */
  464. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  465. programmer_delay(1000 * 1000);
  466. /* FIXME: Check the status register for errors. */
  467. return 0;
  468. }
  469. int spi_block_erase_52(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  470. {
  471. int result;
  472. struct spi_command cmds[] = {
  473. {
  474. .writecnt = JEDEC_WREN_OUTSIZE,
  475. .writearr = (const unsigned char[]){ JEDEC_WREN },
  476. .readcnt = 0,
  477. .readarr = NULL,
  478. }, {
  479. .writecnt = JEDEC_BE_52_OUTSIZE,
  480. .writearr = (const unsigned char[]){
  481. JEDEC_BE_52,
  482. (addr >> 16) & 0xff,
  483. (addr >> 8) & 0xff,
  484. (addr & 0xff)
  485. },
  486. .readcnt = 0,
  487. .readarr = NULL,
  488. }, {
  489. .writecnt = 0,
  490. .writearr = NULL,
  491. .readcnt = 0,
  492. .readarr = NULL,
  493. }};
  494. result = spi_send_multicommand(flash, cmds);
  495. if (result) {
  496. msg_cerr("%s failed during command execution at address 0x%x\n",
  497. __func__, addr);
  498. return result;
  499. }
  500. /* Wait until the Write-In-Progress bit is cleared.
  501. * This usually takes 100-4000 ms, so wait in 100 ms steps.
  502. */
  503. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  504. programmer_delay(100 * 1000);
  505. /* FIXME: Check the status register for errors. */
  506. return 0;
  507. }
  508. /* Block size is usually
  509. * 64k for Macronix
  510. * 32k for SST
  511. * 4-32k non-uniform for EON
  512. */
  513. int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  514. {
  515. int result;
  516. struct spi_command cmds[] = {
  517. {
  518. .writecnt = JEDEC_WREN_OUTSIZE,
  519. .writearr = (const unsigned char[]){ JEDEC_WREN },
  520. .readcnt = 0,
  521. .readarr = NULL,
  522. }, {
  523. .writecnt = JEDEC_BE_D8_OUTSIZE,
  524. .writearr = (const unsigned char[]){
  525. JEDEC_BE_D8,
  526. (addr >> 16) & 0xff,
  527. (addr >> 8) & 0xff,
  528. (addr & 0xff)
  529. },
  530. .readcnt = 0,
  531. .readarr = NULL,
  532. }, {
  533. .writecnt = 0,
  534. .writearr = NULL,
  535. .readcnt = 0,
  536. .readarr = NULL,
  537. }};
  538. result = spi_send_multicommand(flash, cmds);
  539. if (result) {
  540. msg_cerr("%s failed during command execution at address 0x%x\n",
  541. __func__, addr);
  542. return result;
  543. }
  544. /* Wait until the Write-In-Progress bit is cleared.
  545. * This usually takes 100-4000 ms, so wait in 100 ms steps.
  546. */
  547. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  548. programmer_delay(100 * 1000);
  549. /* FIXME: Check the status register for errors. */
  550. return 0;
  551. }
  552. /* Block size is usually
  553. * 4k for PMC
  554. */
  555. int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  556. {
  557. int result;
  558. struct spi_command cmds[] = {
  559. {
  560. .writecnt = JEDEC_WREN_OUTSIZE,
  561. .writearr = (const unsigned char[]){ JEDEC_WREN },
  562. .readcnt = 0,
  563. .readarr = NULL,
  564. }, {
  565. .writecnt = JEDEC_BE_D7_OUTSIZE,
  566. .writearr = (const unsigned char[]){
  567. JEDEC_BE_D7,
  568. (addr >> 16) & 0xff,
  569. (addr >> 8) & 0xff,
  570. (addr & 0xff)
  571. },
  572. .readcnt = 0,
  573. .readarr = NULL,
  574. }, {
  575. .writecnt = 0,
  576. .writearr = NULL,
  577. .readcnt = 0,
  578. .readarr = NULL,
  579. }};
  580. result = spi_send_multicommand(flash, cmds);
  581. if (result) {
  582. msg_cerr("%s failed during command execution at address 0x%x\n",
  583. __func__, addr);
  584. return result;
  585. }
  586. /* Wait until the Write-In-Progress bit is cleared.
  587. * This usually takes 100-4000 ms, so wait in 100 ms steps.
  588. */
  589. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  590. programmer_delay(100 * 1000);
  591. /* FIXME: Check the status register for errors. */
  592. return 0;
  593. }
  594. /* Sector size is usually 4k, though Macronix eliteflash has 64k */
  595. int spi_block_erase_20(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  596. {
  597. int result;
  598. struct spi_command cmds[] = {
  599. {
  600. .writecnt = JEDEC_WREN_OUTSIZE,
  601. .writearr = (const unsigned char[]){ JEDEC_WREN },
  602. .readcnt = 0,
  603. .readarr = NULL,
  604. }, {
  605. .writecnt = JEDEC_SE_OUTSIZE,
  606. .writearr = (const unsigned char[]){
  607. JEDEC_SE,
  608. (addr >> 16) & 0xff,
  609. (addr >> 8) & 0xff,
  610. (addr & 0xff)
  611. },
  612. .readcnt = 0,
  613. .readarr = NULL,
  614. }, {
  615. .writecnt = 0,
  616. .writearr = NULL,
  617. .readcnt = 0,
  618. .readarr = NULL,
  619. }};
  620. result = spi_send_multicommand(flash, cmds);
  621. if (result) {
  622. msg_cerr("%s failed during command execution at address 0x%x\n",
  623. __func__, addr);
  624. return result;
  625. }
  626. /* Wait until the Write-In-Progress bit is cleared.
  627. * This usually takes 15-800 ms, so wait in 10 ms steps.
  628. */
  629. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  630. programmer_delay(10 * 1000);
  631. /* FIXME: Check the status register for errors. */
  632. return 0;
  633. }
  634. int spi_block_erase_60(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  635. {
  636. if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
  637. msg_cerr("%s called with incorrect arguments\n",
  638. __func__);
  639. return -1;
  640. }
  641. return spi_chip_erase_60(flash);
  642. }
  643. int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
  644. {
  645. if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
  646. msg_cerr("%s called with incorrect arguments\n",
  647. __func__);
  648. return -1;
  649. }
  650. return spi_chip_erase_c7(flash);
  651. }
  652. int spi_write_status_enable(struct flashctx *flash)
  653. {
  654. static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
  655. int result;
  656. /* Send EWSR (Enable Write Status Register). */
  657. result = spi_send_command(flash, sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
  658. if (result)
  659. msg_cerr("%s failed\n", __func__);
  660. return result;
  661. }
  662. /*
  663. * This is according the SST25VF016 datasheet, who knows it is more
  664. * generic that this...
  665. */
  666. static int spi_write_status_register_ewsr(const struct flashctx *flash, int status)
  667. {
  668. int result;
  669. int i = 0;
  670. struct spi_command cmds[] = {
  671. {
  672. /* WRSR requires either EWSR or WREN depending on chip type. */
  673. .writecnt = JEDEC_EWSR_OUTSIZE,
  674. .writearr = (const unsigned char[]){ JEDEC_EWSR },
  675. .readcnt = 0,
  676. .readarr = NULL,
  677. }, {
  678. .writecnt = JEDEC_WRSR_OUTSIZE,
  679. .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
  680. .readcnt = 0,
  681. .readarr = NULL,
  682. }, {
  683. .writecnt = 0,
  684. .writearr = NULL,
  685. .readcnt = 0,
  686. .readarr = NULL,
  687. }};
  688. result = spi_send_multicommand(flash, cmds);
  689. if (result) {
  690. msg_cerr("%s failed during command execution\n",
  691. __func__);
  692. /* No point in waiting for the command to complete if execution
  693. * failed.
  694. */
  695. return result;
  696. }
  697. /* WRSR performs a self-timed erase before the changes take effect.
  698. * This may take 50-85 ms in most cases, and some chips apparently
  699. * allow running RDSR only once. Therefore pick an initial delay of
  700. * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
  701. */
  702. programmer_delay(100 * 1000);
  703. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP) {
  704. if (++i > 490) {
  705. msg_cerr("Error: WIP bit after WRSR never cleared\n");
  706. return TIMEOUT_ERROR;
  707. }
  708. programmer_delay(10 * 1000);
  709. }
  710. return 0;
  711. }
  712. int spi_write_status_register_wren(const struct flashctx *flash, int status)
  713. {
  714. int result;
  715. int i = 0;
  716. struct spi_command cmds[] = {
  717. {
  718. /* WRSR requires either EWSR or WREN depending on chip type. */
  719. .writecnt = JEDEC_WREN_OUTSIZE,
  720. .writearr = (const unsigned char[]){ JEDEC_WREN },
  721. .readcnt = 0,
  722. .readarr = NULL,
  723. }, {
  724. .writecnt = JEDEC_WRSR_OUTSIZE,
  725. .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
  726. .readcnt = 0,
  727. .readarr = NULL,
  728. }, {
  729. .writecnt = 0,
  730. .writearr = NULL,
  731. .readcnt = 0,
  732. .readarr = NULL,
  733. }};
  734. result = spi_send_multicommand(flash, cmds);
  735. if (result) {
  736. msg_cerr("%s failed during command execution\n",
  737. __func__);
  738. /* No point in waiting for the command to complete if execution
  739. * failed.
  740. */
  741. return result;
  742. }
  743. /* WRSR performs a self-timed erase before the changes take effect.
  744. * This may take 50-85 ms in most cases, and some chips apparently
  745. * allow running RDSR only once. Therefore pick an initial delay of
  746. * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
  747. */
  748. programmer_delay(100 * 1000);
  749. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP) {
  750. if (++i > 490) {
  751. msg_cerr("Error: WIP bit after WRSR never cleared\n");
  752. return TIMEOUT_ERROR;
  753. }
  754. programmer_delay(10 * 1000);
  755. }
  756. return 0;
  757. }
  758. int spi_write_status_register(const struct flashctx *flash, int status)
  759. {
  760. int ret = 1;
  761. if (flash->write_status) {
  762. ret = flash->write_status(flash, status);
  763. } else {
  764. if (flash->feature_bits & FEATURE_WRSR_WREN)
  765. ret = spi_write_status_register_wren(flash, status);
  766. if (ret && (flash->feature_bits & FEATURE_WRSR_EWSR))
  767. ret = spi_write_status_register_ewsr(flash, status);
  768. }
  769. return ret;
  770. }
  771. int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
  772. {
  773. int result;
  774. struct spi_command cmds[] = {
  775. {
  776. .writecnt = JEDEC_WREN_OUTSIZE,
  777. .writearr = (const unsigned char[]){ JEDEC_WREN },
  778. .readcnt = 0,
  779. .readarr = NULL,
  780. }, {
  781. .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
  782. .writearr = (const unsigned char[]){
  783. JEDEC_BYTE_PROGRAM,
  784. (addr >> 16) & 0xff,
  785. (addr >> 8) & 0xff,
  786. (addr & 0xff),
  787. databyte
  788. },
  789. .readcnt = 0,
  790. .readarr = NULL,
  791. }, {
  792. .writecnt = 0,
  793. .writearr = NULL,
  794. .readcnt = 0,
  795. .readarr = NULL,
  796. }};
  797. result = spi_send_multicommand(flash, cmds);
  798. if (result) {
  799. msg_cerr("%s failed during command execution at address 0x%x\n",
  800. __func__, addr);
  801. }
  802. return result;
  803. }
  804. int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes, unsigned int len)
  805. {
  806. int result;
  807. /* FIXME: Switch to malloc based on len unless that kills speed. */
  808. unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
  809. JEDEC_BYTE_PROGRAM,
  810. (addr >> 16) & 0xff,
  811. (addr >> 8) & 0xff,
  812. (addr >> 0) & 0xff,
  813. };
  814. struct spi_command cmds[] = {
  815. {
  816. .writecnt = JEDEC_WREN_OUTSIZE,
  817. .writearr = (const unsigned char[]){ JEDEC_WREN },
  818. .readcnt = 0,
  819. .readarr = NULL,
  820. }, {
  821. .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
  822. .writearr = cmd,
  823. .readcnt = 0,
  824. .readarr = NULL,
  825. }, {
  826. .writecnt = 0,
  827. .writearr = NULL,
  828. .readcnt = 0,
  829. .readarr = NULL,
  830. }};
  831. if (!len) {
  832. msg_cerr("%s called for zero-length write\n", __func__);
  833. return 1;
  834. }
  835. if (len > 256) {
  836. msg_cerr("%s called for too long a write\n", __func__);
  837. return 1;
  838. }
  839. memcpy(&cmd[4], bytes, len);
  840. result = spi_send_multicommand(flash, cmds);
  841. if (result) {
  842. if (result != SPI_ACCESS_DENIED) {
  843. msg_cerr("%s failed during command execution at address 0x%x\n",
  844. __func__, addr);
  845. }
  846. }
  847. return result;
  848. }
  849. int spi_restore_status(struct flashctx *flash, uint8_t status)
  850. {
  851. msg_cdbg("restoring chip status (0x%02x)\n", status);
  852. return spi_write_status_register(flash, status);
  853. }
  854. /* A generic brute-force block protection disable works like this:
  855. * Write 0x00 to the status register. Check if any locks are still set (that
  856. * part is chip specific). Repeat once.
  857. */
  858. int spi_disable_blockprotect(struct flashctx *flash)
  859. {
  860. uint8_t status;
  861. int result;
  862. status = spi_read_status_register(flash);
  863. /* If block protection is disabled, stop here. */
  864. if ((status & 0x3c) == 0)
  865. return 0;
  866. /* restore status register content upon exit */
  867. register_chip_restore(spi_restore_status, flash, status);
  868. msg_cdbg("Some block protection in effect, disabling\n");
  869. result = spi_write_status_register(flash, status & ~0x3c);
  870. if (result) {
  871. msg_cerr("spi_write_status_register failed\n");
  872. return result;
  873. }
  874. status = spi_read_status_register(flash);
  875. if ((status & 0x3c) != 0) {
  876. msg_cerr("Block protection could not be disabled!\n");
  877. return 1;
  878. }
  879. return 0;
  880. }
  881. int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, unsigned int len)
  882. {
  883. const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
  884. JEDEC_READ,
  885. (address >> 16) & 0xff,
  886. (address >> 8) & 0xff,
  887. (address >> 0) & 0xff,
  888. };
  889. /* Send Read */
  890. return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
  891. }
  892. /*
  893. * Read a part of the flash chip.
  894. * FIXME: Use the chunk code from Michael Karcher instead.
  895. * Each page is read separately in chunks with a maximum size of chunksize.
  896. */
  897. int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
  898. {
  899. int rc = 0, chunk_status = 0;
  900. unsigned int i, j, starthere, lenhere, toread;
  901. unsigned int page_size = flash->page_size;
  902. /* Warning: This loop has a very unusual condition and body.
  903. * The loop needs to go through each page with at least one affected
  904. * byte. The lowest page number is (start / page_size) since that
  905. * division rounds down. The highest page number we want is the page
  906. * where the last byte of the range lives. That last byte has the
  907. * address (start + len - 1), thus the highest page number is
  908. * (start + len - 1) / page_size. Since we want to include that last
  909. * page as well, the loop condition uses <=.
  910. */
  911. for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
  912. /* Byte position of the first byte in the range in this page. */
  913. /* starthere is an offset to the base address of the chip. */
  914. starthere = max(start, i * page_size);
  915. /* Length of bytes in the range in this page. */
  916. lenhere = min(start + len, (i + 1) * page_size) - starthere;
  917. for (j = 0; j < lenhere; j += chunksize) {
  918. toread = min(chunksize, lenhere - j);
  919. chunk_status = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
  920. if (chunk_status) {
  921. if (ignore_error(chunk_status)) {
  922. /* fill this chunk with 0xff bytes and
  923. let caller know about the error */
  924. memset(buf + starthere - start + j, 0xff, toread);
  925. rc = chunk_status;
  926. chunk_status = 0;
  927. continue;
  928. } else {
  929. rc = chunk_status;
  930. break;
  931. }
  932. }
  933. }
  934. if (chunk_status)
  935. break;
  936. }
  937. return rc;
  938. }
  939. /*
  940. * Read a part of the flash chip.
  941. * Ignore pages and read the data continuously, the only bound is the chunksize.
  942. */
  943. int spi_read_unbound(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
  944. {
  945. int rc = 0;
  946. unsigned int i;
  947. for (i = start; i < (start + len); i += chunksize) {
  948. int chunk_status = 0;
  949. unsigned int toread = min(chunksize, start + len - i);
  950. chunk_status = spi_nbyte_read(flash, i, buf + (i - start), toread);
  951. if (chunk_status) {
  952. if (ignore_error(chunk_status)) {
  953. /* fill this chunk with 0xff bytes and
  954. let caller know about the error */
  955. memset(buf + (i - start), 0xff, toread);
  956. rc = chunk_status;
  957. continue;
  958. } else {
  959. rc = chunk_status;
  960. break;
  961. }
  962. }
  963. }
  964. return rc;
  965. }
  966. /*
  967. * Write a part of the flash chip.
  968. * FIXME: Use the chunk code from Michael Karcher instead.
  969. * Each page is written separately in chunks with a maximum size of chunksize.
  970. */
  971. int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
  972. {
  973. int rc = 0;
  974. unsigned int i, j, starthere, lenhere, towrite;
  975. /* FIXME: page_size is the wrong variable. We need max_writechunk_size
  976. * in struct flashctx to do this properly. All chips using
  977. * spi_chip_write_256 have page_size set to max_writechunk_size, so
  978. * we're OK for now.
  979. */
  980. unsigned int page_size = flash->page_size;
  981. /* Warning: This loop has a very unusual condition and body.
  982. * The loop needs to go through each page with at least one affected
  983. * byte. The lowest page number is (start / page_size) since that
  984. * division rounds down. The highest page number we want is the page
  985. * where the last byte of the range lives. That last byte has the
  986. * address (start + len - 1), thus the highest page number is
  987. * (start + len - 1) / page_size. Since we want to include that last
  988. * page as well, the loop condition uses <=.
  989. */
  990. for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
  991. /* Byte position of the first byte in the range in this page. */
  992. /* starthere is an offset to the base address of the chip. */
  993. starthere = max(start, i * page_size);
  994. /* Length of bytes in the range in this page. */
  995. lenhere = min(start + len, (i + 1) * page_size) - starthere;
  996. for (j = 0; j < lenhere; j += chunksize) {
  997. towrite = min(chunksize, lenhere - j);
  998. rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
  999. if (rc)
  1000. break;
  1001. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  1002. programmer_delay(10);
  1003. }
  1004. if (rc)
  1005. break;
  1006. }
  1007. return rc;
  1008. }
  1009. /*
  1010. * Program chip using byte programming. (SLOW!)
  1011. * This is for chips which can only handle one byte writes
  1012. * and for chips where memory mapped programming is impossible
  1013. * (e.g. due to size constraints in IT87* for over 512 kB)
  1014. */
  1015. /* real chunksize is 1, logical chunksize is 1 */
  1016. int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  1017. {
  1018. unsigned int i;
  1019. int result = 0;
  1020. for (i = start; i < start + len; i++) {
  1021. result = spi_byte_program(flash, i, buf[i - start]);
  1022. if (result)
  1023. return 1;
  1024. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  1025. programmer_delay(10);
  1026. }
  1027. return 0;
  1028. }
  1029. int spi_aai_write(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
  1030. {
  1031. uint32_t pos = start;
  1032. int result;
  1033. unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
  1034. JEDEC_AAI_WORD_PROGRAM,
  1035. };
  1036. struct spi_command cmds[] = {
  1037. {
  1038. .writecnt = JEDEC_WREN_OUTSIZE,
  1039. .writearr = (const unsigned char[]){ JEDEC_WREN },
  1040. .readcnt = 0,
  1041. .readarr = NULL,
  1042. }, {
  1043. .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
  1044. .writearr = (const unsigned char[]){
  1045. JEDEC_AAI_WORD_PROGRAM,
  1046. (start >> 16) & 0xff,
  1047. (start >> 8) & 0xff,
  1048. (start & 0xff),
  1049. buf[0],
  1050. buf[1]
  1051. },
  1052. .readcnt = 0,
  1053. .readarr = NULL,
  1054. }, {
  1055. .writecnt = 0,
  1056. .writearr = NULL,
  1057. .readcnt = 0,
  1058. .readarr = NULL,
  1059. }};
  1060. switch (spi_programmer->type) {
  1061. #if CONFIG_INTERNAL == 1
  1062. #if defined(__i386__) || defined(__x86_64__)
  1063. case SPI_CONTROLLER_IT87XX:
  1064. case SPI_CONTROLLER_WBSIO:
  1065. msg_perr("%s: impossible with this SPI controller,"
  1066. " degrading to byte program\n", __func__);
  1067. return spi_chip_write_1(flash, buf, start, len);
  1068. #endif
  1069. #endif
  1070. default:
  1071. break;
  1072. }
  1073. /* The even start address and even length requirements can be either
  1074. * honored outside this function, or we can call spi_byte_program
  1075. * for the first and/or last byte and use AAI for the rest.
  1076. * FIXME: Move this to generic code.
  1077. */
  1078. /* The data sheet requires a start address with the low bit cleared. */
  1079. if (start % 2) {
  1080. msg_cerr("%s: start address not even! Please report a bug at "
  1081. "flashrom@flashrom.org\n", __func__);
  1082. if (spi_chip_write_1(flash, buf, start, start % 2))
  1083. return SPI_GENERIC_ERROR;
  1084. pos += start % 2;
  1085. cmds[1].writearr = (const unsigned char[]){
  1086. JEDEC_AAI_WORD_PROGRAM,
  1087. (pos >> 16) & 0xff,
  1088. (pos >> 8) & 0xff,
  1089. (pos & 0xff),
  1090. buf[pos - start],
  1091. buf[pos - start + 1]
  1092. };
  1093. /* Do not return an error for now. */
  1094. //return SPI_GENERIC_ERROR;
  1095. }
  1096. /* The data sheet requires total AAI write length to be even. */
  1097. if (len % 2) {
  1098. msg_cerr("%s: total write length not even! Please report a "
  1099. "bug at flashrom@flashrom.org\n", __func__);
  1100. /* Do not return an error for now. */
  1101. //return SPI_GENERIC_ERROR;
  1102. }
  1103. result = spi_send_multicommand(flash, cmds);
  1104. if (result) {
  1105. msg_cerr("%s failed during start command execution\n",
  1106. __func__);
  1107. /* FIXME: Should we send WRDI here as well to make sure the chip
  1108. * is not in AAI mode?
  1109. */
  1110. return result;
  1111. }
  1112. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  1113. programmer_delay(10);
  1114. /* We already wrote 2 bytes in the multicommand step. */
  1115. pos += 2;
  1116. /* Are there at least two more bytes to write? */
  1117. while (pos < start + len - 1) {
  1118. cmd[1] = buf[pos++ - start];
  1119. cmd[2] = buf[pos++ - start];
  1120. spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
  1121. while (spi_read_status_register(flash) & JEDEC_RDSR_BIT_WIP)
  1122. programmer_delay(10);
  1123. }
  1124. /* Use WRDI to exit AAI mode. This needs to be done before issuing any
  1125. * other non-AAI command.
  1126. */
  1127. spi_write_disable(flash);
  1128. /* Write remaining byte (if any). */
  1129. if (pos < start + len) {
  1130. if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
  1131. return SPI_GENERIC_ERROR;
  1132. pos += pos % 2;
  1133. }
  1134. return 0;
  1135. }