Skip to content

Commit b7cd63d

Browse files
Bence Csókásgregkh
authored andcommitted
mtd: spi-nor: sst: Factor out common write operation to sst_nor_write_data()
commit 18bcb4a upstream. 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 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0000a77 commit b7cd63d

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

drivers/mtd/spi-nor/sst.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ static const struct flash_info sst_nor_parts[] = {
123123
.fixups = &sst26vf_nor_fixups },
124124
};
125125

126+
static int sst_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
127+
const u_char *buf)
128+
{
129+
u8 op = (len == 1) ? SPINOR_OP_BP : SPINOR_OP_AAI_WP;
130+
int ret;
131+
132+
nor->program_opcode = op;
133+
ret = spi_nor_write_data(nor, to, 1, buf);
134+
if (ret < 0)
135+
return ret;
136+
WARN(ret != len, "While writing %zu byte written %i bytes\n", len, ret);
137+
138+
return spi_nor_wait_till_ready(nor);
139+
}
140+
126141
static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
127142
size_t *retlen, const u_char *buf)
128143
{
@@ -144,33 +159,22 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
144159

145160
/* Start write from odd address. */
146161
if (to % 2) {
147-
nor->program_opcode = SPINOR_OP_BP;
148-
149162
/* write one byte. */
150-
ret = spi_nor_write_data(nor, to, 1, buf);
163+
ret = sst_nor_write_data(nor, to, 1, buf);
151164
if (ret < 0)
152165
goto out;
153-
WARN(ret != 1, "While writing 1 byte written %i bytes\n", ret);
154-
ret = spi_nor_wait_till_ready(nor);
155-
if (ret)
156-
goto out;
157166

158167
to++;
159168
actual++;
160169
}
161170

162171
/* Write out most of the data here. */
163172
for (; actual < len - 1; actual += 2) {
164-
nor->program_opcode = SPINOR_OP_AAI_WP;
165-
166173
/* write two bytes. */
167-
ret = spi_nor_write_data(nor, to, 2, buf + actual);
174+
ret = sst_nor_write_data(nor, to, 2, buf + actual);
168175
if (ret < 0)
169176
goto out;
170-
WARN(ret != 2, "While writing 2 bytes written %i bytes\n", ret);
171-
ret = spi_nor_wait_till_ready(nor);
172-
if (ret)
173-
goto out;
177+
174178
to += 2;
175179
nor->sst_write_second = true;
176180
}
@@ -190,14 +194,9 @@ static int sst_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
190194
if (ret)
191195
goto out;
192196

193-
nor->program_opcode = SPINOR_OP_BP;
194-
ret = spi_nor_write_data(nor, to, 1, buf + actual);
197+
ret = sst_nor_write_data(nor, to, 1, buf + actual);
195198
if (ret < 0)
196199
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;
201200

202201
actual += 1;
203202

0 commit comments

Comments
 (0)