Skip to content

Commit f156b23

Browse files
Pratyush Yadavprati0100
authored andcommitted
mtd: spi-nor: core: avoid odd length/address reads on 8D-8D-8D mode
On Octal DTR capable flashes like Micron Xcella reads cannot start or end at an odd address in Octal DTR mode. Extra bytes need to be read at the start or end to make sure both the start address and length remain even. To avoid allocating too much extra memory, thereby putting unnecessary memory pressure on the system, the temporary buffer containing the extra padding bytes is capped at PAGE_SIZE bytes. The rest of the 2-byte aligned part should be read directly in the main buffer. 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-1-ziniu.wang_1@nxp.com
1 parent 8f5ae30 commit f156b23

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

drivers/mtd/spi-nor/core.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,76 @@ static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
20142014
return info;
20152015
}
20162016

2017+
/*
2018+
* On Octal DTR capable flashes, reads cannot start or end at an odd
2019+
* address in Octal DTR mode. Extra bytes need to be read at the start
2020+
* or end to make sure both the start address and length remain even.
2021+
*/
2022+
static int spi_nor_octal_dtr_read(struct spi_nor *nor, loff_t from, size_t len,
2023+
u_char *buf)
2024+
{
2025+
u_char *tmp_buf;
2026+
size_t tmp_len;
2027+
loff_t start, end;
2028+
int ret, bytes_read;
2029+
2030+
if (IS_ALIGNED(from, 2) && IS_ALIGNED(len, 2))
2031+
return spi_nor_read_data(nor, from, len, buf);
2032+
else if (IS_ALIGNED(from, 2) && len > PAGE_SIZE)
2033+
return spi_nor_read_data(nor, from, round_down(len, PAGE_SIZE),
2034+
buf);
2035+
2036+
tmp_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2037+
if (!tmp_buf)
2038+
return -ENOMEM;
2039+
2040+
start = round_down(from, 2);
2041+
end = round_up(from + len, 2);
2042+
2043+
/*
2044+
* Avoid allocating too much memory. The requested read length might be
2045+
* quite large. Allocating a buffer just as large (slightly bigger, in
2046+
* fact) would put unnecessary memory pressure on the system.
2047+
*
2048+
* For example if the read is from 3 to 1M, then this will read from 2
2049+
* to 4098. The reads from 4098 to 1M will then not need a temporary
2050+
* buffer so they can proceed as normal.
2051+
*/
2052+
tmp_len = min_t(size_t, end - start, PAGE_SIZE);
2053+
2054+
ret = spi_nor_read_data(nor, start, tmp_len, tmp_buf);
2055+
if (ret == 0) {
2056+
ret = -EIO;
2057+
goto out;
2058+
}
2059+
if (ret < 0)
2060+
goto out;
2061+
2062+
/*
2063+
* More bytes are read than actually requested, but that number can't be
2064+
* reported to the calling function or it will confuse its calculations.
2065+
* Calculate how many of the _requested_ bytes were read.
2066+
*/
2067+
bytes_read = ret;
2068+
2069+
if (from != start)
2070+
ret -= from - start;
2071+
2072+
/*
2073+
* Only account for extra bytes at the end if they were actually read.
2074+
* For example, if the total length was truncated because of temporary
2075+
* buffer size limit then the adjustment for the extra bytes at the end
2076+
* is not needed.
2077+
*/
2078+
if (start + bytes_read == end)
2079+
ret -= end - (from + len);
2080+
2081+
memcpy(buf, tmp_buf + (from - start), ret);
2082+
out:
2083+
kfree(tmp_buf);
2084+
return ret;
2085+
}
2086+
20172087
static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
20182088
size_t *retlen, u_char *buf)
20192089
{
@@ -2031,7 +2101,11 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
20312101
while (len) {
20322102
loff_t addr = from;
20332103

2034-
ret = spi_nor_read_data(nor, addr, len, buf);
2104+
if (nor->read_proto == SNOR_PROTO_8_8_8_DTR)
2105+
ret = spi_nor_octal_dtr_read(nor, addr, len, buf);
2106+
else
2107+
ret = spi_nor_read_data(nor, addr, len, buf);
2108+
20352109
if (ret == 0) {
20362110
/* We shouldn't see 0-length reads */
20372111
ret = -EIO;

0 commit comments

Comments
 (0)