Commit 18bcb4aa authored by Csókás, Bence's avatar Csókás, Bence Committed by Pratyush Yadav

mtd: spi-nor: sst: Factor out common write operation to `sst_nor_write_data()`

Writing to the Flash in `sst_nor_write()` is a 3-step process:
first an optional one-byte write to get 2-byte-aligned, then the
bulk of the data is written out in vendor-specific 2-byte writes.
Finally, if there's a byte left over, another one-byte write.
This was implemented 3 times in the body of `sst_nor_write()`.
To reduce code duplication, factor out these sub-steps to their
own function.
Signed-off-by: default avatarCsókás, Bence <csokas.bence@prolan.hu>
Reviewed-by: default avatarPratyush Yadav <pratyush@kernel.org>
[pratyush@kernel.org: fixup whitespace, use %zu instead of %i in WARN()]
Signed-off-by: default avatarPratyush Yadav <pratyush@kernel.org>
Link: https://lore.kernel.org/r/20240710091401.1282824-1-csokas.bence@prolan.hu
parent 8400291e
...@@ -167,6 +167,21 @@ static const struct flash_info sst_nor_parts[] = { ...@@ -167,6 +167,21 @@ static const struct flash_info sst_nor_parts[] = {
} }
}; };
static int sst_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
const u_char *buf)
{
u8 op = (len == 1) ? SPINOR_OP_BP : SPINOR_OP_AAI_WP;
int ret;
nor->program_opcode = op;
ret = spi_nor_write_data(nor, to, 1, buf);
if (ret < 0)
return ret;
WARN(ret != len, "While writing %zu byte written %i bytes\n", len, ret);
return spi_nor_wait_till_ready(nor);
}
static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len, static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
size_t *retlen, const u_char *buf) size_t *retlen, const u_char *buf)
{ {
...@@ -188,16 +203,10 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -188,16 +203,10 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
/* Start write from odd address. */ /* Start write from odd address. */
if (to % 2) { if (to % 2) {
nor->program_opcode = SPINOR_OP_BP;
/* write one byte. */ /* write one byte. */
ret = spi_nor_write_data(nor, to, 1, buf); ret = sst_nor_write_data(nor, to, 1, buf);
if (ret < 0) if (ret < 0)
goto out; goto out;
WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
ret = spi_nor_wait_till_ready(nor);
if (ret)
goto out;
to++; to++;
actual++; actual++;
...@@ -205,16 +214,11 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -205,16 +214,11 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
/* Write out most of the data here. */ /* Write out most of the data here. */
for (; actual < len - 1; actual += 2) { for (; actual < len - 1; actual += 2) {
nor->program_opcode = SPINOR_OP_AAI_WP;
/* write two bytes. */ /* write two bytes. */
ret = spi_nor_write_data(nor, to, 2, buf + actual); ret = sst_nor_write_data(nor, to, 2, buf + actual);
if (ret < 0) if (ret < 0)
goto out; goto out;
WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
ret = spi_nor_wait_till_ready(nor);
if (ret)
goto out;
to += 2; to += 2;
nor->sst_write_second = true; nor->sst_write_second = true;
} }
...@@ -234,14 +238,9 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -234,14 +238,9 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
if (ret) if (ret)
goto out; goto out;
nor->program_opcode = SPINOR_OP_BP; ret = sst_nor_write_data(nor, to, 1, buf + actual);
ret = spi_nor_write_data(nor, to, 1, buf + actual);
if (ret < 0) if (ret < 0)
goto out; goto out;
WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
ret = spi_nor_wait_till_ready(nor);
if (ret)
goto out;
actual += 1; actual += 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment