spiflash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 *vbuf; /* Readback/verify buffer */
  22. const struct spiflash_header *header;
  23. uint32_t crc32; /* Input data CRC32 */
  24. unsigned int input_left; /* Input data unread */
  25. int err; /* Error code to return */
  26. bool eoi; /* Reached end of input */
  27. bool free_header; /* header is malloc()'d */
  28. };
  29. static void *spz_malloc(spz_stream *spz, size_t bytes)
  30. {
  31. void *p = malloc(bytes);
  32. if (!p && !spz->err) {
  33. spz->err = Z_MEM_ERROR;
  34. }
  35. return p;
  36. }
  37. static int spiflash_read_data(spz_stream *spz)
  38. {
  39. int rv;
  40. int (*read_data)(void *, void *, unsigned int);
  41. unsigned int read_block_size;
  42. if (spz->eoi || spz->err)
  43. return 0;
  44. read_data = spz->flash->ops->read_data;
  45. if (!spz->input_left || !read_data) {
  46. spz->eoi = true;
  47. return 0;
  48. }
  49. read_block_size = min(spz->input_left, SPIFLASH_BLOCK_SIZE);
  50. if (!spz->ibuf) {
  51. spz->ibuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  52. if (!spz->ibuf) {
  53. spz->eoi = true;
  54. return 0;
  55. }
  56. }
  57. spz->zs.next_in = spz->ibuf;
  58. spz->zs.avail_in = 0;
  59. rv = read_data(spz->flash->cookie, spz->ibuf, read_block_size);
  60. if (spz->err) {
  61. rv = 0;
  62. } else if (rv < 0) {
  63. spz->err = rv;
  64. rv = 0;
  65. }
  66. if (rv != (int)read_block_size)
  67. spz->eoi = true;
  68. if (rv) {
  69. spz->crc32 = crc32(spz->crc32, spz->ibuf, rv);
  70. spz->input_left -= rv;
  71. if (!spz->input_left) {
  72. if (spz->crc32 != spz->header->crc32) {
  73. spz->err = Z_STREAM_ERROR;
  74. rv = 0;
  75. }
  76. }
  77. }
  78. return spz->zs.avail_in = rv;
  79. }
  80. static int read_data_raw(spz_stream *spz)
  81. {
  82. int rlen;
  83. if (spz->eoi)
  84. return 0;
  85. rlen = spiflash_read_data(spz);
  86. if (rlen) {
  87. spz->optr = spz->ibuf;
  88. spz->zs.next_out = spz->ibuf + rlen;
  89. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE - rlen;
  90. }
  91. return rlen;
  92. }
  93. static int read_data_inflate(spz_stream *spz)
  94. {
  95. int rv = Z_STREAM_END;
  96. spz->optr = spz->zs.next_out = spz->obuf;
  97. spz->zs.avail_out = SPIFLASH_BLOCK_SIZE;
  98. while (spz->zs.avail_out) {
  99. if (!spz->zs.avail_in && !spz->eoi) {
  100. int rlen = spiflash_read_data(spz);
  101. spz->zs.next_in = spz->ibuf;
  102. spz->zs.avail_in = rlen;
  103. }
  104. rv = inflate(&spz->zs, Z_SYNC_FLUSH);
  105. if (rv == Z_OK || (rv == Z_BUF_ERROR && !spz->eoi))
  106. continue;
  107. spz->eoi = true;
  108. if (rv != Z_STREAM_END && !spz->err)
  109. spz->err = rv;
  110. break;
  111. }
  112. return spz->zs.next_out - spz->optr;
  113. }
  114. /*
  115. * spz needs to be initialized to zero except the flash, zs.next_in,
  116. * and zs.avail_in fields.
  117. *
  118. * Returns Z_STREAM_END on end of data.
  119. */
  120. static int spiflash_data_init(spz_stream *spz)
  121. {
  122. int rv = Z_OK;
  123. uint8_t *rdbuf = NULL;
  124. int rlen;
  125. int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
  126. uint32_t header_crc;
  127. MSG("update: initializing... ");
  128. spz->vbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  129. if (!spz->vbuf)
  130. goto err;
  131. rlen = spz->zs.avail_in;
  132. spz->header = (void *)spz->zs.next_in;
  133. read_data = spz->flash->ops->read_data;
  134. spz->eoi = !read_data;
  135. if (!rlen && read_data) {
  136. struct spiflash_header *header;
  137. spz->header = header = spz_malloc(spz, sizeof *spz->header);
  138. spz->free_header = true;
  139. spz->input_left = UINT_MAX; /* Unknown at this point */
  140. rlen = read_data(spz->flash->cookie, header, sizeof *header);
  141. }
  142. if (spz->err)
  143. goto err;
  144. if (rlen < (int)sizeof *spz->header) {
  145. spz->err = rlen ? Z_STREAM_ERROR : Z_STREAM_END;
  146. goto err;
  147. }
  148. rlen -= sizeof *spz->header;
  149. spz->zs.next_in += sizeof *spz->header;
  150. /*
  151. * Check header magic and CRC
  152. */
  153. if (spz->header->magic != SPIFLASH_MAGIC) {
  154. spz->err = Z_STREAM_ERROR;
  155. goto err;
  156. }
  157. header_crc = crc32(0, NULL, 0);
  158. header_crc = crc32(header_crc, (const void *)spz->header,
  159. sizeof spz->header - 4);
  160. if (header_crc != spz->header->header_crc32 ||
  161. spz->header->zlen > spz->header->dlen ||
  162. (spz->eoi && (int)spz->header->zlen > rlen)) {
  163. spz->err = Z_STREAM_ERROR;
  164. goto err;
  165. }
  166. if (!spz->header->dlen) {
  167. /* End of data */
  168. spz->err = Z_STREAM_END;
  169. goto err;
  170. }
  171. MSG("updating 0x%06x..%06x (%u bytes)... ",
  172. spz->header->address, spz->header->address + spz->header->dlen - 1,
  173. spz->header->dlen);
  174. if (rlen > (int)spz->header->zlen)
  175. rlen = spz->header->zlen;
  176. spz->zs.avail_in = rlen;
  177. spz->crc32 = crc32(0, NULL, 0);
  178. if (rlen) {
  179. /* Received data in input buffer already */
  180. spz->crc32 = crc32(spz->crc32, spz->zs.next_in, spz->zs.avail_in);
  181. }
  182. spz->input_left = spz->header->zlen - rlen;
  183. if (!spz->input_left) {
  184. if (spz->crc32 != spz->header->crc32) {
  185. spz->err = Z_STREAM_ERROR;
  186. goto err;
  187. }
  188. }
  189. if (spz->header->zlen == spz->header->dlen) {
  190. /* Assume it is a raw binary; input buffer is output buffer */
  191. spz->read_data = read_data_raw;
  192. spz->optr = spz->zs.next_in;
  193. spz->zs.next_out = spz->zs.next_in + spz->zs.avail_in;
  194. } else {
  195. /* Compressed data? */
  196. spz->obuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  197. if (!spz->obuf)
  198. goto err;
  199. if (rlen >= 14 && !memcmp(spz->zs.next_in, "\37\213\10", 3)) {
  200. /* It is a gzip file */
  201. spz->read_data = read_data_inflate;
  202. /* gzip, max window size */
  203. rv = inflateInit2(&spz->zs, 16 + 15);
  204. if (rv != Z_OK && rv != Z_STREAM_END) {
  205. spz->err = rv;
  206. goto err;
  207. }
  208. spz->eoi = rv == Z_STREAM_END;
  209. spz->end_data = inflateEnd;
  210. } else {
  211. /* Unknown compression format */
  212. spz->err = Z_STREAM_ERROR;
  213. goto err;
  214. }
  215. }
  216. err:
  217. MSG("%s\n", spz->err ? "failed" : "ok");
  218. return spz->err;
  219. }
  220. static int spiflash_data_cleanup(spz_stream *spz)
  221. {
  222. int err = 0;
  223. if (!spz)
  224. return 0;
  225. err = spz->err;
  226. if (spz->flash->ops->close_data) {
  227. int rv = spz->flash->ops->close_data(spz->flash->cookie);
  228. if (!err)
  229. err = rv;
  230. }
  231. if (spz->end_data)
  232. spz->end_data(&spz->zs);
  233. if (spz->free_header)
  234. free((void *)spz->header);
  235. if (spz->vbuf)
  236. free(spz->vbuf);
  237. if (spz->obuf)
  238. free(spz->obuf);
  239. if (spz->ibuf)
  240. free(spz->ibuf);
  241. return err;
  242. }
  243. /*
  244. * Set up a command header with an address according to the SPI
  245. * addressing mode. Returns a pointer to the first byte past the
  246. * address.
  247. */
  248. static void *spiflash_setup_addrcmd(const struct spiflash *flash,
  249. uint32_t addr,
  250. uint8_t cmd24, uint8_t cmd32,
  251. void *cmdbuf)
  252. {
  253. enum spiflash_addr_mode mode = flash->param->addr;
  254. uint8_t *cmd = cmdbuf;
  255. if (!mode)
  256. mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
  257. if (mode == SPIFLASH_ADDR_24BIT) {
  258. addr <<= 8;
  259. *cmd++ = cmd24;
  260. } else {
  261. *cmd++ = cmd32;
  262. *cmd++ = addr >> 24;
  263. }
  264. *cmd++ = addr >> 16;
  265. *cmd++ = addr >> 8;
  266. *cmd++ = addr;
  267. return cmd;
  268. }
  269. static int spiflash_wait_ready(const struct spiflash *flash, int delay)
  270. {
  271. uint8_t cmd = 0x05; /* Read Status Register 1 */
  272. uint8_t sr1 = 0;
  273. int rv;
  274. do {
  275. flash->ops->yield(flash->cookie, delay);
  276. rv = flash->ops->spi_read(flash->cookie, &cmd, 1,
  277. &sr1, 1, flash->param->tshsl);
  278. if (rv)
  279. return rv;
  280. } while (sr1 & 0x01); /* Busy bit set? */
  281. return 0;
  282. }
  283. int spiflash_read(const struct spiflash *flash,
  284. uint32_t addr, void *buffer, size_t len)
  285. {
  286. uint8_t cmdbuf[6];
  287. uint8_t *cmd;
  288. /*
  289. * 13h = Fast Read
  290. * 0Ch = Fast Read with 4-Byte Address
  291. */
  292. cmd = spiflash_setup_addrcmd(flash, addr, 0x13, 0x0c, cmdbuf);
  293. *cmd++ = 0; /* Dummy bits */
  294. return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
  295. buffer, len, flash->param->tshsl1);
  296. }
  297. static int spiflash_write_enable(const struct spiflash *flash)
  298. {
  299. const uint8_t cmd = 0x06; /* 06h = Write Enable */
  300. return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
  301. flash->param->tshsl);
  302. }
  303. static int spiflash_program(const struct spiflash *flash,
  304. uint32_t addr, const void *buffer, size_t len)
  305. {
  306. uint8_t cmdbuf[5];
  307. uint8_t *cmd;
  308. int rv;
  309. rv = spiflash_write_enable(flash);
  310. if (rv)
  311. return rv;
  312. /*
  313. * 02h = Page Program
  314. * 12h = Page Program with 4-Byte Address
  315. */
  316. cmd = spiflash_setup_addrcmd(flash, addr, 0x02, 0x12, cmdbuf);
  317. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  318. buffer, len, flash->param->tshsl2);
  319. if (rv)
  320. return rv;
  321. return spiflash_wait_ready(flash, flash->param->tpp);
  322. }
  323. /*
  324. * Erase up to (long bits) sectors, using block erase if possible.
  325. */
  326. static int spiflash_erase(const struct spiflash *flash,
  327. uint32_t addr, unsigned long sector_mask)
  328. {
  329. uint8_t cmdbuf[5];
  330. uint8_t *cmd;
  331. uint8_t cmd24, cmd32;
  332. uint32_t nextaddr;
  333. int rv;
  334. int delay;
  335. const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
  336. const unsigned long block_sectors = block_mask >> SPIFLASH_SECTOR_SHIFT;
  337. while (sector_mask) {
  338. if (((sector_mask & block_sectors) == block_sectors) &&
  339. !(addr & block_mask)) {
  340. /*
  341. * D8h = 64KB Block Erase
  342. * DCh = 64K Block Erase with 4-Byte Address
  343. */
  344. cmd24 = 0xd8; cmd32 = 0xdc;
  345. delay = flash->param->tbe2;
  346. nextaddr = addr + SPIFLASH_BLOCK_SIZE;
  347. sector_mask >>= 16;
  348. } else {
  349. if (sector_mask & 1) {
  350. /*
  351. * 20h = Sector Erase
  352. * 21h = Sector Erase with 4-Byte Address
  353. */
  354. cmd24 = 0x20; cmd32 = 0x21;
  355. delay = flash->param->tse;
  356. nextaddr = addr + SPIFLASH_SECTOR_SIZE;
  357. sector_mask >>= 1;
  358. } else {
  359. addr += SPIFLASH_SECTOR_SIZE;
  360. sector_mask >>= 1;
  361. continue; /* Skip sector */
  362. }
  363. }
  364. rv = spiflash_write_enable(flash);
  365. if (rv)
  366. return rv;
  367. cmd = spiflash_setup_addrcmd(flash, addr, cmd24, cmd32, cmdbuf);
  368. rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
  369. NULL, 0, flash->param->tshsl2);
  370. if (rv)
  371. return rv;
  372. rv = spiflash_wait_ready(flash, delay);
  373. if (rv)
  374. return rv;
  375. addr = nextaddr;
  376. }
  377. return 0;
  378. }
  379. /*
  380. * from: current flash contents
  381. * to: desired flash contents
  382. *
  383. * These are assumed to be aligned full block buffers
  384. */
  385. enum flashmem_status {
  386. FMS_DONE, /* All done, no programming needed */
  387. FMS_PROGRAM, /* Can be programmed */
  388. FMS_ERASE, /* Needs erase before programming */
  389. FMS_NOTCHECKED /* Not checked yet */
  390. };
  391. static enum flashmem_status
  392. spiflash_memcmp(const void *from, const void *to, size_t len)
  393. {
  394. const uint32_t *pf = from;
  395. const uint32_t *pt = to;
  396. const uint32_t *pfend = (const uint32_t *)((const char *)from + len);
  397. uint32_t notok = 0;
  398. uint32_t notprog = 0;
  399. while (pf < pfend) {
  400. uint32_t f = *pf++;
  401. uint32_t t = *pt++;
  402. notok |= f ^ t; /* Need programming if any data mismatch */
  403. notprog |= ~f & t; /* Need erasing if any 0 -> 1 */
  404. }
  405. return notprog ? FMS_ERASE : notok ? FMS_PROGRAM : FMS_DONE;
  406. }
  407. /*
  408. * Check a block for sectors which need erasing and pages which need
  409. * programming; the prog_mask is 256 bits long and so span multiple words.
  410. *
  411. * The input is spz->optr, and the existing flash content is written
  412. * to spz->vptr.
  413. *
  414. */
  415. static int spiflash_check_block(spz_stream *spz, uint32_t addr,
  416. uint32_t *erase_mask, uint32_t *prog_mask)
  417. {
  418. int rv;
  419. const uint8_t *p, *q;
  420. unsigned int page;
  421. rv = spiflash_read(spz->flash, addr, spz->vbuf, SPIFLASH_BLOCK_SIZE);
  422. if (rv) {
  423. if (!spz->err)
  424. spz->err = rv;
  425. return rv;
  426. }
  427. p = spz->optr;
  428. q = spz->vbuf;
  429. for (page = 0; page < SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE; page++) {
  430. enum flashmem_status status;
  431. switch (spiflash_memcmp(p, q, SPIFLASH_PAGE_SIZE)) {
  432. case FMS_ERASE:
  433. *erase_mask |= UINT32_C(1) <<
  434. (page >> (SPIFLASH_SECTOR_SHIFT-SPIFLASH_PAGE_SHIFT));
  435. break;
  436. case FMS_PROGRAM:
  437. prog_mask[page >> 5] |= UINT32_C(1) << (page & 31);
  438. break;
  439. default:
  440. /* Nothing to do! */
  441. break;
  442. }
  443. p += SPIFLASH_PAGE_SIZE;
  444. q += SPIFLASH_PAGE_SIZE;
  445. }
  446. return 0;
  447. }
  448. int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
  449. {
  450. spz_stream _spz;
  451. spz_stream * const spz = &_spz; /* For consistency in notation */
  452. int err;
  453. enum flashmem_status fs;
  454. uint8_t *padbuf = NULL;
  455. err = 0;
  456. while (!err) {
  457. int rv;
  458. bool eof;
  459. uint32_t addr;
  460. memset(spz, 0, sizeof *spz);
  461. spz->zs.avail_in = buflen;
  462. spz->zs.next_in = buf;
  463. spz->flash = flash;
  464. if (spiflash_data_init(spz))
  465. goto err;
  466. if (!spz->ibuf) {
  467. /* No ibuf allocated, feeding from raw data buffer */
  468. unsigned int bufskip = (spz->zs.next_out - (uint8_t *)buf + 3) & ~3;
  469. if (bufskip >= buflen) {
  470. buflen = 0;
  471. buf = NULL;
  472. } else {
  473. buflen -= bufskip;
  474. buf = spz->zs.next_out;
  475. }
  476. } else {
  477. /* Buffer exhausted, additional data read */
  478. buflen = 0;
  479. buf = NULL;
  480. }
  481. eof = false;
  482. addr = spz->header->address;
  483. while (!eof && !spz->err) {
  484. unsigned int bytes = spz->zs.next_out - spz->optr;
  485. unsigned int padding;
  486. if (bytes < SPIFLASH_BLOCK_SIZE) {
  487. int rv;
  488. rv = spz->read_data(spz);
  489. if (spz->err)
  490. goto err;
  491. eof = !rv;
  492. bytes = spz->zs.next_out - spz->optr;
  493. if (!bytes)
  494. break;
  495. padding = -bytes & (SPIFLASH_BLOCK_SIZE-1);
  496. if (padding) {
  497. if (!padbuf) {
  498. padbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
  499. if (!padbuf)
  500. goto err;
  501. }
  502. memcpy(padbuf, spz->optr, bytes);
  503. memset(padbuf+bytes, 0xff, padding);
  504. spz->optr = padbuf;
  505. eof = true;
  506. }
  507. }
  508. MSG("update: flash block at 0x%06x: ", addr);
  509. uint32_t erase_mask;
  510. uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
  511. rv = spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  512. if (rv)
  513. goto err;
  514. if (erase_mask) {
  515. MSG("erasing... ");
  516. rv = spiflash_erase(spz->flash, addr, erase_mask);
  517. if (rv) {
  518. spz->err = rv;
  519. goto err;
  520. }
  521. rv = spiflash_check_block(spz, addr, &erase_mask, prog_mask);
  522. if (spz->err)
  523. goto err;
  524. if (erase_mask) {
  525. spz->err = SPIFLASH_ERR_ERASE_FAILED;
  526. goto err;
  527. }
  528. }
  529. unsigned int page;
  530. bool first_prog = true;
  531. for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
  532. page++) {
  533. uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
  534. if (prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))) {
  535. if (first_prog) {
  536. MSG("programming %06x... ", addr + page_offs);
  537. } else {
  538. MSG("\b\b\b\b\b\b\b\b\b\b%06x... ", addr+page_offs);
  539. }
  540. first_prog = false;
  541. rv = spiflash_program(spz->flash, addr + page_offs,
  542. spz->optr + page_offs,
  543. SPIFLASH_PAGE_SIZE);
  544. if (rv) {
  545. spz->err = rv;
  546. goto err;
  547. }
  548. /* Read back data and verify */
  549. rv = spiflash_read(spz->flash, addr+page_offs,
  550. spz->vbuf + page_offs,
  551. SPIFLASH_PAGE_SIZE);
  552. if (rv) {
  553. MSG("verify "); /* "verify failed" */
  554. spz->err = rv;
  555. goto err;
  556. }
  557. if (memcmp(spz->optr + page_offs, spz->vbuf + page_offs,
  558. SPIFLASH_PAGE_SIZE)) {
  559. spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
  560. goto err;
  561. }
  562. }
  563. }
  564. spz->optr += SPIFLASH_BLOCK_SIZE;
  565. addr += SPIFLASH_BLOCK_SIZE;
  566. }
  567. err:
  568. err = spiflash_data_cleanup(spz);
  569. }
  570. if (padbuf) {
  571. free(padbuf);
  572. padbuf = NULL;
  573. }
  574. MSG("%s\n", spz->err ? "failed" : "ok");
  575. return spz->err;
  576. }
  577. /*
  578. * Read unique serial number from flash. Note: returns id in
  579. * bigendian ("network") byte order.
  580. */
  581. int spiflash_read_id(const struct spiflash *flash, void *id)
  582. {
  583. static const uint8_t read_unique_id[] = { 0x4b, 0, 0, 0, 0 };
  584. return flash->ops->spi_read(flash->cookie, read_unique_id,
  585. sizeof read_unique_id,
  586. id, SPIFLASH_ID_LEN, flash->param->tshsl);
  587. }
  588. /*
  589. * Read vendor and device ID from flash.
  590. */
  591. int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
  592. {
  593. static const uint8_t read_vdid[] = { 0x90, 0, 0, 0 };
  594. return flash->ops->spi_read(flash->cookie, read_vdid,
  595. sizeof read_vdid,
  596. vdid, SPIFLASH_VDID_LEN, flash->param->tshsl);
  597. }
  598. /*
  599. * Flash data from memory buffer(s)
  600. */