drives/spi_flash: add spi_flash_cmd_write_page_program()

The SPI flashes that support page programming mode had duplicated
the logic for writing in every driver. Add
spi_flash_cmd_write_page_program() and use the common implementation
to reduce code size that comes from duplication. The savings is
~2.5KiB per stage where the spi flash drivers are utilized.

Change-Id: Ie6db03fa8ad33789f1d07a718a769e4ca8bffe1d
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/37963
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
Reviewed-by: Marshall Dawson <marshalldawson3rd@gmail.com>
This commit is contained in:
Aaron Durbin
2019-12-27 15:16:17 -07:00
parent 9e877ec60d
commit d701ef7475
13 changed files with 94 additions and 593 deletions

View File

@@ -103,66 +103,8 @@ static const struct atmel_spi_flash_params atmel_spi_flash_table[] = {
},
};
static int atmel_write(const struct spi_flash *flash, u32 offset, size_t len,
const void *buf)
{
unsigned long byte_addr;
unsigned long page_size;
size_t chunk_len;
size_t actual;
int ret;
u8 cmd[4];
page_size = flash->page_size;
for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = MIN(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(&flash->spi, sizeof(cmd), chunk_len);
cmd[0] = CMD_AT25_PP;
cmd[1] = (offset >> 16) & 0xff;
cmd[2] = (offset >> 8) & 0xff;
cmd[3] = offset & 0xff;
#if CONFIG(DEBUG_SPI_FLASH)
printk(BIOS_SPEW, "PP: %p => cmd = { 0x%02x 0x%02x%02x%02x }"
" chunk_len = %zu\n", buf + actual,
cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
#endif
ret = spi_flash_cmd(&flash->spi, CMD_AT25_WREN, NULL, 0);
if (ret < 0) {
printk(BIOS_WARNING, "SF: Enabling Write failed\n");
goto out;
}
ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd),
buf + actual, chunk_len);
if (ret < 0) {
printk(BIOS_WARNING, "SF: Atmel Page Program failed\n");
goto out;
}
ret = spi_flash_cmd_wait_ready(flash,
SPI_FLASH_PROG_TIMEOUT_MS);
if (ret)
goto out;
offset += chunk_len;
}
#if CONFIG(DEBUG_SPI_FLASH)
printk(BIOS_SPEW, "SF: Atmel: Successfully programmed %zu bytes @"
" 0x%lx\n", len, (unsigned long)(offset - len));
#endif
ret = 0;
out:
return ret;
}
static const struct spi_flash_ops spi_flash_ops = {
.write = atmel_write,
.write = spi_flash_cmd_write_page_program,
.erase = spi_flash_cmd_erase,
};
@@ -193,6 +135,8 @@ int spi_flash_probe_atmel(const struct spi_slave *spi, u8 *idcode,
flash->size = flash->sector_size * params->sectors_per_block *
params->nr_blocks;
flash->erase_cmd = CMD_AT25_SE;
flash->pp_cmd = CMD_AT25_PP;
flash->wren_cmd = CMD_AT25_WREN;
flash->ops = &spi_flash_ops;