xfel/spinor.c

943 lines
24 KiB
C
Raw Normal View History

#include <spinor.h>
2021-06-12 14:09:43 +08:00
struct spinor_info_t {
char * name;
uint32_t id;
uint32_t capacity;
uint32_t blksz;
uint32_t read_granularity;
uint32_t write_granularity;
uint8_t address_length;
uint8_t opcode_read;
uint8_t opcode_write;
uint8_t opcode_write_enable;
uint8_t opcode_erase_4k;
uint8_t opcode_erase_32k;
uint8_t opcode_erase_64k;
uint8_t opcode_erase_256k;
};
struct spinor_pdata_t {
struct spinor_info_t info;
uint32_t swapbuf;
uint32_t swaplen;
2021-06-13 01:29:25 +08:00
uint32_t cmdlen;
2021-06-12 14:09:43 +08:00
};
2021-06-05 17:18:58 +08:00
enum {
OPCODE_SFDP = 0x5a,
OPCODE_RDID = 0x9f,
OPCODE_WRSR = 0x01,
OPCODE_RDSR = 0x05,
OPCODE_WREN = 0x06,
OPCODE_READ = 0x03,
OPCODE_PROG = 0x02,
OPCODE_E4K = 0x20,
OPCODE_E32K = 0x52,
OPCODE_E64K = 0xd8,
OPCODE_ENTER_4B = 0xb7,
OPCODE_EXIT_4B = 0xe9,
};
#define SFDP_MAX_NPH (6)
struct sfdp_header_t {
uint8_t sign[4];
uint8_t minor;
uint8_t major;
uint8_t nph;
uint8_t unused;
};
struct sfdp_parameter_header_t {
uint8_t idlsb;
uint8_t minor;
uint8_t major;
uint8_t length;
uint8_t ptp[3];
uint8_t idmsb;
};
struct sfdp_basic_table_t {
uint8_t minor;
uint8_t major;
uint8_t table[16 * 4];
};
struct sfdp_t {
struct sfdp_header_t h;
struct sfdp_parameter_header_t ph[SFDP_MAX_NPH];
struct sfdp_basic_table_t bt;
};
static const struct spinor_info_t spinor_infos[] = {
{ "w25x40", 0xef3013, 512 * 1024, 4096, 1, 256, 3, OPCODE_READ, OPCODE_PROG, OPCODE_WREN, OPCODE_E4K, 0, OPCODE_E64K, 0 },
};
2021-06-13 01:29:25 +08:00
static inline int spinor_read_sfdp(struct xfel_ctx_t * ctx, uint32_t swapbuf, uint32_t swaplen, uint32_t cmdlen, struct sfdp_t * sfdp)
2021-06-05 17:18:58 +08:00
{
uint32_t addr;
uint8_t tx[5];
2021-06-11 00:06:49 +08:00
int i;
2021-06-05 17:18:58 +08:00
memset(sfdp, 0, sizeof(struct sfdp_t));
tx[0] = OPCODE_SFDP;
tx[1] = 0x0;
tx[2] = 0x0;
tx[3] = 0x0;
tx[4] = 0x0;
2021-06-13 01:29:25 +08:00
if(!fel_spi_xfer(ctx, swapbuf, swaplen, cmdlen, tx, 5, &sfdp->h, sizeof(struct sfdp_header_t)))
2021-06-05 17:18:58 +08:00
return 0;
if((sfdp->h.sign[0] != 'S') || (sfdp->h.sign[1] != 'F') || (sfdp->h.sign[2] != 'D') || (sfdp->h.sign[3] != 'P'))
return 0;
sfdp->h.nph = sfdp->h.nph > SFDP_MAX_NPH ? sfdp->h.nph + 1 : SFDP_MAX_NPH;
for(i = 0; i < sfdp->h.nph; i++)
{
addr = i * sizeof(struct sfdp_parameter_header_t) + sizeof(struct sfdp_header_t);
tx[0] = OPCODE_SFDP;
tx[1] = (addr >> 16) & 0xff;
tx[2] = (addr >> 8) & 0xff;
tx[3] = (addr >> 0) & 0xff;
tx[4] = 0x0;
2021-06-13 01:29:25 +08:00
if(!fel_spi_xfer(ctx, swapbuf, swaplen, cmdlen, tx, 5, &sfdp->ph[i], sizeof(struct sfdp_parameter_header_t)))
2021-06-05 17:18:58 +08:00
return 0;
}
for(i = 0; i < sfdp->h.nph; i++)
{
if((sfdp->ph[i].idlsb == 0x00) && (sfdp->ph[i].idmsb == 0xff))
{
addr = (sfdp->ph[i].ptp[0] << 0) | (sfdp->ph[i].ptp[1] << 8) | (sfdp->ph[i].ptp[2] << 16);
tx[0] = OPCODE_SFDP;
tx[1] = (addr >> 16) & 0xff;
tx[2] = (addr >> 8) & 0xff;
tx[3] = (addr >> 0) & 0xff;
tx[4] = 0x0;
2021-06-13 01:29:25 +08:00
if(fel_spi_xfer(ctx, swapbuf, swaplen, cmdlen, tx, 5, &sfdp->bt.table[0], sfdp->ph[i].length * 4))
2021-06-05 17:18:58 +08:00
{
sfdp->bt.major = sfdp->ph[i].major;
sfdp->bt.minor = sfdp->ph[i].minor;
return 1;
}
}
}
return 0;
}
2021-06-13 01:29:25 +08:00
static inline int spinor_read_id(struct xfel_ctx_t * ctx, uint32_t swapbuf, uint32_t swaplen, uint32_t cmdlen, uint32_t * id)
{
2021-06-05 17:18:58 +08:00
uint8_t tx[1];
uint8_t rx[3];
tx[0] = OPCODE_RDID;
2021-06-13 01:29:25 +08:00
if(!fel_spi_xfer(ctx, swapbuf, swaplen, cmdlen, tx, 1, rx, 3))
2021-06-05 17:18:58 +08:00
return 0;
*id = (rx[0] << 16) | (rx[1] << 8) | (rx[2] << 0);
return 1;
}
2021-06-12 14:09:43 +08:00
static inline int spinor_info(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat)
2021-06-05 17:18:58 +08:00
{
const struct spinor_info_t * t;
struct sfdp_t sfdp;
uint32_t v, id;
int i;
2021-06-13 01:29:25 +08:00
if(spinor_read_sfdp(ctx, pdat->swapbuf, pdat->swaplen, pdat->cmdlen, &sfdp))
2021-06-05 17:18:58 +08:00
{
2021-06-12 14:09:43 +08:00
pdat->info.name = "";
pdat->info.id = 0;
2021-06-05 17:18:58 +08:00
/* Basic flash parameter table 2th dword */
v = (sfdp.bt.table[7] << 24) | (sfdp.bt.table[6] << 16) | (sfdp.bt.table[5] << 8) | (sfdp.bt.table[4] << 0);
if(v & (1 << 31))
{
v &= 0x7fffffff;
2021-06-12 14:09:43 +08:00
pdat->info.capacity = 1 << (v - 3);
2021-06-05 17:18:58 +08:00
}
else
{
2021-06-12 14:09:43 +08:00
pdat->info.capacity = (v + 1) >> 3;
2021-06-05 17:18:58 +08:00
}
/* Basic flash parameter table 1th dword */
v = (sfdp.bt.table[3] << 24) | (sfdp.bt.table[2] << 16) | (sfdp.bt.table[1] << 8) | (sfdp.bt.table[0] << 0);
2021-06-12 14:09:43 +08:00
if((pdat->info.capacity <= (16 * 1024 * 1024)) && (((v >> 17) & 0x3) != 0x2))
pdat->info.address_length = 3;
2021-06-05 17:18:58 +08:00
else
2021-06-12 14:09:43 +08:00
pdat->info.address_length = 4;
2021-06-05 17:18:58 +08:00
if(((v >> 0) & 0x3) == 0x1)
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
else
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = 0x00;
pdat->info.opcode_erase_32k = 0x00;
pdat->info.opcode_erase_64k = 0x00;
pdat->info.opcode_erase_256k = 0x00;
2021-06-05 17:18:58 +08:00
/* Basic flash parameter table 8th dword */
v = (sfdp.bt.table[31] << 24) | (sfdp.bt.table[30] << 16) | (sfdp.bt.table[29] << 8) | (sfdp.bt.table[28] << 0);
switch((v >> 0) & 0xff)
{
case 12:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 15:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_32k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 16:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_64k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 18:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_256k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
switch((v >> 16) & 0xff)
{
case 12:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 15:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_32k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 16:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_64k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 18:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_256k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
/* Basic flash parameter table 9th dword */
v = (sfdp.bt.table[35] << 24) | (sfdp.bt.table[34] << 16) | (sfdp.bt.table[33] << 8) | (sfdp.bt.table[32] << 0);
switch((v >> 0) & 0xff)
{
case 12:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 15:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_32k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 16:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_64k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 18:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_256k = (v >> 8) & 0xff;
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
switch((v >> 16) & 0xff)
{
case 12:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_4k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 15:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_32k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 16:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_64k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
case 18:
2021-06-12 14:09:43 +08:00
pdat->info.opcode_erase_256k = (v >> 24) & 0xff;
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
2021-06-12 14:09:43 +08:00
if(pdat->info.opcode_erase_4k != 0x00)
pdat->info.blksz = 4096;
else if(pdat->info.opcode_erase_32k != 0x00)
pdat->info.blksz = 32768;
else if(pdat->info.opcode_erase_64k != 0x00)
pdat->info.blksz = 65536;
else if(pdat->info.opcode_erase_256k != 0x00)
pdat->info.blksz = 262144;
pdat->info.opcode_write_enable = OPCODE_WREN;
pdat->info.read_granularity = 1;
pdat->info.opcode_read = OPCODE_READ;
2021-06-05 17:18:58 +08:00
if((sfdp.bt.major == 1) && (sfdp.bt.minor < 5))
{
/* Basic flash parameter table 1th dword */
v = (sfdp.bt.table[3] << 24) | (sfdp.bt.table[2] << 16) | (sfdp.bt.table[1] << 8) | (sfdp.bt.table[0] << 0);
if((v >> 2) & 0x1)
2021-06-12 14:09:43 +08:00
pdat->info.write_granularity = 64;
2021-06-05 17:18:58 +08:00
else
2021-06-12 14:09:43 +08:00
pdat->info.write_granularity = 1;
2021-06-05 17:18:58 +08:00
}
else if((sfdp.bt.major == 1) && (sfdp.bt.minor >= 5))
{
/* Basic flash parameter table 11th dword */
v = (sfdp.bt.table[43] << 24) | (sfdp.bt.table[42] << 16) | (sfdp.bt.table[41] << 8) | (sfdp.bt.table[40] << 0);
2021-06-12 14:09:43 +08:00
pdat->info.write_granularity = 1 << ((v >> 4) & 0xf);
2021-06-05 17:18:58 +08:00
}
2021-06-12 14:09:43 +08:00
pdat->info.opcode_write = OPCODE_PROG;
2021-06-05 17:18:58 +08:00
return 1;
}
2021-06-13 01:29:25 +08:00
else if(spinor_read_id(ctx, pdat->swapbuf, pdat->swaplen, pdat->cmdlen, &id) && (id != 0xffffff) && (id != 0))
2021-06-05 17:18:58 +08:00
{
for(i = 0; i < ARRAY_SIZE(spinor_infos); i++)
{
t = &spinor_infos[i];
if(id == t->id)
{
2021-06-12 14:09:43 +08:00
memcpy(&pdat->info, t, sizeof(struct spinor_info_t));
2021-06-05 17:18:58 +08:00
return 1;
}
}
2021-06-10 09:35:50 +08:00
printf("The spi nor flash '0x%x' is not yet supported\r\n", id);
2021-06-05 17:18:58 +08:00
}
return 0;
}
2021-06-12 14:09:43 +08:00
static int spinor_helper_init(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat)
{
uint8_t cbuf[256];
uint32_t clen = 0;
2021-06-12 21:02:21 +08:00
2021-06-13 01:29:25 +08:00
if(fel_spi_init(ctx, &pdat->swapbuf, &pdat->swaplen, &pdat->cmdlen) && spinor_info(ctx, pdat))
{
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* chip reset */
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 2;
cbuf[clen++] = 0x66;
cbuf[clen++] = 0x99;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* wait busy */
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* write enable */
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* write status */
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 2;
cbuf[clen++] = OPCODE_WRSR;
cbuf[clen++] = 0;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* wait busy */
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
2021-06-12 14:09:43 +08:00
if(pdat->info.address_length == 4)
{
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* write enable */
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* entern 4byte address */
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = OPCODE_ENTER_4B;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
2021-06-12 21:02:21 +08:00
/* spi select */
cbuf[clen++] = SPI_CMD_SELECT;
2021-06-12 21:02:21 +08:00
/* wait busy */
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
2021-06-12 21:02:21 +08:00
/* spi deselect */
cbuf[clen++] = SPI_CMD_DESELECT;
}
2021-06-12 21:02:21 +08:00
/* end */
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
{
fel_chip_spi_run(ctx, cbuf, clen);
return 1;
}
}
return 0;
2021-06-05 17:18:58 +08:00
}
2021-06-12 14:09:43 +08:00
static void spinor_helper_read(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr, uint8_t * buf, uint32_t count)
2021-06-05 17:18:58 +08:00
{
2021-06-11 15:38:36 +08:00
uint32_t granularity, n;
2021-06-05 17:18:58 +08:00
uint8_t tx[5];
2021-06-12 14:09:43 +08:00
if(pdat->info.read_granularity == 1)
2021-06-11 15:38:36 +08:00
granularity = (count < 0x7fffffff) ? count : 0x7fffffff;
else
2021-06-12 14:09:43 +08:00
granularity = pdat->info.read_granularity;
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
case 3:
2021-06-11 15:38:36 +08:00
while(count > 0)
{
n = count > granularity ? granularity : count;
2021-06-12 14:09:43 +08:00
tx[0] = pdat->info.opcode_read;
2021-06-11 15:38:36 +08:00
tx[1] = (uint8_t)(addr >> 16);
tx[2] = (uint8_t)(addr >> 8);
tx[3] = (uint8_t)(addr >> 0);
2021-06-13 01:29:25 +08:00
fel_spi_xfer(ctx, pdat->swapbuf, pdat->swaplen, pdat->cmdlen, tx, 4, buf, n);
2021-06-11 15:38:36 +08:00
addr += n;
buf += n;
count -= n;
}
2021-06-05 17:18:58 +08:00
break;
case 4:
2021-06-11 15:38:36 +08:00
while(count > 0)
{
n = count > granularity ? granularity : count;
2021-06-12 14:09:43 +08:00
tx[0] = pdat->info.opcode_read;
2021-06-11 15:38:36 +08:00
tx[1] = (uint8_t)(addr >> 24);
tx[2] = (uint8_t)(addr >> 16);
tx[3] = (uint8_t)(addr >> 8);
tx[4] = (uint8_t)(addr >> 0);
2021-06-13 01:29:25 +08:00
fel_spi_xfer(ctx, pdat->swapbuf, pdat->swaplen, pdat->cmdlen, tx, 5, buf, n);
2021-06-11 15:38:36 +08:00
addr += n;
buf += n;
count -= n;
}
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
}
2021-06-12 14:09:43 +08:00
static void spinor_helper_write(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr, uint8_t * buf, uint32_t count)
2021-06-05 17:18:58 +08:00
{
2021-06-13 01:29:25 +08:00
uint8_t * cbuf;
uint32_t clen;
uint8_t * txbuf;
uint32_t txlen;
uint32_t granularity, n;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
if(pdat->info.write_granularity == 1)
2021-06-11 15:38:36 +08:00
granularity = (count < 0x7fffffff) ? count : 0x7fffffff;
else
2021-06-12 14:09:43 +08:00
granularity = pdat->info.write_granularity;
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
2021-06-11 15:38:36 +08:00
case 3:
2021-06-13 01:29:25 +08:00
cbuf = malloc(pdat->cmdlen);
txbuf = malloc(pdat->swaplen);
2021-06-13 01:29:25 +08:00
if(cbuf && txbuf)
{
2021-06-11 15:38:36 +08:00
while(count > 0)
{
clen = 0;
txlen = 0;
2021-06-13 01:29:25 +08:00
while((clen < (pdat->cmdlen - 19 - 1)) && (txlen < (pdat->swaplen - granularity - 4)))
{
n = count > granularity ? granularity : count;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_TXBUF;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 0) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 8) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 16) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 24) & 0xff;
cbuf[clen++] = ((n + 4) >> 0) & 0xff;
cbuf[clen++] = ((n + 4) >> 8) & 0xff;
cbuf[clen++] = ((n + 4) >> 16) & 0xff;
cbuf[clen++] = ((n + 4) >> 24) & 0xff;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
txbuf[txlen++] = pdat->info.opcode_write;
txbuf[txlen++] = (uint8_t)(addr >> 16);
txbuf[txlen++] = (uint8_t)(addr >> 8);
txbuf[txlen++] = (uint8_t)(addr >> 0);
memcpy(&txbuf[txlen], buf, n);
txlen += n;
addr += n;
buf += n;
count -= n;
}
cbuf[clen++] = SPI_CMD_END;
fel_write(ctx, pdat->swapbuf, txbuf, txlen);
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-11 15:38:36 +08:00
}
}
2021-06-13 01:29:25 +08:00
if(cbuf)
free(cbuf);
if(txbuf)
free(txbuf);
2021-06-11 15:38:36 +08:00
break;
case 4:
2021-06-13 01:29:25 +08:00
cbuf = malloc(pdat->cmdlen);
txbuf = malloc(pdat->swaplen);
2021-06-13 01:29:25 +08:00
if(cbuf && txbuf)
2021-06-11 15:38:36 +08:00
{
while(count > 0)
{
clen = 0;
txlen = 0;
2021-06-13 01:29:25 +08:00
while((clen < (pdat->cmdlen - 19 - 1)) && (txlen < (pdat->swaplen - granularity - 5)))
{
n = count > granularity ? granularity : count;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_TXBUF;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 0) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 8) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 16) & 0xff;
cbuf[clen++] = ((pdat->swapbuf + txlen) >> 24) & 0xff;
cbuf[clen++] = ((n + 5) >> 0) & 0xff;
cbuf[clen++] = ((n + 5) >> 8) & 0xff;
cbuf[clen++] = ((n + 5) >> 16) & 0xff;
cbuf[clen++] = ((n + 5) >> 24) & 0xff;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
txbuf[txlen++] = pdat->info.opcode_write;
txbuf[txlen++] = (uint8_t)(addr >> 24);
txbuf[txlen++] = (uint8_t)(addr >> 16);
txbuf[txlen++] = (uint8_t)(addr >> 8);
txbuf[txlen++] = (uint8_t)(addr >> 0);
memcpy(&txbuf[txlen], buf, n);
txlen += n;
addr += n;
buf += n;
count -= n;
}
cbuf[clen++] = SPI_CMD_END;
fel_write(ctx, pdat->swapbuf, txbuf, txlen);
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-11 15:38:36 +08:00
}
}
2021-06-13 01:29:25 +08:00
if(cbuf)
free(cbuf);
if(txbuf)
free(txbuf);
2021-06-11 15:38:36 +08:00
break;
default:
break;
2021-06-05 17:18:58 +08:00
}
}
2021-06-12 23:05:52 +08:00
static inline void spinor_sector_erase_4k(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr)
2021-06-05 17:18:58 +08:00
{
uint8_t cbuf[256];
uint32_t clen = 0;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
case 3:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 4;
cbuf[clen++] = pdat->info.opcode_erase_4k;
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
case 4:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 5;
cbuf[clen++] = pdat->info.opcode_erase_4k;
cbuf[clen++] = (uint8_t)(addr >> 24);
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
}
2021-06-12 23:05:52 +08:00
static inline void spinor_sector_erase_32k(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr)
2021-06-05 17:18:58 +08:00
{
uint8_t cbuf[256];
uint32_t clen = 0;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
case 3:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 4;
cbuf[clen++] = pdat->info.opcode_erase_32k;
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
case 4:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 5;
cbuf[clen++] = pdat->info.opcode_erase_32k;
cbuf[clen++] = (uint8_t)(addr >> 24);
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
}
2021-06-12 23:05:52 +08:00
static inline void spinor_sector_erase_64k(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr)
2021-06-05 17:18:58 +08:00
{
uint8_t cbuf[256];
uint32_t clen = 0;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
case 3:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 4;
cbuf[clen++] = pdat->info.opcode_erase_64k;
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
case 4:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 5;
cbuf[clen++] = pdat->info.opcode_erase_64k;
cbuf[clen++] = (uint8_t)(addr >> 24);
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
}
2021-06-12 23:05:52 +08:00
static inline void spinor_sector_erase_256k(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint32_t addr)
2021-06-05 17:18:58 +08:00
{
uint8_t cbuf[256];
uint32_t clen = 0;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
switch(pdat->info.address_length)
2021-06-05 17:18:58 +08:00
{
case 3:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 4;
cbuf[clen++] = pdat->info.opcode_erase_256k;
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
case 4:
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 1;
cbuf[clen++] = pdat->info.opcode_write_enable;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_FAST;
cbuf[clen++] = 5;
cbuf[clen++] = pdat->info.opcode_erase_256k;
cbuf[clen++] = (uint8_t)(addr >> 24);
cbuf[clen++] = (uint8_t)(addr >> 16);
cbuf[clen++] = (uint8_t)(addr >> 8);
cbuf[clen++] = (uint8_t)(addr >> 0);
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_SPINOR_WAIT;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
2021-06-13 01:29:25 +08:00
if(clen <= pdat->cmdlen)
fel_chip_spi_run(ctx, cbuf, clen);
2021-06-05 17:18:58 +08:00
break;
default:
break;
}
}
2021-06-12 14:09:43 +08:00
static uint64_t spinor_block_read(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint8_t * buf, uint64_t blkno, uint64_t blkcnt)
2021-06-05 19:58:33 +08:00
{
2021-06-12 14:09:43 +08:00
spinor_helper_read(ctx, pdat, blkno * pdat->info.blksz, buf, blkcnt * pdat->info.blksz);
2021-06-05 19:58:33 +08:00
return blkcnt;
}
2021-06-12 14:09:43 +08:00
static uint64_t spinor_block_write(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint8_t * buf, uint64_t blkno, uint64_t blkcnt)
2021-06-05 19:58:33 +08:00
{
2021-06-12 14:09:43 +08:00
uint64_t addr = blkno * pdat->info.blksz;
int64_t cnt = blkcnt * pdat->info.blksz;
2021-06-05 19:58:33 +08:00
uint32_t len;
while(cnt > 0)
{
2021-06-12 14:09:43 +08:00
if((pdat->info.opcode_erase_256k != 0) && ((addr & 0x3ffff) == 0) && (cnt >= 262144))
2021-06-05 19:58:33 +08:00
{
len = 262144;
2021-06-12 14:09:43 +08:00
spinor_sector_erase_256k(ctx, pdat, addr);
2021-06-05 19:58:33 +08:00
}
2021-06-12 14:09:43 +08:00
else if((pdat->info.opcode_erase_64k != 0) && ((addr & 0xffff) == 0) && (cnt >= 65536))
2021-06-05 19:58:33 +08:00
{
len = 65536;
2021-06-12 14:09:43 +08:00
spinor_sector_erase_64k(ctx, pdat, addr);
2021-06-05 19:58:33 +08:00
}
2021-06-12 14:09:43 +08:00
else if((pdat->info.opcode_erase_32k != 0) && ((addr & 0x7fff) == 0) && (cnt >= 32768))
2021-06-05 19:58:33 +08:00
{
len = 32768;
2021-06-12 14:09:43 +08:00
spinor_sector_erase_32k(ctx, pdat, addr);
2021-06-05 19:58:33 +08:00
}
2021-06-12 14:09:43 +08:00
else if((pdat->info.opcode_erase_4k != 0) && ((addr & 0xfff) == 0) && (cnt >= 4096))
2021-06-05 19:58:33 +08:00
{
len = 4096;
2021-06-12 14:09:43 +08:00
spinor_sector_erase_4k(ctx, pdat, addr);
2021-06-05 19:58:33 +08:00
}
else
{
return 0;
}
addr += len;
cnt -= len;
}
2021-06-12 14:09:43 +08:00
spinor_helper_write(ctx, pdat, blkno * pdat->info.blksz, buf, blkcnt * pdat->info.blksz);
2021-06-05 19:58:33 +08:00
return blkcnt;
}
2021-06-12 14:09:43 +08:00
static uint64_t block_write(struct xfel_ctx_t * ctx, struct spinor_pdata_t * pdat, uint64_t offset, void * buf, uint64_t count)
2021-06-05 19:58:33 +08:00
{
uint64_t blkno, blksz, blkcnt, capacity;
uint64_t len, tmp;
uint64_t ret = 0;
uint8_t * p;
2021-06-12 14:09:43 +08:00
blksz = pdat->info.blksz;
blkcnt = pdat->info.capacity / pdat->info.blksz;
2021-06-05 19:58:33 +08:00
if(!blksz || !blkcnt)
return 0;
2021-06-12 14:09:43 +08:00
capacity = pdat->info.capacity;
2021-06-05 19:58:33 +08:00
if(offset >= capacity)
return 0;
tmp = capacity - offset;
if(count > tmp)
count = tmp;
p = malloc(blksz);
if(!p)
return 0;
blkno = offset / blksz;
tmp = offset % blksz;
if(tmp > 0)
{
len = blksz - tmp;
if(count < len)
len = count;
2021-06-12 14:09:43 +08:00
if(spinor_block_read(ctx, pdat, p, blkno, 1) != 1)
2021-06-05 19:58:33 +08:00
{
free(p);
return ret;
}
memcpy((void *)(&p[tmp]), (const void *)buf, len);
2021-06-12 14:09:43 +08:00
if(spinor_block_write(ctx, pdat, p, blkno, 1) != 1)
2021-06-05 19:58:33 +08:00
{
free(p);
return ret;
}
buf += len;
count -= len;
ret += len;
blkno += 1;
}
tmp = count / blksz;
if(tmp > 0)
{
len = tmp * blksz;
2021-06-12 14:09:43 +08:00
if(spinor_block_write(ctx, pdat, buf, blkno, tmp) != tmp)
2021-06-05 19:58:33 +08:00
{
free(p);
return ret;
}
buf += len;
count -= len;
ret += len;
blkno += tmp;
}
if(count > 0)
{
len = count;
2021-06-12 14:09:43 +08:00
if(spinor_block_read(ctx, pdat, p, blkno, 1) != 1)
2021-06-05 19:58:33 +08:00
{
free(p);
return ret;
}
memcpy((void *)(&p[0]), (const void *)buf, len);
2021-06-12 14:09:43 +08:00
if(spinor_block_write(ctx, pdat, p, blkno, 1) != 1)
2021-06-05 19:58:33 +08:00
{
free(p);
return ret;
}
ret += len;
}
free(p);
return ret;
}
2021-06-05 17:18:58 +08:00
uint64_t spinor_detect(struct xfel_ctx_t * ctx)
{
2021-06-12 14:09:43 +08:00
struct spinor_pdata_t pdat;
2021-06-05 17:18:58 +08:00
2021-06-12 14:09:43 +08:00
if(spinor_helper_init(ctx, &pdat))
return pdat.info.capacity;
2021-06-05 17:18:58 +08:00
return 0;
}
int spinor_read(struct xfel_ctx_t * ctx, uint64_t addr, void * buf, uint64_t len)
{
2021-06-12 14:09:43 +08:00
struct spinor_pdata_t pdat;
2021-06-05 17:18:58 +08:00
struct progress_t p;
uint64_t n;
2021-06-12 14:09:43 +08:00
if(spinor_helper_init(ctx, &pdat))
2021-06-05 17:18:58 +08:00
{
progress_start(&p, len);
while(len > 0)
{
2021-06-06 11:33:34 +08:00
n = len > 65536 ? 65536 : len;
2021-06-12 14:09:43 +08:00
spinor_helper_read(ctx, &pdat, addr, buf, n);
2021-06-05 17:18:58 +08:00
addr += n;
buf += n;
len -= n;
progress_update(&p, n);
}
progress_stop(&p);
return 1;
}
return 0;
}
2021-06-05 17:18:58 +08:00
int spinor_write(struct xfel_ctx_t * ctx, uint64_t addr, void * buf, uint64_t len)
{
2021-06-12 14:09:43 +08:00
struct spinor_pdata_t pdat;
2021-06-05 19:58:33 +08:00
struct progress_t p;
uint64_t n;
2021-06-12 14:09:43 +08:00
if(spinor_helper_init(ctx, &pdat))
2021-06-05 19:58:33 +08:00
{
progress_start(&p, len);
while(len > 0)
{
2021-06-06 11:33:34 +08:00
n = len > 65536 ? 65536 : len;
2021-06-12 14:09:43 +08:00
block_write(ctx, &pdat, addr, buf, n);
2021-06-05 19:58:33 +08:00
addr += n;
buf += n;
len -= n;
progress_update(&p, n);
}
progress_stop(&p);
return 1;
}
return 0;
}