spiflash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. #include "compiler.h"
  2. #include "zlib.h"
  3. #include "spiflash.h"
  4. #if 1
  5. # include "console.h"
  6. # define MSG(...) con_printf(__VA_ARGS__)
  7. #else
  8. # define MSG(...) ((void)0)
  9. #endif
  10. struct spz_stream;
  11. typedef struct spz_stream spz_stream;
  12. struct spz_stream {
  13. z_stream zs;
  14. const struct spiflash *flash;
  15. int (*read_data)(spz_stream *); /* Routine to get more data */
  16. int (*end_data)(z_stream *); /* Termination routine for zlib */
  17. uint8_t *optr; /* Output data pointer into obuf */
  18. /* Note: available output data ends at zs->next_out */
  19. uint8_t *ibuf; /* Input buffer if compressed */
  20. uint8_t *obuf; /* Output buffer */
  21. uint8_t *dbuf; /* Block data buffer */
  22. uint8_t *vbuf; /* Readback/verify buffer */
  23. const struct spiflash_header *header;
  24. uint32_t crc32; /* Input data CRC32 */
  25. unsigned int input_left; /* Input data unread */
  26. int err; /* Error code to return */
  27. bool eoi; /* Reached end of input */
  28. bool free_header; /* header is malloc()'d */
  29. };
  30. static void *spz_malloc(spz_stream *spz, size_t bytes)
  31. {
  32. void *p = malloc(bytes);
  33. if (!p && !spz->err) {
  34. spz->err = Z_MEM_ERROR;
  35. }
  36. return p;
  37. }
  38. static int spiflash_read_data(spz_stream *spz)
  39. {
  40. int rv;
  41. int (*read_data)(void *, void *, unsigned int);
  42. unsigned int read_block_size;
  43. if (spz->eoi || spz->err)
  44. return 0;
  45. read_data = spz->flash->ops->read_data;
  46. if (!spz->input_left || !read_data) {
  47. spz->eoi = true;
  48. return 0;
  49. }
  50. read_block_size = min(spz->input_left, SPIFLASH_BLOCK_SIZE);
  51. if (!spz->ibuf) {
  52. spz->ibuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  53. if (!spz->ibuf) {
  54. spz->eoi = true;
  55. return 0;
  56. }
  57. }
  58. spz->zs.next_in = spz->ibuf;
  59. spz->zs.avail_in = 0;
  60. rv = read_data(spz->flash->cookie, spz->ibuf, read_block_size);
  61. if (spz->err) {
  62. rv = 0;
  63. } else if (rv < 0) {
  64. spz->err = rv;
  65. rv = 0;
  66. }
  67. if (rv != (int)read_block_size)
  68. spz->eoi = true;
  69. if (rv) {
  70. spz->crc32 = crc32(spz->crc32, spz->ibuf, rv);
  71. spz->input_left -= rv;
  72. if (!spz->input_left) {
  73. if (spz->crc32 != spz->header->crc32) {
  74. spz->err = Z_STREAM_ERROR;
  75. rv = 0;
  76. }
  77. }
  78. }
  79. return spz->zs.avail_in = rv;
  80. }
  81. static int read_data_raw(spz_stream *spz)
  82. {
  83. int rlen;
  84. if (spz->eoi)
  85. return 0;
  86. rlen = spiflash_read_data(spz);
  87. if (rlen) {
  88. spz->optr = spz->ibuf;
  89. spz->zs.next_out = spz->ibuf + rlen;
  90. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE - rlen;
  91. }
  92. return rlen;
  93. }
  94. static int read_data_inflate(spz_stream *spz)
  95. {
  96. int rv = Z_STREAM_END;
  97. spz->optr = spz->zs.next_out = spz->obuf;
  98. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE;
  99. while (spz->zs.avail_out) {
  100. if (!spz->zs.avail_in && !spz->eoi) {
  101. int rlen = spiflash_read_data(spz);
  102. spz->zs.next_in = spz->ibuf;
  103. spz->zs.avail_in = rlen;
  104. }
  105. rv = inflate(&spz->zs, Z_SYNC_FLUSH);
  106. if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
  107. continue;
  108. spz->eoi = true;
  109. if (rv != Z_STREAM_END && !spz->err)
  110. spz->err = rv;
  111. break;
  112. }
  113. return spz->zs.next_out - spz->optr;
  114. }
  115. /*
  116. * spz needs to be initialized to zero except the flash, zs.next_in,
  117. * and zs.avail_in fields.
  118. *
  119. * Returns Z_STREAM_END on end of data.
  120. */
  121. static int spiflash_data_init(spz_stream *spz)
  122. {
  123. int rv = Z_OK;
  124. uint8_t *rdbuf = NULL;
  125. int rlen;
  126. int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
  127. uint32_t header_crc;
  128. MSG("update: ");
  129. spz->dbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  130. spz->vbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  131. if (!spz->dbuf || !spz->vbuf)
  132. goto err;
  133. rlen = spz->zs.avail_in;
  134. spz->header = (void *)spz->zs.next_in;
  135. read_data = spz->flash->ops->read_data;
  136. spz->eoi = !read_data;
  137. if (!rlen && read_data) {
  138. struct spiflash_header *header;
  139. spz->header = header = spz_malloc(spz, sizeof *spz->header);
  140. spz->free_header = true;
  141. spz->input_left = UINT_MAX; /* Unknown at this point */
  142. rlen = read_data(spz->flash->cookie, header, sizeof *header);
  143. }
  144. if (spz->err)
  145. goto err;
  146. if (!rlen) {
  147. MSG("done");
  148. spz->err = Z_STREAM_END;
  149. goto not_err;
  150. }
  151. if (rlen < (int)sizeof *spz->header) {
  152. MSG("input underrun");
  153. spz->err = Z_STREAM_ERROR;
  154. goto err;
  155. }
  156. rlen -= sizeof *spz->header;
  157. spz->zs.next_in += sizeof *spz->header;
  158. /*
  159. * Check header magic and CRC
  160. */
  161. if (spz->header->magic != SPIFLASH_MAGIC) {
  162. MSG("bad header magic");
  163. spz->err = Z_STREAM_ERROR;
  164. goto err;
  165. }
  166. header_crc = crc32(0, NULL, 0);
  167. header_crc = crc32(header_crc, (const void *)spz->header,
  168. sizeof *spz->header - 4);
  169. if (header_crc != spz->header->header_crc32) {
  170. MSG("header CRC error (0x%08x vs 0x%08x)...",
  171. header_crc, spz->header->header_crc32);
  172. spz->err = Z_STREAM_ERROR;
  173. goto err;
  174. }
  175. if (!spz->header->dlen) {
  176. /* End of data */
  177. spz->err = Z_STREAM_END;
  178. goto not_err;
  179. }
  180. if (spz->header->zlen > spz->header->dlen ||
  181. (spz->eoi && (int)spz->header->zlen > rlen)) {
  182. spz->err = Z_STREAM_ERROR;
  183. goto err;
  184. }
  185. MSG("data 0x%06x..0x%06x (%u bytes)\n",
  186. spz->header->address, spz->header->address + spz->header->dlen - 1,
  187. spz->header->dlen);
  188. if (rlen > (int)spz->header->zlen)
  189. rlen = spz->header->zlen;
  190. spz->zs.avail_in = rlen;
  191. spz->crc32 = crc32(0, NULL, 0);
  192. if (rlen) {
  193. /* Received data in input buffer already */
  194. spz->crc32 = crc32(spz->crc32, spz->zs.next_in, spz->zs.avail_in);
  195. }
  196. spz->input_left = spz->header->zlen - rlen;
  197. if (!spz->input_left) {
  198. if (spz->crc32 != spz->header->crc32) {
  199. spz->err = Z_STREAM_ERROR;
  200. goto err;
  201. }
  202. }
  203. if (spz->header->zlen == spz->header->dlen) {
  204. /* Assume it is a raw binary; input buffer is output buffer */
  205. spz->read_data = read_data_raw;
  206. spz->optr = spz->zs.next_in;
  207. spz->zs.next_out = spz->zs.next_in + spz->zs.avail_in;
  208. } else {
  209. /* Compressed data? */
  210. spz->obuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  211. if (!spz->obuf)
  212. goto err;
  213. if (rlen >= 14 && !memcmp(spz->zs.next_in, "\37\213\10", 3)) {
  214. /* It is a gzip file */
  215. spz->read_data = read_data_inflate;
  216. /* gzip, max window size */
  217. rv = inflateInit2(&spz->zs, 16 + 15);
  218. if (rv != Z_OK && rv != Z_STREAM_END) {
  219. spz->err = rv;
  220. goto err;
  221. }
  222. spz->eoi = rv == Z_STREAM_END;
  223. spz->end_data = inflateEnd;
  224. } else {
  225. /* Unknown compression format */
  226. spz->err = Z_STREAM_ERROR;
  227. goto err;
  228. }
  229. }
  230. err:
  231. if (spz->err)
  232. MSG(" failed (err %d)\n", spz->err);
  233. not_err:
  234. MSG("\n");
  235. return spz->err;
  236. }
  237. static int spiflash_data_cleanup(spz_stream *spz)
  238. {
  239. int err = 0;
  240. if (!spz)
  241. return 0;
  242. err = spz->err;
  243. if (spz->flash->ops->close_data) {
  244. int rv = spz->flash->ops->close_data(spz->flash->cookie);
  245. if (!err)
  246. err = rv;
  247. }
  248. if (spz->end_data)
  249. spz->end_data(&spz->zs);
  250. if (spz->free_header)
  251. free((void *)spz->header);
  252. if (spz->vbuf)
  253. free(spz->vbuf);
  254. if (spz->dbuf)
  255. free(spz->dbuf);
  256. if (spz->obuf)
  257. free(spz->obuf);
  258. if (spz->ibuf)
  259. free(spz->ibuf);
  260. return err;
  261. }
  262. /*
  263. * Set up a command header with an address according to the SPI
  264. * addressing mode. Returns a pointer to the first byte past the
  265. * address.
  266. */
  267. static void *spiflash_setup_addrcmd(const struct spiflash *flash,
  268. uint32_t addr,
  269. uint8_t cmd24, uint8_t cmd32,
  270. void *cmdbuf)
  271. {
  272. enum spiflash_addr_mode mode = flash->param->addr;
  273. uint8_t *cmd = cmdbuf;
  274. if (!mode)
  275. mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
  276. if (mode == SPIFLASH_ADDR_24BIT) {
  277. *cmd++ = cmd24;
  278. } else {
  279. *cmd++ = cmd32;
  280. *cmd++ = addr >> 24;
  281. }
  282. *cmd++ = addr >> 16;
  283. *cmd++ = addr >> 8;
  284. *cmd++ = addr;
  285. return cmd;
  286. }
  287. static int spiflash_get_status(const struct spiflash *flash,
  288. uint8_t cmd, uint8_t *sr)
  289. {
  290. return flash->ops->spi_read(flash->cookie, &cmd, 1, sr, 1,
  291. flash->param->tshsl);
  292. }
  293. /* This needs a timeout function */
  294. static int spiflash_wait_status(const struct spiflash *flash,
  295. int delay, uint8_t mask, uint8_t val)
  296. {
  297. uint8_t sr1;
  298. int rv;
  299. do {
  300. if (flash->ops->yield)
  301. flash->ops->yield(flash->cookie, delay);
  302. rv = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  303. if (rv)
  304. return rv;
  305. } while ((sr1 & mask) != val); /* Waiting... */
  306. return 0;
  307. }
  308. int spiflash_read(const struct spiflash *flash,
  309. uint32_t addr, void *buffer, size_t len)
  310. {
  311. uint8_t cmdbuf[6];
  312. uint8_t *cmd;
  313. cmd = spiflash_setup_addrcmd(flash, addr,
  314. ROM_FAST_READ, ROM_FAST_READ_32BIT,
  315. cmdbuf);
  316. *cmd++ = 0; /* Dummy cycles */
  317. return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
  318. buffer, len, flash->param->tshsl1);
  319. }
  320. static int spiflash_simple_command(const struct spiflash *flash, uint8_t cmd)
  321. {
  322. return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
  323. flash->param->tshsl);
  324. }
  325. static int spiflash_write_enable(const struct spiflash *flash)
  326. {
  327. uint8_t sr1;
  328. int rv;
  329. rv = spiflash_wait_status(flash, 0, 1, 0);
  330. if (rv)
  331. return rv;
  332. rv = spiflash_simple_command(flash, ROM_WRITE_ENABLE);
  333. if (rv)
  334. return rv;
  335. return spiflash_wait_status(flash, 0, 3, 2);
  336. }
  337. static int spiflash_program(const struct spiflash *flash,
  338. uint32_t addr, const void *buffer, size_t len)
  339. {
  340. uint8_t cmdbuf[5];
  341. uint8_t *cmd;
  342. int rv;
  343. rv = spiflash_write_enable(flash);
  344. if (rv)
  345. return rv;
  346. cmd = spiflash_setup_addrcmd(flash, addr,
  347. ROM_PAGE_PROGRAM, ROM_PAGE_PROGRAM_32BIT,
  348. cmdbuf);
  349. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  350. buffer, len, flash->param->tshsl2);
  351. if (rv)
  352. return rv;
  353. return spiflash_wait_status(flash, flash->param->tpp, 3, 0);
  354. }
  355. /*
  356. * Erase up to (long bits) sectors, using block erase if possible.
  357. */
  358. static int spiflash_erase(const struct spiflash *flash,
  359. uint32_t addr, unsigned long sector_mask)
  360. {
  361. uint8_t cmdbuf[5];
  362. uint8_t *cmd;
  363. uint8_t cmd24, cmd32;
  364. uint32_t erasesize;
  365. int rv;
  366. int delay;
  367. const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
  368. const unsigned long block_sector_mask
  369. = block_mask >> SPIFLASH_SECTOR_SHIFT;
  370. if (!sector_mask) {
  371. MSG("update: nothing to erase\n");
  372. return 0;
  373. }
  374. while (sector_mask) {
  375. if (!(addr & block_mask) &&
  376. ((sector_mask & block_sector_mask) == block_sector_mask)) {
  377. cmd24 = ROM_ERASE_64K;
  378. cmd32 = ROM_ERASE_64K_32BIT;
  379. delay = flash->param->tbe2;
  380. erasesize = SPIFLASH_BLOCK_SIZE;
  381. } else {
  382. cmd24 = ROM_ERASE_4K;
  383. cmd32 = ROM_ERASE_4K_32BIT;
  384. delay = flash->param->tse;
  385. erasesize = SPIFLASH_SECTOR_SIZE;
  386. }
  387. if (sector_mask & 1) {
  388. MSG("\rupdate: erasing %2uK at 0x%06x... ", erasesize >> 10, addr);
  389. rv = spiflash_write_enable(flash);
  390. if (rv)
  391. return rv;
  392. cmd = spiflash_setup_addrcmd(flash, addr, cmd24, cmd32, cmdbuf);
  393. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  394. NULL, 0, flash->param->tshsl2);
  395. if (rv)
  396. return rv;
  397. rv = spiflash_wait_status(flash, delay, 3, 0);
  398. if (rv)
  399. return rv;
  400. }
  401. addr += erasesize;
  402. sector_mask >>= (erasesize >> SPIFLASH_SECTOR_SHIFT);
  403. }
  404. MSG("ok\n");
  405. return 0;
  406. }
  407. /*
  408. * from: current flash contents
  409. * to: desired flash contents
  410. *
  411. * These are assumed to be aligned full block buffers
  412. */
  413. enum flashmem_status {
  414. FMS_DONE, /* All done, no programming needed */
  415. FMS_PROGRAM, /* Can be programmed */
  416. FMS_ERASE, /* Needs erase before programming */
  417. FMS_NOTCHECKED /* Not checked yet */
  418. };
  419. static enum flashmem_status
  420. spiflash_memcmp(const void *from, const void *to, size_t len)
  421. {
  422. const uint32_t *pf = from;
  423. const uint32_t *pt = to;
  424. const uint32_t *pfend = (const uint32_t *)((const char *)from + len);
  425. uint32_t doprog = 0;
  426. uint32_t doerase = 0;
  427. while (pf < pfend) {
  428. uint32_t f = *pf++;
  429. uint32_t t = *pt++;
  430. doprog |= f ^ t; /* Need programming if any data mismatch */
  431. doerase |= ~f & t; /* Need erasing if any 0 -> 1 */
  432. }
  433. return doerase ? FMS_ERASE : doprog ? FMS_PROGRAM : FMS_DONE;
  434. }
  435. /*
  436. * Check a block for sectors which need erasing and pages which need
  437. * programming; the prog_mask is 256 bits long and so span multiple words.
  438. *
  439. * The desired input is spz->dbuf and the existing flash content is
  440. * written to spz->vbuf.
  441. *
  442. */
  443. static int spiflash_check_block(spz_stream *spz, uint32_t addr,
  444. uint32_t *erase_mask, uint32_t *prog_mask)
  445. {
  446. int rv;
  447. const uint8_t *p, *q;
  448. unsigned int page;
  449. rv = spiflash_read(spz->flash, addr, spz->vbuf, SPIFLASH_BLOCK_SIZE);
  450. if (rv) {
  451. if (!spz->err)
  452. spz->err = rv;
  453. return rv;
  454. }
  455. *erase_mask = 0;
  456. memset(prog_mask, 0, SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE/8);
  457. p = spz->vbuf;
  458. q = spz->dbuf;
  459. for (page = 0; page < SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE; page++) {
  460. enum flashmem_status status;
  461. switch (spiflash_memcmp(p, q, SPIFLASH_PAGE_SIZE)) {
  462. case FMS_ERASE:
  463. *erase_mask |= UINT32_C(1) <<
  464. (page >> (SPIFLASH_SECTOR_SHIFT-SPIFLASH_PAGE_SHIFT));
  465. break;
  466. case FMS_PROGRAM:
  467. prog_mask[page >> 5] |= UINT32_C(1) << (page & 31);
  468. break;
  469. default:
  470. /* Nothing to do! */
  471. break;
  472. }
  473. p += SPIFLASH_PAGE_SIZE;
  474. q += SPIFLASH_PAGE_SIZE;
  475. }
  476. return 0;
  477. }
  478. /* Serial Flash Discoverable Parameter Table, see JESD216 */
  479. static int spiflash_get_sfdp(const struct spiflash *flash, void *sfdp)
  480. {
  481. static const uint8_t cmd_read_sfdp[] = { ROM_READ_SFDP, 0, 0, 0, 0 };
  482. return flash->ops->spi_read(flash->cookie, cmd_read_sfdp,
  483. sizeof cmd_read_sfdp, sfdp, SPIFLASH_SFDP_SIZE,
  484. flash->param->tshsl);
  485. }
  486. int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
  487. {
  488. spz_stream _spz;
  489. spz_stream * const spz = &_spz; /* For consistency in notation */
  490. int err = 0;
  491. enum flashmem_status fs;
  492. #if 0
  493. static const uint8_t read_sr_cmd[2] = { ROM_READ_SR1, ROM_READ_SR2 };
  494. uint8_t sr1;
  495. uint32_t *sfdp;
  496. sfdp = malloc(SPIFLASH_SFDP_SIZE);
  497. memset(sfdp, 0, SPIFLASH_SFDP_SIZE);
  498. /* Note: SFDP data is littleendian! */
  499. err = spiflash_get_sfdp(flash, sfdp);
  500. if (err)
  501. return err;
  502. for (int i = 0; i < SPIFLASH_SFDP_SIZE; i += 16) {
  503. MSG("%04x :", i);
  504. for (int j = 0; j < 16; j += 4) {
  505. MSG(" %08x", sfdp[(i+j) >> 2]);
  506. }
  507. MSG("\n");
  508. }
  509. if (sfdp[0] != 0x50444653) {
  510. MSG("update: invalid SFDP information read\n");
  511. return SPIFLASH_ERR_DETECT;
  512. }
  513. /*
  514. * If the flash is busy, try to reset it
  515. */
  516. err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  517. if (err)
  518. return err;
  519. if (sr1 & 0x01) {
  520. udelay(60);
  521. err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  522. if (err)
  523. return err;
  524. if (sr1 & 0x01) {
  525. MSG("update: flash busy, trying reset... ");
  526. err = spiflash_simple_command(flash, ROM_ENABLE_RESET);
  527. if (err)
  528. return err;
  529. err = spiflash_simple_command(flash, ROM_RESET);
  530. if (err)
  531. return err;
  532. udelay(60);
  533. err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
  534. if (err || (sr1 & 0x01)) {
  535. MSG("failed\n");
  536. return SPIFLASH_ERR_NOT_READY;
  537. }
  538. MSG("ok\n");
  539. }
  540. }
  541. #endif
  542. while (!err) {
  543. int rv;
  544. uint32_t addr;
  545. uint32_t data_left;
  546. memset(spz, 0, sizeof *spz);
  547. spz->zs.avail_in = buflen;
  548. spz->zs.next_in = buf;
  549. spz->flash = flash;
  550. if (spiflash_data_init(spz))
  551. goto err;
  552. if (!spz->ibuf) {
  553. /* No ibuf allocated, feeding from raw data buffer */
  554. unsigned int bufskip = (spz->header->zlen + sizeof *spz->header + 3) & ~3;
  555. if (bufskip >= buflen) {
  556. buflen = 0;
  557. buf = NULL;
  558. } else {
  559. buflen -= bufskip;
  560. buf += bufskip;
  561. }
  562. } else {
  563. /* Buffer exhausted, additional data read */
  564. buflen = 0;
  565. buf = NULL;
  566. }
  567. data_left = spz->header->dlen;
  568. addr = spz->header->address;
  569. while (data_left && !spz->err) {
  570. unsigned int bytes = 0;
  571. unsigned int padding;
  572. while (data_left && bytes < SPIFLASH_BLOCK_SIZE) {
  573. unsigned int avail = spz->zs.next_out - spz->optr;
  574. unsigned int need = SPIFLASH_BLOCK_SIZE - bytes;
  575. int rv;
  576. if (need > data_left)
  577. need = data_left;
  578. if (avail) {
  579. if (avail > need)
  580. avail = need;
  581. memcpy(spz->dbuf + bytes, spz->optr, avail);
  582. spz->optr += avail;
  583. bytes += avail;
  584. data_left -= avail;
  585. continue;
  586. }
  587. rv = spz->read_data(spz);
  588. if (spz->err)
  589. goto err;
  590. if (!rv) {
  591. spz->err = Z_STREAM_ERROR; /* Input underrun */
  592. goto err;
  593. }
  594. }
  595. if (bytes < SPIFLASH_BLOCK_SIZE) {
  596. memset(spz->dbuf + bytes, 0xff, SPIFLASH_BLOCK_SIZE - bytes);
  597. }
  598. MSG("update: flash block at 0x%06x (%5u bytes):\n", addr, bytes);
  599. uint32_t erase_mask;
  600. uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
  601. rv = spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  602. if (rv)
  603. goto err;
  604. if (erase_mask) {
  605. rv = spiflash_erase(spz->flash, addr, erase_mask);
  606. if (rv) {
  607. spz->err = rv;
  608. goto err;
  609. }
  610. rv = spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  611. if (spz->err)
  612. goto err;
  613. if (erase_mask) {
  614. MSG("[erase mask = %04x] ", erase_mask);
  615. spz->err = SPIFLASH_ERR_ERASE_FAILED;
  616. goto err;
  617. }
  618. }
  619. unsigned int page;
  620. bool programmed = false;
  621. for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
  622. page++) {
  623. uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
  624. if (!(prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))))
  625. continue; /* No need to program */
  626. programmed = true;
  627. udelay(10000);
  628. MSG("\rupdate: writing at 0x%06x... ", addr + page_offs);
  629. rv = spiflash_program(spz->flash, addr + page_offs,
  630. spz->dbuf + page_offs,
  631. SPIFLASH_PAGE_SIZE);
  632. if (rv) {
  633. spz->err = rv;
  634. goto err;
  635. }
  636. rv = spiflash_read(spz->flash, addr + page_offs,
  637. spz->vbuf + page_offs,
  638. SPIFLASH_PAGE_SIZE);
  639. if (rv) {
  640. MSG("readback ");
  641. goto err;
  642. }
  643. if (memcmp(spz->dbuf + page_offs, spz->vbuf + page_offs,
  644. SPIFLASH_PAGE_SIZE)) {
  645. MSG("verify @ 0x%06x ", addr + page_offs);
  646. spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
  647. goto err;
  648. }
  649. }
  650. if (programmed)
  651. MSG("ok\n");
  652. else
  653. MSG("update: nothing to write\n");
  654. addr += SPIFLASH_BLOCK_SIZE;
  655. }
  656. err:
  657. err = spiflash_data_cleanup(spz);
  658. }
  659. if (err == Z_STREAM_END)
  660. err = 0; /* End of data is not an error */
  661. if (err)
  662. MSG("failed (err %d)\n", err);
  663. return err;
  664. }
  665. /*
  666. * Read unique serial number from flash. Note: returns id in
  667. * bigendian ("network") byte order.
  668. */
  669. int spiflash_read_id(const struct spiflash *flash, void *id)
  670. {
  671. static const uint8_t read_unique_id[] = { ROM_READ_UNIQUE_ID, 0, 0, 0, 0 };
  672. return flash->ops->spi_read(flash->cookie, read_unique_id,
  673. sizeof read_unique_id,
  674. id, SPIFLASH_ID_LEN, flash->param->tshsl);
  675. }
  676. /*
  677. * Read vendor and device ID from flash.
  678. */
  679. int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
  680. {
  681. static const uint8_t read_vdid[] = { ROM_MANUFACTURER_DEVICE_ID, 0, 0, 0 };
  682. return flash->ops->spi_read(flash->cookie, read_vdid,
  683. sizeof read_vdid,
  684. vdid, SPIFLASH_VDID_LEN, flash->param->tshsl);
  685. }
  686. /*
  687. * Flash data from memory buffer(s)
  688. */