Skip to content

Commit 17926cd

Browse files
Pratyush Yadavprati0100
authored andcommitted
mtd: spi-nor: core: avoid odd length/address writes in 8D-8D-8D mode
On Octal DTR capable flashes like Micron Xcella the writes cannot start or end at an odd address in Octal DTR mode. Extra 0xff bytes need to be appended or prepended to make sure the start address and end address are even. 0xff is used because on NOR flashes a program operation can only flip bits from 1 to 0, not the other way round. 0 to 1 flip needs to happen via erases. Signed-off-by: Pratyush Yadav <p.yadav@ti.com> Reviewed-by: Michael Walle <michael@walle.cc> Signed-off-by: Luke Wang <ziniu.wang_1@nxp.com> Signed-off-by: Pratyush Yadav <pratyush@kernel.org> Link: https://lore.kernel.org/r/20250708091646.292-2-ziniu.wang_1@nxp.com
1 parent f156b23 commit 17926cd

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

drivers/mtd/spi-nor/core.c

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,68 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
21282128
return ret;
21292129
}
21302130

2131+
/*
2132+
* On Octal DTR capable flashes, writes cannot start or end at an odd address
2133+
* in Octal DTR mode. Extra 0xff bytes need to be appended or prepended to
2134+
* make sure the start address and end address are even. 0xff is used because
2135+
* on NOR flashes a program operation can only flip bits from 1 to 0, not the
2136+
* other way round. 0 to 1 flip needs to happen via erases.
2137+
*/
2138+
static int spi_nor_octal_dtr_write(struct spi_nor *nor, loff_t to, size_t len,
2139+
const u8 *buf)
2140+
{
2141+
u8 *tmp_buf;
2142+
size_t bytes_written;
2143+
loff_t start, end;
2144+
int ret;
2145+
2146+
if (IS_ALIGNED(to, 2) && IS_ALIGNED(len, 2))
2147+
return spi_nor_write_data(nor, to, len, buf);
2148+
2149+
tmp_buf = kmalloc(nor->params->page_size, GFP_KERNEL);
2150+
if (!tmp_buf)
2151+
return -ENOMEM;
2152+
2153+
memset(tmp_buf, 0xff, nor->params->page_size);
2154+
2155+
start = round_down(to, 2);
2156+
end = round_up(to + len, 2);
2157+
2158+
memcpy(tmp_buf + (to - start), buf, len);
2159+
2160+
ret = spi_nor_write_data(nor, start, end - start, tmp_buf);
2161+
if (ret == 0) {
2162+
ret = -EIO;
2163+
goto out;
2164+
}
2165+
if (ret < 0)
2166+
goto out;
2167+
2168+
/*
2169+
* More bytes are written than actually requested, but that number can't
2170+
* be reported to the calling function or it will confuse its
2171+
* calculations. Calculate how many of the _requested_ bytes were
2172+
* written.
2173+
*/
2174+
bytes_written = ret;
2175+
2176+
if (to != start)
2177+
ret -= to - start;
2178+
2179+
/*
2180+
* Only account for extra bytes at the end if they were actually
2181+
* written. For example, if for some reason the controller could only
2182+
* complete a partial write then the adjustment for the extra bytes at
2183+
* the end is not needed.
2184+
*/
2185+
if (start + bytes_written == end)
2186+
ret -= end - (to + len);
2187+
2188+
out:
2189+
kfree(tmp_buf);
2190+
return ret;
2191+
}
2192+
21312193
/*
21322194
* Write an address range to the nor chip. Data must be written in
21332195
* FLASH_PAGESIZE chunks. The address range may be any size provided
@@ -2164,7 +2226,12 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
21642226
goto write_err;
21652227
}
21662228

2167-
ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
2229+
if (nor->write_proto == SNOR_PROTO_8_8_8_DTR)
2230+
ret = spi_nor_octal_dtr_write(nor, addr, page_remain,
2231+
buf + i);
2232+
else
2233+
ret = spi_nor_write_data(nor, addr, page_remain,
2234+
buf + i);
21682235
spi_nor_unlock_device(nor);
21692236
if (ret < 0)
21702237
goto write_err;

0 commit comments

Comments
 (0)