spi_flash: Make .read() callback optional

All SPI flash chip drivers currently in coreboot use the generic read
functions (spi_flash_cmd_read_fast()/_slow()) as their read callback.
The only use case for specialized read callbacks we have left is with
specialized flash controllers like Intel fast_spi (which sort of
impersonate the flash chip driver by implementing their own probe
function).

This patch unifies the behavior for all normal flash drivers by making
the read callback optional and letting them all fall back to a default
read implementation that handles normal fast/slow reading. Most of the
drivers used to install the respective callback after checking
CONFIG_SPI_FLASH_NO_FAST_READ, but some hardcoded either slow or fast
writes. I have found no indications for why this is and spot-checked
datasheets for affected vendors to make sure they all support both
commands, so I assume this is just some old inaccuracy rather than
important differences that need preserving. (Please yell if you
disagree.)

Also take the opportunity to refactor some of the common spi_flash.c
code a bit because I felt there are too many nested functions that don't
really do enough on their own, and centralizing stuff a bit should make
it easier to follow the code flow. (Some of this is in preparation for
the next patch.)

Change-Id: I2096a3ce619767b41b1b0c0c2b8e95b2bd90a419
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/33282
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-by: Furquan Shaikh <furquan@google.com>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
Julius Werner 2019-06-05 16:34:01 -07:00
parent 381c35c7f9
commit 1b7f99bd6b
12 changed files with 35 additions and 103 deletions

View File

@ -201,11 +201,6 @@ out:
static const struct spi_flash_ops spi_flash_ops = { static const struct spi_flash_ops spi_flash_ops = {
.write = adesto_write, .write = adesto_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
}; };
int spi_flash_probe_adesto(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_adesto(const struct spi_slave *spi, u8 *idcode,

View File

@ -176,11 +176,6 @@ out:
static const struct spi_flash_ops spi_flash_ops = { static const struct spi_flash_ops spi_flash_ops = {
.write = amic_write, .write = amic_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
}; };
int spi_flash_probe_amic(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_amic(const struct spi_slave *spi, u8 *idcode,

View File

@ -157,11 +157,6 @@ out:
static const struct spi_flash_ops spi_flash_ops = { static const struct spi_flash_ops spi_flash_ops = {
.write = atmel_write, .write = atmel_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
}; };
int spi_flash_probe_atmel(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_atmel(const struct spi_slave *spi, u8 *idcode,

View File

@ -292,7 +292,6 @@ static const struct spi_flash_ops spi_flash_ops = {
.write = eon_write, .write = eon_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
.read = spi_flash_cmd_read_fast,
}; };
int spi_flash_probe_eon(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_eon(const struct spi_slave *spi, u8 *idcode,

View File

@ -221,11 +221,6 @@ static const struct spi_flash_ops spi_flash_ops = {
.write = gigadevice_write, .write = gigadevice_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
}; };
int spi_flash_probe_gigadevice(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_gigadevice(const struct spi_slave *spi, u8 *idcode,

View File

@ -268,11 +268,6 @@ static const struct spi_flash_ops spi_flash_ops = {
.write = macronix_write, .write = macronix_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
}; };
int spi_flash_probe_macronix(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_macronix(const struct spi_slave *spi, u8 *idcode,

View File

@ -282,7 +282,6 @@ static int spansion_write(const struct spi_flash *flash, u32 offset, size_t len,
static const struct spi_flash_ops spi_flash_ops = { static const struct spi_flash_ops spi_flash_ops = {
.write = spansion_write, .write = spansion_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.read = spi_flash_cmd_read_slow,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
}; };

View File

@ -31,7 +31,7 @@ static void spi_flash_addr(u32 addr, u8 *cmd)
static int do_spi_flash_cmd(const struct spi_slave *spi, const void *dout, static int do_spi_flash_cmd(const struct spi_slave *spi, const void *dout,
size_t bytes_out, void *din, size_t bytes_in) size_t bytes_out, void *din, size_t bytes_in)
{ {
int ret = 1; int ret;
/* /*
* SPI flash requires command-response kind of behavior. Thus, two * SPI flash requires command-response kind of behavior. Thus, two
* separate SPI vectors are required -- first to transmit dout and other * separate SPI vectors are required -- first to transmit dout and other
@ -49,11 +49,11 @@ static int do_spi_flash_cmd(const struct spi_slave *spi, const void *dout,
if (!bytes_in) if (!bytes_in)
count = 1; count = 1;
if (spi_claim_bus(spi)) ret = spi_claim_bus(spi);
if (ret)
return ret; return ret;
if (spi_xfer_vector(spi, vectors, count) == 0) ret = spi_xfer_vector(spi, vectors, count);
ret = 0;
spi_release_bus(spi); spi_release_bus(spi);
return ret; return ret;
@ -68,18 +68,6 @@ int spi_flash_cmd(const struct spi_slave *spi, u8 cmd, void *response, size_t le
return ret; return ret;
} }
static int spi_flash_cmd_read(const struct spi_slave *spi, const u8 *cmd,
size_t cmd_len, void *data, size_t data_len)
{
int ret = do_spi_flash_cmd(spi, cmd, cmd_len, data, data_len);
if (ret) {
printk(BIOS_WARNING, "SF: Failed to send read command (%zu bytes): %d\n",
data_len, ret);
}
return ret;
}
/* TODO: This code is quite possibly broken and overflowing stacks. Fix ASAP! */ /* TODO: This code is quite possibly broken and overflowing stacks. Fix ASAP! */
#pragma GCC diagnostic push #pragma GCC diagnostic push
#if defined(__GNUC__) && !defined(__clang__) #if defined(__GNUC__) && !defined(__clang__)
@ -103,34 +91,38 @@ int spi_flash_cmd_write(const struct spi_slave *spi, const u8 *cmd,
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
static int spi_flash_cmd_read_array(const struct spi_slave *spi, u8 *cmd,
size_t cmd_len, u32 offset,
size_t len, void *data)
{
spi_flash_addr(offset, cmd);
return spi_flash_cmd_read(spi, cmd, cmd_len, data, len);
}
/* Perform the read operation honoring spi controller fifo size, reissuing /* Perform the read operation honoring spi controller fifo size, reissuing
* the read command until the full request completed. */ * the read command until the full request completed. */
static int spi_flash_cmd_read_array_wrapped(const struct spi_slave *spi, static int spi_flash_read_chunked(const struct spi_flash *flash, u32 offset,
u8 *cmd, size_t cmd_len, u32 offset, size_t len, void *buf)
size_t len, void *buf)
{ {
int ret; u8 cmd[5];
size_t xfer_len; int ret, cmd_len;
int (*do_cmd)(const struct spi_slave *spi, const void *din,
size_t in_bytes, void *out, size_t out_bytes);
if (CONFIG(SPI_FLASH_NO_FAST_READ)) {
cmd_len = 4;
cmd[0] = CMD_READ_ARRAY_SLOW;
do_cmd = do_spi_flash_cmd;
} else {
cmd_len = 5;
cmd[0] = CMD_READ_ARRAY_FAST;
cmd[4] = 0;
do_cmd = do_spi_flash_cmd;
}
uint8_t *data = buf; uint8_t *data = buf;
while (len) { while (len) {
xfer_len = spi_crop_chunk(spi, cmd_len, len); size_t xfer_len = spi_crop_chunk(&flash->spi, cmd_len, len);
spi_flash_addr(offset, cmd);
/* Perform the read. */ ret = do_cmd(&flash->spi, cmd, cmd_len, data, xfer_len);
ret = spi_flash_cmd_read_array(spi, cmd, cmd_len, if (ret) {
offset, xfer_len, data); printk(BIOS_WARNING,
"SF: Failed to send read command %#.2x(%#x, %#zx): %d\n",
if (ret) cmd[0], offset, xfer_len, ret);
return ret; return ret;
}
offset += xfer_len; offset += xfer_len;
data += xfer_len; data += xfer_len;
len -= xfer_len; len -= xfer_len;
@ -139,28 +131,6 @@ static int spi_flash_cmd_read_array_wrapped(const struct spi_slave *spi,
return 0; return 0;
} }
int spi_flash_cmd_read_fast(const struct spi_flash *flash, u32 offset,
size_t len, void *data)
{
u8 cmd[5];
cmd[0] = CMD_READ_ARRAY_FAST;
cmd[4] = 0x00;
return spi_flash_cmd_read_array_wrapped(&flash->spi, cmd, sizeof(cmd),
offset, len, data);
}
int spi_flash_cmd_read_slow(const struct spi_flash *flash, u32 offset,
size_t len, void *data)
{
u8 cmd[4];
cmd[0] = CMD_READ_ARRAY_SLOW;
return spi_flash_cmd_read_array_wrapped(&flash->spi, cmd, sizeof(cmd),
offset, len, data);
}
int spi_flash_cmd_poll_bit(const struct spi_flash *flash, unsigned long timeout, int spi_flash_cmd_poll_bit(const struct spi_flash *flash, unsigned long timeout,
u8 cmd, u8 poll_bit) u8 cmd, u8 poll_bit)
{ {
@ -174,7 +144,7 @@ int spi_flash_cmd_poll_bit(const struct spi_flash *flash, unsigned long timeout,
mono_time_add_msecs(&end, timeout); mono_time_add_msecs(&end, timeout);
do { do {
ret = spi_flash_cmd_read(spi, &cmd, 1, &status, 1); ret = do_spi_flash_cmd(spi, &cmd, 1, &status, 1);
if (ret) if (ret)
return -1; return -1;
if ((status & poll_bit) == 0) if ((status & poll_bit) == 0)
@ -391,7 +361,10 @@ int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash)
int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len, int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len,
void *buf) void *buf)
{ {
return flash->ops->read(flash, offset, len, buf); if (flash->ops->read)
return flash->ops->read(flash, offset, len, buf);
return spi_flash_read_chunked(flash, offset, len, buf);
} }
int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len, int spi_flash_write(const struct spi_flash *flash, u32 offset, size_t len,

View File

@ -34,12 +34,6 @@
/* Send a single-byte command to the device and read the response */ /* Send a single-byte command to the device and read the response */
int spi_flash_cmd(const struct spi_slave *spi, u8 cmd, void *response, size_t len); int spi_flash_cmd(const struct spi_slave *spi, u8 cmd, void *response, size_t len);
int spi_flash_cmd_read_fast(const struct spi_flash *flash, u32 offset,
size_t len, void *data);
int spi_flash_cmd_read_slow(const struct spi_flash *flash, u32 offset,
size_t len, void *data);
/* /*
* Send a multi-byte command to the device followed by (optional) * Send a multi-byte command to the device followed by (optional)
* data. Used for programming the flash array, etc. * data. Used for programming the flash array, etc.

View File

@ -55,14 +55,12 @@ static const struct spi_flash_ops spi_flash_ops_write_ai = {
.write = sst_write_ai, .write = sst_write_ai,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
.read = spi_flash_cmd_read_fast,
}; };
static const struct spi_flash_ops spi_flash_ops_write_256 = { static const struct spi_flash_ops spi_flash_ops_write_256 = {
.write = sst_write_256, .write = sst_write_256,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
.read = spi_flash_cmd_read_fast,
}; };
#define SST_SECTOR_SIZE (4 * 1024) #define SST_SECTOR_SIZE (4 * 1024)

View File

@ -351,7 +351,6 @@ out:
static const struct spi_flash_ops spi_flash_ops = { static const struct spi_flash_ops spi_flash_ops = {
.write = stmicro_write, .write = stmicro_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.read = spi_flash_cmd_read_fast,
}; };
int spi_flash_probe_stmicro(const struct spi_slave *spi, u8 *idcode, int spi_flash_probe_stmicro(const struct spi_slave *spi, u8 *idcode,

View File

@ -646,11 +646,6 @@ static const struct spi_flash_ops spi_flash_ops = {
.write = winbond_write, .write = winbond_write,
.erase = spi_flash_cmd_erase, .erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status, .status = spi_flash_cmd_status,
#if CONFIG(SPI_FLASH_NO_FAST_READ)
.read = spi_flash_cmd_read_slow,
#else
.read = spi_flash_cmd_read_fast,
#endif
.get_write_protection = winbond_get_write_protection, .get_write_protection = winbond_get_write_protection,
.set_write_protection = winbond_set_write_protection, .set_write_protection = winbond_set_write_protection,
}; };