Skip to content

Commit 18bcb4a

Browse files
Csókás, BencePratyush Yadav
Csókás, Bence
authored and
Pratyush Yadav
committed
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: Csókás, Bence <csokas.bence@prolan.hu> Reviewed-by: Pratyush Yadav <pratyush@kernel.org> [pratyush@kernel.org: fixup whitespace, use %zu instead of %i in WARN()] Signed-off-by: Pratyush Yadav <pratyush@kernel.org> Link: https://lore.kernel.org/r/20240710091401.1282824-1-csokas.bence@prolan.hu
1 parent 8400291 commit 18bcb4a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

drivers/mtd/spi-nor/sst.c

+19-20
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,21 @@ static const struct flash_info sst_nor_parts[] = {
167167
}
168168
};
169169

170+
static int sst_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
171+
const u_char *buf)
172+
{
173+
u8 op = (len == 1) ? SPINOR_OP_BP : SPINOR_OP_AAI_WP;
174+
int ret;
175+
176+
nor->program_opcode = op;
177+
ret = spi_nor_write_data(nor, to, 1, buf);
178+
if (ret < 0)
179+
return ret;
180+
WARN(ret != len, "While writing %zu byte written %i bytes\n", len, ret);
181+
182+
return spi_nor_wait_till_ready(nor);
183+
}
184+
170185
static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
171186
size_t *retlen, const u_char *buf)
172187
{
@@ -188,33 +203,22 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
188203

189204
/* Start write from odd address. */
190205
if (to % 2) {
191-
nor->program_opcode = SPINOR_OP_BP;
192-
193206
/* write one byte. */
194-
ret = spi_nor_write_data(nor, to, 1, buf);
207+
ret = sst_nor_write_data(nor, to, 1, buf);
195208
if (ret < 0)
196209
goto out;
197-
WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
198-
ret = spi_nor_wait_till_ready(nor);
199-
if (ret)
200-
goto out;
201210

202211
to++;
203212
actual++;
204213
}
205214

206215
/* Write out most of the data here. */
207216
for (; actual < len - 1; actual += 2) {
208-
nor->program_opcode = SPINOR_OP_AAI_WP;
209-
210217
/* write two bytes. */
211-
ret = spi_nor_write_data(nor, to, 2, buf + actual);
218+
ret = sst_nor_write_data(nor, to, 2, buf + actual);
212219
if (ret < 0)
213220
goto out;
214-
WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
215-
ret = spi_nor_wait_till_ready(nor);
216-
if (ret)
217-
goto out;
221+
218222
to += 2;
219223
nor->sst_write_second = true;
220224
}
@@ -234,14 +238,9 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
234238
if (ret)
235239
goto out;
236240

237-
nor->program_opcode = SPINOR_OP_BP;
238-
ret = spi_nor_write_data(nor, to, 1, buf + actual);
241+
ret = sst_nor_write_data(nor, to, 1, buf + actual);
239242
if (ret < 0)
240243
goto out;
241-
WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
242-
ret = spi_nor_wait_till_ready(nor);
243-
if (ret)
244-
goto out;
245244

246245
actual += 1;
247246

0 commit comments

Comments
 (0)