Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

memory: renesas-rpc-if: Avoid unaligned bus access for HyperFlash

HyperFlash devices in Renesas SoCs use 2-bytes addressing, according
to HW manual paragraph 62.3.3 (which officially describes Serial Flash
access, but seems to be applicable to HyperFlash too). And 1-byte bus
read operations to 2-bytes unaligned addresses in external address space
read mode work incorrectly (returns the other byte from the same word).

Function memcpy_fromio(), used by the driver to read data from the bus,
in ARM64 architecture (to which Renesas cores belong) uses 8-bytes
bus accesses for appropriate aligned addresses, and 1-bytes accesses
for other addresses. This results in incorrect data read from HyperFlash
in unaligned cases.

This issue can be reproduced using something like the following commands
(where mtd1 is a parition on Hyperflash storage, defined properly
in a device tree):

[Correct fragment, read from Hyperflash]

root@rcar-gen3:~# dd if=/dev/mtd1 of=/tmp/zz bs=32 count=1
root@rcar-gen3:~# hexdump -C /tmp/zz
00000000 f4 03 00 aa f5 03 01 aa f6 03 02 aa f7 03 03 aa |................|
00000010 00 00 80 d2 40 20 18 d5 00 06 81 d2 a0 18 a6 f2 |....@ ..........|
00000020

[Incorrect read of the same fragment: see the difference at offsets 8-11]

root@rcar-gen3:~# dd if=/dev/mtd1 of=/tmp/zz bs=12 count=1
root@rcar-gen3:~# hexdump -C /tmp/zz
00000000 f4 03 00 aa f5 03 01 aa 03 03 aa aa |............|
0000000c

Fix this issue by creating a local replacement of the copying function,
that performs only properly aligned bus accesses, and is used for reading
from HyperFlash.

Fixes: ca7d8b980b67f ("memory: add Renesas RPC-IF driver")
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Gabbasov <andrew_gabbasov@mentor.com>
Link: https://lore.kernel.org/r/20210922184830.29147-1-andrew_gabbasov@mentor.com
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>

authored by

Andrew Gabbasov and committed by
Krzysztof Kozlowski
1869023e fff53a55

+46 -2
+46 -2
drivers/memory/renesas-rpc-if.c
··· 185 185 186 186 *val = readl(rpc->base + reg); 187 187 return 0; 188 - 189 188 } 190 189 191 190 static int rpcif_reg_write(void *context, unsigned int reg, unsigned int val) ··· 544 545 } 545 546 EXPORT_SYMBOL(rpcif_manual_xfer); 546 547 548 + static void memcpy_fromio_readw(void *to, 549 + const void __iomem *from, 550 + size_t count) 551 + { 552 + const int maxw = (IS_ENABLED(CONFIG_64BIT)) ? 8 : 4; 553 + u8 buf[2]; 554 + 555 + if (count && ((unsigned long)from & 1)) { 556 + *(u16 *)buf = __raw_readw((void __iomem *)((unsigned long)from & ~1)); 557 + *(u8 *)to = buf[1]; 558 + from++; 559 + to++; 560 + count--; 561 + } 562 + while (count >= 2 && !IS_ALIGNED((unsigned long)from, maxw)) { 563 + *(u16 *)to = __raw_readw(from); 564 + from += 2; 565 + to += 2; 566 + count -= 2; 567 + } 568 + while (count >= maxw) { 569 + #ifdef CONFIG_64BIT 570 + *(u64 *)to = __raw_readq(from); 571 + #else 572 + *(u32 *)to = __raw_readl(from); 573 + #endif 574 + from += maxw; 575 + to += maxw; 576 + count -= maxw; 577 + } 578 + while (count >= 2) { 579 + *(u16 *)to = __raw_readw(from); 580 + from += 2; 581 + to += 2; 582 + count -= 2; 583 + } 584 + if (count) { 585 + *(u16 *)buf = __raw_readw(from); 586 + *(u8 *)to = buf[0]; 587 + } 588 + } 589 + 547 590 ssize_t rpcif_dirmap_read(struct rpcif *rpc, u64 offs, size_t len, void *buf) 548 591 { 549 592 loff_t from = offs & (RPCIF_DIRMAP_SIZE - 1); ··· 607 566 regmap_write(rpc->regmap, RPCIF_DRDMCR, rpc->dummy); 608 567 regmap_write(rpc->regmap, RPCIF_DRDRENR, rpc->ddr); 609 568 610 - memcpy_fromio(buf, rpc->dirmap + from, len); 569 + if (rpc->bus_size == 2) 570 + memcpy_fromio_readw(buf, rpc->dirmap + from, len); 571 + else 572 + memcpy_fromio(buf, rpc->dirmap + from, len); 611 573 612 574 pm_runtime_put(rpc->dev); 613 575