mtd: nand: raw: rockchip_nfc: fix ecc setup
Subsystems (3)
PATCH v2 1/1 mtd: nand: raw: rockchip_nfc: fix ecc setup
The Rockchip boot ROM only checks for NAND chip 0 and with reduced ECC strength. Currently only the read page functions have this condition check added.
Fix by adding the same condition to all read and write page functions by dropping the existing 'selected_bank == 0' check and use the NAND_IS_BOOT_MEDIUM option that was introduced to U-Boot more recently than this driver to behave identically to the Linux driver.
It is now the users responsibility to apply the device tree property "nand-is-boot-medium" to only NAND chip 0.
---
Changed V2:
fix typos
reword
--- drivers/mtd/nand/raw/rockchip_nfc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/mtd/nand/raw/rockchip_nfc.c +8 -6
@@ -440,7 +440,8 @@ static int rk_nfc_write_page_raw(struct mtd_info *mtd,int i, pages_per_blk; pages_per_blk = mtd->erasesize / mtd->writesize; -if ((page < (pages_per_blk * rknand->boot_blks)) && +if ((chip->options &NAND_IS_BOOT_MEDIUM ) && + (page < (pages_per_blk * rknand->boot_blks)) && rknand->boot_ecc != ecc->strength) {/* * There's currently no method to notify the MTD framework that
@@ -560,7 +561,8 @@ static int rk_nfc_write_page_hwecc(struct mtd_info *mtd,* * Configure the ECC algorithm supported by the boot ROM. */ -if (page < (pages_per_blk * rknand->boot_blks)) { +if ((chip->options &NAND_IS_BOOT_MEDIUM ) && + (page < (pages_per_blk * rknand->boot_blks))) { boot_rom_mode =1 ;if (rknand->boot_ecc != ecc->strength)rk_nfc_hw_ecc_setup (chip, rknand->boot_ecc);
@@ -624,8 +626,8 @@ static int rk_nfc_read_page_raw(struct mtd_info *mtd,int i, pages_per_blk; pages_per_blk = mtd->erasesize / mtd->writesize; -if ((page < (pages_per_blk * rknand->boot_blks)) && - nfc->selected_bank ==0 && +if ((chip->options &NAND_IS_BOOT_MEDIUM ) && + (page < (pages_per_blk * rknand->boot_blks)) && rknand->boot_ecc != ecc->strength) {/* * There's currently no method to notify the MTD framework that
@@ -700,8 +702,8 @@ static int rk_nfc_read_page_hwecc(struct mtd_info *mtd,* are used by the boot ROM. * Configure the ECC algorithm supported by the boot ROM. */ -if (page < (pages_per_blk * rknand->boot_blks) && - nfc->selected_bank ==0 ) { +if ((chip->options &NAND_IS_BOOT_MEDIUM ) && + (page < (pages_per_blk * rknand->boot_blks))) { boot_rom_mode =1 ;if (rknand->boot_ecc != ecc->strength)rk_nfc_hw_ecc_setup (chip, rknand->boot_ecc);
Signature · 1 line
2.39.5
Hi Johan,
Resending because the ML rejected my mail sent from my other address... I have to figure out what I set up wrong to trigger the spam filter :)
Quoted 10 lines · Johan Jonker
The Rockchip boot ROM only checks for NAND chip 0 and with reduced ECC strength. Currently only the read page functions have this condition check added.
Fix by adding the same condition to all read and write page functions by dropping the existing '
selected_bank== 0' check and use theNAND_IS_BOOT_MEDIUMoption that was introduced to U-Boot more recently than this driver to behave identically to the Linux driver.
Quoted 7 lines
It is now the users responsibility to apply the device tree property "nand-is-boot-medium" to only NAND chip 0.
Fixes b12dc5d6fa76 ("mtd: nand: NFC drivers for RK3308, RK2928 and others")Signed-off-by Johan Jonker <[email protected]>Tested-by Hüseyin BIYIK <[email protected]>Reviewed-by Simon Glass <[email protected]>
You don't explain how the bug can be triggered. It'd be nice to provide the usecase when this is an issue so that other people looking on the Internet for bug reports could somehow stumble upon this patch.
I'm thinking the issue is that we currently verify all NAND chips use the boot_blks and boot_ecc from the boot medium whereas they might not be used as a boot medium (they are missing the nand-is-boot-medium property) so we cannot actually make use of them. Is that correct?
Considering boot_blks is 0 if rockchip,boot-blks property isn't set, we'll never be able to meet the page < pages_per_blk * 0) condition anyway so we would never enter the if block... or can page actually be negative????
To be clear, I don't disagree with the fix, I just am missing a lot of information that should be in the commit log.
Cheers,
Quentin
Changed References:
Added Linux MTD maintainers. Question below.
Hi,
Quoted 20 lines · Boogie
Hello Quentin
I had reported this bug to Johan so i can give detailed explanation.
The bug was really the lock of "&&
nfc->selected_bank== 0" not existing for write_page_* variants.When rockchip nfc was reading, it required bootblks to be a part of the first nand chip only with the condition "
nfc->selected_bank== 0". But when writing it interpreted 'all' nand chips firstboot_blksas boot blocks.The practical problem. I had mentioned this in V1 and give more explicit details here.
I have 2 nand chips, and a partition (linux below) starts in nand chip 1 (CS=0 in below) and ends in chip2 (CS=2 below). And i am using UBI on top of that mtd.
nand@0 {
reg = <0>, <2>;
label = "rk-nand-0";
nand-bus-width = <8>;
nand-ecc-mode = "hw";
nand-ecc-step-size = <1024>;
nand-ecc-strength = <40>;
Quoted 1 line
nand-is-boot-medium;
This property was introduced during review, but can't find the reason.
https://lore.kernel.org/linux-rockchip/[email protected]/
Quoted 2 lines
rockchip,boot-blks = <8>;
rockchip,boot-ecc-strength = <24>;
Describing 2 nands with 1 node also exposes properties to a second nand that result in reduced ecc strength in both.
Quoted 4 lines
// block 14 - end
linux@1C00000 {
The binding puts the partitions under 1 nand node, but says nothing about a partition across 2 or more nands. Could the MTD maintainers inform us the support status of this 'feature' in Linux and U-Boot.
Quoted 4 lines
label = "linux";
reg = <0x00x1C000000x30xFE400000>;
};
};
I would like to produce a patch with "&& nfc->selected_bank == 0" for Linux. Not sure if they are willing to merge.
Depending on the output/feedback put this patch on hold till I know more.
Johan
Quoted 69 lines
When i create the UBI volume the middle of this partition where the blocks are at 2nd chip block0-7, will be written in boot block strength [ECC:1024/24] but will be read with normal strength [ECC:1024/40].
This causes first UBI creation to be successful, and next scan to fail due to written strength is different that read.
You dont actually need to use UBI at all, any time you write those sectors, you wont be able to read them. A workaround it to mark them bad, but this is not nice, since the blocks are completely fine.
Rockchip NFC technically as minimum needs to know rockchip,boot-blks, & rockchip,boot-ecc-strength props only. Currently the checking condition is rockchip,boot-blks & applied affect is rockchip,boot-ecc-strength.
The bug is this is only applicable to first chip not all chips.
NAND_IS_BOOT_MEDIUMis actually coming from mainline linux. Similar bootrom tricks are also available in other socs. And detection of boot rom blocks is not always straight forward as rockchip's rockchip,boot-blks, they have to do some "if" case acrobatics to detect those. For simplicity reasons mainline linux introduced a global flagNAND_IS_BOOT_MEDIUMand it is applied to other socs as well.Since this "&&
nfc->selected_bank== 0" fix is exactly at the same line with mainline changes ofNAND_IS_BOOT_MEDIUMcheck, i think Johan also integrated both at the same line.In u-boot only mk808 is using nfc with boot blocks and it is already marking the nand device as boot medium, so the code change should not break existing devices.
Additional note: Linux mainline also is lacking the "
nfc->selected_bank== 0" check, so i think similar patch should got to linux as well.@johan if anything i am missing feel free to correct me.
hüseyin
Quoted 44 lines · Quentin Schulz via U-Boot
Hi Johan,
Resending because the ML rejected my mail sent from my other address... I have to figure out what I set up wrong to trigger the spam filter :)
On 7/14/26 8:39 AM, Johan Jonker wrote:
> The Rockchip boot ROM only checks for NAND chip 0 and with > reduced ECC strength. Currently only the read page functions > have this condition check added.
>
> Fix by adding the same condition to all read and write page > functions by dropping the existing 'selected_bank== 0' check > and use theNAND_IS_BOOT_MEDIUMoption that was introduced to > U-Boot more recently than this driver to behave
> identically to the Linux driver.
>
> It is now the users responsibility to apply the device tree > property "nand-is-boot-medium" to only NAND chip 0.
>
> Fixes:b12dc5d6fa76("mtd: nand: NFC drivers for RK3308, RK2928 and others")
> Signed-off-by: Johan Jonker <[email protected]>
> Tested-by: Hüseyin BIYIK <[email protected]>
> Reviewed-by: Simon Glass <[email protected]>You don't explain how the bug can be triggered. It'd be nice to provide the usecase when this is an issue so that other people looking on the Internet for bug reports could somehow stumble upon this patch.
I'm thinking the issue is that we currently verify all NAND chips use the
boot_blksandboot_eccfrom the boot medium whereas they might not be used as a boot medium (they are missing the nand-is-boot-medium property) so we cannot actually make use of them. Is that correct?Considering
boot_blksis 0 if rockchip,boot-blks property isn't set, we'll never be able to meet the page <pages_per_blk* 0) condition anyway so we would never enter the if block... or can page actually be negative????To be clear, I don't disagree with the fix, I just am missing a lot of information that should be in the commit log.
Cheers,
Quentin
Hi Johan
Quoted 51 lines · Johan Jonker
Changed References:
Added Linux MTD maintainers. Question below.Hi,
Quoted 20 lines · Boogie
Hello Quentin
I had reported this bug to Johan so i can give detailed explanation.
The bug was really the lock of "&&
nfc->selected_bank== 0" not existing for write_page_* variants.When rockchip nfc was reading, it required bootblks to be a part of the first nand chip only with the condition "
nfc->selected_bank== 0". But when writing it interpreted 'all' nand chips firstboot_blksas boot blocks.The practical problem. I had mentioned this in V1 and give more explicit details here.
I have 2 nand chips, and a partition (linux below) starts in nand chip 1 (CS=0 in below) and ends in chip2 (CS=2 below). And i am using UBI on top of that mtd.
nand@0 {
reg = <0>, <2>;
label = "rk-nand-0";
nand-bus-width = <8>;
nand-ecc-mode = "hw";
nand-ecc-step-size = <1024>;
nand-ecc-strength = <40>;Quoted 1 line
nand-is-boot-medium;
This property was introduced during review, but can't find the reason.
https://lore.kernel.org/linux-rockchip/[email protected]/Quoted 2 lines
rockchip,boot-blks = <8>;
rockchip,boot-ecc-strength = <24>;Describing 2 nands with 1 node also exposes properties to a second nand that result in reduced ecc strength in both.
Quoted 4 lines
// block 14 - end
linux@1C00000 {The binding puts the partitions under 1 nand node, but says nothing about a partition across 2 or more nands. Could the MTD maintainers inform us the support status of this 'feature' in Linux and U-Boot.
Quoted 4 lines
label = "linux";
reg = <0x00x1C000000x30xFE400000>;
};
};
Quoted 4 lines
I would like to produce a patch with "&&
nfc->selected_bank== 0" for Linux. Not sure if they are willing to merge.Depending on the output/feedback put this patch on hold till I know more.
This current version in mainline linux (and your patch) also works, may be it is better to leave it so (at least for me), because adding "&& nfc->selected_bank == 0" will break existing devices, which is not always worth it specially for legacy devices like ones using this driver.
Quoted 73 lines
Johan
Quoted 69 lines
When i create the UBI volume the middle of this partition where the blocks are at 2nd chip block0-7, will be written in boot block strength [ECC:1024/24] but will be read with normal strength [ECC:1024/40].
This causes first UBI creation to be successful, and next scan to fail due to written strength is different that read.
You dont actually need to use UBI at all, any time you write those sectors, you wont be able to read them. A workaround it to mark them bad, but this is not nice, since the blocks are completely fine.
Rockchip NFC technically as minimum needs to know rockchip,boot-blks, & rockchip,boot-ecc-strength props only. Currently the checking condition is rockchip,boot-blks & applied affect is rockchip,boot-ecc-strength.
The bug is this is only applicable to first chip not all chips.
NAND_IS_BOOT_MEDIUMis actually coming from mainline linux. Similar bootrom tricks are also available in other socs. And detection of boot rom blocks is not always straight forward as rockchip's rockchip,boot-blks, they have to do some "if" case acrobatics to detect those. For simplicity reasons mainline linux introduced a global flagNAND_IS_BOOT_MEDIUMand it is applied to other socs as well.Since this "&&
nfc->selected_bank== 0" fix is exactly at the same line with mainline changes ofNAND_IS_BOOT_MEDIUMcheck, i think Johan also integrated both at the same line.In u-boot only mk808 is using nfc with boot blocks and it is already marking the nand device as boot medium, so the code change should not break existing devices.
Additional note: Linux mainline also is lacking the "
nfc->selected_bank== 0" check, so i think similar patch should got to linux as well.@johan if anything i am missing feel free to correct me.
hüseyin
Quoted 44 lines · Quentin Schulz via U-Boot
Hi Johan,
Resending because the ML rejected my mail sent from my other address... I have to figure out what I set up wrong to trigger the spam filter :)
On 7/14/26 8:39 AM, Johan Jonker wrote:
> The Rockchip boot ROM only checks for NAND chip 0 and with > reduced ECC strength. Currently only the read page functions > have this condition check added.
>
> Fix by adding the same condition to all read and write page > functions by dropping the existing 'selected_bank== 0' check > and use theNAND_IS_BOOT_MEDIUMoption that was introduced to > U-Boot more recently than this driver to behave
> identically to the Linux driver.
>
> It is now the users responsibility to apply the device tree > property "nand-is-boot-medium" to only NAND chip 0.
>
> Fixes:b12dc5d6fa76("mtd: nand: NFC drivers for RK3308, RK2928 and
others")
> Signed-off-by: Johan Jonker <[email protected]>
> Tested-by: Hüseyin BIYIK <[email protected]>
> Reviewed-by: Simon Glass <[email protected]>You don't explain how the bug can be triggered. It'd be nice to provide the usecase when this is an issue so that other people looking on the Internet for bug reports could somehow stumble upon this patch.
I'm thinking the issue is that we currently verify all NAND chips use the
boot_blksandboot_eccfrom the boot medium whereas they might not be used as a boot medium (they are missing the nand-is-boot-medium property) so we cannot actually make use of them. Is that correct?Considering
boot_blksis 0 if rockchip,boot-blks property isn't set, we'll never be able to meet the page <pages_per_blk* 0) condition anyway so we would never enter the if block... or can page actually be negative????To be clear, I don't disagree with the fix, I just am missing a lot of information that should be in the commit log.
Cheers,
Quentin
Quoted 31 lines · Johan Jonker
Changed References:
Added Linux MTD maintainers. Question below.Hi,
Quoted 25 lines · Boogie
Hello Quentin
I had reported this bug to Johan so i can give detailed explanation.
The bug was really the lock of "&&
nfc->selected_bank== 0" not existing for write_page_* variants.When rockchip nfc was reading, it required bootblks to be a part of the first nand chip only with the condition "
nfc->selected_bank== 0". But when writing it interpreted 'all' nand chips firstboot_blksas boot blocks.The practical problem. I had mentioned this in V1 and give more explicit details here.
I have 2 nand chips, and a partition (linux below) starts in nand chip 1 (CS=0 in below) and ends in chip2 (CS=2 below). And i am using UBI on top of that mtd.
nand@0 {
reg = <0>, <2>;
label = "rk-nand-0";
nand-bus-width = <8>;
nand-ecc-mode = "hw";
nand-ecc-step-size = <1024>;
nand-ecc-strength = <40>;nand-is-boot-medium;
What you may want is to declare the NAND using two different nodes, then use mtd-virt-concat to generate one bigger MTD device spanning across the two devices.
The driver should not blindly check the 'cs ==0' condition but instead also take nand-is-boot-medium into account (which would then only be required in the NAND chip with the said boot partition) to make sure the special handling is only applied on the boot partition.
Hope that helps.
Thanks,
Miquèl
Hello Miquel
Quoted 32 lines · Miquel Raynal
Quoted 30 lines · Johan Jonker
Changed References:
Added Linux MTD maintainers. Question below.Hi,
Quoted 23 lines · Boogie
Hello Quentin
I had reported this bug to Johan so i can give detailed explanation.
The bug was really the lock of "&&
nfc->selected_bank== 0" not existing for write_page_* variants.When rockchip nfc was reading, it required bootblks to be a part of the first nand chip only with the condition "
nfc->selected_bank== 0". But when writing it interpreted 'all' nand chips firstboot_blksas boot blocks.The practical problem. I had mentioned this in V1 and give more explicit details here.
I have 2 nand chips, and a partition (linux below) starts in nand chip 1 (CS=0 in below) and ends in chip2 (CS=2 below). And i am using UBI on top of that mtd.
nand@0 {
reg = <0>, <2>;
label = "rk-nand-0";
nand-bus-width = <8>;
nand-ecc-mode = "hw";
nand-ecc-step-size = <1024>;
nand-ecc-strength = <40>;
Quoted 5 lines
Quoted 1 line
Quoted 1 line
nand-is-boot-medium;
What you may want is to declare the NAND using two different nodes, then use mtd-virt-concat to generate one bigger MTD device spanning across the two devices.
Thats also an option but i prefer this way to be more straight forward since entire partition layout can be kept in dts.
Quoted 2 lines
The driver should not blindly check the 'cs ==0' condition but instead
I think my statement was also not accurate, i think the brom at least for 3066 should check only the first chip which has the least cs so nfc->selected_bank == 0. This could have been validated easily by erasing boot blocks in both chips and writing an idb to 2nd chip deliberately and check if the 3066 would boot or not. But i think i burned gpios in my board while messing with the shorting nand chips, 2nd chip is RIP. so i cant test this anymore unfortunately. In any case current working way of rockchip_nfc in mainline linux is also fine. If somehow those low eccs create problem, they can be marked as bad anyways.
But thanks for your input.
Quoted 8 lines
also take nand-is-boot-medium into account (which would then only be required in the NAND chip with the said boot partition) to make sure the special handling is only applied on the boot partition.
Hope that helps.
Thanks,
Miquèl