spiflash.c 19 KB

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