|
@@ -20,6 +20,7 @@ struct spz_stream {
|
|
/* Note: available output data ends at zs->next_out */
|
|
/* Note: available output data ends at zs->next_out */
|
|
uint8_t *ibuf; /* Input buffer if compressed */
|
|
uint8_t *ibuf; /* Input buffer if compressed */
|
|
uint8_t *obuf; /* Output buffer */
|
|
uint8_t *obuf; /* Output buffer */
|
|
|
|
+ uint8_t *dbuf; /* Block data buffer */
|
|
uint8_t *vbuf; /* Readback/verify buffer */
|
|
uint8_t *vbuf; /* Readback/verify buffer */
|
|
const struct spiflash_header *header;
|
|
const struct spiflash_header *header;
|
|
uint32_t crc32; /* Input data CRC32 */
|
|
uint32_t crc32; /* Input data CRC32 */
|
|
@@ -149,10 +150,11 @@ static int spiflash_data_init(spz_stream *spz)
|
|
int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
|
|
int (*read_data)(void *cookie, void *buf, unsigned int bufsize);
|
|
uint32_t header_crc;
|
|
uint32_t header_crc;
|
|
|
|
|
|
- MSG("update: initializing... ");
|
|
|
|
|
|
+ MSG("update: ");
|
|
|
|
|
|
|
|
+ spz->dbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
|
|
spz->vbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
|
|
spz->vbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
|
|
- if (!spz->vbuf)
|
|
|
|
|
|
+ if (!spz->dbuf || !spz->vbuf)
|
|
goto err;
|
|
goto err;
|
|
|
|
|
|
rlen = spz->zs.avail_in;
|
|
rlen = spz->zs.avail_in;
|
|
@@ -169,8 +171,14 @@ static int spiflash_data_init(spz_stream *spz)
|
|
}
|
|
}
|
|
if (spz->err)
|
|
if (spz->err)
|
|
goto err;
|
|
goto err;
|
|
|
|
+ if (!rlen) {
|
|
|
|
+ MSG("done");
|
|
|
|
+ spz->err = Z_STREAM_END;
|
|
|
|
+ goto not_err;
|
|
|
|
+ }
|
|
if (rlen < (int)sizeof *spz->header) {
|
|
if (rlen < (int)sizeof *spz->header) {
|
|
- spz->err = rlen ? Z_STREAM_ERROR : Z_STREAM_END;
|
|
|
|
|
|
+ MSG("input underrun");
|
|
|
|
+ spz->err = Z_STREAM_ERROR;
|
|
goto err;
|
|
goto err;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -181,25 +189,31 @@ static int spiflash_data_init(spz_stream *spz)
|
|
* Check header magic and CRC
|
|
* Check header magic and CRC
|
|
*/
|
|
*/
|
|
if (spz->header->magic != SPIFLASH_MAGIC) {
|
|
if (spz->header->magic != SPIFLASH_MAGIC) {
|
|
|
|
+ MSG("bad header magic");
|
|
spz->err = Z_STREAM_ERROR;
|
|
spz->err = Z_STREAM_ERROR;
|
|
goto err;
|
|
goto err;
|
|
}
|
|
}
|
|
header_crc = crc32(0, NULL, 0);
|
|
header_crc = crc32(0, NULL, 0);
|
|
header_crc = crc32(header_crc, (const void *)spz->header,
|
|
header_crc = crc32(header_crc, (const void *)spz->header,
|
|
- sizeof spz->header - 4);
|
|
|
|
- if (header_crc != spz->header->header_crc32 ||
|
|
|
|
- spz->header->zlen > spz->header->dlen ||
|
|
|
|
- (spz->eoi && (int)spz->header->zlen > rlen)) {
|
|
|
|
|
|
+ sizeof *spz->header - 4);
|
|
|
|
+ if (header_crc != spz->header->header_crc32) {
|
|
|
|
+ MSG("header CRC error (0x%08x vs 0x%08x)...",
|
|
|
|
+ header_crc, spz->header->header_crc32);
|
|
spz->err = Z_STREAM_ERROR;
|
|
spz->err = Z_STREAM_ERROR;
|
|
goto err;
|
|
goto err;
|
|
}
|
|
}
|
|
if (!spz->header->dlen) {
|
|
if (!spz->header->dlen) {
|
|
/* End of data */
|
|
/* End of data */
|
|
spz->err = Z_STREAM_END;
|
|
spz->err = Z_STREAM_END;
|
|
|
|
+ goto not_err;
|
|
|
|
+ }
|
|
|
|
+ if (spz->header->zlen > spz->header->dlen ||
|
|
|
|
+ (spz->eoi && (int)spz->header->zlen > rlen)) {
|
|
|
|
+ spz->err = Z_STREAM_ERROR;
|
|
goto err;
|
|
goto err;
|
|
}
|
|
}
|
|
|
|
|
|
- MSG("updating 0x%06x..%06x (%u bytes)... ",
|
|
|
|
|
|
+ MSG("data 0x%06x..0x%06x (%u bytes)\n",
|
|
spz->header->address, spz->header->address + spz->header->dlen - 1,
|
|
spz->header->address, spz->header->address + spz->header->dlen - 1,
|
|
spz->header->dlen);
|
|
spz->header->dlen);
|
|
|
|
|
|
@@ -251,7 +265,11 @@ static int spiflash_data_init(spz_stream *spz)
|
|
}
|
|
}
|
|
|
|
|
|
err:
|
|
err:
|
|
- MSG("%s\n", spz->err ? "failed" : "ok");
|
|
|
|
|
|
+ if (spz->err)
|
|
|
|
+ MSG(" failed (err %d)\n", spz->err);
|
|
|
|
+
|
|
|
|
+not_err:
|
|
|
|
+ MSG("\n");
|
|
return spz->err;
|
|
return spz->err;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -276,6 +294,8 @@ static int spiflash_data_cleanup(spz_stream *spz)
|
|
free((void *)spz->header);
|
|
free((void *)spz->header);
|
|
if (spz->vbuf)
|
|
if (spz->vbuf)
|
|
free(spz->vbuf);
|
|
free(spz->vbuf);
|
|
|
|
+ if (spz->dbuf)
|
|
|
|
+ free(spz->dbuf);
|
|
if (spz->obuf)
|
|
if (spz->obuf)
|
|
free(spz->obuf);
|
|
free(spz->obuf);
|
|
if (spz->ibuf)
|
|
if (spz->ibuf)
|
|
@@ -301,7 +321,6 @@ static void *spiflash_setup_addrcmd(const struct spiflash *flash,
|
|
mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
|
|
mode = addr < (1 << 24) ? SPIFLASH_ADDR_24BIT : SPIFLASH_ADDR_32BIT;
|
|
|
|
|
|
if (mode == SPIFLASH_ADDR_24BIT) {
|
|
if (mode == SPIFLASH_ADDR_24BIT) {
|
|
- addr <<= 8;
|
|
|
|
*cmd++ = cmd24;
|
|
*cmd++ = cmd24;
|
|
} else {
|
|
} else {
|
|
*cmd++ = cmd32;
|
|
*cmd++ = cmd32;
|
|
@@ -314,19 +333,28 @@ static void *spiflash_setup_addrcmd(const struct spiflash *flash,
|
|
return cmd;
|
|
return cmd;
|
|
}
|
|
}
|
|
|
|
|
|
-static int spiflash_wait_ready(const struct spiflash *flash, int delay)
|
|
|
|
|
|
+static int spiflash_get_status(const struct spiflash *flash,
|
|
|
|
+ uint8_t cmd, uint8_t *sr)
|
|
|
|
+{
|
|
|
|
+ return flash->ops->spi_read(flash->cookie, &cmd, 1, sr, 1,
|
|
|
|
+ flash->param->tshsl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* This needs a timeout function */
|
|
|
|
+static int spiflash_wait_status(const struct spiflash *flash,
|
|
|
|
+ int delay, uint8_t mask, uint8_t val)
|
|
{
|
|
{
|
|
- uint8_t cmd = 0x05; /* Read Status Register 1 */
|
|
|
|
- uint8_t sr1 = 0;
|
|
|
|
|
|
+ uint8_t sr1;
|
|
int rv;
|
|
int rv;
|
|
|
|
|
|
do {
|
|
do {
|
|
- flash->ops->yield(flash->cookie, delay);
|
|
|
|
- rv = flash->ops->spi_read(flash->cookie, &cmd, 1,
|
|
|
|
- &sr1, 1, flash->param->tshsl);
|
|
|
|
|
|
+ if (flash->ops->yield)
|
|
|
|
+ flash->ops->yield(flash->cookie, delay);
|
|
|
|
+
|
|
|
|
+ rv = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
|
|
if (rv)
|
|
if (rv)
|
|
return rv;
|
|
return rv;
|
|
- } while (sr1 & 0x01); /* Busy bit set? */
|
|
|
|
|
|
+ } while ((sr1 & mask) != val); /* Waiting... */
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
@@ -337,25 +365,37 @@ int spiflash_read(const struct spiflash *flash,
|
|
uint8_t cmdbuf[6];
|
|
uint8_t cmdbuf[6];
|
|
uint8_t *cmd;
|
|
uint8_t *cmd;
|
|
|
|
|
|
- /*
|
|
|
|
- * 13h = Fast Read
|
|
|
|
- * 0Ch = Fast Read with 4-Byte Address
|
|
|
|
- */
|
|
|
|
- cmd = spiflash_setup_addrcmd(flash, addr, 0x13, 0x0c, cmdbuf);
|
|
|
|
- *cmd++ = 0; /* Dummy bits */
|
|
|
|
|
|
+ cmd = spiflash_setup_addrcmd(flash, addr,
|
|
|
|
+ ROM_FAST_READ, ROM_FAST_READ_32BIT,
|
|
|
|
+ cmdbuf);
|
|
|
|
+ *cmd++ = 0; /* Dummy cycles */
|
|
|
|
|
|
return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
return flash->ops->spi_read(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
buffer, len, flash->param->tshsl1);
|
|
buffer, len, flash->param->tshsl1);
|
|
}
|
|
}
|
|
|
|
|
|
-static int spiflash_write_enable(const struct spiflash *flash)
|
|
|
|
|
|
+static int spiflash_simple_command(const struct spiflash *flash, uint8_t cmd)
|
|
{
|
|
{
|
|
- const uint8_t cmd = 0x06; /* 06h = Write Enable */
|
|
|
|
-
|
|
|
|
return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
|
|
return flash->ops->spi_write(flash->cookie, &cmd, 1, NULL, 0,
|
|
flash->param->tshsl);
|
|
flash->param->tshsl);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static int spiflash_write_enable(const struct spiflash *flash)
|
|
|
|
+{
|
|
|
|
+ uint8_t sr1;
|
|
|
|
+ int rv;
|
|
|
|
+
|
|
|
|
+ rv = spiflash_wait_status(flash, 0, 1, 0);
|
|
|
|
+ if (rv)
|
|
|
|
+ return rv;
|
|
|
|
+
|
|
|
|
+ rv = spiflash_simple_command(flash, ROM_WRITE_ENABLE);
|
|
|
|
+ if (rv)
|
|
|
|
+ return rv;
|
|
|
|
+
|
|
|
|
+ return spiflash_wait_status(flash, 0, 3, 2);
|
|
|
|
+}
|
|
|
|
+
|
|
static int spiflash_program(const struct spiflash *flash,
|
|
static int spiflash_program(const struct spiflash *flash,
|
|
uint32_t addr, const void *buffer, size_t len)
|
|
uint32_t addr, const void *buffer, size_t len)
|
|
{
|
|
{
|
|
@@ -367,17 +407,15 @@ static int spiflash_program(const struct spiflash *flash,
|
|
if (rv)
|
|
if (rv)
|
|
return rv;
|
|
return rv;
|
|
|
|
|
|
- /*
|
|
|
|
- * 02h = Page Program
|
|
|
|
- * 12h = Page Program with 4-Byte Address
|
|
|
|
- */
|
|
|
|
- cmd = spiflash_setup_addrcmd(flash, addr, 0x02, 0x12, cmdbuf);
|
|
|
|
|
|
+ cmd = spiflash_setup_addrcmd(flash, addr,
|
|
|
|
+ ROM_PAGE_PROGRAM, ROM_PAGE_PROGRAM_32BIT,
|
|
|
|
+ cmdbuf);
|
|
rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
buffer, len, flash->param->tshsl2);
|
|
buffer, len, flash->param->tshsl2);
|
|
if (rv)
|
|
if (rv)
|
|
return rv;
|
|
return rv;
|
|
|
|
|
|
- return spiflash_wait_ready(flash, flash->param->tpp);
|
|
|
|
|
|
+ return spiflash_wait_status(flash, flash->param->tpp, 3, 0);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -389,56 +427,55 @@ static int spiflash_erase(const struct spiflash *flash,
|
|
uint8_t cmdbuf[5];
|
|
uint8_t cmdbuf[5];
|
|
uint8_t *cmd;
|
|
uint8_t *cmd;
|
|
uint8_t cmd24, cmd32;
|
|
uint8_t cmd24, cmd32;
|
|
- uint32_t nextaddr;
|
|
|
|
|
|
+ uint32_t erasesize;
|
|
int rv;
|
|
int rv;
|
|
int delay;
|
|
int delay;
|
|
const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
|
|
const uint32_t block_mask = SPIFLASH_BLOCK_SIZE - 1;
|
|
- const unsigned long block_sectors = block_mask >> SPIFLASH_SECTOR_SHIFT;
|
|
|
|
|
|
+ const unsigned long block_sector_mask
|
|
|
|
+ = block_mask >> SPIFLASH_SECTOR_SHIFT;
|
|
|
|
|
|
|
|
+ if (!sector_mask) {
|
|
|
|
+ MSG("update: nothing to erase\n");
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
while (sector_mask) {
|
|
while (sector_mask) {
|
|
- if (((sector_mask & block_sectors) == block_sectors) &&
|
|
|
|
- !(addr & block_mask)) {
|
|
|
|
- /*
|
|
|
|
- * D8h = 64KB Block Erase
|
|
|
|
- * DCh = 64K Block Erase with 4-Byte Address
|
|
|
|
- */
|
|
|
|
- cmd24 = 0xd8; cmd32 = 0xdc;
|
|
|
|
|
|
+ if (!(addr & block_mask) &&
|
|
|
|
+ ((sector_mask & block_sector_mask) == block_sector_mask)) {
|
|
|
|
+ cmd24 = ROM_ERASE_64K;
|
|
|
|
+ cmd32 = ROM_ERASE_64K_32BIT;
|
|
delay = flash->param->tbe2;
|
|
delay = flash->param->tbe2;
|
|
- nextaddr = addr + SPIFLASH_BLOCK_SIZE;
|
|
|
|
- sector_mask >>= 16;
|
|
|
|
|
|
+ erasesize = SPIFLASH_BLOCK_SIZE;
|
|
} else {
|
|
} else {
|
|
- if (sector_mask & 1) {
|
|
|
|
- /*
|
|
|
|
- * 20h = Sector Erase
|
|
|
|
- * 21h = Sector Erase with 4-Byte Address
|
|
|
|
- */
|
|
|
|
- cmd24 = 0x20; cmd32 = 0x21;
|
|
|
|
- delay = flash->param->tse;
|
|
|
|
- nextaddr = addr + SPIFLASH_SECTOR_SIZE;
|
|
|
|
- sector_mask >>= 1;
|
|
|
|
- } else {
|
|
|
|
- addr += SPIFLASH_SECTOR_SIZE;
|
|
|
|
- sector_mask >>= 1;
|
|
|
|
- continue; /* Skip sector */
|
|
|
|
- }
|
|
|
|
|
|
+ cmd24 = ROM_ERASE_4K;
|
|
|
|
+ cmd32 = ROM_ERASE_4K_32BIT;
|
|
|
|
+ delay = flash->param->tse;
|
|
|
|
+ erasesize = SPIFLASH_SECTOR_SIZE;
|
|
}
|
|
}
|
|
- rv = spiflash_write_enable(flash);
|
|
|
|
- if (rv)
|
|
|
|
- return rv;
|
|
|
|
|
|
|
|
- cmd = spiflash_setup_addrcmd(flash, addr, cmd24, cmd32, cmdbuf);
|
|
|
|
- rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
|
|
- NULL, 0, flash->param->tshsl2);
|
|
|
|
- if (rv)
|
|
|
|
- return rv;
|
|
|
|
|
|
+ if (sector_mask & 1) {
|
|
|
|
+ MSG("\rupdate: erasing %2uK at 0x%06x... ", erasesize >> 10, addr);
|
|
|
|
+
|
|
|
|
+ rv = spiflash_write_enable(flash);
|
|
|
|
+ if (rv)
|
|
|
|
+ return rv;
|
|
|
|
|
|
- rv = spiflash_wait_ready(flash, delay);
|
|
|
|
- if (rv)
|
|
|
|
- return rv;
|
|
|
|
|
|
+ cmd = spiflash_setup_addrcmd(flash, addr, cmd24, cmd32, cmdbuf);
|
|
|
|
+ rv = flash->ops->spi_write(flash->cookie, cmdbuf, cmd - cmdbuf,
|
|
|
|
+ NULL, 0, flash->param->tshsl2);
|
|
|
|
+ if (rv)
|
|
|
|
+ return rv;
|
|
|
|
+
|
|
|
|
+ rv = spiflash_wait_status(flash, delay, 3, 0);
|
|
|
|
+ if (rv)
|
|
|
|
+ return rv;
|
|
|
|
+ }
|
|
|
|
|
|
- addr = nextaddr;
|
|
|
|
|
|
+ addr += erasesize;
|
|
|
|
+ sector_mask >>= (erasesize >> SPIFLASH_SECTOR_SHIFT);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ MSG("ok\n");
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -460,26 +497,26 @@ spiflash_memcmp(const void *from, const void *to, size_t len)
|
|
const uint32_t *pf = from;
|
|
const uint32_t *pf = from;
|
|
const uint32_t *pt = to;
|
|
const uint32_t *pt = to;
|
|
const uint32_t *pfend = (const uint32_t *)((const char *)from + len);
|
|
const uint32_t *pfend = (const uint32_t *)((const char *)from + len);
|
|
- uint32_t notok = 0;
|
|
|
|
- uint32_t notprog = 0;
|
|
|
|
|
|
+ uint32_t doprog = 0;
|
|
|
|
+ uint32_t doerase = 0;
|
|
|
|
|
|
while (pf < pfend) {
|
|
while (pf < pfend) {
|
|
uint32_t f = *pf++;
|
|
uint32_t f = *pf++;
|
|
uint32_t t = *pt++;
|
|
uint32_t t = *pt++;
|
|
|
|
|
|
- notok |= f ^ t; /* Need programming if any data mismatch */
|
|
|
|
- notprog |= ~f & t; /* Need erasing if any 0 -> 1 */
|
|
|
|
|
|
+ doprog |= f ^ t; /* Need programming if any data mismatch */
|
|
|
|
+ doerase |= ~f & t; /* Need erasing if any 0 -> 1 */
|
|
}
|
|
}
|
|
|
|
|
|
- return notprog ? FMS_ERASE : notok ? FMS_PROGRAM : FMS_DONE;
|
|
|
|
|
|
+ return doerase ? FMS_ERASE : doprog ? FMS_PROGRAM : FMS_DONE;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
* Check a block for sectors which need erasing and pages which need
|
|
* Check a block for sectors which need erasing and pages which need
|
|
* programming; the prog_mask is 256 bits long and so span multiple words.
|
|
* programming; the prog_mask is 256 bits long and so span multiple words.
|
|
*
|
|
*
|
|
- * The input is spz->optr, and the existing flash content is written
|
|
|
|
- * to spz->vptr.
|
|
|
|
|
|
+ * The desired input is spz->dbuf and the existing flash content is
|
|
|
|
+ * written to spz->vbuf.
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
static int spiflash_check_block(spz_stream *spz, uint32_t addr,
|
|
static int spiflash_check_block(spz_stream *spz, uint32_t addr,
|
|
@@ -496,8 +533,11 @@ static int spiflash_check_block(spz_stream *spz, uint32_t addr,
|
|
return rv;
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
|
|
- p = spz->optr;
|
|
|
|
- q = spz->vbuf;
|
|
|
|
|
|
+ *erase_mask = 0;
|
|
|
|
+ memset(prog_mask, 0, SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE/8);
|
|
|
|
+
|
|
|
|
+ p = spz->vbuf;
|
|
|
|
+ q = spz->dbuf;
|
|
|
|
|
|
for (page = 0; page < SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE; page++) {
|
|
for (page = 0; page < SPIFLASH_BLOCK_SIZE/SPIFLASH_PAGE_SIZE; page++) {
|
|
enum flashmem_status status;
|
|
enum flashmem_status status;
|
|
@@ -522,19 +562,87 @@ static int spiflash_check_block(spz_stream *spz, uint32_t addr,
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/* Serial Flash Discoverable Parameter Table, see JESD216 */
|
|
|
|
+static int spiflash_get_sfdp(const struct spiflash *flash, void *sfdp)
|
|
|
|
+{
|
|
|
|
+ static const uint8_t cmd_read_sfdp[] = { ROM_READ_SFDP, 0, 0, 0, 0 };
|
|
|
|
+
|
|
|
|
+ return flash->ops->spi_read(flash->cookie, cmd_read_sfdp,
|
|
|
|
+ sizeof cmd_read_sfdp, sfdp, SPIFLASH_SFDP_SIZE,
|
|
|
|
+ flash->param->tshsl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
{
|
|
{
|
|
spz_stream _spz;
|
|
spz_stream _spz;
|
|
spz_stream * const spz = &_spz; /* For consistency in notation */
|
|
spz_stream * const spz = &_spz; /* For consistency in notation */
|
|
- int err;
|
|
|
|
|
|
+ int err = 0;
|
|
enum flashmem_status fs;
|
|
enum flashmem_status fs;
|
|
- uint8_t *padbuf = NULL;
|
|
|
|
|
|
|
|
- err = 0;
|
|
|
|
|
|
+#if 0
|
|
|
|
+ static const uint8_t read_sr_cmd[2] = { ROM_READ_SR1, ROM_READ_SR2 };
|
|
|
|
+ uint8_t sr1;
|
|
|
|
+ uint32_t *sfdp;
|
|
|
|
+
|
|
|
|
+ sfdp = malloc(SPIFLASH_SFDP_SIZE);
|
|
|
|
+ memset(sfdp, 0, SPIFLASH_SFDP_SIZE);
|
|
|
|
+
|
|
|
|
+ /* Note: SFDP data is littleendian! */
|
|
|
|
+ err = spiflash_get_sfdp(flash, sfdp);
|
|
|
|
+ if (err)
|
|
|
|
+ return err;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < SPIFLASH_SFDP_SIZE; i += 16) {
|
|
|
|
+ MSG("%04x :", i);
|
|
|
|
+ for (int j = 0; j < 16; j += 4) {
|
|
|
|
+ MSG(" %08x", sfdp[(i+j) >> 2]);
|
|
|
|
+ }
|
|
|
|
+ MSG("\n");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (sfdp[0] != 0x50444653) {
|
|
|
|
+ MSG("update: invalid SFDP information read\n");
|
|
|
|
+ return SPIFLASH_ERR_DETECT;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * If the flash is busy, try to reset it
|
|
|
|
+ */
|
|
|
|
+ err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
|
|
|
|
+ if (err)
|
|
|
|
+ return err;
|
|
|
|
+ if (sr1 & 0x01) {
|
|
|
|
+ udelay(60);
|
|
|
|
+ err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
|
|
|
|
+ if (err)
|
|
|
|
+ return err;
|
|
|
|
+ if (sr1 & 0x01) {
|
|
|
|
+ MSG("update: flash busy, trying reset... ");
|
|
|
|
+
|
|
|
|
+ err = spiflash_simple_command(flash, ROM_ENABLE_RESET);
|
|
|
|
+ if (err)
|
|
|
|
+ return err;
|
|
|
|
+ err = spiflash_simple_command(flash, ROM_RESET);
|
|
|
|
+ if (err)
|
|
|
|
+ return err;
|
|
|
|
+
|
|
|
|
+ udelay(60);
|
|
|
|
+ err = spiflash_get_status(flash, ROM_READ_SR1, &sr1);
|
|
|
|
+ if (err || (sr1 & 0x01)) {
|
|
|
|
+ MSG("failed\n");
|
|
|
|
+ return SPIFLASH_ERR_NOT_READY;
|
|
|
|
+ }
|
|
|
|
+ MSG("ok\n");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+#endif
|
|
|
|
+
|
|
while (!err) {
|
|
while (!err) {
|
|
int rv;
|
|
int rv;
|
|
- bool eof;
|
|
|
|
uint32_t addr;
|
|
uint32_t addr;
|
|
|
|
+ uint32_t data_left;
|
|
|
|
|
|
memset(spz, 0, sizeof *spz);
|
|
memset(spz, 0, sizeof *spz);
|
|
spz->zs.avail_in = buflen;
|
|
spz->zs.avail_in = buflen;
|
|
@@ -546,13 +654,13 @@ int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
|
|
|
|
if (!spz->ibuf) {
|
|
if (!spz->ibuf) {
|
|
/* No ibuf allocated, feeding from raw data buffer */
|
|
/* No ibuf allocated, feeding from raw data buffer */
|
|
- unsigned int bufskip = (spz->zs.next_out - (uint8_t *)buf + 3) & ~3;
|
|
|
|
|
|
+ unsigned int bufskip = (spz->header->zlen + sizeof *spz->header + 3) & ~3;
|
|
if (bufskip >= buflen) {
|
|
if (bufskip >= buflen) {
|
|
buflen = 0;
|
|
buflen = 0;
|
|
buf = NULL;
|
|
buf = NULL;
|
|
} else {
|
|
} else {
|
|
buflen -= bufskip;
|
|
buflen -= bufskip;
|
|
- buf = spz->zs.next_out;
|
|
|
|
|
|
+ buf += bufskip;
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
/* Buffer exhausted, additional data read */
|
|
/* Buffer exhausted, additional data read */
|
|
@@ -560,39 +668,48 @@ int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
buf = NULL;
|
|
buf = NULL;
|
|
}
|
|
}
|
|
|
|
|
|
- eof = false;
|
|
|
|
- addr = spz->header->address;
|
|
|
|
|
|
+ data_left = spz->header->dlen;
|
|
|
|
+ addr = spz->header->address;
|
|
|
|
|
|
- while (!eof && !spz->err) {
|
|
|
|
- unsigned int bytes = spz->zs.next_out - spz->optr;
|
|
|
|
|
|
+ while (data_left && !spz->err) {
|
|
|
|
+ unsigned int bytes = 0;
|
|
unsigned int padding;
|
|
unsigned int padding;
|
|
-
|
|
|
|
- if (bytes < SPIFLASH_BLOCK_SIZE) {
|
|
|
|
|
|
+
|
|
|
|
+ while (data_left && bytes < SPIFLASH_BLOCK_SIZE) {
|
|
|
|
+ unsigned int avail = spz->zs.next_out - spz->optr;
|
|
|
|
+ unsigned int need = SPIFLASH_BLOCK_SIZE - bytes;
|
|
int rv;
|
|
int rv;
|
|
|
|
|
|
|
|
+ if (need > data_left)
|
|
|
|
+ need = data_left;
|
|
|
|
+
|
|
|
|
+ if (avail) {
|
|
|
|
+ if (avail > need)
|
|
|
|
+ avail = need;
|
|
|
|
+
|
|
|
|
+ memcpy(spz->dbuf + bytes, spz->optr, avail);
|
|
|
|
+ spz->optr += avail;
|
|
|
|
+ bytes += avail;
|
|
|
|
+ data_left -= avail;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
rv = spz->read_data(spz);
|
|
rv = spz->read_data(spz);
|
|
|
|
+
|
|
if (spz->err)
|
|
if (spz->err)
|
|
goto err;
|
|
goto err;
|
|
- eof = !rv;
|
|
|
|
- bytes = spz->zs.next_out - spz->optr;
|
|
|
|
- if (!bytes)
|
|
|
|
- break;
|
|
|
|
-
|
|
|
|
- padding = -bytes & (SPIFLASH_BLOCK_SIZE-1);
|
|
|
|
- if (padding) {
|
|
|
|
- if (!padbuf) {
|
|
|
|
- padbuf = spz_malloc(spz, SPIFLASH_BLOCK_SIZE);
|
|
|
|
- if (!padbuf)
|
|
|
|
- goto err;
|
|
|
|
- }
|
|
|
|
- memcpy(padbuf, spz->optr, bytes);
|
|
|
|
- memset(padbuf+bytes, 0xff, padding);
|
|
|
|
- spz->optr = padbuf;
|
|
|
|
- eof = true;
|
|
|
|
|
|
+
|
|
|
|
+ if (!rv) {
|
|
|
|
+ spz->err = Z_STREAM_ERROR; /* Input underrun */
|
|
|
|
+ goto err;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- MSG("update: flash block at 0x%06x: ", addr);
|
|
|
|
|
|
+ if (bytes < SPIFLASH_BLOCK_SIZE) {
|
|
|
|
+ memset(spz->dbuf + bytes, 0xff, SPIFLASH_BLOCK_SIZE - bytes);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ MSG("update: flash block at 0x%06x (%5u bytes):\n", addr, bytes);
|
|
|
|
|
|
uint32_t erase_mask;
|
|
uint32_t erase_mask;
|
|
uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
|
|
uint32_t prog_mask[SPIFLASH_BLOCK_SIZE >> (SPIFLASH_PAGE_SHIFT+5)];
|
|
@@ -602,8 +719,6 @@ int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
goto err;
|
|
goto err;
|
|
|
|
|
|
if (erase_mask) {
|
|
if (erase_mask) {
|
|
- MSG("erasing... ");
|
|
|
|
-
|
|
|
|
rv = spiflash_erase(spz->flash, addr, erase_mask);
|
|
rv = spiflash_erase(spz->flash, addr, erase_mask);
|
|
if (rv) {
|
|
if (rv) {
|
|
spz->err = rv;
|
|
spz->err = rv;
|
|
@@ -614,69 +729,71 @@ int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
if (spz->err)
|
|
if (spz->err)
|
|
goto err;
|
|
goto err;
|
|
if (erase_mask) {
|
|
if (erase_mask) {
|
|
|
|
+ MSG("[erase mask = %04x] ", erase_mask);
|
|
spz->err = SPIFLASH_ERR_ERASE_FAILED;
|
|
spz->err = SPIFLASH_ERR_ERASE_FAILED;
|
|
goto err;
|
|
goto err;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
unsigned int page;
|
|
unsigned int page;
|
|
- bool first_prog = true;
|
|
|
|
|
|
+ bool programmed = false;
|
|
|
|
|
|
for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
|
|
for (page = 0; page < (SPIFLASH_BLOCK_SIZE >> SPIFLASH_PAGE_SHIFT);
|
|
page++) {
|
|
page++) {
|
|
uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
|
|
uint32_t page_offs = page << SPIFLASH_PAGE_SHIFT;
|
|
|
|
|
|
- if (prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))) {
|
|
|
|
-
|
|
|
|
- if (first_prog) {
|
|
|
|
- MSG("programming %06x... ", addr + page_offs);
|
|
|
|
- } else {
|
|
|
|
- MSG("\b\b\b\b\b\b\b\b\b\b%06x... ", addr+page_offs);
|
|
|
|
- }
|
|
|
|
- first_prog = false;
|
|
|
|
-
|
|
|
|
- rv = spiflash_program(spz->flash, addr + page_offs,
|
|
|
|
- spz->optr + page_offs,
|
|
|
|
- SPIFLASH_PAGE_SIZE);
|
|
|
|
- if (rv) {
|
|
|
|
- spz->err = rv;
|
|
|
|
- goto err;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /* Read back data and verify */
|
|
|
|
- rv = spiflash_read(spz->flash, addr+page_offs,
|
|
|
|
- spz->vbuf + page_offs,
|
|
|
|
- SPIFLASH_PAGE_SIZE);
|
|
|
|
- if (rv) {
|
|
|
|
- MSG("verify "); /* "verify failed" */
|
|
|
|
- spz->err = rv;
|
|
|
|
- goto err;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (memcmp(spz->optr + page_offs, spz->vbuf + page_offs,
|
|
|
|
- SPIFLASH_PAGE_SIZE)) {
|
|
|
|
- spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
|
|
|
|
- goto err;
|
|
|
|
- }
|
|
|
|
|
|
+ if (!(prog_mask[page >> 5] & (UINT32_C(1) << (page & 31))))
|
|
|
|
+ continue; /* No need to program */
|
|
|
|
+
|
|
|
|
+ programmed = true;
|
|
|
|
+
|
|
|
|
+ udelay(10000);
|
|
|
|
+
|
|
|
|
+ MSG("\rupdate: writing at 0x%06x... ", addr + page_offs);
|
|
|
|
+
|
|
|
|
+ rv = spiflash_program(spz->flash, addr + page_offs,
|
|
|
|
+ spz->dbuf + page_offs,
|
|
|
|
+ SPIFLASH_PAGE_SIZE);
|
|
|
|
+ if (rv) {
|
|
|
|
+ spz->err = rv;
|
|
|
|
+ goto err;
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- spz->optr += SPIFLASH_BLOCK_SIZE;
|
|
|
|
- addr += SPIFLASH_BLOCK_SIZE;
|
|
|
|
|
|
+ rv = spiflash_read(spz->flash, addr + page_offs,
|
|
|
|
+ spz->vbuf + page_offs,
|
|
|
|
+ SPIFLASH_PAGE_SIZE);
|
|
|
|
+ if (rv) {
|
|
|
|
+ MSG("readback ");
|
|
|
|
+ goto err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (memcmp(spz->dbuf + page_offs, spz->vbuf + page_offs,
|
|
|
|
+ SPIFLASH_PAGE_SIZE)) {
|
|
|
|
+ MSG("verify @ 0x%06x ", addr + page_offs);
|
|
|
|
+ spz->err = SPIFLASH_ERR_PROGRAM_FAILED;
|
|
|
|
+ goto err;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (programmed)
|
|
|
|
+ MSG("ok\n");
|
|
|
|
+ else
|
|
|
|
+ MSG("update: nothing to write\n");
|
|
|
|
+
|
|
|
|
+ addr += SPIFLASH_BLOCK_SIZE;
|
|
}
|
|
}
|
|
|
|
|
|
err:
|
|
err:
|
|
err = spiflash_data_cleanup(spz);
|
|
err = spiflash_data_cleanup(spz);
|
|
}
|
|
}
|
|
|
|
|
|
- if (padbuf) {
|
|
|
|
- free(padbuf);
|
|
|
|
- padbuf = NULL;
|
|
|
|
- }
|
|
|
|
|
|
+ if (err == Z_STREAM_END)
|
|
|
|
+ err = 0; /* End of data is not an error */
|
|
|
|
|
|
- MSG("%s\n", spz->err ? "failed" : "ok");
|
|
|
|
|
|
+ if (err)
|
|
|
|
+ MSG("failed (err %d)\n", err);
|
|
|
|
|
|
- return spz->err;
|
|
|
|
|
|
+ return err;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
/*
|
|
@@ -685,7 +802,7 @@ int spiflash_flash_files(const struct spiflash *flash, void *buf, size_t buflen)
|
|
*/
|
|
*/
|
|
int spiflash_read_id(const struct spiflash *flash, void *id)
|
|
int spiflash_read_id(const struct spiflash *flash, void *id)
|
|
{
|
|
{
|
|
- static const uint8_t read_unique_id[] = { 0x4b, 0, 0, 0, 0 };
|
|
|
|
|
|
+ static const uint8_t read_unique_id[] = { ROM_READ_UNIQUE_ID, 0, 0, 0, 0 };
|
|
|
|
|
|
return flash->ops->spi_read(flash->cookie, read_unique_id,
|
|
return flash->ops->spi_read(flash->cookie, read_unique_id,
|
|
sizeof read_unique_id,
|
|
sizeof read_unique_id,
|
|
@@ -697,7 +814,7 @@ int spiflash_read_id(const struct spiflash *flash, void *id)
|
|
*/
|
|
*/
|
|
int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
|
|
int spiflash_read_vdid(const struct spiflash *flash, void *vdid)
|
|
{
|
|
{
|
|
- static const uint8_t read_vdid[] = { 0x90, 0, 0, 0 };
|
|
|
|
|
|
+ static const uint8_t read_vdid[] = { ROM_MANUFACTURER_DEVICE_ID, 0, 0, 0 };
|
|
|
|
|
|
return flash->ops->spi_read(flash->cookie, read_vdid,
|
|
return flash->ops->spi_read(flash->cookie, read_vdid,
|
|
sizeof read_vdid,
|
|
sizeof read_vdid,
|