diff options
author | Aaron Durbin <adurbin@chromium.org> | 2019-12-27 15:16:17 -0700 |
---|---|---|
committer | Aaron Durbin <adurbin@chromium.org> | 2020-01-06 15:00:50 +0000 |
commit | d701ef7475fe6d015a61cd91410391c3e0902f53 (patch) | |
tree | a3b1aaaaa6490b62ef7c71e9a119a41bd32d825a /src | |
parent | 9e877ec60d177565776b20e3d61f723a9552ee34 (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/drivers/spi/adesto.c | 62 | ||||
-rw-r--r-- | src/drivers/spi/amic.c | 63 | ||||
-rw-r--r-- | src/drivers/spi/atmel.c | 62 | ||||
-rw-r--r-- | src/drivers/spi/eon.c | 64 | ||||
-rw-r--r-- | src/drivers/spi/gigadevice.c | 67 | ||||
-rw-r--r-- | src/drivers/spi/macronix.c | 60 | ||||
-rw-r--r-- | src/drivers/spi/spansion.c | 61 | ||||
-rw-r--r-- | src/drivers/spi/spi_flash.c | 54 | ||||
-rw-r--r-- | src/drivers/spi/spi_flash_internal.h | 4 | ||||
-rw-r--r-- | src/drivers/spi/sst.c | 64 | ||||
-rw-r--r-- | src/drivers/spi/stmicro.c | 62 | ||||
-rw-r--r-- | src/drivers/spi/winbond.c | 62 | ||||
-rw-r--r-- | src/include/spi_flash.h | 2 |
13 files changed, 94 insertions, 593 deletions
diff --git a/src/drivers/spi/adesto.c b/src/drivers/spi/adesto.c index 974f8ad146..5f467417fd 100644 --- a/src/drivers/spi/adesto.c +++ b/src/drivers/spi/adesto.c @@ -148,66 +148,8 @@ static const struct adesto_spi_flash_params adesto_spi_flash_table[] = { }, }; -static int adesto_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_AT25DF_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_AT25DF_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: adesto 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: adesto: 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 = adesto_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, }; @@ -237,6 +179,8 @@ int spi_flash_probe_adesto(const struct spi_slave *spi, u8 *idcode, flash->size = flash->sector_size *params->sectors_per_block * params->nr_blocks; flash->erase_cmd = CMD_AT25DF_SE; + flash->pp_cmd = CMD_AT25DF_PP; + flash->wren_cmd = CMD_AT25DF_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/amic.c b/src/drivers/spi/amic.c index 254a5b2cff..54c03c05ac 100644 --- a/src/drivers/spi/amic.c +++ b/src/drivers/spi/amic.c @@ -119,67 +119,8 @@ static const struct amic_spi_flash_params amic_spi_flash_table[] = { }, }; -static int amic_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; - byte_addr = offset % page_size; - - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = MIN(len - actual, page_size - byte_addr); - chunk_len = spi_crop_chunk(&flash->spi, sizeof(cmd), chunk_len); - - cmd[0] = CMD_A25_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_A25_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: AMIC 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; - byte_addr = 0; - } - -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, "SF: AMIC: 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 = amic_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, }; @@ -210,6 +151,8 @@ int spi_flash_probe_amic(const struct spi_slave *spi, u8 *idcode, flash->size = flash->sector_size * params->sectors_per_block * params->nr_blocks; flash->erase_cmd = CMD_A25_SE; + flash->pp_cmd = CMD_A25_PP; + flash->wren_cmd = CMD_A25_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/atmel.c b/src/drivers/spi/atmel.c index 4496f4b08c..5e22eb68c1 100644 --- a/src/drivers/spi/atmel.c +++ b/src/drivers/spi/atmel.c @@ -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; diff --git a/src/drivers/spi/eon.c b/src/drivers/spi/eon.c index 2cdbdbbe8d..1f3cbf3c78 100644 --- a/src/drivers/spi/eon.c +++ b/src/drivers/spi/eon.c @@ -235,68 +235,8 @@ static const struct eon_spi_flash_params eon_spi_flash_table[] = { }, }; -static int eon_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 = 0; - 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); - - ret = spi_flash_cmd(&flash->spi, CMD_EN25_WREN, NULL, 0); - if (ret < 0) { - printk(BIOS_WARNING, "SF: Enabling Write failed\n"); - goto out; - } - - cmd[0] = CMD_EN25_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_write(&flash->spi, cmd, sizeof(cmd), - buf + actual, chunk_len); - if (ret < 0) { - printk(BIOS_WARNING, "SF: EON Page Program failed\n"); - goto out; - } - - ret = spi_flash_cmd_wait_ready(flash, - SPI_FLASH_PROG_TIMEOUT_MS); - if (ret) { - printk(BIOS_WARNING, "SF: EON Page Program timeout\n"); - goto out; - } - - offset += chunk_len; - } - -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, "SF: EON: Successfully programmed %zu bytes @ %#x\n", - len, (unsigned int)(offset - len)); -#endif - -out: - return ret; -} - static const struct spi_flash_ops spi_flash_ops = { - .write = eon_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, }; @@ -327,6 +267,8 @@ int spi_flash_probe_eon(const struct spi_slave *spi, u8 *idcode, flash->size = flash->sector_size * params->nr_sectors; flash->erase_cmd = CMD_EN25_SE; flash->status_cmd = CMD_EN25_RDSR; + flash->pp_cmd = CMD_EN25_PP; + flash->wren_cmd = CMD_EN25_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/gigadevice.c b/src/drivers/spi/gigadevice.c index 65494fa240..1f478a867a 100644 --- a/src/drivers/spi/gigadevice.c +++ b/src/drivers/spi/gigadevice.c @@ -164,71 +164,8 @@ static const struct gigadevice_spi_flash_params gigadevice_spi_flash_table[] = { }, }; -static int gigadevice_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 = 0; - 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); - - ret = spi_flash_cmd(&flash->spi, CMD_GD25_WREN, NULL, 0); - if (ret < 0) { - printk(BIOS_WARNING, - "SF gigadevice.c: Enabling Write failed\n"); - goto out; - } - - cmd[0] = CMD_GD25_PP; - cmd[1] = (offset >> 16) & 0xff; - cmd[2] = (offset >> 8) & 0xff; - cmd[3] = offset & 0xff; -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, - "PP gigadevice.c: %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_write(&flash->spi, cmd, sizeof(cmd), - buf + actual, chunk_len); - if (ret < 0) { - printk(BIOS_WARNING, - "SF gigadevice.c: 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 gigadevice.c: Successfully programmed %zu bytes @ %#x\n", - len, (unsigned int)(offset - len)); -#endif - - ret = 0; - -out: - return ret; -} - static const struct spi_flash_ops spi_flash_ops = { - .write = gigadevice_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, }; @@ -264,6 +201,8 @@ int spi_flash_probe_gigadevice(const struct spi_slave *spi, u8 *idcode, (1 << params->nr_blocks_shift); flash->erase_cmd = CMD_GD25_SE; flash->status_cmd = CMD_GD25_RDSR; + flash->pp_cmd = CMD_GD25_PP; + flash->wren_cmd = CMD_GD25_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/macronix.c b/src/drivers/spi/macronix.c index 6643dfa579..adf4f23737 100644 --- a/src/drivers/spi/macronix.c +++ b/src/drivers/spi/macronix.c @@ -200,64 +200,8 @@ static const struct macronix_spi_flash_params macronix_spi_flash_table[] = { }, }; -static int macronix_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 = 0; - 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_MX25XX_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_MX25XX_WREN, NULL, 0); - if (ret < 0) { - printk(BIOS_WARNING, "SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd), - buf + actual, chunk_len); - if (ret < 0) { - printk(BIOS_WARNING, "SF: Macronix Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, - SPI_FLASH_PROG_TIMEOUT_MS); - if (ret) - break; - - offset += chunk_len; - } - -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, "SF: Macronix: Successfully programmed %zu bytes @" - " 0x%lx\n", len, (unsigned long)(offset - len)); -#endif - - return ret; -} - static const struct spi_flash_ops spi_flash_ops = { - .write = macronix_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, }; @@ -288,6 +232,8 @@ int spi_flash_probe_macronix(const struct spi_slave *spi, u8 *idcode, params->nr_blocks; flash->erase_cmd = CMD_MX25XX_SE; flash->status_cmd = CMD_MX25XX_RDSR; + flash->pp_cmd = CMD_MX25XX_PP; + flash->wren_cmd = CMD_MX25XX_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/spansion.c b/src/drivers/spi/spansion.c index 0b119ebe98..01911a0696 100644 --- a/src/drivers/spi/spansion.c +++ b/src/drivers/spi/spansion.c @@ -218,65 +218,8 @@ static const struct spansion_spi_flash_params spansion_spi_flash_table[] = { }, }; -static int spansion_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 = 0; - 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_S25FLXX_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_S25FLXX_WREN, NULL, 0); - if (ret < 0) { - printk(BIOS_WARNING, "SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(&flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - printk(BIOS_WARNING, "SF: SPANSION Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, - SPI_FLASH_PROG_TIMEOUT_MS); - if (ret) - break; - - offset += chunk_len; - } - -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, "SF: SPANSION: Successfully programmed %zu bytes @ 0x%x\n", - len, offset); -#endif - - return ret; -} - static const struct spi_flash_ops spi_flash_ops = { - .write = spansion_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, }; @@ -307,6 +250,8 @@ int spi_flash_probe_spansion(const struct spi_slave *spi, u8 *idcode, flash->size = flash->sector_size * params->nr_sectors; flash->erase_cmd = CMD_S25FLXX_SE; flash->status_cmd = CMD_S25FLXX_RDSR; + flash->pp_cmd = CMD_S25FLXX_PP; + flash->wren_cmd = CMD_S25FLXX_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c index f0d01593f3..ff69a9a1c1 100644 --- a/src/drivers/spi/spi_flash.c +++ b/src/drivers/spi/spi_flash.c @@ -255,6 +255,60 @@ int spi_flash_cmd_status(const struct spi_flash *flash, u8 *reg) return spi_flash_cmd(&flash->spi, flash->status_cmd, reg, sizeof(*reg)); } +int spi_flash_cmd_write_page_program(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 = 0; + u8 cmd[4]; + + page_size = flash->page_size; + cmd[0] = flash->pp_cmd; + + 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); + + spi_flash_addr(offset, cmd); + 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); + } + + ret = spi_flash_cmd(&flash->spi, flash->wren_cmd, 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: 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: : Successfully programmed %zu bytes @ 0x%lx\n", + len, (unsigned long)(offset - len)); + ret = 0; + +out: + return ret; +} + /* * The following table holds all device probe functions * diff --git a/src/drivers/spi/spi_flash_internal.h b/src/drivers/spi/spi_flash_internal.h index 36fa66d67c..f99c90ed36 100644 --- a/src/drivers/spi/spi_flash_internal.h +++ b/src/drivers/spi/spi_flash_internal.h @@ -62,6 +62,10 @@ int spi_flash_cmd_erase(const struct spi_flash *flash, u32 offset, size_t len); /* Read status register. */ int spi_flash_cmd_status(const struct spi_flash *flash, u8 *reg); +/* Write to flash utilizing page program semantics. */ +int spi_flash_cmd_write_page_program(const struct spi_flash *flash, u32 offset, + size_t len, const void *buf); + /* Manufacturer-specific probe functions */ int spi_flash_probe_spansion(const struct spi_slave *spi, u8 *idcode, struct spi_flash *flash); diff --git a/src/drivers/spi/sst.c b/src/drivers/spi/sst.c index 499db3550b..bcc47700d9 100644 --- a/src/drivers/spi/sst.c +++ b/src/drivers/spi/sst.c @@ -53,8 +53,6 @@ struct sst_spi_flash_params { static int sst_write_ai(const struct spi_flash *flash, u32 offset, size_t len, const void *buf); -static int sst_write_256(const struct spi_flash *flash, u32 offset, size_t len, - const void *buf); static const struct spi_flash_ops spi_flash_ops_write_ai = { .write = sst_write_ai, @@ -63,7 +61,7 @@ static const struct spi_flash_ops spi_flash_ops_write_ai = { }; static const struct spi_flash_ops spi_flash_ops_write_256 = { - .write = sst_write_256, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, }; @@ -187,60 +185,6 @@ sst_byte_write(const struct spi_flash *flash, u32 offset, const void *buf) return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT_MS); } -static int sst_write_256(const struct spi_flash *flash, u32 offset, size_t len, - const void *buf) -{ - size_t actual, chunk_len; - unsigned long byte_addr; - unsigned long page_size; - int ret = 0; - u8 cmd[4]; - - page_size = 256; - - 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_SST_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_SST_WREN, NULL, 0); - if (ret < 0) { - printk(BIOS_WARNING, "SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(&flash->spi, cmd, sizeof(cmd), - buf + actual, chunk_len); - if (ret < 0) { - printk(BIOS_WARNING, "SF: SST Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, - SPI_FLASH_PROG_TIMEOUT_MS); - if (ret) - break; - - offset += chunk_len; - } - -#if CONFIG(DEBUG_SPI_FLASH) - printk(BIOS_SPEW, "SF: SST: program %s %zu bytes @ 0x%lx\n", - ret ? "failure" : "success", len, (unsigned long)offset - actual); -#endif - return ret; -} - static int sst_write_ai(const struct spi_flash *flash, u32 offset, size_t len, const void *buf) { @@ -350,6 +294,12 @@ int spi_flash_probe_sst(const struct spi_slave *spi, u8 *idcode, flash->size = flash->sector_size * params->nr_sectors; flash->erase_cmd = CMD_SST_SE; flash->status_cmd = CMD_SST_RDSR; + flash->wren_cmd = CMD_SST_WREN; + + if (params->ops == &spi_flash_ops_write_256) { + flash->page_size = 256; + flash->pp_cmd = CMD_SST_PP; + } flash->ops = params->ops; diff --git a/src/drivers/spi/stmicro.c b/src/drivers/spi/stmicro.c index ad0a0dc538..57bd3ddbb5 100644 --- a/src/drivers/spi/stmicro.c +++ b/src/drivers/spi/stmicro.c @@ -284,66 +284,8 @@ static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = { }, }; -static int stmicro_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 = 0; - 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_M25PXX_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_M25PXX_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: STMicro 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: STMicro: 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 = stmicro_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, }; @@ -384,6 +326,8 @@ int spi_flash_probe_stmicro(const struct spi_slave *spi, u8 *idcode, flash->sector_size = params->page_size * params->pages_per_sector; flash->size = flash->sector_size * params->nr_sectors; flash->erase_cmd = params->op_erase; + flash->pp_cmd = CMD_M25PXX_PP; + flash->wren_cmd = CMD_M25PXX_WREN; flash->ops = &spi_flash_ops; diff --git a/src/drivers/spi/winbond.c b/src/drivers/spi/winbond.c index 3790ce7bf1..b6735c0be7 100644 --- a/src/drivers/spi/winbond.c +++ b/src/drivers/spi/winbond.c @@ -296,64 +296,6 @@ static const struct winbond_spi_flash_params winbond_spi_flash_table[] = { }, }; -static int winbond_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 = 0; - 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_W25_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_W25_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: Winbond 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: Winbond: Successfully programmed %zu bytes @" - " 0x%lx\n", len, (unsigned long)(offset - len)); -#endif - ret = 0; - -out: - return ret; -} - /* * Convert BPx, TB and CMP to a region. * SEC (if available) must be zero. @@ -669,7 +611,7 @@ winbond_set_write_protection(const struct spi_flash *flash, } static const struct spi_flash_ops spi_flash_ops = { - .write = winbond_write, + .write = spi_flash_cmd_write_page_program, .erase = spi_flash_cmd_erase, .status = spi_flash_cmd_status, .get_write_protection = winbond_get_write_protection, @@ -706,6 +648,8 @@ int spi_flash_probe_winbond(const struct spi_slave *spi, u8 *idcode, (1 << params->nr_blocks_shift); flash->erase_cmd = CMD_W25_SE; flash->status_cmd = CMD_W25_RDSR; + flash->pp_cmd = CMD_W25_PP; + flash->wren_cmd = CMD_W25_WREN; flash->flags.dual_spi = params->dual_spi; diff --git a/src/include/spi_flash.h b/src/include/spi_flash.h index 3a0c383676..1a5a82934e 100644 --- a/src/include/spi_flash.h +++ b/src/include/spi_flash.h @@ -104,6 +104,8 @@ struct spi_flash { u32 page_size; u8 erase_cmd; u8 status_cmd; + u8 pp_cmd; /* Page program command. */ + u8 wren_cmd; /* Write Enable command. */ const struct spi_flash_ops *ops; const void *driver_private; }; |