drivers/spi: fix flash writes at page boundaries

There was an assumption that all SPI controllers could
consume a full page of data to write. However, that
assumption doesn't hold when spi_crop_chunk() indicates
sizes smaller than page size. If the requested offset isn't
page aligned from the start then writes will fail corrupting
data since a page boundary isn't honored.

The spansion driver needed quite a bit more work to honor
the spi_crop_chunk() result. It now mimics the other
driver's code. Also, needed to add spi_crop_chunk() to
marvell/bg4cd SoC to make google/cosmos build. SPI obviously
doesn't work on that platform, but it fixes the build error.

Change-Id: I93e24a5a717adcee45a017c164bd960f4592ad50
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/17910
Tested-by: build bot (Jenkins)
Reviewed-by: Matt DeVillier <matt.devillier@gmail.com>
Reviewed-by: Kyösti Mälkki <kyosti.malkki@gmail.com>
This commit is contained in:
Aaron Durbin 2016-12-17 13:16:07 -06:00
parent 06cd903566
commit 41f6690239
10 changed files with 19 additions and 24 deletions

View File

@ -93,9 +93,9 @@ static int adesto_write(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = 1 << stm->params->l2_page_size; page_size = 1 << stm->params->l2_page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -127,7 +127,6 @@ static int adesto_write(const struct spi_flash *flash, u32 offset, size_t len,
goto out; goto out;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -121,9 +121,9 @@ static int atmel_write(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = 1 << stm->params->l2_page_size; page_size = 1 << stm->params->l2_page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -155,7 +155,6 @@ static int atmel_write(const struct spi_flash *flash, u32 offset, size_t len,
goto out; goto out;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -91,9 +91,9 @@ static int eon_write(const struct spi_flash *flash,
u8 cmd[4]; u8 cmd[4];
page_size = 1 << eon->params->page_size; page_size = 1 << eon->params->page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -128,7 +128,6 @@ static int eon_write(const struct spi_flash *flash,
} }
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -132,9 +132,9 @@ static int gigadevice_write(const struct spi_flash *flash, u32 offset,
u8 cmd[4]; u8 cmd[4];
page_size = 1 << stm->params->l2_page_size; page_size = 1 << stm->params->l2_page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -169,7 +169,6 @@ static int gigadevice_write(const struct spi_flash *flash, u32 offset,
goto out; goto out;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -160,9 +160,9 @@ static int macronix_write(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = mcx->params->page_size; page_size = mcx->params->page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -194,7 +194,6 @@ static int macronix_write(const struct spi_flash *flash, u32 offset, size_t len,
break; break;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -205,7 +205,6 @@ static int spansion_write(const struct spi_flash *flash, u32 offset, size_t len,
const void *buf) const void *buf)
{ {
struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash); struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash);
unsigned long page_addr;
unsigned long byte_addr; unsigned long byte_addr;
unsigned long page_size; unsigned long page_size;
size_t chunk_len; size_t chunk_len;
@ -214,16 +213,16 @@ static int spansion_write(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = spsn->params->page_size; page_size = spsn->params->page_size;
page_addr = offset / page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
cmd[0] = CMD_S25FLXX_PP; cmd[0] = CMD_S25FLXX_PP;
cmd[1] = page_addr >> 8; cmd[1] = (offset >> 16) & 0xff;
cmd[2] = page_addr; cmd[2] = (offset >> 8) & 0xff;
cmd[3] = byte_addr; cmd[3] = offset & 0xff;
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH
printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }" printk(BIOS_SPEW, "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x }"
@ -248,8 +247,7 @@ static int spansion_write(const struct spi_flash *flash, u32 offset, size_t len,
if (ret) if (ret)
break; break;
page_addr++; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -171,7 +171,6 @@ static int sst_write_256(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = 256; page_size = 256;
byte_addr = offset % page_size;
/* If the data is not word aligned, write out leading single byte */ /* If the data is not word aligned, write out leading single byte */
actual = offset % 2; actual = offset % 2;
@ -193,6 +192,7 @@ static int sst_write_256(const struct spi_flash *flash, u32 offset, size_t len,
cmd[3] = offset; cmd[3] = offset;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -224,7 +224,6 @@ static int sst_write_256(const struct spi_flash *flash, u32 offset, size_t len,
break; break;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
done: done:

View File

@ -189,9 +189,9 @@ static int stmicro_write(const struct spi_flash *flash,
u8 cmd[4]; u8 cmd[4];
page_size = stm->params->page_size; page_size = stm->params->page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -223,7 +223,6 @@ static int stmicro_write(const struct spi_flash *flash,
goto out; goto out;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -151,9 +151,9 @@ static int winbond_write(const struct spi_flash *flash, u32 offset, size_t len,
u8 cmd[4]; u8 cmd[4];
page_size = 1 << stm->params->l2_page_size; page_size = 1 << stm->params->l2_page_size;
byte_addr = offset % page_size;
for (actual = 0; actual < len; actual += chunk_len) { for (actual = 0; actual < len; actual += chunk_len) {
byte_addr = offset % page_size;
chunk_len = min(len - actual, page_size - byte_addr); chunk_len = min(len - actual, page_size - byte_addr);
chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len);
@ -185,7 +185,6 @@ static int winbond_write(const struct spi_flash *flash, u32 offset, size_t len,
goto out; goto out;
offset += chunk_len; offset += chunk_len;
byte_addr = 0;
} }
#if CONFIG_DEBUG_SPI_FLASH #if CONFIG_DEBUG_SPI_FLASH

View File

@ -19,3 +19,8 @@ int spi_setup_slave(unsigned int bus, unsigned int cs, struct spi_slave *slave)
{ {
return -1; return -1;
} }
unsigned int spi_crop_chunk(unsigned int cmd_len, unsigned int buf_len)
{
return buf_len;
}